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

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