1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.tiff;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20
21 import java.awt.image.BufferedImage;
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.commons.imaging.Imaging;
26 import org.apache.commons.imaging.ImagingTestConstants;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 public class TiffJpegTest extends TiffBaseTest {
33
34 static final String[] testSet0 = { "TestSample.tiff", "TestJpegStrips.tiff", "TestJpegTiles.tiff", "TestJpegStripsRgb.tiff", "TestJpegTilesRgb.tiff",
35 "TestJpegCMYK.tiff" };
36
37 static final String[] testSet1 = { "TestSample264x264.tiff", "TestJpegStrips264x264.tiff", "TestJpegStrips264x264BigEndian.tiff",
38 "TestJpegTiles264x264.tiff", "TestJpegTiles264x264BigEndian.tiff" };
39
40 static final String[] testSet2 = { "TestSampleArgb.tiff", "TestJpegArgb.tiff" };
41
42 static final String[] testSet3 = { "TestSampleRgb127x127.tiff", "TestJpegRgb127x127.tiff" };
43
44 private File getTiffFile(final String name) {
45 final File tiffFolder = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "tiff");
46 final File tiffJpegFolder = new File(tiffFolder, "14");
47 return new File(tiffJpegFolder, name);
48 }
49
50 private int gray(final int argb) {
51 final int r = argb >> 16 & 0xff;
52 final int g = argb >> 8 & 0xff;
53 final int b = argb & 0xff;
54 return (int) (0.299 * r + 0.587 * g + 0.114 * b + 0.5);
55 }
56
57 void performSubImageTest(final String name) throws IOException {
58
59 final File target = getTiffFile(name);
60 final BufferedImage master = Imaging.getBufferedImage(target);
61 final int w = master.getWidth();
62 final int h = master.getHeight();
63 final int[] masterArgb = new int[w * h];
64 master.getRGB(0, 0, w, h, masterArgb, 0, w);
65
66
67 final TiffImageParser tiffImageParser = new TiffImageParser();
68 final TiffImagingParameters params = new TiffImagingParameters();
69 final int testX0 = 11;
70 final int testY0 = 11;
71 final int testW = w - testX0 - 1;
72 final int testH = h - testY0 - 1;
73 params.setSubImage(testX0, testY0, testW, testH);
74
75 final BufferedImage test = tiffImageParser.getBufferedImage(target, params);
76 final int[] testArgb = new int[testW * testH];
77 test.getRGB(0, 0, testW, testH, testArgb, 0, testW);
78 final String label = "Pixel mismatch for " + name;
79 for (int i = 0; i < testH; i++) {
80 for (int j = 0; j < testH; j++) {
81 final int masterIndex = (i + testY0) * w + j + testX0;
82 final int testIndex = i * testW + j;
83 final int masterPixel = masterArgb[masterIndex];
84 final int testPixel = testArgb[testIndex];
85 assertEquals(masterPixel, testPixel, label);
86 }
87 }
88
89 }
90
91
92
93
94
95
96
97
98
99
100 private void processTestSet(final String[] testSet) throws IOException {
101 final File masterFile = getTiffFile(testSet[0]);
102 final BufferedImage masterImage = Imaging.getBufferedImage(masterFile);
103 final int w = masterImage.getWidth();
104 final int h = masterImage.getHeight();
105 final int[] masterArgb = new int[w * h];
106 masterImage.getRGB(0, 0, w, h, masterArgb, 0, w);
107
108 final int[] masterGray = new int[w * h];
109 for (int i = 0; i < masterArgb.length; i++) {
110 masterGray[i] = gray(masterArgb[i]);
111 }
112
113 for (int iFile = 1; iFile < testSet.length; iFile++) {
114 final String name = testSet[iFile];
115 final File f = getTiffFile(name);
116 final BufferedImage test = Imaging.getBufferedImage(f);
117 final int testW = test.getWidth();
118 final int testH = test.getHeight();
119 assertEquals(w, testW, "Width mismatch for " + name);
120 assertEquals(h, testH, "Height mismatch for " + name);
121
122 final String label = "Pixel match for " + name;
123 final int[] testArgb = new int[w * h];
124 test.getRGB(0, 0, w, h, testArgb, 0, w);
125 for (int i = 0; i < testArgb.length; i++) {
126 final int testGray = gray(testArgb[i]);
127 final int masterA = masterArgb[i] >>> 24;
128 final int testA = testArgb[i] >>> 24;
129 assertEquals(masterA, testA, 1, label);
130 assertEquals(masterGray[i], testGray, 4, label);
131 }
132 }
133 }
134
135
136
137
138 @Test
139 public void testFullPixelMatch() throws IOException {
140
141
142
143
144 processTestSet(testSet0);
145 processTestSet(testSet1);
146
147
148 processTestSet(testSet2);
149
150
151
152
153 processTestSet(testSet3);
154 }
155
156
157
158
159 @Test
160 public void testSubImage() throws IOException {
161 performSubImageTest("TestJpegProgressive.tiff");
162 performSubImageTest("TestJpegTiles264x264.tiff");
163 performSubImageTest("TestJpegRgb127x127.tiff");
164 }
165
166 }