瀏覽代碼

Merge branch 'master' of ssh://git.davidventura.com.ar:443/david/orga2-simd

David 11 年之前
父節點
當前提交
e5e4185497
共有 1 個文件被更改,包括 23 次插入38 次删除
  1. 23 38
      filtros/blur_c.c

+ 23 - 38
filtros/blur_c.c

@@ -4,14 +4,6 @@
 #define M_PI 3.14159265358979323846
 #include "../tp2.h"
 
-/**
-* TODO: Sacar de acá y poner adentro de convulción. Lo separe para poder hacer "debagear" mas facil.
-*/
-float gauss(float sigma, int x, int y) {
-//	printf("2*M_PI*sigma*sigma: %f | 1 / (2*M_PI*sigma*sigma): %f | (x*x)+(y*y): %f | - (x*x + y*y)/2*sigma*sigma | exp(...): %f\n", 2*M_PI*sigma*sigma, 1 / (2*M_PI*sigma*sigma), pow(x,2)+pow(y,2), - ((x*x)+(y*y))/(2*sigma*sigma), (1 / (2*M_PI*sigma*sigma)) * exp(- ((x*x)+(y*y))/(2*sigma*sigma)));
-	return (1 / (2*M_PI*sigma*sigma)) * exp(- (pow(x,2)+pow(y,2))/(2*sigma*sigma));
-}
-
 /**
 *	Calculo la matriz de convulsión.
 *	Hago el calculo de todos los componentes.
@@ -27,8 +19,8 @@ float* convulcion_matrix(float sigma, int r) {
 	for (int y = -r; y <= r; y++) {
 		for (int x = -r; x <= r; x++) {
 			// Por cada pixel obtengo la sub-matriz de convulsion para cada componente
-			printf("Sigma: %f | Radio: %d | X: %d | Y: %d | Gauss: %f \n", sigma, r, x, y, gauss(sigma, x, y));
-			conv_matrix[contador] = gauss(sigma, x, y); // B
+			//printf("Sigma: %f | Radio: %d | X: %d | Y: %d | Gauss: %f \n", sigma, r, x, y, (1 / (2*M_PI*sigma*sigma)) * exp(- (pow(x,2)+pow(y,2))/(2*sigma*sigma)));
+			conv_matrix[contador] = (1 / (2*M_PI*sigma*sigma)) * exp(- (pow(x,2)+pow(y,2))/(2*sigma*sigma));
 			contador++;
 		}
 	}
@@ -39,39 +31,32 @@ float* convulcion_matrix(float sigma, int r) {
 void blur_c(unsigned char *src, unsigned char *dst, int cols, int filas, float sigma, int r) {
     unsigned char (*src_matrix)[cols*4] = (unsigned char (*)[cols*4]) src;
     unsigned char (*dst_matrix)[cols*4] = (unsigned char (*)[cols*4]) dst;
-
-	float tmp[3];
-    int i,j=0,x,y;
 	float* mc = convulcion_matrix(sigma, r);
-
-	for(i=r;i<filas-r;i++) {
-		for(j=r;j<cols-r;j++) { // Ignoro los bordes de tamaño R
-			tmp[0]=0;
-			tmp[1]=0;
-			tmp[2]=0;
-			int matPos=0;
-			for(y=-r;y<=r;y++){
-				for(x=-r;x<=r;x++){
-					int img=src_matrix[i+y][4*(x+j)+0];
-					float mat=mc[matPos];
-					float val= img * mat;
-//					tmp[0]+=src_matrix[i+y][4*(x+j)+0]*mc[x+(r*y*2)];
-					tmp[0]+=src_matrix[i+y][4*(x+j)+0] * mat;
-					tmp[1]+=src_matrix[i+y][4*(x+j)+1] * mat;
-					tmp[2]+=src_matrix[i+y][4*(x+j)+2] * mat;
-//					printf("IMG: %d | MATPOS: %d | MATVAL: %f| VAL: %f | TMP[0] %f\n", img, matPos, mat, val, tmp[0]);
-					matPos++;
+	float tmp[3];
+    int i, j, x, y, matPos;
+
+	// Recorro la imagen, ignoro los bordes de tamaño R
+	for(i=r; i<filas-r; i++) {
+		for(j=r; j<cols-r; j++) {
+			tmp[0]=0; tmp[1]=0;	tmp[2]=0; matPos = 0;
+
+			// Recorro la submatriz que le corresponde al pixel actual y hago las multiplicaciones
+			for(y=-r; y<=r; y++) {
+				for(x=-r; x<=r; x++) {
+					tmp[0]+=src_matrix[i+y][4*(x+j)+0] * mc[matPos];
+					tmp[1]+=src_matrix[i+y][4*(x+j)+1] * mc[matPos];
+					tmp[2]+=src_matrix[i+y][4*(x+j)+2] * mc[matPos];
+					matPos++;	// Aumento una posición en la matriz de convulsion
 				}
 			}
-//			exit(0);
-//			printf("%f\n",tmp[0]);
-			dst_matrix[i][4*j+0]=(int)tmp[0];
-			dst_matrix[i][4*j+1]=(int)tmp[1];
-			dst_matrix[i][4*j+2]=(int)tmp[2];
-			//printf("%d %d\n",i,4*j);
+
+			dst_matrix[i][4*j+0]=(int)tmp[0];	// B
+			dst_matrix[i][4*j+1]=(int)tmp[1];	// G
+			dst_matrix[i][4*j+2]=(int)tmp[2];	// R
+			dst_matrix[i][4*j+3]=255;			// A
 		}
 	}
-
+	// Borro la matriz de convulsion
 	free(mc);
 }