1 #include "igs_color_rgb_hls.h"
2 namespace {
pixel_rgba_(const double red_in,const double gre_in,const double blu_in,double & red_out,double & gre_out,double & blu_out,const double hue_pivot,const double hue_scale,const double hue_shift,const double lig_pivot,const double lig_scale,const double lig_shift,const double sat_pivot,const double sat_scale,const double sat_shift)3 void pixel_rgba_(const double red_in, const double gre_in, const double blu_in,
4 double &red_out, double &gre_out, double &blu_out,
5 const double hue_pivot // 0.0 ...0...360...
6 ,
7 const double hue_scale // 1.0 ...1...
8 ,
9 const double hue_shift // 0.0 ...0...360...
10 ,
11 const double lig_pivot // 0.0 ...0...1...
12 ,
13 const double lig_scale // 1.0 ...1...
14 ,
15 const double lig_shift // 0.0 ...0...1...
16 ,
17 const double sat_pivot // 0.0 ...0...1...
18 ,
19 const double sat_scale // 1.0 ...1...
20 ,
21 const double sat_shift // 0.0 ...0...1...
22 ) {
23 double hue, lig, sat;
24 igs::color::rgb_to_hls(red_in, gre_in, blu_in, hue, lig, sat);
25 if ((1.0 != hue_scale) || (0.0 != hue_shift)) {
26 hue -= hue_pivot;
27 while (hue < -180.0) {
28 hue += 360.0;
29 }
30 while (180.0 <= hue) {
31 hue -= 360.0;
32 }
33 hue *= hue_scale;
34 hue += hue_pivot;
35 hue += hue_shift;
36 while (hue < 0.0) {
37 hue += 360.0;
38 }
39 while (360.0 <= hue) {
40 hue -= 360.0;
41 }
42 }
43 if ((1.0 != lig_scale) || (0.0 != lig_shift)) {
44 lig -= lig_pivot;
45 lig *= lig_scale;
46 lig += lig_pivot;
47 lig += lig_shift;
48 lig = (lig < 0.0) ? 0.0 : ((1.0 < lig) ? 1.0 : lig);
49 }
50 if ((1.0 != sat_scale) || (0.0 != sat_shift)) {
51 sat -= sat_pivot;
52 sat *= sat_scale;
53 sat += sat_pivot;
54 sat += sat_shift;
55 sat = (sat < 0.0) ? 0.0 : ((1.0 < sat) ? 1.0 : sat);
56 }
57 igs::color::hls_to_rgb(hue, lig, sat, red_out, gre_out, blu_out);
58 }
59 }
60 //------------------------------------------------------------
61 #include <limits> /* std::numeric_limits */
62 #include "igs_ifx_common.h" /* igs::image::rgba */
63 #include "igs_hls_adjust.h"
64 namespace {
65 /* raster画像にノイズをのせるtemplate */
66 template <class IT, class RT>
change_template_(IT * image_array,const int height,const int width,const int channels,const RT * ref,const int ref_mode,const double hue_pivot,const double hue_scale,const double hue_shift,const double lig_pivot,const double lig_scale,const double lig_shift,const double sat_pivot,const double sat_scale,const double sat_shift,const bool add_blend_sw)67 void change_template_(
68 IT *image_array, const int height, const int width, const int channels
69
70 ,
71 const RT *ref /* 求める画像(out)と同じ高さ、幅、チャンネル数 */
72 ,
73 const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
74
75 ,
76 const double hue_pivot, const double hue_scale, const double hue_shift,
77 const double lig_pivot, const double lig_scale, const double lig_shift,
78 const double sat_pivot, const double sat_scale, const double sat_shift
79
80 ,
81 const bool add_blend_sw) {
82 const int t_max = std::numeric_limits<IT>::max();
83 const double div_val = static_cast<double>(t_max);
84 const double mul_val = static_cast<double>(t_max) + 0.999999;
85 const int pixsize = height * width;
86 const int r_max = std::numeric_limits<RT>::max();
87 if (igs::image::rgba::siz == channels) {
88 using namespace igs::image::rgba;
89 for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
90 /* 変化量初期値 */
91 double refv = 1.0;
92
93 /* 参照画像あればピクセル単位の画像変化量を得る */
94 if (ref != 0) {
95 refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
96 ref += channels; /* continue;の前に行うこと */
97 }
98
99 /* 加算合成で、Alpha値ゼロならRGB値を計算する必要はない */
100 if (add_blend_sw && (0 == image_array[alp])) {
101 continue;
102 }
103 /* 加算合成でなくAlpha合成の時は、
104 Alpha値がゼロでもRGB値は存在する(してもよい) */
105
106 /* RGB値を正規化 */
107 const IT *const ia = image_array;
108 const double rr1 = static_cast<double>(ia[red]) / div_val;
109 const double gg1 = static_cast<double>(ia[gre]) / div_val;
110 const double bb1 = static_cast<double>(ia[blu]) / div_val;
111
112 /* pivot,scale,shiftによってRGB値を変化する */
113 double rr2, gg2, bb2;
114 pixel_rgba_(rr1, gg1, bb1, rr2, gg2, bb2, hue_pivot, hue_scale, hue_shift,
115 lig_pivot, lig_scale, lig_shift, sat_pivot, sat_scale,
116 sat_shift);
117
118 /* 加算合成で、その値がMaxでなければ変化量に乗算 */
119 if (add_blend_sw && (ia[alp] < t_max)) {
120 refv *= static_cast<double>(ia[alp]) / div_val;
121 }
122
123 /* ピクセル単位の変化量があれば、RGBを調整する */
124 if ((ref != 0) || (add_blend_sw && (ia[alp] < t_max))) {
125 rr2 = rr1 + (rr2 - rr1) * refv;
126 gg2 = gg1 + (gg2 - gg1) * refv;
127 bb2 = bb1 + (bb2 - bb1) * refv;
128 }
129
130 /* 結果をRGBに戻す */
131 image_array[red] = static_cast<IT>(rr2 * mul_val);
132 image_array[gre] = static_cast<IT>(gg2 * mul_val);
133 image_array[blu] = static_cast<IT>(bb2 * mul_val);
134 }
135 } else if (igs::image::rgb::siz == channels) {
136 using namespace igs::image::rgb;
137 for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
138 double refv = 1.0;
139 if (ref != 0) {
140 refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
141 ref += channels;
142 }
143
144 const IT *const ia = image_array;
145 const double rr1 = static_cast<double>(ia[red]) / div_val;
146 const double gg1 = static_cast<double>(ia[gre]) / div_val;
147 const double bb1 = static_cast<double>(ia[blu]) / div_val;
148 double rr2, gg2, bb2;
149 pixel_rgba_(rr1, gg1, bb1, rr2, gg2, bb2, hue_pivot, hue_scale, hue_shift,
150 lig_pivot, lig_scale, lig_shift, sat_pivot, sat_scale,
151 sat_shift);
152
153 if (ref != 0) {
154 rr2 = rr1 + (rr2 - rr1) * refv;
155 gg2 = gg1 + (gg2 - gg1) * refv;
156 bb2 = bb1 + (bb2 - bb1) * refv;
157 }
158
159 image_array[red] = static_cast<IT>(rr2 * mul_val);
160 image_array[gre] = static_cast<IT>(gg2 * mul_val);
161 image_array[blu] = static_cast<IT>(bb2 * mul_val);
162 }
163 } else if (1 == channels) { /* grayscale */
164 for (int ii = 0; ii < pixsize; ++ii, image_array += channels) {
165 double refv = 1.0;
166 if (ref != 0) {
167 refv *= igs::color::ref_value(ref, channels, r_max, ref_mode);
168 ref += channels;
169 }
170
171 double li1 = static_cast<double>(image_array[0]) / div_val,
172 li2 = (li1 - lig_pivot) * lig_scale + lig_pivot + lig_shift;
173 li2 = (li2 < 0.0) ? 0.0 : ((1.0 < li2) ? 1.0 : li2);
174
175 if (ref != 0) {
176 li2 = li1 + (li2 - li1) * refv;
177 }
178
179 image_array[0] = static_cast<IT>(li2 * mul_val);
180 }
181 }
182 }
183 }
184
185 #include <stdexcept> /* std::domain_error */
change(unsigned char * image_array,const int height,const int width,const int channels,const int bits,const unsigned char * ref,const int ref_bits,const int ref_mode,const double hue_pivot,const double hue_scale,const double hue_shift,const double lig_pivot,const double lig_scale,const double lig_shift,const double sat_pivot,const double sat_scale,const double sat_shift,const bool add_blend_sw)186 void igs::hls_adjust::change(
187 unsigned char *image_array, const int height, const int width,
188 const int channels, const int bits
189
190 ,
191 const unsigned char *ref /* 求める画像と同じ高、幅、channels数 */
192 ,
193 const int ref_bits /* refがゼロのときはここもゼロ */
194 ,
195 const int ref_mode /* 0=R,1=G,2=B,3=A,4=Luminance,5=Nothing */
196
197 ,
198 const double hue_pivot, const double hue_scale, const double hue_shift,
199 const double lig_pivot, const double lig_scale, const double lig_shift,
200 const double sat_pivot, const double sat_scale, const double sat_shift
201
202 ,
203 const bool add_blend_sw) {
204 if ((1.0 == hue_scale) && (0.0 == hue_shift) && (1.0 == lig_scale) &&
205 (0.0 == lig_shift) && (1.0 == sat_scale) && (0.0 == sat_shift)) {
206 return;
207 }
208
209 if ((igs::image::rgba::siz != channels) &&
210 (igs::image::rgb::siz != channels) && (1 != channels) /* grayscale */
211 ) {
212 throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
213 }
214
215 /* rgb(a)画像にhls(a)でドットノイズを加える */
216 if ((std::numeric_limits<unsigned char>::digits == bits) &&
217 ((std::numeric_limits<unsigned char>::digits == ref_bits) ||
218 (0 == ref_bits))) {
219 change_template_(image_array, height, width, channels, ref, ref_mode,
220 hue_pivot, hue_scale, hue_shift, lig_pivot, lig_scale,
221 lig_shift, sat_pivot, sat_scale, sat_shift, add_blend_sw);
222 } else if ((std::numeric_limits<unsigned short>::digits == bits) &&
223 ((std::numeric_limits<unsigned char>::digits == ref_bits) ||
224 (0 == ref_bits))) {
225 change_template_(reinterpret_cast<unsigned short *>(image_array), height,
226 width, channels, ref, ref_mode, hue_pivot, hue_scale,
227 hue_shift, lig_pivot, lig_scale, lig_shift, sat_pivot,
228 sat_scale, sat_shift, add_blend_sw);
229 } else if ((std::numeric_limits<unsigned short>::digits == bits) &&
230 (std::numeric_limits<unsigned short>::digits == ref_bits)) {
231 change_template_(reinterpret_cast<unsigned short *>(image_array), height,
232 width, channels,
233 reinterpret_cast<const unsigned short *>(ref), ref_mode,
234 hue_pivot, hue_scale, hue_shift, lig_pivot, lig_scale,
235 lig_shift, sat_pivot, sat_scale, sat_shift, add_blend_sw);
236 } else if ((std::numeric_limits<unsigned char>::digits == bits) &&
237 (std::numeric_limits<unsigned short>::digits == ref_bits)) {
238 change_template_(image_array, height, width, channels,
239 reinterpret_cast<const unsigned short *>(ref), ref_mode,
240 hue_pivot, hue_scale, hue_shift, lig_pivot, lig_scale,
241 lig_shift, sat_pivot, sat_scale, sat_shift, add_blend_sw);
242 } else {
243 throw std::domain_error("Bad bits,Not uchar/ushort");
244 }
245 }
246