~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTestCase.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
 
 
18
/* $Id$ */
 
19
 
 
20
package org.apache.xmlgraphics.image.codec.png;
 
21
 
 
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;
 
32
 
 
33
import org.junit.Test;
 
34
 
 
35
import static org.junit.Assert.fail;
 
36
 
 
37
/**
 
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.
 
41
 *
 
42
 * @version $Id$
 
43
 */
 
44
public class PNGEncoderTestCase {
 
45
 
 
46
    @Test
 
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();
 
51
        ig.scale(.5, .5);
 
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));
 
64
        ig.dispose();
 
65
 
 
66
        image = image.getSubimage(50, 0, 50, 25);
 
67
 
 
68
        // Create an output stream where the PNG data
 
69
        // will be stored.
 
70
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
71
        OutputStream os = buildOutputStream(bos);
 
72
        try {
 
73
            // Now, try to encode image
 
74
            PNGEncodeParam params =
 
75
                PNGEncodeParam.getDefaultEncodeParam(image);
 
76
            PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params);
 
77
 
 
78
            pngImageEncoder.encode(image);
 
79
        } finally {
 
80
            os.close();
 
81
        }
 
82
 
 
83
        // Now, try to decode image
 
84
        InputStream is = buildInputStream(bos);
 
85
 
 
86
        PNGImageDecoder pngImageDecoder
 
87
            = new PNGImageDecoder(is, new PNGDecodeParam());
 
88
 
 
89
        RenderedImage decodedRenderedImage = null;
 
90
        decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
 
91
 
 
92
        BufferedImage decodedImage = null;
 
93
        if (decodedRenderedImage instanceof BufferedImage) {
 
94
            decodedImage = (BufferedImage) decodedRenderedImage;
 
95
        } else {
 
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());
 
102
            ig.dispose();
 
103
        }
 
104
 
 
105
        // Compare images
 
106
        if (!checkIdentical(image, decodedImage)) {
 
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
}