kinow commented on a change in pull request #38: Cleaned up PNG component.
URL: https://github.com/apache/commons-imaging/pull/38#discussion_r284485438
##########
File path:
src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
##########
@@ -100,119 +161,97 @@ public String getDefaultExtension() {
// private final static int tRNS = CharsToQuad('t', 'R', 'N', 's');
- public static String getChunkTypeName(final int chunkType) {
- final StringBuilder result = new StringBuilder();
- result.append((char) (0xff & (chunkType >> 24)));
- result.append((char) (0xff & (chunkType >> 16)));
- result.append((char) (0xff & (chunkType >> 8)));
- result.append((char) (0xff & (chunkType >> 0)));
- return result.toString();
+ public static String getChunkTypeName(final int chunkTypeCode) {
+ return new String(
+ ByteBuffer.allocate(4).putInt(chunkTypeCode).array(),
+ StandardCharsets.ISO_8859_1);
}
/**
+ * Extracts all the chunks in the PNG file (in the input stream)
+ * and returns a list of their names.
+ *
+ * @param is
* @return List of String-formatted chunk types, ie. "tRNs".
+ * @throws ImageReadException
+ * @throws IOException
*/
public List<String> getChunkTypes(final InputStream is)
throws ImageReadException, IOException {
final List<PngChunk> chunks = readChunks(is, null, false);
final List<String> chunkTypes = new ArrayList<>(chunks.size());
for (final PngChunk chunk : chunks) {
- chunkTypes.add(getChunkTypeName(chunk.chunkType));
+ chunkTypes.add(getChunkTypeName(chunk.chunkType.toCode()));
}
return chunkTypes;
}
public boolean hasChunkType(final ByteSource byteSource, final ChunkType
chunkType)
throws ImageReadException, IOException {
- try (InputStream is = byteSource.getInputStream()) {
- readSignature(is);
- final List<PngChunk> chunks = readChunks(is, new ChunkType[] {
chunkType }, true);
- return !chunks.isEmpty();
- }
- }
-
- private boolean keepChunk(final int chunkType, final ChunkType[]
chunkTypes) {
- // System.out.println("keepChunk: ");
- if (chunkTypes == null) {
- return true;
- }
-
- for (final ChunkType chunkType2 : chunkTypes) {
- if (chunkType2.value == chunkType) {
- return true;
- }
- }
- return false;
+ return ChunkReader.newInstance(byteSource)
+ .readChunks(new ChunkUniselector(true, chunkType), true)
+ .size() > 0;
}
- private List<PngChunk> readChunks(final InputStream is, final ChunkType[]
chunkTypes,
- final boolean returnAfterFirst) throws ImageReadException,
IOException {
+ /**
+ * Reads all the chunks (satisfying any filters) from the input
+ * stream containing a PNG file.
+ *
+ * Replaces the readChunks(InputStream, ChunkTypes[], boolean)
+ * method. Is clean, and readable now.
+ *
+ * @param inputStream
+ * @param selector
+ * @param returnAfterFirst
+ * @return
+ * @throws IOException
+ * @throws ImageReadException
+ */
+ private List<PngChunk> readChunks(final InputStream inputStream,
+ final ChunkSelector selector, final boolean returnAfterFirst)
+ throws IOException, ImageReadException {
final List<PngChunk> result = new ArrayList<>();
-
- while (true) {
- final int length = read4Bytes("Length", is, "Not a Valid PNG
File", getByteOrder());
- final int chunkType = read4Bytes("ChunkType", is, "Not a Valid PNG
File", getByteOrder());
-
- if (LOGGER.isLoggable(Level.FINEST)) {
- printCharQuad("ChunkType", chunkType);
- debugNumber("Length", length, 4);
- }
- final boolean keep = keepChunk(chunkType, chunkTypes);
-
- byte[] bytes = null;
- if (keep) {
- bytes = readBytes("Chunk Data", is, length,
- "Not a Valid PNG File: Couldn't read Chunk Data.");
- } else {
- skipBytes(is, length, "Not a Valid PNG File");
- }
-
- if (LOGGER.isLoggable(Level.FINEST)) {
- if (bytes != null) {
- debugNumber("bytes", bytes.length, 4);
- }
- }
-
- final int crc = read4Bytes("CRC", is, "Not a Valid PNG File",
getByteOrder());
-
- if (keep) {
- if (chunkType == ChunkType.iCCP.value) {
- result.add(new PngChunkIccp(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.tEXt.value) {
- result.add(new PngChunkText(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.zTXt.value) {
- result.add(new PngChunkZtxt(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.IHDR.value) {
- result.add(new PngChunkIhdr(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.PLTE.value) {
- result.add(new PngChunkPlte(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.pHYs.value) {
- result.add(new PngChunkPhys(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.sCAL.value) {
- result.add(new PngChunkScal(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.IDAT.value) {
- result.add(new PngChunkIdat(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.gAMA.value) {
- result.add(new PngChunkGama(length, chunkType, crc,
bytes));
- } else if (chunkType == ChunkType.iTXt.value) {
- result.add(new PngChunkItxt(length, chunkType, crc,
bytes));
- } else {
- result.add(new PngChunk(length, chunkType, crc, bytes));
+ final DataInputStream dataInput = new DataInputStream(inputStream);
+
+ while(true) {
+ final ChunkLoader nextChunkLoaded = ChunkLoader.run(dataInput,
selector);
+ PngChunk loadedChunk = nextChunkLoaded.loadedChunk();
+
+ if(loadedChunk != null) {
+ { // (Shukant Pal): I essentially hate how developers
+ // have created a dumb PngCrc object. Couldn't it just be
+ // in one method call. Anyways, CRC checking wasn't
+ // done before. I added this.
+ final PngCrc png_crc = new PngCrc();
+
+ final long crc1 =
png_crc.start_partial_crc(loadedChunk.chunkType.array, 4);
+ final long crc2 = loadedChunk.getBytes() == null ? crc1 :
+ png_crc.continue_partial_crc(
+ crc1, loadedChunk.getBytes(),
loadedChunk.contentSize());
+
+ int chunkCRC = (int) png_crc.finish_partial_crc(crc2);
+
+ if(chunkCRC != nextChunkLoaded.chunkCrc()) {
+ throw new ImageReadException("Illegal CRC value found"
+
+ "(ChunkType: " + loadedChunk.chunkType +")," +
+ "(CRC VALUE: " + nextChunkLoaded.chunkCrc() +
")," +
+ "(CRC EXPECTED " + chunkCRC + ")");
+ }
}
-
- if (returnAfterFirst) {
- return result;
+
+ result.add(loadedChunk);
+
+ if(returnAfterFirst) {
+ break;
}
}
-
- if (chunkType == ChunkType.IEND.value) {
+
+ if(nextChunkLoaded.isLast()) {
break;
- }
-
+ } // loadedChunk may be null still
}
-
+
Review comment:
Extra spaces. Running `mvn` might catch a few errors in this pull request.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services