From 7ce6b2ad6ba8ebc50bc95ead830b8ae897f4cda0 Mon Sep 17 00:00:00 2001 From: pc9ha2xvl <2119910569@qq.com> Date: Sat, 7 Dec 2024 01:54:28 +0800 Subject: [PATCH] ADD file via upload --- step2.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 step2.c diff --git a/step2.c b/step2.c new file mode 100644 index 0000000..30ebd0f --- /dev/null +++ b/step2.c @@ -0,0 +1,67 @@ +#include +#include +#include +#define BUF_SIZE 3 +#define WRAPPED(i) ((i) % BUF_SIZE) + +void applySeparableGaussianBlur(float src[5][5], float dst[5][5], int h, int w, float kx[3], float ky[3]) { + float buf[BUF_SIZE][5]; + + + int i; + for (i = 1; i < h-1; i++) { + int bufIndex = WRAPPED(i); + int j; + for (j = 1; j < w-1; j++) { + buf[bufIndex][j] = kx[0] * (src[i-1][j] + src[i+1][j]) + + kx[1] * (src[i][j-1] + src[i][j+1]) + + kx[2] * (src[i-1][j-1] + src[i-1][j+1] + src[i+1][j-1] + src[i+1][j+1]); + } + + buf[bufIndex][0] = buf[bufIndex][1]; + buf[bufIndex][w-1] = buf[bufIndex][w-2]; + } + + + int j; + for ( i = 1; i < h-1; i++) { + for ( j = 1; j < w-1; j++) { + dst[i][j] = ky[0] * (buf[WRAPPED(i-1)][j] + buf[WRAPPED(i+1)][j]) + + ky[1] * (buf[WRAPPED(i)][j-1] + buf[WRAPPED(i)][j+1]) + + ky[2] * (buf[WRAPPED(i-1)][j-1] + buf[WRAPPED(i-1)][j+1] + + buf[WRAPPED(i+1)][j-1] + buf[WRAPPED(i+1)][j+1]); + } + } +} + +int main() { + float src[5][5] = { + {0, 0, 0, 0, 0}, + {0, 1, 1, 1, 0}, + {0, 1, 50, 1, 0}, + {0, 1, 1, 1, 0}, + {0, 0, 0, 0, 0} + }; + float dst[5][5] = {0}; + float kx[3] = {1/16, 2/16, 1/16}; + float ky[3] = {1, 4, 1}; + + clock_t start = clock(); + + applySeparableGaussianBlur(src, dst, 5, 5, kx, ky); + + clock_t end = clock(); + double time_spent = (double)(end - start) / CLOCKS_PER_SEC; + + printf("Result Matrix:\n"); + int i,j; + for (i = 1; i < 4; i++) { + for (j = 1; j < 4; j++) { + printf("%.2f ", dst[i][j]); + } + printf("\n"); + } + printf("Time spent: %lf seconds\n", time_spent); + + return 0; +}