From daa741e235b05d627cb5f9a2e14391bf96a5574d Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 29 Aug 2008 07:48:30 +0000 Subject: [PATCH] * convolution.c: refactor the template functions, removing 30 lines of code in the process. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2804 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/filter/convolution.c | 141 +++++++++++++++----------------------- 1 file changed, 55 insertions(+), 86 deletions(-) diff --git a/pipi/filter/convolution.c b/pipi/filter/convolution.c index 55376ec..0c5ef18 100644 --- a/pipi/filter/convolution.c +++ b/pipi/filter/convolution.c @@ -136,54 +136,39 @@ static pipi_image_t *SUFFIX(conv)(pipi_image_t *src, { for(x = 0; x < w; x++) { - if(FLAG_GRAY) - { - double Y = 0.; - int x2, y2; - - for(j = 0; j < n; j++) - { - y2 = y + j - n / 2; - if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; - else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; - - for(i = 0; i < m; i++) - { - x2 = x + i - m / 2; - if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; - else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; - - Y += mat[j * m + i] * srcdata[y2 * w + x2]; - } - } + double R = 0., G = 0., B = 0.; + double Y = 0.; + int x2, y2, off = 4 * (y * w + x); - dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; - } - else + for(j = 0; j < n; j++) { - double R = 0., G = 0., B = 0.; - int x2, y2, off = 4 * (y * w + x); + y2 = y + j - n / 2; + if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; + else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; - for(j = 0; j < n; j++) + for(i = 0; i < m; i++) { - y2 = y + j - n / 2; - if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; - else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; + double f = mat[j * m + i]; - for(i = 0; i < m; i++) - { - double f = mat[j * m + i]; - - x2 = x + i - m / 2; - if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; - else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; + x2 = x + i - m / 2; + if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; + else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; + if(FLAG_GRAY) + Y += f * srcdata[y2 * w + x2]; + else + { R += f * srcdata[(y2 * w + x2) * 4]; G += f * srcdata[(y2 * w + x2) * 4 + 1]; B += f * srcdata[(y2 * w + x2) * 4 + 2]; } } + } + if(FLAG_GRAY) + dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; + else + { dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R; dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G; dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B; @@ -221,40 +206,32 @@ static pipi_image_t *SUFFIX(sepconv)(pipi_image_t *src, { for(x = 0; x < w; x++) { - if(FLAG_GRAY) - { - double Y = 0.; - int x2; - - for(i = 0; i < m; i++) - { - x2 = x + i - m / 2; - if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; - else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; - - Y += hvec[i] * srcdata[y * w + x2]; - } + double R = 0., G = 0., B = 0.; + double Y = 0.; + int x2, off = 4 * (y * w + x); - buffer[y * w + x] = Y; - } - else + for(i = 0; i < m; i++) { - double R = 0., G = 0., B = 0.; - int x2, off = 4 * (y * w + x); + double f = hvec[i]; - for(i = 0; i < m; i++) - { - double f = hvec[i]; - - x2 = x + i - m / 2; - if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; - else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; + x2 = x + i - m / 2; + if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; + else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; + if(FLAG_GRAY) + Y += f * srcdata[y * w + x2]; + else + { R += f * srcdata[(y * w + x2) * 4]; G += f * srcdata[(y * w + x2) * 4 + 1]; B += f * srcdata[(y * w + x2) * 4 + 2]; } + } + if(FLAG_GRAY) + buffer[y * w + x] = Y; + else + { buffer[off] = R; buffer[off + 1] = G; buffer[off + 2] = B; @@ -266,40 +243,32 @@ static pipi_image_t *SUFFIX(sepconv)(pipi_image_t *src, { for(x = 0; x < w; x++) { - if(FLAG_GRAY) - { - double Y = 0.; - int y2; - - for(j = 0; j < n; j++) - { - y2 = y + j - n / 2; - if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; - else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; - - Y += vvec[j] * buffer[y2 * w + x]; - } + double R = 0., G = 0., B = 0.; + double Y = 0.; + int y2, off = 4 * (y * w + x); - dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; - } - else + for(j = 0; j < n; j++) { - double R = 0., G = 0., B = 0.; - int y2, off = 4 * (y * w + x); + double f = vvec[j]; - for(j = 0; j < n; j++) - { - double f = vvec[j]; - - y2 = y + j - n / 2; - if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; - else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; + y2 = y + j - n / 2; + if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; + else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; + if(FLAG_GRAY) + Y += f * buffer[y2 * w + x]; + else + { R += f * buffer[(y2 * w + x) * 4]; G += f * buffer[(y2 * w + x) * 4 + 1]; B += f * buffer[(y2 * w + x) * 4 + 2]; } + } + if(FLAG_GRAY) + dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; + else + { dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R; dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G; dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;