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-2011 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/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"
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % A u t o G a m m a I m a g e %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % AutoGammaImage() extract the 'mean' from the image and adjust the image
90 % to try make set its gamma appropriatally.
92 % The format of the AutoGammaImage method is:
94 % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
96 % A description of each parameter follows:
98 % o image: The image to auto-level
100 % o exception: return any errors or warnings in this structure.
103 MagickExport MagickBooleanType AutoGammaImage(Image *image,
104 ExceptionInfo *exception)
119 if (image->sync != MagickFalse)
122 Apply gamma correction equally across all given channels.
124 (void) GetImageMean(image,&mean,&sans,exception);
125 gamma=log(mean*QuantumScale)/log_mean;
126 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
129 Auto-gamma each channel separately.
132 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
140 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
141 if ((traits & UpdatePixelTrait) == 0)
143 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
144 status=GetImageMean(image,&mean,&sans,exception);
145 gamma=log(mean*QuantumScale)/log_mean;
146 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
147 (void) SetPixelChannelMask(image,channel_mask);
148 if (status == MagickFalse)
151 return(status != 0 ? MagickTrue : MagickFalse);
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 % A u t o L e v e l I m a g e %
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 % AutoLevelImage() adjusts the levels of a particular image channel by
166 % scaling the minimum and maximum values to the full quantum range.
168 % The format of the LevelImage method is:
170 % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
172 % A description of each parameter follows:
174 % o image: The image to auto-level
176 % o exception: return any errors or warnings in this structure.
179 MagickExport MagickBooleanType AutoLevelImage(Image *image,
180 ExceptionInfo *exception)
182 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % B r i g h t n e s s C o n t r a s t I m a g e %
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
200 % The format of the BrightnessContrastImage method is:
202 % MagickBooleanType BrightnessContrastImage(Image *image,
203 % const double brightness,const double contrast,ExceptionInfo *exception)
205 % A description of each parameter follows:
207 % o image: the image.
209 % o brightness: the brightness percent (-100 .. 100).
211 % o contrast: the contrast percent (-100 .. 100).
213 % o exception: return any errors or warnings in this structure.
216 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
217 const double brightness,const double contrast,ExceptionInfo *exception)
219 #define BrightnessContastImageTag "BrightnessContast/Image"
231 Compute slope and intercept.
233 assert(image != (Image *) NULL);
234 assert(image->signature == MagickSignature);
235 if (image->debug != MagickFalse)
236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
238 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.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);
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % C l u t I m a g e %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
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
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
277 % The format of the ClutImage method is:
279 % MagickBooleanType ClutImage(Image *image,Image *clut_image,
280 % const PixelInterpolateMethod method,ExceptionInfo *exception)
282 % A description of each parameter follows:
284 % o image: the image, which is replaced by indexed CLUT values
286 % o clut_image: the color lookup table image for replacement color values.
288 % o method: the pixel interpolation method.
290 % o exception: return any errors or warnings in this structure.
293 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
294 const PixelInterpolateMethod method,ExceptionInfo *exception)
296 #define ClutImageTag "Clut/Image"
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)
326 clut_map=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
328 if (clut_map == (double *) NULL)
329 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
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)
341 for (x=0; x <= (ssize_t) MaxMap; x++)
346 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
347 (void) InterpolatePixelChannel(clut_image,clut_view,(PixelChannel) i,
348 method,QuantumScale*x*(clut_image->columns-adjust),QuantumScale*x*
349 (clut_image->rows-adjust),clut_map+x*GetPixelChannels(clut_image)+i,
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)
357 for (y=0; y < (ssize_t) image->rows; y++)
365 if (status == MagickFalse)
367 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
368 if (q == (Quantum *) NULL)
373 for (x=0; x < (ssize_t) image->columns; x++)
378 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
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))
394 SetPixelChannel(clut_image,channel,ClampToQuantum(clut_map[
395 ScaleQuantumToMap(GetPixelChannel(clut_image,channel,q))*
396 GetPixelChannels(clut_image)+channel]),q);
398 q+=GetPixelChannels(image);
400 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
402 if (image->progress_monitor != (MagickProgressMonitor) NULL)
407 #if defined(MAGICKCORE_OPENMP_SUPPORT)
408 #pragma omp critical (MagickCore_ClutImage)
410 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
411 if (proceed == MagickFalse)
415 image_view=DestroyCacheView(image_view);
416 clut_map=(double *) RelinquishMagickMemory(clut_map);
417 if ((clut_image->matte != MagickFalse) &&
418 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
419 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % C o l o r D e c i s i o n L i s t I m a g e %
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
435 % (CCC) file which solely contains one or more color corrections and applies
436 % the correction to the image. Here is a sample CCC file:
438 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
439 % <ColorCorrection id="cc03345">
441 % <Slope> 0.9 1.2 0.5 </Slope>
442 % <Offset> 0.4 -0.5 0.6 </Offset>
443 % <Power> 1.0 0.8 1.5 </Power>
446 % <Saturation> 0.85 </Saturation>
449 % </ColorCorrectionCollection>
451 % which includes the slop, offset, and power for each of the RGB channels
452 % as well as the saturation.
454 % The format of the ColorDecisionListImage method is:
456 % MagickBooleanType ColorDecisionListImage(Image *image,
457 % const char *color_correction_collection,ExceptionInfo *exception)
459 % A description of each parameter follows:
461 % o image: the image.
463 % o color_correction_collection: the color correction collection in XML.
465 % o exception: return any errors or warnings in this structure.
468 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
469 const char *color_correction_collection,ExceptionInfo *exception)
471 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
473 typedef struct _Correction
481 typedef struct _ColorCorrection
496 token[MaxTextExtent];
527 Allocate and initialize cdl maps.
529 assert(image != (Image *) NULL);
530 assert(image->signature == MagickSignature);
531 if (image->debug != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
533 if (color_correction_collection == (const char *) NULL)
535 ccc=NewXMLTree((const char *) color_correction_collection,exception);
536 if (ccc == (XMLTreeInfo *) NULL)
538 cc=GetXMLTreeChild(ccc,"ColorCorrection");
539 if (cc == (XMLTreeInfo *) NULL)
541 ccc=DestroyXMLTree(ccc);
544 color_correction.red.slope=1.0;
545 color_correction.red.offset=0.0;
546 color_correction.red.power=1.0;
547 color_correction.green.slope=1.0;
548 color_correction.green.offset=0.0;
549 color_correction.green.power=1.0;
550 color_correction.blue.slope=1.0;
551 color_correction.blue.offset=0.0;
552 color_correction.blue.power=1.0;
553 color_correction.saturation=0.0;
554 sop=GetXMLTreeChild(cc,"SOPNode");
555 if (sop != (XMLTreeInfo *) NULL)
562 slope=GetXMLTreeChild(sop,"Slope");
563 if (slope != (XMLTreeInfo *) NULL)
565 content=GetXMLTreeContent(slope);
566 p=(const char *) content;
567 for (i=0; (*p != '\0') && (i < 3); i++)
569 GetMagickToken(p,&p,token);
571 GetMagickToken(p,&p,token);
576 color_correction.red.slope=InterpretLocaleValue(token,
582 color_correction.green.slope=InterpretLocaleValue(token,
588 color_correction.blue.slope=InterpretLocaleValue(token,
595 offset=GetXMLTreeChild(sop,"Offset");
596 if (offset != (XMLTreeInfo *) NULL)
598 content=GetXMLTreeContent(offset);
599 p=(const char *) content;
600 for (i=0; (*p != '\0') && (i < 3); i++)
602 GetMagickToken(p,&p,token);
604 GetMagickToken(p,&p,token);
609 color_correction.red.offset=InterpretLocaleValue(token,
615 color_correction.green.offset=InterpretLocaleValue(token,
621 color_correction.blue.offset=InterpretLocaleValue(token,
628 power=GetXMLTreeChild(sop,"Power");
629 if (power != (XMLTreeInfo *) NULL)
631 content=GetXMLTreeContent(power);
632 p=(const char *) content;
633 for (i=0; (*p != '\0') && (i < 3); i++)
635 GetMagickToken(p,&p,token);
637 GetMagickToken(p,&p,token);
642 color_correction.red.power=InterpretLocaleValue(token,
648 color_correction.green.power=InterpretLocaleValue(token,
654 color_correction.blue.power=InterpretLocaleValue(token,
662 sat=GetXMLTreeChild(cc,"SATNode");
663 if (sat != (XMLTreeInfo *) NULL)
668 saturation=GetXMLTreeChild(sat,"Saturation");
669 if (saturation != (XMLTreeInfo *) NULL)
671 content=GetXMLTreeContent(saturation);
672 p=(const char *) content;
673 GetMagickToken(p,&p,token);
674 color_correction.saturation=InterpretLocaleValue(token,
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(dynamic,4)
711 for (i=0; i <= (ssize_t) MaxMap; i++)
713 cdl_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
714 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
715 color_correction.red.offset,color_correction.red.power))));
716 cdl_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
717 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
718 color_correction.green.offset,color_correction.green.power))));
719 cdl_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
720 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
721 color_correction.blue.offset,color_correction.blue.power))));
723 if (image->storage_class == PseudoClass)
726 Apply transfer function to colormap.
728 #if defined(MAGICKCORE_OPENMP_SUPPORT)
729 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
731 for (i=0; i < (ssize_t) image->colors; i++)
736 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
737 0.0722*image->colormap[i].blue;
738 image->colormap[i].red=luma+color_correction.saturation*
739 cdl_map[ScaleQuantumToMap(ClampToQuantum(
740 image->colormap[i].red))].red-luma;
741 image->colormap[i].green=luma+color_correction.saturation*
742 cdl_map[ScaleQuantumToMap(ClampToQuantum(
743 image->colormap[i].green))].green-luma;
744 image->colormap[i].blue=luma+color_correction.saturation*
745 cdl_map[ScaleQuantumToMap(ClampToQuantum(
746 image->colormap[i].blue))].blue-luma;
750 Apply transfer function to image.
754 image_view=AcquireCacheView(image);
755 #if defined(MAGICKCORE_OPENMP_SUPPORT)
756 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
758 for (y=0; y < (ssize_t) image->rows; y++)
769 if (status == MagickFalse)
771 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
772 if (q == (Quantum *) NULL)
777 for (x=0; x < (ssize_t) image->columns; x++)
779 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
780 GetPixelBlue(image,q);
781 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
782 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
783 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
784 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
785 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
786 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
787 q+=GetPixelChannels(image);
789 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
791 if (image->progress_monitor != (MagickProgressMonitor) NULL)
796 #if defined(MAGICKCORE_OPENMP_SUPPORT)
797 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
799 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
800 progress++,image->rows);
801 if (proceed == MagickFalse)
805 image_view=DestroyCacheView(image_view);
806 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % C o n t r a s t I m a g e %
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % ContrastImage() enhances the intensity differences between the lighter and
822 % darker elements of the image. Set sharpen to a MagickTrue to increase the
823 % image contrast otherwise the contrast is reduced.
825 % The format of the ContrastImage method is:
827 % MagickBooleanType ContrastImage(Image *image,
828 % const MagickBooleanType sharpen,ExceptionInfo *exception)
830 % A description of each parameter follows:
832 % o image: the image.
834 % o sharpen: Increase or decrease image contrast.
836 % o exception: return any errors or warnings in this structure.
840 static void Contrast(const int sign,double *red,double *green,double *blue)
848 Enhance contrast: dark color become darker, light color become lighter.
850 assert(red != (double *) NULL);
851 assert(green != (double *) NULL);
852 assert(blue != (double *) NULL);
856 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
857 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
859 if (brightness > 1.0)
862 if (brightness < 0.0)
864 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
867 MagickExport MagickBooleanType ContrastImage(Image *image,
868 const MagickBooleanType sharpen,ExceptionInfo *exception)
870 #define ContrastImageTag "Contrast/Image"
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 sign=sharpen != MagickFalse ? 1 : -1;
895 if (image->storage_class == PseudoClass)
898 Contrast enhance colormap.
900 for (i=0; i < (ssize_t) image->colors; i++)
901 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
902 &image->colormap[i].blue);
905 Contrast enhance image.
909 image_view=AcquireCacheView(image);
910 #if defined(MAGICKCORE_OPENMP_SUPPORT)
911 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
913 for (y=0; y < (ssize_t) image->rows; y++)
926 if (status == MagickFalse)
928 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
929 if (q == (Quantum *) NULL)
934 for (x=0; x < (ssize_t) image->columns; x++)
936 red=(double) GetPixelRed(image,q);
937 green=(double) GetPixelGreen(image,q);
938 blue=(double) GetPixelBlue(image,q);
939 Contrast(sign,&red,&green,&blue);
940 SetPixelRed(image,ClampToQuantum(red),q);
941 SetPixelGreen(image,ClampToQuantum(green),q);
942 SetPixelBlue(image,ClampToQuantum(blue),q);
943 q+=GetPixelChannels(image);
945 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
947 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952 #if defined(MAGICKCORE_OPENMP_SUPPORT)
953 #pragma omp critical (MagickCore_ContrastImage)
955 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
956 if (proceed == MagickFalse)
960 image_view=DestroyCacheView(image_view);
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % C o n t r a s t S t r e t c h I m a g e %
973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % ContrastStretchImage() is a simple image enhancement technique that attempts
976 % to improve the contrast in an image by `stretching' the range of intensity
977 % values it contains to span a desired range of values. It differs from the
978 % more sophisticated histogram equalization in that it can only apply a
979 % linear scaling function to the image pixel values. As a result the
980 % `enhancement' is less harsh.
982 % The format of the ContrastStretchImage method is:
984 % MagickBooleanType ContrastStretchImage(Image *image,
985 % const char *levels,ExceptionInfo *exception)
987 % A description of each parameter follows:
989 % o image: the image.
991 % o black_point: the black point.
993 % o white_point: the white point.
995 % o levels: Specify the levels where the black and white points have the
996 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
998 % o exception: return any errors or warnings in this structure.
1001 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1002 const double black_point,const double white_point,ExceptionInfo *exception)
1004 #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1005 #define ContrastStretchImageTag "ContrastStretch/Image"
1029 Allocate histogram and stretch map.
1031 assert(image != (Image *) NULL);
1032 assert(image->signature == MagickSignature);
1033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1035 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1036 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
1037 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,
1038 GetPixelChannels(image)*sizeof(*histogram));
1039 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1040 GetPixelChannels(image)*sizeof(*stretch_map));
1041 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1042 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
1044 if (stretch_map != (double *) NULL)
1045 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1046 if (histogram != (double *) NULL)
1047 histogram=(double *) RelinquishMagickMemory(histogram);
1048 if (white != (double *) NULL)
1049 white=(double *) RelinquishMagickMemory(white);
1050 if (black != (double *) NULL)
1051 black=(double *) RelinquishMagickMemory(black);
1052 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1059 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1060 sizeof(*histogram));
1061 image_view=AcquireCacheView(image);
1062 for (y=0; y < (ssize_t) image->rows; y++)
1064 register const Quantum
1070 if (status == MagickFalse)
1072 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1073 if (p == (const Quantum *) NULL)
1078 for (x=0; x < (ssize_t) image->columns; x++)
1083 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1084 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1085 p+=GetPixelChannels(image);
1089 Find the histogram boundaries by locating the black/white levels.
1091 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1092 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1094 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1103 white[i]=MaxRange(QuantumRange);
1105 for (j=0; j <= (ssize_t) MaxMap; j++)
1107 intensity+=histogram[GetPixelChannels(image)*j+i];
1108 if (intensity > black_point)
1111 black[i]=(MagickRealType) j;
1113 for (j=(ssize_t) MaxMap; j != 0; j--)
1115 intensity+=histogram[GetPixelChannels(image)*j+i];
1116 if (intensity > ((double) image->columns*image->rows-white_point))
1119 white[i]=(MagickRealType) j;
1121 histogram=(double *) RelinquishMagickMemory(histogram);
1123 Stretch the histogram to create the stretched image mapping.
1125 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1126 sizeof(*stretch_map));
1127 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1128 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1130 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1135 for (j=0; j <= (ssize_t) MaxMap; j++)
1137 if (j < (ssize_t) black[i])
1138 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1140 if (j > (ssize_t) white[i])
1141 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1144 if (black[i] != white[i])
1145 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1146 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1147 (white[i]-black[i])));
1150 if (image->storage_class == PseudoClass)
1156 Stretch-contrast colormap.
1158 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1159 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1161 for (j=0; j < (ssize_t) image->colors; j++)
1163 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1165 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1166 if (black[i] != white[i])
1167 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1168 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
1170 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1172 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1173 if (black[i] != white[i])
1174 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1175 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
1177 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1179 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1180 if (black[i] != white[i])
1181 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1182 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
1184 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1186 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1187 if (black[i] != white[i])
1188 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1189 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
1194 Stretch-contrast image.
1198 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1199 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1201 for (y=0; y < (ssize_t) image->rows; y++)
1209 if (status == MagickFalse)
1211 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1212 if (q == (Quantum *) NULL)
1217 for (x=0; x < (ssize_t) image->columns; x++)
1222 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1227 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1228 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1229 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1230 ScaleQuantumToMap(q[i])+i]);
1232 q+=GetPixelChannels(image);
1234 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1236 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1241 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1242 #pragma omp critical (MagickCore_ContrastStretchImage)
1244 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1246 if (proceed == MagickFalse)
1250 image_view=DestroyCacheView(image_view);
1251 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1252 white=(double *) RelinquishMagickMemory(white);
1253 black=(double *) RelinquishMagickMemory(black);
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % E n h a n c e I m a g e %
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % EnhanceImage() applies a digital filter that improves the quality of a
1271 % The format of the EnhanceImage method is:
1273 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1275 % A description of each parameter follows:
1277 % o image: the image.
1279 % o exception: return any errors or warnings in this structure.
1282 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1284 #define EnhancePixel(weight) \
1285 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1286 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1287 enhance_image,channel,q); \
1288 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1289 mean)*distance*distance; \
1290 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1291 QuantumRange/25.0f)) \
1293 aggregate+=(weight)*r[i]; \
1294 total_weight+=(weight); \
1296 r+=GetPixelChannels(image);
1297 #define EnhanceImageTag "Enhance/Image"
1316 Initialize enhanced image attributes.
1318 assert(image != (const Image *) NULL);
1319 assert(image->signature == MagickSignature);
1320 if (image->debug != MagickFalse)
1321 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1322 assert(exception != (ExceptionInfo *) NULL);
1323 assert(exception->signature == MagickSignature);
1324 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1326 if (enhance_image == (Image *) NULL)
1327 return((Image *) NULL);
1328 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1330 enhance_image=DestroyImage(enhance_image);
1331 return((Image *) NULL);
1338 image_view=AcquireCacheView(image);
1339 enhance_view=AcquireCacheView(enhance_image);
1340 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1341 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1343 for (y=0; y < (ssize_t) image->rows; y++)
1345 register const Quantum
1357 if (status == MagickFalse)
1359 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1360 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1362 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1367 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1368 for (x=0; x < (ssize_t) image->columns; x++)
1373 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1389 register const Quantum
1392 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1393 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1394 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1395 if ((traits == UndefinedPixelTrait) ||
1396 (enhance_traits == UndefinedPixelTrait))
1398 SetPixelChannel(enhance_image,channel,p[center+i],q);
1399 if ((enhance_traits & CopyPixelTrait) != 0)
1402 Compute weighted average of target pixel color components.
1407 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1408 EnhancePixel(8.0); EnhancePixel(5.0);
1409 r=p+1*GetPixelChannels(image)*(image->columns+4);
1410 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1411 EnhancePixel(20.0); EnhancePixel(8.0);
1412 r=p+2*GetPixelChannels(image)*(image->columns+4);
1413 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1414 EnhancePixel(40.0); EnhancePixel(10.0);
1415 r=p+3*GetPixelChannels(image)*(image->columns+4);
1416 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1417 EnhancePixel(20.0); EnhancePixel(8.0);
1418 r=p+4*GetPixelChannels(image)*(image->columns+4);
1419 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1420 EnhancePixel(8.0); EnhancePixel(5.0);
1421 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1424 p+=GetPixelChannels(image);
1425 q+=GetPixelChannels(enhance_image);
1427 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1429 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1434 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1435 #pragma omp critical (MagickCore_EnhanceImage)
1437 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1438 if (proceed == MagickFalse)
1442 enhance_view=DestroyCacheView(enhance_view);
1443 image_view=DestroyCacheView(image_view);
1444 return(enhance_image);
1448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452 % E q u a l i z e I m a g e %
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % EqualizeImage() applies a histogram equalization to the image.
1460 % The format of the EqualizeImage method is:
1462 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1464 % A description of each parameter follows:
1466 % o image: the image.
1468 % o exception: return any errors or warnings in this structure.
1471 MagickExport MagickBooleanType EqualizeImage(Image *image,
1472 ExceptionInfo *exception)
1474 #define EqualizeImageTag "Equalize/Image"
1486 black[MaxPixelChannels],
1490 white[MaxPixelChannels];
1499 Allocate and initialize histogram arrays.
1501 assert(image != (Image *) NULL);
1502 assert(image->signature == MagickSignature);
1503 if (image->debug != MagickFalse)
1504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1505 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1506 GetPixelChannels(image)*sizeof(*equalize_map));
1507 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1508 GetPixelChannels(image)*sizeof(*histogram));
1509 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1510 GetPixelChannels(image)*sizeof(*map));
1511 if ((equalize_map == (MagickRealType *) NULL) ||
1512 (histogram == (MagickRealType *) NULL) ||
1513 (map == (MagickRealType *) NULL))
1515 if (map != (MagickRealType *) NULL)
1516 map=(MagickRealType *) RelinquishMagickMemory(map);
1517 if (histogram != (MagickRealType *) NULL)
1518 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1519 if (equalize_map != (MagickRealType *) NULL)
1520 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1521 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1528 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1529 sizeof(*histogram));
1530 image_view=AcquireCacheView(image);
1531 for (y=0; y < (ssize_t) image->rows; y++)
1533 register const Quantum
1539 if (status == MagickFalse)
1541 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1542 if (p == (const Quantum *) NULL)
1547 for (x=0; x < (ssize_t) image->columns; x++)
1552 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1553 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1554 p+=GetPixelChannels(image);
1558 Integrate the histogram to get the equalization map.
1560 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1561 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1563 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1572 for (j=0; j <= (ssize_t) MaxMap; j++)
1574 intensity+=histogram[GetPixelChannels(image)*j+i];
1575 map[GetPixelChannels(image)*j+i]=intensity;
1578 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1579 sizeof(*equalize_map));
1580 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1581 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1583 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1589 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1590 if (black[i] != white[i])
1591 for (j=0; j <= (ssize_t) MaxMap; j++)
1592 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1593 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1594 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1596 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1597 map=(MagickRealType *) RelinquishMagickMemory(map);
1598 if (image->storage_class == PseudoClass)
1606 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1607 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1609 for (j=0; j < (ssize_t) image->colors; j++)
1611 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1612 (white[i]!= black[i]))
1614 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1615 if (black[i] != white[i])
1616 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
1617 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
1619 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1620 (white[i]!= black[i]))
1622 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1623 if (black[i] != white[i])
1624 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
1625 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
1627 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1628 (white[i]!= black[i]))
1630 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1631 if (black[i] != white[i])
1632 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
1633 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
1635 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1636 (white[i]!= black[i]))
1638 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1639 if (black[i] != white[i])
1640 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
1641 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
1649 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1650 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1652 for (y=0; y < (ssize_t) image->rows; y++)
1660 if (status == MagickFalse)
1662 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1663 if (q == (Quantum *) NULL)
1668 for (x=0; x < (ssize_t) image->columns; x++)
1673 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1678 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1679 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1680 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1681 ScaleQuantumToMap(q[i])+i]);
1683 q+=GetPixelChannels(image);
1685 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1687 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1692 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1693 #pragma omp critical (MagickCore_EqualizeImage)
1695 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1696 if (proceed == MagickFalse)
1700 image_view=DestroyCacheView(image_view);
1701 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % G a m m a I m a g e %
1714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1716 % GammaImage() gamma-corrects a particular image channel. The same
1717 % image viewed on different devices will have perceptual differences in the
1718 % way the image's intensities are represented on the screen. Specify
1719 % individual gamma levels for the red, green, and blue channels, or adjust
1720 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1722 % You can also reduce the influence of a particular channel with a gamma
1725 % The format of the GammaImage method is:
1727 % MagickBooleanType GammaImage(Image *image,const double gamma,
1728 % ExceptionInfo *exception)
1730 % A description of each parameter follows:
1732 % o image: the image.
1734 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1736 % o gamma: the image gamma.
1739 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1740 ExceptionInfo *exception)
1742 #define GammaCorrectImageTag "GammaCorrect/Image"
1763 Allocate and initialize gamma maps.
1765 assert(image != (Image *) NULL);
1766 assert(image->signature == MagickSignature);
1767 if (image->debug != MagickFalse)
1768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1771 gamma_map=(double *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1772 if (gamma_map == (double *) NULL)
1773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1775 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1777 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1778 #pragma omp parallel for
1780 for (i=0; i <= (ssize_t) MaxMap; i++)
1781 gamma_map[i]=(MagickRealType) ScaleMapToQuantum((
1782 MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma)));
1783 if (image->storage_class == PseudoClass)
1786 Gamma-correct colormap.
1788 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1789 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1791 for (i=0; i < (ssize_t) image->colors; i++)
1793 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1794 image->colormap[i].red=gamma_map[
1795 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
1796 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1797 image->colormap[i].green=gamma_map[
1798 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
1799 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1800 image->colormap[i].blue=gamma_map[
1801 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
1802 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1803 image->colormap[i].alpha=gamma_map[
1804 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
1808 Gamma-correct image.
1812 image_view=AcquireCacheView(image);
1813 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1814 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1816 for (y=0; y < (ssize_t) image->rows; y++)
1824 if (status == MagickFalse)
1826 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1827 if (q == (Quantum *) NULL)
1832 for (x=0; x < (ssize_t) image->columns; x++)
1837 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1842 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1843 if ((traits & UpdatePixelTrait) != 0)
1844 q[i]=ClampToQuantum(gamma_map[ScaleQuantumToMap(q[i])]);
1846 q+=GetPixelChannels(image);
1848 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1850 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1855 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1856 #pragma omp critical (MagickCore_GammaImage)
1858 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1860 if (proceed == MagickFalse)
1864 image_view=DestroyCacheView(image_view);
1865 gamma_map=(double *) RelinquishMagickMemory(gamma_map);
1866 if (image->gamma != 0.0)
1867 image->gamma*=gamma;
1872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1876 % H a l d C l u t I m a g e %
1880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1882 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1883 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1884 % Create it with the HALD coder. You can apply any color transformation to
1885 % the Hald image and then use this method to apply the transform to the
1888 % The format of the HaldClutImage method is:
1890 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1891 % ExceptionInfo *exception)
1893 % A description of each parameter follows:
1895 % o image: the image, which is replaced by indexed CLUT values
1897 % o hald_image: the color lookup table image for replacement color values.
1899 % o exception: return any errors or warnings in this structure.
1903 static inline size_t MagickMin(const size_t x,const size_t y)
1910 MagickExport MagickBooleanType HaldClutImage(Image *image,
1911 const Image *hald_image,ExceptionInfo *exception)
1913 #define HaldClutImageTag "Clut/Image"
1915 typedef struct _HaldInfo
1947 assert(image != (Image *) NULL);
1948 assert(image->signature == MagickSignature);
1949 if (image->debug != MagickFalse)
1950 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1951 assert(hald_image != (Image *) NULL);
1952 assert(hald_image->signature == MagickSignature);
1953 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1954 return(MagickFalse);
1955 if (image->matte == MagickFalse)
1956 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1962 length=MagickMin(hald_image->columns,hald_image->rows);
1963 for (level=2; (level*level*level) < length; level++) ;
1965 cube_size=level*level;
1966 width=(double) hald_image->columns;
1967 GetPixelInfo(hald_image,&zero);
1968 image_view=AcquireCacheView(image);
1969 hald_view=AcquireCacheView(hald_image);
1970 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1971 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1973 for (y=0; y < (ssize_t) image->rows; y++)
1981 if (status == MagickFalse)
1983 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1984 if (q == (Quantum *) NULL)
1989 for (x=0; x < (ssize_t) image->columns; x++)
2004 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2005 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2006 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2007 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2008 point.x-=floor(point.x);
2009 point.y-=floor(point.y);
2010 point.z-=floor(point.z);
2012 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2013 fmod(offset,width),floor(offset/width),&pixel1,exception);
2015 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2016 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2018 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2021 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2022 fmod(offset,width),floor(offset/width),&pixel1,exception);
2023 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2024 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2026 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2029 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2031 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2032 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2033 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2034 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2035 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2036 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2037 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2038 (image->colorspace == CMYKColorspace))
2039 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2040 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2041 (image->matte != MagickFalse))
2042 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2043 q+=GetPixelChannels(image);
2045 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2047 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2053 #pragma omp critical (MagickCore_HaldClutImage)
2055 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2056 if (proceed == MagickFalse)
2060 hald_view=DestroyCacheView(hald_view);
2061 image_view=DestroyCacheView(image_view);
2066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070 % L e v e l I m a g e %
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 % LevelImage() adjusts the levels of a particular image channel by
2077 % scaling the colors falling between specified white and black points to
2078 % the full available quantum range.
2080 % The parameters provided represent the black, and white points. The black
2081 % point specifies the darkest color in the image. Colors darker than the
2082 % black point are set to zero. White point specifies the lightest color in
2083 % the image. Colors brighter than the white point are set to the maximum
2086 % If a '!' flag is given, map black and white colors to the given levels
2087 % rather than mapping those levels to black and white. See
2088 % LevelizeImage() below.
2090 % Gamma specifies a gamma correction to apply to the image.
2092 % The format of the LevelImage method is:
2094 % MagickBooleanType LevelImage(Image *image,const double black_point,
2095 % const double white_point,const double gamma,ExceptionInfo *exception)
2097 % A description of each parameter follows:
2099 % o image: the image.
2101 % o black_point: The level to map zero (black) to.
2103 % o white_point: The level to map QuantumRange (white) to.
2105 % o exception: return any errors or warnings in this structure.
2108 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2109 const double white_point,const double gamma,ExceptionInfo *exception)
2111 #define LevelImageTag "Level/Image"
2112 #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
2113 pow(scale*((double) (x)-black_point),1.0/gamma)))
2134 Allocate and initialize levels map.
2136 assert(image != (Image *) NULL);
2137 assert(image->signature == MagickSignature);
2138 if (image->debug != MagickFalse)
2139 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2140 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2141 if (image->storage_class == PseudoClass)
2142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2143 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2145 for (i=0; i < (ssize_t) image->colors; i++)
2150 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2151 image->colormap[i].red=(double) LevelQuantum(
2152 image->colormap[i].red);
2153 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2154 image->colormap[i].green=(double) LevelQuantum(
2155 image->colormap[i].green);
2156 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2157 image->colormap[i].blue=(double) LevelQuantum(
2158 image->colormap[i].blue);
2159 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2160 image->colormap[i].alpha=(double) LevelQuantum(
2161 image->colormap[i].alpha);
2168 image_view=AcquireCacheView(image);
2169 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2170 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2172 for (y=0; y < (ssize_t) image->rows; y++)
2180 if (status == MagickFalse)
2182 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2183 if (q == (Quantum *) NULL)
2188 for (x=0; x < (ssize_t) image->columns; x++)
2193 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2198 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2199 if ((traits == UndefinedPixelTrait) ||
2200 ((traits & UpdatePixelTrait) == 0))
2202 q[i]=LevelQuantum(q[i]);
2204 q+=GetPixelChannels(image);
2206 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2208 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2213 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2214 #pragma omp critical (MagickCore_LevelImage)
2216 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2217 if (proceed == MagickFalse)
2221 image_view=DestroyCacheView(image_view);
2226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2230 % L e v e l i z e I m a g e %
2234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2236 % LevelizeImage() applies the reversed LevelImage() operation to just
2237 % the specific channels specified. It compresses the full range of color
2238 % values, so that they lie between the given black and white points. Gamma is
2239 % applied before the values are mapped.
2241 % LevelizeImage() can be called with by using a +level command line
2242 % API option, or using a '!' on a -level or LevelImage() geometry string.
2244 % It can be used for example de-contrast a greyscale image to the exact
2245 % levels specified. Or by using specific levels for each channel of an image
2246 % you can convert a gray-scale image to any linear color gradient, according
2249 % The format of the LevelizeImage method is:
2251 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2252 % const double white_point,const double gamma,ExceptionInfo *exception)
2254 % A description of each parameter follows:
2256 % o image: the image.
2258 % o black_point: The level to map zero (black) to.
2260 % o white_point: The level to map QuantumRange (white) to.
2262 % o gamma: adjust gamma by this factor before mapping values.
2264 % o exception: return any errors or warnings in this structure.
2267 MagickExport MagickBooleanType LevelizeImage(Image *image,
2268 const double black_point,const double white_point,const double gamma,
2269 ExceptionInfo *exception)
2271 #define LevelizeImageTag "Levelize/Image"
2272 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2273 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2292 Allocate and initialize levels map.
2294 assert(image != (Image *) NULL);
2295 assert(image->signature == MagickSignature);
2296 if (image->debug != MagickFalse)
2297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2298 if (image->storage_class == PseudoClass)
2299 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2300 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2302 for (i=0; i < (ssize_t) image->colors; i++)
2307 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2308 image->colormap[i].red=(double) LevelizeValue(
2309 image->colormap[i].red);
2310 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2311 image->colormap[i].green=(double) LevelizeValue(
2312 image->colormap[i].green);
2313 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2314 image->colormap[i].blue=(double) LevelizeValue(
2315 image->colormap[i].blue);
2316 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2317 image->colormap[i].alpha=(double) LevelizeValue(
2318 image->colormap[i].alpha);
2325 image_view=AcquireCacheView(image);
2326 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2327 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2329 for (y=0; y < (ssize_t) image->rows; y++)
2337 if (status == MagickFalse)
2339 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2340 if (q == (Quantum *) NULL)
2345 for (x=0; x < (ssize_t) image->columns; x++)
2350 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2355 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2356 if ((traits & UpdatePixelTrait) != 0)
2357 q[i]=LevelizeValue(q[i]);
2359 q+=GetPixelChannels(image);
2361 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2363 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2368 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2369 #pragma omp critical (MagickCore_LevelizeImage)
2371 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2372 if (proceed == MagickFalse)
2376 image_view=DestroyCacheView(image_view);
2381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2385 % L e v e l I m a g e C o l o r s %
2389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2391 % LevelImageColors() maps the given color to "black" and "white" values,
2392 % linearly spreading out the colors, and level values on a channel by channel
2393 % bases, as per LevelImage(). The given colors allows you to specify
2394 % different level ranges for each of the color channels separately.
2396 % If the boolean 'invert' is set true the image values will modifyed in the
2397 % reverse direction. That is any existing "black" and "white" colors in the
2398 % image will become the color values given, with all other values compressed
2399 % appropriatally. This effectivally maps a greyscale gradient into the given
2402 % The format of the LevelImageColors method is:
2404 % MagickBooleanType LevelImageColors(Image *image,
2405 % const PixelInfo *black_color,const PixelInfo *white_color,
2406 % const MagickBooleanType invert,ExceptionInfo *exception)
2408 % A description of each parameter follows:
2410 % o image: the image.
2412 % o black_color: The color to map black to/from
2414 % o white_point: The color to map white to/from
2416 % o invert: if true map the colors (levelize), rather than from (level)
2418 % o exception: return any errors or warnings in this structure.
2421 MagickExport MagickBooleanType LevelImageColors(Image *image,
2422 const PixelInfo *black_color,const PixelInfo *white_color,
2423 const MagickBooleanType invert,ExceptionInfo *exception)
2432 Allocate and initialize levels map.
2434 assert(image != (Image *) NULL);
2435 assert(image->signature == MagickSignature);
2436 if (image->debug != MagickFalse)
2437 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2439 if (invert == MagickFalse)
2441 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2443 channel_mask=SetPixelChannelMask(image,RedChannel);
2444 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2446 (void) SetPixelChannelMask(image,channel_mask);
2448 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2450 channel_mask=SetPixelChannelMask(image,GreenChannel);
2451 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2453 (void) SetPixelChannelMask(image,channel_mask);
2455 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2457 channel_mask=SetPixelChannelMask(image,BlueChannel);
2458 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2460 (void) SetPixelChannelMask(image,channel_mask);
2462 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2463 (image->colorspace == CMYKColorspace))
2465 channel_mask=SetPixelChannelMask(image,BlackChannel);
2466 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2468 (void) SetPixelChannelMask(image,channel_mask);
2470 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2471 (image->matte == MagickTrue))
2473 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2474 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2476 (void) SetPixelChannelMask(image,channel_mask);
2481 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2483 channel_mask=SetPixelChannelMask(image,RedChannel);
2484 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2486 (void) SetPixelChannelMask(image,channel_mask);
2488 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2490 channel_mask=SetPixelChannelMask(image,GreenChannel);
2491 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2493 (void) SetPixelChannelMask(image,channel_mask);
2495 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2497 channel_mask=SetPixelChannelMask(image,BlueChannel);
2498 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2500 (void) SetPixelChannelMask(image,channel_mask);
2502 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2503 (image->colorspace == CMYKColorspace))
2505 channel_mask=SetPixelChannelMask(image,BlackChannel);
2506 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2508 (void) SetPixelChannelMask(image,channel_mask);
2510 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2511 (image->matte == MagickTrue))
2513 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2514 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2516 (void) SetPixelChannelMask(image,channel_mask);
2519 return(status == 0 ? MagickFalse : MagickTrue);
2523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2527 % L i n e a r S t r e t c h I m a g e %
2531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2533 % LinearStretchImage() discards any pixels below the black point and above
2534 % the white point and levels the remaining pixels.
2536 % The format of the LinearStretchImage method is:
2538 % MagickBooleanType LinearStretchImage(Image *image,
2539 % const double black_point,const double white_point,
2540 % ExceptionInfo *exception)
2542 % A description of each parameter follows:
2544 % o image: the image.
2546 % o black_point: the black point.
2548 % o white_point: the white point.
2550 % o exception: return any errors or warnings in this structure.
2553 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2554 const double black_point,const double white_point,ExceptionInfo *exception)
2556 #define LinearStretchImageTag "LinearStretch/Image"
2574 Allocate histogram and linear map.
2576 assert(image != (Image *) NULL);
2577 assert(image->signature == MagickSignature);
2578 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2579 sizeof(*histogram));
2580 if (histogram == (MagickRealType *) NULL)
2581 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2586 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2587 image_view=AcquireCacheView(image);
2588 for (y=0; y < (ssize_t) image->rows; y++)
2590 register const Quantum
2596 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2597 if (p == (const Quantum *) NULL)
2599 for (x=0; x < (ssize_t) image->columns; x++)
2601 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2602 p+=GetPixelChannels(image);
2605 image_view=DestroyCacheView(image_view);
2607 Find the histogram boundaries by locating the black and white point levels.
2610 for (black=0; black < (ssize_t) MaxMap; black++)
2612 intensity+=histogram[black];
2613 if (intensity >= black_point)
2617 for (white=(ssize_t) MaxMap; white != 0; white--)
2619 intensity+=histogram[white];
2620 if (intensity >= white_point)
2623 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2624 status=LevelImage(image,(double) black,(double) white,1.0,&image->exception);
2629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2633 % M o d u l a t e I m a g e %
2637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2639 % ModulateImage() lets you control the brightness, saturation, and hue
2640 % of an image. Modulate represents the brightness, saturation, and hue
2641 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2642 % modulation is lightness, saturation, and hue. And if the colorspace is
2643 % HWB, use blackness, whiteness, and hue.
2645 % The format of the ModulateImage method is:
2647 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2648 % ExceptionInfo *exception)
2650 % A description of each parameter follows:
2652 % o image: the image.
2654 % o modulate: Define the percent change in brightness, saturation, and hue.
2656 % o exception: return any errors or warnings in this structure.
2660 static void ModulateHSB(const double percent_hue,
2661 const double percent_saturation,const double percent_brightness,double *red,
2662 double *green,double *blue)
2670 Increase or decrease color brightness, saturation, or hue.
2672 assert(red != (double *) NULL);
2673 assert(green != (double *) NULL);
2674 assert(blue != (double *) NULL);
2675 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2676 hue+=0.5*(0.01*percent_hue-1.0);
2681 saturation*=0.01*percent_saturation;
2682 brightness*=0.01*percent_brightness;
2683 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2686 static void ModulateHSL(const double percent_hue,
2687 const double percent_saturation,const double percent_lightness,double *red,
2688 double *green,double *blue)
2696 Increase or decrease color lightness, saturation, or hue.
2698 assert(red != (double *) NULL);
2699 assert(green != (double *) NULL);
2700 assert(blue != (double *) NULL);
2701 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2702 hue+=0.5*(0.01*percent_hue-1.0);
2707 saturation*=0.01*percent_saturation;
2708 lightness*=0.01*percent_lightness;
2709 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2712 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
2720 Increase or decrease color blackness, whiteness, or hue.
2722 assert(red != (double *) NULL);
2723 assert(green != (double *) NULL);
2724 assert(blue != (double *) NULL);
2725 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2726 hue+=0.5*(0.01*percent_hue-1.0);
2731 blackness*=0.01*percent_blackness;
2732 whiteness*=0.01*percent_whiteness;
2733 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2736 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2737 ExceptionInfo *exception)
2739 #define ModulateImageTag "Modulate/Image"
2774 Initialize modulate table.
2776 assert(image != (Image *) NULL);
2777 assert(image->signature == MagickSignature);
2778 if (image->debug != MagickFalse)
2779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2780 if (modulate == (char *) NULL)
2781 return(MagickFalse);
2782 flags=ParseGeometry(modulate,&geometry_info);
2783 percent_brightness=geometry_info.rho;
2784 percent_saturation=geometry_info.sigma;
2785 if ((flags & SigmaValue) == 0)
2786 percent_saturation=100.0;
2787 percent_hue=geometry_info.xi;
2788 if ((flags & XiValue) == 0)
2790 colorspace=UndefinedColorspace;
2791 artifact=GetImageArtifact(image,"modulate:colorspace");
2792 if (artifact != (const char *) NULL)
2793 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2794 MagickFalse,artifact);
2795 if (image->storage_class == PseudoClass)
2800 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2801 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2803 for (i=0; i < (ssize_t) image->colors; i++)
2808 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2809 &image->colormap[i].red,&image->colormap[i].green,
2810 &image->colormap[i].blue);
2816 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2817 &image->colormap[i].red,&image->colormap[i].green,
2818 &image->colormap[i].blue);
2823 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2824 &image->colormap[i].red,&image->colormap[i].green,
2825 &image->colormap[i].blue);
2835 image_view=AcquireCacheView(image);
2836 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2837 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2839 for (y=0; y < (ssize_t) image->rows; y++)
2852 if (status == MagickFalse)
2854 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2855 if (q == (Quantum *) NULL)
2860 for (x=0; x < (ssize_t) image->columns; x++)
2862 red=(double) GetPixelRed(image,q);
2863 green=(double) GetPixelGreen(image,q);
2864 blue=(double) GetPixelBlue(image,q);
2869 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2876 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2882 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2887 SetPixelRed(image,ClampToQuantum(red),q);
2888 SetPixelGreen(image,ClampToQuantum(green),q);
2889 SetPixelBlue(image,ClampToQuantum(blue),q);
2890 q+=GetPixelChannels(image);
2892 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2894 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2899 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2900 #pragma omp critical (MagickCore_ModulateImage)
2902 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
2903 if (proceed == MagickFalse)
2907 image_view=DestroyCacheView(image_view);
2912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2916 % N e g a t e I m a g e %
2920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2922 % NegateImage() negates the colors in the reference image. The grayscale
2923 % option means that only grayscale values within the image are negated.
2925 % The format of the NegateImage method is:
2927 % MagickBooleanType NegateImage(Image *image,
2928 % const MagickBooleanType grayscale,ExceptionInfo *exception)
2930 % A description of each parameter follows:
2932 % o image: the image.
2934 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
2936 % o exception: return any errors or warnings in this structure.
2939 MagickExport MagickBooleanType NegateImage(Image *image,
2940 const MagickBooleanType grayscale,ExceptionInfo *exception)
2942 #define NegateImageTag "Negate/Image"
2959 assert(image != (Image *) NULL);
2960 assert(image->signature == MagickSignature);
2961 if (image->debug != MagickFalse)
2962 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2963 if (image->storage_class == PseudoClass)
2968 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2969 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2971 for (i=0; i < (ssize_t) image->colors; i++)
2973 if (grayscale != MagickFalse)
2974 if ((image->colormap[i].red != image->colormap[i].green) ||
2975 (image->colormap[i].green != image->colormap[i].blue))
2977 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2978 image->colormap[i].red=(Quantum) QuantumRange-
2979 image->colormap[i].red;
2980 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2981 image->colormap[i].green=(Quantum) QuantumRange-
2982 image->colormap[i].green;
2983 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2984 image->colormap[i].blue=(Quantum) QuantumRange-
2985 image->colormap[i].blue;
2993 image_view=AcquireCacheView(image);
2994 if (grayscale != MagickFalse)
2996 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2997 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2999 for (y=0; y < (ssize_t) image->rows; y++)
3010 if (status == MagickFalse)
3012 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3014 if (q == (Quantum *) NULL)
3019 for (x=0; x < (ssize_t) image->columns; x++)
3024 if (IsPixelGray(image,q) != MagickFalse)
3026 q+=GetPixelChannels(image);
3029 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3034 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3035 if ((traits & UpdatePixelTrait) != 0)
3036 q[i]=QuantumRange-q[i];
3038 q+=GetPixelChannels(image);
3040 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3041 if (sync == MagickFalse)
3043 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3048 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3049 #pragma omp critical (MagickCore_NegateImage)
3051 proceed=SetImageProgress(image,NegateImageTag,progress++,
3053 if (proceed == MagickFalse)
3057 image_view=DestroyCacheView(image_view);
3063 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3064 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3066 for (y=0; y < (ssize_t) image->rows; y++)
3074 if (status == MagickFalse)
3076 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3077 if (q == (Quantum *) NULL)
3082 for (x=0; x < (ssize_t) image->columns; x++)
3087 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3092 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3093 if ((traits & UpdatePixelTrait) != 0)
3094 q[i]=QuantumRange-q[i];
3096 q+=GetPixelChannels(image);
3098 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3100 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3105 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3106 #pragma omp critical (MagickCore_NegateImage)
3108 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3109 if (proceed == MagickFalse)
3113 image_view=DestroyCacheView(image_view);
3118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3122 % N o r m a l i z e I m a g e %
3126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3128 % NormalizeImage() enhances the contrast of a color image by mapping the
3129 % darkest 2 percent of all pixel to black and the brightest 1 percent to white.
3131 % The format of the NormalizeImage method is:
3133 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3135 % A description of each parameter follows:
3137 % o image: the image.
3139 % o exception: return any errors or warnings in this structure.
3142 MagickExport MagickBooleanType NormalizeImage(Image *image,
3143 ExceptionInfo *exception)
3149 black_point=(double) image->columns*image->rows*0.0015;
3150 white_point=(double) image->columns*image->rows*0.9995;
3151 return(ContrastStretchImage(image,black_point,white_point,exception));
3155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3159 % S i g m o i d a l C o n t r a s t I m a g e %
3163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3165 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3166 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3167 % sigmoidal transfer function without saturating highlights or shadows.
3168 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3169 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3170 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3171 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3174 % The format of the SigmoidalContrastImage method is:
3176 % MagickBooleanType SigmoidalContrastImage(Image *image,
3177 % const MagickBooleanType sharpen,const char *levels,
3178 % ExceptionInfo *exception)
3180 % A description of each parameter follows:
3182 % o image: the image.
3184 % o sharpen: Increase or decrease image contrast.
3186 % o alpha: strength of the contrast, the larger the number the more
3187 % 'threshold-like' it becomes.
3189 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3191 % o exception: return any errors or warnings in this structure.
3194 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3195 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3196 ExceptionInfo *exception)
3198 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3219 Allocate and initialize sigmoidal maps.
3221 assert(image != (Image *) NULL);
3222 assert(image->signature == MagickSignature);
3223 if (image->debug != MagickFalse)
3224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3225 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3226 sizeof(*sigmoidal_map));
3227 if (sigmoidal_map == (MagickRealType *) NULL)
3228 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3230 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3231 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3232 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3234 for (i=0; i <= (ssize_t) MaxMap; i++)
3236 if (sharpen != MagickFalse)
3238 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3239 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3240 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3241 QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/(double)
3242 QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3243 QuantumRange)))))+0.5));
3246 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3247 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/(double)
3248 QuantumRange*contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(
3249 midpoint/(double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double)
3250 QuantumRange*contrast))))))/(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3251 contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/(double)
3252 QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3253 contrast))))))/contrast)));
3255 if (image->storage_class == PseudoClass)
3258 Sigmoidal-contrast enhance colormap.
3260 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3261 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3263 for (i=0; i < (ssize_t) image->colors; i++)
3265 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3266 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3267 ClampToQuantum(image->colormap[i].red))];
3268 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3269 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3270 ClampToQuantum(image->colormap[i].green))];
3271 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3272 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3273 ClampToQuantum(image->colormap[i].blue))];
3274 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3275 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3276 ClampToQuantum(image->colormap[i].alpha))];
3280 Sigmoidal-contrast enhance image.
3284 image_view=AcquireCacheView(image);
3285 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3286 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3288 for (y=0; y < (ssize_t) image->rows; y++)
3296 if (status == MagickFalse)
3298 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3299 if (q == (Quantum *) NULL)
3304 for (x=0; x < (ssize_t) image->columns; x++)
3309 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3314 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3315 if ((traits & UpdatePixelTrait) != 0)
3316 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3318 q+=GetPixelChannels(image);
3320 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3322 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3327 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3328 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3330 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3332 if (proceed == MagickFalse)
3336 image_view=DestroyCacheView(image_view);
3337 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);