#include #include #include #include "bmp_io.h" #include "pbmpak.h" #define MAX_LEN 80 int main ( long argc, char **argv ); int bmp_2_ppma ( 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 BMP file to an ASCII PPM file. Modified: 08 April 2001 Author: John Burkardt Usage: bmp_2_ppma file.bmp file.ppm Parameters: FILE.BMP is the name of the input BMP file to be read. FILE.PPM is the name of the output ASCII PPM file to be created. */ 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 ( "BMP_2_PPMA:\n" ); printf ( " Please enter the input BMP 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 ( "BMP_2_PPMA:\n" ); printf ( " Please enter the output PPM 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 = bmp_2_ppma ( filein_name, fileout_name ); return result; } /******************************************************************************/ int bmp_2_ppma ( char *filein_name, char *fileout_name ) { /******************************************************************************/ /* Purpose: BMP_2_PPMA reads a BMP file and writes a PPMA file. Modified: 06 April 2001 Author: John Burkardt Parameters: Input, char *FILEIN_NAME, the name of the input BMP file. Input, char *FILEOUT_NAME, the name of the output PPMA file. */ int *barray; int *garray; int maxrgb = 255; int *rarray; int result; int xsize; int ysize; rarray = NULL; garray = NULL; barray = NULL; /* Read the data from the BMP file. */ result = bmp_read ( filein_name, &xsize, &ysize, &rarray, &garray, &barray ); if ( result != 0 ) { printf ( "\n" ); printf ( "BMP_2_PPMA - Fatal error!\n" ); printf ( " BMP_READ failed.\n" ); return 1; } printf ( "\n" ); printf ( "BMP_2_PPMA:\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 = ppma_write ( fileout_name, xsize, ysize, maxrgb, rarray, garray, barray ); if ( result != 0 ) { printf ( "\n" ); printf ( "BMP_2_PPMA - Fatal error!\n" ); printf ( " PPMA_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; printf ( "Flipping \n" ); 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; }