00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 void RGBTOHSV (
00021 IMAGE *in,
00022 IMAGE **out
00023
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
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
00052 if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00053 if (!*out) goto the_end;
00054
00055
00056 if (LINES && !PROGRESS(psum=0.0)) goto the_end;
00057 pinc = 1.0/(double)in->nlin;
00058
00059
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 {
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 }