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 4924507 27 * @summary Test that listeners of bmp reader receive correct events in case of 28 * BI_JPEG and BI_PNG compression types 29 */ 30 31 import java.awt.Color; 32 import java.awt.Graphics2D; 33 import java.awt.image.BufferedImage; 34 import java.io.ByteArrayInputStream; 35 import java.io.ByteArrayOutputStream; 36 import java.io.IOException; 37 import java.util.ArrayList; 38 import java.util.Iterator; 39 import java.util.List; 40 41 import javax.imageio.IIOImage; 42 import javax.imageio.ImageIO; 43 import javax.imageio.ImageReader; 44 import javax.imageio.ImageWriteParam; 45 import javax.imageio.ImageWriter; 46 import javax.imageio.event.IIOReadProgressListener; 47 import javax.imageio.event.IIOReadUpdateListener; 48 49 public class ReaderListenersTest { 50 public static final String[] compTypes = { "BI_JPEG", "BI_PNG" }; 51 52 public static void main(String[] args) { 53 for (int i=0; i< compTypes.length; i++) { 54 doTest(compTypes[i]); 55 } 56 } 57 58 private static void doTest(String compression) { 59 try { 60 BufferedImage img = createTestImage(); 61 62 ImageWriter iw = (ImageWriter) 63 ImageIO.getImageWritersByFormatName("bmp").next(); 64 if (iw == null) { 65 throw new RuntimeException("No writers for bmp format." 66 + " Test failed."); 67 } 68 69 70 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 71 iw.setOutput(ImageIO.createImageOutputStream(baos)); 72 ImageWriteParam param = iw.getDefaultWriteParam(); 73 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 74 param.setCompressionType(compression); 75 76 iw.write(null, new IIOImage(img, null, null), param); 77 baos.close(); 78 79 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 80 81 ImageReader ir = (ImageReader) 82 ImageIO.getImageReadersByFormatName("bmp").next(); 83 if (ir == null) { 84 throw new RuntimeException("No readers for bmp format." 85 + " Test failed."); 86 } 87 88 IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter(); 89 IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter(); 90 ir.addIIOReadProgressListener(progressAdapter); 91 ir.addIIOReadUpdateListener(updateAdapter); 92 ir.setInput(ImageIO.createImageInputStream(bais)); 93 BufferedImage dst = ir.read(0); 94 95 progressAdapter.checkResults(); 96 97 if (!updateAdapter.isImageUpdateUsed) { 98 throw new RuntimeException("imageUpdate was not used." 99 + " Test failed."); 100 } 101 } catch(IOException e) { 102 e.printStackTrace(); 103 throw new RuntimeException("Test failed"); 104 } 105 } 106 107 protected static BufferedImage createTestImage() { 108 BufferedImage res = new BufferedImage(100, 100, 109 BufferedImage.TYPE_INT_RGB); 110 Graphics2D g = res.createGraphics(); 111 g.setColor(Color.red); 112 g.fillRect(0,0, 100,100); 113 return res; 114 } 115 116 static class IIOReadProgressAdapter implements IIOReadProgressListener { 117 List progress = new ArrayList(); 118 public boolean isTestPassed = false; 119 private boolean isImageStarted = false; 120 private boolean isImageComplete = false; 121 private boolean isSequenceComplete = false; 122 private boolean isSequenceStarted = false; 123 124 public void imageComplete(ImageReader source) { 125 System.out.println("Image completed"); 126 if (!isImageComplete) { 127 isImageComplete = true; 128 } else { 129 throw new RuntimeException("The imageComplete() is called twice." 130 + " Test failed."); 131 } 132 checkProgress(); 133 } 134 135 public void imageProgress(ImageReader source, float percentageDone) { 136 System.out.println("Image Progress "+percentageDone); 137 progress.add(new Float(percentageDone)); 138 } 139 140 public void imageStarted(ImageReader source, int imageIndex) { 141 System.out.println("Image Started "+imageIndex); 142 if (!isImageStarted) { 143 isImageStarted = true; 144 } else { 145 throw new RuntimeException("The imageStarted() was called twice. " 146 + " Test failed."); 147 } 148 progress.clear(); 149 } 150 151 public void thumbnailComplete(ImageReader source) { 152 System.out.println("Thubnail completed"); 153 } 154 155 public void thumbnailProgress(ImageReader source, 156 float percentageDone) 157 { 158 System.out.println("Thubnail Progress " + percentageDone); 159 } 160 161 public void thumbnailStarted(ImageReader source, 162 int imageIndex, int thumbnailIndex) 163 { 164 System.out.println("Thubnail started " + imageIndex); 165 } 166 167 public void sequenceComplete(ImageReader source) { 168 if (!isSequenceComplete) { 169 isSequenceComplete = true; 170 } else { 171 throw new RuntimeException("The imageComplete() is called twice." 172 + " Test failed."); 173 } 174 } 175 176 public void sequenceStarted(ImageReader source, int minIndex) { 177 if (!isSequenceStarted) { 178 isSequenceStarted = true; 179 } else { 180 throw new RuntimeException("The imageComplete() is called twice." 181 + " Test failed."); 182 } 183 } 184 185 public void readAborted(ImageReader source) { 186 System.out.println("read Aborted"); 187 checkProgress(); 188 } 189 190 private void checkProgress() { 191 Iterator i = progress.iterator(); 192 if (!i.hasNext()) { 193 throw new RuntimeException("progress values list is empty!"); 194 } 195 float val = ((Float)i.next()).floatValue(); 196 while(i.hasNext()) { 197 float next = ((Float)i.next()).floatValue(); 198 if (val >= next) { 199 throw new RuntimeException("progress values do not increase!"); 200 } 201 val = next; 202 } 203 isTestPassed = true; 204 System.out.println("Test passed."); 205 } 206 207 public void checkResults() { 208 if (isImageStarted && !isImageComplete) { 209 throw new RuntimeException("The imageCompleted was not called." 210 + " Test failed."); 211 } 212 } 213 } 214 215 static class IIOReadUpdateAdapter implements IIOReadUpdateListener { 216 boolean isImageUpdateUsed = false; 217 public void imageUpdate(ImageReader source, BufferedImage theImage, 218 int minX, int minY, int width, int height, 219 int periodX, int periodY, int[] bands) 220 { 221 System.out.println("imageUpdate"); 222 isImageUpdateUsed = true; 223 } 224 public void passComplete(ImageReader source, BufferedImage theImage) { 225 System.out.println("passComplete"); 226 } 227 public void passStarted(ImageReader source, BufferedImage theImage, 228 int pass, int minPass, int maxPass, 229 int minX, int minY, int periodX, int periodY, 230 int[] bands) 231 { 232 System.out.println("passStarted"); 233 } 234 public void thumbnailPassComplete(ImageReader source, 235 BufferedImage theThumbnail) 236 { 237 System.out.println("thumbnailPassComplete"); 238 } 239 public void thumbnailPassStarted(ImageReader source, 240 BufferedImage theThumbnail, 241 int pass, int minPass, int maxPass, 242 int minX, int minY, 243 int periodX, int periodY, 244 int[] bands) 245 { 246 System.out.println("thumbnailPassStarted"); 247 } 248 public void thumbnailUpdate(ImageReader source, 249 BufferedImage theThumbnail, 250 int minX, int minY, 251 int width, int height, 252 int periodX, int periodY, int[] bands) 253 { 254 System.out.println("thumbnailUpdate"); 255 } 256 } 257 }