#include #include #include #include "pbmpak.h" #define MAX_LEN 80 int main ( int argc, char **argv ); /******************************************************************************/ int main ( int argc, char **argv ) { /******************************************************************************/ /* Purpose: PPMA_2_PPMB converts an ASCII PPM file to binary PPM format. PPMA_2_PPMB is a sample application of the PBMPAK library. It calls on the PPMA_READ routine to open and read a user-specified file in the ASCII PPM format. It then calls on the PPMB_WRITE routine to create and write a copy of the data in a file in the binary PPM format. Modified: 04 October 1998 Author: John Burkardt Usage: ppma_2_ppmb file.ppma file.ppmb Parameters: FILE.PPMA is the name of the input ASCII PPM file to be read. FILE.PPMB is the name of the output binary PPM file to be created. */ int *barray; int *garray; char filein_name[MAX_LEN]; char fileout_name[MAX_LEN]; char input[MAX_LEN]; int maxrgb; int *rarray; int result; int xsize; int ysize; /* Get the specification for the input file. */ if ( argc < 2 ) { printf ( "\n" ); printf ( "PPMA_2_PPMB:\n" ); printf ( " Please enter the INPUT PPMA file name:\n" ); if ( fgets ( input, MAX_LEN, stdin ) != NULL ) { sscanf ( input, "%s", filein_name ); } else { printf ( "Input error!\n" ); return EXIT_FAILURE; } } else { strcpy ( filein_name, argv[1] ); } /* Get the specification for the output file. */ if ( argc < 3 ) { printf ( "\n" ); printf ( "PPMA_2_PPMB:\n" ); printf ( " Please enter the OUTPUT PPMB file name:\n" ); if ( fgets ( input, MAX_LEN, stdin ) != NULL ) { sscanf ( input, "%s", fileout_name ); } else { printf ( "Input error!\n" ); return EXIT_FAILURE; } } else { strcpy ( fileout_name, argv[2] ); } /* Read the input file. */ rarray = NULL; garray = NULL; barray = NULL; result = ppma_read ( filein_name, &xsize, &ysize, &maxrgb, &rarray, &garray, &barray ); if ( result != 0 ) { printf ( "\n" ); printf ( "PPMA_2_PPMB: Fatal error!\n" ); printf ( " PPMA_READ failed.\n" ); if ( rarray != NULL ) { free ( rarray ); } if ( garray != NULL ) { free ( garray ); } if ( barray != NULL ) { free ( barray ); } return EXIT_FAILURE; } /* Check the data. */ result = ppm_check_data ( xsize, ysize, maxrgb, rarray, garray, barray ); if ( result != 0 ) { printf ( " PPM_CHECK_DATA reports bad data from the file.\n" ); if ( rarray != NULL ) { free ( rarray ); } if ( garray != NULL ) { free ( garray ); } if ( barray != NULL ) { free ( barray ); } return EXIT_FAILURE; } /* Write the output file. */ result = ppmb_write ( fileout_name, xsize, ysize, maxrgb, rarray, garray, barray ); if ( rarray != NULL ) { free ( rarray ); } if ( garray != NULL ) { free ( garray ); } if ( barray != NULL ) { free ( barray ); } if ( result != 0 ) { printf ( "\n" ); printf ( "PPMA_2_PPMB: Fatal error!\n" ); printf ( " PPMB_WRITE failed.\n" ); return EXIT_FAILURE; } printf ( "\n" ); printf ( "PPMA_2_PPMB:\n" ); printf ( " Normal end of execution.\n" ); return EXIT_SUCCESS; }