/* rgb_io_prb.c 07 November 2001 */ #include #include #include #include "rgb_io.h" int main ( short int argc, char *argv[] ); /******************************************************************************/ int main ( short int argc, char *argv[] ) { /******************************************************************************/ /* Modified: 07 November 2001 */ int byte_swap = 0; long int depth; int depth1; FILE *fpi; FILE *fpo; int i; unsigned char *ib; unsigned char *ig; char infile[256]; unsigned char *ir; unsigned char *ob; unsigned char *og; unsigned char *or; char outfile[256]; long int planes; int result; float scale; long int type; long int x; long int xsize; long int y; long int ysize; printf ( "\n" ); printf ( "RGB_IO_PRB\n"); printf ( " Demonstrate the use of the RGB_IO routines.\n" ); if ( argc < 3 ) { printf ( "\n" ); printf ( "RGB_IO_PRB: Fatal error.\n" ); printf ( " Not enough arguments.\n" ); printf ( " Usage: rgb_io .\n" ); exit ( EXIT_FAILURE ); } sscanf ( argv[1], "%s", infile ); sscanf ( argv[2], "%s", outfile ); if ( argc >= 4 ) { sscanf ( argv[3], "%d", &depth1 ); } else { depth1 = 8; } depth = ( long int ) depth1; if ( ( depth < 1 ) || ( depth > 8 ) ) { printf ( "\n" ); printf ( "RGB_IO_PRB: Fatal error.\n" ); printf ( " Invalid depth request: %d.\n", depth ); printf ( " The depth must be between 1 and 8.\n" ); exit ( EXIT_FAILURE ); } /* Open the input image file. */ fpi = fopen ( infile, "r" ); if ( fpi == NULL ) { printf ( "\n" ); printf ( "RGB_IO_PRB: Fatal error.\n" ); printf ( " Cannot open the input file: %s\n", infile ); exit ( EXIT_FAILURE ); } /* Read the input image file header. */ result = read_rgb_header ( fpi, byte_swap, &xsize, &ysize, &planes, &type ); if ( result == 1 ) { exit ( EXIT_FAILURE ); } printf ( "\n" ); printf ( "RGB_IO_PRB:\n" ); printf ( " The output file will have a depth of %d.\n", depth ); if ( byte_swap ) { printf ( " The input file is assumed to require byte_swapping.\n" ); } else { printf ( " No byte_swapping will be performed.\n" ); } if ( type == 1 ) { printf ( " The file type is NOT run length encoded data.\n" ); } else if ( type == 257 ) { printf ( " The file type is run length encoded (RLE).\n" ); } printf ( " X width in pixels is %d.\n", xsize ); printf ( " Y width in pixels is %d.\n", ysize ); printf ( " Z depth in pixels is %d.\n", planes ); /* Allocate input memory buffers. */ ir = ( unsigned char *) malloc ( xsize * ysize ); ig = ( unsigned char *) malloc ( xsize * ysize ); ib = ( unsigned char *) malloc ( xsize * ysize ); /* Read the input image data. */ read_rgb_data ( fpi, byte_swap, xsize, ysize, planes, type, ir, ig, ib ); close ( fpi ); printf ( "\n" ); printf ( "RGB_IO_PRB: Read the input image data.\n" ); /* Allocate output memory buffers. */ or = ( unsigned char *) malloc ( xsize * ysize ); og = ( unsigned char *) malloc ( xsize * ysize ); ob = ( unsigned char *) malloc ( xsize * ysize ); /* Calculate the scale factor that will reduce 8-bit values (0-255) inclusive) to evenly spaced values of 0-255 for the requested depth resolution. Example: if depth = 2, values are 0, 85, 171, and 255. */ scale = 255.0 / ( powf ( 2.0, ( float ) depth ) - 1.0 ); /* Reduce depth resolution by generating new 8-bit pixel values. */ for ( y = 0; y < ysize; y++ ) { for ( x = 0; x < xsize; x++ ) { i = y * xsize + x; or[i] = ( ir[i] >> ( 8 - depth ) ) * scale; og[i] = ( ig[i] >> ( 8 - depth ) ) * scale; ob[i] = ( ib[i] >> ( 8 - depth ) ) * scale; } } /* Create the output image file. */ printf ( "Now we write the output file.\n" ); fpo = fopen ( outfile, "wb" ); if ( fpo == NULL ) { printf ( "\n" ); printf ( "RGB_IO_PRB: Fatal error.\n" ); printf ( " Cannot open the output file: %s.\n", infile ); exit ( EXIT_FAILURE ); } write_rgb_header ( fpo, byte_swap, xsize, ysize, planes, type ); write_rgb_data ( fpo, byte_swap, xsize, ysize, planes, or, og, ob, type ); close ( fpo ); printf ( "\n" ); printf ( "RGB_IO_PRB: Wrote the output file.\n" ); /* Free the memory buffers. */ free ( ir ); free ( ig ); free ( ib ); free ( or ); free ( og ); free ( ob ); printf ( "\n" ); printf ( "RGB_IO_PRB:\n"); printf ( " Normal end of execution.\n" ); exit ( EXIT_SUCCESS ); }