2018-12-15 16:11:41 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
|
|
|
|
|
2019-01-01 06:35:59 -05:00
|
|
|
#include <assert.h>
|
2018-12-15 16:11:41 -05:00
|
|
|
#include <math.h>
|
|
|
|
|
2019-01-20 11:53:39 -05:00
|
|
|
#include "compiler.h"
|
2018-12-15 16:11:41 -05:00
|
|
|
#include "kernel.h"
|
2019-04-18 19:11:54 -04:00
|
|
|
#include "log.h"
|
2018-12-19 15:50:02 -05:00
|
|
|
#include "utils.h"
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2018-12-31 19:15:51 -05:00
|
|
|
/// Sum a region convolution kernel. Region is defined by a width x height rectangle whose
|
|
|
|
/// top left corner is at (x, y)
|
|
|
|
double sum_kernel(const conv *map, int x, int y, int width, int height) {
|
|
|
|
double ret = 0;
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-02-17 16:54:35 -05:00
|
|
|
// Compute sum of values which are "in range"
|
2019-03-30 05:07:21 -04:00
|
|
|
int xstart = normalize_i_range(x, 0, map->w),
|
|
|
|
xend = normalize_i_range(width + x, 0, map->w);
|
|
|
|
int ystart = normalize_i_range(y, 0, map->h),
|
|
|
|
yend = normalize_i_range(height + y, 0, map->h);
|
2019-02-17 16:54:35 -05:00
|
|
|
assert(yend >= ystart && xend >= xstart);
|
2019-01-01 06:35:59 -05:00
|
|
|
|
2019-02-17 16:54:35 -05:00
|
|
|
int d = map->w;
|
2019-01-01 06:35:59 -05:00
|
|
|
if (map->rsum) {
|
2019-02-17 16:54:35 -05:00
|
|
|
// See sum_kernel_preprocess
|
2019-01-01 06:35:59 -05:00
|
|
|
double v1 = xstart ? map->rsum[(yend - 1) * d + xstart - 1] : 0;
|
|
|
|
double v2 = ystart ? map->rsum[(ystart - 1) * d + xend - 1] : 0;
|
2019-02-17 16:54:35 -05:00
|
|
|
double v3 = (xstart && ystart) ? map->rsum[(ystart - 1) * d + xstart - 1] : 0;
|
2019-01-01 06:35:59 -05:00
|
|
|
return map->rsum[(yend - 1) * d + xend - 1] - v1 - v2 + v3;
|
|
|
|
}
|
|
|
|
|
2018-12-31 19:15:51 -05:00
|
|
|
for (int yi = ystart; yi < yend; yi++) {
|
|
|
|
for (int xi = xstart; xi < xend; xi++) {
|
2019-01-01 06:35:59 -05:00
|
|
|
ret += map->data[yi * d + xi];
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 19:15:51 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2018-12-31 19:15:51 -05:00
|
|
|
double sum_kernel_normalized(const conv *map, int x, int y, int width, int height) {
|
|
|
|
double ret = sum_kernel(map, x, y, width, height);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
if (ret > 1) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
return ret;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
2019-04-18 19:11:54 -04:00
|
|
|
static inline double attr_const gaussian(double r, double x, double y) {
|
2018-12-15 16:11:41 -05:00
|
|
|
// Formula can be found here:
|
|
|
|
// https://en.wikipedia.org/wiki/Gaussian_blur#Mathematics
|
|
|
|
// Except a special case for r == 0 to produce sharp shadows
|
|
|
|
if (r == 0)
|
|
|
|
return 1;
|
|
|
|
return exp(-0.5 * (x * x + y * y) / (r * r)) / (2 * M_PI * r * r);
|
|
|
|
}
|
|
|
|
|
2019-04-18 19:11:54 -04:00
|
|
|
conv *gaussian_kernel(double r, int size) {
|
2018-12-15 16:11:41 -05:00
|
|
|
conv *c;
|
|
|
|
int center = size / 2;
|
|
|
|
double t;
|
2019-04-18 19:11:54 -04:00
|
|
|
assert(size % 2 == 1);
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
c = cvalloc(sizeof(conv) + (size_t)(size * size) * sizeof(double));
|
2019-02-17 16:54:35 -05:00
|
|
|
c->w = c->h = size;
|
2019-01-01 06:35:59 -05:00
|
|
|
c->rsum = NULL;
|
2018-12-15 16:11:41 -05:00
|
|
|
t = 0.0;
|
|
|
|
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
double g = gaussian(r, x - center, y - center);
|
|
|
|
t += g;
|
|
|
|
c->data[y * size + x] = g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
c->data[y * size + x] /= t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-04-18 19:11:54 -04:00
|
|
|
/// Estimate the element of the sum of the first row in a gaussian kernel with standard
|
|
|
|
/// deviation `r` and size `size`,
|
|
|
|
static inline double estimate_first_row_sum(double size, double r) {
|
|
|
|
double factor = erf(size / r / sqrt(2));
|
|
|
|
double a = exp(-0.5 * size * size / (r * r)) / sqrt(2 * M_PI) / r;
|
|
|
|
return a / factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pick a suitable gaussian kernel radius for a given kernel size. The returned radius
|
|
|
|
/// is the maximum possible radius (<= size*2) that satisfies no sum of the rows in
|
|
|
|
/// the kernel are less than `row_limit` (up to certain precision).
|
|
|
|
static inline double gaussian_kernel_std_for_size(int size, double row_limit) {
|
|
|
|
assert(size > 0);
|
|
|
|
if (row_limit >= 1.0 / 2.0 / size) {
|
|
|
|
return size * 2;
|
|
|
|
}
|
|
|
|
double l = 0, r = size * 2;
|
|
|
|
while (r - l > 1e-2) {
|
|
|
|
double mid = (l + r) / 2.0;
|
|
|
|
double vmid = estimate_first_row_sum(size, mid);
|
|
|
|
if (vmid > row_limit) {
|
|
|
|
r = mid;
|
|
|
|
} else {
|
|
|
|
l = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (l + r) / 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a gaussian kernel with auto detected standard deviation. The choosen standard
|
|
|
|
/// deviation tries to make sure the outer most pixels of the shadow are completely
|
|
|
|
/// transparent, so the transition from shadow to the background is smooth.
|
|
|
|
///
|
|
|
|
/// @param[in] shadow_radius the radius of the shadow
|
|
|
|
conv *gaussian_kernel_autodetect_deviation(int shadow_radius) {
|
|
|
|
assert(shadow_radius >= 0);
|
|
|
|
int size = shadow_radius * 2 + 1;
|
|
|
|
|
|
|
|
if (shadow_radius == 0) {
|
|
|
|
return gaussian_kernel(0, size);
|
|
|
|
}
|
|
|
|
double std = gaussian_kernel_std_for_size(shadow_radius, 1.0 / 256.0);
|
|
|
|
return gaussian_kernel(std, size);
|
|
|
|
}
|
|
|
|
|
2018-12-31 19:15:51 -05:00
|
|
|
/// preprocess kernels to make shadow generation faster
|
|
|
|
/// shadow_sum[x*d+y] is the sum of the kernel from (0, 0) to (x, y), inclusive
|
2019-02-17 16:54:35 -05:00
|
|
|
void sum_kernel_preprocess(conv *map) {
|
|
|
|
if (map->rsum) {
|
2019-01-01 06:35:59 -05:00
|
|
|
free(map->rsum);
|
2019-02-17 16:54:35 -05:00
|
|
|
}
|
2018-12-31 19:15:51 -05:00
|
|
|
|
2019-02-17 16:54:35 -05:00
|
|
|
auto sum = map->rsum = ccalloc(map->w * map->h, double);
|
2018-12-31 19:15:51 -05:00
|
|
|
sum[0] = map->data[0];
|
|
|
|
|
2019-02-17 16:54:35 -05:00
|
|
|
for (int x = 1; x < map->w; x++) {
|
2018-12-31 19:15:51 -05:00
|
|
|
sum[x] = sum[x - 1] + map->data[x];
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:54:35 -05:00
|
|
|
const int d = map->w;
|
|
|
|
for (int y = 1; y < map->h; y++) {
|
2018-12-31 19:15:51 -05:00
|
|
|
sum[y * d] = sum[(y - 1) * d] + map->data[y * d];
|
2019-02-17 16:54:35 -05:00
|
|
|
for (int x = 1; x < map->w; x++) {
|
2018-12-31 19:15:51 -05:00
|
|
|
double tmp = sum[(y - 1) * d + x] + sum[y * d + x - 1] -
|
|
|
|
sum[(y - 1) * d + x - 1];
|
|
|
|
sum[y * d + x] = tmp + map->data[y * d + x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 16:11:41 -05:00
|
|
|
// vim: set noet sw=8 ts=8 :
|