1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36 public class PngWritePredictorTest {
37
38 @BeforeAll
39 public static void setUpClass() {
40 }
41
42 public PngWritePredictorTest() {
43 }
44
45
46
47
48
49
50 private int[] populateARGB() {
51
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
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
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
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 }