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 q[channel]=ClampToQuantum(clut_map[ScaleQuantumToMap(q[channel])*
395 GetPixelChannels(clut_image)+channel]);
397 q+=GetPixelChannels(image);
399 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
401 if (image->progress_monitor != (MagickProgressMonitor) NULL)
406 #if defined(MAGICKCORE_OPENMP_SUPPORT)
407 #pragma omp critical (MagickCore_ClutImage)
409 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
410 if (proceed == MagickFalse)
414 image_view=DestroyCacheView(image_view);
415 clut_map=(double *) RelinquishMagickMemory(clut_map);
416 if ((clut_image->matte != MagickFalse) &&
417 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
418 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % C o l o r D e c i s i o n L i s t I m a g e %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
434 % (CCC) file which solely contains one or more color corrections and applies
435 % the correction to the image. Here is a sample CCC file:
437 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
438 % <ColorCorrection id="cc03345">
440 % <Slope> 0.9 1.2 0.5 </Slope>
441 % <Offset> 0.4 -0.5 0.6 </Offset>
442 % <Power> 1.0 0.8 1.5 </Power>
445 % <Saturation> 0.85 </Saturation>
448 % </ColorCorrectionCollection>
450 % which includes the slop, offset, and power for each of the RGB channels
451 % as well as the saturation.
453 % The format of the ColorDecisionListImage method is:
455 % MagickBooleanType ColorDecisionListImage(Image *image,
456 % const char *color_correction_collection,ExceptionInfo *exception)
458 % A description of each parameter follows:
460 % o image: the image.
462 % o color_correction_collection: the color correction collection in XML.
464 % o exception: return any errors or warnings in this structure.
467 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
468 const char *color_correction_collection,ExceptionInfo *exception)
470 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
472 typedef struct _Correction
480 typedef struct _ColorCorrection
495 token[MaxTextExtent];
526 Allocate and initialize cdl maps.
528 assert(image != (Image *) NULL);
529 assert(image->signature == MagickSignature);
530 if (image->debug != MagickFalse)
531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
532 if (color_correction_collection == (const char *) NULL)
534 ccc=NewXMLTree((const char *) color_correction_collection,exception);
535 if (ccc == (XMLTreeInfo *) NULL)
537 cc=GetXMLTreeChild(ccc,"ColorCorrection");
538 if (cc == (XMLTreeInfo *) NULL)
540 ccc=DestroyXMLTree(ccc);
543 color_correction.red.slope=1.0;
544 color_correction.red.offset=0.0;
545 color_correction.red.power=1.0;
546 color_correction.green.slope=1.0;
547 color_correction.green.offset=0.0;
548 color_correction.green.power=1.0;
549 color_correction.blue.slope=1.0;
550 color_correction.blue.offset=0.0;
551 color_correction.blue.power=1.0;
552 color_correction.saturation=0.0;
553 sop=GetXMLTreeChild(cc,"SOPNode");
554 if (sop != (XMLTreeInfo *) NULL)
561 slope=GetXMLTreeChild(sop,"Slope");
562 if (slope != (XMLTreeInfo *) NULL)
564 content=GetXMLTreeContent(slope);
565 p=(const char *) content;
566 for (i=0; (*p != '\0') && (i < 3); i++)
568 GetMagickToken(p,&p,token);
570 GetMagickToken(p,&p,token);
575 color_correction.red.slope=InterpretLocaleValue(token,
581 color_correction.green.slope=InterpretLocaleValue(token,
587 color_correction.blue.slope=InterpretLocaleValue(token,
594 offset=GetXMLTreeChild(sop,"Offset");
595 if (offset != (XMLTreeInfo *) NULL)
597 content=GetXMLTreeContent(offset);
598 p=(const char *) content;
599 for (i=0; (*p != '\0') && (i < 3); i++)
601 GetMagickToken(p,&p,token);
603 GetMagickToken(p,&p,token);
608 color_correction.red.offset=InterpretLocaleValue(token,
614 color_correction.green.offset=InterpretLocaleValue(token,
620 color_correction.blue.offset=InterpretLocaleValue(token,
627 power=GetXMLTreeChild(sop,"Power");
628 if (power != (XMLTreeInfo *) NULL)
630 content=GetXMLTreeContent(power);
631 p=(const char *) content;
632 for (i=0; (*p != '\0') && (i < 3); i++)
634 GetMagickToken(p,&p,token);
636 GetMagickToken(p,&p,token);
641 color_correction.red.power=InterpretLocaleValue(token,
647 color_correction.green.power=InterpretLocaleValue(token,
653 color_correction.blue.power=InterpretLocaleValue(token,
661 sat=GetXMLTreeChild(cc,"SATNode");
662 if (sat != (XMLTreeInfo *) NULL)
667 saturation=GetXMLTreeChild(sat,"Saturation");
668 if (saturation != (XMLTreeInfo *) NULL)
670 content=GetXMLTreeContent(saturation);
671 p=(const char *) content;
672 GetMagickToken(p,&p,token);
673 color_correction.saturation=InterpretLocaleValue(token,
677 ccc=DestroyXMLTree(ccc);
678 if (image->debug != MagickFalse)
680 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
681 " Color Correction Collection:");
682 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
683 " color_correction.red.slope: %g",color_correction.red.slope);
684 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
685 " color_correction.red.offset: %g",color_correction.red.offset);
686 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
687 " color_correction.red.power: %g",color_correction.red.power);
688 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
689 " color_correction.green.slope: %g",color_correction.green.slope);
690 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
691 " color_correction.green.offset: %g",color_correction.green.offset);
692 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
693 " color_correction.green.power: %g",color_correction.green.power);
694 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
695 " color_correction.blue.slope: %g",color_correction.blue.slope);
696 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
697 " color_correction.blue.offset: %g",color_correction.blue.offset);
698 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
699 " color_correction.blue.power: %g",color_correction.blue.power);
700 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
701 " color_correction.saturation: %g",color_correction.saturation);
703 cdl_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
704 if (cdl_map == (PixelPacket *) NULL)
705 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
707 #if defined(MAGICKCORE_OPENMP_SUPPORT)
708 #pragma omp parallel for schedule(dynamic,4)
710 for (i=0; i <= (ssize_t) MaxMap; i++)
712 cdl_map[i].red=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
713 (MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
714 color_correction.red.offset,color_correction.red.power)))));
715 cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
716 (MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
717 color_correction.green.offset,color_correction.green.power)))));
718 cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
719 (MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
720 color_correction.blue.offset,color_correction.blue.power)))));
722 if (image->storage_class == PseudoClass)
725 Apply transfer function to colormap.
727 #if defined(MAGICKCORE_OPENMP_SUPPORT)
728 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
730 for (i=0; i < (ssize_t) image->colors; i++)
735 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
736 0.0722*image->colormap[i].blue;
737 image->colormap[i].red=ClampToQuantum(luma+
738 color_correction.saturation*cdl_map[ScaleQuantumToMap(
739 image->colormap[i].red)].red-luma);
740 image->colormap[i].green=ClampToQuantum(luma+
741 color_correction.saturation*cdl_map[ScaleQuantumToMap(
742 image->colormap[i].green)].green-luma);
743 image->colormap[i].blue=ClampToQuantum(luma+
744 color_correction.saturation*cdl_map[ScaleQuantumToMap(
745 image->colormap[i].blue)].blue-luma);
749 Apply transfer function to image.
753 image_view=AcquireCacheView(image);
754 #if defined(MAGICKCORE_OPENMP_SUPPORT)
755 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
757 for (y=0; y < (ssize_t) image->rows; y++)
768 if (status == MagickFalse)
770 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
771 if (q == (Quantum *) NULL)
776 for (x=0; x < (ssize_t) image->columns; x++)
778 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
779 GetPixelBlue(image,q);
780 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
781 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
782 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
783 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
784 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
785 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
786 q+=GetPixelChannels(image);
788 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
790 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 #if defined(MAGICKCORE_OPENMP_SUPPORT)
796 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
798 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
799 progress++,image->rows);
800 if (proceed == MagickFalse)
804 image_view=DestroyCacheView(image_view);
805 cdl_map=(PixelPacket *) RelinquishMagickMemory(cdl_map);
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814 % C o n t r a s t I m a g e %
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % ContrastImage() enhances the intensity differences between the lighter and
821 % darker elements of the image. Set sharpen to a MagickTrue to increase the
822 % image contrast otherwise the contrast is reduced.
824 % The format of the ContrastImage method is:
826 % MagickBooleanType ContrastImage(Image *image,
827 % const MagickBooleanType sharpen,ExceptionInfo *exception)
829 % A description of each parameter follows:
831 % o image: the image.
833 % o sharpen: Increase or decrease image contrast.
835 % o exception: return any errors or warnings in this structure.
839 static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
847 Enhance contrast: dark color become darker, light color become lighter.
849 assert(red != (Quantum *) NULL);
850 assert(green != (Quantum *) NULL);
851 assert(blue != (Quantum *) NULL);
855 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
856 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
858 if (brightness > 1.0)
861 if (brightness < 0.0)
863 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
866 MagickExport MagickBooleanType ContrastImage(Image *image,
867 const MagickBooleanType sharpen,ExceptionInfo *exception)
869 #define ContrastImageTag "Contrast/Image"
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 sign=sharpen != MagickFalse ? 1 : -1;
894 if (image->storage_class == PseudoClass)
897 Contrast enhance colormap.
899 for (i=0; i < (ssize_t) image->colors; i++)
900 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
901 &image->colormap[i].blue);
904 Contrast enhance image.
908 image_view=AcquireCacheView(image);
909 #if defined(MAGICKCORE_OPENMP_SUPPORT)
910 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
912 for (y=0; y < (ssize_t) image->rows; y++)
925 if (status == MagickFalse)
927 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
928 if (q == (Quantum *) NULL)
933 for (x=0; x < (ssize_t) image->columns; x++)
935 red=GetPixelRed(image,q);
936 green=GetPixelGreen(image,q);
937 blue=GetPixelBlue(image,q);
938 Contrast(sign,&red,&green,&blue);
939 SetPixelRed(image,red,q);
940 SetPixelGreen(image,green,q);
941 SetPixelBlue(image,blue,q);
942 q+=GetPixelChannels(image);
944 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
946 if (image->progress_monitor != (MagickProgressMonitor) NULL)
951 #if defined(MAGICKCORE_OPENMP_SUPPORT)
952 #pragma omp critical (MagickCore_ContrastImage)
954 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
955 if (proceed == MagickFalse)
959 image_view=DestroyCacheView(image_view);
964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968 % C o n t r a s t S t r e t c h I m a g e %
972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974 % ContrastStretchImage() is a simple image enhancement technique that attempts
975 % to improve the contrast in an image by `stretching' the range of intensity
976 % values it contains to span a desired range of values. It differs from the
977 % more sophisticated histogram equalization in that it can only apply a
978 % linear scaling function to the image pixel values. As a result the
979 % `enhancement' is less harsh.
981 % The format of the ContrastStretchImage method is:
983 % MagickBooleanType ContrastStretchImage(Image *image,
984 % const char *levels,ExceptionInfo *exception)
986 % A description of each parameter follows:
988 % o image: the image.
990 % o black_point: the black point.
992 % o white_point: the white point.
994 % o levels: Specify the levels where the black and white points have the
995 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
997 % o exception: return any errors or warnings in this structure.
1000 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1001 const double black_point,const double white_point,ExceptionInfo *exception)
1003 #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1004 #define ContrastStretchImageTag "ContrastStretch/Image"
1028 Allocate histogram and stretch map.
1030 assert(image != (Image *) NULL);
1031 assert(image->signature == MagickSignature);
1032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1034 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1035 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
1036 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,
1037 GetPixelChannels(image)*sizeof(*histogram));
1038 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1039 GetPixelChannels(image)*sizeof(*stretch_map));
1040 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1041 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
1043 if (stretch_map != (double *) NULL)
1044 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1045 if (histogram != (double *) NULL)
1046 histogram=(double *) RelinquishMagickMemory(histogram);
1047 if (white != (double *) NULL)
1048 white=(double *) RelinquishMagickMemory(white);
1049 if (black != (double *) NULL)
1050 black=(double *) RelinquishMagickMemory(black);
1051 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1058 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1059 sizeof(*histogram));
1060 image_view=AcquireCacheView(image);
1061 for (y=0; y < (ssize_t) image->rows; y++)
1063 register const Quantum
1069 if (status == MagickFalse)
1071 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1072 if (p == (const Quantum *) NULL)
1077 for (x=0; x < (ssize_t) image->columns; x++)
1082 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1083 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1084 p+=GetPixelChannels(image);
1088 Find the histogram boundaries by locating the black/white levels.
1090 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1091 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1093 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1102 white[i]=MaxRange(QuantumRange);
1104 for (j=0; j <= (ssize_t) MaxMap; j++)
1106 intensity+=histogram[GetPixelChannels(image)*j+i];
1107 if (intensity > black_point)
1110 black[i]=(MagickRealType) j;
1112 for (j=(ssize_t) MaxMap; j != 0; j--)
1114 intensity+=histogram[GetPixelChannels(image)*j+i];
1115 if (intensity > ((double) image->columns*image->rows-white_point))
1118 white[i]=(MagickRealType) j;
1120 histogram=(double *) RelinquishMagickMemory(histogram);
1122 Stretch the histogram to create the stretched image mapping.
1124 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1125 sizeof(*stretch_map));
1126 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1127 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1129 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1134 for (j=0; j <= (ssize_t) MaxMap; j++)
1136 if (j < (ssize_t) black[i])
1137 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1139 if (j > (ssize_t) white[i])
1140 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1143 if (black[i] != white[i])
1144 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1145 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1146 (white[i]-black[i])));
1149 if (image->storage_class == PseudoClass)
1155 Stretch-contrast colormap.
1157 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1158 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1160 for (j=0; j < (ssize_t) image->colors; j++)
1162 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1164 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1165 if (black[i] != white[i])
1166 image->colormap[j].red=ClampToQuantum(stretch_map[
1167 GetPixelChannels(image)*ScaleQuantumToMap(
1168 image->colormap[j].red)]+i);
1170 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1172 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1173 if (black[i] != white[i])
1174 image->colormap[j].green=ClampToQuantum(stretch_map[
1175 GetPixelChannels(image)*ScaleQuantumToMap(
1176 image->colormap[j].green)]+i);
1178 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1180 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1181 if (black[i] != white[i])
1182 image->colormap[j].blue=ClampToQuantum(stretch_map[
1183 GetPixelChannels(image)*ScaleQuantumToMap(
1184 image->colormap[j].blue)]+i);
1186 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1188 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1189 if (black[i] != white[i])
1190 image->colormap[j].alpha=ClampToQuantum(stretch_map[
1191 GetPixelChannels(image)*ScaleQuantumToMap(
1192 image->colormap[j].alpha)]+i);
1197 Stretch-contrast image.
1201 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1202 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1204 for (y=0; y < (ssize_t) image->rows; y++)
1212 if (status == MagickFalse)
1214 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1215 if (q == (Quantum *) NULL)
1220 for (x=0; x < (ssize_t) image->columns; x++)
1225 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1230 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1231 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1232 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1233 ScaleQuantumToMap(q[i])+i]);
1235 q+=GetPixelChannels(image);
1237 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1239 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1245 #pragma omp critical (MagickCore_ContrastStretchImage)
1247 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1249 if (proceed == MagickFalse)
1253 image_view=DestroyCacheView(image_view);
1254 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1255 white=(double *) RelinquishMagickMemory(white);
1256 black=(double *) RelinquishMagickMemory(black);
1261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1265 % E n h a n c e I m a g e %
1269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271 % EnhanceImage() applies a digital filter that improves the quality of a
1274 % The format of the EnhanceImage method is:
1276 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1278 % A description of each parameter follows:
1280 % o image: the image.
1282 % o exception: return any errors or warnings in this structure.
1285 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1287 #define EnhancePixel(weight) \
1288 mean=((MagickRealType) r[i]+q[channel])/2.0; \
1289 distance=(MagickRealType) r[i]-(MagickRealType) q[channel]; \
1290 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1291 mean)*distance*distance; \
1292 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1293 QuantumRange/25.0f)) \
1295 aggregate+=(weight)*r[i]; \
1296 total_weight+=(weight); \
1298 r+=GetPixelChannels(image);
1299 #define EnhanceImageTag "Enhance/Image"
1318 Initialize enhanced image attributes.
1320 assert(image != (const Image *) NULL);
1321 assert(image->signature == MagickSignature);
1322 if (image->debug != MagickFalse)
1323 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1324 assert(exception != (ExceptionInfo *) NULL);
1325 assert(exception->signature == MagickSignature);
1326 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1328 if (enhance_image == (Image *) NULL)
1329 return((Image *) NULL);
1330 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1332 enhance_image=DestroyImage(enhance_image);
1333 return((Image *) NULL);
1340 image_view=AcquireCacheView(image);
1341 enhance_view=AcquireCacheView(enhance_image);
1342 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1343 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1345 for (y=0; y < (ssize_t) image->rows; y++)
1347 register const Quantum
1359 if (status == MagickFalse)
1361 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1362 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1364 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1369 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1370 for (x=0; x < (ssize_t) image->columns; x++)
1375 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1391 register const Quantum
1394 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1395 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1396 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1397 if ((traits == UndefinedPixelTrait) ||
1398 (enhance_traits == UndefinedPixelTrait))
1400 q[channel]=p[center+i];
1401 if ((enhance_traits & CopyPixelTrait) != 0)
1404 Compute weighted average of target pixel color components.
1409 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1410 EnhancePixel(8.0); EnhancePixel(5.0);
1411 r=p+1*GetPixelChannels(image)*(image->columns+4);
1412 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1413 EnhancePixel(20.0); EnhancePixel(8.0);
1414 r=p+2*GetPixelChannels(image)*(image->columns+4);
1415 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1416 EnhancePixel(40.0); EnhancePixel(10.0);
1417 r=p+3*GetPixelChannels(image)*(image->columns+4);
1418 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1419 EnhancePixel(20.0); EnhancePixel(8.0);
1420 r=p+4*GetPixelChannels(image)*(image->columns+4);
1421 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1422 EnhancePixel(8.0); EnhancePixel(5.0);
1423 q[channel]=ClampToQuantum(aggregate/total_weight);
1425 p+=GetPixelChannels(image);
1426 q+=GetPixelChannels(enhance_image);
1428 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1436 #pragma omp critical (MagickCore_EnhanceImage)
1438 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1439 if (proceed == MagickFalse)
1443 enhance_view=DestroyCacheView(enhance_view);
1444 image_view=DestroyCacheView(image_view);
1445 return(enhance_image);
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % E q u a l i z e I m a g e %
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 % EqualizeImage() applies a histogram equalization to the image.
1461 % The format of the EqualizeImage method is:
1463 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1465 % A description of each parameter follows:
1467 % o image: the image.
1469 % o exception: return any errors or warnings in this structure.
1472 MagickExport MagickBooleanType EqualizeImage(Image *image,
1473 ExceptionInfo *exception)
1475 #define EqualizeImageTag "Equalize/Image"
1487 black[MaxPixelChannels],
1491 white[MaxPixelChannels];
1500 Allocate and initialize histogram arrays.
1502 assert(image != (Image *) NULL);
1503 assert(image->signature == MagickSignature);
1504 if (image->debug != MagickFalse)
1505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1506 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1507 GetPixelChannels(image)*sizeof(*equalize_map));
1508 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1509 GetPixelChannels(image)*sizeof(*histogram));
1510 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1511 GetPixelChannels(image)*sizeof(*map));
1512 if ((equalize_map == (MagickRealType *) NULL) ||
1513 (histogram == (MagickRealType *) NULL) ||
1514 (map == (MagickRealType *) NULL))
1516 if (map != (MagickRealType *) NULL)
1517 map=(MagickRealType *) RelinquishMagickMemory(map);
1518 if (histogram != (MagickRealType *) NULL)
1519 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1520 if (equalize_map != (MagickRealType *) NULL)
1521 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1522 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1529 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1530 sizeof(*histogram));
1531 image_view=AcquireCacheView(image);
1532 for (y=0; y < (ssize_t) image->rows; y++)
1534 register const Quantum
1540 if (status == MagickFalse)
1542 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1543 if (p == (const Quantum *) NULL)
1548 for (x=0; x < (ssize_t) image->columns; x++)
1553 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1554 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1555 p+=GetPixelChannels(image);
1559 Integrate the histogram to get the equalization map.
1561 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1562 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1564 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1573 for (j=0; j <= (ssize_t) MaxMap; j++)
1575 intensity+=histogram[GetPixelChannels(image)*j+i];
1576 map[GetPixelChannels(image)*j+i]=intensity;
1579 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1580 sizeof(*equalize_map));
1581 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1582 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1584 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1590 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1591 if (black[i] != white[i])
1592 for (j=0; j <= (ssize_t) MaxMap; j++)
1593 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1594 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1595 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1597 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1598 map=(MagickRealType *) RelinquishMagickMemory(map);
1599 if (image->storage_class == PseudoClass)
1607 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1608 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1610 for (j=0; j < (ssize_t) image->colors; j++)
1612 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1613 (white[i]!= black[i]))
1615 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1616 if (black[i] != white[i])
1617 image->colormap[j].red=ClampToQuantum(equalize_map[
1618 GetPixelChannels(image)*ScaleQuantumToMap(
1619 image->colormap[j].red)]+i);
1621 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1622 (white[i]!= black[i]))
1624 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1625 if (black[i] != white[i])
1626 image->colormap[j].green=ClampToQuantum(equalize_map[
1627 GetPixelChannels(image)*ScaleQuantumToMap(
1628 image->colormap[j].red)]+i);
1630 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1631 (white[i]!= black[i]))
1633 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1634 if (black[i] != white[i])
1635 image->colormap[j].blue=ClampToQuantum(equalize_map[
1636 GetPixelChannels(image)*ScaleQuantumToMap(
1637 image->colormap[j].blue)]+i);
1639 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1640 (white[i]!= black[i]))
1642 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1643 if (black[i] != white[i])
1644 image->colormap[j].alpha=ClampToQuantum(equalize_map[
1645 GetPixelChannels(image)*ScaleQuantumToMap(
1646 image->colormap[j].alpha)]+i);
1654 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1655 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1657 for (y=0; y < (ssize_t) image->rows; y++)
1665 if (status == MagickFalse)
1667 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1668 if (q == (Quantum *) NULL)
1673 for (x=0; x < (ssize_t) image->columns; x++)
1678 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1683 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1684 if (((traits & UpdatePixelTrait) != 0) && (black[i] != white[i]))
1685 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1686 ScaleQuantumToMap(q[i])+i]);
1688 q+=GetPixelChannels(image);
1690 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1692 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1697 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1698 #pragma omp critical (MagickCore_EqualizeImage)
1700 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1701 if (proceed == MagickFalse)
1705 image_view=DestroyCacheView(image_view);
1706 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % G a m m a I m a g e %
1719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 % GammaImage() gamma-corrects a particular image channel. The same
1722 % image viewed on different devices will have perceptual differences in the
1723 % way the image's intensities are represented on the screen. Specify
1724 % individual gamma levels for the red, green, and blue channels, or adjust
1725 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1727 % You can also reduce the influence of a particular channel with a gamma
1730 % The format of the GammaImage method is:
1732 % MagickBooleanType GammaImage(Image *image,const double gamma,
1733 % ExceptionInfo *exception)
1735 % A description of each parameter follows:
1737 % o image: the image.
1739 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1741 % o gamma: the image gamma.
1744 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1745 ExceptionInfo *exception)
1747 #define GammaCorrectImageTag "GammaCorrect/Image"
1768 Allocate and initialize gamma maps.
1770 assert(image != (Image *) NULL);
1771 assert(image->signature == MagickSignature);
1772 if (image->debug != MagickFalse)
1773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1776 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1777 if (gamma_map == (Quantum *) NULL)
1778 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1780 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1782 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1783 #pragma omp parallel for
1785 for (i=0; i <= (ssize_t) MaxMap; i++)
1786 gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
1787 MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
1788 if (image->storage_class == PseudoClass)
1791 Gamma-correct colormap.
1793 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1794 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1796 for (i=0; i < (ssize_t) image->colors; i++)
1798 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1799 image->colormap[i].red=gamma_map[
1800 ScaleQuantumToMap(image->colormap[i].red)];
1801 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1802 image->colormap[i].green=gamma_map[
1803 ScaleQuantumToMap(image->colormap[i].green)];
1804 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1805 image->colormap[i].blue=gamma_map[
1806 ScaleQuantumToMap(image->colormap[i].blue)];
1807 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1808 image->colormap[i].alpha=gamma_map[
1809 ScaleQuantumToMap(image->colormap[i].alpha)];
1813 Gamma-correct image.
1817 image_view=AcquireCacheView(image);
1818 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1819 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1821 for (y=0; y < (ssize_t) image->rows; y++)
1829 if (status == MagickFalse)
1831 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1832 if (q == (Quantum *) NULL)
1837 for (x=0; x < (ssize_t) image->columns; x++)
1842 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1847 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1848 if ((traits & UpdatePixelTrait) != 0)
1849 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
1851 q+=GetPixelChannels(image);
1853 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1855 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1860 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1861 #pragma omp critical (MagickCore_GammaImage)
1863 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1865 if (proceed == MagickFalse)
1869 image_view=DestroyCacheView(image_view);
1870 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
1871 if (image->gamma != 0.0)
1872 image->gamma*=gamma;
1877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1881 % H a l d C l u t I m a g e %
1885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1888 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1889 % Create it with the HALD coder. You can apply any color transformation to
1890 % the Hald image and then use this method to apply the transform to the
1893 % The format of the HaldClutImage method is:
1895 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1896 % ExceptionInfo *exception)
1898 % A description of each parameter follows:
1900 % o image: the image, which is replaced by indexed CLUT values
1902 % o hald_image: the color lookup table image for replacement color values.
1904 % o exception: return any errors or warnings in this structure.
1908 static inline size_t MagickMin(const size_t x,const size_t y)
1915 MagickExport MagickBooleanType HaldClutImage(Image *image,
1916 const Image *hald_image,ExceptionInfo *exception)
1918 #define HaldClutImageTag "Clut/Image"
1920 typedef struct _HaldInfo
1952 assert(image != (Image *) NULL);
1953 assert(image->signature == MagickSignature);
1954 if (image->debug != MagickFalse)
1955 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1956 assert(hald_image != (Image *) NULL);
1957 assert(hald_image->signature == MagickSignature);
1958 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1959 return(MagickFalse);
1960 if (image->matte == MagickFalse)
1961 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1967 length=MagickMin(hald_image->columns,hald_image->rows);
1968 for (level=2; (level*level*level) < length; level++) ;
1970 cube_size=level*level;
1971 width=(double) hald_image->columns;
1972 GetPixelInfo(hald_image,&zero);
1973 image_view=AcquireCacheView(image);
1974 hald_view=AcquireCacheView(hald_image);
1975 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1976 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1978 for (y=0; y < (ssize_t) image->rows; y++)
1986 if (status == MagickFalse)
1988 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1989 if (q == (Quantum *) NULL)
1994 for (x=0; x < (ssize_t) image->columns; x++)
2009 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2010 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2011 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2012 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2013 point.x-=floor(point.x);
2014 point.y-=floor(point.y);
2015 point.z-=floor(point.z);
2017 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2018 fmod(offset,width),floor(offset/width),&pixel1,exception);
2020 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2021 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2023 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2026 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2027 fmod(offset,width),floor(offset/width),&pixel1,exception);
2028 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2029 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2031 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2034 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2036 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2037 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2038 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2039 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2040 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2041 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2042 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2043 (image->colorspace == CMYKColorspace))
2044 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2045 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2046 (image->matte != MagickFalse))
2047 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2048 q+=GetPixelChannels(image);
2050 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2052 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2057 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2058 #pragma omp critical (MagickCore_HaldClutImage)
2060 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2061 if (proceed == MagickFalse)
2065 hald_view=DestroyCacheView(hald_view);
2066 image_view=DestroyCacheView(image_view);
2071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2075 % L e v e l I m a g e %
2079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2081 % LevelImage() adjusts the levels of a particular image channel by
2082 % scaling the colors falling between specified white and black points to
2083 % the full available quantum range.
2085 % The parameters provided represent the black, and white points. The black
2086 % point specifies the darkest color in the image. Colors darker than the
2087 % black point are set to zero. White point specifies the lightest color in
2088 % the image. Colors brighter than the white point are set to the maximum
2091 % If a '!' flag is given, map black and white colors to the given levels
2092 % rather than mapping those levels to black and white. See
2093 % LevelizeImage() below.
2095 % Gamma specifies a gamma correction to apply to the image.
2097 % The format of the LevelImage method is:
2099 % MagickBooleanType LevelImage(Image *image,const double black_point,
2100 % const double white_point,const double gamma,ExceptionInfo *exception)
2102 % A description of each parameter follows:
2104 % o image: the image.
2106 % o black_point: The level to map zero (black) to.
2108 % o white_point: The level to map QuantumRange (white) to.
2110 % o exception: return any errors or warnings in this structure.
2113 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2114 const double white_point,const double gamma,ExceptionInfo *exception)
2116 #define LevelImageTag "Level/Image"
2117 #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
2118 pow(scale*((double) (x)-black_point),1.0/gamma)))
2139 Allocate and initialize levels map.
2141 assert(image != (Image *) NULL);
2142 assert(image->signature == MagickSignature);
2143 if (image->debug != MagickFalse)
2144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2145 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2146 if (image->storage_class == PseudoClass)
2147 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2148 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2150 for (i=0; i < (ssize_t) image->colors; i++)
2155 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2156 image->colormap[i].red=LevelQuantum(image->colormap[i].red);
2157 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2158 image->colormap[i].green=LevelQuantum(image->colormap[i].green);
2159 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2160 image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
2161 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2162 image->colormap[i].alpha=LevelQuantum(image->colormap[i].alpha);
2169 image_view=AcquireCacheView(image);
2170 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2171 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2173 for (y=0; y < (ssize_t) image->rows; y++)
2181 if (status == MagickFalse)
2183 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2184 if (q == (Quantum *) NULL)
2189 for (x=0; x < (ssize_t) image->columns; x++)
2194 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2199 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2200 if ((traits == UndefinedPixelTrait) ||
2201 ((traits & UpdatePixelTrait) == 0))
2203 q[i]=LevelQuantum(q[i]);
2205 q+=GetPixelChannels(image);
2207 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2209 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2214 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2215 #pragma omp critical (MagickCore_LevelImage)
2217 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2218 if (proceed == MagickFalse)
2222 image_view=DestroyCacheView(image_view);
2227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2231 % L e v e l i z e I m a g e %
2235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2237 % LevelizeImage() applies the reversed LevelImage() operation to just
2238 % the specific channels specified. It compresses the full range of color
2239 % values, so that they lie between the given black and white points. Gamma is
2240 % applied before the values are mapped.
2242 % LevelizeImage() can be called with by using a +level command line
2243 % API option, or using a '!' on a -level or LevelImage() geometry string.
2245 % It can be used for example de-contrast a greyscale image to the exact
2246 % levels specified. Or by using specific levels for each channel of an image
2247 % you can convert a gray-scale image to any linear color gradient, according
2250 % The format of the LevelizeImage method is:
2252 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2253 % const double white_point,const double gamma,ExceptionInfo *exception)
2255 % A description of each parameter follows:
2257 % o image: the image.
2259 % o black_point: The level to map zero (black) to.
2261 % o white_point: The level to map QuantumRange (white) to.
2263 % o gamma: adjust gamma by this factor before mapping values.
2265 % o exception: return any errors or warnings in this structure.
2268 MagickExport MagickBooleanType LevelizeImage(Image *image,
2269 const double black_point,const double white_point,const double gamma,
2270 ExceptionInfo *exception)
2272 #define LevelizeImageTag "Levelize/Image"
2273 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2274 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2293 Allocate and initialize levels map.
2295 assert(image != (Image *) NULL);
2296 assert(image->signature == MagickSignature);
2297 if (image->debug != MagickFalse)
2298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2299 if (image->storage_class == PseudoClass)
2300 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2301 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2303 for (i=0; i < (ssize_t) image->colors; i++)
2308 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2309 image->colormap[i].red=LevelizeValue(image->colormap[i].red);
2310 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2311 image->colormap[i].green=LevelizeValue(image->colormap[i].green);
2312 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2313 image->colormap[i].blue=LevelizeValue(image->colormap[i].blue);
2314 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2315 image->colormap[i].alpha=LevelizeValue(image->colormap[i].alpha);
2322 exception=(&image->exception);
2323 image_view=AcquireCacheView(image);
2324 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2325 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2327 for (y=0; y < (ssize_t) image->rows; y++)
2335 if (status == MagickFalse)
2337 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2338 if (q == (Quantum *) NULL)
2343 for (x=0; x < (ssize_t) image->columns; x++)
2348 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2353 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2354 if ((traits & UpdatePixelTrait) != 0)
2355 q[i]=LevelizeValue(q[i]);
2357 q+=GetPixelChannels(image);
2359 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2361 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2366 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2367 #pragma omp critical (MagickCore_LevelizeImage)
2369 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2370 if (proceed == MagickFalse)
2374 image_view=DestroyCacheView(image_view);
2379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2383 % L e v e l I m a g e C o l o r s %
2387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2389 % LevelImageColors() maps the given color to "black" and "white" values,
2390 % linearly spreading out the colors, and level values on a channel by channel
2391 % bases, as per LevelImage(). The given colors allows you to specify
2392 % different level ranges for each of the color channels separately.
2394 % If the boolean 'invert' is set true the image values will modifyed in the
2395 % reverse direction. That is any existing "black" and "white" colors in the
2396 % image will become the color values given, with all other values compressed
2397 % appropriatally. This effectivally maps a greyscale gradient into the given
2400 % The format of the LevelImageColors method is:
2402 % MagickBooleanType LevelImageColors(Image *image,
2403 % const PixelInfo *black_color,const PixelInfo *white_color,
2404 % const MagickBooleanType invert,ExceptionInfo *exception)
2406 % A description of each parameter follows:
2408 % o image: the image.
2410 % o black_color: The color to map black to/from
2412 % o white_point: The color to map white to/from
2414 % o invert: if true map the colors (levelize), rather than from (level)
2416 % o exception: return any errors or warnings in this structure.
2419 MagickExport MagickBooleanType LevelImageColors(Image *image,
2420 const PixelInfo *black_color,const PixelInfo *white_color,
2421 const MagickBooleanType invert,ExceptionInfo *exception)
2430 Allocate and initialize levels map.
2432 assert(image != (Image *) NULL);
2433 assert(image->signature == MagickSignature);
2434 if (image->debug != MagickFalse)
2435 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2437 if (invert == MagickFalse)
2439 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2441 channel_mask=SetPixelChannelMask(image,RedChannel);
2442 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2444 (void) SetPixelChannelMask(image,channel_mask);
2446 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2448 channel_mask=SetPixelChannelMask(image,GreenChannel);
2449 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2451 (void) SetPixelChannelMask(image,channel_mask);
2453 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2455 channel_mask=SetPixelChannelMask(image,BlueChannel);
2456 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2458 (void) SetPixelChannelMask(image,channel_mask);
2460 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2461 (image->colorspace == CMYKColorspace))
2463 channel_mask=SetPixelChannelMask(image,BlackChannel);
2464 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2466 (void) SetPixelChannelMask(image,channel_mask);
2468 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2469 (image->matte == MagickTrue))
2471 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2472 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2474 (void) SetPixelChannelMask(image,channel_mask);
2479 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2481 channel_mask=SetPixelChannelMask(image,RedChannel);
2482 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2484 (void) SetPixelChannelMask(image,channel_mask);
2486 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2488 channel_mask=SetPixelChannelMask(image,GreenChannel);
2489 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2491 (void) SetPixelChannelMask(image,channel_mask);
2493 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2495 channel_mask=SetPixelChannelMask(image,BlueChannel);
2496 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2498 (void) SetPixelChannelMask(image,channel_mask);
2500 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2501 (image->colorspace == CMYKColorspace))
2503 channel_mask=SetPixelChannelMask(image,BlackChannel);
2504 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2506 (void) SetPixelChannelMask(image,channel_mask);
2508 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2509 (image->matte == MagickTrue))
2511 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2512 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2514 (void) SetPixelChannelMask(image,channel_mask);
2517 return(status == 0 ? MagickFalse : MagickTrue);
2521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2525 % L i n e a r S t r e t c h I m a g e %
2529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2531 % LinearStretchImage() discards any pixels below the black point and above
2532 % the white point and levels the remaining pixels.
2534 % The format of the LinearStretchImage method is:
2536 % MagickBooleanType LinearStretchImage(Image *image,
2537 % const double black_point,const double white_point,
2538 % ExceptionInfo *exception)
2540 % A description of each parameter follows:
2542 % o image: the image.
2544 % o black_point: the black point.
2546 % o white_point: the white point.
2548 % o exception: return any errors or warnings in this structure.
2551 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2552 const double black_point,const double white_point,ExceptionInfo *exception)
2554 #define LinearStretchImageTag "LinearStretch/Image"
2572 Allocate histogram and linear map.
2574 assert(image != (Image *) NULL);
2575 assert(image->signature == MagickSignature);
2576 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2577 sizeof(*histogram));
2578 if (histogram == (MagickRealType *) NULL)
2579 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2584 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2585 image_view=AcquireCacheView(image);
2586 for (y=0; y < (ssize_t) image->rows; y++)
2588 register const Quantum
2594 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2595 if (p == (const Quantum *) NULL)
2597 for (x=0; x < (ssize_t) image->columns; x++)
2599 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2600 p+=GetPixelChannels(image);
2603 image_view=DestroyCacheView(image_view);
2605 Find the histogram boundaries by locating the black and white point levels.
2608 for (black=0; black < (ssize_t) MaxMap; black++)
2610 intensity+=histogram[black];
2611 if (intensity >= black_point)
2615 for (white=(ssize_t) MaxMap; white != 0; white--)
2617 intensity+=histogram[white];
2618 if (intensity >= white_point)
2621 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2622 status=LevelImage(image,(double) black,(double) white,1.0,&image->exception);
2627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2631 % M o d u l a t e I m a g e %
2635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2637 % ModulateImage() lets you control the brightness, saturation, and hue
2638 % of an image. Modulate represents the brightness, saturation, and hue
2639 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2640 % modulation is lightness, saturation, and hue. And if the colorspace is
2641 % HWB, use blackness, whiteness, and hue.
2643 % The format of the ModulateImage method is:
2645 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2646 % ExceptionInfo *exception)
2648 % A description of each parameter follows:
2650 % o image: the image.
2652 % o modulate: Define the percent change in brightness, saturation, and hue.
2654 % o exception: return any errors or warnings in this structure.
2658 static void ModulateHSB(const double percent_hue,
2659 const double percent_saturation,const double percent_brightness,
2660 Quantum *red,Quantum *green,Quantum *blue)
2668 Increase or decrease color brightness, saturation, or hue.
2670 assert(red != (Quantum *) NULL);
2671 assert(green != (Quantum *) NULL);
2672 assert(blue != (Quantum *) NULL);
2673 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2674 hue+=0.5*(0.01*percent_hue-1.0);
2679 saturation*=0.01*percent_saturation;
2680 brightness*=0.01*percent_brightness;
2681 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2684 static void ModulateHSL(const double percent_hue,
2685 const double percent_saturation,const double percent_lightness,
2686 Quantum *red,Quantum *green,Quantum *blue)
2694 Increase or decrease color lightness, saturation, or hue.
2696 assert(red != (Quantum *) NULL);
2697 assert(green != (Quantum *) NULL);
2698 assert(blue != (Quantum *) NULL);
2699 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2700 hue+=0.5*(0.01*percent_hue-1.0);
2705 saturation*=0.01*percent_saturation;
2706 lightness*=0.01*percent_lightness;
2707 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2710 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue)
2718 Increase or decrease color blackness, whiteness, or hue.
2720 assert(red != (Quantum *) NULL);
2721 assert(green != (Quantum *) NULL);
2722 assert(blue != (Quantum *) NULL);
2723 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2724 hue+=0.5*(0.01*percent_hue-1.0);
2729 blackness*=0.01*percent_blackness;
2730 whiteness*=0.01*percent_whiteness;
2731 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2734 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2735 ExceptionInfo *exception)
2737 #define ModulateImageTag "Modulate/Image"
2772 Initialize modulate table.
2774 assert(image != (Image *) NULL);
2775 assert(image->signature == MagickSignature);
2776 if (image->debug != MagickFalse)
2777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2778 if (modulate == (char *) NULL)
2779 return(MagickFalse);
2780 flags=ParseGeometry(modulate,&geometry_info);
2781 percent_brightness=geometry_info.rho;
2782 percent_saturation=geometry_info.sigma;
2783 if ((flags & SigmaValue) == 0)
2784 percent_saturation=100.0;
2785 percent_hue=geometry_info.xi;
2786 if ((flags & XiValue) == 0)
2788 colorspace=UndefinedColorspace;
2789 artifact=GetImageArtifact(image,"modulate:colorspace");
2790 if (artifact != (const char *) NULL)
2791 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2792 MagickFalse,artifact);
2793 if (image->storage_class == PseudoClass)
2798 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2799 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2801 for (i=0; i < (ssize_t) image->colors; i++)
2806 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2807 &image->colormap[i].red,&image->colormap[i].green,
2808 &image->colormap[i].blue);
2814 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2815 &image->colormap[i].red,&image->colormap[i].green,
2816 &image->colormap[i].blue);
2821 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2822 &image->colormap[i].red,&image->colormap[i].green,
2823 &image->colormap[i].blue);
2833 image_view=AcquireCacheView(image);
2834 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2835 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2837 for (y=0; y < (ssize_t) image->rows; y++)
2850 if (status == MagickFalse)
2852 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2853 if (q == (Quantum *) NULL)
2858 for (x=0; x < (ssize_t) image->columns; x++)
2860 red=GetPixelRed(image,q);
2861 green=GetPixelGreen(image,q);
2862 blue=GetPixelBlue(image,q);
2867 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2874 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2880 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2885 SetPixelRed(image,red,q);
2886 SetPixelGreen(image,green,q);
2887 SetPixelBlue(image,blue,q);
2888 q+=GetPixelChannels(image);
2890 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2892 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2897 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2898 #pragma omp critical (MagickCore_ModulateImage)
2900 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
2901 if (proceed == MagickFalse)
2905 image_view=DestroyCacheView(image_view);
2910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2914 % N e g a t e I m a g e %
2918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2920 % NegateImage() negates the colors in the reference image. The grayscale
2921 % option means that only grayscale values within the image are negated.
2923 % The format of the NegateImage method is:
2925 % MagickBooleanType NegateImage(Image *image,
2926 % const MagickBooleanType grayscale,ExceptionInfo *exception)
2928 % A description of each parameter follows:
2930 % o image: the image.
2932 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
2934 % o exception: return any errors or warnings in this structure.
2937 MagickExport MagickBooleanType NegateImage(Image *image,
2938 const MagickBooleanType grayscale,ExceptionInfo *exception)
2940 #define NegateImageTag "Negate/Image"
2957 assert(image != (Image *) NULL);
2958 assert(image->signature == MagickSignature);
2959 if (image->debug != MagickFalse)
2960 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2961 if (image->storage_class == PseudoClass)
2966 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2967 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2969 for (i=0; i < (ssize_t) image->colors; i++)
2971 if (grayscale != MagickFalse)
2972 if ((image->colormap[i].red != image->colormap[i].green) ||
2973 (image->colormap[i].green != image->colormap[i].blue))
2975 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2976 image->colormap[i].red=(Quantum) QuantumRange-
2977 image->colormap[i].red;
2978 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2979 image->colormap[i].green=(Quantum) QuantumRange-
2980 image->colormap[i].green;
2981 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2982 image->colormap[i].blue=(Quantum) QuantumRange-
2983 image->colormap[i].blue;
2991 image_view=AcquireCacheView(image);
2992 if (grayscale != MagickFalse)
2994 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2995 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2997 for (y=0; y < (ssize_t) image->rows; y++)
3008 if (status == MagickFalse)
3010 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3012 if (q == (Quantum *) NULL)
3017 for (x=0; x < (ssize_t) image->columns; x++)
3022 if (IsPixelGray(image,q) != MagickFalse)
3024 q+=GetPixelChannels(image);
3027 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3032 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3033 if ((traits & UpdatePixelTrait) != 0)
3034 q[i]=QuantumRange-q[i];
3036 q+=GetPixelChannels(image);
3038 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3039 if (sync == MagickFalse)
3041 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3046 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3047 #pragma omp critical (MagickCore_NegateImage)
3049 proceed=SetImageProgress(image,NegateImageTag,progress++,
3051 if (proceed == MagickFalse)
3055 image_view=DestroyCacheView(image_view);
3061 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3062 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3064 for (y=0; y < (ssize_t) image->rows; y++)
3072 if (status == MagickFalse)
3074 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3075 if (q == (Quantum *) NULL)
3080 for (x=0; x < (ssize_t) image->columns; x++)
3085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3090 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3091 if ((traits & UpdatePixelTrait) != 0)
3092 q[i]=QuantumRange-q[i];
3094 q+=GetPixelChannels(image);
3096 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3098 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3103 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3104 #pragma omp critical (MagickCore_NegateImage)
3106 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3107 if (proceed == MagickFalse)
3111 image_view=DestroyCacheView(image_view);
3116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3120 % N o r m a l i z e I m a g e %
3124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3126 % NormalizeImage() enhances the contrast of a color image by mapping the
3127 % darkest 2 percent of all pixel to black and the brightest 1 percent to white.
3129 % The format of the NormalizeImage method is:
3131 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3133 % A description of each parameter follows:
3135 % o image: the image.
3137 % o exception: return any errors or warnings in this structure.
3140 MagickExport MagickBooleanType NormalizeImage(Image *image,
3141 ExceptionInfo *exception)
3147 black_point=(double) image->columns*image->rows*0.0015;
3148 white_point=(double) image->columns*image->rows*0.9995;
3149 return(ContrastStretchImage(image,black_point,white_point,exception));
3153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3157 % S i g m o i d a l C o n t r a s t I m a g e %
3161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3163 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3164 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3165 % sigmoidal transfer function without saturating highlights or shadows.
3166 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3167 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3168 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3169 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3172 % The format of the SigmoidalContrastImage method is:
3174 % MagickBooleanType SigmoidalContrastImage(Image *image,
3175 % const MagickBooleanType sharpen,const char *levels,
3176 % ExceptionInfo *exception)
3178 % A description of each parameter follows:
3180 % o image: the image.
3182 % o sharpen: Increase or decrease image contrast.
3184 % o alpha: strength of the contrast, the larger the number the more
3185 % 'threshold-like' it becomes.
3187 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3189 % o exception: return any errors or warnings in this structure.
3192 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3193 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3194 ExceptionInfo *exception)
3196 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3217 Allocate and initialize sigmoidal maps.
3219 assert(image != (Image *) NULL);
3220 assert(image->signature == MagickSignature);
3221 if (image->debug != MagickFalse)
3222 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3223 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3224 sizeof(*sigmoidal_map));
3225 if (sigmoidal_map == (MagickRealType *) NULL)
3226 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3228 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3229 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3230 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3232 for (i=0; i <= (ssize_t) MaxMap; i++)
3234 if (sharpen != MagickFalse)
3236 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3237 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3238 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3239 QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/(double)
3240 QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/(double)
3241 QuantumRange)))))+0.5));
3244 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3245 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/(double)
3246 QuantumRange*contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(
3247 midpoint/(double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double)
3248 QuantumRange*contrast))))))/(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3249 contrast))+((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/(double)
3250 QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/(double) QuantumRange*
3251 contrast))))))/contrast)));
3253 if (image->storage_class == PseudoClass)
3256 Sigmoidal-contrast enhance colormap.
3258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3259 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3261 for (i=0; i < (ssize_t) image->colors; i++)
3263 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3264 image->colormap[i].red=ClampToQuantum(sigmoidal_map[
3265 ScaleQuantumToMap(image->colormap[i].red)]);
3266 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3267 image->colormap[i].green=ClampToQuantum(sigmoidal_map[
3268 ScaleQuantumToMap(image->colormap[i].green)]);
3269 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3270 image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
3271 ScaleQuantumToMap(image->colormap[i].blue)]);
3272 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3273 image->colormap[i].alpha=ClampToQuantum(sigmoidal_map[
3274 ScaleQuantumToMap(image->colormap[i].alpha)]);
3278 Sigmoidal-contrast enhance image.
3282 image_view=AcquireCacheView(image);
3283 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3284 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3286 for (y=0; y < (ssize_t) image->rows; y++)
3294 if (status == MagickFalse)
3296 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3297 if (q == (Quantum *) NULL)
3302 for (x=0; x < (ssize_t) image->columns; x++)
3307 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3312 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3313 if ((traits & UpdatePixelTrait) != 0)
3314 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3316 q+=GetPixelChannels(image);
3318 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3320 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3326 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3328 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3330 if (proceed == MagickFalse)
3334 image_view=DestroyCacheView(image_view);
3335 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);