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.BoxShadow; 35 import com.sun.scenario.effect.FilterContext; 36 import com.sun.scenario.effect.ImageData; 37 import com.sun.scenario.effect.impl.HeapImage; 38 import com.sun.scenario.effect.impl.Renderer; 39 import com.sun.javafx.geom.Rectangle; 40 import com.sun.javafx.geom.transform.BaseTransform; 41 import com.sun.scenario.effect.impl.state.BoxRenderState; 42 43 public class JSWBoxShadowPeer extends JSWEffectPeer<BoxRenderState> { 44 45 public JSWBoxShadowPeer(FilterContext fctx, Renderer r, String uniqueName) { 46 super(fctx, r, uniqueName); 47 } 48 49 @Override 50 public ImageData filter(Effect effect, 51 BoxRenderState brstate, 52 BaseTransform transform, 53 Rectangle outputClip, 54 ImageData... inputs) 55 { 56 setRenderState(brstate); 57 // NOTE: for now, all input images must be TYPE_INT_ARGB_PRE 58 59 // Calculate the amount the image grows on each iteration (size-1) 60 boolean horizontal = (getPass() == 0); 61 int hinc = horizontal ? brstate.getBoxPixelSize(0) - 1 : 0; 62 int vinc = horizontal ? 0 : brstate.getBoxPixelSize(1) - 1; 63 if (hinc < 0) hinc = 0; 64 if (vinc < 0) vinc = 0; 65 int iterations = brstate.getBlurPasses(); 66 float spread = brstate.getSpread(); 67 if (horizontal && (iterations < 1 || (hinc < 1 && vinc < 1))) { 68 inputs[0].addref(); 69 return inputs[0]; 70 } 71 // Calculate the amount the image will grow through the full operation 72 // Always upgrade to the next even amount of growth 73 int growx = (hinc * iterations + 1) & (~0x1); 74 int growy = (vinc * iterations + 1) & (~0x1); 75 76 // Assert: rstate.getEffectTransformSpace() == UserSpace 77 // NOTE: We could still have a transformed ImageData for other reasons... 78 HeapImage src = (HeapImage)inputs[0].getUntransformedImage(); 79 Rectangle srcr = inputs[0].getUntransformedBounds(); 80 81 HeapImage cur = src; 82 int curw = srcr.width; 83 int curh = srcr.height; 84 int curscan = cur.getScanlineStride(); 85 int[] curPixels = cur.getPixelArray(); 86 87 int finalw = curw + growx; 88 int finalh = curh + growy; 89 boolean force = !horizontal; 90 while (force || curw < finalw || curh < finalh) { 91 int neww = curw + hinc; 92 int newh = curh + vinc; 93 if (neww > finalw) neww = finalw; 94 if (newh > finalh) newh = finalh; 95 HeapImage dst = (HeapImage)getRenderer().getCompatibleImage(neww, newh); 96 int newscan = dst.getScanlineStride(); 97 int[] newPixels = dst.getPixelArray(); 98 if (iterations == 0) { 99 // The last "fixup" iteration of 2 should have no spread. 100 spread = 0f; 101 } 102 if (horizontal) { 103 filterHorizontalBlack(newPixels, neww, newh, newscan, 104 curPixels, curw, curh, curscan, 105 spread); 106 } else if (neww < finalw || newh < finalh) { 107 // Use BLACK for shadow color until very last pass 108 filterVerticalBlack(newPixels, neww, newh, newscan, 109 curPixels, curw, curh, curscan, 110 spread); 111 } else { 112 float shadowColor[] = 113 brstate.getShadowColor().getPremultipliedRGBComponents(); 114 if (shadowColor[3] == 1f && 115 shadowColor[0] == 0f && 116 shadowColor[1] == 0f && 117 shadowColor[2] == 0f) 118 { 119 filterVerticalBlack(newPixels, neww, newh, newscan, 120 curPixels, curw, curh, curscan, 121 spread); 122 } else { 123 filterVertical(newPixels, neww, newh, newscan, 124 curPixels, curw, curh, curscan, 125 spread, shadowColor); 126 } 127 } 128 if (cur != src) { 129 getRenderer().releaseCompatibleImage(cur); 130 } 131 iterations--; 132 force = false; 133 cur = dst; 134 curw = neww; 135 curh = newh; 136 curPixels = newPixels; 137 curscan = newscan; 138 } 139 140 Rectangle resBounds = 141 new Rectangle(srcr.x - growx/2, srcr.y - growy/2, curw, curh); 142 return new ImageData(getFilterContext(), cur, resBounds); 143 } 144 145 protected void filterHorizontalBlack(int dstPixels[], int dstw, int dsth, int dstscan, 146 int srcPixels[], int srcw, int srch, int srcscan, 147 float spread) 148 { 149 int hsize = dstw - srcw + 1; 150 // amax goes from hsize*255 to 255 as spread goes from 0 to 1 151 int amax = hsize * 255; 152 amax += (255 - amax) * spread; 153 int kscale = 0x7fffffff / amax; 154 int amin = (amax / 255); 155 int srcoff = 0; 156 int dstoff = 0; 157 for (int y = 0; y < dsth; y++) { 158 int suma = 0; 159 for (int x = 0; x < dstw; x++) { 160 int rgb; 161 // Un-accumulate the data for col-hsize location into the sums. 162 rgb = (x >= hsize) ? srcPixels[srcoff + x - hsize] : 0; 163 suma -= (rgb >>> 24); 164 // Accumulate the data for this col location into the sums. 165 rgb = (x < srcw) ? srcPixels[srcoff + x] : 0; 166 suma += (rgb >>> 24); 167 // Clamp, scale and convert the sum into a color. 168 dstPixels[dstoff + x] = 169 ((suma < amin) ? 0 170 : ((suma >= amax) ? 0xff000000 171 : (((suma * kscale) >> 23) << 24))); 172 } 173 srcoff += srcscan; 174 dstoff += dstscan; 175 } 176 } 177 178 protected void filterVerticalBlack(int dstPixels[], int dstw, int dsth, int dstscan, 179 int srcPixels[], int srcw, int srch, int srcscan, 180 float spread) 181 { 182 int vsize = dsth - srch + 1; 183 // amax goes from hsize*255 to 255 as spread goes from 0 to 1 184 int amax = vsize * 255; 185 amax += (255 - amax) * spread; 186 int kscale = 0x7fffffff / amax; 187 int amin = (amax / 255); 188 int voff = vsize * srcscan; 189 for (int x = 0; x < dstw; x++) { 190 int suma = 0; 191 int srcoff = x; 192 int dstoff = x; 193 for (int y = 0; y < dsth; y++) { 194 int rgb; 195 // Un-accumulate the data for row-vsize location into the sums. 196 rgb = (srcoff >= voff) ? srcPixels[srcoff - voff] : 0; 197 suma -= (rgb >>> 24); 198 // Accumulate the data for this row location into the sums. 199 rgb = (y < srch) ? srcPixels[srcoff] : 0; 200 suma += (rgb >>> 24); 201 // Clamp, scale and convert the sum into a color. 202 dstPixels[dstoff] = 203 ((suma < amin) ? 0 204 : ((suma >= amax) ? 0xff000000 205 : (((suma * kscale) >> 23) << 24))); 206 srcoff += srcscan; 207 dstoff += dstscan; 208 } 209 } 210 } 211 212 protected void filterVertical(int dstPixels[], int dstw, int dsth, int dstscan, 213 int srcPixels[], int srcw, int srch, int srcscan, 214 float spread, float shadowColor[]) 215 { 216 int vsize = dsth - srch + 1; 217 // amax goes from hsize*255 to 255 as spread goes from 0 to 1 218 int amax = vsize * 255; 219 amax += (255 - amax) * spread; 220 int kscalea = 0x7fffffff / amax; 221 int kscaler = (int) (kscalea * shadowColor[0]); 222 int kscaleg = (int) (kscalea * shadowColor[1]); 223 int kscaleb = (int) (kscalea * shadowColor[2]); 224 kscalea *= shadowColor[3]; 225 int amin = (amax / 255); 226 int voff = vsize * srcscan; 227 int shadowRGB = 228 (((int) (shadowColor[0] * 255)) << 16) | 229 (((int) (shadowColor[1] * 255)) << 8) | 230 (((int) (shadowColor[2] * 255)) ) | 231 (((int) (shadowColor[3] * 255)) << 24); 232 for (int x = 0; x < dstw; x++) { 233 int suma = 0; 234 int srcoff = x; 235 int dstoff = x; 236 for (int y = 0; y < dsth; y++) { 237 int rgb; 238 // Un-accumulate the data for row-vsize location into the sums. 239 rgb = (srcoff >= voff) ? srcPixels[srcoff - voff] : 0; 240 suma -= (rgb >>> 24); 241 // Accumulate the data for this row location into the sums. 242 rgb = (y < srch) ? srcPixels[srcoff] : 0; 243 suma += (rgb >>> 24); 244 // Clamp, scale and convert the sum into a color. 245 dstPixels[dstoff] = 246 ((suma < amin) ? 0 247 : ((suma >= amax) ? shadowRGB 248 : ((((suma * kscalea) >> 23) << 24) | 249 (((suma * kscaler) >> 23) << 16) | 250 (((suma * kscaleg) >> 23) << 8) | 251 (((suma * kscaleb) >> 23) )))); 252 srcoff += srcscan; 253 dstoff += dstscan; 254 } 255 } 256 } 257 258 /* 259 * This is a useful routine for some uses - it goes faster than the 260 * horizontal-only and vertical-only loops, but it is hard to use it 261 * in the face of multi-pass box blurs and having to adjust for even 262 * blur sizes, so it is commented out for now... 263 private void filterTranspose(int dstPixels[], int dstw, int dsth, int dstscan, 264 int srcPixels[], int srcw, int srch, int srcscan, 265 int ksize) 266 { 267 int kscale = 0x7fffffff / (ksize * 255); 268 int srcoff = 0; 269 for (int y = 0; y < dstw; y++) { 270 int suma = 0; 271 int dstoff = y; 272 for (int x = 0; x < dsth; x++) { 273 int rgb; 274 // Un-accumulate the data for col-ksize location into the sums. 275 rgb = (x >= ksize) ? srcPixels[srcoff + x - ksize] : 0; 276 suma -= (rgb >>> 24); 277 // Accumulate the data for this col location into the sums. 278 rgb = (x < srcw) ? srcPixels[srcoff + x] : 0; 279 suma += (rgb >>> 24); 280 dstPixels[dstoff] = (((suma * kscale) >> 23) << 24); 281 dstoff += dstscan; 282 } 283 srcoff += srcscan; 284 } 285 } 286 */ 287 }