View Javadoc
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  package org.apache.commons.imaging.formats.png;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  
22  import java.awt.image.BufferedImage;
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  
27  import javax.imageio.ImageIO;
28  
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Provides a test for the PngWriter using predictors
35   */
36  public class PngWritePredictorTest {
37  
38      @BeforeAll
39      public static void setUpClass() {
40      }
41  
42      public PngWritePredictorTest() {
43      }
44  
45      /**
46       * Populate an integer pixel array for a 256-by-256 image with varied colors across the image area and a white and black line down the main diagonal.
47       *
48       * @return a valid array of integers.
49       */
50      private int[] populateARGB() {
51          // populate array with a blend of color components
52          final int[] argb = new int[256 * 256];
53          for (int i = 0; i < 256; i++) {
54              for (int j = 0; j < 256; j++) {
55                  final int red = i;
56                  final int green = 255 - i;
57                  final int blue = j;
58                  argb[i * 256 + j] = ((0xff00 | red) << 8 | green) << 8 | blue;
59              }
60          }
61  
62          // also draw a black and white strip down main diagonal
63          for (int i = 0; i < 256; i++) {
64              argb[i * 256 + i] = 0xff000000;
65              if (i < 255) {
66                  argb[i * 256 + i + 1] = 0xffffffff;
67              }
68          }
69          return argb;
70      }
71  
72      @BeforeEach
73      public void setUp() {
74      }
75  
76      @Test
77      void testWriteWithPredictor() throws IOException {
78          final int[] argb = populateARGB();
79  
80          // Test the RGB (no alpha) case
81          BufferedImage bImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
82          bImage.setRGB(0, 0, 256, 256, argb, 0, 256);
83  
84          byte[] tempFile = null;
85  
86          final PngImagingParameters params = new PngImagingParameters();
87          params.setPredictorEnabled(true);
88          final PngImageParser parser = new PngImageParser();
89          try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
90              parser.writeImage(bImage, bos, params);
91              tempFile = bos.toByteArray();
92          }
93  
94          int[] brgb = new int[256 * 256];
95          bImage = ImageIO.read(new ByteArrayInputStream(tempFile));
96          bImage.getRGB(0, 0, 256, 256, brgb, 0, 256);
97          assertArrayEquals(argb, brgb, "Round trip for RGB failed");
98  
99          // Test the ARGB (some semi-transparent alpha) case
100         for (int i = 0; i < 256; i++) {
101             argb[i * 256 + i] &= 0x88ffffff;
102         }
103         bImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
104         bImage.setRGB(0, 0, 256, 256, argb, 0, 256);
105         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
106             parser.writeImage(bImage, bos, params);
107             tempFile = bos.toByteArray();
108         }
109         brgb = new int[256 * 256];
110         bImage = ImageIO.read(new ByteArrayInputStream(tempFile));
111         bImage.getRGB(0, 0, 256, 256, brgb, 0, 256);
112         assertArrayEquals(argb, brgb, "Round trip for ARGB failed");
113 
114     }
115 }