in src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java [186:319]
private FileInfo readHeader(final InputStream is) throws ImagingException,
IOException {
final byte identifier1 = readByte("Identifier1", is, "Not a Valid PNM File");
final byte identifier2 = readByte("Identifier2", is, "Not a Valid PNM File");
if (identifier1 != PnmConstants.PNM_PREFIX_BYTE) {
throw new ImagingException("PNM file has invalid prefix byte 1");
}
final WhiteSpaceReader wsr = new WhiteSpaceReader(is);
if (identifier2 == PnmConstants.PBM_TEXT_CODE
|| identifier2 == PnmConstants.PBM_RAW_CODE
|| identifier2 == PnmConstants.PGM_TEXT_CODE
|| identifier2 == PnmConstants.PGM_RAW_CODE
|| identifier2 == PnmConstants.PPM_TEXT_CODE
|| identifier2 == PnmConstants.PPM_RAW_CODE) {
final int width;
try {
width = Integer.parseInt(wsr.readtoWhiteSpace());
} catch (final NumberFormatException e) {
throw new ImagingException("Invalid width specified." , e);
}
final int height;
try {
height = Integer.parseInt(wsr.readtoWhiteSpace());
} catch (final NumberFormatException e) {
throw new ImagingException("Invalid height specified." , e);
}
switch (identifier2) {
case PnmConstants.PBM_TEXT_CODE:
return new PbmFileInfo(width, height, false);
case PnmConstants.PBM_RAW_CODE:
return new PbmFileInfo(width, height, true);
case PnmConstants.PGM_TEXT_CODE: {
final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
return new PgmFileInfo(width, height, false, maxgray);
}
case PnmConstants.PGM_RAW_CODE: {
final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
return new PgmFileInfo(width, height, true, maxgray);
}
case PnmConstants.PPM_TEXT_CODE: {
final int max = Integer.parseInt(wsr.readtoWhiteSpace());
return new PpmFileInfo(width, height, false, max);
}
case PnmConstants.PPM_RAW_CODE: {
final int max = Integer.parseInt(wsr.readtoWhiteSpace());
return new PpmFileInfo(width, height, true, max);
}
default:
break;
}
} else if (identifier2 == PnmConstants.PAM_RAW_CODE) {
int width = -1;
boolean seenWidth = false;
int height = -1;
boolean seenHeight = false;
int depth = -1;
boolean seenDepth = false;
int maxVal = -1;
boolean seenMaxVal = false;
final StringBuilder tupleType = new StringBuilder();
boolean seenTupleType = false;
// Advance to next line
wsr.readLine();
String line;
while ((line = wsr.readLine()) != null) {
line = line.trim();
if (line.charAt(0) == '#') {
continue;
}
final StringTokenizer tokenizer = new StringTokenizer(line, " ", false);
final String type = tokenizer.nextToken();
if ("WIDTH".equals(type)) {
seenWidth = true;
if(!tokenizer.hasMoreTokens()) {
throw new ImagingException("PAM header has no WIDTH value");
}
width = Integer.parseInt(tokenizer.nextToken());
} else if ("HEIGHT".equals(type)) {
seenHeight = true;
if(!tokenizer.hasMoreTokens()) {
throw new ImagingException("PAM header has no HEIGHT value");
}
height = Integer.parseInt(tokenizer.nextToken());
} else if ("DEPTH".equals(type)) {
seenDepth = true;
if(!tokenizer.hasMoreTokens()) {
throw new ImagingException("PAM header has no DEPTH value");
}
depth = Integer.parseInt(tokenizer.nextToken());
} else if ("MAXVAL".equals(type)) {
seenMaxVal = true;
if(!tokenizer.hasMoreTokens()) {
throw new ImagingException("PAM header has no MAXVAL value");
}
maxVal = Integer.parseInt(tokenizer.nextToken());
} else if ("TUPLTYPE".equals(type)) {
seenTupleType = true;
if(!tokenizer.hasMoreTokens()) {
throw new ImagingException("PAM header has no TUPLTYPE value");
}
tupleType.append(tokenizer.nextToken());
} else if ("ENDHDR".equals(type)) {
break;
} else {
throw new ImagingException("Invalid PAM file header type " + type);
}
}
if (!seenWidth) {
throw new ImagingException("PAM header has no WIDTH");
}
if (!seenHeight) {
throw new ImagingException("PAM header has no HEIGHT");
}
if (!seenDepth) {
throw new ImagingException("PAM header has no DEPTH");
}
if (!seenMaxVal) {
throw new ImagingException("PAM header has no MAXVAL");
}
if (!seenTupleType) {
throw new ImagingException("PAM header has no TUPLTYPE");
}
return new PamFileInfo(width, height, depth, maxVal, tupleType.toString());
}
throw new ImagingException("PNM file has invalid prefix byte 2");
}