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