Path: blob/master/test/jdk/sun/java2d/loops/ConvertToByteIndexedTest.java
41149 views
/*1* Copyright (c) 2016, 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 711697926* @summary Test verifies whether BufferedImage with primary colors are27* stored properly when we draw into ByteIndexed BufferedImage.28* @run main ConvertToByteIndexedTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.image.BufferedImage;34import java.util.HashMap;3536public class ConvertToByteIndexedTest {37static final int[] SRC_TYPES = new int[] {38BufferedImage.TYPE_INT_RGB,39BufferedImage.TYPE_INT_ARGB,40BufferedImage.TYPE_INT_ARGB_PRE,41BufferedImage.TYPE_INT_BGR,42BufferedImage.TYPE_3BYTE_BGR,43BufferedImage.TYPE_4BYTE_ABGR,44BufferedImage.TYPE_4BYTE_ABGR_PRE,45BufferedImage.TYPE_USHORT_565_RGB,46BufferedImage.TYPE_USHORT_555_RGB,47BufferedImage.TYPE_BYTE_INDEXED};4849static final String[] TYPE_NAME = new String[] {50"INT_RGB",51"INT_ARGB",52"INT_ARGB_PRE",53"INT_BGR",54"3BYTE_BGR",55"4BYTE_ABGR",56"4BYTE_ABGR_PRE",57"USHORT_565_RGB",58"USHORT_555_RGB",59"BYTE_INDEXED"};6061static final Color[] COLORS = new Color[] {62//Color.WHITE,63Color.BLACK,64Color.RED,65Color.YELLOW,66Color.GREEN,67Color.MAGENTA,68Color.CYAN,69Color.BLUE};7071static final HashMap<Integer,String> TYPE_TABLE =72new HashMap<Integer,String>();7374static {75for (int i = 0; i < SRC_TYPES.length; i++) {76TYPE_TABLE.put(new Integer(SRC_TYPES[i]), TYPE_NAME[i]);77}78}7980static int width = 50;81static int height = 50;8283public static void ConvertToByteIndexed(Color color, int srcType) {84// setup source image and graphics for conversion.85BufferedImage srcImage = new BufferedImage(width, height, srcType);86Graphics2D srcG2D = srcImage.createGraphics();87srcG2D.setColor(color);88srcG2D.fillRect(0, 0, width, height);8990// setup destination image and graphics for conversion.91int dstType = BufferedImage.TYPE_BYTE_INDEXED;92BufferedImage dstImage = new BufferedImage(width, height, dstType);93Graphics2D dstG2D = (Graphics2D)dstImage.getGraphics();94// draw source image into Byte Indexed destination95dstG2D.drawImage(srcImage, 0, 0, null);9697// draw into ARGB image to verify individual pixel value.98BufferedImage argbImage = new BufferedImage(width, height,99BufferedImage.TYPE_INT_ARGB);100Graphics2D argbG2D = (Graphics2D)argbImage.getGraphics();101argbG2D.drawImage(dstImage, 0, 0, null);102103for (int i = 0; i < width; i++) {104for (int j = 0; j < height; j++) {105if (color.getRGB() != argbImage.getRGB(i, j)) {106throw new RuntimeException("Conversion from " +107TYPE_TABLE.get(srcType) + " to BYTE_INDEXED is not"108+ " done properly for " + color);109}110}111}112}113114public static void main(String args[]) {115for (int srcType : SRC_TYPES) {116for (Color color : COLORS) {117ConvertToByteIndexed(color, srcType);118}119}120}121}122123124