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.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++)
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 (image->storage_class == PseudoClass)
2222 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2223 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2224 dynamic_number_threads(image,image->columns,1,1)
2226 for (i=0; i < (ssize_t) image->colors; i++)
2231 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2232 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2233 white_point,gamma,image->colormap[i].red));
2234 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2235 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2236 white_point,gamma,image->colormap[i].green));
2237 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2238 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2239 white_point,gamma,image->colormap[i].blue));
2240 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2241 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2242 white_point,gamma,image->colormap[i].alpha));
2249 image_view=AcquireAuthenticCacheView(image,exception);
2250 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2251 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2252 dynamic_number_threads(image,image->columns,image->rows,1)
2254 for (y=0; y < (ssize_t) image->rows; y++)
2262 if (status == MagickFalse)
2264 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2265 if (q == (Quantum *) NULL)
2270 for (x=0; x < (ssize_t) image->columns; x++)
2275 if (GetPixelMask(image,q) != 0)
2277 q+=GetPixelChannels(image);
2280 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2288 channel=GetPixelChannelMapChannel(image,i);
2289 traits=GetPixelChannelMapTraits(image,channel);
2290 if ((traits & UpdatePixelTrait) == 0)
2292 q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
2293 (MagickRealType) q[i]));
2295 q+=GetPixelChannels(image);
2297 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2299 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2304 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2305 #pragma omp critical (MagickCore_LevelImage)
2307 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2308 if (proceed == MagickFalse)
2312 image_view=DestroyCacheView(image_view);
2317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2321 % L e v e l i z e I m a g e %
2325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327 % LevelizeImage() applies the reversed LevelImage() operation to just
2328 % the specific channels specified. It compresses the full range of color
2329 % values, so that they lie between the given black and white points. Gamma is
2330 % applied before the values are mapped.
2332 % LevelizeImage() can be called with by using a +level command line
2333 % API option, or using a '!' on a -level or LevelImage() geometry string.
2335 % It can be used to de-contrast a greyscale image to the exact levels
2336 % specified. Or by using specific levels for each channel of an image you
2337 % can convert a gray-scale image to any linear color gradient, according to
2340 % The format of the LevelizeImage method is:
2342 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2343 % const double white_point,const double gamma,ExceptionInfo *exception)
2345 % A description of each parameter follows:
2347 % o image: the image.
2349 % o black_point: The level to map zero (black) to.
2351 % o white_point: The level to map QuantumRange (white) to.
2353 % o gamma: adjust gamma by this factor before mapping values.
2355 % o exception: return any errors or warnings in this structure.
2358 MagickExport MagickBooleanType LevelizeImage(Image *image,
2359 const double black_point,const double white_point,const double gamma,
2360 ExceptionInfo *exception)
2362 #define LevelizeImageTag "Levelize/Image"
2363 #define LevelizeValue(x) (ClampToQuantum((pow((double) (QuantumScale*(x)), \
2364 1.0/gamma))*(white_point-black_point)+black_point))
2382 Allocate and initialize levels map.
2384 assert(image != (Image *) NULL);
2385 assert(image->signature == MagickSignature);
2386 if (image->debug != MagickFalse)
2387 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2388 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2389 (void) SetImageColorspace(image,sRGBColorspace,exception);
2390 if (image->storage_class == PseudoClass)
2391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2392 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2393 dynamic_number_threads(image,image->columns,1,1)
2395 for (i=0; i < (ssize_t) image->colors; i++)
2400 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2401 image->colormap[i].red=(double) LevelizeValue(
2402 image->colormap[i].red);
2403 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2404 image->colormap[i].green=(double) LevelizeValue(
2405 image->colormap[i].green);
2406 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2407 image->colormap[i].blue=(double) LevelizeValue(
2408 image->colormap[i].blue);
2409 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2410 image->colormap[i].alpha=(double) LevelizeValue(
2411 image->colormap[i].alpha);
2418 image_view=AcquireAuthenticCacheView(image,exception);
2419 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2420 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2421 dynamic_number_threads(image,image->columns,image->rows,1)
2423 for (y=0; y < (ssize_t) image->rows; y++)
2431 if (status == MagickFalse)
2433 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2434 if (q == (Quantum *) NULL)
2439 for (x=0; x < (ssize_t) image->columns; x++)
2444 if (GetPixelMask(image,q) != 0)
2446 q+=GetPixelChannels(image);
2449 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2457 channel=GetPixelChannelMapChannel(image,i);
2458 traits=GetPixelChannelMapTraits(image,channel);
2459 if ((traits & UpdatePixelTrait) == 0)
2461 q[i]=LevelizeValue(q[i]);
2463 q+=GetPixelChannels(image);
2465 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2467 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2472 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2473 #pragma omp critical (MagickCore_LevelizeImage)
2475 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2476 if (proceed == MagickFalse)
2480 image_view=DestroyCacheView(image_view);
2485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2489 % L e v e l I m a g e C o l o r s %
2493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2495 % LevelImageColors() maps the given color to "black" and "white" values,
2496 % linearly spreading out the colors, and level values on a channel by channel
2497 % bases, as per LevelImage(). The given colors allows you to specify
2498 % different level ranges for each of the color channels separately.
2500 % If the boolean 'invert' is set true the image values will modifyed in the
2501 % reverse direction. That is any existing "black" and "white" colors in the
2502 % image will become the color values given, with all other values compressed
2503 % appropriatally. This effectivally maps a greyscale gradient into the given
2506 % The format of the LevelImageColors method is:
2508 % MagickBooleanType LevelImageColors(Image *image,
2509 % const PixelInfo *black_color,const PixelInfo *white_color,
2510 % const MagickBooleanType invert,ExceptionInfo *exception)
2512 % A description of each parameter follows:
2514 % o image: the image.
2516 % o black_color: The color to map black to/from
2518 % o white_point: The color to map white to/from
2520 % o invert: if true map the colors (levelize), rather than from (level)
2522 % o exception: return any errors or warnings in this structure.
2525 MagickExport MagickBooleanType LevelImageColors(Image *image,
2526 const PixelInfo *black_color,const PixelInfo *white_color,
2527 const MagickBooleanType invert,ExceptionInfo *exception)
2536 Allocate and initialize levels map.
2538 assert(image != (Image *) NULL);
2539 assert(image->signature == MagickSignature);
2540 if (image->debug != MagickFalse)
2541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2543 if (invert == MagickFalse)
2545 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2547 channel_mask=SetPixelChannelMask(image,RedChannel);
2548 status|=LevelImage(image,black_color->red,white_color->red,1.0,
2550 (void) SetPixelChannelMask(image,channel_mask);
2552 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2554 channel_mask=SetPixelChannelMask(image,GreenChannel);
2555 status|=LevelImage(image,black_color->green,white_color->green,1.0,
2557 (void) SetPixelChannelMask(image,channel_mask);
2559 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2561 channel_mask=SetPixelChannelMask(image,BlueChannel);
2562 status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
2564 (void) SetPixelChannelMask(image,channel_mask);
2566 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2567 (image->colorspace == CMYKColorspace))
2569 channel_mask=SetPixelChannelMask(image,BlackChannel);
2570 status|=LevelImage(image,black_color->black,white_color->black,1.0,
2572 (void) SetPixelChannelMask(image,channel_mask);
2574 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2575 (image->matte == MagickTrue))
2577 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2578 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
2580 (void) SetPixelChannelMask(image,channel_mask);
2585 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2587 channel_mask=SetPixelChannelMask(image,RedChannel);
2588 status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
2590 (void) SetPixelChannelMask(image,channel_mask);
2592 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2594 channel_mask=SetPixelChannelMask(image,GreenChannel);
2595 status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
2597 (void) SetPixelChannelMask(image,channel_mask);
2599 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2601 channel_mask=SetPixelChannelMask(image,BlueChannel);
2602 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
2604 (void) SetPixelChannelMask(image,channel_mask);
2606 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2607 (image->colorspace == CMYKColorspace))
2609 channel_mask=SetPixelChannelMask(image,BlackChannel);
2610 status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
2612 (void) SetPixelChannelMask(image,channel_mask);
2614 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2615 (image->matte == MagickTrue))
2617 channel_mask=SetPixelChannelMask(image,AlphaChannel);
2618 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
2620 (void) SetPixelChannelMask(image,channel_mask);
2623 return(status == 0 ? MagickFalse : MagickTrue);
2627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2631 % L i n e a r S t r e t c h I m a g e %
2635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2637 % LinearStretchImage() discards any pixels below the black point and above
2638 % the white point and levels the remaining pixels.
2640 % The format of the LinearStretchImage method is:
2642 % MagickBooleanType LinearStretchImage(Image *image,
2643 % const double black_point,const double white_point,
2644 % ExceptionInfo *exception)
2646 % A description of each parameter follows:
2648 % o image: the image.
2650 % o black_point: the black point.
2652 % o white_point: the white point.
2654 % o exception: return any errors or warnings in this structure.
2657 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2658 const double black_point,const double white_point,ExceptionInfo *exception)
2660 #define LinearStretchImageTag "LinearStretch/Image"
2678 Allocate histogram and linear map.
2680 assert(image != (Image *) NULL);
2681 assert(image->signature == MagickSignature);
2682 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2683 sizeof(*histogram));
2684 if (histogram == (MagickRealType *) NULL)
2685 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2690 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2691 image_view=AcquireVirtualCacheView(image,exception);
2692 for (y=0; y < (ssize_t) image->rows; y++)
2694 register const Quantum
2700 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2701 if (p == (const Quantum *) NULL)
2703 for (x=0; x < (ssize_t) image->columns; x++)
2705 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2706 p+=GetPixelChannels(image);
2709 image_view=DestroyCacheView(image_view);
2711 Find the histogram boundaries by locating the black and white point levels.
2714 for (black=0; black < (ssize_t) MaxMap; black++)
2716 intensity+=histogram[black];
2717 if (intensity >= black_point)
2721 for (white=(ssize_t) MaxMap; white != 0; white--)
2723 intensity+=histogram[white];
2724 if (intensity >= white_point)
2727 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2728 status=LevelImage(image,(double) black,(double) white,1.0,exception);
2733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2737 % M o d u l a t e I m a g e %
2741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2743 % ModulateImage() lets you control the brightness, saturation, and hue
2744 % of an image. Modulate represents the brightness, saturation, and hue
2745 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2746 % modulation is lightness, saturation, and hue. And if the colorspace is
2747 % HWB, use blackness, whiteness, and hue.
2749 % The format of the ModulateImage method is:
2751 % MagickBooleanType ModulateImage(Image *image,const char *modulate,
2752 % ExceptionInfo *exception)
2754 % A description of each parameter follows:
2756 % o image: the image.
2758 % o modulate: Define the percent change in brightness, saturation, and hue.
2760 % o exception: return any errors or warnings in this structure.
2764 static void ModulateHSB(const double percent_hue,
2765 const double percent_saturation,const double percent_brightness,double *red,
2766 double *green,double *blue)
2774 Increase or decrease color brightness, saturation, or hue.
2776 assert(red != (double *) NULL);
2777 assert(green != (double *) NULL);
2778 assert(blue != (double *) NULL);
2779 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2780 hue+=0.5*(0.01*percent_hue-1.0);
2785 saturation*=0.01*percent_saturation;
2786 brightness*=0.01*percent_brightness;
2787 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2790 static void ModulateHSL(const double percent_hue,
2791 const double percent_saturation,const double percent_lightness,double *red,
2792 double *green,double *blue)
2800 Increase or decrease color lightness, saturation, or hue.
2802 assert(red != (double *) NULL);
2803 assert(green != (double *) NULL);
2804 assert(blue != (double *) NULL);
2805 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2806 hue+=0.5*(0.01*percent_hue-1.0);
2811 saturation*=0.01*percent_saturation;
2812 lightness*=0.01*percent_lightness;
2813 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2816 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,double *red,double *green,double *blue)
2824 Increase or decrease color blackness, whiteness, or hue.
2826 assert(red != (double *) NULL);
2827 assert(green != (double *) NULL);
2828 assert(blue != (double *) NULL);
2829 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2830 hue+=0.5*(0.01*percent_hue-1.0);
2835 blackness*=0.01*percent_blackness;
2836 whiteness*=0.01*percent_whiteness;
2837 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2840 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
2841 ExceptionInfo *exception)
2843 #define ModulateImageTag "Modulate/Image"
2878 Initialize modulate table.
2880 assert(image != (Image *) NULL);
2881 assert(image->signature == MagickSignature);
2882 if (image->debug != MagickFalse)
2883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2884 if (modulate == (char *) NULL)
2885 return(MagickFalse);
2886 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2887 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2888 flags=ParseGeometry(modulate,&geometry_info);
2889 percent_brightness=geometry_info.rho;
2890 percent_saturation=geometry_info.sigma;
2891 if ((flags & SigmaValue) == 0)
2892 percent_saturation=100.0;
2893 percent_hue=geometry_info.xi;
2894 if ((flags & XiValue) == 0)
2896 colorspace=UndefinedColorspace;
2897 artifact=GetImageArtifact(image,"modulate:colorspace");
2898 if (artifact != (const char *) NULL)
2899 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2900 MagickFalse,artifact);
2901 if (image->storage_class == PseudoClass)
2906 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2907 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2908 dynamic_number_threads(image,image->columns,1,1)
2910 for (i=0; i < (ssize_t) image->colors; i++)
2917 red=image->colormap[i].red;
2918 green=image->colormap[i].green;
2919 blue=image->colormap[i].blue;
2924 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2931 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2937 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2949 image_view=AcquireAuthenticCacheView(image,exception);
2950 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2951 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2952 dynamic_number_threads(image,image->columns,image->rows,1)
2954 for (y=0; y < (ssize_t) image->rows; y++)
2962 if (status == MagickFalse)
2964 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2965 if (q == (Quantum *) NULL)
2970 for (x=0; x < (ssize_t) image->columns; x++)
2977 red=(double) GetPixelRed(image,q);
2978 green=(double) GetPixelGreen(image,q);
2979 blue=(double) GetPixelBlue(image,q);
2984 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2991 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2997 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3002 SetPixelRed(image,ClampToQuantum(red),q);
3003 SetPixelGreen(image,ClampToQuantum(green),q);
3004 SetPixelBlue(image,ClampToQuantum(blue),q);
3005 q+=GetPixelChannels(image);
3007 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3009 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3014 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3015 #pragma omp critical (MagickCore_ModulateImage)
3017 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3018 if (proceed == MagickFalse)
3022 image_view=DestroyCacheView(image_view);
3027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3031 % N e g a t e I m a g e %
3035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3037 % NegateImage() negates the colors in the reference image. The grayscale
3038 % option means that only grayscale values within the image are negated.
3040 % The format of the NegateImage method is:
3042 % MagickBooleanType NegateImage(Image *image,
3043 % const MagickBooleanType grayscale,ExceptionInfo *exception)
3045 % A description of each parameter follows:
3047 % o image: the image.
3049 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3051 % o exception: return any errors or warnings in this structure.
3054 MagickExport MagickBooleanType NegateImage(Image *image,
3055 const MagickBooleanType grayscale,ExceptionInfo *exception)
3057 #define NegateImageTag "Negate/Image"
3074 assert(image != (Image *) NULL);
3075 assert(image->signature == MagickSignature);
3076 if (image->debug != MagickFalse)
3077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3078 if (image->storage_class == PseudoClass)
3083 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3084 #pragma omp parallel for schedule(static) shared(progress,status) \
3085 dynamic_number_threads(image,image->columns,1,1)
3087 for (i=0; i < (ssize_t) image->colors; i++)
3089 if (grayscale != MagickFalse)
3090 if ((image->colormap[i].red != image->colormap[i].green) ||
3091 (image->colormap[i].green != image->colormap[i].blue))
3093 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3094 image->colormap[i].red=QuantumRange-
3095 image->colormap[i].red;
3096 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3097 image->colormap[i].green=QuantumRange-
3098 image->colormap[i].green;
3099 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3100 image->colormap[i].blue=QuantumRange-
3101 image->colormap[i].blue;
3109 image_view=AcquireAuthenticCacheView(image,exception);
3110 if (grayscale != MagickFalse)
3112 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3113 #pragma omp parallel for schedule(static) shared(progress,status) \
3114 dynamic_number_threads(image,image->columns,image->rows,1)
3116 for (y=0; y < (ssize_t) image->rows; y++)
3127 if (status == MagickFalse)
3129 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3131 if (q == (Quantum *) NULL)
3136 for (x=0; x < (ssize_t) image->columns; x++)
3141 if ((GetPixelMask(image,q) != 0) ||
3142 (IsPixelGray(image,q) != MagickFalse))
3144 q+=GetPixelChannels(image);
3147 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3155 channel=GetPixelChannelMapChannel(image,i);
3156 traits=GetPixelChannelMapTraits(image,channel);
3157 if ((traits & UpdatePixelTrait) == 0)
3159 q[i]=QuantumRange-q[i];
3161 q+=GetPixelChannels(image);
3163 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3164 if (sync == MagickFalse)
3166 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3171 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3172 #pragma omp critical (MagickCore_NegateImage)
3174 proceed=SetImageProgress(image,NegateImageTag,progress++,
3176 if (proceed == MagickFalse)
3180 image_view=DestroyCacheView(image_view);
3186 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3187 #pragma omp parallel for schedule(static) shared(progress,status) \
3188 dynamic_number_threads(image,image->columns,image->rows,1)
3190 for (y=0; y < (ssize_t) image->rows; y++)
3198 if (status == MagickFalse)
3200 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3201 if (q == (Quantum *) NULL)
3206 for (x=0; x < (ssize_t) image->columns; x++)
3211 if (GetPixelMask(image,q) != 0)
3213 q+=GetPixelChannels(image);
3216 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3224 channel=GetPixelChannelMapChannel(image,i);
3225 traits=GetPixelChannelMapTraits(image,channel);
3226 if ((traits & UpdatePixelTrait) == 0)
3228 q[i]=QuantumRange-q[i];
3230 q+=GetPixelChannels(image);
3232 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3234 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3239 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3240 #pragma omp critical (MagickCore_NegateImage)
3242 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3243 if (proceed == MagickFalse)
3247 image_view=DestroyCacheView(image_view);
3252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3256 % N o r m a l i z e I m a g e %
3260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3262 % The NormalizeImage() method enhances the contrast of a color image by
3263 % mapping the darkest 2 percent of all pixel to black and the brightest
3264 % 1 percent to white.
3266 % The format of the NormalizeImage method is:
3268 % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception)
3270 % A description of each parameter follows:
3272 % o image: the image.
3274 % o exception: return any errors or warnings in this structure.
3277 MagickExport MagickBooleanType NormalizeImage(Image *image,
3278 ExceptionInfo *exception)
3284 black_point=(double) image->columns*image->rows*0.0015;
3285 white_point=(double) image->columns*image->rows*0.9995;
3286 return(ContrastStretchImage(image,black_point,white_point,exception));
3290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3294 % S i g m o i d a l C o n t r a s t I m a g e %
3298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3300 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3301 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3302 % sigmoidal transfer function without saturating highlights or shadows.
3303 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3304 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3305 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3306 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3309 % The format of the SigmoidalContrastImage method is:
3311 % MagickBooleanType SigmoidalContrastImage(Image *image,
3312 % const MagickBooleanType sharpen,const char *levels,
3313 % ExceptionInfo *exception)
3315 % A description of each parameter follows:
3317 % o image: the image.
3319 % o sharpen: Increase or decrease image contrast.
3321 % o alpha: strength of the contrast, the larger the number the more
3322 % 'threshold-like' it becomes.
3324 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3326 % o exception: return any errors or warnings in this structure.
3329 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3330 const MagickBooleanType sharpen,const double contrast,const double midpoint,
3331 ExceptionInfo *exception)
3333 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3354 Allocate and initialize sigmoidal maps.
3356 assert(image != (Image *) NULL);
3357 assert(image->signature == MagickSignature);
3358 if (image->debug != MagickFalse)
3359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3360 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3361 sizeof(*sigmoidal_map));
3362 if (sigmoidal_map == (MagickRealType *) NULL)
3363 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3365 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3366 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3367 #pragma omp parallel for schedule(static) shared(progress,status) \
3368 dynamic_number_threads(image,image->columns,1,1)
3370 for (i=0; i <= (ssize_t) MaxMap; i++)
3372 if (sharpen != MagickFalse)
3374 #define sigmoidal(a,b,x) (1/(1+exp((a)*((b)-(x)))))
3376 /* Simpilified function scaling,
3377 * with better 'contrast=0' or 'flatline' handling (greyscale)
3380 u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
3381 u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
3382 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3383 (MagickRealType)(MaxMap*(
3384 (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
3385 -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5) ));
3387 /* Scaled sigmoidal formula...
3388 (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
3389 (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
3390 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3391 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3392 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3393 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3394 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3395 (double) QuantumRange)))))+0.5));
3402 http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
3405 min = sigmoidal(contrast,1.0,0.0),
3406 max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
3407 xi = min+(double)i/MaxMap*(max-min);
3408 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
3409 (MagickRealType)(MaxMap*(
3410 QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
3413 /* expanded form of the above */
3414 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3415 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3416 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3417 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3418 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3419 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3420 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3421 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3422 (double) QuantumRange*contrast))))))/contrast)));
3425 if (image->storage_class == PseudoClass)
3428 Sigmoidal-contrast enhance colormap.
3430 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3431 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3432 dynamic_number_threads(image,image->columns,1,1)
3434 for (i=0; i < (ssize_t) image->colors; i++)
3436 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3437 image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
3438 ClampToQuantum(image->colormap[i].red))];
3439 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3440 image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
3441 ClampToQuantum(image->colormap[i].green))];
3442 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3443 image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
3444 ClampToQuantum(image->colormap[i].blue))];
3445 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3446 image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
3447 ClampToQuantum(image->colormap[i].alpha))];
3451 Sigmoidal-contrast enhance image.
3455 image_view=AcquireAuthenticCacheView(image,exception);
3456 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3457 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3458 dynamic_number_threads(image,image->columns,image->rows,1)
3460 for (y=0; y < (ssize_t) image->rows; y++)
3468 if (status == MagickFalse)
3470 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3471 if (q == (Quantum *) NULL)
3476 for (x=0; x < (ssize_t) image->columns; x++)
3481 if (GetPixelMask(image,q) != 0)
3483 q+=GetPixelChannels(image);
3486 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3494 channel=GetPixelChannelMapChannel(image,i);
3495 traits=GetPixelChannelMapTraits(image,channel);
3496 if ((traits & UpdatePixelTrait) == 0)
3498 q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
3500 q+=GetPixelChannels(image);
3502 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3504 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3509 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3510 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3512 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3514 if (proceed == MagickFalse)
3518 image_view=DestroyCacheView(image_view);
3519 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);