1/* 2 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6297016 6294960 6294965 6294984 27 * @summary Test verifies that buffered images are written correctly if 28 * compression BI_BITFIELDS is used 29 */ 30 31import java.awt.Color; 32import java.awt.Graphics; 33import java.awt.Graphics2D; 34import java.awt.color.ColorSpace; 35import java.awt.image.BufferedImage; 36import java.io.ByteArrayInputStream; 37import java.io.ByteArrayOutputStream; 38import java.io.File; 39import java.io.FileOutputStream; 40import java.io.IOException; 41 42import javax.imageio.IIOImage; 43import javax.imageio.ImageIO; 44import javax.imageio.ImageReader; 45import javax.imageio.ImageWriteParam; 46import javax.imageio.ImageWriter; 47 48public class TestCompressionBI_BITFIELDS { 49 50 protected String format = "BMP"; 51 52 protected ImageReader reader; 53 54 protected ImageWriter writer; 55 56 protected boolean doSave = true; 57 58 Color[] colors = { 59 Color.red, Color.green, Color.blue, 60 Color.yellow, Color.white, Color.black}; 61 62 int dx = 50; 63 int h = 200; 64 65 public TestCompressionBI_BITFIELDS() { 66 this("BMP"); 67 } 68 69 public TestCompressionBI_BITFIELDS(String format) { 70 this.format = format; 71 reader = ImageIO.getImageReadersByFormatName(format).next(); 72 writer = ImageIO.getImageWritersByFormatName(format).next(); 73 } 74 75 protected ImageWriteParam prepareWriteParam(BufferedImage src) { 76 ImageWriteParam wparam = writer.getDefaultWriteParam(); 77 wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 78 wparam.setCompressionType("BI_BITFIELDS"); 79 80 return wparam; 81 } 82 83 public BufferedImage writeAndRead(BufferedImage src) throws IOException { 84 writer.reset(); 85 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 86 writer.setOutput(ImageIO.createImageOutputStream(baos)); 87 88 ImageWriteParam wparam = prepareWriteParam(src); 89 90 IIOImage img = new IIOImage(src, null, null); 91 92 writer.write(null, img, wparam); 93 94 if (doSave) { 95 // save images to file in order to be able to check 96 // that image is well-formed using standard windows tools. 97 File f = File.createTempFile("wr_test_", "." + format, new File(".")); 98 System.out.println("Save to file: " + f.getCanonicalPath()); 99 FileOutputStream fos = new FileOutputStream(f); 100 fos.write(baos.toByteArray()); 101 fos.flush(); 102 fos.close(); 103 } 104 105 // read result 106 reader.reset(); 107 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 108 reader.setInput(ImageIO.createImageInputStream(bais)); 109 110 return reader.read(0); 111 } 112 113 public static void main(String[] args) throws IOException { 114 // buffered image types listed below can be saved as BI_BITFIELDS BMP 115 int[] types = {BufferedImage.TYPE_3BYTE_BGR, 116 BufferedImage.TYPE_USHORT_555_RGB, 117 BufferedImage.TYPE_USHORT_565_RGB, 118 BufferedImage.TYPE_BYTE_GRAY, 119 BufferedImage.TYPE_BYTE_BINARY, 120 BufferedImage.TYPE_BYTE_INDEXED, 121 BufferedImage.TYPE_INT_BGR, 122 BufferedImage.TYPE_INT_RGB}; 123 124 for (int i = 0; i < types.length; i++) { 125 System.out.println("Test image " + types[i]); 126 TestCompressionBI_BITFIELDS t = new TestCompressionBI_BITFIELDS(); 127 128 BufferedImage src = 129 t.createTestImage(types[i]); 130 System.out.println("Image for test: " + src); 131 System.out.println("SampleModel: " + src.getSampleModel()); 132 133 BufferedImage dst = null; 134 try { 135 dst = t.writeAndRead(src); 136 } catch (IOException e) { 137 e.printStackTrace(System.out); 138 } 139 140 141 t.compareImages(src, dst); 142 } 143 } 144 145 protected BufferedImage createTestImage(int type) { 146 BufferedImage bimg = new BufferedImage(dx * colors.length, h, type); 147 Graphics2D g = bimg.createGraphics(); 148 149 for (int i = 0; i < colors.length; i++) { 150 g.setColor(colors[i]); 151 g.fillRect(dx * i, 0, dx, h); 152 } 153 return bimg; 154 } 155 156 protected void compareImages(BufferedImage src, BufferedImage dst) { 157 ColorSpace srcCS = src.getColorModel().getColorSpace(); 158 ColorSpace dstCS = dst.getColorModel().getColorSpace(); 159 if (!srcCS.equals(dstCS) && srcCS.getType() == ColorSpace.TYPE_GRAY) { 160 System.out.println("Workaround color difference with GRAY."); 161 BufferedImage tmp = 162 new BufferedImage(src.getWidth(), src.getHeight(), 163 BufferedImage.TYPE_INT_RGB); 164 Graphics g = tmp.createGraphics(); 165 g.drawImage(src, 0, 0, null); 166 src = tmp; 167 } 168 int y = h / 2; 169 for (int i = 0; i < colors.length; i++) { 170 int x = dx * i + dx / 2; 171 int srcRgb = src.getRGB(x, y); 172 int dstRgb = dst.getRGB(x, y); 173 174 if (srcRgb != dstRgb) { 175 throw new RuntimeException("Test failed due to color difference: " + 176 "src_pixel=" + Integer.toHexString(srcRgb) + 177 "dst_pixel=" + Integer.toHexString(dstRgb)); 178 } 179 } 180 } 181} 182