< prev index next >

src/java.desktop/share/native/libawt/awt/image/BufImgSurfaceData.c

Print this page

        

@@ -229,16 +229,18 @@
     if (bipriv->cData == NULL) {
         pRasInfo->invColorTable = NULL;
         pRasInfo->redErrTable = NULL;
         pRasInfo->grnErrTable = NULL;
         pRasInfo->bluErrTable = NULL;
+        pRasInfo->representsPrimary = 0;
     } else {
         pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;
         pRasInfo->redErrTable = bipriv->cData->img_oda_red;
         pRasInfo->grnErrTable = bipriv->cData->img_oda_green;
         pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;
         pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;
+        pRasInfo->representsPrimary = bipriv->cData->representsPrimary;
     }
 }
 
 static void BufImg_Release(JNIEnv *env,
                            SurfaceDataOps *ops,

@@ -257,10 +259,92 @@
         (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,
                                               bipriv->lutbase, JNI_ABORT);
     }
 }
 
+static jboolean calculatePrimaryColorsAprroximation(int* cmap, unsigned char* cube, int cube_size) {
+    int i, j, k;
+    jboolean represents_primary = JNI_TRUE;
+    int index, value, color;
+    // maximum variaton allowed for r,g,b values
+    int dr, dg, db;
+    // values calculated from cmap
+    int r, g, b;
+    // maximum variation allowed for r, g, b values for primary colors
+    int delta = 5;
+    // get the primary color cmap indices from corner of inverse color table
+    for (i = 0; i < cube_size; i += (cube_size - 1)) {
+        for (j = 0; j < cube_size; j += (cube_size - 1)) {
+            for (k = 0; k < cube_size; k += (cube_size - 1)) {
+                // calculate inverse color table index
+                index = i + cube_size * (j + cube_size * k);
+                // get value present in corners of inverse color table
+                value = cube[index];
+                // use the corner values as index for cmap  
+                color = cmap[value];
+                // extract r,g,b values from cmap value
+                (r) = (color)& 0xff;
+                (g) = ((color) >> 8) & 0xff;
+                (b) = ((color) >> 16) & 0xff;
+                /* based on value of i, j, k we can set expected values for r, g, b
+                 * if i,j,k is 0 then r,g,b is 0, if i,j,k is 31 then r,g,b is 255
+                 */
+                dr = (i % (cube_size - 2)) * 255;
+                dg = (j % (cube_size - 2)) * 255;
+                db = (k % (cube_size - 2)) * 255;
+                
+                // if i,j,k values are 31 delta will be -5 and if i,j,k values are 0
+                // delta will be +5. if r,g,b values from cmap vary more than dr,dg,db
+                // then they dont represent primary colors properly.
+                if (i % (cube_size - 2)) {
+                    dr -= delta;
+                    if (r < dr) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+                else {
+                    dr += delta;
+                    if (r > dr) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+                if (j % (cube_size - 2)) {
+                    dg -= delta;
+                    if (g < dg) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+                else {
+                    dg += delta;
+                    if (g > dg) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+                if (k % (cube_size - 2)) {
+                    db -= delta;
+                    if (b < db) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+                else {
+                    db += delta;
+                    if (b > db) {
+                        represents_primary = JNI_FALSE;
+                        break;
+                    }
+                }
+            }
+        }
+    }
+    return represents_primary;
+}
+
 static ColorData *BufImg_SetupICM(JNIEnv *env,
                                   BufImgSDOps *bisdo)
 {
     ColorData *cData = NULL;
     jobject colorData;

@@ -296,10 +380,11 @@
             free(cData);
             return (ColorData*)NULL;
         }
 
         cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);
+        cData->representsPrimary = calculatePrimaryColorsAprroximation(pRgb, cData->img_clr_tbl, 32);
         if (allGray == JNI_TRUE) {
             initInverseGrayLut(pRgb, bisdo->lutsize, cData);
         }
         (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,
                                               JNI_ABORT);
< prev index next >