7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
authorbae
Mon, 24 Dec 2012 14:03:04 +0400
changeset 14884 74d1acdb7ee4
parent 14883 524cf4063ab3
child 14885 ef6e0146b778
7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0 Reviewed-by: prr
jdk/src/share/classes/sun/java2d/cmm/lcms/LCMS.java
jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSImageLayout.java
jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSTransform.java
jdk/src/share/native/sun/java2d/cmm/lcms/LCMS.c
jdk/test/sun/java2d/cmm/ColorConvertOp/GrayTest.java
--- a/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMS.java	Thu Dec 20 14:43:35 2012 -0800
+++ b/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMS.java	Mon Dec 24 14:03:04 2012 +0400
@@ -53,7 +53,9 @@
     public static native long getProfileID(ICC_Profile profile);
 
     public static native long createNativeTransform(
-        long[] profileIDs, int renderType, int inFormatter, int outFormatter,
+        long[] profileIDs, int renderType,
+        int inFormatter, boolean isInIntPacked,
+        int outFormatter, boolean isOutIntPacked,
         Object disposerRef);
 
    /**
--- a/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSImageLayout.java	Thu Dec 20 14:43:35 2012 -0800
+++ b/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSImageLayout.java	Mon Dec 24 14:03:04 2012 +0400
@@ -196,7 +196,8 @@
             case BufferedImage.TYPE_4BYTE_ABGR:
                 byteRaster = (ByteComponentRaster)image.getRaster();
                 nextRowOffset = byteRaster.getScanlineStride();
-                offset = byteRaster.getDataOffset(0);
+                int firstBand = image.getSampleModel().getNumBands() - 1;
+                offset = byteRaster.getDataOffset(firstBand);
                 dataArray = byteRaster.getDataStorage();
                 dataType = DT_BYTE;
                 break;
--- a/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSTransform.java	Thu Dec 20 14:43:35 2012 -0800
+++ b/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSTransform.java	Mon Dec 24 14:03:04 2012 +0400
@@ -55,8 +55,10 @@
 
 public class LCMSTransform implements ColorTransform {
     long ID;
-    private int inFormatter;
-    private int outFormatter;
+    private int inFormatter = 0;
+    private boolean isInIntPacked = false;
+    private int outFormatter = 0;
+    private boolean isOutIntPacked = false;
 
     ICC_Profile[] profiles;
     long [] profileIDs;
@@ -135,18 +137,23 @@
                                           LCMSImageLayout out) {
         // update native transfrom if needed
         if (ID == 0L ||
-            inFormatter != in.pixelType ||
-            outFormatter != out.pixelType) {
+            inFormatter != in.pixelType || isInIntPacked != in.isIntPacked ||
+            outFormatter != out.pixelType || isOutIntPacked != out.isIntPacked)
+        {
 
             if (ID != 0L) {
                 // Disposer will destroy forgotten transform
                 disposerReferent = new Object();
             }
             inFormatter = in.pixelType;
+            isInIntPacked = in.isIntPacked;
+
             outFormatter = out.pixelType;
+            isOutIntPacked = out.isIntPacked;
 
             ID = LCMS.createNativeTransform(profileIDs, renderType,
-                                            inFormatter, outFormatter,
+                                            inFormatter, isInIntPacked,
+                                            outFormatter, isOutIntPacked,
                                             disposerReferent);
         }
 
--- a/jdk/src/share/native/sun/java2d/cmm/lcms/LCMS.c	Thu Dec 20 14:43:35 2012 -0800
+++ b/jdk/src/share/native/sun/java2d/cmm/lcms/LCMS.c	Mon Dec 24 14:03:04 2012 +0400
@@ -159,7 +159,8 @@
  */
 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_createNativeTransform
   (JNIEnv *env, jclass cls, jlongArray profileIDs, jint renderType,
-   jint inFormatter, jint outFormatter, jobject disposerRef)
+   jint inFormatter, jboolean isInIntPacked,
+   jint outFormatter, jboolean isOutIntPacked, jobject disposerRef)
 {
     cmsHPROFILE _iccArray[DF_ICC_BUF_SIZE];
     cmsHPROFILE *iccArray = &_iccArray[0];
@@ -170,6 +171,16 @@
     size = (*env)->GetArrayLength (env, profileIDs);
     ids = (*env)->GetPrimitiveArrayCritical(env, profileIDs, 0);
 
+#ifdef _LITTLE_ENDIAN
+    /* Reversing data packed into int for LE archs */
+    if (isInIntPacked) {
+        inFormatter ^= DOSWAP_SH(1);
+    }
+    if (isOutIntPacked) {
+        outFormatter ^= DOSWAP_SH(1);
+    }
+#endif
+
     if (DF_ICC_BUF_SIZE < size*2) {
         iccArray = (cmsHPROFILE*) malloc(
             size*2*sizeof(cmsHPROFILE));
@@ -567,7 +578,7 @@
   (JNIEnv *env, jclass obj, jobject trans, jobject src, jobject dst)
 {
     storeID_t sTrans;
-    int inFmt, outFmt, srcDType, dstDType;
+    int srcDType, dstDType;
     int srcOffset, srcNextRowOffset, dstOffset, dstNextRowOffset;
     int width, height, i;
     void* inputBuffer;
@@ -576,23 +587,13 @@
     char* outputRow;
     jobject srcData, dstData;
 
-    inFmt = (*env)->GetIntField (env, src, IL_pixelType_fID);
-    outFmt = (*env)->GetIntField (env, dst, IL_pixelType_fID);
     srcOffset = (*env)->GetIntField (env, src, IL_offset_fID);
     srcNextRowOffset = (*env)->GetIntField (env, src, IL_nextRowOffset_fID);
     dstOffset = (*env)->GetIntField (env, dst, IL_offset_fID);
     dstNextRowOffset = (*env)->GetIntField (env, dst, IL_nextRowOffset_fID);
     width = (*env)->GetIntField (env, src, IL_width_fID);
     height = (*env)->GetIntField (env, src, IL_height_fID);
-#ifdef _LITTLE_ENDIAN
-    /* Reversing data packed into int for LE archs */
-    if ((*env)->GetBooleanField (env, src, IL_isIntPacked_fID) == JNI_TRUE) {
-        inFmt ^= DOSWAP_SH(1);
-    }
-    if ((*env)->GetBooleanField (env, dst, IL_isIntPacked_fID) == JNI_TRUE) {
-        outFmt ^= DOSWAP_SH(1);
-    }
-#endif
+
     sTrans.j = (*env)->GetLongField (env, trans, Trans_ID_fID);
 
     if (sTrans.xf == NULL) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/java2d/cmm/ColorConvertOp/GrayTest.java	Mon Dec 24 14:03:04 2012 +0400
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug     7124245
+ * @summary Test verifies that color conversion does not distort
+ *          colors in destination image of standard type.
+ *
+ * @run main GrayTest
+ */
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.color.ColorSpace;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorConvertOp;
+
+public class GrayTest {
+    public static void main(String[] args) {
+        GrayTest t = new GrayTest();
+
+        t.doTest(BufferedImage.TYPE_INT_RGB);
+        t.doTest(BufferedImage.TYPE_INT_BGR);
+        t.doTest(BufferedImage.TYPE_INT_ARGB);
+        t.doTest(BufferedImage.TYPE_3BYTE_BGR);
+        t.doTest(BufferedImage.TYPE_4BYTE_ABGR);
+        System.out.println("Test passed.");
+    }
+
+    private static final int w = 3;
+    private static final int h = 3;
+
+    private BufferedImage src;
+    private BufferedImage dst;
+
+    private ColorConvertOp op;
+
+    public GrayTest() {
+        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
+        op = new ColorConvertOp(cs, null);
+    }
+
+    private void render(Graphics2D g) {
+        g.setColor(Color.red);
+        g.fillRect(0, 0, w, h);
+    }
+
+    private BufferedImage initImage(int type) {
+        BufferedImage img = new BufferedImage(w, h, type);
+        Graphics2D g = img.createGraphics();
+
+        render(g);
+
+        g.dispose();
+
+        return img;
+    }
+
+    public void doTest(int type) {
+        System.out.println("Test for type: " + type);
+        src = initImage(type);
+
+        dst = initImage(type);
+
+        dst = op.filter(src, dst);
+
+        int pixel = dst.getRGB(1, 1);
+        int r = 0xff & (pixel >> 16);
+        int g = 0xff & (pixel >>  8);
+        int b = 0xff & (pixel      );
+
+        System.out.printf("dst: r:%02x, g: %02x, %02x\n",
+                r, g, b);
+
+        if (r != g || r != b) {
+            String msg = String.format("Invalid pixel: %08x", pixel);
+            throw new RuntimeException(msg);
+        }
+        System.out.println("Done.");
+    }
+}