kinow commented on a change in pull request #38: Cleaned up PNG component.
URL: https://github.com/apache/commons-imaging/pull/38#discussion_r284486207
##########
File path: src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
##########
@@ -29,12 +31,126 @@
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.ImagingConstants;
import org.apache.commons.imaging.PixelDensity;
+import org.apache.commons.imaging.formats.png.chunks.PngChunk;
+import org.apache.commons.imaging.formats.png.chunks.PngChunkIhdr;
+import org.apache.commons.imaging.formats.png.scanline.filters.AdaptiveFilter;
+import
org.apache.commons.imaging.formats.png.scanline.filters.AdaptiveFilter.FilterType;
import org.apache.commons.imaging.internal.Debug;
import org.apache.commons.imaging.palette.Palette;
import org.apache.commons.imaging.palette.PaletteFactory;
import org.apache.commons.imaging.palette.SimplePalette;
+/**
+ * Directly writes {@code BufferedImage} objects into an output stream
+ * in the PNG format.
+ *
+ * PngWriter doesn't use the middle step of converting the BufferedImage
+ * into a list of PNG chunks.
+ * TODO: Support above for manual manipulation
+ *
+ * @author Shukant Pal
+ */
class PngWriter {
+
+ /**
+ * Transforms raw image pixel data into the required PNG IDAT
+ * (compressed) form.
+ *
+ * {@code DataTransformer} can be used to split compressed pixel
+ * data into different chunks.
+ */
+ static final class ChunkedCompressor {
+
+ /**
+ * Transformation is done on this pixel data.
+ *
+ * Can be set using {@code setInput} or passed during object
+ * construction.
+ */
+ private byte[] pixelInput;
+
+ /**
+ * Keeps the amount of data that has been transformed already.
+ */
+ private int transformedTill;
+
+ private ByteArrayOutputStream outputStream;
+
+ private DeflaterOutputStream deflaterOutputStream;
+
+ ChunkedCompressor() {
+ this.pixelInput = null;
+ }
+
+ ChunkedCompressor(byte[] pixelInput) {
+ setInput(pixelInput);
+ }
+
+ void setInput(byte[] pixelInput) {
+ this.pixelInput = pixelInput;
+ this.transformedTill = 0;
+ this.outputStream = new ByteArrayOutputStream();
+ this.deflaterOutputStream = new DeflaterOutputStream(outputStream);
+ }
+
+ /**
+ * Returns the index till which input has been transformed
+ * into output.
+ *
+ * @return size of transformed input minus 1
+ */
+ int transformedAmount() {
+ return transformedTill;
+ }
+
+ /**
+ * Transforms (at maximum) {@code transformLength} bytes of
+ * input into the required output.
+ *
+ * See {@code writePixelData} for example usage of this
+ * method.
+ *
+ * @param transformLength
+ * @return
+ */
+ byte[] transform(int transformLength) throws IOException,
ImageWriteException {
+ if(pixelInput == null) {
+ throw new ImageWriteException("Couldn't read pixelInput as it
is "
+ + "null (it wasn't initialized properly, source bug)");
Review comment:
I think this exception can be re-worded to be more clear to the end user
what went wrong. No need to indicate it is an issue in the caller, as that is
implicit.
----------------------------------------------------------------
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