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  package org.apache.commons.imaging.formats.png;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.awt.image.BufferedImage;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.IOException;
26  
27  import org.apache.commons.imaging.Imaging;
28  import org.apache.commons.imaging.ImagingTestConstants;
29  import org.apache.commons.imaging.palette.Palette;
30  import org.apache.commons.imaging.palette.PaletteFactory;
31  import org.apache.commons.imaging.palette.SimplePalette;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Unit tests for class {@link PngWriter}.
36   */
37  public class PngWriterTest extends AbstractPngTest {
38  
39      // The form of the test set is
40      // 0. target file name
41      // 1. Expected colour count (as String) - for testPaletteFactory
42      private static String[][] testSet = { { "1/Oregon Scientific DS6639 - DSC_0307 - small.png", "1" }, { "2/12118.png", "1" }, { "2/28569-4.png", "1" },
43              { "2/28569-8.png", "1" }, { "2/28569.png", "1" }, { "3/testImage.png", "116" }, { "3/testImageNoAlpha.png", "1" },
44              { "4/buttons_level_menu_down.ipad.png", "2" }, { "5/trns-gray.png", "26" }, { "5/trns-palette8.png", "18" }, { "5/trns-rgb.png", "26" }, };
45  
46      private static int countColors(final byte[] bytes) throws IOException {
47          final BufferedImage imageParsed = Imaging.getBufferedImage(bytes);
48          return new PaletteFactory().makeExactRgbPaletteSimple(imageParsed, Integer.MAX_VALUE).length();
49      }
50  
51      private static byte[] getImageBytes(final BufferedImage image, final PngImagingParameters params, final PaletteFactory paletteFactory) throws IOException {
52          try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
53              new PngWriter().writeImage(image, os, params, paletteFactory);
54              return os.toByteArray();
55          }
56      }
57  
58      private File getPngFile(final String name) {
59          final File pngFolder = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "png");
60          return new File(pngFolder, name);
61      }
62  
63      @Test
64      public void testNullParameters() throws IOException {
65          for (final String[] testTarget : testSet) {
66              final String filePath = testTarget[0];
67              final File imageFile = getPngFile(filePath);
68  
69              final BufferedImage image = Imaging.getBufferedImage(imageFile);
70  
71              try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
72                  new PngWriter().writeImage(image, os, null, null);
73                  final byte[] bytes = os.toByteArray();
74                  final int numColors = countColors(bytes);
75                  assertTrue(numColors > 1);
76              }
77          }
78      }
79  
80      @Test
81      public void testPaletteFactory() throws IOException {
82          for (final String[] testTarget : testSet) {
83              final String filePath = testTarget[0];
84              final File imageFile = getPngFile(filePath);
85              final int colourCount = Integer.parseInt(testTarget[1]);
86  
87              final BufferedImage image = Imaging.getBufferedImage(imageFile);
88              final PngImagingParameters params = new PngImagingParameters();
89              params.setForceIndexedColor(true);
90  
91              final byte[] bytes = getImageBytes(image, params, null);
92              final int numColors = countColors(bytes);
93              assertTrue(numColors > 1, imageFile::toString);
94  
95              final PaletteFactory factory = new PaletteFactory() {
96                  @Override
97                  public Palette makeQuantizedRgbPalette(final BufferedImage src, final int max) {
98                      // Force a palette containing nothing but black (all zero's).
99                      return new SimplePalette(new int[max]);
100                 }
101             };
102             final byte[] palettedBytes = getImageBytes(image, params, factory);
103             assertEquals(colourCount, countColors(palettedBytes), filePath);
104         }
105     }
106 }