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.png;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.awt.Color;
25  import java.awt.image.BufferedImage;
26  import java.io.ByteArrayOutputStream;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.imaging.Imaging;
31  import org.junit.jupiter.api.Test;
32  
33  public class PngTextTest extends AbstractPngTest {
34  
35      @Test
36      public void test() throws Exception {
37          final int width = 1;
38          final int height = 1;
39          final BufferedImage srcImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
40          srcImage.setRGB(0, 0, Color.red.getRGB());
41  
42          final PngImagingParameters writeParams = new PngImagingParameters();
43  
44          final List<AbstractPngText> writeTexts = new ArrayList<>();
45          {
46              final String keyword = "a";
47              final String text = "b";
48              writeTexts.add(new AbstractPngText.Text(keyword, text));
49          }
50          {
51              final String keyword = "c";
52              final String text = "d";
53              writeTexts.add(new AbstractPngText.Ztxt(keyword, text));
54          }
55          {
56              final String keyword = "e";
57              final String text = "f";
58              final String languageTag = "g";
59              final String translatedKeyword = "h";
60              writeTexts.add(new AbstractPngText.Itxt(keyword, text, languageTag, translatedKeyword));
61          }
62  
63          writeParams.setTextChunks(writeTexts);
64  
65          final PngImageParser pngImageParser = new PngImageParser();
66          final byte[] bytes;
67          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
68              pngImageParser.writeImage(srcImage, baos, writeParams);
69              bytes = baos.toByteArray();
70          }
71  
72          final PngImageInfo imageInfo = (PngImageInfo) Imaging.getImageInfo(bytes);
73          assertNotNull(imageInfo);
74  
75          final List<AbstractPngText> readTexts = imageInfo.getTextChunks();
76          assertEquals(readTexts.size(), 3);
77          for (final AbstractPngText text : readTexts) {
78              switch (text.keyword) {
79              case "a":
80                  assertEquals(text.text, "b");
81                  break;
82              case "c":
83                  assertEquals(text.text, "d");
84                  break;
85              case "e":
86                  assertEquals(text.text, "f");
87                  break;
88              default:
89                  fail("unknown text chunk.");
90                  break;
91              }
92          }
93      }
94  
95  }