[2603] | 1 | /* |
---|
[2844] | 2 | * libpipi Pathetic image processing interface library |
---|
[2603] | 3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id$ |
---|
| 7 | * |
---|
| 8 | * This library is free software. It comes without any warranty, to |
---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /* |
---|
| 16 | * blur.c: blur functions |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "config.h" |
---|
| 20 | |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <math.h> |
---|
[2904] | 25 | #ifndef M_PI |
---|
| 26 | # define M_PI 3.14159265358979323846 |
---|
| 27 | #endif |
---|
[2603] | 28 | |
---|
| 29 | #include "pipi.h" |
---|
| 30 | #include "pipi_internals.h" |
---|
| 31 | |
---|
[2801] | 32 | #if !defined TEMPLATE_FILE /* This file uses the template system */ |
---|
[2802] | 33 | #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP |
---|
[2801] | 34 | #define TEMPLATE_FILE "filter/blur.c" |
---|
| 35 | #include "pipi_template.h" |
---|
| 36 | |
---|
[2619] | 37 | /* Any standard deviation below this value will be rounded up, in order |
---|
| 38 | * to avoid ridiculously low values. exp(-1/(2*0.2*0.2)) is < 10^-5 so |
---|
| 39 | * there is little chance that any value below 0.2 will be useful. */ |
---|
| 40 | #define BLUR_EPSILON 0.2 |
---|
| 41 | |
---|
[2605] | 42 | pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius) |
---|
[2603] | 43 | { |
---|
[2715] | 44 | return pipi_gaussian_blur_ext(src, radius, radius, 0.0, 0.0, 0.0); |
---|
[2617] | 45 | } |
---|
| 46 | |
---|
| 47 | pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, |
---|
[2715] | 48 | float angle, float dx, float dy) |
---|
[2617] | 49 | { |
---|
[2661] | 50 | pipi_image_t *ret; |
---|
| 51 | double *kernel; |
---|
[2715] | 52 | double Kx, Ky, t = 0.0, sint, cost, bbx, bby; |
---|
[2661] | 53 | int i, j, krx, kry, m, n; |
---|
[2603] | 54 | |
---|
[2619] | 55 | if(rx < BLUR_EPSILON) rx = BLUR_EPSILON; |
---|
| 56 | if(ry < BLUR_EPSILON) ry = BLUR_EPSILON; |
---|
| 57 | |
---|
[2715] | 58 | sint = sin(angle * M_PI / 180.); |
---|
| 59 | cost = cos(angle * M_PI / 180.); |
---|
| 60 | |
---|
| 61 | /* Compute the final ellipse's bounding box */ |
---|
| 62 | bbx = sqrt(rx * rx * cost * cost + ry * ry * sint * sint); |
---|
| 63 | bby = sqrt(ry * ry * cost * cost + rx * rx * sint * sint); |
---|
| 64 | |
---|
[2643] | 65 | /* FIXME: the kernel becomes far too big with large values of dx, because |
---|
| 66 | * we grow both left and right. Fix the growing direction. */ |
---|
[2715] | 67 | krx = (int)(3. * bbx + .99999 + ceil(abs(dx))); |
---|
[2661] | 68 | m = 2 * krx + 1; |
---|
| 69 | Kx = -1. / (2. * rx * rx); |
---|
[2603] | 70 | |
---|
[2715] | 71 | kry = (int)(3. * bby + .99999 + ceil(abs(dy))); |
---|
[2661] | 72 | n = 2 * kry + 1; |
---|
| 73 | Ky = -1. / (2. * ry * ry); |
---|
[2603] | 74 | |
---|
[2661] | 75 | kernel = malloc(m * n * sizeof(double)); |
---|
| 76 | |
---|
| 77 | for(j = -kry; j <= kry; j++) |
---|
[2603] | 78 | { |
---|
[2661] | 79 | for(i = -krx; i <= krx; i++) |
---|
[2603] | 80 | { |
---|
[2737] | 81 | /* FIXME: this level of interpolation sucks. We should |
---|
| 82 | * interpolate on the full NxN grid for better quality. */ |
---|
[2719] | 83 | static double const samples[] = |
---|
| 84 | { |
---|
| 85 | .0, .0, 1, |
---|
[2737] | 86 | -.40, -.40, 0.8, |
---|
| 87 | -.30, .0, 0.9, |
---|
| 88 | -.40, .40, 0.8, |
---|
| 89 | .0, .30, 0.9, |
---|
| 90 | .40, .40, 0.8, |
---|
| 91 | .30, .0, 0.9, |
---|
| 92 | .40, -.40, 0.8, |
---|
| 93 | .0, -.30, 0.9, |
---|
[2719] | 94 | }; |
---|
| 95 | double u, v, ex, ey, d = 0.; |
---|
[2737] | 96 | unsigned int k; |
---|
[2603] | 97 | |
---|
[2737] | 98 | for(k = 0; k < sizeof(samples) / sizeof(*samples) / 3; k++) |
---|
[2719] | 99 | { |
---|
| 100 | u = ((double)i + samples[k * 3]) * cost |
---|
| 101 | - ((double)j + samples[k * 3 + 1]) * sint + dx; |
---|
| 102 | v = ((double)i + samples[k * 3]) * sint |
---|
| 103 | + ((double)j + samples[k * 3 + 1]) * cost + dy; |
---|
| 104 | ex = Kx * u * u; |
---|
| 105 | ey = Ky * v * v; |
---|
| 106 | d += samples[k * 3 + 2] * exp(ex + ey); |
---|
[2800] | 107 | |
---|
| 108 | /* Do not interpolate if this is a standard gaussian. */ |
---|
| 109 | if(!dx && !dy && !angle) |
---|
| 110 | break; |
---|
[2719] | 111 | } |
---|
| 112 | |
---|
[2661] | 113 | kernel[(j + kry) * m + i + krx] = d; |
---|
| 114 | t += d; |
---|
[2608] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[2661] | 118 | for(j = 0; j < n; j++) |
---|
| 119 | for(i = 0; i < m; i++) |
---|
| 120 | kernel[j * m + i] /= t; |
---|
[2617] | 121 | |
---|
[2661] | 122 | ret = pipi_convolution(src, m, n, kernel); |
---|
[2617] | 123 | |
---|
[2608] | 124 | free(kernel); |
---|
| 125 | |
---|
[2661] | 126 | return ret; |
---|
[2603] | 127 | } |
---|
| 128 | |
---|
[2681] | 129 | pipi_image_t *pipi_box_blur(pipi_image_t *src, int size) |
---|
| 130 | { |
---|
| 131 | return pipi_box_blur_ext(src, size, size); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n) |
---|
| 135 | { |
---|
[2802] | 136 | if(src->wrap) |
---|
| 137 | { |
---|
[3342] | 138 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
[2802] | 139 | return boxblur_gray_wrap(src, m, n); |
---|
[2801] | 140 | |
---|
[2802] | 141 | return boxblur_wrap(src, m, n); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
[3342] | 145 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
[2802] | 146 | return boxblur_gray(src, m, n); |
---|
| 147 | |
---|
| 148 | return boxblur(src, m, n); |
---|
| 149 | } |
---|
[2801] | 150 | } |
---|
| 151 | |
---|
| 152 | #else /* XXX: the following functions use the template system */ |
---|
| 153 | |
---|
[2806] | 154 | static pipi_image_t *T(boxblur)(pipi_image_t *src, int m, int n) |
---|
[2801] | 155 | { |
---|
[2743] | 156 | pipi_image_t *dst; |
---|
| 157 | pipi_pixels_t *srcp, *dstp; |
---|
| 158 | float *srcdata, *dstdata; |
---|
| 159 | double *acc; |
---|
[2802] | 160 | int x, y, w, h, i, j, i2, j2, size; |
---|
[2681] | 161 | |
---|
[2743] | 162 | w = src->w; |
---|
| 163 | h = src->h; |
---|
| 164 | size = (2 * m + 1) * (2 * n + 1); |
---|
[2681] | 165 | |
---|
[3342] | 166 | srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
| 167 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
[2743] | 168 | srcdata = (float *)srcp->pixels; |
---|
[2681] | 169 | |
---|
[2743] | 170 | dst = pipi_new(w, h); |
---|
[3342] | 171 | dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
| 172 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
[2743] | 173 | dstdata = (float *)dstp->pixels; |
---|
| 174 | |
---|
[2801] | 175 | acc = malloc(w * (FLAG_GRAY ? 1 : 4) * sizeof(double)); |
---|
[2743] | 176 | |
---|
| 177 | /* Step 1: fill the accumulator */ |
---|
| 178 | for(x = 0; x < w; x++) |
---|
| 179 | { |
---|
[2801] | 180 | double r = 0., g = 0., b = 0., a = 0.; |
---|
| 181 | double t = 0.; |
---|
| 182 | |
---|
| 183 | for(j = -n; j <= n; j++) |
---|
[2743] | 184 | { |
---|
[2802] | 185 | if(FLAG_WRAP) |
---|
| 186 | j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h; |
---|
| 187 | else |
---|
| 188 | j2 = (j < 0) ? 0 : (j >= h) ? h - 1 : j; |
---|
| 189 | |
---|
[2801] | 190 | if(FLAG_GRAY) |
---|
[2743] | 191 | t += srcdata[j2 * w + x]; |
---|
[2801] | 192 | else |
---|
[2743] | 193 | { |
---|
| 194 | r += srcdata[4 * (j2 * w + x)]; |
---|
| 195 | g += srcdata[4 * (j2 * w + x) + 1]; |
---|
| 196 | b += srcdata[4 * (j2 * w + x) + 2]; |
---|
| 197 | a += srcdata[4 * (j2 * w + x) + 3]; |
---|
| 198 | } |
---|
[2801] | 199 | } |
---|
[2743] | 200 | |
---|
[2801] | 201 | if(FLAG_GRAY) |
---|
| 202 | acc[x] = t; |
---|
| 203 | else |
---|
| 204 | { |
---|
[2743] | 205 | acc[4 * x] = r; |
---|
| 206 | acc[4 * x + 1] = g; |
---|
| 207 | acc[4 * x + 2] = b; |
---|
| 208 | acc[4 * x + 3] = a; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* Step 2: blur the image, line by line */ |
---|
| 213 | for(y = 0; y < h; y++) |
---|
| 214 | { |
---|
| 215 | double r = 0., g = 0., b = 0., a = 0.; |
---|
| 216 | double t = 0.; |
---|
| 217 | |
---|
| 218 | /* 2.1: compute the first pixel */ |
---|
[2801] | 219 | for(i = -m; i <= m; i++) |
---|
[2743] | 220 | { |
---|
[2802] | 221 | if(FLAG_WRAP) |
---|
| 222 | i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w; |
---|
| 223 | else |
---|
| 224 | i2 = (i < 0) ? 0 : (i >= w) ? w - 1 : i; |
---|
[2801] | 225 | |
---|
| 226 | if(FLAG_GRAY) |
---|
[2743] | 227 | t += acc[i2]; |
---|
[2801] | 228 | else |
---|
[2743] | 229 | { |
---|
| 230 | r += acc[4 * i2]; |
---|
| 231 | g += acc[4 * i2 + 1]; |
---|
| 232 | b += acc[4 * i2 + 2]; |
---|
| 233 | a += acc[4 * i2 + 3]; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /* 2.2: iterate on the whole line */ |
---|
| 238 | for(x = 0; x < w; x++) |
---|
| 239 | { |
---|
| 240 | int u, u2, v, v2; |
---|
| 241 | |
---|
[2801] | 242 | if(FLAG_GRAY) |
---|
[2743] | 243 | { |
---|
| 244 | dstdata[y * w + x] = t / size; |
---|
| 245 | } |
---|
| 246 | else |
---|
| 247 | { |
---|
| 248 | dstdata[4 * (y * w + x)] = r / size; |
---|
| 249 | dstdata[4 * (y * w + x) + 1] = g / size; |
---|
| 250 | dstdata[4 * (y * w + x) + 2] = b / size; |
---|
| 251 | dstdata[4 * (y * w + x) + 3] = a / size; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | u = x - m; |
---|
[2802] | 255 | if(FLAG_WRAP) |
---|
| 256 | u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w; |
---|
| 257 | else |
---|
| 258 | u2 = (u < 0) ? 0 : (u >= w) ? w - 1 : u; |
---|
[2743] | 259 | v = x + m + 1; |
---|
[2802] | 260 | if(FLAG_WRAP) |
---|
| 261 | v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w; |
---|
| 262 | else |
---|
| 263 | v2 = (v < 0) ? 0 : (v >= w) ? w - 1 : v; |
---|
[2801] | 264 | if(FLAG_GRAY) |
---|
[2743] | 265 | { |
---|
| 266 | t = t - acc[u2] + acc[v2]; |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | r = r - acc[4 * u2] + acc[4 * v2]; |
---|
| 271 | g = g - acc[4 * u2 + 1] + acc[4 * v2 + 1]; |
---|
| 272 | b = b - acc[4 * u2 + 2] + acc[4 * v2 + 2]; |
---|
| 273 | a = a - acc[4 * u2 + 3] + acc[4 * v2 + 3]; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /* 2.3: update the accumulator */ |
---|
| 278 | for(x = 0; x < w; x++) |
---|
| 279 | { |
---|
| 280 | int u, u2, v, v2; |
---|
| 281 | |
---|
| 282 | u = y - n; |
---|
[2802] | 283 | if(FLAG_WRAP) |
---|
[3402] | 284 | u2 = (u < 0) ? h - 1 - ((-u - 1) % h) : u % h; |
---|
[2802] | 285 | else |
---|
[3402] | 286 | u2 = (u < 0) ? 0 : (u >= h) ? h - 1 : u; |
---|
[2743] | 287 | v = y + n + 1; |
---|
[2802] | 288 | if(FLAG_WRAP) |
---|
[3402] | 289 | v2 = (v < 0) ? h - 1 - ((-v - 1) % h) : v % h; |
---|
[2802] | 290 | else |
---|
[3402] | 291 | v2 = (v < 0) ? 0 : (v >= h) ? h - 1 : v; |
---|
[2801] | 292 | if(FLAG_GRAY) |
---|
[2743] | 293 | { |
---|
| 294 | acc[x] += srcdata[v2 * w + x] - srcdata[u2 * w + x]; |
---|
| 295 | } |
---|
| 296 | else |
---|
| 297 | { |
---|
| 298 | int uoff = 4 * (u2 * w + x); |
---|
| 299 | int voff = 4 * (v2 * w + x); |
---|
| 300 | |
---|
| 301 | acc[4 * x] += srcdata[voff] - srcdata[uoff]; |
---|
| 302 | acc[4 * x + 1] += srcdata[voff + 1] - srcdata[uoff + 1]; |
---|
| 303 | acc[4 * x + 2] += srcdata[voff + 2] - srcdata[uoff + 2]; |
---|
| 304 | acc[4 * x + 3] += srcdata[voff + 3] - srcdata[uoff + 3]; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | free(acc); |
---|
| 310 | |
---|
| 311 | return dst; |
---|
[2681] | 312 | } |
---|
| 313 | |
---|
[2801] | 314 | #endif |
---|
| 315 | |
---|