blob: 34026ddb219162833c3d57fe29f9043bc6fbd5b6 [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)
cristyb09db112012-07-11 12:04:31 +0000333 (void) TransformImageColorspace(image,RGBColorspace,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
cristy3559f502012-06-27 13:52:20 +0000738 luma=0.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
739 0.07217*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 {
cristy3559f502012-06-27 13:52:20 +0000782 luma=0.21267*GetPixelRed(image,q)+0.71526*GetPixelGreen(image,q)+0.07217*
cristy4c08aed2011-07-01 19:47:50 +0000783 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;
cristy0a39a5c2012-06-27 12:51:45 +0000859 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
cristy31ac0f02011-03-12 02:04:47 +0000860 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
cristy4205a3c2010-09-12 20:19:59 +0000861 brightness);
cristy3ed852e2009-09-05 21:47:34 +0000862 if (brightness > 1.0)
863 brightness=1.0;
864 else
865 if (brightness < 0.0)
866 brightness=0.0;
cristy0a39a5c2012-06-27 12:51:45 +0000867 ConvertHSBToRGB(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)
cristyb09db112012-07-11 12:04:31 +00002025 (void) TransformImageColorspace(image,RGBColorspace,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"
cristy8cd03c32012-07-07 18:57:59 +00002363#define LevelizeValue(x) (ClampToQuantum((pow((double) (QuantumScale*(x)), \
2364 1.0/gamma))*(white_point-black_point)+black_point))
cristy3ed852e2009-09-05 21:47:34 +00002365
cristyc4c8d132010-01-07 01:58:38 +00002366 CacheView
2367 *image_view;
2368
cristy3ed852e2009-09-05 21:47:34 +00002369 MagickBooleanType
2370 status;
2371
cristybb503372010-05-27 20:51:26 +00002372 MagickOffsetType
2373 progress;
2374
2375 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002376 i;
2377
cristybb503372010-05-27 20:51:26 +00002378 ssize_t
2379 y;
2380
cristy3ed852e2009-09-05 21:47:34 +00002381 /*
2382 Allocate and initialize levels map.
2383 */
2384 assert(image != (Image *) NULL);
2385 assert(image->signature == MagickSignature);
2386 if (image->debug != MagickFalse)
2387 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy25ee24d2012-05-18 12:25:59 +00002388 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00002389 (void) SetImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002390 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002391#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002392 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002393 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002394#endif
cristybb503372010-05-27 20:51:26 +00002395 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002396 {
2397 /*
2398 Level colormap.
2399 */
cristyed231572011-07-14 02:18:59 +00002400 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002401 image->colormap[i].red=(double) LevelizeValue(
2402 image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002403 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002404 image->colormap[i].green=(double) LevelizeValue(
2405 image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002406 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002407 image->colormap[i].blue=(double) LevelizeValue(
2408 image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002409 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002410 image->colormap[i].alpha=(double) LevelizeValue(
2411 image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002412 }
2413 /*
2414 Level image.
2415 */
2416 status=MagickTrue;
2417 progress=0;
cristydb070952012-04-20 14:33:00 +00002418 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002419#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002420 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002421 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002422#endif
cristybb503372010-05-27 20:51:26 +00002423 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002424 {
cristy4c08aed2011-07-01 19:47:50 +00002425 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002426 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002427
cristy8d4629b2010-08-30 17:59:46 +00002428 register ssize_t
2429 x;
2430
cristy3ed852e2009-09-05 21:47:34 +00002431 if (status == MagickFalse)
2432 continue;
2433 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002434 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002435 {
2436 status=MagickFalse;
2437 continue;
2438 }
cristybb503372010-05-27 20:51:26 +00002439 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002440 {
cristy7c0a0a42011-08-23 17:57:25 +00002441 register ssize_t
2442 i;
2443
cristy10a6c612012-01-29 21:41:05 +00002444 if (GetPixelMask(image,q) != 0)
2445 {
2446 q+=GetPixelChannels(image);
2447 continue;
2448 }
cristy7c0a0a42011-08-23 17:57:25 +00002449 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2450 {
cristyabace412011-12-11 15:56:53 +00002451 PixelChannel
2452 channel;
2453
cristy7c0a0a42011-08-23 17:57:25 +00002454 PixelTrait
2455 traits;
2456
cristyabace412011-12-11 15:56:53 +00002457 channel=GetPixelChannelMapChannel(image,i);
2458 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002459 if ((traits & UpdatePixelTrait) == 0)
cristyb8b0d162011-12-18 23:41:28 +00002460 continue;
cristy780e9ef2011-12-18 23:33:50 +00002461 q[i]=LevelizeValue(q[i]);
cristy7c0a0a42011-08-23 17:57:25 +00002462 }
cristyed231572011-07-14 02:18:59 +00002463 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002464 }
2465 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2466 status=MagickFalse;
2467 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2468 {
2469 MagickBooleanType
2470 proceed;
2471
cristyb5d5f722009-11-04 03:03:49 +00002472#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002473 #pragma omp critical (MagickCore_LevelizeImage)
cristy3ed852e2009-09-05 21:47:34 +00002474#endif
2475 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2476 if (proceed == MagickFalse)
2477 status=MagickFalse;
2478 }
2479 }
cristy8d4629b2010-08-30 17:59:46 +00002480 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002481 return(status);
2482}
2483
2484/*
2485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2486% %
2487% %
2488% %
2489% L e v e l I m a g e C o l o r s %
2490% %
2491% %
2492% %
2493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2494%
cristy490408a2011-07-07 14:42:05 +00002495% LevelImageColors() maps the given color to "black" and "white" values,
cristyee0f8d72009-09-19 00:58:29 +00002496% linearly spreading out the colors, and level values on a channel by channel
2497% bases, as per LevelImage(). The given colors allows you to specify
glennrp1e7f7bc2011-03-02 19:25:28 +00002498% different level ranges for each of the color channels separately.
cristy3ed852e2009-09-05 21:47:34 +00002499%
2500% If the boolean 'invert' is set true the image values will modifyed in the
2501% reverse direction. That is any existing "black" and "white" colors in the
2502% image will become the color values given, with all other values compressed
2503% appropriatally. This effectivally maps a greyscale gradient into the given
2504% color gradient.
2505%
cristy490408a2011-07-07 14:42:05 +00002506% The format of the LevelImageColors method is:
cristy3ed852e2009-09-05 21:47:34 +00002507%
cristy490408a2011-07-07 14:42:05 +00002508% MagickBooleanType LevelImageColors(Image *image,
2509% const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002510% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002511%
2512% A description of each parameter follows:
2513%
2514% o image: the image.
2515%
cristy3ed852e2009-09-05 21:47:34 +00002516% o black_color: The color to map black to/from
2517%
2518% o white_point: The color to map white to/from
2519%
2520% o invert: if true map the colors (levelize), rather than from (level)
2521%
cristy7c0a0a42011-08-23 17:57:25 +00002522% o exception: return any errors or warnings in this structure.
2523%
cristy3ed852e2009-09-05 21:47:34 +00002524*/
cristy490408a2011-07-07 14:42:05 +00002525MagickExport MagickBooleanType LevelImageColors(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00002526 const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002527 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002528{
cristybd5a96c2011-08-21 00:04:26 +00002529 ChannelType
2530 channel_mask;
2531
cristy3ed852e2009-09-05 21:47:34 +00002532 MagickStatusType
2533 status;
2534
2535 /*
2536 Allocate and initialize levels map.
2537 */
2538 assert(image != (Image *) NULL);
2539 assert(image->signature == MagickSignature);
2540 if (image->debug != MagickFalse)
2541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2542 status=MagickFalse;
2543 if (invert == MagickFalse)
2544 {
cristyed231572011-07-14 02:18:59 +00002545 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002546 {
cristybd5a96c2011-08-21 00:04:26 +00002547 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy01e9afd2011-08-10 17:38:41 +00002548 status|=LevelImage(image,black_color->red,white_color->red,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002549 exception);
cristybd5a96c2011-08-21 00:04:26 +00002550 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002551 }
cristyed231572011-07-14 02:18:59 +00002552 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002553 {
cristybd5a96c2011-08-21 00:04:26 +00002554 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy01e9afd2011-08-10 17:38:41 +00002555 status|=LevelImage(image,black_color->green,white_color->green,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002556 exception);
cristybd5a96c2011-08-21 00:04:26 +00002557 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002558 }
cristyed231572011-07-14 02:18:59 +00002559 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002560 {
cristybd5a96c2011-08-21 00:04:26 +00002561 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy01e9afd2011-08-10 17:38:41 +00002562 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002563 exception);
cristybd5a96c2011-08-21 00:04:26 +00002564 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002565 }
cristyed231572011-07-14 02:18:59 +00002566 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002567 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002568 {
cristybd5a96c2011-08-21 00:04:26 +00002569 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy01e9afd2011-08-10 17:38:41 +00002570 status|=LevelImage(image,black_color->black,white_color->black,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002571 exception);
cristybd5a96c2011-08-21 00:04:26 +00002572 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002573 }
cristyed231572011-07-14 02:18:59 +00002574 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002575 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002576 {
cristybd5a96c2011-08-21 00:04:26 +00002577 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy01e9afd2011-08-10 17:38:41 +00002578 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002579 exception);
cristybd5a96c2011-08-21 00:04:26 +00002580 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002581 }
cristy3ed852e2009-09-05 21:47:34 +00002582 }
2583 else
2584 {
cristyed231572011-07-14 02:18:59 +00002585 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002586 {
cristybd5a96c2011-08-21 00:04:26 +00002587 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002588 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2589 exception);
cristybd5a96c2011-08-21 00:04:26 +00002590 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002591 }
cristyed231572011-07-14 02:18:59 +00002592 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002593 {
cristybd5a96c2011-08-21 00:04:26 +00002594 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002595 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2596 exception);
cristybd5a96c2011-08-21 00:04:26 +00002597 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002598 }
cristyed231572011-07-14 02:18:59 +00002599 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002600 {
cristybd5a96c2011-08-21 00:04:26 +00002601 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002602 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2603 exception);
cristybd5a96c2011-08-21 00:04:26 +00002604 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002605 }
cristyed231572011-07-14 02:18:59 +00002606 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002607 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002608 {
cristybd5a96c2011-08-21 00:04:26 +00002609 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002610 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2611 exception);
cristybd5a96c2011-08-21 00:04:26 +00002612 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002613 }
cristyed231572011-07-14 02:18:59 +00002614 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002615 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002616 {
cristybd5a96c2011-08-21 00:04:26 +00002617 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002618 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2619 exception);
cristybd5a96c2011-08-21 00:04:26 +00002620 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002621 }
cristy3ed852e2009-09-05 21:47:34 +00002622 }
2623 return(status == 0 ? MagickFalse : MagickTrue);
2624}
2625
2626/*
2627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2628% %
2629% %
2630% %
2631% L i n e a r S t r e t c h I m a g e %
2632% %
2633% %
2634% %
2635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2636%
cristyf1611782011-08-01 15:39:13 +00002637% LinearStretchImage() discards any pixels below the black point and above
2638% the white point and levels the remaining pixels.
cristy3ed852e2009-09-05 21:47:34 +00002639%
2640% The format of the LinearStretchImage method is:
2641%
2642% MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002643% const double black_point,const double white_point,
2644% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002645%
2646% A description of each parameter follows:
2647%
2648% o image: the image.
2649%
2650% o black_point: the black point.
2651%
2652% o white_point: the white point.
2653%
cristy33bd5152011-08-24 01:42:24 +00002654% o exception: return any errors or warnings in this structure.
2655%
cristy3ed852e2009-09-05 21:47:34 +00002656*/
2657MagickExport MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002658 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002659{
2660#define LinearStretchImageTag "LinearStretch/Image"
2661
cristy33bd5152011-08-24 01:42:24 +00002662 CacheView
2663 *image_view;
cristy3ed852e2009-09-05 21:47:34 +00002664
cristy3ed852e2009-09-05 21:47:34 +00002665 MagickBooleanType
2666 status;
2667
2668 MagickRealType
2669 *histogram,
2670 intensity;
2671
cristy8d4629b2010-08-30 17:59:46 +00002672 ssize_t
2673 black,
2674 white,
2675 y;
2676
cristy3ed852e2009-09-05 21:47:34 +00002677 /*
2678 Allocate histogram and linear map.
2679 */
2680 assert(image != (Image *) NULL);
2681 assert(image->signature == MagickSignature);
2682 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2683 sizeof(*histogram));
2684 if (histogram == (MagickRealType *) NULL)
2685 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2686 image->filename);
2687 /*
2688 Form histogram.
2689 */
2690 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
cristydb070952012-04-20 14:33:00 +00002691 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00002692 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002693 {
cristy4c08aed2011-07-01 19:47:50 +00002694 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002695 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002696
cristybb503372010-05-27 20:51:26 +00002697 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002698 x;
2699
cristy33bd5152011-08-24 01:42:24 +00002700 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002701 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002702 break;
cristy33bd5152011-08-24 01:42:24 +00002703 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002704 {
cristy4c08aed2011-07-01 19:47:50 +00002705 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
cristyed231572011-07-14 02:18:59 +00002706 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002707 }
2708 }
cristy33bd5152011-08-24 01:42:24 +00002709 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002710 /*
2711 Find the histogram boundaries by locating the black and white point levels.
2712 */
cristy3ed852e2009-09-05 21:47:34 +00002713 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002714 for (black=0; black < (ssize_t) MaxMap; black++)
cristy3ed852e2009-09-05 21:47:34 +00002715 {
2716 intensity+=histogram[black];
2717 if (intensity >= black_point)
2718 break;
2719 }
2720 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002721 for (white=(ssize_t) MaxMap; white != 0; white--)
cristy3ed852e2009-09-05 21:47:34 +00002722 {
2723 intensity+=histogram[white];
2724 if (intensity >= white_point)
2725 break;
2726 }
2727 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
cristyc82a27b2011-10-21 01:07:16 +00002728 status=LevelImage(image,(double) black,(double) white,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00002729 return(status);
2730}
2731
2732/*
2733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2734% %
2735% %
2736% %
2737% M o d u l a t e I m a g e %
2738% %
2739% %
2740% %
2741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2742%
2743% ModulateImage() lets you control the brightness, saturation, and hue
2744% of an image. Modulate represents the brightness, saturation, and hue
2745% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2746% modulation is lightness, saturation, and hue. And if the colorspace is
2747% HWB, use blackness, whiteness, and hue.
2748%
2749% The format of the ModulateImage method is:
2750%
cristy33bd5152011-08-24 01:42:24 +00002751% MagickBooleanType ModulateImage(Image *image,const char *modulate,
2752% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002753%
2754% A description of each parameter follows:
2755%
2756% o image: the image.
2757%
cristy33bd5152011-08-24 01:42:24 +00002758% o modulate: Define the percent change in brightness, saturation, and hue.
2759%
2760% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002761%
2762*/
2763
2764static void ModulateHSB(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002765 const double percent_saturation,const double percent_brightness,double *red,
2766 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002767{
2768 double
2769 brightness,
2770 hue,
2771 saturation;
2772
2773 /*
2774 Increase or decrease color brightness, saturation, or hue.
2775 */
cristy3094b7f2011-10-01 23:18:02 +00002776 assert(red != (double *) NULL);
2777 assert(green != (double *) NULL);
2778 assert(blue != (double *) NULL);
cristy0a39a5c2012-06-27 12:51:45 +00002779 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
cristy3ed852e2009-09-05 21:47:34 +00002780 hue+=0.5*(0.01*percent_hue-1.0);
2781 while (hue < 0.0)
2782 hue+=1.0;
2783 while (hue > 1.0)
2784 hue-=1.0;
2785 saturation*=0.01*percent_saturation;
2786 brightness*=0.01*percent_brightness;
cristy0a39a5c2012-06-27 12:51:45 +00002787 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002788}
2789
2790static void ModulateHSL(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002791 const double percent_saturation,const double percent_lightness,double *red,
2792 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002793{
2794 double
2795 hue,
2796 lightness,
2797 saturation;
2798
2799 /*
2800 Increase or decrease color lightness, saturation, or hue.
2801 */
cristy3094b7f2011-10-01 23:18:02 +00002802 assert(red != (double *) NULL);
2803 assert(green != (double *) NULL);
2804 assert(blue != (double *) NULL);
cristy0a39a5c2012-06-27 12:51:45 +00002805 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
cristy3ed852e2009-09-05 21:47:34 +00002806 hue+=0.5*(0.01*percent_hue-1.0);
2807 while (hue < 0.0)
2808 hue+=1.0;
2809 while (hue > 1.0)
2810 hue-=1.0;
2811 saturation*=0.01*percent_saturation;
2812 lightness*=0.01*percent_lightness;
cristy0a39a5c2012-06-27 12:51:45 +00002813 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002814}
2815
cristy3094b7f2011-10-01 23:18:02 +00002816static 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 +00002817{
2818 double
2819 blackness,
2820 hue,
2821 whiteness;
2822
2823 /*
2824 Increase or decrease color blackness, whiteness, or hue.
2825 */
cristy3094b7f2011-10-01 23:18:02 +00002826 assert(red != (double *) NULL);
2827 assert(green != (double *) NULL);
2828 assert(blue != (double *) NULL);
cristy0a39a5c2012-06-27 12:51:45 +00002829 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
cristy3ed852e2009-09-05 21:47:34 +00002830 hue+=0.5*(0.01*percent_hue-1.0);
2831 while (hue < 0.0)
2832 hue+=1.0;
2833 while (hue > 1.0)
2834 hue-=1.0;
2835 blackness*=0.01*percent_blackness;
2836 whiteness*=0.01*percent_whiteness;
cristy0a39a5c2012-06-27 12:51:45 +00002837 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
cristy3ed852e2009-09-05 21:47:34 +00002838}
2839
cristy33bd5152011-08-24 01:42:24 +00002840MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2841 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002842{
2843#define ModulateImageTag "Modulate/Image"
2844
cristyc4c8d132010-01-07 01:58:38 +00002845 CacheView
2846 *image_view;
2847
cristy3ed852e2009-09-05 21:47:34 +00002848 ColorspaceType
2849 colorspace;
2850
2851 const char
2852 *artifact;
2853
2854 double
2855 percent_brightness,
2856 percent_hue,
cristy4d412212012-06-27 23:14:48 +00002857 percent_saturation;
cristy3ed852e2009-09-05 21:47:34 +00002858
cristy3ed852e2009-09-05 21:47:34 +00002859 GeometryInfo
2860 geometry_info;
2861
cristy3ed852e2009-09-05 21:47:34 +00002862 MagickBooleanType
2863 status;
2864
cristybb503372010-05-27 20:51:26 +00002865 MagickOffsetType
2866 progress;
2867
cristy3ed852e2009-09-05 21:47:34 +00002868 MagickStatusType
2869 flags;
2870
cristybb503372010-05-27 20:51:26 +00002871 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002872 i;
2873
cristybb503372010-05-27 20:51:26 +00002874 ssize_t
2875 y;
2876
cristy3ed852e2009-09-05 21:47:34 +00002877 /*
cristy2b726bd2010-01-11 01:05:39 +00002878 Initialize modulate table.
cristy3ed852e2009-09-05 21:47:34 +00002879 */
2880 assert(image != (Image *) NULL);
2881 assert(image->signature == MagickSignature);
2882 if (image->debug != MagickFalse)
2883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2884 if (modulate == (char *) NULL)
2885 return(MagickFalse);
cristy0a39a5c2012-06-27 12:51:45 +00002886 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristybc7fdcd2012-06-27 01:43:10 +00002887 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002888 flags=ParseGeometry(modulate,&geometry_info);
2889 percent_brightness=geometry_info.rho;
2890 percent_saturation=geometry_info.sigma;
2891 if ((flags & SigmaValue) == 0)
2892 percent_saturation=100.0;
2893 percent_hue=geometry_info.xi;
2894 if ((flags & XiValue) == 0)
2895 percent_hue=100.0;
2896 colorspace=UndefinedColorspace;
2897 artifact=GetImageArtifact(image,"modulate:colorspace");
2898 if (artifact != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00002899 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +00002900 MagickFalse,artifact);
2901 if (image->storage_class == PseudoClass)
2902 {
2903 /*
2904 Modulate colormap.
2905 */
cristyb5d5f722009-11-04 03:03:49 +00002906#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002907 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002908 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00002909#endif
cristybb503372010-05-27 20:51:26 +00002910 for (i=0; i < (ssize_t) image->colors; i++)
cristy0a39a5c2012-06-27 12:51:45 +00002911 {
cristy4d412212012-06-27 23:14:48 +00002912 double
2913 blue,
2914 green,
2915 red;
2916
cristy0a39a5c2012-06-27 12:51:45 +00002917 red=image->colormap[i].red;
2918 green=image->colormap[i].green;
2919 blue=image->colormap[i].blue;
cristy3ed852e2009-09-05 21:47:34 +00002920 switch (colorspace)
2921 {
2922 case HSBColorspace:
2923 {
2924 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
cristy0a39a5c2012-06-27 12:51:45 +00002925 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002926 break;
2927 }
2928 case HSLColorspace:
2929 default:
2930 {
2931 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
cristy0a39a5c2012-06-27 12:51:45 +00002932 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002933 break;
2934 }
2935 case HWBColorspace:
2936 {
2937 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
cristy0a39a5c2012-06-27 12:51:45 +00002938 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002939 break;
2940 }
2941 }
cristy0a39a5c2012-06-27 12:51:45 +00002942 }
cristy3ed852e2009-09-05 21:47:34 +00002943 }
2944 /*
2945 Modulate image.
2946 */
2947 status=MagickTrue;
2948 progress=0;
cristydb070952012-04-20 14:33:00 +00002949 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00002950#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002951 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002952 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00002953#endif
cristybb503372010-05-27 20:51:26 +00002954 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002955 {
cristy4c08aed2011-07-01 19:47:50 +00002956 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002957 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002958
cristy8d4629b2010-08-30 17:59:46 +00002959 register ssize_t
2960 x;
2961
cristy3ed852e2009-09-05 21:47:34 +00002962 if (status == MagickFalse)
2963 continue;
2964 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002965 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002966 {
2967 status=MagickFalse;
2968 continue;
2969 }
cristybb503372010-05-27 20:51:26 +00002970 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002971 {
cristy4d412212012-06-27 23:14:48 +00002972 double
2973 blue,
2974 green,
2975 red;
2976
cristyda1f9c12011-10-02 21:39:49 +00002977 red=(double) GetPixelRed(image,q);
2978 green=(double) GetPixelGreen(image,q);
2979 blue=(double) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002980 switch (colorspace)
2981 {
2982 case HSBColorspace:
2983 {
2984 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002985 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002986 break;
2987 }
2988 case HSLColorspace:
2989 default:
2990 {
2991 ModulateHSL(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 case HWBColorspace:
2996 {
2997 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002998 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002999 break;
3000 }
3001 }
cristy3094b7f2011-10-01 23:18:02 +00003002 SetPixelRed(image,ClampToQuantum(red),q);
3003 SetPixelGreen(image,ClampToQuantum(green),q);
3004 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +00003005 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003006 }
3007 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3008 status=MagickFalse;
3009 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3010 {
3011 MagickBooleanType
3012 proceed;
3013
cristyb5d5f722009-11-04 03:03:49 +00003014#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003015 #pragma omp critical (MagickCore_ModulateImage)
cristy3ed852e2009-09-05 21:47:34 +00003016#endif
3017 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3018 if (proceed == MagickFalse)
3019 status=MagickFalse;
3020 }
3021 }
3022 image_view=DestroyCacheView(image_view);
3023 return(status);
3024}
3025
3026/*
3027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3028% %
3029% %
3030% %
3031% N e g a t e I m a g e %
3032% %
3033% %
3034% %
3035%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3036%
3037% NegateImage() negates the colors in the reference image. The grayscale
3038% option means that only grayscale values within the image are negated.
3039%
cristy50fbc382011-07-07 02:19:17 +00003040% The format of the NegateImage method is:
cristy3ed852e2009-09-05 21:47:34 +00003041%
3042% MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003043% const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003044%
3045% A description of each parameter follows:
3046%
3047% o image: the image.
3048%
cristy3ed852e2009-09-05 21:47:34 +00003049% o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3050%
cristyb3e7c6c2011-07-24 01:43:55 +00003051% o exception: return any errors or warnings in this structure.
3052%
cristy3ed852e2009-09-05 21:47:34 +00003053*/
cristy3ed852e2009-09-05 21:47:34 +00003054MagickExport MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003055 const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003056{
cristy3ed852e2009-09-05 21:47:34 +00003057#define NegateImageTag "Negate/Image"
3058
cristyc4c8d132010-01-07 01:58:38 +00003059 CacheView
3060 *image_view;
3061
cristy3ed852e2009-09-05 21:47:34 +00003062 MagickBooleanType
3063 status;
3064
cristybb503372010-05-27 20:51:26 +00003065 MagickOffsetType
3066 progress;
3067
3068 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003069 i;
3070
cristybb503372010-05-27 20:51:26 +00003071 ssize_t
3072 y;
3073
cristy3ed852e2009-09-05 21:47:34 +00003074 assert(image != (Image *) NULL);
3075 assert(image->signature == MagickSignature);
3076 if (image->debug != MagickFalse)
3077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3078 if (image->storage_class == PseudoClass)
3079 {
3080 /*
3081 Negate colormap.
3082 */
cristyb5d5f722009-11-04 03:03:49 +00003083#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003084 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003085 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003086#endif
cristybb503372010-05-27 20:51:26 +00003087 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003088 {
3089 if (grayscale != MagickFalse)
3090 if ((image->colormap[i].red != image->colormap[i].green) ||
3091 (image->colormap[i].green != image->colormap[i].blue))
3092 continue;
cristyed231572011-07-14 02:18:59 +00003093 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003094 image->colormap[i].red=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003095 image->colormap[i].red;
cristyed231572011-07-14 02:18:59 +00003096 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003097 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003098 image->colormap[i].green;
cristyed231572011-07-14 02:18:59 +00003099 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy6e963d82012-06-19 15:23:24 +00003100 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00003101 image->colormap[i].blue;
3102 }
3103 }
3104 /*
3105 Negate image.
3106 */
3107 status=MagickTrue;
3108 progress=0;
cristydb070952012-04-20 14:33:00 +00003109 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003110 if (grayscale != MagickFalse)
3111 {
cristyb5d5f722009-11-04 03:03:49 +00003112#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003113 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003114 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003115#endif
cristybb503372010-05-27 20:51:26 +00003116 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003117 {
3118 MagickBooleanType
3119 sync;
3120
cristy4c08aed2011-07-01 19:47:50 +00003121 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003122 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003123
cristy8d4629b2010-08-30 17:59:46 +00003124 register ssize_t
3125 x;
3126
cristy3ed852e2009-09-05 21:47:34 +00003127 if (status == MagickFalse)
3128 continue;
3129 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3130 exception);
cristyacd2ed22011-08-30 01:44:23 +00003131 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003132 {
3133 status=MagickFalse;
3134 continue;
3135 }
cristybb503372010-05-27 20:51:26 +00003136 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003137 {
cristy9aa95be2011-07-20 21:56:45 +00003138 register ssize_t
3139 i;
3140
cristy10a6c612012-01-29 21:41:05 +00003141 if ((GetPixelMask(image,q) != 0) ||
3142 (IsPixelGray(image,q) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003143 {
cristya30d9ba2011-07-23 21:00:48 +00003144 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003145 continue;
3146 }
cristya30d9ba2011-07-23 21:00:48 +00003147 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy9aa95be2011-07-20 21:56:45 +00003148 {
cristyabace412011-12-11 15:56:53 +00003149 PixelChannel
3150 channel;
3151
cristy1aaa3cd2011-08-21 23:48:17 +00003152 PixelTrait
cristy9aa95be2011-07-20 21:56:45 +00003153 traits;
3154
cristyabace412011-12-11 15:56:53 +00003155 channel=GetPixelChannelMapChannel(image,i);
3156 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003157 if ((traits & UpdatePixelTrait) == 0)
3158 continue;
3159 q[i]=QuantumRange-q[i];
cristy9aa95be2011-07-20 21:56:45 +00003160 }
cristya30d9ba2011-07-23 21:00:48 +00003161 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003162 }
3163 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3164 if (sync == MagickFalse)
3165 status=MagickFalse;
3166 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3167 {
3168 MagickBooleanType
3169 proceed;
3170
cristyb5d5f722009-11-04 03:03:49 +00003171#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003172 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003173#endif
3174 proceed=SetImageProgress(image,NegateImageTag,progress++,
3175 image->rows);
3176 if (proceed == MagickFalse)
3177 status=MagickFalse;
3178 }
3179 }
3180 image_view=DestroyCacheView(image_view);
3181 return(MagickTrue);
3182 }
3183 /*
3184 Negate image.
3185 */
cristyb5d5f722009-11-04 03:03:49 +00003186#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003187 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003188 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003189#endif
cristybb503372010-05-27 20:51:26 +00003190 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003191 {
cristy4c08aed2011-07-01 19:47:50 +00003192 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003193 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003194
cristy8d4629b2010-08-30 17:59:46 +00003195 register ssize_t
3196 x;
3197
cristy3ed852e2009-09-05 21:47:34 +00003198 if (status == MagickFalse)
3199 continue;
3200 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003201 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003202 {
3203 status=MagickFalse;
3204 continue;
3205 }
cristybb503372010-05-27 20:51:26 +00003206 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003207 {
cristyf7dc44c2011-07-20 14:41:15 +00003208 register ssize_t
3209 i;
3210
cristyd09f8802012-02-04 16:44:10 +00003211 if (GetPixelMask(image,q) != 0)
3212 {
3213 q+=GetPixelChannels(image);
3214 continue;
3215 }
cristya30d9ba2011-07-23 21:00:48 +00003216 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyf7dc44c2011-07-20 14:41:15 +00003217 {
cristyabace412011-12-11 15:56:53 +00003218 PixelChannel
3219 channel;
3220
cristy1aaa3cd2011-08-21 23:48:17 +00003221 PixelTrait
cristyf7dc44c2011-07-20 14:41:15 +00003222 traits;
3223
cristyabace412011-12-11 15:56:53 +00003224 channel=GetPixelChannelMapChannel(image,i);
3225 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003226 if ((traits & UpdatePixelTrait) == 0)
cristyec9e3a62012-02-01 02:09:32 +00003227 continue;
3228 q[i]=QuantumRange-q[i];
cristyf7dc44c2011-07-20 14:41:15 +00003229 }
cristya30d9ba2011-07-23 21:00:48 +00003230 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003231 }
3232 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3233 status=MagickFalse;
3234 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3235 {
3236 MagickBooleanType
3237 proceed;
3238
cristyb5d5f722009-11-04 03:03:49 +00003239#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003240 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003241#endif
3242 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3243 if (proceed == MagickFalse)
3244 status=MagickFalse;
3245 }
3246 }
3247 image_view=DestroyCacheView(image_view);
3248 return(status);
3249}
3250
3251/*
3252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3253% %
3254% %
3255% %
3256% N o r m a l i z e I m a g e %
3257% %
3258% %
3259% %
3260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3261%
anthony207ce472012-04-04 06:21:26 +00003262% The NormalizeImage() method enhances the contrast of a color image by
3263% mapping the darkest 2 percent of all pixel to black and the brightest
3264% 1 percent to white.
cristy3ed852e2009-09-05 21:47:34 +00003265%
3266% The format of the NormalizeImage method is:
3267%
cristye23ec9d2011-08-16 18:15:40 +00003268% MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003269%
3270% A description of each parameter follows:
3271%
3272% o image: the image.
3273%
cristye23ec9d2011-08-16 18:15:40 +00003274% o exception: return any errors or warnings in this structure.
3275%
cristy3ed852e2009-09-05 21:47:34 +00003276*/
cristye23ec9d2011-08-16 18:15:40 +00003277MagickExport MagickBooleanType NormalizeImage(Image *image,
3278 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003279{
cristy3ed852e2009-09-05 21:47:34 +00003280 double
3281 black_point,
3282 white_point;
3283
cristy530239c2010-07-25 17:34:26 +00003284 black_point=(double) image->columns*image->rows*0.0015;
3285 white_point=(double) image->columns*image->rows*0.9995;
cristye23ec9d2011-08-16 18:15:40 +00003286 return(ContrastStretchImage(image,black_point,white_point,exception));
cristy3ed852e2009-09-05 21:47:34 +00003287}
3288
3289/*
3290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3291% %
3292% %
3293% %
3294% S i g m o i d a l C o n t r a s t I m a g e %
3295% %
3296% %
3297% %
3298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3299%
3300% SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3301% sigmoidal contrast algorithm. Increase the contrast of the image using a
3302% sigmoidal transfer function without saturating highlights or shadows.
3303% Contrast indicates how much to increase the contrast (0 is none; 3 is
anthony207ce472012-04-04 06:21:26 +00003304% typical; 20 is pushing it); mid-point indicates where midtones fall in the
3305% resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3306% sharpen to MagickTrue to increase the image contrast otherwise the contrast
3307% is reduced.
cristy3ed852e2009-09-05 21:47:34 +00003308%
3309% The format of the SigmoidalContrastImage method is:
3310%
3311% MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003312% const MagickBooleanType sharpen,const char *levels,
3313% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003314%
3315% A description of each parameter follows:
3316%
3317% o image: the image.
3318%
cristy3ed852e2009-09-05 21:47:34 +00003319% o sharpen: Increase or decrease image contrast.
3320%
cristyfa769582010-09-30 23:30:03 +00003321% o alpha: strength of the contrast, the larger the number the more
3322% 'threshold-like' it becomes.
cristy3ed852e2009-09-05 21:47:34 +00003323%
cristyfa769582010-09-30 23:30:03 +00003324% o beta: midpoint of the function as a color value 0 to QuantumRange.
cristy3ed852e2009-09-05 21:47:34 +00003325%
cristy33bd5152011-08-24 01:42:24 +00003326% o exception: return any errors or warnings in this structure.
3327%
cristy3ed852e2009-09-05 21:47:34 +00003328*/
cristy3ed852e2009-09-05 21:47:34 +00003329MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003330 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3331 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003332{
3333#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3334
cristyc4c8d132010-01-07 01:58:38 +00003335 CacheView
3336 *image_view;
3337
cristy3ed852e2009-09-05 21:47:34 +00003338 MagickBooleanType
3339 status;
3340
cristybb503372010-05-27 20:51:26 +00003341 MagickOffsetType
3342 progress;
3343
cristy3ed852e2009-09-05 21:47:34 +00003344 MagickRealType
3345 *sigmoidal_map;
3346
cristybb503372010-05-27 20:51:26 +00003347 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003348 i;
3349
cristybb503372010-05-27 20:51:26 +00003350 ssize_t
3351 y;
3352
cristy3ed852e2009-09-05 21:47:34 +00003353 /*
3354 Allocate and initialize sigmoidal maps.
3355 */
3356 assert(image != (Image *) NULL);
3357 assert(image->signature == MagickSignature);
3358 if (image->debug != MagickFalse)
3359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3360 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3361 sizeof(*sigmoidal_map));
3362 if (sigmoidal_map == (MagickRealType *) NULL)
3363 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3364 image->filename);
3365 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
cristyb5d5f722009-11-04 03:03:49 +00003366#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003367 #pragma omp parallel for schedule(static) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003368 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003369#endif
cristybb503372010-05-27 20:51:26 +00003370 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003371 {
3372 if (sharpen != MagickFalse)
3373 {
anthony207ce472012-04-04 06:21:26 +00003374#define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3375#if 1
3376 /* Simpilified function scaling,
3377 * with better 'contrast=0' or 'flatline' handling (greyscale)
3378 */
3379 double
3380 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3381 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3382 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3383 (MagickRealType)(MaxMap*(
3384 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3385 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3386#else
3387 /* Scaled sigmoidal formula...
3388 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3389 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
cristy3ed852e2009-09-05 21:47:34 +00003390 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3391 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
anthony207ce472012-04-04 06:21:26 +00003392 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3393 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3394 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3395 (double) QuantumRange)))))+0.5));
3396#endif
cristy3ed852e2009-09-05 21:47:34 +00003397 continue;
3398 }
anthony207ce472012-04-04 06:21:26 +00003399#if 1
3400 {
3401 /* Inverse -- See
3402 http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
3403 */
3404 double
3405 min = sigmoidal(contrast,1.0,0.0),
3406 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3407 xi = min+(double)i/MaxMap*(max-min);
3408 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3409 (MagickRealType)(MaxMap*(
3410 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3411 }
3412#else
3413 /* expanded form of the above */
cristy3ed852e2009-09-05 21:47:34 +00003414 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
anthony207ce472012-04-04 06:21:26 +00003415 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3416 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3417 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3418 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3419 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3420 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3421 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3422 (double) QuantumRange*contrast))))))/contrast)));
3423#endif
cristy3ed852e2009-09-05 21:47:34 +00003424 }
3425 if (image->storage_class == PseudoClass)
3426 {
3427 /*
3428 Sigmoidal-contrast enhance colormap.
3429 */
cristyb5d5f722009-11-04 03:03:49 +00003430#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003431 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003432 dynamic_number_threads(image,image->columns,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003433#endif
cristybb503372010-05-27 20:51:26 +00003434 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003435 {
cristyed231572011-07-14 02:18:59 +00003436 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003437 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3438 ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00003439 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003440 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3441 ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00003442 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003443 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3444 ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00003445 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003446 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3447 ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00003448 }
3449 }
3450 /*
3451 Sigmoidal-contrast enhance image.
3452 */
3453 status=MagickTrue;
3454 progress=0;
cristydb070952012-04-20 14:33:00 +00003455 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003456#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003457 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003458 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003459#endif
cristybb503372010-05-27 20:51:26 +00003460 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003461 {
cristy4c08aed2011-07-01 19:47:50 +00003462 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003463 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003464
cristy8d4629b2010-08-30 17:59:46 +00003465 register ssize_t
3466 x;
3467
cristy3ed852e2009-09-05 21:47:34 +00003468 if (status == MagickFalse)
3469 continue;
3470 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003471 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003472 {
3473 status=MagickFalse;
3474 continue;
3475 }
cristybb503372010-05-27 20:51:26 +00003476 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003477 {
cristy33bd5152011-08-24 01:42:24 +00003478 register ssize_t
3479 i;
3480
cristy10a6c612012-01-29 21:41:05 +00003481 if (GetPixelMask(image,q) != 0)
3482 {
3483 q+=GetPixelChannels(image);
3484 continue;
3485 }
cristy33bd5152011-08-24 01:42:24 +00003486 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3487 {
cristyabace412011-12-11 15:56:53 +00003488 PixelChannel
3489 channel;
3490
cristy33bd5152011-08-24 01:42:24 +00003491 PixelTrait
3492 traits;
3493
cristyabace412011-12-11 15:56:53 +00003494 channel=GetPixelChannelMapChannel(image,i);
3495 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003496 if ((traits & UpdatePixelTrait) == 0)
3497 continue;
3498 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
cristy33bd5152011-08-24 01:42:24 +00003499 }
cristyed231572011-07-14 02:18:59 +00003500 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003501 }
3502 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3503 status=MagickFalse;
3504 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3505 {
3506 MagickBooleanType
3507 proceed;
3508
cristyb5d5f722009-11-04 03:03:49 +00003509#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003510 #pragma omp critical (MagickCore_SigmoidalContrastImage)
cristy3ed852e2009-09-05 21:47:34 +00003511#endif
3512 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3513 image->rows);
3514 if (proceed == MagickFalse)
3515 status=MagickFalse;
3516 }
3517 }
3518 image_view=DestroyCacheView(image_view);
3519 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);
3520 return(status);
3521}