/* tiff_io_prb.c 05 October 2000 */ #include "sys/file.h" #include #include #include "tiff_io.h" /******************************************************************************/ int main ( short int argc, char *argv[] ) { /******************************************************************************/ /* Test TIFF read/write functions. Modified: 15 April 2001 */ long int depth; 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 pl; int result; float scale; TIFF_DATA *td; long int x; long int xs; long int y; long int ys; if ( argc < 3 ) { printf ( "\n" ); printf ( "TIFF_IO_PRB - Fatal error!\n" ); printf ( " Not enough arguments.\n" ); printf ( " Usage: tiff_prb .\n" ); exit ( EXIT_FAILURE ); } sscanf ( argv[1], "%s", infile ); sscanf ( argv[2], "%s", outfile ); if ( argc >= 4 ) { sscanf ( argv[3], "%d", &depth ); } else { depth = 8; } if ( ( depth < 1 ) || ( depth > 8 ) ) { printf ( "\n" ); printf ( "TIFF_IO_PRB - Fatal error!\n" ); printf ( " Invalid depth request: %d.\n", depth ); exit ( EXIT_FAILURE ); } printf ( "\n" ); printf ( "TIFF_IO_PRB:\n" ); printf ( " The output file will have a depth of %d.\n", depth ); /* Open the input image file. */ fpi = fopen ( infile, "rb" ); if ( fpi == NULL ) { printf ( "\n" ); printf ( "TIFF_IO_PRB - Fatal error!\n" ); printf ( " Cannot open the input file: %s\n", infile ); exit ( EXIT_FAILURE ); } printf ( "\n" ); printf ( "TIFF_IO_PRB:\n" ); printf ( " The input file has been opened.\n" ); /* Allocate space for the header. This should be good for a laugh. */ td = ( TIFF_DATA *) malloc ( sizeof ( TIFF_DATA ) ); /* Read the input image file header. */ result = read_tiff_header ( fpi, &xs, &ys, &pl, td ); if ( result ) { printf ( "\n" ); printf ( "TIFF_IO_PRB - Fatal error!\n" ); printf ( " Error while trying to read the TIFF file header.\n" ); exit ( EXIT_FAILURE ); } printf ( "\n" ); printf ( "TIFF_IO_PRB:\n" ); printf ( " The TIFF file header has been read.\n" ); /* Allocate input and output memory buffers. */ ir = ( unsigned char *) malloc ( xs * ys ); ig = ( unsigned char *) malloc ( xs * ys ); ib = ( unsigned char *) malloc ( xs * ys ); or = ( unsigned char *) malloc ( xs * ys ); og = ( unsigned char *) malloc ( xs * ys ); ob = ( unsigned char *) malloc ( xs * ys ); /* Read the input image data. */ result = read_tiff_data ( fpi, xs, ys, pl, *td, ir, ig, ib ); fclose ( fpi ); if ( result ) { exit ( EXIT_FAILURE ); } /* 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 < ys; y++ ) { for ( x = 0; x < xs; x++ ) { i = y * xs + x; or[i] = ( ir[i] >> ( 8 - depth ) ) * scale; og[i] = ( ig[i] >> ( 8 - depth ) ) * scale; ob[i] = ( ib[i] >> ( 8 - depth ) ) * scale; } } /* Create and open the output image file. */ fpo = fopen ( outfile, "wb" ); if ( fpo == NULL ) { printf ( "\n" ); printf ( "TIFF_IO_PRB - Fatal error!\n" ); printf ( " Cannot open the output file: %s.\n", infile ); exit ( EXIT_FAILURE ); } /* Write the output image. */ write_tiff_header ( fpo, xs, ys, pl ); write_tiff_data ( fpo, xs, ys, pl, or, og, ob ); fclose ( fpo ); /* Free the memory buffers. */ free ( ir ); free ( ig ); free ( ib ); free ( or ); free ( og ); free ( ob ); printf ( "\n" ); printf ( "TIFF_IO_PRB:\n" ); printf ( " Normal end of execution.\n" ); exit ( EXIT_SUCCESS ); }