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.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.BufferedOutputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.commons.imaging.ImageReadException;
35  import org.apache.commons.imaging.ImageWriteException;
36  import org.apache.commons.imaging.ImagingConstants;
37  import org.apache.commons.imaging.common.bytesource.ByteSource;
38  import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
39  import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
40  import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
41  import org.junit.Test;
42  import org.junit.runner.RunWith;
43  import org.junit.runners.Parameterized;
44  
45  @RunWith(Parameterized.class)
46  public class IptcUpdateTest extends IptcBaseTest {
47      private final File imageFile;
48  
49      @Parameterized.Parameters
50      public static Collection<File> data() throws Exception {
51          return getImagesWithIptcData();
52      }
53  
54      public IptcUpdateTest(final File imageFile) {
55          this.imageFile = imageFile;
56      }
57  
58      /*
59       * Remove all Photoshop IPTC data from a JPEG file.
60       */
61      @Test
62      public void testRemove() throws Exception {
63          final ByteSource byteSource = new ByteSourceFile(imageFile);
64  
65          final Map<String, Object> params = new HashMap<>();
66          final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
67          params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
68  
69          final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(
70                  byteSource, params);
71          assertNotNull(metadata);
72  
73          final File noIptcFile = removeIptc(byteSource);
74          
75          final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
76                  new ByteSourceFile(noIptcFile), params);
77          
78          // FIXME should either be null or empty
79          assertTrue(outMetadata == null
80                  || outMetadata.getItems().size() == 0);
81      }
82  
83      public File removeIptc(final ByteSource byteSource) throws Exception {
84          final File noIptcFile = createTempFile(imageFile.getName() + ".iptc.remove.", ".jpg");
85  
86          try (OutputStream os = new BufferedOutputStream(new FileOutputStream(noIptcFile))) {
87              new JpegIptcRewriter().removeIPTC(byteSource, os);
88          }
89          return noIptcFile;
90      }
91  
92      @Test
93      public void testInsert() throws Exception {
94          final ByteSource byteSource = new ByteSourceFile(imageFile);
95  
96          final Map<String, Object> params = new HashMap<>();
97          final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
98          params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
99  
100         final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(
101                 byteSource, params);
102         assertNotNull(metadata);
103 
104         final File noIptcFile = removeIptc(byteSource);
105 
106         final List<IptcBlock> newBlocks = new ArrayList<>();
107         final List<IptcRecord> newRecords = new ArrayList<>();
108 
109         newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
110         newRecords.add(new IptcRecord(IptcTypes.CREDIT,
111                 "William Sorensen"));
112 
113         final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords,
114                 newBlocks);
115 
116         final File updated = createTempFile(imageFile.getName()
117                 + ".iptc.insert.", ".jpg");
118         try (FileOutputStream fos = new FileOutputStream(updated);
119                 OutputStream os = new BufferedOutputStream(fos)) {
120             new JpegIptcRewriter().writeIPTC(new ByteSourceFile(
121                     noIptcFile), os, newData);
122         }
123 
124         final ByteSource updateByteSource = new ByteSourceFile(updated);
125         final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
126                 updateByteSource, params);
127 
128         assertNotNull(outMetadata);
129         assertTrue(outMetadata.getItems().size() == 2);
130     }
131 
132     @Test
133     public void testUpdate() throws Exception {
134         final ByteSource byteSource = new ByteSourceFile(imageFile);
135 
136         final Map<String, Object> params = new HashMap<>();
137         final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
138         params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
139 
140         final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(byteSource, params);
141         assertNotNull(metadata);
142 
143         final List<IptcBlock> newBlocks = metadata.photoshopApp13Data.getNonIptcBlocks();
144         final List<IptcRecord> newRecords = new ArrayList<>();
145 
146         newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
147         newRecords.add(new IptcRecord(IptcTypes.CREDIT,
148                 "William Sorensen"));
149 
150         final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords,
151                 newBlocks);
152 
153         final File updated = writeIptc(byteSource, newData);
154 
155         final ByteSource updateByteSource = new ByteSourceFile(updated);
156         final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
157                 updateByteSource, params);
158 
159         assertNotNull(outMetadata);
160         assertTrue(outMetadata.getItems().size() == 2);
161     }
162 
163     public File writeIptc(final ByteSource byteSource, final PhotoshopApp13Data newData) throws IOException, ImageReadException, ImageWriteException {
164         final File updated = createTempFile(imageFile.getName()
165                 + ".iptc.update.", ".jpg");
166         try (FileOutputStream fos = new FileOutputStream(updated);
167                 OutputStream os = new BufferedOutputStream(fos)) {
168             new JpegIptcRewriter().writeIPTC(byteSource, os, newData);
169         }
170         return updated;
171     }
172 
173     @Test
174     public void testNoChangeUpdate() throws Exception {
175         final ByteSource byteSource = new ByteSourceFile(imageFile);
176 
177         final Map<String, Object> params = new HashMap<>();
178         final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
179         params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
180 
181         final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(byteSource, params);
182         assertNotNull(metadata);
183 
184         final List<IptcBlock> newBlocks = metadata.photoshopApp13Data.getNonIptcBlocks();
185         final List<IptcRecord> oldRecords = metadata.photoshopApp13Data.getRecords();
186         final List<IptcRecord> newRecords = new ArrayList<>();
187         for (final IptcRecord record : oldRecords) {
188             if (record.iptcType != IptcTypes.CITY
189                     && record.iptcType != IptcTypes.CREDIT) {
190                 newRecords.add(record);
191             }
192         }
193 
194         newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
195         newRecords.add(new IptcRecord(IptcTypes.CREDIT, "William Sorensen"));
196 
197         final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);
198 
199         final File updated = writeIptc(byteSource, newData);
200 
201         final ByteSource updateByteSource = new ByteSourceFile(updated);
202         final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(updateByteSource, params);
203 
204         assertNotNull(outMetadata);
205         assertTrue(outMetadata.getItems().size() == newRecords.size());
206     }
207 
208 }