1 /*
2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 6476665 7033534 6830714 8052162 8196572
27  * @summary Verifies color conversion of Component Color Model based images
28  * @run main ColConvCCMTest
29  */
30 
31 import java.awt.color.ColorSpace;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.ColorConvertOp;
34 import java.awt.image.DataBuffer;
35 import java.io.File;
36 import java.io.IOException;
37 import javax.imageio.ImageIO;
38 public class ColConvCCMTest extends ColConvTest {
39 
40     final static int [] dataTypes = {
41         DataBuffer.TYPE_BYTE,
42         DataBuffer.TYPE_DOUBLE,
43         DataBuffer.TYPE_FLOAT,
44         DataBuffer.TYPE_INT,
45         DataBuffer.TYPE_SHORT,
46         DataBuffer.TYPE_USHORT
47     };
48 
49     final static int [] cSpaces = {
50         ColorSpace.CS_sRGB,
51         ColorSpace.CS_LINEAR_RGB,
52         ColorSpace.CS_GRAY,
53         ColorSpace.CS_PYCC,
54         ColorSpace.CS_CIEXYZ
55     };
56 
57     final static double [] ACCURACY = {
58     // Accuracy for color conversions
59         2.5,        // sRGB
60         (isOpenProfile() ? 45.0 : 10.1), // LINEAR_RGB
61         10.5,       // GRAY
62         (isOpenProfile() ? 215.0 : 45.5), // PYCC
63         (isOpenProfile() ? 56.0 : 47.5) // CIEXYZ
64     };
65 
66     final static String [] gldImgNames = {
67         "SRGB.png", "LRGB.png", "GRAY.png", "PYCC.png",  "CIEXYZ.png"
68     };
69 
70     static BufferedImage [] gldImages = null;
71 
testImage(int dataType, int rBits, int gBits, int bBits, int cs, BufferedImage gldImage, double accuracy)72     static boolean testImage(int dataType, int rBits, int gBits, int bBits,
73                               int cs, BufferedImage gldImage,
74                               double accuracy)
75      {
76         BufferedImage src = ImageFactory.createCCMImage(cs, dataType);
77         BufferedImage dst = ImageFactory.createDstImage(
78             BufferedImage.TYPE_INT_RGB);
79         ColorConvertOp op = new ColorConvertOp(null);
80         op.filter(src, dst);
81 
82         ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
83                                                   bBits);
84         boolean result = cmp.compare(gldImage, dst);
85         if (!result) {
86             System.err.println(cmp.getStat());
87         }
88         return result;
89     }
90 
testSubImage(int x0, int y0, int dx, int dy, int dataType, int rBits, int gBits, int bBits, int cs, BufferedImage gldImage, double accuracy)91      static boolean testSubImage(int x0, int y0, int dx, int dy,
92                                  int dataType, int rBits, int gBits,
93                                  int bBits, int cs, BufferedImage gldImage,
94                                  double accuracy)
95      {
96         BufferedImage src = ImageFactory.createCCMImage(cs, dataType);
97         BufferedImage subSrc = src.getSubimage(x0, y0, dx, dy);
98         BufferedImage dst = ImageFactory.createDstImage(
99             BufferedImage.TYPE_INT_RGB);
100         BufferedImage subDst = dst.getSubimage(x0, y0, dx, dy);
101         ColorConvertOp op = new ColorConvertOp(null);
102 
103         op.filter(subSrc, subDst);
104         ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
105                                                   bBits);
106         boolean result = cmp.compare(subDst, gldImage, x0, y0, dx, dy);
107         if (!result) {
108             System.err.println(cmp.getStat());
109         }
110         return result;
111     }
initGoldenImages()112      synchronized public static void initGoldenImages() {
113         if (gldImages == null) {
114             gldImages = new BufferedImage[gldImgNames.length];
115             for (int i = 0; i < gldImgNames.length; i++) {
116                 try {
117                     File gldFile = new File(System.getProperty("test.src", "."),
118                                             gldImgNames[i]);
119 
120                     gldImages[i] = ImageIO.read(gldFile);
121                 } catch (IOException e) {
122                     throw new RuntimeException("Cannot initialize golden " +
123                                                "image: " + gldImgNames[i]);
124                 }
125             }
126         }
127      }
128 
init()129      public void init() {
130         initGoldenImages();
131      }
132 
runTest()133      public void runTest() {
134         for (int i = 0; i < cSpaces.length; i++) {
135             BufferedImage gldImage = gldImages[i];
136             for (int j = 0; j < dataTypes.length; j++) {
137                 if (!testImage(dataTypes[j], 8, 8, 8, cSpaces[i], gldImage,
138                                ACCURACY[i]))
139                 {
140                      throw new RuntimeException(
141                         "Invalid result of the ColorConvertOp for " +
142                         "ColorSpace:" + getCSName(cSpaces[i]) +
143                         " Data type:" +
144                         getDTName(dataTypes[j]) + ". Golden image:" +
145                         gldImages[i]);
146                  }
147 
148                  if (!testSubImage(SI_X, SI_Y, SI_W, SI_H, dataTypes[j],
149                                    8, 8, 8, cSpaces[i], gldImage, ACCURACY[i]))
150                  {
151                     throw new RuntimeException(
152                         "Invalid result of the ColorConvertOp for " +
153                         "ColorSpace:" + getCSName(cSpaces[i]) +
154                         " Data type:" +
155                         getDTName(dataTypes[j]) + ". Golden image:" +
156                         gldImages[i]);
157                  }
158             }
159         }
160      }
161 
main(String [] args)162      public static void main(String [] args) throws Exception {
163          ColConvCCMTest test = new ColConvCCMTest();
164          test.init();
165          test.run();
166      }
167 }
168