Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.74 KB | None | 0 0
  1. /*------------------------------------------------------
  2. Student's Name: Victoria Whiffen
  3. Student's email address: vw705@uowmail.edu.au
  4. Lab group (CL/01, CL/02,...): CL/04
  5. Purpose of this assignment: Modular design, Pointers,
  6. Dynamic arrays 1D and 2D, Binary files
  7. -------------------------------------------------------*/
  8. /* Header Files */
  9. /*The standard input and output library is added to utilise the fopen, fclose, fread, fseek, fprintf functions*/
  10. #include <stdio.h>
  11. /*The general utilities library is included to utilise the calloc and free functions*/
  12. #include <stdlib.h>
  13. /*the math library is included to utilise the pow and sqrt function*/
  14. #include <math.h>
  15. /*the boolen library is included to utilise true and false values*/
  16. #include <stdbool.h>
  17.  
  18. /* Structure Definitions */
  19. /*EACH STRUCTURE IS PACKED TO ENSURE NO SLACK BYTES ARE USED*/
  20. /*the structure FileHeader is used to store the header information of a file*/
  21. /*the important members of this structure include the two fileMarkers and the imageDataOffset*/
  22. /*fileMarker1 and fileMarker2 will indicate whether the file is in BMP format*/
  23. /*the imageDataOffset will indicate the position from the start of the file that the image data starts*/
  24. #pragma pack(push, 1)
  25. typedef struct
  26. {
  27. char fileMarker1;
  28. char fileMarker2;
  29. unsigned int bfSize;
  30. unsigned short unused1;
  31. unsigned short unused2;
  32. unsigned int imageDataOffset;
  33. }FileHeader;
  34. #pragma pop
  35.  
  36. /*the structure InfoHeader is used to store the information header of a file*/
  37. /*the important members of this structure include: the width and height*/
  38. /*the width indicates the number of columns and the height the number of rows that the image data contains*/
  39. #pragma pack(push, 1)
  40. typedef struct
  41. {
  42. unsigned int biSize;
  43. int width;
  44. int height;
  45. unsigned short planes;
  46. unsigned short bitPix;
  47. unsigned int biCompression;
  48. unsigned int biSizeImage;
  49. int biXPelsPerMeter;
  50. int biYPelsPerMeter;
  51. unsigned int biClrUsed;
  52. unsigned int biClrImportant;
  53. }InfoHeader;
  54. #pragma pop
  55.  
  56. /*the structure Pixel is used to store the blue, green and red components of each pixel*/
  57. /*the blue, green and red components are contained in the b,g and r members respectively*/
  58. #pragma pack(push, 1)
  59. typedef struct
  60. {
  61. unsigned char b;
  62. unsigned char g;
  63. unsigned char r;
  64. }Pixel;
  65. #pragma pop
  66.  
  67. /*the structure BMPstatistics is used to store the statistics of the BMP image calculated in the program*/
  68. /*the first three members are used to contain the min, max and mean intensity of the image*/
  69. /*the fourth member is an error flag for the standard deviation*/
  70. /*it will be set to true when a division by zero may occur to prevent this operation*/
  71. /*otherwise it will be set to false and the standard deivation will be calculated then stored in the fifth member*/
  72. #pragma pack(push, 1)
  73. typedef struct
  74. {
  75. unsigned int minimumIntensity;
  76. unsigned int maximumIntensity;
  77. unsigned int meanIntensity;
  78. bool stdDeviationError;
  79. float standardDeviation;
  80. }BMPstatistics;
  81. #pragma pop
  82.  
  83. /* Function Prototypes */
  84. /*the function filestreamCheck will check whether a filestream exists*/
  85. short int filestreamCheck( FILE *filestream , char filename[] );
  86. /*the function headerCheck will inspect the headerData from a file and determine whether the file is in BMP format*/
  87. short int headerCheck( FileHeader headerData );
  88. /*the function allocFrameBuffer will allocate a 2-D array to contain the intensity of each pixel in the image data from the BMP file*/
  89. unsigned char **allocFrameBuffer( int height, int width );
  90. /*the function fillFrameBuffer will fill the 2-D array with the intensity of each pixel in the image data from the BMP file*/
  91. unsigned char **fillFrameBuffer(FILE *FilePointer, int fBufferWidth, int fBufferHeight, Pixel *LineBuffer, unsigned char **FrameBuffer);
  92. /*the function calcBMPstatistics will calculate the min, max, mean and standard deviation of the intensities of the image data*/
  93. BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities);
  94. /*the function printBMPstatistics will validate and print the statistics calculated in the previous function*/
  95. void printBMPstatistics( BMPstatistics BMPstats );
  96.  
  97. /*argc is an integer that will contain the value of the parameters given at the command line*/
  98. /*argv[] is a pointer to an char array that contains a string of each input*/
  99. int main( int argc, char *argv[] )
  100. {
  101. /*a file object filePointer is declared which will be used to control the filestream*/
  102. FILE *filePointer;
  103. /*a strucuture headerData of type FileHeader is declared to later contain the file header of the file*/
  104. FileHeader headerData;
  105. /*a structure infoHeaderData of type InfoHeader is declared to later contain the info header of the BMP file*/
  106. InfoHeader infoHeaderData;
  107. /*a single pointer lineBuffer of type Pixel is declared*/
  108. Pixel *lineBuffer;
  109. /*a double pointer frameBuffer pf type unsigned char is declared*/
  110. unsigned char **frameBuffer;
  111. /*a structure bmpStats of type BMPstatistics is used to store the statistics of the BMP file and an error flag*/
  112. BMPstatistics bmpStats;
  113.  
  114. /*if there are two parameters at the command line argc == 2*/
  115. if( argc == 2 )
  116. {
  117. /*filePointer is used to store the result of opening the second file name given at the command line*/
  118. /*fopen will open argv[1] which is the second input in the mode rb which is reading mode for a binary file*/
  119. filePointer = fopen(argv[1],"rb");
  120. /*the filestreamCheck function will determine if the filestream filePointer exists*/
  121. /*if the filestream filePointer exists the function call will be return 1*/
  122. if( filestreamCheck(filePointer, argv[1]) == 1 )
  123. {
  124. /*the fread function will read 1 element of size headerData and store it in headerData*/
  125. /*if the fread function is successful it will return 1*/
  126. if( fread(&headerData,sizeof(headerData),1,filePointer) == 1 )
  127. {
  128. /*the headerCheck function will check the contents of the structure headerData*/
  129. /*if the contents of the structure confirm the file is of BMP format the funciton will return 1*/
  130. if( headerCheck(headerData) == 1 )
  131. {
  132. /*the fread function will read 1 element of size infoHeaderData and store it in infoHeaderData*/
  133. /*if the fread function is successful it will return 1)*/
  134. if ( fread(&infoHeaderData,sizeof(infoHeaderData),1,filePointer) == 1 )
  135. {
  136. /*lineBuffer is initialised to a 1_D dynamically allocated array*/
  137. /*the array has infoHeaderData.width elements and each element is of size Pixel*/
  138. /*the memory allocated using the calloc function which is type casted to a Pixel pointer from void*/
  139. lineBuffer = (Pixel*)calloc( infoHeaderData.width, sizeof(Pixel) );
  140. /*if the memory is successfully allocated it will not have a NULL value*/
  141. if( lineBuffer != NULL )
  142. {
  143. /*frameBuffer is initialised to a 2-D dynamically allocated array using the function allocFrameBuffer*/
  144. /*the array has infoHeaderData.width*infoHeaderData.height elements each of size unsigned char*/
  145. frameBuffer = allocFrameBuffer(infoHeaderData.width,infoHeaderData.height);
  146. /*if the memory is successfully allocated it will not have a NULL value*/
  147. if( frameBuffer != NULL )
  148. {
  149. /*the fseek function will move the pointer filePointer from the start of the file indicated by SEEK_SET*/
  150. /*it will move it headerData.imageDataOffset units*/
  151. /*if the fseek function is successful it will return 0*/
  152. if (fseek(filePointer,headerData.imageDataOffset,SEEK_SET) == 0)
  153. {
  154. /*the fillFrameBuffer function is called to fill frameBuffer with the intenisty of each pixel of the image data*/
  155. frameBuffer = fillFrameBuffer(filePointer,infoHeaderData.width,infoHeaderData.height,lineBuffer, frameBuffer);
  156. /*after the frameBuffer is filled bmpstats is initialised with the return value of the function calcBMPstatistics*/
  157. /*calcBMPstatistics will calculate the min, max, mean and standard deviation of the intensities in frameBuffer*/
  158. /*it will return all the values calculated in a structure including a possible error flag*/
  159. bmpStats = calcBMPstatistics(infoHeaderData.height,infoHeaderData.width,frameBuffer);
  160. /*the printBMPstatistics function is called to print the data from the bmpStats structure*/
  161. printBMPstatistics(bmpStats);
  162. }
  163. /*if the fseek function is unsuccessful an error message is displayed through the error stream*/
  164. else fprintf(stderr,"An error has occurred when moving the file pointer to the start of the image data \n");
  165. /*the free function called on frameBuffer will free the memory allocated for frameBuffer to prevent a memory leak*/
  166. free(frameBuffer);
  167. }
  168. /*the free function called on the lineBuffer will free the memory allocated for lineBuffer to prevent a memory leak*/
  169. free(lineBuffer);
  170. }
  171. /*if the the memory was not correctly allocated for linebuffer an error message is displayed through the error stream*/
  172. else fprintf(stderr,"An error has occurred when allocating memory for lineBuffer \n");
  173. }
  174. /*if the infoHeaderData was unable to be read an error message is displayed through the error stream*/
  175. else fprintf(stderr,"An error has occurred when reading the information header of the file \n");
  176. }
  177. }
  178. /*if the headerData ws unable to be read an error message is displayed through the error stream*/
  179. else fprintf(stderr,"An error has occured when reading the header of the file \n");
  180. /*the fclose function is called on filePointer to close the file and dissociate it from the stream*/
  181. fclose(filePointer);
  182. }
  183. }
  184.  
  185. /*if there is not 2 parameters at the command line an the error message insufficient number of parameters is displayed*/
  186. else fprintf(stderr,"Insufficient number of parameters \n");
  187. /*the message the program is terminated is used to indicate the end of the program*/
  188. fprintf(stderr,"The program is terminated \n");
  189. return 0;
  190. }
  191.  
  192. /* Function Definitions */
  193. /*the function filestreamCheck verifies a filestream*/
  194. /*parameters: a file object pointer and a character array*/
  195. /*output: a short int*/
  196. /*the function checks if the filestream exists, if it doesn't exist an error message is displayed*/
  197. short int filestreamCheck( FILE *filestream , char filename[] ) /*filestreamCheck function stub*/
  198. { /*short int filestreamCheck(FILE *filestream,char filename[]){*/
  199. /*returnvalue is initialised as a short int and is used as the return value of the function*/ /*fprintf(stderr,"Function filestreamCheck entered\n");*/
  200. short int returnvalue; /*fprintf(stderr,"Checking if the filestream exists\n");*/
  201. /*if the filestream equals NULL then it doesn't exist*/ /*if(filestream==NULL){*/
  202. if( filestream == NULL) /*fprintf(stderr,"Can't open %s \n",filename);*/
  203. { /*return -1;}*/
  204. /*if the filestream doesn't exist an error message is displayed through the error stream*/ /*else{*/
  205. /*the error message will display that it cannot open the file provided*/ /*fprintf(stderr,"%s can be opened \n",filename);*/
  206. fprintf(stderr,"Can't open %s \n",filename); /*return 1;}}*/
  207. /*the returnvalue is initilised as -1*/
  208. returnvalue = -1;
  209. }
  210. /*if the filestream is not equal to NULL the returnvalue is initialised as 1*/
  211. else returnvalue = 1;
  212. /*the returnvalue is then returned to the main function*/
  213. return returnvalue;
  214. }
  215.  
  216. /*the function headerCheck verifies if a file is in BMP format*/
  217. /*input: a structure of type FileHeader*/
  218. /*output: short int*/
  219. short int headerCheck( FileHeader headerData ) /*headerCheck function stub*/
  220. { /*short int headerCheck(FileHeader headerData){*/
  221. /*returnvalue is initialised as a short int and is used as the return value of the function*/ /*fprintf(stderr,"Function headerCheck entered \n");*/
  222. short int returnvalue; /*if( headerData.fileMarker1 == 'B' && headerData.fileMarker2 == 'M'){*/
  223. /*a file is a BMP is the two fileMarkers in the header are the characters B and M respectively*/ /*fprintf(stderr,"The file is confirmed as BMP \n");*/
  224. /*this is verified by checking the values of the members filemarker1 and filemarker2 in the headerData*/ /*return 1;}*/
  225. /*if headerData.fileMarker1 equals B and headerData.fileMarker2 equals M the file is verified*/ /*else{*/
  226. /*if the file is verified as a BMP returnvalue is initilised as 1*/ /*fprintf(stderr,"Incorrect File Format \n");*/
  227. if( headerData.fileMarker1 == 'B' && headerData.fileMarker2 == 'M') returnvalue = 1; /*return -1;}}*/
  228. else
  229. {
  230. /*if the file is not verified as a BMP an error message is displayed through the error stream*/
  231. /*this bypasses the buffer and is done by using the fprintf function which displays incorrect file format*/
  232. fprintf(stderr,"Incorrect File Format \n");
  233. /*the return value is initilised as -1*/
  234. returnvalue = -1;
  235. }
  236. /*the returnvalue is then returned to the main function*/
  237. return returnvalue;
  238. }
  239.  
  240. /*the function allocFrameBuffer dynamically allocates a block of memory for the frameBuffer*/
  241. /*parameters: two integers which are the height and width of the array of memory*/
  242. /*output: double pointer of type unsigned char, which is the frameBuffer*/
  243. unsigned char **allocFrameBuffer( int bufferHeight, int bufferWidth ) /*allocFrameBuffer function stub*/
  244. { /*unsigned char **allocFrameBuffer(int height, int width)*/
  245. /*two integers counter and number of elements are declared*/ /*fprintf(stderr,"Function allocFrameBuffer entered\n");*/
  246. /*counter is used to iterate through height to add rows to the array*/ /*fprintf(stderr,"The height entered is %d\n", height);*/
  247. /*numberofelements is used to store the number of elements in the array*/ /*fprintf(stderr,"The width entered is %d\n", width);*/
  248. int counter, numberofelements; /*int counter, numberofelements;*/
  249. /*two double pointer unsigned chars and one single pointer unsigned char are declared*/ /*unsigned char **returnvalue,*pointer1Darray,**pointer2Darray;*/
  250. /*the returnvalue is used to contain the value returned by the function*/ /*numberofelements=height*width;*/
  251. /*the pointer1Darray is used to contain a 1D dynamically allocated array*/ /*fprintf(stderr,"The number of elements of the array is %d\n", numberofelements);*/
  252. /*the pointer2Darray is used to contain a 2D dynamically allocated array*/ /*pointer1Darray=(unsigned char*)calloc(numberofelements,sizeof(unsigned char));*/
  253. unsigned char **returnvalue, *pointer1Darray, **pointer2Darray; /*if(pointer1Darray == NULL){*/
  254. /*numberofelements is initialised as the multiplication of the height and width of the array*/ /*fprintf(stderr,"An error has occurred allocating the memory for the 1D array\n");*/
  255. numberofelements = bufferHeight*bufferWidth; /*free(pointer1Darray);*/
  256. /*pointer1Darray is initilised as a type case of unsigned char pointer of a calloc function*/ /*return NULL;}*/
  257. /*the calloc call dynamically allocates an array with height*width elements of size unsigned char*/ /*pointer2Darray = (unsigned char**)calloc( height, sizeof(unsigned char*) );*/
  258. pointer1Darray = (unsigned char*)calloc( numberofelements, sizeof(unsigned char) ); /*if(pointer2Darray == NULL){*/
  259. /*if the array is not declared properly it will be equal to NULL*/ /*fprintf(stderr,"An error has occurred allocating the memory for the 2D array\n");*/
  260. if( pointer1Darray == NULL ) /*free(pointer2Darray);*/
  261. { /*return NULL;*/
  262. /*if the array was unable to be declared the pointer tothe 1D array pointer1Darray is freed*/ /*for(counter = 0; counter<height; counter++){*/
  263. free(pointer1Darray); /*pointer2Darray[counter] = pointer1Darray + counter*width;*/
  264. /*the returnvalue is then initialised as NULL*/ /*fprintf(stderr,"%d bytes allocated from %p\n",sizeof(pointer2Darray),pointer2Darray);*/
  265. returnvalue = NULL; /*return pointer2Darray;}}*/
  266. /*an error message is returned through the error stream to communicate there was an error in memory allocation*/
  267. fprintf(stderr,"An error in memory allocation of frameBuffer has occurred \n");
  268. }
  269. /*pointer2Darray is initilised as a type cast of unsigned char double pointer of a calloc function*/
  270. /*there are a height number of elements of size unsigned char pointer*/
  271. pointer2Darray = (unsigned char**)calloc( bufferHeight, sizeof(unsigned char*) );
  272. /*if the array is not declared properly it will be equal to NULL*/
  273. if( pointer2Darray == NULL )
  274. {
  275. /*if the array was unable to be declared the pointer to the 2D array pointer2Darray is freed*/
  276. free(pointer2Darray);
  277. /*the returnvalue is then initialised as NULL*/
  278. returnvalue = NULL;
  279. /*an error message is returned through the error stream to communicate there was an error in memory allocation*/
  280. fprintf(stderr,"An error in memory allocation of frameBuffer has occurred \n");
  281. }
  282. /*if the memory has been allocated properly a for loop initialises each element of the 2D array*/
  283. /*the for loop has a counter that stops before reaching the int value for height*/
  284. for ( counter = 0; counter<bufferHeight; counter++ )
  285. {
  286. /*the index of counter is initialised as the addition of the 1D array and counter*width*/
  287. /*this will create a 2D array with dimensions height*width and elements of type unsigned char*/
  288. pointer2Darray[counter] = pointer1Darray + counter*bufferWidth;
  289. }
  290. /*after the for loop the returnvalue is declared as pointer2Darray*/
  291. returnvalue = pointer2Darray;
  292. /*returnvalue is then returned to the main function*/
  293. return returnvalue;
  294. }
  295.  
  296. /*the function fillFrameBuffer will fill the dynamically allociated array FrameBuffer with the intensity of each pixel of the BMP image*/ /*function stub for fillFrameBuffer*/
  297. /*parameters: filePointer of type FILE, two ints fBufferWidth and fBuffer Height, pointer LineBuffer of type Pixel, double pinter FrameBuffer of type unsigned char*/ /*unsigned char **fillFrameBuffer(FILE *FilePointer, int fBufferWidth, int fBufferHeight, Pixel *LineBuffer, unsigned char **FrameBuffer){*/
  298. /*returns: double pointer of type unsigned char, which is the filled frameBuffer*/ /*int counterRow, counterCol;*/
  299. unsigned char **fillFrameBuffer(FILE *FilePointer, int fBufferWidth, int fBufferHeight, Pixel *LineBuffer, unsigned char **FrameBuffer) /*unsigned char intensity;*/
  300. { /*for( counterRow = 0; counterRow < fBufferHeight; counterRow ++){*/
  301. /*two ints counterRow and counterCol are declared to be used in for loops to iterate through the FrameBuffer*/ /*if( fread(LineBuffer,sizeof(Pixel),fBufferWidth,FilePointer) == fBufferWidth ){*/
  302. int counterRow, counterCol; /*for( counterCol = 0; counterCol < fBufferWidth; counterCol++){*/
  303. /*the unsigned char intensity is declared to contain the intensity of each pixel in the image*/ /*intensity = ((LineBuffer[counterCol].b + LineBuffer[counterCol].g + LineBuffer[counterCol].r)/3);*/
  304. unsigned char intensity; /*fprintf("The intensity of the current pixel is: %d,&intensity)*/
  305. /*the outer for loop iterates through for each row of the FrameBuffer*/ /*FrameBuffer[counterRow][counterCol] = intensity;*/
  306. for( counterRow = 0; counterRow < fBufferHeight; counterRow ++) /*fprintf("The value of the current index is %d,FrameBuffer[counterRow][counterCol])}}*/
  307. { /*else fprintf(stderr,"An error has occurred when reading the image data into the lineBuffer \n");*/
  308. /*for each row the fread function is called*/ /*return FrameBuffer;}*/
  309. /*the fread function will read infoHeaderData.width elements of size Pixel into the LineBuffer*/
  310. /*if the fread function is successful it will return infoHeaderData.width*/
  311. if( fread(LineBuffer,sizeof(Pixel),fBufferWidth,FilePointer) == fBufferWidth )
  312. {
  313. /*the inner for loop will iterate through each element of the lineBuffer*/
  314. /*this is done by using the counter counterCol and increasing until it is not less than the infoHeaderData.width*/
  315. for( counterCol = 0; counterCol < fBufferWidth; counterCol++)
  316. {
  317. /*for each element of the lineBuffer the average of the blue, green and red component is calculated*/
  318. intensity = ((LineBuffer[counterCol].b + LineBuffer[counterCol].g + LineBuffer[counterCol].r)/3);
  319. /*this calculate valued is stored into the FrameBuffer*/
  320. /*the index of that value is row - counterRow and column - counterCol*/
  321. FrameBuffer[counterRow][counterCol] = intensity;
  322. }
  323. }
  324. else
  325. {
  326. /*if the fread function is unsuccessful an error message is desplayed through the error stream*/
  327. fprintf(stderr,"An error has occurred when reading the image data into the lineBuffer \n");
  328. }
  329. }
  330. /*the filled FrameBuffer is returned, if an error has occurred it will be a NULL pointer*/
  331. return FrameBuffer;
  332. }
  333.  
  334. /*the function calcBMPstatistics calculates the minimum, maximum, mean and standard deviation of the intensities of a BMP image*/
  335. /*parameters: unsigned char double pointer to the array of intensities and two integers that give the dimensions of the array */
  336. /*output: structure of type BMPstatistics*/
  337. BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities) /*calcBMPstatistics function stub*/
  338. { /*BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities){*/
  339. /*a structure of type BMPstatistics called statData is declared*/ /*fprintf(stderr,"The array has %d rows\n", numberOfrows);*/
  340. BMPstatistics statData; /*fprintf(stderr,"The array has %d columns %d\n", numberOfcols);*/
  341. /*three integers counterRow, counterCol and numberOfelements are declared*/ /*BMPstatistics statData;*/
  342. /*counterRow and counterCol are used as counters in for loops to iteratre through the rows and columns of the array*/ /*int counterRow, counterCol, numberOfelements;*/
  343. int counterRow, counterCol, numberOfelements; /*float sum = 0, sum4stddev = 0;*/
  344. /*two floats sum and sum4stddev are declared, sum is used to store the total of all the intensities*/ /*statData.minimumIntensity = arrayOfIntensities[0][0];*/
  345. /*sum4stddev is used to store the summation needed to calculate the standard deviation*/ /*statData.maximumIntensity = arrayOfIntensities[0][0];*/
  346. float sum = 0, sum4stddev = 0; /*numberOfelements = numberOfrows*numberOfcols;*/
  347. /*the minimum and maximum intensity in the structure stat data are initialised as the first intensity of the array*/ /*fprintf(stderr,"The array has %d elements\n", numberOfelements);*/
  348. statData.minimumIntensity = arrayOfIntensities[0][0]; /*for( counterRow = 0; counterRow < numberOfrows; counterRow ++){*/
  349. statData.maximumIntensity = arrayOfIntensities[0][0]; /*for( counterCol = 0; counterCol < numberOfcols; counterCol ++){*/
  350. /*numberOfelements is declared as the multiplication of the number of rows and columns of the intenisties array*/ /*sum4stddev += pow(((float)arrayOfIntensities[counterRow][counterCol]-(float)statData.meanIntensity),2);*/
  351. numberOfelements = numberOfrows*numberOfcols; /*fprintf(stderr,"The current sum for standard deviation is %f\n",sum4stddev);}}*/
  352. /*the outer for loop iterates untill the counter, counterRow is still less than the number of rows of the array*/ /*statData.stdDeviationError = false;*/
  353. for( counterRow = 0; counterRow < numberOfrows; counterRow ++) /*if(numberOfelements == 1){*/
  354. { /*fprintf(stderr,"The standard deivation is unable to be calculated\n");*/
  355. /*the inner for loop iterates untill the counter, counterCol is still less than the number of columns of the array*/ /*statData.stdDeviationError = true;}*/
  356. for( counterCol = 0; counterCol < numberOfcols; counterCol ++) /*else{*/
  357. { /*statData.standardDeviation = sqrt( ((1/((float)numberOfelements-1))*sum4stddev) );*/
  358. /*the total sum is calculated with float precision by type casting each index to a float before adding it to the previous*/ /*fprintf(stderr,"The standard deivation is %f\n",statData.standardDeviation);}*/
  359. sum += (float)arrayOfIntensities[counterRow][counterCol]; /*return statData}*/
  360. /*each index is compared to see if its value is less than the set minimum intensity*/
  361. if( arrayOfIntensities[counterRow][counterCol] < statData.minimumIntensity )
  362. {
  363. /*if the current index is less than the set minimum it is reinitilised as the new minimum*/
  364. statData.minimumIntensity = arrayOfIntensities[counterRow][counterCol];
  365. }
  366. /*each index is compared to see if its value is greater than the set maximum intensity*/
  367. if( arrayOfIntensities[counterRow][counterCol] > statData.maximumIntensity )
  368. {
  369. /*if the current index is greater than the set maximum it is reinitilised as the new maximum*/
  370. statData.maximumIntensity = arrayOfIntensities[counterRow][counterCol];
  371. }
  372. }
  373. }
  374. /*the mean is then calculated and stored in the structure statData in the member meanIntensity*/
  375. /*the mean is calculated by dividing the sum by the number of elements in the array of intensities*/
  376. statData.meanIntensity = sum/numberOfelements;
  377. /*the outer for loop iterates untill the counter, counterRow is still less than the number of rows of the array*/
  378. for( counterRow = 0; counterRow < numberOfrows; counterRow ++)
  379. {
  380. /*the inner for loop iterates untill the counter, counterCol is still less than the number of columns of the array*/
  381. for( counterCol = 0; counterCol < numberOfcols; counterCol ++)
  382. {
  383. /*the square of the difference between the current index and the mean is summed to form the sum for standard deviation*/
  384. /*for this operation to work each index and well as the mean have to be type casted to float*/
  385. sum4stddev += pow(((float)arrayOfIntensities[counterRow][counterCol]-(float)statData.meanIntensity),2);
  386. }
  387. }
  388. /*the error flag stdDeviationError in the structure statData is initialised as false before the division by zero check is made*/
  389. statData.stdDeviationError = false;
  390. /*a possible division by zero can only occur when there is 1 element in the array*/
  391. /*when this is true numberOfelements is equal to 1*/
  392. if(numberOfelements == 1)
  393. {
  394. /*when this is true the error flag is reintialised as true and the standard deviation is not calculated*/
  395. statData.stdDeviationError = true;
  396. }
  397. /*when there is more than 1 element in the array, numberOfelements is greater than 1 the standard deivation is calculated*/
  398. else
  399. {
  400. /*standard deviation is calculated by taking the square root of (1/numberOfelements-1) mutiplied by the sum calculated previously*/
  401. /*for this operation to work the number of elements has to be type casted to a float*/
  402. /*the standard deviation is then stored in the the structure statData under the member standardDeviation*/
  403. statData.standardDeviation = sqrt( ((1/((float)numberOfelements-1))*sum4stddev) );
  404. }
  405. /*after all the statistics are calculated the structure statData is then returned to the main function*/
  406. return statData;
  407. }
  408.  
  409. /*the function printBMPstatistics validates and prints the calculated statistics*/
  410. /*input: a structure of type BMPstatistics*/
  411. /*output: void*/
  412. void printBMPstatistics( BMPstatistics BMPstats )
  413. { /*printBMPstatistics function stub*/
  414. /*the message results of statistical analysis is printed via the error stream to bypass a filter*/ /*void printBMPstatistics( BMPstatistics BMPstats ){*/
  415. printf("*** Results of the statistical analysis *** \n"); /*fprintf(stderr,"*** Results of the statistical analysis *** \n");*/
  416. /*the mean, min, max and standard deviation is then printed the same way*/ /*fprintf(stderr,"Mean: %d \n",BMPstats.meanIntensity);*/
  417. /*the mean stored in the member meanIntensity in the structure BMPstats as an integer using %d syntax*/ /*fprintf(stderr,"Min Intensity: %d \n",BMPstats.minimumIntensity);*/
  418. printf("Mean: %d \n",BMPstats.meanIntensity); /*fprintf(stderr,"Max Intensity: %d \n",BMPstats.maximumIntensity);*/
  419. /*the min stored in the member minimumIntensity in the structure BMPstats as an integer using %d syntax*/ /*if(BMPstats.stdDeviationError == true){*/
  420. printf("Min Intensity: %d \n",BMPstats.minimumIntensity); /*fprintf(stderr,"Standard Deviation: NA \n");}*/
  421. /*the max stored in the member maximiumIntensity in the structure BMPstats as an integer using %d syntax*/ /*else{*/
  422. printf("Max Intensity: %d \n",BMPstats.maximumIntensity);
  423. /*the error flag stdDeviationError is checked to see if it is true*/
  424. /*if it is true it means that the standard deviation could not be calculated*/
  425. if(BMPstats.stdDeviationError == true)
  426. {
  427. /*if the standard deviation could not be calculated the value is displayed as NA*/
  428. /*this value is printed through the error stream to indicate and error has occurred*/
  429. fprintf(stderr,"Standard Deviation: NA \n");
  430. }
  431. else
  432. {
  433. /*if the error flag is false it means the standard deviation could be calculated*/
  434. /*it is then printed with 1 decimal point precision by the sytax %.1f*/
  435. /*the standard deiviation is accessed through the member standardDeviation in the structure BMPstats*/
  436. printf("Standard Deviation: %.1f \n",BMPstats.standardDeviation);
  437. }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement