1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.Color; 25 import java.awt.Font; 26 import java.awt.Graphics2D; 27 import java.awt.RenderingHints; 28 import java.awt.geom.Rectangle2D; 29 import java.awt.image.BufferedImage; 30 import java.io.File; 31 import java.io.FileOutputStream; 32 import java.io.IOException; 33 import java.util.Arrays; 34 import java.util.HashSet; 35 import java.util.Iterator; 36 import java.util.Locale; 37 import java.util.Set; 38 import javax.imageio.IIOImage; 39 import javax.imageio.ImageIO; 40 import javax.imageio.ImageWriteParam; 41 import javax.imageio.ImageWriter; 42 import javax.imageio.stream.ImageOutputStream; 43 44 /** 45 * @test @bug 6488522 46 * @summary Check the compression support in imageio ImageWriters 47 * @run main ImageWriterCompressionTest 48 */ 49 public class ImageWriterCompressionTest { 50 51 // ignore jpg (fail): 52 // Caused by: javax.imageio.IIOException: Invalid argument to native writeImage 53 private static final Set<String> IGNORE_FILE_SUFFIXES 54 = new HashSet<String>(Arrays.asList(new String[] { 55 "bmp", "gif", 56 "jpg", "jpeg" 57 } )); 58 59 public static void main(String[] args) { 60 Locale.setDefault(Locale.US); 61 62 final BufferedImage image 63 = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB); 64 65 final Graphics2D g2d = image.createGraphics(); 66 try { 67 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 68 RenderingHints.VALUE_ANTIALIAS_ON); 69 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, 70 RenderingHints.VALUE_RENDER_QUALITY); 71 g2d.scale(2.0, 2.0); 72 73 g2d.setColor(Color.red); 74 g2d.draw(new Rectangle2D.Float(10, 10, 100, 100)); 75 g2d.setColor(Color.blue); 76 g2d.fill(new Rectangle2D.Float(12, 12, 98, 98)); 77 g2d.setColor(Color.green); 78 g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14)); 79 80 for (int i = 0; i < 15; i++) { 81 g2d.drawString("Testing Compression ...", 20, 20 + i * 16); 82 } 83 84 final String[] fileSuffixes = ImageIO.getWriterFileSuffixes(); 85 86 final Set<String> testedWriterClasses = new HashSet<String>(); 87 88 for (String suffix : fileSuffixes) { 89 90 if (!IGNORE_FILE_SUFFIXES.contains(suffix)) { 91 final Iterator<ImageWriter> itWriters 92 = ImageIO.getImageWritersBySuffix(suffix); 93 94 final ImageWriter writer; 95 final ImageWriteParam writerParams; 96 97 if (itWriters.hasNext()) { 98 writer = itWriters.next(); 99 100 if (testedWriterClasses.add(writer.getClass().getName())) { 101 writerParams = writer.getDefaultWriteParam(); 102 103 if (writerParams.canWriteCompressed()) { 104 testCompression(image, writer, writerParams, suffix); 105 } 106 } 107 } else { 108 throw new RuntimeException("Unable to get writer !"); 109 } 110 } 111 } 112 } catch (IOException ioe) { 113 throw new RuntimeException("IO failure", ioe); 114 } 115 finally { 116 g2d.dispose(); 117 } 118 } 119 120 private static void testCompression(final BufferedImage image, 121 final ImageWriter writer, 122 final ImageWriteParam writerParams, 123 final String suffix) 124 throws IOException 125 { 126 System.out.println("Compression types: " 127 + Arrays.toString(writerParams.getCompressionTypes())); 128 129 // Test Compression modes: 130 try { 131 writerParams.setCompressionMode(ImageWriteParam.MODE_DISABLED); 132 saveImage(image, writer, writerParams, "disabled", suffix); 133 } catch (Exception e) { 134 System.out.println("CompressionMode Disabled not supported: "+ e.getMessage()); 135 } 136 137 try { 138 writerParams.setCompressionMode(ImageWriteParam.MODE_DEFAULT); 139 saveImage(image, writer, writerParams, "default", suffix); 140 } catch (Exception e) { 141 System.out.println("CompressionMode Default not supported: "+ e.getMessage()); 142 } 143 144 writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 145 writerParams.setCompressionType(selectCompressionType(suffix, 146 writerParams.getCompressionTypes())); 147 148 System.out.println("Selected Compression type: " 149 + writerParams.getCompressionType()); 150 151 long prev = Long.MAX_VALUE; 152 for (int i = 10; i >= 0; i--) { 153 float quality = 0.1f * i; 154 writerParams.setCompressionQuality(quality); 155 156 long len = saveImage(image, writer, writerParams, 157 String.format("explicit-%.1f", quality), suffix); 158 159 if (len <= 0) { 160 throw new RuntimeException("zero file length !"); 161 } else if (len > prev) { 162 throw new RuntimeException("Incorrect file length: " + len 163 + " larger than previous: " + prev + " !"); 164 } 165 prev = len; 166 } 167 } 168 169 private static String selectCompressionType(final String suffix, 170 final String[] types) 171 { 172 switch (suffix) { 173 case "tif": 174 case "tiff": 175 return "LZW"; 176 default: 177 return types[0]; 178 } 179 } 180 181 private static long saveImage(final BufferedImage image, 182 final ImageWriter writer, 183 final ImageWriteParam writerParams, 184 final String mode, 185 final String suffix) throws IOException 186 { 187 final File imgFile = new File("WriterCompressionTest-" 188 + mode + '.' + suffix); 189 System.out.println("Writing file: " + imgFile.getAbsolutePath()); 190 191 final ImageOutputStream imgOutStream 192 = ImageIO.createImageOutputStream(new FileOutputStream(imgFile)); 193 try { 194 writer.setOutput(imgOutStream); 195 writer.write(null, new IIOImage(image, null, null), writerParams); 196 } finally { 197 imgOutStream.close(); 198 } 199 return imgFile.length(); 200 } 201 }