Advertisement
Lauda

denis_c++

Apr 25th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #include <cilk/cilk.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4.  
  5. #include "BitmapRawConverter.h"
  6. #include "cilktime.h"
  7.  
  8. #include <cilk\reducer_opadd.h>
  9.  
  10. #define __ARG_NUM__ 6
  11.  
  12. using namespace std;
  13.  
  14. void sort9(int *);
  15.  
  16. /**
  17. * @brief Serial version of moving average filter.
  18. * @param inBuffer buffer of input image
  19. * @param outBuffer buffer of output image
  20. * @param width image width
  21. * @param height image height
  22. */
  23. void filter_serial_avg(int* inBuffer, int* outBuffer, int width, int height)
  24. {
  25.     for (int i = 1; i < width - 1; i ++)
  26.     {
  27.         for (int j = 1; j < height - 1; j++)
  28.         {
  29.             //if(i==1 && j>298 && j<=302)
  30.             //  cout << "BEFORE (" << i << ", "<< j <<") : " << inBuffer[j * width + i] << endl;
  31.             // naredne dve petlje realizuju formulu dvostruke sume iz prateceg dokumenta
  32.             for (int m = -1; m <= 1; m++)
  33.             {
  34.                 for (int n = -1; n <= 1; n++)
  35.                 {
  36.                     outBuffer[j * width + i] += inBuffer[(j + n) * width + (i + m)] / 9;
  37.                 }
  38.             }
  39.             //if(i==1 && j>298 && j<=302)
  40.             //  cout << "AFTER (" << i << ", "<< j <<") : " << outBuffer[j * width + i]<<endl;
  41.  
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. /**
  48. * @brief Parallel version of moving average filter.
  49. *
  50. * @param inBuffer buffer of input image
  51. * @param outBuffer buffer of output image
  52. * @param width image width
  53. * @param height image height
  54. */
  55. void filter_parallel_avg(int* inBuffer, int* outBuffer, int width, int height)
  56. {
  57.     cilk_for (int i = 1; i < width - 1; i ++)
  58.     {
  59.         cilk_for (int j = 1; j < height - 1; j++)
  60.         {
  61.             cilk::reducer_opadd<unsigned int> matrix_sum; //nema potrebe, ali je trazeno u zadatku.
  62.             for (int m = -1; m <= 1; m++)
  63.             {
  64.                 for(int n = -1; n <= 1; n++)
  65.                 {
  66.                      matrix_sum += inBuffer[(j + n) * width + (i + m)]; //preciznije
  67.                 }
  68.             }
  69.             outBuffer[j * width + i] = matrix_sum.get_value() / 9;
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. /**
  76. * @brief Serial version of median filter.
  77. * @param inBuffer buffer of input image
  78. * @param outBuffer buffer of output image
  79. * @param width image width
  80. * @param height image height
  81. */
  82. void filter_serial_med(int* inBuffer, int* outBuffer, int width, int height)
  83. {
  84.     for (int i = 1; i < width - 1; i ++)
  85.     {
  86.         for (int j = 1; j < height - 1; j++)
  87.         {
  88.             int a[9];
  89.             int index = 0;
  90.  
  91.             for (int m = -1; m <= 1; m++)
  92.             {
  93.                 for (int n = -1; n <= 1; n++,index++)
  94.                 {
  95.                     a[index] = inBuffer[(j + n) * width + (i + m)];
  96.                 }
  97.             }
  98.             sort9(a);
  99.             //if(i==1 && j==300)
  100.             //  for(int xx=0; xx<9; xx++)
  101.             //      cout << "a[" << xx << "] = " << a[xx] << endl;
  102.             outBuffer[j * width + i] = a[4];
  103.         }
  104.     }
  105. }
  106.  
  107.  
  108. /**
  109. * @brief Parallel version of median filter.
  110. *
  111. * @param inBuffer buffer of input image
  112. * @param outBuffer buffer of output image
  113. * @param width image width
  114. * @param height image height
  115. */
  116. void filter_parallel_med(int* inBuffer, int* outBuffer, int width, int height)
  117. {
  118.     cilk_for (int i = 1; i < width - 1; i ++)
  119.     {
  120.         for (int j = 1; j < height - 1; j++)
  121.         {
  122.             int a[9];
  123.             int index = 0;
  124.  
  125.             for (int m = -1; m <= 1; m++)
  126.             {
  127.                 for (int n = -1; n <= 1; n++,index++)
  128.                 {
  129.                     a[index] = inBuffer[(j + n) * width + (i + m)];
  130.                 }
  131.             }
  132.             sort9(a);
  133.             //if(i==1 && j==300)
  134.             //  for(int xx=0; xx<9; xx++)
  135.             //      cout << "a[" << xx << "] = " << a[xx] << endl;
  136.             outBuffer[j * width + i] = a[4];
  137.         }
  138.     }
  139. }
  140.  
  141.  
  142. /**
  143. * @brief Print program usage.
  144. */
  145. void usage()
  146. {
  147.     cout << "ERROR: call program like DigitalImageProcessing.exe input.bmp outputSerialAvg.bmp"
  148.         "outputParallelAvg.bmp outputSerialMed.bmp outputParallelMed.bmp." << endl;
  149. }
  150.  
  151. void sort9(int *d){
  152.         int i, j;
  153.         for (i = 0; i < 9; i++) {
  154.                 int tmp = d[i];
  155.                 for (j = i; j >= 1 && tmp < d[j-1]; j--)
  156.                         d[j] = d[j-1];
  157.                 d[j] = tmp;
  158.         }
  159. }
  160.  
  161.  
  162. int main(int argc, char* argv[])
  163. {
  164.     if (argc != __ARG_NUM__)
  165.     {
  166.         usage();
  167.         return 0;
  168.     }
  169.  
  170.     BitmapRawConverter inputFile(argv[1]);
  171.  
  172.     unsigned int width = inputFile.getWidth();
  173.     unsigned int height = inputFile.getHeight();
  174.  
  175.     BitmapRawConverter outputFileSerialAvg(width, height);
  176.     BitmapRawConverter outputFileParallelAvg(width, height);
  177.     BitmapRawConverter outputFileSerialMed(width, height);
  178.     BitmapRawConverter outputFileParallelMed(width, height);
  179.  
  180.     unsigned long long start_tick;
  181.     unsigned long long end_tick;
  182.  
  183.  
  184.     // SERIAL VERSION - MOVING AVERAGE FILTER
  185.     cout << "Running serial version of moving average filter" << endl;
  186.     start_tick = cilk_getticks();
  187.     filter_serial_avg(inputFile.getBuffer(), outputFileSerialAvg.getBuffer(), width, height);
  188.     end_tick = cilk_getticks();
  189.     cout << "Ticks: " << end_tick - start_tick << endl;
  190.     outputFileSerialAvg.pixelsToBitmap(argv[2]); // saves the result in a file
  191.  
  192.  
  193.     // PARALLEL VERSION - MOVING AVERAGE FILTER
  194.     cout << "Running parallel version of moving average filter" << endl;
  195.     start_tick = cilk_getticks();
  196.     // TODO: IMPLEMENT filter_parallel_avg FUNCTION
  197.     filter_parallel_avg(inputFile.getBuffer(), outputFileParallelAvg.getBuffer(), width, height);
  198.     end_tick = cilk_getticks();
  199.     cout << "Ticks: " << end_tick - start_tick << endl;
  200.     outputFileParallelAvg.pixelsToBitmap(argv[3]); // saves the result in a file
  201.  
  202.  
  203.     // SERIAL VERSION - MEDIAN FILTER
  204.     cout << "Running serial version of median filter" << endl;
  205.     start_tick = cilk_getticks();
  206.     // TODO: IMPLEMENT filter_serial_med FUNCTION
  207.     filter_serial_med(inputFile.getBuffer(), outputFileSerialMed.getBuffer(), width, height);
  208.     end_tick = cilk_getticks();
  209.     cout << "Ticks: " << end_tick - start_tick << endl;
  210.     outputFileSerialMed.pixelsToBitmap(argv[4]); // saves the result in a file
  211.  
  212.  
  213.     // PARALLEL VERSION - MEDIAN FILTER
  214.     cout << "Running parallel version of median filter" << endl;
  215.     start_tick = cilk_getticks();
  216.     // TODO: IMPLEMENT filter_parallel_med FUNCTION
  217.     filter_parallel_med(inputFile.getBuffer(), outputFileParallelMed.getBuffer(), width, height);
  218.     end_tick = cilk_getticks();
  219.     cout << "Ticks: " << end_tick - start_tick << endl;
  220.     outputFileParallelMed.pixelsToBitmap(argv[5]); // saves the result in a file
  221.  
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement