int ppmb_write ( char *fileout_name, int xsize, int ysize, int maxrgb, int *array ) { FILE *fileout; int result; /* Open the output file. */ fileout = fopen ( fileout_name, "wb" ); if ( fileout == NULL ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " Cannot open the output file %s.\n", fileout_name ); return 1; } /* Write the header. */ result = ppmb_write_header ( fileout, xsize, ysize, maxrgb ); if ( result != 0 ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " PPMB_WRITE_HEADER failed.\n" ); return 1; } /* Write the data. */ result = ppmb_write_data (fileout, xsize, ysize, array); if ( result != 0 ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " PPMB_WRITE_DATA failed.\n" ); return 1; } /* Close the file. */ fclose ( fileout ); return 0; }
bool ppmb_write ( char *file_name, int xsize, int ysize, unsigned char *rarray, unsigned char *garray, unsigned char *barray ) /******************************************************************************/ /* Purpose: PPMB_WRITE writes the header and data for a binary portable pixel map file. Licensing: This code is distributed under the GNU LGPL license. Modified: 16 June 2012 Author: John Burkardt Parameters: Input, char *FILE_NAME, the name of the file to contain the binary portable pixel map data. Input, int XSIZE, YSIZE, the number of rows and columns of data. Input, unsigned char *RARRAY, *GARRAY, *BARRAY, the arrays of XSIZE by YSIZE data values. Output, bool PPMB_WRITE, equals true, if the file could not be written, false, if the file was written. */ { FILE *file_pointer; int i; unsigned char *indexb; unsigned char *indexg; unsigned char *indexr; int j; int maxrgb; bool result; /* Open the output file. */ file_pointer = fopen ( file_name, "wb" ); if ( file_pointer == NULL ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " Cannot open the output file %s.\n", file_name ); return true; } /* Compute the maximum. */ maxrgb = 0; indexr = rarray; indexg = garray; indexb = barray; for ( j = 0; j < ysize; j++ ) { for ( i = 0; i < xsize; i++ ) { if ( maxrgb < *indexr ) { maxrgb = *indexr; } if ( maxrgb < *indexg ) { maxrgb = *indexg; } if ( maxrgb < *indexb ) { maxrgb = *indexb; } indexr = indexr + 1; indexg = indexg + 1; indexb = indexb + 1; } } /* Write the header. */ result = ppmb_write_header ( file_pointer, xsize, ysize, maxrgb ); if ( result ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " PPMB_WRITE_HEADER failed.\n" ); return true; } /* Write the data. */ result = ppmb_write_data ( file_pointer, xsize, ysize, rarray, garray, barray ); if ( result ) { printf ( "\n" ); printf ( "PPMB_WRITE: Fatal error!\n" ); printf ( " PPMB_WRITE_DATA failed.\n" ); return true; } /* Close the file. */ fclose ( file_pointer ); return false; }