Path: blob/master/test/jdk/javax/imageio/plugins/png/PngPremultAlphaTest.java
41155 views
/*1* Copyright (c) 2003, 2017, 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 488847826* @summary Test that colors do not distort when buffered image with27* premultiplied alpha is encoded to png format28*/2930import java.awt.AlphaComposite;31import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.image.BufferedImage;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;3637import javax.imageio.ImageIO;3839public class PngPremultAlphaTest {40protected static int width = 100;41protected static int height = 100;4243protected static String format = "png";4445protected static int[] iBufferedImageTypes = {46BufferedImage.TYPE_INT_RGB,47BufferedImage.TYPE_INT_ARGB,48BufferedImage.TYPE_4BYTE_ABGR_PRE,49BufferedImage.TYPE_INT_ARGB_PRE50};5152protected static String[] strBufferedImageTypes = {53"TYPE_INT_RGB",54"TYPE_INT_ARGB",55"BufferedImage.TYPE_4BYTE_ABGR_PRE",56"BufferedImage.TYPE_INT_ARGB_PRE"57};5859public static void main(String[] arg) {60for(int i=0; i<iBufferedImageTypes.length; i++) {61System.out.println("Test for " + strBufferedImageTypes[i]);62doTest(iBufferedImageTypes[i]);63}64}6566public static void doTest(int type) {67try {68BufferedImage src = new BufferedImage(100, 100,69type);70Graphics2D g = src.createGraphics();71g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));72g.setColor(new Color(0x20, 0x40, 0x60));73g.fillRect(0,0,100,100);7475int[] samples = new int[src.getData().getNumBands()];76src.getData().getPixels(0,0,1,1,samples);77for(int i=0; i<samples.length; i++) {78System.out.println("sample["+i+"]="+Integer.toHexString(samples[i]));79}8081ByteArrayOutputStream baos = new ByteArrayOutputStream();82ImageIO.write(src, format, baos);83baos.close();848586ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());87BufferedImage dst = ImageIO.read(bais);8889isSameColors(src, dst);90} catch (Exception e) {91e.printStackTrace();92throw new RuntimeException("Test failed.");93}94}9596private static boolean isSameColors(BufferedImage src, BufferedImage dst) {97Object dstPixel = dst.getRaster().getDataElements(width/2, height/2, null);98Object srcPixel = src.getRaster().getDataElements(width/2, height/2, null);99100// take into account the rounding error101if ( Math.abs(src.getColorModel().getRed(srcPixel) - dst.getColorModel().getRed(dstPixel)) > 1102|| Math.abs(src.getColorModel().getGreen(srcPixel) - dst.getColorModel().getGreen(dstPixel)) > 1103|| Math.abs(src.getColorModel().getBlue(srcPixel) - dst.getColorModel().getBlue(dstPixel)) > 1) {104showPixel(src, width/2, height/2);105showPixel(dst, width/2, height/2);106107throw new RuntimeException( "Colors are different: "108+ Integer.toHexString(src.getColorModel().getRGB(srcPixel))109+ " and "110+ Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));111}112return true;113}114115private static void showPixel(BufferedImage src, int x, int y) {116System.out.println("Img is " + src);117System.out.println("CM is " + src.getColorModel().getClass().getName());118Object p = src.getRaster().getDataElements(x, y, null);119System.out.println("RGB: " +120Integer.toHexString(src.getColorModel().getRGB(p)));121System.out.println("Red: " +122Integer.toHexString(src.getColorModel().getRed(p)));123System.out.println("Green: " +124Integer.toHexString(src.getColorModel().getGreen(p)));125System.out.println("Blue: " +126Integer.toHexString(src.getColorModel().getBlue(p)));127System.out.println("Alpha: " +128Integer.toHexString(src.getColorModel().getAlpha(p)));129}130}131132133