KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > PNGEncoderTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.ext.awt.image.codec;
18
19 import org.apache.batik.test.*;
20 import java.awt.*;
21 import java.awt.geom.*;
22 import java.awt.image.*;
23 import java.io.*;
24
25 /**
26  * This test validates the PNGEncoder operation. It creates a
27  * BufferedImage, then encodes it with the PNGEncoder, then
28  * decodes it and compares the decoded image with the original one.
29  *
30  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: PNGEncoderTest.java,v 1.4 2005/04/01 02:28:16 deweese Exp $
32  */

33 public class PNGEncoderTest extends AbstractTest {
34     /**
35      * Error when image cannot be encoded
36      * {0} = trace for the exception which was reported
37      */

38     public static final String JavaDoc ERROR_CANNOT_ENCODE_IMAGE
39         = "PNGEncoderTest.error.cannot.encode.image";
40
41     /**
42      * Error when image cannot be decoded
43      * {0} = trace for the exception which was reported
44      */

45     public static final String JavaDoc ERROR_CANNOT_DECODE_IMAGE
46         = "PNGEncoderTest.error.cannot.decode.image";
47
48     /**
49      * Decoded image differs from encoded image
50      */

51     public static final String JavaDoc ERROR_DECODED_DOES_NOT_MATCH_ENCODED
52         = "PNGEncoderTest.error.decoded.does.not.match.encoded";
53
54     public TestReport runImpl() throws Exception JavaDoc {
55         // Create a BufferedImage to be encoded
56
BufferedImage image = new BufferedImage(100, 75, BufferedImage.TYPE_INT_ARGB);
57         Graphics2D ig = image.createGraphics();
58         ig.scale(.5, .5);
59         ig.setPaint(new Color(128,0,0));
60         ig.fillRect(0, 0, 100, 50);
61         ig.setPaint(Color.orange);
62         ig.fillRect(100, 0, 100, 50);
63         ig.setPaint(Color.yellow);
64         ig.fillRect(0, 50, 100, 50);
65         ig.setPaint(Color.red);
66         ig.fillRect(100, 50, 100, 50);
67         ig.setPaint(new Color(255, 127, 127));
68         ig.fillRect(0, 100, 100, 50);
69         ig.setPaint(Color.black);
70         ig.draw(new Rectangle2D.Double(0.5, 0.5, 199, 149));
71         ig.dispose();
72
73         image = image.getSubimage(50, 0, 50, 25);
74
75         // Create an output stream where the PNG data
76
// will be stored.
77
ByteArrayOutputStream bos = new ByteArrayOutputStream();
78         OutputStream os = buildOutputStream(bos);
79
80         // Now, try to encode image
81
PNGEncodeParam params =
82             PNGEncodeParam.getDefaultEncodeParam(image);
83         PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params);
84
85         try{
86             pngImageEncoder.encode(image);
87             os.close();
88         }catch(Exception JavaDoc e){
89             return reportException(ERROR_CANNOT_ENCODE_IMAGE, e);
90         }
91
92         // Now, try to decode image
93
InputStream is
94             = buildInputStream(bos);
95
96         PNGImageDecoder pngImageDecoder
97             = new PNGImageDecoder(is, new PNGDecodeParam());
98
99         RenderedImage decodedRenderedImage = null;
100         try{
101             decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
102         }catch(Exception JavaDoc e){
103             return reportException(ERROR_CANNOT_DECODE_IMAGE,
104                             e);
105         }
106
107         BufferedImage decodedImage = null;
108         if(decodedRenderedImage instanceof BufferedImage){
109             decodedImage = (BufferedImage)decodedRenderedImage;
110         }
111         else{
112             decodedImage = new BufferedImage(decodedRenderedImage.getWidth(),
113                                              decodedRenderedImage.getHeight(),
114                                              BufferedImage.TYPE_INT_ARGB);
115             ig = decodedImage.createGraphics();
116             ig.drawRenderedImage(decodedRenderedImage,
117                                  new AffineTransform());
118             ig.dispose();
119         }
120
121         // Compare images
122
if(checkIdentical(image, decodedImage) != true){
123             return reportError(ERROR_DECODED_DOES_NOT_MATCH_ENCODED);
124         }
125
126         return reportSuccess();
127     }
128
129     /**
130      * Template method for building the PNG output stream. This gives a
131      * chance to sub-classes (e.g., Base64PNGEncoderTest) to add an
132      * additional encoding.
133      */

134     public OutputStream buildOutputStream(ByteArrayOutputStream bos){
135         return bos;
136     }
137
138     /**
139      * Template method for building the PNG input stream. This gives a
140      * chance to sub-classes (e.g., Base64PNGEncoderTest) to add an
141      * additional decoding.
142      */

143     public InputStream buildInputStream(ByteArrayOutputStream bos){
144         return new ByteArrayInputStream(bos.toByteArray());
145     }
146
147     /**
148      * Compares the data for the two images
149      */

150     public static boolean checkIdentical(BufferedImage imgA,
151                                          BufferedImage imgB){
152         boolean identical = true;
153         if(imgA.getWidth() == imgB.getWidth()
154            &&
155            imgA.getHeight() == imgB.getHeight()){
156             int w = imgA.getWidth();
157             int h = imgA.getHeight();
158             for(int i=0; i<h; i++){
159                 for(int j=0; j<w; j++){
160                     if(imgA.getRGB(j,i) != imgB.getRGB(j,i)){
161                         identical = false;
162                         break;
163                     }
164                 }
165                 if( !identical ){
166                     break;
167                 }
168             }
169         }
170
171         return identical;
172     }
173
174 }
175
Popular Tags