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.jpeg.iptc;
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.assertTrue;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.charset.StandardCharsets;
27  import java.util.List;
28  
29  import org.apache.commons.imaging.ImagingException;
30  import org.apache.commons.imaging.bytesource.ByteSource;
31  import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
32  import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
33  import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
34  import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
35  import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
36  import org.apache.commons.imaging.test.TestResources;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Tests for the {#link {@link IptcParser} class.
41   */
42  public class IptcParserTest {
43  
44      /**
45       * Tests for IptcParser encoding support. See IMAGING-168 and pull request #124 for more.
46       *
47       * @throws IOException      when reading input
48       * @throws ImagingException when parsing file
49       */
50      @Test
51      public void testEncodingSupport() throws IOException, ImagingException {
52          // NOTE: We use the JpegParser, so it will send only the block/segment that IptcParser needs for the test image
53          final File file = TestResources.resourceToFile("/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg");
54          final JpegImageParser parser = new JpegImageParser();
55          final JpegImageMetadata metadata = (JpegImageMetadata) parser.getMetadata(file);
56          final JpegPhotoshopMetadata photoshopMetadata = metadata.getPhotoshop();
57          @SuppressWarnings("unchecked")
58          final List<GenericImageMetadataItem> items = (List<GenericImageMetadataItem>) photoshopMetadata.getItems();
59          final GenericImageMetadataItem thanksInMandarin = items.get(3);
60          // converted the thank-you in chinese characters to unicode for comparison here
61          assertArrayEquals("\u8c22\u8c22".getBytes(StandardCharsets.UTF_8), thanksInMandarin.getText().getBytes(StandardCharsets.UTF_8));
62      }
63  
64      /**
65       * Some block types (or Image Resource Blocks in Photoshop specification) have a recommendation to not be interpreted by parsers, as they are handled by
66       * Photoshop in a special way, that varies by platform (e.g. Mac, Windows, etc).
67       *
68       * IMAGING-246 provided a test image with APP13 segments, with some of the block types, such as 1084. When the IptcParser is reading this block, the 4-bytes
69       * length record may give a value that is larger than the block size.
70       *
71       * When such a case happens, the IptcParser fails with an exception, to avoid a memory error. To prevent that, the parser must be able to skip these blocks
72       * that the specification says "It is recommended that you do not interpret or use this data".
73       *
74       * @throws IOException      when reading input
75       * @throws ImagingException when parsing file
76       * @see <a href="https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/">Adobe Photoshop File Formats Specification</a>
77       */
78      @Test
79      public void testSkipBlockTypes() throws ImagingException, IOException {
80          final File imageFile = TestResources.resourceToFile("/images/jpeg/photoshop/IMAGING-246/FallHarvestKitKat_07610.jpg");
81          final JpegImageMetadata metadata = (JpegImageMetadata) new JpegImageParser().getMetadata(ByteSource.file(imageFile), new JpegImagingParameters());
82          final JpegPhotoshopMetadata photoshopMetadata = metadata.getPhotoshop();
83          final PhotoshopApp13Data photoshopApp13Data = photoshopMetadata.photoshopApp13Data;
84          final List<IptcBlock> blocks = photoshopApp13Data.getRawBlocks();
85          assertEquals(2, blocks.size());
86          for (final IptcBlock block : blocks) {
87              if (block.getBlockType() == 1028 || block.getBlockType() == 1061) {
88                  // 0x0404 IPTC-NAA record
89                  // 0x0425 (Photoshop 7.0) Caption digest
90                  final byte[] data = block.getBlockData();
91                  assertTrue(data.length > 0);
92              } else {
93                  fail("Unexpected block type found: " + block.getBlockType());
94              }
95          }
96      }
97  }