Path: blob/master/test/jdk/javax/imageio/plugins/bmp/RLECompressionTest.java
41153 views
/*1* Copyright (c) 2005, 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 633248026* @summary Test verifies that images encoded as BMP with RLE4 or RLE827* compression type are read correctly28*/2930import java.awt.Color;31import java.awt.Graphics2D;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.ImageOutputStream;4344public class RLECompressionTest {45public static final int TEST_RLE8 = 0x01;46public static final int TEST_RLE4 = 0x02;4748private static Color[] usedColors = new Color[] {49Color.black, Color.white, Color.red,50Color.green, Color.blue, Color.yellow };5152int w = 100;53// NB: problem occurs when image height > width54// The problem manifestation is that only first w55// lines of image are filled by decoded data,56// rest of image (all lines below (w-1)-th line)57// is leaved uninitialized (black).58// In order to verify that this problem is solved,59// we use image with height > width.60int h = 2 * w;6162private IndexColorModel getTestColorModel(int type) {63IndexColorModel icm = null;64int bpp = 8;65int size = 256;6667switch(type) {68case TEST_RLE8:69bpp = 8;70size = 256;71break;72case TEST_RLE4:73bpp = 4;74size = 16;75break;76default:77throw new IllegalArgumentException("Wrong test type: " + type);78}7980byte[] palette = new byte[size * 3];81for (int i = 0; i < usedColors.length; i++) {82palette[3 * i] = (byte)(0xff & usedColors[i].getRed());83palette[3 * i + 1] = (byte)(0xff & usedColors[i].getGreen());84palette[3 * i + 2] = (byte)(0xff & usedColors[i].getBlue());85}86// rest of palette is black8788icm = new IndexColorModel(bpp, size, palette, 0, false);89return icm;90}9192private BufferedImage getTestImage(int type) {93BufferedImage src = null;94IndexColorModel icm = getTestColorModel(type);95src = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);96Graphics2D g = src.createGraphics();97g.setColor(Color.white);98g.fillRect(0, 0, w, h);99g.dispose();100101return src;102}103104public void doTest(int type) throws IOException {105BufferedImage src = getTestImage(type);106107ByteArrayOutputStream baos = new ByteArrayOutputStream();108ImageOutputStream ios = ImageIO.createImageOutputStream(baos);109110ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();111writer.setOutput(ios);112113ImageWriteParam wparam = writer.getDefaultWriteParam();114wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);115switch(type) {116case TEST_RLE8:117wparam.setCompressionType("BI_RLE8");118break;119case TEST_RLE4:120wparam.setCompressionType("BI_RLE4");121break;122default:123throw new IllegalArgumentException("Wrong test type: " + type);124}125126writer.write(null, new IIOImage(src, null, null), wparam);127128ios.close();129baos.close();130131// read result132ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());133134BufferedImage dst = ImageIO.read(bais);135136checkResult(src, dst);137}138139private void checkResult(BufferedImage src, BufferedImage dst) {140int x = w / 2;141for (int y = 0; y < h; y++) {142int srcRgb = src.getRGB(x, y);143int dstRgb = dst.getRGB(x, y);144145if (srcRgb != dstRgb) {146throw new RuntimeException("Test failed due to color difference: " +147Integer.toHexString(dstRgb) + " instead of " + Integer.toHexString(srcRgb) +148" at [" + x + ", " + y + "]");149}150}151}152153public static void main(String[] args) throws IOException {154RLECompressionTest test = new RLECompressionTest();155test.doTest(TEST_RLE8);156test.doTest(TEST_RLE4);157}158}159160161