Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/CompressionBug.java
41152 views
/*1* Copyright (c) 2001, 2017, 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*/2223/*24* @test25* @bug 4415068 462220126* @summary Tests if the JPEG writer responds to the compression quality setting27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.io.File;33import java.io.IOException;34import java.util.Iterator;35import java.util.Random;3637import javax.imageio.IIOImage;38import javax.imageio.ImageIO;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.stream.ImageOutputStream;4344public class CompressionBug {4546public CompressionBug() throws IOException {47File fileHighComp = File.createTempFile("CompressionHigh", ".jpg");48File fileLowComp = File.createTempFile("CompressionLow", ".jpg");4950fileHighComp.deleteOnExit();51fileLowComp.deleteOnExit();5253ImageOutputStream iosHighComp =54ImageIO.createImageOutputStream(fileHighComp);55ImageOutputStream iosLowComp =56ImageIO.createImageOutputStream(fileLowComp);5758int width = 100;59int height = 100;60BufferedImage bi =61new BufferedImage(width, height,62BufferedImage.TYPE_INT_RGB);63Graphics g = bi.createGraphics();64Random r = new Random();65for (int i = 0; i < 100; i++) {66Color c = new Color(r.nextInt(256),67r.nextInt(256),68r.nextInt(256));69int x = r.nextInt(width);70int y = r.nextInt(height);71int w = r.nextInt(width - x);72int h = r.nextInt(height - y);73g.setColor(c);74g.fillRect(x, y, w, h);75}7677ImageTypeSpecifier typeSpecifier =78new ImageTypeSpecifier(bi.getColorModel(),79bi.getSampleModel());8081ImageWriter writer = null;82Iterator iter = ImageIO.getImageWriters(typeSpecifier,"jpeg");83while (iter.hasNext()) {84writer = (ImageWriter)iter.next();85break;86}8788IIOImage iioImg = new IIOImage(bi, null, null);89ImageWriteParam wParam = writer.getDefaultWriteParam();90wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);9192// write the highly compressed image (a compression quality setting of93// 0.1f means low visual quality and small file size)94wParam.setCompressionQuality(0.1f);95writer.setOutput(iosHighComp);96writer.write(null, iioImg, wParam);9798// write the somewhat compressed image (a compression quality setting99// of 0.9f means high visual quality and large file size)100wParam.setCompressionQuality(0.9f);101writer.setOutput(iosLowComp);102writer.write(null, iioImg, wParam);103104long sizeOfFileLowComp = fileLowComp.length();105long sizeOfFileHighComp = fileHighComp.length();106107// the highly compressed image file should have a smaller file size108// than the image file with low compression; throw an exception if109// this isn't the case110if (sizeOfFileLowComp < sizeOfFileHighComp) {111throw new RuntimeException("Lower compression quality did not " +112"reduce file size!");113}114}115116public static void main(String args[]) throws IOException {117new CompressionBug();118}119}120121122