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 4413109 4418221 6607198
0N/A * @run main BitDepth
0N/A * @summary Checks that the PNG and JPEG writers can handle various
0N/A * BufferedImage types. An optional list of arguments may be used to
0N/A * test a different format writer or writers.
0N/A */
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageIO;
0N/A
0N/Apublic class BitDepth {
0N/A
0N/A public static void main(String[] args) throws IOException {
0N/A new BitDepth(args);
0N/A }
0N/A
0N/A // Check that the PNG writer can write an all-white image correctly
0N/A private static boolean testPNGByteBinary() throws IOException {
0N/A int width = 10;
0N/A int height = 10;
0N/A
0N/A File f = new File("BlackStripe.png");
0N/A BufferedImage bi = new BufferedImage(width, height,
0N/A BufferedImage.TYPE_BYTE_BINARY);
0N/A Graphics2D g = bi.createGraphics();
0N/A g.setColor(new Color(255, 255, 255));
0N/A g.fillRect(0, 0, width, height);
0N/A
0N/A ImageIO.write(bi, "png", f);
0N/A BufferedImage bi2 = ImageIO.read(f);
0N/A if (bi2.getWidth() != width || bi2.getHeight() != height) {
0N/A System.out.println("Dimensions changed!");
0N/A return false;
0N/A }
0N/A
0N/A for (int y = 0; y < height; y++) {
0N/A for (int x = 0; x < width; x++) {
0N/A int rgb = bi2.getRGB(x, y);
0N/A if (rgb != 0xffffffff) {
0N/A System.out.println("Found a non-white pixel!");
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A f.delete();
0N/A return true;
0N/A }
0N/A
0N/A private static final int[] biRGBTypes = {
0N/A BufferedImage.TYPE_INT_RGB,
0N/A BufferedImage.TYPE_INT_BGR,
0N/A BufferedImage.TYPE_3BYTE_BGR,
0N/A BufferedImage.TYPE_USHORT_565_RGB,
0N/A BufferedImage.TYPE_USHORT_555_RGB
0N/A };
0N/A
0N/A private static final int[] biRGBATypes = {
0N/A BufferedImage.TYPE_INT_ARGB,
0N/A BufferedImage.TYPE_INT_ARGB_PRE,
0N/A BufferedImage.TYPE_4BYTE_ABGR,
0N/A BufferedImage.TYPE_4BYTE_ABGR_PRE
0N/A };
0N/A
0N/A private static final int[] biGrayTypes = {
0N/A BufferedImage.TYPE_BYTE_GRAY,
0N/A BufferedImage.TYPE_USHORT_GRAY,
0N/A BufferedImage.TYPE_BYTE_BINARY
0N/A };
0N/A
0N/A private static final String[] biTypeNames = {
0N/A "CUSTOM",
0N/A "INT_RGB",
0N/A "INT_ARGB",
0N/A "INT_ARGB_PRE",
0N/A "INT_BGR",
0N/A "3BYTE_BGR",
0N/A "4BYTE_ABGR",
0N/A "4BYTE_ABGR_PRE",
0N/A "USHORT_565_RGB",
0N/A "USHORT_555_RGB",
0N/A "BYTE_GRAY",
0N/A "USHORT_GRAY",
0N/A "BYTE_BINARY",
0N/A "BYTE_INDEXED"
0N/A };
0N/A
0N/A private int width = 80;
0N/A private int height = 80;
0N/A private String[] format = { "png", "jpeg" };
0N/A
0N/A public BitDepth(String[] args) throws IOException {
0N/A if (args.length > 0) {
0N/A format = args;
0N/A }
0N/A
0N/A for (int i = 0; i < format.length; i++) {
0N/A testFormat(format[i]);
0N/A }
0N/A }
0N/A
0N/A private void testFormat(String format) throws IOException {
0N/A boolean allOK = true;
0N/A
0N/A for (int i = 0; i < biRGBTypes.length; i++) {
0N/A int type = biRGBTypes[i];
0N/A System.out.println("Testing " + format +
0N/A " writer for type " + biTypeNames[type]);
0N/A File f = testWriteRGB(format, type);
0N/A boolean ok = testReadRGB(f);
0N/A if (ok) {
0N/A f.delete();
0N/A }
0N/A allOK = allOK && ok;
0N/A }
0N/A
0N/A if (format.equals("png")) {
0N/A System.out.println("Testing png writer for black stripe");
0N/A boolean ok = testPNGByteBinary();
0N/A allOK = allOK && ok;
0N/A }
0N/A
0N/A if (!allOK) {
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A }
0N/A
0N/A private File testWriteRGB(String format, int type)
0N/A throws IOException {
0N/A BufferedImage bi = new BufferedImage(width, height, type);
0N/A Graphics2D g = bi.createGraphics();
0N/A
0N/A Color white = new Color(255, 255, 255);
0N/A Color red = new Color(255, 0, 0);
0N/A Color green = new Color(0, 255, 0);
0N/A Color blue = new Color(0, 0, 255);
0N/A
0N/A g.setColor(white);
0N/A g.fillRect(0, 0, width, height);
0N/A g.setColor(red);
0N/A g.fillRect(10, 10, 20, 20);
0N/A g.setColor(green);
0N/A g.fillRect(30, 30, 20, 20);
0N/A g.setColor(blue);
0N/A g.fillRect(50, 50, 20, 20);
0N/A
0N/A File file = new File("BitDepth_" + biTypeNames[type] + "." + format);
0N/A try {
0N/A ImageIO.write(bi, format, file);
0N/A } catch (RuntimeException re) {
0N/A System.out.println("Can't write a type "
0N/A + biTypeNames[type] +
0N/A " BufferedImage!");
0N/A }
0N/A
0N/A return file;
0N/A }
0N/A
0N/A private int colorDistance(int color, int r, int g, int b) {
0N/A int r0 = ((color >> 16) & 0xff) - r;
0N/A int g0 = ((color >> 8) & 0xff) - g;
0N/A int b0 = (color & 0xff) - b;
0N/A return r0*r0 + g0*g0 + b0*b0;
0N/A }
0N/A
0N/A private boolean testReadRGB(File file) throws IOException {
0N/A int[] rgb = new int[3];
0N/A
0N/A BufferedImage bi = ImageIO.read(file);
0N/A if (bi == null) {
0N/A System.out.println("Couldn't read image!");
0N/A return false;
0N/A }
0N/A int r = bi.getRGB(15, 15);
0N/A if (colorDistance(r, 255, 0, 0) > 20) {
0N/A System.out.println("Red was distorted!");
0N/A return false;
0N/A }
0N/A int g = bi.getRGB(35, 35);
0N/A if (colorDistance(g, 0, 255, 0) > 20) {
0N/A System.out.println("Green was distorted!");
0N/A return false;
0N/A }
0N/A int b = bi.getRGB(55, 55);
0N/A if (colorDistance(b, 0, 0, 255) > 20) {
0N/A System.out.println("Blue was distorted!");
0N/A return false;
0N/A }
0N/A int w = bi.getRGB(55, 15);
0N/A if (colorDistance(w, 255, 255, 255) > 20) {
0N/A System.out.println("White was distorted!");
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A}