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
00021 RGBTOHSV (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   )
00026 {
00027   register short j, k;
00028   char msg[SLEN];
00029   double r, g, b, h, s, v, pinc, psum;
00030 
00031   if (TIMES)
00032     TIMING (T_RGBTOHSV);
00033   if (NAMES)
00034     {
00035       MESSAGE ('I', "");
00036       MESSAGE ('I', "RGBTOHSV");
00037       MESSAGE ('I', "");
00038       sprintf (msg, " Input image:   %s", in->text);
00039       MESSAGE ('I', msg);
00040       MESSAGE ('I', " ...............");
00041     }
00042 
00043   /* check input */
00044   if (!CHECKIMG (in))
00045     {
00046       MESSAGE ('E', " Can't identify image.");
00047       goto the_end;
00048     }
00049   else if (in->nbnd != 3)
00050     {
00051       MESSAGE ('E', " Input image must be a three-band image.");
00052       goto the_end;
00053     }
00054   else if ((RANGE (in), in->gmin) < (PIXEL) 0)
00055     {
00056       MESSAGE ('E', " RGB components must be equal to or greater than zero.");
00057       goto the_end;
00058     }
00059 
00060   /* create image of appropriate size */
00061   if (!CHECKIMG (*out))
00062     GETMEM (in->nbnd, in->nlin, in->npix, out);
00063   if (!*out)
00064     goto the_end;
00065 
00066   /* initialize progress indicator */
00067   if (LINES && !PROGRESS (psum = 0.0))
00068     goto the_end;
00069   pinc = 1.0 / (double) in->nlin;
00070 
00071   /* transform RGB to HSV */
00072   for (j = 0; j < in->nlin; j++)
00073     {
00074       for (k = 0; k < in->npix; k++)
00075         {
00076           r = (double) in->data[0][j][k];
00077           g = (double) in->data[1][j][k];
00078           b = (double) in->data[2][j][k];
00079           v = (r > g ? r : g) > b ? (r > g ? r : g) : b;
00080           s = (r < g ? r : g) < b ? (r < g ? r : g) : b;
00081           (*out)->data[2][j][k] = (PIXEL) v;
00082           if (v == s)
00083             {
00084               (*out)->data[0][j][k] =
00085                 j == 0 ? (PIXEL) SMIN : (*out)->data[0][j][k - 1];
00086               (*out)->data[1][j][k] = (PIXEL) SMIN;
00087             }
00088           else
00089             {
00090               if (r == v)
00091                 {
00092                   h =
00093                     g ==
00094                     s ? 5.0 + (v - b) / (v - s) : 1.0 - (v - g) / (v - s);
00095                 }
00096               else if (g == v)
00097                 {
00098                   h =
00099                     b ==
00100                     s ? 1.0 + (v - r) / (v - s) : 3.0 - (v - b) / (v - s);
00101                 }
00102               else              /* (b == s) */
00103                 {
00104                   h =
00105                     r ==
00106                     s ? 3.0 + (v - g) / (v - s) : 5.0 - (v - r) / (v - s);
00107                 }
00108               (*out)->data[0][j][k] = (PIXEL) (h / 6.0 * (double) SMAX);
00109               (*out)->data[1][j][k] = (PIXEL) ((v - s) / v * (double) SMAX);
00110             }
00111         }
00112       if (LINES && !PROGRESS (psum += pinc))
00113         goto the_end;
00114     }
00115 
00116 the_end:
00117   if (TIMES)
00118     TIMING (T_EXIT);
00119 }

Generated on Sun May 18 15:36:13 2003 for tclSadie by doxygen1.3