Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 40.68 KB | Hits: 78 | Expires: Never
Copy text to clipboard
  1. //#include <stdio.h>
  2. //#include <stdlib.h>
  3. //#include <string.h>
  4. //#include <math.h>
  5.  
  6. //#include "imageio.cpp"
  7. //#include "helperMethods.cpp"
  8. //#include "preProcessing.cpp"
  9. //#include "postProcessing.cpp"
  10. //#include "sizeFindingMethods.cpp"
  11.  
  12. #include "parasite.h"
  13.  
  14. //#include <conio.h>;
  15. //#include <iostream>;
  16.  
  17. #include "shlobj.h"
  18. //#include <time.h>
  19.  
  20. ////////////////////////////////////////////////////////////////////////////////
  21. /* Custom HPC Counter                                                         */
  22. ////////////////////////////////////////////////////////////////////////////////
  23.  
  24. //#define BENCHMARKING
  25. //#define ELAPSED
  26. //#define NUM_OF_PASSES 1
  27. //#define DEBUG_OUTPUT
  28. //#define TITLES
  29.  
  30.  
  31. extern "C" {
  32.    #include "hr_time.h"
  33. }
  34.  
  35. extern "C" {
  36.    #include "cputime.h"
  37. }
  38.  
  39. //extern "C" {
  40. //   #include "leak_detector_c.h"
  41. //}
  42.  
  43. ////////////////////////////////////////////////////////////////////////////////
  44. /* End Custom HPC Counter                                                     */
  45. ////////////////////////////////////////////////////////////////////////////////
  46.  
  47. //////////////////////////////////////////////////////////////////////////////////
  48. ///* Timeb headers                                                              */
  49. //////////////////////////////////////////////////////////////////////////////////
  50. //#include <sys/timeb.h>
  51. //#include <time.h>
  52.  
  53. //// to get portable code we need this
  54. //#ifdef WIN32 // _WIN32 is defined by all Windows 32 compilers, but not by others.
  55. //#define timeb _timeb
  56. //#define ftime _ftime
  57. //#endif
  58. //////////////////////////////////////////////////////////////////////////////////
  59. ///* End timeb headers                                                          */
  60. //////////////////////////////////////////////////////////////////////////////////
  61.  
  62. //#include "mmgr.h"
  63.  
  64.  
  65. // Global Variables
  66. short* parasiteSize;
  67. double* parasiteCompactness;
  68. int noOfThreads;
  69. int imagesPerThread;
  70.  
  71. void processImages(struct threadStats*);
  72.  
  73. int main(int argc, unsigned char* argv[])
  74. {
  75.        
  76.         HANDLE *threads;
  77.         struct threadStats *st;
  78.         int numberOfInputImages, extra;
  79.  
  80.         // get the index of the image we want and make a unsigned char array of it then concat it to imageFile.
  81.         // Set the size of the variable 'imageFileName' to 255 characters
  82.         char* masterImageFileName = (char*)malloc(sizeof(char)*255);
  83.         char imageIndexString[10];
  84.  
  85.         // Construct the file name
  86.         int imageIndex = 1;
  87.         itoa(imageIndex,imageIndexString,10);
  88.  
  89.         strcpy(masterImageFileName, "images/colour");
  90.         strcat(masterImageFileName, imageIndexString);
  91.         strcat(masterImageFileName, ".bmp");
  92.  
  93.         // Reads in one specific image defined by the second parameter.
  94.         //strcpy(masterImageFileName, "images/colour137.bmp");
  95.  
  96.         // Set the size of the video name variable 30 characters.
  97.         //char* videoname = (char*)malloc(sizeof(char)*30);
  98.         //if (argc != 3) /* argc should be 3 for correct execution */
  99.         //{
  100.         //    printf("no folder or video name specified:\n");
  101.         //      return 1;
  102.         //}
  103.         //else
  104.         //{
  105.         //      //printf("path: %s\n", argv[1]);
  106.         //      // copy the first argument to the 'masterImageFileName' variable.
  107.         //      strcpy(masterImageFileName, (char*) argv[1]);
  108.         //      // copy the second argument to the 'videoname' variable.
  109.         //      strcpy(videoname, (char*) argv[2]);
  110.         //      //masterImageFileName = (char*) argv[1];
  111.         //}
  112.        
  113.         // Set the integer 'numberOfInputImages'.
  114.         //numberOfInputImages = getNumberOfImages((char*) argv[1]);
  115.  
  116.         int numCores = 1;
  117.         SYSTEM_INFO siSysInfo;
  118.         GetSystemInfo(&siSysInfo);
  119.         numCores = siSysInfo.dwNumberOfProcessors;
  120.  
  121.         printf("////////////////////////////////////////\n");
  122.         printf("/    MULTITHREADED PARASITE ANALYSIS   /\n");
  123.         printf("////////////////////////////////////////\n");
  124.         printf("Starting program...\n\n");
  125.         printf("%d Cores available.\n", numCores);
  126.         printf("___________________\n\n");
  127.  
  128.         numberOfInputImages = 140;
  129.         parasiteSize = (short *)malloc(sizeof(short) * numberOfInputImages);
  130.         parasiteCompactness = (double *)malloc(sizeof(double) * numberOfInputImages);
  131.        
  132.         printf("Enter the number of threads: ");
  133.         scanf("%d",&noOfThreads);
  134.        
  135.         while(noOfThreads>numCores)
  136.         {
  137.                 printf("There is only %d cores available - therefore cannot create more than %d thread(s)\n", numCores, numCores);
  138.                 printf("\n");
  139.                 printf("Enter the number of threads: ");
  140.                 scanf("%d",&noOfThreads);
  141.         }
  142.  
  143.         while(noOfThreads>numberOfInputImages)
  144.         {
  145.                 printf("There is only %d images - therefore cannot create more than %d thread(s)\n", numberOfInputImages, numberOfInputImages);
  146.                 printf("\n");
  147.                 printf("Enter the number of threads: ");
  148.                 scanf("%d",&noOfThreads);
  149.         }      
  150.  
  151.         imagesPerThread = numberOfInputImages/noOfThreads;
  152.         extra = numberOfInputImages % noOfThreads;
  153.  
  154.         threads = (HANDLE*) malloc(sizeof(HANDLE)*(noOfThreads-1)); // 1 handle for each spawned thread not counting master thread
  155.         st = (struct threadStats*) malloc(sizeof(struct threadStats)*noOfThreads); // struct for each thread
  156.  
  157.         double processElapsedTime;
  158.         int offset = imagesPerThread;
  159.         stopWatch multi_stopwatch;
  160.         if(initialiseTimer(&multi_stopwatch)==0)
  161.         {
  162.                 startTimer(&multi_stopwatch, 1);
  163.  
  164.                 int noOfSlaves = noOfThreads-1;
  165.                 for(int i=1;i<=noOfSlaves;i++)
  166.                 {
  167.                         st[i].id = i;
  168.                         st[i].imagesPerThread = imagesPerThread;
  169.                         if (extra > 0) {
  170.                                 st[i].imagesPerThread += 1;
  171.                                 extra--;
  172.                         }
  173.                         st[i].imageOffset = offset;
  174.                         offset += st[i].imagesPerThread;
  175.                        
  176.                         //threads[i] = (HANDLE) _beginthreadex(NULL,0,processImages,(void*)&st[i],0,NULL);
  177.                         threads[i-1] = (HANDLE) _beginthread((void(*)(void*))processImages, 0, (void*)&st[i]);
  178.                 }
  179.  
  180.                 st[0].id = 0;
  181.                 st[0].imagesPerThread = imagesPerThread;
  182.                 st[0].imageOffset = 0;
  183.                 processImages(&st[0]);
  184.  
  185.                 WaitForMultipleObjects(noOfSlaves, threads, TRUE, INFINITE);
  186.                
  187.  
  188.                 stopTimer(&multi_stopwatch, 1);
  189.                 processElapsedTime += getElapsedTimeInMilli(&multi_stopwatch);
  190.  
  191.                 printf("\n");
  192.                 printf("Free thread handles and structs\n");
  193.                 free(st);
  194.                 free(threads);
  195.  
  196.                 //getchar(); // last character in stream will be a return
  197.                 printf("All threads have completed.\n\n");
  198.         }
  199.         else { printf("System timer did not initialise\n"); }
  200.  
  201.         printf("\nProcess Elapsed Time: %f(ms)\n\n", processElapsedTime);
  202.  
  203.         printf("# Cores: %d\n", siSysInfo.dwNumberOfProcessors);
  204.         printf("# Images: %d\n", numberOfInputImages);
  205.         printf("# Threads: %d\n", noOfThreads);
  206.         printf("# Images per thread: %d\n", imagesPerThread);
  207.         //printf("Size of Struct: %d\n", sizeof(struct threadStats)*noOfThreads);
  208.         printf("\n");
  209.  
  210.  
  211.         getchar();
  212.         fflush(NULL);
  213.         printf("Please type enter to obtain results\n");
  214.         getchar();
  215.         //system("pause");
  216.  
  217.         //Compensate for being too small
  218.         compensateForSmallSize(parasiteSize, numberOfInputImages);
  219.         //findParasiteFlux(numberOfInputImages, parasiteSize);
  220.         printf("--------------- Flux Unsorted ---------------\n");
  221.         for(int i = 0; i < numberOfInputImages; i++)
  222.         {
  223.                 if (i%imagesPerThread == 0)
  224.                         printf("++++++++++++++++\n");
  225.                 printf("Array index %d: size = %d \t compactness = %f\n" , i, parasiteSize[i], parasiteCompactness[i]);
  226.  
  227.         }
  228.  
  229.         //findParasiteFluxAlt(numberOfInputImages, parasiteCompactness);
  230.  
  231.         // Use this one.
  232.         int flux;
  233.         if (numberOfInputImages >= 3) {
  234.                 flux = findParasiteFlux3(numberOfInputImages, parasiteSize);
  235.                 printf("Number of head projections: %d\n", flux);
  236.  
  237.                 //// Write the results to the results text file.
  238.                 //FILE* resultsFile;
  239.                 //resultsFile = fopen("Results.txt", "a");
  240.                 //fprintf(resultsFile, "%s, %d,\n", videoName, flux);
  241.                 //fclose(resultsFile);
  242.         }
  243.         else
  244.                 printf("Not enough images to analyse.\n");
  245.  
  246.         free(parasiteSize);
  247.         free(parasiteCompactness);
  248.         free(masterImageFileName);
  249.        
  250.         printf("___________________\n");
  251.         printf("PROGRAM COMPLETED\n\n");
  252.         printf("PRESS ANY KEY TO EXIT\n");
  253.  
  254.         fflush(stdout);
  255.         //getchar();
  256.         getchar();
  257.         return 0;
  258. }
  259.  
  260.  
  261. void processImages(struct threadStats* s)
  262. {
  263.         int threadID = (int)(struct threadStats*)(s)->id;
  264.         int totalThreadImages = (int)(struct threadStats*)(s)->imagesPerThread;
  265.         int threadImageOffset = (int)(struct threadStats*)(s)->imageOffset;
  266.  
  267.         unsigned char processorMask = 0x01;
  268.         processorMask = processorMask << threadID;
  269.         printf("Thread %d gets processor %d with %d images\n", threadID, processorMask, totalThreadImages);
  270.        
  271.         // Calculate the starting image offset for the current thread
  272.         int imageIndex = threadImageOffset;
  273.         //int imageIndex = 11;
  274.  
  275.         // Cannot set affinity before creating thread.
  276.         // SetThreadAffinityMask uses a bitmask for assigning processor cores.
  277.         if (!SetThreadAffinityMask(GetCurrentThread(), (DWORD)processorMask)) {
  278.                 printf("Could not set affinity\n");
  279.                 printf("Err: %d\n", GetLastError());
  280.                 //getchar();
  281.                 _endthread();
  282.         }
  283.  
  284.         /* To double check processor affinity
  285.                 //DWORD proNum = 9;
  286.                 //printf("proNum B: %d\n", proNum);
  287.                 //proNum = GetCurrentProcessorNumber(); // returns a number between 0..N
  288.                 //printf("proNum A: %d\n", proNum);
  289.         */
  290.  
  291.         /* Small test to check processor affinity
  292.  
  293.                 int o=0;
  294.                 int count = 0;
  295.                 while(o<20000000)
  296.                 {
  297.                         t_bmp* img = (t_bmp*)malloc(sizeof(t_bmp));
  298.                         free(img);
  299.                         o++;
  300.                 }
  301.  
  302.                 _endthread();
  303.         */
  304.        
  305.         unsigned char* imageArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  306.         t_bmp* img = (t_bmp*)malloc(sizeof(t_bmp));
  307.  
  308.         char* imageFileName = (char*)malloc(sizeof(char)*255);
  309.         memset(imageFileName, '-', sizeof(char)*255);
  310.         sprintf(imageFileName,"images/colour%d.bmp", (imageIndex+1));
  311.  
  312.  
  313.         //printf("%d\tThread is Running\t%d: output file %s\n", threadID, totalThreadImages, imageFileName);
  314.  
  315.         #ifdef BENCHMARKING
  316.                 #ifdef ELAPSED
  317.                 FILE *benchMarkFileElapsed;
  318.                 char outputName [30];
  319.                 sprintf (outputName, "benchMarkFileElapsed Thread_%d.txt", threadID);
  320.                 benchMarkFileElapsed = fopen(outputName, "w");
  321.                 fprintf(benchMarkFileElapsed, "------------Benchmarking Algorithms (Elapsed)------------\n");
  322.                 fprintf(benchMarkFileElapsed, "Thread ID %d.\n", threadID);
  323.                 fprintf(benchMarkFileElapsed, "Processor ID (start at 0) %d. \n", GetCurrentProcessorNumber());
  324.                 fprintf(benchMarkFileElapsed, "Number of Images %d.\n", totalThreadImages);
  325.                 fprintf(benchMarkFileElapsed, "Average elapsed times in ms.\n");
  326.                 fprintf(benchMarkFileElapsed, "Number of Passes:%d\n\n", NUM_OF_PASSES);
  327.                 double aggregatedElapsedTime = 0;
  328.                 #else
  329.                 FILE *benchMarkFileCPU;
  330.                 char outputName [30];
  331.                 sprintf (outputName, "benchMarkFileCPU Thread_%d.txt", threadID);
  332.                 benchMarkFileCPU = fopen(outputName, "w");
  333.                 fprintf(benchMarkFileCPU, "------------Benchmarking Algorithms (CPU)------------\n");
  334.                 fprintf(benchMarkFileElapsed, "Thread ID %d.\n", threadID);
  335.                 fprintf(benchMarkFileElapsed, "Processor ID %d.\n", GetCurrentProcessorNumber());
  336.                 fprintf(benchMarkFileElapsed, "Number of Images %d.\n", totalThreadImages);
  337.                 fprintf(benchMarkFileCPU, "Average CPU times in ms.\n");
  338.                 fprintf(benchMarkFileCPU, "Number of Passes:%d\n\n", NUM_OF_PASSES);
  339.                 double aggregatedCPUTime = 0;
  340.                 #endif
  341.         #endif
  342.  
  343.         // While there are no more images to load.
  344.         // (imageIndex-1) = starting image index
  345.         // ((threadID*totalThreadImages)+totalThreadImages)) = final image index
  346.         while((imageIndex<(threadImageOffset+totalThreadImages)) && (libbmp_load(imageFileName, img) != 0))
  347.         //while(libbmp_load(imageFileName, img) != 0)
  348.         {
  349.                 #ifdef TITLES
  350.                 printf("------ Processing Frame ------\n");
  351.                 #endif
  352.                 //printf("THREAD %d IMAGE %d\n", threadID, imageIndex);
  353.  
  354.                 // Moy is black and white intensity
  355.                 // moy of 0 is black
  356.                 // moy of 255 is white
  357.                 // height goes from bottom up not top down!!!!
  358.                 // initialize all necessary variables
  359.                 ////////////////////////////////////////////////////////////////////////////////
  360.                 /* Create Histogram                                                           */
  361.                 ////////////////////////////////////////////////////////////////////////////////
  362.                 short* histogram;
  363.                 histogram = createHistogram(img);
  364.  
  365.                 #ifdef DEBUG_OUTPUT
  366.                         //libbmp_write("image.bmp", img);
  367.                         libbmp_write("image.bmp", img);
  368.                 #endif
  369.                 ////////////////////////////////////////////////////////////////////////////////
  370.                 /* End Create Histogram                                                       */
  371.                 ////////////////////////////////////////////////////////////////////////////////
  372.  
  373.                 /**************************************************************************************************************/
  374.  
  375.                 ////////////////////////////////////////////////////////////////////////////////
  376.                 /* Enhance Contrast                                                           */
  377.                 ////////////////////////////////////////////////////////////////////////////////
  378.  
  379.                 #ifdef TITLES
  380.                 printf("-------------------Enhance Contrast\n");
  381.                 #endif
  382.  
  383.                 #ifdef BENCHMARKING
  384.                         t_bmp* initialImg;
  385.                         initialImg = (t_bmp*) malloc(sizeof(t_bmp));
  386.                         libbmp_copyAndCreateImg(img, initialImg);
  387.                
  388.                         #ifdef ELAPSED
  389.                         stopWatch s;
  390.                         if(initialiseTimer(&s)==0) {
  391.                         #endif
  392.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  393.                                         libbmp_copyImgData(initialImg, img);
  394.  
  395.                                         #ifdef ELAPSED
  396.                                         startTimer(&s, processorMask);
  397.                                         #else
  398.                                         initCPUTime();
  399.                                         #endif
  400.                 #endif
  401.  
  402.                                         enhanceContrast(img, histogram);
  403.                                
  404.                 #ifdef BENCHMARKING
  405.                                         #ifdef ELAPSED
  406.                                         stopTimer(&s, processorMask);
  407.                                         #else
  408.                                         double endTime = getCPUTimeSinceStart();
  409.                                         #endif
  410.  
  411.                                         #ifdef ELAPSED
  412.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  413.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  414.                                         #else
  415.                                         printf("CPU Time: %f(ms)\n", endTime);
  416.                                         aggregatedCPUTime += endTime;
  417.                                         #endif
  418.                                 }
  419.                         #ifdef ELAPSED
  420.                         } else { printf("Enhance constrast timer didnt initialise\n"); }
  421.                         #endif
  422.                
  423.  
  424.                         // Print results to file and screen and clean up
  425.                         #ifdef ELAPSED
  426.                         fprintf(benchMarkFileElapsed, "Enhance Contrast       : ");
  427.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  428.  
  429.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  430.                         aggregatedElapsedTime = 0.0;
  431.                         #else
  432.                         fprintf(benchMarkFileCPU, "Enhance Contrast       : ");
  433.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  434.  
  435.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  436.                         aggregatedCPUTime = 0.0;
  437.                         #endif
  438.                
  439.                         for(int i = 0; i < initialImg->height; i++)
  440.                                 free(initialImg->data[i]);
  441.  
  442.                         free(initialImg->data);
  443.                         free(initialImg);
  444.                 #endif
  445.  
  446.                 // free histogram data structure
  447.                 free(histogram);
  448.  
  449.                 #ifdef DEBUG_OUTPUT
  450.                         //enhanceContrast(img, histogram);
  451.                         //libbmp_write("imageEnhanceContrast.bmp", img);
  452.                         libbmp_write("imageStage1.bmp", img);
  453.                 #endif
  454.  
  455.                 ////////////////////////////////////////////////////////////////////////////////
  456.                 /* End Enhance Contrast                                                       */
  457.                 ////////////////////////////////////////////////////////////////////////////////
  458.  
  459.                 /**************************************************************************************************************/
  460.  
  461.                 ////////////////////////////////////////////////////////////////////////////////
  462.                 /* Filtering                                                                  */
  463.                 ////////////////////////////////////////////////////////////////////////////////
  464.  
  465.                 #ifdef TITLES
  466.                 printf("-------------------Filtering\n");
  467.                 #endif
  468.  
  469.                 t_bmp* outputImg = (t_bmp*)malloc(sizeof(t_bmp));
  470.                 libbmp_copyAndCreateImg(img, outputImg);                // initialise outputImage by copying
  471.  
  472.                 #ifdef BENCHMARKING
  473.                         initialImg = (t_bmp*) malloc(sizeof(t_bmp));
  474.                         libbmp_copyAndCreateImg(img, initialImg);
  475.  
  476.                         /* Updated the low pass filter sequential to take into account the border pixels.
  477.                            Also removed a major bug where the convolution operation output the results of each pixel calculation to the same image instead of a new image.
  478.                      */
  479.  
  480.                         #ifdef ELAPSED
  481.                         if(initialiseTimer(&s)==0) {
  482.                         #endif
  483.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  484.                                         libbmp_copyImgData(initialImg, img);
  485.  
  486.                                         #ifdef ELAPSED
  487.                                         startTimer(&s, processorMask);
  488.                                         #else
  489.                                         initCPUTime();
  490.                                         #endif
  491.                 #endif
  492.  
  493.                                         lowPassFilterImage(img, outputImg);
  494.                                         //lowPassFilterImage(img);
  495.                
  496.                 #ifdef BENCHMARKING
  497.                                         #ifdef ELAPSED
  498.                                         stopTimer(&s, processorMask);
  499.                                         #else
  500.                                         double endTime = getCPUTimeSinceStart();
  501.                                         #endif
  502.  
  503.                                         #ifdef ELAPSED
  504.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  505.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  506.                                         #else
  507.                                         printf("CPU Time: %f(ms)\n", endTime);
  508.                                         aggregatedCPUTime += endTime;
  509.                                         #endif
  510.                                 }
  511.                         #ifdef ELAPSED
  512.                         } else { printf("Filtering timer didnt initialise\n"); }
  513.                         #endif
  514.  
  515.                         #ifdef ELAPSED
  516.                         fprintf(benchMarkFileElapsed, "Filtering              : ");
  517.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  518.  
  519.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  520.                         aggregatedElapsedTime = 0.0;
  521.                         #else
  522.                         fprintf(benchMarkFileCPU, "Filtering              : ");
  523.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  524.  
  525.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  526.                         aggregatedCPUTime = 0.0;
  527.                         #endif
  528.  
  529.                         for(int i = 0; i < initialImg->height; i++)
  530.                                 free(initialImg->data[i]);
  531.  
  532.                         free(initialImg->data);
  533.                         free(initialImg);
  534.                 #endif
  535.  
  536.  
  537.                
  538.                 // Switch pointers so img points to outputImg (the result of the low pass filter operation) and outputImg points to the old data (img), which is then removed.
  539.                 t_bmp* temp;
  540.                 //printf("img: %x OutputImage: %x temp: %xd\n\n", img, outputImg, temp);
  541.                 temp = outputImg;
  542.                 //printf("temp = outputImage\n");
  543.                 //printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
  544.                 outputImg = img;
  545.                 //printf("outputImage = img\n");
  546.                 //printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
  547.                 img = temp;
  548.                 //printf("img = temp\n");
  549.                 //printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
  550.                 for(int i = 0; i < outputImg->height; i++)
  551.                         free(outputImg->data[i]);
  552.  
  553.                 free(outputImg->data);
  554.                 free(outputImg);
  555.                
  556.  
  557.  
  558.                 #ifdef DEBUG_OUTPUT
  559.                         //lowPassFilterImage(img);
  560.                         //medianFilterImage(img);
  561.                         //libbmp_write("imageMedianFilter.bmp", img);
  562.                         libbmp_write("imageStage2.bmp", img);
  563.                 #endif
  564.  
  565.                 ////////////////////////////////////////////////////////////////////////////////
  566.                 /* End Filtering                                                              */
  567.                 ////////////////////////////////////////////////////////////////////////////////
  568.  
  569.                 /**************************************************************************************************************/
  570.  
  571.                 ////////////////////////////////////////////////////////////////////////////////
  572.                 /* Segment Image                                                              */
  573.                 ////////////////////////////////////////////////////////////////////////////////
  574.  
  575.                 #ifdef TITLES
  576.                 printf("-------------------Segment Image\n");
  577.                 #endif
  578.                
  579.                 #ifdef BENCHMARKING
  580.                         initialImg = (t_bmp*) malloc(sizeof(t_bmp));
  581.                         libbmp_copyAndCreateImg(img, initialImg);
  582.  
  583.                         #ifdef ELAPSED
  584.                         if(initialiseTimer(&s)==0) {
  585.                         #endif
  586.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  587.                                         libbmp_copyImgData(initialImg, img);
  588.  
  589.                                         #ifdef ELAPSED
  590.                                         startTimer(&s, processorMask);
  591.                                         #else
  592.                                         initCPUTime();
  593.                                         #endif
  594.                 #endif
  595.  
  596.                                         segmentImage(img);
  597.  
  598.                 #ifdef BENCHMARKING
  599.                                         #ifdef ELAPSED
  600.                                         stopTimer(&s, processorMask);
  601.                                         #else
  602.                                         double endTime = getCPUTimeSinceStart();
  603.                                         #endif
  604.  
  605.                                         #ifdef ELAPSED
  606.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  607.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  608.                                         #else
  609.                                         printf("CPU Time: %f(ms)\n", endTime);
  610.                                         aggregatedCPUTime += endTime;
  611.                                         #endif
  612.                                 }
  613.                         #ifdef ELAPSED
  614.                         } else { printf("Segment timer didnt initialise\n"); }
  615.                         #endif
  616.  
  617.                         #ifdef ELAPSED
  618.                         fprintf(benchMarkFileElapsed, "Segment Image          : ");
  619.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  620.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  621.                         aggregatedElapsedTime = 0.0;
  622.                         #else
  623.                         fprintf(benchMarkFileCPU, "Segment Image          : ");
  624.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  625.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  626.                         aggregatedCPUTime = 0.0;
  627.                         #endif
  628.                
  629.                         for(int i = 0; i < initialImg->height; i++)
  630.                                 free(initialImg->data[i]);
  631.  
  632.                         free(initialImg->data);
  633.                         free(initialImg);
  634.                 #endif
  635.  
  636.                 #ifdef DEBUG_OUTPUT
  637.                         //segmentImage(img);
  638.                         //libbmp_write("imageSegmented.bmp", img);
  639.                         libbmp_write("imageStage3.bmp", img);
  640.                 #endif
  641.  
  642.                 ////////////////////////////////////////////////////////////////////////////////
  643.                 /* End Segment Image                                                          */
  644.                 ////////////////////////////////////////////////////////////////////////////////
  645.  
  646.                 /**************************************************************************************************************/
  647.  
  648.                 //Convert image to a 1D array
  649.                 imageToArray(img, imageArray);
  650.  
  651.                 #ifdef DEBUG_OUTPUT
  652.                         createImage(img, imageArray);
  653.                         //libbmp_write("imageWith1DArray.bmp", img);
  654.                         libbmp_write("imageStage4.bmp", img);
  655.                 #endif
  656.  
  657.                 /**************************************************************************************************************/
  658.  
  659.                 ////////////////////////////////////////////////////////////////////////////////
  660.                 /* Dilate Image three times                                                   */
  661.                 ////////////////////////////////////////////////////////////////////////////////
  662.  
  663.                 #ifdef TITLES
  664.                 printf("-------------------Dilate Image\n");
  665.                 #endif
  666.  
  667.                 #ifdef BENCHMARKING
  668.                         unsigned char* initialImgArray;
  669.                         initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  670.                         copyArray(imageArray, initialImgArray);
  671.  
  672.                         #ifdef ELAPSED
  673.                         if(initialiseTimer(&s)==0) {
  674.                         #endif
  675.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  676.                                         copyArray(initialImgArray, imageArray);
  677.  
  678.                                         #ifdef ELAPSED
  679.                                         startTimer(&s, processorMask);
  680.                                         #else
  681.                                         initCPUTime();
  682.                                         #endif
  683.                 #endif
  684.  
  685.                                         dilateImage(img->height, img->width, imageArray);
  686.                                         dilateImage(img->height, img->width, imageArray);
  687.                                         dilateImage(img->height, img->width, imageArray);
  688.                                         dilateImage(img->height, img->width, imageArray);
  689.                                         dilateImage(img->height, img->width, imageArray);
  690.                                         dilateImage(img->height, img->width, imageArray);
  691.  
  692.                 #ifdef BENCHMARKING
  693.                                         #ifdef ELAPSED
  694.                                         stopTimer(&s, processorMask);
  695.                                         #else
  696.                                         double endTime = getCPUTimeSinceStart();
  697.                                         #endif
  698.  
  699.                                         #ifdef ELAPSED
  700.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  701.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  702.                                         #else
  703.                                         printf("CPU Time: %f(ms)\n", endTime);
  704.                                         aggregatedCPUTime += endTime;
  705.                                         #endif
  706.                                 }
  707.                         #ifdef ELAPSED
  708.                         } else { printf("Dilate timer didnt initialise\n"); }
  709.                         #endif
  710.                
  711.                         #ifdef ELAPSED
  712.                         fprintf(benchMarkFileElapsed, "Dilate Image           : ");
  713.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  714.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  715.                         aggregatedElapsedTime = 0.0;
  716.                         #else
  717.                         fprintf(benchMarkFileCPU, "Dilate Image           : ");
  718.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  719.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  720.                         aggregatedCPUTime = 0.0;
  721.                         #endif 
  722.  
  723.                         free(initialImgArray);
  724.                 #endif
  725.  
  726.                 //dilateImage(img->height, img->width, imageArray);
  727.                 //dilateImage(img->height, img->width, imageArray);
  728.                 //dilateImage(img->height, img->width, imageArray);
  729.                 //dilateImage(img->height, img->width, imageArray);
  730.                 //dilateImage(img->height, img->width, imageArray);
  731.                 //dilateImage(img->height, img->width, imageArray);
  732.  
  733.                 #ifdef DEBUG_OUTPUT
  734.                         createImage(img, imageArray);
  735.                         //libbmp_write("imageDilated.bmp", img);
  736.                         libbmp_write("imageStage5.bmp", img);
  737.                 #endif
  738.  
  739.                 ////////////////////////////////////////////////////////////////////////////////
  740.                 /* End Dilate Image three times                                               */
  741.                 ////////////////////////////////////////////////////////////////////////////////
  742.  
  743.                 // The following code was a template for creating a method to fill in the black gaps
  744.                 // in the worm segmentation.
  745.  
  746.                 //int *fillDetails = findLargestArea(img->height, img->width, imageArray);
  747.                 ////fill(imageArray, fillDetails[1], fillDetails[2]);
  748.  
  749.                 //createImage(img, imageArray);
  750.                 //libbmp_write("imageStage5i.bmp", img);
  751.  
  752.                 //free(fillDetails);
  753.  
  754.                 /**************************************************************************************************************/
  755.  
  756.                 ////////////////////////////////////////////////////////////////////////////////
  757.                 /* Erode Image three times                                                    */
  758.                 ////////////////////////////////////////////////////////////////////////////////
  759.  
  760.                 #ifdef TITLES
  761.                 printf("-------------------Erode Image\n");
  762.                 #endif
  763.  
  764.                 #ifdef BENCHMARKING
  765.                         initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  766.                         copyArray(imageArray, initialImgArray);
  767.  
  768.                         #ifdef ELAPSED
  769.                         if(initialiseTimer(&s)==0) {
  770.                         #endif
  771.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  772.                                         copyArray(initialImgArray, imageArray);
  773.  
  774.                                         #ifdef ELAPSED
  775.                                         startTimer(&s, processorMask);
  776.                                         #else
  777.                                         initCPUTime();
  778.                                         #endif
  779.                 #endif
  780.  
  781.                                         erodeImage(img->height, img->width, imageArray);
  782.                                         erodeImage(img->height, img->width, imageArray);
  783.                                         erodeImage(img->height, img->width, imageArray);
  784.                                         erodeImage(img->height, img->width, imageArray);
  785.                                         erodeImage(img->height, img->width, imageArray);
  786.                                         erodeImage(img->height, img->width, imageArray);
  787.  
  788.                 #ifdef BENCHMARKING
  789.                                         #ifdef ELAPSED
  790.                                         stopTimer(&s, processorMask);
  791.                                         #else
  792.                                         double endTime = getCPUTimeSinceStart();
  793.                                         #endif
  794.  
  795.                                         #ifdef ELAPSED
  796.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  797.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  798.                                         #else
  799.                                         printf("CPU Time: %f(ms)\n", endTime);
  800.                                         aggregatedCPUTime += endTime;
  801.                                         #endif
  802.                                 }
  803.                         #ifdef ELAPSED
  804.                         } else { printf("Erode timer didnt initialise\n"); }
  805.                         #endif
  806.  
  807.                         #ifdef ELAPSED
  808.                         fprintf(benchMarkFileElapsed, "Erode Image            : ");
  809.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  810.  
  811.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  812.                         aggregatedElapsedTime = 0.0;
  813.                         #else
  814.                         fprintf(benchMarkFileCPU, "Erode Image            : ");
  815.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  816.  
  817.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  818.                         aggregatedCPUTime = 0.0;
  819.                         #endif
  820.  
  821.                         free(initialImgArray);
  822.                 #endif
  823.  
  824.                 //erodeImage(img->height, img->width, imageArray);
  825.                 //erodeImage(img->height, img->width, imageArray);
  826.                 //erodeImage(img->height, img->width, imageArray);
  827.                 //erodeImage(img->height, img->width, imageArray);
  828.                 //erodeImage(img->height, img->width, imageArray);
  829.                 //erodeImage(img->height, img->width, imageArray);
  830.                
  831.                 #ifdef DEBUG_OUTPUT
  832.                         createImage(img, imageArray);
  833.                         //libbmp_write("imageEroded.bmp", img);
  834.                         libbmp_write("imageStage6.bmp", img);
  835.                 #endif
  836.                 ////////////////////////////////////////////////////////////////////////////////
  837.                 /* Erode Image three times                                                    */
  838.                 ////////////////////////////////////////////////////////////////////////////////
  839.  
  840.                 /**************************************************************************************************************/
  841.  
  842.                 ////////////////////////////////////////////////////////////////////////////////
  843.                 /* Remove Noise                                                               */
  844.                 ////////////////////////////////////////////////////////////////////////////////
  845.  
  846.                 #ifdef TITLES
  847.                 printf("-------------------Remove Noise\n");
  848.                 #endif
  849.  
  850.                 int maskSize = 3;
  851.  
  852.                 #ifdef BENCHMARKING
  853.                         initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  854.                         copyArray(imageArray, initialImgArray);
  855.  
  856.                         #ifdef ELAPSED
  857.                         if(initialiseTimer(&s)==0) {
  858.                         #endif
  859.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  860.                                         copyArray(initialImgArray, imageArray);
  861.  
  862.                                         #ifdef ELAPSED
  863.                                         startTimer(&s, processorMask);
  864.                                         #else
  865.                                         initCPUTime();
  866.                                         #endif
  867.                 #endif
  868.  
  869.                                         removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
  870.                                         removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
  871.                                         removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
  872.                                         removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
  873.  
  874.                 #ifdef BENCHMARKING
  875.                                         #ifdef ELAPSED
  876.                                         stopTimer(&s, processorMask);
  877.                                         #else
  878.                                         double endTime = getCPUTimeSinceStart();
  879.                                         #endif
  880.  
  881.                                         #ifdef ELAPSED
  882.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  883.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  884.                                         #else
  885.                                         printf("CPU Time: %f(ms)\n", endTime);
  886.                                         aggregatedCPUTime += endTime;
  887.                                         #endif
  888.                                 }
  889.                         #ifdef ELAPSED
  890.                         } else { printf("Unwanted noise timer didnt initialise\n"); }
  891.                         #endif
  892.  
  893.                         #ifdef ELAPSED
  894.                         fprintf(benchMarkFileElapsed, "Remove Noise           : ");
  895.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  896.  
  897.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  898.                         aggregatedElapsedTime = 0.0;
  899.                         #else
  900.                         fprintf(benchMarkFileCPU, "Remove Noise           : ");
  901.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  902.  
  903.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  904.                         aggregatedCPUTime = 0.0;
  905.                         #endif
  906.  
  907.                         free(initialImgArray);
  908.                 #endif
  909.  
  910.                 //removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
  911.                 //removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
  912.                 //removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
  913.                 //removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
  914.                
  915.                 #ifdef DEBUG_OUTPUT
  916.                         createImage(img, imageArray);
  917.                         //libbmp_write("imageNoiseRemoval.bmp", img);
  918.                         libbmp_write("imageStage7.bmp", img);
  919.                 #endif
  920.  
  921.                 ////////////////////////////////////////////////////////////////////////////////
  922.                 /* End Remove Noise                                                           */
  923.                 ////////////////////////////////////////////////////////////////////////////////
  924.  
  925.                 /**************************************************************************************************************/
  926.  
  927.                 ////////////////////////////////////////////////////////////////////////////////
  928.                 /* Find Centre Points                                                         */
  929.                 ////////////////////////////////////////////////////////////////////////////////
  930.  
  931.                 //getLineSizeWidth(img->height, img->width, imageArray);
  932.                 //getLineSizeHeight(img->height, img->width, imageArray);
  933.                 //createImage(img, imageArray);
  934.                 //libbmp_write("imageCentrePoints.bmp", img);
  935.  
  936.                 ////////////////////////////////////////////////////////////////////////////////
  937.                 /* End Find Centre Points                                                     */
  938.                 ////////////////////////////////////////////////////////////////////////////////
  939.  
  940.                 /**************************************************************************************************************/
  941.  
  942.                 ////////////////////////////////////////////////////////////////////////////////
  943.                 /* Calculate Area                                                             */
  944.                 ////////////////////////////////////////////////////////////////////////////////
  945.  
  946.                 #ifdef TITLES
  947.                 printf("-------------------Find Pixels\n");
  948.                 #endif
  949.  
  950.                 //libbmp_load("images/colourprocessed1CircleTest.bmp", img);
  951.                 //imageToArray(img, imageArray);
  952.  
  953.                 int *details = (int*) malloc(sizeof(int) * 3);
  954.  
  955.                 #ifdef BENCHMARKING
  956.                         initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  957.                         copyArray(imageArray, initialImgArray);
  958.  
  959.                         #ifdef ELAPSED
  960.                         if(initialiseTimer(&s)==0) {
  961.                         #endif
  962.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  963.                                         copyArray(initialImgArray, imageArray);
  964.  
  965.                                         #ifdef ELAPSED
  966.                                         startTimer(&s, processorMask);
  967.                                         #else
  968.                                         initCPUTime();
  969.                                         #endif
  970.                 #endif
  971.  
  972.                                 free(details);
  973.                                 details = findPixels(img->height, img->width, imageArray);
  974.  
  975.                 #ifdef BENCHMARKING
  976.                                         #ifdef ELAPSED
  977.                                         stopTimer(&s, processorMask);
  978.                                         #else
  979.                                         double endTime = getCPUTimeSinceStart();
  980.                                         #endif
  981.  
  982.                                         #ifdef ELAPSED
  983.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  984.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  985.                                         #else
  986.                                         printf("CPU Time: %f(ms)\n", endTime);
  987.                                         aggregatedCPUTime += endTime;
  988.                                         #endif
  989.                                 }
  990.                         #ifdef ELAPSED
  991.                         } else { printf("Find pixels timer didnt initialise\n"); }
  992.                         #endif
  993.  
  994.                         #ifdef ELAPSED
  995.                         fprintf(benchMarkFileElapsed, "Find Pixels            : ");
  996.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  997.  
  998.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  999.                         aggregatedElapsedTime = 0.0;
  1000.                         #else
  1001.                         fprintf(benchMarkFileCPU, "Find Pixels            : ");
  1002.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  1003.  
  1004.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  1005.                         aggregatedCPUTime = 0.0;
  1006.                         #endif
  1007.  
  1008.                         free(initialImgArray);
  1009.                 #endif
  1010.  
  1011.                 //details = findPixels(img->height, img->width, imageArray);
  1012.                 //printf("**************\n");
  1013.                 //printf("details[0] %d : details[1] %d : details[2] %d\n", details[0], details[1], details[2]); // largestArea, blobCoordI, blobCoordJ
  1014.                 //printf("**************\n");
  1015.  
  1016.                 #ifdef TITLES
  1017.                 printf("-------------------Find and Replace Pixels\n");
  1018.                 #endif
  1019.  
  1020.                 #ifdef BENCHMARKING
  1021.                         initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
  1022.                         copyArray(imageArray, initialImgArray);
  1023.  
  1024.                         //outputArrays(imageArray, "a", initialImgArray, "b");
  1025.  
  1026.                         #ifdef ELAPSED
  1027.                         if(initialiseTimer(&s)==0) {
  1028.                         #endif
  1029.                                 for (int i = 0; i < NUM_OF_PASSES; i++) {
  1030.                                         copyArray(initialImgArray, imageArray);
  1031.  
  1032.                                         #ifdef ELAPSED
  1033.                                         startTimer(&s, processorMask);
  1034.                                         #else
  1035.                                         initCPUTime();
  1036.                                         #endif
  1037.                 #endif
  1038.                                         //outputArray(imageArray, "b1");
  1039.                                         findAndReplace(imageArray, details[1], details[2], 4, 1);
  1040.                                         //outputArray(imageArray, "b2");
  1041.  
  1042.                 #ifdef BENCHMARKING
  1043.                                         #ifdef ELAPSED
  1044.                                         stopTimer(&s, processorMask);
  1045.                                         #else
  1046.                                         double endTime = getCPUTimeSinceStart();
  1047.                                         #endif
  1048.  
  1049.                                         #ifdef ELAPSED
  1050.                                         printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
  1051.                                         aggregatedElapsedTime += getElapsedTimeInMilli(&s);
  1052.                                         #else
  1053.                                         printf("CPU Time: %f(ms)\n", endTime);
  1054.                                         aggregatedCPUTime += endTime;
  1055.                                         #endif
  1056.                                 }
  1057.                         #ifdef ELAPSED
  1058.                         } else { printf("Find and replace timer didnt initialise\n"); }
  1059.                         #endif
  1060.  
  1061.                         //outputArrays(imageArray, "e", initialImgArray, "f");
  1062.  
  1063.                         #ifdef ELAPSED
  1064.                         fprintf(benchMarkFileElapsed, "Find and Replace Pixels: ");
  1065.                         fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  1066.  
  1067.                         printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
  1068.                         aggregatedElapsedTime = 0.0;
  1069.                         #else
  1070.                         fprintf(benchMarkFileCPU, "Find and Replace Pixels: ");
  1071.                         fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
  1072.  
  1073.                         printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
  1074.                         aggregatedCPUTime = 0.0;
  1075.                         #endif
  1076.  
  1077.                         free(initialImgArray);
  1078.                         free(details);
  1079.                 #endif
  1080.  
  1081.                 #ifdef DEBUG_OUTPUT
  1082.                         // Redraw the noise removal image once the noise and fluke have been distinguished.
  1083.                         createImage(img, imageArray);
  1084.                         //libbmp_write("imageNoiseRemoval.bmp", img);
  1085.                         libbmp_write("imageStage8.bmp", img);
  1086.                 #endif
  1087.                
  1088.                 double area = calculateArea(imageArray);
  1089.                 //printf("Area: %g \n", area);
  1090.                 //createImage(img, imageArray);
  1091.                 //sprintf(imageFileName, "images/colourprocessedi%d.bmp", (imageIndex+1));
  1092.                 //libbmp_write(imageFileName, img);
  1093.  
  1094.                 //outputArray(imageArray, "b3");
  1095.  
  1096.  
  1097.                
  1098.                 //double perimiter = calculatePerimeter("imageNoiseRemoval.bmp", img, imageArray);
  1099.                 //if (imageIndex == 11)
  1100.                 //      printf("ThreadID: %d imageIndex: %d image: %d\n", threadID, imageIndex, (imageIndex+1));
  1101.                 double perimiter = calculatePerimeter(img, imageArray);
  1102.                 double compactness = calculateCompactness(perimiter, area);
  1103.                 //printf("Area: %g \n", area);
  1104.                 //printf("Perimiter: %g \n", perimiter);
  1105.                 //printf("Compactness: %g \n", compactness); // printing the compactness value before adding it to the array
  1106.                                                                                                          // seems to cause assignment problems - some values are set to
  1107.                                                                                                          // -0.0
  1108.  
  1109.                 //if (threadID == 2)
  1110.                         parasiteCompactness[imageIndex] = (double) compactness;
  1111.  
  1112.                 //// Calculate bounding box
  1113.                 //findBoundingBox(img->height, img->width, imageArray);
  1114.                 //createImage(img, imageArray);
  1115.                 //char* boundingImageFileName = (char*)malloc(sizeof(char)*19);
  1116.                 //strcpy(boundingImageFileName, "images/imageNoiseBounding");
  1117.                 //strcat(boundingImageFileName, imageIndexString);
  1118.                 //strcat(boundingImageFileName, ".bmp");
  1119.                 //libbmp_write(boundingImageFileName, img);
  1120.  
  1121.  
  1122.  
  1123.                 ////////////////////////////////////////////////////////////////////////////////
  1124.                 /* End Calculate Area                                                         */
  1125.                 ////////////////////////////////////////////////////////////////////////////////
  1126.  
  1127.                 /**************************************************************************************************************/
  1128.  
  1129.  
  1130.                 ////////////////////////////////////////////////////////////////////////////////
  1131.                 /* Find Parasite Size                                                         */
  1132.                 ////////////////////////////////////////////////////////////////////////////////
  1133.                 parasiteSize[imageIndex] = (short) getSizeOfBlobCircleTest(img->height, img->width, imageArray);
  1134.                 ////////////////////////////////////////////////////////////////////////////////
  1135.                 /* End Find Parasite Size                                                     */
  1136.                 ////////////////////////////////////////////////////////////////////////////////
  1137.                
  1138.  
  1139.                 /**************************************************************************************************************/
  1140.  
  1141.                 createImage(img, imageArray);
  1142.  
  1143.                 //strcpy(imageFileName, (char*) argv[1]);
  1144.                 //strcat(imageFileName, "\\colour");
  1145.                 //strcat(imageFileName, "processed");
  1146.                 //strcat(imageFileName, imageIndexString);
  1147.                 //strcat(imageFileName, ".bmp");
  1148.  
  1149.                 sprintf(imageFileName, "images/colourprocessed%d.bmp", (imageIndex+1));
  1150.                 libbmp_write(imageFileName, img);
  1151.  
  1152.                 /**************************************************************************************************************/
  1153.  
  1154.                 ////////////////////////////////////////////////////////////////////////////////
  1155.                 /* Create new image name for next image                                       */
  1156.                 ////////////////////////////////////////////////////////////////////////////////
  1157.                
  1158.                 //printf("image index: %d \n", imageIndex);
  1159.                 imageIndex++;
  1160.                 //itoa(imageIndex,imageIndexString,10); // void itoa(int input, char *buffer, int radix) - base radix: 10 (decimal)
  1161.  
  1162.                 //strcpy(imageFileName, "images/colour");
  1163.                 //strcat(imageFileName, imageIndexString);
  1164.                 //strcat(imageFileName, ".bmp");
  1165.  
  1166.                 sprintf(imageFileName,"images/colour%d.bmp", (imageIndex+1));
  1167.  
  1168.                 //strcpy(imageFileName, (char*) argv[1]);
  1169.                 //strcat(imageFileName, "\\colour");
  1170.                 //strcat(imageFileName, imageIndexString);
  1171.                 //strcat(imageFileName, ".bmp");
  1172.  
  1173.                 ////////////////////////////////////////////////////////////////////////////////
  1174.                 /* End Create new image name for next image                                   */
  1175.                 ////////////////////////////////////////////////////////////////////////////////
  1176.  
  1177.                 #ifdef BENCHMARKING
  1178.                         #ifdef ELAPSED
  1179.                         fprintf(benchMarkFileElapsed, "\n");
  1180.                         #else
  1181.                         fprintf(benchMarkFileCPU, "\n");
  1182.                         #endif
  1183.                 #endif
  1184.  
  1185.                 // Free all allocated memory           
  1186.                 for(int i = 0; i < img->height; i++)
  1187.                         free(img->data[i]);
  1188.                 free(img->data);
  1189.  
  1190.                 fflush(stdout);
  1191.         }
  1192.  
  1193.         #ifdef BENCHMARKING
  1194.                 #ifdef ELAPSED
  1195.                         fclose(benchMarkFileElapsed);
  1196.                 #else
  1197.                         fclose(benchMarkFileCPU);
  1198.                 #endif
  1199.         #endif
  1200.  
  1201.         printf("ThreadID %d cleaning up\n", threadID);
  1202.         free(img);
  1203.         free(imageArray);
  1204.         free(imageFileName);
  1205.         fflush(NULL);
  1206.  
  1207.         //printf("***********************************************************************\n\n");
  1208.  
  1209.         if (threadID != 0)
  1210.         {
  1211.                 printf("ThreadID %d has ended\n", threadID);
  1212.                 fflush(stdout);
  1213.                 _endthread();
  1214.         }
  1215. }