6488522: PNG writer should permit setting compression level and iDAT chunk maximum size
authorprr
Wed, 16 Dec 2015 14:36:37 -0800
changeset 35646 845a3ebfeb00
parent 35645 a96d68e3fda2
child 35647 b9a81a0ab96f
6488522: PNG writer should permit setting compression level and iDAT chunk maximum size Reviewed-by: serb, prr
jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java
jdk/test/javax/imageio/plugins/shared/ImageWriterCompressionTest.java
--- a/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java	Wed Dec 16 13:24:19 2015 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java	Wed Dec 16 14:36:37 2015 -0800
@@ -26,16 +26,13 @@
 package com.sun.imageio.plugins.png;
 
 import java.awt.Rectangle;
-import java.awt.image.ColorModel;
 import java.awt.image.IndexColorModel;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
 import java.awt.image.RenderedImage;
 import java.awt.image.SampleModel;
 import java.io.ByteArrayOutputStream;
-import java.io.DataOutput;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.zip.Deflater;
@@ -46,14 +43,13 @@
 import javax.imageio.ImageWriteParam;
 import javax.imageio.ImageWriter;
 import javax.imageio.metadata.IIOMetadata;
-import javax.imageio.metadata.IIOMetadata;
 import javax.imageio.spi.ImageWriterSpi;
 import javax.imageio.stream.ImageOutputStream;
 import javax.imageio.stream.ImageOutputStreamImpl;
 
