blob: ac870bc41a513f109f924b77267917e68f0b67cc [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace.h"
cristy0898eba2012-04-09 16:38:29 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/composite-private.h"
52#include "MagickCore/enhance.h"
53#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/fx.h"
56#include "MagickCore/gem.h"
cristyd1dd6e42011-09-04 01:46:08 +000057#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000058#include "MagickCore/geometry.h"
59#include "MagickCore/histogram.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/monitor.h"
64#include "MagickCore/monitor-private.h"
65#include "MagickCore/option.h"
cristy1e0fe422012-04-11 18:43:52 +000066#include "MagickCore/pixel.h"
cristy4c08aed2011-07-01 19:47:50 +000067#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/quantum.h"
69#include "MagickCore/quantum-private.h"
70#include "MagickCore/resample.h"
71#include "MagickCore/resample-private.h"
cristyac245f82012-05-05 17:13:57 +000072#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000073#include "MagickCore/statistic.h"
74#include "MagickCore/string_.h"
75#include "MagickCore/string-private.h"
76#include "MagickCore/thread-private.h"
77#include "MagickCore/token.h"
78#include "MagickCore/xml-tree.h"
cristy433d1182011-09-04 13:38:52 +000079#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000080
81/*
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83% %
84% %
85% %
86% A u t o G a m m a I m a g e %
87% %
88% %
89% %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91%
92% AutoGammaImage() extract the 'mean' from the image and adjust the image
93% to try make set its gamma appropriatally.
94%
cristy308b4e62009-09-21 14:40:44 +000095% The format of the AutoGammaImage method is:
cristy3ed852e2009-09-05 21:47:34 +000096%
cristy95111202011-08-09 19:41:42 +000097% MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000098%
99% A description of each parameter follows:
100%
101% o image: The image to auto-level
102%
cristy95111202011-08-09 19:41:42 +0000103% o exception: return any errors or warnings in this structure.
104%
cristy3ed852e2009-09-05 21:47:34 +0000105*/
cristy95111202011-08-09 19:41:42 +0000106MagickExport MagickBooleanType AutoGammaImage(Image *image,
107 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000108{
cristy3ed852e2009-09-05 21:47:34 +0000109 double
cristy4c08aed2011-07-01 19:47:50 +0000110 gamma,
111 log_mean,
112 mean,
113 sans;
anthony4efe5972009-09-11 06:46:40 +0000114
cristy95111202011-08-09 19:41:42 +0000115 MagickStatusType
116 status;
117
cristy01e9afd2011-08-10 17:38:41 +0000118 register ssize_t
119 i;
120
cristy4c08aed2011-07-01 19:47:50 +0000121 log_mean=log(0.5);
cristy5f95f4f2011-10-23 01:01:01 +0000122 if (image->channel_mask == DefaultChannels)
cristy3ed852e2009-09-05 21:47:34 +0000123 {
124 /*
cristy01e9afd2011-08-10 17:38:41 +0000125 Apply gamma correction equally across all given channels.
cristy3ed852e2009-09-05 21:47:34 +0000126 */
cristy95111202011-08-09 19:41:42 +0000127 (void) GetImageMean(image,&mean,&sans,exception);
cristy4c08aed2011-07-01 19:47:50 +0000128 gamma=log(mean*QuantumScale)/log_mean;
cristy01e9afd2011-08-10 17:38:41 +0000129 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
cristy3ed852e2009-09-05 21:47:34 +0000130 }
cristy3ed852e2009-09-05 21:47:34 +0000131 /*
cristy4c08aed2011-07-01 19:47:50 +0000132 Auto-gamma each channel separately.
cristy3ed852e2009-09-05 21:47:34 +0000133 */
cristy4c08aed2011-07-01 19:47:50 +0000134 status=MagickTrue;
cristy01e9afd2011-08-10 17:38:41 +0000135 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
136 {
cristybd5a96c2011-08-21 00:04:26 +0000137 ChannelType
138 channel_mask;
139
cristyabace412011-12-11 15:56:53 +0000140 PixelChannel
141 channel;
142
cristy01e9afd2011-08-10 17:38:41 +0000143 PixelTrait
144 traits;
145
cristyabace412011-12-11 15:56:53 +0000146 channel=GetPixelChannelMapChannel(image,i);
147 traits=GetPixelChannelMapTraits(image,channel);
cristy01e9afd2011-08-10 17:38:41 +0000148 if ((traits & UpdatePixelTrait) == 0)
149 continue;
cristya13d0822011-09-19 00:19:19 +0000150 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
cristy01e9afd2011-08-10 17:38:41 +0000151 status=GetImageMean(image,&mean,&sans,exception);
152 gamma=log(mean*QuantumScale)/log_mean;
153 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
cristybd5a96c2011-08-21 00:04:26 +0000154 (void) SetPixelChannelMask(image,channel_mask);
cristy01e9afd2011-08-10 17:38:41 +0000155 if (status == MagickFalse)
156 break;
157 }
cristy3ed852e2009-09-05 21:47:34 +0000158 return(status != 0 ? MagickTrue : MagickFalse);
159}
160
161/*
162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163% %
164% %
165% %
166% A u t o L e v e l I m a g e %
167% %
168% %
169% %
170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171%
172% AutoLevelImage() adjusts the levels of a particular image channel by
173% scaling the minimum and maximum values to the full quantum range.
174%
175% The format of the LevelImage method is:
176%
cristy95111202011-08-09 19:41:42 +0000177% MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000178%
179% A description of each parameter follows:
180%
181% o image: The image to auto-level
182%
cristy95111202011-08-09 19:41:42 +0000183% o exception: return any errors or warnings in this structure.
184%
cristy3ed852e2009-09-05 21:47:34 +0000185*/
cristy95111202011-08-09 19:41:42 +0000186MagickExport MagickBooleanType AutoLevelImage(Image *image,
187 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000188{
cristyb303c3d2011-09-09 11:24:40 +0000189 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
cristy3ed852e2009-09-05 21:47:34 +0000190}
191
192/*
193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194% %
195% %
196% %
cristya28d6b82010-01-11 20:03:47 +0000197% B r i g h t n e s s C o n t r a s t I m a g e %
198% %
199% %
200% %
201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202%
cristyf4356f92011-08-01 15:33:48 +0000203% BrightnessContrastImage() changes the brightness and/or contrast of an
204% image. It converts the brightness and contrast parameters into slope and
205% intercept and calls a polynomical function to apply to the image.
cristya28d6b82010-01-11 20:03:47 +0000206%
207% The format of the BrightnessContrastImage method is:
208%
209% MagickBooleanType BrightnessContrastImage(Image *image,
cristy444eda62011-08-10 02:07:46 +0000210% const double brightness,const double contrast,ExceptionInfo *exception)
cristya28d6b82010-01-11 20:03:47 +0000211%
212% A description of each parameter follows:
213%
214% o image: the image.
215%
cristya28d6b82010-01-11 20:03:47 +0000216% o brightness: the brightness percent (-100 .. 100).
217%
218% o contrast: the contrast percent (-100 .. 100).
219%
cristy444eda62011-08-10 02:07:46 +0000220% o exception: return any errors or warnings in this structure.
221%
cristya28d6b82010-01-11 20:03:47 +0000222*/
cristya28d6b82010-01-11 20:03:47 +0000223MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
cristy444eda62011-08-10 02:07:46 +0000224 const double brightness,const double contrast,ExceptionInfo *exception)
cristya28d6b82010-01-11 20:03:47 +0000225{
cristya28d6b82010-01-11 20:03:47 +0000226#define BrightnessContastImageTag "BrightnessContast/Image"
227
228 double
229 alpha,
cristya28d6b82010-01-11 20:03:47 +0000230 coefficients[2],
cristye23ec9d2011-08-16 18:15:40 +0000231 intercept,
cristya28d6b82010-01-11 20:03:47 +0000232 slope;
233
234 MagickBooleanType
235 status;
236
237 /*
238 Compute slope and intercept.
239 */
240 assert(image != (Image *) NULL);
241 assert(image->signature == MagickSignature);
242 if (image->debug != MagickFalse)
243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
244 alpha=contrast;
cristy4205a3c2010-09-12 20:19:59 +0000245 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
cristya28d6b82010-01-11 20:03:47 +0000246 if (slope < 0.0)
247 slope=0.0;
248 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
249 coefficients[0]=slope;
250 coefficients[1]=intercept;
cristy444eda62011-08-10 02:07:46 +0000251 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
252 return(status);
253}
254
255/*
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257% %
258% %
259% %
260% C l u t I m a g e %
261% %
262% %
263% %
264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265%
266% ClutImage() replaces each color value in the given image, by using it as an
267% index to lookup a replacement color value in a Color Look UP Table in the
268% form of an image. The values are extracted along a diagonal of the CLUT
269% image so either a horizontal or vertial gradient image can be used.
270%
271% Typically this is used to either re-color a gray-scale image according to a
272% color gradient in the CLUT image, or to perform a freeform histogram
273% (level) adjustment according to the (typically gray-scale) gradient in the
274% CLUT image.
275%
276% When the 'channel' mask includes the matte/alpha transparency channel but
277% one image has no such channel it is assumed that that image is a simple
278% gray-scale image that will effect the alpha channel values, either for
279% gray-scale coloring (with transparent or semi-transparent colors), or
280% a histogram adjustment of existing alpha channel values. If both images
281% have matte channels, direct and normal indexing is applied, which is rarely
282% used.
283%
284% The format of the ClutImage method is:
285%
286% MagickBooleanType ClutImage(Image *image,Image *clut_image,
cristy5c4e2582011-09-11 19:21:03 +0000287% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy444eda62011-08-10 02:07:46 +0000288%
289% A description of each parameter follows:
290%
291% o image: the image, which is replaced by indexed CLUT values
292%
293% o clut_image: the color lookup table image for replacement color values.
294%
cristy5c4e2582011-09-11 19:21:03 +0000295% o method: the pixel interpolation method.
cristy444eda62011-08-10 02:07:46 +0000296%
297% o exception: return any errors or warnings in this structure.
298%
299*/
300MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
cristy5c4e2582011-09-11 19:21:03 +0000301 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy444eda62011-08-10 02:07:46 +0000302{
cristy444eda62011-08-10 02:07:46 +0000303#define ClutImageTag "Clut/Image"
304
305 CacheView
306 *clut_view,
307 *image_view;
308
cristy444eda62011-08-10 02:07:46 +0000309 MagickBooleanType
310 status;
311
312 MagickOffsetType
313 progress;
314
cristy1e0fe422012-04-11 18:43:52 +0000315 PixelInfo
316 *clut_map;
cristy444eda62011-08-10 02:07:46 +0000317
cristy1e0fe422012-04-11 18:43:52 +0000318 register ssize_t
319 i;
320
321 ssize_t adjust,
cristy444eda62011-08-10 02:07:46 +0000322 y;
323
324 assert(image != (Image *) NULL);
325 assert(image->signature == MagickSignature);
326 if (image->debug != MagickFalse)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
328 assert(clut_image != (Image *) NULL);
329 assert(clut_image->signature == MagickSignature);
330 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
331 return(MagickFalse);
cristy0898eba2012-04-09 16:38:29 +0000332 if (IsGrayColorspace(image->colorspace) != MagickFalse)
333 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy1e0fe422012-04-11 18:43:52 +0000334 clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
335 if (clut_map == (PixelInfo *) NULL)
cristy444eda62011-08-10 02:07:46 +0000336 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
337 image->filename);
338 /*
339 Clut image.
340 */
341 status=MagickTrue;
342 progress=0;
343 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
cristydb070952012-04-20 14:33:00 +0000344 clut_view=AcquireVirtualCacheView(clut_image,exception);
cristy444eda62011-08-10 02:07:46 +0000345#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000346 #pragma omp parallel for schedule(static,4) \
cristyddacdd12012-05-07 23:08:14 +0000347 dynamic_number_threads(image->columns,1,1)
cristy444eda62011-08-10 02:07:46 +0000348#endif
cristy1e0fe422012-04-11 18:43:52 +0000349 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy444eda62011-08-10 02:07:46 +0000350 {
cristy1e0fe422012-04-11 18:43:52 +0000351 GetPixelInfo(clut_image,clut_map+i);
anthony6feb7802012-04-12 06:24:29 +0000352 (void) InterpolatePixelInfo(clut_image,clut_view,method,
cristy1e0fe422012-04-11 18:43:52 +0000353 QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i*
354 (clut_image->rows-adjust),clut_map+i,exception);
cristy444eda62011-08-10 02:07:46 +0000355 }
356 clut_view=DestroyCacheView(clut_view);
cristydb070952012-04-20 14:33:00 +0000357 image_view=AcquireAuthenticCacheView(image,exception);
cristy444eda62011-08-10 02:07:46 +0000358#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000359 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000360 dynamic_number_threads(image->columns,image->rows,1)
cristy444eda62011-08-10 02:07:46 +0000361#endif
362 for (y=0; y < (ssize_t) image->rows; y++)
363 {
cristy1e0fe422012-04-11 18:43:52 +0000364 PixelInfo
365 pixel;
366
cristy444eda62011-08-10 02:07:46 +0000367 register Quantum
368 *restrict q;
369
370 register ssize_t
371 x;
372
373 if (status == MagickFalse)
374 continue;
375 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
376 if (q == (Quantum *) NULL)
377 {
378 status=MagickFalse;
379 continue;
380 }
cristy1e0fe422012-04-11 18:43:52 +0000381 GetPixelInfo(image,&pixel);
cristy444eda62011-08-10 02:07:46 +0000382 for (x=0; x < (ssize_t) image->columns; x++)
383 {
cristy10a6c612012-01-29 21:41:05 +0000384 if (GetPixelMask(image,q) != 0)
385 {
386 q+=GetPixelChannels(image);
387 continue;
388 }
cristy1e0fe422012-04-11 18:43:52 +0000389 GetPixelInfoPixel(image,q,&pixel);
cristyac245f82012-05-05 17:13:57 +0000390 pixel.red=clut_map[ScaleQuantumToMap(
391 ClampToQuantum(pixel.red))].red;
392 pixel.green=clut_map[ScaleQuantumToMap(
393 ClampToQuantum(pixel.green))].green;
394 pixel.blue=clut_map[ScaleQuantumToMap(
395 ClampToQuantum(pixel.blue))].blue;
396 pixel.black=clut_map[ScaleQuantumToMap(
397 ClampToQuantum(pixel.black))].black;
398 pixel.alpha=clut_map[ScaleQuantumToMap(
399 ClampToQuantum(pixel.alpha))].alpha;
cristy1e0fe422012-04-11 18:43:52 +0000400 SetPixelInfoPixel(image,&pixel,q);
cristy444eda62011-08-10 02:07:46 +0000401 q+=GetPixelChannels(image);
402 }
403 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
404 status=MagickFalse;
405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
406 {
407 MagickBooleanType
408 proceed;
409
410#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000411 #pragma omp critical (MagickCore_ClutImage)
cristy444eda62011-08-10 02:07:46 +0000412#endif
413 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
414 if (proceed == MagickFalse)
415 status=MagickFalse;
416 }
417 }
418 image_view=DestroyCacheView(image_view);
cristy1e0fe422012-04-11 18:43:52 +0000419 clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
cristy444eda62011-08-10 02:07:46 +0000420 if ((clut_image->matte != MagickFalse) &&
421 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
422 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
cristya28d6b82010-01-11 20:03:47 +0000423 return(status);
424}
425
426/*
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428% %
429% %
430% %
cristy3ed852e2009-09-05 21:47:34 +0000431% C o l o r D e c i s i o n L i s t I m a g e %
432% %
433% %
434% %
435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436%
437% ColorDecisionListImage() accepts a lightweight Color Correction Collection
438% (CCC) file which solely contains one or more color corrections and applies
439% the correction to the image. Here is a sample CCC file:
440%
441% <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
442% <ColorCorrection id="cc03345">
443% <SOPNode>
444% <Slope> 0.9 1.2 0.5 </Slope>
445% <Offset> 0.4 -0.5 0.6 </Offset>
446% <Power> 1.0 0.8 1.5 </Power>
447% </SOPNode>
448% <SATNode>
449% <Saturation> 0.85 </Saturation>
450% </SATNode>
451% </ColorCorrection>
452% </ColorCorrectionCollection>
453%
454% which includes the slop, offset, and power for each of the RGB channels
455% as well as the saturation.
456%
457% The format of the ColorDecisionListImage method is:
458%
459% MagickBooleanType ColorDecisionListImage(Image *image,
cristy1bfa9f02011-08-11 02:35:43 +0000460% const char *color_correction_collection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000461%
462% A description of each parameter follows:
463%
464% o image: the image.
465%
466% o color_correction_collection: the color correction collection in XML.
467%
cristy1bfa9f02011-08-11 02:35:43 +0000468% o exception: return any errors or warnings in this structure.
469%
cristy3ed852e2009-09-05 21:47:34 +0000470*/
471MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
cristy1bfa9f02011-08-11 02:35:43 +0000472 const char *color_correction_collection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000473{
474#define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
475
476 typedef struct _Correction
477 {
478 double
479 slope,
480 offset,
481 power;
482 } Correction;
483
484 typedef struct _ColorCorrection
485 {
486 Correction
487 red,
488 green,
489 blue;
490
491 double
492 saturation;
493 } ColorCorrection;
494
cristyc4c8d132010-01-07 01:58:38 +0000495 CacheView
496 *image_view;
497
cristy3ed852e2009-09-05 21:47:34 +0000498 char
499 token[MaxTextExtent];
500
501 ColorCorrection
502 color_correction;
503
504 const char
505 *content,
506 *p;
507
cristy3ed852e2009-09-05 21:47:34 +0000508 MagickBooleanType
509 status;
510
cristybb503372010-05-27 20:51:26 +0000511 MagickOffsetType
512 progress;
513
cristy101ab702011-10-13 13:06:32 +0000514 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000515 *cdl_map;
516
cristybb503372010-05-27 20:51:26 +0000517 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000518 i;
519
cristybb503372010-05-27 20:51:26 +0000520 ssize_t
521 y;
522
cristy3ed852e2009-09-05 21:47:34 +0000523 XMLTreeInfo
524 *cc,
525 *ccc,
526 *sat,
527 *sop;
528
cristy3ed852e2009-09-05 21:47:34 +0000529 /*
530 Allocate and initialize cdl maps.
531 */
532 assert(image != (Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 if (color_correction_collection == (const char *) NULL)
537 return(MagickFalse);
cristy1bfa9f02011-08-11 02:35:43 +0000538 ccc=NewXMLTree((const char *) color_correction_collection,exception);
cristy3ed852e2009-09-05 21:47:34 +0000539 if (ccc == (XMLTreeInfo *) NULL)
540 return(MagickFalse);
541 cc=GetXMLTreeChild(ccc,"ColorCorrection");
542 if (cc == (XMLTreeInfo *) NULL)
543 {
544 ccc=DestroyXMLTree(ccc);
545 return(MagickFalse);
546 }
547 color_correction.red.slope=1.0;
548 color_correction.red.offset=0.0;
549 color_correction.red.power=1.0;
550 color_correction.green.slope=1.0;
551 color_correction.green.offset=0.0;
552 color_correction.green.power=1.0;
553 color_correction.blue.slope=1.0;
554 color_correction.blue.offset=0.0;
555 color_correction.blue.power=1.0;
556 color_correction.saturation=0.0;
557 sop=GetXMLTreeChild(cc,"SOPNode");
558 if (sop != (XMLTreeInfo *) NULL)
559 {
560 XMLTreeInfo
561 *offset,
562 *power,
563 *slope;
564
565 slope=GetXMLTreeChild(sop,"Slope");
566 if (slope != (XMLTreeInfo *) NULL)
567 {
568 content=GetXMLTreeContent(slope);
569 p=(const char *) content;
570 for (i=0; (*p != '\0') && (i < 3); i++)
571 {
572 GetMagickToken(p,&p,token);
573 if (*token == ',')
574 GetMagickToken(p,&p,token);
575 switch (i)
576 {
cristyc1acd842011-05-19 23:05:47 +0000577 case 0:
578 {
cristy9b34e302011-11-05 02:15:45 +0000579 color_correction.red.slope=StringToDouble(token,(char **) NULL);
cristyc1acd842011-05-19 23:05:47 +0000580 break;
581 }
582 case 1:
583 {
cristydbdd0e32011-11-04 23:29:40 +0000584 color_correction.green.slope=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000585 (char **) NULL);
586 break;
587 }
588 case 2:
589 {
cristydbdd0e32011-11-04 23:29:40 +0000590 color_correction.blue.slope=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000591 (char **) NULL);
592 break;
593 }
cristy3ed852e2009-09-05 21:47:34 +0000594 }
595 }
596 }
597 offset=GetXMLTreeChild(sop,"Offset");
598 if (offset != (XMLTreeInfo *) NULL)
599 {
600 content=GetXMLTreeContent(offset);
601 p=(const char *) content;
602 for (i=0; (*p != '\0') && (i < 3); i++)
603 {
604 GetMagickToken(p,&p,token);
605 if (*token == ',')
606 GetMagickToken(p,&p,token);
607 switch (i)
608 {
cristyc1acd842011-05-19 23:05:47 +0000609 case 0:
610 {
cristydbdd0e32011-11-04 23:29:40 +0000611 color_correction.red.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000612 (char **) NULL);
613 break;
614 }
615 case 1:
616 {
cristydbdd0e32011-11-04 23:29:40 +0000617 color_correction.green.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000618 (char **) NULL);
619 break;
620 }
621 case 2:
622 {
cristydbdd0e32011-11-04 23:29:40 +0000623 color_correction.blue.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000624 (char **) NULL);
625 break;
626 }
cristy3ed852e2009-09-05 21:47:34 +0000627 }
628 }
629 }
630 power=GetXMLTreeChild(sop,"Power");
631 if (power != (XMLTreeInfo *) NULL)
632 {
633 content=GetXMLTreeContent(power);
634 p=(const char *) content;
635 for (i=0; (*p != '\0') && (i < 3); i++)
636 {
637 GetMagickToken(p,&p,token);
638 if (*token == ',')
639 GetMagickToken(p,&p,token);
640 switch (i)
641 {
cristyc1acd842011-05-19 23:05:47 +0000642 case 0:
643 {
cristy9b34e302011-11-05 02:15:45 +0000644 color_correction.red.power=StringToDouble(token,(char **) NULL);
cristyc1acd842011-05-19 23:05:47 +0000645 break;
646 }
647 case 1:
648 {
cristydbdd0e32011-11-04 23:29:40 +0000649 color_correction.green.power=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000650 (char **) NULL);
651 break;
652 }
653 case 2:
654 {
cristydbdd0e32011-11-04 23:29:40 +0000655 color_correction.blue.power=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000656 (char **) NULL);
657 break;
658 }
cristy3ed852e2009-09-05 21:47:34 +0000659 }
660 }
661 }
662 }
663 sat=GetXMLTreeChild(cc,"SATNode");
664 if (sat != (XMLTreeInfo *) NULL)
665 {
666 XMLTreeInfo
667 *saturation;
668
669 saturation=GetXMLTreeChild(sat,"Saturation");
670 if (saturation != (XMLTreeInfo *) NULL)
671 {
672 content=GetXMLTreeContent(saturation);
673 p=(const char *) content;
674 GetMagickToken(p,&p,token);
cristyca826b52012-01-18 18:50:46 +0000675 color_correction.saturation=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000676 }
677 }
678 ccc=DestroyXMLTree(ccc);
679 if (image->debug != MagickFalse)
680 {
681 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
682 " Color Correction Collection:");
683 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000684 " color_correction.red.slope: %g",color_correction.red.slope);
cristy3ed852e2009-09-05 21:47:34 +0000685 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000686 " color_correction.red.offset: %g",color_correction.red.offset);
cristy3ed852e2009-09-05 21:47:34 +0000687 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000688 " color_correction.red.power: %g",color_correction.red.power);
cristy3ed852e2009-09-05 21:47:34 +0000689 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000690 " color_correction.green.slope: %g",color_correction.green.slope);
cristy3ed852e2009-09-05 21:47:34 +0000691 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000692 " color_correction.green.offset: %g",color_correction.green.offset);
cristy3ed852e2009-09-05 21:47:34 +0000693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000694 " color_correction.green.power: %g",color_correction.green.power);
cristy3ed852e2009-09-05 21:47:34 +0000695 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000696 " color_correction.blue.slope: %g",color_correction.blue.slope);
cristy3ed852e2009-09-05 21:47:34 +0000697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000698 " color_correction.blue.offset: %g",color_correction.blue.offset);
cristy3ed852e2009-09-05 21:47:34 +0000699 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000700 " color_correction.blue.power: %g",color_correction.blue.power);
cristy3ed852e2009-09-05 21:47:34 +0000701 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000702 " color_correction.saturation: %g",color_correction.saturation);
cristy3ed852e2009-09-05 21:47:34 +0000703 }
cristy101ab702011-10-13 13:06:32 +0000704 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
705 if (cdl_map == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000706 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
707 image->filename);
cristyb5d5f722009-11-04 03:03:49 +0000708#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000709 #pragma omp parallel for schedule(static,4) \
cristyddacdd12012-05-07 23:08:14 +0000710 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000711#endif
cristybb503372010-05-27 20:51:26 +0000712 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +0000713 {
cristyda1f9c12011-10-02 21:39:49 +0000714 cdl_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
715 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
716 color_correction.red.offset,color_correction.red.power))));
717 cdl_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
718 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
719 color_correction.green.offset,color_correction.green.power))));
720 cdl_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
721 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
722 color_correction.blue.offset,color_correction.blue.power))));
cristy3ed852e2009-09-05 21:47:34 +0000723 }
724 if (image->storage_class == PseudoClass)
725 {
726 /*
727 Apply transfer function to colormap.
728 */
cristyb5d5f722009-11-04 03:03:49 +0000729#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000730 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000731 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000732#endif
cristybb503372010-05-27 20:51:26 +0000733 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
735 double
736 luma;
737
738 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
739 0.0722*image->colormap[i].blue;
cristy22e88202012-01-18 19:03:01 +0000740 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
741 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-
742 luma;
743 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
744 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-
745 luma;
746 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
747 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-
748 luma;
cristy3ed852e2009-09-05 21:47:34 +0000749 }
750 }
751 /*
752 Apply transfer function to image.
753 */
754 status=MagickTrue;
755 progress=0;
cristydb070952012-04-20 14:33:00 +0000756 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000757#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000758 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000759 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000760#endif
cristybb503372010-05-27 20:51:26 +0000761 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000762 {
763 double
764 luma;
765
cristy4c08aed2011-07-01 19:47:50 +0000766 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000767 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000768
cristy8d4629b2010-08-30 17:59:46 +0000769 register ssize_t
770 x;
771
cristy3ed852e2009-09-05 21:47:34 +0000772 if (status == MagickFalse)
773 continue;
774 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000775 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000776 {
777 status=MagickFalse;
778 continue;
779 }
cristybb503372010-05-27 20:51:26 +0000780 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
cristy4c08aed2011-07-01 19:47:50 +0000782 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
783 GetPixelBlue(image,q);
784 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
785 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
786 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
787 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
788 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
789 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
cristyed231572011-07-14 02:18:59 +0000790 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000791 }
792 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
793 status=MagickFalse;
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 {
796 MagickBooleanType
797 proceed;
798
cristyb5d5f722009-11-04 03:03:49 +0000799#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000800 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
cristy3ed852e2009-09-05 21:47:34 +0000801#endif
802 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
803 progress++,image->rows);
804 if (proceed == MagickFalse)
805 status=MagickFalse;
806 }
807 }
808 image_view=DestroyCacheView(image_view);
cristy101ab702011-10-13 13:06:32 +0000809 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
cristy3ed852e2009-09-05 21:47:34 +0000810 return(status);
811}
812
813/*
814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815% %
816% %
817% %
cristy3ed852e2009-09-05 21:47:34 +0000818% C o n t r a s t I m a g e %
819% %
820% %
821% %
822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823%
824% ContrastImage() enhances the intensity differences between the lighter and
825% darker elements of the image. Set sharpen to a MagickTrue to increase the
826% image contrast otherwise the contrast is reduced.
827%
828% The format of the ContrastImage method is:
829%
830% MagickBooleanType ContrastImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000831% const MagickBooleanType sharpen,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000832%
833% A description of each parameter follows:
834%
835% o image: the image.
836%
837% o sharpen: Increase or decrease image contrast.
838%
cristye23ec9d2011-08-16 18:15:40 +0000839% o exception: return any errors or warnings in this structure.
840%
cristy3ed852e2009-09-05 21:47:34 +0000841*/
842
cristy3094b7f2011-10-01 23:18:02 +0000843static void Contrast(const int sign,double *red,double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +0000844{
845 double
846 brightness,
847 hue,
848 saturation;
849
850 /*
851 Enhance contrast: dark color become darker, light color become lighter.
852 */
cristy3094b7f2011-10-01 23:18:02 +0000853 assert(red != (double *) NULL);
854 assert(green != (double *) NULL);
855 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000856 hue=0.0;
857 saturation=0.0;
858 brightness=0.0;
859 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
cristy31ac0f02011-03-12 02:04:47 +0000860 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
cristy4205a3c2010-09-12 20:19:59 +0000861 brightness);
cristy3ed852e2009-09-05 21:47:34 +0000862 if (brightness > 1.0)
863 brightness=1.0;
864 else
865 if (brightness < 0.0)
866 brightness=0.0;
867 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
868}
869
870MagickExport MagickBooleanType ContrastImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000871 const MagickBooleanType sharpen,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000872{
873#define ContrastImageTag "Contrast/Image"
874
cristyc4c8d132010-01-07 01:58:38 +0000875 CacheView
876 *image_view;
877
cristy3ed852e2009-09-05 21:47:34 +0000878 int
879 sign;
880
cristy3ed852e2009-09-05 21:47:34 +0000881 MagickBooleanType
882 status;
883
cristybb503372010-05-27 20:51:26 +0000884 MagickOffsetType
885 progress;
886
887 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000888 i;
889
cristybb503372010-05-27 20:51:26 +0000890 ssize_t
891 y;
892
cristy3ed852e2009-09-05 21:47:34 +0000893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 sign=sharpen != MagickFalse ? 1 : -1;
898 if (image->storage_class == PseudoClass)
899 {
900 /*
901 Contrast enhance colormap.
902 */
cristybb503372010-05-27 20:51:26 +0000903 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000904 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
905 &image->colormap[i].blue);
906 }
907 /*
908 Contrast enhance image.
909 */
910 status=MagickTrue;
911 progress=0;
cristydb070952012-04-20 14:33:00 +0000912 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000913#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000914 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000915 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000916#endif
cristybb503372010-05-27 20:51:26 +0000917 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000918 {
cristy3094b7f2011-10-01 23:18:02 +0000919 double
cristy5afeab82011-04-30 01:30:09 +0000920 blue,
921 green,
922 red;
923
cristy4c08aed2011-07-01 19:47:50 +0000924 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000925 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000926
cristy8d4629b2010-08-30 17:59:46 +0000927 register ssize_t
928 x;
929
cristy3ed852e2009-09-05 21:47:34 +0000930 if (status == MagickFalse)
931 continue;
932 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000933 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000934 {
935 status=MagickFalse;
936 continue;
937 }
cristybb503372010-05-27 20:51:26 +0000938 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000939 {
cristyda1f9c12011-10-02 21:39:49 +0000940 red=(double) GetPixelRed(image,q);
941 green=(double) GetPixelGreen(image,q);
942 blue=(double) GetPixelBlue(image,q);
cristy5afeab82011-04-30 01:30:09 +0000943 Contrast(sign,&red,&green,&blue);
cristyda1f9c12011-10-02 21:39:49 +0000944 SetPixelRed(image,ClampToQuantum(red),q);
945 SetPixelGreen(image,ClampToQuantum(green),q);
946 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +0000947 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000948 }
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
950 status=MagickFalse;
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952 {
953 MagickBooleanType
954 proceed;
955
cristyb5d5f722009-11-04 03:03:49 +0000956#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000957 #pragma omp critical (MagickCore_ContrastImage)
cristy3ed852e2009-09-05 21:47:34 +0000958#endif
959 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
960 if (proceed == MagickFalse)
961 status=MagickFalse;
962 }
963 }
964 image_view=DestroyCacheView(image_view);
965 return(status);
966}
967
968/*
969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970% %
971% %
972% %
973% C o n t r a s t S t r e t c h I m a g e %
974% %
975% %
976% %
977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978%
cristyf1611782011-08-01 15:39:13 +0000979% ContrastStretchImage() is a simple image enhancement technique that attempts
anthonye5b39652012-04-21 05:37:29 +0000980% to improve the contrast in an image by 'stretching' the range of intensity
cristyf1611782011-08-01 15:39:13 +0000981% values it contains to span a desired range of values. It differs from the
982% more sophisticated histogram equalization in that it can only apply a
983% linear scaling function to the image pixel values. As a result the
anthonye5b39652012-04-21 05:37:29 +0000984% 'enhancement' is less harsh.
cristy3ed852e2009-09-05 21:47:34 +0000985%
986% The format of the ContrastStretchImage method is:
987%
988% MagickBooleanType ContrastStretchImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000989% const char *levels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000990%
991% A description of each parameter follows:
992%
993% o image: the image.
994%
cristy3ed852e2009-09-05 21:47:34 +0000995% o black_point: the black point.
996%
997% o white_point: the white point.
998%
999% o levels: Specify the levels where the black and white points have the
1000% range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
1001%
cristye23ec9d2011-08-16 18:15:40 +00001002% o exception: return any errors or warnings in this structure.
1003%
cristy3ed852e2009-09-05 21:47:34 +00001004*/
cristy3ed852e2009-09-05 21:47:34 +00001005MagickExport MagickBooleanType ContrastStretchImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +00001006 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001007{
1008#define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1009#define ContrastStretchImageTag "ContrastStretch/Image"
1010
cristyc4c8d132010-01-07 01:58:38 +00001011 CacheView
1012 *image_view;
1013
cristy3ed852e2009-09-05 21:47:34 +00001014 MagickBooleanType
1015 status;
1016
cristybb503372010-05-27 20:51:26 +00001017 MagickOffsetType
1018 progress;
1019
cristyf45fec72011-08-23 16:02:32 +00001020 double
1021 *black,
cristy3ed852e2009-09-05 21:47:34 +00001022 *histogram,
1023 *stretch_map,
cristyf45fec72011-08-23 16:02:32 +00001024 *white;
cristy3ed852e2009-09-05 21:47:34 +00001025
cristybb503372010-05-27 20:51:26 +00001026 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001027 i;
1028
cristy564a5692012-01-20 23:56:26 +00001029 size_t
1030 number_channels;
1031
cristybb503372010-05-27 20:51:26 +00001032 ssize_t
1033 y;
1034
cristy3ed852e2009-09-05 21:47:34 +00001035 /*
1036 Allocate histogram and stretch map.
1037 */
1038 assert(image != (Image *) NULL);
1039 assert(image->signature == MagickSignature);
1040 if (image->debug != MagickFalse)
1041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyf45fec72011-08-23 16:02:32 +00001042 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1043 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
cristy564a5692012-01-20 23:56:26 +00001044 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
1045 sizeof(*histogram));
cristyf45fec72011-08-23 16:02:32 +00001046 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
cristy465571b2011-08-21 20:43:15 +00001047 GetPixelChannels(image)*sizeof(*stretch_map));
cristyf45fec72011-08-23 16:02:32 +00001048 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1049 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
cristy465571b2011-08-21 20:43:15 +00001050 {
cristyf45fec72011-08-23 16:02:32 +00001051 if (stretch_map != (double *) NULL)
1052 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1053 if (histogram != (double *) NULL)
1054 histogram=(double *) RelinquishMagickMemory(histogram);
1055 if (white != (double *) NULL)
1056 white=(double *) RelinquishMagickMemory(white);
1057 if (black != (double *) NULL)
1058 black=(double *) RelinquishMagickMemory(black);
cristy465571b2011-08-21 20:43:15 +00001059 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1060 image->filename);
1061 }
cristy3ed852e2009-09-05 21:47:34 +00001062 /*
1063 Form histogram.
1064 */
1065 status=MagickTrue;
cristy465571b2011-08-21 20:43:15 +00001066 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1067 sizeof(*histogram));
cristydb070952012-04-20 14:33:00 +00001068 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001069 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001070 {
cristy4c08aed2011-07-01 19:47:50 +00001071 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001072 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001073
cristybb503372010-05-27 20:51:26 +00001074 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001075 x;
1076
1077 if (status == MagickFalse)
1078 continue;
1079 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001080 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001081 {
1082 status=MagickFalse;
1083 continue;
1084 }
cristy43547a52011-08-09 13:07:05 +00001085 for (x=0; x < (ssize_t) image->columns; x++)
1086 {
cristy465571b2011-08-21 20:43:15 +00001087 register ssize_t
1088 i;
1089
1090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy465571b2011-08-21 20:43:15 +00001091 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
cristy43547a52011-08-09 13:07:05 +00001092 p+=GetPixelChannels(image);
1093 }
cristy3ed852e2009-09-05 21:47:34 +00001094 }
cristydb070952012-04-20 14:33:00 +00001095 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001096 /*
1097 Find the histogram boundaries by locating the black/white levels.
1098 */
cristy564a5692012-01-20 23:56:26 +00001099 number_channels=GetPixelChannels(image);
cristyb5d5f722009-11-04 03:03:49 +00001100#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001101 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001102 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001103#endif
cristyc94ba6f2012-01-29 23:19:58 +00001104 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001105 {
cristy465571b2011-08-21 20:43:15 +00001106 double
1107 intensity;
1108
1109 register ssize_t
1110 j;
1111
1112 black[i]=0.0;
1113 white[i]=MaxRange(QuantumRange);
1114 intensity=0.0;
1115 for (j=0; j <= (ssize_t) MaxMap; j++)
1116 {
1117 intensity+=histogram[GetPixelChannels(image)*j+i];
1118 if (intensity > black_point)
1119 break;
1120 }
1121 black[i]=(MagickRealType) j;
1122 intensity=0.0;
1123 for (j=(ssize_t) MaxMap; j != 0; j--)
1124 {
1125 intensity+=histogram[GetPixelChannels(image)*j+i];
1126 if (intensity > ((double) image->columns*image->rows-white_point))
1127 break;
1128 }
1129 white[i]=(MagickRealType) j;
cristy3ed852e2009-09-05 21:47:34 +00001130 }
cristy465571b2011-08-21 20:43:15 +00001131 histogram=(double *) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +00001132 /*
cristy465571b2011-08-21 20:43:15 +00001133 Stretch the histogram to create the stretched image mapping.
cristy3ed852e2009-09-05 21:47:34 +00001134 */
cristy465571b2011-08-21 20:43:15 +00001135 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1136 sizeof(*stretch_map));
cristy564a5692012-01-20 23:56:26 +00001137 number_channels=GetPixelChannels(image);
cristy465571b2011-08-21 20:43:15 +00001138#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001139 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001140 dynamic_number_threads(image->columns,1,1)
cristy465571b2011-08-21 20:43:15 +00001141#endif
cristy564a5692012-01-20 23:56:26 +00001142 for (i=0; i < (ssize_t) number_channels; i++)
cristy465571b2011-08-21 20:43:15 +00001143 {
1144 register ssize_t
1145 j;
1146
1147 for (j=0; j <= (ssize_t) MaxMap; j++)
1148 {
1149 if (j < (ssize_t) black[i])
1150 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1151 else
1152 if (j > (ssize_t) white[i])
1153 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1154 QuantumRange;
1155 else
1156 if (black[i] != white[i])
1157 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1158 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1159 (white[i]-black[i])));
1160 }
1161 }
cristy3ed852e2009-09-05 21:47:34 +00001162 if (image->storage_class == PseudoClass)
1163 {
cristy465571b2011-08-21 20:43:15 +00001164 register ssize_t
1165 j;
1166
cristy3ed852e2009-09-05 21:47:34 +00001167 /*
cristy465571b2011-08-21 20:43:15 +00001168 Stretch-contrast colormap.
cristy3ed852e2009-09-05 21:47:34 +00001169 */
cristyb5d5f722009-11-04 03:03:49 +00001170#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001171 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001172 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001173#endif
cristy465571b2011-08-21 20:43:15 +00001174 for (j=0; j < (ssize_t) image->colors; j++)
cristy3ed852e2009-09-05 21:47:34 +00001175 {
cristyed231572011-07-14 02:18:59 +00001176 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001177 {
cristy465571b2011-08-21 20:43:15 +00001178 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1179 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001180 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1181 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001182 }
cristyed231572011-07-14 02:18:59 +00001183 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001184 {
cristy465571b2011-08-21 20:43:15 +00001185 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1186 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001187 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1188 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001189 }
cristyed231572011-07-14 02:18:59 +00001190 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001191 {
cristy465571b2011-08-21 20:43:15 +00001192 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1193 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001194 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1195 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001196 }
cristyed231572011-07-14 02:18:59 +00001197 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001198 {
cristy465571b2011-08-21 20:43:15 +00001199 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1200 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001201 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1202 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001203 }
1204 }
1205 }
1206 /*
cristy465571b2011-08-21 20:43:15 +00001207 Stretch-contrast image.
cristy3ed852e2009-09-05 21:47:34 +00001208 */
1209 status=MagickTrue;
1210 progress=0;
cristydb070952012-04-20 14:33:00 +00001211 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001212#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001213 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001214 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001215#endif
cristybb503372010-05-27 20:51:26 +00001216 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001217 {
cristy4c08aed2011-07-01 19:47:50 +00001218 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001219 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001220
cristy8d4629b2010-08-30 17:59:46 +00001221 register ssize_t
1222 x;
1223
cristy3ed852e2009-09-05 21:47:34 +00001224 if (status == MagickFalse)
1225 continue;
1226 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001227 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001228 {
1229 status=MagickFalse;
1230 continue;
1231 }
cristybb503372010-05-27 20:51:26 +00001232 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001233 {
cristy465571b2011-08-21 20:43:15 +00001234 register ssize_t
1235 i;
1236
cristy10a6c612012-01-29 21:41:05 +00001237 if (GetPixelMask(image,q) != 0)
1238 {
1239 q+=GetPixelChannels(image);
1240 continue;
1241 }
cristy465571b2011-08-21 20:43:15 +00001242 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1243 {
cristyabace412011-12-11 15:56:53 +00001244 PixelChannel
1245 channel;
1246
cristy465571b2011-08-21 20:43:15 +00001247 PixelTrait
1248 traits;
1249
cristyabace412011-12-11 15:56:53 +00001250 channel=GetPixelChannelMapChannel(image,i);
1251 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001252 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1253 continue;
1254 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1255 ScaleQuantumToMap(q[i])+i]);
cristy465571b2011-08-21 20:43:15 +00001256 }
cristyed231572011-07-14 02:18:59 +00001257 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001258 }
1259 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1260 status=MagickFalse;
1261 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1262 {
1263 MagickBooleanType
1264 proceed;
1265
cristyb5d5f722009-11-04 03:03:49 +00001266#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001267 #pragma omp critical (MagickCore_ContrastStretchImage)
cristy3ed852e2009-09-05 21:47:34 +00001268#endif
1269 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1270 image->rows);
1271 if (proceed == MagickFalse)
1272 status=MagickFalse;
1273 }
1274 }
1275 image_view=DestroyCacheView(image_view);
cristyf45fec72011-08-23 16:02:32 +00001276 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1277 white=(double *) RelinquishMagickMemory(white);
1278 black=(double *) RelinquishMagickMemory(black);
cristy3ed852e2009-09-05 21:47:34 +00001279 return(status);
1280}
1281
1282/*
1283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284% %
1285% %
1286% %
1287% E n h a n c e I m a g e %
1288% %
1289% %
1290% %
1291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1292%
1293% EnhanceImage() applies a digital filter that improves the quality of a
1294% noisy image.
1295%
1296% The format of the EnhanceImage method is:
1297%
1298% Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1299%
1300% A description of each parameter follows:
1301%
1302% o image: the image.
1303%
1304% o exception: return any errors or warnings in this structure.
1305%
1306*/
1307MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1308{
cristy6d8c3d72011-08-22 01:20:01 +00001309#define EnhancePixel(weight) \
cristy0beccfa2011-09-25 20:47:53 +00001310 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1311 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1312 enhance_image,channel,q); \
cristy3ed852e2009-09-05 21:47:34 +00001313 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
cristy6d8c3d72011-08-22 01:20:01 +00001314 mean)*distance*distance; \
cristy3ed852e2009-09-05 21:47:34 +00001315 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1316 QuantumRange/25.0f)) \
1317 { \
cristy6d8c3d72011-08-22 01:20:01 +00001318 aggregate+=(weight)*r[i]; \
cristy3ed852e2009-09-05 21:47:34 +00001319 total_weight+=(weight); \
1320 } \
cristy6d8c3d72011-08-22 01:20:01 +00001321 r+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001322#define EnhanceImageTag "Enhance/Image"
1323
cristyc4c8d132010-01-07 01:58:38 +00001324 CacheView
1325 *enhance_view,
1326 *image_view;
1327
cristy3ed852e2009-09-05 21:47:34 +00001328 Image
1329 *enhance_image;
1330
cristy3ed852e2009-09-05 21:47:34 +00001331 MagickBooleanType
1332 status;
1333
cristybb503372010-05-27 20:51:26 +00001334 MagickOffsetType
1335 progress;
1336
cristybb503372010-05-27 20:51:26 +00001337 ssize_t
1338 y;
1339
cristy3ed852e2009-09-05 21:47:34 +00001340 /*
1341 Initialize enhanced image attributes.
1342 */
1343 assert(image != (const Image *) NULL);
1344 assert(image->signature == MagickSignature);
1345 if (image->debug != MagickFalse)
1346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1347 assert(exception != (ExceptionInfo *) NULL);
1348 assert(exception->signature == MagickSignature);
cristy3ed852e2009-09-05 21:47:34 +00001349 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1350 exception);
1351 if (enhance_image == (Image *) NULL)
1352 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001353 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001354 {
cristy3ed852e2009-09-05 21:47:34 +00001355 enhance_image=DestroyImage(enhance_image);
1356 return((Image *) NULL);
1357 }
1358 /*
1359 Enhance image.
1360 */
1361 status=MagickTrue;
1362 progress=0;
cristydb070952012-04-20 14:33:00 +00001363 image_view=AcquireVirtualCacheView(image,exception);
1364 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001365#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001366 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001367 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001368#endif
cristybb503372010-05-27 20:51:26 +00001369 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001370 {
cristy4c08aed2011-07-01 19:47:50 +00001371 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001372 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001373
cristy4c08aed2011-07-01 19:47:50 +00001374 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001375 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001376
cristy8d4629b2010-08-30 17:59:46 +00001377 register ssize_t
1378 x;
1379
cristy6d8c3d72011-08-22 01:20:01 +00001380 ssize_t
1381 center;
1382
cristy3ed852e2009-09-05 21:47:34 +00001383 if (status == MagickFalse)
1384 continue;
1385 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1386 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1387 exception);
cristy4c08aed2011-07-01 19:47:50 +00001388 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001389 {
1390 status=MagickFalse;
1391 continue;
1392 }
cristyf45fec72011-08-23 16:02:32 +00001393 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
cristybb503372010-05-27 20:51:26 +00001394 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001395 {
cristy6d8c3d72011-08-22 01:20:01 +00001396 register ssize_t
1397 i;
cristy3ed852e2009-09-05 21:47:34 +00001398
cristy10a6c612012-01-29 21:41:05 +00001399 if (GetPixelMask(image,p) != 0)
1400 {
1401 p+=GetPixelChannels(image);
1402 q+=GetPixelChannels(enhance_image);
1403 continue;
1404 }
cristy6d8c3d72011-08-22 01:20:01 +00001405 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1406 {
1407 MagickRealType
1408 aggregate,
1409 distance,
1410 distance_squared,
1411 mean,
1412 total_weight;
cristy3ed852e2009-09-05 21:47:34 +00001413
cristy6d8c3d72011-08-22 01:20:01 +00001414 PixelChannel
1415 channel;
cristy3ed852e2009-09-05 21:47:34 +00001416
cristy6d8c3d72011-08-22 01:20:01 +00001417 PixelTrait
1418 enhance_traits,
1419 traits;
cristy3ed852e2009-09-05 21:47:34 +00001420
cristy6d8c3d72011-08-22 01:20:01 +00001421 register const Quantum
1422 *restrict r;
1423
cristye2a912b2011-12-05 20:02:07 +00001424 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001425 traits=GetPixelChannelMapTraits(image,channel);
cristy6d8c3d72011-08-22 01:20:01 +00001426 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001427 if ((traits == UndefinedPixelTrait) ||
1428 (enhance_traits == UndefinedPixelTrait))
cristy6d8c3d72011-08-22 01:20:01 +00001429 continue;
cristy0beccfa2011-09-25 20:47:53 +00001430 SetPixelChannel(enhance_image,channel,p[center+i],q);
cristy6d8c3d72011-08-22 01:20:01 +00001431 if ((enhance_traits & CopyPixelTrait) != 0)
1432 continue;
1433 /*
1434 Compute weighted average of target pixel color components.
1435 */
1436 aggregate=0.0;
1437 total_weight=0.0;
cristyf45fec72011-08-23 16:02:32 +00001438 r=p;
cristy6d8c3d72011-08-22 01:20:01 +00001439 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1440 EnhancePixel(8.0); EnhancePixel(5.0);
1441 r=p+1*GetPixelChannels(image)*(image->columns+4);
1442 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1443 EnhancePixel(20.0); EnhancePixel(8.0);
1444 r=p+2*GetPixelChannels(image)*(image->columns+4);
1445 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1446 EnhancePixel(40.0); EnhancePixel(10.0);
1447 r=p+3*GetPixelChannels(image)*(image->columns+4);
1448 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1449 EnhancePixel(20.0); EnhancePixel(8.0);
1450 r=p+4*GetPixelChannels(image)*(image->columns+4);
1451 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1452 EnhancePixel(8.0); EnhancePixel(5.0);
cristy0beccfa2011-09-25 20:47:53 +00001453 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1454 total_weight),q);
cristy6d8c3d72011-08-22 01:20:01 +00001455 }
cristyed231572011-07-14 02:18:59 +00001456 p+=GetPixelChannels(image);
1457 q+=GetPixelChannels(enhance_image);
cristy3ed852e2009-09-05 21:47:34 +00001458 }
1459 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1460 status=MagickFalse;
1461 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1462 {
1463 MagickBooleanType
1464 proceed;
1465
cristyb5d5f722009-11-04 03:03:49 +00001466#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001467 #pragma omp critical (MagickCore_EnhanceImage)
cristy3ed852e2009-09-05 21:47:34 +00001468#endif
1469 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1470 if (proceed == MagickFalse)
1471 status=MagickFalse;
1472 }
1473 }
1474 enhance_view=DestroyCacheView(enhance_view);
1475 image_view=DestroyCacheView(image_view);
1476 return(enhance_image);
1477}
1478
1479/*
1480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1481% %
1482% %
1483% %
1484% E q u a l i z e I m a g e %
1485% %
1486% %
1487% %
1488%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1489%
1490% EqualizeImage() applies a histogram equalization to the image.
1491%
1492% The format of the EqualizeImage method is:
1493%
cristy6d8c3d72011-08-22 01:20:01 +00001494% MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001495%
1496% A description of each parameter follows:
1497%
1498% o image: the image.
1499%
cristy6d8c3d72011-08-22 01:20:01 +00001500% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00001501%
1502*/
cristy6d8c3d72011-08-22 01:20:01 +00001503MagickExport MagickBooleanType EqualizeImage(Image *image,
1504 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001505{
cristy3ed852e2009-09-05 21:47:34 +00001506#define EqualizeImageTag "Equalize/Image"
1507
cristyc4c8d132010-01-07 01:58:38 +00001508 CacheView
1509 *image_view;
1510
cristy3ed852e2009-09-05 21:47:34 +00001511 MagickBooleanType
1512 status;
1513
cristybb503372010-05-27 20:51:26 +00001514 MagickOffsetType
1515 progress;
1516
cristyf45fec72011-08-23 16:02:32 +00001517 MagickRealType
cristy5f95f4f2011-10-23 01:01:01 +00001518 black[CompositePixelChannel],
cristy3ed852e2009-09-05 21:47:34 +00001519 *equalize_map,
1520 *histogram,
cristy3ed852e2009-09-05 21:47:34 +00001521 *map,
cristy5f95f4f2011-10-23 01:01:01 +00001522 white[CompositePixelChannel];
cristy3ed852e2009-09-05 21:47:34 +00001523
cristybb503372010-05-27 20:51:26 +00001524 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001525 i;
1526
cristy564a5692012-01-20 23:56:26 +00001527 size_t
1528 number_channels;
1529
cristybb503372010-05-27 20:51:26 +00001530 ssize_t
1531 y;
1532
cristy3ed852e2009-09-05 21:47:34 +00001533 /*
1534 Allocate and initialize histogram arrays.
1535 */
1536 assert(image != (Image *) NULL);
1537 assert(image->signature == MagickSignature);
1538 if (image->debug != MagickFalse)
1539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyf45fec72011-08-23 16:02:32 +00001540 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1541 GetPixelChannels(image)*sizeof(*equalize_map));
1542 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1543 GetPixelChannels(image)*sizeof(*histogram));
1544 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1545 GetPixelChannels(image)*sizeof(*map));
1546 if ((equalize_map == (MagickRealType *) NULL) ||
1547 (histogram == (MagickRealType *) NULL) ||
1548 (map == (MagickRealType *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001549 {
cristyf45fec72011-08-23 16:02:32 +00001550 if (map != (MagickRealType *) NULL)
1551 map=(MagickRealType *) RelinquishMagickMemory(map);
1552 if (histogram != (MagickRealType *) NULL)
1553 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1554 if (equalize_map != (MagickRealType *) NULL)
1555 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
cristy3ed852e2009-09-05 21:47:34 +00001556 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1557 image->filename);
1558 }
1559 /*
1560 Form histogram.
1561 */
cristyf45fec72011-08-23 16:02:32 +00001562 status=MagickTrue;
1563 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1564 sizeof(*histogram));
cristydb070952012-04-20 14:33:00 +00001565 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001566 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001567 {
cristy4c08aed2011-07-01 19:47:50 +00001568 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001569 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001570
cristybb503372010-05-27 20:51:26 +00001571 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001572 x;
1573
cristyf45fec72011-08-23 16:02:32 +00001574 if (status == MagickFalse)
1575 continue;
1576 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001577 if (p == (const Quantum *) NULL)
cristyf45fec72011-08-23 16:02:32 +00001578 {
1579 status=MagickFalse;
1580 continue;
1581 }
cristybb503372010-05-27 20:51:26 +00001582 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001583 {
cristyf45fec72011-08-23 16:02:32 +00001584 register ssize_t
1585 i;
1586
1587 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1588 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
cristyed231572011-07-14 02:18:59 +00001589 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001590 }
1591 }
cristydb070952012-04-20 14:33:00 +00001592 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001593 /*
1594 Integrate the histogram to get the equalization map.
1595 */
cristy564a5692012-01-20 23:56:26 +00001596 number_channels=GetPixelChannels(image);
cristyb5d5f722009-11-04 03:03:49 +00001597#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001598 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001599 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001600#endif
cristyc94ba6f2012-01-29 23:19:58 +00001601 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001602 {
cristyf45fec72011-08-23 16:02:32 +00001603 MagickRealType
1604 intensity;
1605
1606 register ssize_t
1607 j;
1608
1609 intensity=0.0;
1610 for (j=0; j <= (ssize_t) MaxMap; j++)
1611 {
1612 intensity+=histogram[GetPixelChannels(image)*j+i];
1613 map[GetPixelChannels(image)*j+i]=intensity;
1614 }
cristy3ed852e2009-09-05 21:47:34 +00001615 }
cristyf45fec72011-08-23 16:02:32 +00001616 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1617 sizeof(*equalize_map));
cristy564a5692012-01-20 23:56:26 +00001618 number_channels=GetPixelChannels(image);
cristyf45fec72011-08-23 16:02:32 +00001619#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001620 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001621 dynamic_number_threads(image->columns,1,1)
cristyf45fec72011-08-23 16:02:32 +00001622#endif
cristy564a5692012-01-20 23:56:26 +00001623 for (i=0; i < (ssize_t) number_channels; i++)
cristyf45fec72011-08-23 16:02:32 +00001624 {
1625 register ssize_t
1626 j;
1627
1628 black[i]=map[i];
1629 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1630 if (black[i] != white[i])
1631 for (j=0; j <= (ssize_t) MaxMap; j++)
1632 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1633 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1634 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1635 }
1636 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1637 map=(MagickRealType *) RelinquishMagickMemory(map);
cristy3ed852e2009-09-05 21:47:34 +00001638 if (image->storage_class == PseudoClass)
1639 {
cristyf54798b2011-11-21 18:38:23 +00001640 PixelChannel
1641 channel;
1642
cristyf45fec72011-08-23 16:02:32 +00001643 register ssize_t
1644 j;
1645
cristy3ed852e2009-09-05 21:47:34 +00001646 /*
1647 Equalize colormap.
1648 */
cristyb5d5f722009-11-04 03:03:49 +00001649#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001650 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001651 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001652#endif
cristyf45fec72011-08-23 16:02:32 +00001653 for (j=0; j < (ssize_t) image->colors; j++)
cristy3ed852e2009-09-05 21:47:34 +00001654 {
cristyf54798b2011-11-21 18:38:23 +00001655 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001656 {
cristyf54798b2011-11-21 18:38:23 +00001657 channel=GetPixelChannelMapChannel(image,RedPixelChannel);
1658 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001659 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001660 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
1661 channel;
cristyf45fec72011-08-23 16:02:32 +00001662 }
cristyf54798b2011-11-21 18:38:23 +00001663 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001664 {
cristyf54798b2011-11-21 18:38:23 +00001665 channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
1666 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001667 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001668 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
1669 channel;
cristyf45fec72011-08-23 16:02:32 +00001670 }
cristyf54798b2011-11-21 18:38:23 +00001671 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001672 {
cristyf54798b2011-11-21 18:38:23 +00001673 channel=GetPixelChannelMapChannel(image,BluePixelChannel);
1674 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001675 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001676 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
1677 channel;
cristyf45fec72011-08-23 16:02:32 +00001678 }
cristyf54798b2011-11-21 18:38:23 +00001679 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001680 {
cristyf54798b2011-11-21 18:38:23 +00001681 channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1682 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001683 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001684 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
1685 channel;
cristyf45fec72011-08-23 16:02:32 +00001686 }
cristy3ed852e2009-09-05 21:47:34 +00001687 }
1688 }
1689 /*
1690 Equalize image.
1691 */
cristy3ed852e2009-09-05 21:47:34 +00001692 progress=0;
cristydb070952012-04-20 14:33:00 +00001693 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001694#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001695 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001696 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001697#endif
cristybb503372010-05-27 20:51:26 +00001698 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001699 {
cristy4c08aed2011-07-01 19:47:50 +00001700 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001701 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001702
cristy8d4629b2010-08-30 17:59:46 +00001703 register ssize_t
1704 x;
1705
cristy3ed852e2009-09-05 21:47:34 +00001706 if (status == MagickFalse)
1707 continue;
1708 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001709 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001710 {
1711 status=MagickFalse;
1712 continue;
1713 }
cristybb503372010-05-27 20:51:26 +00001714 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001715 {
cristyf45fec72011-08-23 16:02:32 +00001716 register ssize_t
1717 i;
1718
cristy10a6c612012-01-29 21:41:05 +00001719 if (GetPixelMask(image,q) != 0)
1720 {
1721 q+=GetPixelChannels(image);
1722 continue;
1723 }
cristyf45fec72011-08-23 16:02:32 +00001724 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1725 {
cristyabace412011-12-11 15:56:53 +00001726 PixelChannel
1727 channel;
1728
cristyf45fec72011-08-23 16:02:32 +00001729 PixelTrait
1730 traits;
1731
cristyabace412011-12-11 15:56:53 +00001732 channel=GetPixelChannelMapChannel(image,i);
1733 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001734 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1735 continue;
1736 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1737 ScaleQuantumToMap(q[i])+i]);
cristyf45fec72011-08-23 16:02:32 +00001738 }
cristyed231572011-07-14 02:18:59 +00001739 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001740 }
1741 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1742 status=MagickFalse;
1743 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1744 {
1745 MagickBooleanType
1746 proceed;
1747
cristyb5d5f722009-11-04 03:03:49 +00001748#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001749 #pragma omp critical (MagickCore_EqualizeImage)
cristy3ed852e2009-09-05 21:47:34 +00001750#endif
1751 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1752 if (proceed == MagickFalse)
1753 status=MagickFalse;
1754 }
1755 }
1756 image_view=DestroyCacheView(image_view);
cristyf45fec72011-08-23 16:02:32 +00001757 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
cristy3ed852e2009-09-05 21:47:34 +00001758 return(status);
1759}
1760
1761/*
1762%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763% %
1764% %
1765% %
1766% G a m m a I m a g e %
1767% %
1768% %
1769% %
1770%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1771%
1772% GammaImage() gamma-corrects a particular image channel. The same
1773% image viewed on different devices will have perceptual differences in the
1774% way the image's intensities are represented on the screen. Specify
1775% individual gamma levels for the red, green, and blue channels, or adjust
1776% all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1777%
1778% You can also reduce the influence of a particular channel with a gamma
1779% value of 0.
1780%
1781% The format of the GammaImage method is:
1782%
cristyb3e7c6c2011-07-24 01:43:55 +00001783% MagickBooleanType GammaImage(Image *image,const double gamma,
1784% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001785%
1786% A description of each parameter follows:
1787%
1788% o image: the image.
1789%
cristya6360142011-03-23 23:08:04 +00001790% o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1791%
cristy3ed852e2009-09-05 21:47:34 +00001792% o gamma: the image gamma.
1793%
1794*/
cristyb3e7c6c2011-07-24 01:43:55 +00001795MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1796 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001797{
1798#define GammaCorrectImageTag "GammaCorrect/Image"
1799
cristyc4c8d132010-01-07 01:58:38 +00001800 CacheView
1801 *image_view;
1802
cristy3ed852e2009-09-05 21:47:34 +00001803 MagickBooleanType
1804 status;
1805
cristybb503372010-05-27 20:51:26 +00001806 MagickOffsetType
1807 progress;
1808
cristy19593872012-01-22 02:00:33 +00001809 Quantum
1810 *gamma_map;
1811
cristybb503372010-05-27 20:51:26 +00001812 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001813 i;
1814
cristybb503372010-05-27 20:51:26 +00001815 ssize_t
1816 y;
1817
cristy3ed852e2009-09-05 21:47:34 +00001818 /*
1819 Allocate and initialize gamma maps.
1820 */
1821 assert(image != (Image *) NULL);
1822 assert(image->signature == MagickSignature);
1823 if (image->debug != MagickFalse)
1824 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1825 if (gamma == 1.0)
1826 return(MagickTrue);
cristy19593872012-01-22 02:00:33 +00001827 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1828 if (gamma_map == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001829 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1830 image->filename);
1831 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1832 if (gamma != 0.0)
cristyd476a8e2011-07-23 16:13:22 +00001833#if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
cristyac245f82012-05-05 17:13:57 +00001834 #pragma omp parallel for \
cristyddacdd12012-05-07 23:08:14 +00001835 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001836#endif
cristybb503372010-05-27 20:51:26 +00001837 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy19593872012-01-22 02:00:33 +00001838 gamma_map[i]=ScaleMapToQuantum((MagickRealType) (MaxMap*pow((double) i/
1839 MaxMap,1.0/gamma)));
cristy3ed852e2009-09-05 21:47:34 +00001840 if (image->storage_class == PseudoClass)
1841 {
1842 /*
1843 Gamma-correct colormap.
1844 */
cristyb5d5f722009-11-04 03:03:49 +00001845#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001846 #pragma omp parallel for schedule(static) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001847 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001848#endif
cristybb503372010-05-27 20:51:26 +00001849 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001850 {
cristyed231572011-07-14 02:18:59 +00001851 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001852 image->colormap[i].red=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001853 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00001854 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001855 image->colormap[i].green=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001856 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00001857 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001858 image->colormap[i].blue=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001859 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00001860 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001861 image->colormap[i].alpha=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001862 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00001863 }
1864 }
1865 /*
1866 Gamma-correct image.
1867 */
1868 status=MagickTrue;
1869 progress=0;
cristydb070952012-04-20 14:33:00 +00001870 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001871#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001872 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001873 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001874#endif
cristybb503372010-05-27 20:51:26 +00001875 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001876 {
cristy4c08aed2011-07-01 19:47:50 +00001877 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001878 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001879
cristy8d4629b2010-08-30 17:59:46 +00001880 register ssize_t
1881 x;
1882
cristy3ed852e2009-09-05 21:47:34 +00001883 if (status == MagickFalse)
1884 continue;
1885 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001886 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001887 {
1888 status=MagickFalse;
1889 continue;
1890 }
cristybb503372010-05-27 20:51:26 +00001891 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001892 {
cristyd476a8e2011-07-23 16:13:22 +00001893 register ssize_t
1894 i;
1895
cristy10a6c612012-01-29 21:41:05 +00001896 if (GetPixelMask(image,q) != 0)
1897 {
1898 q+=GetPixelChannels(image);
1899 continue;
1900 }
cristya30d9ba2011-07-23 21:00:48 +00001901 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyd476a8e2011-07-23 16:13:22 +00001902 {
cristyabace412011-12-11 15:56:53 +00001903 PixelChannel
1904 channel;
1905
cristyd476a8e2011-07-23 16:13:22 +00001906 PixelTrait
1907 traits;
1908
cristyabace412011-12-11 15:56:53 +00001909 channel=GetPixelChannelMapChannel(image,i);
1910 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001911 if ((traits & UpdatePixelTrait) == 0)
1912 continue;
1913 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
cristyd476a8e2011-07-23 16:13:22 +00001914 }
cristya30d9ba2011-07-23 21:00:48 +00001915 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001916 }
cristy3ed852e2009-09-05 21:47:34 +00001917 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1918 status=MagickFalse;
1919 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1920 {
1921 MagickBooleanType
1922 proceed;
1923
cristyb5d5f722009-11-04 03:03:49 +00001924#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001925 #pragma omp critical (MagickCore_GammaImage)
cristy3ed852e2009-09-05 21:47:34 +00001926#endif
1927 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1928 image->rows);
1929 if (proceed == MagickFalse)
1930 status=MagickFalse;
1931 }
1932 }
1933 image_view=DestroyCacheView(image_view);
cristy19593872012-01-22 02:00:33 +00001934 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
cristy3ed852e2009-09-05 21:47:34 +00001935 if (image->gamma != 0.0)
1936 image->gamma*=gamma;
1937 return(status);
1938}
1939
1940/*
1941%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1942% %
1943% %
1944% %
1945% H a l d C l u t I m a g e %
1946% %
1947% %
1948% %
1949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1950%
1951% HaldClutImage() applies a Hald color lookup table to the image. A Hald
1952% color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1953% Create it with the HALD coder. You can apply any color transformation to
1954% the Hald image and then use this method to apply the transform to the
1955% image.
1956%
1957% The format of the HaldClutImage method is:
1958%
cristy7c0a0a42011-08-23 17:57:25 +00001959% MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1960% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001961%
1962% A description of each parameter follows:
1963%
1964% o image: the image, which is replaced by indexed CLUT values
1965%
1966% o hald_image: the color lookup table image for replacement color values.
1967%
cristy7c0a0a42011-08-23 17:57:25 +00001968% o exception: return any errors or warnings in this structure.
1969%
cristy3ed852e2009-09-05 21:47:34 +00001970*/
1971
1972static inline size_t MagickMin(const size_t x,const size_t y)
1973{
1974 if (x < y)
1975 return(x);
1976 return(y);
1977}
1978
1979MagickExport MagickBooleanType HaldClutImage(Image *image,
cristy7c0a0a42011-08-23 17:57:25 +00001980 const Image *hald_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001981{
cristy3ed852e2009-09-05 21:47:34 +00001982#define HaldClutImageTag "Clut/Image"
1983
1984 typedef struct _HaldInfo
1985 {
1986 MagickRealType
1987 x,
1988 y,
1989 z;
1990 } HaldInfo;
1991
cristyfa112112010-01-04 17:48:07 +00001992 CacheView
cristyd551fbc2011-03-31 18:07:46 +00001993 *hald_view,
cristyfa112112010-01-04 17:48:07 +00001994 *image_view;
1995
cristy3ed852e2009-09-05 21:47:34 +00001996 double
1997 width;
1998
cristy3ed852e2009-09-05 21:47:34 +00001999 MagickBooleanType
2000 status;
2001
cristybb503372010-05-27 20:51:26 +00002002 MagickOffsetType
2003 progress;
2004
cristy4c08aed2011-07-01 19:47:50 +00002005 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002006 zero;
2007
cristy3ed852e2009-09-05 21:47:34 +00002008 size_t
2009 cube_size,
2010 length,
2011 level;
2012
cristybb503372010-05-27 20:51:26 +00002013 ssize_t
2014 y;
2015
cristy3ed852e2009-09-05 21:47:34 +00002016 assert(image != (Image *) NULL);
2017 assert(image->signature == MagickSignature);
2018 if (image->debug != MagickFalse)
2019 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2020 assert(hald_image != (Image *) NULL);
2021 assert(hald_image->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +00002022 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002023 return(MagickFalse);
cristy0898eba2012-04-09 16:38:29 +00002024 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2025 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002026 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002027 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002028 /*
2029 Hald clut image.
2030 */
2031 status=MagickTrue;
2032 progress=0;
2033 length=MagickMin(hald_image->columns,hald_image->rows);
2034 for (level=2; (level*level*level) < length; level++) ;
2035 level*=level;
2036 cube_size=level*level;
2037 width=(double) hald_image->columns;
cristy4c08aed2011-07-01 19:47:50 +00002038 GetPixelInfo(hald_image,&zero);
cristydb070952012-04-20 14:33:00 +00002039 hald_view=AcquireVirtualCacheView(hald_image,exception);
2040 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002041#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002042 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002043 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002044#endif
cristybb503372010-05-27 20:51:26 +00002045 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002046 {
cristy4c08aed2011-07-01 19:47:50 +00002047 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002048 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002049
cristy8d4629b2010-08-30 17:59:46 +00002050 register ssize_t
2051 x;
2052
cristy3ed852e2009-09-05 21:47:34 +00002053 if (status == MagickFalse)
2054 continue;
2055 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002056 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002057 {
2058 status=MagickFalse;
2059 continue;
2060 }
cristybb503372010-05-27 20:51:26 +00002061 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002062 {
cristy7c0a0a42011-08-23 17:57:25 +00002063 double
2064 offset;
2065
2066 HaldInfo
2067 point;
2068
2069 PixelInfo
2070 pixel,
2071 pixel1,
2072 pixel2,
2073 pixel3,
2074 pixel4;
2075
cristy4c08aed2011-07-01 19:47:50 +00002076 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2077 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2078 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002079 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2080 point.x-=floor(point.x);
2081 point.y-=floor(point.y);
2082 point.z-=floor(point.z);
cristy7c0a0a42011-08-23 17:57:25 +00002083 pixel1=zero;
2084 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2085 fmod(offset,width),floor(offset/width),&pixel1,exception);
2086 pixel2=zero;
2087 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2088 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2089 pixel3=zero;
2090 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2091 point.y,&pixel3);
cristy3ed852e2009-09-05 21:47:34 +00002092 offset+=cube_size;
cristy7c0a0a42011-08-23 17:57:25 +00002093 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2094 fmod(offset,width),floor(offset/width),&pixel1,exception);
2095 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2096 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2097 pixel4=zero;
2098 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2099 point.y,&pixel4);
2100 pixel=zero;
2101 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2102 point.z,&pixel);
cristyed231572011-07-14 02:18:59 +00002103 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002104 SetPixelRed(image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00002105 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002106 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00002107 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002108 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002109 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002110 (image->colorspace == CMYKColorspace))
cristyf45fec72011-08-23 16:02:32 +00002111 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2112 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2113 (image->matte != MagickFalse))
2114 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00002115 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002116 }
2117 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2118 status=MagickFalse;
2119 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2120 {
2121 MagickBooleanType
2122 proceed;
2123
cristyb5d5f722009-11-04 03:03:49 +00002124#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002125 #pragma omp critical (MagickCore_HaldClutImage)
cristy3ed852e2009-09-05 21:47:34 +00002126#endif
2127 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2128 if (proceed == MagickFalse)
2129 status=MagickFalse;
2130 }
2131 }
cristyd551fbc2011-03-31 18:07:46 +00002132 hald_view=DestroyCacheView(hald_view);
cristy3ed852e2009-09-05 21:47:34 +00002133 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002134 return(status);
2135}
2136
2137/*
2138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2139% %
2140% %
2141% %
2142% L e v e l I m a g e %
2143% %
2144% %
2145% %
2146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2147%
2148% LevelImage() adjusts the levels of a particular image channel by
2149% scaling the colors falling between specified white and black points to
2150% the full available quantum range.
2151%
2152% The parameters provided represent the black, and white points. The black
2153% point specifies the darkest color in the image. Colors darker than the
2154% black point are set to zero. White point specifies the lightest color in
2155% the image. Colors brighter than the white point are set to the maximum
2156% quantum value.
2157%
2158% If a '!' flag is given, map black and white colors to the given levels
2159% rather than mapping those levels to black and white. See
cristy50fbc382011-07-07 02:19:17 +00002160% LevelizeImage() below.
cristy3ed852e2009-09-05 21:47:34 +00002161%
2162% Gamma specifies a gamma correction to apply to the image.
2163%
2164% The format of the LevelImage method is:
2165%
cristy01e9afd2011-08-10 17:38:41 +00002166% MagickBooleanType LevelImage(Image *image,const double black_point,
2167% const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002168%
2169% A description of each parameter follows:
2170%
2171% o image: the image.
2172%
cristy01e9afd2011-08-10 17:38:41 +00002173% o black_point: The level to map zero (black) to.
2174%
2175% o white_point: The level to map QuantumRange (white) to.
2176%
2177% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002178%
2179*/
cristy780e9ef2011-12-18 23:33:50 +00002180
cristyb8b0d162011-12-18 23:41:28 +00002181static inline MagickRealType LevelPixel(const double black_point,
cristy780e9ef2011-12-18 23:33:50 +00002182 const double white_point,const double gamma,const MagickRealType pixel)
2183{
2184 double
2185 level_pixel,
2186 scale;
2187
cristy780e9ef2011-12-18 23:33:50 +00002188 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2189 level_pixel=(MagickRealType) QuantumRange*pow(scale*((double) pixel-
2190 black_point),1.0/gamma);
2191 return(level_pixel);
2192}
2193
cristy7c0a0a42011-08-23 17:57:25 +00002194MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2195 const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002196{
2197#define LevelImageTag "Level/Image"
cristy3ed852e2009-09-05 21:47:34 +00002198
cristyc4c8d132010-01-07 01:58:38 +00002199 CacheView
2200 *image_view;
2201
cristy3ed852e2009-09-05 21:47:34 +00002202 MagickBooleanType
2203 status;
2204
cristybb503372010-05-27 20:51:26 +00002205 MagickOffsetType
2206 progress;
2207
cristy8d4629b2010-08-30 17:59:46 +00002208 register ssize_t
2209 i;
2210
cristybb503372010-05-27 20:51:26 +00002211 ssize_t
2212 y;
2213
cristy3ed852e2009-09-05 21:47:34 +00002214 /*
2215 Allocate and initialize levels map.
2216 */
2217 assert(image != (Image *) NULL);
2218 assert(image->signature == MagickSignature);
2219 if (image->debug != MagickFalse)
2220 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2221 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002222#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002223 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002224 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002225#endif
cristybb503372010-05-27 20:51:26 +00002226 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002227 {
2228 /*
2229 Level colormap.
2230 */
cristyed231572011-07-14 02:18:59 +00002231 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002232 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2233 white_point,gamma,image->colormap[i].red));
cristyed231572011-07-14 02:18:59 +00002234 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002235 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2236 white_point,gamma,image->colormap[i].green));
cristyed231572011-07-14 02:18:59 +00002237 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002238 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2239 white_point,gamma,image->colormap[i].blue));
cristyed231572011-07-14 02:18:59 +00002240 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002241 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2242 white_point,gamma,image->colormap[i].alpha));
cristy3ed852e2009-09-05 21:47:34 +00002243 }
2244 /*
2245 Level image.
2246 */
2247 status=MagickTrue;
2248 progress=0;
cristydb070952012-04-20 14:33:00 +00002249 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002250#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002251 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002252 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002253#endif
cristybb503372010-05-27 20:51:26 +00002254 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002255 {
cristy4c08aed2011-07-01 19:47:50 +00002256 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002257 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002258
cristy8d4629b2010-08-30 17:59:46 +00002259 register ssize_t
2260 x;
2261
cristy3ed852e2009-09-05 21:47:34 +00002262 if (status == MagickFalse)
2263 continue;
2264 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002265 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002266 {
2267 status=MagickFalse;
2268 continue;
2269 }
cristybb503372010-05-27 20:51:26 +00002270 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002271 {
cristy7c0a0a42011-08-23 17:57:25 +00002272 register ssize_t
2273 i;
2274
cristy10a6c612012-01-29 21:41:05 +00002275 if (GetPixelMask(image,q) != 0)
2276 {
2277 q+=GetPixelChannels(image);
2278 continue;
2279 }
cristy7c0a0a42011-08-23 17:57:25 +00002280 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2281 {
cristyabace412011-12-11 15:56:53 +00002282 PixelChannel
2283 channel;
2284
cristy7c0a0a42011-08-23 17:57:25 +00002285 PixelTrait
2286 traits;
2287
cristyabace412011-12-11 15:56:53 +00002288 channel=GetPixelChannelMapChannel(image,i);
2289 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002290 if ((traits & UpdatePixelTrait) == 0)
cristy7c0a0a42011-08-23 17:57:25 +00002291 continue;
cristy780e9ef2011-12-18 23:33:50 +00002292 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2293 (MagickRealType) q[i]));
cristy7c0a0a42011-08-23 17:57:25 +00002294 }
cristyed231572011-07-14 02:18:59 +00002295 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002296 }
2297 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2298 status=MagickFalse;
2299 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2300 {
2301 MagickBooleanType
2302 proceed;
2303
cristyb5d5f722009-11-04 03:03:49 +00002304#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002305 #pragma omp critical (MagickCore_LevelImage)
cristy3ed852e2009-09-05 21:47:34 +00002306#endif
2307 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2308 if (proceed == MagickFalse)
2309 status=MagickFalse;
2310 }
2311 }
2312 image_view=DestroyCacheView(image_view);
2313 return(status);
2314}
2315
2316/*
2317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2318% %
2319% %
2320% %
cristy33bd5152011-08-24 01:42:24 +00002321% L e v e l i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002322% %
2323% %
2324% %
2325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2326%
cristy50fbc382011-07-07 02:19:17 +00002327% LevelizeImage() applies the reversed LevelImage() operation to just
cristy3ed852e2009-09-05 21:47:34 +00002328% the specific channels specified. It compresses the full range of color
2329% values, so that they lie between the given black and white points. Gamma is
2330% applied before the values are mapped.
2331%
cristy50fbc382011-07-07 02:19:17 +00002332% LevelizeImage() can be called with by using a +level command line
cristy3ed852e2009-09-05 21:47:34 +00002333% API option, or using a '!' on a -level or LevelImage() geometry string.
2334%
anthony31f1bf72012-01-30 12:37:22 +00002335% It can be used to de-contrast a greyscale image to the exact levels
2336% specified. Or by using specific levels for each channel of an image you
2337% can convert a gray-scale image to any linear color gradient, according to
2338% those levels.
cristy3ed852e2009-09-05 21:47:34 +00002339%
cristy50fbc382011-07-07 02:19:17 +00002340% The format of the LevelizeImage method is:
cristy3ed852e2009-09-05 21:47:34 +00002341%
cristy50fbc382011-07-07 02:19:17 +00002342% MagickBooleanType LevelizeImage(Image *image,const double black_point,
cristy7c0a0a42011-08-23 17:57:25 +00002343% const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002344%
2345% A description of each parameter follows:
2346%
2347% o image: the image.
2348%
cristy3ed852e2009-09-05 21:47:34 +00002349% o black_point: The level to map zero (black) to.
2350%
cristy01e9afd2011-08-10 17:38:41 +00002351% o white_point: The level to map QuantumRange (white) to.
cristy3ed852e2009-09-05 21:47:34 +00002352%
2353% o gamma: adjust gamma by this factor before mapping values.
2354%
cristy7c0a0a42011-08-23 17:57:25 +00002355% o exception: return any errors or warnings in this structure.
2356%
cristy3ed852e2009-09-05 21:47:34 +00002357*/
cristyd1a2c0f2011-02-09 14:14:50 +00002358MagickExport MagickBooleanType LevelizeImage(Image *image,
cristy7c0a0a42011-08-23 17:57:25 +00002359 const double black_point,const double white_point,const double gamma,
2360 ExceptionInfo *exception)
cristyd1a2c0f2011-02-09 14:14:50 +00002361{
cristy3ed852e2009-09-05 21:47:34 +00002362#define LevelizeImageTag "Levelize/Image"
cristyce70c172010-01-07 17:15:30 +00002363#define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
cristy50fbc382011-07-07 02:19:17 +00002364 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
cristy3ed852e2009-09-05 21:47:34 +00002365 black_point))
2366
cristyc4c8d132010-01-07 01:58:38 +00002367 CacheView
2368 *image_view;
2369
cristy3ed852e2009-09-05 21:47:34 +00002370 MagickBooleanType
2371 status;
2372
cristybb503372010-05-27 20:51:26 +00002373 MagickOffsetType
2374 progress;
2375
2376 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002377 i;
2378
cristybb503372010-05-27 20:51:26 +00002379 ssize_t
2380 y;
2381
cristy3ed852e2009-09-05 21:47:34 +00002382 /*
2383 Allocate and initialize levels map.
2384 */
2385 assert(image != (Image *) NULL);
2386 assert(image->signature == MagickSignature);
2387 if (image->debug != MagickFalse)
2388 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2389 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002390#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002391 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002392 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002393#endif
cristybb503372010-05-27 20:51:26 +00002394 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002395 {
2396 /*
2397 Level colormap.
2398 */
cristyed231572011-07-14 02:18:59 +00002399 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002400 image->colormap[i].red=(double) LevelizeValue(
2401 image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002402 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002403 image->colormap[i].green=(double) LevelizeValue(
2404 image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002405 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002406 image->colormap[i].blue=(double) LevelizeValue(
2407 image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002408 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002409 image->colormap[i].alpha=(double) LevelizeValue(
2410 image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002411 }
2412 /*
2413 Level image.
2414 */
2415 status=MagickTrue;
2416 progress=0;
cristydb070952012-04-20 14:33:00 +00002417 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002418#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002419 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002420 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002421#endif
cristybb503372010-05-27 20:51:26 +00002422 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002423 {
cristy4c08aed2011-07-01 19:47:50 +00002424 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002425 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002426
cristy8d4629b2010-08-30 17:59:46 +00002427 register ssize_t
2428 x;
2429
cristy3ed852e2009-09-05 21:47:34 +00002430 if (status == MagickFalse)
2431 continue;
2432 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002433 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002434 {
2435 status=MagickFalse;
2436 continue;
2437 }
cristybb503372010-05-27 20:51:26 +00002438 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002439 {
cristy7c0a0a42011-08-23 17:57:25 +00002440 register ssize_t
2441 i;
2442
cristy10a6c612012-01-29 21:41:05 +00002443 if (GetPixelMask(image,q) != 0)
2444 {
2445 q+=GetPixelChannels(image);
2446 continue;
2447 }
cristy7c0a0a42011-08-23 17:57:25 +00002448 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2449 {
cristyabace412011-12-11 15:56:53 +00002450 PixelChannel
2451 channel;
2452
cristy7c0a0a42011-08-23 17:57:25 +00002453 PixelTrait
2454 traits;
2455
cristyabace412011-12-11 15:56:53 +00002456 channel=GetPixelChannelMapChannel(image,i);
2457 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002458 if ((traits & UpdatePixelTrait) == 0)
cristyb8b0d162011-12-18 23:41:28 +00002459 continue;
cristy780e9ef2011-12-18 23:33:50 +00002460 q[i]=LevelizeValue(q[i]);
cristy7c0a0a42011-08-23 17:57:25 +00002461 }
cristyed231572011-07-14 02:18:59 +00002462 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002463 }
2464 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2465 status=MagickFalse;
2466 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2467 {
2468 MagickBooleanType
2469 proceed;
2470
cristyb5d5f722009-11-04 03:03:49 +00002471#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002472 #pragma omp critical (MagickCore_LevelizeImage)
cristy3ed852e2009-09-05 21:47:34 +00002473#endif
2474 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2475 if (proceed == MagickFalse)
2476 status=MagickFalse;
2477 }
2478 }
cristy8d4629b2010-08-30 17:59:46 +00002479 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002480 return(status);
2481}
2482
2483/*
2484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2485% %
2486% %
2487% %
2488% L e v e l I m a g e C o l o r s %
2489% %
2490% %
2491% %
2492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2493%
cristy490408a2011-07-07 14:42:05 +00002494% LevelImageColors() maps the given color to "black" and "white" values,
cristyee0f8d72009-09-19 00:58:29 +00002495% linearly spreading out the colors, and level values on a channel by channel
2496% bases, as per LevelImage(). The given colors allows you to specify
glennrp1e7f7bc2011-03-02 19:25:28 +00002497% different level ranges for each of the color channels separately.
cristy3ed852e2009-09-05 21:47:34 +00002498%
2499% If the boolean 'invert' is set true the image values will modifyed in the
2500% reverse direction. That is any existing "black" and "white" colors in the
2501% image will become the color values given, with all other values compressed
2502% appropriatally. This effectivally maps a greyscale gradient into the given
2503% color gradient.
2504%
cristy490408a2011-07-07 14:42:05 +00002505% The format of the LevelImageColors method is:
cristy3ed852e2009-09-05 21:47:34 +00002506%
cristy490408a2011-07-07 14:42:05 +00002507% MagickBooleanType LevelImageColors(Image *image,
2508% const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002509% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002510%
2511% A description of each parameter follows:
2512%
2513% o image: the image.
2514%
cristy3ed852e2009-09-05 21:47:34 +00002515% o black_color: The color to map black to/from
2516%
2517% o white_point: The color to map white to/from
2518%
2519% o invert: if true map the colors (levelize), rather than from (level)
2520%
cristy7c0a0a42011-08-23 17:57:25 +00002521% o exception: return any errors or warnings in this structure.
2522%
cristy3ed852e2009-09-05 21:47:34 +00002523*/
cristy490408a2011-07-07 14:42:05 +00002524MagickExport MagickBooleanType LevelImageColors(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00002525 const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002526 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002527{
cristybd5a96c2011-08-21 00:04:26 +00002528 ChannelType
2529 channel_mask;
2530
cristy3ed852e2009-09-05 21:47:34 +00002531 MagickStatusType
2532 status;
2533
2534 /*
2535 Allocate and initialize levels map.
2536 */
2537 assert(image != (Image *) NULL);
2538 assert(image->signature == MagickSignature);
2539 if (image->debug != MagickFalse)
2540 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2541 status=MagickFalse;
2542 if (invert == MagickFalse)
2543 {
cristyed231572011-07-14 02:18:59 +00002544 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002545 {
cristybd5a96c2011-08-21 00:04:26 +00002546 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy01e9afd2011-08-10 17:38:41 +00002547 status|=LevelImage(image,black_color->red,white_color->red,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002548 exception);
cristybd5a96c2011-08-21 00:04:26 +00002549 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002550 }
cristyed231572011-07-14 02:18:59 +00002551 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002552 {
cristybd5a96c2011-08-21 00:04:26 +00002553 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy01e9afd2011-08-10 17:38:41 +00002554 status|=LevelImage(image,black_color->green,white_color->green,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002555 exception);
cristybd5a96c2011-08-21 00:04:26 +00002556 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002557 }
cristyed231572011-07-14 02:18:59 +00002558 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002559 {
cristybd5a96c2011-08-21 00:04:26 +00002560 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy01e9afd2011-08-10 17:38:41 +00002561 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002562 exception);
cristybd5a96c2011-08-21 00:04:26 +00002563 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002564 }
cristyed231572011-07-14 02:18:59 +00002565 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002566 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002567 {
cristybd5a96c2011-08-21 00:04:26 +00002568 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy01e9afd2011-08-10 17:38:41 +00002569 status|=LevelImage(image,black_color->black,white_color->black,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002570 exception);
cristybd5a96c2011-08-21 00:04:26 +00002571 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002572 }
cristyed231572011-07-14 02:18:59 +00002573 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002574 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002575 {
cristybd5a96c2011-08-21 00:04:26 +00002576 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy01e9afd2011-08-10 17:38:41 +00002577 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002578 exception);
cristybd5a96c2011-08-21 00:04:26 +00002579 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002580 }
cristy3ed852e2009-09-05 21:47:34 +00002581 }
2582 else
2583 {
cristyed231572011-07-14 02:18:59 +00002584 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002585 {
cristybd5a96c2011-08-21 00:04:26 +00002586 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002587 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2588 exception);
cristybd5a96c2011-08-21 00:04:26 +00002589 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002590 }
cristyed231572011-07-14 02:18:59 +00002591 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002592 {
cristybd5a96c2011-08-21 00:04:26 +00002593 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002594 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2595 exception);
cristybd5a96c2011-08-21 00:04:26 +00002596 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002597 }
cristyed231572011-07-14 02:18:59 +00002598 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002599 {
cristybd5a96c2011-08-21 00:04:26 +00002600 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002601 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2602 exception);
cristybd5a96c2011-08-21 00:04:26 +00002603 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002604 }
cristyed231572011-07-14 02:18:59 +00002605 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002606 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002607 {
cristybd5a96c2011-08-21 00:04:26 +00002608 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002609 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2610 exception);
cristybd5a96c2011-08-21 00:04:26 +00002611 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002612 }
cristyed231572011-07-14 02:18:59 +00002613 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002614 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002615 {
cristybd5a96c2011-08-21 00:04:26 +00002616 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002617 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2618 exception);
cristybd5a96c2011-08-21 00:04:26 +00002619 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002620 }
cristy3ed852e2009-09-05 21:47:34 +00002621 }
2622 return(status == 0 ? MagickFalse : MagickTrue);
2623}
2624
2625/*
2626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2627% %
2628% %
2629% %
2630% L i n e a r S t r e t c h I m a g e %
2631% %
2632% %
2633% %
2634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2635%
cristyf1611782011-08-01 15:39:13 +00002636% LinearStretchImage() discards any pixels below the black point and above
2637% the white point and levels the remaining pixels.
cristy3ed852e2009-09-05 21:47:34 +00002638%
2639% The format of the LinearStretchImage method is:
2640%
2641% MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002642% const double black_point,const double white_point,
2643% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002644%
2645% A description of each parameter follows:
2646%
2647% o image: the image.
2648%
2649% o black_point: the black point.
2650%
2651% o white_point: the white point.
2652%
cristy33bd5152011-08-24 01:42:24 +00002653% o exception: return any errors or warnings in this structure.
2654%
cristy3ed852e2009-09-05 21:47:34 +00002655*/
2656MagickExport MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002657 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002658{
2659#define LinearStretchImageTag "LinearStretch/Image"
2660
cristy33bd5152011-08-24 01:42:24 +00002661 CacheView
2662 *image_view;
cristy3ed852e2009-09-05 21:47:34 +00002663
cristy3ed852e2009-09-05 21:47:34 +00002664 MagickBooleanType
2665 status;
2666
2667 MagickRealType
2668 *histogram,
2669 intensity;
2670
cristy8d4629b2010-08-30 17:59:46 +00002671 ssize_t
2672 black,
2673 white,
2674 y;
2675
cristy3ed852e2009-09-05 21:47:34 +00002676 /*
2677 Allocate histogram and linear map.
2678 */
2679 assert(image != (Image *) NULL);
2680 assert(image->signature == MagickSignature);
2681 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2682 sizeof(*histogram));
2683 if (histogram == (MagickRealType *) NULL)
2684 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2685 image->filename);
2686 /*
2687 Form histogram.
2688 */
2689 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
cristydb070952012-04-20 14:33:00 +00002690 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00002691 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002692 {
cristy4c08aed2011-07-01 19:47:50 +00002693 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002694 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002695
cristybb503372010-05-27 20:51:26 +00002696 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002697 x;
2698
cristy33bd5152011-08-24 01:42:24 +00002699 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002700 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002701 break;
cristy33bd5152011-08-24 01:42:24 +00002702 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002703 {
cristy4c08aed2011-07-01 19:47:50 +00002704 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
cristyed231572011-07-14 02:18:59 +00002705 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002706 }
2707 }
cristy33bd5152011-08-24 01:42:24 +00002708 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002709 /*
2710 Find the histogram boundaries by locating the black and white point levels.
2711 */
cristy3ed852e2009-09-05 21:47:34 +00002712 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002713 for (black=0; black < (ssize_t) MaxMap; black++)
cristy3ed852e2009-09-05 21:47:34 +00002714 {
2715 intensity+=histogram[black];
2716 if (intensity >= black_point)
2717 break;
2718 }
2719 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002720 for (white=(ssize_t) MaxMap; white != 0; white--)
cristy3ed852e2009-09-05 21:47:34 +00002721 {
2722 intensity+=histogram[white];
2723 if (intensity >= white_point)
2724 break;
2725 }
2726 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
cristyc82a27b2011-10-21 01:07:16 +00002727 status=LevelImage(image,(double) black,(double) white,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00002728 return(status);
2729}
2730
2731/*
2732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2733% %
2734% %
2735% %
2736% M o d u l a t e I m a g e %
2737% %
2738% %
2739% %
2740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2741%
2742% ModulateImage() lets you control the brightness, saturation, and hue
2743% of an image. Modulate represents the brightness, saturation, and hue
2744% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2745% modulation is lightness, saturation, and hue. And if the colorspace is
2746% HWB, use blackness, whiteness, and hue.
2747%
2748% The format of the ModulateImage method is:
2749%
cristy33bd5152011-08-24 01:42:24 +00002750% MagickBooleanType ModulateImage(Image *image,const char *modulate,
2751% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002752%
2753% A description of each parameter follows:
2754%
2755% o image: the image.
2756%
cristy33bd5152011-08-24 01:42:24 +00002757% o modulate: Define the percent change in brightness, saturation, and hue.
2758%
2759% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002760%
2761*/
2762
2763static void ModulateHSB(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002764 const double percent_saturation,const double percent_brightness,double *red,
2765 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002766{
2767 double
2768 brightness,
2769 hue,
2770 saturation;
2771
2772 /*
2773 Increase or decrease color brightness, saturation, or hue.
2774 */
cristy3094b7f2011-10-01 23:18:02 +00002775 assert(red != (double *) NULL);
2776 assert(green != (double *) NULL);
2777 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002778 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2779 hue+=0.5*(0.01*percent_hue-1.0);
2780 while (hue < 0.0)
2781 hue+=1.0;
2782 while (hue > 1.0)
2783 hue-=1.0;
2784 saturation*=0.01*percent_saturation;
2785 brightness*=0.01*percent_brightness;
2786 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2787}
2788
2789static void ModulateHSL(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002790 const double percent_saturation,const double percent_lightness,double *red,
2791 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002792{
2793 double
2794 hue,
2795 lightness,
2796 saturation;
2797
2798 /*
2799 Increase or decrease color lightness, saturation, or hue.
2800 */
cristy3094b7f2011-10-01 23:18:02 +00002801 assert(red != (double *) NULL);
2802 assert(green != (double *) NULL);
2803 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002804 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2805 hue+=0.5*(0.01*percent_hue-1.0);
2806 while (hue < 0.0)
2807 hue+=1.0;
2808 while (hue > 1.0)
2809 hue-=1.0;
2810 saturation*=0.01*percent_saturation;
2811 lightness*=0.01*percent_lightness;
2812 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2813}
2814
cristy3094b7f2011-10-01 23:18:02 +00002815static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002816{
2817 double
2818 blackness,
2819 hue,
2820 whiteness;
2821
2822 /*
2823 Increase or decrease color blackness, whiteness, or hue.
2824 */
cristy3094b7f2011-10-01 23:18:02 +00002825 assert(red != (double *) NULL);
2826 assert(green != (double *) NULL);
2827 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002828 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2829 hue+=0.5*(0.01*percent_hue-1.0);
2830 while (hue < 0.0)
2831 hue+=1.0;
2832 while (hue > 1.0)
2833 hue-=1.0;
2834 blackness*=0.01*percent_blackness;
2835 whiteness*=0.01*percent_whiteness;
2836 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2837}
2838
cristy33bd5152011-08-24 01:42:24 +00002839MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2840 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002841{
2842#define ModulateImageTag "Modulate/Image"
2843
cristyc4c8d132010-01-07 01:58:38 +00002844 CacheView
2845 *image_view;
2846
cristy3ed852e2009-09-05 21:47:34 +00002847 ColorspaceType
2848 colorspace;
2849
2850 const char
2851 *artifact;
2852
2853 double
2854 percent_brightness,
2855 percent_hue,
2856 percent_saturation;
2857
cristy3ed852e2009-09-05 21:47:34 +00002858 GeometryInfo
2859 geometry_info;
2860
cristy3ed852e2009-09-05 21:47:34 +00002861 MagickBooleanType
2862 status;
2863
cristybb503372010-05-27 20:51:26 +00002864 MagickOffsetType
2865 progress;
2866
cristy3ed852e2009-09-05 21:47:34 +00002867 MagickStatusType
2868 flags;
2869
cristybb503372010-05-27 20:51:26 +00002870 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002871 i;
2872
cristybb503372010-05-27 20:51:26 +00002873 ssize_t
2874 y;
2875
cristy3ed852e2009-09-05 21:47:34 +00002876 /*
cristy2b726bd2010-01-11 01:05:39 +00002877 Initialize modulate table.
cristy3ed852e2009-09-05 21:47:34 +00002878 */
2879 assert(image != (Image *) NULL);
2880 assert(image->signature == MagickSignature);
2881 if (image->debug != MagickFalse)
2882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2883 if (modulate == (char *) NULL)
2884 return(MagickFalse);
2885 flags=ParseGeometry(modulate,&geometry_info);
2886 percent_brightness=geometry_info.rho;
2887 percent_saturation=geometry_info.sigma;
2888 if ((flags & SigmaValue) == 0)
2889 percent_saturation=100.0;
2890 percent_hue=geometry_info.xi;
2891 if ((flags & XiValue) == 0)
2892 percent_hue=100.0;
2893 colorspace=UndefinedColorspace;
2894 artifact=GetImageArtifact(image,"modulate:colorspace");
2895 if (artifact != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00002896 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +00002897 MagickFalse,artifact);
2898 if (image->storage_class == PseudoClass)
2899 {
2900 /*
2901 Modulate colormap.
2902 */
cristyb5d5f722009-11-04 03:03:49 +00002903#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002904 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002905 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002906#endif
cristybb503372010-05-27 20:51:26 +00002907 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002908 switch (colorspace)
2909 {
2910 case HSBColorspace:
2911 {
2912 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2913 &image->colormap[i].red,&image->colormap[i].green,
2914 &image->colormap[i].blue);
2915 break;
2916 }
2917 case HSLColorspace:
2918 default:
2919 {
2920 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2921 &image->colormap[i].red,&image->colormap[i].green,
2922 &image->colormap[i].blue);
2923 break;
2924 }
2925 case HWBColorspace:
2926 {
2927 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2928 &image->colormap[i].red,&image->colormap[i].green,
2929 &image->colormap[i].blue);
2930 break;
2931 }
2932 }
2933 }
2934 /*
2935 Modulate image.
2936 */
2937 status=MagickTrue;
2938 progress=0;
cristydb070952012-04-20 14:33:00 +00002939 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002940#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002941 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00002942 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002943#endif
cristybb503372010-05-27 20:51:26 +00002944 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002945 {
cristy3094b7f2011-10-01 23:18:02 +00002946 double
cristy5afeab82011-04-30 01:30:09 +00002947 blue,
2948 green,
2949 red;
2950
cristy4c08aed2011-07-01 19:47:50 +00002951 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002952 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002953
cristy8d4629b2010-08-30 17:59:46 +00002954 register ssize_t
2955 x;
2956
cristy3ed852e2009-09-05 21:47:34 +00002957 if (status == MagickFalse)
2958 continue;
2959 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002960 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002961 {
2962 status=MagickFalse;
2963 continue;
2964 }
cristybb503372010-05-27 20:51:26 +00002965 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002966 {
cristyda1f9c12011-10-02 21:39:49 +00002967 red=(double) GetPixelRed(image,q);
2968 green=(double) GetPixelGreen(image,q);
2969 blue=(double) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002970 switch (colorspace)
2971 {
2972 case HSBColorspace:
2973 {
2974 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002975 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002976 break;
2977 }
2978 case HSLColorspace:
2979 default:
2980 {
2981 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002982 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002983 break;
2984 }
2985 case HWBColorspace:
2986 {
2987 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002988 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002989 break;
2990 }
2991 }
cristy3094b7f2011-10-01 23:18:02 +00002992 SetPixelRed(image,ClampToQuantum(red),q);
2993 SetPixelGreen(image,ClampToQuantum(green),q);
2994 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +00002995 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002996 }
2997 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2998 status=MagickFalse;
2999 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3000 {
3001 MagickBooleanType
3002 proceed;
3003
cristyb5d5f722009-11-04 03:03:49 +00003004#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003005 #pragma omp critical (MagickCore_ModulateImage)
cristy3ed852e2009-09-05 21:47:34 +00003006#endif
3007 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3008 if (proceed == MagickFalse)
3009 status=MagickFalse;
3010 }
3011 }
3012 image_view=DestroyCacheView(image_view);
3013 return(status);
3014}
3015
3016/*
3017%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3018% %
3019% %
3020% %
3021% N e g a t e I m a g e %
3022% %
3023% %
3024% %
3025%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3026%
3027% NegateImage() negates the colors in the reference image. The grayscale
3028% option means that only grayscale values within the image are negated.
3029%
cristy50fbc382011-07-07 02:19:17 +00003030% The format of the NegateImage method is:
cristy3ed852e2009-09-05 21:47:34 +00003031%
3032% MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003033% const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003034%
3035% A description of each parameter follows:
3036%
3037% o image: the image.
3038%
cristy3ed852e2009-09-05 21:47:34 +00003039% o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3040%
cristyb3e7c6c2011-07-24 01:43:55 +00003041% o exception: return any errors or warnings in this structure.
3042%
cristy3ed852e2009-09-05 21:47:34 +00003043*/
cristy3ed852e2009-09-05 21:47:34 +00003044MagickExport MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003045 const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003046{
cristy3ed852e2009-09-05 21:47:34 +00003047#define NegateImageTag "Negate/Image"
3048
cristyc4c8d132010-01-07 01:58:38 +00003049 CacheView
3050 *image_view;
3051
cristy3ed852e2009-09-05 21:47:34 +00003052 MagickBooleanType
3053 status;
3054
cristybb503372010-05-27 20:51:26 +00003055 MagickOffsetType
3056 progress;
3057
3058 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003059 i;
3060
cristybb503372010-05-27 20:51:26 +00003061 ssize_t
3062 y;
3063
cristy3ed852e2009-09-05 21:47:34 +00003064 assert(image != (Image *) NULL);
3065 assert(image->signature == MagickSignature);
3066 if (image->debug != MagickFalse)
3067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3068 if (image->storage_class == PseudoClass)
3069 {
3070 /*
3071 Negate colormap.
3072 */
cristyb5d5f722009-11-04 03:03:49 +00003073#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003074 #pragma omp parallel for schedule(static) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003075 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003076#endif
cristybb503372010-05-27 20:51:26 +00003077 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003078 {
3079 if (grayscale != MagickFalse)
3080 if ((image->colormap[i].red != image->colormap[i].green) ||
3081 (image->colormap[i].green != image->colormap[i].blue))
3082 continue;
cristyed231572011-07-14 02:18:59 +00003083 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003084 image->colormap[i].red=(Quantum) QuantumRange-
3085 image->colormap[i].red;
cristyed231572011-07-14 02:18:59 +00003086 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003087 image->colormap[i].green=(Quantum) QuantumRange-
3088 image->colormap[i].green;
cristyed231572011-07-14 02:18:59 +00003089 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003090 image->colormap[i].blue=(Quantum) QuantumRange-
3091 image->colormap[i].blue;
3092 }
3093 }
3094 /*
3095 Negate image.
3096 */
3097 status=MagickTrue;
3098 progress=0;
cristydb070952012-04-20 14:33:00 +00003099 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003100 if (grayscale != MagickFalse)
3101 {
cristyb5d5f722009-11-04 03:03:49 +00003102#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003103 #pragma omp parallel for schedule(static) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003104 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003105#endif
cristybb503372010-05-27 20:51:26 +00003106 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003107 {
3108 MagickBooleanType
3109 sync;
3110
cristy4c08aed2011-07-01 19:47:50 +00003111 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003112 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003113
cristy8d4629b2010-08-30 17:59:46 +00003114 register ssize_t
3115 x;
3116
cristy3ed852e2009-09-05 21:47:34 +00003117 if (status == MagickFalse)
3118 continue;
3119 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3120 exception);
cristyacd2ed22011-08-30 01:44:23 +00003121 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003122 {
3123 status=MagickFalse;
3124 continue;
3125 }
cristybb503372010-05-27 20:51:26 +00003126 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003127 {
cristy9aa95be2011-07-20 21:56:45 +00003128 register ssize_t
3129 i;
3130
cristy10a6c612012-01-29 21:41:05 +00003131 if ((GetPixelMask(image,q) != 0) ||
3132 (IsPixelGray(image,q) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003133 {
cristya30d9ba2011-07-23 21:00:48 +00003134 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003135 continue;
3136 }
cristya30d9ba2011-07-23 21:00:48 +00003137 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy9aa95be2011-07-20 21:56:45 +00003138 {
cristyabace412011-12-11 15:56:53 +00003139 PixelChannel
3140 channel;
3141
cristy1aaa3cd2011-08-21 23:48:17 +00003142 PixelTrait
cristy9aa95be2011-07-20 21:56:45 +00003143 traits;
3144
cristyabace412011-12-11 15:56:53 +00003145 channel=GetPixelChannelMapChannel(image,i);
3146 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003147 if ((traits & UpdatePixelTrait) == 0)
3148 continue;
3149 q[i]=QuantumRange-q[i];
cristy9aa95be2011-07-20 21:56:45 +00003150 }
cristya30d9ba2011-07-23 21:00:48 +00003151 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003152 }
3153 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3154 if (sync == MagickFalse)
3155 status=MagickFalse;
3156 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3157 {
3158 MagickBooleanType
3159 proceed;
3160
cristyb5d5f722009-11-04 03:03:49 +00003161#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003162 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003163#endif
3164 proceed=SetImageProgress(image,NegateImageTag,progress++,
3165 image->rows);
3166 if (proceed == MagickFalse)
3167 status=MagickFalse;
3168 }
3169 }
3170 image_view=DestroyCacheView(image_view);
3171 return(MagickTrue);
3172 }
3173 /*
3174 Negate image.
3175 */
cristyb5d5f722009-11-04 03:03:49 +00003176#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003177 #pragma omp parallel for schedule(static) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003178 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003179#endif
cristybb503372010-05-27 20:51:26 +00003180 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003181 {
cristy4c08aed2011-07-01 19:47:50 +00003182 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003183 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003184
cristy8d4629b2010-08-30 17:59:46 +00003185 register ssize_t
3186 x;
3187
cristy3ed852e2009-09-05 21:47:34 +00003188 if (status == MagickFalse)
3189 continue;
3190 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003191 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003192 {
3193 status=MagickFalse;
3194 continue;
3195 }
cristybb503372010-05-27 20:51:26 +00003196 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003197 {
cristyf7dc44c2011-07-20 14:41:15 +00003198 register ssize_t
3199 i;
3200
cristyd09f8802012-02-04 16:44:10 +00003201 if (GetPixelMask(image,q) != 0)
3202 {
3203 q+=GetPixelChannels(image);
3204 continue;
3205 }
cristya30d9ba2011-07-23 21:00:48 +00003206 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyf7dc44c2011-07-20 14:41:15 +00003207 {
cristyabace412011-12-11 15:56:53 +00003208 PixelChannel
3209 channel;
3210
cristy1aaa3cd2011-08-21 23:48:17 +00003211 PixelTrait
cristyf7dc44c2011-07-20 14:41:15 +00003212 traits;
3213
cristyabace412011-12-11 15:56:53 +00003214 channel=GetPixelChannelMapChannel(image,i);
3215 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003216 if ((traits & UpdatePixelTrait) == 0)
cristyec9e3a62012-02-01 02:09:32 +00003217 continue;
3218 q[i]=QuantumRange-q[i];
cristyf7dc44c2011-07-20 14:41:15 +00003219 }
cristya30d9ba2011-07-23 21:00:48 +00003220 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003221 }
3222 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3223 status=MagickFalse;
3224 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3225 {
3226 MagickBooleanType
3227 proceed;
3228
cristyb5d5f722009-11-04 03:03:49 +00003229#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003230 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003231#endif
3232 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3233 if (proceed == MagickFalse)
3234 status=MagickFalse;
3235 }
3236 }
3237 image_view=DestroyCacheView(image_view);
3238 return(status);
3239}
3240
3241/*
3242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3243% %
3244% %
3245% %
3246% N o r m a l i z e I m a g e %
3247% %
3248% %
3249% %
3250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3251%
anthony207ce472012-04-04 06:21:26 +00003252% The NormalizeImage() method enhances the contrast of a color image by
3253% mapping the darkest 2 percent of all pixel to black and the brightest
3254% 1 percent to white.
cristy3ed852e2009-09-05 21:47:34 +00003255%
3256% The format of the NormalizeImage method is:
3257%
cristye23ec9d2011-08-16 18:15:40 +00003258% MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003259%
3260% A description of each parameter follows:
3261%
3262% o image: the image.
3263%
cristye23ec9d2011-08-16 18:15:40 +00003264% o exception: return any errors or warnings in this structure.
3265%
cristy3ed852e2009-09-05 21:47:34 +00003266*/
cristye23ec9d2011-08-16 18:15:40 +00003267MagickExport MagickBooleanType NormalizeImage(Image *image,
3268 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003269{
cristy3ed852e2009-09-05 21:47:34 +00003270 double
3271 black_point,
3272 white_point;
3273
cristy530239c2010-07-25 17:34:26 +00003274 black_point=(double) image->columns*image->rows*0.0015;
3275 white_point=(double) image->columns*image->rows*0.9995;
cristye23ec9d2011-08-16 18:15:40 +00003276 return(ContrastStretchImage(image,black_point,white_point,exception));
cristy3ed852e2009-09-05 21:47:34 +00003277}
3278
3279/*
3280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3281% %
3282% %
3283% %
3284% S i g m o i d a l C o n t r a s t I m a g e %
3285% %
3286% %
3287% %
3288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3289%
3290% SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3291% sigmoidal contrast algorithm. Increase the contrast of the image using a
3292% sigmoidal transfer function without saturating highlights or shadows.
3293% Contrast indicates how much to increase the contrast (0 is none; 3 is
anthony207ce472012-04-04 06:21:26 +00003294% typical; 20 is pushing it); mid-point indicates where midtones fall in the
3295% resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3296% sharpen to MagickTrue to increase the image contrast otherwise the contrast
3297% is reduced.
cristy3ed852e2009-09-05 21:47:34 +00003298%
3299% The format of the SigmoidalContrastImage method is:
3300%
3301% MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003302% const MagickBooleanType sharpen,const char *levels,
3303% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003304%
3305% A description of each parameter follows:
3306%
3307% o image: the image.
3308%
cristy3ed852e2009-09-05 21:47:34 +00003309% o sharpen: Increase or decrease image contrast.
3310%
cristyfa769582010-09-30 23:30:03 +00003311% o alpha: strength of the contrast, the larger the number the more
3312% 'threshold-like' it becomes.
cristy3ed852e2009-09-05 21:47:34 +00003313%
cristyfa769582010-09-30 23:30:03 +00003314% o beta: midpoint of the function as a color value 0 to QuantumRange.
cristy3ed852e2009-09-05 21:47:34 +00003315%
cristy33bd5152011-08-24 01:42:24 +00003316% o exception: return any errors or warnings in this structure.
3317%
cristy3ed852e2009-09-05 21:47:34 +00003318*/
cristy3ed852e2009-09-05 21:47:34 +00003319MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003320 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3321 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003322{
3323#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3324
cristyc4c8d132010-01-07 01:58:38 +00003325 CacheView
3326 *image_view;
3327
cristy3ed852e2009-09-05 21:47:34 +00003328 MagickBooleanType
3329 status;
3330
cristybb503372010-05-27 20:51:26 +00003331 MagickOffsetType
3332 progress;
3333
cristy3ed852e2009-09-05 21:47:34 +00003334 MagickRealType
3335 *sigmoidal_map;
3336
cristybb503372010-05-27 20:51:26 +00003337 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003338 i;
3339
cristybb503372010-05-27 20:51:26 +00003340 ssize_t
3341 y;
3342
cristy3ed852e2009-09-05 21:47:34 +00003343 /*
3344 Allocate and initialize sigmoidal maps.
3345 */
3346 assert(image != (Image *) NULL);
3347 assert(image->signature == MagickSignature);
3348 if (image->debug != MagickFalse)
3349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3350 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3351 sizeof(*sigmoidal_map));
3352 if (sigmoidal_map == (MagickRealType *) NULL)
3353 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3354 image->filename);
3355 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
cristyb5d5f722009-11-04 03:03:49 +00003356#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003357 #pragma omp parallel for schedule(static) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003358 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003359#endif
cristybb503372010-05-27 20:51:26 +00003360 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003361 {
3362 if (sharpen != MagickFalse)
3363 {
anthony207ce472012-04-04 06:21:26 +00003364#define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3365#if 1
3366 /* Simpilified function scaling,
3367 * with better 'contrast=0' or 'flatline' handling (greyscale)
3368 */
3369 double
3370 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3371 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3372 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3373 (MagickRealType)(MaxMap*(
3374 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3375 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3376#else
3377 /* Scaled sigmoidal formula...
3378 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3379 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
cristy3ed852e2009-09-05 21:47:34 +00003380 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3381 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
anthony207ce472012-04-04 06:21:26 +00003382 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3383 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3384 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3385 (double) QuantumRange)))))+0.5));
3386#endif
cristy3ed852e2009-09-05 21:47:34 +00003387 continue;
3388 }
anthony207ce472012-04-04 06:21:26 +00003389#if 1
3390 {
3391 /* Inverse -- See
3392 http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
3393 */
3394 double
3395 min = sigmoidal(contrast,1.0,0.0),
3396 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3397 xi = min+(double)i/MaxMap*(max-min);
3398 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3399 (MagickRealType)(MaxMap*(
3400 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3401 }
3402#else
3403 /* expanded form of the above */
cristy3ed852e2009-09-05 21:47:34 +00003404 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
anthony207ce472012-04-04 06:21:26 +00003405 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3406 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3407 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3408 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3409 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3410 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3411 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3412 (double) QuantumRange*contrast))))))/contrast)));
3413#endif
cristy3ed852e2009-09-05 21:47:34 +00003414 }
3415 if (image->storage_class == PseudoClass)
3416 {
3417 /*
3418 Sigmoidal-contrast enhance colormap.
3419 */
cristyb5d5f722009-11-04 03:03:49 +00003420#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003421 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003422 dynamic_number_threads(image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003423#endif
cristybb503372010-05-27 20:51:26 +00003424 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003425 {
cristyed231572011-07-14 02:18:59 +00003426 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003427 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3428 ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00003429 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003430 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3431 ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00003432 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003433 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3434 ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00003435 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003436 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3437 ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00003438 }
3439 }
3440 /*
3441 Sigmoidal-contrast enhance image.
3442 */
3443 status=MagickTrue;
3444 progress=0;
cristydb070952012-04-20 14:33:00 +00003445 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003446#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003447 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003448 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003449#endif
cristybb503372010-05-27 20:51:26 +00003450 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003451 {
cristy4c08aed2011-07-01 19:47:50 +00003452 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003453 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003454
cristy8d4629b2010-08-30 17:59:46 +00003455 register ssize_t
3456 x;
3457
cristy3ed852e2009-09-05 21:47:34 +00003458 if (status == MagickFalse)
3459 continue;
3460 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003461 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003462 {
3463 status=MagickFalse;
3464 continue;
3465 }
cristybb503372010-05-27 20:51:26 +00003466 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003467 {
cristy33bd5152011-08-24 01:42:24 +00003468 register ssize_t
3469 i;
3470
cristy10a6c612012-01-29 21:41:05 +00003471 if (GetPixelMask(image,q) != 0)
3472 {
3473 q+=GetPixelChannels(image);
3474 continue;
3475 }
cristy33bd5152011-08-24 01:42:24 +00003476 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3477 {
cristyabace412011-12-11 15:56:53 +00003478 PixelChannel
3479 channel;
3480
cristy33bd5152011-08-24 01:42:24 +00003481 PixelTrait
3482 traits;
3483
cristyabace412011-12-11 15:56:53 +00003484 channel=GetPixelChannelMapChannel(image,i);
3485 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003486 if ((traits & UpdatePixelTrait) == 0)
3487 continue;
3488 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
cristy33bd5152011-08-24 01:42:24 +00003489 }
cristyed231572011-07-14 02:18:59 +00003490 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003491 }
3492 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3493 status=MagickFalse;
3494 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3495 {
3496 MagickBooleanType
3497 proceed;
3498
cristyb5d5f722009-11-04 03:03:49 +00003499#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003500 #pragma omp critical (MagickCore_SigmoidalContrastImage)
cristy3ed852e2009-09-05 21:47:34 +00003501#endif
3502 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3503 image->rows);
3504 if (proceed == MagickFalse)
3505 status=MagickFalse;
3506 }
3507 }
3508 image_view=DestroyCacheView(image_view);
3509 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);
3510 return(status);
3511}