Path: blob/master/test/jdk/javax/imageio/plugins/shared/BitDepth.java
41153 views
/*1* Copyright (c) 2007, 2016, 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 6607198 8147448 820418726* @run main BitDepth27* @summary Checks that ImageIO writers for standard formats can handle28* various BufferedImage RGB types. An optional list of arguments29* may be used to test the writers for a different list of formats.30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.File;36import java.io.IOException;37import java.util.Iterator;38import javax.imageio.ImageIO;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.ImageWriter;41import javax.imageio.stream.ImageOutputStream;4243public class BitDepth {4445public static void main(String[] args) throws IOException {46new BitDepth(args);47}4849// Check that the PNG writer can write an all-white image correctly50private static boolean testPNGByteBinary() throws IOException {51int width = 10;52int height = 10;5354File f = new File("BlackStripe.png");55BufferedImage bi = new BufferedImage(width, height,56BufferedImage.TYPE_BYTE_BINARY);57Graphics2D g = bi.createGraphics();58g.setColor(new Color(255, 255, 255));59g.fillRect(0, 0, width, height);6061ImageIO.write(bi, "png", f);62BufferedImage bi2 = ImageIO.read(f);63if (bi2.getWidth() != width || bi2.getHeight() != height) {64System.out.println("Dimensions changed!");65return false;66}6768for (int y = 0; y < height; y++) {69for (int x = 0; x < width; x++) {70int rgb = bi2.getRGB(x, y);71if (rgb != 0xffffffff) {72System.out.println("Found a non-white pixel!");73return false;74}75}76}7778f.delete();79return true;80}8182private static final int[] biRGBTypes = {83BufferedImage.TYPE_INT_RGB,84BufferedImage.TYPE_INT_BGR,85BufferedImage.TYPE_3BYTE_BGR,86BufferedImage.TYPE_USHORT_565_RGB,87BufferedImage.TYPE_USHORT_555_RGB,88BufferedImage.TYPE_INT_ARGB,89BufferedImage.TYPE_INT_ARGB_PRE,90BufferedImage.TYPE_4BYTE_ABGR,91BufferedImage.TYPE_4BYTE_ABGR_PRE92};9394//private static final int[] biGrayTypes = {95// BufferedImage.TYPE_BYTE_GRAY,96// BufferedImage.TYPE_USHORT_GRAY,97// BufferedImage.TYPE_BYTE_BINARY98//};99100101private static final String[] biTypeNames = {102"CUSTOM",103"INT_RGB",104"INT_ARGB",105"INT_ARGB_PRE",106"INT_BGR",107"3BYTE_BGR",108"4BYTE_ABGR",109"4BYTE_ABGR_PRE",110"USHORT_565_RGB",111"USHORT_555_RGB",112"BYTE_GRAY",113"USHORT_GRAY",114"BYTE_BINARY",115"BYTE_INDEXED"116};117118private int width = 80;119private int height = 80;120private String[] formats = { "png", "jpeg", "tiff", "bmp", "gif" };121122public BitDepth(String[] args) throws IOException {123if (args.length > 0) {124formats = args;125}126127for (String format : formats) {128testFormat(format);129}130}131132private void testFormat(String format) throws IOException {133134boolean allOK = true;135136for (int type : biRGBTypes) {137// TODO: remove the following 'if' block after the 8147448 fix138if ( format.toLowerCase().equals("bmp") && (139(type == BufferedImage.TYPE_INT_ARGB ) ||140(type == BufferedImage.TYPE_INT_ARGB_PRE ) ||141(type == BufferedImage.TYPE_4BYTE_ABGR ) ||142(type == BufferedImage.TYPE_4BYTE_ABGR_PRE ))) {143144System.err.println("cannot use " + biTypeNames[type] +145" for bmp because of JDK-8147448.\t" +146" please update the test after fix of this bug!");147continue;148}149150System.out.println("Testing " + format +151" writer for type " + biTypeNames[type]);152File f = testWriteRGB(format, type);153if (f == null)154continue;155156boolean ok = testReadRGB(f);157if (ok) {158f.delete();159}160allOK = allOK && ok;161}162163if (format.equals("png")) {164System.out.println("Testing png writer for black stripe");165boolean ok = testPNGByteBinary();166allOK = allOK && ok;167}168169if (!allOK) {170throw new RuntimeException("Test failed");171}172}173174private File testWriteRGB(String format, int type) throws IOException {175176BufferedImage bi = new BufferedImage(width, height, type);177Graphics2D g = bi.createGraphics();178179Color white = new Color(255, 255, 255);180Color red = new Color(255, 0, 0);181Color green = new Color(0, 255, 0);182Color blue = new Color(0, 0, 255);183184g.setColor(white);185g.fillRect(0, 0, width, height);186g.setColor(red);187g.fillRect(10, 10, 20, 20);188g.setColor(green);189g.fillRect(30, 30, 20, 20);190g.setColor(blue);191g.fillRect(50, 50, 20, 20);192193ImageTypeSpecifier spec = new ImageTypeSpecifier(bi);194Iterator<ImageWriter> writers = ImageIO.getImageWriters(spec, format);195File file = new File("BitDepth_" + biTypeNames[type] + "." + format);196if (!writers.hasNext()) {197System.out.println("\tNo writers available for type " + biTypeNames[type]198+ " BufferedImage!");199return null;200} else {201ImageWriter writer = writers.next();202try (ImageOutputStream out = ImageIO.createImageOutputStream(file)) {203writer.setOutput(out);204writer.write(bi);205} catch (Exception e) {206System.out.println("\tCan't write a type " + biTypeNames[type]207+ " BufferedImage!");208throw new RuntimeException(e);209}210}211212return file;213}214215private int colorDistance(int color, int r, int g, int b) {216int r0 = ((color >> 16) & 0xff) - r;217int g0 = ((color >> 8) & 0xff) - g;218int b0 = (color & 0xff) - b;219return r0*r0 + g0*g0 + b0*b0;220}221222private boolean testReadRGB(File file) throws IOException {223int[] rgb = new int[3];224225BufferedImage bi = ImageIO.read(file);226if (bi == null) {227System.out.println("Couldn't read image!");228return false;229}230int r = bi.getRGB(15, 15);231if (colorDistance(r, 255, 0, 0) > 20) {232System.out.println("Red was distorted!");233return false;234}235int g = bi.getRGB(35, 35);236if (colorDistance(g, 0, 255, 0) > 20) {237System.out.println("Green was distorted!");238return false;239}240int b = bi.getRGB(55, 55);241if (colorDistance(b, 0, 0, 255) > 20) {242System.out.println("Blue was distorted!");243return false;244}245int w = bi.getRGB(55, 15);246if (colorDistance(w, 255, 255, 255) > 20) {247System.out.println("White was distorted!");248return false;249}250251return true;252}253}254255256