-class CRC {
+final class CRC {
 
-    private static int[] crcTable = new int[256];
+    private static final int[] crcTable = new int[256];
     private int crc = 0xffffffff;
 
     static {
@@ -72,23 +68,25 @@
         }
     }
 
-    public CRC() {}
+    CRC() {}
 
-    public void reset() {
+    void reset() {
         crc = 0xffffffff;
     }
 
-    public void update(byte[] data, int off, int len) {
+    void update(byte[] data, int off, int len) {
+        int c = crc;
         for (int n = 0; n < len; n++) {
-            crc = crcTable[(crc ^ data[off + n]) & 0xff] ^ (crc >>> 8);
+            c = crcTable[(c ^ data[off + n]) & 0xff] ^ (c >>> 8);
         }
+        crc = c;
     }
 
-    public void update(int data) {
+    void update(int data) {
         crc = crcTable[(crc ^ data) & 0xff] ^ (crc >>> 8);
     }
 
-    public int getValue() {
+    int getValue() {
         return crc ^ 0xffffffff;
     }
 }
@@ -96,11 +94,11 @@
 
 final class ChunkStream extends ImageOutputStreamImpl {
 
-    private ImageOutputStream stream;
-    private long startPos;
-    private CRC crc = new CRC();
+    private final ImageOutputStream stream;
+    private final long startPos;
+    private final CRC crc = new CRC();
 
-    public ChunkStream(int type, ImageOutputStream stream) throws IOException {
+    ChunkStream(int type, ImageOutputStream stream) throws IOException {
         this.stream = stream;
         this.startPos = stream.getStreamPosition();
 
@@ -108,25 +106,29 @@
         writeInt(type);
     }
 
+    @Override
     public int read() throws IOException {
         throw new RuntimeException("Method not available");
     }
 
+    @Override
     public int read(byte[] b, int off, int len) throws IOException {
         throw new RuntimeException("Method not available");
     }
 
+    @Override
     public void write(byte[] b, int off, int len) throws IOException {
         crc.update(b, off, len);
         stream.write(b, off, len);
     }
 
+    @Override
     public void write(int b) throws IOException {
         crc.update(b);
         stream.write(b);
     }
 
-    public void finish() throws IOException {
+    void finish() throws IOException {
         // Write CRC
         stream.writeInt(crc.getValue());
 
@@ -140,6 +142,7 @@
         stream.flushBefore(pos);
     }
 
+    @Override
     protected void finalize() throws Throwable {
         // Empty finalizer (for improved performance; no need to call
         // super.finalize() in this case)
@@ -150,24 +153,29 @@
 // fixed length.
 final class IDATOutputStream extends ImageOutputStreamImpl {
 
-    private static byte[] chunkType = {
+    private static final byte[] chunkType = {
         (byte)'I', (byte)'D', (byte)'A', (byte)'T'
     };
 
-    private ImageOutputStream stream;
-    private int chunkLength;
+    private final ImageOutputStream stream;
+    private final int chunkLength;
     private long startPos;
-    private CRC crc = new CRC();
+    private final CRC crc = new CRC();
 
-    Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
-    byte[] buf = new byte[512];
+    private final Deflater def;
+    private final byte[] buf = new byte[512];
+    // reused 1 byte[] array:
+    private final byte[] wbuf1 = new byte[1];
 
     private int bytesRemaining;
 
-    public IDATOutputStream(ImageOutputStream stream, int chunkLength)
-        throws IOException {
+    IDATOutputStream(ImageOutputStream stream, int chunkLength,
+                            int deflaterLevel) throws IOException
+    {
         this.stream = stream;
         this.chunkLength = chunkLength;
+        this.def = new Deflater(deflaterLevel);
+
         startChunk();
     }
 
@@ -206,14 +214,17 @@
         }
     }
 
+    @Override
     public int read() throws IOException {
         throw new RuntimeException("Method not available");
     }
 
+    @Override
     public int read(byte[] b, int off, int len) throws IOException {
         throw new RuntimeException("Method not available");
     }
 
+    @Override
     public void write(byte[] b, int off, int len) throws IOException {
         if (len == 0) {
             return;
@@ -227,7 +238,7 @@
         }
     }
 
-    public void deflate() throws IOException {
+    void deflate() throws IOException {
         int len = def.deflate(buf, 0, buf.length);
         int off = 0;
 
@@ -247,13 +258,13 @@
         }
     }
 
+    @Override
     public void write(int b) throws IOException {
-        byte[] wbuf = new byte[1];
-        wbuf[0] = (byte)b;
-        write(wbuf, 0, 1);
+        wbuf1[0] = (byte)b;
+        write(wbuf1, 0, 1);
     }
 
-    public void finish() throws IOException {
+    void finish() throws IOException {
         try {
             if (!def.finished()) {
                 def.finish();
@@ -267,6 +278,7 @@
         }
     }
 
+    @Override
     protected void finalize() throws Throwable {
         // Empty finalizer (for improved performance; no need to call
         // super.finalize() in this case)
@@ -274,18 +286,76 @@
 }
 
 
-class PNGImageWriteParam extends ImageWriteParam {
+final class PNGImageWriteParam extends ImageWriteParam {
+
+    /** Default quality level = 0.5 ie medium compression */
+    private static final float DEFAULT_QUALITY = 0.5f;
 
-    public PNGImageWriteParam(Locale locale) {
+    private static final String[] compressionNames = {"Deflate"};
+    private static final float[] qualityVals = { 0.00F, 0.30F, 0.75F, 1.00F };
+    private static final String[] qualityDescs = {
+        "High compression",   // 0.00 -> 0.30
+        "Medium compression", // 0.30 -> 0.75
+        "Low compression"     // 0.75 -> 1.00
+    };
+
+    PNGImageWriteParam(Locale locale) {
         super();
         this.canWriteProgressive = true;
         this.locale = locale;
+        this.canWriteCompressed = true;
+        this.compressionTypes = compressionNames;
+        this.compressionType = compressionTypes[0];
+        this.compressionMode = MODE_DEFAULT;
+        this.compressionQuality = DEFAULT_QUALITY;
+    }
+
+    /**
+     * Removes any previous compression quality setting.
+     *
+     * <p> The default implementation resets the compression quality
+     * to <code>0.5F</code>.
+     *
+     * @exception IllegalStateException if the compression mode is not
+     * <code>MODE_EXPLICIT</code>.
+     */
+    @Override
+    public void unsetCompression() {
+        super.unsetCompression();
+        this.compressionType = compressionTypes[0];
+        this.compressionQuality = DEFAULT_QUALITY;
+    }
+
+    /**
+     * Returns <code>true</code> since the PNG plug-in only supports
+     * lossless compression.
+     *
+     * @return <code>true</code>.
+     */
+    @Override
+    public boolean isCompressionLossless() {
+        return true;
+    }
+
+    @Override
+    public String[] getCompressionQualityDescriptions() {
+        super.getCompressionQualityDescriptions();
+        return qualityDescs.clone();
+    }
+
+    @Override
+    public float[] getCompressionQualityValues() {
+        super.getCompressionQualityValues();
+        return qualityVals.clone();
     }
 }
 
 /**
  */
-public class PNGImageWriter extends ImageWriter {
+public final class PNGImageWriter extends ImageWriter {
+
+    /** Default compression level = 4 ie medium compression */
+    private static final int DEFAULT_COMPRESSION_LEVEL = 4;
 
     ImageOutputStream stream = null;
 
@@ -334,6 +404,7 @@
         super(originatingProvider);
     }
 
+    @Override
     public void setOutput(Object output) {
         super.setOutput(output);
         if (output != null) {
@@ -346,16 +417,17 @@
         }
     }
 
-    private static int[] allowedProgressivePasses = { 1, 7 };
-
+    @Override
     public ImageWriteParam getDefaultWriteParam() {
         return new PNGImageWriteParam(getLocale());
     }
 
+    @Override
     public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
         return null;
     }
 
+    @Override
     public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,
                                                ImageWriteParam param) {
         PNGMetadata m = new PNGMetadata();
@@ -363,11 +435,13 @@
         return m;
     }
 
+    @Override
     public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                              ImageWriteParam param) {
         return null;
     }
 
+    @Override
     public IIOMetadata convertImageMetadata(IIOMetadata inData,
                                             ImageTypeSpecifier imageType,
                                             ImageWriteParam param) {
@@ -934,8 +1008,11 @@
     }
 
     // Use sourceXOffset, etc.
-    private void write_IDAT(RenderedImage image) throws IOException {
-        IDATOutputStream ios = new IDATOutputStream(stream, 32768);
+    private void write_IDAT(RenderedImage image, int deflaterLevel)
+        throws IOException
+    {
+        IDATOutputStream ios = new IDATOutputStream(stream, 32768,
+                                                    deflaterLevel);
         try {
             if (metadata.IHDR_interlaceMethod == 1) {
                 for (int i = 0; i < 7; i++) {
@@ -1028,6 +1105,7 @@
         }
     }
 
+    @Override
     public void write(IIOMetadata streamMetadata,
                       IIOImage image,
                       ImageWriteParam param) throws IIOException {
@@ -1110,7 +1188,23 @@
             metadata = new PNGMetadata();
         }
 
+        // reset compression level to default:
+        int deflaterLevel = DEFAULT_COMPRESSION_LEVEL;
+
         if (param != null) {
+            switch(param.getCompressionMode()) {
+            case ImageWriteParam.MODE_DISABLED:
+                deflaterLevel = Deflater.NO_COMPRESSION;
+                break;
+            case ImageWriteParam.MODE_EXPLICIT:
+                float quality = param.getCompressionQuality();
+                if (quality >= 0f && quality <= 1f) {
+                    deflaterLevel = 9 - Math.round(9f * quality);
+                }
+                break;
+            default:
+            }
+
             // Use Adam7 interlacing if set in write param
             switch (param.getProgressiveMode()) {
             case ImageWriteParam.MODE_DEFAULT:
@@ -1119,8 +1213,9 @@
             case ImageWriteParam.MODE_DISABLED:
                 metadata.IHDR_interlaceMethod = 0;
                 break;
-                // MODE_COPY_FROM_METADATA should alreay be taken care of
+                // MODE_COPY_FROM_METADATA should already be taken care of
                 // MODE_EXPLICIT is not allowed
+            default:
             }
         }
 
@@ -1165,7 +1260,7 @@
 
             writeUnknownChunks();
 
-            write_IDAT(im);
+            write_IDAT(im, deflaterLevel);
 
             if (abortRequested()) {
                 processWriteAborted();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/imageio/plugins/shared/ImageWriterCompressionTest.java	Wed Dec 16 14:36:37 2015 -0800
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Set;
+import javax.imageio.IIOImage;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageWriteParam;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.ImageOutputStream;
+
+/**
+ * @test @bug 6488522
+ * @summary Check the compression support in imageio ImageWriters
+ * @run main ImageWriterCompressionTest
+ */
+public class ImageWriterCompressionTest {
+
+    // ignore jpg (fail):
+    // Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
+    private static final Set<String> IGNORE_FILE_SUFFIXES
+        = new HashSet<String>(Arrays.asList(new String[] {
+            "bmp", "gif",
+            "jpg", "jpeg",
+//            "tif", "tiff"
+        } ));
+
+    public static void main(String[] args) {
+        Locale.setDefault(Locale.US);
+
+        final BufferedImage image
+            = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
+
+        final Graphics2D g2d = image.createGraphics();
+        try {
+            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+                                 RenderingHints.VALUE_ANTIALIAS_ON);
+            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
+                                 RenderingHints.VALUE_RENDER_QUALITY);
+            g2d.scale(2.0, 2.0);
+
+            g2d.setColor(Color.red);
+            g2d.draw(new Rectangle2D.Float(10, 10, 100, 100));
+            g2d.setColor(Color.blue);
+            g2d.fill(new Rectangle2D.Float(12, 12, 98, 98));
+            g2d.setColor(Color.green);
+            g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14));
+
+            for (int i = 0; i < 15; i++) {
+                g2d.drawString("Testing Compression ...", 20, 20 + i * 16);
+            }
+
+            final String[] fileSuffixes = ImageIO.getWriterFileSuffixes();
+
+            final Set<String> testedWriterClasses = new HashSet<String>();
+
+            for (String suffix : fileSuffixes) {
+
+                if (!IGNORE_FILE_SUFFIXES.contains(suffix)) {
+                    final Iterator<ImageWriter> itWriters
+                        = ImageIO.getImageWritersBySuffix(suffix);
+
+                    final ImageWriter writer;
+                    final ImageWriteParam writerParams;
+
+                    if (itWriters.hasNext()) {
+                        writer = itWriters.next();
+
+                        if (testedWriterClasses.add(writer.getClass().getName())) {
+                            writerParams = writer.getDefaultWriteParam();
+
+                            if (writerParams.canWriteCompressed()) {
+                                testCompression(image, writer, writerParams, suffix);
+                            }
+                        }
+                    } else {
+                        throw new RuntimeException("Unable to get writer !");
+                    }
+                }
+            }
+        } catch (IOException ioe) {
+            throw new RuntimeException("IO failure", ioe);
+        }
+        finally {
+            g2d.dispose();
+        }
+    }
+
+    private static void testCompression(final BufferedImage image,
+                                        final ImageWriter writer,
+                                        final ImageWriteParam writerParams,
+                                        final String suffix)
+        throws IOException
+    {
+        System.out.println("Compression types: "
+            + Arrays.toString(writerParams.getCompressionTypes()));
+
+        // Test Compression modes:
+        try {
+            writerParams.setCompressionMode(ImageWriteParam.MODE_DISABLED);
+            saveImage(image, writer, writerParams, "disabled", suffix);
+        } catch (Exception e) {
+            System.out.println("CompressionMode Disabled not supported: "+ e.getMessage());
+        }
+
+        try {
+            writerParams.setCompressionMode(ImageWriteParam.MODE_DEFAULT);
+            saveImage(image, writer, writerParams, "default", suffix);
+        } catch (Exception e) {
+            System.out.println("CompressionMode Default not supported: "+ e.getMessage());
+        }
+
+        writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
+        writerParams.setCompressionType(selectCompressionType(suffix,
+                        writerParams.getCompressionTypes()));
+
+        System.out.println("Selected Compression type: "
+            + writerParams.getCompressionType());
+
+        long prev = Long.MAX_VALUE;
+        for (int i = 10; i >= 0; i--) {
+            float quality = 0.1f * i;
+            writerParams.setCompressionQuality(quality);
+
+            long len = saveImage(image, writer, writerParams,
+                                 String.format("explicit-%.1f", quality), suffix);
+
+            if (len <= 0) {
+                throw new RuntimeException("zero file length !");
+            } else if (len > prev) {
+                throw new RuntimeException("Incorrect file length: " + len
+                    + " larger than previous: " + prev + " !");
+            }
+            prev = len;
+        }
+    }
+
+    private static String selectCompressionType(final String suffix,
+                                                final String[] types)
+    {
+        switch (suffix) {
+            case "tif":
+            case "tiff":
+                return "LZW";
+            default:
+                return types[0];
+        }
+    }
+
+    private static long saveImage(final BufferedImage image,
+                                  final ImageWriter writer,
+                                  final ImageWriteParam writerParams,
+                                  final String mode,
+                                  final String suffix) throws IOException
+    {
+        final File imgFile = new File("WriterCompressionTest-"
+                                      + mode + '.' + suffix);
+        System.out.println("Writing file: " + imgFile.getAbsolutePath());
+
+        final ImageOutputStream imgOutStream
+            = ImageIO.createImageOutputStream(new FileOutputStream(imgFile));
+        try {
+            writer.setOutput(imgOutStream);
+            writer.write(null, new IIOImage(image, null, null), writerParams);
+        } finally {
+            imgOutStream.close();
+        }
+        return imgFile.length();
+    }
+}