Path: blob/master/test/jdk/javax/imageio/plugins/bmp/TestCompressionBI_BITFIELDS.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 6297016 6294960 6294965 629498426* @summary Test verifies that buffered images are written correctly if27* compression BI_BITFIELDS is used28*/2930import java.awt.Color;31import java.awt.Graphics;32import java.awt.Graphics2D;33import java.awt.color.ColorSpace;34import java.awt.image.BufferedImage;35import java.io.ByteArrayInputStream;36import java.io.ByteArrayOutputStream;37import java.io.File;38import java.io.FileOutputStream;39import java.io.IOException;4041import javax.imageio.IIOImage;42import javax.imageio.ImageIO;43import javax.imageio.ImageReader;44import javax.imageio.ImageWriteParam;45import javax.imageio.ImageWriter;4647public class TestCompressionBI_BITFIELDS {4849protected String format = "BMP";5051protected ImageReader reader;5253protected ImageWriter writer;5455protected boolean doSave = true;5657Color[] colors = {58Color.red, Color.green, Color.blue,59Color.yellow, Color.white, Color.black};6061int dx = 50;62int h = 200;6364public TestCompressionBI_BITFIELDS() {65this("BMP");66}6768public TestCompressionBI_BITFIELDS(String format) {69this.format = format;70reader = ImageIO.getImageReadersByFormatName(format).next();71writer = ImageIO.getImageWritersByFormatName(format).next();72}7374protected ImageWriteParam prepareWriteParam(BufferedImage src) {75ImageWriteParam wparam = writer.getDefaultWriteParam();76wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);77wparam.setCompressionType("BI_BITFIELDS");7879return wparam;80}8182public BufferedImage writeAndRead(BufferedImage src) throws IOException {83writer.reset();84ByteArrayOutputStream baos = new ByteArrayOutputStream();85writer.setOutput(ImageIO.createImageOutputStream(baos));8687ImageWriteParam wparam = prepareWriteParam(src);8889IIOImage img = new IIOImage(src, null, null);9091writer.write(null, img, wparam);9293if (doSave) {94// save images to file in order to be able to check95// that image is well-formed using standard windows tools.96File f = File.createTempFile("wr_test_", "." + format, new File("."));97System.out.println("Save to file: " + f.getCanonicalPath());98FileOutputStream fos = new FileOutputStream(f);99fos.write(baos.toByteArray());100fos.flush();101fos.close();102}103104// read result105reader.reset();106ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());107reader.setInput(ImageIO.createImageInputStream(bais));108109return reader.read(0);110}111112public static void main(String[] args) throws IOException {113// buffered image types listed below can be saved as BI_BITFIELDS BMP114int[] types = {BufferedImage.TYPE_3BYTE_BGR,115BufferedImage.TYPE_USHORT_555_RGB,116BufferedImage.TYPE_USHORT_565_RGB,117BufferedImage.TYPE_BYTE_GRAY,118BufferedImage.TYPE_BYTE_BINARY,119BufferedImage.TYPE_BYTE_INDEXED,120BufferedImage.TYPE_INT_BGR,121BufferedImage.TYPE_INT_RGB};122123for (int i = 0; i < types.length; i++) {124System.out.println("Test image " + types[i]);125TestCompressionBI_BITFIELDS t = new TestCompressionBI_BITFIELDS();126127BufferedImage src =128t.createTestImage(types[i]);129System.out.println("Image for test: " + src);130System.out.println("SampleModel: " + src.getSampleModel());131132BufferedImage dst = null;133try {134dst = t.writeAndRead(src);135} catch (IOException e) {136e.printStackTrace(System.out);137}138139140t.compareImages(src, dst);141}142}143144protected BufferedImage createTestImage(int type) {145BufferedImage bimg = new BufferedImage(dx * colors.length, h, type);146Graphics2D g = bimg.createGraphics();147148for (int i = 0; i < colors.length; i++) {149g.setColor(colors[i]);150g.fillRect(dx * i, 0, dx, h);151}152return bimg;153}154155protected void compareImages(BufferedImage src, BufferedImage dst) {156ColorSpace srcCS = src.getColorModel().getColorSpace();157ColorSpace dstCS = dst.getColorModel().getColorSpace();158if (!srcCS.equals(dstCS) && srcCS.getType() == ColorSpace.TYPE_GRAY) {159System.out.println("Workaround color difference with GRAY.");160BufferedImage tmp =161new BufferedImage(src.getWidth(), src.getHeight(),162BufferedImage.TYPE_INT_RGB);163Graphics g = tmp.createGraphics();164g.drawImage(src, 0, 0, null);165src = tmp;166}167int y = h / 2;168for (int i = 0; i < colors.length; i++) {169int x = dx * i + dx / 2;170int srcRgb = src.getRGB(x, y);171int dstRgb = dst.getRGB(x, y);172173if (srcRgb != dstRgb) {174throw new RuntimeException("Test failed due to color difference: " +175"src_pixel=" + Integer.toHexString(srcRgb) +176"dst_pixel=" + Integer.toHexString(dstRgb));177}178}179}180}181182183