Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cilk/cilk.h>
- #include <iostream>
- #include <stdlib.h>
- #include "BitmapRawConverter.h"
- #include "cilktime.h"
- #include <cilk\reducer_opadd.h>
- #define __ARG_NUM__ 6
- using namespace std;
- void sort9(int *);
- /**
- * @brief Serial version of moving average filter.
- * @param inBuffer buffer of input image
- * @param outBuffer buffer of output image
- * @param width image width
- * @param height image height
- */
- void filter_serial_avg(int* inBuffer, int* outBuffer, int width, int height)
- {
- for (int i = 1; i < width - 1; i ++)
- {
- for (int j = 1; j < height - 1; j++)
- {
- //if(i==1 && j>298 && j<=302)
- // cout << "BEFORE (" << i << ", "<< j <<") : " << inBuffer[j * width + i] << endl;
- // naredne dve petlje realizuju formulu dvostruke sume iz prateceg dokumenta
- for (int m = -1; m <= 1; m++)
- {
- for (int n = -1; n <= 1; n++)
- {
- outBuffer[j * width + i] += inBuffer[(j + n) * width + (i + m)] / 9;
- }
- }
- //if(i==1 && j>298 && j<=302)
- // cout << "AFTER (" << i << ", "<< j <<") : " << outBuffer[j * width + i]<<endl;
- }
- }
- }
- /**
- * @brief Parallel version of moving average filter.
- *
- * @param inBuffer buffer of input image
- * @param outBuffer buffer of output image
- * @param width image width
- * @param height image height
- */
- void filter_parallel_avg(int* inBuffer, int* outBuffer, int width, int height)
- {
- cilk_for (int i = 1; i < width - 1; i ++)
- {
- cilk_for (int j = 1; j < height - 1; j++)
- {
- cilk::reducer_opadd<unsigned int> matrix_sum; //nema potrebe, ali je trazeno u zadatku.
- for (int m = -1; m <= 1; m++)
- {
- for(int n = -1; n <= 1; n++)
- {
- matrix_sum += inBuffer[(j + n) * width + (i + m)]; //preciznije
- }
- }
- outBuffer[j * width + i] = matrix_sum.get_value() / 9;
- }
- }
- }
- /**
- * @brief Serial version of median filter.
- * @param inBuffer buffer of input image
- * @param outBuffer buffer of output image
- * @param width image width
- * @param height image height
- */
- void filter_serial_med(int* inBuffer, int* outBuffer, int width, int height)
- {
- for (int i = 1; i < width - 1; i ++)
- {
- for (int j = 1; j < height - 1; j++)
- {
- int a[9];
- int index = 0;
- for (int m = -1; m <= 1; m++)
- {
- for (int n = -1; n <= 1; n++,index++)
- {
- a[index] = inBuffer[(j + n) * width + (i + m)];
- }
- }
- sort9(a);
- //if(i==1 && j==300)
- // for(int xx=0; xx<9; xx++)
- // cout << "a[" << xx << "] = " << a[xx] << endl;
- outBuffer[j * width + i] = a[4];
- }
- }
- }
- /**
- * @brief Parallel version of median filter.
- *
- * @param inBuffer buffer of input image
- * @param outBuffer buffer of output image
- * @param width image width
- * @param height image height
- */
- void filter_parallel_med(int* inBuffer, int* outBuffer, int width, int height)
- {
- cilk_for (int i = 1; i < width - 1; i ++)
- {
- for (int j = 1; j < height - 1; j++)
- {
- int a[9];
- int index = 0;
- for (int m = -1; m <= 1; m++)
- {
- for (int n = -1; n <= 1; n++,index++)
- {
- a[index] = inBuffer[(j + n) * width + (i + m)];
- }
- }
- sort9(a);
- //if(i==1 && j==300)
- // for(int xx=0; xx<9; xx++)
- // cout << "a[" << xx << "] = " << a[xx] << endl;
- outBuffer[j * width + i] = a[4];
- }
- }
- }
- /**
- * @brief Print program usage.
- */
- void usage()
- {
- cout << "ERROR: call program like DigitalImageProcessing.exe input.bmp outputSerialAvg.bmp"
- "outputParallelAvg.bmp outputSerialMed.bmp outputParallelMed.bmp." << endl;
- }
- void sort9(int *d){
- int i, j;
- for (i = 0; i < 9; i++) {
- int tmp = d[i];
- for (j = i; j >= 1 && tmp < d[j-1]; j--)
- d[j] = d[j-1];
- d[j] = tmp;
- }
- }
- int main(int argc, char* argv[])
- {
- if (argc != __ARG_NUM__)
- {
- usage();
- return 0;
- }
- BitmapRawConverter inputFile(argv[1]);
- unsigned int width = inputFile.getWidth();
- unsigned int height = inputFile.getHeight();
- BitmapRawConverter outputFileSerialAvg(width, height);
- BitmapRawConverter outputFileParallelAvg(width, height);
- BitmapRawConverter outputFileSerialMed(width, height);
- BitmapRawConverter outputFileParallelMed(width, height);
- unsigned long long start_tick;
- unsigned long long end_tick;
- // SERIAL VERSION - MOVING AVERAGE FILTER
- cout << "Running serial version of moving average filter" << endl;
- start_tick = cilk_getticks();
- filter_serial_avg(inputFile.getBuffer(), outputFileSerialAvg.getBuffer(), width, height);
- end_tick = cilk_getticks();
- cout << "Ticks: " << end_tick - start_tick << endl;
- outputFileSerialAvg.pixelsToBitmap(argv[2]); // saves the result in a file
- // PARALLEL VERSION - MOVING AVERAGE FILTER
- cout << "Running parallel version of moving average filter" << endl;
- start_tick = cilk_getticks();
- // TODO: IMPLEMENT filter_parallel_avg FUNCTION
- filter_parallel_avg(inputFile.getBuffer(), outputFileParallelAvg.getBuffer(), width, height);
- end_tick = cilk_getticks();
- cout << "Ticks: " << end_tick - start_tick << endl;
- outputFileParallelAvg.pixelsToBitmap(argv[3]); // saves the result in a file
- // SERIAL VERSION - MEDIAN FILTER
- cout << "Running serial version of median filter" << endl;
- start_tick = cilk_getticks();
- // TODO: IMPLEMENT filter_serial_med FUNCTION
- filter_serial_med(inputFile.getBuffer(), outputFileSerialMed.getBuffer(), width, height);
- end_tick = cilk_getticks();
- cout << "Ticks: " << end_tick - start_tick << endl;
- outputFileSerialMed.pixelsToBitmap(argv[4]); // saves the result in a file
- // PARALLEL VERSION - MEDIAN FILTER
- cout << "Running parallel version of median filter" << endl;
- start_tick = cilk_getticks();
- // TODO: IMPLEMENT filter_parallel_med FUNCTION
- filter_parallel_med(inputFile.getBuffer(), outputFileParallelMed.getBuffer(), width, height);
- end_tick = cilk_getticks();
- cout << "Ticks: " << end_tick - start_tick << endl;
- outputFileParallelMed.pixelsToBitmap(argv[5]); // saves the result in a file
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement