1 /* 2 * Copyright (c) 2003, 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 4892214 27 * @summary Test checks that colors are not changed by the writing/reading in 28 * the BMP format for TYPE_INT_BGR and TYPE_USHORT_555_RGB buffered 29 * images 30 */ 31 32 import java.awt.Color; 33 import java.awt.Dimension; 34 import java.awt.Graphics; 35 import java.awt.Graphics2D; 36 import java.awt.image.BufferedImage; 37 import java.io.ByteArrayInputStream; 38 import java.io.ByteArrayOutputStream; 39 import java.io.IOException; 40 41 import javax.imageio.ImageIO; 42 import javax.imageio.ImageWriter; 43 import javax.imageio.stream.ImageOutputStream; 44 import javax.swing.JComponent; 45 import javax.swing.JFrame; 46 47 public class WritingColorChangeTest { 48 private static int width = 100; 49 private static int height = 100; 50 private static Color color = new Color(0x10, 0x20, 0x30); 51 52 static int bufferedImageType[] = { 53 BufferedImage.TYPE_USHORT_565_RGB, 54 BufferedImage.TYPE_INT_BGR, 55 BufferedImage.TYPE_INT_RGB, 56 BufferedImage.TYPE_USHORT_555_RGB, 57 }; 58 59 static String bufferedImageStringType[] = { 60 "BufferedImage.TYPE_USHORT_565_RGB", 61 "BufferedImage.TYPE_INT_BGR", 62 "BufferedImage.TYPE_INT_RGB", 63 "BufferedImage.TYPE_USHORT_555_RGB", 64 }; 65 private static String writingFormat = "BMP"; 66 private static ImageWriter writer = (ImageWriter)ImageIO.getImageWritersByFormatName(writingFormat).next(); 67 private int type; 68 69 public static void main(String[] args) { 70 71 //int i = 7; //3; //7; 72 for(int i=0; i<bufferedImageType.length; i++) { 73 System.out.println("\n\nTest for type " + bufferedImageStringType[i]); 74 75 WritingColorChangeTest t1 = new WritingColorChangeTest(bufferedImageType[i]); 76 t1.doTest(); 77 78 } 79 } 80 81 private WritingColorChangeTest(int type) { 82 this.type = type; 83 } 84 85 private void doTest() { 86 BufferedImage src = createTestImage(type); 87 System.out.println("Sample model is " + src.getSampleModel()); 88 89 BufferedImage dst = doModification(src); 90 91 Object dstPixel = dst.getRaster().getDataElements(width/2, height/2, null); 92 Object srcPixel = src.getRaster().getDataElements(width/2, height/2, null); 93 94 if (src.getType() == BufferedImage.TYPE_USHORT_555_RGB || 95 src.getType() == BufferedImage.TYPE_USHORT_565_RGB ) { 96 97 Color cmpColor = new Color(dst.getColorModel().getRed(dstPixel), 98 dst.getColorModel().getGreen(dstPixel), 99 dst.getColorModel().getBlue(dstPixel)); 100 BufferedImage cmp = createTestImage(src.getType(), cmpColor); 101 102 Object cmpPixel = cmp.getRaster().getDataElements(width/2, height/2, null); 103 104 dst = cmp; 105 dstPixel = cmpPixel; 106 } 107 108 if ( (src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) || 109 (src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) || 110 (src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) || 111 (src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel)) ) { 112 113 showPixel(src, width/2, height/2); 114 showPixel(dst, width/2, height/2); 115 116 showRes(dst, src); 117 throw new RuntimeException( 118 "Colors are different: " + 119 Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " + 120 Integer.toHexString(dst.getColorModel().getRGB(dstPixel))); 121 } 122 } 123 124 private BufferedImage doModification(BufferedImage src) { 125 try { 126 BufferedImage dst = null; 127 if (!writer.getOriginatingProvider().canEncodeImage(src)) { 128 throw new RuntimeException(writingFormat+" writer does not support the image type "+type); 129 } 130 System.out.println(writingFormat+" writer claims it can encode the image "+type); 131 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 132 ImageOutputStream ios = ImageIO.createImageOutputStream(baos); 133 writer.setOutput(ios); 134 writer.write(src); 135 ios.close(); 136 baos.close(); 137 138 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 139 dst = ImageIO.read(bais); 140 return dst; 141 } catch (IOException e) { 142 throw new RuntimeException(e); 143 } 144 } 145 146 private static void showPixel(BufferedImage src, int x, int y) { 147 System.out.println("Img is " + src); 148 Object p = src.getRaster().getDataElements(x, y, null); 149 System.out.println("RGB: " + 150 Integer.toHexString(src.getColorModel().getRGB(p))); 151 System.out.println("Red: " + 152 Integer.toHexString(src.getColorModel().getRed(p))); 153 System.out.println("Green: " + 154 Integer.toHexString(src.getColorModel().getGreen(p))); 155 System.out.println("Blue: " + 156 Integer.toHexString(src.getColorModel().getBlue(p))); 157 System.out.println("Alpha: " + 158 Integer.toHexString(src.getColorModel().getAlpha(p))); 159 } 160 161 private static BufferedImage createTestImage(int type) { 162 return createTestImage(type, color); 163 } 164 165 private static BufferedImage createTestImage(int type, Color c) { 166 BufferedImage i = new BufferedImage(width, height, 167 type); 168 Graphics2D g = i.createGraphics(); 169 170 g.setColor(c); 171 g.fillRect(0, 0, width, height); 172 173 return i; 174 } 175 176 private static void showRes(final BufferedImage src, final BufferedImage dst) { 177 final int w = src.getWidth()+ dst.getWidth(); 178 final int h = Math.max(src.getHeight(), dst.getHeight()); 179 180 JFrame f = new JFrame("Test results"); 181 f.getContentPane().add( new JComponent() { 182 public Dimension getPreferredSize() { 183 return new Dimension(w,h); 184 } 185 186 public void paintComponent(Graphics g) { 187 g.drawImage(src,0,0, null); 188 g.drawImage(dst, src.getWidth(),0, null); 189 } 190 }); 191 f.pack(); 192 f.setVisible(true); 193 } 194 }