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.roundtrip;
19  
20  import static org.junit.Assert.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.util.Debug;
27  import org.apache.commons.io.FileUtils;
28  import org.junit.Assert;
29  
30  final class ImageAsserts {
31  
32      private ImageAsserts() {
33      }
34      
35      static void assertEquals(final BufferedImage a, final BufferedImage b) {
36          assertEquals(a, b, 0);
37      }
38  
39      static void assertEquals(final BufferedImage a, final BufferedImage b, final int tolerance) {
40          Assert.assertEquals(a.getWidth(), b.getWidth());
41          Assert.assertEquals(a.getHeight(), b.getHeight());
42  
43          for (int x = 0; x < a.getWidth(); x++) {
44              for (int y = 0; y < a.getHeight(); y++) {
45                  final int a_argb = a.getRGB(x, y);
46                  final int b_argb = b.getRGB(x, y);
47                  if (a_argb != b_argb) {
48                      if (calculateARGBDistance(a_argb, b_argb) <= tolerance) {
49                          continue; // ignore.
50                      }
51                  }
52                  if (a_argb != b_argb) {
53                      Debug.debug("width: " + a.getWidth());
54                      Debug.debug("height: " + a.getHeight());
55                      Debug.debug("distance: " + calculateARGBDistance(a_argb, b_argb));
56                      Debug.debug("x: " + x);
57                      Debug.debug("y: " + y);
58                      Debug.debug("a_argb: " + a_argb + " (0x" + Integer.toHexString(a_argb) + ")");
59                      Debug.debug("b_argb: " + b_argb + " (0x" + Integer.toHexString(b_argb) + ")");
60                  }
61                  Assert.assertEquals(a_argb, b_argb);
62              }
63          }
64      }
65  
66      static int calculateARGBDistance(final int a, final int b) {
67          final int aAlpha = 0xff & (a >> 24);
68          final int aRed = 0xff & (a >> 16);
69          final int aGreen = 0xff & (a >> 8);
70          final int aBlue = 0xff & (a >> 0);
71          final int bAlpha = 0xff & (b >> 24);
72          final int bRed = 0xff & (b >> 16);
73          final int bGreen = 0xff & (b >> 8);
74          final int bBlue = 0xff & (b >> 0);
75          final int diff = Math.abs(aAlpha - bAlpha) + Math.abs(aRed - bRed)
76                  + Math.abs(aGreen - bGreen) + Math.abs(aBlue - bBlue);
77          return diff;
78  
79      }
80  
81      static void assertEquals(final File a, final File b) throws IOException {
82          assertTrue(a.exists() && a.isFile());
83          assertTrue(b.exists() && b.isFile());
84          Assert.assertEquals(a.length(), b.length());
85  
86          final byte aData[] = FileUtils.readFileToByteArray(a);
87          final byte bData[] = FileUtils.readFileToByteArray(b);
88  
89          for (int i = 0; i < a.length(); i++) {
90              final int aByte = 0xff & aData[i];
91              final int bByte = 0xff & bData[i];
92  
93              if (aByte != bByte) {
94                  Debug.debug("a: " + a);
95                  Debug.debug("b: " + b);
96                  Debug.debug("i: " + i);
97                  Debug.debug("aByte: " + aByte + " (0x" + Integer.toHexString(aByte) + ")");
98                  Debug.debug("bByte: " + bByte + " (0x" + Integer.toHexString(bByte) + ")");
99              }
100             Assert.assertEquals(aByte, bByte);
101         }
102     }
103 }