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