0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6557713
0N/A * @summary Test verifies that PNG image writer correctly handles indexed images with
0N/A * various types of transparency.
0N/A *
0N/A * Test for 4bpp OPAQUE image
0N/A * @run main GrayPngTest 4 1 3
0N/A *
0N/A * Test for 4bpp BITMASK image with transparent pixel 3
0N/A * @run main GrayPngTest 4 2 3
0N/A *
0N/A * Test for 4bpp TRANSLUCENT image
0N/A * @run main GrayPngTest 4 3 3
0N/A *
0N/A * Test for 8bpp OPAQUE image
0N/A * @run main GrayPngTest 8 1 127
0N/A *
0N/A * Test for 8bpp BITMASK image with transparent pixel 127
0N/A * @run main GrayPngTest 8 2 127
0N/A *
0N/A * Test for 8bpp TRANSLUCENT image
0N/A * @run main GrayPngTest 8 3 127
0N/A *
0N/A */
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Transparency;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.image.IndexColorModel;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.util.Arrays;
0N/Aimport javax.imageio.ImageIO;
0N/A
0N/Apublic class GrayPngTest {
0N/A
0N/A public static void main(String[] args) throws IOException {
0N/A /*
0N/A * Expected argiments:
0N/A * args[0] - bits per pixel. Supported range: [1, 8]
0N/A * args[1] - transparency type. Should be one form
0N/A * java.awt.Transparency type constants.
0N/A * args[2] - transparent pixel for BITMASK transparency type,
0N/A * otherwise is ignored.
0N/A */
0N/A int bpp = 4;
0N/A int trans_type = Transparency.BITMASK;
0N/A int trans_pixel = 3;
0N/A try {
0N/A bpp = Integer.parseInt(args[0]);
0N/A trans_type = Integer.parseInt(args[1]);
0N/A trans_pixel = Integer.parseInt(args[2]);
0N/A } catch (NumberFormatException e) {
0N/A System.out.println("Ignore ncorrect bpp value: " + args[0]);
0N/A } catch (ArrayIndexOutOfBoundsException e) {
0N/A System.out.println("Default test argumens.");
0N/A }
0N/A
0N/A new GrayPngTest(bpp).doTest(trans_type, trans_pixel);
0N/A }
0N/A
0N/A
0N/A
0N/A private BufferedImage getTestImage(int trans_type, int trans_pixel) {
0N/A
0N/A IndexColorModel icm = null;
0N/A switch(trans_type) {
0N/A case Transparency.OPAQUE:
0N/A icm = new IndexColorModel(bpp, numColors, r, g, b);
0N/A break;
0N/A case Transparency.BITMASK:
0N/A icm = new IndexColorModel(bpp, numColors, r, g, b, trans_pixel);
0N/A break;
0N/A case Transparency.TRANSLUCENT:
0N/A a = Arrays.copyOf(r, r.length);
0N/A icm = new IndexColorModel(bpp, numColors, r, g, b, a);
0N/A break;
0N/A default:
0N/A throw new RuntimeException("Invalid transparency: " + trans_type);
0N/A }
0N/A
0N/A int w = 256 * 2;
0N/A int h = 200;
0N/A
0N/A dx = w / (numColors);
0N/A
0N/A WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
0N/A for (int i = 0; i < numColors; i ++) {
0N/A int rx = i * dx;
0N/A
0N/A int[] samples = new int[h * dx];
0N/A Arrays.fill(samples, i);
0N/A wr.setPixels(rx, 0, dx, h, samples);
0N/A }
0N/A
0N/A // horizontal line with transparent color
0N/A int[] samples = new int[w * 10];
0N/A Arrays.fill(samples, trans_pixel);
0N/A wr.setPixels(0, h / 2 - 5, w, 10, samples);
0N/A
0N/A // create index color model
0N/A return new BufferedImage(icm, wr, false, null);
0N/A }
0N/A
0N/A static File pwd = new File(".");
0N/A
0N/A private BufferedImage src;
0N/A private BufferedImage dst;
0N/A private int bpp;
0N/A private int numColors;
0N/A
0N/A private int dx;
0N/A
0N/A private byte[] r;
0N/A private byte[] g;
0N/A private byte[] b;
0N/A
0N/A private byte[] a;
0N/A
0N/A protected GrayPngTest(int bpp) {
0N/A if (0 > bpp || bpp > 8) {
0N/A throw new RuntimeException("Invalid bpp: " + bpp);
0N/A }
0N/A this.bpp = bpp;
0N/A numColors = (1 << bpp);
0N/A System.out.println("Num colors: " + numColors);
0N/A
0N/A // create palette
0N/A r = new byte[numColors];
0N/A g = new byte[numColors];
0N/A b = new byte[numColors];
0N/A
0N/A int dc = 0xff / (numColors - 1);
0N/A System.out.println("dc = " + dc);
0N/A
0N/A for (int i = 0; i < numColors; i ++) {
0N/A byte l = (byte)(i * dc);
0N/A r[i] = l; g[i] = l; b[i] = l;
0N/A }
0N/A }
0N/A
0N/A public void doTest() throws IOException {
0N/A for (int i = 0; i < numColors; i++) {
0N/A doTest(Transparency.BITMASK, i);
0N/A }
0N/A }
0N/A
0N/A public void doTest(int trans_type, int trans_index) throws IOException {
0N/A src = getTestImage(trans_type, trans_index);
0N/A
0N/A System.out.println("src: " + src);
0N/A
0N/A File f = File.createTempFile("gray_png_" + bpp + "bpp_" +
0N/A trans_type + "tt_" +
0N/A trans_index + "tp_", ".png", pwd);
0N/A System.out.println("File: " + f.getAbsolutePath());
0N/A if (!ImageIO.write(src, "png", f)) {
0N/A throw new RuntimeException("Writing failed!");
0N/A };
0N/A
0N/A try {
0N/A dst = ImageIO.read(f);
0N/A System.out.println("dst: " + dst);
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("Test FAILED.", e);
0N/A }
0N/A
0N/A checkImages();
0N/A }
0N/A
0N/A private void checkImages() {
0N/A for (int i = 0; i < numColors; i++) {
0N/A int src_rgb = src.getRGB(i * dx, 5);
0N/A int dst_rgb = dst.getRGB(i * dx, 5);
0N/A
0N/A // here we check transparency only due to possible colors space
0N/A // differences (sRGB in indexed source and Gray in gray+alpha destination)
0N/A if ((0xff000000 & src_rgb) != (0xff000000 & dst_rgb)) {
0N/A throw new RuntimeException("Test FAILED. Color difference detected: " +
0N/A Integer.toHexString(dst_rgb) + " instead of " +
0N/A Integer.toHexString(src_rgb) + " for index " + i);
0N/A
0N/A }
0N/A }
0N/A }
0N/A}