Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/shared/BitDepth.java
38853 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 4413109 4418221 660719826* @run main BitDepth27* @summary Checks that the PNG and JPEG writers can handle various28* BufferedImage types. An optional list of arguments may be used to29* test a different format writer or writers.30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.File;36import java.io.IOException;37import javax.imageio.ImageIO;3839public class BitDepth {4041public static void main(String[] args) throws IOException {42new BitDepth(args);43}4445// Check that the PNG writer can write an all-white image correctly46private static boolean testPNGByteBinary() throws IOException {47int width = 10;48int height = 10;4950File f = new File("BlackStripe.png");51BufferedImage bi = new BufferedImage(width, height,52BufferedImage.TYPE_BYTE_BINARY);53Graphics2D g = bi.createGraphics();54g.setColor(new Color(255, 255, 255));55g.fillRect(0, 0, width, height);5657ImageIO.write(bi, "png", f);58BufferedImage bi2 = ImageIO.read(f);59if (bi2.getWidth() != width || bi2.getHeight() != height) {60System.out.println("Dimensions changed!");61return false;62}6364for (int y = 0; y < height; y++) {65for (int x = 0; x < width; x++) {66int rgb = bi2.getRGB(x, y);67if (rgb != 0xffffffff) {68System.out.println("Found a non-white pixel!");69return false;70}71}72}7374f.delete();75return true;76}7778private static final int[] biRGBTypes = {79BufferedImage.TYPE_INT_RGB,80BufferedImage.TYPE_INT_BGR,81BufferedImage.TYPE_3BYTE_BGR,82BufferedImage.TYPE_USHORT_565_RGB,83BufferedImage.TYPE_USHORT_555_RGB84};8586private static final int[] biRGBATypes = {87BufferedImage.TYPE_INT_ARGB,88BufferedImage.TYPE_INT_ARGB_PRE,89BufferedImage.TYPE_4BYTE_ABGR,90BufferedImage.TYPE_4BYTE_ABGR_PRE91};9293private static final int[] biGrayTypes = {94BufferedImage.TYPE_BYTE_GRAY,95BufferedImage.TYPE_USHORT_GRAY,96BufferedImage.TYPE_BYTE_BINARY97};9899private static final String[] biTypeNames = {100"CUSTOM",101"INT_RGB",102"INT_ARGB",103"INT_ARGB_PRE",104"INT_BGR",105"3BYTE_BGR",106"4BYTE_ABGR",107"4BYTE_ABGR_PRE",108"USHORT_565_RGB",109"USHORT_555_RGB",110"BYTE_GRAY",111"USHORT_GRAY",112"BYTE_BINARY",113"BYTE_INDEXED"114};115116private int width = 80;117private int height = 80;118private String[] format = { "png", "jpeg" };119120public BitDepth(String[] args) throws IOException {121if (args.length > 0) {122format = args;123}124125for (int i = 0; i < format.length; i++) {126testFormat(format[i]);127}128}129130private void testFormat(String format) throws IOException {131boolean allOK = true;132133for (int i = 0; i < biRGBTypes.length; i++) {134int type = biRGBTypes[i];135System.out.println("Testing " + format +136" writer for type " + biTypeNames[type]);137File f = testWriteRGB(format, type);138boolean ok = testReadRGB(f);139if (ok) {140f.delete();141}142allOK = allOK && ok;143}144145if (format.equals("png")) {146System.out.println("Testing png writer for black stripe");147boolean ok = testPNGByteBinary();148allOK = allOK && ok;149}150151if (!allOK) {152throw new RuntimeException("Test failed");153}154}155156private File testWriteRGB(String format, int type)157throws IOException {158BufferedImage bi = new BufferedImage(width, height, type);159Graphics2D g = bi.createGraphics();160161Color white = new Color(255, 255, 255);162Color red = new Color(255, 0, 0);163Color green = new Color(0, 255, 0);164Color blue = new Color(0, 0, 255);165166g.setColor(white);167g.fillRect(0, 0, width, height);168g.setColor(red);169g.fillRect(10, 10, 20, 20);170g.setColor(green);171g.fillRect(30, 30, 20, 20);172g.setColor(blue);173g.fillRect(50, 50, 20, 20);174175File file = new File("BitDepth_" + biTypeNames[type] + "." + format);176try {177ImageIO.write(bi, format, file);178} catch (RuntimeException re) {179System.out.println("Can't write a type "180+ biTypeNames[type] +181" BufferedImage!");182}183184return file;185}186187private int colorDistance(int color, int r, int g, int b) {188int r0 = ((color >> 16) & 0xff) - r;189int g0 = ((color >> 8) & 0xff) - g;190int b0 = (color & 0xff) - b;191return r0*r0 + g0*g0 + b0*b0;192}193194private boolean testReadRGB(File file) throws IOException {195int[] rgb = new int[3];196197BufferedImage bi = ImageIO.read(file);198if (bi == null) {199System.out.println("Couldn't read image!");200return false;201}202int r = bi.getRGB(15, 15);203if (colorDistance(r, 255, 0, 0) > 20) {204System.out.println("Red was distorted!");205return false;206}207int g = bi.getRGB(35, 35);208if (colorDistance(g, 0, 255, 0) > 20) {209System.out.println("Green was distorted!");210return false;211}212int b = bi.getRGB(55, 55);213if (colorDistance(b, 0, 0, 255) > 20) {214System.out.println("Blue was distorted!");215return false;216}217int w = bi.getRGB(55, 15);218if (colorDistance(w, 255, 255, 255) > 20) {219System.out.println("White was distorted!");220return false;221}222223return true;224}225}226227228