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