Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ReadAsGrayTest.java
41152 views
/*1* Copyright (c) 2009, 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 489340826*27* @summary Test verifies that Image I/O jpeg reader correctly handles28* destination types if number of color components in destination29* differs from number of color components in the jpeg image.30* Particularly, it verifies reading YCbCr image as a grayscaled31* and reading grayscaled jpeg as a RGB.32*33* @run main ReadAsGrayTest34*/3536import java.awt.Color;37import java.awt.Graphics2D;38import java.awt.color.ColorSpace;39import java.awt.image.BufferedImage;40import java.io.File;41import java.io.IOException;42import java.util.Iterator;43import javax.imageio.ImageIO;44import javax.imageio.ImageReadParam;45import javax.imageio.ImageReader;46import javax.imageio.ImageTypeSpecifier;47import javax.imageio.stream.ImageInputStream;48import static java.awt.image.BufferedImage.TYPE_3BYTE_BGR;49import static java.awt.image.BufferedImage.TYPE_BYTE_GRAY;50import static java.awt.color.ColorSpace.TYPE_GRAY;51import static java.awt.color.ColorSpace.CS_sRGB;5253public class ReadAsGrayTest {54static Color[] colors = new Color[] {55Color.white, Color.red, Color.green,56Color.blue, Color.black };5758static final int dx = 50;59static final int h = 100;6061static ColorSpace sRGB = ColorSpace.getInstance(CS_sRGB);626364public static void main(String[] args) throws IOException {65System.out.println("Type TYPE_BYTE_GRAY");66doTest(TYPE_BYTE_GRAY);6768System.out.println("Type TYPE_3BYTE_BGR");69doTest(TYPE_3BYTE_BGR);7071System.out.println("Test PASSED.");72}7374private static void doTest(int type) throws IOException {75BufferedImage src = createTestImage(type);7677File f = new File("test.jpg");7879if (!ImageIO.write(src, "jpg", f)) {80throw new RuntimeException("Failed to write test image.");81}8283ImageInputStream iis = ImageIO.createImageInputStream(f);84ImageReader reader = ImageIO.getImageReaders(iis).next();85reader.setInput(iis);8687Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);88ImageTypeSpecifier srgb = null;89ImageTypeSpecifier gray = null;90// look for gray and srgb types91while ((srgb == null || gray == null) && types.hasNext()) {92ImageTypeSpecifier t = types.next();93if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) {94gray = t;95}96if (t.getColorModel().getColorSpace() == sRGB) {97srgb = t;98}99}100if (gray == null) {101throw new RuntimeException("No gray type available.");102}103if (srgb == null) {104throw new RuntimeException("No srgb type available.");105}106107System.out.println("Read as GRAY...");108testType(reader, gray, src);109110System.out.println("Read as sRGB...");111testType(reader, srgb, src);112}113114private static void testType(ImageReader reader,115ImageTypeSpecifier t,116BufferedImage src)117throws IOException118{119ImageReadParam p = reader.getDefaultReadParam();120p.setDestinationType(t);121BufferedImage dst = reader.read(0, p);122123verify(src, dst, t);124}125126private static void verify(BufferedImage src,127BufferedImage dst,128ImageTypeSpecifier type)129{130BufferedImage test =131type.createBufferedImage(src.getWidth(), src.getHeight());132Graphics2D g = test.createGraphics();133g.drawImage(src, 0, 0, null);134g.dispose();135136for (int i = 0; i < colors.length; i++) {137int x = i * dx + dx / 2;138int y = h / 2;139140Color c_test = new Color(test.getRGB(x, y));141Color c_dst = new Color(dst.getRGB(x, y));142143if (!compareWithTolerance(c_test, c_dst, 0.01f)) {144String msg = String.format("Invalid color: %x instead of %x",145c_dst.getRGB(), c_test.getRGB());146throw new RuntimeException("Test failed: " + msg);147}148}149System.out.println("Verified.");150}151152private static boolean compareWithTolerance(Color a, Color b, float delta) {153float[] a_rgb = new float[3];154a_rgb = a.getRGBColorComponents(a_rgb);155float[] b_rgb = new float[3];156b_rgb = b.getRGBColorComponents(b_rgb);157158for (int i = 0; i < 3; i++) {159if (Math.abs(a_rgb[i] - b_rgb[i]) > delta) {160return false;161}162}163return true;164}165166private static BufferedImage createTestImage(int type) {167BufferedImage img = new BufferedImage(dx * colors.length, h, type);168169Graphics2D g = img.createGraphics();170for (int i = 0; i < colors.length; i++) {171g.setColor(colors[i]);172g.fillRect(i * dx, 0, dx, h);173}174g.dispose();175176return img;177}178}179180181