001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PNGEncoderTest.java 447277 2006-09-18 06:19:34Z jeremias $ */
019:
020: package org.apache.xmlgraphics.image.codec.png;
021:
022: import java.awt.Color;
023: import java.awt.Graphics2D;
024: import java.awt.geom.AffineTransform;
025: import java.awt.geom.Rectangle2D;
026: import java.awt.image.BufferedImage;
027: import java.awt.image.RenderedImage;
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * This test validates the PNGEncoder operation. It creates a
037: * BufferedImage, then encodes it with the PNGEncoder, then
038: * decodes it and compares the decoded image with the original one.
039: *
040: * @author <a href="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
041: * @version $Id: PNGEncoderTest.java 447277 2006-09-18 06:19:34Z jeremias $
042: */
043: public class PNGEncoderTest extends TestCase {
044:
045: public void testPNGEncoding() throws Exception {
046: // Create a BufferedImage to be encoded
047: BufferedImage image = new BufferedImage(100, 75,
048: BufferedImage.TYPE_INT_ARGB);
049: Graphics2D ig = image.createGraphics();
050: ig.scale(.5, .5);
051: ig.setPaint(new Color(128, 0, 0));
052: ig.fillRect(0, 0, 100, 50);
053: ig.setPaint(Color.orange);
054: ig.fillRect(100, 0, 100, 50);
055: ig.setPaint(Color.yellow);
056: ig.fillRect(0, 50, 100, 50);
057: ig.setPaint(Color.red);
058: ig.fillRect(100, 50, 100, 50);
059: ig.setPaint(new Color(255, 127, 127));
060: ig.fillRect(0, 100, 100, 50);
061: ig.setPaint(Color.black);
062: ig.draw(new Rectangle2D.Double(0.5, 0.5, 199, 149));
063: ig.dispose();
064:
065: image = image.getSubimage(50, 0, 50, 25);
066:
067: // Create an output stream where the PNG data
068: // will be stored.
069: ByteArrayOutputStream bos = new ByteArrayOutputStream();
070: OutputStream os = buildOutputStream(bos);
071: try {
072: // Now, try to encode image
073: PNGEncodeParam params = PNGEncodeParam
074: .getDefaultEncodeParam(image);
075: PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os,
076: params);
077:
078: pngImageEncoder.encode(image);
079: } finally {
080: os.close();
081: }
082:
083: // Now, try to decode image
084: InputStream is = buildInputStream(bos);
085:
086: PNGImageDecoder pngImageDecoder = new PNGImageDecoder(is,
087: new PNGDecodeParam());
088:
089: RenderedImage decodedRenderedImage = null;
090: decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
091:
092: BufferedImage decodedImage = null;
093: if (decodedRenderedImage instanceof BufferedImage) {
094: decodedImage = (BufferedImage) decodedRenderedImage;
095: } else {
096: decodedImage = new BufferedImage(decodedRenderedImage
097: .getWidth(), decodedRenderedImage.getHeight(),
098: BufferedImage.TYPE_INT_ARGB);
099: ig = decodedImage.createGraphics();
100: ig.drawRenderedImage(decodedRenderedImage,
101: new AffineTransform());
102: ig.dispose();
103: }
104:
105: // Compare images
106: if (checkIdentical(image, decodedImage) != true) {
107: fail("Decoded image does not match the original");
108: }
109: }
110:
111: /**
112: * Template method for building the PNG output stream. This gives a
113: * chance to sub-classes (e.g., Base64PNGEncoderTest) to add an
114: * additional encoding.
115: */
116: public OutputStream buildOutputStream(ByteArrayOutputStream bos) {
117: return bos;
118: }
119:
120: /**
121: * Template method for building the PNG input stream. This gives a
122: * chance to sub-classes (e.g., Base64PNGEncoderTest) to add an
123: * additional decoding.
124: */
125: public InputStream buildInputStream(ByteArrayOutputStream bos) {
126: return new ByteArrayInputStream(bos.toByteArray());
127: }
128:
129: /**
130: * Compares the data for the two images
131: */
132: public static boolean checkIdentical(BufferedImage imgA,
133: BufferedImage imgB) {
134: boolean identical = true;
135: if (imgA.getWidth() == imgB.getWidth()
136: && imgA.getHeight() == imgB.getHeight()) {
137: int w = imgA.getWidth();
138: int h = imgA.getHeight();
139: for (int i = 0; i < h; i++) {
140: for (int j = 0; j < w; j++) {
141: if (imgA.getRGB(j, i) != imgB.getRGB(j, i)) {
142: identical = false;
143: break;
144: }
145: }
146: if (!identical) {
147: break;
148: }
149: }
150: }
151:
152: return identical;
153: }
154:
155: }
|