#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 = {0,0,0};
/* check the command line parameters */
if ( argc != 7 )
{
printf("singlePixel fileName.ppm nrows ncols red green blue\n");
return 0;
}
nrows = atoi( argv[2] );
ncols = atoi( argv[3] );
/* 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 );
/* Get colors from the command line */
pix.red = getColor( 4, argv );
pix.grn = getColor( 5, argv );
pix.blu = getColor( 6, argv );
setColorPixel( img, nrows/2, ncols/2, pix );
/* write out the image */
writePPMimage( img, argv[1] );
/* free memory */
freeColorImage( &img );
}
Comments: Change the progam so that it puts two pixels next to each other, or two pixels separated by a black pixel. Do this to get an intuition about how pixels in the image file corresponds to physical pixels on the monitor. Look at your images on a variety of monitors with a variety of resolutions.