Path: blob/master/test/jdk/sun/java2d/cmm/ProfileOp/ReadWriteProfileTest.java
41153 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6476665 6523403 6733501 7042594 704306426* @summary Verifies reading and writing profiles and tags of the standard color27* spaces28* @run main ReadWriteProfileTest29*/30import java.awt.color.ColorSpace;31import java.awt.color.ICC_Profile;32import java.util.*;33import java.nio.*;34import java.util.Hashtable;3536public class ReadWriteProfileTest implements Runnable {37/* Location of the tag sig counter in 4-byte words */38final static int TAG_COUNT_OFFSET = 32;3940/* Location of the tag sig table in 4-byte words */41final static int TAG_ELEM_OFFSET = 33;4243static byte[][] profiles;44static int [][] tagSigs;45static Hashtable<Integer,byte[]> [] tags;4647static int [] cspaces = {ColorSpace.CS_sRGB, ColorSpace.CS_PYCC,48ColorSpace.CS_LINEAR_RGB, ColorSpace.CS_CIEXYZ,49ColorSpace.CS_GRAY};5051static String [] csNames = {"sRGB", "PYCC", "LINEAR_RGB", "CIEXYZ", "GRAY"};5253static void getProfileTags(byte [] data, Hashtable tags) {54ByteBuffer byteBuf = ByteBuffer.wrap(data);55IntBuffer intBuf = byteBuf.asIntBuffer();56int tagCount = intBuf.get(TAG_COUNT_OFFSET);57intBuf.position(TAG_ELEM_OFFSET);58for (int i = 0; i < tagCount; i++) {59int tagSig = intBuf.get();60int tagDataOff = intBuf.get();61int tagSize = intBuf.get();6263byte [] tagData = new byte[tagSize];64byteBuf.position(tagDataOff);65byteBuf.get(tagData);66tags.put(tagSig, tagData);67}68}6970static {71profiles = new byte[cspaces.length][];72tags = new Hashtable[cspaces.length];7374for (int i = 0; i < cspaces.length; i++) {75ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);76profiles[i] = pf.getData();77tags[i] = new Hashtable();78getProfileTags(profiles[i], tags[i]);79}80}8182public void run() {83for (int i = 0; i < cspaces.length; i++) {84System.out.println("Profile: " + csNames[i]);85ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);86byte [] data = pf.getData();87pf = ICC_Profile.getInstance(data);88if (!Arrays.equals(data, profiles[i])) {89System.err.println("Incorrect result of getData() " + "with " +90csNames[i] + " profile");91throw new RuntimeException("Incorrect result of getData()");92}9394for (int tagSig : tags[i].keySet()) {95String signature = SigToString(tagSig);96System.out.printf("Tag: %s\n", signature);97System.out.flush();9899byte [] tagData = pf.getData(tagSig);100byte [] empty = new byte[tagData.length];101boolean emptyDataRejected = false;102try {103pf.setData(tagSig, empty);104} catch (IllegalArgumentException e) {105emptyDataRejected = true;106}107if (!emptyDataRejected) {108throw new109RuntimeException("Test failed: empty tag data was not rejected.");110}111try {112pf.setData(tagSig, tagData);113} catch (IllegalArgumentException e) {114// let's ignore this exception for Kodak proprietary tags115if (isKodakExtention(signature)) {116System.out.println("Ignore Kodak tag: " + signature);117} else {118throw new RuntimeException("Test failed!", e);119}120}121byte [] tagData1 = pf.getData(tagSig);122123if (!Arrays.equals(tagData1, tags[i].get(tagSig)))124{125System.err.println("Incorrect result of getData(int) with" +126" tag " +127SigToString(tagSig) +128" of " + csNames[i] + " profile");129130throw new RuntimeException("Incorrect result of " +131"getData(int)");132}133}134}135}136137private static boolean isKodakExtention(String signature) {138return signature.matches("K\\d\\d\\d");139}140141private static String SigToString(int tagSig ) {142return String.format("%c%c%c%c",143(char)(0xff & (tagSig >> 24)),144(char)(0xff & (tagSig >> 16)),145(char)(0xff & (tagSig >> 8)),146(char)(0xff & (tagSig)));147148}149150public static void main(String [] args) {151ReadWriteProfileTest test = new ReadWriteProfileTest();152test.run();153}154}155156157