blur_c.c 1.1 KB

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