#include <stdlib.h>
#include <stdio.h>
#include "../basicColorImage.c"
int getColor( int arg, char *argv[] )
{
int value;
value = atoi( argv[arg] );
if ( value < 0 || value > 255 )
{
printf("color level %s must be between 0 and 255\n", argv[arg]);
exit( EXIT_FAILURE );
}
return value;
}
int main(int argc, char *argv[])
{
int r, nrows, c, ncols, red, grn, blu;
colorImage img ;
pixel pix;
/* check the command line parameters */
if ( argc != 7 )
{
printf("colorChip2 fileName.ppm nrows ncols red green blue\n");
return 0;
}
nrows = atoi( argv[2] );
if ( nrows < 1 )
{
printf("number of rows must be positive\n");
return 0;
}
ncols = atoi( argv[3] );
if ( nrows < 1 )
{
printf("number of columns must be positive\n");
return 0;
}
/* Get colors from the command line */
pix.red = getColor( 4, argv );
pix.grn = getColor( 5, argv );
pix.blu = getColor( 6, argv );
/* create the image structure */
if ( newColorImage( &img, nrows, ncols ) == NULL )
{
printf(">>error<< newColorImage can't allocate memory\n");
return;
}
/* fill in the pixel data */
for ( r=0; r<nrows; r++ )
for ( c=0; c<ncols; c++ )
setColorPixel( img, r, c, pix );
/* write out the image */
writePPMimage( img, argv[1] );
/* free memory */
freeColorImage( &img );
}
Comments: