/* * usage: makeelement box * makeelement disk * makeelement ring * * creates (on stdout) simple binary PGMs suitable for use as * structuring elements in binary morphology operations. */ #include "port.h" #include #ifndef UNIX #include #endif #include void write_pgm( FILE* f, int width, int height, unsigned char* data ); main( int argc, char **argv ) { unsigned char* image; int width, height; #ifndef UNIX _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif if ( argc < 2 ) { fprintf( stderr, "usage: %0 type [ ... ]\n", argv[0] ); fprintf( stderr, " box \n" ); fprintf( stderr, " disk \n" ); fprintf( stderr, " ring \n" ); exit( 1 ); } if ( strcmp( argv[1], "box" ) == 0 ) { if ( argc != 4 ) { fprintf( stderr, "box type must specify with and height.\n" ); exit( 1 ); } width = atoi( argv[2] ); height = atoi( argv[3] ); if ( width < 1 || height < 1 ) { fprintf( stderr, "invalid box dimensions.\n" ); exit( 1 ); } image = (unsigned char *)malloc( width * height ); memset( image, 255, width*height ); write_pgm( stdout, width, height, image ); free( image ); } else if ( strcmp( argv[1], "disk" ) == 0 || strcmp( argv[1], "ring" ) == 0 ) { int i, j; if ( argc != 3 ) { fprintf( stderr, "disk/ring type must specify diameter.\n" ); exit( 1 ); } width = height = atoi( argv[2] ); if ( width < 1 ) { fprintf( stderr, "invalid disk/ring diameter.\n" ); exit( 1 ); } image = (unsigned char *)malloc( width * height ); memset( image, 0, width*height ); for ( i = 0; i < width; ++i ) for ( j = 0; j < height; ++j ) if ( (width/2.0 - (i+0.5)) * (width/2.0 - (i+0.5)) + (height/2.0 - (j+0.5)) * (height/2.0 - (j+0.5)) <= (width/2.0)*(width/2.0) ) image[j*width+i] = 255; if ( strcmp( argv[1], "ring" ) == 0 ) { for ( i = 1; i < width-1; ++i ) for ( j = 1; j < height-1; ++j ) { if ( image[(j-1)*width+i] && image[(j+1)*width+i] && image[j*width+i+1] && image[j*width+i-1] ) image[j*width+i] = 128; } for ( i = 0; i < width*height; ++i ) if ( image[i] != 255 ) image[i] = 0; } write_pgm( stdout, width, height, image ); free( image ); } else { fprintf( stderr, "unknown element type.\n" ); exit( 1 ); } return 0; } void write_pgm( FILE* f, int width, int height, unsigned char* data ) { fprintf( f, "P5\n%d %d\n%d\n", width, height, 255 ); fwrite( data, width*height, 1, f ); fclose( f ); }