Main Page   Data Structures   File List   Data Fields   Globals  

hsvtorgb.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 hue-saturation-value image                    */
00009 /*   to a red-green-blue 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 HSVTORGB (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, l;
00028   char msg[SLEN];
00029   double h, s, v, f, m, n, i, pinc, psum;
00030 
00031   if (TIMES)
00032     TIMING (T_HSVTORGB);
00033   if (NAMES)
00034     {
00035       MESSAGE ('I', "");
00036       MESSAGE ('I', "HSVTORGB");
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   RANGE (in);
00055   if (in->gmin < (PIXEL) 0)
00056     {
00057       MESSAGE ('E', " HSV components must be equal to or greater than zero.");
00058       goto the_end;
00059     }
00060   if (in->gmax > (PIXEL) SMAX)
00061     {
00062       sprintf (msg, " HSV components must be less than or equal to %d.",
00063                SMAX);
00064       MESSAGE ('E', msg);
00065       goto the_end;
00066     }
00067 
00068   /* create image of appropriate size */
00069   if (!CHECKIMG (*out))
00070     GETMEM (in->nbnd, in->nlin, in->npix, out);
00071   if (!*out)
00072     goto the_end;
00073 
00074   /* initialize progress indicator */
00075   if (LINES && !PROGRESS (psum = 0.0))
00076     goto the_end;
00077   pinc = 1.0 / (double) in->nlin;
00078 
00079   /* transform HSV to RGB */
00080   for (j = 0; j < in->nlin; j++)
00081     {
00082       for (k = 0; k < in->npix; k++)
00083         {
00084           h = 6.0 * (double) in->data[0][j][k] / (double) SMAX;
00085           s = (double) in->data[1][j][k] / (double) SMAX;
00086           v = (double) in->data[2][j][k];
00087           l = (short) floor (h);
00088           f = h - (double) l;
00089           m = v * (1.0 - s);
00090           n = v * (1.0 - (s * f));
00091           i = v * (1.0 - (s * (1.0 - f)));
00092           switch (l)
00093             {
00094             case 0:
00095               (*out)->data[0][j][k] = (PIXEL) v;
00096               (*out)->data[1][j][k] = (PIXEL) i;
00097               (*out)->data[2][j][k] = (PIXEL) m;
00098               break;
00099             case 1:
00100               (*out)->data[0][j][k] = (PIXEL) n;
00101               (*out)->data[1][j][k] = (PIXEL) v;
00102               (*out)->data[2][j][k] = (PIXEL) m;
00103               break;
00104             case 2:
00105               (*out)->data[0][j][k] = (PIXEL) m;
00106               (*out)->data[1][j][k] = (PIXEL) v;
00107               (*out)->data[2][j][k] = (PIXEL) i;
00108               break;
00109             case 3:
00110               (*out)->data[0][j][k] = (PIXEL) m;
00111               (*out)->data[1][j][k] = (PIXEL) n;
00112               (*out)->data[2][j][k] = (PIXEL) v;
00113               break;
00114             case 4:
00115               (*out)->data[0][j][k] = (PIXEL) i;
00116               (*out)->data[1][j][k] = (PIXEL) m;
00117               (*out)->data[2][j][k] = (PIXEL) v;
00118               break;
00119             default:
00120               (*out)->data[0][j][k] = (PIXEL) v;
00121               (*out)->data[1][j][k] = (PIXEL) m;
00122               (*out)->data[2][j][k] = (PIXEL) n;
00123               break;
00124             }
00125         }
00126       if (LINES && !PROGRESS (psum += pinc))
00127         goto the_end;
00128     }
00129 
00130 the_end:
00131   if (TIMES)
00132     TIMING (T_EXIT);
00133 }

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