Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------------------------------------
- Student's Name: Braedon Gernyi
- Student's email address: bmg295@uowmail.edu.au
- Lab group: CL/03
- Purpose of this assignment: To gain an understanding of modular program
- design, pointers, dynamic one and two dimensional
- arrays and binary files.
- --------------------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <string.h>
- #include <math.h>
- /* Data type defintions */
- #pragma pack( push , 1 )
- typedef struct
- {
- unsigned char b;
- unsigned char g;
- unsigned char r;
- } Pixel;
- #pragma pop
- #pragma pack( push , 1 )
- typedef struct
- {
- unsigned short fileMarker1; /* File extension number */
- unsigned int bfSize;
- unsigned short unused1;
- unsigned short unused2;
- unsigned int imageDataOffset; /* Offset to the start of image data */
- } FileHeader;
- #pragma pop
- #pragma pack( push , 1 )
- typedef struct
- {
- unsigned int biSize;
- int width; /* Width of the image */
- int height; /* Height of the image */
- 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
- /* Function prototypes */
- unsigned char** array2duc( int width, int length ); /* 2 dimensional unsigned char array allocation */
- void free2duc( unsigned char** array, int width, int height ); /* 2 dimensional unsigned char array free */
- Pixel *pixlinealloc( int length );
- /* Start of main function */
- int main ( int argc, char *argv[] )
- {
- if( argc != 2 ) /* Checks for argument amount (TOTAL INPUTS MUST EQUAL 1) */
- {
- printf("Invalid number of parameters.\n Program terminated");
- return 1;
- }
- char extstr[4]; /* CHECK IF THIS IS VALID ARRAY SIZE BECAUSE VARIABLE */
- /* Copying argument into string */
- /* printf( "File name = %s\n", argv[1] ); */ /* STUB */
- /* printf("size of name = %d\n" , strlen(argv[1])); */ /* STUB */
- int fileext = strlen(argv[1]), i, j = 0; /* Integers used in for loop for getting file extension */
- for ( i = (fileext - 3) ; i < fileext ; i++ )
- {
- extstr[j] = argv[1][i];
- j += 1;
- }
- extstr[3] = 0; /* Setting last char in string to null to specify terminator */
- if ( strcmp( "bmp" , extstr ) != 0 ) /* Checking file input text for bmp file extension */
- {
- printf( "Invalid file extension.\n Program terminated" );
- return 1;
- }
- /* printf( "file ext read = %s \n" , extstr ); */ /* STUB */
- FILE *image; /* Image variable decleration */
- image = fopen( argv[1] , "rb" ); /* Opening image for data reading */
- if ( image == NULL ) /* Terminates program if image reading is un s u c c essful */
- {
- printf( "Unable to read image.\n Program terminated" );
- return 1;
- }
- FileHeader fheader; /* Since read is successful, FileHeader type variable is declared */
- fread(&fheader, sizeof(FileHeader),1,image); /* Reading data into fheader */
- if (fheader.fileMarker1 != 0x4D42) /* Checking for REAL bitmap extension */ /* THIS MIGHT NEED TO BE CHANGED SO SUIT SPECIFICATIONS */
- {
- printf( "Invalid file extension.\n Program terminated" );
- fclose(image);
- return -1;
- }
- InfoHeader iheader;
- fread(&iheader, sizeof(InfoHeader),1,image); /* Reading in information header into iheader for image height and width */
- printf( "\nRead Successful!!\n\nimage width = %d, image height = %d \nImage offset = %d\n" , iheader.width , iheader.height , fheader.imageDataOffset ); /* STUB */
- fseek( image , fheader.imageDataOffset , SEEK_SET ); /* Moving fread back to the data offset */
- /* Pixel test;
- fread( &test , sizeof(Pixel) , 1 , image ); */
- /* printf( "Image test read; Red = %d, Green = %d, Blue = %d" , test.r , test.g , test.b );*/ /* STUB */
- /* printf( "size of Pixel = %d" , sizeof(Pixel) );*/ /* STUB: Checking size of pixel structure (3 bytes) */
- Pixel *lineBuffer = pixlinealloc( iheader.width );
- unsigned char **frameBuffer = array2duc( iheader.width , iheader.height );
- if ( ( lineBuffer == NULL ) || ( frameBuffer == NULL ) )
- {
- printf( "Memory allocation error. \n" );
- fclose ( image );
- return -1;
- }
- int buffCheck, buffMark = 0;
- for ( j = 0 ; j < iheader.height ; j++ )
- {
- for ( i = 0 ; i < iheader.width ; i++ )
- {
- buffCheck = fread( &lineBuffer[i] , sizeof(Pixel) , 1 , image );
- if ( buffCheck < 1 )
- {
- printf( "Error reading image.\n" );
- buffMark = 1;
- break;
- }
- }
- if ( buffMark == 1 )
- {
- break;
- }
- for ( i = 0 ; i < iheader.width ; i++ )
- {
- frameBuffer[i][j] = ( ( lineBuffer[i].b + lineBuffer[i].g + lineBuffer[i].r ) / 3 ) + 0.5;
- }
- }
- /* printf( "LineBuffer @ 5 = %d, %d, %d \n" , lineBuffer[100].b , lineBuffer[100].g , lineBuffer[100].r ); */ /* STUB */
- if ( buffMark == 0 )
- {
- printf("*** Results of the statistical analysis *** \n");
- int sum = 0, min = frameBuffer[0][0], max = frameBuffer[0][0];
- for ( i = 0 ; i < iheader.width ; i++ )
- {
- for ( j = 0 ; j < iheader.height ; j++ )
- {
- sum += frameBuffer[i][j];
- if ( min > frameBuffer[i][j] )
- {
- min = frameBuffer[i][j];
- }
- if ( max < frameBuffer[i][j] )
- {
- max = frameBuffer[i][j];
- }
- }
- }
- int n = iheader.height * iheader.width , mean = ( sum / n ) + 0.5;
- printf( "Mean: %d\nMin Intensity: %d\nMax Intensity: %d\n" , mean , min , max );
- float stddevsumm = 0 , stddev;
- if ( n - 1 != 0 )
- {
- for ( i = 0 ; i < iheader.width ; i++ )
- {
- for ( j = 0 ; j < iheader.height ; j++ )
- {
- stddevsumm += ( frameBuffer[i][j] - mean ) * ( frameBuffer[i][j] - mean );
- }
- }
- stddev = sqrt( stddevsumm / ( n - 1 ) );
- printf( "Standard deviation: %.1f\n" , stddev );
- }
- else
- {
- printf( "Standard deviation: N/A");
- }
- }
- free2duc( frameBuffer , iheader.width , iheader.height );
- free( lineBuffer );
- fclose(image);
- return 0;
- /* LEFT TO DO: Check for memory leakage, add comments */
- }
- unsigned char** array2duc( int width, int height )
- {
- int i;
- unsigned char** array;
- array = calloc( width , sizeof(unsigned char*));
- if ( array == NULL )
- {
- return NULL;
- }
- for ( i = 0 ; i < width ; i++ )
- {
- array[i] = calloc( height , sizeof( unsigned char));
- if ( array[i] == NULL )
- {
- return NULL;
- }
- }
- return array;
- }
- void free2duc( unsigned char** array, int width, int height )
- {
- int i;
- for ( i = 0 ; i < width ; i++ )
- {
- free( array[i] );
- }
- free( array );
- }
- Pixel *pixlinealloc( int length )
- {
- Pixel *array;
- array = (Pixel*)calloc( length, sizeof(Pixel));
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement