1 /* 2 * Copyright (c) 2016, 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 7116979 27 * @summary Test verifies whether BufferedImage with primary colors are 28 * stored properly when we draw into ByteIndexed BufferedImage. 29 * @run main ConvertToByteIndexedTest 30 */ 31 32 import java.awt.Color; 33 import java.awt.Graphics2D; 34 import java.awt.image.BufferedImage; 35 import java.util.HashMap; 36 37 public class ConvertToByteIndexedTest { 38 static final int[] SRC_TYPES = new int[] { 39 BufferedImage.TYPE_INT_RGB, 40 BufferedImage.TYPE_INT_ARGB, 41 BufferedImage.TYPE_INT_ARGB_PRE, 42 BufferedImage.TYPE_INT_BGR, 43 BufferedImage.TYPE_3BYTE_BGR, 44 BufferedImage.TYPE_4BYTE_ABGR, 45 BufferedImage.TYPE_4BYTE_ABGR_PRE, 46 BufferedImage.TYPE_USHORT_565_RGB, 47 BufferedImage.TYPE_USHORT_555_RGB, 48 BufferedImage.TYPE_BYTE_INDEXED}; 49 50 static final String[] TYPE_NAME = new String[] { 51 "INT_RGB", 52 "INT_ARGB", 53 "INT_ARGB_PRE", 54 "INT_BGR", 55 "3BYTE_BGR", 56 "4BYTE_ABGR", 57 "4BYTE_ABGR_PRE", 58 "USHORT_565_RGB", 59 "USHORT_555_RGB", 60 "BYTE_INDEXED"}; 61 62 static final Color[] COLORS = new Color[] { 63 //Color.WHITE, 64 Color.BLACK, 65 Color.RED, 66 Color.YELLOW, 67 Color.GREEN, 68 Color.MAGENTA, 69 Color.CYAN, 70 Color.BLUE}; 71 72 static final HashMap<Integer,String> TYPE_TABLE = 73 new HashMap<Integer,String>(); 74 75 static { 76 for (int i = 0; i < SRC_TYPES.length; i++) { 77 TYPE_TABLE.put(new Integer(SRC_TYPES[i]), TYPE_NAME[i]); 78 } 79 } 80 81 static int width = 50; 82 static int height = 50; 83 84 public static void ConvertToByteIndexed(Color color, int srcType) { 85 // setup source image and graphics for conversion. 86 BufferedImage srcImage = new BufferedImage(width, height, srcType); 87 Graphics2D srcG2D = srcImage.createGraphics(); 88 srcG2D.setColor(color); 89 srcG2D.fillRect(0, 0, width, height); 90 91 // setup destination image and graphics for conversion. 92 int dstType = BufferedImage.TYPE_BYTE_INDEXED; 93 BufferedImage dstImage = new BufferedImage(width, height, dstType); 94 Graphics2D dstG2D = (Graphics2D)dstImage.getGraphics(); 95 // draw source image into Byte Indexed destination 96 dstG2D.drawImage(srcImage, 0, 0, null); 97 98 // draw into ARGB image to verify individual pixel value. 99 BufferedImage argbImage = new BufferedImage(width, height, 100 BufferedImage.TYPE_INT_ARGB); 101 Graphics2D argbG2D = (Graphics2D)argbImage.getGraphics(); 102 argbG2D.drawImage(dstImage, 0, 0, null); 103 104 for (int i = 0; i < width; i++) { 105 for (int j = 0; j < height; j++) { 106 if (color.getRGB() != argbImage.getRGB(i, j)) { 107 throw new RuntimeException("Conversion from " + 108 TYPE_TABLE.get(srcType) + " to BYTE_INDEXED is not" 109 + " done properly for " + color); 110 } 111 } 112 } 113 } 114 115 public static void main(String args[]) { 116 for (int srcType : SRC_TYPES) { 117 for (Color color : COLORS) { 118 ConvertToByteIndexed(color, srcType); 119 } 120 } 121 } 122 }