Main Page   Data Structures   File List   Data Fields   Globals  

rgbtohsv.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 /*-Copyright Information------------------------------------------------------*/
00004 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00005 /*----------------------------------------------------------------------------*/
00006 /*-General Information--------------------------------------------------------*/
00007 /*                                                                            */
00008 /*   This function transforms a red-green-blue image                          */
00009 /*   to a hue-saturation-value image.                                         */
00010 /*                                                                            */
00011 /*----------------------------------------------------------------------------*/
00012 /*-Background Information-----------------------------------------------------*/
00013 /*                                                                            */
00014 /*   Smith, A.R.:                                                             */
00015 /*   "Color Gamut Transform Pairs"                                            */
00016 /*   Computer Graphics, Vol. 12, No. 3 (August 1978), pp. 12-19.              */
00017 /*                                                                            */
00018 /*----------------------------------------------------------------------------*/
00019 /*-Interface Information------------------------------------------------------*/
00020 void RGBTOHSV (
00021 IMAGE *in,      /*  I   Pointer to the input image.                           */
00022 IMAGE **out     /*  O   Address of a pointer to the output image.             */
00023                 /*      The input image may be overwritten (out = &in).       */
00024 /*----------------------------------------------------------------------------*/
00025 ) { register short j, k;
00026     char   msg[SLEN];
00027     double r, g, b, h, s, v, pinc, psum;
00028 
00029     if (TIMES) TIMING(T_RGBTOHSV);
00030     if (NAMES) {
00031         MESSAGE('I',"");
00032         MESSAGE('I',"RGBTOHSV");
00033         MESSAGE('I',"");
00034         sprintf(msg," Input image:   %s",in->text);
00035         MESSAGE('I',msg);
00036         MESSAGE('I'," ...............");
00037     }
00038 
00039     /* check input */
00040     if (!CHECKIMG(in)) {
00041         MESSAGE('E'," Can't identify image.");
00042         goto the_end;
00043     } else if (in->nbnd != 3) {
00044         MESSAGE('E'," Input image must be a three-band image.");
00045         goto the_end;
00046     } else if ((RANGE(in),in->gmin) < (PIXEL)0) {
00047         MESSAGE('E'," RGB components must be equal to or greater than zero.");
00048         goto the_end;
00049     }
00050 
00051     /* create image of appropriate size */
00052     if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00053     if (!*out) goto the_end;
00054 
00055     /* initialize progress indicator */
00056     if (LINES  &&  !PROGRESS(psum=0.0)) goto the_end;
00057     pinc = 1.0/(double)in->nlin;
00058 
00059     /* transform RGB to HSV */
00060     for (j=0; j<in->nlin; j++) {
00061         for (k=0; k<in->npix; k++) {
00062             r = (double)in->data[0][j][k];
00063             g = (double)in->data[1][j][k];
00064             b = (double)in->data[2][j][k];
00065             v = (r > g  ?  r : g) > b  ?  (r > g  ?  r : g) : b;
00066             s = (r < g  ?  r : g) < b  ?  (r < g  ?  r : g) : b;
00067             (*out)->data[2][j][k] = (PIXEL)v;
00068             if (v == s) {
00069                 (*out)->data[0][j][k] = j == 0  ?  (PIXEL)SMIN : (*out)->data[0][j][k-1];
00070                 (*out)->data[1][j][k] = (PIXEL)SMIN;
00071             } else {
00072                 if (r == v) {
00073                     h = g == s  ?  5.0+(v-b)/(v-s) : 1.0-(v-g)/(v-s);
00074                 } else if (g == v) {
00075                     h = b == s  ?  1.0+(v-r)/(v-s) : 3.0-(v-b)/(v-s);
00076                 } else /* (b == s) */ {
00077                     h = r == s  ?  3.0+(v-g)/(v-s) : 5.0-(v-r)/(v-s);
00078                 }
00079                 (*out)->data[0][j][k] = (PIXEL)(h/6.0*(double)SMAX);
00080                 (*out)->data[1][j][k] = (PIXEL)((v-s)/v*(double)SMAX);
00081             }
00082         }
00083         if (LINES  &&  !PROGRESS(psum+=pinc)) goto the_end;
00084     }
00085 
00086     the_end:
00087     if (TIMES) TIMING(T_EXIT);
00088 }

Generated on Wed Apr 9 08:56:10 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002