blob: 461ce27c70088ffc0c4ad48ac17b8e822ab49760 [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"
50#include "MagickCore/composite-private.h"
51#include "MagickCore/enhance.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/fx.h"
55#include "MagickCore/gem.h"
cristyd1dd6e42011-09-04 01:46:08 +000056#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000057#include "MagickCore/geometry.h"
58#include "MagickCore/histogram.h"
59#include "MagickCore/image.h"
60#include "MagickCore/image-private.h"
61#include "MagickCore/memory_.h"
62#include "MagickCore/monitor.h"
63#include "MagickCore/monitor-private.h"
64#include "MagickCore/option.h"
65#include "MagickCore/pixel-accessor.h"
66#include "MagickCore/quantum.h"
67#include "MagickCore/quantum-private.h"
68#include "MagickCore/resample.h"
69#include "MagickCore/resample-private.h"
70#include "MagickCore/statistic.h"
71#include "MagickCore/string_.h"
72#include "MagickCore/string-private.h"
73#include "MagickCore/thread-private.h"
74#include "MagickCore/token.h"
75#include "MagickCore/xml-tree.h"
cristy433d1182011-09-04 13:38:52 +000076#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000077
78/*
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80% %
81% %
82% %
83% A u t o G a m m a I m a g e %
84% %
85% %
86% %
87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88%
89% AutoGammaImage() extract the 'mean' from the image and adjust the image
90% to try make set its gamma appropriatally.
91%
cristy308b4e62009-09-21 14:40:44 +000092% The format of the AutoGammaImage method is:
cristy3ed852e2009-09-05 21:47:34 +000093%
cristy95111202011-08-09 19:41:42 +000094% MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000095%
96% A description of each parameter follows:
97%
98% o image: The image to auto-level
99%
cristy95111202011-08-09 19:41:42 +0000100% o exception: return any errors or warnings in this structure.
101%
cristy3ed852e2009-09-05 21:47:34 +0000102*/
cristy95111202011-08-09 19:41:42 +0000103MagickExport MagickBooleanType AutoGammaImage(Image *image,
104 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000105{
cristy3ed852e2009-09-05 21:47:34 +0000106 double
cristy4c08aed2011-07-01 19:47:50 +0000107 gamma,
108 log_mean,
109 mean,
110 sans;
anthony4efe5972009-09-11 06:46:40 +0000111
cristy95111202011-08-09 19:41:42 +0000112 MagickStatusType
113 status;
114
cristy01e9afd2011-08-10 17:38:41 +0000115 register ssize_t
116 i;
117
cristy4c08aed2011-07-01 19:47:50 +0000118 log_mean=log(0.5);
cristy5f95f4f2011-10-23 01:01:01 +0000119 if (image->channel_mask == DefaultChannels)
cristy3ed852e2009-09-05 21:47:34 +0000120 {
121 /*
cristy01e9afd2011-08-10 17:38:41 +0000122 Apply gamma correction equally across all given channels.
cristy3ed852e2009-09-05 21:47:34 +0000123 */
cristy95111202011-08-09 19:41:42 +0000124 (void) GetImageMean(image,&mean,&sans,exception);
cristy4c08aed2011-07-01 19:47:50 +0000125 gamma=log(mean*QuantumScale)/log_mean;
cristy01e9afd2011-08-10 17:38:41 +0000126 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
cristy3ed852e2009-09-05 21:47:34 +0000127 }
cristy3ed852e2009-09-05 21:47:34 +0000128 /*
cristy4c08aed2011-07-01 19:47:50 +0000129 Auto-gamma each channel separately.
cristy3ed852e2009-09-05 21:47:34 +0000130 */
cristy4c08aed2011-07-01 19:47:50 +0000131 status=MagickTrue;
cristy01e9afd2011-08-10 17:38:41 +0000132 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
133 {
cristybd5a96c2011-08-21 00:04:26 +0000134 ChannelType
135 channel_mask;
136
cristyabace412011-12-11 15:56:53 +0000137 PixelChannel
138 channel;
139
cristy01e9afd2011-08-10 17:38:41 +0000140 PixelTrait
141 traits;
142
cristyabace412011-12-11 15:56:53 +0000143 channel=GetPixelChannelMapChannel(image,i);
144 traits=GetPixelChannelMapTraits(image,channel);
cristy01e9afd2011-08-10 17:38:41 +0000145 if ((traits & UpdatePixelTrait) == 0)
146 continue;
cristya13d0822011-09-19 00:19:19 +0000147 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
cristy01e9afd2011-08-10 17:38:41 +0000148 status=GetImageMean(image,&mean,&sans,exception);
149 gamma=log(mean*QuantumScale)/log_mean;
150 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
cristybd5a96c2011-08-21 00:04:26 +0000151 (void) SetPixelChannelMask(image,channel_mask);
cristy01e9afd2011-08-10 17:38:41 +0000152 if (status == MagickFalse)
153 break;
154 }
cristy3ed852e2009-09-05 21:47:34 +0000155 return(status != 0 ? MagickTrue : MagickFalse);
156}
157
158/*
159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160% %
161% %
162% %
163% A u t o L e v e l I m a g e %
164% %
165% %
166% %
167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168%
169% AutoLevelImage() adjusts the levels of a particular image channel by
170% scaling the minimum and maximum values to the full quantum range.
171%
172% The format of the LevelImage method is:
173%
cristy95111202011-08-09 19:41:42 +0000174% MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000175%
176% A description of each parameter follows:
177%
178% o image: The image to auto-level
179%
cristy95111202011-08-09 19:41:42 +0000180% o exception: return any errors or warnings in this structure.
181%
cristy3ed852e2009-09-05 21:47:34 +0000182*/
cristy95111202011-08-09 19:41:42 +0000183MagickExport MagickBooleanType AutoLevelImage(Image *image,
184 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000185{
cristyb303c3d2011-09-09 11:24:40 +0000186 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
cristy3ed852e2009-09-05 21:47:34 +0000187}
188
189/*
190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191% %
192% %
193% %
cristya28d6b82010-01-11 20:03:47 +0000194% B r i g h t n e s s C o n t r a s t I m a g e %
195% %
196% %
197% %
198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199%
cristyf4356f92011-08-01 15:33:48 +0000200% BrightnessContrastImage() changes the brightness and/or contrast of an
201% image. It converts the brightness and contrast parameters into slope and
202% intercept and calls a polynomical function to apply to the image.
cristya28d6b82010-01-11 20:03:47 +0000203%
204% The format of the BrightnessContrastImage method is:
205%
206% MagickBooleanType BrightnessContrastImage(Image *image,
cristy444eda62011-08-10 02:07:46 +0000207% const double brightness,const double contrast,ExceptionInfo *exception)
cristya28d6b82010-01-11 20:03:47 +0000208%
209% A description of each parameter follows:
210%
211% o image: the image.
212%
cristya28d6b82010-01-11 20:03:47 +0000213% o brightness: the brightness percent (-100 .. 100).
214%
215% o contrast: the contrast percent (-100 .. 100).
216%
cristy444eda62011-08-10 02:07:46 +0000217% o exception: return any errors or warnings in this structure.
218%
cristya28d6b82010-01-11 20:03:47 +0000219*/
cristya28d6b82010-01-11 20:03:47 +0000220MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
cristy444eda62011-08-10 02:07:46 +0000221 const double brightness,const double contrast,ExceptionInfo *exception)
cristya28d6b82010-01-11 20:03:47 +0000222{
cristya28d6b82010-01-11 20:03:47 +0000223#define BrightnessContastImageTag "BrightnessContast/Image"
224
225 double
226 alpha,
cristya28d6b82010-01-11 20:03:47 +0000227 coefficients[2],
cristye23ec9d2011-08-16 18:15:40 +0000228 intercept,
cristya28d6b82010-01-11 20:03:47 +0000229 slope;
230
231 MagickBooleanType
232 status;
233
234 /*
235 Compute slope and intercept.
236 */
237 assert(image != (Image *) NULL);
238 assert(image->signature == MagickSignature);
239 if (image->debug != MagickFalse)
240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
241 alpha=contrast;
cristy4205a3c2010-09-12 20:19:59 +0000242 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
cristya28d6b82010-01-11 20:03:47 +0000243 if (slope < 0.0)
244 slope=0.0;
245 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
246 coefficients[0]=slope;
247 coefficients[1]=intercept;
cristy444eda62011-08-10 02:07:46 +0000248 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
249 return(status);
250}
251
252/*
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% %
255% %
256% %
257% C l u t I m a g e %
258% %
259% %
260% %
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262%
263% ClutImage() replaces each color value in the given image, by using it as an
264% index to lookup a replacement color value in a Color Look UP Table in the
265% form of an image. The values are extracted along a diagonal of the CLUT
266% image so either a horizontal or vertial gradient image can be used.
267%
268% Typically this is used to either re-color a gray-scale image according to a
269% color gradient in the CLUT image, or to perform a freeform histogram
270% (level) adjustment according to the (typically gray-scale) gradient in the
271% CLUT image.
272%
273% When the 'channel' mask includes the matte/alpha transparency channel but
274% one image has no such channel it is assumed that that image is a simple
275% gray-scale image that will effect the alpha channel values, either for
276% gray-scale coloring (with transparent or semi-transparent colors), or
277% a histogram adjustment of existing alpha channel values. If both images
278% have matte channels, direct and normal indexing is applied, which is rarely
279% used.
280%
281% The format of the ClutImage method is:
282%
283% MagickBooleanType ClutImage(Image *image,Image *clut_image,
cristy5c4e2582011-09-11 19:21:03 +0000284% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy444eda62011-08-10 02:07:46 +0000285%
286% A description of each parameter follows:
287%
288% o image: the image, which is replaced by indexed CLUT values
289%
290% o clut_image: the color lookup table image for replacement color values.
291%
cristy5c4e2582011-09-11 19:21:03 +0000292% o method: the pixel interpolation method.
cristy444eda62011-08-10 02:07:46 +0000293%
294% o exception: return any errors or warnings in this structure.
295%
296*/
297MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
cristy5c4e2582011-09-11 19:21:03 +0000298 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy444eda62011-08-10 02:07:46 +0000299{
cristy444eda62011-08-10 02:07:46 +0000300#define ClutImageTag "Clut/Image"
301
302 CacheView
303 *clut_view,
304 *image_view;
305
306 double
307 *clut_map;
308
309 MagickBooleanType
310 status;
311
312 MagickOffsetType
313 progress;
314
315 register ssize_t
316 x;
317
318 ssize_t
319 adjust,
320 y;
321
322 assert(image != (Image *) NULL);
323 assert(image->signature == MagickSignature);
324 if (image->debug != MagickFalse)
325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
326 assert(clut_image != (Image *) NULL);
327 assert(clut_image->signature == MagickSignature);
328 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
329 return(MagickFalse);
330 clut_map=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
331 sizeof(*clut_map));
332 if (clut_map == (double *) NULL)
333 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
334 image->filename);
335 /*
336 Clut image.
337 */
338 status=MagickTrue;
339 progress=0;
340 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
341 clut_view=AcquireCacheView(clut_image);
342#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000343 #pragma omp parallel for schedule(static,4)
cristy444eda62011-08-10 02:07:46 +0000344#endif
345 for (x=0; x <= (ssize_t) MaxMap; x++)
346 {
347 register ssize_t
348 i;
349
350 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
351 (void) InterpolatePixelChannel(clut_image,clut_view,(PixelChannel) i,
cristy5c4e2582011-09-11 19:21:03 +0000352 method,QuantumScale*x*(clut_image->columns-adjust),QuantumScale*x*
353 (clut_image->rows-adjust),clut_map+x*GetPixelChannels(clut_image)+i,
354 exception);
cristy444eda62011-08-10 02:07:46 +0000355 }
356 clut_view=DestroyCacheView(clut_view);
357 image_view=AcquireCacheView(image);
358#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000359 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy444eda62011-08-10 02:07:46 +0000360#endif
361 for (y=0; y < (ssize_t) image->rows; y++)
362 {
363 register Quantum
364 *restrict q;
365
366 register ssize_t
367 x;
368
369 if (status == MagickFalse)
370 continue;
371 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
372 if (q == (Quantum *) NULL)
373 {
374 status=MagickFalse;
375 continue;
376 }
377 for (x=0; x < (ssize_t) image->columns; x++)
378 {
379 register ssize_t
380 i;
381
cristy10a6c612012-01-29 21:41:05 +0000382 if (GetPixelMask(image,q) != 0)
383 {
384 q+=GetPixelChannels(image);
385 continue;
386 }
cristy444eda62011-08-10 02:07:46 +0000387 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
388 {
389 PixelChannel
390 channel;
391
cristy1aaa3cd2011-08-21 23:48:17 +0000392 PixelTrait
cristy444eda62011-08-10 02:07:46 +0000393 clut_traits,
394 traits;
395
cristye2a912b2011-12-05 20:02:07 +0000396 channel=GetPixelChannelMapChannel(clut_image,i);
cristyabace412011-12-11 15:56:53 +0000397 clut_traits=GetPixelChannelMapTraits(clut_image,channel);
cristy444eda62011-08-10 02:07:46 +0000398 traits=GetPixelChannelMapTraits(clut_image,channel);
cristy010d7d12011-08-31 01:02:48 +0000399 if ((traits == UndefinedPixelTrait) ||
400 (clut_traits == UndefinedPixelTrait) ||
401 ((traits & UpdatePixelTrait) == 0))
402 continue;
cristy0beccfa2011-09-25 20:47:53 +0000403 SetPixelChannel(clut_image,channel,ClampToQuantum(clut_map[
404 ScaleQuantumToMap(GetPixelChannel(clut_image,channel,q))*
405 GetPixelChannels(clut_image)+channel]),q);
cristy444eda62011-08-10 02:07:46 +0000406 }
407 q+=GetPixelChannels(image);
408 }
409 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
410 status=MagickFalse;
411 if (image->progress_monitor != (MagickProgressMonitor) NULL)
412 {
413 MagickBooleanType
414 proceed;
415
416#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000417 #pragma omp critical (MagickCore_ClutImage)
cristy444eda62011-08-10 02:07:46 +0000418#endif
419 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
420 if (proceed == MagickFalse)
421 status=MagickFalse;
422 }
423 }
424 image_view=DestroyCacheView(image_view);
425 clut_map=(double *) RelinquishMagickMemory(clut_map);
426 if ((clut_image->matte != MagickFalse) &&
427 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
428 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
cristya28d6b82010-01-11 20:03:47 +0000429 return(status);
430}
431
432/*
433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434% %
435% %
436% %
cristy3ed852e2009-09-05 21:47:34 +0000437% C o l o r D e c i s i o n L i s t I m a g e %
438% %
439% %
440% %
441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442%
443% ColorDecisionListImage() accepts a lightweight Color Correction Collection
444% (CCC) file which solely contains one or more color corrections and applies
445% the correction to the image. Here is a sample CCC file:
446%
447% <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
448% <ColorCorrection id="cc03345">
449% <SOPNode>
450% <Slope> 0.9 1.2 0.5 </Slope>
451% <Offset> 0.4 -0.5 0.6 </Offset>
452% <Power> 1.0 0.8 1.5 </Power>
453% </SOPNode>
454% <SATNode>
455% <Saturation> 0.85 </Saturation>
456% </SATNode>
457% </ColorCorrection>
458% </ColorCorrectionCollection>
459%
460% which includes the slop, offset, and power for each of the RGB channels
461% as well as the saturation.
462%
463% The format of the ColorDecisionListImage method is:
464%
465% MagickBooleanType ColorDecisionListImage(Image *image,
cristy1bfa9f02011-08-11 02:35:43 +0000466% const char *color_correction_collection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000467%
468% A description of each parameter follows:
469%
470% o image: the image.
471%
472% o color_correction_collection: the color correction collection in XML.
473%
cristy1bfa9f02011-08-11 02:35:43 +0000474% o exception: return any errors or warnings in this structure.
475%
cristy3ed852e2009-09-05 21:47:34 +0000476*/
477MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
cristy1bfa9f02011-08-11 02:35:43 +0000478 const char *color_correction_collection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000479{
480#define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
481
482 typedef struct _Correction
483 {
484 double
485 slope,
486 offset,
487 power;
488 } Correction;
489
490 typedef struct _ColorCorrection
491 {
492 Correction
493 red,
494 green,
495 blue;
496
497 double
498 saturation;
499 } ColorCorrection;
500
cristyc4c8d132010-01-07 01:58:38 +0000501 CacheView
502 *image_view;
503
cristy3ed852e2009-09-05 21:47:34 +0000504 char
505 token[MaxTextExtent];
506
507 ColorCorrection
508 color_correction;
509
510 const char
511 *content,
512 *p;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 MagickBooleanType
515 status;
516
cristybb503372010-05-27 20:51:26 +0000517 MagickOffsetType
518 progress;
519
cristy101ab702011-10-13 13:06:32 +0000520 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000521 *cdl_map;
522
cristybb503372010-05-27 20:51:26 +0000523 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000524 i;
525
cristybb503372010-05-27 20:51:26 +0000526 ssize_t
527 y;
528
cristy3ed852e2009-09-05 21:47:34 +0000529 XMLTreeInfo
530 *cc,
531 *ccc,
532 *sat,
533 *sop;
534
cristy3ed852e2009-09-05 21:47:34 +0000535 /*
536 Allocate and initialize cdl maps.
537 */
538 assert(image != (Image *) NULL);
539 assert(image->signature == MagickSignature);
540 if (image->debug != MagickFalse)
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
542 if (color_correction_collection == (const char *) NULL)
543 return(MagickFalse);
cristy1bfa9f02011-08-11 02:35:43 +0000544 ccc=NewXMLTree((const char *) color_correction_collection,exception);
cristy3ed852e2009-09-05 21:47:34 +0000545 if (ccc == (XMLTreeInfo *) NULL)
546 return(MagickFalse);
547 cc=GetXMLTreeChild(ccc,"ColorCorrection");
548 if (cc == (XMLTreeInfo *) NULL)
549 {
550 ccc=DestroyXMLTree(ccc);
551 return(MagickFalse);
552 }
553 color_correction.red.slope=1.0;
554 color_correction.red.offset=0.0;
555 color_correction.red.power=1.0;
556 color_correction.green.slope=1.0;
557 color_correction.green.offset=0.0;
558 color_correction.green.power=1.0;
559 color_correction.blue.slope=1.0;
560 color_correction.blue.offset=0.0;
561 color_correction.blue.power=1.0;
562 color_correction.saturation=0.0;
563 sop=GetXMLTreeChild(cc,"SOPNode");
564 if (sop != (XMLTreeInfo *) NULL)
565 {
566 XMLTreeInfo
567 *offset,
568 *power,
569 *slope;
570
571 slope=GetXMLTreeChild(sop,"Slope");
572 if (slope != (XMLTreeInfo *) NULL)
573 {
574 content=GetXMLTreeContent(slope);
575 p=(const char *) content;
576 for (i=0; (*p != '\0') && (i < 3); i++)
577 {
578 GetMagickToken(p,&p,token);
579 if (*token == ',')
580 GetMagickToken(p,&p,token);
581 switch (i)
582 {
cristyc1acd842011-05-19 23:05:47 +0000583 case 0:
584 {
cristy9b34e302011-11-05 02:15:45 +0000585 color_correction.red.slope=StringToDouble(token,(char **) NULL);
cristyc1acd842011-05-19 23:05:47 +0000586 break;
587 }
588 case 1:
589 {
cristydbdd0e32011-11-04 23:29:40 +0000590 color_correction.green.slope=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000591 (char **) NULL);
592 break;
593 }
594 case 2:
595 {
cristydbdd0e32011-11-04 23:29:40 +0000596 color_correction.blue.slope=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000597 (char **) NULL);
598 break;
599 }
cristy3ed852e2009-09-05 21:47:34 +0000600 }
601 }
602 }
603 offset=GetXMLTreeChild(sop,"Offset");
604 if (offset != (XMLTreeInfo *) NULL)
605 {
606 content=GetXMLTreeContent(offset);
607 p=(const char *) content;
608 for (i=0; (*p != '\0') && (i < 3); i++)
609 {
610 GetMagickToken(p,&p,token);
611 if (*token == ',')
612 GetMagickToken(p,&p,token);
613 switch (i)
614 {
cristyc1acd842011-05-19 23:05:47 +0000615 case 0:
616 {
cristydbdd0e32011-11-04 23:29:40 +0000617 color_correction.red.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000618 (char **) NULL);
619 break;
620 }
621 case 1:
622 {
cristydbdd0e32011-11-04 23:29:40 +0000623 color_correction.green.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000624 (char **) NULL);
625 break;
626 }
627 case 2:
628 {
cristydbdd0e32011-11-04 23:29:40 +0000629 color_correction.blue.offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000630 (char **) NULL);
631 break;
632 }
cristy3ed852e2009-09-05 21:47:34 +0000633 }
634 }
635 }
636 power=GetXMLTreeChild(sop,"Power");
637 if (power != (XMLTreeInfo *) NULL)
638 {
639 content=GetXMLTreeContent(power);
640 p=(const char *) content;
641 for (i=0; (*p != '\0') && (i < 3); i++)
642 {
643 GetMagickToken(p,&p,token);
644 if (*token == ',')
645 GetMagickToken(p,&p,token);
646 switch (i)
647 {
cristyc1acd842011-05-19 23:05:47 +0000648 case 0:
649 {
cristy9b34e302011-11-05 02:15:45 +0000650 color_correction.red.power=StringToDouble(token,(char **) NULL);
cristyc1acd842011-05-19 23:05:47 +0000651 break;
652 }
653 case 1:
654 {
cristydbdd0e32011-11-04 23:29:40 +0000655 color_correction.green.power=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000656 (char **) NULL);
657 break;
658 }
659 case 2:
660 {
cristydbdd0e32011-11-04 23:29:40 +0000661 color_correction.blue.power=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +0000662 (char **) NULL);
663 break;
664 }
cristy3ed852e2009-09-05 21:47:34 +0000665 }
666 }
667 }
668 }
669 sat=GetXMLTreeChild(cc,"SATNode");
670 if (sat != (XMLTreeInfo *) NULL)
671 {
672 XMLTreeInfo
673 *saturation;
674
675 saturation=GetXMLTreeChild(sat,"Saturation");
676 if (saturation != (XMLTreeInfo *) NULL)
677 {
678 content=GetXMLTreeContent(saturation);
679 p=(const char *) content;
680 GetMagickToken(p,&p,token);
cristyca826b52012-01-18 18:50:46 +0000681 color_correction.saturation=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000682 }
683 }
684 ccc=DestroyXMLTree(ccc);
685 if (image->debug != MagickFalse)
686 {
687 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
688 " Color Correction Collection:");
689 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000690 " color_correction.red.slope: %g",color_correction.red.slope);
cristy3ed852e2009-09-05 21:47:34 +0000691 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000692 " color_correction.red.offset: %g",color_correction.red.offset);
cristy3ed852e2009-09-05 21:47:34 +0000693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000694 " color_correction.red.power: %g",color_correction.red.power);
cristy3ed852e2009-09-05 21:47:34 +0000695 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000696 " color_correction.green.slope: %g",color_correction.green.slope);
cristy3ed852e2009-09-05 21:47:34 +0000697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000698 " color_correction.green.offset: %g",color_correction.green.offset);
cristy3ed852e2009-09-05 21:47:34 +0000699 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000700 " color_correction.green.power: %g",color_correction.green.power);
cristy3ed852e2009-09-05 21:47:34 +0000701 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000702 " color_correction.blue.slope: %g",color_correction.blue.slope);
cristy3ed852e2009-09-05 21:47:34 +0000703 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000704 " color_correction.blue.offset: %g",color_correction.blue.offset);
cristy3ed852e2009-09-05 21:47:34 +0000705 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000706 " color_correction.blue.power: %g",color_correction.blue.power);
cristy3ed852e2009-09-05 21:47:34 +0000707 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000708 " color_correction.saturation: %g",color_correction.saturation);
cristy3ed852e2009-09-05 21:47:34 +0000709 }
cristy101ab702011-10-13 13:06:32 +0000710 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
711 if (cdl_map == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000712 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
713 image->filename);
cristyb5d5f722009-11-04 03:03:49 +0000714#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000715 #pragma omp parallel for schedule(static,4)
cristy3ed852e2009-09-05 21:47:34 +0000716#endif
cristybb503372010-05-27 20:51:26 +0000717 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +0000718 {
cristyda1f9c12011-10-02 21:39:49 +0000719 cdl_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
720 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
721 color_correction.red.offset,color_correction.red.power))));
722 cdl_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
723 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
724 color_correction.green.offset,color_correction.green.power))));
725 cdl_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
726 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
727 color_correction.blue.offset,color_correction.blue.power))));
cristy3ed852e2009-09-05 21:47:34 +0000728 }
729 if (image->storage_class == PseudoClass)
730 {
731 /*
732 Apply transfer function to colormap.
733 */
cristyb5d5f722009-11-04 03:03:49 +0000734#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22e88202012-01-18 19:03:01 +0000735 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000736#endif
cristybb503372010-05-27 20:51:26 +0000737 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000738 {
739 double
740 luma;
741
742 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
743 0.0722*image->colormap[i].blue;
cristy22e88202012-01-18 19:03:01 +0000744 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
745 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-
746 luma;
747 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
748 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-
749 luma;
750 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
751 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-
752 luma;
cristy3ed852e2009-09-05 21:47:34 +0000753 }
754 }
755 /*
756 Apply transfer function to image.
757 */
758 status=MagickTrue;
759 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000760 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000761#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000762 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000763#endif
cristybb503372010-05-27 20:51:26 +0000764 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000765 {
766 double
767 luma;
768
cristy4c08aed2011-07-01 19:47:50 +0000769 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000770 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000771
cristy8d4629b2010-08-30 17:59:46 +0000772 register ssize_t
773 x;
774
cristy3ed852e2009-09-05 21:47:34 +0000775 if (status == MagickFalse)
776 continue;
777 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000778 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000779 {
780 status=MagickFalse;
781 continue;
782 }
cristybb503372010-05-27 20:51:26 +0000783 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000784 {
cristy4c08aed2011-07-01 19:47:50 +0000785 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
786 GetPixelBlue(image,q);
787 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
788 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
789 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
790 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
791 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
792 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
cristyed231572011-07-14 02:18:59 +0000793 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000794 }
795 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
796 status=MagickFalse;
797 if (image->progress_monitor != (MagickProgressMonitor) NULL)
798 {
799 MagickBooleanType
800 proceed;
801
cristyb5d5f722009-11-04 03:03:49 +0000802#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000803 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
cristy3ed852e2009-09-05 21:47:34 +0000804#endif
805 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
806 progress++,image->rows);
807 if (proceed == MagickFalse)
808 status=MagickFalse;
809 }
810 }
811 image_view=DestroyCacheView(image_view);
cristy101ab702011-10-13 13:06:32 +0000812 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
cristy3ed852e2009-09-05 21:47:34 +0000813 return(status);
814}
815
816/*
817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818% %
819% %
820% %
cristy3ed852e2009-09-05 21:47:34 +0000821% C o n t r a s t I m a g e %
822% %
823% %
824% %
825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826%
827% ContrastImage() enhances the intensity differences between the lighter and
828% darker elements of the image. Set sharpen to a MagickTrue to increase the
829% image contrast otherwise the contrast is reduced.
830%
831% The format of the ContrastImage method is:
832%
833% MagickBooleanType ContrastImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000834% const MagickBooleanType sharpen,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000835%
836% A description of each parameter follows:
837%
838% o image: the image.
839%
840% o sharpen: Increase or decrease image contrast.
841%
cristye23ec9d2011-08-16 18:15:40 +0000842% o exception: return any errors or warnings in this structure.
843%
cristy3ed852e2009-09-05 21:47:34 +0000844*/
845
cristy3094b7f2011-10-01 23:18:02 +0000846static void Contrast(const int sign,double *red,double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +0000847{
848 double
849 brightness,
850 hue,
851 saturation;
852
853 /*
854 Enhance contrast: dark color become darker, light color become lighter.
855 */
cristy3094b7f2011-10-01 23:18:02 +0000856 assert(red != (double *) NULL);
857 assert(green != (double *) NULL);
858 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000859 hue=0.0;
860 saturation=0.0;
861 brightness=0.0;
862 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
cristy31ac0f02011-03-12 02:04:47 +0000863 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
cristy4205a3c2010-09-12 20:19:59 +0000864 brightness);
cristy3ed852e2009-09-05 21:47:34 +0000865 if (brightness > 1.0)
866 brightness=1.0;
867 else
868 if (brightness < 0.0)
869 brightness=0.0;
870 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
871}
872
873MagickExport MagickBooleanType ContrastImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000874 const MagickBooleanType sharpen,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000875{
876#define ContrastImageTag "Contrast/Image"
877
cristyc4c8d132010-01-07 01:58:38 +0000878 CacheView
879 *image_view;
880
cristy3ed852e2009-09-05 21:47:34 +0000881 int
882 sign;
883
cristy3ed852e2009-09-05 21:47:34 +0000884 MagickBooleanType
885 status;
886
cristybb503372010-05-27 20:51:26 +0000887 MagickOffsetType
888 progress;
889
890 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000891 i;
892
cristybb503372010-05-27 20:51:26 +0000893 ssize_t
894 y;
895
cristy3ed852e2009-09-05 21:47:34 +0000896 assert(image != (Image *) NULL);
897 assert(image->signature == MagickSignature);
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
900 sign=sharpen != MagickFalse ? 1 : -1;
901 if (image->storage_class == PseudoClass)
902 {
903 /*
904 Contrast enhance colormap.
905 */
cristybb503372010-05-27 20:51:26 +0000906 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000907 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
908 &image->colormap[i].blue);
909 }
910 /*
911 Contrast enhance image.
912 */
913 status=MagickTrue;
914 progress=0;
cristy3ed852e2009-09-05 21:47:34 +0000915 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000916#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000917 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000918#endif
cristybb503372010-05-27 20:51:26 +0000919 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000920 {
cristy3094b7f2011-10-01 23:18:02 +0000921 double
cristy5afeab82011-04-30 01:30:09 +0000922 blue,
923 green,
924 red;
925
cristy4c08aed2011-07-01 19:47:50 +0000926 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000927 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000928
cristy8d4629b2010-08-30 17:59:46 +0000929 register ssize_t
930 x;
931
cristy3ed852e2009-09-05 21:47:34 +0000932 if (status == MagickFalse)
933 continue;
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000935 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000936 {
937 status=MagickFalse;
938 continue;
939 }
cristybb503372010-05-27 20:51:26 +0000940 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000941 {
cristyda1f9c12011-10-02 21:39:49 +0000942 red=(double) GetPixelRed(image,q);
943 green=(double) GetPixelGreen(image,q);
944 blue=(double) GetPixelBlue(image,q);
cristy5afeab82011-04-30 01:30:09 +0000945 Contrast(sign,&red,&green,&blue);
cristyda1f9c12011-10-02 21:39:49 +0000946 SetPixelRed(image,ClampToQuantum(red),q);
947 SetPixelGreen(image,ClampToQuantum(green),q);
948 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +0000949 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000950 }
951 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
952 status=MagickFalse;
953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
954 {
955 MagickBooleanType
956 proceed;
957
cristyb5d5f722009-11-04 03:03:49 +0000958#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000959 #pragma omp critical (MagickCore_ContrastImage)
cristy3ed852e2009-09-05 21:47:34 +0000960#endif
961 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
962 if (proceed == MagickFalse)
963 status=MagickFalse;
964 }
965 }
966 image_view=DestroyCacheView(image_view);
967 return(status);
968}
969
970/*
971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972% %
973% %
974% %
975% C o n t r a s t S t r e t c h I m a g e %
976% %
977% %
978% %
979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980%
cristyf1611782011-08-01 15:39:13 +0000981% ContrastStretchImage() is a simple image enhancement technique that attempts
982% to improve the contrast in an image by `stretching' the range of intensity
983% values it contains to span a desired range of values. It differs from the
984% more sophisticated histogram equalization in that it can only apply a
985% linear scaling function to the image pixel values. As a result the
986% `enhancement' is less harsh.
cristy3ed852e2009-09-05 21:47:34 +0000987%
988% The format of the ContrastStretchImage method is:
989%
990% MagickBooleanType ContrastStretchImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +0000991% const char *levels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000992%
993% A description of each parameter follows:
994%
995% o image: the image.
996%
cristy3ed852e2009-09-05 21:47:34 +0000997% o black_point: the black point.
998%
999% o white_point: the white point.
1000%
1001% o levels: Specify the levels where the black and white points have the
1002% range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
1003%
cristye23ec9d2011-08-16 18:15:40 +00001004% o exception: return any errors or warnings in this structure.
1005%
cristy3ed852e2009-09-05 21:47:34 +00001006*/
cristy3ed852e2009-09-05 21:47:34 +00001007MagickExport MagickBooleanType ContrastStretchImage(Image *image,
cristye23ec9d2011-08-16 18:15:40 +00001008 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001009{
1010#define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1011#define ContrastStretchImageTag "ContrastStretch/Image"
1012
cristyc4c8d132010-01-07 01:58:38 +00001013 CacheView
1014 *image_view;
1015
cristy3ed852e2009-09-05 21:47:34 +00001016 MagickBooleanType
1017 status;
1018
cristybb503372010-05-27 20:51:26 +00001019 MagickOffsetType
1020 progress;
1021
cristyf45fec72011-08-23 16:02:32 +00001022 double
1023 *black,
cristy3ed852e2009-09-05 21:47:34 +00001024 *histogram,
1025 *stretch_map,
cristyf45fec72011-08-23 16:02:32 +00001026 *white;
cristy3ed852e2009-09-05 21:47:34 +00001027
cristybb503372010-05-27 20:51:26 +00001028 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001029 i;
1030
cristy564a5692012-01-20 23:56:26 +00001031 size_t
1032 number_channels;
1033
cristybb503372010-05-27 20:51:26 +00001034 ssize_t
1035 y;
1036
cristy3ed852e2009-09-05 21:47:34 +00001037 /*
1038 Allocate histogram and stretch map.
1039 */
1040 assert(image != (Image *) NULL);
1041 assert(image->signature == MagickSignature);
1042 if (image->debug != MagickFalse)
1043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyf45fec72011-08-23 16:02:32 +00001044 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1045 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
cristy564a5692012-01-20 23:56:26 +00001046 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
1047 sizeof(*histogram));
cristyf45fec72011-08-23 16:02:32 +00001048 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
cristy465571b2011-08-21 20:43:15 +00001049 GetPixelChannels(image)*sizeof(*stretch_map));
cristyf45fec72011-08-23 16:02:32 +00001050 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1051 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
cristy465571b2011-08-21 20:43:15 +00001052 {
cristyf45fec72011-08-23 16:02:32 +00001053 if (stretch_map != (double *) NULL)
1054 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1055 if (histogram != (double *) NULL)
1056 histogram=(double *) RelinquishMagickMemory(histogram);
1057 if (white != (double *) NULL)
1058 white=(double *) RelinquishMagickMemory(white);
1059 if (black != (double *) NULL)
1060 black=(double *) RelinquishMagickMemory(black);
cristy465571b2011-08-21 20:43:15 +00001061 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1062 image->filename);
1063 }
cristy3ed852e2009-09-05 21:47:34 +00001064 /*
1065 Form histogram.
1066 */
1067 status=MagickTrue;
cristy465571b2011-08-21 20:43:15 +00001068 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1069 sizeof(*histogram));
cristy3ed852e2009-09-05 21:47:34 +00001070 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00001071 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001074 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001075
cristybb503372010-05-27 20:51:26 +00001076 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001077 x;
1078
1079 if (status == MagickFalse)
1080 continue;
1081 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001082 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001083 {
1084 status=MagickFalse;
1085 continue;
1086 }
cristy43547a52011-08-09 13:07:05 +00001087 for (x=0; x < (ssize_t) image->columns; x++)
1088 {
cristy465571b2011-08-21 20:43:15 +00001089 register ssize_t
1090 i;
1091
1092 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy465571b2011-08-21 20:43:15 +00001093 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
cristy43547a52011-08-09 13:07:05 +00001094 p+=GetPixelChannels(image);
1095 }
cristy3ed852e2009-09-05 21:47:34 +00001096 }
1097 /*
1098 Find the histogram boundaries by locating the black/white levels.
1099 */
cristy564a5692012-01-20 23:56:26 +00001100 number_channels=GetPixelChannels(image);
cristyb5d5f722009-11-04 03:03:49 +00001101#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001102 #pragma omp parallel for schedule(static,4) shared(progress,status)
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)
cristye6178502011-12-23 17:02:29 +00001139 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy465571b2011-08-21 20:43:15 +00001140#endif
cristy564a5692012-01-20 23:56:26 +00001141 for (i=0; i < (ssize_t) number_channels; i++)
cristy465571b2011-08-21 20:43:15 +00001142 {
1143 register ssize_t
1144 j;
1145
1146 for (j=0; j <= (ssize_t) MaxMap; j++)
1147 {
1148 if (j < (ssize_t) black[i])
1149 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1150 else
1151 if (j > (ssize_t) white[i])
1152 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1153 QuantumRange;
1154 else
1155 if (black[i] != white[i])
1156 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1157 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1158 (white[i]-black[i])));
1159 }
1160 }
cristy3ed852e2009-09-05 21:47:34 +00001161 if (image->storage_class == PseudoClass)
1162 {
cristy465571b2011-08-21 20:43:15 +00001163 register ssize_t
1164 j;
1165
cristy3ed852e2009-09-05 21:47:34 +00001166 /*
cristy465571b2011-08-21 20:43:15 +00001167 Stretch-contrast colormap.
cristy3ed852e2009-09-05 21:47:34 +00001168 */
cristyb5d5f722009-11-04 03:03:49 +00001169#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001170 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001171#endif
cristy465571b2011-08-21 20:43:15 +00001172 for (j=0; j < (ssize_t) image->colors; j++)
cristy3ed852e2009-09-05 21:47:34 +00001173 {
cristyed231572011-07-14 02:18:59 +00001174 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001175 {
cristy465571b2011-08-21 20:43:15 +00001176 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1177 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001178 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1179 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001180 }
cristyed231572011-07-14 02:18:59 +00001181 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001182 {
cristy465571b2011-08-21 20:43:15 +00001183 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1184 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001185 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1186 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001187 }
cristyed231572011-07-14 02:18:59 +00001188 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001189 {
cristy465571b2011-08-21 20:43:15 +00001190 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1191 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001192 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1193 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001194 }
cristyed231572011-07-14 02:18:59 +00001195 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00001196 {
cristy465571b2011-08-21 20:43:15 +00001197 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1198 if (black[i] != white[i])
cristyda1f9c12011-10-02 21:39:49 +00001199 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1200 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
cristy3ed852e2009-09-05 21:47:34 +00001201 }
1202 }
1203 }
1204 /*
cristy465571b2011-08-21 20:43:15 +00001205 Stretch-contrast image.
cristy3ed852e2009-09-05 21:47:34 +00001206 */
1207 status=MagickTrue;
1208 progress=0;
cristyb5d5f722009-11-04 03:03:49 +00001209#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001210 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001211#endif
cristybb503372010-05-27 20:51:26 +00001212 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001213 {
cristy4c08aed2011-07-01 19:47:50 +00001214 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001215 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001216
cristy8d4629b2010-08-30 17:59:46 +00001217 register ssize_t
1218 x;
1219
cristy3ed852e2009-09-05 21:47:34 +00001220 if (status == MagickFalse)
1221 continue;
1222 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001223 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001224 {
1225 status=MagickFalse;
1226 continue;
1227 }
cristybb503372010-05-27 20:51:26 +00001228 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001229 {
cristy465571b2011-08-21 20:43:15 +00001230 register ssize_t
1231 i;
1232
cristy10a6c612012-01-29 21:41:05 +00001233 if (GetPixelMask(image,q) != 0)
1234 {
1235 q+=GetPixelChannels(image);
1236 continue;
1237 }
cristy465571b2011-08-21 20:43:15 +00001238 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1239 {
cristyabace412011-12-11 15:56:53 +00001240 PixelChannel
1241 channel;
1242
cristy465571b2011-08-21 20:43:15 +00001243 PixelTrait
1244 traits;
1245
cristyabace412011-12-11 15:56:53 +00001246 channel=GetPixelChannelMapChannel(image,i);
1247 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001248 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1249 continue;
1250 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1251 ScaleQuantumToMap(q[i])+i]);
cristy465571b2011-08-21 20:43:15 +00001252 }
cristyed231572011-07-14 02:18:59 +00001253 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001254 }
1255 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1256 status=MagickFalse;
1257 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1258 {
1259 MagickBooleanType
1260 proceed;
1261
cristyb5d5f722009-11-04 03:03:49 +00001262#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001263 #pragma omp critical (MagickCore_ContrastStretchImage)
cristy3ed852e2009-09-05 21:47:34 +00001264#endif
1265 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1266 image->rows);
1267 if (proceed == MagickFalse)
1268 status=MagickFalse;
1269 }
1270 }
1271 image_view=DestroyCacheView(image_view);
cristyf45fec72011-08-23 16:02:32 +00001272 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1273 white=(double *) RelinquishMagickMemory(white);
1274 black=(double *) RelinquishMagickMemory(black);
cristy3ed852e2009-09-05 21:47:34 +00001275 return(status);
1276}
1277
1278/*
1279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280% %
1281% %
1282% %
1283% E n h a n c e I m a g e %
1284% %
1285% %
1286% %
1287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1288%
1289% EnhanceImage() applies a digital filter that improves the quality of a
1290% noisy image.
1291%
1292% The format of the EnhanceImage method is:
1293%
1294% Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1295%
1296% A description of each parameter follows:
1297%
1298% o image: the image.
1299%
1300% o exception: return any errors or warnings in this structure.
1301%
1302*/
1303MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1304{
cristy6d8c3d72011-08-22 01:20:01 +00001305#define EnhancePixel(weight) \
cristy0beccfa2011-09-25 20:47:53 +00001306 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1307 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1308 enhance_image,channel,q); \
cristy3ed852e2009-09-05 21:47:34 +00001309 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
cristy6d8c3d72011-08-22 01:20:01 +00001310 mean)*distance*distance; \
cristy3ed852e2009-09-05 21:47:34 +00001311 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1312 QuantumRange/25.0f)) \
1313 { \
cristy6d8c3d72011-08-22 01:20:01 +00001314 aggregate+=(weight)*r[i]; \
cristy3ed852e2009-09-05 21:47:34 +00001315 total_weight+=(weight); \
1316 } \
cristy6d8c3d72011-08-22 01:20:01 +00001317 r+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001318#define EnhanceImageTag "Enhance/Image"
1319
cristyc4c8d132010-01-07 01:58:38 +00001320 CacheView
1321 *enhance_view,
1322 *image_view;
1323
cristy3ed852e2009-09-05 21:47:34 +00001324 Image
1325 *enhance_image;
1326
cristy3ed852e2009-09-05 21:47:34 +00001327 MagickBooleanType
1328 status;
1329
cristybb503372010-05-27 20:51:26 +00001330 MagickOffsetType
1331 progress;
1332
cristybb503372010-05-27 20:51:26 +00001333 ssize_t
1334 y;
1335
cristy3ed852e2009-09-05 21:47:34 +00001336 /*
1337 Initialize enhanced image attributes.
1338 */
1339 assert(image != (const Image *) NULL);
1340 assert(image->signature == MagickSignature);
1341 if (image->debug != MagickFalse)
1342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1343 assert(exception != (ExceptionInfo *) NULL);
1344 assert(exception->signature == MagickSignature);
cristy3ed852e2009-09-05 21:47:34 +00001345 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1346 exception);
1347 if (enhance_image == (Image *) NULL)
1348 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00001349 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001350 {
cristy3ed852e2009-09-05 21:47:34 +00001351 enhance_image=DestroyImage(enhance_image);
1352 return((Image *) NULL);
1353 }
1354 /*
1355 Enhance image.
1356 */
1357 status=MagickTrue;
1358 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001359 image_view=AcquireCacheView(image);
1360 enhance_view=AcquireCacheView(enhance_image);
cristyb5d5f722009-11-04 03:03:49 +00001361#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001362 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001363#endif
cristybb503372010-05-27 20:51:26 +00001364 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001365 {
cristy4c08aed2011-07-01 19:47:50 +00001366 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001367 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001368
cristy4c08aed2011-07-01 19:47:50 +00001369 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001370 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001371
cristy8d4629b2010-08-30 17:59:46 +00001372 register ssize_t
1373 x;
1374
cristy6d8c3d72011-08-22 01:20:01 +00001375 ssize_t
1376 center;
1377
cristy3ed852e2009-09-05 21:47:34 +00001378 if (status == MagickFalse)
1379 continue;
1380 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1381 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1382 exception);
cristy4c08aed2011-07-01 19:47:50 +00001383 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001384 {
1385 status=MagickFalse;
1386 continue;
1387 }
cristyf45fec72011-08-23 16:02:32 +00001388 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
cristybb503372010-05-27 20:51:26 +00001389 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001390 {
cristy6d8c3d72011-08-22 01:20:01 +00001391 register ssize_t
1392 i;
cristy3ed852e2009-09-05 21:47:34 +00001393
cristy10a6c612012-01-29 21:41:05 +00001394 if (GetPixelMask(image,p) != 0)
1395 {
1396 p+=GetPixelChannels(image);
1397 q+=GetPixelChannels(enhance_image);
1398 continue;
1399 }
cristy6d8c3d72011-08-22 01:20:01 +00001400 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1401 {
1402 MagickRealType
1403 aggregate,
1404 distance,
1405 distance_squared,
1406 mean,
1407 total_weight;
cristy3ed852e2009-09-05 21:47:34 +00001408
cristy6d8c3d72011-08-22 01:20:01 +00001409 PixelChannel
1410 channel;
cristy3ed852e2009-09-05 21:47:34 +00001411
cristy6d8c3d72011-08-22 01:20:01 +00001412 PixelTrait
1413 enhance_traits,
1414 traits;
cristy3ed852e2009-09-05 21:47:34 +00001415
cristy6d8c3d72011-08-22 01:20:01 +00001416 register const Quantum
1417 *restrict r;
1418
cristye2a912b2011-12-05 20:02:07 +00001419 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001420 traits=GetPixelChannelMapTraits(image,channel);
cristy6d8c3d72011-08-22 01:20:01 +00001421 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001422 if ((traits == UndefinedPixelTrait) ||
1423 (enhance_traits == UndefinedPixelTrait))
cristy6d8c3d72011-08-22 01:20:01 +00001424 continue;
cristy0beccfa2011-09-25 20:47:53 +00001425 SetPixelChannel(enhance_image,channel,p[center+i],q);
cristy6d8c3d72011-08-22 01:20:01 +00001426 if ((enhance_traits & CopyPixelTrait) != 0)
1427 continue;
1428 /*
1429 Compute weighted average of target pixel color components.
1430 */
1431 aggregate=0.0;
1432 total_weight=0.0;
cristyf45fec72011-08-23 16:02:32 +00001433 r=p;
cristy6d8c3d72011-08-22 01:20:01 +00001434 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1435 EnhancePixel(8.0); EnhancePixel(5.0);
1436 r=p+1*GetPixelChannels(image)*(image->columns+4);
1437 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1438 EnhancePixel(20.0); EnhancePixel(8.0);
1439 r=p+2*GetPixelChannels(image)*(image->columns+4);
1440 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1441 EnhancePixel(40.0); EnhancePixel(10.0);
1442 r=p+3*GetPixelChannels(image)*(image->columns+4);
1443 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1444 EnhancePixel(20.0); EnhancePixel(8.0);
1445 r=p+4*GetPixelChannels(image)*(image->columns+4);
1446 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1447 EnhancePixel(8.0); EnhancePixel(5.0);
cristy0beccfa2011-09-25 20:47:53 +00001448 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1449 total_weight),q);
cristy6d8c3d72011-08-22 01:20:01 +00001450 }
cristyed231572011-07-14 02:18:59 +00001451 p+=GetPixelChannels(image);
1452 q+=GetPixelChannels(enhance_image);
cristy3ed852e2009-09-05 21:47:34 +00001453 }
1454 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1455 status=MagickFalse;
1456 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1457 {
1458 MagickBooleanType
1459 proceed;
1460
cristyb5d5f722009-11-04 03:03:49 +00001461#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001462 #pragma omp critical (MagickCore_EnhanceImage)
cristy3ed852e2009-09-05 21:47:34 +00001463#endif
1464 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1465 if (proceed == MagickFalse)
1466 status=MagickFalse;
1467 }
1468 }
1469 enhance_view=DestroyCacheView(enhance_view);
1470 image_view=DestroyCacheView(image_view);
1471 return(enhance_image);
1472}
1473
1474/*
1475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1476% %
1477% %
1478% %
1479% E q u a l i z e I m a g e %
1480% %
1481% %
1482% %
1483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484%
1485% EqualizeImage() applies a histogram equalization to the image.
1486%
1487% The format of the EqualizeImage method is:
1488%
cristy6d8c3d72011-08-22 01:20:01 +00001489% MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001490%
1491% A description of each parameter follows:
1492%
1493% o image: the image.
1494%
cristy6d8c3d72011-08-22 01:20:01 +00001495% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00001496%
1497*/
cristy6d8c3d72011-08-22 01:20:01 +00001498MagickExport MagickBooleanType EqualizeImage(Image *image,
1499 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001500{
cristy3ed852e2009-09-05 21:47:34 +00001501#define EqualizeImageTag "Equalize/Image"
1502
cristyc4c8d132010-01-07 01:58:38 +00001503 CacheView
1504 *image_view;
1505
cristy3ed852e2009-09-05 21:47:34 +00001506 MagickBooleanType
1507 status;
1508
cristybb503372010-05-27 20:51:26 +00001509 MagickOffsetType
1510 progress;
1511
cristyf45fec72011-08-23 16:02:32 +00001512 MagickRealType
cristy5f95f4f2011-10-23 01:01:01 +00001513 black[CompositePixelChannel],
cristy3ed852e2009-09-05 21:47:34 +00001514 *equalize_map,
1515 *histogram,
cristy3ed852e2009-09-05 21:47:34 +00001516 *map,
cristy5f95f4f2011-10-23 01:01:01 +00001517 white[CompositePixelChannel];
cristy3ed852e2009-09-05 21:47:34 +00001518
cristybb503372010-05-27 20:51:26 +00001519 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001520 i;
1521
cristy564a5692012-01-20 23:56:26 +00001522 size_t
1523 number_channels;
1524
cristybb503372010-05-27 20:51:26 +00001525 ssize_t
1526 y;
1527
cristy3ed852e2009-09-05 21:47:34 +00001528 /*
1529 Allocate and initialize histogram arrays.
1530 */
1531 assert(image != (Image *) NULL);
1532 assert(image->signature == MagickSignature);
1533 if (image->debug != MagickFalse)
1534 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyf45fec72011-08-23 16:02:32 +00001535 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1536 GetPixelChannels(image)*sizeof(*equalize_map));
1537 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1538 GetPixelChannels(image)*sizeof(*histogram));
1539 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1540 GetPixelChannels(image)*sizeof(*map));
1541 if ((equalize_map == (MagickRealType *) NULL) ||
1542 (histogram == (MagickRealType *) NULL) ||
1543 (map == (MagickRealType *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001544 {
cristyf45fec72011-08-23 16:02:32 +00001545 if (map != (MagickRealType *) NULL)
1546 map=(MagickRealType *) RelinquishMagickMemory(map);
1547 if (histogram != (MagickRealType *) NULL)
1548 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1549 if (equalize_map != (MagickRealType *) NULL)
1550 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
cristy3ed852e2009-09-05 21:47:34 +00001551 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1552 image->filename);
1553 }
1554 /*
1555 Form histogram.
1556 */
cristyf45fec72011-08-23 16:02:32 +00001557 status=MagickTrue;
1558 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1559 sizeof(*histogram));
1560 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00001561 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001562 {
cristy4c08aed2011-07-01 19:47:50 +00001563 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001564 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001565
cristybb503372010-05-27 20:51:26 +00001566 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001567 x;
1568
cristyf45fec72011-08-23 16:02:32 +00001569 if (status == MagickFalse)
1570 continue;
1571 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001572 if (p == (const Quantum *) NULL)
cristyf45fec72011-08-23 16:02:32 +00001573 {
1574 status=MagickFalse;
1575 continue;
1576 }
cristybb503372010-05-27 20:51:26 +00001577 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001578 {
cristyf45fec72011-08-23 16:02:32 +00001579 register ssize_t
1580 i;
1581
1582 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1583 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
cristyed231572011-07-14 02:18:59 +00001584 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001585 }
1586 }
1587 /*
1588 Integrate the histogram to get the equalization map.
1589 */
cristy564a5692012-01-20 23:56:26 +00001590 number_channels=GetPixelChannels(image);
cristyb5d5f722009-11-04 03:03:49 +00001591#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001592 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001593#endif
cristyc94ba6f2012-01-29 23:19:58 +00001594 for (i=0; i < (ssize_t) number_channels; i++)
cristy3ed852e2009-09-05 21:47:34 +00001595 {
cristyf45fec72011-08-23 16:02:32 +00001596 MagickRealType
1597 intensity;
1598
1599 register ssize_t
1600 j;
1601
1602 intensity=0.0;
1603 for (j=0; j <= (ssize_t) MaxMap; j++)
1604 {
1605 intensity+=histogram[GetPixelChannels(image)*j+i];
1606 map[GetPixelChannels(image)*j+i]=intensity;
1607 }
cristy3ed852e2009-09-05 21:47:34 +00001608 }
cristyf45fec72011-08-23 16:02:32 +00001609 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1610 sizeof(*equalize_map));
cristy564a5692012-01-20 23:56:26 +00001611 number_channels=GetPixelChannels(image);
cristyf45fec72011-08-23 16:02:32 +00001612#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001613 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyf45fec72011-08-23 16:02:32 +00001614#endif
cristy564a5692012-01-20 23:56:26 +00001615 for (i=0; i < (ssize_t) number_channels; i++)
cristyf45fec72011-08-23 16:02:32 +00001616 {
1617 register ssize_t
1618 j;
1619
1620 black[i]=map[i];
1621 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1622 if (black[i] != white[i])
1623 for (j=0; j <= (ssize_t) MaxMap; j++)
1624 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1625 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1626 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1627 }
1628 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1629 map=(MagickRealType *) RelinquishMagickMemory(map);
cristy3ed852e2009-09-05 21:47:34 +00001630 if (image->storage_class == PseudoClass)
1631 {
cristyf54798b2011-11-21 18:38:23 +00001632 PixelChannel
1633 channel;
1634
cristyf45fec72011-08-23 16:02:32 +00001635 register ssize_t
1636 j;
1637
cristy3ed852e2009-09-05 21:47:34 +00001638 /*
1639 Equalize colormap.
1640 */
cristyb5d5f722009-11-04 03:03:49 +00001641#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001642 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001643#endif
cristyf45fec72011-08-23 16:02:32 +00001644 for (j=0; j < (ssize_t) image->colors; j++)
cristy3ed852e2009-09-05 21:47:34 +00001645 {
cristyf54798b2011-11-21 18:38:23 +00001646 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001647 {
cristyf54798b2011-11-21 18:38:23 +00001648 channel=GetPixelChannelMapChannel(image,RedPixelChannel);
1649 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001650 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001651 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
1652 channel;
cristyf45fec72011-08-23 16:02:32 +00001653 }
cristyf54798b2011-11-21 18:38:23 +00001654 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001655 {
cristyf54798b2011-11-21 18:38:23 +00001656 channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
1657 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001658 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001659 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
1660 channel;
cristyf45fec72011-08-23 16:02:32 +00001661 }
cristyf54798b2011-11-21 18:38:23 +00001662 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001663 {
cristyf54798b2011-11-21 18:38:23 +00001664 channel=GetPixelChannelMapChannel(image,BluePixelChannel);
1665 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001666 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001667 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
1668 channel;
cristyf45fec72011-08-23 16:02:32 +00001669 }
cristyf54798b2011-11-21 18:38:23 +00001670 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00001671 {
cristyf54798b2011-11-21 18:38:23 +00001672 channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1673 if (black[channel] != white[channel])
cristyda1f9c12011-10-02 21:39:49 +00001674 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
cristyf54798b2011-11-21 18:38:23 +00001675 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
1676 channel;
cristyf45fec72011-08-23 16:02:32 +00001677 }
cristy3ed852e2009-09-05 21:47:34 +00001678 }
1679 }
1680 /*
1681 Equalize image.
1682 */
cristy3ed852e2009-09-05 21:47:34 +00001683 progress=0;
cristyb5d5f722009-11-04 03:03:49 +00001684#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001685 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001686#endif
cristybb503372010-05-27 20:51:26 +00001687 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001688 {
cristy4c08aed2011-07-01 19:47:50 +00001689 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001690 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001691
cristy8d4629b2010-08-30 17:59:46 +00001692 register ssize_t
1693 x;
1694
cristy3ed852e2009-09-05 21:47:34 +00001695 if (status == MagickFalse)
1696 continue;
1697 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001698 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001699 {
1700 status=MagickFalse;
1701 continue;
1702 }
cristybb503372010-05-27 20:51:26 +00001703 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001704 {
cristyf45fec72011-08-23 16:02:32 +00001705 register ssize_t
1706 i;
1707
cristy10a6c612012-01-29 21:41:05 +00001708 if (GetPixelMask(image,q) != 0)
1709 {
1710 q+=GetPixelChannels(image);
1711 continue;
1712 }
cristyf45fec72011-08-23 16:02:32 +00001713 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1714 {
cristyabace412011-12-11 15:56:53 +00001715 PixelChannel
1716 channel;
1717
cristyf45fec72011-08-23 16:02:32 +00001718 PixelTrait
1719 traits;
1720
cristyabace412011-12-11 15:56:53 +00001721 channel=GetPixelChannelMapChannel(image,i);
1722 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001723 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1724 continue;
1725 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1726 ScaleQuantumToMap(q[i])+i]);
cristyf45fec72011-08-23 16:02:32 +00001727 }
cristyed231572011-07-14 02:18:59 +00001728 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001729 }
1730 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1731 status=MagickFalse;
1732 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1733 {
1734 MagickBooleanType
1735 proceed;
1736
cristyb5d5f722009-11-04 03:03:49 +00001737#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001738 #pragma omp critical (MagickCore_EqualizeImage)
cristy3ed852e2009-09-05 21:47:34 +00001739#endif
1740 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1741 if (proceed == MagickFalse)
1742 status=MagickFalse;
1743 }
1744 }
1745 image_view=DestroyCacheView(image_view);
cristyf45fec72011-08-23 16:02:32 +00001746 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
cristy3ed852e2009-09-05 21:47:34 +00001747 return(status);
1748}
1749
1750/*
1751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1752% %
1753% %
1754% %
1755% G a m m a I m a g e %
1756% %
1757% %
1758% %
1759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1760%
1761% GammaImage() gamma-corrects a particular image channel. The same
1762% image viewed on different devices will have perceptual differences in the
1763% way the image's intensities are represented on the screen. Specify
1764% individual gamma levels for the red, green, and blue channels, or adjust
1765% all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1766%
1767% You can also reduce the influence of a particular channel with a gamma
1768% value of 0.
1769%
1770% The format of the GammaImage method is:
1771%
cristyb3e7c6c2011-07-24 01:43:55 +00001772% MagickBooleanType GammaImage(Image *image,const double gamma,
1773% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001774%
1775% A description of each parameter follows:
1776%
1777% o image: the image.
1778%
cristya6360142011-03-23 23:08:04 +00001779% o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1780%
cristy3ed852e2009-09-05 21:47:34 +00001781% o gamma: the image gamma.
1782%
1783*/
cristyb3e7c6c2011-07-24 01:43:55 +00001784MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1785 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001786{
1787#define GammaCorrectImageTag "GammaCorrect/Image"
1788
cristyc4c8d132010-01-07 01:58:38 +00001789 CacheView
1790 *image_view;
1791
cristy3ed852e2009-09-05 21:47:34 +00001792 MagickBooleanType
1793 status;
1794
cristybb503372010-05-27 20:51:26 +00001795 MagickOffsetType
1796 progress;
1797
cristy19593872012-01-22 02:00:33 +00001798 Quantum
1799 *gamma_map;
1800
cristybb503372010-05-27 20:51:26 +00001801 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001802 i;
1803
cristybb503372010-05-27 20:51:26 +00001804 ssize_t
1805 y;
1806
cristy3ed852e2009-09-05 21:47:34 +00001807 /*
1808 Allocate and initialize gamma maps.
1809 */
1810 assert(image != (Image *) NULL);
1811 assert(image->signature == MagickSignature);
1812 if (image->debug != MagickFalse)
1813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1814 if (gamma == 1.0)
1815 return(MagickTrue);
cristy19593872012-01-22 02:00:33 +00001816 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1817 if (gamma_map == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001818 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1819 image->filename);
1820 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1821 if (gamma != 0.0)
cristyd476a8e2011-07-23 16:13:22 +00001822#if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
cristy564a5692012-01-20 23:56:26 +00001823 #pragma omp parallel for
cristy3ed852e2009-09-05 21:47:34 +00001824#endif
cristybb503372010-05-27 20:51:26 +00001825 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy19593872012-01-22 02:00:33 +00001826 gamma_map[i]=ScaleMapToQuantum((MagickRealType) (MaxMap*pow((double) i/
1827 MaxMap,1.0/gamma)));
cristy3ed852e2009-09-05 21:47:34 +00001828 if (image->storage_class == PseudoClass)
1829 {
1830 /*
1831 Gamma-correct colormap.
1832 */
cristyb5d5f722009-11-04 03:03:49 +00001833#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy19593872012-01-22 02:00:33 +00001834 #pragma omp parallel for schedule(static) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001835#endif
cristybb503372010-05-27 20:51:26 +00001836 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001837 {
cristyed231572011-07-14 02:18:59 +00001838 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001839 image->colormap[i].red=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001840 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00001841 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001842 image->colormap[i].green=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001843 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00001844 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001845 image->colormap[i].blue=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001846 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00001847 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyc94ba6f2012-01-29 23:19:58 +00001848 image->colormap[i].alpha=(MagickRealType) gamma_map[
cristyda1f9c12011-10-02 21:39:49 +00001849 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00001850 }
1851 }
1852 /*
1853 Gamma-correct image.
1854 */
1855 status=MagickTrue;
1856 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001857 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001858#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001859 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001860#endif
cristybb503372010-05-27 20:51:26 +00001861 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001862 {
cristy4c08aed2011-07-01 19:47:50 +00001863 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001864 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001865
cristy8d4629b2010-08-30 17:59:46 +00001866 register ssize_t
1867 x;
1868
cristy3ed852e2009-09-05 21:47:34 +00001869 if (status == MagickFalse)
1870 continue;
1871 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001872 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001873 {
1874 status=MagickFalse;
1875 continue;
1876 }
cristybb503372010-05-27 20:51:26 +00001877 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001878 {
cristyd476a8e2011-07-23 16:13:22 +00001879 register ssize_t
1880 i;
1881
cristy10a6c612012-01-29 21:41:05 +00001882 if (GetPixelMask(image,q) != 0)
1883 {
1884 q+=GetPixelChannels(image);
1885 continue;
1886 }
cristya30d9ba2011-07-23 21:00:48 +00001887 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyd476a8e2011-07-23 16:13:22 +00001888 {
cristyabace412011-12-11 15:56:53 +00001889 PixelChannel
1890 channel;
1891
cristyd476a8e2011-07-23 16:13:22 +00001892 PixelTrait
1893 traits;
1894
cristyabace412011-12-11 15:56:53 +00001895 channel=GetPixelChannelMapChannel(image,i);
1896 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00001897 if ((traits & UpdatePixelTrait) == 0)
1898 continue;
1899 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
cristyd476a8e2011-07-23 16:13:22 +00001900 }
cristya30d9ba2011-07-23 21:00:48 +00001901 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001902 }
cristy3ed852e2009-09-05 21:47:34 +00001903 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1904 status=MagickFalse;
1905 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1906 {
1907 MagickBooleanType
1908 proceed;
1909
cristyb5d5f722009-11-04 03:03:49 +00001910#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00001911 #pragma omp critical (MagickCore_GammaImage)
cristy3ed852e2009-09-05 21:47:34 +00001912#endif
1913 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1914 image->rows);
1915 if (proceed == MagickFalse)
1916 status=MagickFalse;
1917 }
1918 }
1919 image_view=DestroyCacheView(image_view);
cristy19593872012-01-22 02:00:33 +00001920 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
cristy3ed852e2009-09-05 21:47:34 +00001921 if (image->gamma != 0.0)
1922 image->gamma*=gamma;
1923 return(status);
1924}
1925
1926/*
1927%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1928% %
1929% %
1930% %
1931% H a l d C l u t I m a g e %
1932% %
1933% %
1934% %
1935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1936%
1937% HaldClutImage() applies a Hald color lookup table to the image. A Hald
1938% color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1939% Create it with the HALD coder. You can apply any color transformation to
1940% the Hald image and then use this method to apply the transform to the
1941% image.
1942%
1943% The format of the HaldClutImage method is:
1944%
cristy7c0a0a42011-08-23 17:57:25 +00001945% MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1946% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001947%
1948% A description of each parameter follows:
1949%
1950% o image: the image, which is replaced by indexed CLUT values
1951%
1952% o hald_image: the color lookup table image for replacement color values.
1953%
cristy7c0a0a42011-08-23 17:57:25 +00001954% o exception: return any errors or warnings in this structure.
1955%
cristy3ed852e2009-09-05 21:47:34 +00001956*/
1957
1958static inline size_t MagickMin(const size_t x,const size_t y)
1959{
1960 if (x < y)
1961 return(x);
1962 return(y);
1963}
1964
1965MagickExport MagickBooleanType HaldClutImage(Image *image,
cristy7c0a0a42011-08-23 17:57:25 +00001966 const Image *hald_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001967{
cristy3ed852e2009-09-05 21:47:34 +00001968#define HaldClutImageTag "Clut/Image"
1969
1970 typedef struct _HaldInfo
1971 {
1972 MagickRealType
1973 x,
1974 y,
1975 z;
1976 } HaldInfo;
1977
cristyfa112112010-01-04 17:48:07 +00001978 CacheView
cristyd551fbc2011-03-31 18:07:46 +00001979 *hald_view,
cristyfa112112010-01-04 17:48:07 +00001980 *image_view;
1981
cristy3ed852e2009-09-05 21:47:34 +00001982 double
1983 width;
1984
cristy3ed852e2009-09-05 21:47:34 +00001985 MagickBooleanType
1986 status;
1987
cristybb503372010-05-27 20:51:26 +00001988 MagickOffsetType
1989 progress;
1990
cristy4c08aed2011-07-01 19:47:50 +00001991 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001992 zero;
1993
cristy3ed852e2009-09-05 21:47:34 +00001994 size_t
1995 cube_size,
1996 length,
1997 level;
1998
cristybb503372010-05-27 20:51:26 +00001999 ssize_t
2000 y;
2001
cristy3ed852e2009-09-05 21:47:34 +00002002 assert(image != (Image *) NULL);
2003 assert(image->signature == MagickSignature);
2004 if (image->debug != MagickFalse)
2005 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2006 assert(hald_image != (Image *) NULL);
2007 assert(hald_image->signature == MagickSignature);
cristy574cc262011-08-05 01:23:58 +00002008 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002009 return(MagickFalse);
2010 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002011 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002012 /*
2013 Hald clut image.
2014 */
2015 status=MagickTrue;
2016 progress=0;
2017 length=MagickMin(hald_image->columns,hald_image->rows);
2018 for (level=2; (level*level*level) < length; level++) ;
2019 level*=level;
2020 cube_size=level*level;
2021 width=(double) hald_image->columns;
cristy4c08aed2011-07-01 19:47:50 +00002022 GetPixelInfo(hald_image,&zero);
cristy3ed852e2009-09-05 21:47:34 +00002023 image_view=AcquireCacheView(image);
cristyd551fbc2011-03-31 18:07:46 +00002024 hald_view=AcquireCacheView(hald_image);
cristyb5d5f722009-11-04 03:03:49 +00002025#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002026 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002027#endif
cristybb503372010-05-27 20:51:26 +00002028 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002029 {
cristy4c08aed2011-07-01 19:47:50 +00002030 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002031 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002032
cristy8d4629b2010-08-30 17:59:46 +00002033 register ssize_t
2034 x;
2035
cristy3ed852e2009-09-05 21:47:34 +00002036 if (status == MagickFalse)
2037 continue;
2038 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002039 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002040 {
2041 status=MagickFalse;
2042 continue;
2043 }
cristybb503372010-05-27 20:51:26 +00002044 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002045 {
cristy7c0a0a42011-08-23 17:57:25 +00002046 double
2047 offset;
2048
2049 HaldInfo
2050 point;
2051
2052 PixelInfo
2053 pixel,
2054 pixel1,
2055 pixel2,
2056 pixel3,
2057 pixel4;
2058
cristy4c08aed2011-07-01 19:47:50 +00002059 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2060 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2061 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002062 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2063 point.x-=floor(point.x);
2064 point.y-=floor(point.y);
2065 point.z-=floor(point.z);
cristy7c0a0a42011-08-23 17:57:25 +00002066 pixel1=zero;
2067 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2068 fmod(offset,width),floor(offset/width),&pixel1,exception);
2069 pixel2=zero;
2070 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2071 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2072 pixel3=zero;
2073 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2074 point.y,&pixel3);
cristy3ed852e2009-09-05 21:47:34 +00002075 offset+=cube_size;
cristy7c0a0a42011-08-23 17:57:25 +00002076 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2077 fmod(offset,width),floor(offset/width),&pixel1,exception);
2078 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2079 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2080 pixel4=zero;
2081 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2082 point.y,&pixel4);
2083 pixel=zero;
2084 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2085 point.z,&pixel);
cristyed231572011-07-14 02:18:59 +00002086 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002087 SetPixelRed(image,ClampToQuantum(pixel.red),q);
cristyed231572011-07-14 02:18:59 +00002088 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002089 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
cristyed231572011-07-14 02:18:59 +00002090 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf45fec72011-08-23 16:02:32 +00002091 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +00002092 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002093 (image->colorspace == CMYKColorspace))
cristyf45fec72011-08-23 16:02:32 +00002094 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2095 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2096 (image->matte != MagickFalse))
2097 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00002098 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002099 }
2100 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2101 status=MagickFalse;
2102 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2103 {
2104 MagickBooleanType
2105 proceed;
2106
cristyb5d5f722009-11-04 03:03:49 +00002107#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002108 #pragma omp critical (MagickCore_HaldClutImage)
cristy3ed852e2009-09-05 21:47:34 +00002109#endif
2110 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2111 if (proceed == MagickFalse)
2112 status=MagickFalse;
2113 }
2114 }
cristyd551fbc2011-03-31 18:07:46 +00002115 hald_view=DestroyCacheView(hald_view);
cristy3ed852e2009-09-05 21:47:34 +00002116 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002117 return(status);
2118}
2119
2120/*
2121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2122% %
2123% %
2124% %
2125% L e v e l I m a g e %
2126% %
2127% %
2128% %
2129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2130%
2131% LevelImage() adjusts the levels of a particular image channel by
2132% scaling the colors falling between specified white and black points to
2133% the full available quantum range.
2134%
2135% The parameters provided represent the black, and white points. The black
2136% point specifies the darkest color in the image. Colors darker than the
2137% black point are set to zero. White point specifies the lightest color in
2138% the image. Colors brighter than the white point are set to the maximum
2139% quantum value.
2140%
2141% If a '!' flag is given, map black and white colors to the given levels
2142% rather than mapping those levels to black and white. See
cristy50fbc382011-07-07 02:19:17 +00002143% LevelizeImage() below.
cristy3ed852e2009-09-05 21:47:34 +00002144%
2145% Gamma specifies a gamma correction to apply to the image.
2146%
2147% The format of the LevelImage method is:
2148%
cristy01e9afd2011-08-10 17:38:41 +00002149% MagickBooleanType LevelImage(Image *image,const double black_point,
2150% const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002151%
2152% A description of each parameter follows:
2153%
2154% o image: the image.
2155%
cristy01e9afd2011-08-10 17:38:41 +00002156% o black_point: The level to map zero (black) to.
2157%
2158% o white_point: The level to map QuantumRange (white) to.
2159%
2160% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002161%
2162*/
cristy780e9ef2011-12-18 23:33:50 +00002163
cristyb8b0d162011-12-18 23:41:28 +00002164static inline MagickRealType LevelPixel(const double black_point,
cristy780e9ef2011-12-18 23:33:50 +00002165 const double white_point,const double gamma,const MagickRealType pixel)
2166{
2167 double
2168 level_pixel,
2169 scale;
2170
2171 if (pixel < black_point)
2172 return(0.0);
2173 if (pixel > white_point)
cristy0113ca22011-12-19 01:16:22 +00002174 return((MagickRealType) QuantumRange);
cristy780e9ef2011-12-18 23:33:50 +00002175 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2176 level_pixel=(MagickRealType) QuantumRange*pow(scale*((double) pixel-
2177 black_point),1.0/gamma);
2178 return(level_pixel);
2179}
2180
cristy7c0a0a42011-08-23 17:57:25 +00002181MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2182 const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002183{
2184#define LevelImageTag "Level/Image"
cristy3ed852e2009-09-05 21:47:34 +00002185
cristyc4c8d132010-01-07 01:58:38 +00002186 CacheView
2187 *image_view;
2188
cristy3ed852e2009-09-05 21:47:34 +00002189 MagickBooleanType
2190 status;
2191
cristybb503372010-05-27 20:51:26 +00002192 MagickOffsetType
2193 progress;
2194
cristy8d4629b2010-08-30 17:59:46 +00002195 register ssize_t
2196 i;
2197
cristybb503372010-05-27 20:51:26 +00002198 ssize_t
2199 y;
2200
cristy3ed852e2009-09-05 21:47:34 +00002201 /*
2202 Allocate and initialize levels map.
2203 */
2204 assert(image != (Image *) NULL);
2205 assert(image->signature == MagickSignature);
2206 if (image->debug != MagickFalse)
2207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2208 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002209#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002210 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002211#endif
cristybb503372010-05-27 20:51:26 +00002212 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002213 {
2214 /*
2215 Level colormap.
2216 */
cristyed231572011-07-14 02:18:59 +00002217 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002218 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2219 white_point,gamma,image->colormap[i].red));
cristyed231572011-07-14 02:18:59 +00002220 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002221 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2222 white_point,gamma,image->colormap[i].green));
cristyed231572011-07-14 02:18:59 +00002223 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002224 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2225 white_point,gamma,image->colormap[i].blue));
cristyed231572011-07-14 02:18:59 +00002226 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy780e9ef2011-12-18 23:33:50 +00002227 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2228 white_point,gamma,image->colormap[i].alpha));
cristy3ed852e2009-09-05 21:47:34 +00002229 }
2230 /*
2231 Level image.
2232 */
2233 status=MagickTrue;
2234 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002235 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00002236#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002237 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002238#endif
cristybb503372010-05-27 20:51:26 +00002239 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002240 {
cristy4c08aed2011-07-01 19:47:50 +00002241 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002242 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002243
cristy8d4629b2010-08-30 17:59:46 +00002244 register ssize_t
2245 x;
2246
cristy3ed852e2009-09-05 21:47:34 +00002247 if (status == MagickFalse)
2248 continue;
2249 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002250 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002251 {
2252 status=MagickFalse;
2253 continue;
2254 }
cristybb503372010-05-27 20:51:26 +00002255 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002256 {
cristy7c0a0a42011-08-23 17:57:25 +00002257 register ssize_t
2258 i;
2259
cristy10a6c612012-01-29 21:41:05 +00002260 if (GetPixelMask(image,q) != 0)
2261 {
2262 q+=GetPixelChannels(image);
2263 continue;
2264 }
cristy7c0a0a42011-08-23 17:57:25 +00002265 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2266 {
cristyabace412011-12-11 15:56:53 +00002267 PixelChannel
2268 channel;
2269
cristy7c0a0a42011-08-23 17:57:25 +00002270 PixelTrait
2271 traits;
2272
cristyabace412011-12-11 15:56:53 +00002273 channel=GetPixelChannelMapChannel(image,i);
2274 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002275 if ((traits & UpdatePixelTrait) == 0)
cristy7c0a0a42011-08-23 17:57:25 +00002276 continue;
cristy780e9ef2011-12-18 23:33:50 +00002277 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2278 (MagickRealType) q[i]));
cristy7c0a0a42011-08-23 17:57:25 +00002279 }
cristyed231572011-07-14 02:18:59 +00002280 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002281 }
2282 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2283 status=MagickFalse;
2284 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2285 {
2286 MagickBooleanType
2287 proceed;
2288
cristyb5d5f722009-11-04 03:03:49 +00002289#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002290 #pragma omp critical (MagickCore_LevelImage)
cristy3ed852e2009-09-05 21:47:34 +00002291#endif
2292 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2293 if (proceed == MagickFalse)
2294 status=MagickFalse;
2295 }
2296 }
2297 image_view=DestroyCacheView(image_view);
2298 return(status);
2299}
2300
2301/*
2302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2303% %
2304% %
2305% %
cristy33bd5152011-08-24 01:42:24 +00002306% L e v e l i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002307% %
2308% %
2309% %
2310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2311%
cristy50fbc382011-07-07 02:19:17 +00002312% LevelizeImage() applies the reversed LevelImage() operation to just
cristy3ed852e2009-09-05 21:47:34 +00002313% the specific channels specified. It compresses the full range of color
2314% values, so that they lie between the given black and white points. Gamma is
2315% applied before the values are mapped.
2316%
cristy50fbc382011-07-07 02:19:17 +00002317% LevelizeImage() can be called with by using a +level command line
cristy3ed852e2009-09-05 21:47:34 +00002318% API option, or using a '!' on a -level or LevelImage() geometry string.
2319%
anthony31f1bf72012-01-30 12:37:22 +00002320% It can be used to de-contrast a greyscale image to the exact levels
2321% specified. Or by using specific levels for each channel of an image you
2322% can convert a gray-scale image to any linear color gradient, according to
2323% those levels.
cristy3ed852e2009-09-05 21:47:34 +00002324%
cristy50fbc382011-07-07 02:19:17 +00002325% The format of the LevelizeImage method is:
cristy3ed852e2009-09-05 21:47:34 +00002326%
cristy50fbc382011-07-07 02:19:17 +00002327% MagickBooleanType LevelizeImage(Image *image,const double black_point,
cristy7c0a0a42011-08-23 17:57:25 +00002328% const double white_point,const double gamma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002329%
2330% A description of each parameter follows:
2331%
2332% o image: the image.
2333%
cristy3ed852e2009-09-05 21:47:34 +00002334% o black_point: The level to map zero (black) to.
2335%
cristy01e9afd2011-08-10 17:38:41 +00002336% o white_point: The level to map QuantumRange (white) to.
cristy3ed852e2009-09-05 21:47:34 +00002337%
2338% o gamma: adjust gamma by this factor before mapping values.
2339%
cristy7c0a0a42011-08-23 17:57:25 +00002340% o exception: return any errors or warnings in this structure.
2341%
cristy3ed852e2009-09-05 21:47:34 +00002342*/
cristyd1a2c0f2011-02-09 14:14:50 +00002343MagickExport MagickBooleanType LevelizeImage(Image *image,
cristy7c0a0a42011-08-23 17:57:25 +00002344 const double black_point,const double white_point,const double gamma,
2345 ExceptionInfo *exception)
cristyd1a2c0f2011-02-09 14:14:50 +00002346{
cristy3ed852e2009-09-05 21:47:34 +00002347#define LevelizeImageTag "Levelize/Image"
cristyce70c172010-01-07 17:15:30 +00002348#define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
cristy50fbc382011-07-07 02:19:17 +00002349 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
cristy3ed852e2009-09-05 21:47:34 +00002350 black_point))
2351
cristyc4c8d132010-01-07 01:58:38 +00002352 CacheView
2353 *image_view;
2354
cristy3ed852e2009-09-05 21:47:34 +00002355 MagickBooleanType
2356 status;
2357
cristybb503372010-05-27 20:51:26 +00002358 MagickOffsetType
2359 progress;
2360
2361 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002362 i;
2363
cristybb503372010-05-27 20:51:26 +00002364 ssize_t
2365 y;
2366
cristy3ed852e2009-09-05 21:47:34 +00002367 /*
2368 Allocate and initialize levels map.
2369 */
2370 assert(image != (Image *) NULL);
2371 assert(image->signature == MagickSignature);
2372 if (image->debug != MagickFalse)
2373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2374 if (image->storage_class == PseudoClass)
cristyb5d5f722009-11-04 03:03:49 +00002375#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristya6d7a9b2012-01-18 20:04:48 +00002376 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002377#endif
cristybb503372010-05-27 20:51:26 +00002378 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002379 {
2380 /*
2381 Level colormap.
2382 */
cristyed231572011-07-14 02:18:59 +00002383 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002384 image->colormap[i].red=(double) LevelizeValue(
2385 image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002386 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002387 image->colormap[i].green=(double) LevelizeValue(
2388 image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002389 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002390 image->colormap[i].blue=(double) LevelizeValue(
2391 image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002392 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00002393 image->colormap[i].alpha=(double) LevelizeValue(
2394 image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002395 }
2396 /*
2397 Level image.
2398 */
2399 status=MagickTrue;
2400 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002401 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00002402#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002403 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002404#endif
cristybb503372010-05-27 20:51:26 +00002405 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002406 {
cristy4c08aed2011-07-01 19:47:50 +00002407 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002408 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002409
cristy8d4629b2010-08-30 17:59:46 +00002410 register ssize_t
2411 x;
2412
cristy3ed852e2009-09-05 21:47:34 +00002413 if (status == MagickFalse)
2414 continue;
2415 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002416 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002417 {
2418 status=MagickFalse;
2419 continue;
2420 }
cristybb503372010-05-27 20:51:26 +00002421 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002422 {
cristy7c0a0a42011-08-23 17:57:25 +00002423 register ssize_t
2424 i;
2425
cristy10a6c612012-01-29 21:41:05 +00002426 if (GetPixelMask(image,q) != 0)
2427 {
2428 q+=GetPixelChannels(image);
2429 continue;
2430 }
cristy7c0a0a42011-08-23 17:57:25 +00002431 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2432 {
cristyabace412011-12-11 15:56:53 +00002433 PixelChannel
2434 channel;
2435
cristy7c0a0a42011-08-23 17:57:25 +00002436 PixelTrait
2437 traits;
2438
cristyabace412011-12-11 15:56:53 +00002439 channel=GetPixelChannelMapChannel(image,i);
2440 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00002441 if ((traits & UpdatePixelTrait) == 0)
cristyb8b0d162011-12-18 23:41:28 +00002442 continue;
cristy780e9ef2011-12-18 23:33:50 +00002443 q[i]=LevelizeValue(q[i]);
cristy7c0a0a42011-08-23 17:57:25 +00002444 }
cristyed231572011-07-14 02:18:59 +00002445 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002446 }
2447 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2448 status=MagickFalse;
2449 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2450 {
2451 MagickBooleanType
2452 proceed;
2453
cristyb5d5f722009-11-04 03:03:49 +00002454#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002455 #pragma omp critical (MagickCore_LevelizeImage)
cristy3ed852e2009-09-05 21:47:34 +00002456#endif
2457 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2458 if (proceed == MagickFalse)
2459 status=MagickFalse;
2460 }
2461 }
cristy8d4629b2010-08-30 17:59:46 +00002462 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002463 return(status);
2464}
2465
2466/*
2467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2468% %
2469% %
2470% %
2471% L e v e l I m a g e C o l o r s %
2472% %
2473% %
2474% %
2475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2476%
cristy490408a2011-07-07 14:42:05 +00002477% LevelImageColors() maps the given color to "black" and "white" values,
cristyee0f8d72009-09-19 00:58:29 +00002478% linearly spreading out the colors, and level values on a channel by channel
2479% bases, as per LevelImage(). The given colors allows you to specify
glennrp1e7f7bc2011-03-02 19:25:28 +00002480% different level ranges for each of the color channels separately.
cristy3ed852e2009-09-05 21:47:34 +00002481%
2482% If the boolean 'invert' is set true the image values will modifyed in the
2483% reverse direction. That is any existing "black" and "white" colors in the
2484% image will become the color values given, with all other values compressed
2485% appropriatally. This effectivally maps a greyscale gradient into the given
2486% color gradient.
2487%
cristy490408a2011-07-07 14:42:05 +00002488% The format of the LevelImageColors method is:
cristy3ed852e2009-09-05 21:47:34 +00002489%
cristy490408a2011-07-07 14:42:05 +00002490% MagickBooleanType LevelImageColors(Image *image,
2491% const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002492% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002493%
2494% A description of each parameter follows:
2495%
2496% o image: the image.
2497%
cristy3ed852e2009-09-05 21:47:34 +00002498% o black_color: The color to map black to/from
2499%
2500% o white_point: The color to map white to/from
2501%
2502% o invert: if true map the colors (levelize), rather than from (level)
2503%
cristy7c0a0a42011-08-23 17:57:25 +00002504% o exception: return any errors or warnings in this structure.
2505%
cristy3ed852e2009-09-05 21:47:34 +00002506*/
cristy490408a2011-07-07 14:42:05 +00002507MagickExport MagickBooleanType LevelImageColors(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00002508 const PixelInfo *black_color,const PixelInfo *white_color,
cristy7c0a0a42011-08-23 17:57:25 +00002509 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002510{
cristybd5a96c2011-08-21 00:04:26 +00002511 ChannelType
2512 channel_mask;
2513
cristy3ed852e2009-09-05 21:47:34 +00002514 MagickStatusType
2515 status;
2516
2517 /*
2518 Allocate and initialize levels map.
2519 */
2520 assert(image != (Image *) NULL);
2521 assert(image->signature == MagickSignature);
2522 if (image->debug != MagickFalse)
2523 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2524 status=MagickFalse;
2525 if (invert == MagickFalse)
2526 {
cristyed231572011-07-14 02:18:59 +00002527 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002528 {
cristybd5a96c2011-08-21 00:04:26 +00002529 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy01e9afd2011-08-10 17:38:41 +00002530 status|=LevelImage(image,black_color->red,white_color->red,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002531 exception);
cristybd5a96c2011-08-21 00:04:26 +00002532 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002533 }
cristyed231572011-07-14 02:18:59 +00002534 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002535 {
cristybd5a96c2011-08-21 00:04:26 +00002536 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy01e9afd2011-08-10 17:38:41 +00002537 status|=LevelImage(image,black_color->green,white_color->green,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002538 exception);
cristybd5a96c2011-08-21 00:04:26 +00002539 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002540 }
cristyed231572011-07-14 02:18:59 +00002541 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002542 {
cristybd5a96c2011-08-21 00:04:26 +00002543 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy01e9afd2011-08-10 17:38:41 +00002544 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002545 exception);
cristybd5a96c2011-08-21 00:04:26 +00002546 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002547 }
cristyed231572011-07-14 02:18:59 +00002548 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002549 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002550 {
cristybd5a96c2011-08-21 00:04:26 +00002551 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy01e9afd2011-08-10 17:38:41 +00002552 status|=LevelImage(image,black_color->black,white_color->black,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002553 exception);
cristybd5a96c2011-08-21 00:04:26 +00002554 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002555 }
cristyed231572011-07-14 02:18:59 +00002556 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002557 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002558 {
cristybd5a96c2011-08-21 00:04:26 +00002559 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy01e9afd2011-08-10 17:38:41 +00002560 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
cristy7c0a0a42011-08-23 17:57:25 +00002561 exception);
cristybd5a96c2011-08-21 00:04:26 +00002562 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002563 }
cristy3ed852e2009-09-05 21:47:34 +00002564 }
2565 else
2566 {
cristyed231572011-07-14 02:18:59 +00002567 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002568 {
cristybd5a96c2011-08-21 00:04:26 +00002569 channel_mask=SetPixelChannelMask(image,RedChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002570 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2571 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 ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002575 {
cristybd5a96c2011-08-21 00:04:26 +00002576 channel_mask=SetPixelChannelMask(image,GreenChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002577 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2578 exception);
cristybd5a96c2011-08-21 00:04:26 +00002579 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002580 }
cristyed231572011-07-14 02:18:59 +00002581 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyf89cb1d2011-07-07 01:24:37 +00002582 {
cristybd5a96c2011-08-21 00:04:26 +00002583 channel_mask=SetPixelChannelMask(image,BlueChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002584 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2585 exception);
cristybd5a96c2011-08-21 00:04:26 +00002586 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002587 }
cristyed231572011-07-14 02:18:59 +00002588 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002589 (image->colorspace == CMYKColorspace))
cristyf89cb1d2011-07-07 01:24:37 +00002590 {
cristybd5a96c2011-08-21 00:04:26 +00002591 channel_mask=SetPixelChannelMask(image,BlackChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002592 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2593 exception);
cristybd5a96c2011-08-21 00:04:26 +00002594 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002595 }
cristyed231572011-07-14 02:18:59 +00002596 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00002597 (image->matte == MagickTrue))
cristyf89cb1d2011-07-07 01:24:37 +00002598 {
cristybd5a96c2011-08-21 00:04:26 +00002599 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristy7c0a0a42011-08-23 17:57:25 +00002600 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2601 exception);
cristybd5a96c2011-08-21 00:04:26 +00002602 (void) SetPixelChannelMask(image,channel_mask);
cristyf89cb1d2011-07-07 01:24:37 +00002603 }
cristy3ed852e2009-09-05 21:47:34 +00002604 }
2605 return(status == 0 ? MagickFalse : MagickTrue);
2606}
2607
2608/*
2609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2610% %
2611% %
2612% %
2613% L i n e a r S t r e t c h I m a g e %
2614% %
2615% %
2616% %
2617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2618%
cristyf1611782011-08-01 15:39:13 +00002619% LinearStretchImage() discards any pixels below the black point and above
2620% the white point and levels the remaining pixels.
cristy3ed852e2009-09-05 21:47:34 +00002621%
2622% The format of the LinearStretchImage method is:
2623%
2624% MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002625% const double black_point,const double white_point,
2626% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002627%
2628% A description of each parameter follows:
2629%
2630% o image: the image.
2631%
2632% o black_point: the black point.
2633%
2634% o white_point: the white point.
2635%
cristy33bd5152011-08-24 01:42:24 +00002636% o exception: return any errors or warnings in this structure.
2637%
cristy3ed852e2009-09-05 21:47:34 +00002638*/
2639MagickExport MagickBooleanType LinearStretchImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00002640 const double black_point,const double white_point,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002641{
2642#define LinearStretchImageTag "LinearStretch/Image"
2643
cristy33bd5152011-08-24 01:42:24 +00002644 CacheView
2645 *image_view;
cristy3ed852e2009-09-05 21:47:34 +00002646
cristy3ed852e2009-09-05 21:47:34 +00002647 MagickBooleanType
2648 status;
2649
2650 MagickRealType
2651 *histogram,
2652 intensity;
2653
cristy8d4629b2010-08-30 17:59:46 +00002654 ssize_t
2655 black,
2656 white,
2657 y;
2658
cristy3ed852e2009-09-05 21:47:34 +00002659 /*
2660 Allocate histogram and linear map.
2661 */
2662 assert(image != (Image *) NULL);
2663 assert(image->signature == MagickSignature);
2664 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2665 sizeof(*histogram));
2666 if (histogram == (MagickRealType *) NULL)
2667 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2668 image->filename);
2669 /*
2670 Form histogram.
2671 */
2672 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
cristy33bd5152011-08-24 01:42:24 +00002673 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00002674 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002675 {
cristy4c08aed2011-07-01 19:47:50 +00002676 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002677 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002678
cristybb503372010-05-27 20:51:26 +00002679 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002680 x;
2681
cristy33bd5152011-08-24 01:42:24 +00002682 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002683 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002684 break;
cristy33bd5152011-08-24 01:42:24 +00002685 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002686 {
cristy4c08aed2011-07-01 19:47:50 +00002687 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
cristyed231572011-07-14 02:18:59 +00002688 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002689 }
2690 }
cristy33bd5152011-08-24 01:42:24 +00002691 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002692 /*
2693 Find the histogram boundaries by locating the black and white point levels.
2694 */
cristy3ed852e2009-09-05 21:47:34 +00002695 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002696 for (black=0; black < (ssize_t) MaxMap; black++)
cristy3ed852e2009-09-05 21:47:34 +00002697 {
2698 intensity+=histogram[black];
2699 if (intensity >= black_point)
2700 break;
2701 }
2702 intensity=0.0;
cristybb503372010-05-27 20:51:26 +00002703 for (white=(ssize_t) MaxMap; white != 0; white--)
cristy3ed852e2009-09-05 21:47:34 +00002704 {
2705 intensity+=histogram[white];
2706 if (intensity >= white_point)
2707 break;
2708 }
2709 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
cristyc82a27b2011-10-21 01:07:16 +00002710 status=LevelImage(image,(double) black,(double) white,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00002711 return(status);
2712}
2713
2714/*
2715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2716% %
2717% %
2718% %
2719% M o d u l a t e I m a g e %
2720% %
2721% %
2722% %
2723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2724%
2725% ModulateImage() lets you control the brightness, saturation, and hue
2726% of an image. Modulate represents the brightness, saturation, and hue
2727% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2728% modulation is lightness, saturation, and hue. And if the colorspace is
2729% HWB, use blackness, whiteness, and hue.
2730%
2731% The format of the ModulateImage method is:
2732%
cristy33bd5152011-08-24 01:42:24 +00002733% MagickBooleanType ModulateImage(Image *image,const char *modulate,
2734% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002735%
2736% A description of each parameter follows:
2737%
2738% o image: the image.
2739%
cristy33bd5152011-08-24 01:42:24 +00002740% o modulate: Define the percent change in brightness, saturation, and hue.
2741%
2742% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00002743%
2744*/
2745
2746static void ModulateHSB(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002747 const double percent_saturation,const double percent_brightness,double *red,
2748 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002749{
2750 double
2751 brightness,
2752 hue,
2753 saturation;
2754
2755 /*
2756 Increase or decrease color brightness, saturation, or hue.
2757 */
cristy3094b7f2011-10-01 23:18:02 +00002758 assert(red != (double *) NULL);
2759 assert(green != (double *) NULL);
2760 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002761 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2762 hue+=0.5*(0.01*percent_hue-1.0);
2763 while (hue < 0.0)
2764 hue+=1.0;
2765 while (hue > 1.0)
2766 hue-=1.0;
2767 saturation*=0.01*percent_saturation;
2768 brightness*=0.01*percent_brightness;
2769 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2770}
2771
2772static void ModulateHSL(const double percent_hue,
cristy3094b7f2011-10-01 23:18:02 +00002773 const double percent_saturation,const double percent_lightness,double *red,
2774 double *green,double *blue)
cristy3ed852e2009-09-05 21:47:34 +00002775{
2776 double
2777 hue,
2778 lightness,
2779 saturation;
2780
2781 /*
2782 Increase or decrease color lightness, saturation, or hue.
2783 */
cristy3094b7f2011-10-01 23:18:02 +00002784 assert(red != (double *) NULL);
2785 assert(green != (double *) NULL);
2786 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002787 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2788 hue+=0.5*(0.01*percent_hue-1.0);
2789 while (hue < 0.0)
2790 hue+=1.0;
2791 while (hue > 1.0)
2792 hue-=1.0;
2793 saturation*=0.01*percent_saturation;
2794 lightness*=0.01*percent_lightness;
2795 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2796}
2797
cristy3094b7f2011-10-01 23:18:02 +00002798static 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 +00002799{
2800 double
2801 blackness,
2802 hue,
2803 whiteness;
2804
2805 /*
2806 Increase or decrease color blackness, whiteness, or hue.
2807 */
cristy3094b7f2011-10-01 23:18:02 +00002808 assert(red != (double *) NULL);
2809 assert(green != (double *) NULL);
2810 assert(blue != (double *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002811 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2812 hue+=0.5*(0.01*percent_hue-1.0);
2813 while (hue < 0.0)
2814 hue+=1.0;
2815 while (hue > 1.0)
2816 hue-=1.0;
2817 blackness*=0.01*percent_blackness;
2818 whiteness*=0.01*percent_whiteness;
2819 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2820}
2821
cristy33bd5152011-08-24 01:42:24 +00002822MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2823 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002824{
2825#define ModulateImageTag "Modulate/Image"
2826
cristyc4c8d132010-01-07 01:58:38 +00002827 CacheView
2828 *image_view;
2829
cristy3ed852e2009-09-05 21:47:34 +00002830 ColorspaceType
2831 colorspace;
2832
2833 const char
2834 *artifact;
2835
2836 double
2837 percent_brightness,
2838 percent_hue,
2839 percent_saturation;
2840
cristy3ed852e2009-09-05 21:47:34 +00002841 GeometryInfo
2842 geometry_info;
2843
cristy3ed852e2009-09-05 21:47:34 +00002844 MagickBooleanType
2845 status;
2846
cristybb503372010-05-27 20:51:26 +00002847 MagickOffsetType
2848 progress;
2849
cristy3ed852e2009-09-05 21:47:34 +00002850 MagickStatusType
2851 flags;
2852
cristybb503372010-05-27 20:51:26 +00002853 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002854 i;
2855
cristybb503372010-05-27 20:51:26 +00002856 ssize_t
2857 y;
2858
cristy3ed852e2009-09-05 21:47:34 +00002859 /*
cristy2b726bd2010-01-11 01:05:39 +00002860 Initialize modulate table.
cristy3ed852e2009-09-05 21:47:34 +00002861 */
2862 assert(image != (Image *) NULL);
2863 assert(image->signature == MagickSignature);
2864 if (image->debug != MagickFalse)
2865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2866 if (modulate == (char *) NULL)
2867 return(MagickFalse);
2868 flags=ParseGeometry(modulate,&geometry_info);
2869 percent_brightness=geometry_info.rho;
2870 percent_saturation=geometry_info.sigma;
2871 if ((flags & SigmaValue) == 0)
2872 percent_saturation=100.0;
2873 percent_hue=geometry_info.xi;
2874 if ((flags & XiValue) == 0)
2875 percent_hue=100.0;
2876 colorspace=UndefinedColorspace;
2877 artifact=GetImageArtifact(image,"modulate:colorspace");
2878 if (artifact != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00002879 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +00002880 MagickFalse,artifact);
2881 if (image->storage_class == PseudoClass)
2882 {
2883 /*
2884 Modulate colormap.
2885 */
cristyb5d5f722009-11-04 03:03:49 +00002886#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002887 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002888#endif
cristybb503372010-05-27 20:51:26 +00002889 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002890 switch (colorspace)
2891 {
2892 case HSBColorspace:
2893 {
2894 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2895 &image->colormap[i].red,&image->colormap[i].green,
2896 &image->colormap[i].blue);
2897 break;
2898 }
2899 case HSLColorspace:
2900 default:
2901 {
2902 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2903 &image->colormap[i].red,&image->colormap[i].green,
2904 &image->colormap[i].blue);
2905 break;
2906 }
2907 case HWBColorspace:
2908 {
2909 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2910 &image->colormap[i].red,&image->colormap[i].green,
2911 &image->colormap[i].blue);
2912 break;
2913 }
2914 }
2915 }
2916 /*
2917 Modulate image.
2918 */
2919 status=MagickTrue;
2920 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00002921 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00002922#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002923 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00002924#endif
cristybb503372010-05-27 20:51:26 +00002925 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002926 {
cristy3094b7f2011-10-01 23:18:02 +00002927 double
cristy5afeab82011-04-30 01:30:09 +00002928 blue,
2929 green,
2930 red;
2931
cristy4c08aed2011-07-01 19:47:50 +00002932 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00002933 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00002934
cristy8d4629b2010-08-30 17:59:46 +00002935 register ssize_t
2936 x;
2937
cristy3ed852e2009-09-05 21:47:34 +00002938 if (status == MagickFalse)
2939 continue;
2940 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002941 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002942 {
2943 status=MagickFalse;
2944 continue;
2945 }
cristybb503372010-05-27 20:51:26 +00002946 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002947 {
cristyda1f9c12011-10-02 21:39:49 +00002948 red=(double) GetPixelRed(image,q);
2949 green=(double) GetPixelGreen(image,q);
2950 blue=(double) GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00002951 switch (colorspace)
2952 {
2953 case HSBColorspace:
2954 {
2955 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002956 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002957 break;
2958 }
2959 case HSLColorspace:
2960 default:
2961 {
2962 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002963 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002964 break;
2965 }
2966 case HWBColorspace:
2967 {
2968 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
cristy5afeab82011-04-30 01:30:09 +00002969 &red,&green,&blue);
cristy3ed852e2009-09-05 21:47:34 +00002970 break;
2971 }
2972 }
cristy3094b7f2011-10-01 23:18:02 +00002973 SetPixelRed(image,ClampToQuantum(red),q);
2974 SetPixelGreen(image,ClampToQuantum(green),q);
2975 SetPixelBlue(image,ClampToQuantum(blue),q);
cristyed231572011-07-14 02:18:59 +00002976 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002977 }
2978 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2979 status=MagickFalse;
2980 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2981 {
2982 MagickBooleanType
2983 proceed;
2984
cristyb5d5f722009-11-04 03:03:49 +00002985#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00002986 #pragma omp critical (MagickCore_ModulateImage)
cristy3ed852e2009-09-05 21:47:34 +00002987#endif
2988 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
2989 if (proceed == MagickFalse)
2990 status=MagickFalse;
2991 }
2992 }
2993 image_view=DestroyCacheView(image_view);
2994 return(status);
2995}
2996
2997/*
2998%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2999% %
3000% %
3001% %
3002% N e g a t e I m a g e %
3003% %
3004% %
3005% %
3006%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3007%
3008% NegateImage() negates the colors in the reference image. The grayscale
3009% option means that only grayscale values within the image are negated.
3010%
cristy50fbc382011-07-07 02:19:17 +00003011% The format of the NegateImage method is:
cristy3ed852e2009-09-05 21:47:34 +00003012%
3013% MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003014% const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003015%
3016% A description of each parameter follows:
3017%
3018% o image: the image.
3019%
cristy3ed852e2009-09-05 21:47:34 +00003020% o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3021%
cristyb3e7c6c2011-07-24 01:43:55 +00003022% o exception: return any errors or warnings in this structure.
3023%
cristy3ed852e2009-09-05 21:47:34 +00003024*/
cristy3ed852e2009-09-05 21:47:34 +00003025MagickExport MagickBooleanType NegateImage(Image *image,
cristyb3e7c6c2011-07-24 01:43:55 +00003026 const MagickBooleanType grayscale,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003027{
cristy3ed852e2009-09-05 21:47:34 +00003028#define NegateImageTag "Negate/Image"
3029
cristyc4c8d132010-01-07 01:58:38 +00003030 CacheView
3031 *image_view;
3032
cristy3ed852e2009-09-05 21:47:34 +00003033 MagickBooleanType
3034 status;
3035
cristybb503372010-05-27 20:51:26 +00003036 MagickOffsetType
3037 progress;
3038
3039 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003040 i;
3041
cristybb503372010-05-27 20:51:26 +00003042 ssize_t
3043 y;
3044
cristy3ed852e2009-09-05 21:47:34 +00003045 assert(image != (Image *) NULL);
3046 assert(image->signature == MagickSignature);
3047 if (image->debug != MagickFalse)
3048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3049 if (image->storage_class == PseudoClass)
3050 {
3051 /*
3052 Negate colormap.
3053 */
cristyb5d5f722009-11-04 03:03:49 +00003054#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003055 #pragma omp parallel for schedule(static) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003056#endif
cristybb503372010-05-27 20:51:26 +00003057 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003058 {
3059 if (grayscale != MagickFalse)
3060 if ((image->colormap[i].red != image->colormap[i].green) ||
3061 (image->colormap[i].green != image->colormap[i].blue))
3062 continue;
cristyed231572011-07-14 02:18:59 +00003063 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003064 image->colormap[i].red=(Quantum) QuantumRange-
3065 image->colormap[i].red;
cristyed231572011-07-14 02:18:59 +00003066 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003067 image->colormap[i].green=(Quantum) QuantumRange-
3068 image->colormap[i].green;
cristyed231572011-07-14 02:18:59 +00003069 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003070 image->colormap[i].blue=(Quantum) QuantumRange-
3071 image->colormap[i].blue;
3072 }
3073 }
3074 /*
3075 Negate image.
3076 */
3077 status=MagickTrue;
3078 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003079 image_view=AcquireCacheView(image);
3080 if (grayscale != MagickFalse)
3081 {
cristyb5d5f722009-11-04 03:03:49 +00003082#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003083 #pragma omp parallel for schedule(static) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003084#endif
cristybb503372010-05-27 20:51:26 +00003085 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003086 {
3087 MagickBooleanType
3088 sync;
3089
cristy4c08aed2011-07-01 19:47:50 +00003090 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003091 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003092
cristy8d4629b2010-08-30 17:59:46 +00003093 register ssize_t
3094 x;
3095
cristy3ed852e2009-09-05 21:47:34 +00003096 if (status == MagickFalse)
3097 continue;
3098 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3099 exception);
cristyacd2ed22011-08-30 01:44:23 +00003100 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003101 {
3102 status=MagickFalse;
3103 continue;
3104 }
cristybb503372010-05-27 20:51:26 +00003105 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003106 {
cristy9aa95be2011-07-20 21:56:45 +00003107 register ssize_t
3108 i;
3109
cristy10a6c612012-01-29 21:41:05 +00003110 if ((GetPixelMask(image,q) != 0) ||
3111 (IsPixelGray(image,q) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00003112 {
cristya30d9ba2011-07-23 21:00:48 +00003113 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003114 continue;
3115 }
cristya30d9ba2011-07-23 21:00:48 +00003116 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy9aa95be2011-07-20 21:56:45 +00003117 {
cristyabace412011-12-11 15:56:53 +00003118 PixelChannel
3119 channel;
3120
cristy1aaa3cd2011-08-21 23:48:17 +00003121 PixelTrait
cristy9aa95be2011-07-20 21:56:45 +00003122 traits;
3123
cristyabace412011-12-11 15:56:53 +00003124 channel=GetPixelChannelMapChannel(image,i);
3125 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003126 if ((traits & UpdatePixelTrait) == 0)
3127 continue;
3128 q[i]=QuantumRange-q[i];
cristy9aa95be2011-07-20 21:56:45 +00003129 }
cristya30d9ba2011-07-23 21:00:48 +00003130 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003131 }
3132 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3133 if (sync == MagickFalse)
3134 status=MagickFalse;
3135 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3136 {
3137 MagickBooleanType
3138 proceed;
3139
cristyb5d5f722009-11-04 03:03:49 +00003140#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003141 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003142#endif
3143 proceed=SetImageProgress(image,NegateImageTag,progress++,
3144 image->rows);
3145 if (proceed == MagickFalse)
3146 status=MagickFalse;
3147 }
3148 }
3149 image_view=DestroyCacheView(image_view);
3150 return(MagickTrue);
3151 }
3152 /*
3153 Negate image.
3154 */
cristyb5d5f722009-11-04 03:03:49 +00003155#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003156 #pragma omp parallel for schedule(static) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003157#endif
cristybb503372010-05-27 20:51:26 +00003158 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003159 {
cristy4c08aed2011-07-01 19:47:50 +00003160 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003161 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003162
cristy8d4629b2010-08-30 17:59:46 +00003163 register ssize_t
3164 x;
3165
cristy3ed852e2009-09-05 21:47:34 +00003166 if (status == MagickFalse)
3167 continue;
3168 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003169 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003170 {
3171 status=MagickFalse;
3172 continue;
3173 }
cristybb503372010-05-27 20:51:26 +00003174 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003175 {
cristyf7dc44c2011-07-20 14:41:15 +00003176 register ssize_t
3177 i;
3178
cristyd09f8802012-02-04 16:44:10 +00003179 if (GetPixelMask(image,q) != 0)
3180 {
3181 q+=GetPixelChannels(image);
3182 continue;
3183 }
cristya30d9ba2011-07-23 21:00:48 +00003184 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyf7dc44c2011-07-20 14:41:15 +00003185 {
cristyabace412011-12-11 15:56:53 +00003186 PixelChannel
3187 channel;
3188
cristy1aaa3cd2011-08-21 23:48:17 +00003189 PixelTrait
cristyf7dc44c2011-07-20 14:41:15 +00003190 traits;
3191
cristyabace412011-12-11 15:56:53 +00003192 channel=GetPixelChannelMapChannel(image,i);
3193 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003194 if ((traits & UpdatePixelTrait) == 0)
cristyec9e3a62012-02-01 02:09:32 +00003195 continue;
3196 q[i]=QuantumRange-q[i];
cristyf7dc44c2011-07-20 14:41:15 +00003197 }
cristya30d9ba2011-07-23 21:00:48 +00003198 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003199 }
3200 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3201 status=MagickFalse;
3202 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3203 {
3204 MagickBooleanType
3205 proceed;
3206
cristyb5d5f722009-11-04 03:03:49 +00003207#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003208 #pragma omp critical (MagickCore_NegateImage)
cristy3ed852e2009-09-05 21:47:34 +00003209#endif
3210 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3211 if (proceed == MagickFalse)
3212 status=MagickFalse;
3213 }
3214 }
3215 image_view=DestroyCacheView(image_view);
3216 return(status);
3217}
3218
3219/*
3220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3221% %
3222% %
3223% %
3224% N o r m a l i z e I m a g e %
3225% %
3226% %
3227% %
3228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3229%
cristya4dfb122011-07-07 19:01:57 +00003230% NormalizeImage() enhances the contrast of a color image by mapping the
3231% darkest 2 percent of all pixel to black and the brightest 1 percent to white.
cristy3ed852e2009-09-05 21:47:34 +00003232%
3233% The format of the NormalizeImage method is:
3234%
cristye23ec9d2011-08-16 18:15:40 +00003235% MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003236%
3237% A description of each parameter follows:
3238%
3239% o image: the image.
3240%
cristye23ec9d2011-08-16 18:15:40 +00003241% o exception: return any errors or warnings in this structure.
3242%
cristy3ed852e2009-09-05 21:47:34 +00003243*/
cristye23ec9d2011-08-16 18:15:40 +00003244MagickExport MagickBooleanType NormalizeImage(Image *image,
3245 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003246{
cristy3ed852e2009-09-05 21:47:34 +00003247 double
3248 black_point,
3249 white_point;
3250
cristy530239c2010-07-25 17:34:26 +00003251 black_point=(double) image->columns*image->rows*0.0015;
3252 white_point=(double) image->columns*image->rows*0.9995;
cristye23ec9d2011-08-16 18:15:40 +00003253 return(ContrastStretchImage(image,black_point,white_point,exception));
cristy3ed852e2009-09-05 21:47:34 +00003254}
3255
3256/*
3257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3258% %
3259% %
3260% %
3261% S i g m o i d a l C o n t r a s t I m a g e %
3262% %
3263% %
3264% %
3265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3266%
3267% SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3268% sigmoidal contrast algorithm. Increase the contrast of the image using a
3269% sigmoidal transfer function without saturating highlights or shadows.
3270% Contrast indicates how much to increase the contrast (0 is none; 3 is
anthony31f1bf72012-01-30 12:37:22 +00003271% typical; 20 is pushing it); mid-point indicates where threshold 'knee' of
3272% the curve falls (typical 50% for mid-gray). Set sharpen to MagickTrue to
3273% increase the image contrast otherwise the contrast is reduced.
cristy3ed852e2009-09-05 21:47:34 +00003274%
3275% The format of the SigmoidalContrastImage method is:
3276%
3277% MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003278% const MagickBooleanType sharpen,const char *levels,
3279% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003280%
3281% A description of each parameter follows:
3282%
3283% o image: the image.
3284%
cristy3ed852e2009-09-05 21:47:34 +00003285% o sharpen: Increase or decrease image contrast.
3286%
cristyfa769582010-09-30 23:30:03 +00003287% o alpha: strength of the contrast, the larger the number the more
3288% 'threshold-like' it becomes.
cristy3ed852e2009-09-05 21:47:34 +00003289%
cristyfa769582010-09-30 23:30:03 +00003290% o beta: midpoint of the function as a color value 0 to QuantumRange.
cristy3ed852e2009-09-05 21:47:34 +00003291%
cristy33bd5152011-08-24 01:42:24 +00003292% o exception: return any errors or warnings in this structure.
3293%
cristy3ed852e2009-09-05 21:47:34 +00003294*/
cristy3ed852e2009-09-05 21:47:34 +00003295MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
cristy33bd5152011-08-24 01:42:24 +00003296 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3297 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003298{
3299#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3300
cristyc4c8d132010-01-07 01:58:38 +00003301 CacheView
3302 *image_view;
3303
cristy3ed852e2009-09-05 21:47:34 +00003304 MagickBooleanType
3305 status;
3306
cristybb503372010-05-27 20:51:26 +00003307 MagickOffsetType
3308 progress;
3309
cristy3ed852e2009-09-05 21:47:34 +00003310 MagickRealType
3311 *sigmoidal_map;
3312
cristybb503372010-05-27 20:51:26 +00003313 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003314 i;
3315
cristybb503372010-05-27 20:51:26 +00003316 ssize_t
3317 y;
3318
cristy3ed852e2009-09-05 21:47:34 +00003319 /*
3320 Allocate and initialize sigmoidal maps.
3321 */
3322 assert(image != (Image *) NULL);
3323 assert(image->signature == MagickSignature);
3324 if (image->debug != MagickFalse)
3325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3326 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3327 sizeof(*sigmoidal_map));
3328 if (sigmoidal_map == (MagickRealType *) NULL)
3329 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3330 image->filename);
3331 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
cristyb5d5f722009-11-04 03:03:49 +00003332#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003333 #pragma omp parallel for schedule(static) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003334#endif
cristybb503372010-05-27 20:51:26 +00003335 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003336 {
3337 if (sharpen != MagickFalse)
3338 {
3339 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3340 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
cristy33bd5152011-08-24 01:42:24 +00003341 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3342 QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/(double)
3343 QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3344 QuantumRange)))))+0.5));
cristy3ed852e2009-09-05 21:47:34 +00003345 continue;
3346 }
3347 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
cristy33bd5152011-08-24 01:42:24 +00003348 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/(double)
3349 QuantumRange*contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(
3350 midpoint/(double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double)
3351 QuantumRange*contrast))))))/(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3352 contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/(double)
3353 QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3354 contrast))))))/contrast)));
cristy3ed852e2009-09-05 21:47:34 +00003355 }
3356 if (image->storage_class == PseudoClass)
3357 {
3358 /*
3359 Sigmoidal-contrast enhance colormap.
3360 */
cristyb5d5f722009-11-04 03:03:49 +00003361#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003362 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003363#endif
cristybb503372010-05-27 20:51:26 +00003364 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003365 {
cristyed231572011-07-14 02:18:59 +00003366 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003367 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3368 ClampToQuantum(image->colormap[i].red))];
cristyed231572011-07-14 02:18:59 +00003369 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003370 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3371 ClampToQuantum(image->colormap[i].green))];
cristyed231572011-07-14 02:18:59 +00003372 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003373 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3374 ClampToQuantum(image->colormap[i].blue))];
cristyed231572011-07-14 02:18:59 +00003375 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristyda1f9c12011-10-02 21:39:49 +00003376 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3377 ClampToQuantum(image->colormap[i].alpha))];
cristy3ed852e2009-09-05 21:47:34 +00003378 }
3379 }
3380 /*
3381 Sigmoidal-contrast enhance image.
3382 */
3383 status=MagickTrue;
3384 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003385 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003386#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003387 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003388#endif
cristybb503372010-05-27 20:51:26 +00003389 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003390 {
cristy4c08aed2011-07-01 19:47:50 +00003391 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003392 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003393
cristy8d4629b2010-08-30 17:59:46 +00003394 register ssize_t
3395 x;
3396
cristy3ed852e2009-09-05 21:47:34 +00003397 if (status == MagickFalse)
3398 continue;
3399 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003400 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003401 {
3402 status=MagickFalse;
3403 continue;
3404 }
cristybb503372010-05-27 20:51:26 +00003405 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003406 {
cristy33bd5152011-08-24 01:42:24 +00003407 register ssize_t
3408 i;
3409
cristy10a6c612012-01-29 21:41:05 +00003410 if (GetPixelMask(image,q) != 0)
3411 {
3412 q+=GetPixelChannels(image);
3413 continue;
3414 }
cristy33bd5152011-08-24 01:42:24 +00003415 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3416 {
cristyabace412011-12-11 15:56:53 +00003417 PixelChannel
3418 channel;
3419
cristy33bd5152011-08-24 01:42:24 +00003420 PixelTrait
3421 traits;
3422
cristyabace412011-12-11 15:56:53 +00003423 channel=GetPixelChannelMapChannel(image,i);
3424 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +00003425 if ((traits & UpdatePixelTrait) == 0)
3426 continue;
3427 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
cristy33bd5152011-08-24 01:42:24 +00003428 }
cristyed231572011-07-14 02:18:59 +00003429 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003430 }
3431 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3432 status=MagickFalse;
3433 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3434 {
3435 MagickBooleanType
3436 proceed;
3437
cristyb5d5f722009-11-04 03:03:49 +00003438#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +00003439 #pragma omp critical (MagickCore_SigmoidalContrastImage)
cristy3ed852e2009-09-05 21:47:34 +00003440#endif
3441 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3442 image->rows);
3443 if (proceed == MagickFalse)
3444 status=MagickFalse;
3445 }
3446 }
3447 image_view=DestroyCacheView(image_view);
3448 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);
3449 return(status);
3450}