View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * If an invalid width is specified, should throw {@link ImagingException} rather than {@link NumberFormatException}.
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  }