Path: blob/master/test/jdk/javax/imageio/plugins/wbmp/WBMPPluginTest.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 464187226* @summary Tests writing and reading abilities of WBMP 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;5051public class WBMPPluginTest {5253private static final int[] types = {54BufferedImage.TYPE_INT_RGB, // = 1;55BufferedImage.TYPE_INT_ARGB, // = 2;56BufferedImage.TYPE_INT_ARGB_PRE, // = 3;57BufferedImage.TYPE_INT_BGR, // = 4;58BufferedImage.TYPE_3BYTE_BGR, // = 5;59BufferedImage.TYPE_4BYTE_ABGR, // = 6;60BufferedImage.TYPE_4BYTE_ABGR_PRE, // 761BufferedImage.TYPE_USHORT_565_RGB, // 862BufferedImage.TYPE_USHORT_555_RGB, // 963BufferedImage.TYPE_BYTE_GRAY, // 1064BufferedImage.TYPE_USHORT_GRAY, //1165BufferedImage.TYPE_BYTE_BINARY, //1266BufferedImage.TYPE_BYTE_INDEXED //1367};6869private static String format = "WBMP";7071private static ImageReader ir = null;72private static ImageWriter iw = null;73private BufferedImage img;74private ImageWriteParam param;75private ByteArrayOutputStream baos;7677private static void init() {7879Iterator i = ImageIO.getImageWritersByFormatName(format);80if (!i.hasNext()) {81throw new RuntimeException("No available ImageWrites for "+format+" format!");82}83iw = (ImageWriter)i.next();8485i = ImageIO.getImageReadersByFormatName(format);86if (!i.hasNext()) {87throw new RuntimeException("No available ImageReaders for " +format+" format!");88}8990ir = (ImageReader)i.next();91}9293public static void main(String[] args) {94if (args.length > 0) {95format = args[0];96System.out.println("Test format " + format);97}9899init();100ImageIO.setUseCache(false);101102for (int i=0; i<types.length; i++) {103boolean bPassed = true;104Object reason = null;105106try {107108BufferedImage image = createTestImage(types[i]);109110ImageWriteParam param = iw.getDefaultWriteParam();111112WBMPPluginTest t = new WBMPPluginTest(image, param);113boolean res = false;114res = t.test();115if (!res) {116bPassed = false;117reason = new String("Null result");118}119} catch (IllegalArgumentException ex) {120System.out.println("Expected exception type was caught: " + ex);121122} catch (Throwable ex ) {123System.out.println("FAILED");124ex.printStackTrace();125bPassed = false;126reason = ex;127throw new RuntimeException("Test for type " + types[i] + " FAILED due to exception");128}129/*130System.out.println("Type " + types[i] + " result: " +131(bPassed ? "PASSED" : "FAILED") +132((reason != null) ? (" Reason: " + reason) : ""));133*/134System.out.println("Test for type " + types[i] + " PASSED");135}136137System.out.println("END OF TEST");138}139140public WBMPPluginTest(BufferedImage img, ImageWriteParam param) {141142this.img = img;143this.param = param;144baos = new ByteArrayOutputStream();145}146147public boolean test() throws IIOException, IOException {148149ir.reset();150iw.reset();151152String[] suffixes = iw.getOriginatingProvider().getFileSuffixes();153154IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);155IIOImage iio_img = new IIOImage(img, null, md);156157System.out.println("Image type " + img.getType());158159String fname = "test"+img.getType()+"."+suffixes[0];160161iw.setOutput(ImageIO.createImageOutputStream(new FileOutputStream(new File(fname))));162System.out.print("write image ... ");163iw.write(iio_img);164System.out.println("OK");165System.out.print("read image ... ");166167byte[] ba_image = baos.toByteArray();168169ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);170171ir.setInput(ImageIO.createImageInputStream(new FileInputStream(new File(fname))));172173BufferedImage res = ir.read(0);174System.out.println("OK");175176System.out.print("compare images ... ");177boolean r = compare(img,res);178System.out.println(r?"OK":"FAILED");179return r;180}181182private boolean compare(BufferedImage in, BufferedImage out) {183int width = in.getWidth();184int height = in.getHeight();185if (out.getWidth() != width || out.getHeight() != height) {186throw new RuntimeException("Dimensions changed!");187}188189Raster oldras = in.getRaster();190ColorModel oldcm = in.getColorModel();191Raster newras = out.getRaster();192ColorModel newcm = out.getColorModel();193194for (int j = 0; j < height; j++) {195for (int i = 0; i < width; i++) {196Object oldpixel = oldras.getDataElements(i, j, null);197int oldrgb = oldcm.getRGB(oldpixel);198int oldalpha = oldcm.getAlpha(oldpixel);199200Object newpixel = newras.getDataElements(i, j, null);201int newrgb = newcm.getRGB(newpixel);202int newalpha = newcm.getAlpha(newpixel);203204if (newrgb != oldrgb ||205newalpha != oldalpha) {206throw new RuntimeException("Pixels differ at " + i +207", " + j);208}209}210}211return true;212}213214215private static BufferedImage createTestImage(int type) throws IOException {216217int w = 200;218int h = 200;219BufferedImage b = new BufferedImage(w, h, type);220Graphics2D g = b.createGraphics();221g.setColor(Color.white);222g.fillRect(0,0, w, h);223g.setColor(Color.black);224g.fillOval(10, 10, w -20, h-20);225226return b;227}228229}230231232