| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % GGGG EEEEE M M % |
| 7 | % G E MM MM % |
| 8 | % G GG EEE M M M % |
| 9 | % G G E M M % |
| 10 | % GGGG EEEEE M M % |
| 11 | % % |
| 12 | % % |
| 13 | % Graphic Gems - Graphic Support Methods % |
| 14 | % % |
| 15 | % Software Design % |
| cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % August 1996 % |
| 18 | % % |
| 19 | % % |
| cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/color-private.h" |
| 45 | #include "MagickCore/draw.h" |
| 46 | #include "MagickCore/gem.h" |
| cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 47 | #include "MagickCore/gem-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #include "MagickCore/image.h" |
| 49 | #include "MagickCore/image-private.h" |
| 50 | #include "MagickCore/log.h" |
| 51 | #include "MagickCore/memory_.h" |
| 52 | #include "MagickCore/pixel-accessor.h" |
| cristy | 35f1530 | 2012-06-07 14:59:02 +0000 | [diff] [blame] | 53 | #include "MagickCore/pixel-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 54 | #include "MagickCore/quantum.h" |
| 55 | #include "MagickCore/quantum-private.h" |
| 56 | #include "MagickCore/random_.h" |
| 57 | #include "MagickCore/resize.h" |
| 58 | #include "MagickCore/transform.h" |
| 59 | #include "MagickCore/signature-private.h" |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 63 | % % |
| 64 | % % |
| 65 | % % |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 66 | % C o n v e r t H C L T o R G B % |
| 67 | % % |
| 68 | % % |
| 69 | % % |
| 70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 | % |
| 72 | % ConvertHCLToRGB() transforms a (hue, chroma, luma) to a (red, green, |
| 73 | % blue) triple. |
| 74 | % |
| 75 | % The format of the ConvertHCLToRGBImage method is: |
| 76 | % |
| 77 | % void ConvertHCLToRGB(const double hue,const double chroma, |
| 78 | % const double luma,double *red,double *green,double *blue) |
| 79 | % |
| 80 | % A description of each parameter follows: |
| 81 | % |
| cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 82 | % o hue, chroma, luma: A double value representing a component of the |
| 83 | % HCL color space. |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 84 | % |
| 85 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 86 | % |
| 87 | */ |
| 88 | MagickPrivate void ConvertHCLToRGB(const double hue,const double chroma, |
| 89 | const double luma,double *red,double *green,double *blue) |
| 90 | { |
| 91 | double |
| cristy | 5756d82 | 2013-04-09 01:14:34 +0000 | [diff] [blame] | 92 | b, |
| 93 | c, |
| 94 | g, |
| 95 | h, |
| 96 | m, |
| 97 | r, |
| cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 98 | x; |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | Convert HCL to RGB colorspace. |
| 102 | */ |
| 103 | assert(red != (double *) NULL); |
| 104 | assert(green != (double *) NULL); |
| 105 | assert(blue != (double *) NULL); |
| cristy | 5756d82 | 2013-04-09 01:14:34 +0000 | [diff] [blame] | 106 | h=6.0*hue; |
| 107 | c=chroma; |
| 108 | x=c*(1.0-fabs(fmod(h,2.0)-1.0)); |
| 109 | r=0.0; |
| 110 | g=0.0; |
| 111 | b=0.0; |
| 112 | if ((0.0 <= h) && (h < 1.0)) |
| 113 | { |
| 114 | r=c; |
| 115 | g=x; |
| 116 | } |
| 117 | else |
| 118 | if ((1.0 <= h) && (h < 2.0)) |
| 119 | { |
| 120 | r=x; |
| 121 | g=c; |
| 122 | } |
| 123 | else |
| 124 | if ((2.0 <= h) && (h < 3.0)) |
| 125 | { |
| 126 | g=c; |
| 127 | b=x; |
| 128 | } |
| 129 | else |
| 130 | if ((3.0 <= h) && (h < 4.0)) |
| 131 | { |
| 132 | g=x; |
| 133 | b=c; |
| 134 | } |
| 135 | else |
| 136 | if ((4.0 <= h) && (h < 5.0)) |
| 137 | { |
| 138 | r=x; |
| 139 | b=c; |
| 140 | } |
| 141 | else |
| 142 | if ((5.0 <= h) && (h < 6.0)) |
| 143 | { |
| 144 | r=c; |
| 145 | b=x; |
| 146 | } |
| cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 147 | m=luma-(0.298839*r+0.586811*g+0.114350*b); |
| 148 | *red=QuantumRange*(r+m); |
| 149 | *green=QuantumRange*(g+m); |
| 150 | *blue=QuantumRange*(b+m); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 155 | % % |
| 156 | % % |
| 157 | % % |
| 158 | % C o n v e r t H C L p T o R G B % |
| 159 | % % |
| 160 | % % |
| 161 | % % |
| 162 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 163 | % |
| 164 | % ConvertHCLpToRGB() transforms a (hue, chroma, luma) to a (red, green, |
| 165 | % blue) triple. Since HCL colorspace is wider than RGB, we instead choose a |
| 166 | % saturation strategy to project it on the RGB cube. |
| 167 | % |
| 168 | % The format of the ConvertHCLpToRGBImage method is: |
| 169 | % |
| 170 | % void ConvertHCLpToRGB(const double hue,const double chroma, |
| 171 | % const double luma,double *red,double *green,double *blue) |
| 172 | % |
| 173 | % A description of each parameter follows: |
| 174 | % |
| 175 | % o hue, chroma, luma: A double value representing a componenet of the |
| 176 | % HCLp color space. |
| 177 | % |
| 178 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 179 | % |
| 180 | */ |
| 181 | MagickPrivate void ConvertHCLpToRGB(const double hue,const double chroma, |
| 182 | const double luma,double *red,double *green,double *blue) |
| 183 | { |
| 184 | double |
| 185 | b, |
| 186 | c, |
| 187 | g, |
| 188 | h, |
| 189 | m, |
| 190 | r, |
| 191 | x, |
| 192 | z; |
| 193 | |
| 194 | /* |
| 195 | Convert HCLp to RGB colorspace. |
| 196 | */ |
| 197 | assert(red != (double *) NULL); |
| 198 | assert(green != (double *) NULL); |
| 199 | assert(blue != (double *) NULL); |
| 200 | h=6.0*hue; |
| 201 | c=chroma; |
| 202 | x=c*(1.0-fabs(fmod(h,2.0)-1.0)); |
| 203 | r=0.0; |
| 204 | g=0.0; |
| 205 | b=0.0; |
| 206 | if ((0.0 <= h) && (h < 1.0)) |
| 207 | { |
| 208 | r=c; |
| 209 | g=x; |
| 210 | } |
| 211 | else |
| 212 | if ((1.0 <= h) && (h < 2.0)) |
| 213 | { |
| 214 | r=x; |
| 215 | g=c; |
| 216 | } |
| 217 | else |
| 218 | if ((2.0 <= h) && (h < 3.0)) |
| 219 | { |
| 220 | g=c; |
| 221 | b=x; |
| 222 | } |
| 223 | else |
| 224 | if ((3.0 <= h) && (h < 4.0)) |
| 225 | { |
| 226 | g=x; |
| 227 | b=c; |
| 228 | } |
| 229 | else |
| 230 | if ((4.0 <= h) && (h < 5.0)) |
| 231 | { |
| 232 | r=x; |
| 233 | b=c; |
| 234 | } |
| 235 | else |
| 236 | if ((5.0 <= h) && (h < 6.0)) |
| 237 | { |
| 238 | r=c; |
| 239 | b=x; |
| 240 | } |
| 241 | m=luma-(0.298839*r+0.586811*g+0.114350*b); |
| cristy | c41e5fa | 2013-05-01 01:24:25 +0000 | [diff] [blame] | 242 | z=1.0; |
| 243 | if (m < 0.0) |
| 244 | { |
| 245 | z=luma/(luma-m); |
| 246 | m=0.0; |
| 247 | } |
| 248 | else |
| 249 | if (m+c > 1.0) |
| 250 | { |
| 251 | z=(1.0-luma)/(m+c-luma); |
| 252 | m=1.0-z*c; |
| 253 | } |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 254 | *red=QuantumRange*(z*r+m); |
| 255 | *green=QuantumRange*(z*g+m); |
| 256 | *blue=QuantumRange*(z*b+m); |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | % % |
| 262 | % % |
| 263 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 264 | % C o n v e r t H S B T o R G B % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | % % |
| 266 | % % |
| 267 | % % |
| 268 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 269 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 270 | % ConvertHSBToRGB() transforms a (hue, saturation, brightness) to a (red, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | % green, blue) triple. |
| 272 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 273 | % The format of the ConvertHSBToRGBImage method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 275 | % void ConvertHSBToRGB(const double hue,const double saturation, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 276 | % const double brightness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | % |
| 278 | % A description of each parameter follows: |
| 279 | % |
| 280 | % o hue, saturation, brightness: A double value representing a |
| 281 | % component of the HSB color space. |
| 282 | % |
| 283 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 284 | % |
| 285 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 286 | MagickPrivate void ConvertHSBToRGB(const double hue,const double saturation, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 287 | const double brightness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 288 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 289 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 290 | f, |
| 291 | h, |
| 292 | p, |
| 293 | q, |
| 294 | t; |
| 295 | |
| 296 | /* |
| 297 | Convert HSB to RGB colorspace. |
| 298 | */ |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 299 | assert(red != (double *) NULL); |
| 300 | assert(green != (double *) NULL); |
| 301 | assert(blue != (double *) NULL); |
| cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 302 | if (saturation == 0.0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 304 | *red=QuantumRange*brightness; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | *green=(*red); |
| 306 | *blue=(*red); |
| 307 | return; |
| 308 | } |
| cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 309 | h=6.0*(hue-floor(hue)); |
| 310 | f=h-floor((double) h); |
| 311 | p=brightness*(1.0-saturation); |
| 312 | q=brightness*(1.0-saturation*f); |
| 313 | t=brightness*(1.0-(saturation*(1.0-f))); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 314 | switch ((int) h) |
| 315 | { |
| 316 | case 0: |
| 317 | default: |
| 318 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 319 | *red=QuantumRange*brightness; |
| 320 | *green=QuantumRange*t; |
| 321 | *blue=QuantumRange*p; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | case 1: |
| 325 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 326 | *red=QuantumRange*q; |
| 327 | *green=QuantumRange*brightness; |
| 328 | *blue=QuantumRange*p; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | case 2: |
| 332 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 333 | *red=QuantumRange*p; |
| 334 | *green=QuantumRange*brightness; |
| 335 | *blue=QuantumRange*t; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | case 3: |
| 339 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 340 | *red=QuantumRange*p; |
| 341 | *green=QuantumRange*q; |
| 342 | *blue=QuantumRange*brightness; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 343 | break; |
| 344 | } |
| 345 | case 4: |
| 346 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 347 | *red=QuantumRange*t; |
| 348 | *green=QuantumRange*p; |
| 349 | *blue=QuantumRange*brightness; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case 5: |
| 353 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 354 | *red=QuantumRange*brightness; |
| 355 | *green=QuantumRange*p; |
| 356 | *blue=QuantumRange*q; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 364 | % % |
| 365 | % % |
| 366 | % % |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 367 | % C o n v e r t H S I T o R G B % |
| 368 | % % |
| 369 | % % |
| 370 | % % |
| 371 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 372 | % |
| 373 | % ConvertHSIToRGB() transforms a (hue, saturation, intensity) to a (red, |
| 374 | % green, blue) triple. |
| 375 | % |
| 376 | % The format of the ConvertHSIToRGBImage method is: |
| 377 | % |
| 378 | % void ConvertHSIToRGB(const double hue,const double saturation, |
| 379 | % const double intensity,double *red,double *green,double *blue) |
| 380 | % |
| 381 | % A description of each parameter follows: |
| 382 | % |
| 383 | % o hue, saturation, intensity: A double value representing a |
| 384 | % component of the HSI color space. |
| 385 | % |
| 386 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 387 | % |
| 388 | */ |
| 389 | MagickPrivate void ConvertHSIToRGB(const double hue,const double saturation, |
| 390 | const double intensity,double *red,double *green,double *blue) |
| 391 | { |
| 392 | double |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 393 | b, |
| cristy | 6eb1817 | 2013-05-07 18:43:59 +0000 | [diff] [blame] | 394 | g, |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 395 | h, |
| 396 | r; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | Convert HSI to RGB colorspace. |
| 400 | */ |
| 401 | assert(red != (double *) NULL); |
| 402 | assert(green != (double *) NULL); |
| 403 | assert(blue != (double *) NULL); |
| 404 | h=360.0*hue; |
| 405 | h-=360.0*floor(h/360.0); |
| 406 | if (h < 120.0) |
| 407 | { |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 408 | b=intensity*(1.0-saturation); |
| 409 | r=intensity*(1.0+saturation*cos(h*(MagickPI/180.0))/cos((60.0-h)* |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 410 | (MagickPI/180.0))); |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 411 | g=3.0*intensity-r-b; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | if (h < 240.0) |
| 415 | { |
| 416 | h-=120.0; |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 417 | r=intensity*(1.0-saturation); |
| 418 | g=intensity*(1.0+saturation*cos(h*(MagickPI/180.0))/cos((60.0-h)* |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 419 | (MagickPI/180.0))); |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 420 | b=3.0*intensity-r-g; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 421 | } |
| 422 | else |
| 423 | { |
| 424 | h-=240.0; |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 425 | g=intensity*(1.0-saturation); |
| 426 | b=intensity*(1.0+saturation*cos(h*(MagickPI/180.0))/cos((60.0-h)* |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 427 | (MagickPI/180.0))); |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 428 | r=3.0*intensity-g-b; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 429 | } |
| cristy | bed2d4b | 2013-05-07 18:41:38 +0000 | [diff] [blame] | 430 | *red=QuantumRange*r; |
| 431 | *green=QuantumRange*g; |
| 432 | *blue=QuantumRange*b; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
| 436 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 437 | % % |
| 438 | % % |
| 439 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 440 | % C o n v e r t H S L T o R G B % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | % % |
| 442 | % % |
| 443 | % % |
| 444 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 445 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 446 | % ConvertHSLToRGB() transforms a (hue, saturation, lightness) to a (red, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 447 | % green, blue) triple. |
| 448 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 449 | % The format of the ConvertHSLToRGBImage method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 450 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 451 | % void ConvertHSLToRGB(const double hue,const double saturation, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 452 | % const double lightness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 453 | % |
| 454 | % A description of each parameter follows: |
| 455 | % |
| 456 | % o hue, saturation, lightness: A double value representing a |
| 457 | % component of the HSL color space. |
| 458 | % |
| 459 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 460 | % |
| 461 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 462 | MagickExport void ConvertHSLToRGB(const double hue,const double saturation, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 463 | const double lightness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 465 | double |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 466 | c, |
| 467 | h, |
| 468 | min, |
| 469 | x; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | Convert HSL to RGB colorspace. |
| 473 | */ |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 474 | assert(red != (double *) NULL); |
| 475 | assert(green != (double *) NULL); |
| 476 | assert(blue != (double *) NULL); |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 477 | h=hue*360.0; |
| 478 | if (lightness <= 0.5) |
| 479 | c=2.0*lightness*saturation; |
| cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 480 | else |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 481 | c=(2.0-2.0*lightness)*saturation; |
| 482 | min=lightness-0.5*c; |
| 483 | h-=360.0*floor(h/360.0); |
| 484 | h/=60.0; |
| 485 | x=c*(1.0-fabs(h-2.0*floor(h/2.0)-1.0)); |
| 486 | switch ((int) floor(h)) |
| 487 | { |
| 488 | case 0: |
| 489 | { |
| 490 | *red=QuantumRange*(min+c); |
| 491 | *green=QuantumRange*(min+x); |
| 492 | *blue=QuantumRange*min; |
| 493 | break; |
| 494 | } |
| 495 | case 1: |
| 496 | { |
| 497 | *red=QuantumRange*(min+x); |
| 498 | *green=QuantumRange*(min+c); |
| 499 | *blue=QuantumRange*min; |
| 500 | break; |
| 501 | } |
| 502 | case 2: |
| 503 | { |
| 504 | *red=QuantumRange*min; |
| 505 | *green=QuantumRange*(min+c); |
| 506 | *blue=QuantumRange*(min+x); |
| 507 | break; |
| 508 | } |
| 509 | case 3: |
| 510 | { |
| 511 | *red=QuantumRange*min; |
| 512 | *green=QuantumRange*(min+x); |
| 513 | *blue=QuantumRange*(min+c); |
| 514 | break; |
| 515 | } |
| 516 | case 4: |
| 517 | { |
| 518 | *red=QuantumRange*(min+x); |
| 519 | *green=QuantumRange*min; |
| 520 | *blue=QuantumRange*(min+c); |
| 521 | break; |
| 522 | } |
| 523 | case 5: |
| 524 | { |
| 525 | *red=QuantumRange*(min+c); |
| 526 | *green=QuantumRange*min; |
| 527 | *blue=QuantumRange*(min+x); |
| 528 | break; |
| 529 | } |
| 530 | default: |
| 531 | { |
| 532 | *red=0.0; |
| 533 | *green=0.0; |
| 534 | *blue=0.0; |
| 535 | } |
| 536 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 541 | % % |
| 542 | % % |
| 543 | % % |
| cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 544 | % C o n v e r t H S V T o R G B % |
| 545 | % % |
| 546 | % % |
| 547 | % % |
| 548 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 549 | % |
| 550 | % ConvertHSVToRGB() transforms a (hue, saturation, value) to a (red, |
| 551 | % green, blue) triple. |
| 552 | % |
| 553 | % The format of the ConvertHSVToRGBImage method is: |
| 554 | % |
| 555 | % void ConvertHSVToRGB(const double hue,const double saturation, |
| 556 | % const double value,double *red,double *green,double *blue) |
| 557 | % |
| 558 | % A description of each parameter follows: |
| 559 | % |
| 560 | % o hue, saturation, value: A double value representing a |
| 561 | % component of the HSV color space. |
| 562 | % |
| 563 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 564 | % |
| 565 | */ |
| 566 | MagickPrivate void ConvertHSVToRGB(const double hue,const double saturation, |
| 567 | const double value,double *red,double *green,double *blue) |
| 568 | { |
| 569 | double |
| 570 | c, |
| 571 | h, |
| 572 | min, |
| 573 | x; |
| 574 | |
| 575 | /* |
| 576 | Convert HSV to RGB colorspace. |
| 577 | */ |
| 578 | assert(red != (double *) NULL); |
| 579 | assert(green != (double *) NULL); |
| 580 | assert(blue != (double *) NULL); |
| 581 | h=hue*360.0; |
| 582 | c=value*saturation; |
| 583 | min=value-c; |
| 584 | h-=360.0*floor(h/360.0); |
| 585 | h/=60.0; |
| 586 | x=c*(1.0-fabs(h-2.0*floor(h/2.0)-1.0)); |
| 587 | switch ((int) floor(h)) |
| 588 | { |
| 589 | case 0: |
| 590 | { |
| 591 | *red=QuantumRange*(min+c); |
| 592 | *green=QuantumRange*(min+x); |
| 593 | *blue=QuantumRange*min; |
| 594 | break; |
| 595 | } |
| 596 | case 1: |
| 597 | { |
| 598 | *red=QuantumRange*(min+x); |
| 599 | *green=QuantumRange*(min+c); |
| 600 | *blue=QuantumRange*min; |
| 601 | break; |
| 602 | } |
| 603 | case 2: |
| 604 | { |
| 605 | *red=QuantumRange*min; |
| 606 | *green=QuantumRange*(min+c); |
| 607 | *blue=QuantumRange*(min+x); |
| 608 | break; |
| 609 | } |
| 610 | case 3: |
| 611 | { |
| 612 | *red=QuantumRange*min; |
| 613 | *green=QuantumRange*(min+x); |
| 614 | *blue=QuantumRange*(min+c); |
| 615 | break; |
| 616 | } |
| 617 | case 4: |
| 618 | { |
| 619 | *red=QuantumRange*(min+x); |
| 620 | *green=QuantumRange*min; |
| 621 | *blue=QuantumRange*(min+c); |
| 622 | break; |
| 623 | } |
| 624 | case 5: |
| 625 | { |
| 626 | *red=QuantumRange*(min+c); |
| 627 | *green=QuantumRange*min; |
| 628 | *blue=QuantumRange*(min+x); |
| 629 | break; |
| 630 | } |
| 631 | default: |
| 632 | { |
| 633 | *red=0.0; |
| 634 | *green=0.0; |
| 635 | *blue=0.0; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 642 | % % |
| 643 | % % |
| 644 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 645 | % C o n v e r t H W B T o R G B % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | % % |
| 647 | % % |
| 648 | % % |
| 649 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 650 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 651 | % ConvertHWBToRGB() transforms a (hue, whiteness, blackness) to a (red, green, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 652 | % blue) triple. |
| 653 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 654 | % The format of the ConvertHWBToRGBImage method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 656 | % void ConvertHWBToRGB(const double hue,const double whiteness, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 657 | % const double blackness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 658 | % |
| 659 | % A description of each parameter follows: |
| 660 | % |
| 661 | % o hue, whiteness, blackness: A double value representing a |
| 662 | % component of the HWB color space. |
| 663 | % |
| 664 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 665 | % |
| 666 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 667 | MagickPrivate void ConvertHWBToRGB(const double hue,const double whiteness, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 668 | const double blackness,double *red,double *green,double *blue) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 669 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 670 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 671 | b, |
| 672 | f, |
| 673 | g, |
| 674 | n, |
| 675 | r, |
| 676 | v; |
| 677 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 678 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 679 | i; |
| 680 | |
| 681 | /* |
| 682 | Convert HWB to RGB colorspace. |
| 683 | */ |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 684 | assert(red != (double *) NULL); |
| 685 | assert(green != (double *) NULL); |
| 686 | assert(blue != (double *) NULL); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | v=1.0-blackness; |
| cristy | af10b11 | 2012-04-18 13:25:37 +0000 | [diff] [blame] | 688 | if (hue == -1.0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 689 | { |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 690 | *red=QuantumRange*v; |
| 691 | *green=QuantumRange*v; |
| 692 | *blue=QuantumRange*v; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | return; |
| 694 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 695 | i=(ssize_t) floor(6.0*hue); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 696 | f=6.0*hue-i; |
| 697 | if ((i & 0x01) != 0) |
| 698 | f=1.0-f; |
| 699 | n=whiteness+f*(v-whiteness); /* linear interpolation */ |
| 700 | switch (i) |
| 701 | { |
| 702 | default: |
| 703 | case 6: |
| 704 | case 0: r=v; g=n; b=whiteness; break; |
| 705 | case 1: r=n; g=v; b=whiteness; break; |
| 706 | case 2: r=whiteness; g=v; b=n; break; |
| 707 | case 3: r=whiteness; g=n; b=v; break; |
| 708 | case 4: r=n; g=whiteness; b=v; break; |
| 709 | case 5: r=v; g=whiteness; b=n; break; |
| 710 | } |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 711 | *red=QuantumRange*r; |
| 712 | *green=QuantumRange*g; |
| 713 | *blue=QuantumRange*b; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
| 717 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 718 | % % |
| 719 | % % |
| 720 | % % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 721 | % C o n v e r t L C H a b T o R G B % |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 722 | % % |
| 723 | % % |
| 724 | % % |
| 725 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 726 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 727 | % ConvertLCHabToRGB() transforms a (luma, chroma, hue) to a (red, green, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 728 | % blue) triple. |
| 729 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 730 | % The format of the ConvertLCHabToRGBImage method is: |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 731 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 732 | % void ConvertLCHabToRGB(const double luma,const double chroma, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 733 | % const double hue,double *red,double *green,double *blue) |
| 734 | % |
| 735 | % A description of each parameter follows: |
| 736 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 737 | % o luma, chroma, hue: A double value representing a component of the |
| 738 | % LCHab color space. |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 739 | % |
| 740 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 741 | % |
| 742 | */ |
| cristy | 35605e9 | 2013-05-06 11:33:57 +0000 | [diff] [blame] | 743 | |
| 744 | static inline void ConvertLCHabToXYZ(const double luma,const double chroma, |
| 745 | const double hue,double *X,double *Y,double *Z) |
| 746 | { |
| 747 | ConvertLabToXYZ(luma,chroma*cos(hue*MagickPI/180.0),chroma* |
| 748 | sin(hue*MagickPI/180.0),X,Y,Z); |
| 749 | } |
| 750 | |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 751 | MagickPrivate void ConvertLCHabToRGB(const double luma,const double chroma, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 752 | const double hue,double *red,double *green,double *blue) |
| 753 | { |
| 754 | double |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 755 | X, |
| 756 | Y, |
| 757 | Z; |
| 758 | |
| 759 | /* |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 760 | Convert LCHab to RGB colorspace. |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 761 | */ |
| 762 | assert(red != (double *) NULL); |
| 763 | assert(green != (double *) NULL); |
| 764 | assert(blue != (double *) NULL); |
| cristy | 6d897c7 | 2013-06-19 19:20:40 +0000 | [diff] [blame] | 765 | ConvertLCHabToXYZ(100.0*luma,255.0*(chroma-0.5),360.0*hue,&X,&Y,&Z); |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 766 | ConvertXYZToRGB(X,Y,Z,red,green,blue); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 771 | % % |
| 772 | % % |
| 773 | % % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 774 | % C o n v e r t L C H u v T o R G B % |
| 775 | % % |
| 776 | % % |
| 777 | % % |
| 778 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 779 | % |
| 780 | % ConvertLCHuvToRGB() transforms a (luma, chroma, hue) to a (red, green, |
| 781 | % blue) triple. |
| 782 | % |
| 783 | % The format of the ConvertLCHuvToRGBImage method is: |
| 784 | % |
| 785 | % void ConvertLCHuvToRGB(const double luma,const double chroma, |
| 786 | % const double hue,double *red,double *green,double *blue) |
| 787 | % |
| 788 | % A description of each parameter follows: |
| 789 | % |
| 790 | % o luma, chroma, hue: A double value representing a component of the |
| 791 | % LCHuv color space. |
| 792 | % |
| 793 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 794 | % |
| 795 | */ |
| cristy | a871691 | 2013-05-06 12:04:10 +0000 | [diff] [blame] | 796 | |
| 797 | static inline void ConvertLCHuvToXYZ(const double luma,const double chroma, |
| 798 | const double hue,double *X,double *Y,double *Z) |
| 799 | { |
| 800 | ConvertLuvToXYZ(luma,chroma*cos(hue*MagickPI/180.0),chroma* |
| 801 | sin(hue*MagickPI/180.0),X,Y,Z); |
| 802 | } |
| 803 | |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 804 | MagickPrivate void ConvertLCHuvToRGB(const double luma,const double chroma, |
| 805 | const double hue,double *red,double *green,double *blue) |
| 806 | { |
| 807 | double |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 808 | X, |
| 809 | Y, |
| 810 | Z; |
| 811 | |
| 812 | /* |
| 813 | Convert LCHuv to RGB colorspace. |
| 814 | */ |
| 815 | assert(red != (double *) NULL); |
| 816 | assert(green != (double *) NULL); |
| 817 | assert(blue != (double *) NULL); |
| cristy | 5119921 | 2013-06-19 23:40:01 +0000 | [diff] [blame] | 818 | ConvertLCHuvToXYZ(100.0*luma,255.0*(chroma-0.5),360.0*hue,&X,&Y,&Z); |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 819 | ConvertXYZToRGB(X,Y,Z,red,green,blue); |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 824 | % % |
| 825 | % % |
| 826 | % % |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 827 | % C o n v e r t R G B T o H C L % |
| 828 | % % |
| 829 | % % |
| 830 | % % |
| 831 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 832 | % |
| 833 | % ConvertRGBToHCL() transforms a (red, green, blue) to a (hue, chroma, |
| 834 | % luma) triple. |
| 835 | % |
| 836 | % The format of the ConvertRGBToHCL method is: |
| 837 | % |
| 838 | % void ConvertRGBToHCL(const double red,const double green, |
| 839 | % const double blue,double *hue,double *chroma,double *luma) |
| 840 | % |
| 841 | % A description of each parameter follows: |
| 842 | % |
| 843 | % o red, green, blue: A Quantum value representing the red, green, and |
| 844 | % blue component of a pixel. |
| 845 | % |
| 846 | % o hue, chroma, luma: A pointer to a double value representing a |
| 847 | % component of the HCL color space. |
| 848 | % |
| 849 | */ |
| cristy | 5756d82 | 2013-04-09 01:14:34 +0000 | [diff] [blame] | 850 | |
| 851 | static inline double MagickMax(const double x,const double y) |
| 852 | { |
| 853 | if (x > y) |
| 854 | return(x); |
| 855 | return(y); |
| 856 | } |
| 857 | |
| 858 | static inline double MagickMin(const double x,const double y) |
| 859 | { |
| 860 | if (x < y) |
| 861 | return(x); |
| 862 | return(y); |
| 863 | } |
| 864 | |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 865 | MagickPrivate void ConvertRGBToHCL(const double red,const double green, |
| 866 | const double blue,double *hue,double *chroma,double *luma) |
| 867 | { |
| 868 | double |
| cristy | 5756d82 | 2013-04-09 01:14:34 +0000 | [diff] [blame] | 869 | b, |
| 870 | c, |
| 871 | g, |
| 872 | h, |
| 873 | max, |
| 874 | r; |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | Convert RGB to HCL colorspace. |
| 878 | */ |
| cristy | 9427c42 | 2013-04-08 12:03:11 +0000 | [diff] [blame] | 879 | assert(hue != (double *) NULL); |
| cristy | 5756d82 | 2013-04-09 01:14:34 +0000 | [diff] [blame] | 880 | assert(chroma != (double *) NULL); |
| 881 | assert(luma != (double *) NULL); |
| 882 | r=red; |
| 883 | g=green; |
| 884 | b=blue; |
| 885 | max=MagickMax(r,MagickMax(g,b)); |
| 886 | c=max-(double) MagickMin(r,MagickMin(g,b)); |
| 887 | h=0.0; |
| 888 | if (c == 0.0) |
| 889 | h=0.0; |
| 890 | else |
| 891 | if (red == max) |
| 892 | h=fmod((g-b)/c+6.0,6.0); |
| 893 | else |
| 894 | if (green == max) |
| 895 | h=((b-r)/c)+2.0; |
| 896 | else |
| 897 | if (blue == max) |
| 898 | h=((r-g)/c)+4.0; |
| 899 | *hue=(h/6.0); |
| 900 | *chroma=QuantumScale*c; |
| cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 901 | *luma=QuantumScale*(0.298839*r+0.586811*g+0.114350*b); |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 906 | % % |
| 907 | % % |
| 908 | % % |
| 909 | % C o n v e r t R G B T o H C L p % |
| 910 | % % |
| 911 | % % |
| 912 | % % |
| 913 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 914 | % |
| 915 | % ConvertRGBToHCLp() transforms a (red, green, blue) to a (hue, chroma, |
| 916 | % luma) triple. |
| 917 | % |
| 918 | % The format of the ConvertRGBToHCLp method is: |
| 919 | % |
| 920 | % void ConvertRGBToHCLp(const double red,const double green, |
| 921 | % const double blue,double *hue,double *chroma,double *luma) |
| 922 | % |
| 923 | % A description of each parameter follows: |
| 924 | % |
| 925 | % o red, green, blue: A Quantum value representing the red, green, and |
| 926 | % blue component of a pixel. |
| 927 | % |
| 928 | % o hue, chroma, luma: A pointer to a double value representing a |
| 929 | % component of the HCL color space. |
| 930 | % |
| 931 | */ |
| 932 | MagickPrivate void ConvertRGBToHCLp(const double red,const double green, |
| 933 | const double blue,double *hue,double *chroma,double *luma) |
| 934 | { |
| 935 | double |
| 936 | b, |
| 937 | c, |
| 938 | g, |
| 939 | h, |
| 940 | max, |
| 941 | r; |
| 942 | |
| 943 | /* |
| 944 | Convert RGB to HCL colorspace. |
| 945 | */ |
| 946 | assert(hue != (double *) NULL); |
| 947 | assert(chroma != (double *) NULL); |
| 948 | assert(luma != (double *) NULL); |
| 949 | r=red; |
| 950 | g=green; |
| 951 | b=blue; |
| 952 | max=MagickMax(r,MagickMax(g,b)); |
| 953 | c=max-(double) MagickMin(r,MagickMin(g,b)); |
| 954 | h=0.0; |
| 955 | if (c == 0.0) |
| 956 | h=0.0; |
| 957 | else |
| 958 | if (red == max) |
| 959 | h=fmod((g-b)/c+6.0,6.0); |
| 960 | else |
| 961 | if (green == max) |
| 962 | h=((b-r)/c)+2.0; |
| 963 | else |
| 964 | if (blue == max) |
| 965 | h=((r-g)/c)+4.0; |
| 966 | *hue=(h/6.0); |
| 967 | *chroma=QuantumScale*c; |
| 968 | *luma=QuantumScale*(0.298839*r+0.586811*g+0.114350*b); |
| cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* |
| 972 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 973 | % % |
| 974 | % % |
| 975 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 976 | % C o n v e r t R G B T o H S B % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 977 | % % |
| 978 | % % |
| 979 | % % |
| 980 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 981 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 982 | % ConvertRGBToHSB() transforms a (red, green, blue) to a (hue, saturation, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 983 | % brightness) triple. |
| 984 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 985 | % The format of the ConvertRGBToHSB method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 986 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 987 | % void ConvertRGBToHSB(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 988 | % const double blue,double *hue,double *saturation,double *brightness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 989 | % |
| 990 | % A description of each parameter follows: |
| 991 | % |
| 992 | % o red, green, blue: A Quantum value representing the red, green, and |
| 993 | % blue component of a pixel.. |
| 994 | % |
| 995 | % o hue, saturation, brightness: A pointer to a double value representing a |
| 996 | % component of the HSB color space. |
| 997 | % |
| 998 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 999 | MagickPrivate void ConvertRGBToHSB(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 1000 | const double blue,double *hue,double *saturation,double *brightness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1001 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1002 | double |
| 1003 | b, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | delta, |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1005 | g, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1006 | max, |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1007 | min, |
| 1008 | r; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | Convert RGB to HSB colorspace. |
| 1012 | */ |
| 1013 | assert(hue != (double *) NULL); |
| 1014 | assert(saturation != (double *) NULL); |
| 1015 | assert(brightness != (double *) NULL); |
| 1016 | *hue=0.0; |
| 1017 | *saturation=0.0; |
| 1018 | *brightness=0.0; |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1019 | r=red; |
| 1020 | g=green; |
| 1021 | b=blue; |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1022 | min=r < g ? r : g; |
| 1023 | if (b < min) |
| 1024 | min=b; |
| 1025 | max=r > g ? r : g; |
| 1026 | if (b > max) |
| 1027 | max=b; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1028 | if (max == 0.0) |
| 1029 | return; |
| 1030 | delta=max-min; |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1031 | *saturation=delta/max; |
| 1032 | *brightness=QuantumScale*max; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1033 | if (delta == 0.0) |
| 1034 | return; |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1035 | if (r == max) |
| 1036 | *hue=(g-b)/delta; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1037 | else |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1038 | if (g == max) |
| 1039 | *hue=2.0+(b-r)/delta; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | else |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1041 | *hue=4.0+(r-g)/delta; |
| cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 1042 | *hue/=6.0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1043 | if (*hue < 0.0) |
| 1044 | *hue+=1.0; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1049 | % % |
| 1050 | % % |
| 1051 | % % |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 1052 | % C o n v e r t R G B T o H S I % |
| 1053 | % % |
| 1054 | % % |
| 1055 | % % |
| 1056 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1057 | % |
| 1058 | % ConvertRGBToHSI() transforms a (red, green, blue) to a (hue, saturation, |
| 1059 | % intensity) triple. |
| 1060 | % |
| 1061 | % The format of the ConvertRGBToHSI method is: |
| 1062 | % |
| 1063 | % void ConvertRGBToHSI(const double red,const double green, |
| 1064 | % const double blue,double *hue,double *saturation,double *intensity) |
| 1065 | % |
| 1066 | % A description of each parameter follows: |
| 1067 | % |
| 1068 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1069 | % blue component of a pixel.. |
| 1070 | % |
| 1071 | % o hue, saturation, intensity: A pointer to a double value representing a |
| 1072 | % component of the HSI color space. |
| 1073 | % |
| 1074 | */ |
| 1075 | MagickPrivate void ConvertRGBToHSI(const double red,const double green, |
| 1076 | const double blue,double *hue,double *saturation,double *intensity) |
| 1077 | { |
| 1078 | double |
| 1079 | alpha, |
| 1080 | beta; |
| 1081 | |
| 1082 | /* |
| 1083 | Convert RGB to HSI colorspace. |
| 1084 | */ |
| 1085 | assert(hue != (double *) NULL); |
| 1086 | assert(saturation != (double *) NULL); |
| 1087 | assert(intensity != (double *) NULL); |
| 1088 | *intensity=(QuantumScale*red+QuantumScale*green+QuantumScale*blue)/3.0; |
| 1089 | if (*intensity <= 0.0) |
| 1090 | { |
| 1091 | *hue=0.0; |
| 1092 | *saturation=0.0; |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1093 | return; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 1094 | } |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1095 | *saturation=1.0-MagickMin(QuantumScale*red,MagickMin(QuantumScale*green, |
| 1096 | QuantumScale*blue))/(*intensity); |
| 1097 | alpha=0.5*(2.0*QuantumScale*red-QuantumScale*green-QuantumScale*blue); |
| cristy | be708af | 2013-05-02 23:42:41 +0000 | [diff] [blame] | 1098 | beta=0.8660254037844385*(QuantumScale*green-QuantumScale*blue); |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1099 | *hue=atan2(beta,alpha)*(180.0/MagickPI)/360.0; |
| 1100 | if (*hue < 0.0) |
| 1101 | *hue+=1.0; |
| cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1106 | % % |
| 1107 | % % |
| 1108 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1109 | % C o n v e r t R G B T o H S L % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | % % |
| 1111 | % % |
| 1112 | % % |
| 1113 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1114 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1115 | % ConvertRGBToHSL() transforms a (red, green, blue) to a (hue, saturation, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1116 | % lightness) triple. |
| 1117 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1118 | % The format of the ConvertRGBToHSL method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1119 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1120 | % void ConvertRGBToHSL(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 1121 | % const double blue,double *hue,double *saturation,double *lightness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1122 | % |
| 1123 | % A description of each parameter follows: |
| 1124 | % |
| 1125 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1126 | % blue component of a pixel.. |
| 1127 | % |
| 1128 | % o hue, saturation, lightness: A pointer to a double value representing a |
| 1129 | % component of the HSL color space. |
| 1130 | % |
| 1131 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1132 | MagickExport void ConvertRGBToHSL(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 1133 | const double blue,double *hue,double *saturation,double *lightness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1134 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1135 | double |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1136 | c, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1137 | max, |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1138 | min; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* |
| 1141 | Convert RGB to HSL colorspace. |
| 1142 | */ |
| 1143 | assert(hue != (double *) NULL); |
| 1144 | assert(saturation != (double *) NULL); |
| 1145 | assert(lightness != (double *) NULL); |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1146 | max=MagickMax(QuantumScale*red,MagickMax(QuantumScale*green, |
| 1147 | QuantumScale*blue)); |
| 1148 | min=MagickMin(QuantumScale*red,MagickMin(QuantumScale*green, |
| 1149 | QuantumScale*blue)); |
| 1150 | c=max-min; |
| 1151 | *lightness=(max+min)/2.0; |
| 1152 | if (c <= 0.0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1153 | { |
| 1154 | *hue=0.0; |
| 1155 | *saturation=0.0; |
| 1156 | return; |
| 1157 | } |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1158 | if (max == (QuantumScale*red)) |
| 1159 | { |
| 1160 | *hue=(QuantumScale*green-QuantumScale*blue)/c; |
| 1161 | if ((QuantumScale*green) < (QuantumScale*blue)) |
| 1162 | *hue+=6.0; |
| 1163 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | else |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1165 | if (max == (QuantumScale*green)) |
| 1166 | *hue=2.0+(QuantumScale*blue-QuantumScale*red)/c; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1167 | else |
| cristy | a5a45a7 | 2013-05-02 16:06:57 +0000 | [diff] [blame] | 1168 | *hue=4.0+(QuantumScale*red-QuantumScale*green)/c; |
| 1169 | *hue*=60.0/360.0; |
| 1170 | if (*lightness <= 0.5) |
| 1171 | *saturation=c/(2.0*(*lightness)); |
| 1172 | else |
| 1173 | *saturation=c/(2.0-2.0*(*lightness)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1178 | % % |
| 1179 | % % |
| 1180 | % % |
| cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 1181 | % C o n v e r t R G B T o H S V % |
| 1182 | % % |
| 1183 | % % |
| 1184 | % % |
| 1185 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1186 | % |
| 1187 | % ConvertRGBToHSV() transforms a (red, green, blue) to a (hue, saturation, |
| 1188 | % value) triple. |
| 1189 | % |
| 1190 | % The format of the ConvertRGBToHSV method is: |
| 1191 | % |
| 1192 | % void ConvertRGBToHSV(const double red,const double green, |
| 1193 | % const double blue,double *hue,double *saturation,double *value) |
| 1194 | % |
| 1195 | % A description of each parameter follows: |
| 1196 | % |
| 1197 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1198 | % blue component of a pixel.. |
| 1199 | % |
| 1200 | % o hue, saturation, value: A pointer to a double value representing a |
| 1201 | % component of the HSV color space. |
| 1202 | % |
| 1203 | */ |
| 1204 | MagickPrivate void ConvertRGBToHSV(const double red,const double green, |
| 1205 | const double blue,double *hue,double *saturation,double *value) |
| 1206 | { |
| 1207 | double |
| 1208 | c, |
| 1209 | max, |
| 1210 | min; |
| 1211 | |
| 1212 | /* |
| 1213 | Convert RGB to HSV colorspace. |
| 1214 | */ |
| 1215 | assert(hue != (double *) NULL); |
| 1216 | assert(saturation != (double *) NULL); |
| 1217 | assert(value != (double *) NULL); |
| 1218 | max=MagickMax(QuantumScale*red,MagickMax(QuantumScale*green, |
| 1219 | QuantumScale*blue)); |
| 1220 | min=MagickMin(QuantumScale*red,MagickMin(QuantumScale*green, |
| 1221 | QuantumScale*blue)); |
| 1222 | c=max-min; |
| 1223 | *value=max; |
| 1224 | if (c <= 0.0) |
| 1225 | { |
| 1226 | *hue=0.0; |
| 1227 | *saturation=0.0; |
| 1228 | return; |
| 1229 | } |
| 1230 | if (max == (QuantumScale*red)) |
| 1231 | { |
| 1232 | *hue=(QuantumScale*green-QuantumScale*blue)/c; |
| 1233 | if ((QuantumScale*green) < (QuantumScale*blue)) |
| 1234 | *hue+=6.0; |
| 1235 | } |
| 1236 | else |
| 1237 | if (max == (QuantumScale*green)) |
| 1238 | *hue=2.0+(QuantumScale*blue-QuantumScale*red)/c; |
| 1239 | else |
| 1240 | *hue=4.0+(QuantumScale*red-QuantumScale*green)/c; |
| 1241 | *hue*=60.0/360.0; |
| 1242 | *saturation=c/max; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1247 | % % |
| 1248 | % % |
| 1249 | % % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1250 | % C o n v e r t R G B T o H W B % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1251 | % % |
| 1252 | % % |
| 1253 | % % |
| 1254 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1255 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1256 | % ConvertRGBToHWB() transforms a (red, green, blue) to a (hue, whiteness, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | % blackness) triple. |
| 1258 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1259 | % The format of the ConvertRGBToHWB method is: |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1260 | % |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1261 | % void ConvertRGBToHWB(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 1262 | % const double blue,double *hue,double *whiteness,double *blackness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1263 | % |
| 1264 | % A description of each parameter follows: |
| 1265 | % |
| 1266 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1267 | % blue component of a pixel. |
| 1268 | % |
| 1269 | % o hue, whiteness, blackness: A pointer to a double value representing a |
| 1270 | % component of the HWB color space. |
| 1271 | % |
| 1272 | */ |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1273 | MagickPrivate void ConvertRGBToHWB(const double red,const double green, |
| cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 1274 | const double blue,double *hue,double *whiteness,double *blackness) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | { |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1276 | double |
| 1277 | b, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1278 | f, |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1279 | g, |
| 1280 | p, |
| 1281 | r, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1282 | v, |
| 1283 | w; |
| 1284 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1285 | /* |
| 1286 | Convert RGB to HWB colorspace. |
| 1287 | */ |
| 1288 | assert(hue != (double *) NULL); |
| 1289 | assert(whiteness != (double *) NULL); |
| 1290 | assert(blackness != (double *) NULL); |
| cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 1291 | r=red; |
| 1292 | g=green; |
| 1293 | b=blue; |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1294 | w=MagickMin(r,MagickMin(g,b)); |
| 1295 | v=MagickMax(r,MagickMax(g,b)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | *blackness=1.0-QuantumScale*v; |
| 1297 | *whiteness=QuantumScale*w; |
| 1298 | if (v == w) |
| 1299 | { |
| cristy | af10b11 | 2012-04-18 13:25:37 +0000 | [diff] [blame] | 1300 | *hue=(-1.0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1301 | return; |
| 1302 | } |
| cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 1303 | f=(r == w) ? g-b : ((g == w) ? b-r : r-g); |
| 1304 | p=(r == w) ? 3.0 : ((g == w) ? 5.0 : 1.0); |
| 1305 | *hue=(p-f/(v-1.0*w))/6.0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1310 | % % |
| 1311 | % % |
| 1312 | % % |
| cristy | a83d4c0 | 2013-04-19 19:39:30 +0000 | [diff] [blame] | 1313 | % C o n v e r t R G B T o L C H a b % |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1314 | % % |
| 1315 | % % |
| 1316 | % % |
| 1317 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1318 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1319 | % ConvertRGBToLCHab() transforms a (red, green, blue) to a (luma, chroma, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1320 | % hue) triple. |
| 1321 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1322 | % The format of the ConvertRGBToLCHab method is: |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1323 | % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1324 | % void ConvertRGBToLCHab(const double red,const double green, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1325 | % const double blue,double *luma,double *chroma,double *hue) |
| 1326 | % |
| 1327 | % A description of each parameter follows: |
| 1328 | % |
| 1329 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1330 | % blue component of a pixel. |
| 1331 | % |
| 1332 | % o luma, chroma, hue: A pointer to a double value representing a |
| 1333 | % component of the LCH color space. |
| 1334 | % |
| 1335 | */ |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1336 | |
| 1337 | static inline void ConvertXYZToLCHab(const double X,const double Y, |
| 1338 | const double Z,double *luma,double *chroma,double *hue) |
| 1339 | { |
| 1340 | double |
| 1341 | a, |
| 1342 | b; |
| 1343 | |
| 1344 | ConvertXYZToLab(X,Y,Z,luma,&a,&b); |
| cristy | 846740b | 2013-06-19 18:19:55 +0000 | [diff] [blame] | 1345 | *chroma=hypot(255.0*(a-0.5),255.0*(b-0.5))/255.0+0.5; |
| cristy | 6d897c7 | 2013-06-19 19:20:40 +0000 | [diff] [blame] | 1346 | *hue=180.0*atan2(255.0*(b-0.5),255.0*(a-0.5))/MagickPI/360.0; |
| cristy | 89106f8 | 2013-05-06 17:04:29 +0000 | [diff] [blame] | 1347 | if (*hue < 0.0) |
| 1348 | *hue+=1.0; |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1351 | MagickPrivate void ConvertRGBToLCHab(const double red,const double green, |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1352 | const double blue,double *luma,double *chroma,double *hue) |
| 1353 | { |
| 1354 | double |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1355 | X, |
| 1356 | Y, |
| 1357 | Z; |
| 1358 | |
| 1359 | /* |
| cristy | 9427c42 | 2013-04-08 12:03:11 +0000 | [diff] [blame] | 1360 | Convert RGB to LCHab colorspace. |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1361 | */ |
| 1362 | assert(luma != (double *) NULL); |
| 1363 | assert(chroma != (double *) NULL); |
| 1364 | assert(hue != (double *) NULL); |
| 1365 | ConvertRGBToXYZ(red,green,blue,&X,&Y,&Z); |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1366 | ConvertXYZToLCHab(X,Y,Z,luma,chroma,hue); |
| cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1371 | % % |
| 1372 | % % |
| 1373 | % % |
| cristy | a83d4c0 | 2013-04-19 19:39:30 +0000 | [diff] [blame] | 1374 | % C o n v e r t R G B T o L C H u v % |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1375 | % % |
| 1376 | % % |
| 1377 | % % |
| 1378 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1379 | % |
| 1380 | % ConvertRGBToLCHuv() transforms a (red, green, blue) to a (luma, chroma, |
| 1381 | % hue) triple. |
| 1382 | % |
| 1383 | % The format of the ConvertRGBToLCHuv method is: |
| 1384 | % |
| 1385 | % void ConvertRGBToLCHuv(const double red,const double green, |
| 1386 | % const double blue,double *luma,double *chroma,double *hue) |
| 1387 | % |
| 1388 | % A description of each parameter follows: |
| 1389 | % |
| 1390 | % o red, green, blue: A Quantum value representing the red, green, and |
| 1391 | % blue component of a pixel. |
| 1392 | % |
| 1393 | % o luma, chroma, hue: A pointer to a double value representing a |
| 1394 | % component of the LCHuv color space. |
| 1395 | % |
| 1396 | */ |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1397 | |
| 1398 | static inline void ConvertXYZToLCHuv(const double X,const double Y, |
| 1399 | const double Z,double *luma,double *chroma,double *hue) |
| 1400 | { |
| 1401 | double |
| cristy | 9db2dba | 2013-05-06 14:04:13 +0000 | [diff] [blame] | 1402 | u, |
| 1403 | v; |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1404 | |
| cristy | 9db2dba | 2013-05-06 14:04:13 +0000 | [diff] [blame] | 1405 | ConvertXYZToLuv(X,Y,Z,luma,&u,&v); |
| cristy | 846740b | 2013-06-19 18:19:55 +0000 | [diff] [blame] | 1406 | *chroma=hypot(354.0*u-134.0,262.0*v-140.0)/255.0+0.5; |
| cristy | 6d897c7 | 2013-06-19 19:20:40 +0000 | [diff] [blame] | 1407 | *hue=180.0*atan2(262.0*v-140.0,354.0*u-134.0)/MagickPI/360.0; |
| cristy | 14a4e0f | 2013-05-06 19:53:58 +0000 | [diff] [blame] | 1408 | if (*hue < 0.0) |
| 1409 | *hue+=1.0; |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1412 | MagickPrivate void ConvertRGBToLCHuv(const double red,const double green, |
| 1413 | const double blue,double *luma,double *chroma,double *hue) |
| 1414 | { |
| 1415 | double |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1416 | X, |
| 1417 | Y, |
| 1418 | Z; |
| 1419 | |
| 1420 | /* |
| cristy | 9427c42 | 2013-04-08 12:03:11 +0000 | [diff] [blame] | 1421 | Convert RGB to LCHuv colorspace. |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1422 | */ |
| 1423 | assert(luma != (double *) NULL); |
| 1424 | assert(chroma != (double *) NULL); |
| 1425 | assert(hue != (double *) NULL); |
| 1426 | ConvertRGBToXYZ(red,green,blue,&X,&Y,&Z); |
| cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 1427 | ConvertXYZToLCHuv(X,Y,Z,luma,chroma,hue); |
| cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1432 | % % |
| 1433 | % % |
| 1434 | % % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1435 | % E x p a n d A f f i n e % |
| 1436 | % % |
| 1437 | % % |
| 1438 | % % |
| 1439 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1440 | % |
| 1441 | % ExpandAffine() computes the affine's expansion factor, i.e. the square root |
| 1442 | % of the factor by which the affine transform affects area. In an affine |
| 1443 | % transform composed of scaling, rotation, shearing, and translation, returns |
| 1444 | % the amount of scaling. |
| 1445 | % |
| 1446 | % The format of the ExpandAffine method is: |
| 1447 | % |
| 1448 | % double ExpandAffine(const AffineMatrix *affine) |
| 1449 | % |
| 1450 | % A description of each parameter follows: |
| 1451 | % |
| cristy | 2d7ffbb | 2013-04-19 19:43:01 +0000 | [diff] [blame] | 1452 | % o expansion: ExpandAffine returns the affine's expansion factor. |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1453 | % |
| 1454 | % o affine: A pointer the affine transform of type AffineMatrix. |
| 1455 | % |
| 1456 | */ |
| 1457 | MagickExport double ExpandAffine(const AffineMatrix *affine) |
| 1458 | { |
| 1459 | assert(affine != (const AffineMatrix *) NULL); |
| 1460 | return(sqrt(fabs(affine->sx*affine->sy-affine->rx*affine->ry))); |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1465 | % % |
| 1466 | % % |
| 1467 | % % |
| 1468 | % G e n e r a t e D i f f e r e n t i a l N o i s e % |
| 1469 | % % |
| 1470 | % % |
| 1471 | % % |
| 1472 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1473 | % |
| cristy | 82b1583 | 2009-10-06 19:17:37 +0000 | [diff] [blame] | 1474 | % GenerateDifferentialNoise() generates differentual noise. |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1475 | % |
| 1476 | % The format of the GenerateDifferentialNoise method is: |
| 1477 | % |
| 1478 | % double GenerateDifferentialNoise(RandomInfo *random_info, |
| cristy | 9ed1f81 | 2011-10-08 02:00:08 +0000 | [diff] [blame] | 1479 | % const Quantum pixel,const NoiseType noise_type,const double attenuate) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1480 | % |
| 1481 | % A description of each parameter follows: |
| 1482 | % |
| 1483 | % o random_info: the random info. |
| 1484 | % |
| 1485 | % o pixel: noise is relative to this pixel value. |
| 1486 | % |
| 1487 | % o noise_type: the type of noise. |
| 1488 | % |
| 1489 | % o attenuate: attenuate the noise. |
| 1490 | % |
| 1491 | */ |
| cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 1492 | MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info, |
| cristy | 9ed1f81 | 2011-10-08 02:00:08 +0000 | [diff] [blame] | 1493 | const Quantum pixel,const NoiseType noise_type,const double attenuate) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1494 | { |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1495 | #define SigmaUniform (attenuate*0.015625) |
| 1496 | #define SigmaGaussian (attenuate*0.015625) |
| 1497 | #define SigmaImpulse (attenuate*0.1) |
| 1498 | #define SigmaLaplacian (attenuate*0.0390625) |
| 1499 | #define SigmaMultiplicativeGaussian (attenuate*0.5) |
| cristy | 4ce9df6 | 2011-10-12 12:06:02 +0000 | [diff] [blame] | 1500 | #define SigmaPoisson (attenuate*12.5) |
| cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 1501 | #define SigmaRandom (attenuate) |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1502 | #define TauGaussian (attenuate*0.078125) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1503 | |
| cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 1504 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1505 | alpha, |
| 1506 | beta, |
| 1507 | noise, |
| 1508 | sigma; |
| 1509 | |
| 1510 | alpha=GetPseudoRandomValue(random_info); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1511 | switch (noise_type) |
| 1512 | { |
| 1513 | case UniformNoise: |
| 1514 | default: |
| 1515 | { |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1516 | noise=(double) (pixel+QuantumRange*SigmaUniform*(alpha-0.5)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | } |
| 1519 | case GaussianNoise: |
| 1520 | { |
| cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 1521 | double |
| cristy | 62faa60 | 2010-02-20 03:36:17 +0000 | [diff] [blame] | 1522 | gamma, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1523 | tau; |
| 1524 | |
| cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 1525 | if (alpha == 0.0) |
| 1526 | alpha=1.0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1527 | beta=GetPseudoRandomValue(random_info); |
| cristy | 62faa60 | 2010-02-20 03:36:17 +0000 | [diff] [blame] | 1528 | gamma=sqrt(-2.0*log(alpha)); |
| cristy | 55a91cd | 2010-12-01 00:57:40 +0000 | [diff] [blame] | 1529 | sigma=gamma*cos((double) (2.0*MagickPI*beta)); |
| 1530 | tau=gamma*sin((double) (2.0*MagickPI*beta)); |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1531 | noise=(double) (pixel+sqrt((double) pixel)*SigmaGaussian*sigma+ |
| 1532 | QuantumRange*TauGaussian*tau); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | } |
| 1535 | case ImpulseNoise: |
| 1536 | { |
| 1537 | if (alpha < (SigmaImpulse/2.0)) |
| 1538 | noise=0.0; |
| cristy | 37c2407 | 2011-10-08 01:26:00 +0000 | [diff] [blame] | 1539 | else |
| 1540 | if (alpha >= (1.0-(SigmaImpulse/2.0))) |
| 1541 | noise=(double) QuantumRange; |
| 1542 | else |
| 1543 | noise=(double) pixel; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | } |
| 1546 | case LaplacianNoise: |
| 1547 | { |
| 1548 | if (alpha <= 0.5) |
| 1549 | { |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1550 | if (alpha <= MagickEpsilon) |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1551 | noise=(double) (pixel-QuantumRange); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1552 | else |
| cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 1553 | noise=(double) (pixel+QuantumRange*SigmaLaplacian*log(2.0*alpha)+ |
| 1554 | 0.5); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | beta=1.0-alpha; |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1558 | if (beta <= (0.5*MagickEpsilon)) |
| cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 1559 | noise=(double) (pixel+QuantumRange); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1560 | else |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1561 | noise=(double) (pixel-QuantumRange*SigmaLaplacian*log(2.0*beta)+0.5); |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | } |
| 1564 | case MultiplicativeGaussianNoise: |
| 1565 | { |
| 1566 | sigma=1.0; |
| 1567 | if (alpha > MagickEpsilon) |
| 1568 | sigma=sqrt(-2.0*log(alpha)); |
| 1569 | beta=GetPseudoRandomValue(random_info); |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1570 | noise=(double) (pixel+pixel*SigmaMultiplicativeGaussian*sigma* |
| 1571 | cos((double) (2.0*MagickPI*beta))/2.0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1572 | break; |
| 1573 | } |
| 1574 | case PoissonNoise: |
| 1575 | { |
| cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 1576 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1577 | poisson; |
| 1578 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1579 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1580 | i; |
| 1581 | |
| cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 1582 | poisson=exp(-SigmaPoisson*QuantumScale*pixel); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1583 | for (i=0; alpha > poisson; i++) |
| 1584 | { |
| 1585 | beta=GetPseudoRandomValue(random_info); |
| 1586 | alpha*=beta; |
| 1587 | } |
| cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 1588 | noise=(double) (QuantumRange*i/SigmaPoisson); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | break; |
| 1590 | } |
| 1591 | case RandomNoise: |
| 1592 | { |
| cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 1593 | noise=(double) (QuantumRange*SigmaRandom*alpha); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | } |
| 1596 | } |
| 1597 | return(noise); |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1602 | % % |
| 1603 | % % |
| 1604 | % % |
| 1605 | % G e t O p t i m a l K e r n e l W i d t h % |
| 1606 | % % |
| 1607 | % % |
| 1608 | % % |
| 1609 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1610 | % |
| 1611 | % GetOptimalKernelWidth() computes the optimal kernel radius for a convolution |
| 1612 | % filter. Start with the minimum value of 3 pixels and walk out until we drop |
| 1613 | % below the threshold of one pixel numerical accuracy. |
| 1614 | % |
| 1615 | % The format of the GetOptimalKernelWidth method is: |
| 1616 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1617 | % size_t GetOptimalKernelWidth(const double radius, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1618 | % const double sigma) |
| 1619 | % |
| 1620 | % A description of each parameter follows: |
| 1621 | % |
| cristy | 2d7ffbb | 2013-04-19 19:43:01 +0000 | [diff] [blame] | 1622 | % o width: GetOptimalKernelWidth returns the optimal width of a |
| 1623 | % convolution kernel. |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1624 | % |
| 1625 | % o radius: the radius of the Gaussian, in pixels, not counting the center |
| 1626 | % pixel. |
| 1627 | % |
| 1628 | % o sigma: the standard deviation of the Gaussian, in pixels. |
| 1629 | % |
| 1630 | */ |
| cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 1631 | MagickPrivate size_t GetOptimalKernelWidth1D(const double radius, |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1632 | const double sigma) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1633 | { |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1634 | double |
| 1635 | alpha, |
| 1636 | beta, |
| 1637 | gamma, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | normalize, |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1639 | value; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1640 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1641 | register ssize_t |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1642 | i; |
| 1643 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1644 | size_t |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1645 | width; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1646 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1647 | ssize_t |
| 1648 | j; |
| 1649 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1650 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1651 | if (radius > MagickEpsilon) |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1652 | return((size_t) (2.0*ceil(radius)+1.0)); |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1653 | gamma=fabs(sigma); |
| 1654 | if (gamma <= MagickEpsilon) |
| anthony | c106172 | 2010-05-14 06:23:49 +0000 | [diff] [blame] | 1655 | return(3UL); |
| cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1656 | alpha=PerceptibleReciprocal(2.0*gamma*gamma); |
| 1657 | beta=(double) PerceptibleReciprocal((double) MagickSQ2PI*gamma); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1658 | for (width=5; ; ) |
| 1659 | { |
| 1660 | normalize=0.0; |
| cristy | 35faaa7 | 2013-03-08 12:33:42 +0000 | [diff] [blame] | 1661 | j=(ssize_t) (width-1)/2; |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1662 | for (i=(-j); i <= j; i++) |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1663 | normalize+=exp(-((double) (i*i))*alpha)*beta; |
| 1664 | value=exp(-((double) (j*j))*alpha)*beta/normalize; |
| cristy | 20908da | 2009-12-02 14:34:11 +0000 | [diff] [blame] | 1665 | if ((value < QuantumScale) || (value < MagickEpsilon)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | width+=2; |
| 1668 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1669 | return((size_t) (width-2)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 1672 | MagickPrivate size_t GetOptimalKernelWidth2D(const double radius, |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1673 | const double sigma) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1674 | { |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1675 | double |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1676 | alpha, |
| 1677 | beta, |
| 1678 | gamma, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1679 | normalize, |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1680 | value; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1681 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1682 | size_t |
| 1683 | width; |
| 1684 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1685 | ssize_t |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1686 | j, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1687 | u, |
| 1688 | v; |
| 1689 | |
| 1690 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1691 | if (radius > MagickEpsilon) |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1692 | return((size_t) (2.0*ceil(radius)+1.0)); |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1693 | gamma=fabs(sigma); |
| 1694 | if (gamma <= MagickEpsilon) |
| anthony | c106172 | 2010-05-14 06:23:49 +0000 | [diff] [blame] | 1695 | return(3UL); |
| cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1696 | alpha=PerceptibleReciprocal(2.0*gamma*gamma); |
| 1697 | beta=(double) PerceptibleReciprocal((double) Magick2PI*gamma*gamma); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1698 | for (width=5; ; ) |
| 1699 | { |
| 1700 | normalize=0.0; |
| cristy | 35faaa7 | 2013-03-08 12:33:42 +0000 | [diff] [blame] | 1701 | j=(ssize_t) (width-1)/2; |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1702 | for (v=(-j); v <= j; v++) |
| cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 1703 | for (u=(-j); u <= j; u++) |
| cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 1704 | normalize+=exp(-((double) (u*u+v*v))*alpha)*beta; |
| 1705 | value=exp(-((double) (j*j))*alpha)*beta/normalize; |
| cristy | 20908da | 2009-12-02 14:34:11 +0000 | [diff] [blame] | 1706 | if ((value < QuantumScale) || (value < MagickEpsilon)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1707 | break; |
| 1708 | width+=2; |
| 1709 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1710 | return((size_t) (width-2)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 1713 | MagickPrivate size_t GetOptimalKernelWidth(const double radius, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1714 | const double sigma) |
| 1715 | { |
| 1716 | return(GetOptimalKernelWidth1D(radius,sigma)); |
| 1717 | } |