Debian Codegraph @

imagery/i.rgb.his/rgb2his.c

raw
1 /******************************************************************************
2 NAME: RGB2HIS
3
4 PURPOSE To process red,green,blue bands to hue,intensity,saturation.
5
6 ALGORITHM:
7 Get red, green, blue from input buffer
8 Create the HIS bands
9 Write to output buffer
10
11 ASSUMPTION:
12 The input images are read to the input buffer.
13
14 ******************************************************************************/
15 /* For GRASS one row from each cell map is passed in and each cell in
16 each band is processed and written out. CWU GIS Lab: DBS 8/90 */
17
18 #include <grass/gis.h>
19 #include "globals.h"
20
21 void rgb2his(CELL *rowbuffer[3], int columns)
22 {
23 int sample; /* sample indicator */
24 double red; /* the red band output */
25 double green; /* the green band output */
26 double blue; /* the blue band output */
27 double scaler; /* red value */
28 double scaleg; /* green value */
29 double scaleb; /* blue value */
30 double high; /* maximum red, green, blue */
31 double low; /* minimum red, green, blue */
32 double intens; /* intensity */
33 double sat; /* saturation */
34 double hue = 0.0L; /* hue */
35
36 for (sample = 0; sample < columns; sample++) {
37 if (Rast_is_c_null_value(&rowbuffer[0][sample]) ||
38 Rast_is_c_null_value(&rowbuffer[1][sample]) ||
39 Rast_is_c_null_value(&rowbuffer[2][sample])) {
40 Rast_set_c_null_value(&rowbuffer[0][sample], 1);
41 Rast_set_c_null_value(&rowbuffer[1][sample], 1);
42 Rast_set_c_null_value(&rowbuffer[2][sample], 1);
43 continue;
44 }
45
46 scaler = (double)rowbuffer[0][sample];
47 scaler /= 255.0;
48 scaleg = (double)rowbuffer[1][sample];
49 scaleg /= 255.0;
50 scaleb = (double)rowbuffer[2][sample];
51 scaleb /= 255.0;
52
53 high = scaler;
54 if (scaleg > high)
55 high = scaleg;
56 if (scaleb > high)
57 high = scaleb;
58 low = scaler;
59 if (scaleg < low)
60 low = scaleg;
61 if (scaleb < low)
62 low = scaleb;
63 /*
64 calculate the lightness (intensity)
65 */
66 intens = ((high + low) / 2.0);
67 /*
68 if min = max the achromatic case R=G=B
69 */
70 if (high == low) {
71 sat = 0.0;
72 /* hue = -1.0; */
73 hue = 0.0;
74 rowbuffer[0][sample] = (unsigned char)hue;
75 rowbuffer[1][sample] = (unsigned char)(intens * 255.);
76 rowbuffer[2][sample] = (unsigned char)(sat * 255.);
77 }
78 /*
79 else chromatic case
80 */
81 else if (high != low) {
82 if (intens <= 0.5)
83 sat = (high - low) / (high + low);
84 else
85 /*
86 sat = (high-low)/(2 - (high-low));
87 */
88 sat = (high - low) / (2 - high - low);
89 red = (high - scaler) / (high - low);
90 green = (high - scaleg) / (high - low);
91 blue = (high - scaleb) / (high - low);
92 /*
93 resulting color between yellow and magenta
94 */
95 if (scaler == high)
96 hue = blue - green;
97 /*
98 resulting color between cyan and yellow
99 */
100 else if (scaleg == high)
101 hue = 2 + red - blue;
102 /*
103 resulting color between magenta and cyan
104 */
105 else if (scaleb == high)
106 hue = 4 + green - red;
107 /*
108 convert to degrees
109 */
110 hue *= 60.0;
111 /*
112 make nonnegative
113 */
114 if (hue < 0.0)
115 hue += 360.0;
116 /*
117 set the HIS output values
118 */
119 rowbuffer[0][sample] = (unsigned char)(255.0 * hue / 360.0 + 0.5);
120 rowbuffer[1][sample] = (unsigned char)(intens * 255. + 0.5);
121 rowbuffer[2][sample] = (unsigned char)(sat * 255. + 0.5);
122 }
123 }
124 }