diff --git a/pipi/filter/blur.c b/pipi/filter/blur.c index 47af29a..8396341 100644 --- a/pipi/filter/blur.c +++ b/pipi/filter/blur.c @@ -45,7 +45,7 @@ pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, float *srcdata, *dstdata; double *kernel, *buffer; double K; - int x, y, i, w, h, kr, kw; + int x, y, i, w, h, kr, kw, gray; if(rx < BLUR_EPSILON) rx = BLUR_EPSILON; if(ry < BLUR_EPSILON) ry = BLUR_EPSILON; @@ -53,14 +53,18 @@ pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, w = src->w; h = src->h; - srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F); + gray = (src->last_modified == PIPI_PIXELS_Y_F); + + srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) + : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); srcdata = (float *)srcp->pixels; dst = pipi_new(w, h); - dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); dstdata = (float *)dstp->pixels; - buffer = malloc(w * h * 4 * sizeof(double)); + buffer = malloc(w * h * (gray ? 1 : 4) * sizeof(double)); kr = (int)(3. * rx + 1.99999); kw = 2 * kr + 1; @@ -74,26 +78,48 @@ pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, { for(x = 0; x < w; x++) { - double R = 0., G = 0., B = 0., t = 0.; - int x2, off = 4 * (y * w + x); - - for(i = -kr; i <= kr; i++) + if(gray) { - double f = kernel[i + kr]; + double Y = 0., t = 0.; + int x2; - x2 = x + i; - if(x2 < 0) x2 = 0; - else if(x2 >= w) x2 = w - 1; + for(i = -kr; i <= kr; i++) + { + double f = kernel[i + kr]; - R += f * srcdata[(y * w + x2) * 4]; - G += f * srcdata[(y * w + x2) * 4 + 1]; - B += f * srcdata[(y * w + x2) * 4 + 2]; - t += f; - } + x2 = x + i; + if(x2 < 0) x2 = 0; + else if(x2 >= w) x2 = w - 1; + + Y += f * srcdata[y * w + x2]; + t += f; + } - buffer[off] = R / t; - buffer[off + 1] = G / t; - buffer[off + 2] = B / t; + buffer[y * w + x] = Y / t; + } + else + { + double R = 0., G = 0., B = 0., t = 0.; + int x2, off = 4 * (y * w + x); + + for(i = -kr; i <= kr; i++) + { + double f = kernel[i + kr]; + + x2 = x + i; + if(x2 < 0) x2 = 0; + else if(x2 >= w) x2 = w - 1; + + R += f * srcdata[(y * w + x2) * 4]; + G += f * srcdata[(y * w + x2) * 4 + 1]; + B += f * srcdata[(y * w + x2) * 4 + 2]; + t += f; + } + + buffer[off] = R / t; + buffer[off + 1] = G / t; + buffer[off + 2] = B / t; + } } } @@ -111,26 +137,48 @@ pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, { for(x = 0; x < w; x++) { - double R = 0., G = 0., B = 0., t = 0.; - int y2, off = 4 * (y * w + x); - - for(i = -kr; i <= kr; i++) + if(gray) { - double f = kernel[i + kr]; + double Y = 0., t = 0.; + int y2; - y2 = y + i; - if(y2 < 0) y2 = 0; - else if(y2 >= h) y2 = h - 1; + for(i = -kr; i <= kr; i++) + { + double f = kernel[i + kr]; - R += f * buffer[(y2 * w + x) * 4]; - G += f * buffer[(y2 * w + x) * 4 + 1]; - B += f * buffer[(y2 * w + x) * 4 + 2]; - t += f; - } + y2 = y + i; + if(y2 < 0) y2 = 0; + else if(y2 >= h) y2 = h - 1; + + Y += f * buffer[y2 * w + x]; + t += f; + } - dstdata[off] = R / t; - dstdata[off + 1] = G / t; - dstdata[off + 2] = B / t; + dstdata[y * w + x] = Y / t; + } + else + { + double R = 0., G = 0., B = 0., t = 0.; + int y2, off = 4 * (y * w + x); + + for(i = -kr; i <= kr; i++) + { + double f = kernel[i + kr]; + + y2 = y + i; + if(y2 < 0) y2 = 0; + else if(y2 >= h) y2 = h - 1; + + R += f * buffer[(y2 * w + x) * 4]; + G += f * buffer[(y2 * w + x) * 4 + 1]; + B += f * buffer[(y2 * w + x) * 4 + 2]; + t += f; + } + + dstdata[off] = R / t; + dstdata[off + 1] = G / t; + dstdata[off + 2] = B / t; + } } }