Path: blob/master/test/jdk/javax/imageio/plugins/png/GrayPngTest.java
41155 views
/*1* Copyright (c) 2007, 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 655771326* @summary Test verifies that PNG image writer correctly handles indexed images with27* various types of transparency.28*29* Test for 4bpp OPAQUE image30* @run main GrayPngTest 4 1 331*32* Test for 4bpp BITMASK image with transparent pixel 333* @run main GrayPngTest 4 2 334*35* Test for 4bpp TRANSLUCENT image36* @run main GrayPngTest 4 3 337*38* Test for 8bpp OPAQUE image39* @run main GrayPngTest 8 1 12740*41* Test for 8bpp BITMASK image with transparent pixel 12742* @run main GrayPngTest 8 2 12743*44* Test for 8bpp TRANSLUCENT image45* @run main GrayPngTest 8 3 12746*47*/4849import java.awt.Color;50import java.awt.Transparency;51import java.awt.image.BufferedImage;52import java.awt.image.IndexColorModel;53import java.awt.image.WritableRaster;54import java.io.File;55import java.io.IOException;56import java.util.Arrays;57import javax.imageio.ImageIO;5859public class GrayPngTest {6061public static void main(String[] args) throws IOException {62/*63* Expected argiments:64* args[0] - bits per pixel. Supported range: [1, 8]65* args[1] - transparency type. Should be one form66* java.awt.Transparency type constants.67* args[2] - transparent pixel for BITMASK transparency type,68* otherwise is ignored.69*/70int bpp = 4;71int trans_type = Transparency.BITMASK;72int trans_pixel = 3;73try {74bpp = Integer.parseInt(args[0]);75trans_type = Integer.parseInt(args[1]);76trans_pixel = Integer.parseInt(args[2]);77} catch (NumberFormatException e) {78System.out.println("Ignore ncorrect bpp value: " + args[0]);79} catch (ArrayIndexOutOfBoundsException e) {80System.out.println("Default test argumens.");81}8283new GrayPngTest(bpp).doTest(trans_type, trans_pixel);84}85868788private BufferedImage getTestImage(int trans_type, int trans_pixel) {8990IndexColorModel icm = null;91switch(trans_type) {92case Transparency.OPAQUE:93icm = new IndexColorModel(bpp, numColors, r, g, b);94break;95case Transparency.BITMASK:96icm = new IndexColorModel(bpp, numColors, r, g, b, trans_pixel);97break;98case Transparency.TRANSLUCENT:99a = Arrays.copyOf(r, r.length);100icm = new IndexColorModel(bpp, numColors, r, g, b, a);101break;102default:103throw new RuntimeException("Invalid transparency: " + trans_type);104}105106int w = 256 * 2;107int h = 200;108109dx = w / (numColors);110111WritableRaster wr = icm.createCompatibleWritableRaster(w, h);112for (int i = 0; i < numColors; i ++) {113int rx = i * dx;114115int[] samples = new int[h * dx];116Arrays.fill(samples, i);117wr.setPixels(rx, 0, dx, h, samples);118}119120// horizontal line with transparent color121int[] samples = new int[w * 10];122Arrays.fill(samples, trans_pixel);123wr.setPixels(0, h / 2 - 5, w, 10, samples);124125// create index color model126return new BufferedImage(icm, wr, false, null);127}128129static File pwd = new File(".");130131private BufferedImage src;132private BufferedImage dst;133private int bpp;134private int numColors;135136private int dx;137138private byte[] r;139private byte[] g;140private byte[] b;141142private byte[] a;143144protected GrayPngTest(int bpp) {145if (0 > bpp || bpp > 8) {146throw new RuntimeException("Invalid bpp: " + bpp);147}148this.bpp = bpp;149numColors = (1 << bpp);150System.out.println("Num colors: " + numColors);151152// create palette153r = new byte[numColors];154g = new byte[numColors];155b = new byte[numColors];156157int dc = 0xff / (numColors - 1);158System.out.println("dc = " + dc);159160for (int i = 0; i < numColors; i ++) {161byte l = (byte)(i * dc);162r[i] = l; g[i] = l; b[i] = l;163}164}165166public void doTest() throws IOException {167for (int i = 0; i < numColors; i++) {168doTest(Transparency.BITMASK, i);169}170}171172public void doTest(int trans_type, int trans_index) throws IOException {173src = getTestImage(trans_type, trans_index);174175System.out.println("src: " + src);176177File f = File.createTempFile("gray_png_" + bpp + "bpp_" +178trans_type + "tt_" +179trans_index + "tp_", ".png", pwd);180System.out.println("File: " + f.getAbsolutePath());181if (!ImageIO.write(src, "png", f)) {182throw new RuntimeException("Writing failed!");183};184185try {186dst = ImageIO.read(f);187System.out.println("dst: " + dst);188} catch (Exception e) {189throw new RuntimeException("Test FAILED.", e);190}191192checkImages();193}194195private void checkImages() {196for (int i = 0; i < numColors; i++) {197int src_rgb = src.getRGB(i * dx, 5);198int dst_rgb = dst.getRGB(i * dx, 5);199200// here we check transparency only due to possible colors space201// differences (sRGB in indexed source and Gray in gray+alpha destination)202if ((0xff000000 & src_rgb) != (0xff000000 & dst_rgb)) {203throw new RuntimeException("Test FAILED. Color difference detected: " +204Integer.toHexString(dst_rgb) + " instead of " +205Integer.toHexString(src_rgb) + " for index " + i);206207}208}209}210}211212213