8146508: 6488522 was committed with incorrect author attribution.
authorprr
Wed, 16 Dec 2015 15:22:56 -0800
changeset 35647 b9a81a0ab96f
parent 35646 845a3ebfeb00
child 35648 091db1c018f5
8146508: 6488522 was committed with incorrect author attribution. Reviewed-by: bpb
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 14:36:37 2015 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java	Wed Dec 16 15:22:56 2015 -0800
@@ -26,13 +26,16 @@
 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;
@@ -43,13 +46,14 @@
 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;
 
-final class CRC {
+class CRC {
 
-    private static final int[] crcTable = new int[256];
+    private static int[] crcTable = new int[256];
     private int crc = 0xffffffff;
 
     static {
@@ -68,25 +72,23 @@
         }
     }
 
-    CRC() {}
+    public CRC() {}
 
-    void reset() {
+    public void reset() {
         crc = 0xffffffff;
     }
 
-    void update(byte[] data, int off, int len) {
-        int c = crc;
+    public void update(byte[] data, int off, int len) {
         for (int n = 0; n < len; n++) {
-            c = crcTable[(c ^ data[off + n]) & 0xff] ^ (c >>> 8);
+            crc = crcTable[(crc ^ data[off + n]) & 0xff] ^ (crc >>> 8);
         }
-        crc = c;
     }
 
-    void update(int data) {
+    public void update(int data) {
         crc = crcTable[(crc ^ data) & 0xff] ^ (crc >>> 8);
     }
 
-    int getValue() {
+    public int getValue() {
         return crc ^ 0xffffffff;
     }
 }
@@ -94,11 +96,11 @@
 
 final class ChunkStream extends ImageOutputStreamImpl {
 
-    private final ImageOutputStream stream;
-    private final long startPos;
-    private final CRC crc = new CRC();
+    private ImageOutputStream stream;
+    private long startPos;
+    private CRC crc = new CRC();
 
-    ChunkStream(int type, ImageOutputStream stream) throws IOException {
+    public ChunkStream(int type, ImageOutputStream stream) throws IOException {
         this.stream = stream;
         this.startPos = stream.getStreamPosition();
 
@@ -106,29 +108,25 @@
         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);
     }
 
-    void finish() throws IOException {
+    public void finish() throws IOException {
         // Write CRC
         stream.writeInt(crc.getValue());
 
@@ -142,7 +140,6 @@
         stream.flushBefore(pos);
     }
 
-    @Override
     protected void finalize() throws Throwable {
         // Empty finalizer (for improved performance; no need to call
         // super.finalize() in this case)
@@ -153,29 +150,24 @@
 // fixed length.
 final class IDATOutputStream extends ImageOutputStreamImpl {
 
-    private static final byte[] chunkType = {
+    private static byte[] chunkType = {
         (byte)'I', (byte)'D', (byte)'A', (byte)'T'
     };
 
-    private final ImageOutputStream stream;
-    private final int chunkLength;
+    private ImageOutputStream stream;
+    private int chunkLength;
     private long startPos;
-    private final CRC crc = new CRC();
+    private CRC crc = new CRC();
 
-    private final Deflater def;
-    private final byte[] buf = new byte[512];
-    // reused 1 byte[] array:
-    private final byte[] wbuf1 = new byte[1];
+    Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
+    byte[] buf = new byte[512];
 
     private int bytesRemaining;
 
-    IDATOutputStream(ImageOutputStream stream, int chunkLength,
-                            int deflaterLevel) throws IOException
-    {
+    public IDATOutputStream(ImageOutputStream stream, int chunkLength)
+        throws IOException {
         this.stream = stream;
         this.chunkLength = chunkLength;
-        this.def = new Deflater(deflaterLevel);
-
         startChunk();
     }
 
@@ -214,17 +206,14 @@
         }
     }
 
-    @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;
@@ -238,7 +227,7 @@
         }
     }
 
-    void deflate() throws IOException {
+    public void deflate() throws IOException {
         int len = def.deflate(buf, 0, buf.length);
         int off = 0;
 
@@ -258,13 +247,13 @@
         }
     }
 
-    @Override
     public void write(int b) throws IOException {
-        wbuf1[0] = (byte)b;
-        write(wbuf1, 0, 1);
+        byte[] wbuf = new byte[1];
+        wbuf[0] = (byte)b;
+        write(wbuf, 0, 1);
     }
 
-    void finish() throws IOException {
+    public void finish() throws IOException {
         try {
             if (!def.finished()) {
                 def.finish();
@@ -278,7 +267,6 @@
         }
     }
 
-    @Override
     protected void finalize() throws Throwable {
         // Empty finalizer (for improved performance; no need to call
         // super.finalize() in this case)
@@ -286,76 +274,18 @@
 }
 
 
-final class PNGImageWriteParam extends ImageWriteParam {
-
-    /** Default quality level = 0.5 ie medium compression */
-    private static final float DEFAULT_QUALITY = 0.5f;
+class PNGImageWriteParam extends ImageWriteParam {
 
-    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) {
+    public 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 final class PNGImageWriter extends ImageWriter {
-
-    /** Default compression level = 4 ie medium compression */
-    private static final int DEFAULT_COMPRESSION_LEVEL = 4;
+public class PNGImageWriter extends ImageWriter {
 
     ImageOutputStream stream = null;
 
@@ -404,7 +334,6 @@
         super(originatingProvider);
     }
 
-    @Override
     public void setOutput(Object output) {
         super.setOutput(output);
         if (output != null) {
@@ -417,17 +346,16 @@
         }
     }
 
-    @Override
+    private static int[] allowedProgressivePasses = { 1, 7 };
+
     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();
@@ -435,13 +363,11 @@
         return m;
     }
 
-    @Override
     public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                              ImageWriteParam param) {
         return null;
     }
 
-    @Override
     public IIOMetadata convertImageMetadata(IIOMetadata inData,
                                             ImageTypeSpecifier imageType,
                                             ImageWriteParam param) {
@@ -1008,11 +934,8 @@
     }
 
     // Use sourceXOffset, etc.
-    private void write_IDAT(RenderedImage image, int deflaterLevel)
-        throws IOException
-    {
-        IDATOutputStream ios = new IDATOutputStream(stream, 32768,
-                                                    deflaterLevel);
+    private void write_IDAT(RenderedImage image) throws IOException {
+        IDATOutputStream ios = new IDATOutputStream(stream, 32768);
         try {
             if (metadata.IHDR_interlaceMethod == 1) {
                 for (int i = 0; i < 7; i++) {
@@ -1105,7 +1028,6 @@
         }
     }
 
-    @Override
     public void write(IIOMetadata streamMetadata,
                       IIOImage image,
                       ImageWriteParam param) throws IIOException {
@@ -1188,23 +1110,7 @@
             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:
@@ -1213,9 +1119,8 @@
             case ImageWriteParam.MODE_DISABLED:
                 metadata.IHDR_interlaceMethod = 0;
                 break;
-                // MODE_COPY_FROM_METADATA should already be taken care of
+                // MODE_COPY_FROM_METADATA should alreay be taken care of
                 // MODE_EXPLICIT is not allowed
-            default:
             }
         }
 
@@ -1260,7 +1165,7 @@
 
             writeUnknownChunks();
 
-            write_IDAT(im, deflaterLevel);
+            write_IDAT(im);
 
             if (abortRequested()) {
                 processWriteAborted();
--- a/jdk/test/javax/imageio/plugins/shared/ImageWriterCompressionTest.java	Wed Dec 16 14:36:37 2015 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,202 +0,0 @@
-/*
- * 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();
-    }
-}