Path: blob/master/test/jdk/javax/imageio/plugins/bmp/Write3ByteBgrTest.java
41153 views
/*1* Copyright (c) 2003, 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 4892194 801442726* @summary Test checks that we able to encode TYPE_3BYTE_BGR images to bmp27* format. Test failed if ArrayIndexOutOfBoundsException will be thrown28* or pixel colors will be changed by the writing/reading.29*/3031import java.awt.Color;32import java.awt.Dimension;33import java.awt.Graphics;34import java.awt.Graphics2D;35import java.awt.color.ColorSpace;36import java.awt.image.BufferedImage;37import java.awt.image.ColorModel;38import java.awt.image.ComponentColorModel;39import java.awt.image.DataBuffer;40import java.awt.image.Raster;41import java.awt.image.WritableRaster;42import java.io.ByteArrayInputStream;43import java.io.ByteArrayOutputStream;44import java.io.File;45import java.io.FileOutputStream;46import java.io.IOException;4748import javax.imageio.IIOImage;49import javax.imageio.ImageIO;50import javax.imageio.ImageWriter;51import javax.imageio.stream.ImageOutputStream;52import javax.swing.JComponent;53import javax.swing.JFrame;5455public class Write3ByteBgrTest {56private static int width = 100;57private static int height = 100;58private static Color color = new Color(0x10, 0x20, 0x30);5960static int bufferedImageType[] = {61BufferedImage.TYPE_CUSTOM,62BufferedImage.TYPE_BYTE_BINARY,63BufferedImage.TYPE_3BYTE_BGR64};6566static String bufferedImageStringType[] = {67"BufferedImage.TYPE_CUSTOM: test for BandedSampleModel",68"BufferedImage.TYPE_BYTE_BINARY",69"BufferedImage.TYPE_3BYTE_BGR"70};7172private static String writingFormat = "BMP";73private static ImageWriter writer = (ImageWriter)ImageIO.getImageWritersByFormatName(writingFormat).next();74private int type;7576public static void main(String[] args) {7778//int i = 0;79for(int i=0; i<bufferedImageType.length; i++) {80Write3ByteBgrTest t1 = new Write3ByteBgrTest(bufferedImageType[i]);8182System.out.println("\n\nImage test for type " + bufferedImageStringType[i]);83t1.doImageTest();84}85}8687private Write3ByteBgrTest(int type) {88this.type = type;89}9091private void doImageTest() {92try {93BufferedImage src = createTestImage(type);94BufferedImage dst = writeImage(src);9596compareImages(src, dst);97} catch (ArrayIndexOutOfBoundsException e) {98throw new RuntimeException("Test failed: index out of array bounds!");99}100}101102103private void compareImages(BufferedImage src, BufferedImage dst) {104Object dstPixel = dst.getRaster().getDataElements(width/2, height/2, null);105Object srcPixel = src.getRaster().getDataElements(width/2, height/2, null);106107if ( (src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) ||108(src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) ||109(src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) ||110(src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel)) ) {111112showPixel(src, width/2, height/2);113showPixel(dst, width/2, height/2);114115showRes(dst, src);116throw new RuntimeException(117"Colors are different: " +118Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " +119Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));120}121}122123private BufferedImage writeImage(BufferedImage src) {124try {125BufferedImage dst = null;126if (!writer.getOriginatingProvider().canEncodeImage(src)) {127throw new RuntimeException(writingFormat+" writer does not support the image type "+type);128}129System.out.println(writingFormat+" writer claims it can encode the image "+type);130ByteArrayOutputStream baos = new ByteArrayOutputStream();131ImageOutputStream ios = ImageIO.createImageOutputStream(baos);132writer.setOutput(ios);133IIOImage img = new IIOImage(src.getRaster(), null, null);134writer.write(img);135ios.close();136baos.close();137138// save to file139File f = new File("test"+src.getType()+".bmp");140FileOutputStream fos = new FileOutputStream(f);141fos.write(baos.toByteArray());142fos.close();143144145ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());146dst = ImageIO.read(bais);147return dst;148} catch (IOException e) {149e.printStackTrace();150throw new RuntimeException(e);151}152}153154private static void showPixel(BufferedImage src, int x, int y) {155System.out.println("Img is " + src);156Object p = src.getRaster().getDataElements(x, y, null);157System.out.println("RGB: " +158Integer.toHexString(src.getColorModel().getRGB(p)));159System.out.println("Red: " +160Integer.toHexString(src.getColorModel().getRed(p)));161System.out.println("Green: " +162Integer.toHexString(src.getColorModel().getGreen(p)));163System.out.println("Blue: " +164Integer.toHexString(src.getColorModel().getBlue(p)));165System.out.println("Alpha: " +166Integer.toHexString(src.getColorModel().getAlpha(p)));167}168169private static BufferedImage createTestImage(int type) {170return createTestImage(type, color);171}172173private static BufferedImage createTestImage(int type, Color c) {174BufferedImage i = null;175if (type == BufferedImage.TYPE_CUSTOM) {176WritableRaster wr = Raster.createBandedRaster(177DataBuffer.TYPE_BYTE,178width, height,179width, // scanlineStride180new int[] { 0, 1, 2},// bankIndices[],181new int[] { 1, 2, 0},// bankOffsets[],182null);183184ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);185186ColorModel cm = new ComponentColorModel(cs,187new int[] { 8, 8, 8},188false,189false,190ColorModel.OPAQUE,191DataBuffer.TYPE_BYTE);192i = new BufferedImage(cm, wr, false, null);193} else {194i = new BufferedImage(width, height, type);195}196197Graphics2D g = i.createGraphics();198199g.setColor(c);200g.fillRect(0, 0, width, height);201g.setColor(Color.white);202g.drawRect(10,10, width-20, height-20);203204return i;205}206207private static void showRes(final BufferedImage src,208final BufferedImage dst)209{210final int w = src.getWidth()+ dst.getWidth();211final int h = Math.max(src.getHeight(), dst.getHeight());212213JFrame f = new JFrame("Test results");214f.getContentPane().add( new JComponent() {215public Dimension getPreferredSize() {216return new Dimension(w,h);217}218219public void paintComponent(Graphics g) {220g.drawImage(src,0,0, null);221g.drawImage(dst, src.getWidth(),0, null);222}223});224f.pack();225f.setVisible(true);226}227}228229230