| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include "../tp2.h"
- #define M_PI 3.14159265358979323846
- #define DEBUG 0
- /**
- * Calculo la matriz de convulsión.
- * Hago el calculo de todos los componentes.
- * PRE: Esto asume que existen "r" pixeles para cada lado del pixel actual para hacer la cuenta
- + TODO: Hay que hacer free de la matriz
- */
- float* convulcion_matrix(float sigma, int r) {
- unsigned int contador = 0;
- unsigned int largo = 2*r + 1;
- float* conv_matrix = malloc(sizeof(float)*largo*largo);
- #if DEBUG
- printf("Sigma: %f | Radio: %d \n", sigma,r);
- #endif
- // Recorro cada pixel
- for (int y = -r; y <= r; y++) {
- for (int x = -r; x <= r; x++) {
- conv_matrix[contador] = (1 / (2*M_PI*sigma*sigma)) * exp(- (pow(x,2)+pow(y,2))/(2*sigma*sigma));
- #if DEBUG
- printf("%.8f\t", conv_matrix[contador]);
- #endif
- contador++;
- }
- #if DEBUG
- printf("\n");
- #endif
- }
- return conv_matrix;
- }
- 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* mc = convulcion_matrix(sigma, r);
- 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
- }
- }
- 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);
- }
|