#include #include #include #include /* Puzzle I08 -- filled circle 29 Apr 15 -- modified to concentric circles - lrb PPM version */ void makeCircles( FILE *image, int nrows, int ncols, int numCircles ) { int r, c ; /* row and col of current pixel */ int rc, cc ; /* row and col of center of image */ int rd, cd ; /* row and col difference from center */ double radius, dist ; /* radius of circle, distance from center */ int rwidth; /* width of ring */ int w; /* work variable */ /* calculate center of image, size of radius */ rc = nrows/2; cc = ncols/2; if ( nrowsradius) { fputc( 255, image ); fputc( 255, image ); fputc( 255, image ); } else { fputc( rcolor[w], image ); fputc( gcolor[w], image ); fputc( bcolor[w], image ); } } } } void openImage( char *argv[], FILE **image, int *nrows, int *ncols, int *numCircles ) { /* open the image file for writing */ if ( (*image = fopen( argv[1], "wb") ) == NULL ) { printf("file %s could not be created\n", argv[1] ); exit( EXIT_FAILURE ); } *nrows = atoi( argv[2] ); if ( *nrows < 1 ) { printf("number of rows must be positive\n"); exit( EXIT_FAILURE ); } *ncols = atoi( argv[3] ); if ( *ncols < 1 ) { printf("number of columns must be positive\n"); exit( EXIT_FAILURE ); } *numCircles = atoi( argv[4] ); if ( *numCircles < 1 ) { printf("numCircles must be > 0\n"); exit( EXIT_FAILURE ); } } /* write out the PPM Header information | | The number of gray levels is assumed to be 255. | */ void writeHeader( FILE *image, int nrows, int ncols ) { fprintf( image, "P6 "); fprintf( image, "# Created by colCircles\n"); /* width, height, max color */ fprintf( image, "%d %d %d ", ncols, nrows, 255 ); } int main( int argc, char *argv[] ) { int nrows, ncols; FILE *image; int numCircles; /* check the command line parameters */ if ( argc != 5 ) { printf("colcircles fileName.ppm nrows ncols numCircles\n"); return 0; } /* Open the image file, get number of rows and columns */ openImage( argv, &image, &nrows, &ncols, &numCircles ); /* write header information */ writeHeader( image, nrows, ncols ); /* Create the Image */ makeCircles( image, nrows, ncols, numCircles); /* close the file */ fclose ( image ); return 1; }