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.assertEquals;
20  
21  import java.awt.Color;
22  import java.awt.Graphics2D;
23  import java.awt.image.BufferedImage;
24  import java.io.File;
25  import java.io.IOException;
26  
27  import org.apache.commons.imaging.Imaging;
28  import org.apache.commons.imaging.ImagingException;
29  import org.apache.commons.imaging.ImagingTestConstants;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Performs tests that access the content of TIFF files containing non-opaque alpha-channel pixels
34   */
35  public class TiffReadAlphaTest {
36  
37      private final static String[] names = { "TransparencyTestStripAssociated.tif", "TransparencyTestStripUnassociated.tif",
38              "TransparencyTestTileAssociated.tif", "TransparencyTestTileUnassociated.tif" };
39  
40      private final static int[][] testSite = { { 40, 40, 0xffff0000 }, { 60, 40, 0xff77ff77 }, { 40, 60, 0xffff0000 }, { 60, 60, 0xff008800 } };
41  
42      /**
43       * Gets a file from the TIFF test directory that contains floating-point data.
44       *
45       * @param name a valid file name
46       * @return a valid file reference.
47       */
48      private File getTiffFile(final String name) {
49          final File tiffFolder = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "tiff");
50          final File alphaFolder = new File(tiffFolder, "12");
51          return new File(alphaFolder, name);
52      }
53  
54      @Test
55      public void test() throws ImagingException, IOException {
56          for (final String name : names) {
57              final File subject = getTiffFile(name);
58              final BufferedImage overlay = Imaging.getBufferedImage(subject);
59              final BufferedImage composite = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
60              final Graphics2D g2d = composite.createGraphics();
61              g2d.setColor(Color.white);
62              g2d.fillRect(0, 0, 101, 101);
63              g2d.setColor(Color.black);
64              g2d.fillRect(0, 50, 101, 51);
65              g2d.drawImage(overlay, 0, 0, null);
66  
67              for (final int[] element : testSite) {
68                  final int x = element[0];
69                  final int y = element[1];
70                  final int p = element[2];
71                  final int t = composite.getRGB(x, y);
72                  assertEquals(t, p, "Error for " + name + " at position " + x + ", " + y);
73              }
74          }
75      }
76  }