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 | |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 15 | // scale factor for the blur radius to match the behavior of the all existing blur |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 16 | // code (both on the CPU and the GPU). This magic constant is 1/sqrt(3). |
| 17 | |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 18 | // TODO: get rid of this fudge factor and move any required fudging up into |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 19 | // the calling library |
| 20 | |
| 21 | #define kBlurRadiusFudgeFactor SkFloatToScalar( .57735f ) |
| 22 | |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 23 | #define UNROLL_SEPARABLE_LOOPS |
| 24 | |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 25 | /** |
| 26 | * This function performs a box blur in X, of the given radius. If the |
skia.committer@gmail.com | 884e60b | 2012-11-16 02:01:17 +0000 | [diff] [blame] | 27 | * "transpose" parameter is true, it will transpose the pixels on write, |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 28 | * such that X and Y are swapped. Reads are always performed from contiguous |
| 29 | * memory in X, for speed. The destination buffer (dst) must be at least |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 30 | * (width + leftRadius + rightRadius) * height bytes in size. |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 31 | * |
| 32 | * This is what the inner loop looks like before unrolling, and with the two |
| 33 | * cases broken out separately (width < diameter, width >= diameter): |
skia.committer@gmail.com | 76bf70d | 2013-02-20 07:02:30 +0000 | [diff] [blame] | 34 | * |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 35 | * if (width < diameter) { |
| 36 | * for (int x = 0; x < width; ++x) { |
| 37 | * sum += *right++; |
skia.committer@gmail.com | 76bf70d | 2013-02-20 07:02:30 +0000 | [diff] [blame] | 38 | * *dptr = (sum * scale + half) >> 24; |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 39 | * dptr += dst_x_stride; |
| 40 | * } |
| 41 | * for (int x = width; x < diameter; ++x) { |
| 42 | * *dptr = (sum * scale + half) >> 24; |
| 43 | * dptr += dst_x_stride; |
| 44 | * } |
| 45 | * for (int x = 0; x < width; ++x) { |
| 46 | * *dptr = (sum * scale + half) >> 24; |
| 47 | * sum -= *left++; |
| 48 | * dptr += dst_x_stride; |
| 49 | * } |
| 50 | * } else { |
| 51 | * for (int x = 0; x < diameter; ++x) { |
| 52 | * sum += *right++; |
| 53 | * *dptr = (sum * scale + half) >> 24; |
| 54 | * dptr += dst_x_stride; |
| 55 | * } |
| 56 | * for (int x = diameter; x < width; ++x) { |
| 57 | * sum += *right++; |
| 58 | * *dptr = (sum * scale + half) >> 24; |
| 59 | * sum -= *left++; |
| 60 | * dptr += dst_x_stride; |
| 61 | * } |
| 62 | * for (int x = 0; x < diameter; ++x) { |
| 63 | * *dptr = (sum * scale + half) >> 24; |
| 64 | * sum -= *left++; |
| 65 | * dptr += dst_x_stride; |
| 66 | * } |
| 67 | * } |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 68 | */ |
| 69 | static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst, |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 70 | int leftRadius, int rightRadius, int width, int height, |
| 71 | bool transpose) |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 72 | { |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 73 | int diameter = leftRadius + rightRadius; |
| 74 | int kernelSize = diameter + 1; |
| 75 | int border = SkMin32(width, diameter); |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 76 | uint32_t scale = (1 << 24) / kernelSize; |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 77 | int new_width = width + SkMax32(leftRadius, rightRadius) * 2; |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 78 | int dst_x_stride = transpose ? height : 1; |
| 79 | int dst_y_stride = transpose ? 1 : new_width; |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 80 | #ifndef SK_DISABLE_BLUR_ROUNDING |
| 81 | uint32_t half = 1 << 23; |
| 82 | #else |
| 83 | uint32_t half = 0; |
| 84 | #endif |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 85 | for (int y = 0; y < height; ++y) { |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 86 | uint32_t sum = 0; |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 87 | uint8_t* dptr = dst + y * dst_y_stride; |
| 88 | const uint8_t* right = src + y * src_y_stride; |
| 89 | const uint8_t* left = right; |
senorblanco@chromium.org | 336b4da | 2012-11-20 17:09:40 +0000 | [diff] [blame] | 90 | for (int x = 0; x < rightRadius - leftRadius; x++) { |
| 91 | *dptr = 0; |
| 92 | dptr += dst_x_stride; |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 93 | } |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 94 | #define LEFT_BORDER_ITER \ |
| 95 | sum += *right++; \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 96 | *dptr = (sum * scale + half) >> 24; \ |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 97 | dptr += dst_x_stride; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 98 | |
| 99 | int x = 0; |
| 100 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 101 | for (; x < border - 16; x += 16) { |
| 102 | LEFT_BORDER_ITER |
| 103 | LEFT_BORDER_ITER |
| 104 | LEFT_BORDER_ITER |
| 105 | LEFT_BORDER_ITER |
| 106 | LEFT_BORDER_ITER |
| 107 | LEFT_BORDER_ITER |
| 108 | LEFT_BORDER_ITER |
| 109 | LEFT_BORDER_ITER |
| 110 | LEFT_BORDER_ITER |
| 111 | LEFT_BORDER_ITER |
| 112 | LEFT_BORDER_ITER |
| 113 | LEFT_BORDER_ITER |
| 114 | LEFT_BORDER_ITER |
| 115 | LEFT_BORDER_ITER |
| 116 | LEFT_BORDER_ITER |
| 117 | LEFT_BORDER_ITER |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 118 | } |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 119 | #endif |
| 120 | for (; x < border; ++x) { |
| 121 | LEFT_BORDER_ITER |
| 122 | } |
| 123 | #undef LEFT_BORDER_ITER |
| 124 | #define TRIVIAL_ITER \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 125 | *dptr = (sum * scale + half) >> 24; \ |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 126 | dptr += dst_x_stride; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 127 | x = width; |
| 128 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 129 | for (; x < diameter - 16; x += 16) { |
| 130 | TRIVIAL_ITER |
| 131 | TRIVIAL_ITER |
| 132 | TRIVIAL_ITER |
| 133 | TRIVIAL_ITER |
| 134 | TRIVIAL_ITER |
| 135 | TRIVIAL_ITER |
| 136 | TRIVIAL_ITER |
| 137 | TRIVIAL_ITER |
| 138 | TRIVIAL_ITER |
| 139 | TRIVIAL_ITER |
| 140 | TRIVIAL_ITER |
| 141 | TRIVIAL_ITER |
| 142 | TRIVIAL_ITER |
| 143 | TRIVIAL_ITER |
| 144 | TRIVIAL_ITER |
| 145 | TRIVIAL_ITER |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 146 | } |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 147 | #endif |
| 148 | for (; x < diameter; ++x) { |
| 149 | TRIVIAL_ITER |
| 150 | } |
| 151 | #undef TRIVIAL_ITER |
| 152 | #define CENTER_ITER \ |
| 153 | sum += *right++; \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 154 | *dptr = (sum * scale + half) >> 24; \ |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 155 | sum -= *left++; \ |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 156 | dptr += dst_x_stride; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 157 | |
| 158 | x = diameter; |
| 159 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 160 | for (; x < width - 16; x += 16) { |
| 161 | CENTER_ITER |
| 162 | CENTER_ITER |
| 163 | CENTER_ITER |
| 164 | CENTER_ITER |
| 165 | CENTER_ITER |
| 166 | CENTER_ITER |
| 167 | CENTER_ITER |
| 168 | CENTER_ITER |
| 169 | CENTER_ITER |
| 170 | CENTER_ITER |
| 171 | CENTER_ITER |
| 172 | CENTER_ITER |
| 173 | CENTER_ITER |
| 174 | CENTER_ITER |
| 175 | CENTER_ITER |
| 176 | CENTER_ITER |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 177 | } |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 178 | #endif |
| 179 | for (; x < width; ++x) { |
| 180 | CENTER_ITER |
| 181 | } |
| 182 | #undef CENTER_ITER |
| 183 | #define RIGHT_BORDER_ITER \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 184 | *dptr = (sum * scale + half) >> 24; \ |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 185 | sum -= *left++; \ |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 186 | dptr += dst_x_stride; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 187 | |
| 188 | x = 0; |
| 189 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 190 | for (; x < border - 16; x += 16) { |
| 191 | RIGHT_BORDER_ITER |
| 192 | RIGHT_BORDER_ITER |
| 193 | RIGHT_BORDER_ITER |
| 194 | RIGHT_BORDER_ITER |
| 195 | RIGHT_BORDER_ITER |
| 196 | RIGHT_BORDER_ITER |
| 197 | RIGHT_BORDER_ITER |
| 198 | RIGHT_BORDER_ITER |
| 199 | RIGHT_BORDER_ITER |
| 200 | RIGHT_BORDER_ITER |
| 201 | RIGHT_BORDER_ITER |
| 202 | RIGHT_BORDER_ITER |
| 203 | RIGHT_BORDER_ITER |
| 204 | RIGHT_BORDER_ITER |
| 205 | RIGHT_BORDER_ITER |
| 206 | RIGHT_BORDER_ITER |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 207 | } |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 208 | #endif |
| 209 | for (; x < border; ++x) { |
| 210 | RIGHT_BORDER_ITER |
| 211 | } |
| 212 | #undef RIGHT_BORDER_ITER |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 213 | for (int x = 0; x < leftRadius - rightRadius; ++x) { |
senorblanco@chromium.org | 336b4da | 2012-11-20 17:09:40 +0000 | [diff] [blame] | 214 | *dptr = 0; |
| 215 | dptr += dst_x_stride; |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 216 | } |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 217 | SkASSERT(sum == 0); |
| 218 | } |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 219 | return new_width; |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 220 | } |
| 221 | |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 222 | /** |
| 223 | * This variant of the box blur handles blurring of non-integer radii. It |
| 224 | * keeps two running sums: an outer sum for the rounded-up kernel radius, and |
| 225 | * an inner sum for the rounded-down kernel radius. For each pixel, it linearly |
| 226 | * interpolates between them. In float this would be: |
| 227 | * outer_weight * outer_sum / kernelSize + |
| 228 | * (1.0 - outer_weight) * innerSum / (kernelSize - 2) |
skia.committer@gmail.com | 76bf70d | 2013-02-20 07:02:30 +0000 | [diff] [blame] | 229 | * |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 230 | * This is what the inner loop looks like before unrolling, and with the two |
| 231 | * cases broken out separately (width < diameter, width >= diameter): |
skia.committer@gmail.com | 76bf70d | 2013-02-20 07:02:30 +0000 | [diff] [blame] | 232 | * |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 233 | * if (width < diameter) { |
| 234 | * for (int x = 0; x < width; x++) { |
| 235 | * inner_sum = outer_sum; |
| 236 | * outer_sum += *right++; |
| 237 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 238 | * dptr += dst_x_stride; |
| 239 | * } |
| 240 | * for (int x = width; x < diameter; ++x) { |
| 241 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 242 | * dptr += dst_x_stride; |
| 243 | * } |
| 244 | * for (int x = 0; x < width; x++) { |
| 245 | * inner_sum = outer_sum - *left++; |
| 246 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 247 | * dptr += dst_x_stride; |
| 248 | * outer_sum = inner_sum; |
| 249 | * } |
| 250 | * } else { |
| 251 | * for (int x = 0; x < diameter; x++) { |
| 252 | * inner_sum = outer_sum; |
| 253 | * outer_sum += *right++; |
| 254 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 255 | * dptr += dst_x_stride; |
| 256 | * } |
| 257 | * for (int x = diameter; x < width; ++x) { |
| 258 | * inner_sum = outer_sum - *left; |
| 259 | * outer_sum += *right++; |
| 260 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 261 | * dptr += dst_x_stride; |
| 262 | * outer_sum -= *left++; |
| 263 | * } |
| 264 | * for (int x = 0; x < diameter; x++) { |
| 265 | * inner_sum = outer_sum - *left++; |
| 266 | * *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
| 267 | * dptr += dst_x_stride; |
| 268 | * outer_sum = inner_sum; |
| 269 | * } |
| 270 | * } |
| 271 | * } |
| 272 | * return new_width; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 273 | */ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 274 | |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 275 | static int boxBlurInterp(const uint8_t* src, int src_y_stride, uint8_t* dst, |
| 276 | int radius, int width, int height, |
| 277 | bool transpose, uint8_t outer_weight) |
| 278 | { |
| 279 | int diameter = radius * 2; |
| 280 | int kernelSize = diameter + 1; |
| 281 | int border = SkMin32(width, diameter); |
| 282 | int inner_weight = 255 - outer_weight; |
| 283 | outer_weight += outer_weight >> 7; |
| 284 | inner_weight += inner_weight >> 7; |
| 285 | uint32_t outer_scale = (outer_weight << 16) / kernelSize; |
| 286 | uint32_t inner_scale = (inner_weight << 16) / (kernelSize - 2); |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 287 | #ifndef SK_DISABLE_BLUR_ROUNDING |
| 288 | uint32_t half = 1 << 23; |
| 289 | #else |
| 290 | uint32_t half = 0; |
| 291 | #endif |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 292 | int new_width = width + diameter; |
| 293 | int dst_x_stride = transpose ? height : 1; |
| 294 | int dst_y_stride = transpose ? 1 : new_width; |
| 295 | for (int y = 0; y < height; ++y) { |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 296 | uint32_t outer_sum = 0, inner_sum = 0; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 297 | uint8_t* dptr = dst + y * dst_y_stride; |
| 298 | const uint8_t* right = src + y * src_y_stride; |
| 299 | const uint8_t* left = right; |
| 300 | int x = 0; |
| 301 | |
| 302 | #define LEFT_BORDER_ITER \ |
| 303 | inner_sum = outer_sum; \ |
| 304 | outer_sum += *right++; \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 305 | *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 306 | dptr += dst_x_stride; |
| 307 | |
| 308 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 309 | for (;x < border - 16; x += 16) { |
| 310 | LEFT_BORDER_ITER |
| 311 | LEFT_BORDER_ITER |
| 312 | LEFT_BORDER_ITER |
| 313 | LEFT_BORDER_ITER |
| 314 | LEFT_BORDER_ITER |
| 315 | LEFT_BORDER_ITER |
| 316 | LEFT_BORDER_ITER |
| 317 | LEFT_BORDER_ITER |
| 318 | LEFT_BORDER_ITER |
| 319 | LEFT_BORDER_ITER |
| 320 | LEFT_BORDER_ITER |
| 321 | LEFT_BORDER_ITER |
| 322 | LEFT_BORDER_ITER |
| 323 | LEFT_BORDER_ITER |
| 324 | LEFT_BORDER_ITER |
| 325 | LEFT_BORDER_ITER |
| 326 | } |
| 327 | #endif |
| 328 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 329 | for (;x < border; ++x) { |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 330 | LEFT_BORDER_ITER |
| 331 | } |
| 332 | #undef LEFT_BORDER_ITER |
| 333 | for (int x = width; x < diameter; ++x) { |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 334 | *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 335 | dptr += dst_x_stride; |
| 336 | } |
| 337 | x = diameter; |
| 338 | |
| 339 | #define CENTER_ITER \ |
| 340 | inner_sum = outer_sum - *left; \ |
| 341 | outer_sum += *right++; \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 342 | *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 343 | dptr += dst_x_stride; \ |
| 344 | outer_sum -= *left++; |
| 345 | |
| 346 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 347 | for (; x < width - 16; x += 16) { |
| 348 | CENTER_ITER |
| 349 | CENTER_ITER |
| 350 | CENTER_ITER |
| 351 | CENTER_ITER |
| 352 | CENTER_ITER |
| 353 | CENTER_ITER |
| 354 | CENTER_ITER |
| 355 | CENTER_ITER |
| 356 | CENTER_ITER |
| 357 | CENTER_ITER |
| 358 | CENTER_ITER |
| 359 | CENTER_ITER |
| 360 | CENTER_ITER |
| 361 | CENTER_ITER |
| 362 | CENTER_ITER |
| 363 | CENTER_ITER |
| 364 | } |
| 365 | #endif |
| 366 | for (; x < width; ++x) { |
| 367 | CENTER_ITER |
| 368 | } |
| 369 | #undef CENTER_ITER |
| 370 | |
| 371 | #define RIGHT_BORDER_ITER \ |
| 372 | inner_sum = outer_sum - *left++; \ |
senorblanco@chromium.org | 4a525d7 | 2013-02-19 16:09:10 +0000 | [diff] [blame] | 373 | *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \ |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 374 | dptr += dst_x_stride; \ |
| 375 | outer_sum = inner_sum; |
| 376 | |
| 377 | x = 0; |
| 378 | #ifdef UNROLL_SEPARABLE_LOOPS |
| 379 | for (; x < border - 16; x += 16) { |
| 380 | RIGHT_BORDER_ITER |
| 381 | RIGHT_BORDER_ITER |
| 382 | RIGHT_BORDER_ITER |
| 383 | RIGHT_BORDER_ITER |
| 384 | RIGHT_BORDER_ITER |
| 385 | RIGHT_BORDER_ITER |
| 386 | RIGHT_BORDER_ITER |
| 387 | RIGHT_BORDER_ITER |
| 388 | RIGHT_BORDER_ITER |
| 389 | RIGHT_BORDER_ITER |
| 390 | RIGHT_BORDER_ITER |
| 391 | RIGHT_BORDER_ITER |
| 392 | RIGHT_BORDER_ITER |
| 393 | RIGHT_BORDER_ITER |
| 394 | RIGHT_BORDER_ITER |
| 395 | RIGHT_BORDER_ITER |
| 396 | } |
| 397 | #endif |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 398 | for (; x < border; ++x) { |
senorblanco@chromium.org | 9b0d4d7 | 2012-11-27 22:57:41 +0000 | [diff] [blame] | 399 | RIGHT_BORDER_ITER |
| 400 | } |
| 401 | #undef RIGHT_BORDER_ITER |
| 402 | SkASSERT(outer_sum == 0 && inner_sum == 0); |
| 403 | } |
| 404 | return new_width; |
| 405 | } |
| 406 | |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 407 | static void get_adjusted_radii(SkScalar passRadius, int *loRadius, int *hiRadius) |
| 408 | { |
| 409 | *loRadius = *hiRadius = SkScalarCeil(passRadius); |
| 410 | if (SkIntToScalar(*hiRadius) - passRadius > SkFloatToScalar(0.5f)) { |
| 411 | *loRadius = *hiRadius - 1; |
| 412 | } |
| 413 | } |
| 414 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 415 | // Unrolling the integer blur kernel seems to give us a ~15% speedup on Windows, |
| 416 | // breakeven on Mac, and ~15% slowdown on Linux. |
| 417 | // Reading a word at a time when bulding the sum buffer seems to give |
| 418 | // 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] | 419 | #if defined(SK_BUILD_FOR_WIN32) |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 420 | #define UNROLL_KERNEL_LOOP 1 |
| 421 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 423 | /** The sum buffer is an array of u32 to hold the accumulated sum of all of the |
| 424 | src values at their position, plus all values above and to the left. |
| 425 | When we sample into this buffer, we need an initial row and column of 0s, |
| 426 | so we have an index correspondence as follows: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 427 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 428 | src[i, j] == sum[i+1, j+1] |
| 429 | sum[0, j] == sum[i, 0] == 0 |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 430 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 431 | We assume that the sum buffer's stride == its width |
| 432 | */ |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 433 | static void build_sum_buffer(uint32_t sum[], int srcW, int srcH, |
| 434 | const uint8_t src[], int srcRB) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 435 | int sumW = srcW + 1; |
| 436 | |
| 437 | SkASSERT(srcRB >= srcW); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | // mod srcRB so we can apply it after each row |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 439 | srcRB -= srcW; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | |
| 441 | int x, y; |
| 442 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 443 | // zero out the top row and column |
| 444 | memset(sum, 0, sumW * sizeof(sum[0])); |
| 445 | sum += sumW; |
| 446 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | // special case first row |
| 448 | uint32_t X = 0; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 449 | *sum++ = 0; // initialze the first column to 0 |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 450 | for (x = srcW - 1; x >= 0; --x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 451 | X = *src++ + X; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 452 | *sum++ = X; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | } |
| 454 | src += srcRB; |
| 455 | |
| 456 | // now do the rest of the rows |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 457 | for (y = srcH - 1; y > 0; --y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | uint32_t L = 0; |
| 459 | uint32_t C = 0; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 460 | *sum++ = 0; // initialze the first column to 0 |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 461 | |
| 462 | for (x = srcW - 1; !SkIsAlign4((intptr_t) src) && x >= 0; x--) { |
| 463 | uint32_t T = sum[-sumW]; |
| 464 | X = *src++ + L + T - C; |
| 465 | *sum++ = X; |
| 466 | L = X; |
| 467 | C = T; |
| 468 | } |
| 469 | |
| 470 | for (; x >= 4; x-=4) { |
| 471 | uint32_t T = sum[-sumW]; |
| 472 | X = *src++ + L + T - C; |
| 473 | *sum++ = X; |
| 474 | L = X; |
| 475 | C = T; |
| 476 | T = sum[-sumW]; |
| 477 | X = *src++ + L + T - C; |
| 478 | *sum++ = X; |
| 479 | L = X; |
| 480 | C = T; |
| 481 | T = sum[-sumW]; |
| 482 | X = *src++ + L + T - C; |
| 483 | *sum++ = X; |
| 484 | L = X; |
| 485 | C = T; |
| 486 | T = sum[-sumW]; |
| 487 | X = *src++ + L + T - C; |
| 488 | *sum++ = X; |
| 489 | L = X; |
| 490 | C = T; |
| 491 | } |
| 492 | |
| 493 | for (; x >= 0; --x) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 494 | uint32_t T = sum[-sumW]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | X = *src++ + L + T - C; |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 496 | *sum++ = X; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 497 | L = X; |
| 498 | C = T; |
| 499 | } |
| 500 | src += srcRB; |
| 501 | } |
| 502 | } |
| 503 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 504 | /** |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 505 | * This is the path for apply_kernel() to be taken when the kernel |
| 506 | * is wider than the source image. |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 507 | */ |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 508 | static void kernel_clamped(uint8_t dst[], int rx, int ry, const uint32_t sum[], |
| 509 | int sw, int sh) { |
| 510 | SkASSERT(2*rx > sw); |
| 511 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | uint32_t scale = (1 << 24) / ((2*rx + 1)*(2*ry + 1)); |
| 513 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 514 | int sumStride = sw + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 515 | |
| 516 | int dw = sw + 2*rx; |
| 517 | int dh = sh + 2*ry; |
| 518 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 519 | int prev_y = -2*ry; |
| 520 | int next_y = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 522 | for (int y = 0; y < dh; ++y) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 523 | int py = SkClampPos(prev_y) * sumStride; |
| 524 | int ny = SkFastMin32(next_y, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 525 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 526 | int prev_x = -2*rx; |
| 527 | int next_x = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 529 | for (int x = 0; x < dw; ++x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | int px = SkClampPos(prev_x); |
| 531 | int nx = SkFastMin32(next_x, sw); |
| 532 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 533 | // TODO: should we be adding 1/2 (1 << 23) to round to the |
| 534 | // nearest integer here? |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 535 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 536 | *dst++ = SkToU8(tmp * scale >> 24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | |
| 538 | prev_x += 1; |
| 539 | next_x += 1; |
| 540 | } |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 541 | |
| 542 | prev_y += 1; |
| 543 | next_y += 1; |
| 544 | } |
| 545 | } |
| 546 | /** |
| 547 | * sw and sh are the width and height of the src. Since the sum buffer |
| 548 | * matches that, but has an extra row and col at the beginning (with zeros), |
| 549 | * we can just use sw and sh as our "max" values for pinning coordinates |
| 550 | * when sampling into sum[][] |
| 551 | * |
| 552 | * The inner loop is conceptually simple; we break it into several sections |
| 553 | * to improve performance. Here's the original version: |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 554 | for (int x = 0; x < dw; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 555 | int px = SkClampPos(prev_x); |
| 556 | int nx = SkFastMin32(next_x, sw); |
| 557 | |
| 558 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 559 | *dst++ = SkToU8(tmp * scale >> 24); |
| 560 | |
| 561 | prev_x += 1; |
| 562 | next_x += 1; |
| 563 | } |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 564 | * The sections are: |
| 565 | * left-hand section, where prev_x is clamped to 0 |
| 566 | * center section, where neither prev_x nor next_x is clamped |
| 567 | * right-hand section, where next_x is clamped to sw |
| 568 | * On some operating systems, the center section is unrolled for additional |
| 569 | * speedup. |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 570 | */ |
| 571 | static void apply_kernel(uint8_t dst[], int rx, int ry, const uint32_t sum[], |
| 572 | int sw, int sh) { |
| 573 | if (2*rx > sw) { |
| 574 | kernel_clamped(dst, rx, ry, sum, sw, sh); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | uint32_t scale = (1 << 24) / ((2*rx + 1)*(2*ry + 1)); |
| 579 | |
| 580 | int sumStride = sw + 1; |
| 581 | |
| 582 | int dw = sw + 2*rx; |
| 583 | int dh = sh + 2*ry; |
| 584 | |
| 585 | int prev_y = -2*ry; |
| 586 | int next_y = 1; |
| 587 | |
| 588 | SkASSERT(2*rx <= dw - 2*rx); |
| 589 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 590 | for (int y = 0; y < dh; ++y) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 591 | int py = SkClampPos(prev_y) * sumStride; |
| 592 | int ny = SkFastMin32(next_y, sh) * sumStride; |
| 593 | |
| 594 | int prev_x = -2*rx; |
| 595 | int next_x = 1; |
| 596 | int x = 0; |
| 597 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 598 | for (; x < 2*rx; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 599 | SkASSERT(prev_x <= 0); |
| 600 | SkASSERT(next_x <= sw); |
| 601 | |
| 602 | int px = 0; |
| 603 | int nx = next_x; |
| 604 | |
| 605 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 606 | *dst++ = SkToU8(tmp * scale >> 24); |
| 607 | |
| 608 | prev_x += 1; |
| 609 | next_x += 1; |
| 610 | } |
| 611 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 612 | int i0 = prev_x + py; |
| 613 | int i1 = next_x + ny; |
| 614 | int i2 = next_x + py; |
| 615 | int i3 = prev_x + ny; |
| 616 | |
| 617 | #if UNROLL_KERNEL_LOOP |
| 618 | for (; x < dw - 2*rx - 4; x += 4) { |
| 619 | SkASSERT(prev_x >= 0); |
| 620 | SkASSERT(next_x <= sw); |
| 621 | |
| 622 | uint32_t tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 623 | *dst++ = SkToU8(tmp * scale >> 24); |
| 624 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 625 | *dst++ = SkToU8(tmp * scale >> 24); |
| 626 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 627 | *dst++ = SkToU8(tmp * scale >> 24); |
| 628 | tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 629 | *dst++ = SkToU8(tmp * scale >> 24); |
| 630 | |
| 631 | prev_x += 4; |
| 632 | next_x += 4; |
| 633 | } |
| 634 | #endif |
| 635 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 636 | for (; x < dw - 2*rx; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 637 | SkASSERT(prev_x >= 0); |
| 638 | SkASSERT(next_x <= sw); |
| 639 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 640 | uint32_t tmp = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 641 | *dst++ = SkToU8(tmp * scale >> 24); |
| 642 | |
| 643 | prev_x += 1; |
| 644 | next_x += 1; |
| 645 | } |
| 646 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 647 | for (; x < dw; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 648 | SkASSERT(prev_x >= 0); |
| 649 | SkASSERT(next_x > sw); |
| 650 | |
| 651 | int px = prev_x; |
| 652 | int nx = sw; |
| 653 | |
| 654 | uint32_t tmp = sum[px+py] + sum[nx+ny] - sum[nx+py] - sum[px+ny]; |
| 655 | *dst++ = SkToU8(tmp * scale >> 24); |
| 656 | |
| 657 | prev_x += 1; |
| 658 | next_x += 1; |
| 659 | } |
| 660 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 661 | prev_y += 1; |
| 662 | next_y += 1; |
| 663 | } |
| 664 | } |
| 665 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 666 | /** |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 667 | * This is the path for apply_kernel_interp() to be taken when the kernel |
| 668 | * is wider than the source image. |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 669 | */ |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 670 | static void kernel_interp_clamped(uint8_t dst[], int rx, int ry, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 671 | const uint32_t sum[], int sw, int sh, U8CPU outerWeight) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 672 | SkASSERT(2*rx > sw); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 673 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 674 | int innerWeight = 255 - outerWeight; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 675 | |
| 676 | // round these guys up if they're bigger than 127 |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 677 | outerWeight += outerWeight >> 7; |
| 678 | innerWeight += innerWeight >> 7; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 679 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 680 | uint32_t outerScale = (outerWeight << 16) / ((2*rx + 1)*(2*ry + 1)); |
| 681 | uint32_t innerScale = (innerWeight << 16) / ((2*rx - 1)*(2*ry - 1)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 683 | int sumStride = sw + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 684 | |
| 685 | int dw = sw + 2*rx; |
| 686 | int dh = sh + 2*ry; |
| 687 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 688 | int prev_y = -2*ry; |
| 689 | int next_y = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 691 | for (int y = 0; y < dh; ++y) { |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 692 | int py = SkClampPos(prev_y) * sumStride; |
| 693 | int ny = SkFastMin32(next_y, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 694 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 695 | int ipy = SkClampPos(prev_y + 1) * sumStride; |
| 696 | int iny = SkClampMax(next_y - 1, sh) * sumStride; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 697 | |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 698 | int prev_x = -2*rx; |
| 699 | int next_x = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 700 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 701 | for (int x = 0; x < dw; ++x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 702 | int px = SkClampPos(prev_x); |
| 703 | int nx = SkFastMin32(next_x, sw); |
| 704 | |
| 705 | int ipx = SkClampPos(prev_x + 1); |
| 706 | int inx = SkClampMax(next_x - 1, sw); |
| 707 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 708 | uint32_t outerSum = sum[px+py] + sum[nx+ny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 709 | - sum[nx+py] - sum[px+ny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 710 | uint32_t innerSum = sum[ipx+ipy] + sum[inx+iny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 711 | - sum[inx+ipy] - sum[ipx+iny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 712 | *dst++ = SkToU8((outerSum * outerScale |
| 713 | + innerSum * innerScale) >> 24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 714 | |
| 715 | prev_x += 1; |
| 716 | next_x += 1; |
| 717 | } |
| 718 | prev_y += 1; |
| 719 | next_y += 1; |
| 720 | } |
| 721 | } |
| 722 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 723 | /** |
| 724 | * sw and sh are the width and height of the src. Since the sum buffer |
| 725 | * matches that, but has an extra row and col at the beginning (with zeros), |
| 726 | * we can just use sw and sh as our "max" values for pinning coordinates |
| 727 | * when sampling into sum[][] |
| 728 | * |
| 729 | * The inner loop is conceptually simple; we break it into several variants |
| 730 | * to improve performance. Here's the original version: |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 731 | for (int x = 0; x < dw; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 732 | int px = SkClampPos(prev_x); |
| 733 | int nx = SkFastMin32(next_x, sw); |
| 734 | |
| 735 | int ipx = SkClampPos(prev_x + 1); |
| 736 | int inx = SkClampMax(next_x - 1, sw); |
| 737 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 738 | uint32_t outerSum = sum[px+py] + sum[nx+ny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 739 | - sum[nx+py] - sum[px+ny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 740 | uint32_t innerSum = sum[ipx+ipy] + sum[inx+iny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 741 | - sum[inx+ipy] - sum[ipx+iny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 742 | *dst++ = SkToU8((outerSum * outerScale |
| 743 | + innerSum * innerScale) >> 24); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 744 | |
| 745 | prev_x += 1; |
| 746 | next_x += 1; |
| 747 | } |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 748 | * The sections are: |
| 749 | * left-hand section, where prev_x is clamped to 0 |
| 750 | * center section, where neither prev_x nor next_x is clamped |
| 751 | * right-hand section, where next_x is clamped to sw |
| 752 | * On some operating systems, the center section is unrolled for additional |
| 753 | * speedup. |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 754 | */ |
| 755 | static void apply_kernel_interp(uint8_t dst[], int rx, int ry, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 756 | const uint32_t sum[], int sw, int sh, U8CPU outerWeight) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 757 | SkASSERT(rx > 0 && ry > 0); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 758 | SkASSERT(outerWeight <= 255); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 759 | |
| 760 | if (2*rx > sw) { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 761 | kernel_interp_clamped(dst, rx, ry, sum, sw, sh, outerWeight); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 765 | int innerWeight = 255 - outerWeight; |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 766 | |
| 767 | // round these guys up if they're bigger than 127 |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 768 | outerWeight += outerWeight >> 7; |
| 769 | innerWeight += innerWeight >> 7; |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 770 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 771 | uint32_t outerScale = (outerWeight << 16) / ((2*rx + 1)*(2*ry + 1)); |
| 772 | uint32_t innerScale = (innerWeight << 16) / ((2*rx - 1)*(2*ry - 1)); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 773 | |
| 774 | int sumStride = sw + 1; |
| 775 | |
| 776 | int dw = sw + 2*rx; |
| 777 | int dh = sh + 2*ry; |
| 778 | |
| 779 | int prev_y = -2*ry; |
| 780 | int next_y = 1; |
| 781 | |
| 782 | SkASSERT(2*rx <= dw - 2*rx); |
| 783 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 784 | for (int y = 0; y < dh; ++y) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 785 | int py = SkClampPos(prev_y) * sumStride; |
| 786 | int ny = SkFastMin32(next_y, sh) * sumStride; |
| 787 | |
| 788 | int ipy = SkClampPos(prev_y + 1) * sumStride; |
| 789 | int iny = SkClampMax(next_y - 1, sh) * sumStride; |
| 790 | |
| 791 | int prev_x = -2*rx; |
| 792 | int next_x = 1; |
| 793 | int x = 0; |
| 794 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 795 | for (; x < 2*rx; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 796 | SkASSERT(prev_x < 0); |
| 797 | SkASSERT(next_x <= sw); |
| 798 | |
| 799 | int px = 0; |
| 800 | int nx = next_x; |
| 801 | |
| 802 | int ipx = 0; |
| 803 | int inx = next_x - 1; |
| 804 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 805 | uint32_t outerSum = sum[px+py] + sum[nx+ny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 806 | - sum[nx+py] - sum[px+ny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 807 | uint32_t innerSum = sum[ipx+ipy] + sum[inx+iny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 808 | - sum[inx+ipy] - sum[ipx+iny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 809 | *dst++ = SkToU8((outerSum * outerScale |
| 810 | + innerSum * innerScale) >> 24); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 811 | |
| 812 | prev_x += 1; |
| 813 | next_x += 1; |
| 814 | } |
| 815 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 816 | int i0 = prev_x + py; |
| 817 | int i1 = next_x + ny; |
| 818 | int i2 = next_x + py; |
| 819 | int i3 = prev_x + ny; |
| 820 | int i4 = prev_x + 1 + ipy; |
| 821 | int i5 = next_x - 1 + iny; |
| 822 | int i6 = next_x - 1 + ipy; |
| 823 | int i7 = prev_x + 1 + iny; |
| 824 | |
| 825 | #if UNROLL_KERNEL_LOOP |
| 826 | for (; x < dw - 2*rx - 4; x += 4) { |
| 827 | SkASSERT(prev_x >= 0); |
| 828 | SkASSERT(next_x <= sw); |
| 829 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 830 | uint32_t outerSum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 831 | uint32_t innerSum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 832 | *dst++ = SkToU8((outerSum * outerScale |
| 833 | + innerSum * innerScale) >> 24); |
| 834 | outerSum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 835 | innerSum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 836 | *dst++ = SkToU8((outerSum * outerScale |
| 837 | + innerSum * innerScale) >> 24); |
| 838 | outerSum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 839 | innerSum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 840 | *dst++ = SkToU8((outerSum * outerScale |
| 841 | + innerSum * innerScale) >> 24); |
| 842 | outerSum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 843 | innerSum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 844 | *dst++ = SkToU8((outerSum * outerScale |
| 845 | + innerSum * innerScale) >> 24); |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 846 | |
| 847 | prev_x += 4; |
| 848 | next_x += 4; |
| 849 | } |
| 850 | #endif |
| 851 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 852 | for (; x < dw - 2*rx; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 853 | SkASSERT(prev_x >= 0); |
| 854 | SkASSERT(next_x <= sw); |
| 855 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 856 | uint32_t outerSum = sum[i0++] + sum[i1++] - sum[i2++] - sum[i3++]; |
| 857 | uint32_t innerSum = sum[i4++] + sum[i5++] - sum[i6++] - sum[i7++]; |
| 858 | *dst++ = SkToU8((outerSum * outerScale |
| 859 | + innerSum * innerScale) >> 24); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 860 | |
| 861 | prev_x += 1; |
| 862 | next_x += 1; |
| 863 | } |
| 864 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 865 | for (; x < dw; ++x) { |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 866 | SkASSERT(prev_x >= 0); |
| 867 | SkASSERT(next_x > sw); |
| 868 | |
| 869 | int px = prev_x; |
| 870 | int nx = sw; |
| 871 | |
| 872 | int ipx = prev_x + 1; |
| 873 | int inx = sw; |
| 874 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 875 | uint32_t outerSum = sum[px+py] + sum[nx+ny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 876 | - sum[nx+py] - sum[px+ny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 877 | uint32_t innerSum = sum[ipx+ipy] + sum[inx+iny] |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 878 | - sum[inx+ipy] - sum[ipx+iny]; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 879 | *dst++ = SkToU8((outerSum * outerScale |
| 880 | + innerSum * innerScale) >> 24); |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 881 | |
| 882 | prev_x += 1; |
| 883 | next_x += 1; |
| 884 | } |
| 885 | |
| 886 | prev_y += 1; |
| 887 | next_y += 1; |
| 888 | } |
| 889 | } |
| 890 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 891 | #include "SkColorPriv.h" |
| 892 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 893 | static void merge_src_with_blur(uint8_t dst[], int dstRB, |
| 894 | const uint8_t src[], int srcRB, |
| 895 | const uint8_t blur[], int blurRB, |
| 896 | int sw, int sh) { |
| 897 | dstRB -= sw; |
| 898 | srcRB -= sw; |
| 899 | blurRB -= sw; |
| 900 | while (--sh >= 0) { |
| 901 | for (int x = sw - 1; x >= 0; --x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 902 | *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src))); |
| 903 | dst += 1; |
| 904 | src += 1; |
| 905 | blur += 1; |
| 906 | } |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 907 | dst += dstRB; |
| 908 | src += srcRB; |
| 909 | blur += blurRB; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | static void clamp_with_orig(uint8_t dst[], int dstRowBytes, |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 914 | const uint8_t src[], int srcRowBytes, |
| 915 | int sw, int sh, |
reed@android.com | 4560767 | 2009-09-21 00:27:08 +0000 | [diff] [blame] | 916 | SkBlurMask::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 917 | int x; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 918 | while (--sh >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 919 | switch (style) { |
| 920 | case SkBlurMask::kSolid_Style: |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 921 | for (x = sw - 1; x >= 0; --x) { |
| 922 | int s = *src; |
| 923 | int d = *dst; |
| 924 | *dst = SkToU8(s + d - SkMulDiv255Round(s, d)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 925 | dst += 1; |
| 926 | src += 1; |
| 927 | } |
| 928 | break; |
| 929 | case SkBlurMask::kOuter_Style: |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 930 | for (x = sw - 1; x >= 0; --x) { |
| 931 | if (*src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 932 | *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src))); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 933 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 934 | dst += 1; |
| 935 | src += 1; |
| 936 | } |
| 937 | break; |
| 938 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 939 | SkDEBUGFAIL("Unexpected blur style here"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | dst += dstRowBytes - sw; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 943 | src += srcRowBytes - sw; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 947 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 949 | // we use a local function to wrap the class static method to work around |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 950 | // a bug in gcc98 |
| 951 | void SkMask_FreeImage(uint8_t* image); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 952 | void SkMask_FreeImage(uint8_t* image) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 953 | SkMask::FreeImage(image); |
| 954 | } |
| 955 | |
| 956 | bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 957 | SkScalar radius, Style style, Quality quality, |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 958 | SkIPoint* margin, bool separable) |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 959 | { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 960 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 961 | if (src.fFormat != SkMask::kA8_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 962 | return false; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 963 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 964 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 965 | // Force high quality off for small radii (performance) |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 966 | if (radius < SkIntToScalar(3)) { |
| 967 | quality = kLow_Quality; |
| 968 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 969 | |
| 970 | // highQuality: use three box blur passes as a cheap way |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 971 | // to approximate a Gaussian blur |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 972 | int passCount = (kHigh_Quality == quality) ? 3 : 1; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 973 | SkScalar passRadius = (kHigh_Quality == quality) ? |
| 974 | SkScalarMul( radius, kBlurRadiusFudgeFactor): |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 975 | radius; |
| 976 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 977 | int rx = SkScalarCeil(passRadius); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 978 | int outerWeight = 255 - SkScalarRound((SkIntToScalar(rx) - passRadius) * 255); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 979 | |
| 980 | SkASSERT(rx >= 0); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 981 | SkASSERT((unsigned)outerWeight <= 255); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 982 | if (rx <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 983 | return false; |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 984 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 985 | |
| 986 | int ry = rx; // only do square blur for now |
| 987 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 988 | int padx = passCount * rx; |
| 989 | int pady = passCount * ry; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 990 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 991 | if (margin) { |
| 992 | margin->set(padx, pady); |
| 993 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 994 | dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady, |
| 995 | src.fBounds.fRight + padx, src.fBounds.fBottom + pady); |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 996 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 997 | dst->fRowBytes = dst->fBounds.width(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 998 | dst->fFormat = SkMask::kA8_Format; |
| 999 | dst->fImage = NULL; |
| 1000 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1001 | if (src.fImage) { |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1002 | size_t dstSize = dst->computeImageSize(); |
| 1003 | if (0 == dstSize) { |
| 1004 | return false; // too big to allocate, abort |
| 1005 | } |
| 1006 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1007 | int sw = src.fBounds.width(); |
| 1008 | int sh = src.fBounds.height(); |
| 1009 | const uint8_t* sp = src.fImage; |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1010 | uint8_t* dp = SkMask::AllocImage(dstSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1011 | SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); |
| 1012 | |
| 1013 | // build the blurry destination |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 1014 | if (separable) { |
| 1015 | SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); |
| 1016 | uint8_t* tp = tmpBuffer.get(); |
| 1017 | int w = sw, h = sh; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1018 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1019 | if (outerWeight == 255) { |
senorblanco@chromium.org | c438130 | 2012-11-16 17:22:33 +0000 | [diff] [blame] | 1020 | int loRadius, hiRadius; |
| 1021 | get_adjusted_radii(passRadius, &loRadius, &hiRadius); |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1022 | if (kHigh_Quality == quality) { |
| 1023 | // Do three X blurs, with a transpose on the final one. |
| 1024 | w = boxBlur(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h, false); |
| 1025 | w = boxBlur(tp, w, dp, hiRadius, loRadius, w, h, false); |
| 1026 | w = boxBlur(dp, w, tp, hiRadius, hiRadius, w, h, true); |
| 1027 | // Do three Y blurs, with a transpose on the final one. |
| 1028 | h = boxBlur(tp, h, dp, loRadius, hiRadius, h, w, false); |
| 1029 | h = boxBlur(dp, h, tp, hiRadius, loRadius, h, w, false); |
| 1030 | h = boxBlur(tp, h, dp, hiRadius, hiRadius, h, w, true); |
| 1031 | } else { |
| 1032 | w = boxBlur(sp, src.fRowBytes, tp, rx, rx, w, h, true); |
| 1033 | h = boxBlur(tp, h, dp, ry, ry, h, w, true); |
| 1034 | } |
senorblanco@chromium.org | 908276b | 2012-11-15 20:27:35 +0000 | [diff] [blame] | 1035 | } else { |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1036 | if (kHigh_Quality == quality) { |
| 1037 | // Do three X blurs, with a transpose on the final one. |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1038 | w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, false, outerWeight); |
| 1039 | w = boxBlurInterp(tp, w, dp, rx, w, h, false, outerWeight); |
| 1040 | w = boxBlurInterp(dp, w, tp, rx, w, h, true, outerWeight); |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1041 | // Do three Y blurs, with a transpose on the final one. |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1042 | h = boxBlurInterp(tp, h, dp, ry, h, w, false, outerWeight); |
| 1043 | h = boxBlurInterp(dp, h, tp, ry, h, w, false, outerWeight); |
| 1044 | h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1045 | } else { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1046 | w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, true, outerWeight); |
| 1047 | h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight); |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1048 | } |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } else { |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1051 | const size_t storageW = sw + 2 * (passCount - 1) * rx + 1; |
| 1052 | const size_t storageH = sh + 2 * (passCount - 1) * ry + 1; |
| 1053 | SkAutoTMalloc<uint32_t> storage(storageW * storageH); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1054 | uint32_t* sumBuffer = storage.get(); |
| 1055 | |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1056 | //pass1: sp is source, dp is destination |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1057 | build_sum_buffer(sumBuffer, sw, sh, sp, src.fRowBytes); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1058 | if (outerWeight == 255) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1059 | apply_kernel(dp, rx, ry, sumBuffer, sw, sh); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1060 | } else { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1061 | apply_kernel_interp(dp, rx, ry, sumBuffer, sw, sh, outerWeight); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1062 | } |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1063 | |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 1064 | if (kHigh_Quality == quality) { |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1065 | //pass2: dp is source, tmpBuffer is destination |
| 1066 | int tmp_sw = sw + 2 * rx; |
| 1067 | int tmp_sh = sh + 2 * ry; |
| 1068 | SkAutoTMalloc<uint8_t> tmpBuffer(dstSize); |
| 1069 | build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, dp, tmp_sw); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1070 | if (outerWeight == 255) |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1071 | apply_kernel(tmpBuffer.get(), rx, ry, sumBuffer, tmp_sw, tmp_sh); |
| 1072 | else |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1073 | apply_kernel_interp(tmpBuffer.get(), rx, ry, sumBuffer, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1074 | tmp_sw, tmp_sh, outerWeight); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1075 | |
| 1076 | //pass3: tmpBuffer is source, dp is destination |
| 1077 | tmp_sw += 2 * rx; |
| 1078 | tmp_sh += 2 * ry; |
| 1079 | build_sum_buffer(sumBuffer, tmp_sw, tmp_sh, tmpBuffer.get(), tmp_sw); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1080 | if (outerWeight == 255) |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1081 | apply_kernel(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh); |
| 1082 | else |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1083 | apply_kernel_interp(dp, rx, ry, sumBuffer, tmp_sw, tmp_sh, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1084 | outerWeight); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 1085 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | dst->fImage = dp; |
| 1089 | // if need be, alloc the "real" dst (same size as src) and copy/merge |
| 1090 | // the blur into it (applying the src) |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1091 | if (style == kInner_Style) { |
| 1092 | // now we allocate the "real" dst, mirror the size of src |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1093 | size_t srcSize = src.computeImageSize(); |
| 1094 | if (0 == srcSize) { |
| 1095 | return false; // too big to allocate, abort |
| 1096 | } |
| 1097 | dst->fImage = SkMask::AllocImage(srcSize); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1098 | merge_src_with_blur(dst->fImage, src.fRowBytes, |
| 1099 | sp, src.fRowBytes, |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1100 | dp + passCount * (rx + ry * dst->fRowBytes), |
| 1101 | dst->fRowBytes, sw, sh); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1102 | SkMask::FreeImage(dp); |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1103 | } else if (style != kNormal_Style) { |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 1104 | clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes), |
| 1105 | dst->fRowBytes, sp, src.fRowBytes, sw, sh, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1106 | } |
| 1107 | (void)autoCall.detach(); |
| 1108 | } |
| 1109 | |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1110 | if (style == kInner_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1111 | dst->fBounds = src.fBounds; // restore trimmed bounds |
reed@android.com | 0e3c664 | 2009-09-18 13:41:56 +0000 | [diff] [blame] | 1112 | dst->fRowBytes = src.fRowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | |
senorblanco@chromium.org | 71f0f34 | 2012-11-13 20:35:21 +0000 | [diff] [blame] | 1118 | bool SkBlurMask::BlurSeparable(SkMask* dst, const SkMask& src, |
| 1119 | SkScalar radius, Style style, Quality quality, |
| 1120 | SkIPoint* margin) |
| 1121 | { |
| 1122 | return SkBlurMask::Blur(dst, src, radius, style, quality, margin, true); |
| 1123 | } |
| 1124 | |
| 1125 | bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, |
| 1126 | SkScalar radius, Style style, Quality quality, |
| 1127 | SkIPoint* margin) |
| 1128 | { |
| 1129 | return SkBlurMask::Blur(dst, src, radius, style, quality, margin, false); |
| 1130 | } |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1131 | |
| 1132 | /* Convolving a box with itself three times results in a piecewise |
| 1133 | quadratic function: |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1134 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1135 | 0 x <= -1.5 |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1136 | 9/8 + 3/2 x + 1/2 x^2 -1.5 < x <= -.5 |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1137 | 3/4 - x^2 -.5 < x <= .5 |
| 1138 | 9/8 - 3/2 x + 1/2 x^2 0.5 < x <= 1.5 |
| 1139 | 0 1.5 < x |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1140 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1141 | Mathematica: |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1142 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1143 | g[x_] := Piecewise [ { |
| 1144 | {9/8 + 3/2 x + 1/2 x^2 , -1.5 < x <= -.5}, |
| 1145 | {3/4 - x^2 , -.5 < x <= .5}, |
| 1146 | {9/8 - 3/2 x + 1/2 x^2 , 0.5 < x <= 1.5} |
| 1147 | }, 0] |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1148 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1149 | To get the profile curve of the blurred step function at the rectangle |
| 1150 | edge, we evaluate the indefinite integral, which is piecewise cubic: |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1151 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1152 | 0 x <= -1.5 |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1153 | 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3 -1.5 < x <= -0.5 |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1154 | 1/2 + 3/4 x - 1/3 x^3 -.5 < x <= .5 |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1155 | 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3 .5 < x <= 1.5 |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1156 | 1 1.5 < x |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1157 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1158 | in Mathematica code: |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1159 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1160 | gi[x_] := Piecewise[ { |
| 1161 | { 0 , x <= -1.5 }, |
| 1162 | { 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3, -1.5 < x <= -0.5 }, |
| 1163 | { 1/2 + 3/4 x - 1/3 x^3 , -.5 < x <= .5}, |
| 1164 | { 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3, .5 < x <= 1.5} |
| 1165 | },1] |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1166 | */ |
| 1167 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1168 | static float gaussianIntegral(float x) { |
| 1169 | if (x > 1.5f) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1170 | return 0.0f; |
| 1171 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1172 | if (x < -1.5f) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1173 | return 1.0f; |
| 1174 | } |
| 1175 | |
| 1176 | float x2 = x*x; |
| 1177 | float x3 = x2*x; |
| 1178 | |
jvanverth@google.com | 9c4e5ac | 2013-01-07 18:41:28 +0000 | [diff] [blame] | 1179 | if ( x > 0.5f ) { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1180 | return 0.5625f - (x3 / 6.0f - 3.0f * x2 * 0.25f + 1.125f * x); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1181 | } |
jvanverth@google.com | 9c4e5ac | 2013-01-07 18:41:28 +0000 | [diff] [blame] | 1182 | if ( x > -0.5f ) { |
| 1183 | return 0.5f - (0.75f * x - x3 / 3.0f); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1184 | } |
jvanverth@google.com | 9c4e5ac | 2013-01-07 18:41:28 +0000 | [diff] [blame] | 1185 | return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1188 | // Compute the size of the array allocated for the profile. |
| 1189 | |
| 1190 | static int compute_profile_size(SkScalar radius) { |
| 1191 | return SkScalarRoundToInt(radius * 3); |
| 1192 | |
| 1193 | } |
| 1194 | |
| 1195 | /* compute_profile allocates and fills in an array of floating |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1196 | point values between 0 and 255 for the profile signature of |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1197 | a blurred half-plane with the given blur radius. Since we're |
| 1198 | going to be doing screened multiplications (i.e., 1 - (1-x)(1-y)) |
| 1199 | all the time, we actually fill in the profile pre-inverted |
| 1200 | (already done 255-x). |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1201 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1202 | It's the responsibility of the caller to delete the |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1203 | memory returned in profile_out. |
| 1204 | */ |
| 1205 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1206 | static void compute_profile(SkScalar radius, unsigned int **profile_out) { |
| 1207 | int size = compute_profile_size(radius); |
| 1208 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1209 | int center = size >> 1; |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 1210 | unsigned int *profile = SkNEW_ARRAY(unsigned int, size); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1211 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1212 | float invr = 1.f/radius; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1213 | |
| 1214 | profile[0] = 255; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1215 | for (int x = 1 ; x < size ; ++x) { |
jvanverth@google.com | d98df1a | 2013-02-20 19:02:34 +0000 | [diff] [blame] | 1216 | float scaled_x = (center - x - .5f) * invr; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1217 | float gi = gaussianIntegral(scaled_x); |
| 1218 | profile[x] = 255 - (uint8_t) (255.f * gi); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | *profile_out = profile; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1224 | // TODO MAYBE: Maintain a profile cache to avoid recomputing this for |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1225 | // commonly used radii. Consider baking some of the most common blur radii |
| 1226 | // directly in as static data? |
| 1227 | |
| 1228 | // Implementation adapted from Michael Herf's approach: |
| 1229 | // http://stereopsis.com/shadowrect/ |
| 1230 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1231 | static inline unsigned int profile_lookup( unsigned int *profile, int loc, int blurred_width, int sharp_width ) { |
| 1232 | int dx = SkAbs32(((loc << 1) + 1) - blurred_width) - sharp_width; // how far are we from the original edge? |
| 1233 | int ox = dx >> 1; |
| 1234 | if (ox < 0) { |
| 1235 | ox = 0; |
| 1236 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1237 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1238 | return profile[ox]; |
| 1239 | } |
| 1240 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1241 | bool SkBlurMask::BlurRect(SkMask *dst, const SkRect &src, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1242 | SkScalar provided_radius, Style style, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1243 | SkIPoint *margin, SkMask::CreateMode createMode) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1244 | int profile_size; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1245 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1246 | float radius = SkScalarToFloat(SkScalarMul(provided_radius, kBlurRadiusFudgeFactor)); |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1247 | |
humper@google.com | 1e1a24e | 2013-02-20 18:35:40 +0000 | [diff] [blame] | 1248 | // adjust blur radius to match interpretation from boxfilter code |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1249 | radius = (radius + .5f) * 2.f; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1250 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1251 | profile_size = compute_profile_size(radius); |
| 1252 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1253 | int pad = profile_size/2; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1254 | if (margin) { |
| 1255 | margin->set( pad, pad ); |
| 1256 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1257 | |
humper@google.com | 68a690c | 2013-03-11 21:16:20 +0000 | [diff] [blame^] | 1258 | dst->fBounds.set(SkScalarRoundToInt(src.fLeft - pad), |
| 1259 | SkScalarRoundToInt(src.fTop - pad), |
| 1260 | SkScalarRoundToInt(src.fRight + pad), |
| 1261 | SkScalarRoundToInt(src.fBottom + pad)); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1262 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1263 | dst->fRowBytes = dst->fBounds.width(); |
| 1264 | dst->fFormat = SkMask::kA8_Format; |
| 1265 | dst->fImage = NULL; |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1266 | |
| 1267 | int sw = SkScalarFloorToInt(src.width()); |
| 1268 | int sh = SkScalarFloorToInt(src.height()); |
| 1269 | |
| 1270 | if (createMode == SkMask::kJustComputeBounds_CreateMode) { |
| 1271 | if (style == kInner_Style) { |
humper@google.com | 68a690c | 2013-03-11 21:16:20 +0000 | [diff] [blame^] | 1272 | dst->fBounds.set(SkScalarRoundToInt(src.fLeft), |
| 1273 | SkScalarRoundToInt(src.fTop), |
| 1274 | SkScalarRoundToInt(src.fRight), |
| 1275 | SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 1276 | dst->fRowBytes = sw; |
| 1277 | } |
| 1278 | return true; |
| 1279 | } |
| 1280 | unsigned int *profile = NULL; |
| 1281 | |
| 1282 | compute_profile(radius, &profile); |
| 1283 | SkAutoTDeleteArray<unsigned int> ada(profile); |
| 1284 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1285 | size_t dstSize = dst->computeImageSize(); |
| 1286 | if (0 == dstSize) { |
| 1287 | return false; // too big to allocate, abort |
| 1288 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1289 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1290 | uint8_t* dp = SkMask::AllocImage(dstSize); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1291 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1292 | dst->fImage = dp; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1293 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1294 | int dstHeight = dst->fBounds.height(); |
| 1295 | int dstWidth = dst->fBounds.width(); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1296 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1297 | // nearest odd number less than the profile size represents the center |
| 1298 | // of the (2x scaled) profile |
| 1299 | int center = ( profile_size & ~1 ) - 1; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1300 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1301 | int w = sw - center; |
| 1302 | int h = sh - center; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1303 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1304 | uint8_t *outptr = dp; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1305 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1306 | SkAutoTMalloc<uint8_t> horizontalScanline(dstWidth); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1307 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1308 | for (int x = 0 ; x < dstWidth ; ++x) { |
| 1309 | if (profile_size <= sw) { |
| 1310 | horizontalScanline[x] = profile_lookup(profile, x, dstWidth, w); |
| 1311 | } else { |
| 1312 | float span = float(sw)/radius; |
jvanverth@google.com | d98df1a | 2013-02-20 19:02:34 +0000 | [diff] [blame] | 1313 | float giX = 1.5f - (x+.5f)/radius; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1314 | horizontalScanline[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span))); |
| 1315 | } |
| 1316 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1317 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1318 | for (int y = 0 ; y < dstHeight ; ++y) { |
| 1319 | unsigned int profile_y; |
| 1320 | if (profile_size <= sh) { |
| 1321 | profile_y = profile_lookup(profile, y, dstHeight, h); |
| 1322 | } else { |
| 1323 | float span = float(sh)/radius; |
jvanverth@google.com | d98df1a | 2013-02-20 19:02:34 +0000 | [diff] [blame] | 1324 | float giY = 1.5f - (y+.5f)/radius; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1325 | profile_y = (uint8_t) (255 * (gaussianIntegral(giY) - gaussianIntegral(giY + span))); |
| 1326 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1327 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1328 | for (int x = 0 ; x < dstWidth ; x++) { |
| 1329 | unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], profile_y); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1330 | *(outptr++) = maskval; |
| 1331 | } |
| 1332 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1333 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1334 | if (style == kInner_Style) { |
| 1335 | // now we allocate the "real" dst, mirror the size of src |
jvanverth@google.com | d98df1a | 2013-02-20 19:02:34 +0000 | [diff] [blame] | 1336 | size_t srcSize = (size_t)(src.width() * src.height()); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1337 | if (0 == srcSize) { |
| 1338 | return false; // too big to allocate, abort |
| 1339 | } |
| 1340 | dst->fImage = SkMask::AllocImage(srcSize); |
| 1341 | for (int y = 0 ; y < sh ; y++) { |
| 1342 | uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad; |
| 1343 | uint8_t *inner_scanline = dst->fImage + y*sw; |
| 1344 | memcpy(inner_scanline, blur_scanline, sw); |
| 1345 | } |
| 1346 | SkMask::FreeImage(dp); |
| 1347 | |
humper@google.com | 68a690c | 2013-03-11 21:16:20 +0000 | [diff] [blame^] | 1348 | dst->fBounds.set(SkScalarRoundToInt(src.fLeft), |
| 1349 | SkScalarRoundToInt(src.fTop), |
| 1350 | SkScalarRoundToInt(src.fRight), |
| 1351 | SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1352 | dst->fRowBytes = sw; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1353 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1354 | } else if (style == kOuter_Style) { |
| 1355 | for (int y = pad ; y < dstHeight-pad ; y++) { |
| 1356 | uint8_t *dst_scanline = dp + y*dstWidth + pad; |
| 1357 | memset(dst_scanline, 0, sw); |
| 1358 | } |
| 1359 | } |
| 1360 | // normal and solid styles are the same for analytic rect blurs, so don't |
| 1361 | // need to handle solid specially. |
| 1362 | |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | // The "simple" blur is a direct implementation of separable convolution with a discrete |
| 1367 | // gaussian kernel. It's "ground truth" in a sense; too slow to be used, but very |
| 1368 | // useful for correctness comparisons. |
| 1369 | |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1370 | bool SkBlurMask::BlurGroundTruth(SkMask* dst, const SkMask& src, SkScalar provided_radius, |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1371 | Style style, SkIPoint* margin) { |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1372 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1373 | if (src.fFormat != SkMask::kA8_Format) { |
| 1374 | return false; |
| 1375 | } |
| 1376 | |
| 1377 | float radius = SkScalarToFloat(SkScalarMul(provided_radius, kBlurRadiusFudgeFactor)); |
| 1378 | float stddev = SkScalarToFloat(radius) /2.0f; |
| 1379 | float variance = stddev * stddev; |
| 1380 | |
| 1381 | int windowSize = SkScalarCeil(stddev*4); |
| 1382 | // round window size up to nearest odd number |
| 1383 | windowSize |= 1; |
| 1384 | |
| 1385 | SkAutoTMalloc<float> gaussWindow(windowSize); |
| 1386 | |
| 1387 | int halfWindow = windowSize >> 1; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1388 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1389 | gaussWindow[halfWindow] = 1; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1390 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1391 | float windowSum = 1; |
| 1392 | for (int x = 1 ; x <= halfWindow ; ++x) { |
| 1393 | float gaussian = expf(-x*x / variance); |
| 1394 | gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian; |
| 1395 | windowSum += 2*gaussian; |
| 1396 | } |
| 1397 | |
| 1398 | // leave the filter un-normalized for now; we will divide by the normalization |
| 1399 | // sum later; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1400 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1401 | int pad = halfWindow; |
| 1402 | if (margin) { |
| 1403 | margin->set( pad, pad ); |
| 1404 | } |
| 1405 | |
| 1406 | dst->fBounds = src.fBounds; |
| 1407 | dst->fBounds.outset(pad, pad); |
| 1408 | |
| 1409 | dst->fRowBytes = dst->fBounds.width(); |
| 1410 | dst->fFormat = SkMask::kA8_Format; |
| 1411 | dst->fImage = NULL; |
| 1412 | |
| 1413 | if (src.fImage) { |
| 1414 | |
| 1415 | size_t dstSize = dst->computeImageSize(); |
| 1416 | if (0 == dstSize) { |
| 1417 | return false; // too big to allocate, abort |
| 1418 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1419 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1420 | int srcWidth = src.fBounds.width(); |
| 1421 | int srcHeight = src.fBounds.height(); |
| 1422 | int dstWidth = dst->fBounds.width(); |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1423 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1424 | const uint8_t* srcPixels = src.fImage; |
| 1425 | uint8_t* dstPixels = SkMask::AllocImage(dstSize); |
| 1426 | SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dstPixels); |
| 1427 | |
| 1428 | // do the actual blur. First, make a padded copy of the source. |
| 1429 | // use double pad so we never have to check if we're outside anything |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1430 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1431 | int padWidth = srcWidth + 4*pad; |
| 1432 | int padHeight = srcHeight; |
| 1433 | int padSize = padWidth * padHeight; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1434 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1435 | SkAutoTMalloc<uint8_t> padPixels(padSize); |
| 1436 | memset(padPixels, 0, padSize); |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1437 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1438 | for (int y = 0 ; y < srcHeight; ++y) { |
| 1439 | uint8_t* padptr = padPixels + y * padWidth + 2*pad; |
| 1440 | const uint8_t* srcptr = srcPixels + y * srcWidth; |
| 1441 | memcpy(padptr, srcptr, srcWidth); |
| 1442 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1443 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1444 | // blur in X, transposing the result into a temporary floating point buffer. |
| 1445 | // also double-pad the intermediate result so that the second blur doesn't |
| 1446 | // have to do extra conditionals. |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1447 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1448 | int tmpWidth = padHeight + 4*pad; |
| 1449 | int tmpHeight = padWidth - 2*pad; |
| 1450 | int tmpSize = tmpWidth * tmpHeight; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1451 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1452 | SkAutoTMalloc<float> tmpImage(tmpSize); |
| 1453 | memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0])); |
| 1454 | |
| 1455 | for (int y = 0 ; y < padHeight ; ++y) { |
| 1456 | uint8_t *srcScanline = padPixels + y*padWidth; |
| 1457 | for (int x = pad ; x < padWidth - pad ; ++x) { |
| 1458 | float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output |
| 1459 | uint8_t *windowCenter = srcScanline + x; |
| 1460 | for (int i = -pad ; i <= pad ; ++i) { |
| 1461 | *outPixel += gaussWindow[pad+i]*windowCenter[i]; |
| 1462 | } |
| 1463 | *outPixel /= windowSum; |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1464 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1465 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1466 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1467 | // blur in Y; now filling in the actual desired destination. We have to do |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1468 | // the transpose again; these transposes guarantee that we read memory in |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1469 | // linear order. |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1470 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1471 | for (int y = 0 ; y < tmpHeight ; ++y) { |
| 1472 | float *srcScanline = tmpImage + y*tmpWidth; |
| 1473 | for (int x = pad ; x < tmpWidth - pad ; ++x) { |
| 1474 | float *windowCenter = srcScanline + x; |
| 1475 | float finalValue = 0; |
| 1476 | for (int i = -pad ; i <= pad ; ++i) { |
| 1477 | finalValue += gaussWindow[pad+i]*windowCenter[i]; |
| 1478 | } |
| 1479 | finalValue /= windowSum; |
| 1480 | uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output |
| 1481 | int integerPixel = int(finalValue + 0.5f); |
| 1482 | *outPixel = SkClampMax( SkClampPos(integerPixel), 255 ); |
| 1483 | } |
| 1484 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 1485 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 1486 | dst->fImage = dstPixels; |
| 1487 | // if need be, alloc the "real" dst (same size as src) and copy/merge |
| 1488 | // the blur into it (applying the src) |
| 1489 | if (style == kInner_Style) { |
| 1490 | // now we allocate the "real" dst, mirror the size of src |
| 1491 | size_t srcSize = src.computeImageSize(); |
| 1492 | if (0 == srcSize) { |
| 1493 | return false; // too big to allocate, abort |
| 1494 | } |
| 1495 | dst->fImage = SkMask::AllocImage(srcSize); |
| 1496 | merge_src_with_blur(dst->fImage, src.fRowBytes, |
| 1497 | srcPixels, src.fRowBytes, |
| 1498 | dstPixels + pad*dst->fRowBytes + pad, |
| 1499 | dst->fRowBytes, srcWidth, srcHeight); |
| 1500 | SkMask::FreeImage(dstPixels); |
| 1501 | } else if (style != kNormal_Style) { |
| 1502 | clamp_with_orig(dstPixels + pad*dst->fRowBytes + pad, |
| 1503 | dst->fRowBytes, srcPixels, src.fRowBytes, srcWidth, srcHeight, style); |
| 1504 | } |
| 1505 | (void)autoCall.detach(); |
| 1506 | } |
| 1507 | |
| 1508 | if (style == kInner_Style) { |
| 1509 | dst->fBounds = src.fBounds; // restore trimmed bounds |
| 1510 | dst->fRowBytes = src.fRowBytes; |
| 1511 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1512 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | } |