home *** CD-ROM | disk | FTP | other *** search
- /*
- * rgb2gray.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /* RGB2GRAY: program converts 24-bit true color image to
- * grayscale image. Type of grayscale depends
- * on -t option.
- * usage: rgb2gray inFile outFile -t type
- * The <type> argument defines what type
- * of grayscale image to output
- * r|R = red plane
- * g|G = green plane
- * b|B = blue plane
- * i|I = intensity
- * h|H = hue
- * s|S = saturation
- *
- */
-
- #include "rgb2gray.h"
- unsigned char **image; /* input/output image */
- unsigned char **imagetmp; /* used for swapping RGB planes */
- extern short tiffInput; /* flag=0 if no ImageIn to set tags; else =1 */
-
- void
- main (int argc, char *argv[])
- {
- Image *imgIO; /* structure for I/O images */
- char type; /* type of image to output */
- int ix, iy;
- double r, g, b, intens, min, c, d, temp;
-
- if (argc < 4)
- usage (argv[0]);
-
- /* read the image in */
- imgIO = ImageIn (argv[1]);
-
- /* check to make sure it's an RGB image!! */
- if (imgIO->bps != 8 || imgIO->spp != 3) {
- printf ("Input file is not an RGB image!!!\n");
- exit (1);
- }
- /* reset tiffInput so that we write a grayscale file (i.e tags are not copied) */
- tiffInput = 0;
-
- type = argv[3][0];
- switch (type) {
- case 'r':
- case 'R':
- imagetmp = imgIO->img;
- imgIO->img = imgIO->imgR;
- imgIO->imgR = imagetmp;
- break;
- case 'g':
- case 'G':
- imagetmp = imgIO->img;
- imgIO->img = imgIO->imgG;
- imgIO->imgG = imagetmp;
- break;
- case 'b':
- case 'B':
- imagetmp = imgIO->img;
- imgIO->img = imgIO->imgB;
- imgIO->imgB = imagetmp;
- break;
- case 'i':
- case 'I':
- for (ix = 0; ix < imgIO->width; ix++) {
- for (iy = 0; iy < imgIO->height; iy++) {
- r = (double) imgIO->imgR[iy][ix] / 255.0;
- g = (double) imgIO->imgG[iy][ix] / 255.0;
- b = (double) imgIO->imgB[iy][ix] / 255.0;
- intens = (r + g + b) / 3.0;
- imgIO->img[iy][ix] = (unsigned char) (255.0 * intens + 0.5);
- }
- }
- break;
- case 'h':
- case 'H':
- for (ix = 0; ix < imgIO->width; ix++) {
- for (iy = 0; iy < imgIO->height; iy++) {
- r = (double) imgIO->imgR[iy][ix] / 255.0;
- g = (double) imgIO->imgG[iy][ix] / 255.0;
- b = (double) imgIO->imgB[iy][ix] / 255.0;
- c = 0.5 * (2.0 * r - g - b);
- d = sqrt ((r - g) * (r - g) + (r - b) * (g - b));
- if (d == 0.0)
- imgIO->img[iy][ix] = (unsigned char) 255; /* arbitrary value -> hue undefined */
- else {
- temp = c / d; /* imprecision causes > |1| */
- if (temp > 1.0)
- temp = 1.0;
- else if (temp < -1.0)
- temp = -1.0;
- temp = acos (temp);
- if (b > g)
- temp = 2.0 * M_PI - temp;
- imgIO->img[iy][ix] = (unsigned char) (temp * 100.0 / M_PI); /* scale 0-200 */
- }
- }
- }
- break;
- case 's':
- case 'S':
- for (ix = 0; ix < imgIO->width; ix++) {
- for (iy = 0; iy < imgIO->height; iy++) {
- r = (double) imgIO->imgR[iy][ix] / 255.0;
- g = (double) imgIO->imgG[iy][ix] / 255.0;
- b = (double) imgIO->imgB[iy][ix] / 255.0;
- intens = (r + g + b) / 3.0;
- min = (r < g) ? r : g;
- min = (min < b) ? min : b;
- if (intens == 0.0)
- imgIO->img[iy][ix] = (unsigned char) 0;
- else
- imgIO->img[iy][ix] = (unsigned char) (255.0 * (1.0 - min / intens) + 0.5);
- }
- }
- break;
- default:
- printf ("Illegal option %c\n", type);
- exit (1);
- }
- /* write the image out */
- ImageOut (argv[2], imgIO);
- exit (0);
- }
-
- /*
- * usage of routine
- */
-
- void
- usage (char *progname)
- {
- progname = last_bs (progname);
- printf ("USAGE: %s inimg outimg type [-L]\n", progname);
- printf ("\n%s accepts RGB color image input file, and produces\n", progname);
- printf ("a grayscale file specified by type.\n\n");
- printf ("ARGUMENTS:\n");
- printf (" inimg: input image filename (TIF)\n");
- printf (" outimg: output image filename (TIF)\n");
- printf (" type: r|R produces RED plane\n");
- printf (" g|G produces GREEN plane\n");
- printf (" b|B produces BLUE plane\n");
- printf (" i|I produces INTENSITY values\n");
- printf (" h|H produces HUE values\n");
- printf (" s|S produces SATURATION values\n");
- printf (" NOTE: the HUE values are arbitrarily scaled from\n");
- printf (" 0 to 200, where red=0, green=67, and blue=133.\n");
- printf (" The gray tvalue, i.e. red=green=blue,\n");
- printf (" is arbitrarily set to 255.\n\n");
- printf ("OPTIONS:\n");
- printf (" -L: print Software License for this module\n");
- exit (1);
- }
-