blur_c.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. 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. printf("r: %d, plsgiv %d\n", r, sizeof(float)*r*r);
  21. unsigned int indice_de_mierda=0;
  22. for(y=-r;y<r;y++) {
  23. for(x=-r;x<r;x++){
  24. val=(1/(2*M_PI*sigma*sigma) * exp(- ((x*x)+(y*y))/(2*sigma*sigma)));
  25. mc[indice_de_mierda]= val;
  26. indice_de_mierda++;
  27. }
  28. }
  29. for(i=r;i<filas-r-1;i++){ //
  30. for(j=r;j<cols-r;j++) { //ignoro los bordes de tamaño R
  31. tmp[0]=0;
  32. tmp[1]=0;
  33. tmp[2]=0;
  34. for(y=0;y<2*r;y++){
  35. for(x=0;x<2*r;x++){
  36. tmp[0]+=src_matrix[i+y][4*(x+j)+0]*mc[x+(r*y*2)];
  37. tmp[1]+=src_matrix[i+y][4*(x+j)+1]*mc[x+(r*y*2)];
  38. tmp[2]+=src_matrix[i+y][4*(x+j)+2]*mc[x+(r*y*2)];
  39. }
  40. }
  41. printf("%f\n",tmp[0]);
  42. dst_matrix[i][4*j+0]=(int)tmp[0];
  43. dst_matrix[i][4*j+1]=(int)tmp[1];
  44. dst_matrix[i][4*j+2]=(int)tmp[2];
  45. //printf("%d %d\n",i,4*j);
  46. }
  47. }
  48. free(mc);
  49. }