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  
18  package org.apache.commons.imaging.formats.jpeg.iptc;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.stream.Stream;
28  
29  import org.apache.commons.imaging.bytesource.ByteSource;
30  import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
31  import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
32  import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.MethodSource;
35  
36  public class IptcAddTest extends IptcBaseTest {
37  
38      public static Stream<File> data() throws Exception {
39          return getJpegImages().stream();
40      }
41  
42      /*
43       * Add a few IPTC values to JPEG images, whether or not they have existing IPTC data.
44       */
45      @ParameterizedTest
46      @MethodSource("data")
47      public void testAddIptcData(final File imageFile) throws Exception {
48          final ByteSource byteSource = ByteSource.file(imageFile);
49  
50          final JpegImagingParameters params = new JpegImagingParameters();
51  
52          final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(byteSource, params);
53          if (metadata == null) {
54              // FIXME select only files with meta for this test
55              return;
56          }
57  
58          final List<IptcBlock> newBlocks = new ArrayList<>(metadata.photoshopApp13Data.getNonIptcBlocks());
59          final List<IptcRecord> oldRecords = metadata.photoshopApp13Data.getRecords();
60  
61          final List<IptcRecord> newRecords = new ArrayList<>();
62          for (final IptcRecord record : oldRecords) {
63              if (record.iptcType != IptcTypes.CITY && record.iptcType != IptcTypes.CREDIT) {
64                  newRecords.add(record);
65              }
66          }
67  
68          newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
69          newRecords.add(new IptcRecord(IptcTypes.CREDIT, "William Sorensen"));
70  
71          final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);
72  
73          byte[] bytes;
74          try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
75              new JpegIptcRewriter().writeIptc(byteSource, os, newData);
76              bytes = os.toByteArray();
77          }
78  
79          final ByteSource updateByteSource = ByteSource.array(bytes, "test.jpg");
80          final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(updateByteSource, params);
81  
82          assertNotNull(outMetadata);
83          assertEquals(outMetadata.getItems().size(), newRecords.size());
84      }
85  }