blob: c3c6447bd49ca976bc667b9f128c008c0fee220f [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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000347 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000360 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000710 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000731 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000759 dynamic_number_threads(image,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;
cristycf12ee42012-06-14 11:02:23 +0000859 ConvertsRGBToHSB(*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;
cristycf12ee42012-06-14 11:02:23 +0000867 ConvertHSBTosRGB(hue,saturation,brightness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +0000868}
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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000915 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001102 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001140 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001172 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001214 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001367 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001599 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001621 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001651 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001696 dynamic_number_threads(image,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 \
cristy4ee2b0c2012-05-15 00:30:35 +00001835 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001847 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00001873 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00002043 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00002224 dynamic_number_threads(image,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) \
cristy4ee2b0c2012-05-15 00:30:35 +00002252 dynamic_number_threads(image,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);
cristy25ee24d2012-05-18 12:25:59 +00002389 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2390 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002391 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002392#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002393 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002394 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002395#endif
cristybb503372010-05-27 20:51:26 +00002396 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002397 {
2398 /*
2399 Level colormap.
2400 */
cristyed231572011-07-14 02:18:59 +00002401 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002402 image->colormap[i].red=(double) LevelizeValue(
2403 image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002404 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002405 image->colormap[i].green=(double) LevelizeValue(
2406 image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002407 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002408 image->colormap[i].blue=(double) LevelizeValue(
2409 image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002410 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002411 image->colormap[i].alpha=(double) LevelizeValue(
2412 image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002413 }
2414 /*
2415 Level image.
2416 */
2417 status=MagickTrue;
2418 progress=0;
cristydb070952012-04-20 14:33:00 +00002419 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002420#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002421 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002422 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002423#endif
cristybb503372010-05-27 20:51:26 +00002424 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002425 {
cristy4c08aed2011-07-01 19:47:50 +00002426 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002427 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002428
cristy8d4629b2010-08-30 17:59:46 +00002429 register ssize_t
2430 x;
2431
cristy3ed852e2009-09-05 21:47:34 +00002432 if (status == MagickFalse)
2433 continue;
2434 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002435 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002436 {
2437 status=MagickFalse;
2438 continue;
2439 }
cristybb503372010-05-27 20:51:26 +00002440 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002441 {
cristy7c0a0a42011-08-23 17:57:25 +00002442 register ssize_t
2443 i;
2444
cristy10a6c612012-01-29 21:41:05 +00002445 if (GetPixelMask(image,q) != 0)
2446 {
2447 q+=GetPixelChannels(image);
2448 continue;
2449 }
cristy7c0a0a42011-08-23 17:57:25 +00002450 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2451 {
cristyabace412011-12-11 15:56:53 +00002452 PixelChannel
2453 channel;
2454
cristy7c0a0a42011-08-23 17:57:25 +00002455 PixelTrait
2456 traits;
2457
cristyabace412011-12-11 15:56:53 +00002458 channel=GetPixelChannelMapChannel(image,i);
2459 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002460 if ((traits & UpdatePixelTrait) == 0)
cristyb8b0d162011-12-18 23:41:28 +00002461 continue;
cristy780e9ef2011-12-18 23:33:50 +00002462 q[i]=LevelizeValue(q[i]);
cristy7c0a0a42011-08-23 17:57:25 +00002463 }
cristyed231572011-07-14 02:18:59 +00002464 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002465 }
2466 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2467 status=MagickFalse;
2468 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2469 {
2470 MagickBooleanType
2471 proceed;
2472
cristyb5d5f722009-11-04 03:03:49 +00002473#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002474 #pragma omp critical (MagickCore_LevelizeImage)
cristy3ed852e2009-09-05 21:47:34 +00002475#endif
2476 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2477 if (proceed == MagickFalse)
2478 status=MagickFalse;
2479 }
2480 }
cristy8d4629b2010-08-30 17:59:46 +00002481 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002482 return(status);
2483}
2484
2485/*
2486%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2487% %
2488% %
2489% %
2490% L e v e l I m a g e C o l o r s %
2491% %
2492% %
2493% %
2494%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2495%
cristy490408a2011-07-07 14:42:05 +00002496% LevelImageColors() maps the given color to "black" and "white" values,
cristyee0f8d72009-09-19 00:58:29 +00002497% linearly spreading out the colors, and level values on a channel by channel
2498% bases, as per LevelImage(). The given colors allows you to specify
glennrp1e7f7bc2011-03-02 19:25:28 +00002499% different level ranges for each of the color channels separately.
cristy3ed852e2009-09-05 21:47:34 +00002500%
2501% If the boolean 'invert' is set true the image values will modifyed in the
2502% reverse direction. That is any existing "black" and "white" colors in the
2503% image will become the color values given, with all other values compressed
2504% appropriatally. This effectivally maps a greyscale gradient into the given
2505% color gradient.
2506%
cristy490408a2011-07-07 14:42:05 +00002507% The format of the LevelImageColors method is:
cristy3ed852e2009-09-05 21:47:34 +00002508%
cristy490408a2011-07-07 14:42:05 +00002509% MagickBooleanType LevelImageColors(Image *image,
2510% const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002511% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002512%
2513% A description of each parameter follows:
2514%
2515% o image: the image.
2516%
cristy3ed852e2009-09-05 21:47:34 +00002517% o black_color: The color to map black to/from
2518%
2519% o white_point: The color to map white to/from
2520%
2521% o invert: if true map the colors (levelize), rather than from (level)
2522%
cristy7c0a0a42011-08-23 17:57:25 +00002523% o exception: return any errors or warnings in this structure.
2524%
cristy3ed852e2009-09-05 21:47:34 +00002525*/
cristy490408a2011-07-07 14:42:05 +00002526MagickExport MagickBooleanType LevelImageColors(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00002527 const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002528 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002529{
cristybd5a96c2011-08-21 00:04:26 +00002530 ChannelType
2531 channel_mask;
2532
cristy3ed852e2009-09-05 21:47:34 +00002533 MagickStatusType
2534 status;
2535
2536 /*
2537 Allocate and initialize levels map.
2538 */
2539 assert(image != (Image *) NULL);
2540 assert(image->signature == MagickSignature);
2541 if (image->debug != MagickFalse)
2542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2543 status=MagickFalse;
2544 if (invert == MagickFalse)
2545 {
cristyed231572011-07-14 02:18:59 +00002546 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002547 {
cristybd5a96c2011-08-21 00:04:26 +00002548 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy01e9afd2011-08-10 17:38:41 +00002549 status|=LevelImage(image,black_color->red,white_color->red,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002550 exception);
cristybd5a96c2011-08-21 00:04:26 +00002551 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002552 }
cristyed231572011-07-14 02:18:59 +00002553 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002554 {
cristybd5a96c2011-08-21 00:04:26 +00002555 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy01e9afd2011-08-10 17:38:41 +00002556 status|=LevelImage(image,black_color->green,white_color->green,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002557 exception);
cristybd5a96c2011-08-21 00:04:26 +00002558 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002559 }
cristyed231572011-07-14 02:18:59 +00002560 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002561 {
cristybd5a96c2011-08-21 00:04:26 +00002562 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy01e9afd2011-08-10 17:38:41 +00002563 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002564 exception);
cristybd5a96c2011-08-21 00:04:26 +00002565 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002566 }
cristyed231572011-07-14 02:18:59 +00002567 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002568 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002569 {
cristybd5a96c2011-08-21 00:04:26 +00002570 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy01e9afd2011-08-10 17:38:41 +00002571 status|=LevelImage(image,black_color->black,white_color->black,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002572 exception);
cristybd5a96c2011-08-21 00:04:26 +00002573 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002574 }
cristyed231572011-07-14 02:18:59 +00002575 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002576 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002577 {
cristybd5a96c2011-08-21 00:04:26 +00002578 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy01e9afd2011-08-10 17:38:41 +00002579 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002580 exception);
cristybd5a96c2011-08-21 00:04:26 +00002581 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002582 }
cristy3ed852e2009-09-05 21:47:34 +00002583 }
2584 else
2585 {
cristyed231572011-07-14 02:18:59 +00002586 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002587 {
cristybd5a96c2011-08-21 00:04:26 +00002588 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002589 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2590 exception);
cristybd5a96c2011-08-21 00:04:26 +00002591 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002592 }
cristyed231572011-07-14 02:18:59 +00002593 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002594 {
cristybd5a96c2011-08-21 00:04:26 +00002595 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002596 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2597 exception);
cristybd5a96c2011-08-21 00:04:26 +00002598 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002599 }
cristyed231572011-07-14 02:18:59 +00002600 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002601 {
cristybd5a96c2011-08-21 00:04:26 +00002602 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002603 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2604 exception);
cristybd5a96c2011-08-21 00:04:26 +00002605 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002606 }
cristyed231572011-07-14 02:18:59 +00002607 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002608 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002609 {
cristybd5a96c2011-08-21 00:04:26 +00002610 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002611 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2612 exception);
cristybd5a96c2011-08-21 00:04:26 +00002613 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002614 }
cristyed231572011-07-14 02:18:59 +00002615 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002616 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002617 {
cristybd5a96c2011-08-21 00:04:26 +00002618 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002619 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2620 exception);
cristybd5a96c2011-08-21 00:04:26 +00002621 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002622 }
cristy3ed852e2009-09-05 21:47:34 +00002623 }
2624 return(status == 0 ? MagickFalse : MagickTrue);
2625}
2626
2627/*
2628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2629% %
2630% %
2631% %
2632% L i n e a r S t r e t c h I m a g e %
2633% %
2634% %
2635% %
2636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2637%
cristyf1611782011-08-01 15:39:13 +00002638% LinearStretchImage() discards any pixels below the black point and above
2639% the white point and levels the remaining pixels.
cristy3ed852e2009-09-05 21:47:34 +00002640%
2641% The format of the LinearStretchImage method is:
2642%
2643% MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002644% const double black_point,const double white_point,
2645% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002646%
2647% A description of each parameter follows:
2648%
2649% o image: the image.
2650%
2651% o black_point: the black point.
2652%
2653% o white_point: the white point.
2654%
cristy33bd5152011-08-24 01:42:24 +00002655% o exception: return any errors or warnings in this structure.
2656%
cristy3ed852e2009-09-05 21:47:34 +00002657*/
2658MagickExport MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002659 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002660{
2661#define LinearStretchImageTag "LinearStretch/Image"
2662
cristy33bd5152011-08-24 01:42:24 +00002663 CacheView
2664 *image_view;
cristy3ed852e2009-09-05 21:47:34 +00002665
cristy3ed852e2009-09-05 21:47:34 +00002666 MagickBooleanType
2667 status;
2668
2669 MagickRealType
2670 *histogram,
2671 intensity;
2672
cristy8d4629b2010-08-30 17:59:46 +00002673 ssize_t
2674 black,
2675 white,
2676 y;
2677
cristy3ed852e2009-09-05 21:47:34 +00002678 /*
2679 Allocate histogram and linear map.
2680 */
2681 assert(image != (Image *) NULL);
2682 assert(image->signature == MagickSignature);
2683 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2684 sizeof(*histogram));
2685 if (histogram == (MagickRealType *) NULL)
2686 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2687 image->filename);
2688 /*
2689 Form histogram.
2690 */
2691 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
cristydb070952012-04-20 14:33:00 +00002692 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00002693 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002694 {
cristy4c08aed2011-07-01 19:47:50 +00002695 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002696 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002697
cristybb503372010-05-27 20:51:26 +00002698 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002699 x;
2700
cristy33bd5152011-08-24 01:42:24 +00002701 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002702 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002703 break;
cristy33bd5152011-08-24 01:42:24 +00002704 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002705 {
cristy4c08aed2011-07-01 19:47:50 +00002706 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
cristyed231572011-07-14 02:18:59 +00002707 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002708 }
2709 }
cristy33bd5152011-08-24 01:42:24 +00002710 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002711 /*
2712 Find the histogram boundaries by locating the black and white point levels.
2713 */
cristy3ed852e2009-09-05 21:47:34 +00002714 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002715 for (black=0; black < (ssize_t) MaxMap; black++)
cristy3ed852e2009-09-05 21:47:34 +00002716 {
2717 intensity+=histogram[black];
2718 if (intensity >= black_point)
2719 break;
2720 }
2721 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002722 for (white=(ssize_t) MaxMap; white != 0; white--)
cristy3ed852e2009-09-05 21:47:34 +00002723 {
2724 intensity+=histogram[white];
2725 if (intensity >= white_point)
2726 break;
2727 }
2728 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
cristyc82a27b2011-10-21 01:07:16 +00002729 status=LevelImage(image,(double) black,(double) white,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00002730 return(status);
2731}
2732
2733/*
2734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2735% %
2736% %
2737% %
2738% M o d u l a t e I m a g e %
2739% %
2740% %
2741% %
2742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2743%
2744% ModulateImage() lets you control the brightness, saturation, and hue
2745% of an image. Modulate represents the brightness, saturation, and hue
2746% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2747% modulation is lightness, saturation, and hue. And if the colorspace is
2748% HWB, use blackness, whiteness, and hue.
2749%
2750% The format of the ModulateImage method is:
2751%
cristy33bd5152011-08-24 01:42:24 +00002752% MagickBooleanType ModulateImage(Image *image,const char *modulate,
2753% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002754%
2755% A description of each parameter follows:
2756%
2757% o image: the image.
2758%
cristy33bd5152011-08-24 01:42:24 +00002759% o modulate: Define the percent change in brightness, saturation, and hue.
2760%
2761% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002762%
2763*/
2764
2765static void ModulateHSB(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002766 const double percent_saturation,const double percent_brightness,double *red,
2767 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002768{
2769 double
2770 brightness,
2771 hue,
2772 saturation;
2773
2774 /*
2775 Increase or decrease color brightness, saturation, or hue.
2776 */
cristy3094b7f2011-10-01 23:18:02 +00002777 assert(red != (double *) NULL);
2778 assert(green != (double *) NULL);
2779 assert(blue != (double *) NULL);
cristycf12ee42012-06-14 11:02:23 +00002780 ConvertsRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
cristy3ed852e2009-09-05 21:47:34 +00002781 hue+=0.5*(0.01*percent_hue-1.0);
2782 while (hue < 0.0)
2783 hue+=1.0;
2784 while (hue > 1.0)
2785 hue-=1.0;
2786 saturation*=0.01*percent_saturation;
2787 brightness*=0.01*percent_brightness;
cristycf12ee42012-06-14 11:02:23 +00002788 ConvertHSBTosRGB(hue,saturation,brightness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002789}
2790
2791static void ModulateHSL(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002792 const double percent_saturation,const double percent_lightness,double *red,
2793 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002794{
2795 double
2796 hue,
2797 lightness,
2798 saturation;
2799
2800 /*
2801 Increase or decrease color lightness, saturation, or hue.
2802 */
cristy3094b7f2011-10-01 23:18:02 +00002803 assert(red != (double *) NULL);
2804 assert(green != (double *) NULL);
2805 assert(blue != (double *) NULL);
cristycf12ee42012-06-14 11:02:23 +00002806 ConvertsRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
cristy3ed852e2009-09-05 21:47:34 +00002807 hue+=0.5*(0.01*percent_hue-1.0);
2808 while (hue < 0.0)
2809 hue+=1.0;
2810 while (hue > 1.0)
2811 hue-=1.0;
2812 saturation*=0.01*percent_saturation;
2813 lightness*=0.01*percent_lightness;
cristycf12ee42012-06-14 11:02:23 +00002814 ConvertHSLTosRGB(hue,saturation,lightness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002815}
2816
cristy3094b7f2011-10-01 23:18:02 +00002817static 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 +00002818{
2819 double
2820 blackness,
2821 hue,
2822 whiteness;
2823
2824 /*
2825 Increase or decrease color blackness, whiteness, or hue.
2826 */
cristy3094b7f2011-10-01 23:18:02 +00002827 assert(red != (double *) NULL);
2828 assert(green != (double *) NULL);
2829 assert(blue != (double *) NULL);
cristycf12ee42012-06-14 11:02:23 +00002830 ConvertsRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
cristy3ed852e2009-09-05 21:47:34 +00002831 hue+=0.5*(0.01*percent_hue-1.0);
2832 while (hue < 0.0)
2833 hue+=1.0;
2834 while (hue > 1.0)
2835 hue-=1.0;
2836 blackness*=0.01*percent_blackness;
2837 whiteness*=0.01*percent_whiteness;
cristycf12ee42012-06-14 11:02:23 +00002838 ConvertHWBTosRGB(hue,whiteness,blackness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002839}
2840
cristy33bd5152011-08-24 01:42:24 +00002841MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2842 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002843{
2844#define ModulateImageTag "Modulate/Image"
2845
cristyc4c8d132010-01-07 01:58:38 +00002846 CacheView
2847 *image_view;
2848
cristy3ed852e2009-09-05 21:47:34 +00002849 ColorspaceType
2850 colorspace;
2851
2852 const char
2853 *artifact;
2854
2855 double
2856 percent_brightness,
2857 percent_hue,
2858 percent_saturation;
2859
cristy3ed852e2009-09-05 21:47:34 +00002860 GeometryInfo
2861 geometry_info;
2862
cristy3ed852e2009-09-05 21:47:34 +00002863 MagickBooleanType
2864 status;
2865
cristybb503372010-05-27 20:51:26 +00002866 MagickOffsetType
2867 progress;
2868
cristy3ed852e2009-09-05 21:47:34 +00002869 MagickStatusType
2870 flags;
2871
cristybb503372010-05-27 20:51:26 +00002872 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002873 i;
2874
cristybb503372010-05-27 20:51:26 +00002875 ssize_t
2876 y;
2877
cristy3ed852e2009-09-05 21:47:34 +00002878 /*
cristy2b726bd2010-01-11 01:05:39 +00002879 Initialize modulate table.
cristy3ed852e2009-09-05 21:47:34 +00002880 */
2881 assert(image != (Image *) NULL);
2882 assert(image->signature == MagickSignature);
2883 if (image->debug != MagickFalse)
2884 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2885 if (modulate == (char *) NULL)
2886 return(MagickFalse);
cristybc7fdcd2012-06-27 01:43:10 +00002887 if (IssRGBColorspace(image->colorspace) == MagickFalse)
2888 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002889 flags=ParseGeometry(modulate,&geometry_info);
2890 percent_brightness=geometry_info.rho;
2891 percent_saturation=geometry_info.sigma;
2892 if ((flags & SigmaValue) == 0)
2893 percent_saturation=100.0;
2894 percent_hue=geometry_info.xi;
2895 if ((flags & XiValue) == 0)
2896 percent_hue=100.0;
2897 colorspace=UndefinedColorspace;
2898 artifact=GetImageArtifact(image,"modulate:colorspace");
2899 if (artifact != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00002900 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +00002901 MagickFalse,artifact);
2902 if (image->storage_class == PseudoClass)
2903 {
2904 /*
2905 Modulate colormap.
2906 */
cristyb5d5f722009-11-04 03:03:49 +00002907#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002908 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002909 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002910#endif
cristybb503372010-05-27 20:51:26 +00002911 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002912 switch (colorspace)
2913 {
2914 case HSBColorspace:
2915 {
2916 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2917 &image->colormap[i].red,&image->colormap[i].green,
2918 &image->colormap[i].blue);
2919 break;
2920 }
2921 case HSLColorspace:
2922 default:
2923 {
2924 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2925 &image->colormap[i].red,&image->colormap[i].green,
2926 &image->colormap[i].blue);
2927 break;
2928 }
2929 case HWBColorspace:
2930 {
2931 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2932 &image->colormap[i].red,&image->colormap[i].green,
2933 &image->colormap[i].blue);
2934 break;
2935 }
2936 }
2937 }
2938 /*
2939 Modulate image.
2940 */
2941 status=MagickTrue;
2942 progress=0;
cristydb070952012-04-20 14:33:00 +00002943 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002944#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002945 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002946 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002947#endif
cristybb503372010-05-27 20:51:26 +00002948 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002949 {
cristy3094b7f2011-10-01 23:18:02 +00002950 double
cristy5afeab82011-04-30 01:30:09 +00002951 blue,
2952 green,
2953 red;
2954
cristy4c08aed2011-07-01 19:47:50 +00002955 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002956 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002957
cristy8d4629b2010-08-30 17:59:46 +00002958 register ssize_t
2959 x;
2960
cristy3ed852e2009-09-05 21:47:34 +00002961 if (status == MagickFalse)
2962 continue;
2963 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002964 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002965 {
2966 status=MagickFalse;
2967 continue;
2968 }
cristybb503372010-05-27 20:51:26 +00002969 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002970 {
cristyda1f9c12011-10-02 21:39:49 +00002971 red=(double) GetPixelRed(image,q);
2972 green=(double) GetPixelGreen(image,q);
2973 blue=(double) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002974 switch (colorspace)
2975 {
2976 case HSBColorspace:
2977 {
2978 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002979 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002980 break;
2981 }
2982 case HSLColorspace:
2983 default:
2984 {
2985 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002986 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002987 break;
2988 }
2989 case HWBColorspace:
2990 {
2991 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002992 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002993 break;
2994 }
2995 }
cristy3094b7f2011-10-01 23:18:02 +00002996 SetPixelRed(image,ClampToQuantum(red),q);
2997 SetPixelGreen(image,ClampToQuantum(green),q);
2998 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +00002999 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003000 }
3001 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3002 status=MagickFalse;
3003 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3004 {
3005 MagickBooleanType
3006 proceed;
3007
cristyb5d5f722009-11-04 03:03:49 +00003008#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003009 #pragma omp critical (MagickCore_ModulateImage)
cristy3ed852e2009-09-05 21:47:34 +00003010#endif
3011 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3012 if (proceed == MagickFalse)
3013 status=MagickFalse;
3014 }
3015 }
3016 image_view=DestroyCacheView(image_view);
3017 return(status);
3018}
3019
3020/*
3021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3022% %
3023% %
3024% %
3025% N e g a t e I m a g e %
3026% %
3027% %
3028% %
3029%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3030%
3031% NegateImage() negates the colors in the reference image. The grayscale
3032% option means that only grayscale values within the image are negated.
3033%
cristy50fbc382011-07-07 02:19:17 +00003034% The format of the NegateImage method is:
cristy3ed852e2009-09-05 21:47:34 +00003035%
3036% MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003037% const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003038%
3039% A description of each parameter follows:
3040%
3041% o image: the image.
3042%
cristy3ed852e2009-09-05 21:47:34 +00003043% o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3044%
cristyb3e7c6c2011-07-24 01:43:55 +00003045% o exception: return any errors or warnings in this structure.
3046%
cristy3ed852e2009-09-05 21:47:34 +00003047*/
cristy3ed852e2009-09-05 21:47:34 +00003048MagickExport MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003049 const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003050{
cristy3ed852e2009-09-05 21:47:34 +00003051#define NegateImageTag "Negate/Image"
3052
cristyc4c8d132010-01-07 01:58:38 +00003053 CacheView
3054 *image_view;
3055
cristy3ed852e2009-09-05 21:47:34 +00003056 MagickBooleanType
3057 status;
3058
cristybb503372010-05-27 20:51:26 +00003059 MagickOffsetType
3060 progress;
3061
3062 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003063 i;
3064
cristybb503372010-05-27 20:51:26 +00003065 ssize_t
3066 y;
3067
cristy3ed852e2009-09-05 21:47:34 +00003068 assert(image != (Image *) NULL);
3069 assert(image->signature == MagickSignature);
3070 if (image->debug != MagickFalse)
3071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3072 if (image->storage_class == PseudoClass)
3073 {
3074 /*
3075 Negate colormap.
3076 */
cristyb5d5f722009-11-04 03:03:49 +00003077#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003078 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003079 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003080#endif
cristybb503372010-05-27 20:51:26 +00003081 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003082 {
3083 if (grayscale != MagickFalse)
3084 if ((image->colormap[i].red != image->colormap[i].green) ||
3085 (image->colormap[i].green != image->colormap[i].blue))
3086 continue;
cristyed231572011-07-14 02:18:59 +00003087 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003088 image->colormap[i].red=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003089 image->colormap[i].red;
cristyed231572011-07-14 02:18:59 +00003090 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003091 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003092 image->colormap[i].green;
cristyed231572011-07-14 02:18:59 +00003093 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003094 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003095 image->colormap[i].blue;
3096 }
3097 }
3098 /*
3099 Negate image.
3100 */
3101 status=MagickTrue;
3102 progress=0;
cristydb070952012-04-20 14:33:00 +00003103 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003104 if (grayscale != MagickFalse)
3105 {
cristyb5d5f722009-11-04 03:03:49 +00003106#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003107 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003108 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003109#endif
cristybb503372010-05-27 20:51:26 +00003110 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003111 {
3112 MagickBooleanType
3113 sync;
3114
cristy4c08aed2011-07-01 19:47:50 +00003115 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003116 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003117
cristy8d4629b2010-08-30 17:59:46 +00003118 register ssize_t
3119 x;
3120
cristy3ed852e2009-09-05 21:47:34 +00003121 if (status == MagickFalse)
3122 continue;
3123 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3124 exception);
cristyacd2ed22011-08-30 01:44:23 +00003125 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003126 {
3127 status=MagickFalse;
3128 continue;
3129 }
cristybb503372010-05-27 20:51:26 +00003130 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003131 {
cristy9aa95be2011-07-20 21:56:45 +00003132 register ssize_t
3133 i;
3134
cristy10a6c612012-01-29 21:41:05 +00003135 if ((GetPixelMask(image,q) != 0) ||
3136 (IsPixelGray(image,q) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003137 {
cristya30d9ba2011-07-23 21:00:48 +00003138 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003139 continue;
3140 }
cristya30d9ba2011-07-23 21:00:48 +00003141 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy9aa95be2011-07-20 21:56:45 +00003142 {
cristyabace412011-12-11 15:56:53 +00003143 PixelChannel
3144 channel;
3145
cristy1aaa3cd2011-08-21 23:48:17 +00003146 PixelTrait
cristy9aa95be2011-07-20 21:56:45 +00003147 traits;
3148
cristyabace412011-12-11 15:56:53 +00003149 channel=GetPixelChannelMapChannel(image,i);
3150 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003151 if ((traits & UpdatePixelTrait) == 0)
3152 continue;
3153 q[i]=QuantumRange-q[i];
cristy9aa95be2011-07-20 21:56:45 +00003154 }
cristya30d9ba2011-07-23 21:00:48 +00003155 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003156 }
3157 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3158 if (sync == MagickFalse)
3159 status=MagickFalse;
3160 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3161 {
3162 MagickBooleanType
3163 proceed;
3164
cristyb5d5f722009-11-04 03:03:49 +00003165#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003166 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003167#endif
3168 proceed=SetImageProgress(image,NegateImageTag,progress++,
3169 image->rows);
3170 if (proceed == MagickFalse)
3171 status=MagickFalse;
3172 }
3173 }
3174 image_view=DestroyCacheView(image_view);
3175 return(MagickTrue);
3176 }
3177 /*
3178 Negate image.
3179 */
cristyb5d5f722009-11-04 03:03:49 +00003180#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003181 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003182 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003183#endif
cristybb503372010-05-27 20:51:26 +00003184 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003185 {
cristy4c08aed2011-07-01 19:47:50 +00003186 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003187 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003188
cristy8d4629b2010-08-30 17:59:46 +00003189 register ssize_t
3190 x;
3191
cristy3ed852e2009-09-05 21:47:34 +00003192 if (status == MagickFalse)
3193 continue;
3194 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003195 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003196 {
3197 status=MagickFalse;
3198 continue;
3199 }
cristybb503372010-05-27 20:51:26 +00003200 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003201 {
cristyf7dc44c2011-07-20 14:41:15 +00003202 register ssize_t
3203 i;
3204
cristyd09f8802012-02-04 16:44:10 +00003205 if (GetPixelMask(image,q) != 0)
3206 {
3207 q+=GetPixelChannels(image);
3208 continue;
3209 }
cristya30d9ba2011-07-23 21:00:48 +00003210 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyf7dc44c2011-07-20 14:41:15 +00003211 {
cristyabace412011-12-11 15:56:53 +00003212 PixelChannel
3213 channel;
3214
cristy1aaa3cd2011-08-21 23:48:17 +00003215 PixelTrait
cristyf7dc44c2011-07-20 14:41:15 +00003216 traits;
3217
cristyabace412011-12-11 15:56:53 +00003218 channel=GetPixelChannelMapChannel(image,i);
3219 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003220 if ((traits & UpdatePixelTrait) == 0)
cristyec9e3a62012-02-01 02:09:32 +00003221 continue;
3222 q[i]=QuantumRange-q[i];
cristyf7dc44c2011-07-20 14:41:15 +00003223 }
cristya30d9ba2011-07-23 21:00:48 +00003224 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003225 }
3226 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3227 status=MagickFalse;
3228 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3229 {
3230 MagickBooleanType
3231 proceed;
3232
cristyb5d5f722009-11-04 03:03:49 +00003233#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003234 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003235#endif
3236 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3237 if (proceed == MagickFalse)
3238 status=MagickFalse;
3239 }
3240 }
3241 image_view=DestroyCacheView(image_view);
3242 return(status);
3243}
3244
3245/*
3246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3247% %
3248% %
3249% %
3250% N o r m a l i z e I m a g e %
3251% %
3252% %
3253% %
3254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3255%
anthony207ce472012-04-04 06:21:26 +00003256% The NormalizeImage() method enhances the contrast of a color image by
3257% mapping the darkest 2 percent of all pixel to black and the brightest
3258% 1 percent to white.
cristy3ed852e2009-09-05 21:47:34 +00003259%
3260% The format of the NormalizeImage method is:
3261%
cristye23ec9d2011-08-16 18:15:40 +00003262% MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003263%
3264% A description of each parameter follows:
3265%
3266% o image: the image.
3267%
cristye23ec9d2011-08-16 18:15:40 +00003268% o exception: return any errors or warnings in this structure.
3269%
cristy3ed852e2009-09-05 21:47:34 +00003270*/
cristye23ec9d2011-08-16 18:15:40 +00003271MagickExport MagickBooleanType NormalizeImage(Image *image,
3272 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003273{
cristy3ed852e2009-09-05 21:47:34 +00003274 double
3275 black_point,
3276 white_point;
3277
cristy530239c2010-07-25 17:34:26 +00003278 black_point=(double) image->columns*image->rows*0.0015;
3279 white_point=(double) image->columns*image->rows*0.9995;
cristye23ec9d2011-08-16 18:15:40 +00003280 return(ContrastStretchImage(image,black_point,white_point,exception));
cristy3ed852e2009-09-05 21:47:34 +00003281}
3282
3283/*
3284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3285% %
3286% %
3287% %
3288% S i g m o i d a l C o n t r a s t I m a g e %
3289% %
3290% %
3291% %
3292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3293%
3294% SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3295% sigmoidal contrast algorithm. Increase the contrast of the image using a
3296% sigmoidal transfer function without saturating highlights or shadows.
3297% Contrast indicates how much to increase the contrast (0 is none; 3 is
anthony207ce472012-04-04 06:21:26 +00003298% typical; 20 is pushing it); mid-point indicates where midtones fall in the
3299% resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3300% sharpen to MagickTrue to increase the image contrast otherwise the contrast
3301% is reduced.
cristy3ed852e2009-09-05 21:47:34 +00003302%
3303% The format of the SigmoidalContrastImage method is:
3304%
3305% MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003306% const MagickBooleanType sharpen,const char *levels,
3307% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003308%
3309% A description of each parameter follows:
3310%
3311% o image: the image.
3312%
cristy3ed852e2009-09-05 21:47:34 +00003313% o sharpen: Increase or decrease image contrast.
3314%
cristyfa769582010-09-30 23:30:03 +00003315% o alpha: strength of the contrast, the larger the number the more
3316% 'threshold-like' it becomes.
cristy3ed852e2009-09-05 21:47:34 +00003317%
cristyfa769582010-09-30 23:30:03 +00003318% o beta: midpoint of the function as a color value 0 to QuantumRange.
cristy3ed852e2009-09-05 21:47:34 +00003319%
cristy33bd5152011-08-24 01:42:24 +00003320% o exception: return any errors or warnings in this structure.
3321%
cristy3ed852e2009-09-05 21:47:34 +00003322*/
cristy3ed852e2009-09-05 21:47:34 +00003323MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003324 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3325 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003326{
3327#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3328
cristyc4c8d132010-01-07 01:58:38 +00003329 CacheView
3330 *image_view;
3331
cristy3ed852e2009-09-05 21:47:34 +00003332 MagickBooleanType
3333 status;
3334
cristybb503372010-05-27 20:51:26 +00003335 MagickOffsetType
3336 progress;
3337
cristy3ed852e2009-09-05 21:47:34 +00003338 MagickRealType
3339 *sigmoidal_map;
3340
cristybb503372010-05-27 20:51:26 +00003341 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003342 i;
3343
cristybb503372010-05-27 20:51:26 +00003344 ssize_t
3345 y;
3346
cristy3ed852e2009-09-05 21:47:34 +00003347 /*
3348 Allocate and initialize sigmoidal maps.
3349 */
3350 assert(image != (Image *) NULL);
3351 assert(image->signature == MagickSignature);
3352 if (image->debug != MagickFalse)
3353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3354 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3355 sizeof(*sigmoidal_map));
3356 if (sigmoidal_map == (MagickRealType *) NULL)
3357 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3358 image->filename);
3359 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
cristyb5d5f722009-11-04 03:03:49 +00003360#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003361 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003362 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003363#endif
cristybb503372010-05-27 20:51:26 +00003364 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003365 {
3366 if (sharpen != MagickFalse)
3367 {
anthony207ce472012-04-04 06:21:26 +00003368#define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3369#if 1
3370 /* Simpilified function scaling,
3371 * with better 'contrast=0' or 'flatline' handling (greyscale)
3372 */
3373 double
3374 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3375 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3376 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3377 (MagickRealType)(MaxMap*(
3378 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3379 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3380#else
3381 /* Scaled sigmoidal formula...
3382 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3383 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
cristy3ed852e2009-09-05 21:47:34 +00003384 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3385 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
anthony207ce472012-04-04 06:21:26 +00003386 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3387 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3388 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3389 (double) QuantumRange)))))+0.5));
3390#endif
cristy3ed852e2009-09-05 21:47:34 +00003391 continue;
3392 }
anthony207ce472012-04-04 06:21:26 +00003393#if 1
3394 {
3395 /* Inverse -- See
3396 http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
3397 */
3398 double
3399 min = sigmoidal(contrast,1.0,0.0),
3400 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3401 xi = min+(double)i/MaxMap*(max-min);
3402 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3403 (MagickRealType)(MaxMap*(
3404 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3405 }
3406#else
3407 /* expanded form of the above */
cristy3ed852e2009-09-05 21:47:34 +00003408 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
anthony207ce472012-04-04 06:21:26 +00003409 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3410 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3411 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3412 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3413 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3414 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3415 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3416 (double) QuantumRange*contrast))))))/contrast)));
3417#endif
cristy3ed852e2009-09-05 21:47:34 +00003418 }
3419 if (image->storage_class == PseudoClass)
3420 {
3421 /*
3422 Sigmoidal-contrast enhance colormap.
3423 */
cristyb5d5f722009-11-04 03:03:49 +00003424#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003425 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003426 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003427#endif
cristybb503372010-05-27 20:51:26 +00003428 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003429 {
cristyed231572011-07-14 02:18:59 +00003430 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003431 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3432 ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00003433 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003434 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3435 ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00003436 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003437 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3438 ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00003439 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003440 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3441 ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00003442 }
3443 }
3444 /*
3445 Sigmoidal-contrast enhance image.
3446 */
3447 status=MagickTrue;
3448 progress=0;
cristydb070952012-04-20 14:33:00 +00003449 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003450#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003451 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003452 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003453#endif
cristybb503372010-05-27 20:51:26 +00003454 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003455 {
cristy4c08aed2011-07-01 19:47:50 +00003456 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003457 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003458
cristy8d4629b2010-08-30 17:59:46 +00003459 register ssize_t
3460 x;
3461
cristy3ed852e2009-09-05 21:47:34 +00003462 if (status == MagickFalse)
3463 continue;
3464 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003465 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003466 {
3467 status=MagickFalse;
3468 continue;
3469 }
cristybb503372010-05-27 20:51:26 +00003470 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003471 {
cristy33bd5152011-08-24 01:42:24 +00003472 register ssize_t
3473 i;
3474
cristy10a6c612012-01-29 21:41:05 +00003475 if (GetPixelMask(image,q) != 0)
3476 {
3477 q+=GetPixelChannels(image);
3478 continue;
3479 }
cristy33bd5152011-08-24 01:42:24 +00003480 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3481 {
cristyabace412011-12-11 15:56:53 +00003482 PixelChannel
3483 channel;
3484
cristy33bd5152011-08-24 01:42:24 +00003485 PixelTrait
3486 traits;
3487
cristyabace412011-12-11 15:56:53 +00003488 channel=GetPixelChannelMapChannel(image,i);
3489 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003490 if ((traits & UpdatePixelTrait) == 0)
3491 continue;
3492 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
cristy33bd5152011-08-24 01:42:24 +00003493 }
cristyed231572011-07-14 02:18:59 +00003494 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003495 }
3496 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3497 status=MagickFalse;
3498 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3499 {
3500 MagickBooleanType
3501 proceed;
3502
cristyb5d5f722009-11-04 03:03:49 +00003503#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003504 #pragma omp critical (MagickCore_SigmoidalContrastImage)
cristy3ed852e2009-09-05 21:47:34 +00003505#endif
3506 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3507 image->rows);
3508 if (proceed == MagickFalse)
3509 status=MagickFalse;
3510 }
3511 }
3512 image_view=DestroyCacheView(image_view);
3513 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);
3514 return(status);
3515}