2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
20
package org.apache.xmlgraphics.image.codec.png;
22
import java.awt.Color;
23
import java.awt.Graphics2D;
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.Rectangle2D;
26
import java.awt.image.BufferedImage;
27
import java.awt.image.RenderedImage;
28
import java.io.ByteArrayInputStream;
29
import java.io.ByteArrayOutputStream;
30
import java.io.InputStream;
31
import java.io.OutputStream;
33
import org.junit.Test;
35
import static org.junit.Assert.fail;
38
* This test validates the PNGEncoder operation. It creates a
39
* BufferedImage, then encodes it with the PNGEncoder, then
40
* decodes it and compares the decoded image with the original one.
44
public class PNGEncoderTestCase {
47
public void testPNGEncoding() throws Exception {
48
// Create a BufferedImage to be encoded
49
BufferedImage image = new BufferedImage(100, 75, BufferedImage.TYPE_INT_ARGB);
50
Graphics2D ig = image.createGraphics();
52
ig.setPaint(new Color(128, 0, 0));
53
ig.fillRect(0, 0, 100, 50);
54
ig.setPaint(Color.orange);
55
ig.fillRect(100, 0, 100, 50);
56
ig.setPaint(Color.yellow);
57
ig.fillRect(0, 50, 100, 50);
58
ig.setPaint(Color.red);
59
ig.fillRect(100, 50, 100, 50);
60
ig.setPaint(new Color(255, 127, 127));
61
ig.fillRect(0, 100, 100, 50);
62
ig.setPaint(Color.black);
63
ig.draw(new Rectangle2D.Double(0.5, 0.5, 199, 149));
66
image = image.getSubimage(50, 0, 50, 25);
68
// Create an output stream where the PNG data
70
ByteArrayOutputStream bos = new ByteArrayOutputStream();
71
OutputStream os = buildOutputStream(bos);
73
// Now, try to encode image
74
PNGEncodeParam params =
75
PNGEncodeParam.getDefaultEncodeParam(image);
76
PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params);
78
pngImageEncoder.encode(image);
83
// Now, try to decode image
84
InputStream is = buildInputStream(bos);
86
PNGImageDecoder pngImageDecoder
87
= new PNGImageDecoder(is, new PNGDecodeParam());
89
RenderedImage decodedRenderedImage = null;
90
decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
92
BufferedImage decodedImage = null;
93
if (decodedRenderedImage instanceof BufferedImage) {
94
decodedImage = (BufferedImage) decodedRenderedImage;
96
decodedImage = new BufferedImage(decodedRenderedImage.getWidth(),
97
decodedRenderedImage.getHeight(),
98
BufferedImage.TYPE_INT_ARGB);
99
ig = decodedImage.createGraphics();
100
ig.drawRenderedImage(decodedRenderedImage,
101
new AffineTransform());
106
if (!checkIdentical(image, decodedImage)) {
107
fail("Decoded image does not match the original");
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.
116
public OutputStream buildOutputStream(ByteArrayOutputStream bos) {
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.
125
public InputStream buildInputStream(ByteArrayOutputStream bos) {
126
return new ByteArrayInputStream(bos.toByteArray());
130
* Compares the data for the two images
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)) {