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.formats.tiff;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.awt.image.BufferedImage;
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.commons.imaging.Imaging;
27  import org.apache.commons.imaging.ImagingTestConstants;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Reads files in the BigTIFF samples folder and compares the BigTIFF files against the standard "classic" tiff image.
32   */
33  public class TiffBigTiffTest extends TiffBaseTest {
34  
35      private boolean doImagesMatch(final int w, final int h, final int[] classicRgb, final BufferedImage image) {
36          final int iW = image.getWidth();
37          final int iH = image.getHeight();
38          if (iW != w || iH != h) {
39              return false;
40          }
41          final int[] argb = new int[w * h];
42          image.getRGB(0, 0, w, h, argb, 0, w);
43          for (int i = 0; i < argb.length; i++) {
44              if ((argb[i] & 0x00ffffff) != (classicRgb[i] & 0x00ffffff)) {
45                  return false;
46              }
47          }
48          return true;
49      }
50  
51      @Test
52      public void test() throws IOException {
53          final File tiffFolder = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "tiff");
54          final File bigTiffFolder = new File(tiffFolder, "13");
55  
56          final File classicFile = new File(bigTiffFolder, "Classic.tif");
57          final BufferedImage classicImage = Imaging.getBufferedImage(classicFile);
58          assertNotNull(classicImage);
59          final int w = classicImage.getWidth();
60          final int h = classicImage.getHeight();
61          final int[] classicRgb = new int[w * h];
62          classicImage.getRGB(0, 0, w, h, classicRgb, 0, w); // data for comparison
63  
64          int nTest = 0;
65          final File[] files = bigTiffFolder.listFiles();
66          for (final File f : files) {
67              final String name = f.getName();
68              if (!name.toLowerCase().startsWith("bigtiff")) {
69                  continue;
70              }
71              final BufferedImage image = Imaging.getBufferedImage(f);
72              assertNotNull(image);
73              final boolean status = doImagesMatch(w, h, classicRgb, image);
74              assertTrue(status, "BigTIFF content does not match test image for " + name);
75              nTest++;
76          }
77  
78          assertTrue(nTest > 0, "JUnit test failed to find sample BigTIFF files");
79      }
80  
81  }