1 /*
2  * Copyright (c) 2012, 2013, 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 4892259
27  *
28  * @summary Verify that calls to IIOReadUpdateListener passStarted and
29  * passComplete are consistent.
30  *
31  * @run main GIFPassListenerTest
32  */
33 
34 import java.awt.image.*;
35 import java.io.*;
36 import java.util.*;
37 import javax.imageio.*;
38 import javax.imageio.event.*;
39 import javax.imageio.stream.*;
40 
41 public class GIFPassListenerTest {
42 
createTestImageStream(boolean progressive)43     private static ImageInputStream createTestImageStream(boolean progressive) throws IOException {
44         ByteArrayOutputStream output = new ByteArrayOutputStream();
45         Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("gif");
46         if (!writers.hasNext()) {
47             return null;
48         }
49         ImageWriter writer = writers.next();
50         ImageWriteParam param = writer.getDefaultWriteParam();
51         param.setProgressiveMode(progressive ?
52                 ImageWriteParam.MODE_DEFAULT : ImageWriteParam.MODE_DISABLED);
53         ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
54         writer.setOutput(imageOutput);
55         BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
56         writer.write(null, new IIOImage(image, null, null), param);
57         imageOutput.flush();
58         ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
59         return ImageIO.createImageInputStream(input);
60     }
61 
checkImage(boolean progressive)62     private static void checkImage(boolean progressive) throws Exception {
63         ImageInputStream iis = createTestImageStream(progressive);
64         ImageReader reader = ImageIO.getImageReaders(iis).next();
65         reader.setInput(iis);
66         ReadUpdateHandler handler = new ReadUpdateHandler();
67         reader.addIIOReadUpdateListener(handler);
68         reader.readAll(null);
69         if (handler.isPassStarted) {
70             throw new RuntimeException("passStarted without passComplete.");
71         }
72         if (progressive && (handler.numPasses == 0)) {
73             throw new RuntimeException("passStarted wasn't called for progressive image");
74         }
75         if (!progressive && (handler.numPasses != 0)) {
76             throw new RuntimeException("passStarted was called for non-progressive image");
77         }
78         iis.close();
79     }
80 
main(String args[])81     public static void main(String args[]) throws Exception {
82         checkImage(true);
83         checkImage(false);
84     }
85 
86     private static class ReadUpdateHandler implements IIOReadUpdateListener {
87         public boolean isPassStarted = false;
88         public int numPasses = 0;
89 
90         @Override
imageUpdate(ImageReader source, BufferedImage theImage, int minX, int minY, int width, int height, int periodX, int periodY, int[] bands)91         public void imageUpdate(ImageReader source, BufferedImage theImage, int minX, int minY,
92                 int width, int height, int periodX, int periodY, int[] bands) {
93         }
94 
95         @Override
passStarted(ImageReader source, BufferedImage theImage, int pass, int minPass, int maxPass, int minX, int minY, int periodX, int periodY, int[] bands)96         public void passStarted(ImageReader source, BufferedImage theImage, int pass, int minPass,
97                 int maxPass, int minX, int minY, int periodX, int periodY, int[] bands) {
98             if (isPassStarted) {
99                 throw new RuntimeException("reentered passStarted!");
100             }
101             isPassStarted = true;
102             numPasses++;
103         }
104 
105         @Override
passComplete(ImageReader source, BufferedImage theImage)106         public void passComplete(ImageReader source, BufferedImage theImage) {
107             if (!isPassStarted) {
108                 throw new RuntimeException("passComplete without passStarted!");
109             }
110             isPassStarted = false;
111         }
112 
113         @Override
thumbnailPassStarted(ImageReader source, BufferedImage theThumbnail, int pass, int minPass, int maxPass, int minX, int minY, int periodX, int periodY, int[] bands)114         public void thumbnailPassStarted(ImageReader source, BufferedImage theThumbnail, int pass,
115                 int minPass, int maxPass, int minX, int minY, int periodX, int periodY, int[] bands) {
116         }
117 
118         @Override
thumbnailPassComplete(ImageReader source, BufferedImage theThumbnail)119         public void thumbnailPassComplete(ImageReader source, BufferedImage theThumbnail) {
120         }
121 
122         @Override
thumbnailUpdate(ImageReader source, BufferedImage theThumbnail, int minX, int minY, int width, int height, int periodX, int periodY, int[] bands)123         public void thumbnailUpdate(ImageReader source, BufferedImage theThumbnail, int minX, int minY,
124                 int width, int height, int periodX, int periodY, int[] bands) {
125         }
126     }
127 }
128