Path: blob/master/test/jdk/javax/imageio/plugins/bmp/CompressionModeTest.java
41153 views
/*1* Copyright (c) 2003, 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 489346426* @summary Tests bmp writer behavior with different compression modes27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.io.File;3334import javax.imageio.IIOImage;35import javax.imageio.ImageIO;36import javax.imageio.ImageTypeSpecifier;37import javax.imageio.ImageWriteParam;38import javax.imageio.ImageWriter;39import javax.imageio.metadata.IIOMetadata;40import javax.imageio.stream.ImageOutputStream;4142public class CompressionModeTest {4344public static void main(String args[]) {45int[] iModes = { ImageWriteParam.MODE_DISABLED,46ImageWriteParam.MODE_EXPLICIT,47ImageWriteParam.MODE_COPY_FROM_METADATA,48ImageWriteParam.MODE_DEFAULT };4950String[] strModes = { "ImageWriteParam.MODE_DISABLED",51"ImageWriteParam.MODE_EXPLICIT",52"ImageWriteParam.MODE_COPY_FROM_METADATA",53"ImageWriteParam.MODE_DEFAULT" };5455for(int i=0; i<iModes.length; i++) {56System.out.println("Test compression mode "+strModes[i]);57doTest(iModes[i]);58}59}6061private static void doTest(int mode) {62String fileFormat = "bmp";63try {64ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix(fileFormat).next();65if(iw == null) {66throw new RuntimeException("No available image writer for "67+ fileFormat68+ " Test failed.");69}7071File file = new File("image." + fileFormat);72ImageOutputStream ios = ImageIO.createImageOutputStream(file);73iw.setOutput(ios);7475BufferedImage bimg = new BufferedImage(100,76100, BufferedImage.TYPE_INT_RGB);77Graphics g = bimg.getGraphics();78g.setColor(Color.green);79g.fillRect(0,0,100,100);8081ImageWriteParam param = iw.getDefaultWriteParam();8283param.setCompressionMode(mode);8485IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg),86param);8788IIOImage iioImg = new IIOImage(bimg, null, meta);89iw.write(null, iioImg, param);90} catch(Exception e) {91e.printStackTrace();92throw new RuntimeException("Test failed.");93}94}95}969798