2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Image Enhancement Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/colorspace-private.h"
51 #include "MagickCore/composite-private.h"
52 #include "MagickCore/enhance.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/fx.h"
56 #include "MagickCore/gem.h"
57 #include "MagickCore/gem-private.h"
58 #include "MagickCore/geometry.h"
59 #include "MagickCore/histogram.h"
60 #include "MagickCore/image.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/quantum.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/resample.h"
71 #include "MagickCore/resample-private.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #include "MagickCore/thread-private.h"
77 #include "MagickCore/token.h"
78 #include "MagickCore/xml-tree.h"
79 #include "MagickCore/xml-tree-private.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % A u t o G a m m a I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % AutoGammaImage() extract the 'mean' from the image and adjust the image
93 % to try make set its gamma appropriatally.
95 % The format of the AutoGammaImage method is:
97 % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
99 % A description of each parameter follows:
101 % o image: The image to auto-level
103 % o exception: return any errors or warnings in this structure.
106 MagickExport MagickBooleanType AutoGammaImage(Image *image,
107 ExceptionInfo *exception)
122 if (image->channel_mask == DefaultChannels)
125 Apply gamma correction equally across all given channels.
127 (void) GetImageMean(image,&mean,&sans,exception);
128 gamma=log(mean*QuantumScale)/log_mean;
129 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
132 Auto-gamma each channel separately.
135 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
146 channel=GetPixelChannelMapChannel(image,i);
147 traits=GetPixelChannelMapTraits(image,channel);
148 if ((traits & UpdatePixelTrait) == 0)
150 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
151 status=GetImageMean(image,&mean,&sans,exception);
152 gamma=log(mean*QuantumScale)/log_mean;
153 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
154 (void) SetPixelChannelMask(image,channel_mask);
155 if (status == MagickFalse)
158 return(status != 0 ? MagickTrue : MagickFalse);
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % A u t o L e v e l I m a g e %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % AutoLevelImage() adjusts the levels of a particular image channel by
173 % scaling the minimum and maximum values to the full quantum range.
175 % The format of the LevelImage method is:
177 % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
179 % A description of each parameter follows:
181 % o image: The image to auto-level
183 % o exception: return any errors or warnings in this structure.
186 MagickExport MagickBooleanType AutoLevelImage(Image *image,
187 ExceptionInfo *exception)
189 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % B r i g h t n e s s C o n t r a s t I m a g e %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % BrightnessContrastImage() changes the brightness and/or contrast of an
204 % image. It converts the brightness and contrast parameters into slope and
205 % intercept and calls a polynomical function to apply to the image.
207 % The format of the BrightnessContrastImage method is:
209 % MagickBooleanType BrightnessContrastImage(Image *image,
210 % const double brightness,const double contrast,ExceptionInfo *exception)
212 % A description of each parameter follows:
214 % o image: the image.
216 % o brightness: the brightness percent (-100 .. 100).
218 % o contrast: the contrast percent (-100 .. 100).
220 % o exception: return any errors or warnings in this structure.
223 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
224 const double brightness,const double contrast,ExceptionInfo *exception)
226 #define BrightnessContastImageTag "BrightnessContast/Image"
238 Compute slope and intercept.
240 assert(image != (Image *) NULL);
241 assert(image->signature == MagickSignature);
242 if (image->debug != MagickFalse)
243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
245 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
248 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
249 coefficients[0]=slope;
250 coefficients[1]=intercept;
251 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % C l u t I m a g e %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % ClutImage() replaces each color value in the given image, by using it as an
267 % index to lookup a replacement color value in a Color Look UP Table in the
268 % form of an image. The values are extracted along a diagonal of the CLUT
269 % image so either a horizontal or vertial gradient image can be used.
271 % Typically this is used to either re-color a gray-scale image according to a
272 % color gradient in the CLUT image, or to perform a freeform histogram
273 % (level) adjustment according to the (typically gray-scale) gradient in the
276 % When the 'channel' mask includes the matte/alpha transparency channel but
277 % one image has no such channel it is assumed that that image is a simple
278 % gray-scale image that will effect the alpha channel values, either for
279 % gray-scale coloring (with transparent or semi-transparent colors), or
280 % a histogram adjustment of existing alpha channel values. If both images
281 % have matte channels, direct and normal indexing is applied, which is rarely
284 % The format of the ClutImage method is:
286 % MagickBooleanType ClutImage(Image *image,Image *clut_image,
287 % const PixelInterpolateMethod method,ExceptionInfo *exception)
289 % A description of each parameter follows:
291 % o image: the image, which is replaced by indexed CLUT values
293 % o clut_image: the color lookup table image for replacement color values.
295 % o method: the pixel interpolation method.
297 % o exception: return any errors or warnings in this structure.
300 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
301 const PixelInterpolateMethod method,ExceptionInfo *exception)
303 #define ClutImageTag "Clut/Image"
324 assert(image != (Image *) NULL);
325 assert(image->signature == MagickSignature);
326 if (image->debug != MagickFalse)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
328 assert(clut_image != (Image *) NULL);
329 assert(clut_image->signature == MagickSignature);
330 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
332 if (IsGrayColorspace(image->colorspace) != MagickFalse)
333 (void) TransformImageColorspace(image,RGBColorspace,exception);
334 clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
335 if (clut_map == (PixelInfo *) NULL)
336 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
343 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
344 clut_view=AcquireVirtualCacheView(clut_image,exception);
345 #if defined(MAGICKCORE_OPENMP_SUPPORT)
346 #pragma omp parallel for schedule(static,4) \
347 dynamic_number_threads(image,image->columns,1,1)
349 for (i=0; i <= (ssize_t) MaxMap; i++)
351 GetPixelInfo(clut_image,clut_map+i);
352 (void) InterpolatePixelInfo(clut_image,clut_view,method,
353 QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i*
354 (clut_image->rows-adjust),clut_map+i,exception);
356 clut_view=DestroyCacheView(clut_view);
357 image_view=AcquireAuthenticCacheView(image,exception);
358 #if defined(MAGICKCORE_OPENMP_SUPPORT)
359 #pragma omp parallel for schedule(static,4) shared(progress,status) \
360 dynamic_number_threads(image,image->columns,image->rows,1)
362 for (y=0; y < (ssize_t) image->rows; y++)
373 if (status == MagickFalse)
375 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
376 if (q == (Quantum *) NULL)
381 GetPixelInfo(image,&pixel);
382 for (x=0; x < (ssize_t) image->columns; x++)
384 if (GetPixelMask(image,q) != 0)
386 q+=GetPixelChannels(image);
389 GetPixelInfoPixel(image,q,&pixel);
390 pixel.red=clut_map[ScaleQuantumToMap(
391 ClampToQuantum(pixel.red))].red;
392 pixel.green=clut_map[ScaleQuantumToMap(
393 ClampToQuantum(pixel.green))].green;
394 pixel.blue=clut_map[ScaleQuantumToMap(
395 ClampToQuantum(pixel.blue))].blue;
396 pixel.black=clut_map[ScaleQuantumToMap(
397 ClampToQuantum(pixel.black))].black;
398 pixel.alpha=clut_map[ScaleQuantumToMap(
399 ClampToQuantum(pixel.alpha))].alpha;
400 SetPixelInfoPixel(image,&pixel,q);
401 q+=GetPixelChannels(image);
403 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
410 #if defined(MAGICKCORE_OPENMP_SUPPORT)
411 #pragma omp critical (MagickCore_ClutImage)
413 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
414 if (proceed == MagickFalse)
418 image_view=DestroyCacheView(image_view);
419 clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
420 if ((clut_image->matte != MagickFalse) &&
421 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
422 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % C o l o r D e c i s i o n L i s t I m a g e %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
438 % (CCC) file which solely contains one or more color corrections and applies
439 % the correction to the image. Here is a sample CCC file:
441 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
442 % <ColorCorrection id="cc03345">
444 % <Slope> 0.9 1.2 0.5 </Slope>
445 % <Offset> 0.4 -0.5 0.6 </Offset>
446 % <Power> 1.0 0.8 1.5 </Power>
449 % <Saturation> 0.85 </Saturation>
452 % </ColorCorrectionCollection>
454 % which includes the slop, offset, and power for each of the RGB channels
455 % as well as the saturation.
457 % The format of the ColorDecisionListImage method is:
459 % MagickBooleanType ColorDecisionListImage(Image *image,
460 % const char *color_correction_collection,ExceptionInfo *exception)
462 % A description of each parameter follows:
464 % o image: the image.
466 % o color_correction_collection: the color correction collection in XML.
468 % o exception: return any errors or warnings in this structure.
471 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
472 const char *color_correction_collection,ExceptionInfo *exception)
474 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
476 typedef struct _Correction
484 typedef struct _ColorCorrection
499 token[MaxTextExtent];
530 Allocate and initialize cdl maps.
532 assert(image != (Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 if (color_correction_collection == (const char *) NULL)
538 ccc=NewXMLTree((const char *) color_correction_collection,exception);
539 if (ccc == (XMLTreeInfo *) NULL)
541 cc=GetXMLTreeChild(ccc,"ColorCorrection");
542 if (cc == (XMLTreeInfo *) NULL)
544 ccc=DestroyXMLTree(ccc);
547 color_correction.red.slope=1.0;
548 color_correction.red.offset=0.0;
549 color_correction.red.power=1.0;
550 color_correction.green.slope=1.0;
551 color_correction.green.offset=0.0;
552 color_correction.green.power=1.0;
553 color_correction.blue.slope=1.0;
554 color_correction.blue.offset=0.0;
555 color_correction.blue.power=1.0;
556 color_correction.saturation=0.0;
557 sop=GetXMLTreeChild(cc,"SOPNode");
558 if (sop != (XMLTreeInfo *) NULL)
565 slope=GetXMLTreeChild(sop,"Slope");
566 if (slope != (XMLTreeInfo *) NULL)
568 content=GetXMLTreeContent(slope);
569 p=(const char *) content;
570 for (i=0; (*p != '\0') && (i < 3); i++)
572 GetMagickToken(p,&p,token);
574 GetMagickToken(p,&p,token);
579 color_correction.red.slope=StringToDouble(token,(char **) NULL);
584 color_correction.green.slope=StringToDouble(token,
590 color_correction.blue.slope=StringToDouble(token,
597 offset=GetXMLTreeChild(sop,"Offset");
598 if (offset != (XMLTreeInfo *) NULL)
600 content=GetXMLTreeContent(offset);
601 p=(const char *) content;
602 for (i=0; (*p != '\0') && (i < 3); i++)
604 GetMagickToken(p,&p,token);
606 GetMagickToken(p,&p,token);
611 color_correction.red.offset=StringToDouble(token,
617 color_correction.green.offset=StringToDouble(token,
623 color_correction.blue.offset=StringToDouble(token,
630 power=GetXMLTreeChild(sop,"Power");
631 if (power != (XMLTreeInfo *) NULL)
633 content=GetXMLTreeContent(power);
634 p=(const char *) content;
635 for (i=0; (*p != '\0') && (i < 3); i++)
637 GetMagickToken(p,&p,token);
639 GetMagickToken(p,&p,token);
644 color_correction.red.power=StringToDouble(token,(char **) NULL);
649 color_correction.green.power=StringToDouble(token,
655 color_correction.blue.power=StringToDouble(token,
663 sat=GetXMLTreeChild(cc,"SATNode");
664 if (sat != (XMLTreeInfo *) NULL)
669 saturation=GetXMLTreeChild(sat,"Saturation");
670 if (saturation != (XMLTreeInfo *) NULL)
672 content=GetXMLTreeContent(saturation);
673 p=(const char *) content;
674 GetMagickToken(p,&p,token);
675 color_correction.saturation=StringToDouble(token,(char **) NULL);
678 ccc=DestroyXMLTree(ccc);
679 if (image->debug != MagickFalse)
681 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
682 " Color Correction Collection:");
683 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
684 " color_correction.red.slope: %g",color_correction.red.slope);
685 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
686 " color_correction.red.offset: %g",color_correction.red.offset);
687 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
688 " color_correction.red.power: %g",color_correction.red.power);
689 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
690 " color_correction.green.slope: %g",color_correction.green.slope);
691 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
692 " color_correction.green.offset: %g",color_correction.green.offset);
693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
694 " color_correction.green.power: %g",color_correction.green.power);
695 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
696 " color_correction.blue.slope: %g",color_correction.blue.slope);
697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
698 " color_correction.blue.offset: %g",color_correction.blue.offset);
699 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
700 " color_correction.blue.power: %g",color_correction.blue.power);
701 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
702 " color_correction.saturation: %g",color_correction.saturation);
704 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
705 if (cdl_map == (PixelInfo *) NULL)
706 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
708 #if defined(MAGICKCORE_OPENMP_SUPPORT)
709 #pragma omp parallel for schedule(static,4) \
710 dynamic_number_threads(image,image->columns,1,1)
712 for (i=0; i <= (ssize_t) MaxMap; i++)
714 cdl_map[i].red=(double) ScaleMapToQuantum((double)
715 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
716 color_correction.red.offset,color_correction.red.power))));
717 cdl_map[i].green=(double) ScaleMapToQuantum((double)
718 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
719 color_correction.green.offset,color_correction.green.power))));
720 cdl_map[i].blue=(double) ScaleMapToQuantum((double)
721 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
722 color_correction.blue.offset,color_correction.blue.power))));
724 if (image->storage_class == PseudoClass)
725 for (i=0; i < (ssize_t) image->colors; i++)
728 Apply transfer function to colormap.
733 luma=0.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
734 0.07217*image->colormap[i].blue;
735 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
736 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-luma;
737 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
738 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-luma;
739 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
740 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-luma;
743 Apply transfer function to image.
747 image_view=AcquireAuthenticCacheView(image,exception);
748 #if defined(MAGICKCORE_OPENMP_SUPPORT)
749 #pragma omp parallel for schedule(static,4) shared(progress,status) \
750 dynamic_number_threads(image,image->columns,image->rows,1)
752 for (y=0; y < (ssize_t) image->rows; y++)
763 if (status == MagickFalse)
765 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
766 if (q == (Quantum *) NULL)
771 for (x=0; x < (ssize_t) image->columns; x++)
773 luma=0.21267*GetPixelRed(image,q)+0.71526*GetPixelGreen(image,q)+0.07217*
774 GetPixelBlue(image,q);
775 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
776 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
777 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
778 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
779 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
780 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
781 q+=GetPixelChannels(image);
783 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
785 if (image->progress_monitor != (MagickProgressMonitor) NULL)
790 #if defined(MAGICKCORE_OPENMP_SUPPORT)
791 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
793 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
794 progress++,image->rows);
795 if (proceed == MagickFalse)
799 image_view=DestroyCacheView(image_view);
800 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % C o n t r a s t I m a g e %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % ContrastImage() enhances the intensity differences between the lighter and
816 % darker elements of the image. Set sharpen to a MagickTrue to increase the
817 % image contrast otherwise the contrast is reduced.
819 % The format of the ContrastImage method is:
821 % MagickBooleanType ContrastImage(Image *image,
822 % const MagickBooleanType sharpen,ExceptionInfo *exception)
824 % A description of each parameter follows:
826 % o image: the image.
828 % o sharpen: Increase or decrease image contrast.
830 % o exception: return any errors or warnings in this structure.
834 static void Contrast(const int sign,double *red,double *green,double *blue)
842 Enhance contrast: dark color become darker, light color become lighter.
844 assert(red != (double *) NULL);
845 assert(green != (double *) NULL);
846 assert(blue != (double *) NULL);
850 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
851 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
853 if (brightness > 1.0)
856 if (brightness < 0.0)
858 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
861 MagickExport MagickBooleanType ContrastImage(Image *image,
862 const MagickBooleanType sharpen,ExceptionInfo *exception)
864 #define ContrastImageTag "Contrast/Image"
884 assert(image != (Image *) NULL);
885 assert(image->signature == MagickSignature);
886 if (image->debug != MagickFalse)
887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
888 sign=sharpen != MagickFalse ? 1 : -1;
889 if (image->storage_class == PseudoClass)
892 Contrast enhance colormap.
894 for (i=0; i < (ssize_t) image->colors; i++)
895 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
896 &image->colormap[i].blue);
899 Contrast enhance image.
903 image_view=AcquireAuthenticCacheView(image,exception);
904 #if defined(MAGICKCORE_OPENMP_SUPPORT)
905 #pragma omp parallel for schedule(static,4) shared(progress,status) \
906 dynamic_number_threads(image,image->columns,image->rows,1)
908 for (y=0; y < (ssize_t) image->rows; y++)
921 if (status == MagickFalse)
923 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
924 if (q == (Quantum *) NULL)
929 for (x=0; x < (ssize_t) image->columns; x++)
931 red=(double) GetPixelRed(image,q);
932 green=(double) GetPixelGreen(image,q);
933 blue=(double) GetPixelBlue(image,q);
934 Contrast(sign,&red,&green,&blue);
935 SetPixelRed(image,ClampToQuantum(red),q);
936 SetPixelGreen(image,ClampToQuantum(green),q);
937 SetPixelBlue(image,ClampToQuantum(blue),q);
938 q+=GetPixelChannels(image);
940 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
942 if (image->progress_monitor != (MagickProgressMonitor) NULL)
947 #if defined(MAGICKCORE_OPENMP_SUPPORT)
948 #pragma omp critical (MagickCore_ContrastImage)
950 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
951 if (proceed == MagickFalse)
955 image_view=DestroyCacheView(image_view);
960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % C o n t r a s t S t r e t c h I m a g e %
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 % ContrastStretchImage() is a simple image enhancement technique that attempts
971 % to improve the contrast in an image by 'stretching' the range of intensity
972 % values it contains to span a desired range of values. It differs from the
973 % more sophisticated histogram equalization in that it can only apply a
974 % linear scaling function to the image pixel values. As a result the
975 % 'enhancement' is less harsh.
977 % The format of the ContrastStretchImage method is:
979 % MagickBooleanType ContrastStretchImage(Image *image,
980 % const char *levels,ExceptionInfo *exception)
982 % A description of each parameter follows:
984 % o image: the image.
986 % o black_point: the black point.
988 % o white_point: the white point.
990 % o levels: Specify the levels where the black and white points have the
991 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
993 % o exception: return any errors or warnings in this structure.
996 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
997 const double black_point,const double white_point,ExceptionInfo *exception)
999 #define MaxRange(color) ((double) ScaleQuantumToMap((Quantum) (color)))
1000 #define ContrastStretchImageTag "ContrastStretch/Image"
1027 Allocate histogram and stretch map.
1029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
1031 if (image->debug != MagickFalse)
1032 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1033 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1034 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
1035 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
1036 sizeof(*histogram));
1037 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1038 GetPixelChannels(image)*sizeof(*stretch_map));
1039 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1040 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
1042 if (stretch_map != (double *) NULL)
1043 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1044 if (histogram != (double *) NULL)
1045 histogram=(double *) RelinquishMagickMemory(histogram);
1046 if (white != (double *) NULL)
1047 white=(double *) RelinquishMagickMemory(white);
1048 if (black != (double *) NULL)
1049 black=(double *) RelinquishMagickMemory(black);
1050 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1057 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1058 sizeof(*histogram));
1059 image_view=AcquireVirtualCacheView(image,exception);
1060 for (y=0; y < (ssize_t) image->rows; y++)
1062 register const Quantum
1068 if (status == MagickFalse)
1070 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1071 if (p == (const Quantum *) NULL)
1076 for (x=0; x < (ssize_t) image->columns; x++)
1084 pixel=(double) GetPixelIntensity(image,p);
1085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1087 if (image->channel_mask != DefaultChannels)
1088 pixel=(double) p[i];
1089 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
1090 ClampToQuantum(pixel))+i]++;
1092 p+=GetPixelChannels(image);
1095 image_view=DestroyCacheView(image_view);
1097 Find the histogram boundaries by locating the black/white levels.
1099 number_channels=GetPixelChannels(image);
1100 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1101 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1102 dynamic_number_threads(image,image->columns,1,1)
1104 for (i=0; i < (ssize_t) number_channels; i++)
1113 white[i]=MaxRange(QuantumRange);
1115 for (j=0; j <= (ssize_t) MaxMap; j++)
1117 intensity+=histogram[GetPixelChannels(image)*j+i];
1118 if (intensity > black_point)
1121 black[i]=(double) j;
1123 for (j=(ssize_t) MaxMap; j != 0; j--)
1125 intensity+=histogram[GetPixelChannels(image)*j+i];
1126 if (intensity > ((double) image->columns*image->rows-white_point))
1129 white[i]=(double) j;
1131 histogram=(double *) RelinquishMagickMemory(histogram);
1133 Stretch the histogram to create the stretched image mapping.
1135 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1136 sizeof(*stretch_map));
1137 number_channels=GetPixelChannels(image);
1138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1139 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1140 dynamic_number_threads(image,image->columns,1,1)
1142 for (i=0; i < (ssize_t) number_channels; i++)
1147 for (j=0; j <= (ssize_t) MaxMap; j++)
1149 if (j < (ssize_t) black[i])
1150 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1152 if (j > (ssize_t) white[i])
1153 stretch_map[GetPixelChannels(image)*j+i]=(double)
1156 if (black[i] != white[i])
1157 stretch_map[GetPixelChannels(image)*j+i]=(double)
1158 ScaleMapToQuantum((double) (MaxMap*(j-black[i])/
1159 (white[i]-black[i])));
1162 if (image->storage_class == PseudoClass)
1168 Stretch-contrast colormap.
1170 for (j=0; j < (ssize_t) image->colors; j++)
1172 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1174 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1175 if (black[i] != white[i])
1176 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1177 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
1179 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1181 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1182 if (black[i] != white[i])
1183 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1184 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
1186 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1188 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1189 if (black[i] != white[i])
1190 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1191 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
1193 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1195 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1196 if (black[i] != white[i])
1197 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1198 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
1203 Stretch-contrast image.
1207 image_view=AcquireAuthenticCacheView(image,exception);
1208 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1209 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1210 dynamic_number_threads(image,image->columns,image->rows,1)
1212 for (y=0; y < (ssize_t) image->rows; y++)
1220 if (status == MagickFalse)
1222 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1223 if (q == (Quantum *) NULL)
1228 for (x=0; x < (ssize_t) image->columns; x++)
1233 if (GetPixelMask(image,q) != 0)
1235 q+=GetPixelChannels(image);
1238 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1246 channel=GetPixelChannelMapChannel(image,i);
1247 traits=GetPixelChannelMapTraits(image,channel);
1248 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1250 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1251 ScaleQuantumToMap(q[i])+i]);
1253 q+=GetPixelChannels(image);
1255 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1257 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp critical (MagickCore_ContrastStretchImage)
1265 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1267 if (proceed == MagickFalse)
1271 image_view=DestroyCacheView(image_view);
1272 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1273 white=(double *) RelinquishMagickMemory(white);
1274 black=(double *) RelinquishMagickMemory(black);
1279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1283 % E n h a n c e I m a g e %
1287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1289 % EnhanceImage() applies a digital filter that improves the quality of a
1292 % The format of the EnhanceImage method is:
1294 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1296 % A description of each parameter follows:
1298 % o image: the image.
1300 % o exception: return any errors or warnings in this structure.
1303 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1305 #define EnhancePixel(weight) \
1306 mean=((double) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1307 distance=(double) r[i]-(double) GetPixelChannel( \
1308 enhance_image,channel,q); \
1309 distance_squared=QuantumScale*(2.0*((double) QuantumRange+1.0)+ \
1310 mean)*distance*distance; \
1311 if (distance_squared < ((double) QuantumRange*(double) \
1312 QuantumRange/25.0f)) \
1314 aggregate+=(weight)*r[i]; \
1315 total_weight+=(weight); \
1317 r+=GetPixelChannels(image);
1318 #define EnhanceImageTag "Enhance/Image"
1337 Initialize enhanced image attributes.
1339 assert(image != (const Image *) NULL);
1340 assert(image->signature == MagickSignature);
1341 if (image->debug != MagickFalse)
1342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1343 assert(exception != (ExceptionInfo *) NULL);
1344 assert(exception->signature == MagickSignature);
1345 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1347 if (enhance_image == (Image *) NULL)
1348 return((Image *) NULL);
1349 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1351 enhance_image=DestroyImage(enhance_image);
1352 return((Image *) NULL);
1359 image_view=AcquireVirtualCacheView(image,exception);
1360 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1361 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1362 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1363 dynamic_number_threads(image,image->columns,image->rows,1)
1365 for (y=0; y < (ssize_t) image->rows; y++)
1367 register const Quantum
1379 if (status == MagickFalse)
1381 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1382 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1384 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1389 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1390 for (x=0; x < (ssize_t) image->columns; x++)
1395 if (GetPixelMask(image,p) != 0)
1397 p+=GetPixelChannels(image);
1398 q+=GetPixelChannels(enhance_image);
1401 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1417 register const Quantum
1420 channel=GetPixelChannelMapChannel(image,i);
1421 traits=GetPixelChannelMapTraits(image,channel);
1422 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1423 if ((traits == UndefinedPixelTrait) ||
1424 (enhance_traits == UndefinedPixelTrait))
1426 SetPixelChannel(enhance_image,channel,p[center+i],q);
1427 if ((enhance_traits & CopyPixelTrait) != 0)
1430 Compute weighted average of target pixel color components.
1435 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1436 EnhancePixel(8.0); EnhancePixel(5.0);
1437 r=p+1*GetPixelChannels(image)*(image->columns+4);
1438 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1439 EnhancePixel(20.0); EnhancePixel(8.0);
1440 r=p+2*GetPixelChannels(image)*(image->columns+4);
1441 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1442 EnhancePixel(40.0); EnhancePixel(10.0);
1443 r=p+3*GetPixelChannels(image)*(image->columns+4);
1444 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1445 EnhancePixel(20.0); EnhancePixel(8.0);
1446 r=p+4*GetPixelChannels(image)*(image->columns+4);
1447 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1448 EnhancePixel(8.0); EnhancePixel(5.0);
1449 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1452 p+=GetPixelChannels(image);
1453 q+=GetPixelChannels(enhance_image);
1455 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1457 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1462 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1463 #pragma omp critical (MagickCore_EnhanceImage)
1465 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1466 if (proceed == MagickFalse)
1470 enhance_view=DestroyCacheView(enhance_view);
1471 image_view=DestroyCacheView(image_view);
1472 return(enhance_image);
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480 % E q u a l i z e I m a g e %
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 % EqualizeImage() applies a histogram equalization to the image.
1488 % The format of the EqualizeImage method is:
1490 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1492 % A description of each parameter follows:
1494 % o image: the image.
1496 % o exception: return any errors or warnings in this structure.
1499 MagickExport MagickBooleanType EqualizeImage(Image *image,
1500 ExceptionInfo *exception)
1502 #define EqualizeImageTag "Equalize/Image"
1514 black[CompositePixelChannel],
1518 white[CompositePixelChannel];
1530 Allocate and initialize histogram arrays.
1532 assert(image != (Image *) NULL);
1533 assert(image->signature == MagickSignature);
1534 if (image->debug != MagickFalse)
1535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1536 equalize_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1537 GetPixelChannels(image)*sizeof(*equalize_map));
1538 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,
1539 GetPixelChannels(image)*sizeof(*histogram));
1540 map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1541 GetPixelChannels(image)*sizeof(*map));
1542 if ((equalize_map == (double *) NULL) ||
1543 (histogram == (double *) NULL) ||
1544 (map == (double *) NULL))
1546 if (map != (double *) NULL)
1547 map=(double *) RelinquishMagickMemory(map);
1548 if (histogram != (double *) NULL)
1549 histogram=(double *) RelinquishMagickMemory(histogram);
1550 if (equalize_map != (double *) NULL)
1551 equalize_map=(double *) RelinquishMagickMemory(equalize_map);
1552 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1559 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1560 sizeof(*histogram));
1561 image_view=AcquireVirtualCacheView(image,exception);
1562 for (y=0; y < (ssize_t) image->rows; y++)
1564 register const Quantum
1570 if (status == MagickFalse)
1572 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1573 if (p == (const Quantum *) NULL)
1578 for (x=0; x < (ssize_t) image->columns; x++)
1583 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1584 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1585 p+=GetPixelChannels(image);
1588 image_view=DestroyCacheView(image_view);
1590 Integrate the histogram to get the equalization map.
1592 number_channels=GetPixelChannels(image);
1593 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1594 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1595 dynamic_number_threads(image,image->columns,1,1)
1597 for (i=0; i < (ssize_t) number_channels; i++)
1606 for (j=0; j <= (ssize_t) MaxMap; j++)
1608 intensity+=histogram[GetPixelChannels(image)*j+i];
1609 map[GetPixelChannels(image)*j+i]=intensity;
1612 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1613 sizeof(*equalize_map));
1614 number_channels=GetPixelChannels(image);
1615 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1616 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1617 dynamic_number_threads(image,image->columns,1,1)
1619 for (i=0; i < (ssize_t) number_channels; i++)
1625 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1626 if (black[i] != white[i])
1627 for (j=0; j <= (ssize_t) MaxMap; j++)
1628 equalize_map[GetPixelChannels(image)*j+i]=(double)
1629 ScaleMapToQuantum((double) ((MaxMap*(map[
1630 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1632 histogram=(double *) RelinquishMagickMemory(histogram);
1633 map=(double *) RelinquishMagickMemory(map);
1634 if (image->storage_class == PseudoClass)
1645 for (j=0; j < (ssize_t) image->colors; j++)
1647 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1649 channel=GetPixelChannelMapChannel(image,RedPixelChannel);
1650 if (black[channel] != white[channel])
1651 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
1652 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
1655 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1657 channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
1658 if (black[channel] != white[channel])
1659 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
1660 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
1663 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1665 channel=GetPixelChannelMapChannel(image,BluePixelChannel);
1666 if (black[channel] != white[channel])
1667 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
1668 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
1671 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1673 channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1674 if (black[channel] != white[channel])
1675 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
1676 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
1685 image_view=AcquireAuthenticCacheView(image,exception);
1686 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1687 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1688 dynamic_number_threads(image,image->columns,image->rows,1)
1690 for (y=0; y < (ssize_t) image->rows; y++)
1698 if (status == MagickFalse)
1700 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1701 if (q == (Quantum *) NULL)
1706 for (x=0; x < (ssize_t) image->columns; x++)
1711 if (GetPixelMask(image,q) != 0)
1713 q+=GetPixelChannels(image);
1716 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1724 channel=GetPixelChannelMapChannel(image,i);
1725 traits=GetPixelChannelMapTraits(image,channel);
1726 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1728 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1729 ScaleQuantumToMap(q[i])+i]);
1731 q+=GetPixelChannels(image);
1733 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1735 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1740 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1741 #pragma omp critical (MagickCore_EqualizeImage)
1743 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1744 if (proceed == MagickFalse)
1748 image_view=DestroyCacheView(image_view);
1749 equalize_map=(double *) RelinquishMagickMemory(equalize_map);
1754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1758 % G a m m a I m a g e %
1762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1764 % GammaImage() gamma-corrects a particular image channel. The same
1765 % image viewed on different devices will have perceptual differences in the
1766 % way the image's intensities are represented on the screen. Specify
1767 % individual gamma levels for the red, green, and blue channels, or adjust
1768 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1770 % You can also reduce the influence of a particular channel with a gamma
1773 % The format of the GammaImage method is:
1775 % MagickBooleanType GammaImage(Image *image,const double gamma,
1776 % ExceptionInfo *exception)
1778 % A description of each parameter follows:
1780 % o image: the image.
1782 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1784 % o gamma: the image gamma.
1787 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1788 ExceptionInfo *exception)
1790 #define GammaCorrectImageTag "GammaCorrect/Image"
1811 Allocate and initialize gamma maps.
1813 assert(image != (Image *) NULL);
1814 assert(image->signature == MagickSignature);
1815 if (image->debug != MagickFalse)
1816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1819 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1820 if (gamma_map == (Quantum *) NULL)
1821 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1823 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1825 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1826 #pragma omp parallel for \
1827 dynamic_number_threads(image,image->columns,1,1)
1829 for (i=0; i <= (ssize_t) MaxMap; i++)
1830 gamma_map[i]=ScaleMapToQuantum((double) (MaxMap*pow((double) i/
1831 MaxMap,1.0/gamma)));
1832 if (image->storage_class == PseudoClass)
1833 for (i=0; i < (ssize_t) image->colors; i++)
1836 Gamma-correct colormap.
1838 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1839 image->colormap[i].red=(double) gamma_map[
1840 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
1841 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1842 image->colormap[i].green=(double) gamma_map[
1843 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
1844 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1845 image->colormap[i].blue=(double) gamma_map[
1846 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
1847 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1848 image->colormap[i].alpha=(double) gamma_map[
1849 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
1852 Gamma-correct image.
1856 image_view=AcquireAuthenticCacheView(image,exception);
1857 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1858 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1859 dynamic_number_threads(image,image->columns,image->rows,1)
1861 for (y=0; y < (ssize_t) image->rows; y++)
1869 if (status == MagickFalse)
1871 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1872 if (q == (Quantum *) NULL)
1877 for (x=0; x < (ssize_t) image->columns; x++)
1882 if (GetPixelMask(image,q) != 0)
1884 q+=GetPixelChannels(image);
1887 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1895 channel=GetPixelChannelMapChannel(image,i);
1896 traits=GetPixelChannelMapTraits(image,channel);
1897 if ((traits & UpdatePixelTrait) == 0)
1899 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
1901 q+=GetPixelChannels(image);
1903 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1905 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1910 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1911 #pragma omp critical (MagickCore_GammaImage)
1913 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1915 if (proceed == MagickFalse)
1919 image_view=DestroyCacheView(image_view);
1920 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
1921 if (image->gamma != 0.0)
1922 image->gamma*=gamma;
1927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1931 % H a l d C l u t I m a g e %
1935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1938 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1939 % Create it with the HALD coder. You can apply any color transformation to
1940 % the Hald image and then use this method to apply the transform to the
1943 % The format of the HaldClutImage method is:
1945 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1946 % ExceptionInfo *exception)
1948 % A description of each parameter follows:
1950 % o image: the image, which is replaced by indexed CLUT values
1952 % o hald_image: the color lookup table image for replacement color values.
1954 % o exception: return any errors or warnings in this structure.
1958 static inline size_t MagickMin(const size_t x,const size_t y)
1965 MagickExport MagickBooleanType HaldClutImage(Image *image,
1966 const Image *hald_image,ExceptionInfo *exception)
1968 #define HaldClutImageTag "Clut/Image"
1970 typedef struct _HaldInfo
2002 assert(image != (Image *) NULL);
2003 assert(image->signature == MagickSignature);
2004 if (image->debug != MagickFalse)
2005 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2006 assert(hald_image != (Image *) NULL);
2007 assert(hald_image->signature == MagickSignature);
2008 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2009 return(MagickFalse);
2010 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2011 (void) TransformImageColorspace(image,RGBColorspace,exception);
2012 if (image->matte == MagickFalse)
2013 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2019 length=MagickMin(hald_image->columns,hald_image->rows);
2020 for (level=2; (level*level*level) < length; level++) ;
2022 cube_size=level*level;
2023 width=(double) hald_image->columns;
2024 GetPixelInfo(hald_image,&zero);
2025 hald_view=AcquireVirtualCacheView(hald_image,exception);
2026 image_view=AcquireAuthenticCacheView(image,exception);
2027 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2028 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2029 dynamic_number_threads(image,image->columns,image->rows,1)
2031 for (y=0; y < (ssize_t) image->rows; y++)
2039 if (status == MagickFalse)
2041 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2042 if (q == (Quantum *) NULL)
2047 for (x=0; x < (ssize_t) image->columns; x++)
2062 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2063 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2064 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2065 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2066 point.x-=floor(point.x);
2067 point.y-=floor(point.y);
2068 point.z-=floor(point.z);
2070 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2071 fmod(offset,width),floor(offset/width),&pixel1,exception);
2073 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2074 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2076 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2079 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2080 fmod(offset,width),floor(offset/width),&pixel1,exception);
2081 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2082 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2084 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2087 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2089 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2090 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2091 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2092 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2093 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2094 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2095 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2096 (image->colorspace == CMYKColorspace))
2097 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2098 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2099 (image->matte != MagickFalse))
2100 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2101 q+=GetPixelChannels(image);
2103 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2105 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2110 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2111 #pragma omp critical (MagickCore_HaldClutImage)
2113 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2114 if (proceed == MagickFalse)
2118 hald_view=DestroyCacheView(hald_view);
2119 image_view=DestroyCacheView(image_view);
2124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2128 % L e v e l I m a g e %
2132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2134 % LevelImage() adjusts the levels of a particular image channel by
2135 % scaling the colors falling between specified white and black points to
2136 % the full available quantum range.
2138 % The parameters provided represent the black, and white points. The black
2139 % point specifies the darkest color in the image. Colors darker than the
2140 % black point are set to zero. White point specifies the lightest color in
2141 % the image. Colors brighter than the white point are set to the maximum
2144 % If a '!' flag is given, map black and white colors to the given levels
2145 % rather than mapping those levels to black and white. See
2146 % LevelizeImage() below.
2148 % Gamma specifies a gamma correction to apply to the image.
2150 % The format of the LevelImage method is:
2152 % MagickBooleanType LevelImage(Image *image,const double black_point,
2153 % const double white_point,const double gamma,ExceptionInfo *exception)
2155 % A description of each parameter follows:
2157 % o image: the image.
2159 % o black_point: The level to map zero (black) to.
2161 % o white_point: The level to map QuantumRange (white) to.
2163 % o exception: return any errors or warnings in this structure.
2167 static inline double LevelPixel(const double black_point,
2168 const double white_point,const double gamma,const double pixel)
2174 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2175 level_pixel=(double) QuantumRange*pow(scale*((double) pixel-
2176 black_point),1.0/gamma);
2177 return(level_pixel);
2180 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2181 const double white_point,const double gamma,ExceptionInfo *exception)
2183 #define LevelImageTag "Level/Image"
2201 Allocate and initialize levels map.
2203 assert(image != (Image *) NULL);
2204 assert(image->signature == MagickSignature);
2205 if (image->debug != MagickFalse)
2206 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2207 if (image->storage_class == PseudoClass)
2208 for (i=0; i < (ssize_t) image->colors; i++)
2213 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2214 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2215 white_point,gamma,image->colormap[i].red));
2216 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2217 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2218 white_point,gamma,image->colormap[i].green));
2219 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2220 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2221 white_point,gamma,image->colormap[i].blue));
2222 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2223 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2224 white_point,gamma,image->colormap[i].alpha));
2231 image_view=AcquireAuthenticCacheView(image,exception);
2232 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2233 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2234 dynamic_number_threads(image,image->columns,image->rows,1)
2236 for (y=0; y < (ssize_t) image->rows; y++)
2244 if (status == MagickFalse)
2246 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2247 if (q == (Quantum *) NULL)
2252 for (x=0; x < (ssize_t) image->columns; x++)
2257 if (GetPixelMask(image,q) != 0)
2259 q+=GetPixelChannels(image);
2262 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2270 channel=GetPixelChannelMapChannel(image,i);
2271 traits=GetPixelChannelMapTraits(image,channel);
2272 if ((traits & UpdatePixelTrait) == 0)
2274 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2277 q+=GetPixelChannels(image);
2279 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2281 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2287 #pragma omp critical (MagickCore_LevelImage)
2289 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2290 if (proceed == MagickFalse)
2294 image_view=DestroyCacheView(image_view);
2299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2303 % L e v e l i z e I m a g e %
2307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2309 % LevelizeImage() applies the reversed LevelImage() operation to just
2310 % the specific channels specified. It compresses the full range of color
2311 % values, so that they lie between the given black and white points. Gamma is
2312 % applied before the values are mapped.
2314 % LevelizeImage() can be called with by using a +level command line
2315 % API option, or using a '!' on a -level or LevelImage() geometry string.
2317 % It can be used to de-contrast a greyscale image to the exact levels
2318 % specified. Or by using specific levels for each channel of an image you
2319 % can convert a gray-scale image to any linear color gradient, according to
2322 % The format of the LevelizeImage method is:
2324 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2325 % const double white_point,const double gamma,ExceptionInfo *exception)
2327 % A description of each parameter follows:
2329 % o image: the image.
2331 % o black_point: The level to map zero (black) to.
2333 % o white_point: The level to map QuantumRange (white) to.
2335 % o gamma: adjust gamma by this factor before mapping values.
2337 % o exception: return any errors or warnings in this structure.
2340 MagickExport MagickBooleanType LevelizeImage(Image *image,
2341 const double black_point,const double white_point,const double gamma,
2342 ExceptionInfo *exception)
2344 #define LevelizeImageTag "Levelize/Image"
2345 #define LevelizeValue(x) (ClampToQuantum((pow((double) (QuantumScale*(x)), \
2346 1.0/gamma))*(white_point-black_point)+black_point))
2364 Allocate and initialize levels map.
2366 assert(image != (Image *) NULL);
2367 assert(image->signature == MagickSignature);
2368 if (image->debug != MagickFalse)
2369 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2370 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2371 (void) SetImageColorspace(image,RGBColorspace,exception);
2372 if (image->storage_class == PseudoClass)
2373 for (i=0; i < (ssize_t) image->colors; i++)
2378 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2379 image->colormap[i].red=(double) LevelizeValue(
2380 image->colormap[i].red);
2381 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2382 image->colormap[i].green=(double) LevelizeValue(
2383 image->colormap[i].green);
2384 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2385 image->colormap[i].blue=(double) LevelizeValue(
2386 image->colormap[i].blue);
2387 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2388 image->colormap[i].alpha=(double) LevelizeValue(
2389 image->colormap[i].alpha);
2396 image_view=AcquireAuthenticCacheView(image,exception);
2397 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2398 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2399 dynamic_number_threads(image,image->columns,image->rows,1)
2401 for (y=0; y < (ssize_t) image->rows; y++)
2409 if (status == MagickFalse)
2411 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2412 if (q == (Quantum *) NULL)
2417 for (x=0; x < (ssize_t) image->columns; x++)
2422 if (GetPixelMask(image,q) != 0)
2424 q+=GetPixelChannels(image);
2427 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2435 channel=GetPixelChannelMapChannel(image,i);
2436 traits=GetPixelChannelMapTraits(image,channel);
2437 if ((traits & UpdatePixelTrait) == 0)
2439 q[i]=LevelizeValue(q[i]);
2441 q+=GetPixelChannels(image);
2443 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2445 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2451 #pragma omp critical (MagickCore_LevelizeImage)
2453 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2454 if (proceed == MagickFalse)
2458 image_view=DestroyCacheView(image_view);
2463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2467 % L e v e l I m a g e C o l o r s %
2471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2473 % LevelImageColors() maps the given color to "black" and "white" values,
2474 % linearly spreading out the colors, and level values on a channel by channel
2475 % bases, as per LevelImage(). The given colors allows you to specify
2476 % different level ranges for each of the color channels separately.
2478 % If the boolean 'invert' is set true the image values will modifyed in the
2479 % reverse direction. That is any existing "black" and "white" colors in the
2480 % image will become the color values given, with all other values compressed
2481 % appropriatally. This effectivally maps a greyscale gradient into the given
2484 % The format of the LevelImageColors method is:
2486 % MagickBooleanType LevelImageColors(Image *image,
2487 % const PixelInfo *black_color,const PixelInfo *white_color,
2488 % const MagickBooleanType invert,ExceptionInfo *exception)
2490 % A description of each parameter follows:
2492 % o image: the image.
2494 % o black_color: The color to map black to/from
2496 % o white_point: The color to map white to/from
2498 % o invert: if true map the colors (levelize), rather than from (level)
2500 % o exception: return any errors or warnings in this structure.
2503 MagickExport MagickBooleanType LevelImageColors(Image *image,
2504 const PixelInfo *black_color,const PixelInfo *white_color,
2505 const MagickBooleanType invert,ExceptionInfo *exception)
2514 Allocate and initialize levels map.
2516 assert(image != (Image *) NULL);
2517 assert(image->signature == MagickSignature);
2518 if (image->debug != MagickFalse)
2519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2521 if (invert == MagickFalse)
2523 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2525 channel_mask=SetPixelChannelMask(image,RedChannel);
2526 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2528 (void) SetPixelChannelMask(image,channel_mask);
2530 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2532 channel_mask=SetPixelChannelMask(image,GreenChannel);
2533 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2535 (void) SetPixelChannelMask(image,channel_mask);
2537 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2539 channel_mask=SetPixelChannelMask(image,BlueChannel);
2540 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2542 (void) SetPixelChannelMask(image,channel_mask);
2544 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2545 (image->colorspace == CMYKColorspace))
2547 channel_mask=SetPixelChannelMask(image,BlackChannel);
2548 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2550 (void) SetPixelChannelMask(image,channel_mask);
2552 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2553 (image->matte == MagickTrue))
2555 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2556 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2558 (void) SetPixelChannelMask(image,channel_mask);
2563 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2565 channel_mask=SetPixelChannelMask(image,RedChannel);
2566 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2568 (void) SetPixelChannelMask(image,channel_mask);
2570 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2572 channel_mask=SetPixelChannelMask(image,GreenChannel);
2573 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2575 (void) SetPixelChannelMask(image,channel_mask);
2577 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2579 channel_mask=SetPixelChannelMask(image,BlueChannel);
2580 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2582 (void) SetPixelChannelMask(image,channel_mask);
2584 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2585 (image->colorspace == CMYKColorspace))
2587 channel_mask=SetPixelChannelMask(image,BlackChannel);
2588 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2590 (void) SetPixelChannelMask(image,channel_mask);
2592 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2593 (image->matte == MagickTrue))
2595 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2596 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2598 (void) SetPixelChannelMask(image,channel_mask);
2601 return(status == 0 ? MagickFalse : MagickTrue);
2605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2609 % L i n e a r S t r e t c h I m a g e %
2613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2615 % LinearStretchImage() discards any pixels below the black point and above
2616 % the white point and levels the remaining pixels.
2618 % The format of the LinearStretchImage method is:
2620 % MagickBooleanType LinearStretchImage(Image *image,
2621 % const double black_point,const double white_point,
2622 % ExceptionInfo *exception)
2624 % A description of each parameter follows:
2626 % o image: the image.
2628 % o black_point: the black point.
2630 % o white_point: the white point.
2632 % o exception: return any errors or warnings in this structure.
2635 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2636 const double black_point,const double white_point,ExceptionInfo *exception)
2638 #define LinearStretchImageTag "LinearStretch/Image"
2656 Allocate histogram and linear map.
2658 assert(image != (Image *) NULL);
2659 assert(image->signature == MagickSignature);
2660 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,
2661 sizeof(*histogram));
2662 if (histogram == (double *) NULL)
2663 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2668 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2669 image_view=AcquireVirtualCacheView(image,exception);
2670 for (y=0; y < (ssize_t) image->rows; y++)
2672 register const Quantum
2678 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2679 if (p == (const Quantum *) NULL)
2681 for (x=0; x < (ssize_t) image->columns; x++)
2683 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2684 p+=GetPixelChannels(image);
2687 image_view=DestroyCacheView(image_view);
2689 Find the histogram boundaries by locating the black and white point levels.
2692 for (black=0; black < (ssize_t) MaxMap; black++)
2694 intensity+=histogram[black];
2695 if (intensity >= black_point)
2699 for (white=(ssize_t) MaxMap; white != 0; white--)
2701 intensity+=histogram[white];
2702 if (intensity >= white_point)
2705 histogram=(double *) RelinquishMagickMemory(histogram);
2706 status=LevelImage(image,(double) black,(double) white,1.0,exception);
2711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2715 % M o d u l a t e I m a g e %
2719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2721 % ModulateImage() lets you control the brightness, saturation, and hue
2722 % of an image. Modulate represents the brightness, saturation, and hue
2723 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2724 % modulation is lightness, saturation, and hue. And if the colorspace is
2725 % HWB, use blackness, whiteness, and hue.
2727 % The format of the ModulateImage method is:
2729 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2730 % ExceptionInfo *exception)
2732 % A description of each parameter follows:
2734 % o image: the image.
2736 % o modulate: Define the percent change in brightness, saturation, and hue.
2738 % o exception: return any errors or warnings in this structure.
2742 static void ModulateHSB(const double percent_hue,
2743 const double percent_saturation,const double percent_brightness,double *red,
2744 double *green,double *blue)
2752 Increase or decrease color brightness, saturation, or hue.
2754 assert(red != (double *) NULL);
2755 assert(green != (double *) NULL);
2756 assert(blue != (double *) NULL);
2757 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2758 hue+=0.5*(0.01*percent_hue-1.0);
2763 saturation*=0.01*percent_saturation;
2764 brightness*=0.01*percent_brightness;
2765 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2768 static void ModulateHSL(const double percent_hue,
2769 const double percent_saturation,const double percent_lightness,double *red,
2770 double *green,double *blue)
2778 Increase or decrease color lightness, saturation, or hue.
2780 assert(red != (double *) NULL);
2781 assert(green != (double *) NULL);
2782 assert(blue != (double *) NULL);
2783 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2784 hue+=0.5*(0.01*percent_hue-1.0);
2789 saturation*=0.01*percent_saturation;
2790 lightness*=0.01*percent_lightness;
2791 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2794 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
2802 Increase or decrease color blackness, whiteness, or hue.
2804 assert(red != (double *) NULL);
2805 assert(green != (double *) NULL);
2806 assert(blue != (double *) NULL);
2807 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2808 hue+=0.5*(0.01*percent_hue-1.0);
2813 blackness*=0.01*percent_blackness;
2814 whiteness*=0.01*percent_whiteness;
2815 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2818 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2819 ExceptionInfo *exception)
2821 #define ModulateImageTag "Modulate/Image"
2856 Initialize modulate table.
2858 assert(image != (Image *) NULL);
2859 assert(image->signature == MagickSignature);
2860 if (image->debug != MagickFalse)
2861 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2862 if (modulate == (char *) NULL)
2863 return(MagickFalse);
2864 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2865 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2866 flags=ParseGeometry(modulate,&geometry_info);
2867 percent_brightness=geometry_info.rho;
2868 percent_saturation=geometry_info.sigma;
2869 if ((flags & SigmaValue) == 0)
2870 percent_saturation=100.0;
2871 percent_hue=geometry_info.xi;
2872 if ((flags & XiValue) == 0)
2874 colorspace=UndefinedColorspace;
2875 artifact=GetImageArtifact(image,"modulate:colorspace");
2876 if (artifact != (const char *) NULL)
2877 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2878 MagickFalse,artifact);
2879 if (image->storage_class == PseudoClass)
2880 for (i=0; i < (ssize_t) image->colors; i++)
2890 red=image->colormap[i].red;
2891 green=image->colormap[i].green;
2892 blue=image->colormap[i].blue;
2897 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2904 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2910 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2921 image_view=AcquireAuthenticCacheView(image,exception);
2922 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2923 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2924 dynamic_number_threads(image,image->columns,image->rows,1)
2926 for (y=0; y < (ssize_t) image->rows; y++)
2934 if (status == MagickFalse)
2936 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2937 if (q == (Quantum *) NULL)
2942 for (x=0; x < (ssize_t) image->columns; x++)
2949 red=(double) GetPixelRed(image,q);
2950 green=(double) GetPixelGreen(image,q);
2951 blue=(double) GetPixelBlue(image,q);
2956 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2963 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2969 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2974 SetPixelRed(image,ClampToQuantum(red),q);
2975 SetPixelGreen(image,ClampToQuantum(green),q);
2976 SetPixelBlue(image,ClampToQuantum(blue),q);
2977 q+=GetPixelChannels(image);
2979 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2981 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2986 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2987 #pragma omp critical (MagickCore_ModulateImage)
2989 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
2990 if (proceed == MagickFalse)
2994 image_view=DestroyCacheView(image_view);
2999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3003 % N e g a t e I m a g e %
3007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3009 % NegateImage() negates the colors in the reference image. The grayscale
3010 % option means that only grayscale values within the image are negated.
3012 % The format of the NegateImage method is:
3014 % MagickBooleanType NegateImage(Image *image,
3015 % const MagickBooleanType grayscale,ExceptionInfo *exception)
3017 % A description of each parameter follows:
3019 % o image: the image.
3021 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3023 % o exception: return any errors or warnings in this structure.
3026 MagickExport MagickBooleanType NegateImage(Image *image,
3027 const MagickBooleanType grayscale,ExceptionInfo *exception)
3029 #define NegateImageTag "Negate/Image"
3046 assert(image != (Image *) NULL);
3047 assert(image->signature == MagickSignature);
3048 if (image->debug != MagickFalse)
3049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3050 if (image->storage_class == PseudoClass)
3051 for (i=0; i < (ssize_t) image->colors; i++)
3056 if (grayscale != MagickFalse)
3057 if ((image->colormap[i].red != image->colormap[i].green) ||
3058 (image->colormap[i].green != image->colormap[i].blue))
3060 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3061 image->colormap[i].red=QuantumRange-image->colormap[i].red;
3062 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3063 image->colormap[i].green=QuantumRange-image->colormap[i].green;
3064 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3065 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
3072 image_view=AcquireAuthenticCacheView(image,exception);
3073 if (grayscale != MagickFalse)
3075 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3076 #pragma omp parallel for schedule(static) shared(progress,status) \
3077 dynamic_number_threads(image,image->columns,image->rows,1)
3079 for (y=0; y < (ssize_t) image->rows; y++)
3090 if (status == MagickFalse)
3092 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3094 if (q == (Quantum *) NULL)
3099 for (x=0; x < (ssize_t) image->columns; x++)
3104 if ((GetPixelMask(image,q) != 0) ||
3105 (IsPixelGray(image,q) != MagickFalse))
3107 q+=GetPixelChannels(image);
3110 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3118 channel=GetPixelChannelMapChannel(image,i);
3119 traits=GetPixelChannelMapTraits(image,channel);
3120 if ((traits & UpdatePixelTrait) == 0)
3122 q[i]=QuantumRange-q[i];
3124 q+=GetPixelChannels(image);
3126 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3127 if (sync == MagickFalse)
3129 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3134 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3135 #pragma omp critical (MagickCore_NegateImage)
3137 proceed=SetImageProgress(image,NegateImageTag,progress++,
3139 if (proceed == MagickFalse)
3143 image_view=DestroyCacheView(image_view);
3149 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3150 #pragma omp parallel for schedule(static) shared(progress,status) \
3151 dynamic_number_threads(image,image->columns,image->rows,1)
3153 for (y=0; y < (ssize_t) image->rows; y++)
3161 if (status == MagickFalse)
3163 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3164 if (q == (Quantum *) NULL)
3169 for (x=0; x < (ssize_t) image->columns; x++)
3174 if (GetPixelMask(image,q) != 0)
3176 q+=GetPixelChannels(image);
3179 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3187 channel=GetPixelChannelMapChannel(image,i);
3188 traits=GetPixelChannelMapTraits(image,channel);
3189 if ((traits & UpdatePixelTrait) == 0)
3191 q[i]=QuantumRange-q[i];
3193 q+=GetPixelChannels(image);
3195 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3197 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3203 #pragma omp critical (MagickCore_NegateImage)
3205 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3206 if (proceed == MagickFalse)
3210 image_view=DestroyCacheView(image_view);
3215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3219 % N o r m a l i z e I m a g e %
3223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3225 % The NormalizeImage() method enhances the contrast of a color image by
3226 % mapping the darkest 2 percent of all pixel to black and the brightest
3227 % 1 percent to white.
3229 % The format of the NormalizeImage method is:
3231 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3233 % A description of each parameter follows:
3235 % o image: the image.
3237 % o exception: return any errors or warnings in this structure.
3240 MagickExport MagickBooleanType NormalizeImage(Image *image,
3241 ExceptionInfo *exception)
3247 black_point=(double) image->columns*image->rows*0.0015;
3248 white_point=(double) image->columns*image->rows*0.9995;
3249 return(ContrastStretchImage(image,black_point,white_point,exception));
3253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3257 % S i g m o i d a l C o n t r a s t I m a g e %
3261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3263 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3264 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3265 % sigmoidal transfer function without saturating highlights or shadows.
3266 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3267 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3268 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3269 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3272 % The format of the SigmoidalContrastImage method is:
3274 % MagickBooleanType SigmoidalContrastImage(Image *image,
3275 % const MagickBooleanType sharpen,const char *levels,
3276 % ExceptionInfo *exception)
3278 % A description of each parameter follows:
3280 % o image: the image.
3282 % o sharpen: Increase or decrease image contrast.
3284 % o alpha: strength of the contrast, the larger the number the more
3285 % 'threshold-like' it becomes.
3287 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3289 % o exception: return any errors or warnings in this structure.
3292 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3293 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3294 ExceptionInfo *exception)
3296 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3317 Sigmoidal with inflexion point moved to b and "slope constant" set to a.
3319 #define Sigmoidal(a,b,x) ( 1.0/(1.0+exp((a)*((b)-(x)))) )
3321 Scaled sigmoidal formula: (1/(1+exp(a*(b-x))) - 1/(1+exp(a*b)))
3323 (1/(1+exp(a*(b-1))) - 1/(1+exp(a*b))).
3324 See http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html and
3325 http://www.cs.dartmouth.edu/farid/downloads/tutorials/fip.pdf.
3327 #define ScaledSigmoidal(a,b,x) ( \
3328 (Sigmoidal((a),(b),(x))-Sigmoidal((a),(b),0.0)) / \
3329 (Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0)) )
3330 #define InverseScaledSigmoidal(a,b,x) ( \
3331 (b) - log( -1.0+1.0/((Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0))*(x)+ \
3332 Sigmoidal((a),(b),0.0)) ) / (a) )
3334 The limit of ScaledSigmoidal as a->0 is the identity, but a=0 gives a
3335 division by zero. This is fixed below by hardwiring the identity when a is
3336 small. This would appear to be safe because the series expansion of the
3337 sigmoidal function around x=b is 1/2-a*(b-x)/4+... so that s(1)-s(0) is
3342 Allocate and initialize sigmoidal maps.
3344 assert(image != (Image *) NULL);
3345 assert(image->signature == MagickSignature);
3346 if (image->debug != MagickFalse)
3347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3348 sigmoidal_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,
3349 sizeof(*sigmoidal_map));
3350 if (sigmoidal_map == (Quantum *) NULL)
3351 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3353 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3354 if (contrast<4.0*MagickEpsilon)
3355 for (i=0; i <= (ssize_t) MaxMap; i++)
3356 sigmoidal_map[i]=ScaleMapToQuantum((double) i);
3357 else if (sharpen != MagickFalse)
3358 for (i=0; i <= (ssize_t) MaxMap; i++)
3359 sigmoidal_map[i]=ScaleMapToQuantum( (double) (MaxMap*
3360 ScaledSigmoidal(contrast,QuantumScale*midpoint,(double) i/MaxMap)));
3362 for (i=0; i <= (ssize_t) MaxMap; i++)
3363 sigmoidal_map[i]=ScaleMapToQuantum((double) (MaxMap*
3364 InverseScaledSigmoidal(contrast,QuantumScale*midpoint,
3365 (double) i/MaxMap)));
3366 if (image->storage_class == PseudoClass)
3367 for (i=0; i < (ssize_t) image->colors; i++)
3370 Sigmoidal-contrast enhance colormap.
3372 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3373 image->colormap[i].red=(double) ClampToQuantum((double) sigmoidal_map[
3374 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))]);
3375 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3376 image->colormap[i].green=(double) ClampToQuantum((double) sigmoidal_map[
3377 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))]);
3378 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3379 image->colormap[i].blue=(double) ClampToQuantum((double) sigmoidal_map[
3380 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))]);
3381 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3382 image->colormap[i].alpha=(double) ClampToQuantum((double) sigmoidal_map[
3383 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))]);
3386 Sigmoidal-contrast enhance image.
3390 image_view=AcquireAuthenticCacheView(image,exception);
3391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3392 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3393 dynamic_number_threads(image,image->columns,image->rows,1)
3395 for (y=0; y < (ssize_t) image->rows; y++)
3403 if (status == MagickFalse)
3405 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3406 if (q == (Quantum *) NULL)
3411 for (x=0; x < (ssize_t) image->columns; x++)
3416 if (GetPixelMask(image,q) != 0)
3418 q+=GetPixelChannels(image);
3421 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3429 channel=GetPixelChannelMapChannel(image,i);
3430 traits=GetPixelChannelMapTraits(image,channel);
3431 if ((traits & UpdatePixelTrait) == 0)
3433 q[i]=ClampToQuantum((double) sigmoidal_map[ScaleQuantumToMap(q[i])]);
3435 q+=GetPixelChannels(image);
3437 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3439 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3444 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3445 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3447 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3449 if (proceed == MagickFalse)
3453 image_view=DestroyCacheView(image_view);
3454 sigmoidal_map=(Quantum *) RelinquishMagickMemory(sigmoidal_map);