#include <stdlib.h>
#include <stdio.h>
#include "../basicImage.c"
#include "../basicColorImage.c"
void toColorChannels( colorImage cimg, image red, image grn, image blu )
{
int r, c, avg;
pixel pix;
for ( r=0; r<cimg.nrows; r++ )
for ( c=0; c<cimg.ncols; c++ )
{
pix = getColorPixel( cimg, r, c ) ;
setPixel( red, r, c, pix.red );
setPixel( grn, r, c, pix.grn );
setPixel( blu, r, c, pix.blu );
}
}
void channelsToColor( colorImage cimg, image red, image grn, image blu )
{
int r, c, avg;
pixel pix;
for ( r=0; r<cimg.nrows; r++ )
for ( c=0; c<cimg.ncols; c++ )
{
pix.red = getPixel( red, r, c );
pix.grn = getPixel( grn, r, c );
pix.blu = getPixel( blu, r, c );
setColorPixel( cimg, r, c, pix );
}
}
int main ( int argc, char* argv[] )
{
colorImage cimg;
image red, green, blue;
int threshold = 0;
int flag = 0;
if ( argc != 6 ||
((strcmp( argv[1], "-s")!=0) && (strcmp( argv[1], "-c")!=0)) )
{
printf("colorToChannels -s colorImage.ppm red.pgm green.pgm blue.pgm\n");
printf(" --- separate color image into color channels\n");
printf("colorToChannels -c colorImage.ppm red.pgm green.pgm blue.pgm\n");
printf(" --- combine color channels into color image\n");
exit( EXIT_FAILURE );
}
if ( strcmp( argv[1], "-s")== 0 )
{
/* read in the old image */
readPPMimage( &cimg, argv[2]);
/* create three empty gray level images*/
if ( newImage( &red, cimg.nrows, cimg.ncols ) == NULL )
{
printf(">>error<< can't allocate memory for red image\n");
exit( EXIT_FAILURE );
}
if ( newImage( &green, cimg.nrows, cimg.ncols ) == NULL )
{
printf(">>error<< can't allocate memory for green image\n");
exit( EXIT_FAILURE );
}
if ( newImage( &blue, cimg.nrows, cimg.ncols ) == NULL )
{
printf(">>error<< can't allocate memory for blue image\n");
exit( EXIT_FAILURE );
}
/* fill in gray level image */
toColorChannels( cimg, red, green, blue );
/* write the images to disk */
writePGMimage( red, argv[3]);
writePGMimage( green, argv[4]);
writePGMimage( blue, argv[5]);
}
else
{
/* read in three color channel images */
readPGMimage( &red, argv[3]) ;
readPGMimage( &green, argv[4]) ;
readPGMimage( &blue, argv[5]) ;
if ( red.nrows != green.nrows || red.ncols != green.ncols )
{
printf(">>error<< red and green channels are different sizes\n");
exit( EXIT_FAILURE );
}
if ( red.nrows != blue.nrows || red.ncols != blue.ncols )
{
printf(">>error<< red and blue channels are different sizes\n");
exit( EXIT_FAILURE );
}
/* create empty color image */
if ( newColorImage( &cimg, red.nrows, red.ncols ) == NULL )
{
printf(">>error<< can't allocate memory for color image\n");
exit( EXIT_FAILURE );
}
/* fill in color image */
channelsToColor( cimg, red, green, blue );
/* write the color image to disk */
writePPMimage( cimg, argv[2]);
}
freeColorImage( &cimg );
freeImage( &red );
freeImage( &green );
freeImage( &blue );
}
Comments: