Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1.  
  2. Image_t* filter2D_Parallel_1(Image_t* im, float* mask, int numMaxThreads)
  3. {
  4. //Crear imagen resultado
  5.  
  6. Image_t* imOut = new Image_t;
  7. vector<thread> threads;
  8. (*imOut) = (*im); //Por qué entre paréntesis?
  9. imOut->row_pointers = (char**)malloc(sizeof(char*)*im->height);
  10. for (int i = 0; i < im->height; i++) {
  11. imOut->row_pointers[i] = (char*)malloc(sizeof(char)*im->width * 4);
  12. memset(imOut->row_pointers[i], 0, sizeof(char)*im->width * 4);
  13. }
  14. //Por cada pixel de “im”
  15. //Mirar número máximo de hilos
  16. //Calcular bucles. -> Si 40000 funciona no es necesario cambiar gran cosa
  17. //Llamada a thread.
  18. for (int i = 1; i < (im->height - 1); i++)
  19. {
  20. for (int j = 1; j < (im->width - 1); j++)
  21. {
  22. threads.push_back(thread(filter2D_Thread1, im, imOut, mask, j, i));
  23. //cout << "Pasando el hilo i= " << i << " j = " << j << "\n";
  24. }
  25. }
  26.  
  27. for (auto& th : threads)th.join();
  28.  
  29. //Esperar numMaxThreads threads
  30. //Avanzar numMaxThreads posiciones en “im”
  31. //Return imagen resultado
  32. return imOut;
  33. }
  34.  
  35. void filter2D_Thread1(Image_t* im, Image_t* result, float* mask, int j, int i)
  36. {
  37. //Designar el pixel que me toca calcular
  38. //Pixel que calcular =
  39. pixel32bpp pixlIn;
  40. //pixlIn = *((pixel32bpp*)(&(im->row_pointers[i][(j) * 4]))); //Posible que posWidth y posHeight estén en orden inverso
  41.  
  42. float a, r, g, b;
  43. a = r = g = b = 0;
  44.  
  45.  
  46. //Dar valor a dicho pixel en la imagen im
  47. //meter en thread el bucle que vemos en filtro2d
  48. //Aplicar el cálculo visto en filtro_2d para un sólo píxel.
  49. //for (int i = 1; i < (im->height - 1); i++)
  50. //for (int j = 1; j < (im->width - 1); j++)
  51. {
  52. for (int y = -1; y < 2; y++)
  53. for (int x = -1; x < 2; x++)
  54. { //Es posible que i y j estén al revés
  55. pixlIn = *((pixel32bpp*)(&(im->row_pointers[i + y][(j + x) * 4])));
  56. a += ((float)pixlIn.a)*mask[(y + 1) * 3 + x + 1];
  57. r += ((float)pixlIn.r)*mask[(y + 1) * 3 + x + 1];
  58. g += ((float)pixlIn.g)*mask[(y + 1) * 3 + x + 1];
  59. b += ((float)pixlIn.b)*mask[(y + 1) * 3 + x + 1];
  60. }
  61. result->row_pointers[i][j * 4] = (char)r;
  62. result->row_pointers[i][j * 4 + 1] = (char)g;
  63. result->row_pointers[i][j * 4 + 2] = (char)b;
  64. result->row_pointers[i][j * 4 + 3] = (char)a;
  65. }
  66. //Dar valor a result[PixelDelThread]
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement