Path: blob/master/test/jdk/javax/imageio/plugins/shared/ImageWriterCompressionTest.java
41153 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Color;24import java.awt.Font;25import java.awt.Graphics2D;26import java.awt.RenderingHints;27import java.awt.geom.Rectangle2D;28import java.awt.image.BufferedImage;29import java.io.File;30import java.io.FileOutputStream;31import java.io.IOException;32import java.util.Arrays;33import java.util.HashSet;34import java.util.Iterator;35import java.util.Locale;36import java.util.Set;37import javax.imageio.IIOImage;38import javax.imageio.ImageIO;39import javax.imageio.ImageWriteParam;40import javax.imageio.ImageWriter;41import javax.imageio.stream.ImageOutputStream;4243/**44* @test45* @bug 648852246* @summary Check the compression support in imageio ImageWriters47* @run main ImageWriterCompressionTest48*/49public class ImageWriterCompressionTest {5051// ignore jpg (fail):52// Caused by: javax.imageio.IIOException: Invalid argument to native writeImage53private static final Set<String> IGNORE_FILE_SUFFIXES54= new HashSet<String>(Arrays.asList(new String[] {55"bmp", "gif",56"jpg", "jpeg"57} ));5859public static void main(String[] args) {60Locale.setDefault(Locale.US);6162final BufferedImage image63= new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);6465final Graphics2D g2d = image.createGraphics();66try {67g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,68RenderingHints.VALUE_ANTIALIAS_ON);69g2d.setRenderingHint(RenderingHints.KEY_RENDERING,70RenderingHints.VALUE_RENDER_QUALITY);71g2d.scale(2.0, 2.0);7273g2d.setColor(Color.red);74g2d.draw(new Rectangle2D.Float(10, 10, 100, 100));75g2d.setColor(Color.blue);76g2d.fill(new Rectangle2D.Float(12, 12, 98, 98));77g2d.setColor(Color.green);78g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14));7980for (int i = 0; i < 15; i++) {81g2d.drawString("Testing Compression ...", 20, 20 + i * 16);82}8384final String[] fileSuffixes = ImageIO.getWriterFileSuffixes();8586final Set<String> testedWriterClasses = new HashSet<String>();8788for (String suffix : fileSuffixes) {8990if (!IGNORE_FILE_SUFFIXES.contains(suffix)) {91final Iterator<ImageWriter> itWriters92= ImageIO.getImageWritersBySuffix(suffix);9394final ImageWriter writer;95final ImageWriteParam writerParams;9697if (itWriters.hasNext()) {98writer = itWriters.next();99100if (testedWriterClasses.add(writer.getClass().getName())) {101writerParams = writer.getDefaultWriteParam();102103if (writerParams.canWriteCompressed()) {104testCompression(image, writer, writerParams, suffix);105}106}107} else {108throw new RuntimeException("Unable to get writer !");109}110}111}112} catch (IOException ioe) {113throw new RuntimeException("IO failure", ioe);114}115finally {116g2d.dispose();117}118}119120private static void testCompression(final BufferedImage image,121final ImageWriter writer,122final ImageWriteParam writerParams,123final String suffix)124throws IOException125{126System.out.println("Compression types: "127+ Arrays.toString(writerParams.getCompressionTypes()));128129// Test Compression modes:130try {131writerParams.setCompressionMode(ImageWriteParam.MODE_DISABLED);132saveImage(image, writer, writerParams, "disabled", suffix);133} catch (Exception e) {134System.out.println("CompressionMode Disabled not supported: "+ e.getMessage());135}136137try {138writerParams.setCompressionMode(ImageWriteParam.MODE_DEFAULT);139saveImage(image, writer, writerParams, "default", suffix);140} catch (Exception e) {141System.out.println("CompressionMode Default not supported: "+ e.getMessage());142}143144writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);145writerParams.setCompressionType(selectCompressionType(suffix,146writerParams.getCompressionTypes()));147148System.out.println("Selected Compression type: "149+ writerParams.getCompressionType());150151long prev = Long.MAX_VALUE;152for (int i = 10; i >= 0; i--) {153float quality = 0.1f * i;154writerParams.setCompressionQuality(quality);155156long len = saveImage(image, writer, writerParams,157String.format("explicit-%.1f", quality), suffix);158159if (len <= 0) {160throw new RuntimeException("zero file length !");161} else if (len > prev) {162throw new RuntimeException("Incorrect file length: " + len163+ " larger than previous: " + prev + " !");164}165prev = len;166}167}168169private static String selectCompressionType(final String suffix,170final String[] types)171{172switch (suffix) {173case "tif":174case "tiff":175return "LZW";176default:177return types[0];178}179}180181private static long saveImage(final BufferedImage image,182final ImageWriter writer,183final ImageWriteParam writerParams,184final String mode,185final String suffix) throws IOException186{187final File imgFile = new File("WriterCompressionTest-"188+ mode + '.' + suffix);189System.out.println("Writing file: " + imgFile.getAbsolutePath());190191final ImageOutputStream imgOutStream192= ImageIO.createImageOutputStream(new FileOutputStream(imgFile));193try {194writer.setOutput(imgOutStream);195writer.write(null, new IIOImage(image, null, null), writerParams);196} finally {197imgOutStream.close();198}199return imgFile.length();200}201}202203204