kinow commented on a change in pull request #38: Cleaned up PNG component.
URL: https://github.com/apache/commons-imaging/pull/38#discussion_r284486752
##########
File path: src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
##########
@@ -74,46 +207,47 @@ private void writeChunk(final OutputStream os, final
ChunkType chunkType,
crc1, data, data.length);
final int crc = (int) png_crc.finish_partial_crc(crc2);
- writeInt(os, crc);
+ writeInt(outputStream, crc);
}
- private static class ImageHeader {
- public final int width;
- public final int height;
- public final byte bitDepth;
- public final PngColorType pngColorType;
- public final byte compressionMethod;
- public final byte filterMethod;
- public final InterlaceMethod interlaceMethod;
-
- ImageHeader(final int width, final int height, final byte bitDepth,
- final PngColorType pngColorType, final byte compressionMethod,
final byte filterMethod,
- final InterlaceMethod interlaceMethod) {
- this.width = width;
- this.height = height;
- this.bitDepth = bitDepth;
- this.pngColorType = pngColorType;
- this.compressionMethod = compressionMethod;
- this.filterMethod = filterMethod;
- this.interlaceMethod = interlaceMethod;
+ /**
+ * Puts the chunk contents (including length, type and CRC) into
+ * the output stream provided.
+ *
+ * Also calculates the CRC if required. (used by writeHeader)
+ *
+ * @param outputStream stream into which chunk is to be written
+ * @param chunk chunk to write
+ * @throws IOException
+ */
+ private void writeChunk(final DataOutputStream outputStream,
+ final PngChunk chunk) throws IOException {
+ outputStream.writeInt(chunk.contentSize());
+ outputStream.writeInt(chunk.chunkType.toCode());
+
+ if (chunk.getBytes() != null) {
+ outputStream.write(chunk.getBytes());
}
- }
+ int chunkCRC;
- private void writeChunkIHDR(final OutputStream os, final ImageHeader
value) throws IOException {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- writeInt(baos, value.width);
- writeInt(baos, value.height);
- baos.write(0xff & value.bitDepth);
- baos.write(0xff & value.pngColorType.getValue());
- baos.write(0xff & value.compressionMethod);
- baos.write(0xff & value.filterMethod);
- baos.write(0xff & value.interlaceMethod.ordinal());
-
- writeChunk(os, ChunkType.IHDR, baos.toByteArray());
+ if (chunk.wasChanged(true)) {
+ final PngCrc png_crc = new PngCrc();
+
+ final long crc1 = png_crc.start_partial_crc(chunk.chunkType.array,
4);
+ final long crc2 = chunk.getBytes() == null ? crc1 :
png_crc.continue_partial_crc(
+ crc1, chunk.getBytes(), chunk.contentSize());
+ chunkCRC = (int) png_crc.finish_partial_crc(crc2);
+ } else {
+ chunkCRC = chunk.crc;
+ }
+
+ outputStream.writeInt(chunkCRC);
}
- private void writeChunkiTXt(final OutputStream os, final PngText.Itxt text)
+ // Hooray, ImageHeader and writeChunkIhdr eliminated... (remove this later)
Review comment:
Note needed? Or was it for development/debug?
----------------------------------------------------------------
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