8019201: Regression: java.awt.image.ConvolveOp throws java.awt.image.ImagingOpException
authorbae
Thu, 25 Jul 2013 17:14:39 +0400
changeset 19010 8148c141f16e
parent 19009 1f1914231324
child 19011 b738ae865548
8019201: Regression: java.awt.image.ConvolveOp throws java.awt.image.ImagingOpException Reviewed-by: prr
jdk/src/share/native/sun/awt/medialib/awt_ImagingLib.c
jdk/test/sun/awt/image/ImagingLib/SamePackingTypeTest.java
--- a/jdk/src/share/native/sun/awt/medialib/awt_ImagingLib.c	Wed Jul 24 13:05:03 2013 -0700
+++ b/jdk/src/share/native/sun/awt/medialib/awt_ImagingLib.c	Thu Jul 25 17:14:39 2013 +0400
@@ -2606,38 +2606,37 @@
 
 #define ERR_BAD_IMAGE_LAYOUT (-2)
 
-#define CHECK_DST_ARRAY(start_offset, elements_per_pixel)             \
-    do {                                                              \
-        int offset = (start_offset);                                  \
-        int lastScanOffset;                                           \
-                                                                      \
-        if (!SAFE_TO_MULT(rasterP->scanlineStride,                    \
-                          (rasterP->height - 1)))                     \
-        {                                                             \
-            return ERR_BAD_IMAGE_LAYOUT;                              \
-        }                                                             \
-        lastScanOffset = rasterP->scanlineStride *                    \
-            (rasterP->height - 1);                                    \
-                                                                      \
-        if (!SAFE_TO_ADD(offset, lastScanOffset)) {                   \
-            return ERR_BAD_IMAGE_LAYOUT;                              \
-        }                                                             \
-        lastScanOffset += offset;                                     \
-                                                                      \
-        if (!SAFE_TO_MULT((elements_per_pixel), rasterP->width)) {    \
-            return ERR_BAD_IMAGE_LAYOUT;                              \
-        }                                                             \
-        offset = (elements_per_pixel) * rasterP->width;               \
-                                                                      \
-        if (!SAFE_TO_ADD(offset, lastScanOffset)) {                   \
-            return ERR_BAD_IMAGE_LAYOUT;                              \
-        }                                                             \
-        lastScanOffset += offset;                                     \
-                                                                      \
-        if (dataArrayLength < lastScanOffset) {                       \
-            return ERR_BAD_IMAGE_LAYOUT;                              \
-        }                                                             \
-    } while(0);                                                       \
+#define CHECK_DST_ARRAY(start_offset, elements_per_scan, elements_per_pixel) \
+    do {                                                                     \
+        int offset = (start_offset);                                         \
+        int lastScanOffset;                                                  \
+                                                                             \
+        if (!SAFE_TO_MULT((elements_per_scan),                               \
+                          (rasterP->height - 1)))                            \
+        {                                                                    \
+            return ERR_BAD_IMAGE_LAYOUT;                                     \
+        }                                                                    \
+        lastScanOffset = (elements_per_scan) * (rasterP->height - 1);        \
+                                                                             \
+        if (!SAFE_TO_ADD(offset, lastScanOffset)) {                          \
+            return ERR_BAD_IMAGE_LAYOUT;                                     \
+        }                                                                    \
+        lastScanOffset += offset;                                            \
+                                                                             \
+        if (!SAFE_TO_MULT((elements_per_pixel), rasterP->width)) {           \
+            return ERR_BAD_IMAGE_LAYOUT;                                     \
+        }                                                                    \
+        offset = (elements_per_pixel) * rasterP->width;                      \
+                                                                             \
+        if (!SAFE_TO_ADD(offset, lastScanOffset)) {                          \
+            return ERR_BAD_IMAGE_LAYOUT;                                     \
+        }                                                                    \
+        lastScanOffset += offset;                                            \
+                                                                             \
+        if (dataArrayLength < lastScanOffset) {                              \
+            return ERR_BAD_IMAGE_LAYOUT;                                     \
+        }                                                                    \
+    } while(0);                                                              \
 
 static int
 storeImageArray(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
@@ -2665,39 +2664,33 @@
 
     if (hintP->packing == BYTE_INTERLEAVED) {
         /* Write it back to the destination */
-        CHECK_DST_ARRAY(hintP->channelOffset, hintP->numChans);
+        if (rasterP->dataType != BYTE_DATA_TYPE) {
+            /* We are working with a raster which was marked
+               as a byte interleaved due to performance reasons.
+               So, we have to convert the length of the data
+               array to bytes as well.
+            */
+            if (!SAFE_TO_MULT(rasterP->dataSize, dataArrayLength)) {
+                return ERR_BAD_IMAGE_LAYOUT;
+            }
+            dataArrayLength *= rasterP->dataSize;
+        }
+
+        CHECK_DST_ARRAY(hintP->dataOffset, hintP->sStride, hintP->numChans);
         cmDataP = (unsigned char *) mlib_ImageGetData(mlibImP);
         mStride = mlib_ImageGetStride(mlibImP);
         dataP = (unsigned char *)(*env)->GetPrimitiveArrayCritical(env,
                                                       rasterP->jdata, NULL);
         if (dataP == NULL) return 0;
-        cDataP = dataP + hintP->channelOffset;
+        cDataP = dataP + hintP->dataOffset;
         for (y=0; y < rasterP->height;
-             y++, cmDataP += mStride, cDataP += rasterP->scanlineStride)
+             y++, cmDataP += mStride, cDataP += hintP->sStride)
         {
             memcpy(cDataP, cmDataP, rasterP->width*hintP->numChans);
         }
         (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
                                               JNI_ABORT);
     }
-    else if (hintP->packing == SHORT_INTERLEAVED) {
-        /* Write it back to the destination */
-        unsigned short *sdataP, *sDataP;
-        unsigned short *smDataP = (unsigned short *)mlib_ImageGetData(mlibImP);
-        CHECK_DST_ARRAY(hintP->channelOffset, hintP->numChans);
-        mStride = mlib_ImageGetStride(mlibImP);
-        sdataP = (unsigned short *)(*env)->GetPrimitiveArrayCritical(env,
-                                                      rasterP->jdata, NULL);
-        if (sdataP == NULL) return -1;
-        sDataP = sdataP + hintP->channelOffset;
-        for (y=0; y < rasterP->height;
-            y++, smDataP += mStride, sDataP += rasterP->scanlineStride)
-        {
-            memcpy(sDataP, smDataP, rasterP->width*hintP->numChans);
-        }
-        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, sdataP,
-                                              JNI_ABORT);
-    }
     else if (dstP->cmodel.cmType == DIRECT_CM_TYPE) {
         /* Just need to move bits */
         if (mlibImP->type == MLIB_BYTE) {
@@ -3499,7 +3492,7 @@
     }
 
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
@@ -3575,7 +3568,7 @@
     }
 
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
@@ -3651,7 +3644,7 @@
     }
 
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
@@ -3730,7 +3723,7 @@
     }
 
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
@@ -3827,7 +3820,7 @@
         return -1;
     }
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
@@ -3925,7 +3918,7 @@
     }
 
     dataArrayLength = (*env)->GetArrayLength(env, jOutDataP);
-    CHECK_DST_ARRAY(rasterP->chanOffsets[0], 1);
+    CHECK_DST_ARRAY(rasterP->chanOffsets[0], rasterP->scanlineStride, 1);
 
     outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
     if (outDataP == NULL) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/awt/image/ImagingLib/SamePackingTypeTest.java	Thu Jul 25 17:14:39 2013 +0400
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2013, 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     8019201
+ * @summary Test verifies that medialib glue code does not throw
+ *          an ImagingOpException for certain pairs of source and
+ *          destination images.
+ *
+ * @run main SamePackingTypeTest
+ */
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR;
+import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE;
+import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
+import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;
+import java.awt.image.BufferedImageOp;
+import java.awt.image.ConvolveOp;
+import java.awt.image.ImagingOpException;
+import java.awt.image.Kernel;
+import java.util.Arrays;
+
+
+public class SamePackingTypeTest {
+
+    public static void main(String[] args) {
+        BufferedImageOp op = createTestOp();
+
+        try {
+            System.out.print("Integer-based images... ");
+            doTest(op, TYPE_INT_ARGB, TYPE_INT_ARGB_PRE);
+            System.out.println("done.");
+
+            System.out.print("Byte-based images... ");
+            doTest(op, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE);
+            System.out.println("done");
+        } catch (ImagingOpException e) {
+            throw new RuntimeException("Test FAILED", e);
+        }
+    }
+
+    private static void doTest(BufferedImageOp op, int stype, int dtype) {
+        final int size = 100;
+
+        final BufferedImage src = new BufferedImage(size, size, stype);
+        Graphics2D g = src.createGraphics();
+        g.setColor(Color.red);
+        g.fillRect(0, 0, size, size);
+        g.dispose();
+
+
+        final BufferedImage dst = new BufferedImage(size, size, dtype);
+        g = dst.createGraphics();
+        g.setColor(Color.blue);
+        g.fillRect(0, 0, size, size);
+        g.dispose();
+
+        op.filter(src, dst);
+
+        final int rgb = dst.getRGB(size - 1, size - 1);
+        System.out.printf("dst: 0x%X ", rgb);
+
+        if (rgb != 0xFFFF0000) {
+            throw new RuntimeException(String.format("Wrong color in dst: 0x%X", rgb));
+        }
+    }
+
+    private static BufferedImageOp createTestOp() {
+        final int size = 1;
+        final float v = 1f / (size * size);
+        final float[] k_data = new float[size * size];
+        Arrays.fill(k_data, v);
+
+        Kernel k = new Kernel(size, size, k_data);
+        return new ConvolveOp(k);
+    }
+}