1 /*
   2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file was originally generated by JSLC
  28  * and then hand edited for performance.
  29  */
  30 
  31 package com.sun.scenario.effect.impl.sw.java;
  32 
  33 import com.sun.scenario.effect.Effect;
  34 import com.sun.scenario.effect.FilterContext;
  35 import com.sun.scenario.effect.ImageData;
  36 import com.sun.scenario.effect.impl.HeapImage;
  37 import com.sun.scenario.effect.impl.Renderer;
  38 import com.sun.javafx.geom.Rectangle;
  39 import com.sun.javafx.geom.transform.BaseTransform;
  40 import com.sun.scenario.effect.impl.state.BoxRenderState;
  41 
  42 public class JSWBoxBlurPeer extends JSWEffectPeer<BoxRenderState> {
  43 
  44     public JSWBoxBlurPeer(FilterContext fctx, Renderer r, String uniqueName) {
  45         super(fctx, r, uniqueName);
  46     }
  47 
  48     @Override
  49     public ImageData filter(Effect effect,
  50                             BoxRenderState brstate,
  51                             BaseTransform transform,
  52                             Rectangle outputClip,
  53                             ImageData... inputs)
  54     {
  55         setRenderState(brstate);
  56         // NOTE: for now, all input images must be TYPE_INT_ARGB_PRE
  57 
  58         // Calculate the amount the image grows on each iteration (size-1)
  59         boolean horizontal = (getPass() == 0);
  60         int hinc = horizontal ? brstate.getBoxPixelSize(0) - 1 : 0;
  61         int vinc = horizontal ? 0 : brstate.getBoxPixelSize(1) - 1;
  62         int iterations = brstate.getBlurPasses();
  63         if (iterations < 1 || (hinc < 1 && vinc < 1)) {
  64             inputs[0].addref();
  65             return inputs[0];
  66         }
  67         // Calculate the amount the image will grow through the full operation
  68         // Always upgrade to the next even amount of growth
  69         int growx = (hinc * iterations + 1) & (~0x1);
  70         int growy = (vinc * iterations + 1) & (~0x1);
  71 
  72         // Assert: rstate.getEffectTransformSpace() == UserSpace
  73         // NOTE: We could still have a transformed ImageData for other reasons...
  74         HeapImage src = (HeapImage)inputs[0].getUntransformedImage();
  75         Rectangle srcr = inputs[0].getUntransformedBounds();
  76 
  77         HeapImage cur = src;
  78         int curw = srcr.width;
  79         int curh = srcr.height;
  80         int curscan = cur.getScanlineStride();
  81         int[] curPixels = cur.getPixelArray();
  82 
  83         int finalw = curw + growx;
  84         int finalh = curh + growy;
  85         while (curw < finalw || curh < finalh) {
  86             int neww = curw + hinc;
  87             int newh = curh + vinc;
  88             if (neww > finalw) neww = finalw;
  89             if (newh > finalh) newh = finalh;
  90             HeapImage dst = (HeapImage)getRenderer().getCompatibleImage(neww, newh);
  91             int newscan = dst.getScanlineStride();
  92             int[] newPixels = dst.getPixelArray();
  93             if (horizontal) {
  94                 filterHorizontal(newPixels, neww, newh, newscan,
  95                                  curPixels, curw, curh, curscan);
  96             } else {
  97                 filterVertical(newPixels, neww, newh, newscan,
  98                                curPixels, curw, curh, curscan);
  99             }
 100             if (cur != src) {
 101                 getRenderer().releaseCompatibleImage(cur);
 102             }
 103             cur = dst;
 104             curw = neww;
 105             curh = newh;
 106             curPixels = newPixels;
 107             curscan = newscan;
 108         }
 109 
 110         Rectangle resBounds =
 111             new Rectangle(srcr.x - growx/2, srcr.y - growy/2, curw, curh);
 112         return new ImageData(getFilterContext(), cur, resBounds);
 113     }
 114 
 115     protected void filterHorizontal(int dstPixels[], int dstw, int dsth, int dstscan,
 116                                     int srcPixels[], int srcw, int srch, int srcscan)
 117     {
 118         int hsize = dstw - srcw + 1;
 119         int kscale = 0x7fffffff / (hsize * 255);
 120         int srcoff = 0;
 121         int dstoff = 0;
 122         for (int y = 0; y < dsth; y++) {
 123             int suma = 0;
 124             int sumr = 0;
 125             int sumg = 0;
 126             int sumb = 0;
 127             for (int x = 0; x < dstw; x++) {
 128                 int rgb;
 129                 // Un-accumulate the data for col-hsize location into the sums.
 130                 rgb = (x >= hsize) ? srcPixels[srcoff + x - hsize] : 0;
 131                 suma -= (rgb >>> 24);
 132                 sumr -= (rgb >>  16) & 0xff;
 133                 sumg -= (rgb >>   8) & 0xff;
 134                 sumb -= (rgb       ) & 0xff;
 135                 // Accumulate the data for this col location into the sums.
 136                 rgb = (x < srcw) ? srcPixels[srcoff + x] : 0;
 137                 suma += (rgb >>> 24);
 138                 sumr += (rgb >>  16) & 0xff;
 139                 sumg += (rgb >>   8) & 0xff;
 140                 sumb += (rgb       ) & 0xff;
 141                 dstPixels[dstoff + x] =
 142                     (((suma * kscale) >> 23) << 24) +
 143                     (((sumr * kscale) >> 23) << 16) +
 144                     (((sumg * kscale) >> 23) <<  8) +
 145                     (((sumb * kscale) >> 23)      );
 146             }
 147             srcoff += srcscan;
 148             dstoff += dstscan;
 149         }
 150     }
 151 
 152     protected void filterVertical(int dstPixels[], int dstw, int dsth, int dstscan,
 153                                   int srcPixels[], int srcw, int srch, int srcscan)
 154     {
 155         int vsize = dsth - srch + 1;
 156         int kscale = 0x7fffffff / (vsize * 255);
 157         int voff = vsize * srcscan;
 158         for (int x = 0; x < dstw; x++) {
 159             int suma = 0;
 160             int sumr = 0;
 161             int sumg = 0;
 162             int sumb = 0;
 163             int srcoff = x;
 164             int dstoff = x;
 165             for (int y = 0; y < dsth; y++) {
 166                 int rgb;
 167                 // Un-accumulate the data for row-vsize location into the sums.
 168                 rgb = (srcoff >= voff) ? srcPixels[srcoff - voff] : 0;
 169                 suma -= (rgb >>> 24);
 170                 sumr -= (rgb >>  16) & 0xff;
 171                 sumg -= (rgb >>   8) & 0xff;
 172                 sumb -= (rgb       ) & 0xff;
 173                 // Accumulate the data for this row location into the sums.
 174                 rgb = (y < srch) ? srcPixels[srcoff] : 0;
 175                 suma += (rgb >>> 24);
 176                 sumr += (rgb >>  16) & 0xff;
 177                 sumg += (rgb >>   8) & 0xff;
 178                 sumb += (rgb       ) & 0xff;
 179                 dstPixels[dstoff] =
 180                     (((suma * kscale) >> 23) << 24) +
 181                     (((sumr * kscale) >> 23) << 16) +
 182                     (((sumg * kscale) >> 23) <<  8) +
 183                     (((sumb * kscale) >> 23)      );
 184                 srcoff += srcscan;
 185                 dstoff += dstscan;
 186             }
 187         }
 188     }
 189 
 190     /*
 191      * This is a useful routine for some uses - it goes faster than the
 192      * horizontal-only and vertical-only loops, but it is hard to use it
 193      * in the face of multi-pass box blurs and having to adjust for even
 194      * blur sizes, so it is commented out for now...
 195     private void filterTranspose(int dstPixels[], int dstw, int dsth, int dstscan,
 196                                  int srcPixels[], int srcw, int srch, int srcscan,
 197                                  int ksize)
 198     {
 199         int kscale = 0x7fffffff / (ksize * 255);
 200         int srcoff = 0;
 201         for (int y = 0; y < dstw; y++) {
 202             int suma = 0;
 203             int sumr = 0;
 204             int sumg = 0;
 205             int sumb = 0;
 206             int dstoff = y;
 207             for (int x = 0; x < dsth; x++) {
 208                 int rgb;
 209                 // Un-accumulate the data for col-ksize location into the sums.
 210                 rgb = (x >= ksize) ? srcPixels[srcoff + x - ksize] : 0;
 211                 suma -= (rgb >>> 24);
 212                 sumr -= (rgb >>  16) & 0xff;
 213                 sumg -= (rgb >>   8) & 0xff;
 214                 sumb -= (rgb       ) & 0xff;
 215                 // Accumulate the data for this col location into the sums.
 216                 rgb = (x < srcw) ? srcPixels[srcoff + x] : 0;
 217                 suma += (rgb >>> 24);
 218                 sumr += (rgb >>  16) & 0xff;
 219                 sumg += (rgb >>   8) & 0xff;
 220                 sumb += (rgb       ) & 0xff;
 221                 dstPixels[dstoff] =
 222                     (((suma * kscale) >> 23) << 24) +
 223                     (((sumr * kscale) >> 23) << 16) +
 224                     (((sumg * kscale) >> 23) <<  8) +
 225                     (((sumb * kscale) >> 23)      );
 226                 dstoff += dstscan;
 227             }
 228             srcoff += srcscan;
 229         }
 230     }
 231      */
 232 }