Debian Codegraph @

src/utils/kernel.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <assert.h>
5 #include <math.h>
6
7 #include "compiler.h"
8 #include "kernel.h"
9 #include "misc.h"
10
11 /// Sum a region convolution kernel. Region is defined by a width x height rectangle whose
12 /// top left corner is at (x, y)
13 double sum_kernel(const conv *map, int x, int y, int width, int height) {
14 double ret = 0;
15
16 // Compute sum of values which are "in range"
17 int xstart = normalize_i_range(x, 0, map->w),
18 xend = normalize_i_range(width + x, 0, map->w);
19 int ystart = normalize_i_range(y, 0, map->h),
20 yend = normalize_i_range(height + y, 0, map->h);
21 assert(yend >= ystart && xend >= xstart);
22
23 int d = map->w;
24 if (map->rsum) {
25 // See sum_kernel_preprocess
26 double v1 = xstart ? map->rsum[(yend - 1) * d + xstart - 1] : 0;
27 double v2 = ystart ? map->rsum[(ystart - 1) * d + xend - 1] : 0;
28 double v3 = (xstart && ystart) ? map->rsum[(ystart - 1) * d + xstart - 1] : 0;
29 return map->rsum[(yend - 1) * d + xend - 1] - v1 - v2 + v3;
30 }
31
32 for (int yi = ystart; yi < yend; yi++) {
33 for (int xi = xstart; xi < xend; xi++) {
34 ret += map->data[yi * d + xi];
35 }
36 }
37
38 return ret;
39 }
40
41 double sum_kernel_normalized(const conv *map, int x, int y, int width, int height) {
42 double ret = sum_kernel(map, x, y, width, height);
43 if (ret < 0) {
44 ret = 0;
45 }
46 if (ret > 1) {
47 ret = 1;
48 }
49 return ret;
50 }
51
52 static inline double attr_const gaussian(double r, double x, double y) {
53 // Formula can be found here:
54 // https://en.wikipedia.org/wiki/Gaussian_blur#Mathematics
55 // Except a special case for r == 0 to produce sharp shadows
56 if (r == 0) {
57 return 1;
58 }
59 return exp(-0.5 * (x * x + y * y) / (r * r)) / (2 * M_PI * r * r);
60 }
61
62 conv *gaussian_kernel(double r, int size) {
63 conv *c;
64 int center = size / 2;
65 double t;
66 assert(size % 2 == 1);
67
68 c = cvalloc(sizeof(conv) + (size_t)(size * size) * sizeof(double));
69 c->w = c->h = size;
70 c->rsum = NULL;
71 t = 0.0;
72
73 for (int y = 0; y < size; y++) {
74 for (int x = 0; x < size; x++) {
75 double g = gaussian(r, x - center, y - center);
76 t += g;
77 c->data[y * size + x] = g;
78 }
79 }
80
81 for (int y = 0; y < size; y++) {
82 for (int x = 0; x < size; x++) {
83 c->data[y * size + x] /= t;
84 }
85 }
86
87 return c;
88 }
89
90 /// Estimate the element of the sum of the first row in a gaussian kernel with standard
91 /// deviation `r` and size `size`,
92 static inline double estimate_first_row_sum(double size, double r) {
93 // `factor` is integral of gaussian from -size to size
94 double factor = erf(size / r / sqrt(2));
95 // `a` is gaussian at (size, 0)
96 double a = exp(-0.5 * size * size / (r * r)) / sqrt(2 * M_PI) / r;
97 // The sum of the whole kernel is normalized to 1, i.e. each element is divided by
98 // factor squared. So the sum of the first row is a * factor / factor^2 = a /
99 // factor
100 return a / factor;
101 }
102
103 /// Pick a suitable gaussian kernel standard deviation for a given kernel size. The
104 /// returned radius is the maximum possible radius (<= size*2) that satisfies no sum of
105 /// the rows in the kernel are less than `row_limit` (up to certain precision).
106 double gaussian_kernel_std_for_size(double size, double row_limit) {
107 assert(size > 0);
108 if (row_limit >= 1.0 / 2.0 / size) {
109 return size * 2;
110 }
111 double l = 0, r = size * 2;
112 while (r - l > 1e-2) {
113 double mid = (l + r) / 2.0;
114 double vmid = estimate_first_row_sum(size, mid);
115 if (vmid > row_limit) {
116 r = mid;
117 } else {
118 l = mid;
119 }
120 }
121 return (l + r) / 2.0;
122 }
123
124 /// Create a gaussian kernel with auto detected standard deviation. The choosen standard
125 /// deviation tries to make sure the outer most pixels of the shadow are completely
126 /// transparent, so the transition from shadow to the background is smooth.
127 ///
128 /// @param[in] shadow_radius the radius of the shadow
129 conv *gaussian_kernel_autodetect_deviation(double shadow_radius) {
130 assert(shadow_radius >= 0);
131 int size = (int)(shadow_radius * 2 + 1);
132
133 if (shadow_radius == 0) {
134 return gaussian_kernel(0, size);
135 }
136 double std = gaussian_kernel_std_for_size(shadow_radius, 0.5 / 256.0);
137 return gaussian_kernel(std, size);
138 }
139
140 /// preprocess kernels to make shadow generation faster
141 /// shadow_sum[x*d+y] is the sum of the kernel from (0, 0) to (x, y), inclusive
142 void sum_kernel_preprocess(conv *map) {
143 if (map->rsum) {
144 free(map->rsum);
145 }
146
147 auto sum = map->rsum = ccalloc(map->w * map->h, double);
148 sum[0] = map->data[0];
149
150 for (int x = 1; x < map->w; x++) {
151 sum[x] = sum[x - 1] + map->data[x];
152 }
153
154 const int d = map->w;
155 for (int y = 1; y < map->h; y++) {
156 sum[y * d] = sum[(y - 1) * d] + map->data[y * d];
157 for (int x = 1; x < map->w; x++) {
158 double tmp = sum[(y - 1) * d + x] + sum[y * d + x - 1] -
159 sum[(y - 1) * d + x - 1];
160 sum[y * d + x] = tmp + map->data[y * d + x];
161 }
162 }
163 }
164
165 // vim: set noet sw=8 ts=8 :