#include #include #include /* Puzzle I08 -- filled circle 29 Apr 15 -- modified to concentric circles - lrb */ void makeCircles( FILE *image, int nrows, int ncols, int minGray, int maxGray, int numCircles ) { const int levelInc = (maxGray-minGray)/numCircles; /* increment for gray level */ 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( maxGray, image ); else fputc( maxGray-w*levelInc, image ); } } } void openImage( char *argv[], FILE **image, int *nrows, int *ncols, int *minGray, int *maxGray, 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 ); } *minGray = atoi( argv[4] ); if ( *minGray < 1 || *minGray > 255 ) { printf("minGray must be > 0 and < 256\n"); exit( EXIT_FAILURE ); } *maxGray = atoi( argv[5] ); if ( *maxGray < 1 || *maxGray > 255 ) { printf("maxGray must be > 0 and < 256\n"); exit( EXIT_FAILURE ); } *numCircles = atoi( argv[6] ); if ( *numCircles < 1 ) { printf("numCircles must be > 0\n"); exit( EXIT_FAILURE ); } } /* write out the PGM Header information | | The number of gray levels is assumed to be 255. | */ void writeHeader( FILE *image, int nrows, int ncols ) { fprintf( image, "P5 "); fprintf( image, "# Created by conCircles\n"); /* width, height, number of gray levels */ fprintf( image, "%d %d %d ", ncols, nrows, 255 ); } int main( int argc, char *argv[] ) { int nrows, ncols; FILE *image; int minGray, maxGray, numCircles; /* check the command line parameters */ if ( argc != 7 ) { printf("concircles fileName.pgm nrows ncols minGray maxGray numCircles\n"); return 0; } /* Open the image file, get number of rows and columns */ openImage( argv, &image, &nrows, &ncols, &minGray, &maxGray, &numCircles ); /* write header information */ writeHeader( image, nrows, ncols ); /* Create the Image */ makeCircles( image, nrows, ncols, minGray, maxGray, numCircles); /* close the file */ fclose ( image ); return 1; }