cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % EEEEE N N H H AAA N N CCCC EEEEE % |
| 7 | % E NN N H H A A NN N C E % |
| 8 | % EEE N N N HHHHH AAAAA N N N C EEE % |
| 9 | % E N NN H H A A N NN C E % |
| 10 | % EEEEE N N H H A A N N CCCC EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Enhancement Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 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 | */ |
| 43 | #include "magick/studio.h" |
| 44 | #include "magick/artifact.h" |
| 45 | #include "magick/cache.h" |
| 46 | #include "magick/cache-view.h" |
| 47 | #include "magick/color.h" |
| 48 | #include "magick/color-private.h" |
| 49 | #include "magick/colorspace.h" |
| 50 | #include "magick/composite-private.h" |
| 51 | #include "magick/enhance.h" |
| 52 | #include "magick/exception.h" |
| 53 | #include "magick/exception-private.h" |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 54 | #include "magick/fx.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 55 | #include "magick/gem.h" |
| 56 | #include "magick/geometry.h" |
| 57 | #include "magick/histogram.h" |
| 58 | #include "magick/image.h" |
| 59 | #include "magick/image-private.h" |
| 60 | #include "magick/memory_.h" |
| 61 | #include "magick/monitor.h" |
| 62 | #include "magick/monitor-private.h" |
| 63 | #include "magick/option.h" |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 64 | #include "magick/pixel-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 65 | #include "magick/quantum.h" |
| 66 | #include "magick/quantum-private.h" |
| 67 | #include "magick/resample.h" |
| 68 | #include "magick/resample-private.h" |
| 69 | #include "magick/statistic.h" |
| 70 | #include "magick/string_.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 71 | #include "magick/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | #include "magick/thread-private.h" |
| 73 | #include "magick/token.h" |
| 74 | #include "magick/xml-tree.h" |
| 75 | |
| 76 | /* |
| 77 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 78 | % % |
| 79 | % % |
| 80 | % % |
| 81 | % A u t o G a m m a I m a g e % |
| 82 | % % |
| 83 | % % |
| 84 | % % |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % |
| 87 | % AutoGammaImage() extract the 'mean' from the image and adjust the image |
| 88 | % to try make set its gamma appropriatally. |
| 89 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 90 | % The format of the AutoGammaImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 91 | % |
| 92 | % MagickBooleanType AutoGammaImage(Image *image) |
| 93 | % MagickBooleanType AutoGammaImageChannel(Image *image, |
| 94 | % const ChannelType channel) |
| 95 | % |
| 96 | % A description of each parameter follows: |
| 97 | % |
| 98 | % o image: The image to auto-level |
| 99 | % |
| 100 | % o channel: The channels to auto-level. If the special 'SyncChannels' |
| 101 | % flag is set all given channels is adjusted in the same way using the |
| 102 | % mean average of those channels. |
| 103 | % |
| 104 | */ |
| 105 | |
| 106 | MagickExport MagickBooleanType AutoGammaImage(Image *image) |
| 107 | { |
| 108 | return(AutoGammaImageChannel(image,DefaultChannels)); |
| 109 | } |
| 110 | |
| 111 | MagickExport MagickBooleanType AutoGammaImageChannel(Image *image, |
| 112 | const ChannelType channel) |
| 113 | { |
| 114 | MagickStatusType |
| 115 | status; |
| 116 | |
| 117 | double |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 118 | mean,sans,gamma,logmean; |
anthony | 4efe597 | 2009-09-11 06:46:40 +0000 | [diff] [blame] | 119 | |
| 120 | logmean=log(0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 121 | |
| 122 | if ((channel & SyncChannels) != 0 ) |
| 123 | { |
| 124 | /* |
| 125 | Apply gamma correction equally accross all given channels |
| 126 | */ |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 127 | (void) GetImageChannelMean(image,channel,&mean,&sans,&image->exception); |
| 128 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | return LevelImageChannel(image, channel, |
| 130 | 0.0, (double)QuantumRange, gamma); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | auto-gamma each channel separateally |
| 135 | */ |
| 136 | status = MagickTrue; |
| 137 | if ((channel & RedChannel) != 0) |
| 138 | { |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 139 | (void) GetImageChannelMean(image,RedChannel,&mean,&sans, |
| 140 | &image->exception); |
| 141 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 142 | status = status && LevelImageChannel(image, RedChannel, |
| 143 | 0.0, (double)QuantumRange, gamma); |
| 144 | } |
| 145 | if ((channel & GreenChannel) != 0) |
| 146 | { |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 147 | (void) GetImageChannelMean(image,GreenChannel,&mean,&sans, |
| 148 | &image->exception); |
| 149 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | status = status && LevelImageChannel(image, GreenChannel, |
| 151 | 0.0, (double)QuantumRange, gamma); |
| 152 | } |
| 153 | if ((channel & BlueChannel) != 0) |
| 154 | { |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 155 | (void) GetImageChannelMean(image,BlueChannel,&mean,&sans, |
| 156 | &image->exception); |
| 157 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 158 | status = status && LevelImageChannel(image, BlueChannel, |
| 159 | 0.0, (double)QuantumRange, gamma); |
| 160 | } |
| 161 | if (((channel & OpacityChannel) != 0) && |
| 162 | (image->matte == MagickTrue)) |
| 163 | { |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 164 | (void) GetImageChannelMean(image,OpacityChannel,&mean,&sans, |
| 165 | &image->exception); |
| 166 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 167 | status = status && LevelImageChannel(image, OpacityChannel, |
| 168 | 0.0, (double)QuantumRange, gamma); |
| 169 | } |
| 170 | if (((channel & IndexChannel) != 0) && |
| 171 | (image->colorspace == CMYKColorspace)) |
| 172 | { |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 173 | (void) GetImageChannelMean(image,IndexChannel,&mean,&sans, |
| 174 | &image->exception); |
| 175 | gamma=log(mean*QuantumScale)/logmean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | status = status && LevelImageChannel(image, IndexChannel, |
| 177 | 0.0, (double)QuantumRange, gamma); |
| 178 | } |
| 179 | return(status != 0 ? MagickTrue : MagickFalse); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 184 | % % |
| 185 | % % |
| 186 | % % |
| 187 | % A u t o L e v e l I m a g e % |
| 188 | % % |
| 189 | % % |
| 190 | % % |
| 191 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 192 | % |
| 193 | % AutoLevelImage() adjusts the levels of a particular image channel by |
| 194 | % scaling the minimum and maximum values to the full quantum range. |
| 195 | % |
| 196 | % The format of the LevelImage method is: |
| 197 | % |
| 198 | % MagickBooleanType AutoLevelImage(Image *image) |
| 199 | % MagickBooleanType AutoLevelImageChannel(Image *image, |
| 200 | % const ChannelType channel) |
| 201 | % |
| 202 | % A description of each parameter follows: |
| 203 | % |
| 204 | % o image: The image to auto-level |
| 205 | % |
| 206 | % o channel: The channels to auto-level. If the special 'SyncChannels' |
| 207 | % flag is set the min/max/mean value of all given channels is used for |
| 208 | % all given channels, to all channels in the same way. |
| 209 | % |
| 210 | */ |
| 211 | |
| 212 | MagickExport MagickBooleanType AutoLevelImage(Image *image) |
| 213 | { |
| 214 | return(AutoLevelImageChannel(image,DefaultChannels)); |
| 215 | } |
| 216 | |
| 217 | MagickExport MagickBooleanType AutoLevelImageChannel(Image *image, |
| 218 | const ChannelType channel) |
| 219 | { |
| 220 | /* |
| 221 | This is simply a convenience function around a Min/Max Histogram Stretch |
| 222 | */ |
| 223 | return MinMaxStretchImage(image, channel, 0.0, 0.0); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 228 | % % |
| 229 | % % |
| 230 | % % |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 231 | % B r i g h t n e s s C o n t r a s t I m a g e % |
| 232 | % % |
| 233 | % % |
| 234 | % % |
| 235 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 236 | % |
| 237 | % Use BrightnessContrastImage() to change the brightness and/or contrast of |
| 238 | % an image. It converts the brightness and contrast parameters into slope |
| 239 | % and intercept and calls a polynomical function to apply to the image. |
| 240 | % |
| 241 | % The format of the BrightnessContrastImage method is: |
| 242 | % |
| 243 | % MagickBooleanType BrightnessContrastImage(Image *image, |
| 244 | % const double brightness,const double contrast) |
| 245 | % MagickBooleanType BrightnessContrastImageChannel(Image *image, |
| 246 | % const ChannelType channel,const double brightness, |
| 247 | % const double contrast) |
| 248 | % |
| 249 | % A description of each parameter follows: |
| 250 | % |
| 251 | % o image: the image. |
| 252 | % |
| 253 | % o channel: the channel. |
| 254 | % |
| 255 | % o brightness: the brightness percent (-100 .. 100). |
| 256 | % |
| 257 | % o contrast: the contrast percent (-100 .. 100). |
| 258 | % |
| 259 | */ |
| 260 | |
| 261 | MagickExport MagickBooleanType BrightnessContrastImage(Image *image, |
| 262 | const double brightness,const double contrast) |
| 263 | { |
| 264 | MagickBooleanType |
| 265 | status; |
| 266 | |
| 267 | status=BrightnessContrastImageChannel(image,DefaultChannels,brightness, |
| 268 | contrast); |
| 269 | return(status); |
| 270 | } |
| 271 | |
| 272 | MagickExport MagickBooleanType BrightnessContrastImageChannel(Image *image, |
| 273 | const ChannelType channel,const double brightness,const double contrast) |
| 274 | { |
| 275 | #define BrightnessContastImageTag "BrightnessContast/Image" |
| 276 | |
| 277 | double |
| 278 | alpha, |
| 279 | intercept, |
| 280 | coefficients[2], |
| 281 | slope; |
| 282 | |
| 283 | MagickBooleanType |
| 284 | status; |
| 285 | |
| 286 | /* |
| 287 | Compute slope and intercept. |
| 288 | */ |
| 289 | assert(image != (Image *) NULL); |
| 290 | assert(image->signature == MagickSignature); |
| 291 | if (image->debug != MagickFalse) |
| 292 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 293 | alpha=contrast; |
cristy | 4205a3c | 2010-09-12 20:19:59 +0000 | [diff] [blame] | 294 | slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0)); |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 295 | if (slope < 0.0) |
| 296 | slope=0.0; |
| 297 | intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope); |
| 298 | coefficients[0]=slope; |
| 299 | coefficients[1]=intercept; |
| 300 | status=FunctionImageChannel(image,channel,PolynomialFunction,2,coefficients, |
| 301 | &image->exception); |
| 302 | return(status); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 307 | % % |
| 308 | % % |
| 309 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 310 | % C o l o r D e c i s i o n L i s t I m a g e % |
| 311 | % % |
| 312 | % % |
| 313 | % % |
| 314 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 315 | % |
| 316 | % ColorDecisionListImage() accepts a lightweight Color Correction Collection |
| 317 | % (CCC) file which solely contains one or more color corrections and applies |
| 318 | % the correction to the image. Here is a sample CCC file: |
| 319 | % |
| 320 | % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2"> |
| 321 | % <ColorCorrection id="cc03345"> |
| 322 | % <SOPNode> |
| 323 | % <Slope> 0.9 1.2 0.5 </Slope> |
| 324 | % <Offset> 0.4 -0.5 0.6 </Offset> |
| 325 | % <Power> 1.0 0.8 1.5 </Power> |
| 326 | % </SOPNode> |
| 327 | % <SATNode> |
| 328 | % <Saturation> 0.85 </Saturation> |
| 329 | % </SATNode> |
| 330 | % </ColorCorrection> |
| 331 | % </ColorCorrectionCollection> |
| 332 | % |
| 333 | % which includes the slop, offset, and power for each of the RGB channels |
| 334 | % as well as the saturation. |
| 335 | % |
| 336 | % The format of the ColorDecisionListImage method is: |
| 337 | % |
| 338 | % MagickBooleanType ColorDecisionListImage(Image *image, |
| 339 | % const char *color_correction_collection) |
| 340 | % |
| 341 | % A description of each parameter follows: |
| 342 | % |
| 343 | % o image: the image. |
| 344 | % |
| 345 | % o color_correction_collection: the color correction collection in XML. |
| 346 | % |
| 347 | */ |
| 348 | MagickExport MagickBooleanType ColorDecisionListImage(Image *image, |
| 349 | const char *color_correction_collection) |
| 350 | { |
| 351 | #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image" |
| 352 | |
| 353 | typedef struct _Correction |
| 354 | { |
| 355 | double |
| 356 | slope, |
| 357 | offset, |
| 358 | power; |
| 359 | } Correction; |
| 360 | |
| 361 | typedef struct _ColorCorrection |
| 362 | { |
| 363 | Correction |
| 364 | red, |
| 365 | green, |
| 366 | blue; |
| 367 | |
| 368 | double |
| 369 | saturation; |
| 370 | } ColorCorrection; |
| 371 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 372 | CacheView |
| 373 | *image_view; |
| 374 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 375 | char |
| 376 | token[MaxTextExtent]; |
| 377 | |
| 378 | ColorCorrection |
| 379 | color_correction; |
| 380 | |
| 381 | const char |
| 382 | *content, |
| 383 | *p; |
| 384 | |
| 385 | ExceptionInfo |
| 386 | *exception; |
| 387 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 388 | MagickBooleanType |
| 389 | status; |
| 390 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 391 | MagickOffsetType |
| 392 | progress; |
| 393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 394 | PixelPacket |
| 395 | *cdl_map; |
| 396 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 397 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 398 | i; |
| 399 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 400 | ssize_t |
| 401 | y; |
| 402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 403 | XMLTreeInfo |
| 404 | *cc, |
| 405 | *ccc, |
| 406 | *sat, |
| 407 | *sop; |
| 408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | /* |
| 410 | Allocate and initialize cdl maps. |
| 411 | */ |
| 412 | assert(image != (Image *) NULL); |
| 413 | assert(image->signature == MagickSignature); |
| 414 | if (image->debug != MagickFalse) |
| 415 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 416 | if (color_correction_collection == (const char *) NULL) |
| 417 | return(MagickFalse); |
| 418 | ccc=NewXMLTree((const char *) color_correction_collection,&image->exception); |
| 419 | if (ccc == (XMLTreeInfo *) NULL) |
| 420 | return(MagickFalse); |
| 421 | cc=GetXMLTreeChild(ccc,"ColorCorrection"); |
| 422 | if (cc == (XMLTreeInfo *) NULL) |
| 423 | { |
| 424 | ccc=DestroyXMLTree(ccc); |
| 425 | return(MagickFalse); |
| 426 | } |
| 427 | color_correction.red.slope=1.0; |
| 428 | color_correction.red.offset=0.0; |
| 429 | color_correction.red.power=1.0; |
| 430 | color_correction.green.slope=1.0; |
| 431 | color_correction.green.offset=0.0; |
| 432 | color_correction.green.power=1.0; |
| 433 | color_correction.blue.slope=1.0; |
| 434 | color_correction.blue.offset=0.0; |
| 435 | color_correction.blue.power=1.0; |
| 436 | color_correction.saturation=0.0; |
| 437 | sop=GetXMLTreeChild(cc,"SOPNode"); |
| 438 | if (sop != (XMLTreeInfo *) NULL) |
| 439 | { |
| 440 | XMLTreeInfo |
| 441 | *offset, |
| 442 | *power, |
| 443 | *slope; |
| 444 | |
| 445 | slope=GetXMLTreeChild(sop,"Slope"); |
| 446 | if (slope != (XMLTreeInfo *) NULL) |
| 447 | { |
| 448 | content=GetXMLTreeContent(slope); |
| 449 | p=(const char *) content; |
| 450 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 451 | { |
| 452 | GetMagickToken(p,&p,token); |
| 453 | if (*token == ',') |
| 454 | GetMagickToken(p,&p,token); |
| 455 | switch (i) |
| 456 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 457 | case 0: color_correction.red.slope=StringToDouble(token); break; |
| 458 | case 1: color_correction.green.slope=StringToDouble(token); break; |
| 459 | case 2: color_correction.blue.slope=StringToDouble(token); break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | offset=GetXMLTreeChild(sop,"Offset"); |
| 464 | if (offset != (XMLTreeInfo *) NULL) |
| 465 | { |
| 466 | content=GetXMLTreeContent(offset); |
| 467 | p=(const char *) content; |
| 468 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 469 | { |
| 470 | GetMagickToken(p,&p,token); |
| 471 | if (*token == ',') |
| 472 | GetMagickToken(p,&p,token); |
| 473 | switch (i) |
| 474 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 475 | case 0: color_correction.red.offset=StringToDouble(token); break; |
| 476 | case 1: color_correction.green.offset=StringToDouble(token); break; |
| 477 | case 2: color_correction.blue.offset=StringToDouble(token); break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
| 481 | power=GetXMLTreeChild(sop,"Power"); |
| 482 | if (power != (XMLTreeInfo *) NULL) |
| 483 | { |
| 484 | content=GetXMLTreeContent(power); |
| 485 | p=(const char *) content; |
| 486 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 487 | { |
| 488 | GetMagickToken(p,&p,token); |
| 489 | if (*token == ',') |
| 490 | GetMagickToken(p,&p,token); |
| 491 | switch (i) |
| 492 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 493 | case 0: color_correction.red.power=StringToDouble(token); break; |
| 494 | case 1: color_correction.green.power=StringToDouble(token); break; |
| 495 | case 2: color_correction.blue.power=StringToDouble(token); break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | sat=GetXMLTreeChild(cc,"SATNode"); |
| 501 | if (sat != (XMLTreeInfo *) NULL) |
| 502 | { |
| 503 | XMLTreeInfo |
| 504 | *saturation; |
| 505 | |
| 506 | saturation=GetXMLTreeChild(sat,"Saturation"); |
| 507 | if (saturation != (XMLTreeInfo *) NULL) |
| 508 | { |
| 509 | content=GetXMLTreeContent(saturation); |
| 510 | p=(const char *) content; |
| 511 | GetMagickToken(p,&p,token); |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 512 | color_correction.saturation=StringToDouble(token); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | ccc=DestroyXMLTree(ccc); |
| 516 | if (image->debug != MagickFalse) |
| 517 | { |
| 518 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
| 519 | " Color Correction Collection:"); |
| 520 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 521 | " color_correction.red.slope: %g",color_correction.red.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 522 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 523 | " color_correction.red.offset: %g",color_correction.red.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 525 | " color_correction.red.power: %g",color_correction.red.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 527 | " color_correction.green.slope: %g",color_correction.green.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 528 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 529 | " color_correction.green.offset: %g",color_correction.green.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 531 | " color_correction.green.power: %g",color_correction.green.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 532 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 533 | " color_correction.blue.slope: %g",color_correction.blue.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 534 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 535 | " color_correction.blue.offset: %g",color_correction.blue.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 537 | " color_correction.blue.power: %g",color_correction.blue.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 538 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 539 | " color_correction.saturation: %g",color_correction.saturation); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | } |
| 541 | cdl_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map)); |
| 542 | if (cdl_map == (PixelPacket *) NULL) |
| 543 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 544 | image->filename); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 545 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 546 | #pragma omp parallel for schedule(dynamic,4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 547 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 548 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 549 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 550 | cdl_map[i].red=ClampToQuantum((MagickRealType) ScaleMapToQuantum(( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+ |
| 552 | color_correction.red.offset,color_correction.red.power))))); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 553 | cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum(( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+ |
| 555 | color_correction.green.offset,color_correction.green.power))))); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 556 | cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum(( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 557 | MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+ |
| 558 | color_correction.blue.offset,color_correction.blue.power))))); |
| 559 | } |
| 560 | if (image->storage_class == PseudoClass) |
| 561 | { |
| 562 | /* |
| 563 | Apply transfer function to colormap. |
| 564 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 565 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 566 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 568 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | { |
| 570 | double |
| 571 | luma; |
| 572 | |
| 573 | luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+ |
| 574 | 0.0722*image->colormap[i].blue; |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 575 | image->colormap[i].red=ClampToQuantum(luma+color_correction.saturation* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 576 | cdl_map[ScaleQuantumToMap(image->colormap[i].red)].red-luma); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 577 | image->colormap[i].green=ClampToQuantum(luma+ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | color_correction.saturation*cdl_map[ScaleQuantumToMap( |
| 579 | image->colormap[i].green)].green-luma); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 580 | image->colormap[i].blue=ClampToQuantum(luma+color_correction.saturation* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 581 | cdl_map[ScaleQuantumToMap(image->colormap[i].blue)].blue-luma); |
| 582 | } |
| 583 | } |
| 584 | /* |
| 585 | Apply transfer function to image. |
| 586 | */ |
| 587 | status=MagickTrue; |
| 588 | progress=0; |
| 589 | exception=(&image->exception); |
| 590 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 591 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 592 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 593 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 594 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 595 | { |
| 596 | double |
| 597 | luma; |
| 598 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 600 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 601 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 602 | register ssize_t |
| 603 | x; |
| 604 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | if (status == MagickFalse) |
| 606 | continue; |
| 607 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 608 | if (q == (PixelPacket *) NULL) |
| 609 | { |
| 610 | status=MagickFalse; |
| 611 | continue; |
| 612 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 613 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | { |
| 615 | luma=0.2126*q->red+0.7152*q->green+0.0722*q->blue; |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 616 | q->red=ClampToQuantum(luma+color_correction.saturation* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | (cdl_map[ScaleQuantumToMap(q->red)].red-luma)); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 618 | q->green=ClampToQuantum(luma+color_correction.saturation* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 619 | (cdl_map[ScaleQuantumToMap(q->green)].green-luma)); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 620 | q->blue=ClampToQuantum(luma+color_correction.saturation* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 621 | (cdl_map[ScaleQuantumToMap(q->blue)].blue-luma)); |
| 622 | q++; |
| 623 | } |
| 624 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 625 | status=MagickFalse; |
| 626 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 627 | { |
| 628 | MagickBooleanType |
| 629 | proceed; |
| 630 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 631 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 632 | #pragma omp critical (MagickCore_ColorDecisionListImageChannel) |
| 633 | #endif |
| 634 | proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag, |
| 635 | progress++,image->rows); |
| 636 | if (proceed == MagickFalse) |
| 637 | status=MagickFalse; |
| 638 | } |
| 639 | } |
| 640 | image_view=DestroyCacheView(image_view); |
| 641 | cdl_map=(PixelPacket *) RelinquishMagickMemory(cdl_map); |
| 642 | return(status); |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 647 | % % |
| 648 | % % |
| 649 | % % |
| 650 | % C l u t I m a g e % |
| 651 | % % |
| 652 | % % |
| 653 | % % |
| 654 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 655 | % |
| 656 | % ClutImage() replaces each color value in the given image, by using it as an |
| 657 | % index to lookup a replacement color value in a Color Look UP Table in the |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 658 | % form of an image. The values are extracted along a diagonal of the CLUT |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 659 | % image so either a horizontal or vertial gradient image can be used. |
| 660 | % |
| 661 | % Typically this is used to either re-color a gray-scale image according to a |
| 662 | % color gradient in the CLUT image, or to perform a freeform histogram |
| 663 | % (level) adjustment according to the (typically gray-scale) gradient in the |
| 664 | % CLUT image. |
| 665 | % |
| 666 | % When the 'channel' mask includes the matte/alpha transparency channel but |
| 667 | % one image has no such channel it is assumed that that image is a simple |
| 668 | % gray-scale image that will effect the alpha channel values, either for |
| 669 | % gray-scale coloring (with transparent or semi-transparent colors), or |
| 670 | % a histogram adjustment of existing alpha channel values. If both images |
| 671 | % have matte channels, direct and normal indexing is applied, which is rarely |
| 672 | % used. |
| 673 | % |
| 674 | % The format of the ClutImage method is: |
| 675 | % |
| 676 | % MagickBooleanType ClutImage(Image *image,Image *clut_image) |
| 677 | % MagickBooleanType ClutImageChannel(Image *image, |
| 678 | % const ChannelType channel,Image *clut_image) |
| 679 | % |
| 680 | % A description of each parameter follows: |
| 681 | % |
| 682 | % o image: the image, which is replaced by indexed CLUT values |
| 683 | % |
| 684 | % o clut_image: the color lookup table image for replacement color values. |
| 685 | % |
| 686 | % o channel: the channel. |
| 687 | % |
| 688 | */ |
| 689 | |
| 690 | MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image) |
| 691 | { |
| 692 | return(ClutImageChannel(image,DefaultChannels,clut_image)); |
| 693 | } |
| 694 | |
| 695 | MagickExport MagickBooleanType ClutImageChannel(Image *image, |
| 696 | const ChannelType channel,const Image *clut_image) |
| 697 | { |
| 698 | #define ClutImageTag "Clut/Image" |
| 699 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 700 | CacheView |
cristy | 708333f | 2011-03-26 01:25:07 +0000 | [diff] [blame^] | 701 | *clut_view, |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 702 | *image_view; |
| 703 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 704 | ExceptionInfo |
| 705 | *exception; |
| 706 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 707 | MagickBooleanType |
| 708 | status; |
| 709 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 710 | MagickOffsetType |
| 711 | progress; |
| 712 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | MagickPixelPacket |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 714 | *clut_map; |
| 715 | |
| 716 | register ssize_t |
| 717 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 718 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 719 | ssize_t |
| 720 | adjust, |
| 721 | y; |
| 722 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | assert(image != (Image *) NULL); |
| 724 | assert(image->signature == MagickSignature); |
| 725 | if (image->debug != MagickFalse) |
| 726 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 727 | assert(clut_image != (Image *) NULL); |
| 728 | assert(clut_image->signature == MagickSignature); |
| 729 | if (SetImageStorageClass(image,DirectClass) == MagickFalse) |
| 730 | return(MagickFalse); |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 731 | clut_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL, |
| 732 | sizeof(*clut_map)); |
| 733 | if (clut_map == (MagickPixelPacket *) NULL) |
| 734 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 735 | image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | /* |
| 737 | Clut image. |
| 738 | */ |
| 739 | status=MagickTrue; |
| 740 | progress=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 741 | adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 742 | exception=(&image->exception); |
cristy | 708333f | 2011-03-26 01:25:07 +0000 | [diff] [blame^] | 743 | clut_view=AcquireCacheView(clut_image); |
cristy | af6bc72 | 2011-03-25 19:16:14 +0000 | [diff] [blame] | 744 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 745 | #pragma omp parallel for schedule(dynamic,4) |
| 746 | #endif |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 747 | for (i=0; i <= (ssize_t) MaxMap; i++) |
| 748 | { |
| 749 | GetMagickPixelPacket(clut_image,clut_map+i); |
cristy | 708333f | 2011-03-26 01:25:07 +0000 | [diff] [blame^] | 750 | (void) InterpolatePixelPacket(clut_image,clut_view,clut_image->interpolate, |
cristy | d76c51e | 2011-03-26 00:21:26 +0000 | [diff] [blame] | 751 | QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i* |
| 752 | (clut_image->rows-adjust),clut_map+i,exception); |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 753 | } |
cristy | 708333f | 2011-03-26 01:25:07 +0000 | [diff] [blame^] | 754 | clut_view=DestroyCacheView(clut_view); |
| 755 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 756 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 757 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 758 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 759 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 760 | { |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 761 | MagickPixelPacket |
| 762 | pixel; |
| 763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 764 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 765 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 767 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 768 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 770 | register ssize_t |
| 771 | x; |
| 772 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 773 | if (status == MagickFalse) |
| 774 | continue; |
| 775 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 776 | if (q == (PixelPacket *) NULL) |
| 777 | { |
| 778 | status=MagickFalse; |
| 779 | continue; |
| 780 | } |
| 781 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 782 | GetMagickPixelPacket(image,&pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 783 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 784 | { |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 785 | SetMagickPixelPacket(image,q,indexes+x,&pixel); |
cristy | 2253f17 | 2011-03-24 23:39:51 +0000 | [diff] [blame] | 786 | if ((channel & RedChannel) != 0) |
| 787 | SetRedPixelComponent(q,ClampRedPixelComponent(clut_map+ |
| 788 | ScaleQuantumToMap(q->red))); |
| 789 | if ((channel & GreenChannel) != 0) |
| 790 | SetGreenPixelComponent(q,ClampGreenPixelComponent(clut_map+ |
| 791 | ScaleQuantumToMap(q->green))); |
| 792 | if ((channel & BlueChannel) != 0) |
| 793 | SetBluePixelComponent(q,ClampBluePixelComponent(clut_map+ |
| 794 | ScaleQuantumToMap(q->blue))); |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 795 | if ((channel & OpacityChannel) != 0) |
| 796 | { |
| 797 | if (clut_image->matte == MagickFalse) |
| 798 | q->opacity=(Quantum) (QuantumRange-MagickPixelIntensityToQuantum( |
cristy | d76c51e | 2011-03-26 00:21:26 +0000 | [diff] [blame] | 799 | clut_map+ScaleQuantumToMap((Quantum) GetAlphaPixelComponent(q)))); |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 800 | else |
| 801 | if (image->matte == MagickFalse) |
| 802 | SetOpacityPixelComponent(q,ClampOpacityPixelComponent(clut_map+ |
cristy | d76c51e | 2011-03-26 00:21:26 +0000 | [diff] [blame] | 803 | ScaleQuantumToMap((Quantum) MagickPixelIntensity(&pixel)))); |
cristy | 3635df2 | 2011-03-25 00:16:16 +0000 | [diff] [blame] | 804 | else |
| 805 | SetOpacityPixelComponent(q,ClampOpacityPixelComponent( |
| 806 | clut_map+ScaleQuantumToMap(q->opacity))); |
| 807 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 808 | if (((channel & IndexChannel) != 0) && |
| 809 | (image->colorspace == CMYKColorspace)) |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 810 | indexes[x]=ClampToQuantum((clut_map+(ssize_t) indexes[x])->index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | q++; |
| 812 | } |
| 813 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 814 | status=MagickFalse; |
| 815 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 816 | { |
| 817 | MagickBooleanType |
| 818 | proceed; |
| 819 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 820 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 821 | #pragma omp critical (MagickCore_ClutImageChannel) |
| 822 | #endif |
| 823 | proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows); |
| 824 | if (proceed == MagickFalse) |
| 825 | status=MagickFalse; |
| 826 | } |
| 827 | } |
| 828 | image_view=DestroyCacheView(image_view); |
cristy | 49f3724 | 2011-03-22 18:18:23 +0000 | [diff] [blame] | 829 | clut_map=(MagickPixelPacket *) RelinquishMagickMemory(clut_map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 830 | if ((clut_image->matte != MagickFalse) && ((channel & OpacityChannel) != 0)) |
| 831 | (void) SetImageAlphaChannel(image,ActivateAlphaChannel); |
| 832 | return(status); |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 837 | % % |
| 838 | % % |
| 839 | % % |
| 840 | % C o n t r a s t I m a g e % |
| 841 | % % |
| 842 | % % |
| 843 | % % |
| 844 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 845 | % |
| 846 | % ContrastImage() enhances the intensity differences between the lighter and |
| 847 | % darker elements of the image. Set sharpen to a MagickTrue to increase the |
| 848 | % image contrast otherwise the contrast is reduced. |
| 849 | % |
| 850 | % The format of the ContrastImage method is: |
| 851 | % |
| 852 | % MagickBooleanType ContrastImage(Image *image, |
| 853 | % const MagickBooleanType sharpen) |
| 854 | % |
| 855 | % A description of each parameter follows: |
| 856 | % |
| 857 | % o image: the image. |
| 858 | % |
| 859 | % o sharpen: Increase or decrease image contrast. |
| 860 | % |
| 861 | */ |
| 862 | |
| 863 | static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue) |
| 864 | { |
| 865 | double |
| 866 | brightness, |
| 867 | hue, |
| 868 | saturation; |
| 869 | |
| 870 | /* |
| 871 | Enhance contrast: dark color become darker, light color become lighter. |
| 872 | */ |
| 873 | assert(red != (Quantum *) NULL); |
| 874 | assert(green != (Quantum *) NULL); |
| 875 | assert(blue != (Quantum *) NULL); |
| 876 | hue=0.0; |
| 877 | saturation=0.0; |
| 878 | brightness=0.0; |
| 879 | ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness); |
cristy | 31ac0f0 | 2011-03-12 02:04:47 +0000 | [diff] [blame] | 880 | brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)- |
cristy | 4205a3c | 2010-09-12 20:19:59 +0000 | [diff] [blame] | 881 | brightness); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 882 | if (brightness > 1.0) |
| 883 | brightness=1.0; |
| 884 | else |
| 885 | if (brightness < 0.0) |
| 886 | brightness=0.0; |
| 887 | ConvertHSBToRGB(hue,saturation,brightness,red,green,blue); |
| 888 | } |
| 889 | |
| 890 | MagickExport MagickBooleanType ContrastImage(Image *image, |
| 891 | const MagickBooleanType sharpen) |
| 892 | { |
| 893 | #define ContrastImageTag "Contrast/Image" |
| 894 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 895 | CacheView |
| 896 | *image_view; |
| 897 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 898 | ExceptionInfo |
| 899 | *exception; |
| 900 | |
| 901 | int |
| 902 | sign; |
| 903 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 904 | MagickBooleanType |
| 905 | status; |
| 906 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 907 | MagickOffsetType |
| 908 | progress; |
| 909 | |
| 910 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | i; |
| 912 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 913 | ssize_t |
| 914 | y; |
| 915 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 916 | assert(image != (Image *) NULL); |
| 917 | assert(image->signature == MagickSignature); |
| 918 | if (image->debug != MagickFalse) |
| 919 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 920 | sign=sharpen != MagickFalse ? 1 : -1; |
| 921 | if (image->storage_class == PseudoClass) |
| 922 | { |
| 923 | /* |
| 924 | Contrast enhance colormap. |
| 925 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 926 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | Contrast(sign,&image->colormap[i].red,&image->colormap[i].green, |
| 928 | &image->colormap[i].blue); |
| 929 | } |
| 930 | /* |
| 931 | Contrast enhance image. |
| 932 | */ |
| 933 | status=MagickTrue; |
| 934 | progress=0; |
| 935 | exception=(&image->exception); |
| 936 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 937 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 938 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 940 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 941 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 942 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 943 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 944 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 945 | register ssize_t |
| 946 | x; |
| 947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | if (status == MagickFalse) |
| 949 | continue; |
| 950 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 951 | if (q == (PixelPacket *) NULL) |
| 952 | { |
| 953 | status=MagickFalse; |
| 954 | continue; |
| 955 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 956 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 957 | { |
| 958 | Contrast(sign,&q->red,&q->green,&q->blue); |
| 959 | q++; |
| 960 | } |
| 961 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 962 | status=MagickFalse; |
| 963 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 964 | { |
| 965 | MagickBooleanType |
| 966 | proceed; |
| 967 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 968 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 969 | #pragma omp critical (MagickCore_ContrastImage) |
| 970 | #endif |
| 971 | proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows); |
| 972 | if (proceed == MagickFalse) |
| 973 | status=MagickFalse; |
| 974 | } |
| 975 | } |
| 976 | image_view=DestroyCacheView(image_view); |
| 977 | return(status); |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 982 | % % |
| 983 | % % |
| 984 | % % |
| 985 | % C o n t r a s t S t r e t c h I m a g e % |
| 986 | % % |
| 987 | % % |
| 988 | % % |
| 989 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 990 | % |
| 991 | % The ContrastStretchImage() is a simple image enhancement technique that |
| 992 | % attempts to improve the contrast in an image by `stretching' the range of |
| 993 | % intensity values it contains to span a desired range of values. It differs |
| 994 | % from the more sophisticated histogram equalization in that it can only |
| 995 | % apply % a linear scaling function to the image pixel values. As a result |
| 996 | % the `enhancement' is less harsh. |
| 997 | % |
| 998 | % The format of the ContrastStretchImage method is: |
| 999 | % |
| 1000 | % MagickBooleanType ContrastStretchImage(Image *image, |
| 1001 | % const char *levels) |
| 1002 | % MagickBooleanType ContrastStretchImageChannel(Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1003 | % const size_t channel,const double black_point, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | % const double white_point) |
| 1005 | % |
| 1006 | % A description of each parameter follows: |
| 1007 | % |
| 1008 | % o image: the image. |
| 1009 | % |
| 1010 | % o channel: the channel. |
| 1011 | % |
| 1012 | % o black_point: the black point. |
| 1013 | % |
| 1014 | % o white_point: the white point. |
| 1015 | % |
| 1016 | % o levels: Specify the levels where the black and white points have the |
| 1017 | % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.). |
| 1018 | % |
| 1019 | */ |
| 1020 | |
| 1021 | MagickExport MagickBooleanType ContrastStretchImage(Image *image, |
| 1022 | const char *levels) |
| 1023 | { |
| 1024 | double |
| 1025 | black_point, |
| 1026 | white_point; |
| 1027 | |
| 1028 | GeometryInfo |
| 1029 | geometry_info; |
| 1030 | |
| 1031 | MagickBooleanType |
| 1032 | status; |
| 1033 | |
| 1034 | MagickStatusType |
| 1035 | flags; |
| 1036 | |
| 1037 | /* |
| 1038 | Parse levels. |
| 1039 | */ |
| 1040 | if (levels == (char *) NULL) |
| 1041 | return(MagickFalse); |
| 1042 | flags=ParseGeometry(levels,&geometry_info); |
| 1043 | black_point=geometry_info.rho; |
| 1044 | white_point=(double) image->columns*image->rows; |
| 1045 | if ((flags & SigmaValue) != 0) |
| 1046 | white_point=geometry_info.sigma; |
| 1047 | if ((flags & PercentValue) != 0) |
| 1048 | { |
| 1049 | black_point*=(double) QuantumRange/100.0; |
| 1050 | white_point*=(double) QuantumRange/100.0; |
| 1051 | } |
| 1052 | if ((flags & SigmaValue) == 0) |
| 1053 | white_point=(double) image->columns*image->rows-black_point; |
| 1054 | status=ContrastStretchImageChannel(image,DefaultChannels,black_point, |
| 1055 | white_point); |
| 1056 | return(status); |
| 1057 | } |
| 1058 | |
| 1059 | MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image, |
| 1060 | const ChannelType channel,const double black_point,const double white_point) |
| 1061 | { |
| 1062 | #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color))) |
| 1063 | #define ContrastStretchImageTag "ContrastStretch/Image" |
| 1064 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1065 | CacheView |
| 1066 | *image_view; |
| 1067 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1068 | double |
| 1069 | intensity; |
| 1070 | |
| 1071 | ExceptionInfo |
| 1072 | *exception; |
| 1073 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | MagickBooleanType |
| 1075 | status; |
| 1076 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1077 | MagickOffsetType |
| 1078 | progress; |
| 1079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1080 | MagickPixelPacket |
| 1081 | black, |
| 1082 | *histogram, |
| 1083 | *stretch_map, |
| 1084 | white; |
| 1085 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1086 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1087 | i; |
| 1088 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1089 | ssize_t |
| 1090 | y; |
| 1091 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1092 | /* |
| 1093 | Allocate histogram and stretch map. |
| 1094 | */ |
| 1095 | assert(image != (Image *) NULL); |
| 1096 | assert(image->signature == MagickSignature); |
| 1097 | if (image->debug != MagickFalse) |
| 1098 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1099 | histogram=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL, |
| 1100 | sizeof(*histogram)); |
| 1101 | stretch_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL, |
| 1102 | sizeof(*stretch_map)); |
| 1103 | if ((histogram == (MagickPixelPacket *) NULL) || |
| 1104 | (stretch_map == (MagickPixelPacket *) NULL)) |
| 1105 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1106 | image->filename); |
| 1107 | /* |
| 1108 | Form histogram. |
| 1109 | */ |
| 1110 | status=MagickTrue; |
| 1111 | exception=(&image->exception); |
| 1112 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram)); |
| 1113 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1114 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1115 | { |
| 1116 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1117 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1118 | |
| 1119 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1120 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1121 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1122 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1123 | x; |
| 1124 | |
| 1125 | if (status == MagickFalse) |
| 1126 | continue; |
| 1127 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 1128 | if (p == (const PixelPacket *) NULL) |
| 1129 | { |
| 1130 | status=MagickFalse; |
| 1131 | continue; |
| 1132 | } |
| 1133 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
| 1134 | if (channel == DefaultChannels) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1135 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1136 | { |
| 1137 | Quantum |
| 1138 | intensity; |
| 1139 | |
| 1140 | intensity=PixelIntensityToQuantum(p); |
| 1141 | histogram[ScaleQuantumToMap(intensity)].red++; |
| 1142 | histogram[ScaleQuantumToMap(intensity)].green++; |
| 1143 | histogram[ScaleQuantumToMap(intensity)].blue++; |
| 1144 | histogram[ScaleQuantumToMap(intensity)].index++; |
| 1145 | p++; |
| 1146 | } |
| 1147 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1148 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1149 | { |
| 1150 | if ((channel & RedChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1151 | histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1152 | if ((channel & GreenChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1153 | histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1154 | if ((channel & BlueChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1155 | histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1156 | if ((channel & OpacityChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1157 | histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | if (((channel & IndexChannel) != 0) && |
| 1159 | (image->colorspace == CMYKColorspace)) |
| 1160 | histogram[ScaleQuantumToMap(indexes[x])].index++; |
| 1161 | p++; |
| 1162 | } |
| 1163 | } |
| 1164 | /* |
| 1165 | Find the histogram boundaries by locating the black/white levels. |
| 1166 | */ |
| 1167 | black.red=0.0; |
| 1168 | white.red=MaxRange(QuantumRange); |
| 1169 | if ((channel & RedChannel) != 0) |
| 1170 | { |
| 1171 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1172 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1173 | { |
| 1174 | intensity+=histogram[i].red; |
| 1175 | if (intensity > black_point) |
| 1176 | break; |
| 1177 | } |
| 1178 | black.red=(MagickRealType) i; |
| 1179 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1180 | for (i=(ssize_t) MaxMap; i != 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1181 | { |
| 1182 | intensity+=histogram[i].red; |
| 1183 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1184 | break; |
| 1185 | } |
| 1186 | white.red=(MagickRealType) i; |
| 1187 | } |
| 1188 | black.green=0.0; |
| 1189 | white.green=MaxRange(QuantumRange); |
| 1190 | if ((channel & GreenChannel) != 0) |
| 1191 | { |
| 1192 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1193 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1194 | { |
| 1195 | intensity+=histogram[i].green; |
| 1196 | if (intensity > black_point) |
| 1197 | break; |
| 1198 | } |
| 1199 | black.green=(MagickRealType) i; |
| 1200 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1201 | for (i=(ssize_t) MaxMap; i != 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1202 | { |
| 1203 | intensity+=histogram[i].green; |
| 1204 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1205 | break; |
| 1206 | } |
| 1207 | white.green=(MagickRealType) i; |
| 1208 | } |
| 1209 | black.blue=0.0; |
| 1210 | white.blue=MaxRange(QuantumRange); |
| 1211 | if ((channel & BlueChannel) != 0) |
| 1212 | { |
| 1213 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1214 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1215 | { |
| 1216 | intensity+=histogram[i].blue; |
| 1217 | if (intensity > black_point) |
| 1218 | break; |
| 1219 | } |
| 1220 | black.blue=(MagickRealType) i; |
| 1221 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1222 | for (i=(ssize_t) MaxMap; i != 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | { |
| 1224 | intensity+=histogram[i].blue; |
| 1225 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1226 | break; |
| 1227 | } |
| 1228 | white.blue=(MagickRealType) i; |
| 1229 | } |
| 1230 | black.opacity=0.0; |
| 1231 | white.opacity=MaxRange(QuantumRange); |
| 1232 | if ((channel & OpacityChannel) != 0) |
| 1233 | { |
| 1234 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1235 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1236 | { |
| 1237 | intensity+=histogram[i].opacity; |
| 1238 | if (intensity > black_point) |
| 1239 | break; |
| 1240 | } |
| 1241 | black.opacity=(MagickRealType) i; |
| 1242 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1243 | for (i=(ssize_t) MaxMap; i != 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1244 | { |
| 1245 | intensity+=histogram[i].opacity; |
| 1246 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1247 | break; |
| 1248 | } |
| 1249 | white.opacity=(MagickRealType) i; |
| 1250 | } |
| 1251 | black.index=0.0; |
| 1252 | white.index=MaxRange(QuantumRange); |
| 1253 | if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace)) |
| 1254 | { |
| 1255 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1256 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | { |
| 1258 | intensity+=histogram[i].index; |
| 1259 | if (intensity > black_point) |
| 1260 | break; |
| 1261 | } |
| 1262 | black.index=(MagickRealType) i; |
| 1263 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1264 | for (i=(ssize_t) MaxMap; i != 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1265 | { |
| 1266 | intensity+=histogram[i].index; |
| 1267 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1268 | break; |
| 1269 | } |
| 1270 | white.index=(MagickRealType) i; |
| 1271 | } |
| 1272 | histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram); |
| 1273 | /* |
| 1274 | Stretch the histogram to create the stretched image mapping. |
| 1275 | */ |
| 1276 | (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map)); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1277 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1278 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1279 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1280 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1281 | { |
| 1282 | if ((channel & RedChannel) != 0) |
| 1283 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1284 | if (i < (ssize_t) black.red) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1285 | stretch_map[i].red=0.0; |
| 1286 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1287 | if (i > (ssize_t) white.red) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1288 | stretch_map[i].red=(MagickRealType) QuantumRange; |
| 1289 | else |
| 1290 | if (black.red != white.red) |
| 1291 | stretch_map[i].red=(MagickRealType) ScaleMapToQuantum( |
| 1292 | (MagickRealType) (MaxMap*(i-black.red)/(white.red-black.red))); |
| 1293 | } |
| 1294 | if ((channel & GreenChannel) != 0) |
| 1295 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1296 | if (i < (ssize_t) black.green) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1297 | stretch_map[i].green=0.0; |
| 1298 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1299 | if (i > (ssize_t) white.green) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1300 | stretch_map[i].green=(MagickRealType) QuantumRange; |
| 1301 | else |
| 1302 | if (black.green != white.green) |
| 1303 | stretch_map[i].green=(MagickRealType) ScaleMapToQuantum( |
| 1304 | (MagickRealType) (MaxMap*(i-black.green)/(white.green- |
| 1305 | black.green))); |
| 1306 | } |
| 1307 | if ((channel & BlueChannel) != 0) |
| 1308 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1309 | if (i < (ssize_t) black.blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1310 | stretch_map[i].blue=0.0; |
| 1311 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1312 | if (i > (ssize_t) white.blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1313 | stretch_map[i].blue=(MagickRealType) QuantumRange; |
| 1314 | else |
| 1315 | if (black.blue != white.blue) |
| 1316 | stretch_map[i].blue=(MagickRealType) ScaleMapToQuantum( |
| 1317 | (MagickRealType) (MaxMap*(i-black.blue)/(white.blue- |
| 1318 | black.blue))); |
| 1319 | } |
| 1320 | if ((channel & OpacityChannel) != 0) |
| 1321 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1322 | if (i < (ssize_t) black.opacity) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1323 | stretch_map[i].opacity=0.0; |
| 1324 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1325 | if (i > (ssize_t) white.opacity) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1326 | stretch_map[i].opacity=(MagickRealType) QuantumRange; |
| 1327 | else |
| 1328 | if (black.opacity != white.opacity) |
| 1329 | stretch_map[i].opacity=(MagickRealType) ScaleMapToQuantum( |
| 1330 | (MagickRealType) (MaxMap*(i-black.opacity)/(white.opacity- |
| 1331 | black.opacity))); |
| 1332 | } |
| 1333 | if (((channel & IndexChannel) != 0) && |
| 1334 | (image->colorspace == CMYKColorspace)) |
| 1335 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1336 | if (i < (ssize_t) black.index) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1337 | stretch_map[i].index=0.0; |
| 1338 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1339 | if (i > (ssize_t) white.index) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1340 | stretch_map[i].index=(MagickRealType) QuantumRange; |
| 1341 | else |
| 1342 | if (black.index != white.index) |
| 1343 | stretch_map[i].index=(MagickRealType) ScaleMapToQuantum( |
| 1344 | (MagickRealType) (MaxMap*(i-black.index)/(white.index- |
| 1345 | black.index))); |
| 1346 | } |
| 1347 | } |
| 1348 | /* |
| 1349 | Stretch the image. |
| 1350 | */ |
| 1351 | if (((channel & OpacityChannel) != 0) || (((channel & IndexChannel) != 0) && |
| 1352 | (image->colorspace == CMYKColorspace))) |
| 1353 | image->storage_class=DirectClass; |
| 1354 | if (image->storage_class == PseudoClass) |
| 1355 | { |
| 1356 | /* |
| 1357 | Stretch colormap. |
| 1358 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1359 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1360 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1361 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1362 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | { |
| 1364 | if ((channel & RedChannel) != 0) |
| 1365 | { |
| 1366 | if (black.red != white.red) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1367 | image->colormap[i].red=ClampToQuantum(stretch_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1368 | ScaleQuantumToMap(image->colormap[i].red)].red); |
| 1369 | } |
| 1370 | if ((channel & GreenChannel) != 0) |
| 1371 | { |
| 1372 | if (black.green != white.green) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1373 | image->colormap[i].green=ClampToQuantum(stretch_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1374 | ScaleQuantumToMap(image->colormap[i].green)].green); |
| 1375 | } |
| 1376 | if ((channel & BlueChannel) != 0) |
| 1377 | { |
| 1378 | if (black.blue != white.blue) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1379 | image->colormap[i].blue=ClampToQuantum(stretch_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1380 | ScaleQuantumToMap(image->colormap[i].blue)].blue); |
| 1381 | } |
| 1382 | if ((channel & OpacityChannel) != 0) |
| 1383 | { |
| 1384 | if (black.opacity != white.opacity) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1385 | image->colormap[i].opacity=ClampToQuantum(stretch_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1386 | ScaleQuantumToMap(image->colormap[i].opacity)].opacity); |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | /* |
| 1391 | Stretch image. |
| 1392 | */ |
| 1393 | status=MagickTrue; |
| 1394 | progress=0; |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1395 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1396 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1397 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1398 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1399 | { |
| 1400 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1401 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1403 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1404 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1405 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1406 | register ssize_t |
| 1407 | x; |
| 1408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1409 | if (status == MagickFalse) |
| 1410 | continue; |
| 1411 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 1412 | if (q == (PixelPacket *) NULL) |
| 1413 | { |
| 1414 | status=MagickFalse; |
| 1415 | continue; |
| 1416 | } |
| 1417 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1418 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1419 | { |
| 1420 | if ((channel & RedChannel) != 0) |
| 1421 | { |
| 1422 | if (black.red != white.red) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1423 | q->red=ClampToQuantum(stretch_map[ScaleQuantumToMap(q->red)].red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1424 | } |
| 1425 | if ((channel & GreenChannel) != 0) |
| 1426 | { |
| 1427 | if (black.green != white.green) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1428 | q->green=ClampToQuantum(stretch_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1429 | q->green)].green); |
| 1430 | } |
| 1431 | if ((channel & BlueChannel) != 0) |
| 1432 | { |
| 1433 | if (black.blue != white.blue) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1434 | q->blue=ClampToQuantum(stretch_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1435 | q->blue)].blue); |
| 1436 | } |
| 1437 | if ((channel & OpacityChannel) != 0) |
| 1438 | { |
| 1439 | if (black.opacity != white.opacity) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1440 | q->opacity=ClampToQuantum(stretch_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1441 | q->opacity)].opacity); |
| 1442 | } |
| 1443 | if (((channel & IndexChannel) != 0) && |
| 1444 | (image->colorspace == CMYKColorspace)) |
| 1445 | { |
| 1446 | if (black.index != white.index) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1447 | indexes[x]=(IndexPacket) ClampToQuantum(stretch_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1448 | ScaleQuantumToMap(indexes[x])].index); |
| 1449 | } |
| 1450 | q++; |
| 1451 | } |
| 1452 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1453 | status=MagickFalse; |
| 1454 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1455 | { |
| 1456 | MagickBooleanType |
| 1457 | proceed; |
| 1458 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1459 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1460 | #pragma omp critical (MagickCore_ContrastStretchImageChannel) |
| 1461 | #endif |
| 1462 | proceed=SetImageProgress(image,ContrastStretchImageTag,progress++, |
| 1463 | image->rows); |
| 1464 | if (proceed == MagickFalse) |
| 1465 | status=MagickFalse; |
| 1466 | } |
| 1467 | } |
| 1468 | image_view=DestroyCacheView(image_view); |
| 1469 | stretch_map=(MagickPixelPacket *) RelinquishMagickMemory(stretch_map); |
| 1470 | return(status); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1475 | % % |
| 1476 | % % |
| 1477 | % % |
| 1478 | % E n h a n c e I m a g e % |
| 1479 | % % |
| 1480 | % % |
| 1481 | % % |
| 1482 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1483 | % |
| 1484 | % EnhanceImage() applies a digital filter that improves the quality of a |
| 1485 | % noisy image. |
| 1486 | % |
| 1487 | % The format of the EnhanceImage method is: |
| 1488 | % |
| 1489 | % Image *EnhanceImage(const Image *image,ExceptionInfo *exception) |
| 1490 | % |
| 1491 | % A description of each parameter follows: |
| 1492 | % |
| 1493 | % o image: the image. |
| 1494 | % |
| 1495 | % o exception: return any errors or warnings in this structure. |
| 1496 | % |
| 1497 | */ |
| 1498 | MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception) |
| 1499 | { |
| 1500 | #define Enhance(weight) \ |
| 1501 | mean=((MagickRealType) r->red+pixel.red)/2; \ |
| 1502 | distance=(MagickRealType) r->red-(MagickRealType) pixel.red; \ |
| 1503 | distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \ |
| 1504 | mean)*distance*distance; \ |
| 1505 | mean=((MagickRealType) r->green+pixel.green)/2; \ |
| 1506 | distance=(MagickRealType) r->green-(MagickRealType) pixel.green; \ |
| 1507 | distance_squared+=4.0*distance*distance; \ |
| 1508 | mean=((MagickRealType) r->blue+pixel.blue)/2; \ |
| 1509 | distance=(MagickRealType) r->blue-(MagickRealType) pixel.blue; \ |
| 1510 | distance_squared+=QuantumScale*(3.0*((MagickRealType) \ |
| 1511 | QuantumRange+1.0)-1.0-mean)*distance*distance; \ |
| 1512 | mean=((MagickRealType) r->opacity+pixel.opacity)/2; \ |
| 1513 | distance=(MagickRealType) r->opacity-(MagickRealType) pixel.opacity; \ |
| 1514 | distance_squared+=QuantumScale*(3.0*((MagickRealType) \ |
| 1515 | QuantumRange+1.0)-1.0-mean)*distance*distance; \ |
| 1516 | if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \ |
| 1517 | QuantumRange/25.0f)) \ |
| 1518 | { \ |
| 1519 | aggregate.red+=(weight)*r->red; \ |
| 1520 | aggregate.green+=(weight)*r->green; \ |
| 1521 | aggregate.blue+=(weight)*r->blue; \ |
| 1522 | aggregate.opacity+=(weight)*r->opacity; \ |
| 1523 | total_weight+=(weight); \ |
| 1524 | } \ |
| 1525 | r++; |
| 1526 | #define EnhanceImageTag "Enhance/Image" |
| 1527 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1528 | CacheView |
| 1529 | *enhance_view, |
| 1530 | *image_view; |
| 1531 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1532 | Image |
| 1533 | *enhance_image; |
| 1534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1535 | MagickBooleanType |
| 1536 | status; |
| 1537 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1538 | MagickOffsetType |
| 1539 | progress; |
| 1540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1541 | MagickPixelPacket |
| 1542 | zero; |
| 1543 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1544 | ssize_t |
| 1545 | y; |
| 1546 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1547 | /* |
| 1548 | Initialize enhanced image attributes. |
| 1549 | */ |
| 1550 | assert(image != (const Image *) NULL); |
| 1551 | assert(image->signature == MagickSignature); |
| 1552 | if (image->debug != MagickFalse) |
| 1553 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1554 | assert(exception != (ExceptionInfo *) NULL); |
| 1555 | assert(exception->signature == MagickSignature); |
| 1556 | if ((image->columns < 5) || (image->rows < 5)) |
| 1557 | return((Image *) NULL); |
| 1558 | enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 1559 | exception); |
| 1560 | if (enhance_image == (Image *) NULL) |
| 1561 | return((Image *) NULL); |
| 1562 | if (SetImageStorageClass(enhance_image,DirectClass) == MagickFalse) |
| 1563 | { |
| 1564 | InheritException(exception,&enhance_image->exception); |
| 1565 | enhance_image=DestroyImage(enhance_image); |
| 1566 | return((Image *) NULL); |
| 1567 | } |
| 1568 | /* |
| 1569 | Enhance image. |
| 1570 | */ |
| 1571 | status=MagickTrue; |
| 1572 | progress=0; |
| 1573 | (void) ResetMagickMemory(&zero,0,sizeof(zero)); |
| 1574 | image_view=AcquireCacheView(image); |
| 1575 | enhance_view=AcquireCacheView(enhance_image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1576 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1577 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1578 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1579 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1580 | { |
| 1581 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1582 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1583 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1584 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1585 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1586 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1587 | register ssize_t |
| 1588 | x; |
| 1589 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1590 | /* |
| 1591 | Read another scan line. |
| 1592 | */ |
| 1593 | if (status == MagickFalse) |
| 1594 | continue; |
| 1595 | p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception); |
| 1596 | q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1, |
| 1597 | exception); |
| 1598 | if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) |
| 1599 | { |
| 1600 | status=MagickFalse; |
| 1601 | continue; |
| 1602 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1603 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1604 | { |
| 1605 | MagickPixelPacket |
| 1606 | aggregate; |
| 1607 | |
| 1608 | MagickRealType |
| 1609 | distance, |
| 1610 | distance_squared, |
| 1611 | mean, |
| 1612 | total_weight; |
| 1613 | |
| 1614 | PixelPacket |
| 1615 | pixel; |
| 1616 | |
| 1617 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1618 | *restrict r; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* |
| 1621 | Compute weighted average of target pixel color components. |
| 1622 | */ |
| 1623 | aggregate=zero; |
| 1624 | total_weight=0.0; |
| 1625 | r=p+2*(image->columns+4)+2; |
| 1626 | pixel=(*r); |
| 1627 | r=p; |
| 1628 | Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0); |
| 1629 | r=p+(image->columns+4); |
| 1630 | Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0); |
| 1631 | r=p+2*(image->columns+4); |
| 1632 | Enhance(10.0); Enhance(40.0); Enhance(80.0); Enhance(40.0); Enhance(10.0); |
| 1633 | r=p+3*(image->columns+4); |
| 1634 | Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0); |
| 1635 | r=p+4*(image->columns+4); |
| 1636 | Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0); |
| 1637 | q->red=(Quantum) ((aggregate.red+(total_weight/2)-1)/total_weight); |
| 1638 | q->green=(Quantum) ((aggregate.green+(total_weight/2)-1)/total_weight); |
| 1639 | q->blue=(Quantum) ((aggregate.blue+(total_weight/2)-1)/total_weight); |
| 1640 | q->opacity=(Quantum) ((aggregate.opacity+(total_weight/2)-1)/ |
| 1641 | total_weight); |
| 1642 | p++; |
| 1643 | q++; |
| 1644 | } |
| 1645 | if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse) |
| 1646 | status=MagickFalse; |
| 1647 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1648 | { |
| 1649 | MagickBooleanType |
| 1650 | proceed; |
| 1651 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1652 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1653 | #pragma omp critical (MagickCore_EnhanceImage) |
| 1654 | #endif |
| 1655 | proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows); |
| 1656 | if (proceed == MagickFalse) |
| 1657 | status=MagickFalse; |
| 1658 | } |
| 1659 | } |
| 1660 | enhance_view=DestroyCacheView(enhance_view); |
| 1661 | image_view=DestroyCacheView(image_view); |
| 1662 | return(enhance_image); |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1667 | % % |
| 1668 | % % |
| 1669 | % % |
| 1670 | % E q u a l i z e I m a g e % |
| 1671 | % % |
| 1672 | % % |
| 1673 | % % |
| 1674 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1675 | % |
| 1676 | % EqualizeImage() applies a histogram equalization to the image. |
| 1677 | % |
| 1678 | % The format of the EqualizeImage method is: |
| 1679 | % |
| 1680 | % MagickBooleanType EqualizeImage(Image *image) |
| 1681 | % MagickBooleanType EqualizeImageChannel(Image *image, |
| 1682 | % const ChannelType channel) |
| 1683 | % |
| 1684 | % A description of each parameter follows: |
| 1685 | % |
| 1686 | % o image: the image. |
| 1687 | % |
| 1688 | % o channel: the channel. |
| 1689 | % |
| 1690 | */ |
| 1691 | |
| 1692 | MagickExport MagickBooleanType EqualizeImage(Image *image) |
| 1693 | { |
| 1694 | return(EqualizeImageChannel(image,DefaultChannels)); |
| 1695 | } |
| 1696 | |
| 1697 | MagickExport MagickBooleanType EqualizeImageChannel(Image *image, |
| 1698 | const ChannelType channel) |
| 1699 | { |
| 1700 | #define EqualizeImageTag "Equalize/Image" |
| 1701 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1702 | CacheView |
| 1703 | *image_view; |
| 1704 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1705 | ExceptionInfo |
| 1706 | *exception; |
| 1707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1708 | MagickBooleanType |
| 1709 | status; |
| 1710 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1711 | MagickOffsetType |
| 1712 | progress; |
| 1713 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1714 | MagickPixelPacket |
| 1715 | black, |
| 1716 | *equalize_map, |
| 1717 | *histogram, |
| 1718 | intensity, |
| 1719 | *map, |
| 1720 | white; |
| 1721 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1722 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1723 | i; |
| 1724 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1725 | ssize_t |
| 1726 | y; |
| 1727 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1728 | /* |
| 1729 | Allocate and initialize histogram arrays. |
| 1730 | */ |
| 1731 | assert(image != (Image *) NULL); |
| 1732 | assert(image->signature == MagickSignature); |
| 1733 | if (image->debug != MagickFalse) |
| 1734 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1735 | equalize_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL, |
| 1736 | sizeof(*equalize_map)); |
| 1737 | histogram=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL, |
| 1738 | sizeof(*histogram)); |
| 1739 | map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map)); |
| 1740 | if ((equalize_map == (MagickPixelPacket *) NULL) || |
| 1741 | (histogram == (MagickPixelPacket *) NULL) || |
| 1742 | (map == (MagickPixelPacket *) NULL)) |
| 1743 | { |
| 1744 | if (map != (MagickPixelPacket *) NULL) |
| 1745 | map=(MagickPixelPacket *) RelinquishMagickMemory(map); |
| 1746 | if (histogram != (MagickPixelPacket *) NULL) |
| 1747 | histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram); |
| 1748 | if (equalize_map != (MagickPixelPacket *) NULL) |
| 1749 | equalize_map=(MagickPixelPacket *) RelinquishMagickMemory(equalize_map); |
| 1750 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1751 | image->filename); |
| 1752 | } |
| 1753 | /* |
| 1754 | Form histogram. |
| 1755 | */ |
| 1756 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram)); |
| 1757 | exception=(&image->exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1758 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1759 | { |
| 1760 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1761 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1762 | |
| 1763 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1764 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1765 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1766 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1767 | x; |
| 1768 | |
| 1769 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 1770 | if (p == (const PixelPacket *) NULL) |
| 1771 | break; |
| 1772 | indexes=GetVirtualIndexQueue(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1773 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1774 | { |
| 1775 | if ((channel & RedChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1776 | histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1777 | if ((channel & GreenChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1778 | histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1779 | if ((channel & BlueChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1780 | histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1781 | if ((channel & OpacityChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1782 | histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1783 | if (((channel & IndexChannel) != 0) && |
| 1784 | (image->colorspace == CMYKColorspace)) |
| 1785 | histogram[ScaleQuantumToMap(indexes[x])].index++; |
| 1786 | p++; |
| 1787 | } |
| 1788 | } |
| 1789 | /* |
| 1790 | Integrate the histogram to get the equalization map. |
| 1791 | */ |
| 1792 | (void) ResetMagickMemory(&intensity,0,sizeof(intensity)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1793 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1794 | { |
| 1795 | if ((channel & RedChannel) != 0) |
| 1796 | intensity.red+=histogram[i].red; |
| 1797 | if ((channel & GreenChannel) != 0) |
| 1798 | intensity.green+=histogram[i].green; |
| 1799 | if ((channel & BlueChannel) != 0) |
| 1800 | intensity.blue+=histogram[i].blue; |
| 1801 | if ((channel & OpacityChannel) != 0) |
| 1802 | intensity.opacity+=histogram[i].opacity; |
| 1803 | if (((channel & IndexChannel) != 0) && |
| 1804 | (image->colorspace == CMYKColorspace)) |
| 1805 | intensity.index+=histogram[i].index; |
| 1806 | map[i]=intensity; |
| 1807 | } |
| 1808 | black=map[0]; |
| 1809 | white=map[(int) MaxMap]; |
| 1810 | (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map)); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1811 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1812 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1813 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1814 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1815 | { |
| 1816 | if (((channel & RedChannel) != 0) && (white.red != black.red)) |
| 1817 | equalize_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 1818 | ((MaxMap*(map[i].red-black.red))/(white.red-black.red))); |
| 1819 | if (((channel & GreenChannel) != 0) && (white.green != black.green)) |
| 1820 | equalize_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 1821 | ((MaxMap*(map[i].green-black.green))/(white.green-black.green))); |
| 1822 | if (((channel & BlueChannel) != 0) && (white.blue != black.blue)) |
| 1823 | equalize_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 1824 | ((MaxMap*(map[i].blue-black.blue))/(white.blue-black.blue))); |
| 1825 | if (((channel & OpacityChannel) != 0) && (white.opacity != black.opacity)) |
| 1826 | equalize_map[i].opacity=(MagickRealType) ScaleMapToQuantum( |
| 1827 | (MagickRealType) ((MaxMap*(map[i].opacity-black.opacity))/ |
| 1828 | (white.opacity-black.opacity))); |
| 1829 | if ((((channel & IndexChannel) != 0) && |
| 1830 | (image->colorspace == CMYKColorspace)) && |
| 1831 | (white.index != black.index)) |
| 1832 | equalize_map[i].index=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 1833 | ((MaxMap*(map[i].index-black.index))/(white.index-black.index))); |
| 1834 | } |
| 1835 | histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram); |
| 1836 | map=(MagickPixelPacket *) RelinquishMagickMemory(map); |
| 1837 | if (image->storage_class == PseudoClass) |
| 1838 | { |
| 1839 | /* |
| 1840 | Equalize colormap. |
| 1841 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1842 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1843 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1844 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1845 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1846 | { |
| 1847 | if (((channel & RedChannel) != 0) && (white.red != black.red)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1848 | image->colormap[i].red=ClampToQuantum(equalize_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1849 | ScaleQuantumToMap(image->colormap[i].red)].red); |
| 1850 | if (((channel & GreenChannel) != 0) && (white.green != black.green)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1851 | image->colormap[i].green=ClampToQuantum(equalize_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1852 | ScaleQuantumToMap(image->colormap[i].green)].green); |
| 1853 | if (((channel & BlueChannel) != 0) && (white.blue != black.blue)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1854 | image->colormap[i].blue=ClampToQuantum(equalize_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1855 | ScaleQuantumToMap(image->colormap[i].blue)].blue); |
| 1856 | if (((channel & OpacityChannel) != 0) && |
| 1857 | (white.opacity != black.opacity)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1858 | image->colormap[i].opacity=ClampToQuantum(equalize_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1859 | ScaleQuantumToMap(image->colormap[i].opacity)].opacity); |
| 1860 | } |
| 1861 | } |
| 1862 | /* |
| 1863 | Equalize image. |
| 1864 | */ |
| 1865 | status=MagickTrue; |
| 1866 | progress=0; |
| 1867 | exception=(&image->exception); |
| 1868 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1869 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1870 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1871 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1872 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1873 | { |
| 1874 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1875 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1876 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1877 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1878 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1880 | register ssize_t |
| 1881 | x; |
| 1882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1883 | if (status == MagickFalse) |
| 1884 | continue; |
| 1885 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 1886 | if (q == (PixelPacket *) NULL) |
| 1887 | { |
| 1888 | status=MagickFalse; |
| 1889 | continue; |
| 1890 | } |
| 1891 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1892 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1893 | { |
| 1894 | if (((channel & RedChannel) != 0) && (white.red != black.red)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1895 | q->red=ClampToQuantum(equalize_map[ScaleQuantumToMap(q->red)].red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1896 | if (((channel & GreenChannel) != 0) && (white.green != black.green)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1897 | q->green=ClampToQuantum(equalize_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1898 | q->green)].green); |
| 1899 | if (((channel & BlueChannel) != 0) && (white.blue != black.blue)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1900 | q->blue=ClampToQuantum(equalize_map[ScaleQuantumToMap(q->blue)].blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1901 | if (((channel & OpacityChannel) != 0) && (white.opacity != black.opacity)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1902 | q->opacity=ClampToQuantum(equalize_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1903 | q->opacity)].opacity); |
| 1904 | if ((((channel & IndexChannel) != 0) && |
| 1905 | (image->colorspace == CMYKColorspace)) && |
| 1906 | (white.index != black.index)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1907 | indexes[x]=ClampToQuantum(equalize_map[ScaleQuantumToMap( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1908 | indexes[x])].index); |
| 1909 | q++; |
| 1910 | } |
| 1911 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1912 | status=MagickFalse; |
| 1913 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1914 | { |
| 1915 | MagickBooleanType |
| 1916 | proceed; |
| 1917 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1918 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1919 | #pragma omp critical (MagickCore_EqualizeImageChannel) |
| 1920 | #endif |
| 1921 | proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows); |
| 1922 | if (proceed == MagickFalse) |
| 1923 | status=MagickFalse; |
| 1924 | } |
| 1925 | } |
| 1926 | image_view=DestroyCacheView(image_view); |
| 1927 | equalize_map=(MagickPixelPacket *) RelinquishMagickMemory(equalize_map); |
| 1928 | return(status); |
| 1929 | } |
| 1930 | |
| 1931 | /* |
| 1932 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1933 | % % |
| 1934 | % % |
| 1935 | % % |
| 1936 | % G a m m a I m a g e % |
| 1937 | % % |
| 1938 | % % |
| 1939 | % % |
| 1940 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1941 | % |
| 1942 | % GammaImage() gamma-corrects a particular image channel. The same |
| 1943 | % image viewed on different devices will have perceptual differences in the |
| 1944 | % way the image's intensities are represented on the screen. Specify |
| 1945 | % individual gamma levels for the red, green, and blue channels, or adjust |
| 1946 | % all three with the gamma parameter. Values typically range from 0.8 to 2.3. |
| 1947 | % |
| 1948 | % You can also reduce the influence of a particular channel with a gamma |
| 1949 | % value of 0. |
| 1950 | % |
| 1951 | % The format of the GammaImage method is: |
| 1952 | % |
cristy | a636014 | 2011-03-23 23:08:04 +0000 | [diff] [blame] | 1953 | % MagickBooleanType GammaImage(Image *image,const char *level) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1954 | % MagickBooleanType GammaImageChannel(Image *image, |
| 1955 | % const ChannelType channel,const double gamma) |
| 1956 | % |
| 1957 | % A description of each parameter follows: |
| 1958 | % |
| 1959 | % o image: the image. |
| 1960 | % |
| 1961 | % o channel: the channel. |
| 1962 | % |
cristy | a636014 | 2011-03-23 23:08:04 +0000 | [diff] [blame] | 1963 | % o level: the image gamma as a string (e.g. 1.6,1.2,1.0). |
| 1964 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1965 | % o gamma: the image gamma. |
| 1966 | % |
| 1967 | */ |
| 1968 | MagickExport MagickBooleanType GammaImage(Image *image,const char *level) |
| 1969 | { |
| 1970 | GeometryInfo |
| 1971 | geometry_info; |
| 1972 | |
| 1973 | MagickPixelPacket |
| 1974 | gamma; |
| 1975 | |
| 1976 | MagickStatusType |
| 1977 | flags, |
| 1978 | status; |
| 1979 | |
| 1980 | assert(image != (Image *) NULL); |
| 1981 | assert(image->signature == MagickSignature); |
| 1982 | if (image->debug != MagickFalse) |
| 1983 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1984 | if (level == (char *) NULL) |
| 1985 | return(MagickFalse); |
| 1986 | flags=ParseGeometry(level,&geometry_info); |
| 1987 | gamma.red=geometry_info.rho; |
| 1988 | gamma.green=geometry_info.sigma; |
| 1989 | if ((flags & SigmaValue) == 0) |
| 1990 | gamma.green=gamma.red; |
| 1991 | gamma.blue=geometry_info.xi; |
| 1992 | if ((flags & XiValue) == 0) |
| 1993 | gamma.blue=gamma.red; |
| 1994 | if ((gamma.red == 1.0) && (gamma.green == 1.0) && (gamma.blue == 1.0)) |
| 1995 | return(MagickTrue); |
| 1996 | if ((gamma.red == gamma.green) && (gamma.green == gamma.blue)) |
| 1997 | status=GammaImageChannel(image,(const ChannelType) (RedChannel | |
| 1998 | GreenChannel | BlueChannel),(double) gamma.red); |
| 1999 | else |
| 2000 | { |
| 2001 | status=GammaImageChannel(image,RedChannel,(double) gamma.red); |
| 2002 | status|=GammaImageChannel(image,GreenChannel,(double) gamma.green); |
| 2003 | status|=GammaImageChannel(image,BlueChannel,(double) gamma.blue); |
| 2004 | } |
| 2005 | return(status != 0 ? MagickTrue : MagickFalse); |
| 2006 | } |
| 2007 | |
| 2008 | MagickExport MagickBooleanType GammaImageChannel(Image *image, |
| 2009 | const ChannelType channel,const double gamma) |
| 2010 | { |
| 2011 | #define GammaCorrectImageTag "GammaCorrect/Image" |
| 2012 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2013 | CacheView |
| 2014 | *image_view; |
| 2015 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2016 | ExceptionInfo |
| 2017 | *exception; |
| 2018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2019 | MagickBooleanType |
| 2020 | status; |
| 2021 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2022 | MagickOffsetType |
| 2023 | progress; |
| 2024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2025 | Quantum |
| 2026 | *gamma_map; |
| 2027 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2028 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2029 | i; |
| 2030 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2031 | ssize_t |
| 2032 | y; |
| 2033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2034 | /* |
| 2035 | Allocate and initialize gamma maps. |
| 2036 | */ |
| 2037 | assert(image != (Image *) NULL); |
| 2038 | assert(image->signature == MagickSignature); |
| 2039 | if (image->debug != MagickFalse) |
| 2040 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2041 | if (gamma == 1.0) |
| 2042 | return(MagickTrue); |
| 2043 | gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map)); |
| 2044 | if (gamma_map == (Quantum *) NULL) |
| 2045 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2046 | image->filename); |
| 2047 | (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map)); |
| 2048 | if (gamma != 0.0) |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2049 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2050 | #pragma omp parallel for schedule(dynamic,4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2051 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2052 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2053 | gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum(( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2054 | MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma)))); |
| 2055 | if (image->storage_class == PseudoClass) |
| 2056 | { |
| 2057 | /* |
| 2058 | Gamma-correct colormap. |
| 2059 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2060 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2061 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2062 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2063 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2064 | { |
| 2065 | if ((channel & RedChannel) != 0) |
| 2066 | image->colormap[i].red=gamma_map[ |
| 2067 | ScaleQuantumToMap(image->colormap[i].red)]; |
| 2068 | if ((channel & GreenChannel) != 0) |
| 2069 | image->colormap[i].green=gamma_map[ |
| 2070 | ScaleQuantumToMap(image->colormap[i].green)]; |
| 2071 | if ((channel & BlueChannel) != 0) |
| 2072 | image->colormap[i].blue=gamma_map[ |
| 2073 | ScaleQuantumToMap(image->colormap[i].blue)]; |
| 2074 | if ((channel & OpacityChannel) != 0) |
| 2075 | { |
| 2076 | if (image->matte == MagickFalse) |
| 2077 | image->colormap[i].opacity=gamma_map[ |
| 2078 | ScaleQuantumToMap(image->colormap[i].opacity)]; |
| 2079 | else |
| 2080 | image->colormap[i].opacity=(Quantum) QuantumRange- |
| 2081 | gamma_map[ScaleQuantumToMap((Quantum) (QuantumRange- |
| 2082 | image->colormap[i].opacity))]; |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | /* |
| 2087 | Gamma-correct image. |
| 2088 | */ |
| 2089 | status=MagickTrue; |
| 2090 | progress=0; |
| 2091 | exception=(&image->exception); |
| 2092 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2093 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2094 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2095 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2096 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2097 | { |
| 2098 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2099 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2100 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2101 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2102 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2103 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2104 | register ssize_t |
| 2105 | x; |
| 2106 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2107 | if (status == MagickFalse) |
| 2108 | continue; |
| 2109 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2110 | if (q == (PixelPacket *) NULL) |
| 2111 | { |
| 2112 | status=MagickFalse; |
| 2113 | continue; |
| 2114 | } |
| 2115 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2116 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2117 | { |
cristy | 6cbd7f5 | 2009-10-17 16:06:51 +0000 | [diff] [blame] | 2118 | if (channel == DefaultChannels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2119 | { |
cristy | 6cbd7f5 | 2009-10-17 16:06:51 +0000 | [diff] [blame] | 2120 | q->red=gamma_map[ScaleQuantumToMap(q->red)]; |
| 2121 | q->green=gamma_map[ScaleQuantumToMap(q->green)]; |
| 2122 | q->blue=gamma_map[ScaleQuantumToMap(q->blue)]; |
| 2123 | } |
| 2124 | else |
| 2125 | { |
| 2126 | if ((channel & RedChannel) != 0) |
| 2127 | q->red=gamma_map[ScaleQuantumToMap(q->red)]; |
| 2128 | if ((channel & GreenChannel) != 0) |
| 2129 | q->green=gamma_map[ScaleQuantumToMap(q->green)]; |
| 2130 | if ((channel & BlueChannel) != 0) |
| 2131 | q->blue=gamma_map[ScaleQuantumToMap(q->blue)]; |
| 2132 | if ((channel & OpacityChannel) != 0) |
| 2133 | { |
| 2134 | if (image->matte == MagickFalse) |
| 2135 | q->opacity=gamma_map[ScaleQuantumToMap(q->opacity)]; |
| 2136 | else |
| 2137 | q->opacity=(Quantum) QuantumRange-gamma_map[ |
cristy | 46f0820 | 2010-01-10 04:04:21 +0000 | [diff] [blame] | 2138 | ScaleQuantumToMap((Quantum) GetAlphaPixelComponent(q))]; |
cristy | 6cbd7f5 | 2009-10-17 16:06:51 +0000 | [diff] [blame] | 2139 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2140 | } |
| 2141 | q++; |
| 2142 | } |
| 2143 | if (((channel & IndexChannel) != 0) && |
| 2144 | (image->colorspace == CMYKColorspace)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2145 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2146 | indexes[x]=gamma_map[ScaleQuantumToMap(indexes[x])]; |
| 2147 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2148 | status=MagickFalse; |
| 2149 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2150 | { |
| 2151 | MagickBooleanType |
| 2152 | proceed; |
| 2153 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2154 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2155 | #pragma omp critical (MagickCore_GammaImageChannel) |
| 2156 | #endif |
| 2157 | proceed=SetImageProgress(image,GammaCorrectImageTag,progress++, |
| 2158 | image->rows); |
| 2159 | if (proceed == MagickFalse) |
| 2160 | status=MagickFalse; |
| 2161 | } |
| 2162 | } |
| 2163 | image_view=DestroyCacheView(image_view); |
| 2164 | gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map); |
| 2165 | if (image->gamma != 0.0) |
| 2166 | image->gamma*=gamma; |
| 2167 | return(status); |
| 2168 | } |
| 2169 | |
| 2170 | /* |
| 2171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2172 | % % |
| 2173 | % % |
| 2174 | % % |
| 2175 | % H a l d C l u t I m a g e % |
| 2176 | % % |
| 2177 | % % |
| 2178 | % % |
| 2179 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2180 | % |
| 2181 | % HaldClutImage() applies a Hald color lookup table to the image. A Hald |
| 2182 | % color lookup table is a 3-dimensional color cube mapped to 2 dimensions. |
| 2183 | % Create it with the HALD coder. You can apply any color transformation to |
| 2184 | % the Hald image and then use this method to apply the transform to the |
| 2185 | % image. |
| 2186 | % |
| 2187 | % The format of the HaldClutImage method is: |
| 2188 | % |
| 2189 | % MagickBooleanType HaldClutImage(Image *image,Image *hald_image) |
| 2190 | % MagickBooleanType HaldClutImageChannel(Image *image, |
| 2191 | % const ChannelType channel,Image *hald_image) |
| 2192 | % |
| 2193 | % A description of each parameter follows: |
| 2194 | % |
| 2195 | % o image: the image, which is replaced by indexed CLUT values |
| 2196 | % |
| 2197 | % o hald_image: the color lookup table image for replacement color values. |
| 2198 | % |
| 2199 | % o channel: the channel. |
| 2200 | % |
| 2201 | */ |
| 2202 | |
| 2203 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 2204 | { |
| 2205 | if (x < y) |
| 2206 | return(x); |
| 2207 | return(y); |
| 2208 | } |
| 2209 | |
| 2210 | MagickExport MagickBooleanType HaldClutImage(Image *image, |
| 2211 | const Image *hald_image) |
| 2212 | { |
| 2213 | return(HaldClutImageChannel(image,DefaultChannels,hald_image)); |
| 2214 | } |
| 2215 | |
| 2216 | MagickExport MagickBooleanType HaldClutImageChannel(Image *image, |
| 2217 | const ChannelType channel,const Image *hald_image) |
| 2218 | { |
| 2219 | #define HaldClutImageTag "Clut/Image" |
| 2220 | |
| 2221 | typedef struct _HaldInfo |
| 2222 | { |
| 2223 | MagickRealType |
| 2224 | x, |
| 2225 | y, |
| 2226 | z; |
| 2227 | } HaldInfo; |
| 2228 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 2229 | CacheView |
| 2230 | *image_view; |
| 2231 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2232 | double |
| 2233 | width; |
| 2234 | |
| 2235 | ExceptionInfo |
| 2236 | *exception; |
| 2237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2238 | MagickBooleanType |
| 2239 | status; |
| 2240 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2241 | MagickOffsetType |
| 2242 | progress; |
| 2243 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2244 | MagickPixelPacket |
| 2245 | zero; |
| 2246 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2247 | size_t |
| 2248 | cube_size, |
| 2249 | length, |
| 2250 | level; |
| 2251 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2252 | ssize_t |
| 2253 | y; |
| 2254 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2255 | assert(image != (Image *) NULL); |
| 2256 | assert(image->signature == MagickSignature); |
| 2257 | if (image->debug != MagickFalse) |
| 2258 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2259 | assert(hald_image != (Image *) NULL); |
| 2260 | assert(hald_image->signature == MagickSignature); |
| 2261 | if (SetImageStorageClass(image,DirectClass) == MagickFalse) |
| 2262 | return(MagickFalse); |
| 2263 | if (image->matte == MagickFalse) |
| 2264 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel); |
| 2265 | /* |
| 2266 | Hald clut image. |
| 2267 | */ |
| 2268 | status=MagickTrue; |
| 2269 | progress=0; |
| 2270 | length=MagickMin(hald_image->columns,hald_image->rows); |
| 2271 | for (level=2; (level*level*level) < length; level++) ; |
| 2272 | level*=level; |
| 2273 | cube_size=level*level; |
| 2274 | width=(double) hald_image->columns; |
| 2275 | GetMagickPixelPacket(hald_image,&zero); |
| 2276 | exception=(&image->exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2277 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2278 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2279 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2280 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2281 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2282 | { |
| 2283 | double |
| 2284 | offset; |
| 2285 | |
| 2286 | HaldInfo |
| 2287 | point; |
| 2288 | |
| 2289 | MagickPixelPacket |
| 2290 | pixel, |
| 2291 | pixel1, |
| 2292 | pixel2, |
| 2293 | pixel3, |
| 2294 | pixel4; |
| 2295 | |
| 2296 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2297 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2298 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2299 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2300 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2301 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2302 | register ssize_t |
| 2303 | x; |
| 2304 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2305 | if (status == MagickFalse) |
| 2306 | continue; |
| 2307 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2308 | if (q == (PixelPacket *) NULL) |
| 2309 | { |
| 2310 | status=MagickFalse; |
| 2311 | continue; |
| 2312 | } |
| 2313 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
| 2314 | pixel=zero; |
| 2315 | pixel1=zero; |
| 2316 | pixel2=zero; |
| 2317 | pixel3=zero; |
| 2318 | pixel4=zero; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2319 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2320 | { |
| 2321 | point.x=QuantumScale*(level-1.0)*q->red; |
| 2322 | point.y=QuantumScale*(level-1.0)*q->green; |
| 2323 | point.z=QuantumScale*(level-1.0)*q->blue; |
| 2324 | offset=point.x+level*floor(point.y)+cube_size*floor(point.z); |
| 2325 | point.x-=floor(point.x); |
| 2326 | point.y-=floor(point.y); |
| 2327 | point.z-=floor(point.z); |
cristy | d76c51e | 2011-03-26 00:21:26 +0000 | [diff] [blame] | 2328 | (void) InterpolatePixelPacket(image,image_view,image->interpolate, |
| 2329 | fmod(offset,width),floor(offset/width),&pixel1,exception); |
| 2330 | (void) InterpolatePixelPacket(image,image_view,image->interpolate, |
| 2331 | fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2332 | MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2, |
| 2333 | pixel2.opacity,point.y,&pixel3); |
| 2334 | offset+=cube_size; |
cristy | d76c51e | 2011-03-26 00:21:26 +0000 | [diff] [blame] | 2335 | (void) InterpolatePixelPacket(image,image_view,image->interpolate, |
| 2336 | fmod(offset,width),floor(offset/width),&pixel1,exception); |
| 2337 | (void) InterpolatePixelPacket(image,image_view,image->interpolate, |
| 2338 | fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2339 | MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2, |
| 2340 | pixel2.opacity,point.y,&pixel4); |
| 2341 | MagickPixelCompositeAreaBlend(&pixel3,pixel3.opacity,&pixel4, |
| 2342 | pixel4.opacity,point.z,&pixel); |
| 2343 | if ((channel & RedChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2344 | SetRedPixelComponent(q,ClampRedPixelComponent(&pixel)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2345 | if ((channel & GreenChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2346 | SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2347 | if ((channel & BlueChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2348 | SetBluePixelComponent(q,ClampBluePixelComponent(&pixel)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2349 | if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2350 | SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2351 | if (((channel & IndexChannel) != 0) && |
| 2352 | (image->colorspace == CMYKColorspace)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2353 | indexes[x]=ClampToQuantum(pixel.index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2354 | q++; |
| 2355 | } |
| 2356 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2357 | status=MagickFalse; |
| 2358 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2359 | { |
| 2360 | MagickBooleanType |
| 2361 | proceed; |
| 2362 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2363 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2364 | #pragma omp critical (MagickCore_HaldClutImageChannel) |
| 2365 | #endif |
| 2366 | proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows); |
| 2367 | if (proceed == MagickFalse) |
| 2368 | status=MagickFalse; |
| 2369 | } |
| 2370 | } |
| 2371 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2372 | return(status); |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2377 | % % |
| 2378 | % % |
| 2379 | % % |
| 2380 | % L e v e l I m a g e % |
| 2381 | % % |
| 2382 | % % |
| 2383 | % % |
| 2384 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2385 | % |
| 2386 | % LevelImage() adjusts the levels of a particular image channel by |
| 2387 | % scaling the colors falling between specified white and black points to |
| 2388 | % the full available quantum range. |
| 2389 | % |
| 2390 | % The parameters provided represent the black, and white points. The black |
| 2391 | % point specifies the darkest color in the image. Colors darker than the |
| 2392 | % black point are set to zero. White point specifies the lightest color in |
| 2393 | % the image. Colors brighter than the white point are set to the maximum |
| 2394 | % quantum value. |
| 2395 | % |
| 2396 | % If a '!' flag is given, map black and white colors to the given levels |
| 2397 | % rather than mapping those levels to black and white. See |
| 2398 | % LevelizeImageChannel() and LevelizeImageChannel(), below. |
| 2399 | % |
| 2400 | % Gamma specifies a gamma correction to apply to the image. |
| 2401 | % |
| 2402 | % The format of the LevelImage method is: |
| 2403 | % |
| 2404 | % MagickBooleanType LevelImage(Image *image,const char *levels) |
| 2405 | % |
| 2406 | % A description of each parameter follows: |
| 2407 | % |
| 2408 | % o image: the image. |
| 2409 | % |
| 2410 | % o levels: Specify the levels where the black and white points have the |
| 2411 | % range of 0-QuantumRange, and gamma has the range 0-10 (e.g. 10x90%+2). |
| 2412 | % A '!' flag inverts the re-mapping. |
| 2413 | % |
| 2414 | */ |
| 2415 | |
| 2416 | MagickExport MagickBooleanType LevelImage(Image *image,const char *levels) |
| 2417 | { |
| 2418 | double |
| 2419 | black_point, |
| 2420 | gamma, |
| 2421 | white_point; |
| 2422 | |
| 2423 | GeometryInfo |
| 2424 | geometry_info; |
| 2425 | |
| 2426 | MagickBooleanType |
| 2427 | status; |
| 2428 | |
| 2429 | MagickStatusType |
| 2430 | flags; |
| 2431 | |
| 2432 | /* |
| 2433 | Parse levels. |
| 2434 | */ |
| 2435 | if (levels == (char *) NULL) |
| 2436 | return(MagickFalse); |
| 2437 | flags=ParseGeometry(levels,&geometry_info); |
| 2438 | black_point=geometry_info.rho; |
| 2439 | white_point=(double) QuantumRange; |
| 2440 | if ((flags & SigmaValue) != 0) |
| 2441 | white_point=geometry_info.sigma; |
| 2442 | gamma=1.0; |
| 2443 | if ((flags & XiValue) != 0) |
| 2444 | gamma=geometry_info.xi; |
| 2445 | if ((flags & PercentValue) != 0) |
| 2446 | { |
| 2447 | black_point*=(double) image->columns*image->rows/100.0; |
| 2448 | white_point*=(double) image->columns*image->rows/100.0; |
| 2449 | } |
| 2450 | if ((flags & SigmaValue) == 0) |
| 2451 | white_point=(double) QuantumRange-black_point; |
| 2452 | if ((flags & AspectValue ) == 0) |
| 2453 | status=LevelImageChannel(image,DefaultChannels,black_point,white_point, |
| 2454 | gamma); |
| 2455 | else |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2456 | status=LevelizeImage(image,black_point,white_point,gamma); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2457 | return(status); |
| 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2462 | % % |
| 2463 | % % |
| 2464 | % % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2465 | % L e v e l i z e I m a g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2466 | % % |
| 2467 | % % |
| 2468 | % % |
| 2469 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2470 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2471 | % LevelizeImage() applies the normal level operation to the image, spreading |
| 2472 | % out the values between the black and white points over the entire range of |
| 2473 | % values. Gamma correction is also applied after the values has been mapped. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2474 | % |
| 2475 | % It is typically used to improve image contrast, or to provide a controlled |
| 2476 | % linear threshold for the image. If the black and white points are set to |
| 2477 | % the minimum and maximum values found in the image, the image can be |
| 2478 | % normalized. or by swapping black and white values, negate the image. |
| 2479 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2480 | % The format of the LevelizeImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2481 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2482 | % MagickBooleanType LevelizeImage(Image *image,const double black_point, |
| 2483 | % const double white_point,const double gamma) |
| 2484 | % MagickBooleanType LevelizeImageChannel(Image *image, |
| 2485 | % const ChannelType channel,const double black_point, |
| 2486 | % const double white_point,const double gamma) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2487 | % |
| 2488 | % A description of each parameter follows: |
| 2489 | % |
| 2490 | % o image: the image. |
| 2491 | % |
| 2492 | % o channel: the channel. |
| 2493 | % |
| 2494 | % o black_point: The level which is to be mapped to zero (black) |
| 2495 | % |
| 2496 | % o white_point: The level which is to be mapped to QuantiumRange (white) |
| 2497 | % |
| 2498 | % o gamma: adjust gamma by this factor before mapping values. |
| 2499 | % use 1.0 for purely linear stretching of image color values |
| 2500 | % |
| 2501 | */ |
| 2502 | MagickExport MagickBooleanType LevelImageChannel(Image *image, |
| 2503 | const ChannelType channel,const double black_point,const double white_point, |
| 2504 | const double gamma) |
| 2505 | { |
| 2506 | #define LevelImageTag "Level/Image" |
cristy | bcfb043 | 2010-05-06 01:45:33 +0000 | [diff] [blame] | 2507 | #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \ |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2508 | pow(scale*((double) (x)-black_point),1.0/gamma))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2509 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2510 | CacheView |
| 2511 | *image_view; |
| 2512 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2513 | ExceptionInfo |
| 2514 | *exception; |
| 2515 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2516 | MagickBooleanType |
| 2517 | status; |
| 2518 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2519 | MagickOffsetType |
| 2520 | progress; |
| 2521 | |
anthony | 7fe39fc | 2010-04-06 03:19:20 +0000 | [diff] [blame] | 2522 | register double |
| 2523 | scale; |
| 2524 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2525 | register ssize_t |
| 2526 | i; |
| 2527 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2528 | ssize_t |
| 2529 | y; |
| 2530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2531 | /* |
| 2532 | Allocate and initialize levels map. |
| 2533 | */ |
| 2534 | assert(image != (Image *) NULL); |
| 2535 | assert(image->signature == MagickSignature); |
| 2536 | if (image->debug != MagickFalse) |
| 2537 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2538 | scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2539 | if (image->storage_class == PseudoClass) |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2540 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2541 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2542 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2543 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2544 | { |
| 2545 | /* |
| 2546 | Level colormap. |
| 2547 | */ |
| 2548 | if ((channel & RedChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2549 | image->colormap[i].red=LevelQuantum(image->colormap[i].red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2550 | if ((channel & GreenChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2551 | image->colormap[i].green=LevelQuantum(image->colormap[i].green); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2552 | if ((channel & BlueChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2553 | image->colormap[i].blue=LevelQuantum(image->colormap[i].blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2554 | if ((channel & OpacityChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2555 | image->colormap[i].opacity=LevelQuantum(image->colormap[i].opacity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2556 | } |
| 2557 | /* |
| 2558 | Level image. |
| 2559 | */ |
| 2560 | status=MagickTrue; |
| 2561 | progress=0; |
| 2562 | exception=(&image->exception); |
| 2563 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2564 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2565 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2566 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2567 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2568 | { |
| 2569 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2570 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2571 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2572 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2573 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2574 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2575 | register ssize_t |
| 2576 | x; |
| 2577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2578 | if (status == MagickFalse) |
| 2579 | continue; |
| 2580 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2581 | if (q == (PixelPacket *) NULL) |
| 2582 | { |
| 2583 | status=MagickFalse; |
| 2584 | continue; |
| 2585 | } |
| 2586 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2587 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2588 | { |
| 2589 | if ((channel & RedChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2590 | q->red=LevelQuantum(q->red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2591 | if ((channel & GreenChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2592 | q->green=LevelQuantum(q->green); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2593 | if ((channel & BlueChannel) != 0) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2594 | q->blue=LevelQuantum(q->blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2595 | if (((channel & OpacityChannel) != 0) && |
| 2596 | (image->matte == MagickTrue)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2597 | q->opacity=(Quantum) (QuantumRange-LevelQuantum(QuantumRange- |
| 2598 | q->opacity)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2599 | if (((channel & IndexChannel) != 0) && |
| 2600 | (image->colorspace == CMYKColorspace)) |
cristy | c1f508d | 2010-04-22 01:21:47 +0000 | [diff] [blame] | 2601 | indexes[x]=LevelQuantum(indexes[x]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2602 | q++; |
| 2603 | } |
| 2604 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2605 | status=MagickFalse; |
| 2606 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2607 | { |
| 2608 | MagickBooleanType |
| 2609 | proceed; |
| 2610 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2611 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2612 | #pragma omp critical (MagickCore_LevelImageChannel) |
| 2613 | #endif |
| 2614 | proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows); |
| 2615 | if (proceed == MagickFalse) |
| 2616 | status=MagickFalse; |
| 2617 | } |
| 2618 | } |
| 2619 | image_view=DestroyCacheView(image_view); |
| 2620 | return(status); |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2625 | % % |
| 2626 | % % |
| 2627 | % % |
| 2628 | % L e v e l i z e I m a g e C h a n n e l % |
| 2629 | % % |
| 2630 | % % |
| 2631 | % % |
| 2632 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2633 | % |
| 2634 | % LevelizeImageChannel() applies the reversed LevelImage() operation to just |
| 2635 | % the specific channels specified. It compresses the full range of color |
| 2636 | % values, so that they lie between the given black and white points. Gamma is |
| 2637 | % applied before the values are mapped. |
| 2638 | % |
| 2639 | % LevelizeImageChannel() can be called with by using a +level command line |
| 2640 | % API option, or using a '!' on a -level or LevelImage() geometry string. |
| 2641 | % |
| 2642 | % It can be used for example de-contrast a greyscale image to the exact |
| 2643 | % levels specified. Or by using specific levels for each channel of an image |
| 2644 | % you can convert a gray-scale image to any linear color gradient, according |
| 2645 | % to those levels. |
| 2646 | % |
| 2647 | % The format of the LevelizeImageChannel method is: |
| 2648 | % |
| 2649 | % MagickBooleanType LevelizeImageChannel(Image *image, |
| 2650 | % const ChannelType channel,const char *levels) |
| 2651 | % |
| 2652 | % A description of each parameter follows: |
| 2653 | % |
| 2654 | % o image: the image. |
| 2655 | % |
| 2656 | % o channel: the channel. |
| 2657 | % |
| 2658 | % o black_point: The level to map zero (black) to. |
| 2659 | % |
| 2660 | % o white_point: The level to map QuantiumRange (white) to. |
| 2661 | % |
| 2662 | % o gamma: adjust gamma by this factor before mapping values. |
| 2663 | % |
| 2664 | */ |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2665 | |
| 2666 | MagickExport MagickBooleanType LevelizeImage(Image *image, |
| 2667 | const double black_point,const double white_point,const double gamma) |
| 2668 | { |
| 2669 | MagickBooleanType |
| 2670 | status; |
| 2671 | |
| 2672 | status=LevelizeImageChannel(image,DefaultChannels,black_point,white_point, |
| 2673 | gamma); |
| 2674 | return(status); |
| 2675 | } |
| 2676 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2677 | MagickExport MagickBooleanType LevelizeImageChannel(Image *image, |
| 2678 | const ChannelType channel,const double black_point,const double white_point, |
| 2679 | const double gamma) |
| 2680 | { |
| 2681 | #define LevelizeImageTag "Levelize/Image" |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 2682 | #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2683 | pow((double)(QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \ |
| 2684 | black_point)) |
| 2685 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2686 | CacheView |
| 2687 | *image_view; |
| 2688 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2689 | ExceptionInfo |
| 2690 | *exception; |
| 2691 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2692 | MagickBooleanType |
| 2693 | status; |
| 2694 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2695 | MagickOffsetType |
| 2696 | progress; |
| 2697 | |
| 2698 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2699 | i; |
| 2700 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2701 | ssize_t |
| 2702 | y; |
| 2703 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2704 | /* |
| 2705 | Allocate and initialize levels map. |
| 2706 | */ |
| 2707 | assert(image != (Image *) NULL); |
| 2708 | assert(image->signature == MagickSignature); |
| 2709 | if (image->debug != MagickFalse) |
| 2710 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2711 | if (image->storage_class == PseudoClass) |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2712 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2713 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2714 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2715 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2716 | { |
| 2717 | /* |
| 2718 | Level colormap. |
| 2719 | */ |
| 2720 | if ((channel & RedChannel) != 0) |
| 2721 | image->colormap[i].red=LevelizeValue(image->colormap[i].red); |
| 2722 | if ((channel & GreenChannel) != 0) |
| 2723 | image->colormap[i].green=LevelizeValue(image->colormap[i].green); |
| 2724 | if ((channel & BlueChannel) != 0) |
| 2725 | image->colormap[i].blue=LevelizeValue(image->colormap[i].blue); |
| 2726 | if ((channel & OpacityChannel) != 0) |
| 2727 | image->colormap[i].opacity=LevelizeValue(image->colormap[i].opacity); |
| 2728 | } |
| 2729 | /* |
| 2730 | Level image. |
| 2731 | */ |
| 2732 | status=MagickTrue; |
| 2733 | progress=0; |
| 2734 | exception=(&image->exception); |
| 2735 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2736 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2737 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2738 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2739 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2740 | { |
| 2741 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2742 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2743 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2744 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2745 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2746 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2747 | register ssize_t |
| 2748 | x; |
| 2749 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2750 | if (status == MagickFalse) |
| 2751 | continue; |
| 2752 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2753 | if (q == (PixelPacket *) NULL) |
| 2754 | { |
| 2755 | status=MagickFalse; |
| 2756 | continue; |
| 2757 | } |
| 2758 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2759 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2760 | { |
| 2761 | if ((channel & RedChannel) != 0) |
| 2762 | q->red=LevelizeValue(q->red); |
| 2763 | if ((channel & GreenChannel) != 0) |
| 2764 | q->green=LevelizeValue(q->green); |
| 2765 | if ((channel & BlueChannel) != 0) |
| 2766 | q->blue=LevelizeValue(q->blue); |
| 2767 | if (((channel & OpacityChannel) != 0) && |
| 2768 | (image->matte == MagickTrue)) |
| 2769 | q->opacity=LevelizeValue(q->opacity); |
| 2770 | if (((channel & IndexChannel) != 0) && |
| 2771 | (image->colorspace == CMYKColorspace)) |
| 2772 | indexes[x]=LevelizeValue(indexes[x]); |
| 2773 | q++; |
| 2774 | } |
| 2775 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2776 | status=MagickFalse; |
| 2777 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2778 | { |
| 2779 | MagickBooleanType |
| 2780 | proceed; |
| 2781 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2782 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2783 | #pragma omp critical (MagickCore_LevelizeImageChannel) |
| 2784 | #endif |
| 2785 | proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows); |
| 2786 | if (proceed == MagickFalse) |
| 2787 | status=MagickFalse; |
| 2788 | } |
| 2789 | } |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2790 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2791 | return(status); |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2796 | % % |
| 2797 | % % |
| 2798 | % % |
| 2799 | % L e v e l I m a g e C o l o r s % |
| 2800 | % % |
| 2801 | % % |
| 2802 | % % |
| 2803 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2804 | % |
cristy | ee0f8d7 | 2009-09-19 00:58:29 +0000 | [diff] [blame] | 2805 | % LevelImageColor() maps the given color to "black" and "white" values, |
| 2806 | % linearly spreading out the colors, and level values on a channel by channel |
| 2807 | % bases, as per LevelImage(). The given colors allows you to specify |
glennrp | 1e7f7bc | 2011-03-02 19:25:28 +0000 | [diff] [blame] | 2808 | % different level ranges for each of the color channels separately. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2809 | % |
| 2810 | % If the boolean 'invert' is set true the image values will modifyed in the |
| 2811 | % reverse direction. That is any existing "black" and "white" colors in the |
| 2812 | % image will become the color values given, with all other values compressed |
| 2813 | % appropriatally. This effectivally maps a greyscale gradient into the given |
| 2814 | % color gradient. |
| 2815 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2816 | % The format of the LevelColorsImageChannel method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2817 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2818 | % MagickBooleanType LevelColorsImage(Image *image, |
cristy | ee0f8d7 | 2009-09-19 00:58:29 +0000 | [diff] [blame] | 2819 | % const MagickPixelPacket *black_color, |
| 2820 | % const MagickPixelPacket *white_color,const MagickBooleanType invert) |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2821 | % MagickBooleanType LevelColorsImageChannel(Image *image, |
| 2822 | % const ChannelType channel,const MagickPixelPacket *black_color, |
| 2823 | % const MagickPixelPacket *white_color,const MagickBooleanType invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2824 | % |
| 2825 | % A description of each parameter follows: |
| 2826 | % |
| 2827 | % o image: the image. |
| 2828 | % |
| 2829 | % o channel: the channel. |
| 2830 | % |
| 2831 | % o black_color: The color to map black to/from |
| 2832 | % |
| 2833 | % o white_point: The color to map white to/from |
| 2834 | % |
| 2835 | % o invert: if true map the colors (levelize), rather than from (level) |
| 2836 | % |
| 2837 | */ |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2838 | |
| 2839 | MagickExport MagickBooleanType LevelColorsImage(Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2840 | const MagickPixelPacket *black_color,const MagickPixelPacket *white_color, |
| 2841 | const MagickBooleanType invert) |
| 2842 | { |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2843 | MagickBooleanType |
| 2844 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2845 | |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 2846 | status=LevelColorsImageChannel(image,DefaultChannels,black_color,white_color, |
| 2847 | invert); |
| 2848 | return(status); |
| 2849 | } |
| 2850 | |
| 2851 | MagickExport MagickBooleanType LevelColorsImageChannel(Image *image, |
| 2852 | const ChannelType channel,const MagickPixelPacket *black_color, |
| 2853 | const MagickPixelPacket *white_color,const MagickBooleanType invert) |
| 2854 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2855 | MagickStatusType |
| 2856 | status; |
| 2857 | |
| 2858 | /* |
| 2859 | Allocate and initialize levels map. |
| 2860 | */ |
| 2861 | assert(image != (Image *) NULL); |
| 2862 | assert(image->signature == MagickSignature); |
| 2863 | if (image->debug != MagickFalse) |
| 2864 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2865 | status=MagickFalse; |
| 2866 | if (invert == MagickFalse) |
| 2867 | { |
| 2868 | if ((channel & RedChannel) != 0) |
| 2869 | status|=LevelImageChannel(image,RedChannel, |
| 2870 | black_color->red,white_color->red,(double) 1.0); |
| 2871 | if ((channel & GreenChannel) != 0) |
| 2872 | status|=LevelImageChannel(image,GreenChannel, |
| 2873 | black_color->green,white_color->green,(double) 1.0); |
| 2874 | if ((channel & BlueChannel) != 0) |
| 2875 | status|=LevelImageChannel(image,BlueChannel, |
| 2876 | black_color->blue,white_color->blue,(double) 1.0); |
| 2877 | if (((channel & OpacityChannel) != 0) && |
| 2878 | (image->matte == MagickTrue)) |
| 2879 | status|=LevelImageChannel(image,OpacityChannel, |
| 2880 | black_color->opacity,white_color->opacity,(double) 1.0); |
| 2881 | if (((channel & IndexChannel) != 0) && |
| 2882 | (image->colorspace == CMYKColorspace)) |
| 2883 | status|=LevelImageChannel(image,IndexChannel, |
| 2884 | black_color->index,white_color->index,(double) 1.0); |
| 2885 | } |
| 2886 | else |
| 2887 | { |
| 2888 | if ((channel & RedChannel) != 0) |
| 2889 | status|=LevelizeImageChannel(image,RedChannel, |
| 2890 | black_color->red,white_color->red,(double) 1.0); |
| 2891 | if ((channel & GreenChannel) != 0) |
| 2892 | status|=LevelizeImageChannel(image,GreenChannel, |
| 2893 | black_color->green,white_color->green,(double) 1.0); |
| 2894 | if ((channel & BlueChannel) != 0) |
| 2895 | status|=LevelizeImageChannel(image,BlueChannel, |
| 2896 | black_color->blue,white_color->blue,(double) 1.0); |
| 2897 | if (((channel & OpacityChannel) != 0) && |
| 2898 | (image->matte == MagickTrue)) |
| 2899 | status|=LevelizeImageChannel(image,OpacityChannel, |
| 2900 | black_color->opacity,white_color->opacity,(double) 1.0); |
| 2901 | if (((channel & IndexChannel) != 0) && |
| 2902 | (image->colorspace == CMYKColorspace)) |
| 2903 | status|=LevelizeImageChannel(image,IndexChannel, |
| 2904 | black_color->index,white_color->index,(double) 1.0); |
| 2905 | } |
| 2906 | return(status == 0 ? MagickFalse : MagickTrue); |
| 2907 | } |
| 2908 | |
| 2909 | /* |
| 2910 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2911 | % % |
| 2912 | % % |
| 2913 | % % |
| 2914 | % L i n e a r S t r e t c h I m a g e % |
| 2915 | % % |
| 2916 | % % |
| 2917 | % % |
| 2918 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2919 | % |
| 2920 | % The LinearStretchImage() discards any pixels below the black point and |
| 2921 | % above the white point and levels the remaining pixels. |
| 2922 | % |
| 2923 | % The format of the LinearStretchImage method is: |
| 2924 | % |
| 2925 | % MagickBooleanType LinearStretchImage(Image *image, |
| 2926 | % const double black_point,const double white_point) |
| 2927 | % |
| 2928 | % A description of each parameter follows: |
| 2929 | % |
| 2930 | % o image: the image. |
| 2931 | % |
| 2932 | % o black_point: the black point. |
| 2933 | % |
| 2934 | % o white_point: the white point. |
| 2935 | % |
| 2936 | */ |
| 2937 | MagickExport MagickBooleanType LinearStretchImage(Image *image, |
| 2938 | const double black_point,const double white_point) |
| 2939 | { |
| 2940 | #define LinearStretchImageTag "LinearStretch/Image" |
| 2941 | |
| 2942 | ExceptionInfo |
| 2943 | *exception; |
| 2944 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2945 | MagickBooleanType |
| 2946 | status; |
| 2947 | |
| 2948 | MagickRealType |
| 2949 | *histogram, |
| 2950 | intensity; |
| 2951 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2952 | ssize_t |
| 2953 | black, |
| 2954 | white, |
| 2955 | y; |
| 2956 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2957 | /* |
| 2958 | Allocate histogram and linear map. |
| 2959 | */ |
| 2960 | assert(image != (Image *) NULL); |
| 2961 | assert(image->signature == MagickSignature); |
| 2962 | histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL, |
| 2963 | sizeof(*histogram)); |
| 2964 | if (histogram == (MagickRealType *) NULL) |
| 2965 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2966 | image->filename); |
| 2967 | /* |
| 2968 | Form histogram. |
| 2969 | */ |
| 2970 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram)); |
| 2971 | exception=(&image->exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2972 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2973 | { |
| 2974 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2975 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2976 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2977 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2978 | x; |
| 2979 | |
| 2980 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 2981 | if (p == (const PixelPacket *) NULL) |
| 2982 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2983 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2984 | { |
| 2985 | histogram[ScaleQuantumToMap(PixelIntensityToQuantum(p))]++; |
| 2986 | p++; |
| 2987 | } |
| 2988 | } |
| 2989 | /* |
| 2990 | Find the histogram boundaries by locating the black and white point levels. |
| 2991 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2992 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2993 | for (black=0; black < (ssize_t) MaxMap; black++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2994 | { |
| 2995 | intensity+=histogram[black]; |
| 2996 | if (intensity >= black_point) |
| 2997 | break; |
| 2998 | } |
| 2999 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3000 | for (white=(ssize_t) MaxMap; white != 0; white--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3001 | { |
| 3002 | intensity+=histogram[white]; |
| 3003 | if (intensity >= white_point) |
| 3004 | break; |
| 3005 | } |
| 3006 | histogram=(MagickRealType *) RelinquishMagickMemory(histogram); |
| 3007 | status=LevelImageChannel(image,DefaultChannels,(double) black,(double) white, |
| 3008 | 1.0); |
| 3009 | return(status); |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3014 | % % |
| 3015 | % % |
| 3016 | % % |
| 3017 | % M o d u l a t e I m a g e % |
| 3018 | % % |
| 3019 | % % |
| 3020 | % % |
| 3021 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3022 | % |
| 3023 | % ModulateImage() lets you control the brightness, saturation, and hue |
| 3024 | % of an image. Modulate represents the brightness, saturation, and hue |
| 3025 | % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the |
| 3026 | % modulation is lightness, saturation, and hue. And if the colorspace is |
| 3027 | % HWB, use blackness, whiteness, and hue. |
| 3028 | % |
| 3029 | % The format of the ModulateImage method is: |
| 3030 | % |
| 3031 | % MagickBooleanType ModulateImage(Image *image,const char *modulate) |
| 3032 | % |
| 3033 | % A description of each parameter follows: |
| 3034 | % |
| 3035 | % o image: the image. |
| 3036 | % |
| 3037 | % o modulate: Define the percent change in brightness, saturation, and |
| 3038 | % hue. |
| 3039 | % |
| 3040 | */ |
| 3041 | |
| 3042 | static void ModulateHSB(const double percent_hue, |
| 3043 | const double percent_saturation,const double percent_brightness, |
| 3044 | Quantum *red,Quantum *green,Quantum *blue) |
| 3045 | { |
| 3046 | double |
| 3047 | brightness, |
| 3048 | hue, |
| 3049 | saturation; |
| 3050 | |
| 3051 | /* |
| 3052 | Increase or decrease color brightness, saturation, or hue. |
| 3053 | */ |
| 3054 | assert(red != (Quantum *) NULL); |
| 3055 | assert(green != (Quantum *) NULL); |
| 3056 | assert(blue != (Quantum *) NULL); |
| 3057 | ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness); |
| 3058 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3059 | while (hue < 0.0) |
| 3060 | hue+=1.0; |
| 3061 | while (hue > 1.0) |
| 3062 | hue-=1.0; |
| 3063 | saturation*=0.01*percent_saturation; |
| 3064 | brightness*=0.01*percent_brightness; |
| 3065 | ConvertHSBToRGB(hue,saturation,brightness,red,green,blue); |
| 3066 | } |
| 3067 | |
| 3068 | static void ModulateHSL(const double percent_hue, |
| 3069 | const double percent_saturation,const double percent_lightness, |
| 3070 | Quantum *red,Quantum *green,Quantum *blue) |
| 3071 | { |
| 3072 | double |
| 3073 | hue, |
| 3074 | lightness, |
| 3075 | saturation; |
| 3076 | |
| 3077 | /* |
| 3078 | Increase or decrease color lightness, saturation, or hue. |
| 3079 | */ |
| 3080 | assert(red != (Quantum *) NULL); |
| 3081 | assert(green != (Quantum *) NULL); |
| 3082 | assert(blue != (Quantum *) NULL); |
| 3083 | ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness); |
| 3084 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3085 | while (hue < 0.0) |
| 3086 | hue+=1.0; |
| 3087 | while (hue > 1.0) |
| 3088 | hue-=1.0; |
| 3089 | saturation*=0.01*percent_saturation; |
| 3090 | lightness*=0.01*percent_lightness; |
| 3091 | ConvertHSLToRGB(hue,saturation,lightness,red,green,blue); |
| 3092 | } |
| 3093 | |
| 3094 | static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue) |
| 3095 | { |
| 3096 | double |
| 3097 | blackness, |
| 3098 | hue, |
| 3099 | whiteness; |
| 3100 | |
| 3101 | /* |
| 3102 | Increase or decrease color blackness, whiteness, or hue. |
| 3103 | */ |
| 3104 | assert(red != (Quantum *) NULL); |
| 3105 | assert(green != (Quantum *) NULL); |
| 3106 | assert(blue != (Quantum *) NULL); |
| 3107 | ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness); |
| 3108 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3109 | while (hue < 0.0) |
| 3110 | hue+=1.0; |
| 3111 | while (hue > 1.0) |
| 3112 | hue-=1.0; |
| 3113 | blackness*=0.01*percent_blackness; |
| 3114 | whiteness*=0.01*percent_whiteness; |
| 3115 | ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue); |
| 3116 | } |
| 3117 | |
| 3118 | MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate) |
| 3119 | { |
| 3120 | #define ModulateImageTag "Modulate/Image" |
| 3121 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3122 | CacheView |
| 3123 | *image_view; |
| 3124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3125 | ColorspaceType |
| 3126 | colorspace; |
| 3127 | |
| 3128 | const char |
| 3129 | *artifact; |
| 3130 | |
| 3131 | double |
| 3132 | percent_brightness, |
| 3133 | percent_hue, |
| 3134 | percent_saturation; |
| 3135 | |
| 3136 | ExceptionInfo |
| 3137 | *exception; |
| 3138 | |
| 3139 | GeometryInfo |
| 3140 | geometry_info; |
| 3141 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3142 | MagickBooleanType |
| 3143 | status; |
| 3144 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3145 | MagickOffsetType |
| 3146 | progress; |
| 3147 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3148 | MagickStatusType |
| 3149 | flags; |
| 3150 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3151 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3152 | i; |
| 3153 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3154 | ssize_t |
| 3155 | y; |
| 3156 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3157 | /* |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 3158 | Initialize modulate table. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3159 | */ |
| 3160 | assert(image != (Image *) NULL); |
| 3161 | assert(image->signature == MagickSignature); |
| 3162 | if (image->debug != MagickFalse) |
| 3163 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3164 | if (modulate == (char *) NULL) |
| 3165 | return(MagickFalse); |
| 3166 | flags=ParseGeometry(modulate,&geometry_info); |
| 3167 | percent_brightness=geometry_info.rho; |
| 3168 | percent_saturation=geometry_info.sigma; |
| 3169 | if ((flags & SigmaValue) == 0) |
| 3170 | percent_saturation=100.0; |
| 3171 | percent_hue=geometry_info.xi; |
| 3172 | if ((flags & XiValue) == 0) |
| 3173 | percent_hue=100.0; |
| 3174 | colorspace=UndefinedColorspace; |
| 3175 | artifact=GetImageArtifact(image,"modulate:colorspace"); |
| 3176 | if (artifact != (const char *) NULL) |
| 3177 | colorspace=(ColorspaceType) ParseMagickOption(MagickColorspaceOptions, |
| 3178 | MagickFalse,artifact); |
| 3179 | if (image->storage_class == PseudoClass) |
| 3180 | { |
| 3181 | /* |
| 3182 | Modulate colormap. |
| 3183 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3184 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3185 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3186 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3187 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3188 | switch (colorspace) |
| 3189 | { |
| 3190 | case HSBColorspace: |
| 3191 | { |
| 3192 | ModulateHSB(percent_hue,percent_saturation,percent_brightness, |
| 3193 | &image->colormap[i].red,&image->colormap[i].green, |
| 3194 | &image->colormap[i].blue); |
| 3195 | break; |
| 3196 | } |
| 3197 | case HSLColorspace: |
| 3198 | default: |
| 3199 | { |
| 3200 | ModulateHSL(percent_hue,percent_saturation,percent_brightness, |
| 3201 | &image->colormap[i].red,&image->colormap[i].green, |
| 3202 | &image->colormap[i].blue); |
| 3203 | break; |
| 3204 | } |
| 3205 | case HWBColorspace: |
| 3206 | { |
| 3207 | ModulateHWB(percent_hue,percent_saturation,percent_brightness, |
| 3208 | &image->colormap[i].red,&image->colormap[i].green, |
| 3209 | &image->colormap[i].blue); |
| 3210 | break; |
| 3211 | } |
| 3212 | } |
| 3213 | } |
| 3214 | /* |
| 3215 | Modulate image. |
| 3216 | */ |
| 3217 | status=MagickTrue; |
| 3218 | progress=0; |
| 3219 | exception=(&image->exception); |
| 3220 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3221 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3222 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3223 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3224 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3225 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3226 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3227 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3228 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3229 | register ssize_t |
| 3230 | x; |
| 3231 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3232 | if (status == MagickFalse) |
| 3233 | continue; |
| 3234 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 3235 | if (q == (PixelPacket *) NULL) |
| 3236 | { |
| 3237 | status=MagickFalse; |
| 3238 | continue; |
| 3239 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3240 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3241 | { |
| 3242 | switch (colorspace) |
| 3243 | { |
| 3244 | case HSBColorspace: |
| 3245 | { |
| 3246 | ModulateHSB(percent_hue,percent_saturation,percent_brightness, |
| 3247 | &q->red,&q->green,&q->blue); |
| 3248 | break; |
| 3249 | } |
| 3250 | case HSLColorspace: |
| 3251 | default: |
| 3252 | { |
| 3253 | ModulateHSL(percent_hue,percent_saturation,percent_brightness, |
| 3254 | &q->red,&q->green,&q->blue); |
| 3255 | break; |
| 3256 | } |
| 3257 | case HWBColorspace: |
| 3258 | { |
| 3259 | ModulateHWB(percent_hue,percent_saturation,percent_brightness, |
| 3260 | &q->red,&q->green,&q->blue); |
| 3261 | break; |
| 3262 | } |
| 3263 | } |
| 3264 | q++; |
| 3265 | } |
| 3266 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3267 | status=MagickFalse; |
| 3268 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3269 | { |
| 3270 | MagickBooleanType |
| 3271 | proceed; |
| 3272 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3273 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3274 | #pragma omp critical (MagickCore_ModulateImage) |
| 3275 | #endif |
| 3276 | proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows); |
| 3277 | if (proceed == MagickFalse) |
| 3278 | status=MagickFalse; |
| 3279 | } |
| 3280 | } |
| 3281 | image_view=DestroyCacheView(image_view); |
| 3282 | return(status); |
| 3283 | } |
| 3284 | |
| 3285 | /* |
| 3286 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3287 | % % |
| 3288 | % % |
| 3289 | % % |
| 3290 | % N e g a t e I m a g e % |
| 3291 | % % |
| 3292 | % % |
| 3293 | % % |
| 3294 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3295 | % |
| 3296 | % NegateImage() negates the colors in the reference image. The grayscale |
| 3297 | % option means that only grayscale values within the image are negated. |
| 3298 | % |
| 3299 | % The format of the NegateImageChannel method is: |
| 3300 | % |
| 3301 | % MagickBooleanType NegateImage(Image *image, |
| 3302 | % const MagickBooleanType grayscale) |
| 3303 | % MagickBooleanType NegateImageChannel(Image *image, |
| 3304 | % const ChannelType channel,const MagickBooleanType grayscale) |
| 3305 | % |
| 3306 | % A description of each parameter follows: |
| 3307 | % |
| 3308 | % o image: the image. |
| 3309 | % |
| 3310 | % o channel: the channel. |
| 3311 | % |
| 3312 | % o grayscale: If MagickTrue, only negate grayscale pixels within the image. |
| 3313 | % |
| 3314 | */ |
| 3315 | |
| 3316 | MagickExport MagickBooleanType NegateImage(Image *image, |
| 3317 | const MagickBooleanType grayscale) |
| 3318 | { |
| 3319 | MagickBooleanType |
| 3320 | status; |
| 3321 | |
| 3322 | status=NegateImageChannel(image,DefaultChannels,grayscale); |
| 3323 | return(status); |
| 3324 | } |
| 3325 | |
| 3326 | MagickExport MagickBooleanType NegateImageChannel(Image *image, |
| 3327 | const ChannelType channel,const MagickBooleanType grayscale) |
| 3328 | { |
| 3329 | #define NegateImageTag "Negate/Image" |
| 3330 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3331 | CacheView |
| 3332 | *image_view; |
| 3333 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3334 | ExceptionInfo |
| 3335 | *exception; |
| 3336 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3337 | MagickBooleanType |
| 3338 | status; |
| 3339 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3340 | MagickOffsetType |
| 3341 | progress; |
| 3342 | |
| 3343 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3344 | i; |
| 3345 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3346 | ssize_t |
| 3347 | y; |
| 3348 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3349 | assert(image != (Image *) NULL); |
| 3350 | assert(image->signature == MagickSignature); |
| 3351 | if (image->debug != MagickFalse) |
| 3352 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3353 | if (image->storage_class == PseudoClass) |
| 3354 | { |
| 3355 | /* |
| 3356 | Negate colormap. |
| 3357 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3358 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3359 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3360 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3361 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3362 | { |
| 3363 | if (grayscale != MagickFalse) |
| 3364 | if ((image->colormap[i].red != image->colormap[i].green) || |
| 3365 | (image->colormap[i].green != image->colormap[i].blue)) |
| 3366 | continue; |
| 3367 | if ((channel & RedChannel) != 0) |
| 3368 | image->colormap[i].red=(Quantum) QuantumRange- |
| 3369 | image->colormap[i].red; |
| 3370 | if ((channel & GreenChannel) != 0) |
| 3371 | image->colormap[i].green=(Quantum) QuantumRange- |
| 3372 | image->colormap[i].green; |
| 3373 | if ((channel & BlueChannel) != 0) |
| 3374 | image->colormap[i].blue=(Quantum) QuantumRange- |
| 3375 | image->colormap[i].blue; |
| 3376 | } |
| 3377 | } |
| 3378 | /* |
| 3379 | Negate image. |
| 3380 | */ |
| 3381 | status=MagickTrue; |
| 3382 | progress=0; |
| 3383 | exception=(&image->exception); |
| 3384 | image_view=AcquireCacheView(image); |
| 3385 | if (grayscale != MagickFalse) |
| 3386 | { |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3387 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3388 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3389 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3390 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3391 | { |
| 3392 | MagickBooleanType |
| 3393 | sync; |
| 3394 | |
| 3395 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3396 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3397 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3398 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3399 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3400 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3401 | register ssize_t |
| 3402 | x; |
| 3403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3404 | if (status == MagickFalse) |
| 3405 | continue; |
| 3406 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 3407 | exception); |
| 3408 | if (q == (PixelPacket *) NULL) |
| 3409 | { |
| 3410 | status=MagickFalse; |
| 3411 | continue; |
| 3412 | } |
| 3413 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3414 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3415 | { |
| 3416 | if ((q->red != q->green) || (q->green != q->blue)) |
| 3417 | { |
| 3418 | q++; |
| 3419 | continue; |
| 3420 | } |
| 3421 | if ((channel & RedChannel) != 0) |
| 3422 | q->red=(Quantum) QuantumRange-q->red; |
| 3423 | if ((channel & GreenChannel) != 0) |
| 3424 | q->green=(Quantum) QuantumRange-q->green; |
| 3425 | if ((channel & BlueChannel) != 0) |
| 3426 | q->blue=(Quantum) QuantumRange-q->blue; |
| 3427 | if ((channel & OpacityChannel) != 0) |
| 3428 | q->opacity=(Quantum) QuantumRange-q->opacity; |
| 3429 | if (((channel & IndexChannel) != 0) && |
| 3430 | (image->colorspace == CMYKColorspace)) |
| 3431 | indexes[x]=(IndexPacket) QuantumRange-indexes[x]; |
| 3432 | q++; |
| 3433 | } |
| 3434 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
| 3435 | if (sync == MagickFalse) |
| 3436 | status=MagickFalse; |
| 3437 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3438 | { |
| 3439 | MagickBooleanType |
| 3440 | proceed; |
| 3441 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3442 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3443 | #pragma omp critical (MagickCore_NegateImageChannel) |
| 3444 | #endif |
| 3445 | proceed=SetImageProgress(image,NegateImageTag,progress++, |
| 3446 | image->rows); |
| 3447 | if (proceed == MagickFalse) |
| 3448 | status=MagickFalse; |
| 3449 | } |
| 3450 | } |
| 3451 | image_view=DestroyCacheView(image_view); |
| 3452 | return(MagickTrue); |
| 3453 | } |
| 3454 | /* |
| 3455 | Negate image. |
| 3456 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3457 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3458 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3459 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3460 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3461 | { |
| 3462 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3463 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3464 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3465 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3466 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3467 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3468 | register ssize_t |
| 3469 | x; |
| 3470 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3471 | if (status == MagickFalse) |
| 3472 | continue; |
| 3473 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 3474 | if (q == (PixelPacket *) NULL) |
| 3475 | { |
| 3476 | status=MagickFalse; |
| 3477 | continue; |
| 3478 | } |
| 3479 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3480 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3481 | { |
| 3482 | if ((channel & RedChannel) != 0) |
| 3483 | q->red=(Quantum) QuantumRange-q->red; |
| 3484 | if ((channel & GreenChannel) != 0) |
| 3485 | q->green=(Quantum) QuantumRange-q->green; |
| 3486 | if ((channel & BlueChannel) != 0) |
| 3487 | q->blue=(Quantum) QuantumRange-q->blue; |
| 3488 | if ((channel & OpacityChannel) != 0) |
| 3489 | q->opacity=(Quantum) QuantumRange-q->opacity; |
| 3490 | if (((channel & IndexChannel) != 0) && |
| 3491 | (image->colorspace == CMYKColorspace)) |
| 3492 | indexes[x]=(IndexPacket) QuantumRange-indexes[x]; |
| 3493 | q++; |
| 3494 | } |
| 3495 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3496 | status=MagickFalse; |
| 3497 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3498 | { |
| 3499 | MagickBooleanType |
| 3500 | proceed; |
| 3501 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3502 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3503 | #pragma omp critical (MagickCore_NegateImageChannel) |
| 3504 | #endif |
| 3505 | proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows); |
| 3506 | if (proceed == MagickFalse) |
| 3507 | status=MagickFalse; |
| 3508 | } |
| 3509 | } |
| 3510 | image_view=DestroyCacheView(image_view); |
| 3511 | return(status); |
| 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3516 | % % |
| 3517 | % % |
| 3518 | % % |
| 3519 | % N o r m a l i z e I m a g e % |
| 3520 | % % |
| 3521 | % % |
| 3522 | % % |
| 3523 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3524 | % |
| 3525 | % The NormalizeImage() method enhances the contrast of a color image by |
| 3526 | % mapping the darkest 2 percent of all pixel to black and the brightest |
| 3527 | % 1 percent to white. |
| 3528 | % |
| 3529 | % The format of the NormalizeImage method is: |
| 3530 | % |
| 3531 | % MagickBooleanType NormalizeImage(Image *image) |
| 3532 | % MagickBooleanType NormalizeImageChannel(Image *image, |
| 3533 | % const ChannelType channel) |
| 3534 | % |
| 3535 | % A description of each parameter follows: |
| 3536 | % |
| 3537 | % o image: the image. |
| 3538 | % |
| 3539 | % o channel: the channel. |
| 3540 | % |
| 3541 | */ |
| 3542 | |
| 3543 | MagickExport MagickBooleanType NormalizeImage(Image *image) |
| 3544 | { |
| 3545 | MagickBooleanType |
| 3546 | status; |
| 3547 | |
| 3548 | status=NormalizeImageChannel(image,DefaultChannels); |
| 3549 | return(status); |
| 3550 | } |
| 3551 | |
| 3552 | MagickExport MagickBooleanType NormalizeImageChannel(Image *image, |
| 3553 | const ChannelType channel) |
| 3554 | { |
| 3555 | double |
| 3556 | black_point, |
| 3557 | white_point; |
| 3558 | |
cristy | 530239c | 2010-07-25 17:34:26 +0000 | [diff] [blame] | 3559 | black_point=(double) image->columns*image->rows*0.0015; |
| 3560 | white_point=(double) image->columns*image->rows*0.9995; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3561 | return(ContrastStretchImageChannel(image,channel,black_point,white_point)); |
| 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3566 | % % |
| 3567 | % % |
| 3568 | % % |
| 3569 | % S i g m o i d a l C o n t r a s t I m a g e % |
| 3570 | % % |
| 3571 | % % |
| 3572 | % % |
| 3573 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3574 | % |
| 3575 | % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear |
| 3576 | % sigmoidal contrast algorithm. Increase the contrast of the image using a |
| 3577 | % sigmoidal transfer function without saturating highlights or shadows. |
| 3578 | % Contrast indicates how much to increase the contrast (0 is none; 3 is |
| 3579 | % typical; 20 is pushing it); mid-point indicates where midtones fall in the |
| 3580 | % resultant image (0 is white; 50% is middle-gray; 100% is black). Set |
| 3581 | % sharpen to MagickTrue to increase the image contrast otherwise the contrast |
| 3582 | % is reduced. |
| 3583 | % |
| 3584 | % The format of the SigmoidalContrastImage method is: |
| 3585 | % |
| 3586 | % MagickBooleanType SigmoidalContrastImage(Image *image, |
| 3587 | % const MagickBooleanType sharpen,const char *levels) |
| 3588 | % MagickBooleanType SigmoidalContrastImageChannel(Image *image, |
| 3589 | % const ChannelType channel,const MagickBooleanType sharpen, |
| 3590 | % const double contrast,const double midpoint) |
| 3591 | % |
| 3592 | % A description of each parameter follows: |
| 3593 | % |
| 3594 | % o image: the image. |
| 3595 | % |
| 3596 | % o channel: the channel. |
| 3597 | % |
| 3598 | % o sharpen: Increase or decrease image contrast. |
| 3599 | % |
cristy | fa76958 | 2010-09-30 23:30:03 +0000 | [diff] [blame] | 3600 | % o alpha: strength of the contrast, the larger the number the more |
| 3601 | % 'threshold-like' it becomes. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3602 | % |
cristy | fa76958 | 2010-09-30 23:30:03 +0000 | [diff] [blame] | 3603 | % o beta: midpoint of the function as a color value 0 to QuantumRange. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3604 | % |
| 3605 | */ |
| 3606 | |
| 3607 | MagickExport MagickBooleanType SigmoidalContrastImage(Image *image, |
| 3608 | const MagickBooleanType sharpen,const char *levels) |
| 3609 | { |
| 3610 | GeometryInfo |
| 3611 | geometry_info; |
| 3612 | |
| 3613 | MagickBooleanType |
| 3614 | status; |
| 3615 | |
| 3616 | MagickStatusType |
| 3617 | flags; |
| 3618 | |
| 3619 | flags=ParseGeometry(levels,&geometry_info); |
| 3620 | if ((flags & SigmaValue) == 0) |
| 3621 | geometry_info.sigma=1.0*QuantumRange/2.0; |
| 3622 | if ((flags & PercentValue) != 0) |
| 3623 | geometry_info.sigma=1.0*QuantumRange*geometry_info.sigma/100.0; |
| 3624 | status=SigmoidalContrastImageChannel(image,DefaultChannels,sharpen, |
| 3625 | geometry_info.rho,geometry_info.sigma); |
| 3626 | return(status); |
| 3627 | } |
| 3628 | |
| 3629 | MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image, |
| 3630 | const ChannelType channel,const MagickBooleanType sharpen, |
| 3631 | const double contrast,const double midpoint) |
| 3632 | { |
| 3633 | #define SigmoidalContrastImageTag "SigmoidalContrast/Image" |
| 3634 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3635 | CacheView |
| 3636 | *image_view; |
| 3637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3638 | ExceptionInfo |
| 3639 | *exception; |
| 3640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3641 | MagickBooleanType |
| 3642 | status; |
| 3643 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3644 | MagickOffsetType |
| 3645 | progress; |
| 3646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3647 | MagickRealType |
| 3648 | *sigmoidal_map; |
| 3649 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3650 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3651 | i; |
| 3652 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3653 | ssize_t |
| 3654 | y; |
| 3655 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3656 | /* |
| 3657 | Allocate and initialize sigmoidal maps. |
| 3658 | */ |
| 3659 | assert(image != (Image *) NULL); |
| 3660 | assert(image->signature == MagickSignature); |
| 3661 | if (image->debug != MagickFalse) |
| 3662 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3663 | sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL, |
| 3664 | sizeof(*sigmoidal_map)); |
| 3665 | if (sigmoidal_map == (MagickRealType *) NULL) |
| 3666 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3667 | image->filename); |
| 3668 | (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map)); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3669 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3670 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3671 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3672 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3673 | { |
| 3674 | if (sharpen != MagickFalse) |
| 3675 | { |
| 3676 | sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 3677 | (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange- |
| 3678 | (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/ |
| 3679 | (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/ |
| 3680 | (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/ |
| 3681 | (double) QuantumRange)))))+0.5)); |
| 3682 | continue; |
| 3683 | } |
| 3684 | sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType) |
| 3685 | (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/ |
| 3686 | (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/ |
| 3687 | (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/ |
| 3688 | (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/ |
| 3689 | (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+ |
| 3690 | ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/ |
| 3691 | (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/ |
| 3692 | (double) QuantumRange*contrast))))))/contrast))); |
| 3693 | } |
| 3694 | if (image->storage_class == PseudoClass) |
| 3695 | { |
| 3696 | /* |
| 3697 | Sigmoidal-contrast enhance colormap. |
| 3698 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3699 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3700 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3701 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3702 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3703 | { |
| 3704 | if ((channel & RedChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3705 | image->colormap[i].red=ClampToQuantum(sigmoidal_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3706 | ScaleQuantumToMap(image->colormap[i].red)]); |
| 3707 | if ((channel & GreenChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3708 | image->colormap[i].green=ClampToQuantum(sigmoidal_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3709 | ScaleQuantumToMap(image->colormap[i].green)]); |
| 3710 | if ((channel & BlueChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3711 | image->colormap[i].blue=ClampToQuantum(sigmoidal_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3712 | ScaleQuantumToMap(image->colormap[i].blue)]); |
| 3713 | if ((channel & OpacityChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3714 | image->colormap[i].opacity=ClampToQuantum(sigmoidal_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3715 | ScaleQuantumToMap(image->colormap[i].opacity)]); |
| 3716 | } |
| 3717 | } |
| 3718 | /* |
| 3719 | Sigmoidal-contrast enhance image. |
| 3720 | */ |
| 3721 | status=MagickTrue; |
| 3722 | progress=0; |
| 3723 | exception=(&image->exception); |
| 3724 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3725 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3726 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3727 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3728 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3729 | { |
| 3730 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3731 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3732 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3733 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3734 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3735 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3736 | register ssize_t |
| 3737 | x; |
| 3738 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3739 | if (status == MagickFalse) |
| 3740 | continue; |
| 3741 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 3742 | if (q == (PixelPacket *) NULL) |
| 3743 | { |
| 3744 | status=MagickFalse; |
| 3745 | continue; |
| 3746 | } |
| 3747 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3748 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3749 | { |
| 3750 | if ((channel & RedChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3751 | q->red=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->red)]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3752 | if ((channel & GreenChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3753 | q->green=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->green)]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3754 | if ((channel & BlueChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3755 | q->blue=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->blue)]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3756 | if ((channel & OpacityChannel) != 0) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3757 | q->opacity=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->opacity)]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3758 | if (((channel & IndexChannel) != 0) && |
| 3759 | (image->colorspace == CMYKColorspace)) |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 3760 | indexes[x]=(IndexPacket) ClampToQuantum(sigmoidal_map[ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3761 | ScaleQuantumToMap(indexes[x])]); |
| 3762 | q++; |
| 3763 | } |
| 3764 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3765 | status=MagickFalse; |
| 3766 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3767 | { |
| 3768 | MagickBooleanType |
| 3769 | proceed; |
| 3770 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3771 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3772 | #pragma omp critical (MagickCore_SigmoidalContrastImageChannel) |
| 3773 | #endif |
| 3774 | proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++, |
| 3775 | image->rows); |
| 3776 | if (proceed == MagickFalse) |
| 3777 | status=MagickFalse; |
| 3778 | } |
| 3779 | } |
| 3780 | image_view=DestroyCacheView(image_view); |
| 3781 | sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map); |
| 3782 | return(status); |
| 3783 | } |