2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#include "SkBlurMask.h"
|
2011-09-27 17:38:17 +00:00
|
|
|
#include "SkMath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkTemplates.h"
|
2011-11-28 18:22:01 +00:00
|
|
|
#include "SkEndian.h"
|
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
|
|
|
|
SkScalar SkBlurMask::ConvertRadiusToSigma(SkScalar radius) {
|
|
|
|
// This constant approximates the scaling done in the software path's
|
|
|
|
// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
|
|
|
|
// IMHO, it actually should be 1: we blur "less" than we should do
|
|
|
|
// according to the CSS and canvas specs, simply because Safari does the same.
|
|
|
|
// Firefox used to do the same too, until 4.0 where they fixed it. So at some
|
|
|
|
// point we should probably get rid of these scaling constants and rebaseline
|
|
|
|
// all the blur tests.
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
|
2013-08-27 16:14:03 +00:00
|
|
|
|
|
|
|
return radius ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
|
|
|
|
}
|
2013-01-04 20:29:03 +00:00
|
|
|
|
2012-11-27 22:57:41 +00:00
|
|
|
#define UNROLL_SEPARABLE_LOOPS
|
|
|
|
|
2012-11-15 20:27:35 +00:00
|
|
|
/**
|
|
|
|
* This function performs a box blur in X, of the given radius. If the
|
2012-11-16 02:01:17 +00:00
|
|
|
* "transpose" parameter is true, it will transpose the pixels on write,
|
2012-11-15 20:27:35 +00:00
|
|
|
* such that X and Y are swapped. Reads are always performed from contiguous
|
|
|
|
* memory in X, for speed. The destination buffer (dst) must be at least
|
2012-11-27 22:57:41 +00:00
|
|
|
* (width + leftRadius + rightRadius) * height bytes in size.
|
2013-02-19 16:09:10 +00:00
|
|
|
*
|
|
|
|
* This is what the inner loop looks like before unrolling, and with the two
|
|
|
|
* cases broken out separately (width < diameter, width >= diameter):
|
2013-02-20 07:02:30 +00:00
|
|
|
*
|
2013-02-19 16:09:10 +00:00
|
|
|
* if (width < diameter) {
|
|
|
|
* for (int x = 0; x < width; ++x) {
|
|
|
|
* sum += *right++;
|
2013-02-20 07:02:30 +00:00
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
2013-02-19 16:09:10 +00:00
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = width; x < diameter; ++x) {
|
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = 0; x < width; ++x) {
|
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
|
|
|
* sum -= *left++;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* } else {
|
|
|
|
* for (int x = 0; x < diameter; ++x) {
|
|
|
|
* sum += *right++;
|
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = diameter; x < width; ++x) {
|
|
|
|
* sum += *right++;
|
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
|
|
|
* sum -= *left++;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = 0; x < diameter; ++x) {
|
|
|
|
* *dptr = (sum * scale + half) >> 24;
|
|
|
|
* sum -= *left++;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* }
|
2012-11-15 20:27:35 +00:00
|
|
|
*/
|
|
|
|
static int boxBlur(const uint8_t* src, int src_y_stride, uint8_t* dst,
|
2012-11-16 17:22:33 +00:00
|
|
|
int leftRadius, int rightRadius, int width, int height,
|
|
|
|
bool transpose)
|
2012-11-13 20:35:21 +00:00
|
|
|
{
|
2012-11-27 22:57:41 +00:00
|
|
|
int diameter = leftRadius + rightRadius;
|
|
|
|
int kernelSize = diameter + 1;
|
|
|
|
int border = SkMin32(width, diameter);
|
2012-11-13 20:35:21 +00:00
|
|
|
uint32_t scale = (1 << 24) / kernelSize;
|
2012-11-16 17:22:33 +00:00
|
|
|
int new_width = width + SkMax32(leftRadius, rightRadius) * 2;
|
2012-11-15 20:27:35 +00:00
|
|
|
int dst_x_stride = transpose ? height : 1;
|
|
|
|
int dst_y_stride = transpose ? 1 : new_width;
|
2013-02-19 16:09:10 +00:00
|
|
|
uint32_t half = 1 << 23;
|
2012-11-13 20:35:21 +00:00
|
|
|
for (int y = 0; y < height; ++y) {
|
2013-02-19 16:09:10 +00:00
|
|
|
uint32_t sum = 0;
|
2012-11-15 20:27:35 +00:00
|
|
|
uint8_t* dptr = dst + y * dst_y_stride;
|
|
|
|
const uint8_t* right = src + y * src_y_stride;
|
|
|
|
const uint8_t* left = right;
|
2012-11-20 17:09:40 +00:00
|
|
|
for (int x = 0; x < rightRadius - leftRadius; x++) {
|
|
|
|
*dptr = 0;
|
|
|
|
dptr += dst_x_stride;
|
2012-11-16 17:22:33 +00:00
|
|
|
}
|
2012-11-27 22:57:41 +00:00
|
|
|
#define LEFT_BORDER_ITER \
|
|
|
|
sum += *right++; \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (sum * scale + half) >> 24; \
|
2012-11-15 20:27:35 +00:00
|
|
|
dptr += dst_x_stride;
|
2012-11-27 22:57:41 +00:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < border - 16; x += 16) {
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
2012-11-27 22:57:41 +00:00
|
|
|
#endif
|
|
|
|
for (; x < border; ++x) {
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#undef LEFT_BORDER_ITER
|
|
|
|
#define TRIVIAL_ITER \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (sum * scale + half) >> 24; \
|
2012-11-15 20:27:35 +00:00
|
|
|
dptr += dst_x_stride;
|
2012-11-27 22:57:41 +00:00
|
|
|
x = width;
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < diameter - 16; x += 16) {
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
TRIVIAL_ITER
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; x < diameter; ++x) {
|
|
|
|
TRIVIAL_ITER
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
2012-11-27 22:57:41 +00:00
|
|
|
#undef TRIVIAL_ITER
|
|
|
|
#define CENTER_ITER \
|
|
|
|
sum += *right++; \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (sum * scale + half) >> 24; \
|
2012-11-27 22:57:41 +00:00
|
|
|
sum -= *left++; \
|
2012-11-15 20:27:35 +00:00
|
|
|
dptr += dst_x_stride;
|
2012-11-27 22:57:41 +00:00
|
|
|
|
|
|
|
x = diameter;
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < width - 16; x += 16) {
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; x < width; ++x) {
|
|
|
|
CENTER_ITER
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
2012-11-27 22:57:41 +00:00
|
|
|
#undef CENTER_ITER
|
|
|
|
#define RIGHT_BORDER_ITER \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (sum * scale + half) >> 24; \
|
2012-11-27 22:57:41 +00:00
|
|
|
sum -= *left++; \
|
2012-11-15 20:27:35 +00:00
|
|
|
dptr += dst_x_stride;
|
2012-11-27 22:57:41 +00:00
|
|
|
|
|
|
|
x = 0;
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < border - 16; x += 16) {
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; x < border; ++x) {
|
|
|
|
RIGHT_BORDER_ITER
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
2012-11-27 22:57:41 +00:00
|
|
|
#undef RIGHT_BORDER_ITER
|
2013-02-20 16:42:06 +00:00
|
|
|
for (int x = 0; x < leftRadius - rightRadius; ++x) {
|
2012-11-20 17:09:40 +00:00
|
|
|
*dptr = 0;
|
|
|
|
dptr += dst_x_stride;
|
2012-11-16 17:22:33 +00:00
|
|
|
}
|
2012-11-13 20:35:21 +00:00
|
|
|
SkASSERT(sum == 0);
|
|
|
|
}
|
2012-11-15 20:27:35 +00:00
|
|
|
return new_width;
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 22:57:41 +00:00
|
|
|
/**
|
|
|
|
* This variant of the box blur handles blurring of non-integer radii. It
|
|
|
|
* keeps two running sums: an outer sum for the rounded-up kernel radius, and
|
|
|
|
* an inner sum for the rounded-down kernel radius. For each pixel, it linearly
|
|
|
|
* interpolates between them. In float this would be:
|
|
|
|
* outer_weight * outer_sum / kernelSize +
|
|
|
|
* (1.0 - outer_weight) * innerSum / (kernelSize - 2)
|
2013-02-20 07:02:30 +00:00
|
|
|
*
|
2013-02-19 16:09:10 +00:00
|
|
|
* This is what the inner loop looks like before unrolling, and with the two
|
|
|
|
* cases broken out separately (width < diameter, width >= diameter):
|
2013-02-20 07:02:30 +00:00
|
|
|
*
|
2013-02-19 16:09:10 +00:00
|
|
|
* if (width < diameter) {
|
|
|
|
* for (int x = 0; x < width; x++) {
|
|
|
|
* inner_sum = outer_sum;
|
|
|
|
* outer_sum += *right++;
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = width; x < diameter; ++x) {
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = 0; x < width; x++) {
|
|
|
|
* inner_sum = outer_sum - *left++;
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* outer_sum = inner_sum;
|
|
|
|
* }
|
|
|
|
* } else {
|
|
|
|
* for (int x = 0; x < diameter; x++) {
|
|
|
|
* inner_sum = outer_sum;
|
|
|
|
* outer_sum += *right++;
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* }
|
|
|
|
* for (int x = diameter; x < width; ++x) {
|
|
|
|
* inner_sum = outer_sum - *left;
|
|
|
|
* outer_sum += *right++;
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* outer_sum -= *left++;
|
|
|
|
* }
|
|
|
|
* for (int x = 0; x < diameter; x++) {
|
|
|
|
* inner_sum = outer_sum - *left++;
|
|
|
|
* *dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
|
|
|
* dptr += dst_x_stride;
|
|
|
|
* outer_sum = inner_sum;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* return new_width;
|
2012-11-27 22:57:41 +00:00
|
|
|
*/
|
2013-02-19 16:09:10 +00:00
|
|
|
|
2012-11-27 22:57:41 +00:00
|
|
|
static int boxBlurInterp(const uint8_t* src, int src_y_stride, uint8_t* dst,
|
|
|
|
int radius, int width, int height,
|
|
|
|
bool transpose, uint8_t outer_weight)
|
|
|
|
{
|
|
|
|
int diameter = radius * 2;
|
|
|
|
int kernelSize = diameter + 1;
|
|
|
|
int border = SkMin32(width, diameter);
|
|
|
|
int inner_weight = 255 - outer_weight;
|
|
|
|
outer_weight += outer_weight >> 7;
|
|
|
|
inner_weight += inner_weight >> 7;
|
|
|
|
uint32_t outer_scale = (outer_weight << 16) / kernelSize;
|
|
|
|
uint32_t inner_scale = (inner_weight << 16) / (kernelSize - 2);
|
2013-02-19 16:09:10 +00:00
|
|
|
uint32_t half = 1 << 23;
|
2012-11-27 22:57:41 +00:00
|
|
|
int new_width = width + diameter;
|
|
|
|
int dst_x_stride = transpose ? height : 1;
|
|
|
|
int dst_y_stride = transpose ? 1 : new_width;
|
|
|
|
for (int y = 0; y < height; ++y) {
|
2013-02-19 16:09:10 +00:00
|
|
|
uint32_t outer_sum = 0, inner_sum = 0;
|
2012-11-27 22:57:41 +00:00
|
|
|
uint8_t* dptr = dst + y * dst_y_stride;
|
|
|
|
const uint8_t* right = src + y * src_y_stride;
|
|
|
|
const uint8_t* left = right;
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
#define LEFT_BORDER_ITER \
|
|
|
|
inner_sum = outer_sum; \
|
|
|
|
outer_sum += *right++; \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \
|
2012-11-27 22:57:41 +00:00
|
|
|
dptr += dst_x_stride;
|
|
|
|
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (;x < border - 16; x += 16) {
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
LEFT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
for (;x < border; ++x) {
|
2012-11-27 22:57:41 +00:00
|
|
|
LEFT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#undef LEFT_BORDER_ITER
|
|
|
|
for (int x = width; x < diameter; ++x) {
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24;
|
2012-11-27 22:57:41 +00:00
|
|
|
dptr += dst_x_stride;
|
|
|
|
}
|
|
|
|
x = diameter;
|
|
|
|
|
|
|
|
#define CENTER_ITER \
|
|
|
|
inner_sum = outer_sum - *left; \
|
|
|
|
outer_sum += *right++; \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \
|
2012-11-27 22:57:41 +00:00
|
|
|
dptr += dst_x_stride; \
|
|
|
|
outer_sum -= *left++;
|
|
|
|
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < width - 16; x += 16) {
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
CENTER_ITER
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; x < width; ++x) {
|
|
|
|
CENTER_ITER
|
|
|
|
}
|
|
|
|
#undef CENTER_ITER
|
|
|
|
|
|
|
|
#define RIGHT_BORDER_ITER \
|
|
|
|
inner_sum = outer_sum - *left++; \
|
2013-02-19 16:09:10 +00:00
|
|
|
*dptr = (outer_sum * outer_scale + inner_sum * inner_scale + half) >> 24; \
|
2012-11-27 22:57:41 +00:00
|
|
|
dptr += dst_x_stride; \
|
|
|
|
outer_sum = inner_sum;
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
#ifdef UNROLL_SEPARABLE_LOOPS
|
|
|
|
for (; x < border - 16; x += 16) {
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-20 16:42:06 +00:00
|
|
|
for (; x < border; ++x) {
|
2012-11-27 22:57:41 +00:00
|
|
|
RIGHT_BORDER_ITER
|
|
|
|
}
|
|
|
|
#undef RIGHT_BORDER_ITER
|
|
|
|
SkASSERT(outer_sum == 0 && inner_sum == 0);
|
|
|
|
}
|
|
|
|
return new_width;
|
|
|
|
}
|
|
|
|
|
2012-11-16 17:22:33 +00:00
|
|
|
static void get_adjusted_radii(SkScalar passRadius, int *loRadius, int *hiRadius)
|
|
|
|
{
|
2013-12-17 19:22:07 +00:00
|
|
|
*loRadius = *hiRadius = SkScalarCeilToInt(passRadius);
|
2013-11-25 19:44:07 +00:00
|
|
|
if (SkIntToScalar(*hiRadius) - passRadius > 0.5f) {
|
2012-11-16 17:22:33 +00:00
|
|
|
*loRadius = *hiRadius - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
|
2009-09-18 13:41:56 +00:00
|
|
|
static void merge_src_with_blur(uint8_t dst[], int dstRB,
|
|
|
|
const uint8_t src[], int srcRB,
|
|
|
|
const uint8_t blur[], int blurRB,
|
|
|
|
int sw, int sh) {
|
|
|
|
dstRB -= sw;
|
|
|
|
srcRB -= sw;
|
|
|
|
blurRB -= sw;
|
|
|
|
while (--sh >= 0) {
|
|
|
|
for (int x = sw - 1; x >= 0; --x) {
|
2008-12-17 15:59:43 +00:00
|
|
|
*dst = SkToU8(SkAlphaMul(*blur, SkAlpha255To256(*src)));
|
|
|
|
dst += 1;
|
|
|
|
src += 1;
|
|
|
|
blur += 1;
|
|
|
|
}
|
2009-09-18 13:41:56 +00:00
|
|
|
dst += dstRB;
|
|
|
|
src += srcRB;
|
|
|
|
blur += blurRB;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clamp_with_orig(uint8_t dst[], int dstRowBytes,
|
2009-09-18 13:41:56 +00:00
|
|
|
const uint8_t src[], int srcRowBytes,
|
|
|
|
int sw, int sh,
|
2009-09-21 00:27:08 +00:00
|
|
|
SkBlurMask::Style style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
int x;
|
2009-09-18 13:41:56 +00:00
|
|
|
while (--sh >= 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
switch (style) {
|
|
|
|
case SkBlurMask::kSolid_Style:
|
2009-09-18 13:41:56 +00:00
|
|
|
for (x = sw - 1; x >= 0; --x) {
|
|
|
|
int s = *src;
|
|
|
|
int d = *dst;
|
|
|
|
*dst = SkToU8(s + d - SkMulDiv255Round(s, d));
|
2008-12-17 15:59:43 +00:00
|
|
|
dst += 1;
|
|
|
|
src += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SkBlurMask::kOuter_Style:
|
2009-09-18 13:41:56 +00:00
|
|
|
for (x = sw - 1; x >= 0; --x) {
|
|
|
|
if (*src) {
|
2008-12-17 15:59:43 +00:00
|
|
|
*dst = SkToU8(SkAlphaMul(*dst, SkAlpha255To256(255 - *src)));
|
2009-09-18 13:41:56 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
dst += 1;
|
|
|
|
src += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2011-12-28 14:59:50 +00:00
|
|
|
SkDEBUGFAIL("Unexpected blur style here");
|
2008-12-17 15:59:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dst += dstRowBytes - sw;
|
2009-09-18 13:41:56 +00:00
|
|
|
src += srcRowBytes - sw;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 14:59:59 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-01-11 20:54:44 +00:00
|
|
|
// we use a local function to wrap the class static method to work around
|
2008-12-17 15:59:43 +00:00
|
|
|
// a bug in gcc98
|
|
|
|
void SkMask_FreeImage(uint8_t* image);
|
2011-08-12 14:59:59 +00:00
|
|
|
void SkMask_FreeImage(uint8_t* image) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMask::FreeImage(image);
|
|
|
|
}
|
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
bool SkBlurMask::BoxBlur(SkMask* dst, const SkMask& src,
|
|
|
|
SkScalar sigma, Style style, Quality quality,
|
2014-04-28 15:09:14 +00:00
|
|
|
SkIPoint* margin, bool force_quality) {
|
2013-02-20 16:42:06 +00:00
|
|
|
|
2011-08-12 14:59:59 +00:00
|
|
|
if (src.fFormat != SkMask::kA8_Format) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2011-08-12 14:59:59 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-02-18 19:03:01 +00:00
|
|
|
// Force high quality off for small radii (performance)
|
2014-04-28 15:09:14 +00:00
|
|
|
if (!force_quality && sigma <= SkIntToScalar(2)) {
|
2012-11-29 17:09:27 +00:00
|
|
|
quality = kLow_Quality;
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
SkScalar passRadius;
|
|
|
|
if (kHigh_Quality == quality) {
|
|
|
|
// For the high quality path the 3 pass box blur kernel width is
|
|
|
|
// 6*rad+1 while the full Gaussian width is 6*sigma.
|
|
|
|
passRadius = sigma - (1/6.0f);
|
|
|
|
} else {
|
2013-08-28 07:01:18 +00:00
|
|
|
// For the low quality path we only attempt to cover 3*sigma of the
|
|
|
|
// Gaussian blur area (1.5*sigma on each side). The single pass box
|
2013-08-27 16:14:03 +00:00
|
|
|
// blur's kernel size is 2*rad+1.
|
|
|
|
passRadius = 1.5f*sigma - 0.5f;
|
|
|
|
}
|
|
|
|
|
2013-02-21 07:15:03 +00:00
|
|
|
// highQuality: use three box blur passes as a cheap way
|
2013-02-20 16:42:06 +00:00
|
|
|
// to approximate a Gaussian blur
|
2012-11-29 17:09:27 +00:00
|
|
|
int passCount = (kHigh_Quality == quality) ? 3 : 1;
|
2013-02-20 16:42:06 +00:00
|
|
|
|
2013-12-17 19:22:07 +00:00
|
|
|
int rx = SkScalarCeilToInt(passRadius);
|
|
|
|
int outerWeight = 255 - SkScalarRoundToInt((SkIntToScalar(rx) - passRadius) * 255);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkASSERT(rx >= 0);
|
2013-02-20 16:42:06 +00:00
|
|
|
SkASSERT((unsigned)outerWeight <= 255);
|
2009-09-18 13:41:56 +00:00
|
|
|
if (rx <= 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2009-09-18 13:41:56 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
int ry = rx; // only do square blur for now
|
|
|
|
|
2011-02-18 19:03:01 +00:00
|
|
|
int padx = passCount * rx;
|
|
|
|
int pady = passCount * ry;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
if (margin) {
|
|
|
|
margin->set(padx, pady);
|
|
|
|
}
|
2011-02-18 19:03:01 +00:00
|
|
|
dst->fBounds.set(src.fBounds.fLeft - padx, src.fBounds.fTop - pady,
|
2013-08-27 16:14:03 +00:00
|
|
|
src.fBounds.fRight + padx, src.fBounds.fBottom + pady);
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2009-03-19 21:52:42 +00:00
|
|
|
dst->fRowBytes = dst->fBounds.width();
|
2008-12-17 15:59:43 +00:00
|
|
|
dst->fFormat = SkMask::kA8_Format;
|
|
|
|
dst->fImage = NULL;
|
|
|
|
|
2009-09-18 13:41:56 +00:00
|
|
|
if (src.fImage) {
|
2009-04-24 12:43:40 +00:00
|
|
|
size_t dstSize = dst->computeImageSize();
|
|
|
|
if (0 == dstSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
int sw = src.fBounds.width();
|
|
|
|
int sh = src.fBounds.height();
|
|
|
|
const uint8_t* sp = src.fImage;
|
2009-04-24 12:43:40 +00:00
|
|
|
uint8_t* dp = SkMask::AllocImage(dstSize);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dp);
|
|
|
|
|
|
|
|
// build the blurry destination
|
2013-06-11 15:23:42 +00:00
|
|
|
SkAutoTMalloc<uint8_t> tmpBuffer(dstSize);
|
|
|
|
uint8_t* tp = tmpBuffer.get();
|
|
|
|
int w = sw, h = sh;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-06-11 15:23:42 +00:00
|
|
|
if (outerWeight == 255) {
|
|
|
|
int loRadius, hiRadius;
|
|
|
|
get_adjusted_radii(passRadius, &loRadius, &hiRadius);
|
|
|
|
if (kHigh_Quality == quality) {
|
|
|
|
// Do three X blurs, with a transpose on the final one.
|
|
|
|
w = boxBlur(sp, src.fRowBytes, tp, loRadius, hiRadius, w, h, false);
|
|
|
|
w = boxBlur(tp, w, dp, hiRadius, loRadius, w, h, false);
|
|
|
|
w = boxBlur(dp, w, tp, hiRadius, hiRadius, w, h, true);
|
|
|
|
// Do three Y blurs, with a transpose on the final one.
|
|
|
|
h = boxBlur(tp, h, dp, loRadius, hiRadius, h, w, false);
|
|
|
|
h = boxBlur(dp, h, tp, hiRadius, loRadius, h, w, false);
|
|
|
|
h = boxBlur(tp, h, dp, hiRadius, hiRadius, h, w, true);
|
2012-11-15 20:27:35 +00:00
|
|
|
} else {
|
2013-06-11 15:23:42 +00:00
|
|
|
w = boxBlur(sp, src.fRowBytes, tp, rx, rx, w, h, true);
|
|
|
|
h = boxBlur(tp, h, dp, ry, ry, h, w, true);
|
2012-11-13 20:35:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-11-29 17:09:27 +00:00
|
|
|
if (kHigh_Quality == quality) {
|
2013-06-11 15:23:42 +00:00
|
|
|
// Do three X blurs, with a transpose on the final one.
|
|
|
|
w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, false, outerWeight);
|
|
|
|
w = boxBlurInterp(tp, w, dp, rx, w, h, false, outerWeight);
|
|
|
|
w = boxBlurInterp(dp, w, tp, rx, w, h, true, outerWeight);
|
|
|
|
// Do three Y blurs, with a transpose on the final one.
|
|
|
|
h = boxBlurInterp(tp, h, dp, ry, h, w, false, outerWeight);
|
|
|
|
h = boxBlurInterp(dp, h, tp, ry, h, w, false, outerWeight);
|
|
|
|
h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight);
|
|
|
|
} else {
|
|
|
|
w = boxBlurInterp(sp, src.fRowBytes, tp, rx, w, h, true, outerWeight);
|
|
|
|
h = boxBlurInterp(tp, h, dp, ry, h, w, true, outerWeight);
|
2011-02-18 19:03:01 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst->fImage = dp;
|
|
|
|
// if need be, alloc the "real" dst (same size as src) and copy/merge
|
|
|
|
// the blur into it (applying the src)
|
2009-09-18 13:41:56 +00:00
|
|
|
if (style == kInner_Style) {
|
|
|
|
// now we allocate the "real" dst, mirror the size of src
|
2009-04-24 12:43:40 +00:00
|
|
|
size_t srcSize = src.computeImageSize();
|
|
|
|
if (0 == srcSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
|
|
|
dst->fImage = SkMask::AllocImage(srcSize);
|
2009-09-18 13:41:56 +00:00
|
|
|
merge_src_with_blur(dst->fImage, src.fRowBytes,
|
|
|
|
sp, src.fRowBytes,
|
2011-08-12 14:59:59 +00:00
|
|
|
dp + passCount * (rx + ry * dst->fRowBytes),
|
|
|
|
dst->fRowBytes, sw, sh);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMask::FreeImage(dp);
|
2009-09-18 13:41:56 +00:00
|
|
|
} else if (style != kNormal_Style) {
|
2011-08-12 14:59:59 +00:00
|
|
|
clamp_with_orig(dp + passCount * (rx + ry * dst->fRowBytes),
|
|
|
|
dst->fRowBytes, sp, src.fRowBytes, sw, sh, style);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
(void)autoCall.detach();
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:41:56 +00:00
|
|
|
if (style == kInner_Style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
dst->fBounds = src.fBounds; // restore trimmed bounds
|
2009-09-18 13:41:56 +00:00
|
|
|
dst->fRowBytes = src.fRowBytes;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
/* Convolving a box with itself three times results in a piecewise
|
|
|
|
quadratic function:
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
0 x <= -1.5
|
2013-02-20 16:42:06 +00:00
|
|
|
9/8 + 3/2 x + 1/2 x^2 -1.5 < x <= -.5
|
2013-01-04 20:29:03 +00:00
|
|
|
3/4 - x^2 -.5 < x <= .5
|
|
|
|
9/8 - 3/2 x + 1/2 x^2 0.5 < x <= 1.5
|
|
|
|
0 1.5 < x
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
Mathematica:
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
g[x_] := Piecewise [ {
|
|
|
|
{9/8 + 3/2 x + 1/2 x^2 , -1.5 < x <= -.5},
|
|
|
|
{3/4 - x^2 , -.5 < x <= .5},
|
|
|
|
{9/8 - 3/2 x + 1/2 x^2 , 0.5 < x <= 1.5}
|
|
|
|
}, 0]
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
To get the profile curve of the blurred step function at the rectangle
|
|
|
|
edge, we evaluate the indefinite integral, which is piecewise cubic:
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
0 x <= -1.5
|
2013-02-20 16:42:06 +00:00
|
|
|
9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3 -1.5 < x <= -0.5
|
2013-01-04 20:29:03 +00:00
|
|
|
1/2 + 3/4 x - 1/3 x^3 -.5 < x <= .5
|
2013-02-20 16:42:06 +00:00
|
|
|
7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3 .5 < x <= 1.5
|
2013-01-04 20:29:03 +00:00
|
|
|
1 1.5 < x
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
in Mathematica code:
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
gi[x_] := Piecewise[ {
|
|
|
|
{ 0 , x <= -1.5 },
|
|
|
|
{ 9/16 + 9/8 x + 3/4 x^2 + 1/6 x^3, -1.5 < x <= -0.5 },
|
|
|
|
{ 1/2 + 3/4 x - 1/3 x^3 , -.5 < x <= .5},
|
|
|
|
{ 7/16 + 9/8 x - 3/4 x^2 + 1/6 x^3, .5 < x <= 1.5}
|
|
|
|
},1]
|
2013-01-04 20:29:03 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
static float gaussianIntegral(float x) {
|
|
|
|
if (x > 1.5f) {
|
2013-01-04 20:29:03 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2013-02-20 16:42:06 +00:00
|
|
|
if (x < -1.5f) {
|
2013-01-04 20:29:03 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float x2 = x*x;
|
|
|
|
float x3 = x2*x;
|
|
|
|
|
2013-01-07 18:41:28 +00:00
|
|
|
if ( x > 0.5f ) {
|
2013-02-20 16:42:06 +00:00
|
|
|
return 0.5625f - (x3 / 6.0f - 3.0f * x2 * 0.25f + 1.125f * x);
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
2013-01-07 18:41:28 +00:00
|
|
|
if ( x > -0.5f ) {
|
|
|
|
return 0.5f - (0.75f * x - x3 / 3.0f);
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
2013-01-07 18:41:28 +00:00
|
|
|
return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x);
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 15:34:43 +00:00
|
|
|
/* ComputeBlurProfile allocates and fills in an array of floating
|
2013-01-05 02:02:05 +00:00
|
|
|
point values between 0 and 255 for the profile signature of
|
2013-01-04 20:29:03 +00:00
|
|
|
a blurred half-plane with the given blur radius. Since we're
|
|
|
|
going to be doing screened multiplications (i.e., 1 - (1-x)(1-y))
|
|
|
|
all the time, we actually fill in the profile pre-inverted
|
|
|
|
(already done 255-x).
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-03-11 20:16:28 +00:00
|
|
|
It's the responsibility of the caller to delete the
|
2013-01-04 20:29:03 +00:00
|
|
|
memory returned in profile_out.
|
|
|
|
*/
|
|
|
|
|
2014-01-30 15:34:43 +00:00
|
|
|
void SkBlurMask::ComputeBlurProfile(SkScalar sigma, uint8_t **profile_out) {
|
2013-08-27 16:14:03 +00:00
|
|
|
int size = SkScalarCeilToInt(6*sigma);
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
int center = size >> 1;
|
2014-01-30 15:34:43 +00:00
|
|
|
uint8_t *profile = SkNEW_ARRAY(uint8_t, size);
|
2013-01-04 20:29:03 +00:00
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
float invr = 1.f/(2*sigma);
|
2013-01-04 20:29:03 +00:00
|
|
|
|
|
|
|
profile[0] = 255;
|
2013-02-20 16:42:06 +00:00
|
|
|
for (int x = 1 ; x < size ; ++x) {
|
2013-02-20 19:02:34 +00:00
|
|
|
float scaled_x = (center - x - .5f) * invr;
|
2013-02-20 16:42:06 +00:00
|
|
|
float gi = gaussianIntegral(scaled_x);
|
|
|
|
profile[x] = 255 - (uint8_t) (255.f * gi);
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*profile_out = profile;
|
|
|
|
}
|
|
|
|
|
2013-01-05 02:02:05 +00:00
|
|
|
// TODO MAYBE: Maintain a profile cache to avoid recomputing this for
|
2013-01-04 20:29:03 +00:00
|
|
|
// commonly used radii. Consider baking some of the most common blur radii
|
|
|
|
// directly in as static data?
|
|
|
|
|
|
|
|
// Implementation adapted from Michael Herf's approach:
|
|
|
|
// http://stereopsis.com/shadowrect/
|
|
|
|
|
2014-01-30 15:34:43 +00:00
|
|
|
uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, int blurred_width, int sharp_width) {
|
2013-02-20 16:42:06 +00:00
|
|
|
int dx = SkAbs32(((loc << 1) + 1) - blurred_width) - sharp_width; // how far are we from the original edge?
|
|
|
|
int ox = dx >> 1;
|
|
|
|
if (ox < 0) {
|
|
|
|
ox = 0;
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
return profile[ox];
|
|
|
|
}
|
|
|
|
|
2014-01-31 03:01:59 +00:00
|
|
|
void SkBlurMask::ComputeBlurredScanline(uint8_t *pixels, const uint8_t *profile,
|
2014-01-30 15:34:43 +00:00
|
|
|
unsigned int width, SkScalar sigma) {
|
|
|
|
|
|
|
|
unsigned int profile_size = SkScalarCeilToInt(6*sigma);
|
|
|
|
SkAutoTMalloc<uint8_t> horizontalScanline(width);
|
|
|
|
|
|
|
|
unsigned int sw = width - profile_size;
|
|
|
|
// nearest odd number less than the profile size represents the center
|
|
|
|
// of the (2x scaled) profile
|
|
|
|
int center = ( profile_size & ~1 ) - 1;
|
|
|
|
|
|
|
|
int w = sw - center;
|
|
|
|
|
|
|
|
for (unsigned int x = 0 ; x < width ; ++x) {
|
|
|
|
if (profile_size <= sw) {
|
|
|
|
pixels[x] = ProfileLookup(profile, x, width, w);
|
|
|
|
} else {
|
|
|
|
float span = float(sw)/(2*sigma);
|
|
|
|
float giX = 1.5f - (x+.5f)/(2*sigma);
|
|
|
|
pixels[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span)));
|
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
}
|
2014-01-30 15:34:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 07:01:18 +00:00
|
|
|
bool SkBlurMask::BlurRect(SkScalar sigma, SkMask *dst,
|
2013-08-27 16:14:03 +00:00
|
|
|
const SkRect &src, Style style,
|
|
|
|
SkIPoint *margin, SkMask::CreateMode createMode) {
|
|
|
|
int profile_size = SkScalarCeilToInt(6*sigma);
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int pad = profile_size/2;
|
2013-01-04 20:29:03 +00:00
|
|
|
if (margin) {
|
|
|
|
margin->set( pad, pad );
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-03-12 07:12:32 +00:00
|
|
|
dst->fBounds.set(SkScalarRoundToInt(src.fLeft - pad),
|
|
|
|
SkScalarRoundToInt(src.fTop - pad),
|
|
|
|
SkScalarRoundToInt(src.fRight + pad),
|
2013-03-11 21:16:20 +00:00
|
|
|
SkScalarRoundToInt(src.fBottom + pad));
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
dst->fRowBytes = dst->fBounds.width();
|
|
|
|
dst->fFormat = SkMask::kA8_Format;
|
|
|
|
dst->fImage = NULL;
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-03-11 20:16:28 +00:00
|
|
|
int sw = SkScalarFloorToInt(src.width());
|
|
|
|
int sh = SkScalarFloorToInt(src.height());
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-03-11 20:16:28 +00:00
|
|
|
if (createMode == SkMask::kJustComputeBounds_CreateMode) {
|
|
|
|
if (style == kInner_Style) {
|
2013-03-12 07:12:32 +00:00
|
|
|
dst->fBounds.set(SkScalarRoundToInt(src.fLeft),
|
|
|
|
SkScalarRoundToInt(src.fTop),
|
|
|
|
SkScalarRoundToInt(src.fRight),
|
2013-03-11 21:16:20 +00:00
|
|
|
SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds
|
2013-03-11 20:16:28 +00:00
|
|
|
dst->fRowBytes = sw;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-30 15:34:43 +00:00
|
|
|
uint8_t *profile = NULL;
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2014-01-30 15:34:43 +00:00
|
|
|
ComputeBlurProfile(sigma, &profile);
|
|
|
|
SkAutoTDeleteArray<uint8_t> ada(profile);
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
size_t dstSize = dst->computeImageSize();
|
|
|
|
if (0 == dstSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
uint8_t* dp = SkMask::AllocImage(dstSize);
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
dst->fImage = dp;
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int dstHeight = dst->fBounds.height();
|
|
|
|
int dstWidth = dst->fBounds.width();
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
uint8_t *outptr = dp;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
SkAutoTMalloc<uint8_t> horizontalScanline(dstWidth);
|
2014-01-30 15:34:43 +00:00
|
|
|
SkAutoTMalloc<uint8_t> verticalScanline(dstHeight);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 15:34:43 +00:00
|
|
|
ComputeBlurredScanline(horizontalScanline, profile, dstWidth, sigma);
|
|
|
|
ComputeBlurredScanline(verticalScanline, profile, dstHeight, sigma);
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
for (int y = 0 ; y < dstHeight ; ++y) {
|
|
|
|
for (int x = 0 ; x < dstWidth ; x++) {
|
2014-01-30 15:34:43 +00:00
|
|
|
unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]);
|
2013-02-20 16:42:06 +00:00
|
|
|
*(outptr++) = maskval;
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
if (style == kInner_Style) {
|
|
|
|
// now we allocate the "real" dst, mirror the size of src
|
2013-02-20 19:02:34 +00:00
|
|
|
size_t srcSize = (size_t)(src.width() * src.height());
|
2013-02-20 16:42:06 +00:00
|
|
|
if (0 == srcSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
|
|
|
dst->fImage = SkMask::AllocImage(srcSize);
|
|
|
|
for (int y = 0 ; y < sh ; y++) {
|
|
|
|
uint8_t *blur_scanline = dp + (y+pad)*dstWidth + pad;
|
|
|
|
uint8_t *inner_scanline = dst->fImage + y*sw;
|
|
|
|
memcpy(inner_scanline, blur_scanline, sw);
|
|
|
|
}
|
|
|
|
SkMask::FreeImage(dp);
|
|
|
|
|
2013-03-12 07:12:32 +00:00
|
|
|
dst->fBounds.set(SkScalarRoundToInt(src.fLeft),
|
|
|
|
SkScalarRoundToInt(src.fTop),
|
|
|
|
SkScalarRoundToInt(src.fRight),
|
2013-03-11 21:16:20 +00:00
|
|
|
SkScalarRoundToInt(src.fBottom)); // restore trimmed bounds
|
2013-02-20 16:42:06 +00:00
|
|
|
dst->fRowBytes = sw;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
} else if (style == kOuter_Style) {
|
|
|
|
for (int y = pad ; y < dstHeight-pad ; y++) {
|
|
|
|
uint8_t *dst_scanline = dp + y*dstWidth + pad;
|
|
|
|
memset(dst_scanline, 0, sw);
|
|
|
|
}
|
2013-03-11 22:18:54 +00:00
|
|
|
} else if (style == kSolid_Style) {
|
|
|
|
for (int y = pad ; y < dstHeight-pad ; y++) {
|
|
|
|
uint8_t *dst_scanline = dp + y*dstWidth + pad;
|
|
|
|
memset(dst_scanline, 0xff, sw);
|
2013-03-12 07:12:32 +00:00
|
|
|
}
|
2013-02-20 16:42:06 +00:00
|
|
|
}
|
|
|
|
// normal and solid styles are the same for analytic rect blurs, so don't
|
|
|
|
// need to handle solid specially.
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2014-03-10 21:42:06 +00:00
|
|
|
bool SkBlurMask::BlurRRect(SkScalar sigma, SkMask *dst,
|
|
|
|
const SkRRect &src, Style style,
|
|
|
|
SkIPoint *margin, SkMask::CreateMode createMode) {
|
|
|
|
// Temporary for now -- always fail, should cause caller to fall back
|
|
|
|
// to old path. Plumbing just to land API and parallelize effort.
|
|
|
|
|
|
|
|
return false;
|
2013-08-27 16:14:03 +00:00
|
|
|
}
|
2014-03-10 21:42:06 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
// The "simple" blur is a direct implementation of separable convolution with a discrete
|
|
|
|
// gaussian kernel. It's "ground truth" in a sense; too slow to be used, but very
|
|
|
|
// useful for correctness comparisons.
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
bool SkBlurMask::BlurGroundTruth(SkScalar sigma, SkMask* dst, const SkMask& src,
|
|
|
|
Style style, SkIPoint* margin) {
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
if (src.fFormat != SkMask::kA8_Format) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-08-27 16:14:03 +00:00
|
|
|
float variance = sigma * sigma;
|
2013-02-20 16:42:06 +00:00
|
|
|
|
2013-12-17 19:22:07 +00:00
|
|
|
int windowSize = SkScalarCeilToInt(sigma*6);
|
2013-02-20 16:42:06 +00:00
|
|
|
// round window size up to nearest odd number
|
|
|
|
windowSize |= 1;
|
|
|
|
|
|
|
|
SkAutoTMalloc<float> gaussWindow(windowSize);
|
|
|
|
|
|
|
|
int halfWindow = windowSize >> 1;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
gaussWindow[halfWindow] = 1;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
float windowSum = 1;
|
|
|
|
for (int x = 1 ; x <= halfWindow ; ++x) {
|
2013-09-05 16:39:03 +00:00
|
|
|
float gaussian = expf(-x*x / (2*variance));
|
2013-02-20 16:42:06 +00:00
|
|
|
gaussWindow[halfWindow + x] = gaussWindow[halfWindow-x] = gaussian;
|
|
|
|
windowSum += 2*gaussian;
|
|
|
|
}
|
|
|
|
|
|
|
|
// leave the filter un-normalized for now; we will divide by the normalization
|
|
|
|
// sum later;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int pad = halfWindow;
|
|
|
|
if (margin) {
|
|
|
|
margin->set( pad, pad );
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->fBounds = src.fBounds;
|
|
|
|
dst->fBounds.outset(pad, pad);
|
|
|
|
|
|
|
|
dst->fRowBytes = dst->fBounds.width();
|
|
|
|
dst->fFormat = SkMask::kA8_Format;
|
|
|
|
dst->fImage = NULL;
|
|
|
|
|
|
|
|
if (src.fImage) {
|
|
|
|
|
|
|
|
size_t dstSize = dst->computeImageSize();
|
|
|
|
if (0 == dstSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int srcWidth = src.fBounds.width();
|
|
|
|
int srcHeight = src.fBounds.height();
|
|
|
|
int dstWidth = dst->fBounds.width();
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
const uint8_t* srcPixels = src.fImage;
|
|
|
|
uint8_t* dstPixels = SkMask::AllocImage(dstSize);
|
|
|
|
SkAutoTCallVProc<uint8_t, SkMask_FreeImage> autoCall(dstPixels);
|
|
|
|
|
|
|
|
// do the actual blur. First, make a padded copy of the source.
|
|
|
|
// use double pad so we never have to check if we're outside anything
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int padWidth = srcWidth + 4*pad;
|
|
|
|
int padHeight = srcHeight;
|
|
|
|
int padSize = padWidth * padHeight;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
SkAutoTMalloc<uint8_t> padPixels(padSize);
|
|
|
|
memset(padPixels, 0, padSize);
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
for (int y = 0 ; y < srcHeight; ++y) {
|
|
|
|
uint8_t* padptr = padPixels + y * padWidth + 2*pad;
|
|
|
|
const uint8_t* srcptr = srcPixels + y * srcWidth;
|
|
|
|
memcpy(padptr, srcptr, srcWidth);
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
// blur in X, transposing the result into a temporary floating point buffer.
|
|
|
|
// also double-pad the intermediate result so that the second blur doesn't
|
|
|
|
// have to do extra conditionals.
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
int tmpWidth = padHeight + 4*pad;
|
|
|
|
int tmpHeight = padWidth - 2*pad;
|
|
|
|
int tmpSize = tmpWidth * tmpHeight;
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
SkAutoTMalloc<float> tmpImage(tmpSize);
|
|
|
|
memset(tmpImage, 0, tmpSize*sizeof(tmpImage[0]));
|
|
|
|
|
|
|
|
for (int y = 0 ; y < padHeight ; ++y) {
|
|
|
|
uint8_t *srcScanline = padPixels + y*padWidth;
|
|
|
|
for (int x = pad ; x < padWidth - pad ; ++x) {
|
|
|
|
float *outPixel = tmpImage + (x-pad)*tmpWidth + y + 2*pad; // transposed output
|
|
|
|
uint8_t *windowCenter = srcScanline + x;
|
|
|
|
for (int i = -pad ; i <= pad ; ++i) {
|
|
|
|
*outPixel += gaussWindow[pad+i]*windowCenter[i];
|
|
|
|
}
|
|
|
|
*outPixel /= windowSum;
|
2013-02-21 07:15:03 +00:00
|
|
|
}
|
2013-02-20 16:42:06 +00:00
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
// blur in Y; now filling in the actual desired destination. We have to do
|
2013-02-21 07:15:03 +00:00
|
|
|
// the transpose again; these transposes guarantee that we read memory in
|
2013-02-20 16:42:06 +00:00
|
|
|
// linear order.
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
for (int y = 0 ; y < tmpHeight ; ++y) {
|
|
|
|
float *srcScanline = tmpImage + y*tmpWidth;
|
|
|
|
for (int x = pad ; x < tmpWidth - pad ; ++x) {
|
|
|
|
float *windowCenter = srcScanline + x;
|
|
|
|
float finalValue = 0;
|
|
|
|
for (int i = -pad ; i <= pad ; ++i) {
|
|
|
|
finalValue += gaussWindow[pad+i]*windowCenter[i];
|
|
|
|
}
|
|
|
|
finalValue /= windowSum;
|
|
|
|
uint8_t *outPixel = dstPixels + (x-pad)*dstWidth + y; // transposed output
|
|
|
|
int integerPixel = int(finalValue + 0.5f);
|
|
|
|
*outPixel = SkClampMax( SkClampPos(integerPixel), 255 );
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 07:15:03 +00:00
|
|
|
|
2013-02-20 16:42:06 +00:00
|
|
|
dst->fImage = dstPixels;
|
|
|
|
// if need be, alloc the "real" dst (same size as src) and copy/merge
|
|
|
|
// the blur into it (applying the src)
|
|
|
|
if (style == kInner_Style) {
|
|
|
|
// now we allocate the "real" dst, mirror the size of src
|
|
|
|
size_t srcSize = src.computeImageSize();
|
|
|
|
if (0 == srcSize) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
|
|
|
dst->fImage = SkMask::AllocImage(srcSize);
|
|
|
|
merge_src_with_blur(dst->fImage, src.fRowBytes,
|
|
|
|
srcPixels, src.fRowBytes,
|
|
|
|
dstPixels + pad*dst->fRowBytes + pad,
|
|
|
|
dst->fRowBytes, srcWidth, srcHeight);
|
|
|
|
SkMask::FreeImage(dstPixels);
|
|
|
|
} else if (style != kNormal_Style) {
|
|
|
|
clamp_with_orig(dstPixels + pad*dst->fRowBytes + pad,
|
|
|
|
dst->fRowBytes, srcPixels, src.fRowBytes, srcWidth, srcHeight, style);
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
2013-02-20 16:42:06 +00:00
|
|
|
(void)autoCall.detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style == kInner_Style) {
|
|
|
|
dst->fBounds = src.fBounds; // restore trimmed bounds
|
|
|
|
dst->fRowBytes = src.fRowBytes;
|
2013-01-04 20:29:03 +00:00
|
|
|
}
|
2013-01-05 02:02:05 +00:00
|
|
|
|
2013-01-04 20:29:03 +00:00
|
|
|
return true;
|
|
|
|
}
|