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 6523403 6733501 7042594 7043064
27 * @summary Verifies reading and writing profiles and tags of the standard color
28 * spaces
29 * @run main ReadWriteProfileTest
30 */
31import java.awt.color.ColorSpace;
32import java.awt.color.ICC_Profile;
33import java.util.*;
34import java.nio.*;
35import java.util.Hashtable;
36
37public class ReadWriteProfileTest implements Runnable {
38    /* Location of the tag sig counter in 4-byte words */
39    final static int TAG_COUNT_OFFSET = 32;
40
41    /* Location of the tag sig table in 4-byte words */
42    final static int TAG_ELEM_OFFSET = 33;
43
44    static byte[][] profiles;
45    static int [][] tagSigs;
46    static Hashtable<Integer,byte[]> [] tags;
47
48    static int [] cspaces = {ColorSpace.CS_sRGB, ColorSpace.CS_PYCC,
49                             ColorSpace.CS_LINEAR_RGB, ColorSpace.CS_CIEXYZ,
50                             ColorSpace.CS_GRAY};
51
52    static String [] csNames = {"sRGB", "PYCC", "LINEAR_RGB", "CIEXYZ", "GRAY"};
53
54    static void getProfileTags(byte [] data, Hashtable tags) {
55        ByteBuffer byteBuf = ByteBuffer.wrap(data);
56        IntBuffer intBuf = byteBuf.asIntBuffer();
57        int tagCount = intBuf.get(TAG_COUNT_OFFSET);
58        intBuf.position(TAG_ELEM_OFFSET);
59        for (int i = 0; i < tagCount; i++) {
60            int tagSig = intBuf.get();
61            int tagDataOff = intBuf.get();
62            int tagSize = intBuf.get();
63
64            byte [] tagData = new byte[tagSize];
65            byteBuf.position(tagDataOff);
66            byteBuf.get(tagData);
67            tags.put(tagSig, tagData);
68        }
69    }
70
71    static {
72        profiles = new byte[cspaces.length][];
73        tags = new Hashtable[cspaces.length];
74
75        for (int i = 0; i < cspaces.length; i++) {
76            ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
77            profiles[i] = pf.getData();
78            tags[i] = new Hashtable();
79            getProfileTags(profiles[i], tags[i]);
80        }
81    }
82
83    public void run() {
84        for (int i = 0; i < cspaces.length; i++) {
85            System.out.println("Profile: " + csNames[i]);
86            ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
87            byte [] data = pf.getData();
88            pf = ICC_Profile.getInstance(data);
89            if (!Arrays.equals(data, profiles[i])) {
90                System.err.println("Incorrect result of getData() " + "with " +
91                                   csNames[i] + " profile");
92                throw new RuntimeException("Incorrect result of getData()");
93            }
94
95            for (int tagSig : tags[i].keySet()) {
96                String signature = SigToString(tagSig);
97                System.out.printf("Tag: %s\n", signature);
98                System.out.flush();
99
100                byte [] tagData = pf.getData(tagSig);
101                byte [] empty = new byte[tagData.length];
102                boolean emptyDataRejected = false;
103                try {
104                    pf.setData(tagSig, empty);
105                } catch (IllegalArgumentException e) {
106                    emptyDataRejected = true;
107                }
108                if (!emptyDataRejected) {
109                    throw new
110                        RuntimeException("Test failed: empty tag data was not rejected.");
111                }
112                try {
113                    pf.setData(tagSig, tagData);
114                } catch (IllegalArgumentException e) {
115                    // let's ignore this exception for Kodak proprietary tags
116                    if (isKodakExtention(signature)) {
117                        System.out.println("Ignore Kodak tag: " + signature);
118                    } else {
119                        throw new RuntimeException("Test failed!", e);
120                    }
121                }
122                byte [] tagData1 = pf.getData(tagSig);
123
124                if (!Arrays.equals(tagData1, tags[i].get(tagSig)))
125                {
126                    System.err.println("Incorrect result of getData(int) with" +
127                                       " tag " +
128                                       SigToString(tagSig) +
129                                       " of " + csNames[i] + " profile");
130
131                    throw new RuntimeException("Incorrect result of " +
132                                               "getData(int)");
133                }
134            }
135        }
136    }
137
138    private static boolean isKodakExtention(String signature) {
139        return signature.matches("K\\d\\d\\d");
140    }
141
142    private static String SigToString(int tagSig ) {
143              return String.format("%c%c%c%c",
144                        (char)(0xff & (tagSig >> 24)),
145                        (char)(0xff & (tagSig >> 16)),
146                        (char)(0xff & (tagSig >>  8)),
147                        (char)(0xff & (tagSig)));
148
149    }
150
151    public static void main(String [] args) {
152        ReadWriteProfileTest test = new ReadWriteProfileTest();
153        test.run();
154    }
155}
156