Path: blob/master/test/jdk/javax/imageio/plugins/bmp/NoExtraBytesTest.java
41153 views
/*1* Copyright (c) 2006, 2017, 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 507687826* @summary Test verifies that ImageIO creates BMP images with correct bpp27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.Transparency;32import java.awt.color.ColorSpace;33import java.awt.image.BufferedImage;34import java.awt.image.ColorModel;35import java.awt.image.ComponentColorModel;36import java.awt.image.DataBuffer;37import java.awt.image.DirectColorModel;38import java.awt.image.Raster;39import java.awt.image.WritableRaster;40import java.io.File;41import java.io.FileInputStream;42import java.io.IOException;43import java.util.Hashtable;4445import javax.imageio.IIOImage;46import javax.imageio.ImageIO;47import javax.imageio.ImageReader;48import javax.imageio.metadata.IIOMetadata;49import javax.imageio.stream.ImageInputStream;5051import org.w3c.dom.Node;5253public class NoExtraBytesTest {5455private static Hashtable<Integer, Integer> tests = null;56private static Color[] usedColors = new Color[] { Color.red, Color.green, Color.blue, Color.yellow, Color.white, Color.black };5758private static final int TYPE_INT_GRB = 0x100;59private static final int TYPE_INT_GBR = 0x101;60private static final int TYPE_INT_RBG = 0x102;61private static final int TYPE_INT_BRG = 0x103;62private static final int TYPE_INT_555_GRB = 0x104;63private static final int TYPE_3BYTE_RGB = 0x105;64private static final int TYPE_3BYTE_GRB = 0x106;6566private static final int w = 300;67private static final int h = 200;68private static final int dx = w / usedColors.length;6970public static void main(String[] args) throws IOException {71initTests();7273for (Integer type : tests.keySet()) {74new NoExtraBytesTest(type.intValue(), tests.get(type).intValue()).doTest();75}76System.out.println("Test passed.");77}7879private static void initTests() {80tests = new Hashtable<Integer, Integer>();8182tests.put(new Integer(BufferedImage.TYPE_INT_RGB), new Integer(24));83tests.put(new Integer(BufferedImage.TYPE_INT_BGR), new Integer(24));84tests.put(new Integer(BufferedImage.TYPE_3BYTE_BGR), new Integer(24));85tests.put(new Integer(TYPE_INT_GRB), new Integer(24));86tests.put(new Integer(TYPE_INT_GBR), new Integer(24));87tests.put(new Integer(TYPE_INT_RBG), new Integer(24));88tests.put(new Integer(TYPE_INT_BRG), new Integer(24));89tests.put(new Integer(BufferedImage.TYPE_USHORT_555_RGB), new Integer(16));90tests.put(new Integer(BufferedImage.TYPE_USHORT_565_RGB), new Integer(16));91tests.put(new Integer(TYPE_INT_555_GRB), new Integer(16));92tests.put(new Integer(TYPE_3BYTE_RGB), new Integer(24));93tests.put(new Integer(TYPE_3BYTE_GRB), new Integer(24));94}9596private static String getImageTypeName(int t) {97switch(t) {98case BufferedImage.TYPE_INT_RGB:99return "TYPE_INT_RGB";100case BufferedImage.TYPE_INT_BGR:101return "TYPE_INT_BGR";102case BufferedImage.TYPE_3BYTE_BGR:103return "TYPE_3BYTE_BGR";104case BufferedImage.TYPE_USHORT_555_RGB:105return "TYPE_USHORT_555_RGB";106case BufferedImage.TYPE_USHORT_565_RGB:107return "TYPE_USHORT_565_RGB";108case TYPE_INT_GRB:109return "TYPE_INT_GRB";110case TYPE_INT_GBR:111return "TYPE_INT_GBR";112case TYPE_INT_RBG:113return "TYPE_INT_RBG";114case TYPE_INT_BRG:115return "TYPE_INT_BRG";116case TYPE_INT_555_GRB:117return "TYPE_INT_555_GRB";118case TYPE_3BYTE_RGB:119return "TYPE_3BYTE_RGB";120case TYPE_3BYTE_GRB:121return "TYPE_3BYTE_GRB";122default:123throw new IllegalArgumentException("Unknown image type: " + t);124}125}126private static BufferedImage createTestImage(int type) {127BufferedImage dst = null;128ColorModel colorModel = null;129WritableRaster raster = null;130ColorSpace cs = null;131System.out.println("Create image for " + getImageTypeName(type));132switch(type) {133case TYPE_INT_GRB:134colorModel = new DirectColorModel(24,1350x0000ff00,1360x00ff0000,1370x000000ff);138break;139case TYPE_INT_GBR:140colorModel = new DirectColorModel(24,1410x000000ff,1420x00ff0000,1430x0000ff00);144break;145case TYPE_INT_RBG:146colorModel = new DirectColorModel(24,1470x00ff0000,1480x000000ff,1490x0000ff00);150break;151case TYPE_INT_BRG:152colorModel = new DirectColorModel(24,1530x0000ff00,1540x000000ff,1550x00ff0000);156break;157case TYPE_INT_555_GRB:158colorModel = new DirectColorModel(24,1590x0000001F,1600x000003e0,1610x00007c00);162break;163case TYPE_3BYTE_RGB:164cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);165int[] nBits = {8, 8, 8};166int[] bOffs = {0, 1, 2};167colorModel = new ComponentColorModel(cs, nBits, false, false,168Transparency.OPAQUE,169DataBuffer.TYPE_BYTE);170raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,171w, h,172w*3, 3,173bOffs, null);174break;175case TYPE_3BYTE_GRB:176cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);177//nBits = {8, 8, 8};178//bOffs = {0, 1, 2};179colorModel = new ComponentColorModel(cs, new int[] { 8, 8, 8 }, false, false,180Transparency.OPAQUE,181DataBuffer.TYPE_BYTE);182raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,183w, h,184w*3, 3,185new int[] { 1, 0, 2}, null);186break;187default:188dst = new BufferedImage(w, h, type);189//colorModel = ImageTypeSpecifier.createFromBufferedImageType(type).getColorModel();190}191192if (dst == null) {193if (raster == null) {194raster = colorModel.createCompatibleWritableRaster(w, h);195}196197dst = new BufferedImage(colorModel, raster, false, null);198}199Graphics g = dst.createGraphics();200for (int i = 0; i < usedColors.length; i ++) {201g.setColor(usedColors[i]);202g.fillRect(i * dx, 0, dx, h);203}204g.dispose();205206return dst;207}208209private BufferedImage src;210private int expectedColorDepth;211private int type;212213private IIOImage iio_dst;214215public NoExtraBytesTest(int type, int expectedColorDepth) {216this.type = type;217this.src = createTestImage(type);218this.expectedColorDepth = expectedColorDepth;219}220221public void doTest() throws IOException {222// write src as BMP223System.out.println("Test for image: " + getImageTypeName(type));224System.out.println("image is " + src);225226File f = File.createTempFile("sizeTest_", ".bmp", new File("."));227System.out.println("Use file " + f.getCanonicalPath());228ImageIO.write(src, "BMP", f);229230//read it again231read(f);232233checkColorDepth();234235checkImageContent();236}237238private void read(File f) throws IOException {239ImageReader reader = ImageIO.getImageReadersByFormatName("BMP").next();240241ImageInputStream iis =242ImageIO.createImageInputStream(new FileInputStream(f));243244reader.setInput(iis);245246iio_dst = reader.readAll(0, reader.getDefaultReadParam());247}248249private void checkColorDepth() {250IIOMetadata dst = iio_dst.getMetadata();251252Node data = dst.getAsTree("javax_imageio_bmp_1.0");253254Node n = data.getFirstChild();255256while (n != null && !("BitsPerPixel".equals(n.getNodeName()))) {257System.out.println("Node " + n.getNodeName());258n = n.getNextSibling();259}260if (n == null) {261throw new RuntimeException("No BitsPerSample node!");262}263264int bpp = 0;265String value = n.getNodeValue();266System.out.println("value = " + value);267try {268bpp = Integer.parseInt(value);269} catch (NumberFormatException e) {270throw new RuntimeException("Wrong bpp value: " + value, e);271}272273if (bpp != this.expectedColorDepth) {274throw new RuntimeException("Wrong color depth: " + bpp +275" (should be " + this.expectedColorDepth + ")");276}277}278279private void checkImageContent() {280BufferedImage dst =281(BufferedImage)iio_dst.getRenderedImage();282int y = h / 2;283int x = dx / 2;284285for (int i = 0; i < usedColors.length; i++, x += dx) {286int srcRgb = src.getRGB(x, y);287int dstRgb = dst.getRGB(x, y);288int rgb = usedColors[i].getRGB();289290if (dstRgb != srcRgb || dstRgb != rgb) {291throw new RuntimeException("Wrong color at [" + x + ", " + y +292"] " + Integer.toHexString(dstRgb) +293" (srcRgb=" + Integer.toHexString(srcRgb) +294", original color is " + Integer.toHexString(rgb) + ")");295}296297}298System.out.println("Image colors are OK.");299}300}301302303