Path: blob/master/test/jdk/javax/imageio/plugins/bmp/RleEncodingTest.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 489344626* @summary Tests that we get IOException if we try to encode the incompatible27* image with RLE compression28*/2930import java.awt.Color;31import java.awt.Graphics;32import java.awt.image.BufferedImage;33import java.awt.image.IndexColorModel;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.IOException;3738import javax.imageio.IIOImage;39import javax.imageio.ImageIO;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.stream.ImageInputStream;43import javax.imageio.stream.ImageOutputStream;4445public class RleEncodingTest {4647private static int testIdx = 1;4849public static void main(String args[]) throws Exception {50try {51int mode = ImageWriteParam.MODE_EXPLICIT;52String type = "BI_RLE4";53doTest(type, mode);5455type = "BI_RLE8";56doTest(type, mode);5758mode = ImageWriteParam.MODE_DEFAULT;59type = "BI_RLE4";60doTest(type, mode);6162type = "BI_RLE8";63doTest(type, mode);6465System.out.println("Test 4bpp image.");66encodeRLE4Test();6768System.out.println("Test 8bpp image.");69encodeRLE8Test();70} catch (IOException e) {71e.printStackTrace();72throw new RuntimeException("Unexpected exception. Test failed");73}74}7576private static void doTest(String compressionType,77int compressionMode) throws IOException78{79BufferedImage bimg = new BufferedImage(100, 100,80BufferedImage.TYPE_INT_RGB);81Graphics g = bimg.getGraphics();82g.setColor(Color.green);83g.fillRect(0, 0, 100, 100);8485doTest(bimg, compressionType, compressionMode);86}8788private static void encodeRLE4Test() throws IOException {89// create 4bpp image90byte[] r = new byte[16];91r[0] = (byte)0xff;92byte[] g = new byte[16];93g[1] = (byte)0xff;94byte[] b = new byte[16];95b[2] = (byte)0xff;96IndexColorModel icm = new IndexColorModel(4, 16, r, g, b);9798BufferedImage bimg = new BufferedImage(100, 100,99BufferedImage.TYPE_BYTE_BINARY,100icm);101102Graphics gr = bimg.getGraphics();103gr.setColor(Color.green);104gr.fillRect(0, 0, 100, 100);105106doTest(bimg, "BI_RLE4", ImageWriteParam.MODE_EXPLICIT);107}108109private static void encodeRLE8Test() throws IOException {110// create 8bpp image111byte[] r = new byte[256];112r[0] = (byte)0xff;113byte[] g = new byte[256];114g[1] = (byte)0xff;115byte[] b = new byte[256];116b[2] = (byte)0xff;117IndexColorModel icm = new IndexColorModel(8, 256, r, g, b);118119BufferedImage bimg = new BufferedImage(100, 100,120BufferedImage.TYPE_BYTE_INDEXED,121icm);122Graphics gr = bimg.getGraphics();123gr.setColor(Color.green);124gr.fillRect(0, 0, 100, 100);125126doTest(bimg, "BI_RLE8", ImageWriteParam.MODE_EXPLICIT);127}128129private static void doTest(BufferedImage src,130String compressionType,131int compressionMode) throws IOException132{133134ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix("bmp").next();135if (iw == null) {136throw new RuntimeException("No available writer. Test failed.");137}138139IIOImage iioImg = new IIOImage(src, null, null);140ImageWriteParam param = iw.getDefaultWriteParam();141142143ByteArrayOutputStream baos = new ByteArrayOutputStream();144ImageOutputStream ios = ImageIO.createImageOutputStream(baos);145iw.setOutput(ios);146147System.out.println("Compression Type is " + compressionType);148System.out.println("Compression Mode is " + compressionMode);149150param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);151param.setCompressionType(compressionType);152if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {153param.setCompressionMode(compressionMode);154}155try {156iw.write(null, iioImg, param);157} catch (IOException e) {158int bpp = src.getColorModel().getPixelSize();159if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {160if ((compressionType.equals("BI_RLE4") && bpp != 4)161|| (compressionType.equals("BI_RLE8") && bpp != 8))162{163System.out.println("Can not encode "+ bpp+ "bpp image as"164+ compressionType);165return;166} else {167throw new RuntimeException("Unable to encode "168+ bpp + "bpp image as "169+ compressionType170+ ". Test failed");171}172}173}174baos.close();175176ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());177ImageInputStream iis = ImageIO.createImageInputStream(bais);178179BufferedImage dst = ImageIO.read(iis);180181int w = src.getWidth();182int h = src.getHeight();183184Object dstPixel = dst.getRaster().getDataElements(w/2, h/2, null);185Object srcPixel = src.getRaster().getDataElements(w/2, h/2, null);186187if ( (src.getColorModel().getRed(srcPixel)188!= dst.getColorModel().getRed(dstPixel))189|| (src.getColorModel().getGreen(srcPixel)190!= dst.getColorModel().getGreen(dstPixel))191|| (src.getColorModel().getBlue(srcPixel)192!= dst.getColorModel().getBlue(dstPixel))193|| (src.getColorModel().getAlpha(srcPixel)194!= dst.getColorModel().getAlpha(dstPixel)) ) {195196showPixel(src, w/2, h/2);197showPixel(dst, w/2, h/2);198199throw new RuntimeException(200"Colors are different: " +201Integer.toHexString(src.getColorModel().getRGB(srcPixel))202+ " and " +203Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));204}205206}207208private static void showPixel(BufferedImage src, int x, int y) {209System.out.println("Img is " + src);210Object p = src.getRaster().getDataElements(x, y, null);211System.out.println("RGB: " +212Integer.toHexString(src.getColorModel().getRGB(p)));213System.out.println("Red: " +214Integer.toHexString(src.getColorModel().getRed(p)));215System.out.println("Green: " +216Integer.toHexString(src.getColorModel().getGreen(p)));217System.out.println("Blue: " +218Integer.toHexString(src.getColorModel().getBlue(p)));219System.out.println("Alpha: " +220Integer.toHexString(src.getColorModel().getAlpha(p)));221}222}223224225