epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkBlurMask.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 13 | #include "SkEndian.h" |
| 14 | |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 15 | /** |
| 16 | * This function performs a box blur in X, of the given radius. If the |
| 17 | * "transpose" parameter is true, it will transpose the pixels on write, |
| 18 | * such that X and Y are swapped. Reads are always performed from contiguous |
| 19 | * memory in X, for speed. The destination buffer (dst) must be at least |
| 20 | * (width + radius * 2) * height bytes in size. |
| 21 | */ |
| 22 | static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, |
| 23 | int radius, int width, int height, bool transpose) |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 24 | { |
| 25 | int kernelSize = radius * 2 + 1; |
| 26 | int border = SkMin32(width, radius * 2); |
| 27 | uint32_t scale = (1 << 24) / kernelSize; |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 28 | int new_width = width + radius * 2; |
| 29 | int dst_x_stride = transpose ? height : 1; |
| 30 | int dst_y_stride = transpose ? 1 : new_width; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 31 | for (int y = 0; y < height; ++y) { |
| 32 | int sum = 0; |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 33 | uint8_t* dptr = dst + y * dst_y_stride; |
| 34 | const uint8_t* right = src + y * src_y_stride; |
| 35 | const uint8_t* left = right; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 36 | for (int x = 0; x < border; ++x) { |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 37 | sum += *right++; |
| 38 | *dptr = (sum * scale) >> 24; |
| 39 | dptr += dst_x_stride; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 40 | } |
| 41 | for (int x = width; x < radius * 2; ++x) { |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 42 | *dptr = (sum * scale) >> 24; |
| 43 | dptr += dst_x_stride; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 44 | } |
| 45 | for (int x = radius * 2; x < width; ++x) { |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 46 | sum += *right++; |
| 47 | *dptr = (sum * scale) >> 24; |
| 48 | sum -= *left++; |
| 49 | dptr += dst_x_stride; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 50 | } |
| 51 | for (int x = 0; x < border; ++x) { |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 52 | *dptr = (sum * scale) >> 24; |
| 53 | sum -= *left++; |
| 54 | dptr += dst_x_stride; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 55 | } |
| 56 | SkASSERT(sum == 0); |
| 57 | } |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 58 | return new_width; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 61 | // Unrolling the integer blur kernel seems to give us a ~15% speedup on Windows, |
| 62 | // breakeven on Mac, and ~15% slowdown on Linux. |
| 63 | // Reading a word at a time when bulding the sum buffer seems to give |
| 64 | // us no appreciable speedup on Windows or Mac, and 2% slowdown on Linux. |
tomhudson@google.com | 054ff1e | 2012-01-11 19:29:08 +0000 | [diff] [blame] | 65 | #if defined(SK_BUILD_FOR_WIN32) |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 66 | #define UNROLL_KERNEL_LOOP 1 |
| 67 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 69 | /** The sum buffer is an array of u32 to hold the accumulated sum of all of the |
| 70 | src values at their position, plus all values above and to the left. |
| 71 | When we sample into this buffer, we need an initial row and column of 0s, |
| 72 | so we have an index correspondence as follows: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 74 | src[i, j] == sum[i+1, j+1] |
| 75 | sum[0, j] == sum[i, 0] == 0 |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 76 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 77 | We assume that the sum buffer's stride == its width |
| 78 | */ |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 79 | static void build_sum_buffer(uint32_t sum[], int srcW, int srcH, |
| 80 | const uint8_t src[], int srcRB) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 81 | int sumW = srcW + 1; |
| 82 | |
| 83 | SkASSERT(srcRB >= srcW); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | // mod srcRB so we can apply it after each row |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 85 | srcRB -= srcW; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
| 87 | int x, y; |
| 88 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 89 | // zero out the top row and column |
| 90 | memset(sum, 0, sumW * sizeof(sum[0])); |
| 91 | sum += sumW; |
| 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | // special case first row |
| 94 | uint32_t X = 0; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 95 | *sum++ = 0; // initialze the first column to 0 |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 96 | for (x = srcW - 1; x >= 0; --x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | X = *src++ + X; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 98 | *sum++ = X; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | src += srcRB; |
| 101 | |
| 102 | // now do the rest of the rows |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 103 | for (y = srcH - 1; y > 0; --y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | uint32_t L = 0; |
| 105 | uint32_t C = 0; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 106 | *sum++ = 0; // initialze the first column to 0 |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 107 | |
| 108 | for (x = srcW - 1; !SkIsAlign4((intptr_t) src) && x >= 0; x--) { |
| 109 | uint32_t T = sum[-sumW]; |
| 110 | X = *src++ + L + T - C; |
| 111 | *sum++ = X; |
| 112 | L = X; |
| 113 | C = T; |
| 114 | } |
| 115 | |
| 116 | for (; x >= 4; x-=4) { |
| 117 | uint32_t T = sum[-sumW]; |
| 118 | X = *src++ + L + T - C; |
| 119 | *sum++ = X; |
| 120 | L = X; |
| 121 | C = T; |
| 122 | T = sum[-sumW]; |
| 123 | X = *src++ + L + T - C; |
| 124 | *sum++ = X; |
| 125 | L = X; |
| 126 | C = T; |
| 127 | T = sum[-sumW]; |
| 128 | X = *src++ + L + T - C; |
| 129 | *sum++ = X; |
| 130 | L = X; |
| 131 | C = T; |
| 132 | T = sum[-sumW]; |
| 133 | X = *src++ + L + T - C; |
| 134 | *sum++ = X; |
| 135 | L = X; |
| 136 | C = T; |
| 137 | } |
| 138 | |
| 139 | for (; x >= 0; --x) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 140 | uint32_t T = sum[-sumW]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | X = *src++ + L + T - C; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 142 | *sum++ = X; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | L = X; |
| 144 | C = T; |
| 145 | } |
| 146 | src += srcRB; |
| 147 | } |
| 148 | } |
| 149 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 150 | /** |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 151 | * This is the path for apply_kernel() to be taken when the kernel |
| 152 | * is wider than the source image. |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 153 | */ |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 154 | static void kernel_clamped(uint8_t dst[], int rx, int ry, const uint32_t sum[], |
| 155 | int sw, int sh) { |
| 156 | SkASSERT(2*rx > sw); |
| 157 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | uint32_t scale = (1 << 24) / ((2*rx + 1)*(2*ry + 1)); |
| 159 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 160 | int sumStride = sw + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | |
| 162 | int dw = sw + 2*rx; |
| 163 | int dh = sh + 2*ry; |
| 164 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 165 | int prev_y = -2*ry; |
| 166 | int next_y = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 168 | for (int y = 0; y < dh; y++) { |
| 169 | int py = SkClampPos(prev_y) * sumStride; |
| 170 | int ny = SkFastMin32(next_y, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 172 | int prev_x = -2*rx; |
| 173 | int next_x = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 175 | for (int x = 0; x < dw; x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | int px = SkClampPos(prev_x); |
| 177 | int nx = SkFastMin32(next_x, sw); |
| 178 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 179 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 180 | *dst++ = SkToU8(tmp * scale >> 24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | |
| 182 | prev_x += 1; |
| 183 | next_x += 1; |
| 184 | } |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 185 | |
| 186 | prev_y += 1; |
| 187 | next_y += 1; |
| 188 | } |
| 189 | } |
| 190 | /** |
| 191 | * sw and sh are the width and height of the src. Since the sum buffer |
| 192 | * matches that, but has an extra row and col at the beginning (with zeros), |
| 193 | * we can just use sw and sh as our "max" values for pinning coordinates |
| 194 | * when sampling into sum[][] |
| 195 | * |
| 196 | * The inner loop is conceptually simple; we break it into several sections |
| 197 | * to improve performance. Here's the original version: |
| 198 | for (int x = 0; x < dw; x++) { |
| 199 | int px = SkClampPos(prev_x); |
| 200 | int nx = SkFastMin32(next_x, sw); |
| 201 | |
| 202 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 203 | *dst++ = SkToU8(tmp * scale >> 24); |
| 204 | |
| 205 | prev_x += 1; |
| 206 | next_x += 1; |
| 207 | } |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 208 | * The sections are: |
| 209 | * left-hand section, where prev_x is clamped to 0 |
| 210 | * center section, where neither prev_x nor next_x is clamped |
| 211 | * right-hand section, where next_x is clamped to sw |
| 212 | * On some operating systems, the center section is unrolled for additional |
| 213 | * speedup. |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 214 | */ |
| 215 | static void apply_kernel(uint8_t dst[], int rx, int ry, const uint32_t sum[], |
| 216 | int sw, int sh) { |
| 217 | if (2*rx > sw) { |
| 218 | kernel_clamped(dst, rx, ry, sum, sw, sh); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | uint32_t scale = (1 << 24) / ((2*rx + 1)*(2*ry + 1)); |
| 223 | |
| 224 | int sumStride = sw + 1; |
| 225 | |
| 226 | int dw = sw + 2*rx; |
| 227 | int dh = sh + 2*ry; |
| 228 | |
| 229 | int prev_y = -2*ry; |
| 230 | int next_y = 1; |
| 231 | |
| 232 | SkASSERT(2*rx <= dw - 2*rx); |
| 233 | |
| 234 | for (int y = 0; y < dh; y++) { |
| 235 | int py = SkClampPos(prev_y) * sumStride; |
| 236 | int ny = SkFastMin32(next_y, sh) * sumStride; |
| 237 | |
| 238 | int prev_x = -2*rx; |
| 239 | int next_x = 1; |
| 240 | int x = 0; |
| 241 | |
| 242 | for (; x < 2*rx; x++) { |
| 243 | SkASSERT(prev_x <= 0); |
| 244 | SkASSERT(next_x <= sw); |
| 245 | |
| 246 | int px = 0; |
| 247 | int nx = next_x; |
| 248 | |
| 249 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 250 | *dst++ = SkToU8(tmp * scale >> 24); |
| 251 | |
| 252 | prev_x += 1; |
| 253 | next_x += 1; |
| 254 | } |
| 255 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 256 | int i0 = prev_x + py; |
| 257 | int i1 = next_x + ny; |
| 258 | int i2 = next_x + py; |
| 259 | int i3 = prev_x + ny; |
| 260 | |
| 261 | #if UNROLL_KERNEL_LOOP |
| 262 | for (; x < dw - 2*rx - 4; x += 4) { |
| 263 | SkASSERT(prev_x >= 0); |
| 264 | SkASSERT(next_x <= sw); |
| 265 | |
| 266 | uint32_t tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 267 | *dst++ = SkToU8(tmp * scale >> 24); |
| 268 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 269 | *dst++ = SkToU8(tmp * scale >> 24); |
| 270 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 271 | *dst++ = SkToU8(tmp * scale >> 24); |
| 272 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 273 | *dst++ = SkToU8(tmp * scale >> 24); |
| 274 | |
| 275 | prev_x += 4; |
| 276 | next_x += 4; |
| 277 | } |
| 278 | #endif |
| 279 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 280 | for (; x < dw - 2*rx; x++) { |
| 281 | SkASSERT(prev_x >= 0); |
| 282 | SkASSERT(next_x <= sw); |
| 283 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 284 | uint32_t tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 285 | *dst++ = SkToU8(tmp * scale >> 24); |
| 286 | |
| 287 | prev_x += 1; |
| 288 | next_x += 1; |
| 289 | } |
| 290 | |
| 291 | for (; x < dw; x++) { |
| 292 | SkASSERT(prev_x >= 0); |
| 293 | SkASSERT(next_x > sw); |
| 294 | |
| 295 | int px = prev_x; |
| 296 | int nx = sw; |
| 297 | |
| 298 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 299 | *dst++ = SkToU8(tmp * scale >> 24); |
| 300 | |
| 301 | prev_x += 1; |
| 302 | next_x += 1; |
| 303 | } |
| 304 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | prev_y += 1; |
| 306 | next_y += 1; |
| 307 | } |
| 308 | } |
| 309 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 310 | /** |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 311 | * This is the path for apply_kernel_interp() to be taken when the kernel |
| 312 | * is wider than the source image. |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 313 | */ |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 314 | static void kernel_interp_clamped(uint8_t dst[], int rx, int ry, |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 315 | const uint32_t sum[], int sw, int sh, U8CPU outer_weight) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 316 | SkASSERT(2*rx > sw); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | |
| 318 | int inner_weight = 255 - outer_weight; |
| 319 | |
| 320 | // round these guys up if they're bigger than 127 |
| 321 | outer_weight += outer_weight >> 7; |
| 322 | inner_weight += inner_weight >> 7; |
| 323 | |
| 324 | uint32_t outer_scale = (outer_weight << 16) / ((2*rx + 1)*(2*ry + 1)); |
| 325 | uint32_t inner_scale = (inner_weight << 16) / ((2*rx - 1)*(2*ry - 1)); |
| 326 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 327 | int sumStride = sw + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | |
| 329 | int dw = sw + 2*rx; |
| 330 | int dh = sh + 2*ry; |
| 331 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 332 | int prev_y = -2*ry; |
| 333 | int next_y = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 335 | for (int y = 0; y < dh; y++) { |
| 336 | int py = SkClampPos(prev_y) * sumStride; |
| 337 | int ny = SkFastMin32(next_y, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 339 | int ipy = SkClampPos(prev_y + 1) * sumStride; |
| 340 | int iny = SkClampMax(next_y - 1, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 342 | int prev_x = -2*rx; |
| 343 | int next_x = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 345 | for (int x = 0; x < dw; x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | int px = SkClampPos(prev_x); |
| 347 | int nx = SkFastMin32(next_x, sw); |
| 348 | |
| 349 | int ipx = SkClampPos(prev_x + 1); |
| 350 | int inx = SkClampMax(next_x - 1, sw); |
| 351 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 352 | uint32_t outer_sum = sum[px+py] + sum[nx+ny] |
| 353 | - sum[nx+py] - sum[px+ny]; |
| 354 | uint32_t inner_sum = sum[ipx+ipy] + sum[inx+iny] |
| 355 | - sum[inx+ipy] - sum[ipx+iny]; |
| 356 | *dst++ = SkToU8((outer_sum * outer_scale |
| 357 | + inner_sum * inner_scale) >> 24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | |
| 359 | prev_x += 1; |
| 360 | next_x += 1; |
| 361 | } |
| 362 | prev_y += 1; |
| 363 | next_y += 1; |
| 364 | } |
| 365 | } |
| 366 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 367 | /** |
| 368 | * sw and sh are the width and height of the src. Since the sum buffer |
| 369 | * matches that, but has an extra row and col at the beginning (with zeros), |
| 370 | * we can just use sw and sh as our "max" values for pinning coordinates |
| 371 | * when sampling into sum[][] |
| 372 | * |
| 373 | * The inner loop is conceptually simple; we break it into several variants |
| 374 | * to improve performance. Here's the original version: |
| 375 | for (int x = 0; x < dw; x++) { |
| 376 | int px = SkClampPos(prev_x); |
| 377 | int nx = SkFastMin32(next_x, sw); |
| 378 | |
| 379 | int ipx = SkClampPos(prev_x + 1); |
| 380 | int inx = SkClampMax(next_x - 1, sw); |
| 381 | |
| 382 | uint32_t outer_sum = sum[px+py] + sum[nx+ny] |
| 383 | - sum[nx+py] - sum[px+ny]; |
| 384 | uint32_t inner_sum = sum[ipx+ipy] + sum[inx+iny] |
| 385 | - sum[inx+ipy] - sum[ipx+iny]; |
| 386 | *dst++ = SkToU8((outer_sum * outer_scale |
| 387 | + inner_sum * inner_scale) >> 24); |
| 388 | |
| 389 | prev_x += 1; |
| 390 | next_x += 1; |
| 391 | } |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 392 | * The sections are: |
| 393 | * left-hand section, where prev_x is clamped to 0 |
| 394 | * center section, where neither prev_x nor next_x is clamped |
| 395 | * right-hand section, where next_x is clamped to sw |
| 396 | * On some operating systems, the center section is unrolled for additional |
| 397 | * speedup. |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 398 | */ |
| 399 | static void apply_kernel_interp(uint8_t dst[], int rx, int ry, |
| 400 | const uint32_t sum[], int sw, int sh, U8CPU outer_weight) { |
| 401 | SkASSERT(rx > 0 && ry > 0); |
| 402 | SkASSERT(outer_weight <= 255); |
| 403 | |
| 404 | if (2*rx > sw) { |
| 405 | kernel_interp_clamped(dst, rx, ry, sum, sw, sh, outer_weight); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | int inner_weight = 255 - outer_weight; |
| 410 | |
| 411 | // round these guys up if they're bigger than 127 |
| 412 | outer_weight += outer_weight >> 7; |
| 413 | inner_weight += inner_weight >> 7; |
| 414 | |
| 415 | uint32_t outer_scale = (outer_weight << 16) / ((2*rx + 1)*(2*ry + 1)); |
| 416 | uint32_t inner_scale = (inner_weight << 16) / ((2*rx - 1)*(2*ry - 1)); |
| 417 | |
| 418 | int sumStride = sw + 1; |
| 419 | |
| 420 | int dw = sw + 2*rx; |
| 421 | int dh = sh + 2*ry; |
| 422 | |
| 423 | int prev_y = -2*ry; |
| 424 | int next_y = 1; |
| 425 | |
| 426 | SkASSERT(2*rx <= dw - 2*rx); |
| 427 | |
| 428 | for (int y = 0; y < dh; y++) { |
| 429 | int py = SkClampPos(prev_y) * sumStride; |
| 430 | int ny = SkFastMin32(next_y, sh) * sumStride; |
| 431 | |
| 432 | int ipy = SkClampPos(prev_y + 1) * sumStride; |
| 433 | int iny = SkClampMax(next_y - 1, sh) * sumStride; |
| 434 | |
| 435 | int prev_x = -2*rx; |
| 436 | int next_x = 1; |
| 437 | int x = 0; |
| 438 | |
| 439 | for (; x < 2*rx; x++) { |
| 440 | SkASSERT(prev_x < 0); |
| 441 | SkASSERT(next_x <= sw); |
| 442 | |
| 443 | int px = 0; |
| 444 | int nx = next_x; |
| 445 | |
| 446 | int ipx = 0; |
| 447 | int inx = next_x - 1; |
| 448 | |
| 449 | uint32_t outer_sum = sum[px+py] + sum[nx+ny] |
| 450 | - sum[nx+py] - sum[px+ny]; |
| 451 | uint32_t inner_sum = sum[ipx+ipy] + sum[inx+iny] |
| 452 | - sum[inx+ipy] - sum[ipx+iny]; |
| 453 | *dst++ = SkToU8((outer_sum * outer_scale |
| 454 | + inner_sum * inner_scale) >> 24); |
| 455 | |
| 456 | prev_x += 1; |
| 457 | next_x += 1; |
| 458 | } |
| 459 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 460 | int i0 = prev_x + py; |
| 461 | int i1 = next_x + ny; |
| 462 | int i2 = next_x + py; |
| 463 | int i3 = prev_x + ny; |
| 464 | int i4 = prev_x + 1 + ipy; |
| 465 | int i5 = next_x - 1 + iny; |
| 466 | int i6 = next_x - 1 + ipy; |
| 467 | int i7 = prev_x + 1 + iny; |
| 468 | |
| 469 | #if UNROLL_KERNEL_LOOP |
| 470 | for (; x < dw - 2*rx - 4; x += 4) { |
| 471 | SkASSERT(prev_x >= 0); |
| 472 | SkASSERT(next_x <= sw); |
| 473 | |
| 474 | uint32_t outer_sum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 475 | uint32_t inner_sum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 476 | *dst++ = SkToU8((outer_sum * outer_scale |
| 477 | + inner_sum * inner_scale) >> 24); |
| 478 | outer_sum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 479 | inner_sum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 480 | *dst++ = SkToU8((outer_sum * outer_scale |
| 481 | + inner_sum * inner_scale) >> 24); |
| 482 | outer_sum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 483 | inner_sum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 484 | *dst++ = SkToU8((outer_sum * outer_scale |
| 485 | + inner_sum * inner_scale) >> 24); |
| 486 | outer_sum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 487 | inner_sum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 488 | *dst++ = SkToU8((outer_sum * outer_scale |
| 489 | + inner_sum * inner_scale) >> 24); |
| 490 | |
| 491 | prev_x += 4; |
| 492 | next_x += 4; |
| 493 | } |
| 494 | #endif |
| 495 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 496 | for (; x < dw - 2*rx; x++) { |
| 497 | SkASSERT(prev_x >= 0); |
| 498 | SkASSERT(next_x <= sw); |
| 499 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 500 | uint32_t outer_sum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 501 | uint32_t inner_sum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 502 | *dst++ = SkToU8((outer_sum * outer_scale |
| 503 | + inner_sum * inner_scale) >> 24); |
| 504 | |
| 505 | prev_x += 1; |
| 506 | next_x += 1; |
| 507 | } |
| 508 | |
| 509 | for (; x < dw; x++) { |
| 510 | SkASSERT(prev_x >= 0); |
| 511 | SkASSERT(next_x > sw); |
| 512 | |
| 513 | int px = prev_x; |
| 514 | int nx = sw; |
| 515 | |
| 516 | int ipx = prev_x + 1; |
| 517 | int inx = sw; |
| 518 | |
| 519 | uint32_t outer_sum = sum[px+py] + sum[nx+ny] |
| 520 | - sum[nx+py] - sum[px+ny]; |
| 521 | uint32_t inner_sum = sum[ipx+ipy] + sum[inx+iny] |
| 522 | - sum[inx+ipy] - sum[ipx+iny]; |
| 523 | *dst++ = SkToU8((outer_sum * outer_scale |
| 524 | + inner_sum * inner_scale) >> 24); |
| 525 | |
| 526 | prev_x += 1; |
| 527 | next_x += 1; |
| 528 | } |
| 529 | |
| 530 | prev_y += 1; |
| 531 | next_y += 1; |
| 532 | } |
| 533 | } |
| 534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | #include "SkColorPriv.h" |
| 536 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 537 | static void merge_src_with_blur(uint8_t dst[], int dstRB, |
| 538 | const uint8_t src[], int srcRB, |
| 539 | const uint8_t blur[], int blurRB, |
| 540 | int sw, int sh) { |
| 541 | dstRB -= sw; |
| 542 | srcRB -= sw; |
| 543 | blurRB -= sw; |
| 544 | while (--sh >= 0) { |
| 545 | for (int x = sw - 1; x >= 0; --x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 546 | *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src))); |
| 547 | dst += 1; |
| 548 | src += 1; |
| 549 | blur += 1; |
| 550 | } |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 551 | dst += dstRB; |
| 552 | src += srcRB; |
| 553 | blur += blurRB; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | static void clamp_with_orig(uint8_t dst[], int dstRowBytes, |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 558 | const uint8_t src[], int srcRowBytes, |
| 559 | int sw, int sh, |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 560 | SkBlurMask::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 561 | int x; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 562 | while (--sh >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | switch (style) { |
| 564 | case SkBlurMask::kSolid_Style: |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 565 | for (x = sw - 1; x >= 0; --x) { |
| 566 | int s = *src; |
| 567 | int d = *dst; |
| 568 | *dst = SkToU8(s + d - SkMulDiv255Round(s, d)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 569 | dst += 1; |
| 570 | src += 1; |
| 571 | } |
| 572 | break; |
| 573 | case SkBlurMask::kOuter_Style: |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 574 | for (x = sw - 1; x >= 0; --x) { |
| 575 | if (*src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 576 | *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src))); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 577 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | dst += 1; |
| 579 | src += 1; |
| 580 | } |
| 581 | break; |
| 582 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 583 | SkDEBUGFAIL("Unexpected blur style here"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | dst += dstRowBytes - sw; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 587 | src += srcRowBytes - sw; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 591 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 592 | |
| 593 | // we use a local funciton to wrap the class static method to work around |
| 594 | // a bug in gcc98 |
| 595 | void SkMask_FreeImage(uint8_t* image); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 596 | void SkMask_FreeImage(uint8_t* image) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | SkMask::FreeImage(image); |
| 598 | } |
| 599 | |
| 600 | bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 601 | SkScalar radius, Style style, Quality quality, |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 602 | SkIPoint* margin, bool separable) |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 603 | { |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 604 | if (src.fFormat != SkMask::kA8_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | return false; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 606 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 607 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 608 | // Force high quality off for small radii (performance) |
| 609 | if (radius < SkIntToScalar(3)) quality = kLow_Quality; |
| 610 | |
| 611 | // highQuality: use three box blur passes as a cheap way to approximate a Gaussian blur |
| 612 | int passCount = (quality == kHigh_Quality) ? 3 : 1; |
| 613 | SkScalar passRadius = SkScalarDiv(radius, SkScalarSqrt(SkIntToScalar(passCount))); |
| 614 | |
| 615 | int rx = SkScalarCeil(passRadius); |
| 616 | int outer_weight = 255 - SkScalarRound((SkIntToScalar(rx) - passRadius) * 255); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | |
| 618 | SkASSERT(rx >= 0); |
| 619 | SkASSERT((unsigned)outer_weight <= 255); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 620 | if (rx <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 621 | return false; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 622 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 623 | |
| 624 | int ry = rx; // only do square blur for now |
| 625 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 626 | int padx = passCount * rx; |
| 627 | int pady = passCount * ry; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 628 | if (margin) { |
| 629 | margin->set(padx, pady); |
| 630 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 631 | dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, |
| 632 | src.fBounds.fRight + padx, src.fBounds.fBottom + pady); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 633 | dst->fRowBytes = dst->fBounds.width(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 634 | dst->fFormat = SkMask::kA8_Format; |
| 635 | dst->fImage = NULL; |
| 636 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 637 | if (src.fImage) { |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 638 | size_t dstSize = dst->computeImageSize(); |
| 639 | if (0 == dstSize) { |
| 640 | return false; // too big to allocate, abort |
| 641 | } |
| 642 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | int sw = src.fBounds.width(); |
| 644 | int sh = src.fBounds.height(); |
| 645 | const uint8_t* sp = src.fImage; |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 646 | uint8_t* dp = SkMask::AllocImage(dstSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 647 | |
| 648 | SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); |
| 649 | |
| 650 | // build the blurry destination |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 651 | if (separable) { |
| 652 | SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); |
| 653 | uint8_t* tp = tmpBuffer.get(); |
| 654 | int w = sw, h = sh; |
| 655 | |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 656 | if (quality == kHigh_Quality) { |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame^] | 657 | // Do three X blurs, with a transpose on the final one. |
| 658 | w = boxBlur(sp, src.fRowBytes, tp, rx, w, h, false); |
| 659 | w = boxBlur(tp, w, dp, rx, w, h, false); |
| 660 | w = boxBlur(dp, w, tp, rx, w, h, true); |
| 661 | // Do three Y blurs, with a transpose on the final one. |
| 662 | h = boxBlur(tp, h, dp, ry, h, w, false); |
| 663 | h = boxBlur(dp, h, tp, ry, h, w, false); |
| 664 | h = boxBlur(tp, h, dp, ry, h, w, true); |
| 665 | } else { |
| 666 | w = boxBlur(sp, src.fRowBytes, tp, rx, w, h, true); |
| 667 | h = boxBlur(tp, h, dp, ry, h, w, true); |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 668 | } |
| 669 | } else { |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 670 | const size_t storageW = sw + 2 * (passCount - 1) * rx + 1; |
| 671 | const size_t storageH = sh + 2 * (passCount - 1) * ry + 1; |
| 672 | SkAutoTMalloc<uint32_t> storage(storageW * storageH); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 673 | uint32_t* sumBuffer = storage.get(); |
| 674 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 675 | //pass1: sp is source, dp is destination |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 676 | build_sum_buffer(sumBuffer, sw, sh, sp, src.fRowBytes); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 677 | if (outer_weight == 255) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | apply_kernel(dp, rx, ry, sumBuffer, sw, sh); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 679 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 680 | apply_kernel_interp(dp, rx, ry, sumBuffer, sw, sh, outer_weight); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 681 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 682 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 683 | if (quality == kHigh_Quality) { |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 684 | //pass2: dp is source, tmpBuffer is destination |
| 685 | int tmp_sw = sw + 2 * rx; |
| 686 | int tmp_sh = sh + 2 * ry; |
| 687 | SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); |
| 688 | build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, dp, tmp_sw); |
| 689 | if (outer_weight == 255) |
| 690 | apply_kernel(tmpBuffer.get(), rx, ry, sumBuffer, tmp_sw, tmp_sh); |
| 691 | else |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 692 | apply_kernel_interp(tmpBuffer.get(), rx, ry, sumBuffer, |
| 693 | tmp_sw, tmp_sh, outer_weight); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 694 | |
| 695 | //pass3: tmpBuffer is source, dp is destination |
| 696 | tmp_sw += 2 * rx; |
| 697 | tmp_sh += 2 * ry; |
| 698 | build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, tmpBuffer.get(), tmp_sw); |
| 699 | if (outer_weight == 255) |
| 700 | apply_kernel(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh); |
| 701 | else |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 702 | apply_kernel_interp(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh, |
| 703 | outer_weight); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 704 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | dst->fImage = dp; |
| 708 | // if need be, alloc the "real" dst (same size as src) and copy/merge |
| 709 | // the blur into it (applying the src) |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 710 | if (style == kInner_Style) { |
| 711 | // now we allocate the "real" dst, mirror the size of src |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 712 | size_t srcSize = src.computeImageSize(); |
| 713 | if (0 == srcSize) { |
| 714 | return false; // too big to allocate, abort |
| 715 | } |
| 716 | dst->fImage = SkMask::AllocImage(srcSize); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 717 | merge_src_with_blur(dst->fImage, src.fRowBytes, |
| 718 | sp, src.fRowBytes, |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 719 | dp + passCount * (rx + ry * dst->fRowBytes), |
| 720 | dst->fRowBytes, sw, sh); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 721 | SkMask::FreeImage(dp); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 722 | } else if (style != kNormal_Style) { |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 723 | clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes), |
| 724 | dst->fRowBytes, sp, src.fRowBytes, sw, sh, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 725 | } |
| 726 | (void)autoCall.detach(); |
| 727 | } |
| 728 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 729 | if (style == kInner_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 730 | dst->fBounds = src.fBounds; // restore trimmed bounds |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 731 | dst->fRowBytes = src.fRowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | } |
| 733 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 734 | return true; |
| 735 | } |
| 736 | |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 737 | bool SkBlurMask::BlurSeparable(SkMask* dst, const SkMask& src, |
| 738 | SkScalar radius, Style style, Quality quality, |
| 739 | SkIPoint* margin) |
| 740 | { |
| 741 | return SkBlurMask::Blur(dst, src, radius, style, quality, margin, true); |
| 742 | } |
| 743 | |
| 744 | bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, |
| 745 | SkScalar radius, Style style, Quality quality, |
| 746 | SkIPoint* margin) |
| 747 | { |
| 748 | return SkBlurMask::Blur(dst, src, radius, style, quality, margin, false); |
| 749 | } |