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 % |
| 16 | % John Cristy % |
| 17 | % August 1996 % |
| 18 | % % |
| 19 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 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 | % |
| 82 | % o hue, chroma, luma: A double value representing a |
| 83 | % component of the HCL color space. |
| 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 |
| 92 | b, |
| 93 | c, |
| 94 | g, |
| 95 | h, |
| 96 | m, |
| 97 | r, |
| 98 | x; |
| 99 | |
| 100 | /* |
| 101 | Convert HCL to RGB colorspace. |
| 102 | */ |
| 103 | assert(red != (double *) NULL); |
| 104 | assert(green != (double *) NULL); |
| 105 | assert(blue != (double *) NULL); |
| 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 | } |
| 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 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 158 | % 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] | 159 | % % |
| 160 | % % |
| 161 | % % |
| 162 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 163 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 164 | % ConvertHSBToRGB() transforms a (hue, saturation, brightness) to a (red, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | % green, blue) triple. |
| 166 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 167 | % The format of the ConvertHSBToRGBImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 168 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 169 | % void ConvertHSBToRGB(const double hue,const double saturation, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 170 | % const double brightness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | % |
| 172 | % A description of each parameter follows: |
| 173 | % |
| 174 | % o hue, saturation, brightness: A double value representing a |
| 175 | % component of the HSB color space. |
| 176 | % |
| 177 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 178 | % |
| 179 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 180 | MagickPrivate void ConvertHSBToRGB(const double hue,const double saturation, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 181 | const double brightness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 183 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | f, |
| 185 | h, |
| 186 | p, |
| 187 | q, |
| 188 | t; |
| 189 | |
| 190 | /* |
| 191 | Convert HSB to RGB colorspace. |
| 192 | */ |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 193 | assert(red != (double *) NULL); |
| 194 | assert(green != (double *) NULL); |
| 195 | assert(blue != (double *) NULL); |
cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 196 | if (saturation == 0.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 198 | *red=QuantumRange*brightness; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | *green=(*red); |
| 200 | *blue=(*red); |
| 201 | return; |
| 202 | } |
cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 203 | h=6.0*(hue-floor(hue)); |
| 204 | f=h-floor((double) h); |
| 205 | p=brightness*(1.0-saturation); |
| 206 | q=brightness*(1.0-saturation*f); |
| 207 | t=brightness*(1.0-(saturation*(1.0-f))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | switch ((int) h) |
| 209 | { |
| 210 | case 0: |
| 211 | default: |
| 212 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 213 | *red=QuantumRange*brightness; |
| 214 | *green=QuantumRange*t; |
| 215 | *blue=QuantumRange*p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | case 1: |
| 219 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 220 | *red=QuantumRange*q; |
| 221 | *green=QuantumRange*brightness; |
| 222 | *blue=QuantumRange*p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | case 2: |
| 226 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 227 | *red=QuantumRange*p; |
| 228 | *green=QuantumRange*brightness; |
| 229 | *blue=QuantumRange*t; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | case 3: |
| 233 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 234 | *red=QuantumRange*p; |
| 235 | *green=QuantumRange*q; |
| 236 | *blue=QuantumRange*brightness; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | case 4: |
| 240 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 241 | *red=QuantumRange*t; |
| 242 | *green=QuantumRange*p; |
| 243 | *blue=QuantumRange*brightness; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | break; |
| 245 | } |
| 246 | case 5: |
| 247 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 248 | *red=QuantumRange*brightness; |
| 249 | *green=QuantumRange*p; |
| 250 | *blue=QuantumRange*q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 258 | % % |
| 259 | % % |
| 260 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 261 | % 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] | 262 | % % |
| 263 | % % |
| 264 | % % |
| 265 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 266 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 267 | % ConvertHSLToRGB() transforms a (hue, saturation, lightness) to a (red, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 268 | % green, blue) triple. |
| 269 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 270 | % The format of the ConvertHSLToRGBImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 272 | % void ConvertHSLToRGB(const double hue,const double saturation, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 273 | % const double lightness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | % |
| 275 | % A description of each parameter follows: |
| 276 | % |
| 277 | % o hue, saturation, lightness: A double value representing a |
| 278 | % component of the HSL color space. |
| 279 | % |
| 280 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 281 | % |
| 282 | */ |
| 283 | |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 284 | static inline double ConvertHueToRGB(double m1,double m2,double hue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 285 | { |
| 286 | if (hue < 0.0) |
| 287 | hue+=1.0; |
| 288 | if (hue > 1.0) |
| 289 | hue-=1.0; |
| 290 | if ((6.0*hue) < 1.0) |
cristy | a163b0c | 2010-04-13 01:04:49 +0000 | [diff] [blame] | 291 | return(m1+6.0*(m2-m1)*hue); |
| 292 | if ((2.0*hue) < 1.0) |
| 293 | return(m2); |
| 294 | if ((3.0*hue) < 2.0) |
| 295 | return(m1+6.0*(m2-m1)*(2.0/3.0-hue)); |
| 296 | return(m1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 297 | } |
| 298 | |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 299 | MagickExport void ConvertHSLToRGB(const double hue,const double saturation, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 300 | const double lightness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 301 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 302 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | b, |
| 304 | g, |
| 305 | r, |
| 306 | m1, |
| 307 | m2; |
| 308 | |
| 309 | /* |
| 310 | Convert HSL to RGB colorspace. |
| 311 | */ |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 312 | assert(red != (double *) NULL); |
| 313 | assert(green != (double *) NULL); |
| 314 | assert(blue != (double *) NULL); |
cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 315 | if (saturation == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 316 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 317 | *red=QuantumRange*lightness; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | *green=(*red); |
| 319 | *blue=(*red); |
| 320 | return; |
| 321 | } |
cristy | 27cabb8 | 2010-04-14 11:33:57 +0000 | [diff] [blame] | 322 | if (lightness < 0.5) |
cristy | 98a65d5 | 2010-04-14 02:12:38 +0000 | [diff] [blame] | 323 | m2=lightness*(saturation+1.0); |
| 324 | else |
| 325 | m2=(lightness+saturation)-(lightness*saturation); |
| 326 | m1=2.0*lightness-m2; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 327 | r=ConvertHueToRGB(m1,m2,hue+1.0/3.0); |
| 328 | g=ConvertHueToRGB(m1,m2,hue); |
| 329 | b=ConvertHueToRGB(m1,m2,hue-1.0/3.0); |
| 330 | *red=QuantumRange*r; |
| 331 | *green=QuantumRange*g; |
| 332 | *blue=QuantumRange*b; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 337 | % % |
| 338 | % % |
| 339 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 340 | % 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] | 341 | % % |
| 342 | % % |
| 343 | % % |
| 344 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 345 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 346 | % ConvertHWBToRGB() transforms a (hue, whiteness, blackness) to a (red, green, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | % blue) triple. |
| 348 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 349 | % The format of the ConvertHWBToRGBImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 350 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 351 | % void ConvertHWBToRGB(const double hue,const double whiteness, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 352 | % const double blackness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 353 | % |
| 354 | % A description of each parameter follows: |
| 355 | % |
| 356 | % o hue, whiteness, blackness: A double value representing a |
| 357 | % component of the HWB color space. |
| 358 | % |
| 359 | % o red, green, blue: A pointer to a pixel component of type Quantum. |
| 360 | % |
| 361 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 362 | MagickPrivate void ConvertHWBToRGB(const double hue,const double whiteness, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 363 | const double blackness,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 365 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | b, |
| 367 | f, |
| 368 | g, |
| 369 | n, |
| 370 | r, |
| 371 | v; |
| 372 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 373 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | i; |
| 375 | |
| 376 | /* |
| 377 | Convert HWB to RGB colorspace. |
| 378 | */ |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 379 | assert(red != (double *) NULL); |
| 380 | assert(green != (double *) NULL); |
| 381 | assert(blue != (double *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | v=1.0-blackness; |
cristy | af10b11 | 2012-04-18 13:25:37 +0000 | [diff] [blame] | 383 | if (hue == -1.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 384 | { |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 385 | *red=QuantumRange*v; |
| 386 | *green=QuantumRange*v; |
| 387 | *blue=QuantumRange*v; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 390 | i=(ssize_t) floor(6.0*hue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 391 | f=6.0*hue-i; |
| 392 | if ((i & 0x01) != 0) |
| 393 | f=1.0-f; |
| 394 | n=whiteness+f*(v-whiteness); /* linear interpolation */ |
| 395 | switch (i) |
| 396 | { |
| 397 | default: |
| 398 | case 6: |
| 399 | case 0: r=v; g=n; b=whiteness; break; |
| 400 | case 1: r=n; g=v; b=whiteness; break; |
| 401 | case 2: r=whiteness; g=v; b=n; break; |
| 402 | case 3: r=whiteness; g=n; b=v; break; |
| 403 | case 4: r=n; g=whiteness; b=v; break; |
| 404 | case 5: r=v; g=whiteness; b=n; break; |
| 405 | } |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 406 | *red=QuantumRange*r; |
| 407 | *green=QuantumRange*g; |
| 408 | *blue=QuantumRange*b; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 413 | % % |
| 414 | % % |
| 415 | % % |
cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame^] | 416 | % C o n v e r t R G B T o H C L % |
| 417 | % % |
| 418 | % % |
| 419 | % % |
| 420 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 421 | % |
| 422 | % ConvertRGBToHCL() transforms a (red, green, blue) to a (hue, chroma, |
| 423 | % luma) triple. |
| 424 | % |
| 425 | % The format of the ConvertRGBToHCL method is: |
| 426 | % |
| 427 | % void ConvertRGBToHCL(const double red,const double green, |
| 428 | % const double blue,double *hue,double *chroma,double *luma) |
| 429 | % |
| 430 | % A description of each parameter follows: |
| 431 | % |
| 432 | % o red, green, blue: A Quantum value representing the red, green, and |
| 433 | % blue component of a pixel. |
| 434 | % |
| 435 | % o hue, chroma, luma: A pointer to a double value representing a |
| 436 | % component of the HCL color space. |
| 437 | % |
| 438 | */ |
| 439 | MagickPrivate void ConvertRGBToHCL(const double red,const double green, |
| 440 | const double blue,double *hue,double *chroma,double *luma) |
| 441 | { |
| 442 | double |
| 443 | b, |
| 444 | c, |
| 445 | g, |
| 446 | h, |
| 447 | max, |
| 448 | r; |
| 449 | |
| 450 | /* |
| 451 | Convert RGB to HCL colorspace. |
| 452 | */ |
| 453 | assert(hue != (double *) NULL); |
| 454 | assert(chroma != (double *) NULL); |
| 455 | assert(luma != (double *) NULL); |
| 456 | r=red; |
| 457 | g=green; |
| 458 | b=blue; |
| 459 | max=MagickMax(r,MagickMax(g,b)); |
| 460 | c=max-(double) MagickMin(r,MagickMin(g,b)); |
| 461 | h=0.0; |
| 462 | if (c == 0) |
| 463 | h=0.0; |
| 464 | else |
| 465 | if (red == max) |
| 466 | h=fmod((g-b)/c,6.0); |
| 467 | else |
| 468 | if (green == max) |
| 469 | h=((b-r)/c)+2.0; |
| 470 | else |
| 471 | if (blue == max) |
| 472 | h=((r-g)/c)+4.0; |
| 473 | *hue=(h/6.0); |
| 474 | *chroma=QuantumScale*c; |
| 475 | *luma=QuantumScale*(0.298839*r+0.586811*g+0.114350*b); |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 480 | % % |
| 481 | % % |
| 482 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 483 | % 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] | 484 | % % |
| 485 | % % |
| 486 | % % |
| 487 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 488 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 489 | % ConvertRGBToHSB() transforms a (red, green, blue) to a (hue, saturation, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 490 | % brightness) triple. |
| 491 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 492 | % The format of the ConvertRGBToHSB method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 493 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 494 | % void ConvertRGBToHSB(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 495 | % const double blue,double *hue,double *saturation,double *brightness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 496 | % |
| 497 | % A description of each parameter follows: |
| 498 | % |
| 499 | % o red, green, blue: A Quantum value representing the red, green, and |
| 500 | % blue component of a pixel.. |
| 501 | % |
| 502 | % o hue, saturation, brightness: A pointer to a double value representing a |
| 503 | % component of the HSB color space. |
| 504 | % |
| 505 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 506 | MagickPrivate void ConvertRGBToHSB(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 507 | const double blue,double *hue,double *saturation,double *brightness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 509 | double |
| 510 | b, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | delta, |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 512 | g, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | max, |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 514 | min, |
| 515 | r; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | Convert RGB to HSB colorspace. |
| 519 | */ |
| 520 | assert(hue != (double *) NULL); |
| 521 | assert(saturation != (double *) NULL); |
| 522 | assert(brightness != (double *) NULL); |
| 523 | *hue=0.0; |
| 524 | *saturation=0.0; |
| 525 | *brightness=0.0; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 526 | r=red; |
| 527 | g=green; |
| 528 | b=blue; |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 529 | min=r < g ? r : g; |
| 530 | if (b < min) |
| 531 | min=b; |
| 532 | max=r > g ? r : g; |
| 533 | if (b > max) |
| 534 | max=b; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | if (max == 0.0) |
| 536 | return; |
| 537 | delta=max-min; |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 538 | *saturation=delta/max; |
| 539 | *brightness=QuantumScale*max; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | if (delta == 0.0) |
| 541 | return; |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 542 | if (r == max) |
| 543 | *hue=(g-b)/delta; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 544 | else |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 545 | if (g == max) |
| 546 | *hue=2.0+(b-r)/delta; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 547 | else |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 548 | *hue=4.0+(r-g)/delta; |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 549 | *hue/=6.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 550 | if (*hue < 0.0) |
| 551 | *hue+=1.0; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 556 | % % |
| 557 | % % |
| 558 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 559 | % 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] | 560 | % % |
| 561 | % % |
| 562 | % % |
| 563 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 564 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 565 | % ConvertRGBToHSL() transforms a (red, green, blue) to a (hue, saturation, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 566 | % lightness) triple. |
| 567 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 568 | % The format of the ConvertRGBToHSL method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 570 | % void ConvertRGBToHSL(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 571 | % const double blue,double *hue,double *saturation,double *lightness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 572 | % |
| 573 | % A description of each parameter follows: |
| 574 | % |
| 575 | % o red, green, blue: A Quantum value representing the red, green, and |
| 576 | % blue component of a pixel.. |
| 577 | % |
| 578 | % o hue, saturation, lightness: A pointer to a double value representing a |
| 579 | % component of the HSL color space. |
| 580 | % |
| 581 | */ |
| 582 | |
| 583 | static inline double MagickMax(const double x,const double y) |
| 584 | { |
| 585 | if (x > y) |
| 586 | return(x); |
| 587 | return(y); |
| 588 | } |
| 589 | |
| 590 | static inline double MagickMin(const double x,const double y) |
| 591 | { |
| 592 | if (x < y) |
| 593 | return(x); |
| 594 | return(y); |
| 595 | } |
| 596 | |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 597 | MagickExport void ConvertRGBToHSL(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 598 | const double blue,double *hue,double *saturation,double *lightness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 600 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 601 | b, |
| 602 | delta, |
| 603 | g, |
| 604 | max, |
| 605 | min, |
| 606 | r; |
| 607 | |
| 608 | /* |
| 609 | Convert RGB to HSL colorspace. |
| 610 | */ |
| 611 | assert(hue != (double *) NULL); |
| 612 | assert(saturation != (double *) NULL); |
| 613 | assert(lightness != (double *) NULL); |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 614 | r=QuantumScale*red; |
| 615 | g=QuantumScale*green; |
| 616 | b=QuantumScale*blue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | max=MagickMax(r,MagickMax(g,b)); |
| 618 | min=MagickMin(r,MagickMin(g,b)); |
| 619 | *lightness=(double) ((min+max)/2.0); |
| 620 | delta=max-min; |
| 621 | if (delta == 0.0) |
| 622 | { |
| 623 | *hue=0.0; |
| 624 | *saturation=0.0; |
| 625 | return; |
| 626 | } |
| 627 | if (*lightness < 0.5) |
| 628 | *saturation=(double) (delta/(min+max)); |
| 629 | else |
| 630 | *saturation=(double) (delta/(2.0-max-min)); |
| 631 | if (r == max) |
| 632 | *hue=((((max-b)/6.0)+(delta/2.0))-(((max-g)/6.0)+(delta/2.0)))/delta; |
| 633 | else |
| 634 | if (g == max) |
| 635 | *hue=(1.0/3.0)+((((max-r)/6.0)+(delta/2.0))-(((max-b)/6.0)+(delta/2.0)))/ |
| 636 | delta; |
| 637 | else |
| 638 | if (b == max) |
| 639 | *hue=(2.0/3.0)+((((max-g)/6.0)+(delta/2.0))-(((max-r)/6.0)+ |
| 640 | (delta/2.0)))/delta; |
| 641 | if (*hue < 0.0) |
| 642 | *hue+=1.0; |
| 643 | if (*hue > 1.0) |
| 644 | *hue-=1.0; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 649 | % % |
| 650 | % % |
| 651 | % % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 652 | % 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] | 653 | % % |
| 654 | % % |
| 655 | % % |
| 656 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 657 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 658 | % ConvertRGBToHWB() transforms a (red, green, blue) to a (hue, whiteness, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 659 | % blackness) triple. |
| 660 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 661 | % The format of the ConvertRGBToHWB method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | % |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 663 | % void ConvertRGBToHWB(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 664 | % const double blue,double *hue,double *whiteness,double *blackness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 665 | % |
| 666 | % A description of each parameter follows: |
| 667 | % |
| 668 | % o red, green, blue: A Quantum value representing the red, green, and |
| 669 | % blue component of a pixel. |
| 670 | % |
| 671 | % o hue, whiteness, blackness: A pointer to a double value representing a |
| 672 | % component of the HWB color space. |
| 673 | % |
| 674 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 675 | MagickPrivate void ConvertRGBToHWB(const double red,const double green, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 676 | const double blue,double *hue,double *whiteness,double *blackness) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 677 | { |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 678 | double |
| 679 | b, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 680 | f, |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 681 | g, |
| 682 | p, |
| 683 | r, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 684 | v, |
| 685 | w; |
| 686 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | /* |
| 688 | Convert RGB to HWB colorspace. |
| 689 | */ |
| 690 | assert(hue != (double *) NULL); |
| 691 | assert(whiteness != (double *) NULL); |
| 692 | assert(blackness != (double *) NULL); |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 693 | r=red; |
| 694 | g=green; |
| 695 | b=blue; |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 696 | w=MagickMin(r,MagickMin(g,b)); |
| 697 | v=MagickMax(r,MagickMax(g,b)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 698 | *blackness=1.0-QuantumScale*v; |
| 699 | *whiteness=QuantumScale*w; |
| 700 | if (v == w) |
| 701 | { |
cristy | af10b11 | 2012-04-18 13:25:37 +0000 | [diff] [blame] | 702 | *hue=(-1.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 705 | f=(r == w) ? g-b : ((g == w) ? b-r : r-g); |
| 706 | p=(r == w) ? 3.0 : ((g == w) ? 5.0 : 1.0); |
| 707 | *hue=(p-f/(v-1.0*w))/6.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
| 711 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 712 | % % |
| 713 | % % |
| 714 | % % |
| 715 | % E x p a n d A f f i n e % |
| 716 | % % |
| 717 | % % |
| 718 | % % |
| 719 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 720 | % |
| 721 | % ExpandAffine() computes the affine's expansion factor, i.e. the square root |
| 722 | % of the factor by which the affine transform affects area. In an affine |
| 723 | % transform composed of scaling, rotation, shearing, and translation, returns |
| 724 | % the amount of scaling. |
| 725 | % |
| 726 | % The format of the ExpandAffine method is: |
| 727 | % |
| 728 | % double ExpandAffine(const AffineMatrix *affine) |
| 729 | % |
| 730 | % A description of each parameter follows: |
| 731 | % |
| 732 | % o expansion: Method ExpandAffine returns the affine's expansion factor. |
| 733 | % |
| 734 | % o affine: A pointer the affine transform of type AffineMatrix. |
| 735 | % |
| 736 | */ |
| 737 | MagickExport double ExpandAffine(const AffineMatrix *affine) |
| 738 | { |
| 739 | assert(affine != (const AffineMatrix *) NULL); |
| 740 | return(sqrt(fabs(affine->sx*affine->sy-affine->rx*affine->ry))); |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 745 | % % |
| 746 | % % |
| 747 | % % |
| 748 | % 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 % |
| 749 | % % |
| 750 | % % |
| 751 | % % |
| 752 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 753 | % |
cristy | 82b1583 | 2009-10-06 19:17:37 +0000 | [diff] [blame] | 754 | % GenerateDifferentialNoise() generates differentual noise. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 755 | % |
| 756 | % The format of the GenerateDifferentialNoise method is: |
| 757 | % |
| 758 | % double GenerateDifferentialNoise(RandomInfo *random_info, |
cristy | 9ed1f81 | 2011-10-08 02:00:08 +0000 | [diff] [blame] | 759 | % const Quantum pixel,const NoiseType noise_type,const double attenuate) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 760 | % |
| 761 | % A description of each parameter follows: |
| 762 | % |
| 763 | % o random_info: the random info. |
| 764 | % |
| 765 | % o pixel: noise is relative to this pixel value. |
| 766 | % |
| 767 | % o noise_type: the type of noise. |
| 768 | % |
| 769 | % o attenuate: attenuate the noise. |
| 770 | % |
| 771 | */ |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 772 | MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info, |
cristy | 9ed1f81 | 2011-10-08 02:00:08 +0000 | [diff] [blame] | 773 | const Quantum pixel,const NoiseType noise_type,const double attenuate) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | { |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 775 | #define SigmaUniform (attenuate*0.015625) |
| 776 | #define SigmaGaussian (attenuate*0.015625) |
| 777 | #define SigmaImpulse (attenuate*0.1) |
| 778 | #define SigmaLaplacian (attenuate*0.0390625) |
| 779 | #define SigmaMultiplicativeGaussian (attenuate*0.5) |
cristy | 4ce9df6 | 2011-10-12 12:06:02 +0000 | [diff] [blame] | 780 | #define SigmaPoisson (attenuate*12.5) |
cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 781 | #define SigmaRandom (attenuate) |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 782 | #define TauGaussian (attenuate*0.078125) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | |
cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 784 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 785 | alpha, |
| 786 | beta, |
| 787 | noise, |
| 788 | sigma; |
| 789 | |
| 790 | alpha=GetPseudoRandomValue(random_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 791 | switch (noise_type) |
| 792 | { |
| 793 | case UniformNoise: |
| 794 | default: |
| 795 | { |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 796 | noise=(double) (pixel+QuantumRange*SigmaUniform*(alpha-0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | case GaussianNoise: |
| 800 | { |
cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 801 | double |
cristy | 62faa60 | 2010-02-20 03:36:17 +0000 | [diff] [blame] | 802 | gamma, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | tau; |
| 804 | |
cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 805 | if (alpha == 0.0) |
| 806 | alpha=1.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 807 | beta=GetPseudoRandomValue(random_info); |
cristy | 62faa60 | 2010-02-20 03:36:17 +0000 | [diff] [blame] | 808 | gamma=sqrt(-2.0*log(alpha)); |
cristy | 55a91cd | 2010-12-01 00:57:40 +0000 | [diff] [blame] | 809 | sigma=gamma*cos((double) (2.0*MagickPI*beta)); |
| 810 | tau=gamma*sin((double) (2.0*MagickPI*beta)); |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 811 | noise=(double) (pixel+sqrt((double) pixel)*SigmaGaussian*sigma+ |
| 812 | QuantumRange*TauGaussian*tau); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | case ImpulseNoise: |
| 816 | { |
| 817 | if (alpha < (SigmaImpulse/2.0)) |
| 818 | noise=0.0; |
cristy | 37c2407 | 2011-10-08 01:26:00 +0000 | [diff] [blame] | 819 | else |
| 820 | if (alpha >= (1.0-(SigmaImpulse/2.0))) |
| 821 | noise=(double) QuantumRange; |
| 822 | else |
| 823 | noise=(double) pixel; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | break; |
| 825 | } |
| 826 | case LaplacianNoise: |
| 827 | { |
| 828 | if (alpha <= 0.5) |
| 829 | { |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 830 | if (alpha <= MagickEpsilon) |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 831 | noise=(double) (pixel-QuantumRange); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 832 | else |
cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 833 | noise=(double) (pixel+QuantumRange*SigmaLaplacian*log(2.0*alpha)+ |
| 834 | 0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | beta=1.0-alpha; |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 838 | if (beta <= (0.5*MagickEpsilon)) |
cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 839 | noise=(double) (pixel+QuantumRange); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 840 | else |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 841 | noise=(double) (pixel-QuantumRange*SigmaLaplacian*log(2.0*beta)+0.5); |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | case MultiplicativeGaussianNoise: |
| 845 | { |
| 846 | sigma=1.0; |
| 847 | if (alpha > MagickEpsilon) |
| 848 | sigma=sqrt(-2.0*log(alpha)); |
| 849 | beta=GetPseudoRandomValue(random_info); |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 850 | noise=(double) (pixel+pixel*SigmaMultiplicativeGaussian*sigma* |
| 851 | cos((double) (2.0*MagickPI*beta))/2.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | case PoissonNoise: |
| 855 | { |
cristy | adb41ca | 2009-10-22 15:02:28 +0000 | [diff] [blame] | 856 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | poisson; |
| 858 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 859 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 860 | i; |
| 861 | |
cristy | 7118edf | 2011-10-08 13:33:25 +0000 | [diff] [blame] | 862 | poisson=exp(-SigmaPoisson*QuantumScale*pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 863 | for (i=0; alpha > poisson; i++) |
| 864 | { |
| 865 | beta=GetPseudoRandomValue(random_info); |
| 866 | alpha*=beta; |
| 867 | } |
cristy | 6bbabe6 | 2011-10-09 13:54:18 +0000 | [diff] [blame] | 868 | noise=(double) (QuantumRange*i/SigmaPoisson); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | case RandomNoise: |
| 872 | { |
cristy | 785eb59 | 2012-07-08 22:12:15 +0000 | [diff] [blame] | 873 | noise=(double) (QuantumRange*SigmaRandom*alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | } |
| 877 | return(noise); |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 882 | % % |
| 883 | % % |
| 884 | % % |
| 885 | % G e t O p t i m a l K e r n e l W i d t h % |
| 886 | % % |
| 887 | % % |
| 888 | % % |
| 889 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 890 | % |
| 891 | % GetOptimalKernelWidth() computes the optimal kernel radius for a convolution |
| 892 | % filter. Start with the minimum value of 3 pixels and walk out until we drop |
| 893 | % below the threshold of one pixel numerical accuracy. |
| 894 | % |
| 895 | % The format of the GetOptimalKernelWidth method is: |
| 896 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 897 | % size_t GetOptimalKernelWidth(const double radius, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 898 | % const double sigma) |
| 899 | % |
| 900 | % A description of each parameter follows: |
| 901 | % |
| 902 | % o width: Method GetOptimalKernelWidth returns the optimal width of |
| 903 | % a convolution kernel. |
| 904 | % |
| 905 | % o radius: the radius of the Gaussian, in pixels, not counting the center |
| 906 | % pixel. |
| 907 | % |
| 908 | % o sigma: the standard deviation of the Gaussian, in pixels. |
| 909 | % |
| 910 | */ |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 911 | MagickPrivate size_t GetOptimalKernelWidth1D(const double radius, |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 912 | const double sigma) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 913 | { |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 914 | double |
| 915 | alpha, |
| 916 | beta, |
| 917 | gamma, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | normalize, |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 919 | value; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 920 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 921 | register ssize_t |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 922 | i; |
| 923 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 924 | size_t |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 925 | width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 926 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 927 | ssize_t |
| 928 | j; |
| 929 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 931 | if (radius > MagickEpsilon) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 932 | return((size_t) (2.0*ceil(radius)+1.0)); |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 933 | gamma=fabs(sigma); |
| 934 | if (gamma <= MagickEpsilon) |
anthony | c106172 | 2010-05-14 06:23:49 +0000 | [diff] [blame] | 935 | return(3UL); |
cristy | 35f1530 | 2012-06-07 14:59:02 +0000 | [diff] [blame] | 936 | alpha=MagickEpsilonReciprocal(2.0*gamma*gamma); |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 937 | beta=(double) MagickEpsilonReciprocal((MagickRealType) MagickSQ2PI*gamma); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | for (width=5; ; ) |
| 939 | { |
| 940 | normalize=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 941 | j=(ssize_t) width/2; |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 942 | for (i=(-j); i <= j; i++) |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 943 | normalize+=exp(-((double) (i*i))*alpha)*beta; |
| 944 | value=exp(-((double) (j*j))*alpha)*beta/normalize; |
cristy | 20908da | 2009-12-02 14:34:11 +0000 | [diff] [blame] | 945 | if ((value < QuantumScale) || (value < MagickEpsilon)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 946 | break; |
| 947 | width+=2; |
| 948 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 949 | return((size_t) (width-2)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | } |
| 951 | |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 952 | MagickPrivate size_t GetOptimalKernelWidth2D(const double radius, |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 953 | const double sigma) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | { |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 955 | double |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 956 | alpha, |
| 957 | beta, |
| 958 | gamma, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 959 | normalize, |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 960 | value; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 961 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 962 | size_t |
| 963 | width; |
| 964 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 965 | ssize_t |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 966 | j, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 967 | u, |
| 968 | v; |
| 969 | |
| 970 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 971 | if (radius > MagickEpsilon) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 972 | return((size_t) (2.0*ceil(radius)+1.0)); |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 973 | gamma=fabs(sigma); |
| 974 | if (gamma <= MagickEpsilon) |
anthony | c106172 | 2010-05-14 06:23:49 +0000 | [diff] [blame] | 975 | return(3UL); |
cristy | 35f1530 | 2012-06-07 14:59:02 +0000 | [diff] [blame] | 976 | alpha=MagickEpsilonReciprocal(2.0*gamma*gamma); |
cristy | caf4580 | 2012-06-16 18:28:54 +0000 | [diff] [blame] | 977 | beta=(double) MagickEpsilonReciprocal((MagickRealType) Magick2PI*gamma*gamma); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 978 | for (width=5; ; ) |
| 979 | { |
| 980 | normalize=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 981 | j=(ssize_t) width/2; |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 982 | for (v=(-j); v <= j; v++) |
cristy | 47e0050 | 2009-12-17 19:19:57 +0000 | [diff] [blame] | 983 | for (u=(-j); u <= j; u++) |
cristy | e96405a | 2010-05-19 02:24:31 +0000 | [diff] [blame] | 984 | normalize+=exp(-((double) (u*u+v*v))*alpha)*beta; |
| 985 | value=exp(-((double) (j*j))*alpha)*beta/normalize; |
cristy | 20908da | 2009-12-02 14:34:11 +0000 | [diff] [blame] | 986 | if ((value < QuantumScale) || (value < MagickEpsilon)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 987 | break; |
| 988 | width+=2; |
| 989 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 990 | return((size_t) (width-2)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | } |
| 992 | |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 993 | MagickPrivate size_t GetOptimalKernelWidth(const double radius, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 994 | const double sigma) |
| 995 | { |
| 996 | return(GetOptimalKernelWidth1D(radius,sigma)); |
| 997 | } |