#include #include #include #include "bmp_io.h" #include "pbmpak.h" #define MAX_LEN 80 int main ( long argc, char **argv ); int ppmb_2_bmp ( char *filein_name, char *fileout_name ); int imat_vert_flip ( int xsize, int ysize, int *array ); /******************************************************************************/ int main ( long argc, char **argv ) { /******************************************************************************/ /* Purpose: This program converts a binary PPM file to a BMP file. Modified: 08 April 2001 Author: John Burkardt Usage: ppmb_2_bmp file.ppm file.bmp Parameters: FILE.PPM is the name of the input binary PPM file to be created. FILE.BMP is the name of the output BMP file to be read. */ char filein_name[MAX_LEN]; char fileout_name[MAX_LEN]; char input[MAX_LEN]; int result; /* Get the specification for the input file. */ if ( argc < 2 ) { printf ( "\n" ); printf ( "PPMB_2_BMP:\n" ); printf ( " Please enter the input PPM 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 ( "PPMB_2_BMP:\n" ); printf ( " Please enter the output BMP 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] ); } result = ppmb_2_bmp ( filein_name, fileout_name ); return result; } /******************************************************************************/ int ppmb_2_bmp ( char *filein_name, char *fileout_name ) { /******************************************************************************/ /* Purpose: PPMB_2_BMP reads a PPMB (binary PPM) file and writes a BMP file. Modified: 06 April 2001 Author: John Burkardt Parameters: Input, char *FILEIN_NAME, the name of the input PPMA file. Input, char *FILEOUT_NAME, the name of the output BMP file. */ int *barray; int *garray; int maxrgb; int *rarray; int result; int xsize; int ysize; rarray = NULL; garray = NULL; barray = NULL; /* Read the data from the PPMB file. */ result = ppmb_read ( filein_name, &xsize, &ysize, &maxrgb, &rarray, &garray, &barray ); if ( result != 0 ) { printf ( "\n" ); printf ( "PPMB_2_BMP - Fatal error!\n" ); printf ( " PPMB_READ failed.\n" ); return 1; } printf ( "\n" ); printf ( "PPMB_2_BMP:\n" ); printf ( " XSIZE = %d.\n", xsize ); printf ( " YSIZE = %d.\n", ysize ); /* The BMP up-down orientation is the opposite of what it is in PPM. */ result = imat_vert_flip ( xsize, ysize, rarray ); result = imat_vert_flip ( xsize, ysize, garray ); result = imat_vert_flip ( xsize, ysize, barray ); result = bmp_write ( fileout_name, xsize, ysize, rarray, garray, barray ); if ( result != 0 ) { printf ( "\n" ); printf ( "PPMB_2_BMP - Fatal error!\n" ); printf ( " BMP_WRITE failed.\n" ); return 2; } /* Free the memory. */ if ( rarray != NULL ) { free ( rarray ); } if ( garray != NULL ) { free ( garray ); } if ( barray != NULL ) { free ( barray ); } return 0; } /******************************************************************************/ int imat_vert_flip ( int xsize, int ysize, int *array ) { /******************************************************************************/ /* Purpose: IMAT_VERT_FLIP swaps rows of a 2D array, to flip it vertically. Modified: 07 April 2001 Author: John Burkardt Parameters: Input, int XSIZE, int YSIZE, the number of columns and rows. Input, int *ARRAY, the address of the first element of the array. */ int i; int j; int k1; int k2; int temp; for ( j = 0; j <= ( ysize / 2 ); j++ ) { k1 = xsize * j; k2 = xsize * ( ysize - 1 - j ); for ( i = 0; i < xsize; i++ ) { temp = array[k1+i]; array[k1+i] = array[k2+i]; array[k2+i] = temp; } } return 0; }