I20 Answer
#include <stdlib.h>
#include <stdio.h>
#include "basicImage.c"
int main ( int argc, char* argv[] )
{
image img ;
int r, c;
int nrows, ncols, below, above, radius ;
int rc, cc; /* coordinates of center */
if ( argc != 7)
{
printf("diagImage fileName nrows ncols below above radius\n");
return EXIT_SUCCESS;
}
nrows = atoi( argv[2] );
ncols = atoi( argv[3] );
below = atoi( argv[4] );
above = atoi( argv[5] );
radius= atoi( argv[6] );
if ( newImage( &img, nrows, ncols ) == NULL )
{
printf(">>error<< can't allocate memory\n");
return;
}
/* calculate the coordinates of the center */
rc = nrows/2;
cc = ncols/2;
/* First create the diagonally split two level image */
for ( r = 0; r < img.nrows; r++ )
for ( c = 0; c < img.ncols; c++ )
if ( r > c )
setPixel( img, r, c, below );
else
setPixel( img, r, c, above );
/* start the random number generator */
srand( time(NULL) );
/* Next randomly select 25% of the pixels */
for ( j=0; j < (nrows*ncols)/4; j++ )
{
r = rand()%nrows;
c = rand()%ncols;
/* if the selected pixel is inside of the circle */
/* reverse its gray level */
if ( (r-rc)*(r-rc)+(c-cc)*(c-cc) < radius*radius )
if ( r > c )
setPixel( img, r, c, above );
else
setPixel( img, r, c, below );
}
writePGMImage( img, argv[1]);
freeImage( &img );
}