1 /* 2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4641872 27 * @summary Tests writing and reading abilities of WBMP plugin 28 */ 29 30 import java.awt.Color; 31 import java.awt.Graphics2D; 32 import java.awt.image.BufferedImage; 33 import java.awt.image.ColorModel; 34 import java.awt.image.Raster; 35 import java.io.ByteArrayInputStream; 36 import java.io.ByteArrayOutputStream; 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.FileOutputStream; 40 import java.io.IOException; 41 import java.util.Iterator; 42 43 import javax.imageio.IIOException; 44 import javax.imageio.IIOImage; 45 import javax.imageio.ImageIO; 46 import javax.imageio.ImageReader; 47 import javax.imageio.ImageTypeSpecifier; 48 import javax.imageio.ImageWriteParam; 49 import javax.imageio.ImageWriter; 50 import javax.imageio.metadata.IIOMetadata; 51 52 public class WBMPPluginTest { 53 54 private static final int[] types = { 55 BufferedImage.TYPE_INT_RGB, // = 1; 56 BufferedImage.TYPE_INT_ARGB, // = 2; 57 BufferedImage.TYPE_INT_ARGB_PRE, // = 3; 58 BufferedImage.TYPE_INT_BGR, // = 4; 59 BufferedImage.TYPE_3BYTE_BGR, // = 5; 60 BufferedImage.TYPE_4BYTE_ABGR, // = 6; 61 BufferedImage.TYPE_4BYTE_ABGR_PRE, // 7 62 BufferedImage.TYPE_USHORT_565_RGB, // 8 63 BufferedImage.TYPE_USHORT_555_RGB, // 9 64 BufferedImage.TYPE_BYTE_GRAY, // 10 65 BufferedImage.TYPE_USHORT_GRAY, //11 66 BufferedImage.TYPE_BYTE_BINARY, //12 67 BufferedImage.TYPE_BYTE_INDEXED //13 68 }; 69 70 private static String format = "WBMP"; 71 72 private static ImageReader ir = null; 73 private static ImageWriter iw = null; 74 private BufferedImage img; 75 private ImageWriteParam param; 76 private ByteArrayOutputStream baos; 77 78 private static void init() { 79 80 Iterator i = ImageIO.getImageWritersByFormatName(format); 81 if (!i.hasNext()) { 82 throw new RuntimeException("No available ImageWrites for "+format+" format!"); 83 } 84 iw = (ImageWriter)i.next(); 85 86 i = ImageIO.getImageReadersByFormatName(format); 87 if (!i.hasNext()) { 88 throw new RuntimeException("No available ImageReaders for " +format+" format!"); 89 } 90 91 ir = (ImageReader)i.next(); 92 } 93 94 public static void main(String[] args) { 95 if (args.length > 0) { 96 format = args[0]; 97 System.out.println("Test format " + format); 98 } 99 100 init(); 101 ImageIO.setUseCache(false); 102 103 for (int i=0; i<types.length; i++) { 104 boolean bPassed = true; 105 Object reason = null; 106 107 try { 108 109 BufferedImage image = createTestImage(types[i]); 110 111 ImageWriteParam param = iw.getDefaultWriteParam(); 112 113 WBMPPluginTest t = new WBMPPluginTest(image, param); 114 boolean res = false; 115 res = t.test(); 116 if (!res) { 117 bPassed = false; 118 reason = new String("Null result"); 119 } 120 } catch (IllegalArgumentException ex) { 121 System.out.println("Expected exception type was caught: " + ex); 122 123 } catch (Throwable ex ) { 124 System.out.println("FAILED"); 125 ex.printStackTrace(); 126 bPassed = false; 127 reason = ex; 128 throw new RuntimeException("Test for type " + types[i] + " FAILED due to exception"); 129 } 130 /* 131 System.out.println("Type " + types[i] + " result: " + 132 (bPassed ? "PASSED" : "FAILED") + 133 ((reason != null) ? (" Reason: " + reason) : "")); 134 */ 135 System.out.println("Test for type " + types[i] + " PASSED"); 136 } 137 138 System.out.println("END OF TEST"); 139 } 140 141 public WBMPPluginTest(BufferedImage img, ImageWriteParam param) { 142 143 this.img = img; 144 this.param = param; 145 baos = new ByteArrayOutputStream(); 146 } 147 148 public boolean test() throws IIOException, IOException { 149 150 ir.reset(); 151 iw.reset(); 152 153 String[] suffixes = iw.getOriginatingProvider().getFileSuffixes(); 154 155 IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param); 156 IIOImage iio_img = new IIOImage(img, null, md); 157 158 System.out.println("Image type " + img.getType()); 159 160 String fname = "test"+img.getType()+"."+suffixes[0]; 161 162 iw.setOutput(ImageIO.createImageOutputStream(new FileOutputStream(new File(fname)))); 163 System.out.print("write image ... "); 164 iw.write(iio_img); 165 System.out.println("OK"); 166 System.out.print("read image ... "); 167 168 byte[] ba_image = baos.toByteArray(); 169 170 ByteArrayInputStream bais = new ByteArrayInputStream(ba_image); 171 172 ir.setInput(ImageIO.createImageInputStream(new FileInputStream(new File(fname)))); 173 174 BufferedImage res = ir.read(0); 175 System.out.println("OK"); 176 177 System.out.print("compare images ... "); 178 boolean r = compare(img,res); 179 System.out.println(r?"OK":"FAILED"); 180 return r; 181 } 182 183 private boolean compare(BufferedImage in, BufferedImage out) { 184 int width = in.getWidth(); 185 int height = in.getHeight(); 186 if (out.getWidth() != width || out.getHeight() != height) { 187 throw new RuntimeException("Dimensions changed!"); 188 } 189 190 Raster oldras = in.getRaster(); 191 ColorModel oldcm = in.getColorModel(); 192 Raster newras = out.getRaster(); 193 ColorModel newcm = out.getColorModel(); 194 195 for (int j = 0; j < height; j++) { 196 for (int i = 0; i < width; i++) { 197 Object oldpixel = oldras.getDataElements(i, j, null); 198 int oldrgb = oldcm.getRGB(oldpixel); 199 int oldalpha = oldcm.getAlpha(oldpixel); 200 201 Object newpixel = newras.getDataElements(i, j, null); 202 int newrgb = newcm.getRGB(newpixel); 203 int newalpha = newcm.getAlpha(newpixel); 204 205 if (newrgb != oldrgb || 206 newalpha != oldalpha) { 207 throw new RuntimeException("Pixels differ at " + i + 208 ", " + j); 209 } 210 } 211 } 212 return true; 213 } 214 215 216 private static BufferedImage createTestImage(int type) throws IOException { 217 218 int w = 200; 219 int h = 200; 220 BufferedImage b = new BufferedImage(w, h, type); 221 Graphics2D g = b.createGraphics(); 222 g.setColor(Color.white); 223 g.fillRect(0,0, w, h); 224 g.setColor(Color.black); 225 g.fillOval(10, 10, w -20, h-20); 226 227 return b; 228 } 229 230 }