/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 7116979 * @summary Test verifies whether image having black pixels is converted * properly from all formats to Byte Indexed format. * @run main ConvertToByteIndexedTest */ import java.awt.*; import java.awt.image.*; public class ConvertToByteIndexedTest { //Array for all buffer image types static final int[] types = new int[]{ BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE, BufferedImage.TYPE_INT_BGR, BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR, BufferedImage.TYPE_4BYTE_ABGR_PRE, BufferedImage.TYPE_USHORT_565_RGB, BufferedImage.TYPE_USHORT_555_RGB, BufferedImage.TYPE_BYTE_GRAY, BufferedImage.TYPE_USHORT_GRAY, BufferedImage.TYPE_BYTE_BINARY, BufferedImage.TYPE_BYTE_INDEXED}; //Dimensions of image static int width = 50; static int height = 50; //Function to convert different image formats to Byte_Indexed public void convertImage(int type1, int type2){ //Create input image with type1 format BufferedImage type1Image = new BufferedImage(width, height, type1); Graphics2D type1G2D = type1Image.createGraphics(); type1G2D.setColor(Color.BLACK); type1G2D.fillRect(0, 0, type1Image.getWidth(), type1Image.getHeight()); //Draw type2 image with type 1 image as input BufferedImage type2Image = new BufferedImage(type1Image.getWidth(), type1Image.getHeight(), type2); Graphics2D type2G2D = (Graphics2D) type2Image.getGraphics(); type2G2D.drawImage(type1Image, 0, 0, null); //Convert type 2 image to argb image so that we can check pixel values BufferedImage argbImage = new BufferedImage (type2Image.getWidth(), type2Image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D argbG2D = (Graphics2D) argbImage.getGraphics(); argbG2D.drawImage(type2Image, 0, 0, null); //Verify all pixel values for (int i = 0; i < argbImage.getHeight(); i++) { for (int j = 0; j < argbImage.getWidth(); j++) { if (Color.BLACK.getRGB() != argbImage.getRGB(j, i)) throw new RuntimeException("Pixels values are not" + " matching after conversion"); } } } public static void main(String args[]){ ConvertToByteIndexedTest convertImage = new ConvertToByteIndexedTest(); // Iterate and convert image from different formats to Byte_Indexed for(int i = 0; i < types.length; i++) { convertImage.convertImage (types[i], BufferedImage.TYPE_BYTE_INDEXED); } } }