2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE N N H H AAA N N CCCC EEEEE %
7 % E NN N H H A A NN N C E %
8 % EEE N N N HHHHH AAAAA N N N C EEE %
9 % E N NN H H A A N NN C E %
10 % EEEEE N N H H A A N N CCCC EEEEE %
13 % MagickCore Image Enhancement Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/composite-private.h"
52 #include "MagickCore/enhance.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/fx.h"
56 #include "MagickCore/gem.h"
57 #include "MagickCore/gem-private.h"
58 #include "MagickCore/geometry.h"
59 #include "MagickCore/histogram.h"
60 #include "MagickCore/image.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/quantum.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/resample.h"
71 #include "MagickCore/resample-private.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #include "MagickCore/thread-private.h"
77 #include "MagickCore/token.h"
78 #include "MagickCore/xml-tree.h"
79 #include "MagickCore/xml-tree-private.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % A u t o G a m m a I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % AutoGammaImage() extract the 'mean' from the image and adjust the image
93 % to try make set its gamma appropriatally.
95 % The format of the AutoGammaImage method is:
97 % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
99 % A description of each parameter follows:
101 % o image: The image to auto-level
103 % o exception: return any errors or warnings in this structure.
106 MagickExport MagickBooleanType AutoGammaImage(Image *image,
107 ExceptionInfo *exception)
122 if (image->channel_mask == DefaultChannels)
125 Apply gamma correction equally across all given channels.
127 (void) GetImageMean(image,&mean,&sans,exception);
128 gamma=log(mean*QuantumScale)/log_mean;
129 return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception));
132 Auto-gamma each channel separately.
135 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
146 channel=GetPixelChannelMapChannel(image,i);
147 traits=GetPixelChannelMapTraits(image,channel);
148 if ((traits & UpdatePixelTrait) == 0)
150 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
151 status=GetImageMean(image,&mean,&sans,exception);
152 gamma=log(mean*QuantumScale)/log_mean;
153 status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
154 (void) SetPixelChannelMask(image,channel_mask);
155 if (status == MagickFalse)
158 return(status != 0 ? MagickTrue : MagickFalse);
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % A u t o L e v e l I m a g e %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % AutoLevelImage() adjusts the levels of a particular image channel by
173 % scaling the minimum and maximum values to the full quantum range.
175 % The format of the LevelImage method is:
177 % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
179 % A description of each parameter follows:
181 % o image: The image to auto-level
183 % o exception: return any errors or warnings in this structure.
186 MagickExport MagickBooleanType AutoLevelImage(Image *image,
187 ExceptionInfo *exception)
189 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % B r i g h t n e s s C o n t r a s t I m a g e %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % BrightnessContrastImage() changes the brightness and/or contrast of an
204 % image. It converts the brightness and contrast parameters into slope and
205 % intercept and calls a polynomical function to apply to the image.
207 % The format of the BrightnessContrastImage method is:
209 % MagickBooleanType BrightnessContrastImage(Image *image,
210 % const double brightness,const double contrast,ExceptionInfo *exception)
212 % A description of each parameter follows:
214 % o image: the image.
216 % o brightness: the brightness percent (-100 .. 100).
218 % o contrast: the contrast percent (-100 .. 100).
220 % o exception: return any errors or warnings in this structure.
223 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
224 const double brightness,const double contrast,ExceptionInfo *exception)
226 #define BrightnessContastImageTag "BrightnessContast/Image"
238 Compute slope and intercept.
240 assert(image != (Image *) NULL);
241 assert(image->signature == MagickSignature);
242 if (image->debug != MagickFalse)
243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
245 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
248 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
249 coefficients[0]=slope;
250 coefficients[1]=intercept;
251 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % C l u t I m a g e %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % ClutImage() replaces each color value in the given image, by using it as an
267 % index to lookup a replacement color value in a Color Look UP Table in the
268 % form of an image. The values are extracted along a diagonal of the CLUT
269 % image so either a horizontal or vertial gradient image can be used.
271 % Typically this is used to either re-color a gray-scale image according to a
272 % color gradient in the CLUT image, or to perform a freeform histogram
273 % (level) adjustment according to the (typically gray-scale) gradient in the
276 % When the 'channel' mask includes the matte/alpha transparency channel but
277 % one image has no such channel it is assumed that that image is a simple
278 % gray-scale image that will effect the alpha channel values, either for
279 % gray-scale coloring (with transparent or semi-transparent colors), or
280 % a histogram adjustment of existing alpha channel values. If both images
281 % have matte channels, direct and normal indexing is applied, which is rarely
284 % The format of the ClutImage method is:
286 % MagickBooleanType ClutImage(Image *image,Image *clut_image,
287 % const PixelInterpolateMethod method,ExceptionInfo *exception)
289 % A description of each parameter follows:
291 % o image: the image, which is replaced by indexed CLUT values
293 % o clut_image: the color lookup table image for replacement color values.
295 % o method: the pixel interpolation method.
297 % o exception: return any errors or warnings in this structure.
300 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
301 const PixelInterpolateMethod method,ExceptionInfo *exception)
303 #define ClutImageTag "Clut/Image"
324 assert(image != (Image *) NULL);
325 assert(image->signature == MagickSignature);
326 if (image->debug != MagickFalse)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
328 assert(clut_image != (Image *) NULL);
329 assert(clut_image->signature == MagickSignature);
330 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
332 if (IsGrayColorspace(image->colorspace) != MagickFalse)
333 (void) TransformImageColorspace(image,RGBColorspace,exception);
334 clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
335 if (clut_map == (PixelInfo *) NULL)
336 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
343 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
344 clut_view=AcquireVirtualCacheView(clut_image,exception);
345 #if defined(MAGICKCORE_OPENMP_SUPPORT)
346 #pragma omp parallel for schedule(static,4) \
347 dynamic_number_threads(image,image->columns,1,1)
349 for (i=0; i <= (ssize_t) MaxMap; i++)
351 GetPixelInfo(clut_image,clut_map+i);
352 (void) InterpolatePixelInfo(clut_image,clut_view,method,
353 QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i*
354 (clut_image->rows-adjust),clut_map+i,exception);
356 clut_view=DestroyCacheView(clut_view);
357 image_view=AcquireAuthenticCacheView(image,exception);
358 #if defined(MAGICKCORE_OPENMP_SUPPORT)
359 #pragma omp parallel for schedule(static,4) shared(progress,status) \
360 dynamic_number_threads(image,image->columns,image->rows,1)
362 for (y=0; y < (ssize_t) image->rows; y++)
373 if (status == MagickFalse)
375 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
376 if (q == (Quantum *) NULL)
381 GetPixelInfo(image,&pixel);
382 for (x=0; x < (ssize_t) image->columns; x++)
384 if (GetPixelMask(image,q) != 0)
386 q+=GetPixelChannels(image);
389 GetPixelInfoPixel(image,q,&pixel);
390 pixel.red=clut_map[ScaleQuantumToMap(
391 ClampToQuantum(pixel.red))].red;
392 pixel.green=clut_map[ScaleQuantumToMap(
393 ClampToQuantum(pixel.green))].green;
394 pixel.blue=clut_map[ScaleQuantumToMap(
395 ClampToQuantum(pixel.blue))].blue;
396 pixel.black=clut_map[ScaleQuantumToMap(
397 ClampToQuantum(pixel.black))].black;
398 pixel.alpha=clut_map[ScaleQuantumToMap(
399 ClampToQuantum(pixel.alpha))].alpha;
400 SetPixelInfoPixel(image,&pixel,q);
401 q+=GetPixelChannels(image);
403 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
410 #if defined(MAGICKCORE_OPENMP_SUPPORT)
411 #pragma omp critical (MagickCore_ClutImage)
413 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
414 if (proceed == MagickFalse)
418 image_view=DestroyCacheView(image_view);
419 clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
420 if ((clut_image->matte != MagickFalse) &&
421 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
422 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % C o l o r D e c i s i o n L i s t I m a g e %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
438 % (CCC) file which solely contains one or more color corrections and applies
439 % the correction to the image. Here is a sample CCC file:
441 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
442 % <ColorCorrection id="cc03345">
444 % <Slope> 0.9 1.2 0.5 </Slope>
445 % <Offset> 0.4 -0.5 0.6 </Offset>
446 % <Power> 1.0 0.8 1.5 </Power>
449 % <Saturation> 0.85 </Saturation>
452 % </ColorCorrectionCollection>
454 % which includes the slop, offset, and power for each of the RGB channels
455 % as well as the saturation.
457 % The format of the ColorDecisionListImage method is:
459 % MagickBooleanType ColorDecisionListImage(Image *image,
460 % const char *color_correction_collection,ExceptionInfo *exception)
462 % A description of each parameter follows:
464 % o image: the image.
466 % o color_correction_collection: the color correction collection in XML.
468 % o exception: return any errors or warnings in this structure.
471 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
472 const char *color_correction_collection,ExceptionInfo *exception)
474 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
476 typedef struct _Correction
484 typedef struct _ColorCorrection
499 token[MaxTextExtent];
530 Allocate and initialize cdl maps.
532 assert(image != (Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 if (color_correction_collection == (const char *) NULL)
538 ccc=NewXMLTree((const char *) color_correction_collection,exception);
539 if (ccc == (XMLTreeInfo *) NULL)
541 cc=GetXMLTreeChild(ccc,"ColorCorrection");
542 if (cc == (XMLTreeInfo *) NULL)
544 ccc=DestroyXMLTree(ccc);
547 color_correction.red.slope=1.0;
548 color_correction.red.offset=0.0;
549 color_correction.red.power=1.0;
550 color_correction.green.slope=1.0;
551 color_correction.green.offset=0.0;
552 color_correction.green.power=1.0;
553 color_correction.blue.slope=1.0;
554 color_correction.blue.offset=0.0;
555 color_correction.blue.power=1.0;
556 color_correction.saturation=0.0;
557 sop=GetXMLTreeChild(cc,"SOPNode");
558 if (sop != (XMLTreeInfo *) NULL)
565 slope=GetXMLTreeChild(sop,"Slope");
566 if (slope != (XMLTreeInfo *) NULL)
568 content=GetXMLTreeContent(slope);
569 p=(const char *) content;
570 for (i=0; (*p != '\0') && (i < 3); i++)
572 GetMagickToken(p,&p,token);
574 GetMagickToken(p,&p,token);
579 color_correction.red.slope=StringToDouble(token,(char **) NULL);
584 color_correction.green.slope=StringToDouble(token,
590 color_correction.blue.slope=StringToDouble(token,
597 offset=GetXMLTreeChild(sop,"Offset");
598 if (offset != (XMLTreeInfo *) NULL)
600 content=GetXMLTreeContent(offset);
601 p=(const char *) content;
602 for (i=0; (*p != '\0') && (i < 3); i++)
604 GetMagickToken(p,&p,token);
606 GetMagickToken(p,&p,token);
611 color_correction.red.offset=StringToDouble(token,
617 color_correction.green.offset=StringToDouble(token,
623 color_correction.blue.offset=StringToDouble(token,
630 power=GetXMLTreeChild(sop,"Power");
631 if (power != (XMLTreeInfo *) NULL)
633 content=GetXMLTreeContent(power);
634 p=(const char *) content;
635 for (i=0; (*p != '\0') && (i < 3); i++)
637 GetMagickToken(p,&p,token);
639 GetMagickToken(p,&p,token);
644 color_correction.red.power=StringToDouble(token,(char **) NULL);
649 color_correction.green.power=StringToDouble(token,
655 color_correction.blue.power=StringToDouble(token,
663 sat=GetXMLTreeChild(cc,"SATNode");
664 if (sat != (XMLTreeInfo *) NULL)
669 saturation=GetXMLTreeChild(sat,"Saturation");
670 if (saturation != (XMLTreeInfo *) NULL)
672 content=GetXMLTreeContent(saturation);
673 p=(const char *) content;
674 GetMagickToken(p,&p,token);
675 color_correction.saturation=StringToDouble(token,(char **) NULL);
678 ccc=DestroyXMLTree(ccc);
679 if (image->debug != MagickFalse)
681 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
682 " Color Correction Collection:");
683 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
684 " color_correction.red.slope: %g",color_correction.red.slope);
685 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
686 " color_correction.red.offset: %g",color_correction.red.offset);
687 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
688 " color_correction.red.power: %g",color_correction.red.power);
689 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
690 " color_correction.green.slope: %g",color_correction.green.slope);
691 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
692 " color_correction.green.offset: %g",color_correction.green.offset);
693 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
694 " color_correction.green.power: %g",color_correction.green.power);
695 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
696 " color_correction.blue.slope: %g",color_correction.blue.slope);
697 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
698 " color_correction.blue.offset: %g",color_correction.blue.offset);
699 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
700 " color_correction.blue.power: %g",color_correction.blue.power);
701 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
702 " color_correction.saturation: %g",color_correction.saturation);
704 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
705 if (cdl_map == (PixelInfo *) NULL)
706 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
708 #if defined(MAGICKCORE_OPENMP_SUPPORT)
709 #pragma omp parallel for schedule(static,4) \
710 dynamic_number_threads(image,image->columns,1,1)
712 for (i=0; i <= (ssize_t) MaxMap; i++)
714 cdl_map[i].red=(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.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
739 0.07217*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.21267*GetPixelRed(image,q)+0.71526*GetPixelGreen(image,q)+0.07217*
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 ConvertRGBToHSB(*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 ConvertHSBToRGB(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++)
1093 pixel=(double) GetPixelIntensity(image,p);
1094 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1096 if (image->channel_mask != DefaultChannels)
1098 histogram[GetPixelChannels(image)*ScaleQuantumToMap(pixel)+i]++;
1100 p+=GetPixelChannels(image);
1103 image_view=DestroyCacheView(image_view);
1105 Find the histogram boundaries by locating the black/white levels.
1107 number_channels=GetPixelChannels(image);
1108 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1109 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1110 dynamic_number_threads(image,image->columns,1,1)
1112 for (i=0; i < (ssize_t) number_channels; i++)
1121 white[i]=MaxRange(QuantumRange);
1123 for (j=0; j <= (ssize_t) MaxMap; j++)
1125 intensity+=histogram[GetPixelChannels(image)*j+i];
1126 if (intensity > black_point)
1129 black[i]=(MagickRealType) j;
1131 for (j=(ssize_t) MaxMap; j != 0; j--)
1133 intensity+=histogram[GetPixelChannels(image)*j+i];
1134 if (intensity > ((double) image->columns*image->rows-white_point))
1137 white[i]=(MagickRealType) j;
1139 histogram=(double *) RelinquishMagickMemory(histogram);
1141 Stretch the histogram to create the stretched image mapping.
1143 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1144 sizeof(*stretch_map));
1145 number_channels=GetPixelChannels(image);
1146 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1147 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1148 dynamic_number_threads(image,image->columns,1,1)
1150 for (i=0; i < (ssize_t) number_channels; i++)
1155 for (j=0; j <= (ssize_t) MaxMap; j++)
1157 if (j < (ssize_t) black[i])
1158 stretch_map[GetPixelChannels(image)*j+i]=0.0;
1160 if (j > (ssize_t) white[i])
1161 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1164 if (black[i] != white[i])
1165 stretch_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1166 ScaleMapToQuantum((MagickRealType) (MaxMap*(j-black[i])/
1167 (white[i]-black[i])));
1170 if (image->storage_class == PseudoClass)
1176 Stretch-contrast colormap.
1178 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1179 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1180 dynamic_number_threads(image,image->columns,1,1)
1182 for (j=0; j < (ssize_t) image->colors; j++)
1184 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1186 i=GetPixelChannelMapChannel(image,RedPixelChannel);
1187 if (black[i] != white[i])
1188 image->colormap[j].red=stretch_map[GetPixelChannels(image)*
1189 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
1191 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1193 i=GetPixelChannelMapChannel(image,GreenPixelChannel);
1194 if (black[i] != white[i])
1195 image->colormap[j].green=stretch_map[GetPixelChannels(image)*
1196 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
1198 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1200 i=GetPixelChannelMapChannel(image,BluePixelChannel);
1201 if (black[i] != white[i])
1202 image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
1203 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
1205 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1207 i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1208 if (black[i] != white[i])
1209 image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
1210 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
1215 Stretch-contrast image.
1219 image_view=AcquireAuthenticCacheView(image,exception);
1220 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1221 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1222 dynamic_number_threads(image,image->columns,image->rows,1)
1224 for (y=0; y < (ssize_t) image->rows; y++)
1232 if (status == MagickFalse)
1234 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1235 if (q == (Quantum *) NULL)
1240 for (x=0; x < (ssize_t) image->columns; x++)
1245 if (GetPixelMask(image,q) != 0)
1247 q+=GetPixelChannels(image);
1250 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1258 channel=GetPixelChannelMapChannel(image,i);
1259 traits=GetPixelChannelMapTraits(image,channel);
1260 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1262 q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1263 ScaleQuantumToMap(q[i])+i]);
1265 q+=GetPixelChannels(image);
1267 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1269 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1274 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1275 #pragma omp critical (MagickCore_ContrastStretchImage)
1277 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1279 if (proceed == MagickFalse)
1283 image_view=DestroyCacheView(image_view);
1284 stretch_map=(double *) RelinquishMagickMemory(stretch_map);
1285 white=(double *) RelinquishMagickMemory(white);
1286 black=(double *) RelinquishMagickMemory(black);
1291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1295 % E n h a n c e I m a g e %
1299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % EnhanceImage() applies a digital filter that improves the quality of a
1304 % The format of the EnhanceImage method is:
1306 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1308 % A description of each parameter follows:
1310 % o image: the image.
1312 % o exception: return any errors or warnings in this structure.
1315 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1317 #define EnhancePixel(weight) \
1318 mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
1319 distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
1320 enhance_image,channel,q); \
1321 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1322 mean)*distance*distance; \
1323 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1324 QuantumRange/25.0f)) \
1326 aggregate+=(weight)*r[i]; \
1327 total_weight+=(weight); \
1329 r+=GetPixelChannels(image);
1330 #define EnhanceImageTag "Enhance/Image"
1349 Initialize enhanced image attributes.
1351 assert(image != (const Image *) NULL);
1352 assert(image->signature == MagickSignature);
1353 if (image->debug != MagickFalse)
1354 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1355 assert(exception != (ExceptionInfo *) NULL);
1356 assert(exception->signature == MagickSignature);
1357 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1359 if (enhance_image == (Image *) NULL)
1360 return((Image *) NULL);
1361 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1363 enhance_image=DestroyImage(enhance_image);
1364 return((Image *) NULL);
1371 image_view=AcquireVirtualCacheView(image,exception);
1372 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1373 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1374 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1375 dynamic_number_threads(image,image->columns,image->rows,1)
1377 for (y=0; y < (ssize_t) image->rows; y++)
1379 register const Quantum
1391 if (status == MagickFalse)
1393 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1394 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1396 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1401 center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
1402 for (x=0; x < (ssize_t) image->columns; x++)
1407 if (GetPixelMask(image,p) != 0)
1409 p+=GetPixelChannels(image);
1410 q+=GetPixelChannels(enhance_image);
1413 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1429 register const Quantum
1432 channel=GetPixelChannelMapChannel(image,i);
1433 traits=GetPixelChannelMapTraits(image,channel);
1434 enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
1435 if ((traits == UndefinedPixelTrait) ||
1436 (enhance_traits == UndefinedPixelTrait))
1438 SetPixelChannel(enhance_image,channel,p[center+i],q);
1439 if ((enhance_traits & CopyPixelTrait) != 0)
1442 Compute weighted average of target pixel color components.
1447 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1448 EnhancePixel(8.0); EnhancePixel(5.0);
1449 r=p+1*GetPixelChannels(image)*(image->columns+4);
1450 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1451 EnhancePixel(20.0); EnhancePixel(8.0);
1452 r=p+2*GetPixelChannels(image)*(image->columns+4);
1453 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1454 EnhancePixel(40.0); EnhancePixel(10.0);
1455 r=p+3*GetPixelChannels(image)*(image->columns+4);
1456 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1457 EnhancePixel(20.0); EnhancePixel(8.0);
1458 r=p+4*GetPixelChannels(image)*(image->columns+4);
1459 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1460 EnhancePixel(8.0); EnhancePixel(5.0);
1461 SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
1464 p+=GetPixelChannels(image);
1465 q+=GetPixelChannels(enhance_image);
1467 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1469 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1474 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1475 #pragma omp critical (MagickCore_EnhanceImage)
1477 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1478 if (proceed == MagickFalse)
1482 enhance_view=DestroyCacheView(enhance_view);
1483 image_view=DestroyCacheView(image_view);
1484 return(enhance_image);
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 % E q u a l i z e I m a g e %
1496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % EqualizeImage() applies a histogram equalization to the image.
1500 % The format of the EqualizeImage method is:
1502 % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception)
1504 % A description of each parameter follows:
1506 % o image: the image.
1508 % o exception: return any errors or warnings in this structure.
1511 MagickExport MagickBooleanType EqualizeImage(Image *image,
1512 ExceptionInfo *exception)
1514 #define EqualizeImageTag "Equalize/Image"
1526 black[CompositePixelChannel],
1530 white[CompositePixelChannel];
1542 Allocate and initialize histogram arrays.
1544 assert(image != (Image *) NULL);
1545 assert(image->signature == MagickSignature);
1546 if (image->debug != MagickFalse)
1547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1548 equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1549 GetPixelChannels(image)*sizeof(*equalize_map));
1550 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1551 GetPixelChannels(image)*sizeof(*histogram));
1552 map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
1553 GetPixelChannels(image)*sizeof(*map));
1554 if ((equalize_map == (MagickRealType *) NULL) ||
1555 (histogram == (MagickRealType *) NULL) ||
1556 (map == (MagickRealType *) NULL))
1558 if (map != (MagickRealType *) NULL)
1559 map=(MagickRealType *) RelinquishMagickMemory(map);
1560 if (histogram != (MagickRealType *) NULL)
1561 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1562 if (equalize_map != (MagickRealType *) NULL)
1563 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1564 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1571 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1572 sizeof(*histogram));
1573 image_view=AcquireVirtualCacheView(image,exception);
1574 for (y=0; y < (ssize_t) image->rows; y++)
1576 register const Quantum
1582 if (status == MagickFalse)
1584 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1585 if (p == (const Quantum *) NULL)
1590 for (x=0; x < (ssize_t) image->columns; x++)
1595 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1596 histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
1597 p+=GetPixelChannels(image);
1600 image_view=DestroyCacheView(image_view);
1602 Integrate the histogram to get the equalization map.
1604 number_channels=GetPixelChannels(image);
1605 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1606 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1607 dynamic_number_threads(image,image->columns,1,1)
1609 for (i=0; i < (ssize_t) number_channels; i++)
1618 for (j=0; j <= (ssize_t) MaxMap; j++)
1620 intensity+=histogram[GetPixelChannels(image)*j+i];
1621 map[GetPixelChannels(image)*j+i]=intensity;
1624 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
1625 sizeof(*equalize_map));
1626 number_channels=GetPixelChannels(image);
1627 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1628 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1629 dynamic_number_threads(image,image->columns,1,1)
1631 for (i=0; i < (ssize_t) number_channels; i++)
1637 white[i]=map[GetPixelChannels(image)*MaxMap+i];
1638 if (black[i] != white[i])
1639 for (j=0; j <= (ssize_t) MaxMap; j++)
1640 equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
1641 ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
1642 GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
1644 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
1645 map=(MagickRealType *) RelinquishMagickMemory(map);
1646 if (image->storage_class == PseudoClass)
1657 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1658 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1659 dynamic_number_threads(image,image->columns,1,1)
1661 for (j=0; j < (ssize_t) image->colors; j++)
1663 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1665 channel=GetPixelChannelMapChannel(image,RedPixelChannel);
1666 if (black[channel] != white[channel])
1667 image->colormap[j].red=equalize_map[GetPixelChannels(image)*
1668 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
1671 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1673 channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
1674 if (black[channel] != white[channel])
1675 image->colormap[j].green=equalize_map[GetPixelChannels(image)*
1676 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
1679 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1681 channel=GetPixelChannelMapChannel(image,BluePixelChannel);
1682 if (black[channel] != white[channel])
1683 image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
1684 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
1687 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1689 channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
1690 if (black[channel] != white[channel])
1691 image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
1692 ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
1701 image_view=AcquireAuthenticCacheView(image,exception);
1702 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1703 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1704 dynamic_number_threads(image,image->columns,image->rows,1)
1706 for (y=0; y < (ssize_t) image->rows; y++)
1714 if (status == MagickFalse)
1716 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1717 if (q == (Quantum *) NULL)
1722 for (x=0; x < (ssize_t) image->columns; x++)
1727 if (GetPixelMask(image,q) != 0)
1729 q+=GetPixelChannels(image);
1732 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1740 channel=GetPixelChannelMapChannel(image,i);
1741 traits=GetPixelChannelMapTraits(image,channel);
1742 if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
1744 q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
1745 ScaleQuantumToMap(q[i])+i]);
1747 q+=GetPixelChannels(image);
1749 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1751 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1756 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1757 #pragma omp critical (MagickCore_EqualizeImage)
1759 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1760 if (proceed == MagickFalse)
1764 image_view=DestroyCacheView(image_view);
1765 equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
1770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1774 % G a m m a I m a g e %
1778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1780 % GammaImage() gamma-corrects a particular image channel. The same
1781 % image viewed on different devices will have perceptual differences in the
1782 % way the image's intensities are represented on the screen. Specify
1783 % individual gamma levels for the red, green, and blue channels, or adjust
1784 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1786 % You can also reduce the influence of a particular channel with a gamma
1789 % The format of the GammaImage method is:
1791 % MagickBooleanType GammaImage(Image *image,const double gamma,
1792 % ExceptionInfo *exception)
1794 % A description of each parameter follows:
1796 % o image: the image.
1798 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1800 % o gamma: the image gamma.
1803 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1804 ExceptionInfo *exception)
1806 #define GammaCorrectImageTag "GammaCorrect/Image"
1827 Allocate and initialize gamma maps.
1829 assert(image != (Image *) NULL);
1830 assert(image->signature == MagickSignature);
1831 if (image->debug != MagickFalse)
1832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1835 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1836 if (gamma_map == (Quantum *) NULL)
1837 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1839 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1841 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1842 #pragma omp parallel for \
1843 dynamic_number_threads(image,image->columns,1,1)
1845 for (i=0; i <= (ssize_t) MaxMap; i++)
1846 gamma_map[i]=ScaleMapToQuantum((MagickRealType) (MaxMap*pow((double) i/
1847 MaxMap,1.0/gamma)));
1848 if (image->storage_class == PseudoClass)
1851 Gamma-correct colormap.
1853 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1854 #pragma omp parallel for schedule(static) shared(progress,status) \
1855 dynamic_number_threads(image,image->columns,1,1)
1857 for (i=0; i < (ssize_t) image->colors; i++)
1859 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1860 image->colormap[i].red=(MagickRealType) gamma_map[
1861 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
1862 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1863 image->colormap[i].green=(MagickRealType) gamma_map[
1864 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
1865 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1866 image->colormap[i].blue=(MagickRealType) gamma_map[
1867 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
1868 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1869 image->colormap[i].alpha=(MagickRealType) gamma_map[
1870 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
1874 Gamma-correct image.
1878 image_view=AcquireAuthenticCacheView(image,exception);
1879 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1880 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1881 dynamic_number_threads(image,image->columns,image->rows,1)
1883 for (y=0; y < (ssize_t) image->rows; y++)
1891 if (status == MagickFalse)
1893 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1894 if (q == (Quantum *) NULL)
1899 for (x=0; x < (ssize_t) image->columns; x++)
1904 if (GetPixelMask(image,q) != 0)
1906 q+=GetPixelChannels(image);
1909 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1917 channel=GetPixelChannelMapChannel(image,i);
1918 traits=GetPixelChannelMapTraits(image,channel);
1919 if ((traits & UpdatePixelTrait) == 0)
1921 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
1923 q+=GetPixelChannels(image);
1925 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1927 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1932 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1933 #pragma omp critical (MagickCore_GammaImage)
1935 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
1937 if (proceed == MagickFalse)
1941 image_view=DestroyCacheView(image_view);
1942 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
1943 if (image->gamma != 0.0)
1944 image->gamma*=gamma;
1949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1953 % H a l d C l u t I m a g e %
1957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
1960 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
1961 % Create it with the HALD coder. You can apply any color transformation to
1962 % the Hald image and then use this method to apply the transform to the
1965 % The format of the HaldClutImage method is:
1967 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
1968 % ExceptionInfo *exception)
1970 % A description of each parameter follows:
1972 % o image: the image, which is replaced by indexed CLUT values
1974 % o hald_image: the color lookup table image for replacement color values.
1976 % o exception: return any errors or warnings in this structure.
1980 static inline size_t MagickMin(const size_t x,const size_t y)
1987 MagickExport MagickBooleanType HaldClutImage(Image *image,
1988 const Image *hald_image,ExceptionInfo *exception)
1990 #define HaldClutImageTag "Clut/Image"
1992 typedef struct _HaldInfo
2024 assert(image != (Image *) NULL);
2025 assert(image->signature == MagickSignature);
2026 if (image->debug != MagickFalse)
2027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2028 assert(hald_image != (Image *) NULL);
2029 assert(hald_image->signature == MagickSignature);
2030 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2031 return(MagickFalse);
2032 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2033 (void) TransformImageColorspace(image,RGBColorspace,exception);
2034 if (image->matte == MagickFalse)
2035 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2041 length=MagickMin(hald_image->columns,hald_image->rows);
2042 for (level=2; (level*level*level) < length; level++) ;
2044 cube_size=level*level;
2045 width=(double) hald_image->columns;
2046 GetPixelInfo(hald_image,&zero);
2047 hald_view=AcquireVirtualCacheView(hald_image,exception);
2048 image_view=AcquireAuthenticCacheView(image,exception);
2049 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2050 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2051 dynamic_number_threads(image,image->columns,image->rows,1)
2053 for (y=0; y < (ssize_t) image->rows; y++)
2061 if (status == MagickFalse)
2063 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2064 if (q == (Quantum *) NULL)
2069 for (x=0; x < (ssize_t) image->columns; x++)
2084 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2085 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2086 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2087 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2088 point.x-=floor(point.x);
2089 point.y-=floor(point.y);
2090 point.z-=floor(point.z);
2092 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2093 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 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2102 fmod(offset,width),floor(offset/width),&pixel1,exception);
2103 (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
2104 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2106 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2109 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2111 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2112 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2113 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2114 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2115 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2116 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2117 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2118 (image->colorspace == CMYKColorspace))
2119 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2120 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2121 (image->matte != MagickFalse))
2122 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2123 q+=GetPixelChannels(image);
2125 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2127 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2132 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2133 #pragma omp critical (MagickCore_HaldClutImage)
2135 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2136 if (proceed == MagickFalse)
2140 hald_view=DestroyCacheView(hald_view);
2141 image_view=DestroyCacheView(image_view);
2146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2150 % L e v e l I m a g e %
2154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2156 % LevelImage() adjusts the levels of a particular image channel by
2157 % scaling the colors falling between specified white and black points to
2158 % the full available quantum range.
2160 % The parameters provided represent the black, and white points. The black
2161 % point specifies the darkest color in the image. Colors darker than the
2162 % black point are set to zero. White point specifies the lightest color in
2163 % the image. Colors brighter than the white point are set to the maximum
2166 % If a '!' flag is given, map black and white colors to the given levels
2167 % rather than mapping those levels to black and white. See
2168 % LevelizeImage() below.
2170 % Gamma specifies a gamma correction to apply to the image.
2172 % The format of the LevelImage method is:
2174 % MagickBooleanType LevelImage(Image *image,const double black_point,
2175 % const double white_point,const double gamma,ExceptionInfo *exception)
2177 % A description of each parameter follows:
2179 % o image: the image.
2181 % o black_point: The level to map zero (black) to.
2183 % o white_point: The level to map QuantumRange (white) to.
2185 % o exception: return any errors or warnings in this structure.
2189 static inline MagickRealType LevelPixel(const double black_point,
2190 const double white_point,const double gamma,const MagickRealType pixel)
2196 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2197 level_pixel=(MagickRealType) QuantumRange*pow(scale*((double) pixel-
2198 black_point),1.0/gamma);
2199 return(level_pixel);
2202 MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
2203 const double white_point,const double gamma,ExceptionInfo *exception)
2205 #define LevelImageTag "Level/Image"
2223 Allocate and initialize levels map.
2225 assert(image != (Image *) NULL);
2226 assert(image->signature == MagickSignature);
2227 if (image->debug != MagickFalse)
2228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2229 if (image->storage_class == PseudoClass)
2230 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2231 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2232 dynamic_number_threads(image,image->columns,1,1)
2234 for (i=0; i < (ssize_t) image->colors; i++)
2239 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2240 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2241 white_point,gamma,image->colormap[i].red));
2242 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2243 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2244 white_point,gamma,image->colormap[i].green));
2245 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2246 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2247 white_point,gamma,image->colormap[i].blue));
2248 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2249 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2250 white_point,gamma,image->colormap[i].alpha));
2257 image_view=AcquireAuthenticCacheView(image,exception);
2258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2259 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2260 dynamic_number_threads(image,image->columns,image->rows,1)
2262 for (y=0; y < (ssize_t) image->rows; y++)
2270 if (status == MagickFalse)
2272 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2273 if (q == (Quantum *) NULL)
2278 for (x=0; x < (ssize_t) image->columns; x++)
2283 if (GetPixelMask(image,q) != 0)
2285 q+=GetPixelChannels(image);
2288 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2296 channel=GetPixelChannelMapChannel(image,i);
2297 traits=GetPixelChannelMapTraits(image,channel);
2298 if ((traits & UpdatePixelTrait) == 0)
2300 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2301 (MagickRealType) q[i]));
2303 q+=GetPixelChannels(image);
2305 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2307 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2312 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2313 #pragma omp critical (MagickCore_LevelImage)
2315 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2316 if (proceed == MagickFalse)
2320 image_view=DestroyCacheView(image_view);
2325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2329 % L e v e l i z e I m a g e %
2333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2335 % LevelizeImage() applies the reversed LevelImage() operation to just
2336 % the specific channels specified. It compresses the full range of color
2337 % values, so that they lie between the given black and white points. Gamma is
2338 % applied before the values are mapped.
2340 % LevelizeImage() can be called with by using a +level command line
2341 % API option, or using a '!' on a -level or LevelImage() geometry string.
2343 % It can be used to de-contrast a greyscale image to the exact levels
2344 % specified. Or by using specific levels for each channel of an image you
2345 % can convert a gray-scale image to any linear color gradient, according to
2348 % The format of the LevelizeImage method is:
2350 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2351 % const double white_point,const double gamma,ExceptionInfo *exception)
2353 % A description of each parameter follows:
2355 % o image: the image.
2357 % o black_point: The level to map zero (black) to.
2359 % o white_point: The level to map QuantumRange (white) to.
2361 % o gamma: adjust gamma by this factor before mapping values.
2363 % o exception: return any errors or warnings in this structure.
2366 MagickExport MagickBooleanType LevelizeImage(Image *image,
2367 const double black_point,const double white_point,const double gamma,
2368 ExceptionInfo *exception)
2370 #define LevelizeImageTag "Levelize/Image"
2371 #define LevelizeValue(x) (ClampToQuantum((pow((double) (QuantumScale*(x)), \
2372 1.0/gamma))*(white_point-black_point)+black_point))
2390 Allocate and initialize levels map.
2392 assert(image != (Image *) NULL);
2393 assert(image->signature == MagickSignature);
2394 if (image->debug != MagickFalse)
2395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2396 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2397 (void) SetImageColorspace(image,RGBColorspace,exception);
2398 if (image->storage_class == PseudoClass)
2399 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2400 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2401 dynamic_number_threads(image,image->columns,1,1)
2403 for (i=0; i < (ssize_t) image->colors; i++)
2408 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2409 image->colormap[i].red=(double) LevelizeValue(
2410 image->colormap[i].red);
2411 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2412 image->colormap[i].green=(double) LevelizeValue(
2413 image->colormap[i].green);
2414 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2415 image->colormap[i].blue=(double) LevelizeValue(
2416 image->colormap[i].blue);
2417 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2418 image->colormap[i].alpha=(double) LevelizeValue(
2419 image->colormap[i].alpha);
2426 image_view=AcquireAuthenticCacheView(image,exception);
2427 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2428 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2429 dynamic_number_threads(image,image->columns,image->rows,1)
2431 for (y=0; y < (ssize_t) image->rows; y++)
2439 if (status == MagickFalse)
2441 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2442 if (q == (Quantum *) NULL)
2447 for (x=0; x < (ssize_t) image->columns; x++)
2452 if (GetPixelMask(image,q) != 0)
2454 q+=GetPixelChannels(image);
2457 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2465 channel=GetPixelChannelMapChannel(image,i);
2466 traits=GetPixelChannelMapTraits(image,channel);
2467 if ((traits & UpdatePixelTrait) == 0)
2469 q[i]=LevelizeValue(q[i]);
2471 q+=GetPixelChannels(image);
2473 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2475 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2480 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2481 #pragma omp critical (MagickCore_LevelizeImage)
2483 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2484 if (proceed == MagickFalse)
2488 image_view=DestroyCacheView(image_view);
2493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2497 % L e v e l I m a g e C o l o r s %
2501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2503 % LevelImageColors() maps the given color to "black" and "white" values,
2504 % linearly spreading out the colors, and level values on a channel by channel
2505 % bases, as per LevelImage(). The given colors allows you to specify
2506 % different level ranges for each of the color channels separately.
2508 % If the boolean 'invert' is set true the image values will modifyed in the
2509 % reverse direction. That is any existing "black" and "white" colors in the
2510 % image will become the color values given, with all other values compressed
2511 % appropriatally. This effectivally maps a greyscale gradient into the given
2514 % The format of the LevelImageColors method is:
2516 % MagickBooleanType LevelImageColors(Image *image,
2517 % const PixelInfo *black_color,const PixelInfo *white_color,
2518 % const MagickBooleanType invert,ExceptionInfo *exception)
2520 % A description of each parameter follows:
2522 % o image: the image.
2524 % o black_color: The color to map black to/from
2526 % o white_point: The color to map white to/from
2528 % o invert: if true map the colors (levelize), rather than from (level)
2530 % o exception: return any errors or warnings in this structure.
2533 MagickExport MagickBooleanType LevelImageColors(Image *image,
2534 const PixelInfo *black_color,const PixelInfo *white_color,
2535 const MagickBooleanType invert,ExceptionInfo *exception)
2544 Allocate and initialize levels map.
2546 assert(image != (Image *) NULL);
2547 assert(image->signature == MagickSignature);
2548 if (image->debug != MagickFalse)
2549 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2551 if (invert == MagickFalse)
2553 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2555 channel_mask=SetPixelChannelMask(image,RedChannel);
2556 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2558 (void) SetPixelChannelMask(image,channel_mask);
2560 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2562 channel_mask=SetPixelChannelMask(image,GreenChannel);
2563 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2565 (void) SetPixelChannelMask(image,channel_mask);
2567 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2569 channel_mask=SetPixelChannelMask(image,BlueChannel);
2570 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2572 (void) SetPixelChannelMask(image,channel_mask);
2574 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2575 (image->colorspace == CMYKColorspace))
2577 channel_mask=SetPixelChannelMask(image,BlackChannel);
2578 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2580 (void) SetPixelChannelMask(image,channel_mask);
2582 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2583 (image->matte == MagickTrue))
2585 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2586 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2588 (void) SetPixelChannelMask(image,channel_mask);
2593 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2595 channel_mask=SetPixelChannelMask(image,RedChannel);
2596 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2598 (void) SetPixelChannelMask(image,channel_mask);
2600 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2602 channel_mask=SetPixelChannelMask(image,GreenChannel);
2603 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2605 (void) SetPixelChannelMask(image,channel_mask);
2607 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2609 channel_mask=SetPixelChannelMask(image,BlueChannel);
2610 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2612 (void) SetPixelChannelMask(image,channel_mask);
2614 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2615 (image->colorspace == CMYKColorspace))
2617 channel_mask=SetPixelChannelMask(image,BlackChannel);
2618 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2620 (void) SetPixelChannelMask(image,channel_mask);
2622 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2623 (image->matte == MagickTrue))
2625 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2626 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2628 (void) SetPixelChannelMask(image,channel_mask);
2631 return(status == 0 ? MagickFalse : MagickTrue);
2635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2639 % L i n e a r S t r e t c h I m a g e %
2643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2645 % LinearStretchImage() discards any pixels below the black point and above
2646 % the white point and levels the remaining pixels.
2648 % The format of the LinearStretchImage method is:
2650 % MagickBooleanType LinearStretchImage(Image *image,
2651 % const double black_point,const double white_point,
2652 % ExceptionInfo *exception)
2654 % A description of each parameter follows:
2656 % o image: the image.
2658 % o black_point: the black point.
2660 % o white_point: the white point.
2662 % o exception: return any errors or warnings in this structure.
2665 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2666 const double black_point,const double white_point,ExceptionInfo *exception)
2668 #define LinearStretchImageTag "LinearStretch/Image"
2686 Allocate histogram and linear map.
2688 assert(image != (Image *) NULL);
2689 assert(image->signature == MagickSignature);
2690 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2691 sizeof(*histogram));
2692 if (histogram == (MagickRealType *) NULL)
2693 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2698 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2699 image_view=AcquireVirtualCacheView(image,exception);
2700 for (y=0; y < (ssize_t) image->rows; y++)
2702 register const Quantum
2708 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2709 if (p == (const Quantum *) NULL)
2711 for (x=0; x < (ssize_t) image->columns; x++)
2713 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2714 p+=GetPixelChannels(image);
2717 image_view=DestroyCacheView(image_view);
2719 Find the histogram boundaries by locating the black and white point levels.
2722 for (black=0; black < (ssize_t) MaxMap; black++)
2724 intensity+=histogram[black];
2725 if (intensity >= black_point)
2729 for (white=(ssize_t) MaxMap; white != 0; white--)
2731 intensity+=histogram[white];
2732 if (intensity >= white_point)
2735 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2736 status=LevelImage(image,(double) black,(double) white,1.0,exception);
2741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2745 % M o d u l a t e I m a g e %
2749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2751 % ModulateImage() lets you control the brightness, saturation, and hue
2752 % of an image. Modulate represents the brightness, saturation, and hue
2753 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2754 % modulation is lightness, saturation, and hue. And if the colorspace is
2755 % HWB, use blackness, whiteness, and hue.
2757 % The format of the ModulateImage method is:
2759 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2760 % ExceptionInfo *exception)
2762 % A description of each parameter follows:
2764 % o image: the image.
2766 % o modulate: Define the percent change in brightness, saturation, and hue.
2768 % o exception: return any errors or warnings in this structure.
2772 static void ModulateHSB(const double percent_hue,
2773 const double percent_saturation,const double percent_brightness,double *red,
2774 double *green,double *blue)
2782 Increase or decrease color brightness, saturation, or hue.
2784 assert(red != (double *) NULL);
2785 assert(green != (double *) NULL);
2786 assert(blue != (double *) NULL);
2787 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2788 hue+=0.5*(0.01*percent_hue-1.0);
2793 saturation*=0.01*percent_saturation;
2794 brightness*=0.01*percent_brightness;
2795 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2798 static void ModulateHSL(const double percent_hue,
2799 const double percent_saturation,const double percent_lightness,double *red,
2800 double *green,double *blue)
2808 Increase or decrease color lightness, saturation, or hue.
2810 assert(red != (double *) NULL);
2811 assert(green != (double *) NULL);
2812 assert(blue != (double *) NULL);
2813 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2814 hue+=0.5*(0.01*percent_hue-1.0);
2819 saturation*=0.01*percent_saturation;
2820 lightness*=0.01*percent_lightness;
2821 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2824 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
2832 Increase or decrease color blackness, whiteness, or hue.
2834 assert(red != (double *) NULL);
2835 assert(green != (double *) NULL);
2836 assert(blue != (double *) NULL);
2837 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2838 hue+=0.5*(0.01*percent_hue-1.0);
2843 blackness*=0.01*percent_blackness;
2844 whiteness*=0.01*percent_whiteness;
2845 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2848 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2849 ExceptionInfo *exception)
2851 #define ModulateImageTag "Modulate/Image"
2886 Initialize modulate table.
2888 assert(image != (Image *) NULL);
2889 assert(image->signature == MagickSignature);
2890 if (image->debug != MagickFalse)
2891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2892 if (modulate == (char *) NULL)
2893 return(MagickFalse);
2894 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2895 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2896 flags=ParseGeometry(modulate,&geometry_info);
2897 percent_brightness=geometry_info.rho;
2898 percent_saturation=geometry_info.sigma;
2899 if ((flags & SigmaValue) == 0)
2900 percent_saturation=100.0;
2901 percent_hue=geometry_info.xi;
2902 if ((flags & XiValue) == 0)
2904 colorspace=UndefinedColorspace;
2905 artifact=GetImageArtifact(image,"modulate:colorspace");
2906 if (artifact != (const char *) NULL)
2907 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2908 MagickFalse,artifact);
2909 if (image->storage_class == PseudoClass)
2914 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2915 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2916 dynamic_number_threads(image,image->columns,1,1)
2918 for (i=0; i < (ssize_t) image->colors; i++)
2925 red=image->colormap[i].red;
2926 green=image->colormap[i].green;
2927 blue=image->colormap[i].blue;
2932 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2939 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2945 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2957 image_view=AcquireAuthenticCacheView(image,exception);
2958 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2959 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2960 dynamic_number_threads(image,image->columns,image->rows,1)
2962 for (y=0; y < (ssize_t) image->rows; y++)
2970 if (status == MagickFalse)
2972 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2973 if (q == (Quantum *) NULL)
2978 for (x=0; x < (ssize_t) image->columns; x++)
2985 red=(double) GetPixelRed(image,q);
2986 green=(double) GetPixelGreen(image,q);
2987 blue=(double) GetPixelBlue(image,q);
2992 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2999 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3005 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3010 SetPixelRed(image,ClampToQuantum(red),q);
3011 SetPixelGreen(image,ClampToQuantum(green),q);
3012 SetPixelBlue(image,ClampToQuantum(blue),q);
3013 q+=GetPixelChannels(image);
3015 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3017 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3022 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3023 #pragma omp critical (MagickCore_ModulateImage)
3025 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3026 if (proceed == MagickFalse)
3030 image_view=DestroyCacheView(image_view);
3035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3039 % N e g a t e I m a g e %
3043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3045 % NegateImage() negates the colors in the reference image. The grayscale
3046 % option means that only grayscale values within the image are negated.
3048 % The format of the NegateImage method is:
3050 % MagickBooleanType NegateImage(Image *image,
3051 % const MagickBooleanType grayscale,ExceptionInfo *exception)
3053 % A description of each parameter follows:
3055 % o image: the image.
3057 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3059 % o exception: return any errors or warnings in this structure.
3062 MagickExport MagickBooleanType NegateImage(Image *image,
3063 const MagickBooleanType grayscale,ExceptionInfo *exception)
3065 #define NegateImageTag "Negate/Image"
3082 assert(image != (Image *) NULL);
3083 assert(image->signature == MagickSignature);
3084 if (image->debug != MagickFalse)
3085 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3086 if (image->storage_class == PseudoClass)
3091 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3092 #pragma omp parallel for schedule(static) shared(progress,status) \
3093 dynamic_number_threads(image,image->columns,1,1)
3095 for (i=0; i < (ssize_t) image->colors; i++)
3097 if (grayscale != MagickFalse)
3098 if ((image->colormap[i].red != image->colormap[i].green) ||
3099 (image->colormap[i].green != image->colormap[i].blue))
3101 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3102 image->colormap[i].red=QuantumRange-
3103 image->colormap[i].red;
3104 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3105 image->colormap[i].green=QuantumRange-
3106 image->colormap[i].green;
3107 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3108 image->colormap[i].blue=QuantumRange-
3109 image->colormap[i].blue;
3117 image_view=AcquireAuthenticCacheView(image,exception);
3118 if (grayscale != MagickFalse)
3120 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3121 #pragma omp parallel for schedule(static) shared(progress,status) \
3122 dynamic_number_threads(image,image->columns,image->rows,1)
3124 for (y=0; y < (ssize_t) image->rows; y++)
3135 if (status == MagickFalse)
3137 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3139 if (q == (Quantum *) NULL)
3144 for (x=0; x < (ssize_t) image->columns; x++)
3149 if ((GetPixelMask(image,q) != 0) ||
3150 (IsPixelGray(image,q) != MagickFalse))
3152 q+=GetPixelChannels(image);
3155 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3163 channel=GetPixelChannelMapChannel(image,i);
3164 traits=GetPixelChannelMapTraits(image,channel);
3165 if ((traits & UpdatePixelTrait) == 0)
3167 q[i]=QuantumRange-q[i];
3169 q+=GetPixelChannels(image);
3171 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3172 if (sync == MagickFalse)
3174 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3179 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3180 #pragma omp critical (MagickCore_NegateImage)
3182 proceed=SetImageProgress(image,NegateImageTag,progress++,
3184 if (proceed == MagickFalse)
3188 image_view=DestroyCacheView(image_view);
3194 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3195 #pragma omp parallel for schedule(static) shared(progress,status) \
3196 dynamic_number_threads(image,image->columns,image->rows,1)
3198 for (y=0; y < (ssize_t) image->rows; y++)
3206 if (status == MagickFalse)
3208 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3209 if (q == (Quantum *) NULL)
3214 for (x=0; x < (ssize_t) image->columns; x++)
3219 if (GetPixelMask(image,q) != 0)
3221 q+=GetPixelChannels(image);
3224 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3232 channel=GetPixelChannelMapChannel(image,i);
3233 traits=GetPixelChannelMapTraits(image,channel);
3234 if ((traits & UpdatePixelTrait) == 0)
3236 q[i]=QuantumRange-q[i];
3238 q+=GetPixelChannels(image);
3240 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3242 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3247 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3248 #pragma omp critical (MagickCore_NegateImage)
3250 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3251 if (proceed == MagickFalse)
3255 image_view=DestroyCacheView(image_view);
3260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3264 % N o r m a l i z e I m a g e %
3268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3270 % The NormalizeImage() method enhances the contrast of a color image by
3271 % mapping the darkest 2 percent of all pixel to black and the brightest
3272 % 1 percent to white.
3274 % The format of the NormalizeImage method is:
3276 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3278 % A description of each parameter follows:
3280 % o image: the image.
3282 % o exception: return any errors or warnings in this structure.
3285 MagickExport MagickBooleanType NormalizeImage(Image *image,
3286 ExceptionInfo *exception)
3292 black_point=(double) image->columns*image->rows*0.0015;
3293 white_point=(double) image->columns*image->rows*0.9995;
3294 return(ContrastStretchImage(image,black_point,white_point,exception));
3298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3302 % S i g m o i d a l C o n t r a s t I m a g e %
3306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3308 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3309 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3310 % sigmoidal transfer function without saturating highlights or shadows.
3311 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3312 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3313 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3314 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3317 % The format of the SigmoidalContrastImage method is:
3319 % MagickBooleanType SigmoidalContrastImage(Image *image,
3320 % const MagickBooleanType sharpen,const char *levels,
3321 % ExceptionInfo *exception)
3323 % A description of each parameter follows:
3325 % o image: the image.
3327 % o sharpen: Increase or decrease image contrast.
3329 % o alpha: strength of the contrast, the larger the number the more
3330 % 'threshold-like' it becomes.
3332 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3334 % o exception: return any errors or warnings in this structure.
3337 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3338 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3339 ExceptionInfo *exception)
3341 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3362 Allocate and initialize sigmoidal maps.
3364 assert(image != (Image *) NULL);
3365 assert(image->signature == MagickSignature);
3366 if (image->debug != MagickFalse)
3367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3368 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3369 sizeof(*sigmoidal_map));
3370 if (sigmoidal_map == (MagickRealType *) NULL)
3371 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3373 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3374 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3375 #pragma omp parallel for schedule(static) shared(progress,status) \
3376 dynamic_number_threads(image,image->columns,1,1)
3378 for (i=0; i <= (ssize_t) MaxMap; i++)
3380 if (sharpen != MagickFalse)
3382 #define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3384 /* Simpilified function scaling,
3385 * with better 'contrast=0' or 'flatline' handling (greyscale)
3388 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3389 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3390 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3391 (MagickRealType)(MaxMap*(
3392 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3393 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3395 /* Scaled sigmoidal formula...
3396 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3397 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
3398 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3399 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3400 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3401 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3402 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3403 (double) QuantumRange)))))+0.5));
3409 /* Broken: not the inverse of any of the above variants */
3411 min = sigmoidal(contrast,1.0,0.0),
3412 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3413 xi = min+(double)i/MaxMap*(max-min);
3414 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3415 (MagickRealType)(MaxMap*(
3416 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3419 /* Inverse of the second -sigmoidal-contrast function above
3420 * and pretty close to being an inverse of the second version
3421 * (with MagickEpsilon). See
3422 * http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html.
3424 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3425 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3426 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3427 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3428 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3429 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3430 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3431 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3432 (double) QuantumRange*contrast))))))/contrast)));
3435 if (image->storage_class == PseudoClass)
3438 Sigmoidal-contrast enhance colormap.
3440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3441 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3442 dynamic_number_threads(image,image->columns,1,1)
3444 for (i=0; i < (ssize_t) image->colors; i++)
3446 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3447 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3448 ClampToQuantum(image->colormap[i].red))];
3449 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3450 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3451 ClampToQuantum(image->colormap[i].green))];
3452 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3453 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3454 ClampToQuantum(image->colormap[i].blue))];
3455 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3456 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3457 ClampToQuantum(image->colormap[i].alpha))];
3461 Sigmoidal-contrast enhance image.
3465 image_view=AcquireAuthenticCacheView(image,exception);
3466 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3467 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3468 dynamic_number_threads(image,image->columns,image->rows,1)
3470 for (y=0; y < (ssize_t) image->rows; y++)
3478 if (status == MagickFalse)
3480 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3481 if (q == (Quantum *) NULL)
3486 for (x=0; x < (ssize_t) image->columns; x++)
3491 if (GetPixelMask(image,q) != 0)
3493 q+=GetPixelChannels(image);
3496 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3504 channel=GetPixelChannelMapChannel(image,i);
3505 traits=GetPixelChannelMapTraits(image,channel);
3506 if ((traits & UpdatePixelTrait) == 0)
3508 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3510 q+=GetPixelChannels(image);
3512 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3514 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3519 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3520 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3522 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3524 if (proceed == MagickFalse)
3528 image_view=DestroyCacheView(image_view);
3529 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);