Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BMPPluginTest.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 4641872 489219426* @summary Tests writing and reading abilities of BMP plugin27*/2829import java.awt.Color;30import java.awt.Graphics2D;31import java.awt.image.BufferedImage;32import java.awt.image.ColorModel;33import java.awt.image.Raster;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.File;37import java.io.FileInputStream;38import java.io.FileOutputStream;39import java.io.IOException;40import java.util.Iterator;4142import javax.imageio.IIOException;43import javax.imageio.IIOImage;44import javax.imageio.ImageIO;45import javax.imageio.ImageReader;46import javax.imageio.ImageTypeSpecifier;47import javax.imageio.ImageWriteParam;48import javax.imageio.ImageWriter;49import javax.imageio.metadata.IIOMetadata;50import javax.imageio.spi.ImageWriterSpi;5152public class BMPPluginTest {5354private static final int[] types = {55BufferedImage.TYPE_INT_RGB, // = 1;56BufferedImage.TYPE_INT_ARGB, // = 2;57BufferedImage.TYPE_INT_ARGB_PRE, // = 3;58BufferedImage.TYPE_INT_BGR, // = 4;59BufferedImage.TYPE_3BYTE_BGR, // = 5;60BufferedImage.TYPE_4BYTE_ABGR, // = 6;61BufferedImage.TYPE_4BYTE_ABGR_PRE, // 762BufferedImage.TYPE_USHORT_565_RGB, // 863BufferedImage.TYPE_USHORT_555_RGB, // 964BufferedImage.TYPE_BYTE_GRAY, // 1065BufferedImage.TYPE_USHORT_GRAY, //1166BufferedImage.TYPE_BYTE_BINARY, //1267BufferedImage.TYPE_BYTE_INDEXED //1368};6970private static String format = "BMP";7172private static ImageReader ir = null;73private static ImageWriter iw = null;74private BufferedImage img;75private ImageWriteParam param;76private ByteArrayOutputStream baos;7778private static void init() {7980Iterator i = ImageIO.getImageWritersByFormatName(format);81if (!i.hasNext()) {82throw new RuntimeException("No available ImageWrites for "+format+" format!");83}84iw = (ImageWriter)i.next();8586i = ImageIO.getImageReadersByFormatName(format);87if (!i.hasNext()) {88throw new RuntimeException("No available ImageReaders for " +format+" format!");89}9091ir = (ImageReader)i.next();92}9394public static void main(String[] args) {95if (args.length > 0) {96format = args[0];97System.out.println("Test format " + format);98}99100init();101System.out.println("IR="+ir);102System.out.println("IW="+iw);103ImageIO.setUseCache(false);104105for (int i=0; i<types.length; i++) {106boolean bPassed = true;107Object reason = null;108109try {110111BufferedImage image = createTestImage(types[i]);112113ImageWriteParam param = iw.getDefaultWriteParam();114115BMPPluginTest t = new BMPPluginTest(image, param);116boolean res = false;117res = t.test();118if (!res) {119bPassed = false;120reason = new String("Null result");121}122} catch (IllegalArgumentException ex) {123System.out.println("Expected exception type was caught: " + ex);124125} catch (Throwable ex ) {126System.out.println("FAILED");127ex.printStackTrace();128bPassed = false;129reason = ex;130throw new RuntimeException("Test for type " + types[i] + " FAILED due to exception");131}132/*133System.out.println("Type " + types[i] + " result: " +134(bPassed ? "PASSED" : "FAILED") +135((reason != null) ? (" Reason: " + reason) : ""));136*/137System.out.println("Test for type " + types[i] + " PASSED");138}139140System.out.println("END OF TEST");141}142143public BMPPluginTest(BufferedImage img, ImageWriteParam param) {144145this.img = img;146this.param = param;147baos = new ByteArrayOutputStream();148}149150public boolean test() throws IIOException, IOException {151152ir.reset();153iw.reset();154155String[] suffixes = iw.getOriginatingProvider().getFileSuffixes();156157IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);158159System.out.println("Image type " + img.getType());160161ImageWriterSpi spi = iw.getOriginatingProvider();162boolean bCanEncode = spi.canEncodeImage(img);163164System.out.println("Can encode image? " + (bCanEncode ? "YES" : "NO"));165if (!bCanEncode) {166return true;167}168IIOImage iio_img = new IIOImage(img, null, md);169170String fname = "test"+img.getType()+"."+suffixes[0];171172iw.setOutput(ImageIO.createImageOutputStream(new FileOutputStream(new File(fname))));173System.out.print("write image ... ");174iw.write(iio_img);175System.out.println("OK");176System.out.print("read image ... ");177178byte[] ba_image = baos.toByteArray();179180ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);181182ir.setInput(ImageIO.createImageInputStream(new FileInputStream(new File(fname))));183184BufferedImage res = ir.read(0);185System.out.println("OK");186187System.out.print("compare images ... ");188boolean r = compare(img,res);189System.out.println(r?"OK":"FAILED");190return r;191}192193private boolean compare(BufferedImage in, BufferedImage out) {194int width = in.getWidth();195int height = in.getHeight();196if (out.getWidth() != width || out.getHeight() != height) {197throw new RuntimeException("Dimensions changed!");198}199200Raster oldras = in.getRaster();201ColorModel oldcm = in.getColorModel();202Raster newras = out.getRaster();203ColorModel newcm = out.getColorModel();204205for (int j = 0; j < height; j++) {206for (int i = 0; i < width; i++) {207Object oldpixel = oldras.getDataElements(i, j, null);208int oldrgb = oldcm.getRGB(oldpixel);209int oldalpha = oldcm.getAlpha(oldpixel);210211Object newpixel = newras.getDataElements(i, j, null);212int newrgb = newcm.getRGB(newpixel);213int newalpha = newcm.getAlpha(newpixel);214215if (newrgb != oldrgb ||216newalpha != oldalpha) {217throw new RuntimeException("Pixels differ at " + i +218", " + j);219}220}221}222return true;223}224225226private static BufferedImage createTestImage(int type) throws IOException {227228int w = 200;229int h = 200;230BufferedImage b = new BufferedImage(w, h, type);231Graphics2D g = b.createGraphics();232g.setColor(Color.white);233g.fillRect(0,0, w, h);234g.setColor(Color.black);235g.fillOval(10, 10, w -20, h-20);236237return b;238}239240}241242243