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 ImageReadException("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 = Integer.parseInt(wsr.readtoWhiteSpace());
final int height = Integer.parseInt(wsr.readtoWhiteSpace());
if (identifier2 == PnmConstants.PBM_TEXT_CODE) {
return new PbmFileInfo(width, height, false);
} else if (identifier2 == PnmConstants.PBM_RAW_CODE) {
return new PbmFileInfo(width, height, true);
} else if (identifier2 == PnmConstants.PGM_TEXT_CODE) {
final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
return new PgmFileInfo(width, height, false, maxgray);
} else if (identifier2 == PnmConstants.PGM_RAW_CODE) {
final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
return new PgmFileInfo(width, height, true, maxgray);
} else if (identifier2 == PnmConstants.PPM_TEXT_CODE) {
final int max = Integer.parseInt(wsr.readtoWhiteSpace());
return new PpmFileInfo(width, height, false, max);
} else if (identifier2 == PnmConstants.PPM_RAW_CODE) {
final int max = Integer.parseInt(wsr.readtoWhiteSpace());
return new PpmFileInfo(width, height, true, max);
} else {
throw new ImageReadException("PNM file has invalid header.");
}
} 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;
width = Integer.parseInt(tokenizer.nextToken());
} else if ("HEIGHT".equals(type)) {
seenHeight = true;
height = Integer.parseInt(tokenizer.nextToken());
} else if ("DEPTH".equals(type)) {
seenDepth = true;
depth = Integer.parseInt(tokenizer.nextToken());
} else if ("MAXVAL".equals(type)) {
seenMaxVal = true;
maxVal = Integer.parseInt(tokenizer.nextToken());
} else if ("TUPLTYPE".equals(type)) {
seenTupleType = true;
tupleType.append(tokenizer.nextToken());
} else if ("ENDHDR".equals(type)) {
break;
} else {
throw new ImageReadException("Invalid PAM file header type " + type);
}
}
if (!seenWidth) {
throw new ImageReadException("PAM header has no WIDTH");
} else if (!seenHeight) {
throw new ImageReadException("PAM header has no HEIGHT");
} else if (!seenDepth) {
throw new ImageReadException("PAM header has no DEPTH");
} else if (!seenMaxVal) {
throw new ImageReadException("PAM header has no MAXVAL");
} else if (!seenTupleType) {
throw new ImageReadException("PAM header has no TUPLTYPE");
}
return new PamFileInfo(width, height, depth, maxVal, tupleType.toString());
} else {
throw new ImageReadException("PNM file has invalid prefix byte 2");
}
}