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 import static org.junit.jupiter.api.Assertions.fail;
21
22 import java.awt.image.BufferedImage;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.apache.commons.imaging.Imaging;
29 import org.apache.commons.imaging.ImagingException;
30 import org.junit.jupiter.api.Test;
31
32 public class TiffSubImageTest extends TiffBaseTest {
33 final List<File> imageFileList;
34
35 TiffSubImageTest() throws IOException, ImagingException {
36 imageFileList = getTiffImages();
37 }
38
39 private void processBadParams(final File target, final int x, final int y, final int width, final int height, final String comment) throws IOException {
40 final TiffImageParser tiffImageParser = new TiffImageParser();
41 try {
42 final TiffImagingParameters params = new TiffImagingParameters();
43 params.setSubImage(x, y, width, height);
44 tiffImageParser.getBufferedImage(target, params);
45 fail("Reading TIFF sub-image failed to detect bad parameter: " + comment);
46 } catch (final ImagingException | IllegalArgumentException ire) {
47
48 }
49 }
50
51 @Test
52 public void testBadSubImage() throws ImagingException, IOException {
53 final TiffImageParser tiffImageParser = new TiffImageParser();
54 final File target = imageFileList.get(0);
55 final BufferedImage referenceImage = Imaging.getBufferedImage(target);
56 final int width = referenceImage.getWidth();
57 final int height = referenceImage.getHeight();
58
59 final TiffImagingParameters params = new TiffImagingParameters();
60 params.setSubImage(0, 0, width, height);
61
62 final BufferedImage image = tiffImageParser.getBufferedImage(target, params);
63 assertEquals(image.getWidth(), width, "Improper width when sub-imaging entire image");
64 assertEquals(image.getHeight(), height, "Improper height when sub-imaging entire image");
65
66 processBadParams(target, -1, 0, width, height, "negative x position");
67 processBadParams(target, 0, -1, width, height, "negative y position");
68 processBadParams(target, 0, 0, 0, height, "zero width");
69 processBadParams(target, 0, 0, width, 0, "zero height");
70 processBadParams(target, 1, 0, width, height, "sub-image width extends beyond bounds");
71 processBadParams(target, 0, 1, width, height, "sub-image height extends beyond bounds");
72 }
73
74 @Test
75 public void testSubImage() throws ImagingException, ImagingException, IOException {
76 final TiffImageParser tiffImageParser = new TiffImageParser();
77 final BufferedImage src = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
78 final TiffImagingParameters params = new TiffImagingParameters();
79 final byte[] imageBytes;
80 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
81 tiffImageParser.writeImage(src, baos, params);
82 imageBytes = baos.toByteArray();
83 }
84
85 params.setSubImage(0, 0, 2, 3);
86 final BufferedImage image = tiffImageParser.getBufferedImage(imageBytes, params);
87 assertEquals(image.getWidth(), 2);
88 assertEquals(image.getHeight(), 3);
89 }
90
91 @Test
92 public void testSubImageCorrectness() throws ImagingException, IOException {
93 final TiffImageParser tiffImageParser = new TiffImageParser();
94 for (final File target : imageFileList) {
95 final BufferedImage referenceImage = Imaging.getBufferedImage(target);
96 final int rW = referenceImage.getWidth();
97 final int rH = referenceImage.getHeight();
98 if (rW < 3 || rH < 3) {
99 continue;
100 }
101 final int[] rArgb = new int[rW * rH];
102 referenceImage.getRGB(0, 0, rW, rH, rArgb, 0, rW);
103 final TiffImagingParameters params = new TiffImagingParameters();
104 params.setSubImage(1, 1, rW - 2, rH - 2);
105 final BufferedImage image = tiffImageParser.getBufferedImage(target, params);
106 final int iW = image.getWidth();
107 final int iH = image.getHeight();
108 assertEquals(iW, rW - 2, "Invalid subimage width");
109 assertEquals(iH, rH - 2, "Invalid subimage height");
110 final int[] iArgb = new int[iW * iH];
111 image.getRGB(0, 0, iW, iH, iArgb, 0, iW);
112 for (int i = 0; i < iH; i++) {
113 for (int j = 0; j < iW; j++) {
114 final int rTest = rArgb[(i + 1) * rW + j + 1];
115 final int iTest = iArgb[i * iW + j];
116 assertEquals(iTest, rTest, "Invalid pixel lookup for " + target.getName() + " at " + i + ", " + j);
117 }
118 }
119 }
120 }
121
122 }