reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* libs/graphics/effects/SkBlurMask.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkBlurMask.h" |
| 19 | #include "SkTemplates.h" |
| 20 | |
| 21 | static void build_sum_buffer(uint32_t dst[], int w, int h, const uint8_t src[], int srcRB) |
| 22 | { |
| 23 | SkASSERT(srcRB >= w); |
| 24 | // mod srcRB so we can apply it after each row |
| 25 | srcRB -= w; |
| 26 | |
| 27 | int x, y; |
| 28 | |
| 29 | // special case first row |
| 30 | uint32_t X = 0; |
| 31 | for (x = w - 1; x >= 0; --x) |
| 32 | { |
| 33 | X = *src++ + X; |
| 34 | *dst++ = X; |
| 35 | } |
| 36 | src += srcRB; |
| 37 | |
| 38 | // now do the rest of the rows |
| 39 | for (y = h - 1; y > 0; --y) |
| 40 | { |
| 41 | uint32_t L = 0; |
| 42 | uint32_t C = 0; |
| 43 | for (x = w - 1; x >= 0; --x) |
| 44 | { |
| 45 | uint32_t T = dst[-w]; |
| 46 | X = *src++ + L + T - C; |
| 47 | *dst++ = X; |
| 48 | L = X; |
| 49 | C = T; |
| 50 | } |
| 51 | src += srcRB; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void apply_kernel(uint8_t dst[], int rx, int ry, const uint32_t src[], int sw, int sh) |
| 56 | { |
| 57 | uint32_t scale = (1 << 24) / ((2*rx + 1)*(2*ry + 1)); |
| 58 | |
| 59 | int rowBytes = sw; |
| 60 | |
| 61 | int dw = sw + 2*rx; |
| 62 | int dh = sh + 2*ry; |
| 63 | |
| 64 | sw -= 1; // now it is max_x |
| 65 | sh -= 1; // now it is max_y |
| 66 | |
| 67 | int prev_y = -ry - 1 -ry; |
| 68 | int next_y = ry -ry; |
| 69 | |
| 70 | for (int y = 0; y < dh; y++) |
| 71 | { |
| 72 | int py = SkClampPos(prev_y) * rowBytes; |
| 73 | int ny = SkFastMin32(next_y, sh) * rowBytes; |
| 74 | |
| 75 | int prev_x = -rx - 1 -rx; |
| 76 | int next_x = rx -rx; |
| 77 | |
| 78 | for (int x = 0; x < dw; x++) |
| 79 | { |
| 80 | int px = SkClampPos(prev_x); |
| 81 | int nx = SkFastMin32(next_x, sw); |
| 82 | |
| 83 | uint32_t sum = src[px+py] + src[nx+ny] - src[nx+py] - src[px+ny]; |
| 84 | *dst++ = SkToU8(sum * scale >> 24); |
| 85 | |
| 86 | prev_x += 1; |
| 87 | next_x += 1; |
| 88 | } |
| 89 | prev_y += 1; |
| 90 | next_y += 1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void apply_kernel_interp(uint8_t dst[], int rx, int ry, const uint32_t src[], int sw, int sh, U8CPU outer_weight) |
| 95 | { |
| 96 | SkASSERT(rx > 0 && ry > 0); |
| 97 | SkASSERT(outer_weight <= 255); |
| 98 | |
| 99 | int inner_weight = 255 - outer_weight; |
| 100 | |
| 101 | // round these guys up if they're bigger than 127 |
| 102 | outer_weight += outer_weight >> 7; |
| 103 | inner_weight += inner_weight >> 7; |
| 104 | |
| 105 | uint32_t outer_scale = (outer_weight << 16) / ((2*rx + 1)*(2*ry + 1)); |
| 106 | uint32_t inner_scale = (inner_weight << 16) / ((2*rx - 1)*(2*ry - 1)); |
| 107 | |
| 108 | int rowBytes = sw; |
| 109 | |
| 110 | int dw = sw + 2*rx; |
| 111 | int dh = sh + 2*ry; |
| 112 | |
| 113 | sw -= 1; // now it is max_x |
| 114 | sh -= 1; // now it is max_y |
| 115 | |
| 116 | int prev_y = -ry - 1 -ry; |
| 117 | int next_y = ry -ry; |
| 118 | |
| 119 | for (int y = 0; y < dh; y++) |
| 120 | { |
| 121 | int py = SkClampPos(prev_y) * rowBytes; |
| 122 | int ny = SkFastMin32(next_y, sh) * rowBytes; |
| 123 | |
| 124 | int ipy = SkClampPos(prev_y + 1) * rowBytes; |
| 125 | int iny = SkClampMax(next_y - 1, sh) * rowBytes; |
| 126 | |
| 127 | int prev_x = -rx - 1 -rx; |
| 128 | int next_x = rx -rx; |
| 129 | |
| 130 | for (int x = 0; x < dw; x++) |
| 131 | { |
| 132 | int px = SkClampPos(prev_x); |
| 133 | int nx = SkFastMin32(next_x, sw); |
| 134 | |
| 135 | int ipx = SkClampPos(prev_x + 1); |
| 136 | int inx = SkClampMax(next_x - 1, sw); |
| 137 | |
| 138 | uint32_t outer_sum = src[px+py] + src[nx+ny] - src[nx+py] - src[px+ny]; |
| 139 | uint32_t inner_sum = src[ipx+ipy] + src[inx+iny] - src[inx+ipy] - src[ipx+iny]; |
| 140 | *dst++ = SkToU8((outer_sum * outer_scale + inner_sum * inner_scale) >> 24); |
| 141 | |
| 142 | prev_x += 1; |
| 143 | next_x += 1; |
| 144 | } |
| 145 | prev_y += 1; |
| 146 | next_y += 1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #include "SkColorPriv.h" |
| 151 | |
| 152 | static void merge_src_with_blur(uint8_t dst[], |
| 153 | const uint8_t src[], int sw, int sh, |
| 154 | const uint8_t blur[], int blurRowBytes) |
| 155 | { |
| 156 | while (--sh >= 0) |
| 157 | { |
| 158 | for (int x = sw - 1; x >= 0; --x) |
| 159 | { |
| 160 | *dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src))); |
| 161 | dst += 1; |
| 162 | src += 1; |
| 163 | blur += 1; |
| 164 | } |
| 165 | blur += blurRowBytes - sw; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void clamp_with_orig(uint8_t dst[], int dstRowBytes, |
| 170 | const uint8_t src[], int sw, int sh, |
| 171 | SkBlurMask::Style style) |
| 172 | { |
| 173 | int x; |
| 174 | while (--sh >= 0) |
| 175 | { |
| 176 | switch (style) { |
| 177 | case SkBlurMask::kSolid_Style: |
| 178 | for (x = sw - 1; x >= 0; --x) |
| 179 | { |
| 180 | *dst = SkToU8(*src + SkAlphaMul(*dst, SkAlpha255To256(255 - *src))); |
| 181 | dst += 1; |
| 182 | src += 1; |
| 183 | } |
| 184 | break; |
| 185 | case SkBlurMask::kOuter_Style: |
| 186 | for (x = sw - 1; x >= 0; --x) |
| 187 | { |
| 188 | if (*src) |
| 189 | *dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src))); |
| 190 | dst += 1; |
| 191 | src += 1; |
| 192 | } |
| 193 | break; |
| 194 | default: |
| 195 | SkASSERT(!"Unexpected blur style here"); |
| 196 | break; |
| 197 | } |
| 198 | dst += dstRowBytes - sw; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | //////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | // we use a local funciton to wrap the class static method to work around |
| 205 | // a bug in gcc98 |
| 206 | void SkMask_FreeImage(uint8_t* image); |
| 207 | void SkMask_FreeImage(uint8_t* image) |
| 208 | { |
| 209 | SkMask::FreeImage(image); |
| 210 | } |
| 211 | |
| 212 | bool SkBlurMask::Blur(SkMask* dst, const SkMask& src, |
| 213 | SkScalar radius, Style style) |
| 214 | { |
| 215 | if (src.fFormat != SkMask::kA8_Format) |
| 216 | return false; |
| 217 | |
| 218 | int rx = SkScalarCeil(radius); |
| 219 | int outer_weight = 255 - SkScalarRound((SkIntToScalar(rx) - radius) * 255); |
| 220 | |
| 221 | SkASSERT(rx >= 0); |
| 222 | SkASSERT((unsigned)outer_weight <= 255); |
| 223 | |
| 224 | if (rx == 0) |
| 225 | return false; |
| 226 | |
| 227 | int ry = rx; // only do square blur for now |
| 228 | |
| 229 | dst->fBounds.set(src.fBounds.fLeft - rx, src.fBounds.fTop - ry, |
| 230 | src.fBounds.fRight + rx, src.fBounds.fBottom + ry); |
| 231 | dst->fRowBytes = SkToU16(dst->fBounds.width()); |
| 232 | dst->fFormat = SkMask::kA8_Format; |
| 233 | dst->fImage = NULL; |
| 234 | |
| 235 | if (src.fImage) |
| 236 | { |
| 237 | int sw = src.fBounds.width(); |
| 238 | int sh = src.fBounds.height(); |
| 239 | const uint8_t* sp = src.fImage; |
| 240 | uint8_t* dp = SkMask::AllocImage(dst->computeImageSize()); |
| 241 | |
| 242 | SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp); |
| 243 | |
| 244 | // build the blurry destination |
| 245 | { |
| 246 | SkAutoTMalloc<uint32_t> storage(sw * sh); |
| 247 | uint32_t* sumBuffer = storage.get(); |
| 248 | |
| 249 | build_sum_buffer(sumBuffer, sw, sh, sp, src.fRowBytes); |
| 250 | if (outer_weight == 255) |
| 251 | apply_kernel(dp, rx, ry, sumBuffer, sw, sh); |
| 252 | else |
| 253 | apply_kernel_interp(dp, rx, ry, sumBuffer, sw, sh, outer_weight); |
| 254 | } |
| 255 | |
| 256 | dst->fImage = dp; |
| 257 | // if need be, alloc the "real" dst (same size as src) and copy/merge |
| 258 | // the blur into it (applying the src) |
| 259 | if (style == kInner_Style) |
| 260 | { |
| 261 | dst->fImage = SkMask::AllocImage(src.computeImageSize()); |
| 262 | merge_src_with_blur(dst->fImage, sp, sw, sh, |
| 263 | dp + rx + ry*dst->fBounds.width(), |
| 264 | dst->fBounds.width()); |
| 265 | SkMask::FreeImage(dp); |
| 266 | } |
| 267 | else if (style != kNormal_Style) |
| 268 | { |
| 269 | clamp_with_orig(dp + rx + ry*dst->fBounds.width(), |
| 270 | dst->fBounds.width(), |
| 271 | sp, sw, sh, |
| 272 | style); |
| 273 | } |
| 274 | (void)autoCall.detach(); |
| 275 | } |
| 276 | |
| 277 | if (style == kInner_Style) |
| 278 | { |
| 279 | dst->fBounds = src.fBounds; // restore trimmed bounds |
| 280 | dst->fRowBytes = SkToU16(dst->fBounds.width()); |
| 281 | } |
| 282 | |
| 283 | #if 0 |
| 284 | if (gamma && dst->fImage) |
| 285 | { |
| 286 | uint8_t* image = dst->fImage; |
| 287 | uint8_t* stop = image + dst->computeImageSize(); |
| 288 | |
| 289 | for (; image < stop; image += 1) |
| 290 | *image = gamma[*image]; |
| 291 | } |
| 292 | #endif |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | #if 0 |
| 297 | void SkBlurMask::BuildSqrtGamma(uint8_t gamma[256], SkScalar percent) |
| 298 | { |
| 299 | SkASSERT(gamma); |
| 300 | SkASSERT(percent >= 0 && percent <= SK_Scalar1); |
| 301 | |
| 302 | int scale = SkScalarRound(percent * 256); |
| 303 | |
| 304 | for (int i = 0; i < 256; i++) |
| 305 | { |
| 306 | SkFixed n = i * 257; |
| 307 | n += n >> 15; |
| 308 | SkASSERT(n >= 0 && n <= SK_Fixed1); |
| 309 | n = SkFixedSqrt(n); |
| 310 | n = n * 255 >> 16; |
| 311 | n = SkAlphaBlend(n, i, scale); |
| 312 | gamma[i] = SkToU8(n); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void SkBlurMask::BuildSqrGamma(uint8_t gamma[256], SkScalar percent) |
| 317 | { |
| 318 | SkASSERT(gamma); |
| 319 | SkASSERT(percent >= 0 && percent <= SK_Scalar1); |
| 320 | |
| 321 | int scale = SkScalarRound(percent * 256); |
| 322 | SkFixed div255 = SK_Fixed1 / 255; |
| 323 | |
| 324 | for (int i = 0; i < 256; i++) |
| 325 | { |
| 326 | int square = i * i; |
| 327 | int linear = i * 255; |
| 328 | int n = SkAlphaBlend(square, linear, scale); |
| 329 | gamma[i] = SkToU8(n * div255 >> 16); |
| 330 | } |
| 331 | } |
| 332 | #endif |