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  
22  import java.awt.Graphics2D;
23  import java.awt.image.BufferedImage;
24  import java.io.ByteArrayOutputStream;
25  import java.util.Collections;
26  
27  import javax.imageio.ImageIO;
28  
29  import org.junit.jupiter.api.Test;
30  
31  public class IptcFullDiscardTest {
32  
33      private byte[] addMetaData(final byte[] bytes) throws Exception {
34          final IptcRecord record = new IptcRecord(IptcTypes.KEYWORDS, "meta; data");
35          final PhotoshopApp13Data data = new PhotoshopApp13Data(Collections.singletonList(record), Collections.emptyList());
36          final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
37          new JpegIptcRewriter().writeIptc(bytes, byteArrayOutputStream, data);
38          return byteArrayOutputStream.toByteArray();
39      }
40  
41      private byte[] generateImage() throws Exception {
42          final BufferedImage image = new BufferedImage(100, 50, BufferedImage.TYPE_3BYTE_BGR); // was TYPE_INT_ARGB but that fails on Continuum
43          final Graphics2D graphics2D = image.createGraphics();
44          graphics2D.drawString("Hello World!", 10, 10);
45          final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
46          ImageIO.write(image, "jpg", byteArrayOutputStream);
47          return byteArrayOutputStream.toByteArray();
48      }
49  
50      private byte[] removeMetaData(final byte[] bytes, final boolean removeApp13Segment) throws Exception {
51          final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
52          new JpegIptcRewriter().removeIptc(bytes, byteArrayOutputStream, removeApp13Segment);
53          return byteArrayOutputStream.toByteArray();
54      }
55  
56      @Test
57      public void testLeaveApp13Segment() throws Exception {
58          final byte[] originalImage = generateImage();
59          final byte[] taggedImage = addMetaData(originalImage);
60          final byte[] untaggedImage = removeMetaData(taggedImage, false);
61          assertEquals(18, untaggedImage.length - originalImage.length);
62      }
63  
64      @Test
65      public void testRemoveApp13Segment() throws Exception {
66          final byte[] originalImage = generateImage();
67          final byte[] taggedImage = addMetaData(originalImage);
68          final byte[] untaggedImage = removeMetaData(taggedImage, true);
69          assertEquals(originalImage.length, untaggedImage.length);
70      }
71  }