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  package org.apache.commons.imaging.roundtrip;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.awt.image.BufferedImage;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.util.stream.Stream;
26  
27  import org.apache.commons.imaging.AbstractImageParser;
28  import org.apache.commons.imaging.ImagingException;
29  import org.apache.commons.imaging.ImagingParameters;
30  import org.apache.commons.imaging.common.RgbBufferedImageFactory;
31  import org.apache.commons.imaging.internal.ImageParserFactory;
32  import org.junit.jupiter.params.provider.Arguments;
33  
34  public class RoundtripBase {
35  
36      public static Stream<Arguments> createRoundtripArguments(final BufferedImage[] images) {
37          return Stream.of(images).flatMap(i -> Stream.of(FormatInfo.READ_WRITE_FORMATS).map(f -> Arguments.of(i, f)));
38      }
39  
40      protected void roundtrip(final FormatInfo formatInfo, final BufferedImage testImage, final String tempPrefix, final boolean imageExact)
41              throws IOException, ImagingException, ImagingException {
42  
43          final AbstractImageParser abstractImageParser = ImageParserFactory.getImageParser(formatInfo.format);
44  
45          final ImagingParameters params = ImageParserFactory.getImageParser(formatInfo.format).getDefaultParameters();
46          byte[] temp1;
47          try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
48              abstractImageParser.writeImage(testImage, bos, params);
49              temp1 = bos.toByteArray();
50          }
51  
52          final ImagingParameters readParams = ImageParserFactory.getImageParser(formatInfo.format).getDefaultParameters();
53          readParams.setBufferedImageFactory(new RgbBufferedImageFactory());
54          final BufferedImage image2 = abstractImageParser.getBufferedImage(temp1, readParams);
55          assertNotNull(image2);
56  
57          if (imageExact) {
58              // note tolerance when comparing grayscale images
59              // BufferedImages of
60              ImageAsserts.assertImageEquals(testImage, image2);
61          }
62  
63          if (formatInfo.identicalSecondWrite) {
64              byte[] temp2;
65              try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
66                  abstractImageParser.writeImage(image2, bos, params);
67                  temp2 = bos.toByteArray();
68              }
69  
70              assertArrayEquals(temp1, temp2);
71          }
72      }
73  }