2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE N N H H AAA N N CCCC EEEEE %
7 % E NN N H H A A NN N C E %
8 % EEE N N N HHHHH AAAAA N N N C EEE %
9 % E N NN H H A A N NN C E %
10 % EEEEE N N H H A A N N CCCC EEEEE %
13 % MagickCore Image Enhancement Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/composite-private.h"
52 #include "MagickCore/enhance.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/fx.h"
56 #include "MagickCore/gem.h"
57 #include "MagickCore/gem-private.h"
58 #include "MagickCore/geometry.h"
59 #include "MagickCore/histogram.h"
60 #include "MagickCore/image.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/quantum.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/resample.h"
71 #include "MagickCore/resample-private.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #include "MagickCore/thread-private.h"
77 #include "MagickCore/token.h"
78 #include "MagickCore/xml-tree.h"
79 #include "MagickCore/xml-tree-private.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % A u t o G a m m a I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % AutoGammaImage() extract the 'mean' from the image and adjust the image
93 % to try make set its gamma appropriatally.
95 % The format of the AutoGammaImage method is:
97 % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
99 % A description of each parameter follows:
101 % o image: The image to auto-level
103 % o exception: return any errors or warnings in this structure.
106 MagickExport MagickBooleanType AutoGammaImage(Image *image,
107 ExceptionInfo *exception)
122 if (image->channel_mask == DefaultChannels)
125 Apply gamma correction equally across all given channels.
127 (void) GetImageMean(image,&mean,&sans,exception);
128 gamma=log(mean*QuantumScale)/log_mean;
129 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
132 Auto-gamma each channel separately.
135 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
146 channel=GetPixelChannelMapChannel(image,i);
147 traits=GetPixelChannelMapTraits(image,channel);
148 if ((traits & UpdatePixelTrait) == 0)
150 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
151 status=GetImageMean(image,&mean,&sans,exception);
152 gamma=log(mean*QuantumScale)/log_mean;
153 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
154 (void) SetPixelChannelMask(image,channel_mask);
155 if (status == MagickFalse)
158 return(status != 0 ? MagickTrue : MagickFalse);
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % A u t o L e v e l I m a g e %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % AutoLevelImage() adjusts the levels of a particular image channel by
173 % scaling the minimum and maximum values to the full quantum range.
175 % The format of the LevelImage method is:
177 % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
179 % A description of each parameter follows:
181 % o image: The image to auto-level
183 % o exception: return any errors or warnings in this structure.
186 MagickExport MagickBooleanType AutoLevelImage(Image *image,
187 ExceptionInfo *exception)
189 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % B r i g h t n e s s C o n t r a s t I m a g e %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % BrightnessContrastImage() changes the brightness and/or contrast of an
204 % image. It converts the brightness and contrast parameters into slope and
205 % intercept and calls a polynomical function to apply to the image.
207 % The format of the BrightnessContrastImage method is:
209 % MagickBooleanType BrightnessContrastImage(Image *image,
210 % const double brightness,const double contrast,ExceptionInfo *exception)
212 % A description of each parameter follows:
214 % o image: the image.
216 % o brightness: the brightness percent (-100 .. 100).
218 % o contrast: the contrast percent (-100 .. 100).
220 % o exception: return any errors or warnings in this structure.
223 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
224 const double brightness,const double contrast,ExceptionInfo *exception)
226 #define BrightnessContastImageTag "BrightnessContast/Image"
238 Compute slope and intercept.
240 assert(image != (Image *) NULL);
241 assert(image->signature == MagickSignature);
242 if (image->debug != MagickFalse)
243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
245 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
248 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
249 coefficients[0]=slope;
250 coefficients[1]=intercept;
251 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % C l u t I m a g e %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % ClutImage() replaces each color value in the given image, by using it as an
267 % index to lookup a replacement color value in a Color Look UP Table in the
268 % form of an image. The values are extracted along a diagonal of the CLUT
269 % image so either a horizontal or vertial gradient image can be used.
271 % Typically this is used to either re-color a gray-scale image according to a
272 % color gradient in the CLUT image, or to perform a freeform histogram
273 % (level) adjustment according to the (typically gray-scale) gradient in the
276 % When the 'channel' mask includes the matte/alpha transparency channel but
277 % one image has no such channel it is assumed that that image is a simple
278 % gray-scale image that will effect the alpha channel values, either for
279 % gray-scale coloring (with transparent or semi-transparent colors), or
280 % a histogram adjustment of existing alpha channel values. If both images
281 % have matte channels, direct and normal indexing is applied, which is rarely
284 % The format of the ClutImage method is:
286 % MagickBooleanType ClutImage(Image *image,Image *clut_image,
287 % const PixelInterpolateMethod method,ExceptionInfo *exception)
289 % A description of each parameter follows:
291 % o image: the image, which is replaced by indexed CLUT values
293 % o clut_image: the color lookup table image for replacement color values.
295 % o method: the pixel interpolation method.
297 % o exception: return any errors or warnings in this structure.
300 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
301 const PixelInterpolateMethod method,ExceptionInfo *exception)
303 #define ClutImageTag "Clut/Image"
324 assert(image != (Image *) NULL);
325 assert(image->signature == MagickSignature);
326 if (image->debug != MagickFalse)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
328 assert(clut_image != (Image *) NULL);
329 assert(clut_image->signature == MagickSignature);
330 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
332 if (IsGrayColorspace(image->colorspace) != MagickFalse)
333 (void) TransformImageColorspace(image,sRGBColorspace,exception);
334 clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
335 if (clut_map == (PixelInfo *) NULL)
336 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
343 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
344 clut_view=AcquireVirtualCacheView(clut_image,exception);
345 #if defined(MAGICKCORE_OPENMP_SUPPORT)
346 #pragma omp parallel for schedule(static,4) \
347 dynamic_number_threads(image,image->columns,1,1)
349 for (i=0; i <= (ssize_t) MaxMap; i++)
351 GetPixelInfo(clut_image,clut_map+i);
352 (void) InterpolatePixelInfo(clut_image,clut_view,method,
353 QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i*
354 (clut_image->rows-adjust),clut_map+i,exception);
356 clut_view=DestroyCacheView(clut_view);
357 image_view=AcquireAuthenticCacheView(image,exception);
358 #if defined(MAGICKCORE_OPENMP_SUPPORT)
359 #pragma omp parallel for schedule(static,4) shared(progress,status) \
360 dynamic_number_threads(image,image->columns,image->rows,1)
362 for (y=0; y < (ssize_t) image->rows; y++)
373 if (status == MagickFalse)
375 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
376 if (q == (Quantum *) NULL)
381 GetPixelInfo(image,&pixel);
382 for (x=0; x < (ssize_t) image->columns; x++)
384 if (GetPixelMask(image,q) != 0)
386 q+=GetPixelChannels(image);
389 GetPixelInfoPixel(image,q,&pixel);
390 pixel.red=clut_map[ScaleQuantumToMap(
391 ClampToQuantum(pixel.red))].red;
392 pixel.green=clut_map[ScaleQuantumToMap(
393 ClampToQuantum(pixel.green))].green;
394 pixel.blue=clut_map[ScaleQuantumToMap(
395 ClampToQuantum(pixel.blue))].blue;
396 pixel.black=clut_map[ScaleQuantumToMap(
397 ClampToQuantum(pixel.black))].black;
398 pixel.alpha=clut_map[ScaleQuantumToMap(
399 ClampToQuantum(pixel.alpha))].alpha;
400 SetPixelInfoPixel(image,&pixel,q);
401 q+=GetPixelChannels(image);
403 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
410 #if defined(MAGICKCORE_OPENMP_SUPPORT)
411 #pragma omp critical (MagickCore_ClutImage)
413 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
414 if (proceed == MagickFalse)
418 image_view=DestroyCacheView(image_view);
419 clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
420 if ((clut_image->matte != MagickFalse) &&
421 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
422 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % C o l o r D e c i s i o n L i s t I m a g e %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
438 % (CCC) file which solely contains one or more color corrections and applies
439 % the correction to the image. Here is a sample CCC file:
441 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
442 % <ColorCorrection id="cc03345">
444 % <Slope> 0.9 1.2 0.5 </Slope>
445 % <Offset> 0.4 -0.5 0.6 </Offset>
446 % <Power> 1.0 0.8 1.5 </Power>
449 % <Saturation> 0.85 </Saturation>
452 % </ColorCorrectionCollection>
454 % which includes the slop, offset, and power for each of the RGB channels
455 % as well as the saturation.
457 % The format of the ColorDecisionListImage method is:
459 % MagickBooleanType ColorDecisionListImage(Image *image,
460 % const char *color_correction_collection,ExceptionInfo *exception)
462 % A description of each parameter follows:
464 % o image: the image.
466 % o color_correction_collection: the color correction collection in XML.
468 % o exception: return any errors or warnings in this structure.
471 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
472 const char *color_correction_collection,ExceptionInfo *exception)
474 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
476 typedef struct _Correction
484 typedef struct _ColorCorrection
499 token[MaxTextExtent];
530 Allocate and initialize cdl maps.
532 assert(image != (Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 if (color_correction_collection == (const char *) NULL)
538 ccc=NewXMLTree((const char *) color_correction_collection,exception);
539 if (ccc == (XMLTreeInfo *) NULL)
541 cc=GetXMLTreeChild(ccc,"ColorCorrection");
542 if (cc == (XMLTreeInfo *) NULL)
544 ccc=DestroyXMLTree(ccc);
547 color_correction.red.slope=1.0;
548 color_correction.red.offset=0.0;
549 color_correction.red.power=1.0;
550 color_correction.green.slope=1.0;
551 color_correction.green.offset=0.0;
552 color_correction.green.power=1.0;
553 color_correction.blue.slope=1.0;
554 color_correction.blue.offset=0.0;
555 color_correction.blue.power=1.0;
556 color_correction.saturation=0.0;
557 sop=GetXMLTreeChild(cc,"SOPNode");
558 if (sop != (XMLTreeInfo *) NULL)
565 slope=GetXMLTreeChild(sop,"Slope");
566 if (slope != (XMLTreeInfo *) NULL)
568 content=GetXMLTreeContent(slope);
569 p=(const char *) content;
570 for (i=0; (*p != '\0') && (i < 3); i++)
572 GetMagickToken(p,&p,token);
574 GetMagickToken(p,&p,token);
579 color_correction.red.slope=StringToDouble(token,(char **) NULL);
584 color_correction.green.slope=StringToDouble(token,
590 color_correction.blue.slope=StringToDouble(token,
597 offset=GetXMLTreeChild(sop,"Offset");
598 if (offset != (XMLTreeInfo *) NULL)
600 content=GetXMLTreeContent(offset);
601 p=(const char *) content;
602 for (i=0; (*p != '\0') && (i < 3); i++)
604 GetMagickToken(p,&p,token);
606 GetMagickToken(p,&p,token);
611 color_correction.red.offset=StringToDouble(token,
617 color_correction.green.offset=StringToDouble(token,
623 color_correction.blue.offset=StringToDouble(token,
630 power=GetXMLTreeChild(sop,"Power");
631 if (power != (XMLTreeInfo *) NULL)
633 content=GetXMLTreeContent(power);
634 p=(const char *) content;
635 for (i=0; (*p != '\0') && (i < 3); i++)
637 GetMagickToken(p,&p,token);
639 GetMagickToken(p,&p,token);
644 color_correction.red.power=StringToDouble(token,(char **) NULL);
649 color_correction.green.power=StringToDouble(token,
655 color_correction.blue.power=StringToDouble(token,
663 sat=GetXMLTreeChild(cc,"SATNode");
664 if (sat != (XMLTreeInfo *) NULL)
669 saturation=GetXMLTreeChild(sat,"Saturation");
670 if (saturation != (XMLTreeInfo *) NULL)
672 content=GetXMLTreeContent(saturation);
673 p=(const char *) content;
674 GetMagickToken(p,&p,token);
675 color_correction.saturation=StringToDouble(token,(char **) NULL);
678 ccc=DestroyXMLTree(ccc);
679 if (image->debug != MagickFalse)
681 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
682 " Color Correction Collection:");
683 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
684 " color_correction.red.slope: %g",color_correction.red.slope);
685 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
686 " color_correction.red.offset: %g",color_correction.red.offset);
687 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
688 " color_correction.red.power: %g",color_correction.red.power);
689 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
690 " color_correction.green.slope: %g",color_correction.green.slope);
691 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
692 " color_correction.green.offset: %g",color_correction.green.offset);
693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
694 " color_correction.green.power: %g",color_correction.green.power);
695 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
696 " color_correction.blue.slope: %g",color_correction.blue.slope);
697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
698 " color_correction.blue.offset: %g",color_correction.blue.offset);
699 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
700 " color_correction.blue.power: %g",color_correction.blue.power);
701 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
702 " color_correction.saturation: %g",color_correction.saturation);
704 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
705 if (cdl_map == (PixelInfo *) NULL)
706 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
708 #if defined(MAGICKCORE_OPENMP_SUPPORT)
709 #pragma omp parallel for schedule(static,4) \
710 dynamic_number_threads(image,image->columns,1,1)
712 for (i=0; i <= (ssize_t) MaxMap; i++)
714 cdl_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
715 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
716 color_correction.red.offset,color_correction.red.power))));
717 cdl_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
718 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
719 color_correction.green.offset,color_correction.green.power))));
720 cdl_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
721 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
722 color_correction.blue.offset,color_correction.blue.power))));
724 if (image->storage_class == PseudoClass)
727 Apply transfer function to colormap.
729 #if defined(MAGICKCORE_OPENMP_SUPPORT)
730 #pragma omp parallel for schedule(static,4) shared(progress,status) \
731 dynamic_number_threads(image,image->columns,1,1)
733 for (i=0; i < (ssize_t) image->colors; i++)
738 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
739 0.0722*image->colormap[i].blue;
740 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
741 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-
743 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
744 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-
746 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
747 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-
752 Apply transfer function to image.
756 image_view=AcquireAuthenticCacheView(image,exception);
757 #if defined(MAGICKCORE_OPENMP_SUPPORT)
758 #pragma omp parallel for schedule(static,4) shared(progress,status) \
759 dynamic_number_threads(image,image->columns,image->rows,1)
761 for (y=0; y < (ssize_t) image->rows; y++)
772 if (status == MagickFalse)
774 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
775 if (q == (Quantum *) NULL)
780 for (x=0; x < (ssize_t) image->columns; x++)
782 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
783 GetPixelBlue(image,q);
784 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
785 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
786 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
787 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
788 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
789 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
790 q+=GetPixelChannels(image);
792 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
799 #if defined(MAGICKCORE_OPENMP_SUPPORT)
800 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
802 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
803 progress++,image->rows);
804 if (proceed == MagickFalse)
808 image_view=DestroyCacheView(image_view);
809 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % C o n t r a s t I m a g e %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % ContrastImage() enhances the intensity differences between the lighter and
825 % darker elements of the image. Set sharpen to a MagickTrue to increase the
826 % image contrast otherwise the contrast is reduced.
828 % The format of the ContrastImage method is:
830 % MagickBooleanType ContrastImage(Image *image,
831 % const MagickBooleanType sharpen,ExceptionInfo *exception)
833 % A description of each parameter follows:
835 % o image: the image.
837 % o sharpen: Increase or decrease image contrast.
839 % o exception: return any errors or warnings in this structure.
843 static void Contrast(const int sign,double *red,double *green,double *blue)
851 Enhance contrast: dark color become darker, light color become lighter.
853 assert(red != (double *) NULL);
854 assert(green != (double *) NULL);
855 assert(blue != (double *) NULL);
859 ConvertsRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
860 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
862 if (brightness > 1.0)
865 if (brightness < 0.0)
867 ConvertHSBTosRGB(hue,saturation,brightness,red,green,blue);
870 MagickExport MagickBooleanType ContrastImage(Image *image,
871 const MagickBooleanType sharpen,ExceptionInfo *exception)
873 #define ContrastImageTag "Contrast/Image"
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 sign=sharpen != MagickFalse ? 1 : -1;
898 if (image->storage_class == PseudoClass)
901 Contrast enhance colormap.
903 for (i=0; i < (ssize_t) image->colors; i++)
904 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
905 &image->colormap[i].blue);
908 Contrast enhance image.
912 image_view=AcquireAuthenticCacheView(image,exception);
913 #if defined(MAGICKCORE_OPENMP_SUPPORT)
914 #pragma omp parallel for schedule(static,4) shared(progress,status) \
915 dynamic_number_threads(image,image->columns,image->rows,1)
917 for (y=0; y < (ssize_t) image->rows; y++)
930 if (status == MagickFalse)
932 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
933 if (q == (Quantum *) NULL)
938 for (x=0; x < (ssize_t) image->columns; x++)
940 red=(double) GetPixelRed(image,q);
941 green=(double) GetPixelGreen(image,q);
942 blue=(double) GetPixelBlue(image,q);
943 Contrast(sign,&red,&green,&blue);
944 SetPixelRed(image,ClampToQuantum(red),q);
945 SetPixelGreen(image,ClampToQuantum(green),q);
946 SetPixelBlue(image,ClampToQuantum(blue),q);
947 q+=GetPixelChannels(image);
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
956 #if defined(MAGICKCORE_OPENMP_SUPPORT)
957 #pragma omp critical (MagickCore_ContrastImage)
959 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
960 if (proceed == MagickFalse)
964 image_view=DestroyCacheView(image_view);
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % C o n t r a s t S t r e t c h I m a g e %
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % ContrastStretchImage() is a simple image enhancement technique that attempts
980 % to improve the contrast in an image by 'stretching' the range of intensity
981 % values it contains to span a desired range of values. It differs from the
982 % more sophisticated histogram equalization in that it can only apply a
983 % linear scaling function to the image pixel values. As a result the
984 % 'enhancement' is less harsh.
986 % The format of the ContrastStretchImage method is:
988 % MagickBooleanType ContrastStretchImage(Image *image,
989 % const char *levels,ExceptionInfo *exception)
991 % A description of each parameter follows:
993 % o image: the image.
995 % o black_point: the black point.
997 % o white_point: the white point.
999 % o levels: Specify the levels where the black and white points have the
1000 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
1002 % o exception: return any errors or warnings in this structure.
1005 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1006 const double black_point,const double white_point,ExceptionInfo *exception)
1008 #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1009 #define ContrastStretchImageTag "ContrastStretch/Image"
1036 Allocate histogram and stretch map.
1038 assert(image != (Image *) NULL);
1039 assert(image->signature == MagickSignature);
1040 if (image->debug != MagickFalse)
1041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1042 black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
1043 white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
1044 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
1045 sizeof(*histogram));
1046 stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
1047 GetPixelChannels(image)*sizeof(*stretch_map));
1048 if ((black == (double *) NULL) || (white == (double *) NULL) ||
1049 (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
1051 if (stretch_map != (double *) NULL)
1052 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1053 if (histogram != (double *) NULL)
1054 histogram=(double *) RelinquishMagickMemory(histogram);
1055 if (white != (double *) NULL)
1056 white=(double *) RelinquishMagickMemory(white);
1057 if (black != (double *) NULL)
1058 black=(double *) RelinquishMagickMemory(black);
1059 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1066 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1067 sizeof(*histogram));
1068 image_view=AcquireVirtualCacheView(image,exception);
1069 for (y=0; y < (ssize_t) image->rows; y++)
1071 register const Quantum
1077 if (status == MagickFalse)
1079 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1080 if (p == (const Quantum *) NULL)
1085 for (x=0; x < (ssize_t) image->columns; x++)
1090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1091 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1092 p+=GetPixelChannels(image);
1095 image_view=DestroyCacheView(image_view);
1097 Find the histogram boundaries by locating the black/white levels.
1099 number_channels=GetPixelChannels(image);
1100 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1101 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1102 dynamic_number_threads(image,image->columns,1,1)
1104 for (i=0; i < (ssize_t) number_channels; i++)
1113 white[i]=MaxRange(QuantumRange);
1115 for (j=0; j <= (ssize_t) MaxMap; j++)
1117 intensity+=histogram[GetPixelChannels(image)*j+i];
1118 if (intensity > black_point)
1121 black[i]=(MagickRealType) j;
1123 for (j=(ssize_t) MaxMap; j != 0; j--)
1125 intensity+=histogram[GetPixelChannels(image)*j+i];
1126 if (intensity > ((double) image->columns*image->rows-white_point))
1129 white[i]=(MagickRealType) j;
1131 histogram=(double *) RelinquishMagickMemory(histogram);
1133 Stretch the histogram to create the stretched image mapping.
1135 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1136 sizeof(*stretch_map));
1137 number_channels=GetPixelChannels(image);
1138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1139 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1140 dynamic_number_threads(image,image->columns,1,1)
1142 for (i=0; i < (ssize_t) number_channels; i++)
1147 for (j=0; j <= (ssize_t) MaxMap; j++)
1149 if (j < (ssize_t) black[i])
1150 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1152 if (j > (ssize_t) white[i])
1153 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1156 if (black[i] != white[i])
1157 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1158 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1159 (white[i]-black[i])));
1162 if (image->storage_class == PseudoClass)
1168 Stretch-contrast colormap.
1170 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1171 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1172 dynamic_number_threads(image,image->columns,1,1)
1174 for (j=0; j < (ssize_t) image->colors; j++)
1176 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1178 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1179 if (black[i] != white[i])
1180 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1181 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
1183 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1185 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1186 if (black[i] != white[i])
1187 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1188 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
1190 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1192 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1193 if (black[i] != white[i])
1194 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1195 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
1197 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1199 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1200 if (black[i] != white[i])
1201 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1202 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
1207 Stretch-contrast image.
1211 image_view=AcquireAuthenticCacheView(image,exception);
1212 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1213 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1214 dynamic_number_threads(image,image->columns,image->rows,1)
1216 for (y=0; y < (ssize_t) image->rows; y++)
1224 if (status == MagickFalse)
1226 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1227 if (q == (Quantum *) NULL)
1232 for (x=0; x < (ssize_t) image->columns; x++)
1237 if (GetPixelMask(image,q) != 0)
1239 q+=GetPixelChannels(image);
1242 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1250 channel=GetPixelChannelMapChannel(image,i);
1251 traits=GetPixelChannelMapTraits(image,channel);
1252 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1254 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1255 ScaleQuantumToMap(q[i])+i]);
1257 q+=GetPixelChannels(image);
1259 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1261 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1266 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1267 #pragma omp critical (MagickCore_ContrastStretchImage)
1269 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1271 if (proceed == MagickFalse)
1275 image_view=DestroyCacheView(image_view);
1276 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1277 white=(double *) RelinquishMagickMemory(white);
1278 black=(double *) RelinquishMagickMemory(black);
1283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1287 % E n h a n c e I m a g e %
1291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1293 % EnhanceImage() applies a digital filter that improves the quality of a
1296 % The format of the EnhanceImage method is:
1298 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1300 % A description of each parameter follows:
1302 % o image: the image.
1304 % o exception: return any errors or warnings in this structure.
1307 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1309 #define EnhancePixel(weight) \
1310 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1311 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1312 enhance_image,channel,q); \
1313 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1314 mean)*distance*distance; \
1315 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1316 QuantumRange/25.0f)) \
1318 aggregate+=(weight)*r[i]; \
1319 total_weight+=(weight); \
1321 r+=GetPixelChannels(image);
1322 #define EnhanceImageTag "Enhance/Image"
1341 Initialize enhanced image attributes.
1343 assert(image != (const Image *) NULL);
1344 assert(image->signature == MagickSignature);
1345 if (image->debug != MagickFalse)
1346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1347 assert(exception != (ExceptionInfo *) NULL);
1348 assert(exception->signature == MagickSignature);
1349 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1351 if (enhance_image == (Image *) NULL)
1352 return((Image *) NULL);
1353 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1355 enhance_image=DestroyImage(enhance_image);
1356 return((Image *) NULL);
1363 image_view=AcquireVirtualCacheView(image,exception);
1364 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1365 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1366 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1367 dynamic_number_threads(image,image->columns,image->rows,1)
1369 for (y=0; y < (ssize_t) image->rows; y++)
1371 register const Quantum
1383 if (status == MagickFalse)
1385 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1386 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1388 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1393 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1394 for (x=0; x < (ssize_t) image->columns; x++)
1399 if (GetPixelMask(image,p) != 0)
1401 p+=GetPixelChannels(image);
1402 q+=GetPixelChannels(enhance_image);
1405 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1421 register const Quantum
1424 channel=GetPixelChannelMapChannel(image,i);
1425 traits=GetPixelChannelMapTraits(image,channel);
1426 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1427 if ((traits == UndefinedPixelTrait) ||
1428 (enhance_traits == UndefinedPixelTrait))
1430 SetPixelChannel(enhance_image,channel,p[center+i],q);
1431 if ((enhance_traits & CopyPixelTrait) != 0)
1434 Compute weighted average of target pixel color components.
1439 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1440 EnhancePixel(8.0); EnhancePixel(5.0);
1441 r=p+1*GetPixelChannels(image)*(image->columns+4);
1442 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1443 EnhancePixel(20.0); EnhancePixel(8.0);
1444 r=p+2*GetPixelChannels(image)*(image->columns+4);
1445 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1446 EnhancePixel(40.0); EnhancePixel(10.0);
1447 r=p+3*GetPixelChannels(image)*(image->columns+4);
1448 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1449 EnhancePixel(20.0); EnhancePixel(8.0);
1450 r=p+4*GetPixelChannels(image)*(image->columns+4);
1451 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1452 EnhancePixel(8.0); EnhancePixel(5.0);
1453 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1456 p+=GetPixelChannels(image);
1457 q+=GetPixelChannels(enhance_image);
1459 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1461 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1466 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1467 #pragma omp critical (MagickCore_EnhanceImage)
1469 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1470 if (proceed == MagickFalse)
1474 enhance_view=DestroyCacheView(enhance_view);
1475 image_view=DestroyCacheView(image_view);
1476 return(enhance_image);
1480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % E q u a l i z e I m a g e %
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490 % EqualizeImage() applies a histogram equalization to the image.
1492 % The format of the EqualizeImage method is:
1494 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1496 % A description of each parameter follows:
1498 % o image: the image.
1500 % o exception: return any errors or warnings in this structure.
1503 MagickExport MagickBooleanType EqualizeImage(Image *image,
1504 ExceptionInfo *exception)
1506 #define EqualizeImageTag "Equalize/Image"
1518 black[CompositePixelChannel],
1522 white[CompositePixelChannel];
1534 Allocate and initialize histogram arrays.
1536 assert(image != (Image *) NULL);
1537 assert(image->signature == MagickSignature);
1538 if (image->debug != MagickFalse)
1539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1540 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1541 GetPixelChannels(image)*sizeof(*equalize_map));
1542 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1543 GetPixelChannels(image)*sizeof(*histogram));
1544 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1545 GetPixelChannels(image)*sizeof(*map));
1546 if ((equalize_map == (MagickRealType *) NULL) ||
1547 (histogram == (MagickRealType *) NULL) ||
1548 (map == (MagickRealType *) NULL))
1550 if (map != (MagickRealType *) NULL)
1551 map=(MagickRealType *) RelinquishMagickMemory(map);
1552 if (histogram != (MagickRealType *) NULL)
1553 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1554 if (equalize_map != (MagickRealType *) NULL)
1555 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1556 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1563 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1564 sizeof(*histogram));
1565 image_view=AcquireVirtualCacheView(image,exception);
1566 for (y=0; y < (ssize_t) image->rows; y++)
1568 register const Quantum
1574 if (status == MagickFalse)
1576 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1577 if (p == (const Quantum *) NULL)
1582 for (x=0; x < (ssize_t) image->columns; x++)
1587 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1588 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1589 p+=GetPixelChannels(image);
1592 image_view=DestroyCacheView(image_view);
1594 Integrate the histogram to get the equalization map.
1596 number_channels=GetPixelChannels(image);
1597 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1598 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1599 dynamic_number_threads(image,image->columns,1,1)
1601 for (i=0; i < (ssize_t) number_channels; i++)
1610 for (j=0; j <= (ssize_t) MaxMap; j++)
1612 intensity+=histogram[GetPixelChannels(image)*j+i];
1613 map[GetPixelChannels(image)*j+i]=intensity;
1616 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1617 sizeof(*equalize_map));
1618 number_channels=GetPixelChannels(image);
1619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1620 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1621 dynamic_number_threads(image,image->columns,1,1)
1623 for (i=0; i < (ssize_t) number_channels; i++)
1629 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1630 if (black[i] != white[i])
1631 for (j=0; j <= (ssize_t) MaxMap; j++)
1632 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1633 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1634 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1636 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1637 map=(MagickRealType *) RelinquishMagickMemory(map);
1638 if (image->storage_class == PseudoClass)
1649 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1650 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1651 dynamic_number_threads(image,image->columns,1,1)
1653 for (j=0; j < (ssize_t) image->colors; j++)
1655 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1657 channel=GetPixelChannelMapChannel(image,RedPixelChannel);
1658 if (black[channel] != white[channel])
1659 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
1660 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
1663 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1665 channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
1666 if (black[channel] != white[channel])
1667 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
1668 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
1671 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1673 channel=GetPixelChannelMapChannel(image,BluePixelChannel);
1674 if (black[channel] != white[channel])
1675 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
1676 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
1679 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1681 channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1682 if (black[channel] != white[channel])
1683 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
1684 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
1693 image_view=AcquireAuthenticCacheView(image,exception);
1694 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1695 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1696 dynamic_number_threads(image,image->columns,image->rows,1)
1698 for (y=0; y < (ssize_t) image->rows; y++)
1706 if (status == MagickFalse)
1708 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1709 if (q == (Quantum *) NULL)
1714 for (x=0; x < (ssize_t) image->columns; x++)
1719 if (GetPixelMask(image,q) != 0)
1721 q+=GetPixelChannels(image);
1724 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1732 channel=GetPixelChannelMapChannel(image,i);
1733 traits=GetPixelChannelMapTraits(image,channel);
1734 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1736 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1737 ScaleQuantumToMap(q[i])+i]);
1739 q+=GetPixelChannels(image);
1741 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1743 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1748 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1749 #pragma omp critical (MagickCore_EqualizeImage)
1751 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1752 if (proceed == MagickFalse)
1756 image_view=DestroyCacheView(image_view);
1757 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766 % G a m m a I m a g e %
1770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1772 % GammaImage() gamma-corrects a particular image channel. The same
1773 % image viewed on different devices will have perceptual differences in the
1774 % way the image's intensities are represented on the screen. Specify
1775 % individual gamma levels for the red, green, and blue channels, or adjust
1776 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1778 % You can also reduce the influence of a particular channel with a gamma
1781 % The format of the GammaImage method is:
1783 % MagickBooleanType GammaImage(Image *image,const double gamma,
1784 % ExceptionInfo *exception)
1786 % A description of each parameter follows:
1788 % o image: the image.
1790 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1792 % o gamma: the image gamma.
1795 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1796 ExceptionInfo *exception)
1798 #define GammaCorrectImageTag "GammaCorrect/Image"
1819 Allocate and initialize gamma maps.
1821 assert(image != (Image *) NULL);
1822 assert(image->signature == MagickSignature);
1823 if (image->debug != MagickFalse)
1824 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1827 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1828 if (gamma_map == (Quantum *) NULL)
1829 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1831 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1833 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1834 #pragma omp parallel for \
1835 dynamic_number_threads(image,image->columns,1,1)
1837 for (i=0; i <= (ssize_t) MaxMap; i++)
1838 gamma_map[i]=ScaleMapToQuantum((MagickRealType) (MaxMap*pow((double) i/
1839 MaxMap,1.0/gamma)));
1840 if (image->storage_class == PseudoClass)
1843 Gamma-correct colormap.
1845 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1846 #pragma omp parallel for schedule(static) shared(progress,status) \
1847 dynamic_number_threads(image,image->columns,1,1)
1849 for (i=0; i < (ssize_t) image->colors; i++)
1851 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1852 image->colormap[i].red=(MagickRealType) gamma_map[
1853 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
1854 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1855 image->colormap[i].green=(MagickRealType) gamma_map[
1856 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
1857 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1858 image->colormap[i].blue=(MagickRealType) gamma_map[
1859 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
1860 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1861 image->colormap[i].alpha=(MagickRealType) gamma_map[
1862 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
1866 Gamma-correct image.
1870 image_view=AcquireAuthenticCacheView(image,exception);
1871 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1872 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1873 dynamic_number_threads(image,image->columns,image->rows,1)
1875 for (y=0; y < (ssize_t) image->rows; y++)
1883 if (status == MagickFalse)
1885 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1886 if (q == (Quantum *) NULL)
1891 for (x=0; x < (ssize_t) image->columns; x++)
1896 if (GetPixelMask(image,q) != 0)
1898 q+=GetPixelChannels(image);
1901 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1909 channel=GetPixelChannelMapChannel(image,i);
1910 traits=GetPixelChannelMapTraits(image,channel);
1911 if ((traits & UpdatePixelTrait) == 0)
1913 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
1915 q+=GetPixelChannels(image);
1917 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1919 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1924 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1925 #pragma omp critical (MagickCore_GammaImage)
1927 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1929 if (proceed == MagickFalse)
1933 image_view=DestroyCacheView(image_view);
1934 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
1935 if (image->gamma != 0.0)
1936 image->gamma*=gamma;
1941 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1945 % H a l d C l u t I m a g e %
1949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1952 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1953 % Create it with the HALD coder. You can apply any color transformation to
1954 % the Hald image and then use this method to apply the transform to the
1957 % The format of the HaldClutImage method is:
1959 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1960 % ExceptionInfo *exception)
1962 % A description of each parameter follows:
1964 % o image: the image, which is replaced by indexed CLUT values
1966 % o hald_image: the color lookup table image for replacement color values.
1968 % o exception: return any errors or warnings in this structure.
1972 static inline size_t MagickMin(const size_t x,const size_t y)
1979 MagickExport MagickBooleanType HaldClutImage(Image *image,
1980 const Image *hald_image,ExceptionInfo *exception)
1982 #define HaldClutImageTag "Clut/Image"
1984 typedef struct _HaldInfo
2016 assert(image != (Image *) NULL);
2017 assert(image->signature == MagickSignature);
2018 if (image->debug != MagickFalse)
2019 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2020 assert(hald_image != (Image *) NULL);
2021 assert(hald_image->signature == MagickSignature);
2022 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2023 return(MagickFalse);
2024 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2025 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2026 if (image->matte == MagickFalse)
2027 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2033 length=MagickMin(hald_image->columns,hald_image->rows);
2034 for (level=2; (level*level*level) < length; level++) ;
2036 cube_size=level*level;
2037 width=(double) hald_image->columns;
2038 GetPixelInfo(hald_image,&zero);
2039 hald_view=AcquireVirtualCacheView(hald_image,exception);
2040 image_view=AcquireAuthenticCacheView(image,exception);
2041 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2042 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2043 dynamic_number_threads(image,image->columns,image->rows,1)
2045 for (y=0; y < (ssize_t) image->rows; y++)
2053 if (status == MagickFalse)
2055 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2056 if (q == (Quantum *) NULL)
2061 for (x=0; x < (ssize_t) image->columns; x++)
2076 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2077 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2078 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2079 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2080 point.x-=floor(point.x);
2081 point.y-=floor(point.y);
2082 point.z-=floor(point.z);
2084 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2085 fmod(offset,width),floor(offset/width),&pixel1,exception);
2087 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2088 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2090 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2093 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2094 fmod(offset,width),floor(offset/width),&pixel1,exception);
2095 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2096 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2098 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2101 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2103 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2104 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2105 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2106 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2107 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2108 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2109 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2110 (image->colorspace == CMYKColorspace))
2111 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2112 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2113 (image->matte != MagickFalse))
2114 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2115 q+=GetPixelChannels(image);
2117 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2119 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2124 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2125 #pragma omp critical (MagickCore_HaldClutImage)
2127 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2128 if (proceed == MagickFalse)
2132 hald_view=DestroyCacheView(hald_view);
2133 image_view=DestroyCacheView(image_view);
2138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2142 % L e v e l I m a g e %
2146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2148 % LevelImage() adjusts the levels of a particular image channel by
2149 % scaling the colors falling between specified white and black points to
2150 % the full available quantum range.
2152 % The parameters provided represent the black, and white points. The black
2153 % point specifies the darkest color in the image. Colors darker than the
2154 % black point are set to zero. White point specifies the lightest color in
2155 % the image. Colors brighter than the white point are set to the maximum
2158 % If a '!' flag is given, map black and white colors to the given levels
2159 % rather than mapping those levels to black and white. See
2160 % LevelizeImage() below.
2162 % Gamma specifies a gamma correction to apply to the image.
2164 % The format of the LevelImage method is:
2166 % MagickBooleanType LevelImage(Image *image,const double black_point,
2167 % const double white_point,const double gamma,ExceptionInfo *exception)
2169 % A description of each parameter follows:
2171 % o image: the image.
2173 % o black_point: The level to map zero (black) to.
2175 % o white_point: The level to map QuantumRange (white) to.
2177 % o exception: return any errors or warnings in this structure.
2181 static inline MagickRealType LevelPixel(const double black_point,
2182 const double white_point,const double gamma,const MagickRealType pixel)
2188 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2189 level_pixel=(MagickRealType) QuantumRange*pow(scale*((double) pixel-
2190 black_point),1.0/gamma);
2191 return(level_pixel);
2194 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2195 const double white_point,const double gamma,ExceptionInfo *exception)
2197 #define LevelImageTag "Level/Image"
2215 Allocate and initialize levels map.
2217 assert(image != (Image *) NULL);
2218 assert(image->signature == MagickSignature);
2219 if (image->debug != MagickFalse)
2220 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2221 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2222 (void) SetImageColorspace(image,sRGBColorspace,exception);
2223 if (image->storage_class == PseudoClass)
2224 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2225 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2226 dynamic_number_threads(image,image->columns,1,1)
2228 for (i=0; i < (ssize_t) image->colors; i++)
2233 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2234 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2235 white_point,gamma,image->colormap[i].red));
2236 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2237 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2238 white_point,gamma,image->colormap[i].green));
2239 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2240 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2241 white_point,gamma,image->colormap[i].blue));
2242 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2243 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2244 white_point,gamma,image->colormap[i].alpha));
2251 image_view=AcquireAuthenticCacheView(image,exception);
2252 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2253 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2254 dynamic_number_threads(image,image->columns,image->rows,1)
2256 for (y=0; y < (ssize_t) image->rows; y++)
2264 if (status == MagickFalse)
2266 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2267 if (q == (Quantum *) NULL)
2272 for (x=0; x < (ssize_t) image->columns; x++)
2277 if (GetPixelMask(image,q) != 0)
2279 q+=GetPixelChannels(image);
2282 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2290 channel=GetPixelChannelMapChannel(image,i);
2291 traits=GetPixelChannelMapTraits(image,channel);
2292 if ((traits & UpdatePixelTrait) == 0)
2294 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2295 (MagickRealType) q[i]));
2297 q+=GetPixelChannels(image);
2299 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2301 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2307 #pragma omp critical (MagickCore_LevelImage)
2309 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2310 if (proceed == MagickFalse)
2314 image_view=DestroyCacheView(image_view);
2319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2323 % L e v e l i z e I m a g e %
2327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2329 % LevelizeImage() applies the reversed LevelImage() operation to just
2330 % the specific channels specified. It compresses the full range of color
2331 % values, so that they lie between the given black and white points. Gamma is
2332 % applied before the values are mapped.
2334 % LevelizeImage() can be called with by using a +level command line
2335 % API option, or using a '!' on a -level or LevelImage() geometry string.
2337 % It can be used to de-contrast a greyscale image to the exact levels
2338 % specified. Or by using specific levels for each channel of an image you
2339 % can convert a gray-scale image to any linear color gradient, according to
2342 % The format of the LevelizeImage method is:
2344 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2345 % const double white_point,const double gamma,ExceptionInfo *exception)
2347 % A description of each parameter follows:
2349 % o image: the image.
2351 % o black_point: The level to map zero (black) to.
2353 % o white_point: The level to map QuantumRange (white) to.
2355 % o gamma: adjust gamma by this factor before mapping values.
2357 % o exception: return any errors or warnings in this structure.
2360 MagickExport MagickBooleanType LevelizeImage(Image *image,
2361 const double black_point,const double white_point,const double gamma,
2362 ExceptionInfo *exception)
2364 #define LevelizeImageTag "Levelize/Image"
2365 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2366 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2385 Allocate and initialize levels map.
2387 assert(image != (Image *) NULL);
2388 assert(image->signature == MagickSignature);
2389 if (image->debug != MagickFalse)
2390 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2391 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2392 (void) SetImageColorspace(image,sRGBColorspace,exception);
2393 if (image->storage_class == PseudoClass)
2394 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2395 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2396 dynamic_number_threads(image,image->columns,1,1)
2398 for (i=0; i < (ssize_t) image->colors; i++)
2403 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2404 image->colormap[i].red=(double) LevelizeValue(
2405 image->colormap[i].red);
2406 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2407 image->colormap[i].green=(double) LevelizeValue(
2408 image->colormap[i].green);
2409 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2410 image->colormap[i].blue=(double) LevelizeValue(
2411 image->colormap[i].blue);
2412 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2413 image->colormap[i].alpha=(double) LevelizeValue(
2414 image->colormap[i].alpha);
2421 image_view=AcquireAuthenticCacheView(image,exception);
2422 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2423 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2424 dynamic_number_threads(image,image->columns,image->rows,1)
2426 for (y=0; y < (ssize_t) image->rows; y++)
2434 if (status == MagickFalse)
2436 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2437 if (q == (Quantum *) NULL)
2442 for (x=0; x < (ssize_t) image->columns; x++)
2447 if (GetPixelMask(image,q) != 0)
2449 q+=GetPixelChannels(image);
2452 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2460 channel=GetPixelChannelMapChannel(image,i);
2461 traits=GetPixelChannelMapTraits(image,channel);
2462 if ((traits & UpdatePixelTrait) == 0)
2464 q[i]=LevelizeValue(q[i]);
2466 q+=GetPixelChannels(image);
2468 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2470 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2475 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2476 #pragma omp critical (MagickCore_LevelizeImage)
2478 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2479 if (proceed == MagickFalse)
2483 image_view=DestroyCacheView(image_view);
2488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2492 % L e v e l I m a g e C o l o r s %
2496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2498 % LevelImageColors() maps the given color to "black" and "white" values,
2499 % linearly spreading out the colors, and level values on a channel by channel
2500 % bases, as per LevelImage(). The given colors allows you to specify
2501 % different level ranges for each of the color channels separately.
2503 % If the boolean 'invert' is set true the image values will modifyed in the
2504 % reverse direction. That is any existing "black" and "white" colors in the
2505 % image will become the color values given, with all other values compressed
2506 % appropriatally. This effectivally maps a greyscale gradient into the given
2509 % The format of the LevelImageColors method is:
2511 % MagickBooleanType LevelImageColors(Image *image,
2512 % const PixelInfo *black_color,const PixelInfo *white_color,
2513 % const MagickBooleanType invert,ExceptionInfo *exception)
2515 % A description of each parameter follows:
2517 % o image: the image.
2519 % o black_color: The color to map black to/from
2521 % o white_point: The color to map white to/from
2523 % o invert: if true map the colors (levelize), rather than from (level)
2525 % o exception: return any errors or warnings in this structure.
2528 MagickExport MagickBooleanType LevelImageColors(Image *image,
2529 const PixelInfo *black_color,const PixelInfo *white_color,
2530 const MagickBooleanType invert,ExceptionInfo *exception)
2539 Allocate and initialize levels map.
2541 assert(image != (Image *) NULL);
2542 assert(image->signature == MagickSignature);
2543 if (image->debug != MagickFalse)
2544 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2546 if (invert == MagickFalse)
2548 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2550 channel_mask=SetPixelChannelMask(image,RedChannel);
2551 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2553 (void) SetPixelChannelMask(image,channel_mask);
2555 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2557 channel_mask=SetPixelChannelMask(image,GreenChannel);
2558 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2560 (void) SetPixelChannelMask(image,channel_mask);
2562 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2564 channel_mask=SetPixelChannelMask(image,BlueChannel);
2565 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2567 (void) SetPixelChannelMask(image,channel_mask);
2569 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2570 (image->colorspace == CMYKColorspace))
2572 channel_mask=SetPixelChannelMask(image,BlackChannel);
2573 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2575 (void) SetPixelChannelMask(image,channel_mask);
2577 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2578 (image->matte == MagickTrue))
2580 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2581 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2583 (void) SetPixelChannelMask(image,channel_mask);
2588 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2590 channel_mask=SetPixelChannelMask(image,RedChannel);
2591 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2593 (void) SetPixelChannelMask(image,channel_mask);
2595 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2597 channel_mask=SetPixelChannelMask(image,GreenChannel);
2598 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2600 (void) SetPixelChannelMask(image,channel_mask);
2602 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2604 channel_mask=SetPixelChannelMask(image,BlueChannel);
2605 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2607 (void) SetPixelChannelMask(image,channel_mask);
2609 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2610 (image->colorspace == CMYKColorspace))
2612 channel_mask=SetPixelChannelMask(image,BlackChannel);
2613 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2615 (void) SetPixelChannelMask(image,channel_mask);
2617 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2618 (image->matte == MagickTrue))
2620 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2621 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2623 (void) SetPixelChannelMask(image,channel_mask);
2626 return(status == 0 ? MagickFalse : MagickTrue);
2630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2634 % L i n e a r S t r e t c h I m a g e %
2638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2640 % LinearStretchImage() discards any pixels below the black point and above
2641 % the white point and levels the remaining pixels.
2643 % The format of the LinearStretchImage method is:
2645 % MagickBooleanType LinearStretchImage(Image *image,
2646 % const double black_point,const double white_point,
2647 % ExceptionInfo *exception)
2649 % A description of each parameter follows:
2651 % o image: the image.
2653 % o black_point: the black point.
2655 % o white_point: the white point.
2657 % o exception: return any errors or warnings in this structure.
2660 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2661 const double black_point,const double white_point,ExceptionInfo *exception)
2663 #define LinearStretchImageTag "LinearStretch/Image"
2681 Allocate histogram and linear map.
2683 assert(image != (Image *) NULL);
2684 assert(image->signature == MagickSignature);
2685 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2686 sizeof(*histogram));
2687 if (histogram == (MagickRealType *) NULL)
2688 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2693 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2694 image_view=AcquireVirtualCacheView(image,exception);
2695 for (y=0; y < (ssize_t) image->rows; y++)
2697 register const Quantum
2703 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2704 if (p == (const Quantum *) NULL)
2706 for (x=0; x < (ssize_t) image->columns; x++)
2708 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2709 p+=GetPixelChannels(image);
2712 image_view=DestroyCacheView(image_view);
2714 Find the histogram boundaries by locating the black and white point levels.
2717 for (black=0; black < (ssize_t) MaxMap; black++)
2719 intensity+=histogram[black];
2720 if (intensity >= black_point)
2724 for (white=(ssize_t) MaxMap; white != 0; white--)
2726 intensity+=histogram[white];
2727 if (intensity >= white_point)
2730 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2731 status=LevelImage(image,(double) black,(double) white,1.0,exception);
2736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2740 % M o d u l a t e I m a g e %
2744 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2746 % ModulateImage() lets you control the brightness, saturation, and hue
2747 % of an image. Modulate represents the brightness, saturation, and hue
2748 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2749 % modulation is lightness, saturation, and hue. And if the colorspace is
2750 % HWB, use blackness, whiteness, and hue.
2752 % The format of the ModulateImage method is:
2754 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2755 % ExceptionInfo *exception)
2757 % A description of each parameter follows:
2759 % o image: the image.
2761 % o modulate: Define the percent change in brightness, saturation, and hue.
2763 % o exception: return any errors or warnings in this structure.
2767 static void ModulateHSB(const double percent_hue,
2768 const double percent_saturation,const double percent_brightness,double *red,
2769 double *green,double *blue)
2777 Increase or decrease color brightness, saturation, or hue.
2779 assert(red != (double *) NULL);
2780 assert(green != (double *) NULL);
2781 assert(blue != (double *) NULL);
2782 ConvertsRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2783 hue+=0.5*(0.01*percent_hue-1.0);
2788 saturation*=0.01*percent_saturation;
2789 brightness*=0.01*percent_brightness;
2790 ConvertHSBTosRGB(hue,saturation,brightness,red,green,blue);
2793 static void ModulateHSL(const double percent_hue,
2794 const double percent_saturation,const double percent_lightness,double *red,
2795 double *green,double *blue)
2803 Increase or decrease color lightness, saturation, or hue.
2805 assert(red != (double *) NULL);
2806 assert(green != (double *) NULL);
2807 assert(blue != (double *) NULL);
2808 ConvertsRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2809 hue+=0.5*(0.01*percent_hue-1.0);
2814 saturation*=0.01*percent_saturation;
2815 lightness*=0.01*percent_lightness;
2816 ConvertHSLTosRGB(hue,saturation,lightness,red,green,blue);
2819 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
2827 Increase or decrease color blackness, whiteness, or hue.
2829 assert(red != (double *) NULL);
2830 assert(green != (double *) NULL);
2831 assert(blue != (double *) NULL);
2832 ConvertsRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2833 hue+=0.5*(0.01*percent_hue-1.0);
2838 blackness*=0.01*percent_blackness;
2839 whiteness*=0.01*percent_whiteness;
2840 ConvertHWBTosRGB(hue,whiteness,blackness,red,green,blue);
2843 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2844 ExceptionInfo *exception)
2846 #define ModulateImageTag "Modulate/Image"
2881 Initialize modulate table.
2883 assert(image != (Image *) NULL);
2884 assert(image->signature == MagickSignature);
2885 if (image->debug != MagickFalse)
2886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2887 if (modulate == (char *) NULL)
2888 return(MagickFalse);
2889 flags=ParseGeometry(modulate,&geometry_info);
2890 percent_brightness=geometry_info.rho;
2891 percent_saturation=geometry_info.sigma;
2892 if ((flags & SigmaValue) == 0)
2893 percent_saturation=100.0;
2894 percent_hue=geometry_info.xi;
2895 if ((flags & XiValue) == 0)
2897 colorspace=UndefinedColorspace;
2898 artifact=GetImageArtifact(image,"modulate:colorspace");
2899 if (artifact != (const char *) NULL)
2900 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2901 MagickFalse,artifact);
2902 if (image->storage_class == PseudoClass)
2907 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2908 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2909 dynamic_number_threads(image,image->columns,1,1)
2911 for (i=0; i < (ssize_t) image->colors; i++)
2916 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2917 &image->colormap[i].red,&image->colormap[i].green,
2918 &image->colormap[i].blue);
2924 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2925 &image->colormap[i].red,&image->colormap[i].green,
2926 &image->colormap[i].blue);
2931 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2932 &image->colormap[i].red,&image->colormap[i].green,
2933 &image->colormap[i].blue);
2943 image_view=AcquireAuthenticCacheView(image,exception);
2944 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2945 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2946 dynamic_number_threads(image,image->columns,image->rows,1)
2948 for (y=0; y < (ssize_t) image->rows; y++)
2961 if (status == MagickFalse)
2963 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2964 if (q == (Quantum *) NULL)
2969 for (x=0; x < (ssize_t) image->columns; x++)
2971 red=(double) GetPixelRed(image,q);
2972 green=(double) GetPixelGreen(image,q);
2973 blue=(double) GetPixelBlue(image,q);
2978 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2985 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2991 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2996 SetPixelRed(image,ClampToQuantum(red),q);
2997 SetPixelGreen(image,ClampToQuantum(green),q);
2998 SetPixelBlue(image,ClampToQuantum(blue),q);
2999 q+=GetPixelChannels(image);
3001 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3003 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3008 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3009 #pragma omp critical (MagickCore_ModulateImage)
3011 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3012 if (proceed == MagickFalse)
3016 image_view=DestroyCacheView(image_view);
3021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3025 % N e g a t e I m a g e %
3029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3031 % NegateImage() negates the colors in the reference image. The grayscale
3032 % option means that only grayscale values within the image are negated.
3034 % The format of the NegateImage method is:
3036 % MagickBooleanType NegateImage(Image *image,
3037 % const MagickBooleanType grayscale,ExceptionInfo *exception)
3039 % A description of each parameter follows:
3041 % o image: the image.
3043 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3045 % o exception: return any errors or warnings in this structure.
3048 MagickExport MagickBooleanType NegateImage(Image *image,
3049 const MagickBooleanType grayscale,ExceptionInfo *exception)
3051 #define NegateImageTag "Negate/Image"
3068 assert(image != (Image *) NULL);
3069 assert(image->signature == MagickSignature);
3070 if (image->debug != MagickFalse)
3071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3072 if (image->storage_class == PseudoClass)
3077 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3078 #pragma omp parallel for schedule(static) shared(progress,status) \
3079 dynamic_number_threads(image,image->columns,1,1)
3081 for (i=0; i < (ssize_t) image->colors; i++)
3083 if (grayscale != MagickFalse)
3084 if ((image->colormap[i].red != image->colormap[i].green) ||
3085 (image->colormap[i].green != image->colormap[i].blue))
3087 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3088 image->colormap[i].red=(Quantum) QuantumRange-
3089 image->colormap[i].red;
3090 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3091 image->colormap[i].green=(Quantum) QuantumRange-
3092 image->colormap[i].green;
3093 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3094 image->colormap[i].blue=(Quantum) QuantumRange-
3095 image->colormap[i].blue;
3103 image_view=AcquireAuthenticCacheView(image,exception);
3104 if (grayscale != MagickFalse)
3106 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3107 #pragma omp parallel for schedule(static) shared(progress,status) \
3108 dynamic_number_threads(image,image->columns,image->rows,1)
3110 for (y=0; y < (ssize_t) image->rows; y++)
3121 if (status == MagickFalse)
3123 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3125 if (q == (Quantum *) NULL)
3130 for (x=0; x < (ssize_t) image->columns; x++)
3135 if ((GetPixelMask(image,q) != 0) ||
3136 (IsPixelGray(image,q) != MagickFalse))
3138 q+=GetPixelChannels(image);
3141 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3149 channel=GetPixelChannelMapChannel(image,i);
3150 traits=GetPixelChannelMapTraits(image,channel);
3151 if ((traits & UpdatePixelTrait) == 0)
3153 q[i]=QuantumRange-q[i];
3155 q+=GetPixelChannels(image);
3157 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3158 if (sync == MagickFalse)
3160 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3166 #pragma omp critical (MagickCore_NegateImage)
3168 proceed=SetImageProgress(image,NegateImageTag,progress++,
3170 if (proceed == MagickFalse)
3174 image_view=DestroyCacheView(image_view);
3180 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3181 #pragma omp parallel for schedule(static) shared(progress,status) \
3182 dynamic_number_threads(image,image->columns,image->rows,1)
3184 for (y=0; y < (ssize_t) image->rows; y++)
3192 if (status == MagickFalse)
3194 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3195 if (q == (Quantum *) NULL)
3200 for (x=0; x < (ssize_t) image->columns; x++)
3205 if (GetPixelMask(image,q) != 0)
3207 q+=GetPixelChannels(image);
3210 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3218 channel=GetPixelChannelMapChannel(image,i);
3219 traits=GetPixelChannelMapTraits(image,channel);
3220 if ((traits & UpdatePixelTrait) == 0)
3222 q[i]=QuantumRange-q[i];
3224 q+=GetPixelChannels(image);
3226 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3228 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3233 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3234 #pragma omp critical (MagickCore_NegateImage)
3236 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3237 if (proceed == MagickFalse)
3241 image_view=DestroyCacheView(image_view);
3246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3250 % N o r m a l i z e I m a g e %
3254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3256 % The NormalizeImage() method enhances the contrast of a color image by
3257 % mapping the darkest 2 percent of all pixel to black and the brightest
3258 % 1 percent to white.
3260 % The format of the NormalizeImage method is:
3262 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3264 % A description of each parameter follows:
3266 % o image: the image.
3268 % o exception: return any errors or warnings in this structure.
3271 MagickExport MagickBooleanType NormalizeImage(Image *image,
3272 ExceptionInfo *exception)
3278 black_point=(double) image->columns*image->rows*0.0015;
3279 white_point=(double) image->columns*image->rows*0.9995;
3280 return(ContrastStretchImage(image,black_point,white_point,exception));
3284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3288 % S i g m o i d a l C o n t r a s t I m a g e %
3292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3294 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3295 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3296 % sigmoidal transfer function without saturating highlights or shadows.
3297 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3298 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3299 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3300 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3303 % The format of the SigmoidalContrastImage method is:
3305 % MagickBooleanType SigmoidalContrastImage(Image *image,
3306 % const MagickBooleanType sharpen,const char *levels,
3307 % ExceptionInfo *exception)
3309 % A description of each parameter follows:
3311 % o image: the image.
3313 % o sharpen: Increase or decrease image contrast.
3315 % o alpha: strength of the contrast, the larger the number the more
3316 % 'threshold-like' it becomes.
3318 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3320 % o exception: return any errors or warnings in this structure.
3323 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3324 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3325 ExceptionInfo *exception)
3327 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3348 Allocate and initialize sigmoidal maps.
3350 assert(image != (Image *) NULL);
3351 assert(image->signature == MagickSignature);
3352 if (image->debug != MagickFalse)
3353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3354 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3355 sizeof(*sigmoidal_map));
3356 if (sigmoidal_map == (MagickRealType *) NULL)
3357 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3359 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3360 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3361 #pragma omp parallel for schedule(static) shared(progress,status) \
3362 dynamic_number_threads(image,image->columns,1,1)
3364 for (i=0; i <= (ssize_t) MaxMap; i++)
3366 if (sharpen != MagickFalse)
3368 #define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3370 /* Simpilified function scaling,
3371 * with better 'contrast=0' or 'flatline' handling (greyscale)
3374 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3375 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3376 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3377 (MagickRealType)(MaxMap*(
3378 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3379 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3381 /* Scaled sigmoidal formula...
3382 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3383 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
3384 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3385 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3386 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3387 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3388 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3389 (double) QuantumRange)))))+0.5));
3396 http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
3399 min = sigmoidal(contrast,1.0,0.0),
3400 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3401 xi = min+(double)i/MaxMap*(max-min);
3402 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3403 (MagickRealType)(MaxMap*(
3404 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3407 /* expanded form of the above */
3408 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3409 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3410 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3411 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3412 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3413 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3414 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3415 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3416 (double) QuantumRange*contrast))))))/contrast)));
3419 if (image->storage_class == PseudoClass)
3422 Sigmoidal-contrast enhance colormap.
3424 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3425 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3426 dynamic_number_threads(image,image->columns,1,1)
3428 for (i=0; i < (ssize_t) image->colors; i++)
3430 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3431 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3432 ClampToQuantum(image->colormap[i].red))];
3433 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3434 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3435 ClampToQuantum(image->colormap[i].green))];
3436 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3437 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3438 ClampToQuantum(image->colormap[i].blue))];
3439 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3440 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3441 ClampToQuantum(image->colormap[i].alpha))];
3445 Sigmoidal-contrast enhance image.
3449 image_view=AcquireAuthenticCacheView(image,exception);
3450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3451 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3452 dynamic_number_threads(image,image->columns,image->rows,1)
3454 for (y=0; y < (ssize_t) image->rows; y++)
3462 if (status == MagickFalse)
3464 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3465 if (q == (Quantum *) NULL)
3470 for (x=0; x < (ssize_t) image->columns; x++)
3475 if (GetPixelMask(image,q) != 0)
3477 q+=GetPixelChannels(image);
3480 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3488 channel=GetPixelChannelMapChannel(image,i);
3489 traits=GetPixelChannelMapTraits(image,channel);
3490 if ((traits & UpdatePixelTrait) == 0)
3492 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3494 q+=GetPixelChannels(image);
3496 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3498 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3503 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3504 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3506 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3508 if (proceed == MagickFalse)
3512 image_view=DestroyCacheView(image_view);
3513 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);