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