1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37 public class PngWriterTest extends AbstractPngTest {
38
39
40
41
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
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 }