1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.pnm;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.apache.commons.imaging.ImageFormat;
23 import org.apache.commons.imaging.ImageFormats;
24 import org.apache.commons.imaging.ImageInfo;
25 import org.apache.commons.imaging.ImagingException;
26
27 final class PpmFileInfo extends AbstractFileInfo {
28 private final int max;
29 private final float scale;
30 private final int bytesPerSample;
31
32 PpmFileInfo(final int width, final int height, final boolean rawBits, final int max) throws ImagingException {
33 super(width, height, rawBits);
34 if (max <= 0) {
35 throw new ImagingException("PPM maxVal " + max + " is out of range [1;65535]");
36 }
37 if (max <= 255) {
38 scale = 255f;
39 bytesPerSample = 1;
40 } else if (max <= 65535) {
41 scale = 65535f;
42 bytesPerSample = 2;
43 } else {
44 throw new ImagingException("PPM maxVal " + max + " is out of range [1;65535]");
45 }
46 this.max = max;
47 }
48
49 @Override
50 public int getBitDepth() {
51 return max;
52 }
53
54 @Override
55 public ImageInfo.ColorType getColorType() {
56 return ImageInfo.ColorType.RGB;
57 }
58
59 @Override
60 public ImageFormat getImageType() {
61 return ImageFormats.PPM;
62 }
63
64 @Override
65 public String getImageTypeDescription() {
66 return "PPM: portable pixmap file format";
67 }
68
69 @Override
70 public String getMimeType() {
71 return "image/x-portable-pixmap";
72 }
73
74 @Override
75 public int getNumComponents() {
76 return 3;
77 }
78
79 @Override
80 public int getRgb(final InputStream is) throws IOException {
81 int red = readSample(is, bytesPerSample);
82 int green = readSample(is, bytesPerSample);
83 int blue = readSample(is, bytesPerSample);
84
85 red = scaleSample(red, scale, max);
86 green = scaleSample(green, scale, max);
87 blue = scaleSample(blue, scale, max);
88 final int alpha = 0xff;
89
90 return (0xff & alpha) << 24 | (0xff & red) << 16 | (0xff & green) << 8 | (0xff & blue) << 0;
91 }
92
93 @Override
94 public int getRgb(final WhiteSpaceReader wsr) throws IOException {
95 int red = Integer.parseInt(wsr.readtoWhiteSpace());
96 int green = Integer.parseInt(wsr.readtoWhiteSpace());
97 int blue = Integer.parseInt(wsr.readtoWhiteSpace());
98
99 red = scaleSample(red, scale, max);
100 green = scaleSample(green, scale, max);
101 blue = scaleSample(blue, scale, max);
102 final int alpha = 0xff;
103
104 return (0xff & alpha) << 24 | (0xff & red) << 16 | (0xff & green) << 8 | (0xff & blue) << 0;
105 }
106
107 @Override
108 public boolean hasAlpha() {
109 return false;
110 }
111 }