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