Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------------------
- Student's Name: Victoria Whiffen
- Student's email address: vw705@uowmail.edu.au
- Lab group (CL/01, CL/02,...): CL/04
- Purpose of this assignment: Modular design, Pointers,
- Dynamic arrays 1D and 2D, Binary files
- -------------------------------------------------------*/
- /* Header Files */
- /*The standard input and output library is added to utilise the fopen, fclose, fread, fseek, fprintf functions*/
- #include <stdio.h>
- /*The general utilities library is included to utilise the calloc and free functions*/
- #include <stdlib.h>
- /*the math library is included to utilise the pow and sqrt function*/
- #include <math.h>
- /*the boolen library is included to utilise true and false values*/
- #include <stdbool.h>
- /* Structure Definitions */
- /*EACH STRUCTURE IS PACKED TO ENSURE NO SLACK BYTES ARE USED*/
- /*the structure FileHeader is used to store the header information of a file*/
- /*the important members of this structure include the two fileMarkers and the imageDataOffset*/
- /*fileMarker1 and fileMarker2 will indicate whether the file is in BMP format*/
- /*the imageDataOffset will indicate the position from the start of the file that the image data starts*/
- #pragma pack(push, 1)
- typedef struct
- {
- char fileMarker1;
- char fileMarker2;
- unsigned int bfSize;
- unsigned short unused1;
- unsigned short unused2;
- unsigned int imageDataOffset;
- }FileHeader;
- #pragma pop
- /*the structure InfoHeader is used to store the information header of a file*/
- /*the important members of this structure include: the width and height*/
- /*the width indicates the number of columns and the height the number of rows that the image data contains*/
- #pragma pack(push, 1)
- typedef struct
- {
- unsigned int biSize;
- int width;
- int height;
- unsigned short planes;
- unsigned short bitPix;
- unsigned int biCompression;
- unsigned int biSizeImage;
- int biXPelsPerMeter;
- int biYPelsPerMeter;
- unsigned int biClrUsed;
- unsigned int biClrImportant;
- }InfoHeader;
- #pragma pop
- /*the structure Pixel is used to store the blue, green and red components of each pixel*/
- /*the blue, green and red components are contained in the b,g and r members respectively*/
- #pragma pack(push, 1)
- typedef struct
- {
- unsigned char b;
- unsigned char g;
- unsigned char r;
- }Pixel;
- #pragma pop
- /*the structure BMPstatistics is used to store the statistics of the BMP image calculated in the program*/
- /*the first three members are used to contain the min, max and mean intensity of the image*/
- /*the fourth member is an error flag for the standard deviation*/
- /*it will be set to true when a division by zero may occur to prevent this operation*/
- /*otherwise it will be set to false and the standard deivation will be calculated then stored in the fifth member*/
- #pragma pack(push, 1)
- typedef struct
- {
- unsigned int minimumIntensity;
- unsigned int maximumIntensity;
- unsigned int meanIntensity;
- bool stdDeviationError;
- float standardDeviation;
- }BMPstatistics;
- #pragma pop
- /* Function Prototypes */
- /*the function filestreamCheck will check whether a filestream exists*/
- short int filestreamCheck( FILE *filestream , char filename[] );
- /*the function headerCheck will inspect the headerData from a file and determine whether the file is in BMP format*/
- short int headerCheck( FileHeader headerData );
- /*the function allocFrameBuffer will allocate a 2-D array to contain the intensity of each pixel in the image data from the BMP file*/
- unsigned char **allocFrameBuffer( int height, int width );
- /*the function fillFrameBuffer will fill the 2-D array with the intensity of each pixel in the image data from the BMP file*/
- unsigned char **fillFrameBuffer(FILE *FilePointer, int fBufferWidth, int fBufferHeight, Pixel *LineBuffer, unsigned char **FrameBuffer);
- /*the function calcBMPstatistics will calculate the min, max, mean and standard deviation of the intensities of the image data*/
- BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities);
- /*the function printBMPstatistics will validate and print the statistics calculated in the previous function*/
- void printBMPstatistics( BMPstatistics BMPstats );
- /*argc is an integer that will contain the value of the parameters given at the command line*/
- /*argv[] is a pointer to an char array that contains a string of each input*/
- int main( int argc, char *argv[] )
- {
- /*a file object filePointer is declared which will be used to control the filestream*/
- FILE *filePointer;
- /*a strucuture headerData of type FileHeader is declared to later contain the file header of the file*/
- FileHeader headerData;
- /*a structure infoHeaderData of type InfoHeader is declared to later contain the info header of the BMP file*/
- InfoHeader infoHeaderData;
- /*a single pointer lineBuffer of type Pixel is declared*/
- Pixel *lineBuffer;
- /*a double pointer frameBuffer pf type unsigned char is declared*/
- unsigned char **frameBuffer;
- /*a structure bmpStats of type BMPstatistics is used to store the statistics of the BMP file and an error flag*/
- BMPstatistics bmpStats;
- /*if there are two parameters at the command line argc == 2*/
- if( argc == 2 )
- {
- /*filePointer is used to store the result of opening the second file name given at the command line*/
- /*fopen will open argv[1] which is the second input in the mode rb which is reading mode for a binary file*/
- filePointer = fopen(argv[1],"rb");
- /*the filestreamCheck function will determine if the filestream filePointer exists*/
- /*if the filestream filePointer exists the function call will be return 1*/
- if( filestreamCheck(filePointer, argv[1]) == 1 )
- {
- /*the fread function will read 1 element of size headerData and store it in headerData*/
- /*if the fread function is successful it will return 1*/
- if( fread(&headerData,sizeof(headerData),1,filePointer) == 1 )
- {
- /*the headerCheck function will check the contents of the structure headerData*/
- /*if the contents of the structure confirm the file is of BMP format the funciton will return 1*/
- if( headerCheck(headerData) == 1 )
- {
- /*the fread function will read 1 element of size infoHeaderData and store it in infoHeaderData*/
- /*if the fread function is successful it will return 1)*/
- if ( fread(&infoHeaderData,sizeof(infoHeaderData),1,filePointer) == 1 )
- {
- /*lineBuffer is initialised to a 1_D dynamically allocated array*/
- /*the array has infoHeaderData.width elements and each element is of size Pixel*/
- /*the memory allocated using the calloc function which is type casted to a Pixel pointer from void*/
- lineBuffer = (Pixel*)calloc( infoHeaderData.width, sizeof(Pixel) );
- /*if the memory is successfully allocated it will not have a NULL value*/
- if( lineBuffer != NULL )
- {
- /*frameBuffer is initialised to a 2-D dynamically allocated array using the function allocFrameBuffer*/
- /*the array has infoHeaderData.width*infoHeaderData.height elements each of size unsigned char*/
- frameBuffer = allocFrameBuffer(infoHeaderData.width,infoHeaderData.height);
- /*if the memory is successfully allocated it will not have a NULL value*/
- if( frameBuffer != NULL )
- {
- /*the fseek function will move the pointer filePointer from the start of the file indicated by SEEK_SET*/
- /*it will move it headerData.imageDataOffset units*/
- /*if the fseek function is successful it will return 0*/
- if (fseek(filePointer,headerData.imageDataOffset,SEEK_SET) == 0)
- {
- /*the fillFrameBuffer function is called to fill frameBuffer with the intenisty of each pixel of the image data*/
- frameBuffer = fillFrameBuffer(filePointer,infoHeaderData.width,infoHeaderData.height,lineBuffer, frameBuffer);
- /*after the frameBuffer is filled bmpstats is initialised with the return value of the function calcBMPstatistics*/
- /*calcBMPstatistics will calculate the min, max, mean and standard deviation of the intensities in frameBuffer*/
- /*it will return all the values calculated in a structure including a possible error flag*/
- bmpStats = calcBMPstatistics(infoHeaderData.height,infoHeaderData.width,frameBuffer);
- /*the printBMPstatistics function is called to print the data from the bmpStats structure*/
- printBMPstatistics(bmpStats);
- }
- /*if the fseek function is unsuccessful an error message is displayed through the error stream*/
- else fprintf(stderr,"An error has occurred when moving the file pointer to the start of the image data \n");
- /*the free function called on frameBuffer will free the memory allocated for frameBuffer to prevent a memory leak*/
- free(frameBuffer);
- }
- /*the free function called on the lineBuffer will free the memory allocated for lineBuffer to prevent a memory leak*/
- free(lineBuffer);
- }
- /*if the the memory was not correctly allocated for linebuffer an error message is displayed through the error stream*/
- else fprintf(stderr,"An error has occurred when allocating memory for lineBuffer \n");
- }
- /*if the infoHeaderData was unable to be read an error message is displayed through the error stream*/
- else fprintf(stderr,"An error has occurred when reading the information header of the file \n");
- }
- }
- /*if the headerData ws unable to be read an error message is displayed through the error stream*/
- else fprintf(stderr,"An error has occured when reading the header of the file \n");
- /*the fclose function is called on filePointer to close the file and dissociate it from the stream*/
- fclose(filePointer);
- }
- }
- /*if there is not 2 parameters at the command line an the error message insufficient number of parameters is displayed*/
- else fprintf(stderr,"Insufficient number of parameters \n");
- /*the message the program is terminated is used to indicate the end of the program*/
- fprintf(stderr,"The program is terminated \n");
- return 0;
- }
- /* Function Definitions */
- /*the function filestreamCheck verifies a filestream*/
- /*parameters: a file object pointer and a character array*/
- /*output: a short int*/
- /*the function checks if the filestream exists, if it doesn't exist an error message is displayed*/
- short int filestreamCheck( FILE *filestream , char filename[] ) /*filestreamCheck function stub*/
- { /*short int filestreamCheck(FILE *filestream,char filename[]){*/
- /*returnvalue is initialised as a short int and is used as the return value of the function*/ /*fprintf(stderr,"Function filestreamCheck entered\n");*/
- short int returnvalue; /*fprintf(stderr,"Checking if the filestream exists\n");*/
- /*if the filestream equals NULL then it doesn't exist*/ /*if(filestream==NULL){*/
- if( filestream == NULL) /*fprintf(stderr,"Can't open %s \n",filename);*/
- { /*return -1;}*/
- /*if the filestream doesn't exist an error message is displayed through the error stream*/ /*else{*/
- /*the error message will display that it cannot open the file provided*/ /*fprintf(stderr,"%s can be opened \n",filename);*/
- fprintf(stderr,"Can't open %s \n",filename); /*return 1;}}*/
- /*the returnvalue is initilised as -1*/
- returnvalue = -1;
- }
- /*if the filestream is not equal to NULL the returnvalue is initialised as 1*/
- else returnvalue = 1;
- /*the returnvalue is then returned to the main function*/
- return returnvalue;
- }
- /*the function headerCheck verifies if a file is in BMP format*/
- /*input: a structure of type FileHeader*/
- /*output: short int*/
- short int headerCheck( FileHeader headerData ) /*headerCheck function stub*/
- { /*short int headerCheck(FileHeader headerData){*/
- /*returnvalue is initialised as a short int and is used as the return value of the function*/ /*fprintf(stderr,"Function headerCheck entered \n");*/
- short int returnvalue; /*if( headerData.fileMarker1 == 'B' && headerData.fileMarker2 == 'M'){*/
- /*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");*/
- /*this is verified by checking the values of the members filemarker1 and filemarker2 in the headerData*/ /*return 1;}*/
- /*if headerData.fileMarker1 equals B and headerData.fileMarker2 equals M the file is verified*/ /*else{*/
- /*if the file is verified as a BMP returnvalue is initilised as 1*/ /*fprintf(stderr,"Incorrect File Format \n");*/
- if( headerData.fileMarker1 == 'B' && headerData.fileMarker2 == 'M') returnvalue = 1; /*return -1;}}*/
- else
- {
- /*if the file is not verified as a BMP an error message is displayed through the error stream*/
- /*this bypasses the buffer and is done by using the fprintf function which displays incorrect file format*/
- fprintf(stderr,"Incorrect File Format \n");
- /*the return value is initilised as -1*/
- returnvalue = -1;
- }
- /*the returnvalue is then returned to the main function*/
- return returnvalue;
- }
- /*the function allocFrameBuffer dynamically allocates a block of memory for the frameBuffer*/
- /*parameters: two integers which are the height and width of the array of memory*/
- /*output: double pointer of type unsigned char, which is the frameBuffer*/
- unsigned char **allocFrameBuffer( int bufferHeight, int bufferWidth ) /*allocFrameBuffer function stub*/
- { /*unsigned char **allocFrameBuffer(int height, int width)*/
- /*two integers counter and number of elements are declared*/ /*fprintf(stderr,"Function allocFrameBuffer entered\n");*/
- /*counter is used to iterate through height to add rows to the array*/ /*fprintf(stderr,"The height entered is %d\n", height);*/
- /*numberofelements is used to store the number of elements in the array*/ /*fprintf(stderr,"The width entered is %d\n", width);*/
- int counter, numberofelements; /*int counter, numberofelements;*/
- /*two double pointer unsigned chars and one single pointer unsigned char are declared*/ /*unsigned char **returnvalue,*pointer1Darray,**pointer2Darray;*/
- /*the returnvalue is used to contain the value returned by the function*/ /*numberofelements=height*width;*/
- /*the pointer1Darray is used to contain a 1D dynamically allocated array*/ /*fprintf(stderr,"The number of elements of the array is %d\n", numberofelements);*/
- /*the pointer2Darray is used to contain a 2D dynamically allocated array*/ /*pointer1Darray=(unsigned char*)calloc(numberofelements,sizeof(unsigned char));*/
- unsigned char **returnvalue, *pointer1Darray, **pointer2Darray; /*if(pointer1Darray == NULL){*/
- /*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");*/
- numberofelements = bufferHeight*bufferWidth; /*free(pointer1Darray);*/
- /*pointer1Darray is initilised as a type case of unsigned char pointer of a calloc function*/ /*return NULL;}*/
- /*the calloc call dynamically allocates an array with height*width elements of size unsigned char*/ /*pointer2Darray = (unsigned char**)calloc( height, sizeof(unsigned char*) );*/
- pointer1Darray = (unsigned char*)calloc( numberofelements, sizeof(unsigned char) ); /*if(pointer2Darray == NULL){*/
- /*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");*/
- if( pointer1Darray == NULL ) /*free(pointer2Darray);*/
- { /*return NULL;*/
- /*if the array was unable to be declared the pointer tothe 1D array pointer1Darray is freed*/ /*for(counter = 0; counter<height; counter++){*/
- free(pointer1Darray); /*pointer2Darray[counter] = pointer1Darray + counter*width;*/
- /*the returnvalue is then initialised as NULL*/ /*fprintf(stderr,"%d bytes allocated from %p\n",sizeof(pointer2Darray),pointer2Darray);*/
- returnvalue = NULL; /*return pointer2Darray;}}*/
- /*an error message is returned through the error stream to communicate there was an error in memory allocation*/
- fprintf(stderr,"An error in memory allocation of frameBuffer has occurred \n");
- }
- /*pointer2Darray is initilised as a type cast of unsigned char double pointer of a calloc function*/
- /*there are a height number of elements of size unsigned char pointer*/
- pointer2Darray = (unsigned char**)calloc( bufferHeight, sizeof(unsigned char*) );
- /*if the array is not declared properly it will be equal to NULL*/
- if( pointer2Darray == NULL )
- {
- /*if the array was unable to be declared the pointer to the 2D array pointer2Darray is freed*/
- free(pointer2Darray);
- /*the returnvalue is then initialised as NULL*/
- returnvalue = NULL;
- /*an error message is returned through the error stream to communicate there was an error in memory allocation*/
- fprintf(stderr,"An error in memory allocation of frameBuffer has occurred \n");
- }
- /*if the memory has been allocated properly a for loop initialises each element of the 2D array*/
- /*the for loop has a counter that stops before reaching the int value for height*/
- for ( counter = 0; counter<bufferHeight; counter++ )
- {
- /*the index of counter is initialised as the addition of the 1D array and counter*width*/
- /*this will create a 2D array with dimensions height*width and elements of type unsigned char*/
- pointer2Darray[counter] = pointer1Darray + counter*bufferWidth;
- }
- /*after the for loop the returnvalue is declared as pointer2Darray*/
- returnvalue = pointer2Darray;
- /*returnvalue is then returned to the main function*/
- return returnvalue;
- }
- /*the function fillFrameBuffer will fill the dynamically allociated array FrameBuffer with the intensity of each pixel of the BMP image*/ /*function stub for fillFrameBuffer*/
- /*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){*/
- /*returns: double pointer of type unsigned char, which is the filled frameBuffer*/ /*int counterRow, counterCol;*/
- unsigned char **fillFrameBuffer(FILE *FilePointer, int fBufferWidth, int fBufferHeight, Pixel *LineBuffer, unsigned char **FrameBuffer) /*unsigned char intensity;*/
- { /*for( counterRow = 0; counterRow < fBufferHeight; counterRow ++){*/
- /*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 ){*/
- int counterRow, counterCol; /*for( counterCol = 0; counterCol < fBufferWidth; counterCol++){*/
- /*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);*/
- unsigned char intensity; /*fprintf("The intensity of the current pixel is: %d,&intensity)*/
- /*the outer for loop iterates through for each row of the FrameBuffer*/ /*FrameBuffer[counterRow][counterCol] = intensity;*/
- for( counterRow = 0; counterRow < fBufferHeight; counterRow ++) /*fprintf("The value of the current index is %d,FrameBuffer[counterRow][counterCol])}}*/
- { /*else fprintf(stderr,"An error has occurred when reading the image data into the lineBuffer \n");*/
- /*for each row the fread function is called*/ /*return FrameBuffer;}*/
- /*the fread function will read infoHeaderData.width elements of size Pixel into the LineBuffer*/
- /*if the fread function is successful it will return infoHeaderData.width*/
- if( fread(LineBuffer,sizeof(Pixel),fBufferWidth,FilePointer) == fBufferWidth )
- {
- /*the inner for loop will iterate through each element of the lineBuffer*/
- /*this is done by using the counter counterCol and increasing until it is not less than the infoHeaderData.width*/
- for( counterCol = 0; counterCol < fBufferWidth; counterCol++)
- {
- /*for each element of the lineBuffer the average of the blue, green and red component is calculated*/
- intensity = ((LineBuffer[counterCol].b + LineBuffer[counterCol].g + LineBuffer[counterCol].r)/3);
- /*this calculate valued is stored into the FrameBuffer*/
- /*the index of that value is row - counterRow and column - counterCol*/
- FrameBuffer[counterRow][counterCol] = intensity;
- }
- }
- else
- {
- /*if the fread function is unsuccessful an error message is desplayed through the error stream*/
- fprintf(stderr,"An error has occurred when reading the image data into the lineBuffer \n");
- }
- }
- /*the filled FrameBuffer is returned, if an error has occurred it will be a NULL pointer*/
- return FrameBuffer;
- }
- /*the function calcBMPstatistics calculates the minimum, maximum, mean and standard deviation of the intensities of a BMP image*/
- /*parameters: unsigned char double pointer to the array of intensities and two integers that give the dimensions of the array */
- /*output: structure of type BMPstatistics*/
- BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities) /*calcBMPstatistics function stub*/
- { /*BMPstatistics calcBMPstatistics(int numberOfrows, int numberOfcols, unsigned char **arrayOfIntensities){*/
- /*a structure of type BMPstatistics called statData is declared*/ /*fprintf(stderr,"The array has %d rows\n", numberOfrows);*/
- BMPstatistics statData; /*fprintf(stderr,"The array has %d columns %d\n", numberOfcols);*/
- /*three integers counterRow, counterCol and numberOfelements are declared*/ /*BMPstatistics statData;*/
- /*counterRow and counterCol are used as counters in for loops to iteratre through the rows and columns of the array*/ /*int counterRow, counterCol, numberOfelements;*/
- int counterRow, counterCol, numberOfelements; /*float sum = 0, sum4stddev = 0;*/
- /*two floats sum and sum4stddev are declared, sum is used to store the total of all the intensities*/ /*statData.minimumIntensity = arrayOfIntensities[0][0];*/
- /*sum4stddev is used to store the summation needed to calculate the standard deviation*/ /*statData.maximumIntensity = arrayOfIntensities[0][0];*/
- float sum = 0, sum4stddev = 0; /*numberOfelements = numberOfrows*numberOfcols;*/
- /*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);*/
- statData.minimumIntensity = arrayOfIntensities[0][0]; /*for( counterRow = 0; counterRow < numberOfrows; counterRow ++){*/
- statData.maximumIntensity = arrayOfIntensities[0][0]; /*for( counterCol = 0; counterCol < numberOfcols; counterCol ++){*/
- /*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);*/
- numberOfelements = numberOfrows*numberOfcols; /*fprintf(stderr,"The current sum for standard deviation is %f\n",sum4stddev);}}*/
- /*the outer for loop iterates untill the counter, counterRow is still less than the number of rows of the array*/ /*statData.stdDeviationError = false;*/
- for( counterRow = 0; counterRow < numberOfrows; counterRow ++) /*if(numberOfelements == 1){*/
- { /*fprintf(stderr,"The standard deivation is unable to be calculated\n");*/
- /*the inner for loop iterates untill the counter, counterCol is still less than the number of columns of the array*/ /*statData.stdDeviationError = true;}*/
- for( counterCol = 0; counterCol < numberOfcols; counterCol ++) /*else{*/
- { /*statData.standardDeviation = sqrt( ((1/((float)numberOfelements-1))*sum4stddev) );*/
- /*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);}*/
- sum += (float)arrayOfIntensities[counterRow][counterCol]; /*return statData}*/
- /*each index is compared to see if its value is less than the set minimum intensity*/
- if( arrayOfIntensities[counterRow][counterCol] < statData.minimumIntensity )
- {
- /*if the current index is less than the set minimum it is reinitilised as the new minimum*/
- statData.minimumIntensity = arrayOfIntensities[counterRow][counterCol];
- }
- /*each index is compared to see if its value is greater than the set maximum intensity*/
- if( arrayOfIntensities[counterRow][counterCol] > statData.maximumIntensity )
- {
- /*if the current index is greater than the set maximum it is reinitilised as the new maximum*/
- statData.maximumIntensity = arrayOfIntensities[counterRow][counterCol];
- }
- }
- }
- /*the mean is then calculated and stored in the structure statData in the member meanIntensity*/
- /*the mean is calculated by dividing the sum by the number of elements in the array of intensities*/
- statData.meanIntensity = sum/numberOfelements;
- /*the outer for loop iterates untill the counter, counterRow is still less than the number of rows of the array*/
- for( counterRow = 0; counterRow < numberOfrows; counterRow ++)
- {
- /*the inner for loop iterates untill the counter, counterCol is still less than the number of columns of the array*/
- for( counterCol = 0; counterCol < numberOfcols; counterCol ++)
- {
- /*the square of the difference between the current index and the mean is summed to form the sum for standard deviation*/
- /*for this operation to work each index and well as the mean have to be type casted to float*/
- sum4stddev += pow(((float)arrayOfIntensities[counterRow][counterCol]-(float)statData.meanIntensity),2);
- }
- }
- /*the error flag stdDeviationError in the structure statData is initialised as false before the division by zero check is made*/
- statData.stdDeviationError = false;
- /*a possible division by zero can only occur when there is 1 element in the array*/
- /*when this is true numberOfelements is equal to 1*/
- if(numberOfelements == 1)
- {
- /*when this is true the error flag is reintialised as true and the standard deviation is not calculated*/
- statData.stdDeviationError = true;
- }
- /*when there is more than 1 element in the array, numberOfelements is greater than 1 the standard deivation is calculated*/
- else
- {
- /*standard deviation is calculated by taking the square root of (1/numberOfelements-1) mutiplied by the sum calculated previously*/
- /*for this operation to work the number of elements has to be type casted to a float*/
- /*the standard deviation is then stored in the the structure statData under the member standardDeviation*/
- statData.standardDeviation = sqrt( ((1/((float)numberOfelements-1))*sum4stddev) );
- }
- /*after all the statistics are calculated the structure statData is then returned to the main function*/
- return statData;
- }
- /*the function printBMPstatistics validates and prints the calculated statistics*/
- /*input: a structure of type BMPstatistics*/
- /*output: void*/
- void printBMPstatistics( BMPstatistics BMPstats )
- { /*printBMPstatistics function stub*/
- /*the message results of statistical analysis is printed via the error stream to bypass a filter*/ /*void printBMPstatistics( BMPstatistics BMPstats ){*/
- printf("*** Results of the statistical analysis *** \n"); /*fprintf(stderr,"*** Results of the statistical analysis *** \n");*/
- /*the mean, min, max and standard deviation is then printed the same way*/ /*fprintf(stderr,"Mean: %d \n",BMPstats.meanIntensity);*/
- /*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);*/
- printf("Mean: %d \n",BMPstats.meanIntensity); /*fprintf(stderr,"Max Intensity: %d \n",BMPstats.maximumIntensity);*/
- /*the min stored in the member minimumIntensity in the structure BMPstats as an integer using %d syntax*/ /*if(BMPstats.stdDeviationError == true){*/
- printf("Min Intensity: %d \n",BMPstats.minimumIntensity); /*fprintf(stderr,"Standard Deviation: NA \n");}*/
- /*the max stored in the member maximiumIntensity in the structure BMPstats as an integer using %d syntax*/ /*else{*/
- printf("Max Intensity: %d \n",BMPstats.maximumIntensity);
- /*the error flag stdDeviationError is checked to see if it is true*/
- /*if it is true it means that the standard deviation could not be calculated*/
- if(BMPstats.stdDeviationError == true)
- {
- /*if the standard deviation could not be calculated the value is displayed as NA*/
- /*this value is printed through the error stream to indicate and error has occurred*/
- fprintf(stderr,"Standard Deviation: NA \n");
- }
- else
- {
- /*if the error flag is false it means the standard deviation could be calculated*/
- /*it is then printed with 1 decimal point precision by the sytax %.1f*/
- /*the standard deiviation is accessed through the member standardDeviation in the structure BMPstats*/
- printf("Standard Deviation: %.1f \n",BMPstats.standardDeviation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement