Path: blob/master/test/jdk/javax/imageio/plugins/bmp/EmbeddedFormatTest.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 6294920 629492626* @summary Test verifies that BMP images with compression types BI_JPEG and27* BI_PNG are read correctly in case of 1, 8, 16, 24 and 32 bpp28*/2930import java.awt.Color;31import java.awt.Graphics;32import java.awt.image.BufferedImage;33import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.IOException;3637import javax.imageio.IIOImage;38import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.stream.ImageInputStream;43import javax.imageio.stream.ImageOutputStream;4445public class EmbeddedFormatTest {46ImageWriter writer;47ImageReader reader;4849static int[] bi_types = {50BufferedImage.TYPE_INT_RGB,51BufferedImage.TYPE_3BYTE_BGR,52BufferedImage.TYPE_USHORT_555_RGB,53BufferedImage.TYPE_BYTE_GRAY,54BufferedImage.TYPE_BYTE_BINARY55};5657public EmbeddedFormatTest() {58writer = ImageIO.getImageWritersByFormatName("BMP").next();59reader = ImageIO.getImageReadersByFormatName("BMP").next();60}6162public void doTest(String compression, int bi_type) throws IOException {63System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));64BufferedImage src = createTestImage(bi_type);65writer.reset();6667ByteArrayOutputStream baos = new ByteArrayOutputStream();68ImageOutputStream ios =69ImageIO.createImageOutputStream(baos);70writer.setOutput(ios);7172ImageWriteParam wparam = prepareWriteParam(compression);73writer.write(null, new IIOImage(src, null, null), wparam);74ios.flush();75ios.close();7677// read result78ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());79ImageInputStream iis = ImageIO.createImageInputStream(bais);80reader.reset();81reader.setInput(iis);8283BufferedImage dst = reader.read(0);8485checkResult(dst);86}8788protected BufferedImage createTestImage(int type) {89BufferedImage img = new BufferedImage(200, 200, type);90Graphics g = img.createGraphics();91g.setColor(Color.black);92g.fillRect(0, 0, 200, 200);93g.setColor(Color.white);94g.fillRect(50, 50, 100, 100);9596return img;97}9899protected void checkResult(BufferedImage img) {100int imgBlack = img.getRGB(25, 25);101if (imgBlack != 0xff000000) {102throw new RuntimeException("Wrong black color: " +103Integer.toHexString(imgBlack));104}105106int imgWhite = img.getRGB(100, 100);107if (imgWhite != 0xffffffff) {108throw new RuntimeException("Wrong white color: " +109Integer.toHexString(imgWhite));110}111}112113protected ImageWriteParam prepareWriteParam(String compression) {114ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();115imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);116imageWriteParam.setCompressionType(compression);117return imageWriteParam;118}119120121public static void main(String[] args) throws IOException {122EmbeddedFormatTest t = new EmbeddedFormatTest();123124for (int i = 0; i < bi_types.length; i++) {125t.doTest("BI_JPEG", bi_types[i]);126t.doTest("BI_PNG", bi_types[i]);127}128}129130static String getImageTypeName(int type) {131switch(type) {132case BufferedImage.TYPE_INT_RGB:133return "TYPE_INT_RGB";134case BufferedImage.TYPE_3BYTE_BGR:135return "TYPE_3BYTE_BGR";136case BufferedImage.TYPE_USHORT_555_RGB:137return "TYPE_USHORT_555_RGB";138case BufferedImage.TYPE_BYTE_GRAY:139return "TYPE_BYTE_GRAY";140case BufferedImage.TYPE_BYTE_BINARY:141return "TYPE_BYTE_BINARY";142default:143return "TBD";144}145}146}147148149