blur_c.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define M_PI 3.14159265358979323846
  5. #include "../tp2.h"
  6. void blur_c (
  7. unsigned char *src,
  8. unsigned char *dst,
  9. int cols,
  10. int filas,
  11. float sigma,
  12. unsigned int r)
  13. {
  14. unsigned char (*src_matrix)[cols*4] = (unsigned char (*)[cols*4]) src;
  15. unsigned char (*dst_matrix)[cols*4] = (unsigned char (*)[cols*4]) dst;
  16. unsigned int i,j=0,x,y;
  17. float tmp[3];
  18. float val;
  19. float *mc=malloc(sizeof(float)*40*r*r);
  20. unsigned int indice_de_mierda=0;
  21. for(y=-r;y<r;y++) {
  22. for(x=-r;x<r;x++){
  23. val=(1/(2*M_PI*sigma*sigma) * exp(- ((x*x)+(y*y))/(2*sigma*sigma)));
  24. mc[indice_de_mierda]= val;
  25. indice_de_mierda++;
  26. }
  27. }
  28. for(i=r;i<filas-r-1;i++){ //
  29. for(j=r;j<cols-r;j++) { //ignoro los bordes de tamaño R
  30. tmp[0]=0;
  31. tmp[1]=0;
  32. tmp[2]=0;
  33. for(y=0;y<2*r;y++){
  34. for(x=0;x<2*r;x++){
  35. tmp[0]+=src_matrix[i+y][4*(x+j)+0]*mc[x+(r*y*2)];
  36. tmp[1]+=src_matrix[i+y][4*(x+j)+1]*mc[x+(r*y*2)];
  37. tmp[2]+=src_matrix[i+y][4*(x+j)+2]*mc[x+(r*y*2)];
  38. }
  39. }
  40. printf("%f\n",tmp[0]);
  41. dst_matrix[i][4*j+0]=(int)tmp[0];
  42. dst_matrix[i][4*j+1]=(int)tmp[1];
  43. dst_matrix[i][4*j+2]=(int)tmp[2];
  44. //printf("%d %d\n",i,4*j);
  45. }
  46. }
  47. free(mc);
  48. }