00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 void
00021 HSVTORGB (IMAGE * in,
00022 IMAGE ** out
00023
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
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
00069 if (!CHECKIMG (*out))
00070 GETMEM (in->nbnd, in->nlin, in->npix, out);
00071 if (!*out)
00072 goto the_end;
00073
00074
00075 if (LINES && !PROGRESS (psum = 0.0))
00076 goto the_end;
00077 pinc = 1.0 / (double) in->nlin;
00078
00079
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 }