You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by da...@apache.org on 2012/09/29 08:40:40 UTC
svn commit: r1391758 - in
/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm:
FileInfo.java PgmFileInfo.java PnmImageParser.java PpmFileInfo.java
Author: damjan
Date: Sat Sep 29 06:40:39 2012
New Revision: 1391758
URL: http://svn.apache.org/viewvc?rev=1391758&view=rev
Log:
Support the PNM maxVal field properly.
Clean up and fix various warnings.
Modified:
commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/FileInfo.java
commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PgmFileInfo.java
commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PpmFileInfo.java
Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/FileInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/FileInfo.java?rev=1391758&r1=1391757&r2=1391758&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/FileInfo.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/FileInfo.java Sat Sep 29 06:40:39 2012
@@ -51,6 +51,30 @@ public abstract class FileInfo {
protected void newline() {
// do nothing by default.
}
+
+ protected static int readSample(InputStream is, int bytesPerSample) throws IOException {
+ int sample = 0;
+ for (int i = 0; i < bytesPerSample; i++) {
+ int nextByte = is.read();
+ if (nextByte < 0) {
+ throw new IOException("PNM: Unexpected EOF");
+ }
+ sample <<= 8;
+ sample |= nextByte;
+ }
+ return sample;
+ }
+
+ protected static int scaleSample(int sample, float scale, int max) throws IOException {
+ if (sample < 0) {
+ // Even netpbm tools break for files like this
+ throw new IOException("Negative pixel values are invalid in PNM files");
+ } else if (sample > max) {
+ // invalid values -> black
+ sample = 0;
+ }
+ return (int)((sample * max / scale) + 0.5f);
+ }
public void readImage(ImageBuilder imageBuilder, InputStream is)
throws IOException {
Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PgmFileInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PgmFileInfo.java?rev=1391758&r1=1391757&r2=1391758&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PgmFileInfo.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PgmFileInfo.java Sat Sep 29 06:40:39 2012
@@ -21,13 +21,28 @@ import java.io.InputStream;
import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageReadException;
public class PgmFileInfo extends FileInfo {
- private final int max; // TODO: handle max
+ private final int max;
+ private final float scale;
+ private final int bytesPerSample;
- public PgmFileInfo(int width, int height, boolean RAWBITS, int max) {
+ public PgmFileInfo(int width, int height, boolean RAWBITS, int max) throws ImageReadException {
super(width, height, RAWBITS);
-
+ if (max <= 0) {
+ throw new ImageReadException("PGM maxVal " + max
+ + " is out of range [1;65535]");
+ } else if (max <= 255) {
+ scale = 255f;
+ bytesPerSample = 1;
+ } else if (max <= 65535) {
+ scale = 65535f;
+ bytesPerSample = 2;
+ } else {
+ throw new ImageReadException("PGM maxVal " + max
+ + " is out of range [1;65535]");
+ }
this.max = max;
}
@@ -63,10 +78,9 @@ public class PgmFileInfo extends FileInf
@Override
public int getRGB(InputStream is) throws IOException {
- int sample = is.read();
- if (sample < 0) {
- throw new IOException("PGM: Unexpected EOF");
- }
+ int sample = readSample(is, bytesPerSample);
+
+ sample = scaleSample(sample, scale, max);
int alpha = 0xff;
@@ -80,6 +94,8 @@ public class PgmFileInfo extends FileInf
public int getRGB(WhiteSpaceReader wsr) throws IOException {
int sample = Integer.parseInt(wsr.readtoWhiteSpace());
+ sample = scaleSample(sample, scale, max);
+
int alpha = 0xff;
int rgb = ((0xff & alpha) << 24) | ((0xff & sample) << 16)
Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java?rev=1391758&r1=1391757&r2=1391758&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java Sat Sep 29 06:40:39 2012
@@ -227,29 +227,6 @@ public class PnmImageParser extends Imag
return true;
}
- private int[] getColorTable(byte bytes[]) throws ImageReadException {
- if ((bytes.length % 3) != 0) {
- throw new ImageReadException("Bad Color Table Length: "
- + bytes.length);
- }
- int length = bytes.length / 3;
-
- int result[] = new int[length];
-
- for (int i = 0; i < length; i++) {
- int red = 0xff & bytes[(i * 3) + 0];
- int green = 0xff & bytes[(i * 3) + 1];
- int blue = 0xff & bytes[(i * 3) + 2];
-
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
- result[i] = rgb;
- }
-
- return result;
- }
-
@Override
public BufferedImage getBufferedImage(ByteSource byteSource, Map<String,Object> params)
throws ImageReadException, IOException {
Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PpmFileInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PpmFileInfo.java?rev=1391758&r1=1391757&r2=1391758&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PpmFileInfo.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PpmFileInfo.java Sat Sep 29 06:40:39 2012
@@ -21,13 +21,28 @@ import java.io.InputStream;
import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageReadException;
public class PpmFileInfo extends FileInfo {
- private final int max; // TODO: handle max
+ private final int max;
+ private final float scale;
+ private final int bytesPerSample;
- public PpmFileInfo(int width, int height, boolean RAWBITS, int max) {
+ public PpmFileInfo(int width, int height, boolean RAWBITS, int max) throws ImageReadException {
super(width, height, RAWBITS);
-
+ if (max <= 0) {
+ throw new ImageReadException("PGM maxVal " + max
+ + " is out of range [1;65535]");
+ } else if (max <= 255) {
+ scale = 255f;
+ bytesPerSample = 1;
+ } else if (max <= 65535) {
+ scale = 65535f;
+ bytesPerSample = 2;
+ } else {
+ throw new ImageReadException("PGM maxVal " + max
+ + " is out of range [1;65535]");
+ }
this.max = max;
}
@@ -63,14 +78,13 @@ public class PpmFileInfo extends FileInf
@Override
public int getRGB(InputStream is) throws IOException {
- int red = is.read();
- int green = is.read();
- int blue = is.read();
-
- if ((red < 0) || (green < 0) || (blue < 0)) {
- throw new IOException("PPM: Unexpected EOF");
- }
-
+ int red = readSample(is, bytesPerSample);
+ int green = readSample(is, bytesPerSample);
+ int blue = readSample(is, bytesPerSample);
+
+ red = scaleSample(red, scale, max);
+ green = scaleSample(green, scale, max);
+ blue = scaleSample(blue, scale, max);
int alpha = 0xff;
int rgb = ((0xff & alpha) << 24) | ((0xff & red) << 16)
@@ -85,6 +99,9 @@ public class PpmFileInfo extends FileInf
int green = Integer.parseInt(wsr.readtoWhiteSpace());
int blue = Integer.parseInt(wsr.readtoWhiteSpace());
+ red = scaleSample(red, scale, max);
+ green = scaleSample(green, scale, max);
+ blue = scaleSample(blue, scale, max);
int alpha = 0xff;
int rgb = ((0xff & alpha) << 24) | ((0xff & red) << 16)