Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BmpBigDestinationTest.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 492936726* @summary tests what BMP image was decoded correctly if destination buffered27* image is bigger than source image28*/2930import java.awt.Color;31import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.IOException;36import java.util.Iterator;3738import javax.imageio.ImageIO;39import javax.imageio.ImageReadParam;40import javax.imageio.ImageReader;41import javax.imageio.ImageTypeSpecifier;42import javax.imageio.ImageWriter;4344public class BmpBigDestinationTest {45static String format = "BMP";46public static void main(String[] args) {47try {48BufferedImage src = new BufferedImage(100, 100,49BufferedImage.TYPE_INT_RGB);50Graphics2D g = src.createGraphics();51g.setColor(Color.red);52g.fillRect(0,0,100, 100);5354ByteArrayOutputStream baos = new ByteArrayOutputStream();5556ImageWriter iw =57(ImageWriter)ImageIO.getImageWritersByFormatName(format).next();58if (iw == null) {59throw new RuntimeException("No writer available. Test failed.");60}6162iw.setOutput(ImageIO.createImageOutputStream(baos));63iw.write(src);6465byte[] data = baos.toByteArray();6667ImageReader ir =68(ImageReader)ImageIO.getImageReadersByFormatName(format).next();69ir.setInput(70ImageIO.createImageInputStream(71new ByteArrayInputStream(data)));7273Iterator specifiers = ir.getImageTypes(0);74ImageTypeSpecifier typeSpecifier = null;7576if (specifiers.hasNext()) {77typeSpecifier = (ImageTypeSpecifier) specifiers.next();78}79ImageReadParam param = new ImageReadParam();80BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);81param.setDestination(dst);8283ir.read(0, param);8485checkResults(src,dst);8687} catch (IOException e) {88e.printStackTrace();89throw new RuntimeException("Unexpected exception. Test failed.");90}91}9293private static void checkResults(BufferedImage src, BufferedImage dst) {94for(int x=0; x<src.getWidth(); x++) {95for(int y=0; y<src.getHeight(); y++) {96int srcRgb = src.getRGB(x,y);97int dstRgb = dst.getRGB(x,y);98if (srcRgb != dstRgb) {99throw new RuntimeException("Images are different at point ["100+ x + "," + y + "]");101}102}103}104}105}106107108