Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.81 KB | None | 0 0
  1. /*--------------------------------------------------------------------------------------
  2.    
  3.     Student's Name:                 Braedon Gernyi
  4.    
  5.     Student's email address:        bmg295@uowmail.edu.au
  6.    
  7.     Lab group:                      CL/03
  8.    
  9.     Purpose of this assignment:     To gain an understanding of modular program
  10.                                     design, pointers, dynamic one and two dimensional
  11.                                     arrays and binary files.
  12.                                    
  13. --------------------------------------------------------------------------------------*/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <strings.h>
  19. #include <string.h>
  20. #include <math.h>
  21.  
  22. /* Data type defintions */
  23. #pragma pack( push , 1 )
  24. typedef struct
  25. {
  26.     unsigned char b;
  27.     unsigned char g;
  28.     unsigned char r;
  29. } Pixel;
  30. #pragma pop
  31.  
  32.  
  33. #pragma pack( push , 1 )
  34. typedef struct
  35. {
  36.     unsigned short fileMarker1; /* File extension number */
  37.     unsigned int bfSize;
  38.     unsigned short unused1;
  39.     unsigned short unused2;
  40.     unsigned int imageDataOffset; /* Offset to the start of image data */
  41. } FileHeader;
  42. #pragma pop
  43.  
  44.  
  45. #pragma pack( push , 1 )
  46. typedef struct
  47. {
  48.     unsigned int biSize;
  49.     int width; /* Width of the image */
  50.     int height; /* Height of the image */
  51.     unsigned short planes;
  52.     unsigned short bitPix;
  53.     unsigned int biCompression;
  54.     unsigned int biSizeImage;
  55.     int biXPelsPerMeter;
  56.     int biYPelsPerMeter;
  57.     unsigned int biClrUsed;
  58.     unsigned int biClrImportant;
  59. } InfoHeader;
  60. #pragma pop
  61.  
  62.  
  63. /* Function prototypes */
  64. unsigned char** array2duc( int width, int length ); /* 2 dimensional unsigned char array allocation */
  65. void free2duc( unsigned char** array, int width, int height ); /* 2 dimensional unsigned char array free */
  66. Pixel *pixlinealloc( int length );
  67.  
  68.  
  69. /* Start of main function */
  70. int main ( int argc, char *argv[] )
  71. {
  72.     if( argc != 2 ) /* Checks for argument amount (TOTAL INPUTS MUST EQUAL 1) */
  73.     {
  74.         printf("Invalid number of parameters.\n Program terminated");
  75.         return 1;
  76.     }
  77.  
  78.     char extstr[4]; /* CHECK IF THIS IS VALID ARRAY SIZE BECAUSE VARIABLE */
  79.     /* Copying argument into string */
  80. /*  printf( "File name = %s\n", argv[1] ); */  /* STUB */
  81.    
  82. /*  printf("size of name = %d\n" , strlen(argv[1])); */ /* STUB */
  83.    
  84.     int fileext = strlen(argv[1]), i, j = 0; /* Integers used in for loop for getting file extension */
  85.     for ( i = (fileext - 3) ; i < fileext ; i++ )
  86.     {
  87.         extstr[j] = argv[1][i];
  88.         j += 1;
  89.     }
  90.    
  91.     extstr[3] = 0; /* Setting last char in string to null to specify terminator */
  92.    
  93.     if ( strcmp( "bmp" , extstr ) != 0 ) /* Checking file input text for bmp file extension */
  94.     {
  95.         printf( "Invalid file extension.\n Program terminated" );
  96.         return 1;
  97.     }
  98.  
  99. /*  printf( "file ext read = %s \n" , extstr ); */ /* STUB */
  100.    
  101.     FILE *image; /* Image variable decleration */
  102.     image = fopen( argv[1] , "rb" ); /* Opening image for data reading */
  103.     if ( image == NULL ) /* Terminates program if image reading is un s u c c essful */
  104.     {
  105.         printf( "Unable to read image.\n Program terminated" );
  106.         return 1;
  107.     }
  108.    
  109.     FileHeader fheader; /* Since read is successful, FileHeader type variable is declared */
  110.    
  111.     fread(&fheader, sizeof(FileHeader),1,image); /* Reading data into fheader */
  112.  
  113.     if (fheader.fileMarker1 != 0x4D42) /* Checking for REAL bitmap extension */ /* THIS MIGHT NEED TO BE CHANGED SO SUIT SPECIFICATIONS */
  114.     {
  115.         printf( "Invalid file extension.\n Program terminated" );
  116.         fclose(image);
  117.         return -1;
  118.     }
  119.    
  120.     InfoHeader iheader;
  121.    
  122.     fread(&iheader, sizeof(InfoHeader),1,image); /* Reading in information header into iheader for image height and width */
  123.    
  124.     printf( "\nRead Successful!!\n\nimage width = %d, image height = %d \nImage offset = %d\n" , iheader.width , iheader.height , fheader.imageDataOffset ); /* STUB */
  125.    
  126.     fseek( image , fheader.imageDataOffset , SEEK_SET ); /* Moving fread back to the data offset */
  127.    
  128. /*  Pixel test;
  129.     fread( &test , sizeof(Pixel) , 1 , image ); */
  130.    
  131. /*  printf( "Image test read; Red = %d, Green = %d, Blue = %d" , test.r , test.g , test.b );*/ /* STUB */
  132.  
  133. /*  printf( "size of Pixel = %d" , sizeof(Pixel) );*/ /* STUB: Checking size of pixel structure (3 bytes) */
  134.  
  135.     Pixel *lineBuffer = pixlinealloc( iheader.width );
  136.     unsigned char **frameBuffer = array2duc( iheader.width , iheader.height );
  137.    
  138.     if ( ( lineBuffer == NULL ) || ( frameBuffer == NULL ) )
  139.     {
  140.         printf( "Memory allocation error. \n" );
  141.         fclose ( image );
  142.         return -1;
  143.     }
  144.    
  145.     int buffCheck, buffMark = 0;
  146.    
  147.     for ( j = 0 ; j < iheader.height ; j++ )
  148.     {
  149.        
  150.         for ( i = 0 ; i < iheader.width ; i++ )
  151.         {
  152.             buffCheck = fread( &lineBuffer[i] , sizeof(Pixel) , 1 , image );
  153.            
  154.             if ( buffCheck < 1 )
  155.             {
  156.                 printf( "Error reading image.\n" );
  157.                 buffMark = 1;
  158.                 break;
  159.             }
  160.         }
  161.        
  162.         if ( buffMark == 1 )
  163.         {
  164.             break;
  165.         }
  166.                
  167.         for ( i = 0 ; i < iheader.width ; i++ )
  168.         {
  169.             frameBuffer[i][j] = ( ( lineBuffer[i].b + lineBuffer[i].g + lineBuffer[i].r ) / 3 ) + 0.5;
  170.         }
  171.     }
  172.    
  173. /*  printf( "LineBuffer @ 5 = %d, %d, %d \n" , lineBuffer[100].b , lineBuffer[100].g , lineBuffer[100].r ); */ /* STUB */
  174.  
  175.     if ( buffMark == 0 )
  176.     {
  177.  
  178.         printf("*** Results of the statistical analysis *** \n");
  179.    
  180.         int sum = 0, min = frameBuffer[0][0], max = frameBuffer[0][0];
  181.        
  182.         for ( i = 0 ; i < iheader.width ; i++ )
  183.         {
  184.             for ( j = 0 ; j < iheader.height ; j++ )
  185.             {
  186.                 sum += frameBuffer[i][j];
  187.            
  188.                 if ( min > frameBuffer[i][j] )
  189.                 {
  190.                     min = frameBuffer[i][j];
  191.                 }
  192.            
  193.                 if ( max < frameBuffer[i][j] )
  194.                 {
  195.                     max = frameBuffer[i][j];
  196.                 }
  197.             }
  198.         }
  199.    
  200.         int n = iheader.height * iheader.width , mean = ( sum / n ) + 0.5;
  201.         printf( "Mean: %d\nMin Intensity: %d\nMax Intensity: %d\n" , mean , min , max );
  202.    
  203.         float stddevsumm = 0 , stddev;
  204.    
  205.         if ( n - 1 != 0 )
  206.         {
  207.             for ( i = 0 ; i < iheader.width ; i++ )
  208.             {
  209.                 for ( j = 0 ; j < iheader.height ; j++ )
  210.                 {
  211.                     stddevsumm += ( frameBuffer[i][j] - mean ) * ( frameBuffer[i][j] - mean );
  212.                 }
  213.             }
  214.        
  215.             stddev = sqrt( stddevsumm / ( n - 1 ) );
  216.             printf( "Standard deviation: %.1f\n" , stddev );
  217.         }
  218.         else
  219.         {
  220.             printf( "Standard deviation: N/A");
  221.         }
  222.     }
  223.    
  224.    
  225.     free2duc( frameBuffer , iheader.width , iheader.height );
  226.     free( lineBuffer );
  227.     fclose(image);
  228.    
  229.     return 0;
  230.    
  231.     /* LEFT TO DO: Check for memory leakage, add comments */
  232. }
  233.  
  234. unsigned char** array2duc( int width, int height )
  235. {
  236.     int i;
  237.     unsigned char** array;
  238.    
  239.     array = calloc( width , sizeof(unsigned char*));
  240.    
  241.     if ( array == NULL )
  242.     {
  243.         return NULL;
  244.     }
  245.    
  246.     for ( i = 0 ; i < width ; i++ )
  247.     {
  248.         array[i] = calloc( height , sizeof( unsigned char));
  249.         if ( array[i] == NULL )
  250.         {
  251.             return NULL;
  252.         }
  253.     }
  254.    
  255.     return array;
  256. }
  257.  
  258.  
  259. void free2duc( unsigned char** array, int width, int height )
  260. {
  261.     int i;
  262.    
  263.     for ( i = 0 ; i < width ; i++ )
  264.     {
  265.         free( array[i] );
  266.     }
  267.     free( array );
  268. }
  269.  
  270. Pixel *pixlinealloc( int length )
  271. {
  272.     Pixel *array;
  273.    
  274.     array = (Pixel*)calloc( length, sizeof(Pixel));
  275.     return array;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement