changeset 12628:8b318cc1e0ba

8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank Reviewed-by: prr, flar
author psadhukhan
date Thu, 06 Aug 2015 11:36:52 +0300
parents 133282574936
children f45d4e52e7e2
files src/java.desktop/share/classes/java/awt/image/RescaleOp.java test/java/awt/image/RescaleOp/RescaleAlphaTest.java
diffstat 2 files changed, 153 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/java/awt/image/RescaleOp.java	Wed Aug 05 19:11:27 2015 +0300
+++ b/src/java.desktop/share/classes/java/awt/image/RescaleOp.java	Thu Aug 06 11:36:52 2015 +0300
@@ -326,16 +326,16 @@
     public final BufferedImage filter (BufferedImage src, BufferedImage dst) {
         ColorModel srcCM = src.getColorModel();
         ColorModel dstCM;
-        int numBands = srcCM.getNumColorComponents();
-
+        int numSrcColorComp = srcCM.getNumColorComponents();
+        int scaleConst = length;
 
         if (srcCM instanceof IndexColorModel) {
             throw new
                 IllegalArgumentException("Rescaling cannot be "+
                                          "performed on an indexed image");
         }
-        if (length != 1 && length != numBands &&
-            length != srcCM.getNumComponents())
+        if (scaleConst != 1 && scaleConst != numSrcColorComp &&
+            scaleConst != srcCM.getNumComponents())
         {
             throw new IllegalArgumentException("Number of scaling constants "+
                                                "does not equal the number of"+
@@ -346,13 +346,14 @@
         boolean needToConvert = false;
 
         // Include alpha
-        if (length > numBands && srcCM.hasAlpha()) {
-            length = numBands+1;
+        if (scaleConst > numSrcColorComp && srcCM.hasAlpha()) {
+            scaleConst = numSrcColorComp+1;
         }
 
         int width = src.getWidth();
         int height = src.getHeight();
 
+        BufferedImage origDst = dst;
         if (dst == null) {
             dst = createCompatibleDestImage(src, null);
             dstCM = srcCM;
@@ -380,7 +381,19 @@
 
         }
 
-        BufferedImage origDst = dst;
+        boolean scaleAlpha = true;
+
+        //
+        // The number of sets of scaling constants may be one,
+        // in which case the same constants are applied to all color
+        // (but NOT alpha) components. Otherwise, the number of sets
+        // of scaling constants may equal the number of Source color
+        // components, in which case NO rescaling of the alpha component
+        // (if present) is performed.
+        //
+        if (numSrcColorComp == scaleConst || scaleConst == 1) {
+            scaleAlpha = false;
+        }
 
         //
         // Try to use a native BI rescale operation first
@@ -392,12 +405,13 @@
             WritableRaster srcRaster = src.getRaster();
             WritableRaster dstRaster = dst.getRaster();
 
-            if (srcCM.hasAlpha()) {
-                if (numBands-1 == length || length == 1) {
+            if (!scaleAlpha) {
+                if (srcCM.hasAlpha()) {
+                    // Do not rescale Alpha component
                     int minx = srcRaster.getMinX();
                     int miny = srcRaster.getMinY();
-                    int[] bands = new int[numBands-1];
-                    for (int i=0; i < numBands-1; i++) {
+                    int[] bands = new int[numSrcColorComp];
+                    for (int i=0; i < numSrcColorComp; i++) {
                         bands[i] = i;
                     }
                     srcRaster =
@@ -407,14 +421,11 @@
                                                       minx, miny,
                                                       bands);
                 }
-            }
-            if (dstCM.hasAlpha()) {
-                int dstNumBands = dstRaster.getNumBands();
-                if (dstNumBands-1 == length || length == 1) {
+                if (dstCM.hasAlpha()) {
                     int minx = dstRaster.getMinX();
                     int miny = dstRaster.getMinY();
-                    int[] bands = new int[numBands-1];
-                    for (int i=0; i < numBands-1; i++) {
+                    int[] bands = new int[numSrcColorComp];
+                    for (int i=0; i < numSrcColorComp; i++) {
                         bands[i] = i;
                     }
                     dstRaster =
@@ -429,17 +440,42 @@
             //
             // Call the raster filter method
             //
-            filter(srcRaster, dstRaster);
+            filterRasterImpl(srcRaster, dstRaster, scaleConst);
 
+            //
+            // here copy the unscaled src alpha to destination alpha channel
+            //
+            if (!scaleAlpha) {
+                Raster srcAlphaRaster = null;
+                WritableRaster dstAlphaRaster = null;
+
+                if (srcCM.hasAlpha()) {
+                    srcAlphaRaster = src.getAlphaRaster();
+                }
+                if (dstCM.hasAlpha()) {
+                    dstAlphaRaster = dst.getAlphaRaster();
+                    if (srcAlphaRaster != null) {
+                        dstAlphaRaster.setRect(srcAlphaRaster);
+                    } else {
+                        int alpha = 0xff << 24;
+                        for (int cy=0; cy < dst.getHeight(); cy++) {
+                            for (int cx=0; cx < dst.getWidth(); cx++) {
+                                int color = dst.getRGB(cx, cy);
+
+                                dst.setRGB(cx, cy, color | alpha);
+                            }
+                        }
+                    }
+                }
+            }
         }
 
         if (needToConvert) {
             // ColorModels are not the same
             ColorConvertOp ccop = new ColorConvertOp(hints);
-            ccop.filter(dst, origDst);
+            dst = ccop.filter(dst, origDst);
         }
-
-        return origDst;
+        return dst;
     }
 
     /**
@@ -461,6 +497,10 @@
      *         stated in the class comments.
      */
     public final WritableRaster filter (Raster src, WritableRaster dst)  {
+        return filterRasterImpl(src, dst, length);
+    }
+
+    private WritableRaster filterRasterImpl(Raster src, WritableRaster dst, int scaleConst) {
         int numBands = src.getNumBands();
         int width  = src.getWidth();
         int height = src.getHeight();
@@ -484,15 +524,15 @@
                             + " does not equal number of bands in dest "
                             + dst.getNumBands());
         }
+
         // Make sure that the arrays match
         // Make sure that the low/high/constant arrays match
-        if (length != 1 && length != src.getNumBands()) {
+        if (scaleConst != 1 && scaleConst != src.getNumBands()) {
             throw new IllegalArgumentException("Number of scaling constants "+
                                                "does not equal the number of"+
                                                " of bands in the src raster");
         }
 
-
         //
         // Try for a native raster rescale first
         //
@@ -523,7 +563,7 @@
             //
             // Fall back to the slow code
             //
-            if (length > 1) {
+            if (scaleConst > 1) {
                 step = 1;
             }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/image/RescaleOp/RescaleAlphaTest.java	Thu Aug 06 11:36:52 2015 +0300
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2015, 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 8080287
+ * @run RescaleAlphaTest
+ * @summary RescaleOp with scaleFactor/alpha should copy alpha to destination
+ * channel
+ */
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.RescaleOp;
+import java.awt.Color;
+import java.awt.Frame;
+import java.io.IOException;
+
+public class RescaleAlphaTest {
+
+    BufferedImage bimg = null, bimg1;
+    int w = 10, h = 10;
+    float scaleFactor = 0.5f;
+    float offset = 0.0f;
+
+    public static void main(String[] args) throws Exception {
+        RescaleAlphaTest test = new RescaleAlphaTest();
+        test.startTest();
+    }
+
+    private void startTest() throws Exception {
+
+        // Test with source image with alpha channel
+
+        bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g2d = bimg.createGraphics();
+        g2d.setColor(Color.GREEN);
+        g2d.fillRect(0, 0, w, h);
+
+        RescaleOp res = new RescaleOp(scaleFactor, offset, null);
+        bimg1 = res.filter(bimg, null);
+
+        // check if destination image has alpha channel copied from src
+        checkForAlpha(bimg1);
+
+        // Test with source image without alpha channel
+        bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
+        g2d = bimg.createGraphics();
+        g2d.setColor(Color.GREEN);
+        g2d.fillRect(0, 0, w, h);
+
+
+        res = new RescaleOp(scaleFactor, offset, null);
+
+        // Create destination image with alpha channel
+        bimg1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
+        bimg1 = res.filter(bimg, bimg1);
+
+        // check if filtered destination image has alpha channel
+        checkForAlpha(bimg1);
+
+    }
+
+    private void checkForAlpha(BufferedImage bi) throws IOException {
+        int argb = bi.getRGB(w/2, h/2);
+        if ((argb >>> 24) != 255) {
+            throw new
+            RuntimeException("Wrong alpha in destination image.RescaleOp with alpha failed.");
+        }
+    }
+}