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.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.awt.image.BufferedImage;
24  import java.io.File;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.imaging.ImageFormats;
29  import org.apache.commons.imaging.Imaging;
30  import org.apache.commons.imaging.util.Debug;
31  import org.apache.commons.io.FilenameUtils;
32  import org.junit.Test;
33  
34  public class PngMultipleRoundtripTest extends PngBaseTest {
35  
36      @Test
37      public void test() throws Exception {
38          final String imagesFolderPath = FilenameUtils.separatorsToSystem(
39                  "src\\test\\data\\images\\png\\3");
40          final File imagesFolder = new File(imagesFolderPath);
41          assertTrue(imagesFolder.exists() && imagesFolder.isDirectory());
42  
43          final File files[] = imagesFolder.listFiles();
44          for (final File file : files) {
45              final File imageFile = file;
46              if (!imageFile.isFile()) {
47                  continue;
48              }
49              if (!imageFile.getName().toLowerCase().endsWith(".png")) {
50                  continue;
51              }
52  
53              Debug.debug();
54              Debug.debug("imageFile", imageFile);
55  
56              File lastFile = imageFile;
57              for (int j = 0; j < 10; j++) {
58                  final Map<String, Object> readParams = new HashMap<>();
59                  // readParams.put(ImagingConstants.BUFFERED_IMAGE_FACTORY,
60                  // new RgbBufferedImageFactory());
61                  final BufferedImage image = Imaging.getBufferedImage(lastFile,
62                          readParams);
63                  assertNotNull(image);
64  
65                  final File tempFile = createTempFile(imageFile.getName() + "." + j
66                          + ".", ".png");
67                  Debug.debug("tempFile", tempFile);
68  
69                  final Map<String, Object> writeParams = new HashMap<>();
70                  Imaging.writeImage(image, tempFile,
71                          ImageFormats.PNG, writeParams);
72  
73                  lastFile = tempFile;
74              }
75          }
76      }
77  
78  }