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=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
705 if (cdl_map == (PixelPacket *) 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=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
714 (MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
715 color_correction.red.offset,color_correction.red.power)))));
716 cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
717 (MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
718 color_correction.green.offset,color_correction.green.power)))));
719 cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
720 (MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
721 color_correction.blue.offset,color_correction.blue.power)))));
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=ClampToQuantum(luma+
739 color_correction.saturation*cdl_map[ScaleQuantumToMap(
740 image->colormap[i].red)].red-luma);
741 image->colormap[i].green=ClampToQuantum(luma+
742 color_correction.saturation*cdl_map[ScaleQuantumToMap(
743 image->colormap[i].green)].green-luma);
744 image->colormap[i].blue=ClampToQuantum(luma+
745 color_correction.saturation*cdl_map[ScaleQuantumToMap(
746 image->colormap[i].blue)].blue-luma);
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=(PixelPacket *) 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,Quantum *red,Quantum *green,Quantum *blue)
848 Enhance contrast: dark color become darker, light color become lighter.
850 assert(red != (Quantum *) NULL);
851 assert(green != (Quantum *) NULL);
852 assert(blue != (Quantum *) 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=GetPixelRed(image,q);
937 green=GetPixelGreen(image,q);
938 blue=GetPixelBlue(image,q);
939 Contrast(sign,&red,&green,&blue);
940 SetPixelRed(image,red,q);
941 SetPixelGreen(image,green,q);
942 SetPixelBlue(image,blue,q);
943 q+=GetPixelChannels(image);
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=ClampToQuantum(stretch_map[
1168 GetPixelChannels(image)*ScaleQuantumToMap(
1169 image->colormap[j].red)]+i);
1171 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1173 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1174 if (black[i] != white[i])
1175 image->colormap[j].green=ClampToQuantum(stretch_map[
1176 GetPixelChannels(image)*ScaleQuantumToMap(
1177 image->colormap[j].green)]+i);
1179 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1181 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1182 if (black[i] != white[i])
1183 image->colormap[j].blue=ClampToQuantum(stretch_map[
1184 GetPixelChannels(image)*ScaleQuantumToMap(
1185 image->colormap[j].blue)]+i);
1187 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1189 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1190 if (black[i] != white[i])
1191 image->colormap[j].alpha=ClampToQuantum(stretch_map[
1192 GetPixelChannels(image)*ScaleQuantumToMap(
1193 image->colormap[j].alpha)]+i);
1198 Stretch-contrast image.
1202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1205 for (y=0; y < (ssize_t) image->rows; y++)
1213 if (status == MagickFalse)
1215 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1216 if (q == (Quantum *) NULL)
1221 for (x=0; x < (ssize_t) image->columns; x++)
1226 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1231 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1232 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1233 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1234 ScaleQuantumToMap(q[i])+i]);
1236 q+=GetPixelChannels(image);
1238 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1240 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1245 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1246 #pragma omp critical (MagickCore_ContrastStretchImage)
1248 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1250 if (proceed == MagickFalse)
1254 image_view=DestroyCacheView(image_view);
1255 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1256 white=(double *) RelinquishMagickMemory(white);
1257 black=(double *) RelinquishMagickMemory(black);
1262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1266 % E n h a n c e I m a g e %
1270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1272 % EnhanceImage() applies a digital filter that improves the quality of a
1275 % The format of the EnhanceImage method is:
1277 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1279 % A description of each parameter follows:
1281 % o image: the image.
1283 % o exception: return any errors or warnings in this structure.
1286 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1288 #define EnhancePixel(weight) \
1289 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1290 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1291 enhance_image,channel,q); \
1292 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1293 mean)*distance*distance; \
1294 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1295 QuantumRange/25.0f)) \
1297 aggregate+=(weight)*r[i]; \
1298 total_weight+=(weight); \
1300 r+=GetPixelChannels(image);
1301 #define EnhanceImageTag "Enhance/Image"
1320 Initialize enhanced image attributes.
1322 assert(image != (const Image *) NULL);
1323 assert(image->signature == MagickSignature);
1324 if (image->debug != MagickFalse)
1325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1326 assert(exception != (ExceptionInfo *) NULL);
1327 assert(exception->signature == MagickSignature);
1328 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1330 if (enhance_image == (Image *) NULL)
1331 return((Image *) NULL);
1332 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1334 enhance_image=DestroyImage(enhance_image);
1335 return((Image *) NULL);
1342 image_view=AcquireCacheView(image);
1343 enhance_view=AcquireCacheView(enhance_image);
1344 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1345 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1347 for (y=0; y < (ssize_t) image->rows; y++)
1349 register const Quantum
1361 if (status == MagickFalse)
1363 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1364 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1366 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1371 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1372 for (x=0; x < (ssize_t) image->columns; x++)
1377 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1393 register const Quantum
1396 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1397 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1398 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1399 if ((traits == UndefinedPixelTrait) ||
1400 (enhance_traits == UndefinedPixelTrait))
1402 SetPixelChannel(enhance_image,channel,p[center+i],q);
1403 if ((enhance_traits & CopyPixelTrait) != 0)
1406 Compute weighted average of target pixel color components.
1411 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1412 EnhancePixel(8.0); EnhancePixel(5.0);
1413 r=p+1*GetPixelChannels(image)*(image->columns+4);
1414 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1415 EnhancePixel(20.0); EnhancePixel(8.0);
1416 r=p+2*GetPixelChannels(image)*(image->columns+4);
1417 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1418 EnhancePixel(40.0); EnhancePixel(10.0);
1419 r=p+3*GetPixelChannels(image)*(image->columns+4);
1420 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1421 EnhancePixel(20.0); EnhancePixel(8.0);
1422 r=p+4*GetPixelChannels(image)*(image->columns+4);
1423 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1424 EnhancePixel(8.0); EnhancePixel(5.0);
1425 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1428 p+=GetPixelChannels(image);
1429 q+=GetPixelChannels(enhance_image);
1431 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1433 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1438 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1439 #pragma omp critical (MagickCore_EnhanceImage)
1441 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1442 if (proceed == MagickFalse)
1446 enhance_view=DestroyCacheView(enhance_view);
1447 image_view=DestroyCacheView(image_view);
1448 return(enhance_image);
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 % E q u a l i z e I m a g e %
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % EqualizeImage() applies a histogram equalization to the image.
1464 % The format of the EqualizeImage method is:
1466 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1468 % A description of each parameter follows:
1470 % o image: the image.
1472 % o exception: return any errors or warnings in this structure.
1475 MagickExport MagickBooleanType EqualizeImage(Image *image,
1476 ExceptionInfo *exception)
1478 #define EqualizeImageTag "Equalize/Image"
1490 black[MaxPixelChannels],
1494 white[MaxPixelChannels];
1503 Allocate and initialize histogram arrays.
1505 assert(image != (Image *) NULL);
1506 assert(image->signature == MagickSignature);
1507 if (image->debug != MagickFalse)
1508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1509 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1510 GetPixelChannels(image)*sizeof(*equalize_map));
1511 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1512 GetPixelChannels(image)*sizeof(*histogram));
1513 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1514 GetPixelChannels(image)*sizeof(*map));
1515 if ((equalize_map == (MagickRealType *) NULL) ||
1516 (histogram == (MagickRealType *) NULL) ||
1517 (map == (MagickRealType *) NULL))
1519 if (map != (MagickRealType *) NULL)
1520 map=(MagickRealType *) RelinquishMagickMemory(map);
1521 if (histogram != (MagickRealType *) NULL)
1522 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1523 if (equalize_map != (MagickRealType *) NULL)
1524 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1525 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1532 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1533 sizeof(*histogram));
1534 image_view=AcquireCacheView(image);
1535 for (y=0; y < (ssize_t) image->rows; y++)
1537 register const Quantum
1543 if (status == MagickFalse)
1545 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1546 if (p == (const Quantum *) NULL)
1551 for (x=0; x < (ssize_t) image->columns; x++)
1556 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1557 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1558 p+=GetPixelChannels(image);
1562 Integrate the histogram to get the equalization map.
1564 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1565 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1567 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1576 for (j=0; j <= (ssize_t) MaxMap; j++)
1578 intensity+=histogram[GetPixelChannels(image)*j+i];
1579 map[GetPixelChannels(image)*j+i]=intensity;
1582 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1583 sizeof(*equalize_map));
1584 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1585 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1587 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1593 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1594 if (black[i] != white[i])
1595 for (j=0; j <= (ssize_t) MaxMap; j++)
1596 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1597 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1598 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1600 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1601 map=(MagickRealType *) RelinquishMagickMemory(map);
1602 if (image->storage_class == PseudoClass)
1610 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1611 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1613 for (j=0; j < (ssize_t) image->colors; j++)
1615 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1616 (white[i]!= black[i]))
1618 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1619 if (black[i] != white[i])
1620 image->colormap[j].red=ClampToQuantum(equalize_map[
1621 GetPixelChannels(image)*ScaleQuantumToMap(
1622 image->colormap[j].red)]+i);
1624 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1625 (white[i]!= black[i]))
1627 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1628 if (black[i] != white[i])
1629 image->colormap[j].green=ClampToQuantum(equalize_map[
1630 GetPixelChannels(image)*ScaleQuantumToMap(
1631 image->colormap[j].red)]+i);
1633 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1634 (white[i]!= black[i]))
1636 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1637 if (black[i] != white[i])
1638 image->colormap[j].blue=ClampToQuantum(equalize_map[
1639 GetPixelChannels(image)*ScaleQuantumToMap(
1640 image->colormap[j].blue)]+i);
1642 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1643 (white[i]!= black[i]))
1645 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1646 if (black[i] != white[i])
1647 image->colormap[j].alpha=ClampToQuantum(equalize_map[
1648 GetPixelChannels(image)*ScaleQuantumToMap(
1649 image->colormap[j].alpha)]+i);
1657 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1658 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1660 for (y=0; y < (ssize_t) image->rows; y++)
1668 if (status == MagickFalse)
1670 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1671 if (q == (Quantum *) NULL)
1676 for (x=0; x < (ssize_t) image->columns; x++)
1681 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1686 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1687 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1688 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1689 ScaleQuantumToMap(q[i])+i]);
1691 q+=GetPixelChannels(image);
1693 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1700 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1701 #pragma omp critical (MagickCore_EqualizeImage)
1703 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1704 if (proceed == MagickFalse)
1708 image_view=DestroyCacheView(image_view);
1709 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1718 % G a m m a I m a g e %
1722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724 % GammaImage() gamma-corrects a particular image channel. The same
1725 % image viewed on different devices will have perceptual differences in the
1726 % way the image's intensities are represented on the screen. Specify
1727 % individual gamma levels for the red, green, and blue channels, or adjust
1728 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1730 % You can also reduce the influence of a particular channel with a gamma
1733 % The format of the GammaImage method is:
1735 % MagickBooleanType GammaImage(Image *image,const double gamma,
1736 % ExceptionInfo *exception)
1738 % A description of each parameter follows:
1740 % o image: the image.
1742 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1744 % o gamma: the image gamma.
1747 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1748 ExceptionInfo *exception)
1750 #define GammaCorrectImageTag "GammaCorrect/Image"
1771 Allocate and initialize gamma maps.
1773 assert(image != (Image *) NULL);
1774 assert(image->signature == MagickSignature);
1775 if (image->debug != MagickFalse)
1776 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1779 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1780 if (gamma_map == (Quantum *) NULL)
1781 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1783 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1785 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1786 #pragma omp parallel for
1788 for (i=0; i <= (ssize_t) MaxMap; i++)
1789 gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
1790 MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
1791 if (image->storage_class == PseudoClass)
1794 Gamma-correct colormap.
1796 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1797 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1799 for (i=0; i < (ssize_t) image->colors; i++)
1801 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1802 image->colormap[i].red=gamma_map[
1803 ScaleQuantumToMap(image->colormap[i].red)];
1804 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1805 image->colormap[i].green=gamma_map[
1806 ScaleQuantumToMap(image->colormap[i].green)];
1807 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1808 image->colormap[i].blue=gamma_map[
1809 ScaleQuantumToMap(image->colormap[i].blue)];
1810 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1811 image->colormap[i].alpha=gamma_map[
1812 ScaleQuantumToMap(image->colormap[i].alpha)];
1816 Gamma-correct image.
1820 image_view=AcquireCacheView(image);
1821 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1822 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1824 for (y=0; y < (ssize_t) image->rows; y++)
1832 if (status == MagickFalse)
1834 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1835 if (q == (Quantum *) NULL)
1840 for (x=0; x < (ssize_t) image->columns; x++)
1845 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1850 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1851 if ((traits & UpdatePixelTrait) != 0)
1852 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
1854 q+=GetPixelChannels(image);
1856 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1858 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1863 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1864 #pragma omp critical (MagickCore_GammaImage)
1866 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1868 if (proceed == MagickFalse)
1872 image_view=DestroyCacheView(image_view);
1873 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
1874 if (image->gamma != 0.0)
1875 image->gamma*=gamma;
1880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1884 % H a l d C l u t I m a g e %
1888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1890 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1891 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1892 % Create it with the HALD coder. You can apply any color transformation to
1893 % the Hald image and then use this method to apply the transform to the
1896 % The format of the HaldClutImage method is:
1898 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1899 % ExceptionInfo *exception)
1901 % A description of each parameter follows:
1903 % o image: the image, which is replaced by indexed CLUT values
1905 % o hald_image: the color lookup table image for replacement color values.
1907 % o exception: return any errors or warnings in this structure.
1911 static inline size_t MagickMin(const size_t x,const size_t y)
1918 MagickExport MagickBooleanType HaldClutImage(Image *image,
1919 const Image *hald_image,ExceptionInfo *exception)
1921 #define HaldClutImageTag "Clut/Image"
1923 typedef struct _HaldInfo
1955 assert(image != (Image *) NULL);
1956 assert(image->signature == MagickSignature);
1957 if (image->debug != MagickFalse)
1958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1959 assert(hald_image != (Image *) NULL);
1960 assert(hald_image->signature == MagickSignature);
1961 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1962 return(MagickFalse);
1963 if (image->matte == MagickFalse)
1964 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1970 length=MagickMin(hald_image->columns,hald_image->rows);
1971 for (level=2; (level*level*level) < length; level++) ;
1973 cube_size=level*level;
1974 width=(double) hald_image->columns;
1975 GetPixelInfo(hald_image,&zero);
1976 image_view=AcquireCacheView(image);
1977 hald_view=AcquireCacheView(hald_image);
1978 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1979 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1981 for (y=0; y < (ssize_t) image->rows; y++)
1989 if (status == MagickFalse)
1991 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1992 if (q == (Quantum *) NULL)
1997 for (x=0; x < (ssize_t) image->columns; x++)
2012 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2013 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2014 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2015 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2016 point.x-=floor(point.x);
2017 point.y-=floor(point.y);
2018 point.z-=floor(point.z);
2020 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2021 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 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2030 fmod(offset,width),floor(offset/width),&pixel1,exception);
2031 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2032 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2034 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2037 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2039 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2040 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2041 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2042 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2043 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2044 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2045 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2046 (image->colorspace == CMYKColorspace))
2047 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2048 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2049 (image->matte != MagickFalse))
2050 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2051 q+=GetPixelChannels(image);
2053 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2055 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2061 #pragma omp critical (MagickCore_HaldClutImage)
2063 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2064 if (proceed == MagickFalse)
2068 hald_view=DestroyCacheView(hald_view);
2069 image_view=DestroyCacheView(image_view);
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078 % L e v e l I m a g e %
2082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2084 % LevelImage() adjusts the levels of a particular image channel by
2085 % scaling the colors falling between specified white and black points to
2086 % the full available quantum range.
2088 % The parameters provided represent the black, and white points. The black
2089 % point specifies the darkest color in the image. Colors darker than the
2090 % black point are set to zero. White point specifies the lightest color in
2091 % the image. Colors brighter than the white point are set to the maximum
2094 % If a '!' flag is given, map black and white colors to the given levels
2095 % rather than mapping those levels to black and white. See
2096 % LevelizeImage() below.
2098 % Gamma specifies a gamma correction to apply to the image.
2100 % The format of the LevelImage method is:
2102 % MagickBooleanType LevelImage(Image *image,const double black_point,
2103 % const double white_point,const double gamma,ExceptionInfo *exception)
2105 % A description of each parameter follows:
2107 % o image: the image.
2109 % o black_point: The level to map zero (black) to.
2111 % o white_point: The level to map QuantumRange (white) to.
2113 % o exception: return any errors or warnings in this structure.
2116 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2117 const double white_point,const double gamma,ExceptionInfo *exception)
2119 #define LevelImageTag "Level/Image"
2120 #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
2121 pow(scale*((double) (x)-black_point),1.0/gamma)))
2142 Allocate and initialize levels map.
2144 assert(image != (Image *) NULL);
2145 assert(image->signature == MagickSignature);
2146 if (image->debug != MagickFalse)
2147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2148 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2149 if (image->storage_class == PseudoClass)
2150 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2151 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2153 for (i=0; i < (ssize_t) image->colors; i++)
2158 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2159 image->colormap[i].red=LevelQuantum(image->colormap[i].red);
2160 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2161 image->colormap[i].green=LevelQuantum(image->colormap[i].green);
2162 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2163 image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
2164 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2165 image->colormap[i].alpha=LevelQuantum(image->colormap[i].alpha);
2172 image_view=AcquireCacheView(image);
2173 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2174 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2176 for (y=0; y < (ssize_t) image->rows; y++)
2184 if (status == MagickFalse)
2186 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2187 if (q == (Quantum *) NULL)
2192 for (x=0; x < (ssize_t) image->columns; x++)
2197 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2202 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2203 if ((traits == UndefinedPixelTrait) ||
2204 ((traits & UpdatePixelTrait) == 0))
2206 q[i]=LevelQuantum(q[i]);
2208 q+=GetPixelChannels(image);
2210 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2212 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2217 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2218 #pragma omp critical (MagickCore_LevelImage)
2220 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2221 if (proceed == MagickFalse)
2225 image_view=DestroyCacheView(image_view);
2230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2234 % L e v e l i z e I m a g e %
2238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2240 % LevelizeImage() applies the reversed LevelImage() operation to just
2241 % the specific channels specified. It compresses the full range of color
2242 % values, so that they lie between the given black and white points. Gamma is
2243 % applied before the values are mapped.
2245 % LevelizeImage() can be called with by using a +level command line
2246 % API option, or using a '!' on a -level or LevelImage() geometry string.
2248 % It can be used for example de-contrast a greyscale image to the exact
2249 % levels specified. Or by using specific levels for each channel of an image
2250 % you can convert a gray-scale image to any linear color gradient, according
2253 % The format of the LevelizeImage method is:
2255 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2256 % const double white_point,const double gamma,ExceptionInfo *exception)
2258 % A description of each parameter follows:
2260 % o image: the image.
2262 % o black_point: The level to map zero (black) to.
2264 % o white_point: The level to map QuantumRange (white) to.
2266 % o gamma: adjust gamma by this factor before mapping values.
2268 % o exception: return any errors or warnings in this structure.
2271 MagickExport MagickBooleanType LevelizeImage(Image *image,
2272 const double black_point,const double white_point,const double gamma,
2273 ExceptionInfo *exception)
2275 #define LevelizeImageTag "Levelize/Image"
2276 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2277 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2296 Allocate and initialize levels map.
2298 assert(image != (Image *) NULL);
2299 assert(image->signature == MagickSignature);
2300 if (image->debug != MagickFalse)
2301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2302 if (image->storage_class == PseudoClass)
2303 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2304 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2306 for (i=0; i < (ssize_t) image->colors; i++)
2311 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2312 image->colormap[i].red=LevelizeValue(image->colormap[i].red);
2313 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2314 image->colormap[i].green=LevelizeValue(image->colormap[i].green);
2315 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2316 image->colormap[i].blue=LevelizeValue(image->colormap[i].blue);
2317 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2318 image->colormap[i].alpha=LevelizeValue(image->colormap[i].alpha);
2325 exception=(&image->exception);
2326 image_view=AcquireCacheView(image);
2327 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2328 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2330 for (y=0; y < (ssize_t) image->rows; y++)
2338 if (status == MagickFalse)
2340 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2341 if (q == (Quantum *) NULL)
2346 for (x=0; x < (ssize_t) image->columns; x++)
2351 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2356 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2357 if ((traits & UpdatePixelTrait) != 0)
2358 q[i]=LevelizeValue(q[i]);
2360 q+=GetPixelChannels(image);
2362 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2364 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2369 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2370 #pragma omp critical (MagickCore_LevelizeImage)
2372 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2373 if (proceed == MagickFalse)
2377 image_view=DestroyCacheView(image_view);
2382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2386 % L e v e l I m a g e C o l o r s %
2390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2392 % LevelImageColors() maps the given color to "black" and "white" values,
2393 % linearly spreading out the colors, and level values on a channel by channel
2394 % bases, as per LevelImage(). The given colors allows you to specify
2395 % different level ranges for each of the color channels separately.
2397 % If the boolean 'invert' is set true the image values will modifyed in the
2398 % reverse direction. That is any existing "black" and "white" colors in the
2399 % image will become the color values given, with all other values compressed
2400 % appropriatally. This effectivally maps a greyscale gradient into the given
2403 % The format of the LevelImageColors method is:
2405 % MagickBooleanType LevelImageColors(Image *image,
2406 % const PixelInfo *black_color,const PixelInfo *white_color,
2407 % const MagickBooleanType invert,ExceptionInfo *exception)
2409 % A description of each parameter follows:
2411 % o image: the image.
2413 % o black_color: The color to map black to/from
2415 % o white_point: The color to map white to/from
2417 % o invert: if true map the colors (levelize), rather than from (level)
2419 % o exception: return any errors or warnings in this structure.
2422 MagickExport MagickBooleanType LevelImageColors(Image *image,
2423 const PixelInfo *black_color,const PixelInfo *white_color,
2424 const MagickBooleanType invert,ExceptionInfo *exception)
2433 Allocate and initialize levels map.
2435 assert(image != (Image *) NULL);
2436 assert(image->signature == MagickSignature);
2437 if (image->debug != MagickFalse)
2438 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2440 if (invert == MagickFalse)
2442 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2444 channel_mask=SetPixelChannelMask(image,RedChannel);
2445 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2447 (void) SetPixelChannelMask(image,channel_mask);
2449 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2451 channel_mask=SetPixelChannelMask(image,GreenChannel);
2452 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2454 (void) SetPixelChannelMask(image,channel_mask);
2456 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2458 channel_mask=SetPixelChannelMask(image,BlueChannel);
2459 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2461 (void) SetPixelChannelMask(image,channel_mask);
2463 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2464 (image->colorspace == CMYKColorspace))
2466 channel_mask=SetPixelChannelMask(image,BlackChannel);
2467 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2469 (void) SetPixelChannelMask(image,channel_mask);
2471 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2472 (image->matte == MagickTrue))
2474 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2475 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2477 (void) SetPixelChannelMask(image,channel_mask);
2482 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2484 channel_mask=SetPixelChannelMask(image,RedChannel);
2485 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2487 (void) SetPixelChannelMask(image,channel_mask);
2489 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2491 channel_mask=SetPixelChannelMask(image,GreenChannel);
2492 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2494 (void) SetPixelChannelMask(image,channel_mask);
2496 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2498 channel_mask=SetPixelChannelMask(image,BlueChannel);
2499 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2501 (void) SetPixelChannelMask(image,channel_mask);
2503 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2504 (image->colorspace == CMYKColorspace))
2506 channel_mask=SetPixelChannelMask(image,BlackChannel);
2507 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2509 (void) SetPixelChannelMask(image,channel_mask);
2511 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2512 (image->matte == MagickTrue))
2514 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2515 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2517 (void) SetPixelChannelMask(image,channel_mask);
2520 return(status == 0 ? MagickFalse : MagickTrue);
2524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2528 % L i n e a r S t r e t c h I m a g e %
2532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2534 % LinearStretchImage() discards any pixels below the black point and above
2535 % the white point and levels the remaining pixels.
2537 % The format of the LinearStretchImage method is:
2539 % MagickBooleanType LinearStretchImage(Image *image,
2540 % const double black_point,const double white_point,
2541 % ExceptionInfo *exception)
2543 % A description of each parameter follows:
2545 % o image: the image.
2547 % o black_point: the black point.
2549 % o white_point: the white point.
2551 % o exception: return any errors or warnings in this structure.
2554 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2555 const double black_point,const double white_point,ExceptionInfo *exception)
2557 #define LinearStretchImageTag "LinearStretch/Image"
2575 Allocate histogram and linear map.
2577 assert(image != (Image *) NULL);
2578 assert(image->signature == MagickSignature);
2579 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2580 sizeof(*histogram));
2581 if (histogram == (MagickRealType *) NULL)
2582 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2587 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2588 image_view=AcquireCacheView(image);
2589 for (y=0; y < (ssize_t) image->rows; y++)
2591 register const Quantum
2597 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2598 if (p == (const Quantum *) NULL)
2600 for (x=0; x < (ssize_t) image->columns; x++)
2602 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2603 p+=GetPixelChannels(image);
2606 image_view=DestroyCacheView(image_view);
2608 Find the histogram boundaries by locating the black and white point levels.
2611 for (black=0; black < (ssize_t) MaxMap; black++)
2613 intensity+=histogram[black];
2614 if (intensity >= black_point)
2618 for (white=(ssize_t) MaxMap; white != 0; white--)
2620 intensity+=histogram[white];
2621 if (intensity >= white_point)
2624 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2625 status=LevelImage(image,(double) black,(double) white,1.0,&image->exception);
2630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2634 % M o d u l a t e I m a g e %
2638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2640 % ModulateImage() lets you control the brightness, saturation, and hue
2641 % of an image. Modulate represents the brightness, saturation, and hue
2642 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2643 % modulation is lightness, saturation, and hue. And if the colorspace is
2644 % HWB, use blackness, whiteness, and hue.
2646 % The format of the ModulateImage method is:
2648 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2649 % ExceptionInfo *exception)
2651 % A description of each parameter follows:
2653 % o image: the image.
2655 % o modulate: Define the percent change in brightness, saturation, and hue.
2657 % o exception: return any errors or warnings in this structure.
2661 static void ModulateHSB(const double percent_hue,
2662 const double percent_saturation,const double percent_brightness,
2663 Quantum *red,Quantum *green,Quantum *blue)
2671 Increase or decrease color brightness, saturation, or hue.
2673 assert(red != (Quantum *) NULL);
2674 assert(green != (Quantum *) NULL);
2675 assert(blue != (Quantum *) NULL);
2676 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2677 hue+=0.5*(0.01*percent_hue-1.0);
2682 saturation*=0.01*percent_saturation;
2683 brightness*=0.01*percent_brightness;
2684 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2687 static void ModulateHSL(const double percent_hue,
2688 const double percent_saturation,const double percent_lightness,
2689 Quantum *red,Quantum *green,Quantum *blue)
2697 Increase or decrease color lightness, saturation, or hue.
2699 assert(red != (Quantum *) NULL);
2700 assert(green != (Quantum *) NULL);
2701 assert(blue != (Quantum *) NULL);
2702 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2703 hue+=0.5*(0.01*percent_hue-1.0);
2708 saturation*=0.01*percent_saturation;
2709 lightness*=0.01*percent_lightness;
2710 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2713 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue)
2721 Increase or decrease color blackness, whiteness, or hue.
2723 assert(red != (Quantum *) NULL);
2724 assert(green != (Quantum *) NULL);
2725 assert(blue != (Quantum *) NULL);
2726 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2727 hue+=0.5*(0.01*percent_hue-1.0);
2732 blackness*=0.01*percent_blackness;
2733 whiteness*=0.01*percent_whiteness;
2734 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2737 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2738 ExceptionInfo *exception)
2740 #define ModulateImageTag "Modulate/Image"
2775 Initialize modulate table.
2777 assert(image != (Image *) NULL);
2778 assert(image->signature == MagickSignature);
2779 if (image->debug != MagickFalse)
2780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2781 if (modulate == (char *) NULL)
2782 return(MagickFalse);
2783 flags=ParseGeometry(modulate,&geometry_info);
2784 percent_brightness=geometry_info.rho;
2785 percent_saturation=geometry_info.sigma;
2786 if ((flags & SigmaValue) == 0)
2787 percent_saturation=100.0;
2788 percent_hue=geometry_info.xi;
2789 if ((flags & XiValue) == 0)
2791 colorspace=UndefinedColorspace;
2792 artifact=GetImageArtifact(image,"modulate:colorspace");
2793 if (artifact != (const char *) NULL)
2794 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2795 MagickFalse,artifact);
2796 if (image->storage_class == PseudoClass)
2801 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2802 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2804 for (i=0; i < (ssize_t) image->colors; i++)
2809 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2810 &image->colormap[i].red,&image->colormap[i].green,
2811 &image->colormap[i].blue);
2817 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2818 &image->colormap[i].red,&image->colormap[i].green,
2819 &image->colormap[i].blue);
2824 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2825 &image->colormap[i].red,&image->colormap[i].green,
2826 &image->colormap[i].blue);
2836 image_view=AcquireCacheView(image);
2837 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2838 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2840 for (y=0; y < (ssize_t) image->rows; y++)
2853 if (status == MagickFalse)
2855 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2856 if (q == (Quantum *) NULL)
2861 for (x=0; x < (ssize_t) image->columns; x++)
2863 red=GetPixelRed(image,q);
2864 green=GetPixelGreen(image,q);
2865 blue=GetPixelBlue(image,q);
2870 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2877 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2883 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2888 SetPixelRed(image,red,q);
2889 SetPixelGreen(image,green,q);
2890 SetPixelBlue(image,blue,q);
2891 q+=GetPixelChannels(image);
2893 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2895 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2900 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2901 #pragma omp critical (MagickCore_ModulateImage)
2903 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
2904 if (proceed == MagickFalse)
2908 image_view=DestroyCacheView(image_view);
2913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2917 % N e g a t e I m a g e %
2921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2923 % NegateImage() negates the colors in the reference image. The grayscale
2924 % option means that only grayscale values within the image are negated.
2926 % The format of the NegateImage method is:
2928 % MagickBooleanType NegateImage(Image *image,
2929 % const MagickBooleanType grayscale,ExceptionInfo *exception)
2931 % A description of each parameter follows:
2933 % o image: the image.
2935 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
2937 % o exception: return any errors or warnings in this structure.
2940 MagickExport MagickBooleanType NegateImage(Image *image,
2941 const MagickBooleanType grayscale,ExceptionInfo *exception)
2943 #define NegateImageTag "Negate/Image"
2960 assert(image != (Image *) NULL);
2961 assert(image->signature == MagickSignature);
2962 if (image->debug != MagickFalse)
2963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2964 if (image->storage_class == PseudoClass)
2969 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2970 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2972 for (i=0; i < (ssize_t) image->colors; i++)
2974 if (grayscale != MagickFalse)
2975 if ((image->colormap[i].red != image->colormap[i].green) ||
2976 (image->colormap[i].green != image->colormap[i].blue))
2978 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2979 image->colormap[i].red=(Quantum) QuantumRange-
2980 image->colormap[i].red;
2981 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2982 image->colormap[i].green=(Quantum) QuantumRange-
2983 image->colormap[i].green;
2984 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2985 image->colormap[i].blue=(Quantum) QuantumRange-
2986 image->colormap[i].blue;
2994 image_view=AcquireCacheView(image);
2995 if (grayscale != MagickFalse)
2997 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2998 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3000 for (y=0; y < (ssize_t) image->rows; y++)
3011 if (status == MagickFalse)
3013 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3015 if (q == (Quantum *) NULL)
3020 for (x=0; x < (ssize_t) image->columns; x++)
3025 if (IsPixelGray(image,q) != MagickFalse)
3027 q+=GetPixelChannels(image);
3030 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3035 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3036 if ((traits & UpdatePixelTrait) != 0)
3037 q[i]=QuantumRange-q[i];
3039 q+=GetPixelChannels(image);
3041 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3042 if (sync == MagickFalse)
3044 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3049 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3050 #pragma omp critical (MagickCore_NegateImage)
3052 proceed=SetImageProgress(image,NegateImageTag,progress++,
3054 if (proceed == MagickFalse)
3058 image_view=DestroyCacheView(image_view);
3064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3065 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3067 for (y=0; y < (ssize_t) image->rows; y++)
3075 if (status == MagickFalse)
3077 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3078 if (q == (Quantum *) NULL)
3083 for (x=0; x < (ssize_t) image->columns; x++)
3088 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3093 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3094 if ((traits & UpdatePixelTrait) != 0)
3095 q[i]=QuantumRange-q[i];
3097 q+=GetPixelChannels(image);
3099 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3101 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3106 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3107 #pragma omp critical (MagickCore_NegateImage)
3109 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3110 if (proceed == MagickFalse)
3114 image_view=DestroyCacheView(image_view);
3119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3123 % N o r m a l i z e I m a g e %
3127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129 % NormalizeImage() enhances the contrast of a color image by mapping the
3130 % darkest 2 percent of all pixel to black and the brightest 1 percent to white.
3132 % The format of the NormalizeImage method is:
3134 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3136 % A description of each parameter follows:
3138 % o image: the image.
3140 % o exception: return any errors or warnings in this structure.
3143 MagickExport MagickBooleanType NormalizeImage(Image *image,
3144 ExceptionInfo *exception)
3150 black_point=(double) image->columns*image->rows*0.0015;
3151 white_point=(double) image->columns*image->rows*0.9995;
3152 return(ContrastStretchImage(image,black_point,white_point,exception));
3156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3160 % S i g m o i d a l C o n t r a s t I m a g e %
3164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3166 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3167 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3168 % sigmoidal transfer function without saturating highlights or shadows.
3169 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3170 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3171 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3172 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3175 % The format of the SigmoidalContrastImage method is:
3177 % MagickBooleanType SigmoidalContrastImage(Image *image,
3178 % const MagickBooleanType sharpen,const char *levels,
3179 % ExceptionInfo *exception)
3181 % A description of each parameter follows:
3183 % o image: the image.
3185 % o sharpen: Increase or decrease image contrast.
3187 % o alpha: strength of the contrast, the larger the number the more
3188 % 'threshold-like' it becomes.
3190 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3192 % o exception: return any errors or warnings in this structure.
3195 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3196 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3197 ExceptionInfo *exception)
3199 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3220 Allocate and initialize sigmoidal maps.
3222 assert(image != (Image *) NULL);
3223 assert(image->signature == MagickSignature);
3224 if (image->debug != MagickFalse)
3225 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3226 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3227 sizeof(*sigmoidal_map));
3228 if (sigmoidal_map == (MagickRealType *) NULL)
3229 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3231 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3232 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3233 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3235 for (i=0; i <= (ssize_t) MaxMap; i++)
3237 if (sharpen != MagickFalse)
3239 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3240 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3241 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3242 QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/(double)
3243 QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3244 QuantumRange)))))+0.5));
3247 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3248 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/(double)
3249 QuantumRange*contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(
3250 midpoint/(double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double)
3251 QuantumRange*contrast))))))/(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3252 contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/(double)
3253 QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3254 contrast))))))/contrast)));
3256 if (image->storage_class == PseudoClass)
3259 Sigmoidal-contrast enhance colormap.
3261 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3262 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3264 for (i=0; i < (ssize_t) image->colors; i++)
3266 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3267 image->colormap[i].red=ClampToQuantum(sigmoidal_map[
3268 ScaleQuantumToMap(image->colormap[i].red)]);
3269 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3270 image->colormap[i].green=ClampToQuantum(sigmoidal_map[
3271 ScaleQuantumToMap(image->colormap[i].green)]);
3272 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3273 image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
3274 ScaleQuantumToMap(image->colormap[i].blue)]);
3275 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3276 image->colormap[i].alpha=ClampToQuantum(sigmoidal_map[
3277 ScaleQuantumToMap(image->colormap[i].alpha)]);
3281 Sigmoidal-contrast enhance image.
3285 image_view=AcquireCacheView(image);
3286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3287 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3289 for (y=0; y < (ssize_t) image->rows; y++)
3297 if (status == MagickFalse)
3299 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3300 if (q == (Quantum *) NULL)
3305 for (x=0; x < (ssize_t) image->columns; x++)
3310 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3315 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3316 if ((traits & UpdatePixelTrait) != 0)
3317 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3319 q+=GetPixelChannels(image);
3321 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3323 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3328 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3329 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3331 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3333 if (proceed == MagickFalse)
3337 image_view=DestroyCacheView(image_view);
3338 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);