1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.pnm;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.awt.image.BufferedImage;
24 import java.awt.image.DataBufferInt;
25 import java.io.IOException;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28
29 import org.apache.commons.imaging.ImageFormats;
30 import org.apache.commons.imaging.ImageInfo;
31 import org.apache.commons.imaging.Imaging;
32 import org.apache.commons.imaging.ImagingException;
33 import org.junit.jupiter.api.Test;
34
35 public class PnmImageParserTest {
36
37 private static final Charset US_ASCII = StandardCharsets.US_ASCII;
38
39 @Test
40 public void testGetImageInfo_happyCase() throws ImagingException, IOException {
41 final byte[] bytes = "P1\n3 2\n0 1 0\n1 0 1\n".getBytes(US_ASCII);
42 final PnmImagingParameters params = new PnmImagingParameters();
43 final PnmImageParser underTest = new PnmImageParser();
44 final ImageInfo results = underTest.getImageInfo(bytes, params);
45 assertEquals(results.getBitsPerPixel(), 1);
46 assertEquals(results.getWidth(), 3);
47 assertEquals(results.getHeight(), 2);
48 assertEquals(results.getNumberOfImages(), 1);
49 }
50
51 @Test
52 public void testGetImageInfo_invalidHeight() {
53 final byte[] bytes = "P1\n2 a\n0 0\n0 0\n0 0\n0 0\n0 0\n0 1\n1 1\n1 1\n1 1\n1 1\n1 1\n".getBytes(US_ASCII);
54 final PnmImagingParameters params = new PnmImagingParameters();
55 final PnmImageParser underTest = new PnmImageParser();
56 assertThrows(ImagingException.class, () -> underTest.getImageInfo(bytes, params));
57 }
58
59
60
61
62 @Test
63 public void testGetImageInfo_invalidWidth() {
64 final byte[] bytes = "P1\na 2\n0 0 0 0 0 0 0 0 0 0 0\n1 1 1 1 1 1 1 1 1 1 1\n".getBytes(US_ASCII);
65 final PnmImagingParameters params = new PnmImagingParameters();
66 final PnmImageParser underTest = new PnmImageParser();
67 assertThrows(ImagingException.class, () -> underTest.getImageInfo(bytes, params));
68 }
69
70 @Test
71 public void testGetImageInfo_missingWidthValue() {
72 final byte[] bytes = "P7\nWIDTH \n".getBytes(US_ASCII);
73 final PnmImagingParameters params = new PnmImagingParameters();
74 final PnmImageParser underTest = new PnmImageParser();
75 assertThrows(ImagingException.class, () -> underTest.getImageInfo(bytes, params));
76 }
77
78 @Test
79 public void testWriteImageRaw_happyCase() throws ImagingException, ImagingException, IOException {
80 final BufferedImage srcImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
81
82 final byte[] dstBytes = Imaging.writeImageToBytes(srcImage, ImageFormats.PNM);
83 final BufferedImage dstImage = Imaging.getBufferedImage(dstBytes);
84
85 assertEquals(srcImage.getWidth(), dstImage.getWidth());
86 assertEquals(srcImage.getHeight(), dstImage.getHeight());
87
88 final DataBufferInt srcData = (DataBufferInt) srcImage.getRaster().getDataBuffer();
89 final DataBufferInt dstData = (DataBufferInt) dstImage.getRaster().getDataBuffer();
90
91 for (int bank = 0; bank < srcData.getNumBanks(); bank++) {
92 final int[] actual = srcData.getData(bank);
93 final int[] expected = dstData.getData(bank);
94
95 assertArrayEquals(actual, expected);
96 }
97 }
98 }