2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE N N H H AAA N N CCCC EEEEE %
7 % E NN N H H A A NN N C E %
8 % EEE N N N HHHHH AAAAA N N N C EEE %
9 % E N NN H H A A N NN C E %
10 % EEEEE N N H H A A N N CCCC EEEEE %
13 % MagickCore Image Enhancement Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/composite-private.h"
51 #include "MagickCore/enhance.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/fx.h"
55 #include "MagickCore/gem.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/histogram.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/quantum.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/resample.h"
68 #include "MagickCore/resample-private.h"
69 #include "MagickCore/statistic.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/string-private.h"
72 #include "MagickCore/thread-private.h"
73 #include "MagickCore/token.h"
74 #include "MagickCore/xml-tree.h"
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % A u t o G a m m a I m a g e %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % AutoGammaImage() extract the 'mean' from the image and adjust the image
88 % to try make set its gamma appropriatally.
90 % The format of the AutoGammaImage method is:
92 % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception)
94 % A description of each parameter follows:
96 % o image: The image to auto-level
98 % o exception: return any errors or warnings in this structure.
101 MagickExport MagickBooleanType AutoGammaImage(Image *image,
102 ExceptionInfo *exception)
114 if (image->sync != MagickFalse)
117 Apply gamma correction equally accross all given channels.
119 (void) GetImageMean(image,&mean,&sans,exception);
120 gamma=log(mean*QuantumScale)/log_mean;
121 return(LevelImage(image,0.0,(double) QuantumRange,gamma));
124 Auto-gamma each channel separately.
127 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
129 PushPixelChannelMap(image,RedChannel);
130 (void) GetImageMean(image,&mean,&sans,exception);
131 gamma=log(mean*QuantumScale)/log_mean;
132 status=status && LevelImage(image,0.0,(double) QuantumRange,gamma);
133 PopPixelChannelMap(image);
135 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
137 PushPixelChannelMap(image,GreenChannel);
138 (void) GetImageMean(image,&mean,&sans,exception);
139 gamma=log(mean*QuantumScale)/log_mean;
140 status=status && LevelImage(image,0.0,(double) QuantumRange,gamma);
141 PopPixelChannelMap(image);
143 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
145 PushPixelChannelMap(image,BlueChannel);
146 (void) GetImageMean(image,&mean,&sans,exception);
147 gamma=log(mean*QuantumScale)/log_mean;
148 status=status && LevelImage(image,0.0,(double) QuantumRange,gamma);
149 PopPixelChannelMap(image);
151 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
152 (image->colorspace == CMYKColorspace))
154 PushPixelChannelMap(image,BlackChannel);
155 (void) GetImageMean(image,&mean,&sans,exception);
156 gamma=log(mean*QuantumScale)/log_mean;
157 status=status && LevelImage(image,0.0,(double) QuantumRange,gamma);
158 PopPixelChannelMap(image);
160 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
161 (image->matte == MagickTrue))
163 PushPixelChannelMap(image,AlphaChannel);
164 (void) GetImageMean(image,&mean,&sans,exception);
165 gamma=log(mean*QuantumScale)/log_mean;
166 status=status && LevelImage(image,0.0,(double) QuantumRange,gamma);
167 PopPixelChannelMap(image);
169 return(status != 0 ? MagickTrue : MagickFalse);
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 % A u t o L e v e l I m a g e %
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % AutoLevelImage() adjusts the levels of a particular image channel by
184 % scaling the minimum and maximum values to the full quantum range.
186 % The format of the LevelImage method is:
188 % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception)
190 % A description of each parameter follows:
192 % o image: The image to auto-level
194 % o exception: return any errors or warnings in this structure.
197 MagickExport MagickBooleanType AutoLevelImage(Image *image,
198 ExceptionInfo *exception)
200 return(MinMaxStretchImage(image,0.0,0.0,exception));
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % B r i g h t n e s s C o n t r a s t I m a g e %
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 % BrightnessContrastImage() changes the brightness and/or contrast of an
215 % image. It converts the brightness and contrast parameters into slope and
216 % intercept and calls a polynomical function to apply to the image.
218 % The format of the BrightnessContrastImage method is:
220 % MagickBooleanType BrightnessContrastImage(Image *image,
221 % const double brightness,const double contrast,ExceptionInfo *exception)
223 % A description of each parameter follows:
225 % o image: the image.
227 % o brightness: the brightness percent (-100 .. 100).
229 % o contrast: the contrast percent (-100 .. 100).
231 % o exception: return any errors or warnings in this structure.
234 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
235 const double brightness,const double contrast,ExceptionInfo *exception)
237 #define BrightnessContastImageTag "BrightnessContast/Image"
249 Compute slope and intercept.
251 assert(image != (Image *) NULL);
252 assert(image->signature == MagickSignature);
253 if (image->debug != MagickFalse)
254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
256 slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
259 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
260 coefficients[0]=slope;
261 coefficients[1]=intercept;
262 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % C l u t I m a g e %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % ClutImage() replaces each color value in the given image, by using it as an
278 % index to lookup a replacement color value in a Color Look UP Table in the
279 % form of an image. The values are extracted along a diagonal of the CLUT
280 % image so either a horizontal or vertial gradient image can be used.
282 % Typically this is used to either re-color a gray-scale image according to a
283 % color gradient in the CLUT image, or to perform a freeform histogram
284 % (level) adjustment according to the (typically gray-scale) gradient in the
287 % When the 'channel' mask includes the matte/alpha transparency channel but
288 % one image has no such channel it is assumed that that image is a simple
289 % gray-scale image that will effect the alpha channel values, either for
290 % gray-scale coloring (with transparent or semi-transparent colors), or
291 % a histogram adjustment of existing alpha channel values. If both images
292 % have matte channels, direct and normal indexing is applied, which is rarely
295 % The format of the ClutImage method is:
297 % MagickBooleanType ClutImage(Image *image,Image *clut_image,
298 % ExceptionInfo *exception)
300 % A description of each parameter follows:
302 % o image: the image, which is replaced by indexed CLUT values
304 % o clut_image: the color lookup table image for replacement color values.
306 % o channel: the channel.
308 % o exception: return any errors or warnings in this structure.
311 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
312 ExceptionInfo *exception)
314 #define ClampAlphaPixelChannel(pixel) ClampToQuantum((pixel)->alpha)
315 #define ClampBlackPixelChannel(pixel) ClampToQuantum((pixel)->black)
316 #define ClampBluePixelChannel(pixel) ClampToQuantum((pixel)->blue)
317 #define ClampGreenPixelChannel(pixel) ClampToQuantum((pixel)->green)
318 #define ClampRedPixelChannel(pixel) ClampToQuantum((pixel)->red)
319 #define ClutImageTag "Clut/Image"
341 assert(image != (Image *) NULL);
342 assert(image->signature == MagickSignature);
343 if (image->debug != MagickFalse)
344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
345 assert(clut_image != (Image *) NULL);
346 assert(clut_image->signature == MagickSignature);
347 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
349 clut_map=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
351 if (clut_map == (double *) NULL)
352 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
359 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
360 clut_view=AcquireCacheView(clut_image);
361 #if defined(MAGICKCORE_OPENMP_SUPPORT)
362 #pragma omp parallel for schedule(dynamic,4)
364 for (x=0; x <= (ssize_t) MaxMap; x++)
369 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
370 (void) InterpolatePixelChannel(clut_image,clut_view,(PixelChannel) i,
371 UndefinedInterpolatePixel,QuantumScale*x*(clut_image->columns-adjust),
372 QuantumScale*x*(clut_image->rows-adjust),clut_map+x*
373 GetPixelChannels(clut_image)+i,exception);
375 clut_view=DestroyCacheView(clut_view);
376 image_view=AcquireCacheView(image);
377 #if defined(MAGICKCORE_OPENMP_SUPPORT)
378 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
380 for (y=0; y < (ssize_t) image->rows; y++)
388 if (status == MagickFalse)
390 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
391 if (q == (Quantum *) NULL)
396 for (x=0; x < (ssize_t) image->columns; x++)
401 for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
410 clut_traits=GetPixelChannelMapTraits(clut_image,(PixelChannel) i);
411 if (clut_traits == UndefinedPixelTrait)
413 channel=GetPixelChannelMapChannel(clut_image,(PixelChannel) i);
414 traits=GetPixelChannelMapTraits(clut_image,channel);
415 if (traits == UndefinedPixelTrait)
417 if ((traits & UpdatePixelTrait) == 0)
419 q[channel]=ClampToQuantum(clut_map[ScaleQuantumToMap(q[channel])*
420 GetPixelChannels(clut_image)+channel]);
422 q+=GetPixelChannels(image);
424 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
431 #if defined(MAGICKCORE_OPENMP_SUPPORT)
432 #pragma omp critical (MagickCore_ClutImage)
434 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
435 if (proceed == MagickFalse)
439 image_view=DestroyCacheView(image_view);
440 clut_map=(double *) RelinquishMagickMemory(clut_map);
441 if ((clut_image->matte != MagickFalse) &&
442 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
443 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % C o l o r D e c i s i o n L i s t I m a g e %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
459 % (CCC) file which solely contains one or more color corrections and applies
460 % the correction to the image. Here is a sample CCC file:
462 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
463 % <ColorCorrection id="cc03345">
465 % <Slope> 0.9 1.2 0.5 </Slope>
466 % <Offset> 0.4 -0.5 0.6 </Offset>
467 % <Power> 1.0 0.8 1.5 </Power>
470 % <Saturation> 0.85 </Saturation>
473 % </ColorCorrectionCollection>
475 % which includes the slop, offset, and power for each of the RGB channels
476 % as well as the saturation.
478 % The format of the ColorDecisionListImage method is:
480 % MagickBooleanType ColorDecisionListImage(Image *image,
481 % const char *color_correction_collection)
483 % A description of each parameter follows:
485 % o image: the image.
487 % o color_correction_collection: the color correction collection in XML.
490 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
491 const char *color_correction_collection)
493 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
495 typedef struct _Correction
503 typedef struct _ColorCorrection
518 token[MaxTextExtent];
552 Allocate and initialize cdl maps.
554 assert(image != (Image *) NULL);
555 assert(image->signature == MagickSignature);
556 if (image->debug != MagickFalse)
557 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
558 if (color_correction_collection == (const char *) NULL)
560 ccc=NewXMLTree((const char *) color_correction_collection,&image->exception);
561 if (ccc == (XMLTreeInfo *) NULL)
563 cc=GetXMLTreeChild(ccc,"ColorCorrection");
564 if (cc == (XMLTreeInfo *) NULL)
566 ccc=DestroyXMLTree(ccc);
569 color_correction.red.slope=1.0;
570 color_correction.red.offset=0.0;
571 color_correction.red.power=1.0;
572 color_correction.green.slope=1.0;
573 color_correction.green.offset=0.0;
574 color_correction.green.power=1.0;
575 color_correction.blue.slope=1.0;
576 color_correction.blue.offset=0.0;
577 color_correction.blue.power=1.0;
578 color_correction.saturation=0.0;
579 sop=GetXMLTreeChild(cc,"SOPNode");
580 if (sop != (XMLTreeInfo *) NULL)
587 slope=GetXMLTreeChild(sop,"Slope");
588 if (slope != (XMLTreeInfo *) NULL)
590 content=GetXMLTreeContent(slope);
591 p=(const char *) content;
592 for (i=0; (*p != '\0') && (i < 3); i++)
594 GetMagickToken(p,&p,token);
596 GetMagickToken(p,&p,token);
601 color_correction.red.slope=InterpretLocaleValue(token,
607 color_correction.green.slope=InterpretLocaleValue(token,
613 color_correction.blue.slope=InterpretLocaleValue(token,
620 offset=GetXMLTreeChild(sop,"Offset");
621 if (offset != (XMLTreeInfo *) NULL)
623 content=GetXMLTreeContent(offset);
624 p=(const char *) content;
625 for (i=0; (*p != '\0') && (i < 3); i++)
627 GetMagickToken(p,&p,token);
629 GetMagickToken(p,&p,token);
634 color_correction.red.offset=InterpretLocaleValue(token,
640 color_correction.green.offset=InterpretLocaleValue(token,
646 color_correction.blue.offset=InterpretLocaleValue(token,
653 power=GetXMLTreeChild(sop,"Power");
654 if (power != (XMLTreeInfo *) NULL)
656 content=GetXMLTreeContent(power);
657 p=(const char *) content;
658 for (i=0; (*p != '\0') && (i < 3); i++)
660 GetMagickToken(p,&p,token);
662 GetMagickToken(p,&p,token);
667 color_correction.red.power=InterpretLocaleValue(token,
673 color_correction.green.power=InterpretLocaleValue(token,
679 color_correction.blue.power=InterpretLocaleValue(token,
687 sat=GetXMLTreeChild(cc,"SATNode");
688 if (sat != (XMLTreeInfo *) NULL)
693 saturation=GetXMLTreeChild(sat,"Saturation");
694 if (saturation != (XMLTreeInfo *) NULL)
696 content=GetXMLTreeContent(saturation);
697 p=(const char *) content;
698 GetMagickToken(p,&p,token);
699 color_correction.saturation=InterpretLocaleValue(token,
703 ccc=DestroyXMLTree(ccc);
704 if (image->debug != MagickFalse)
706 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
707 " Color Correction Collection:");
708 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
709 " color_correction.red.slope: %g",color_correction.red.slope);
710 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
711 " color_correction.red.offset: %g",color_correction.red.offset);
712 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
713 " color_correction.red.power: %g",color_correction.red.power);
714 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
715 " color_correction.green.slope: %g",color_correction.green.slope);
716 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
717 " color_correction.green.offset: %g",color_correction.green.offset);
718 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
719 " color_correction.green.power: %g",color_correction.green.power);
720 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
721 " color_correction.blue.slope: %g",color_correction.blue.slope);
722 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
723 " color_correction.blue.offset: %g",color_correction.blue.offset);
724 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
725 " color_correction.blue.power: %g",color_correction.blue.power);
726 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
727 " color_correction.saturation: %g",color_correction.saturation);
729 cdl_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
730 if (cdl_map == (PixelPacket *) NULL)
731 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
733 #if defined(MAGICKCORE_OPENMP_SUPPORT)
734 #pragma omp parallel for schedule(dynamic,4)
736 for (i=0; i <= (ssize_t) MaxMap; i++)
738 cdl_map[i].red=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
739 MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
740 color_correction.red.offset,color_correction.red.power)))));
741 cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
742 MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
743 color_correction.green.offset,color_correction.green.power)))));
744 cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
745 MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
746 color_correction.blue.offset,color_correction.blue.power)))));
748 if (image->storage_class == PseudoClass)
751 Apply transfer function to colormap.
753 #if defined(MAGICKCORE_OPENMP_SUPPORT)
754 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
756 for (i=0; i < (ssize_t) image->colors; i++)
761 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
762 0.0722*image->colormap[i].blue;
763 image->colormap[i].red=ClampToQuantum(luma+color_correction.saturation*
764 cdl_map[ScaleQuantumToMap(image->colormap[i].red)].red-luma);
765 image->colormap[i].green=ClampToQuantum(luma+
766 color_correction.saturation*cdl_map[ScaleQuantumToMap(
767 image->colormap[i].green)].green-luma);
768 image->colormap[i].blue=ClampToQuantum(luma+color_correction.saturation*
769 cdl_map[ScaleQuantumToMap(image->colormap[i].blue)].blue-luma);
773 Apply transfer function to image.
777 exception=(&image->exception);
778 image_view=AcquireCacheView(image);
779 #if defined(MAGICKCORE_OPENMP_SUPPORT)
780 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
782 for (y=0; y < (ssize_t) image->rows; y++)
793 if (status == MagickFalse)
795 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
796 if (q == (const Quantum *) NULL)
801 for (x=0; x < (ssize_t) image->columns; x++)
803 luma=0.2126*GetPixelRed(image,q)+0.7152*GetPixelGreen(image,q)+0.0722*
804 GetPixelBlue(image,q);
805 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
806 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
807 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
808 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
809 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
810 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
811 q+=GetPixelChannels(image);
813 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
815 if (image->progress_monitor != (MagickProgressMonitor) NULL)
820 #if defined(MAGICKCORE_OPENMP_SUPPORT)
821 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
823 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
824 progress++,image->rows);
825 if (proceed == MagickFalse)
829 image_view=DestroyCacheView(image_view);
830 cdl_map=(PixelPacket *) RelinquishMagickMemory(cdl_map);
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 % C o n t r a s t I m a g e %
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 % ContrastImage() enhances the intensity differences between the lighter and
846 % darker elements of the image. Set sharpen to a MagickTrue to increase the
847 % image contrast otherwise the contrast is reduced.
849 % The format of the ContrastImage method is:
851 % MagickBooleanType ContrastImage(Image *image,
852 % const MagickBooleanType sharpen)
854 % A description of each parameter follows:
856 % o image: the image.
858 % o sharpen: Increase or decrease image contrast.
862 static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
870 Enhance contrast: dark color become darker, light color become lighter.
872 assert(red != (Quantum *) NULL);
873 assert(green != (Quantum *) NULL);
874 assert(blue != (Quantum *) NULL);
878 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
879 brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
881 if (brightness > 1.0)
884 if (brightness < 0.0)
886 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
889 MagickExport MagickBooleanType ContrastImage(Image *image,
890 const MagickBooleanType sharpen)
892 #define ContrastImageTag "Contrast/Image"
915 assert(image != (Image *) NULL);
916 assert(image->signature == MagickSignature);
917 if (image->debug != MagickFalse)
918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
919 sign=sharpen != MagickFalse ? 1 : -1;
920 if (image->storage_class == PseudoClass)
923 Contrast enhance colormap.
925 for (i=0; i < (ssize_t) image->colors; i++)
926 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
927 &image->colormap[i].blue);
930 Contrast enhance image.
934 exception=(&image->exception);
935 image_view=AcquireCacheView(image);
936 #if defined(MAGICKCORE_OPENMP_SUPPORT)
937 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
939 for (y=0; y < (ssize_t) image->rows; y++)
952 if (status == MagickFalse)
954 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
955 if (q == (const Quantum *) NULL)
960 for (x=0; x < (ssize_t) image->columns; x++)
962 red=GetPixelRed(image,q);
963 green=GetPixelGreen(image,q);
964 blue=GetPixelBlue(image,q);
965 Contrast(sign,&red,&green,&blue);
966 SetPixelRed(image,red,q);
967 SetPixelGreen(image,green,q);
968 SetPixelBlue(image,blue,q);
969 q+=GetPixelChannels(image);
971 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
973 if (image->progress_monitor != (MagickProgressMonitor) NULL)
978 #if defined(MAGICKCORE_OPENMP_SUPPORT)
979 #pragma omp critical (MagickCore_ContrastImage)
981 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
982 if (proceed == MagickFalse)
986 image_view=DestroyCacheView(image_view);
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % C o n t r a s t S t r e t c h I m a g e %
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001 % ContrastStretchImage() is a simple image enhancement technique that attempts
1002 % to improve the contrast in an image by `stretching' the range of intensity
1003 % values it contains to span a desired range of values. It differs from the
1004 % more sophisticated histogram equalization in that it can only apply a
1005 % linear scaling function to the image pixel values. As a result the
1006 % `enhancement' is less harsh.
1008 % The format of the ContrastStretchImage method is:
1010 % MagickBooleanType ContrastStretchImage(Image *image,
1011 % const char *levels)
1013 % A description of each parameter follows:
1015 % o image: the image.
1017 % o black_point: the black point.
1019 % o white_point: the white point.
1021 % o levels: Specify the levels where the black and white points have the
1022 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
1025 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1026 const double black_point,const double white_point)
1028 #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1029 #define ContrastStretchImageTag "ContrastStretch/Image"
1059 Allocate histogram and stretch map.
1061 assert(image != (Image *) NULL);
1062 assert(image->signature == MagickSignature);
1063 if (image->debug != MagickFalse)
1064 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1065 histogram=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
1066 sizeof(*histogram));
1067 stretch_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
1068 sizeof(*stretch_map));
1069 if ((histogram == (PixelInfo *) NULL) ||
1070 (stretch_map == (PixelInfo *) NULL))
1071 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1077 exception=(&image->exception);
1078 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
1079 image_view=AcquireCacheView(image);
1080 for (y=0; y < (ssize_t) image->rows; y++)
1082 register const Quantum
1088 if (status == MagickFalse)
1090 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1091 if (p == (const Quantum *) NULL)
1096 for (x=0; x < (ssize_t) image->columns; x++)
1098 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1099 histogram[ScaleQuantumToMap(GetPixelRed(image,p))].red++;
1100 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1101 histogram[ScaleQuantumToMap(GetPixelGreen(image,p))].green++;
1102 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1103 histogram[ScaleQuantumToMap(GetPixelBlue(image,p))].blue++;
1104 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1105 (image->colorspace == CMYKColorspace))
1106 histogram[ScaleQuantumToMap(GetPixelBlack(image,p))].black++;
1107 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1108 histogram[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha++;
1109 p+=GetPixelChannels(image);
1113 Find the histogram boundaries by locating the black/white levels.
1116 white.red=MaxRange(QuantumRange);
1117 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1120 for (i=0; i <= (ssize_t) MaxMap; i++)
1122 intensity+=histogram[i].red;
1123 if (intensity > black_point)
1126 black.red=(MagickRealType) i;
1128 for (i=(ssize_t) MaxMap; i != 0; i--)
1130 intensity+=histogram[i].red;
1131 if (intensity > ((double) image->columns*image->rows-white_point))
1134 white.red=(MagickRealType) i;
1137 white.green=MaxRange(QuantumRange);
1138 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1141 for (i=0; i <= (ssize_t) MaxMap; i++)
1143 intensity+=histogram[i].green;
1144 if (intensity > black_point)
1147 black.green=(MagickRealType) i;
1149 for (i=(ssize_t) MaxMap; i != 0; i--)
1151 intensity+=histogram[i].green;
1152 if (intensity > ((double) image->columns*image->rows-white_point))
1155 white.green=(MagickRealType) i;
1158 white.blue=MaxRange(QuantumRange);
1159 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1162 for (i=0; i <= (ssize_t) MaxMap; i++)
1164 intensity+=histogram[i].blue;
1165 if (intensity > black_point)
1168 black.blue=(MagickRealType) i;
1170 for (i=(ssize_t) MaxMap; i != 0; i--)
1172 intensity+=histogram[i].blue;
1173 if (intensity > ((double) image->columns*image->rows-white_point))
1176 white.blue=(MagickRealType) i;
1179 white.alpha=MaxRange(QuantumRange);
1180 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1183 for (i=0; i <= (ssize_t) MaxMap; i++)
1185 intensity+=histogram[i].alpha;
1186 if (intensity > black_point)
1189 black.alpha=(MagickRealType) i;
1191 for (i=(ssize_t) MaxMap; i != 0; i--)
1193 intensity+=histogram[i].alpha;
1194 if (intensity > ((double) image->columns*image->rows-white_point))
1197 white.alpha=(MagickRealType) i;
1200 white.black=MaxRange(QuantumRange);
1201 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && (image->colorspace == CMYKColorspace))
1204 for (i=0; i <= (ssize_t) MaxMap; i++)
1206 intensity+=histogram[i].black;
1207 if (intensity > black_point)
1210 black.black=(MagickRealType) i;
1212 for (i=(ssize_t) MaxMap; i != 0; i--)
1214 intensity+=histogram[i].black;
1215 if (intensity > ((double) image->columns*image->rows-white_point))
1218 white.black=(MagickRealType) i;
1220 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
1222 Stretch the histogram to create the stretched image mapping.
1224 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map));
1225 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1226 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1228 for (i=0; i <= (ssize_t) MaxMap; i++)
1230 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1232 if (i < (ssize_t) black.red)
1233 stretch_map[i].red=0.0;
1235 if (i > (ssize_t) white.red)
1236 stretch_map[i].red=(MagickRealType) QuantumRange;
1238 if (black.red != white.red)
1239 stretch_map[i].red=(MagickRealType) ScaleMapToQuantum(
1240 (MagickRealType) (MaxMap*(i-black.red)/(white.red-black.red)));
1242 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1244 if (i < (ssize_t) black.green)
1245 stretch_map[i].green=0.0;
1247 if (i > (ssize_t) white.green)
1248 stretch_map[i].green=(MagickRealType) QuantumRange;
1250 if (black.green != white.green)
1251 stretch_map[i].green=(MagickRealType) ScaleMapToQuantum(
1252 (MagickRealType) (MaxMap*(i-black.green)/(white.green-
1255 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1257 if (i < (ssize_t) black.blue)
1258 stretch_map[i].blue=0.0;
1260 if (i > (ssize_t) white.blue)
1261 stretch_map[i].blue=(MagickRealType) QuantumRange;
1263 if (black.blue != white.blue)
1264 stretch_map[i].blue=(MagickRealType) ScaleMapToQuantum(
1265 (MagickRealType) (MaxMap*(i-black.blue)/(white.blue-
1268 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1270 if (i < (ssize_t) black.alpha)
1271 stretch_map[i].alpha=0.0;
1273 if (i > (ssize_t) white.alpha)
1274 stretch_map[i].alpha=(MagickRealType) QuantumRange;
1276 if (black.alpha != white.alpha)
1277 stretch_map[i].alpha=(MagickRealType) ScaleMapToQuantum(
1278 (MagickRealType) (MaxMap*(i-black.alpha)/(white.alpha-
1281 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1282 (image->colorspace == CMYKColorspace))
1284 if (i < (ssize_t) black.black)
1285 stretch_map[i].black=0.0;
1287 if (i > (ssize_t) white.black)
1288 stretch_map[i].black=(MagickRealType) QuantumRange;
1290 if (black.black != white.black)
1291 stretch_map[i].black=(MagickRealType) ScaleMapToQuantum(
1292 (MagickRealType) (MaxMap*(i-black.black)/(white.black-
1299 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) || (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1300 (image->colorspace == CMYKColorspace)))
1301 image->storage_class=DirectClass;
1302 if (image->storage_class == PseudoClass)
1307 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1308 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1310 for (i=0; i < (ssize_t) image->colors; i++)
1312 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1314 if (black.red != white.red)
1315 image->colormap[i].red=ClampToQuantum(stretch_map[
1316 ScaleQuantumToMap(image->colormap[i].red)].red);
1318 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1320 if (black.green != white.green)
1321 image->colormap[i].green=ClampToQuantum(stretch_map[
1322 ScaleQuantumToMap(image->colormap[i].green)].green);
1324 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1326 if (black.blue != white.blue)
1327 image->colormap[i].blue=ClampToQuantum(stretch_map[
1328 ScaleQuantumToMap(image->colormap[i].blue)].blue);
1330 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1332 if (black.alpha != white.alpha)
1333 image->colormap[i].alpha=ClampToQuantum(stretch_map[
1334 ScaleQuantumToMap(image->colormap[i].alpha)].alpha);
1343 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1344 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1346 for (y=0; y < (ssize_t) image->rows; y++)
1354 if (status == MagickFalse)
1356 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1357 if (q == (const Quantum *) NULL)
1362 for (x=0; x < (ssize_t) image->columns; x++)
1364 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1366 if (black.red != white.red)
1367 SetPixelRed(image,ClampToQuantum(stretch_map[ScaleQuantumToMap(
1368 GetPixelRed(image,q))].red),q);
1370 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1372 if (black.green != white.green)
1373 SetPixelGreen(image,ClampToQuantum(stretch_map[ScaleQuantumToMap(
1374 GetPixelGreen(image,q))].green),q);
1376 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1378 if (black.blue != white.blue)
1379 SetPixelBlue(image,ClampToQuantum(stretch_map[ScaleQuantumToMap(
1380 GetPixelBlue(image,q))].blue),q);
1382 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1383 (image->colorspace == CMYKColorspace))
1385 if (black.black != white.black)
1386 SetPixelBlack(image,ClampToQuantum(stretch_map[ScaleQuantumToMap(
1387 GetPixelBlack(image,q))].black),q);
1389 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1391 if (black.alpha != white.alpha)
1392 SetPixelAlpha(image,ClampToQuantum(stretch_map[ScaleQuantumToMap(
1393 GetPixelAlpha(image,q))].alpha),q);
1395 q+=GetPixelChannels(image);
1397 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1399 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1404 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1405 #pragma omp critical (MagickCore_ContrastStretchImage)
1407 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1409 if (proceed == MagickFalse)
1413 image_view=DestroyCacheView(image_view);
1414 stretch_map=(PixelInfo *) RelinquishMagickMemory(stretch_map);
1419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423 % E n h a n c e I m a g e %
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429 % EnhanceImage() applies a digital filter that improves the quality of a
1432 % The format of the EnhanceImage method is:
1434 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1436 % A description of each parameter follows:
1438 % o image: the image.
1440 % o exception: return any errors or warnings in this structure.
1443 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1445 #define Enhance(weight) \
1446 mean=((MagickRealType) GetPixelRed(image,r)+pixel.red)/2; \
1447 distance=(MagickRealType) GetPixelRed(image,r)-(MagickRealType) pixel.red; \
1448 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1449 mean)*distance*distance; \
1450 mean=((MagickRealType) GetPixelGreen(image,r)+pixel.green)/2; \
1451 distance=(MagickRealType) GetPixelGreen(image,r)- \
1452 (MagickRealType) pixel.green; \
1453 distance_squared+=4.0*distance*distance; \
1454 mean=((MagickRealType) GetPixelBlue(image,r)+pixel.blue)/2; \
1455 distance=(MagickRealType) GetPixelBlue(image,r)- \
1456 (MagickRealType) pixel.blue; \
1457 distance_squared+=QuantumScale*(3.0*((MagickRealType) \
1458 QuantumRange+1.0)-1.0-mean)*distance*distance; \
1459 mean=((MagickRealType) GetPixelAlpha(image,r)+pixel.alpha)/2; \
1460 distance=(MagickRealType) GetPixelAlpha(image,r)-(MagickRealType) pixel.alpha; \
1461 distance_squared+=QuantumScale*(3.0*((MagickRealType) \
1462 QuantumRange+1.0)-1.0-mean)*distance*distance; \
1463 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1464 QuantumRange/25.0f)) \
1466 aggregate.red+=(weight)*GetPixelRed(image,r); \
1467 aggregate.green+=(weight)*GetPixelGreen(image,r); \
1468 aggregate.blue+=(weight)*GetPixelBlue(image,r); \
1469 aggregate.alpha+=(weight)*GetPixelAlpha(image,r); \
1470 total_weight+=(weight); \
1473 #define EnhanceImageTag "Enhance/Image"
1495 Initialize enhanced image attributes.
1497 assert(image != (const Image *) NULL);
1498 assert(image->signature == MagickSignature);
1499 if (image->debug != MagickFalse)
1500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1501 assert(exception != (ExceptionInfo *) NULL);
1502 assert(exception->signature == MagickSignature);
1503 if ((image->columns < 5) || (image->rows < 5))
1504 return((Image *) NULL);
1505 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1507 if (enhance_image == (Image *) NULL)
1508 return((Image *) NULL);
1509 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1511 enhance_image=DestroyImage(enhance_image);
1512 return((Image *) NULL);
1519 (void) ResetMagickMemory(&zero,0,sizeof(zero));
1520 image_view=AcquireCacheView(image);
1521 enhance_view=AcquireCacheView(enhance_image);
1522 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1523 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1525 for (y=0; y < (ssize_t) image->rows; y++)
1527 register const Quantum
1537 Read another scan line.
1539 if (status == MagickFalse)
1541 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1542 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1544 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1549 for (x=0; x < (ssize_t) image->columns; x++)
1563 register const Quantum
1567 Compute weighted average of target pixel color components.
1571 r=p+2*(image->columns+4)+2;
1572 GetPixelPacket(image,r,&pixel);
1574 Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0);
1575 r=p+(image->columns+4);
1576 Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0);
1577 r=p+2*(image->columns+4);
1578 Enhance(10.0); Enhance(40.0); Enhance(80.0); Enhance(40.0); Enhance(10.0);
1579 r=p+3*(image->columns+4);
1580 Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0);
1581 r=p+4*(image->columns+4);
1582 Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0);
1583 SetPixelRed(enhance_image,(Quantum) ((aggregate.red+
1584 (total_weight/2)-1)/total_weight),q);
1585 SetPixelGreen(enhance_image,(Quantum) ((aggregate.green+
1586 (total_weight/2)-1)/total_weight),q);
1587 SetPixelBlue(enhance_image,(Quantum) ((aggregate.blue+
1588 (total_weight/2)-1)/total_weight),q);
1589 SetPixelAlpha(enhance_image,(Quantum) ((aggregate.alpha+
1590 (total_weight/2)-1)/total_weight),q);
1591 p+=GetPixelChannels(image);
1592 q+=GetPixelChannels(enhance_image);
1594 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1596 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1601 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1602 #pragma omp critical (MagickCore_EnhanceImage)
1604 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1605 if (proceed == MagickFalse)
1609 enhance_view=DestroyCacheView(enhance_view);
1610 image_view=DestroyCacheView(image_view);
1611 return(enhance_image);
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 % E q u a l i z e I m a g e %
1623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1625 % EqualizeImage() applies a histogram equalization to the image.
1627 % The format of the EqualizeImage method is:
1629 % MagickBooleanType EqualizeImage(Image *image)
1631 % A description of each parameter follows:
1633 % o image: the image.
1635 % o channel: the channel.
1638 MagickExport MagickBooleanType EqualizeImage(Image *image)
1640 #define EqualizeImageTag "Equalize/Image"
1669 Allocate and initialize histogram arrays.
1671 assert(image != (Image *) NULL);
1672 assert(image->signature == MagickSignature);
1673 if (image->debug != MagickFalse)
1674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1675 equalize_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
1676 sizeof(*equalize_map));
1677 histogram=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
1678 sizeof(*histogram));
1679 map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
1680 if ((equalize_map == (PixelInfo *) NULL) ||
1681 (histogram == (PixelInfo *) NULL) ||
1682 (map == (PixelInfo *) NULL))
1684 if (map != (PixelInfo *) NULL)
1685 map=(PixelInfo *) RelinquishMagickMemory(map);
1686 if (histogram != (PixelInfo *) NULL)
1687 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
1688 if (equalize_map != (PixelInfo *) NULL)
1689 equalize_map=(PixelInfo *) RelinquishMagickMemory(equalize_map);
1690 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1696 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
1697 exception=(&image->exception);
1698 for (y=0; y < (ssize_t) image->rows; y++)
1700 register const Quantum
1706 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1707 if (p == (const Quantum *) NULL)
1709 for (x=0; x < (ssize_t) image->columns; x++)
1711 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1712 histogram[ScaleQuantumToMap(GetPixelRed(image,p))].red++;
1713 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1714 histogram[ScaleQuantumToMap(GetPixelGreen(image,p))].green++;
1715 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1716 histogram[ScaleQuantumToMap(GetPixelBlue(image,p))].blue++;
1717 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1718 (image->colorspace == CMYKColorspace))
1719 histogram[ScaleQuantumToMap(GetPixelBlack(image,p))].black++;
1720 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1721 histogram[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha++;
1722 p+=GetPixelChannels(image);
1726 Integrate the histogram to get the equalization map.
1728 (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
1729 for (i=0; i <= (ssize_t) MaxMap; i++)
1731 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1732 intensity.red+=histogram[i].red;
1733 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1734 intensity.green+=histogram[i].green;
1735 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1736 intensity.blue+=histogram[i].blue;
1737 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1738 (image->colorspace == CMYKColorspace))
1739 intensity.black+=histogram[i].black;
1740 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1741 intensity.alpha+=histogram[i].alpha;
1745 white=map[(int) MaxMap];
1746 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
1747 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1748 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1750 for (i=0; i <= (ssize_t) MaxMap; i++)
1752 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1753 (white.red != black.red))
1754 equalize_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1755 ((MaxMap*(map[i].red-black.red))/(white.red-black.red)));
1756 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1757 (white.green != black.green))
1758 equalize_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1759 ((MaxMap*(map[i].green-black.green))/(white.green-black.green)));
1760 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1761 (white.blue != black.blue))
1762 equalize_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1763 ((MaxMap*(map[i].blue-black.blue))/(white.blue-black.blue)));
1764 if ((((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1765 (image->colorspace == CMYKColorspace)) &&
1766 (white.black != black.black))
1767 equalize_map[i].black=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1768 ((MaxMap*(map[i].black-black.black))/(white.black-black.black)));
1769 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1770 (white.alpha != black.alpha))
1771 equalize_map[i].alpha=(MagickRealType) ScaleMapToQuantum(
1772 (MagickRealType) ((MaxMap*(map[i].alpha-black.alpha))/
1773 (white.alpha-black.alpha)));
1775 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
1776 map=(PixelInfo *) RelinquishMagickMemory(map);
1777 if (image->storage_class == PseudoClass)
1782 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1783 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1785 for (i=0; i < (ssize_t) image->colors; i++)
1787 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1788 (white.red != black.red))
1789 image->colormap[i].red=ClampToQuantum(equalize_map[
1790 ScaleQuantumToMap(image->colormap[i].red)].red);
1791 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1792 (white.green != black.green))
1793 image->colormap[i].green=ClampToQuantum(equalize_map[
1794 ScaleQuantumToMap(image->colormap[i].green)].green);
1795 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1796 (white.blue != black.blue))
1797 image->colormap[i].blue=ClampToQuantum(equalize_map[
1798 ScaleQuantumToMap(image->colormap[i].blue)].blue);
1799 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1800 (white.alpha != black.alpha))
1801 image->colormap[i].alpha=ClampToQuantum(equalize_map[
1802 ScaleQuantumToMap(image->colormap[i].alpha)].alpha);
1810 exception=(&image->exception);
1811 image_view=AcquireCacheView(image);
1812 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1813 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1815 for (y=0; y < (ssize_t) image->rows; y++)
1823 if (status == MagickFalse)
1825 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1826 if (q == (const Quantum *) NULL)
1831 for (x=0; x < (ssize_t) image->columns; x++)
1833 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
1834 (white.red != black.red))
1835 SetPixelRed(image,ClampToQuantum(equalize_map[
1836 ScaleQuantumToMap(GetPixelRed(image,q))].red),q);
1837 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
1838 (white.green != black.green))
1839 SetPixelGreen(image,ClampToQuantum(equalize_map[
1840 ScaleQuantumToMap(GetPixelGreen(image,q))].green),q);
1841 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
1842 (white.blue != black.blue))
1843 SetPixelBlue(image,ClampToQuantum(equalize_map[
1844 ScaleQuantumToMap(GetPixelBlue(image,q))].blue),q);
1845 if ((((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1846 (image->colorspace == CMYKColorspace)) &&
1847 (white.black != black.black))
1848 SetPixelBlack(image,ClampToQuantum(equalize_map[
1849 ScaleQuantumToMap(GetPixelBlack(image,q))].black),q);
1850 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
1851 (white.alpha != black.alpha))
1852 SetPixelAlpha(image,ClampToQuantum(equalize_map[
1853 ScaleQuantumToMap(GetPixelAlpha(image,q))].alpha),q);
1854 q+=GetPixelChannels(image);
1856 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1858 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1863 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1864 #pragma omp critical (MagickCore_EqualizeImage)
1866 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1867 if (proceed == MagickFalse)
1871 image_view=DestroyCacheView(image_view);
1872 equalize_map=(PixelInfo *) RelinquishMagickMemory(equalize_map);
1877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1881 % G a m m a I m a g e %
1885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887 % GammaImage() gamma-corrects a particular image channel. The same
1888 % image viewed on different devices will have perceptual differences in the
1889 % way the image's intensities are represented on the screen. Specify
1890 % individual gamma levels for the red, green, and blue channels, or adjust
1891 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1893 % You can also reduce the influence of a particular channel with a gamma
1896 % The format of the GammaImage method is:
1898 % MagickBooleanType GammaImage(Image *image,const double gamma,
1899 % ExceptionInfo *exception)
1901 % A description of each parameter follows:
1903 % o image: the image.
1905 % o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
1907 % o gamma: the image gamma.
1910 MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
1911 ExceptionInfo *exception)
1913 #define GammaCorrectImageTag "GammaCorrect/Image"
1934 Allocate and initialize gamma maps.
1936 assert(image != (Image *) NULL);
1937 assert(image->signature == MagickSignature);
1938 if (image->debug != MagickFalse)
1939 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1942 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
1943 if (gamma_map == (Quantum *) NULL)
1944 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1946 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
1948 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
1949 #pragma omp parallel for
1951 for (i=0; i <= (ssize_t) MaxMap; i++)
1952 gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
1953 MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
1954 if (image->storage_class == PseudoClass)
1957 Gamma-correct colormap.
1959 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1960 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1962 for (i=0; i < (ssize_t) image->colors; i++)
1964 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1965 image->colormap[i].red=gamma_map[
1966 ScaleQuantumToMap(image->colormap[i].red)];
1967 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1968 image->colormap[i].green=gamma_map[
1969 ScaleQuantumToMap(image->colormap[i].green)];
1970 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1971 image->colormap[i].blue=gamma_map[
1972 ScaleQuantumToMap(image->colormap[i].blue)];
1973 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1974 image->colormap[i].alpha=gamma_map[
1975 ScaleQuantumToMap(image->colormap[i].alpha)];
1979 Gamma-correct image.
1983 image_view=AcquireCacheView(image);
1984 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1985 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1987 for (y=0; y < (ssize_t) image->rows; y++)
1995 if (status == MagickFalse)
1997 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1998 if (q == (const Quantum *) NULL)
2003 for (x=0; x < (ssize_t) image->columns; x++)
2008 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2013 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2014 if ((traits & UpdatePixelTrait) != 0)
2015 q[i]=gamma_map[ScaleQuantumToMap(q[i])];
2017 q+=GetPixelChannels(image);
2019 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2021 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2026 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2027 #pragma omp critical (MagickCore_GammaImage)
2029 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
2031 if (proceed == MagickFalse)
2035 image_view=DestroyCacheView(image_view);
2036 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
2037 if (image->gamma != 0.0)
2038 image->gamma*=gamma;
2043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047 % H a l d C l u t I m a g e %
2051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2053 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
2054 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
2055 % Create it with the HALD coder. You can apply any color transformation to
2056 % the Hald image and then use this method to apply the transform to the
2059 % The format of the HaldClutImage method is:
2061 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image)
2063 % A description of each parameter follows:
2065 % o image: the image, which is replaced by indexed CLUT values
2067 % o hald_image: the color lookup table image for replacement color values.
2071 static inline size_t MagickMin(const size_t x,const size_t y)
2078 MagickExport MagickBooleanType HaldClutImage(Image *image,
2079 const Image *hald_image)
2081 #define HaldClutImageTag "Clut/Image"
2083 typedef struct _HaldInfo
2118 assert(image != (Image *) NULL);
2119 assert(image->signature == MagickSignature);
2120 if (image->debug != MagickFalse)
2121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2122 assert(hald_image != (Image *) NULL);
2123 assert(hald_image->signature == MagickSignature);
2124 exception=(&image->exception);
2125 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2126 return(MagickFalse);
2127 if (image->matte == MagickFalse)
2128 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2134 length=MagickMin(hald_image->columns,hald_image->rows);
2135 for (level=2; (level*level*level) < length; level++) ;
2137 cube_size=level*level;
2138 width=(double) hald_image->columns;
2139 GetPixelInfo(hald_image,&zero);
2140 image_view=AcquireCacheView(image);
2141 hald_view=AcquireCacheView(hald_image);
2142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2143 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2145 for (y=0; y < (ssize_t) image->rows; y++)
2166 if (status == MagickFalse)
2168 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2169 if (q == (const Quantum *) NULL)
2179 for (x=0; x < (ssize_t) image->columns; x++)
2181 point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
2182 point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
2183 point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
2184 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2185 point.x-=floor(point.x);
2186 point.y-=floor(point.y);
2187 point.z-=floor(point.z);
2188 (void) InterpolatePixelInfo(image,hald_view,
2189 UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
2191 (void) InterpolatePixelInfo(image,hald_view,
2192 UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
2193 width),&pixel2,exception);
2194 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,
2195 pixel2.alpha,point.y,&pixel3);
2197 (void) InterpolatePixelInfo(image,hald_view,
2198 UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
2200 (void) InterpolatePixelInfo(image,hald_view,
2201 UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
2202 width),&pixel2,exception);
2203 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,
2204 pixel2.alpha,point.y,&pixel4);
2205 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,
2206 pixel4.alpha,point.z,&pixel);
2207 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2209 ClampToQuantum(pixel.red),q);
2210 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2211 SetPixelGreen(image,
2212 ClampToQuantum(pixel.green),q);
2213 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2215 ClampToQuantum(pixel.blue),q);
2216 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2217 (image->colorspace == CMYKColorspace))
2218 SetPixelBlack(image,
2219 ClampToQuantum(pixel.black),q);
2220 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && (image->matte != MagickFalse))
2221 SetPixelAlpha(image,
2222 ClampToQuantum(pixel.alpha),q);
2223 q+=GetPixelChannels(image);
2225 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2227 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2232 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2233 #pragma omp critical (MagickCore_HaldClutImage)
2235 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2236 if (proceed == MagickFalse)
2240 hald_view=DestroyCacheView(hald_view);
2241 image_view=DestroyCacheView(image_view);
2246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2250 % L e v e l I m a g e %
2254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2256 % LevelImage() adjusts the levels of a particular image channel by
2257 % scaling the colors falling between specified white and black points to
2258 % the full available quantum range.
2260 % The parameters provided represent the black, and white points. The black
2261 % point specifies the darkest color in the image. Colors darker than the
2262 % black point are set to zero. White point specifies the lightest color in
2263 % the image. Colors brighter than the white point are set to the maximum
2266 % If a '!' flag is given, map black and white colors to the given levels
2267 % rather than mapping those levels to black and white. See
2268 % LevelizeImage() below.
2270 % Gamma specifies a gamma correction to apply to the image.
2272 % The format of the LevelImage method is:
2274 % MagickBooleanType LevelImage(Image *image,const char *levels)
2276 % A description of each parameter follows:
2278 % o image: the image.
2280 % o levels: Specify the levels where the black and white points have the
2281 % range of 0-QuantumRange, and gamma has the range 0-10 (e.g. 10x90%+2).
2282 % A '!' flag inverts the re-mapping.
2285 MagickExport MagickBooleanType LevelImage(Image *image,
2286 const double black_point,const double white_point,const double gamma)
2288 #define LevelImageTag "Level/Image"
2289 #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
2290 pow(scale*((double) (x)-black_point),1.0/gamma)))
2314 Allocate and initialize levels map.
2316 assert(image != (Image *) NULL);
2317 assert(image->signature == MagickSignature);
2318 if (image->debug != MagickFalse)
2319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2320 scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2321 if (image->storage_class == PseudoClass)
2322 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2323 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2325 for (i=0; i < (ssize_t) image->colors; i++)
2330 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2331 image->colormap[i].red=LevelQuantum(image->colormap[i].red);
2332 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2333 image->colormap[i].green=LevelQuantum(image->colormap[i].green);
2334 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2335 image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
2336 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2337 image->colormap[i].alpha=LevelQuantum(image->colormap[i].alpha);
2344 exception=(&image->exception);
2345 image_view=AcquireCacheView(image);
2346 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2347 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2349 for (y=0; y < (ssize_t) image->rows; y++)
2357 if (status == MagickFalse)
2359 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2360 if (q == (const Quantum *) NULL)
2365 for (x=0; x < (ssize_t) image->columns; x++)
2367 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2368 SetPixelRed(image,LevelQuantum(
2369 GetPixelRed(image,q)),q);
2370 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2371 SetPixelGreen(image,
2372 LevelQuantum(GetPixelGreen(image,q)),q);
2373 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2375 LevelQuantum(GetPixelBlue(image,q)),q);
2376 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2377 (image->matte == MagickTrue))
2378 SetPixelAlpha(image,
2379 LevelQuantum(GetPixelAlpha(image,q)),q);
2380 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2381 (image->colorspace == CMYKColorspace))
2382 SetPixelBlack(image,
2383 LevelQuantum(GetPixelBlack(image,q)),q);
2384 q+=GetPixelChannels(image);
2386 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2388 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2393 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2394 #pragma omp critical (MagickCore_LevelImage)
2396 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2397 if (proceed == MagickFalse)
2401 image_view=DestroyCacheView(image_view);
2406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2410 % L e v e l i z e I m a g e C h a n n e l %
2414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2416 % LevelizeImage() applies the reversed LevelImage() operation to just
2417 % the specific channels specified. It compresses the full range of color
2418 % values, so that they lie between the given black and white points. Gamma is
2419 % applied before the values are mapped.
2421 % LevelizeImage() can be called with by using a +level command line
2422 % API option, or using a '!' on a -level or LevelImage() geometry string.
2424 % It can be used for example de-contrast a greyscale image to the exact
2425 % levels specified. Or by using specific levels for each channel of an image
2426 % you can convert a gray-scale image to any linear color gradient, according
2429 % The format of the LevelizeImage method is:
2431 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2432 % const double white_point,const double gamma)
2434 % A description of each parameter follows:
2436 % o image: the image.
2438 % o black_point: The level to map zero (black) to.
2440 % o white_point: The level to map QuantiumRange (white) to.
2442 % o gamma: adjust gamma by this factor before mapping values.
2445 MagickExport MagickBooleanType LevelizeImage(Image *image,
2446 const double black_point,const double white_point,const double gamma)
2448 #define LevelizeImageTag "Levelize/Image"
2449 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2450 pow((double) (QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2472 Allocate and initialize levels map.
2474 assert(image != (Image *) NULL);
2475 assert(image->signature == MagickSignature);
2476 if (image->debug != MagickFalse)
2477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2478 if (image->storage_class == PseudoClass)
2479 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2480 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2482 for (i=0; i < (ssize_t) image->colors; i++)
2487 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2488 image->colormap[i].red=LevelizeValue(image->colormap[i].red);
2489 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2490 image->colormap[i].green=LevelizeValue(image->colormap[i].green);
2491 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2492 image->colormap[i].blue=LevelizeValue(image->colormap[i].blue);
2493 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2494 image->colormap[i].alpha=LevelizeValue(image->colormap[i].alpha);
2501 exception=(&image->exception);
2502 image_view=AcquireCacheView(image);
2503 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2504 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2506 for (y=0; y < (ssize_t) image->rows; y++)
2514 if (status == MagickFalse)
2516 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2517 if (q == (const Quantum *) NULL)
2522 for (x=0; x < (ssize_t) image->columns; x++)
2524 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2525 SetPixelRed(image,LevelizeValue(GetPixelRed(image,q)),q);
2526 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2527 SetPixelGreen(image,LevelizeValue(GetPixelGreen(image,q)),q);
2528 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2529 SetPixelBlue(image,LevelizeValue(GetPixelBlue(image,q)),q);
2530 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2531 (image->colorspace == CMYKColorspace))
2532 SetPixelBlack(image,LevelizeValue(GetPixelBlack(image,q)),q);
2533 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2534 (image->matte == MagickTrue))
2535 SetPixelAlpha(image,LevelizeValue(GetPixelAlpha(image,q)),q);
2536 q+=GetPixelChannels(image);
2538 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2540 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2545 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2546 #pragma omp critical (MagickCore_LevelizeImage)
2548 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2549 if (proceed == MagickFalse)
2553 image_view=DestroyCacheView(image_view);
2558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2562 % L e v e l I m a g e C o l o r s %
2566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2568 % LevelImageColors() maps the given color to "black" and "white" values,
2569 % linearly spreading out the colors, and level values on a channel by channel
2570 % bases, as per LevelImage(). The given colors allows you to specify
2571 % different level ranges for each of the color channels separately.
2573 % If the boolean 'invert' is set true the image values will modifyed in the
2574 % reverse direction. That is any existing "black" and "white" colors in the
2575 % image will become the color values given, with all other values compressed
2576 % appropriatally. This effectivally maps a greyscale gradient into the given
2579 % The format of the LevelImageColors method is:
2581 % MagickBooleanType LevelImageColors(Image *image,
2582 % const PixelInfo *black_color,const PixelInfo *white_color,
2583 % const MagickBooleanType invert)
2585 % A description of each parameter follows:
2587 % o image: the image.
2589 % o black_color: The color to map black to/from
2591 % o white_point: The color to map white to/from
2593 % o invert: if true map the colors (levelize), rather than from (level)
2596 MagickExport MagickBooleanType LevelImageColors(Image *image,
2597 const PixelInfo *black_color,const PixelInfo *white_color,
2598 const MagickBooleanType invert)
2604 Allocate and initialize levels map.
2606 assert(image != (Image *) NULL);
2607 assert(image->signature == MagickSignature);
2608 if (image->debug != MagickFalse)
2609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2611 if (invert == MagickFalse)
2613 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2615 PushPixelChannelMap(image,RedChannel);
2616 status|=LevelImage(image,black_color->red,white_color->red,1.0);
2617 PopPixelChannelMap(image);
2619 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2621 PushPixelChannelMap(image,GreenChannel);
2622 status|=LevelImage(image,black_color->green,white_color->green,1.0);
2623 PopPixelChannelMap(image);
2625 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2627 PushPixelChannelMap(image,BlueChannel);
2628 status|=LevelImage(image,black_color->blue,white_color->blue,1.0);
2629 PopPixelChannelMap(image);
2631 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2632 (image->colorspace == CMYKColorspace))
2634 PushPixelChannelMap(image,BlackChannel);
2635 status|=LevelImage(image,black_color->black,white_color->black,1.0);
2636 PopPixelChannelMap(image);
2638 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2639 (image->matte == MagickTrue))
2641 PushPixelChannelMap(image,AlphaChannel);
2642 status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0);
2643 PopPixelChannelMap(image);
2648 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2650 PushPixelChannelMap(image,RedChannel);
2651 status|=LevelizeImage(image,black_color->red,white_color->red,1.0);
2652 PopPixelChannelMap(image);
2654 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2656 PushPixelChannelMap(image,GreenChannel);
2657 status|=LevelizeImage(image,black_color->green,white_color->green,
2659 PopPixelChannelMap(image);
2661 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2663 PushPixelChannelMap(image,BlueChannel);
2664 status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0);
2665 PopPixelChannelMap(image);
2667 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2668 (image->colorspace == CMYKColorspace))
2670 PushPixelChannelMap(image,BlackChannel);
2671 status|=LevelizeImage(image,black_color->black,white_color->black,
2673 PopPixelChannelMap(image);
2675 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2676 (image->matte == MagickTrue))
2678 PushPixelChannelMap(image,AlphaChannel);
2679 status|=LevelizeImage(image,black_color->alpha,white_color->alpha,
2681 PopPixelChannelMap(image);
2684 return(status == 0 ? MagickFalse : MagickTrue);
2688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2692 % L i n e a r S t r e t c h I m a g e %
2696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2698 % LinearStretchImage() discards any pixels below the black point and above
2699 % the white point and levels the remaining pixels.
2701 % The format of the LinearStretchImage method is:
2703 % MagickBooleanType LinearStretchImage(Image *image,
2704 % const double black_point,const double white_point)
2706 % A description of each parameter follows:
2708 % o image: the image.
2710 % o black_point: the black point.
2712 % o white_point: the white point.
2715 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2716 const double black_point,const double white_point)
2718 #define LinearStretchImageTag "LinearStretch/Image"
2736 Allocate histogram and linear map.
2738 assert(image != (Image *) NULL);
2739 assert(image->signature == MagickSignature);
2740 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
2741 sizeof(*histogram));
2742 if (histogram == (MagickRealType *) NULL)
2743 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2748 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
2749 exception=(&image->exception);
2750 for (y=0; y < (ssize_t) image->rows; y++)
2752 register const Quantum
2758 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2759 if (p == (const Quantum *) NULL)
2761 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2763 histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
2764 p+=GetPixelChannels(image);
2768 Find the histogram boundaries by locating the black and white point levels.
2771 for (black=0; black < (ssize_t) MaxMap; black++)
2773 intensity+=histogram[black];
2774 if (intensity >= black_point)
2778 for (white=(ssize_t) MaxMap; white != 0; white--)
2780 intensity+=histogram[white];
2781 if (intensity >= white_point)
2784 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
2785 status=LevelImage(image,(double) black,(double) white,1.0);
2790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2794 % M o d u l a t e I m a g e %
2798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2800 % ModulateImage() lets you control the brightness, saturation, and hue
2801 % of an image. Modulate represents the brightness, saturation, and hue
2802 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
2803 % modulation is lightness, saturation, and hue. And if the colorspace is
2804 % HWB, use blackness, whiteness, and hue.
2806 % The format of the ModulateImage method is:
2808 % MagickBooleanType ModulateImage(Image *image,const char *modulate)
2810 % A description of each parameter follows:
2812 % o image: the image.
2814 % o modulate: Define the percent change in brightness, saturation, and
2819 static void ModulateHSB(const double percent_hue,
2820 const double percent_saturation,const double percent_brightness,
2821 Quantum *red,Quantum *green,Quantum *blue)
2829 Increase or decrease color brightness, saturation, or hue.
2831 assert(red != (Quantum *) NULL);
2832 assert(green != (Quantum *) NULL);
2833 assert(blue != (Quantum *) NULL);
2834 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
2835 hue+=0.5*(0.01*percent_hue-1.0);
2840 saturation*=0.01*percent_saturation;
2841 brightness*=0.01*percent_brightness;
2842 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
2845 static void ModulateHSL(const double percent_hue,
2846 const double percent_saturation,const double percent_lightness,
2847 Quantum *red,Quantum *green,Quantum *blue)
2855 Increase or decrease color lightness, saturation, or hue.
2857 assert(red != (Quantum *) NULL);
2858 assert(green != (Quantum *) NULL);
2859 assert(blue != (Quantum *) NULL);
2860 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
2861 hue+=0.5*(0.01*percent_hue-1.0);
2866 saturation*=0.01*percent_saturation;
2867 lightness*=0.01*percent_lightness;
2868 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
2871 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue)
2879 Increase or decrease color blackness, whiteness, or hue.
2881 assert(red != (Quantum *) NULL);
2882 assert(green != (Quantum *) NULL);
2883 assert(blue != (Quantum *) NULL);
2884 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
2885 hue+=0.5*(0.01*percent_hue-1.0);
2890 blackness*=0.01*percent_blackness;
2891 whiteness*=0.01*percent_whiteness;
2892 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
2895 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
2897 #define ModulateImageTag "Modulate/Image"
2935 Initialize modulate table.
2937 assert(image != (Image *) NULL);
2938 assert(image->signature == MagickSignature);
2939 if (image->debug != MagickFalse)
2940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2941 if (modulate == (char *) NULL)
2942 return(MagickFalse);
2943 flags=ParseGeometry(modulate,&geometry_info);
2944 percent_brightness=geometry_info.rho;
2945 percent_saturation=geometry_info.sigma;
2946 if ((flags & SigmaValue) == 0)
2947 percent_saturation=100.0;
2948 percent_hue=geometry_info.xi;
2949 if ((flags & XiValue) == 0)
2951 colorspace=UndefinedColorspace;
2952 artifact=GetImageArtifact(image,"modulate:colorspace");
2953 if (artifact != (const char *) NULL)
2954 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
2955 MagickFalse,artifact);
2956 if (image->storage_class == PseudoClass)
2961 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2962 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2964 for (i=0; i < (ssize_t) image->colors; i++)
2969 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
2970 &image->colormap[i].red,&image->colormap[i].green,
2971 &image->colormap[i].blue);
2977 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
2978 &image->colormap[i].red,&image->colormap[i].green,
2979 &image->colormap[i].blue);
2984 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
2985 &image->colormap[i].red,&image->colormap[i].green,
2986 &image->colormap[i].blue);
2996 exception=(&image->exception);
2997 image_view=AcquireCacheView(image);
2998 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2999 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3001 for (y=0; y < (ssize_t) image->rows; y++)
3014 if (status == MagickFalse)
3016 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3017 if (q == (const Quantum *) NULL)
3022 for (x=0; x < (ssize_t) image->columns; x++)
3024 red=GetPixelRed(image,q);
3025 green=GetPixelGreen(image,q);
3026 blue=GetPixelBlue(image,q);
3031 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3038 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3044 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3049 SetPixelRed(image,red,q);
3050 SetPixelGreen(image,green,q);
3051 SetPixelBlue(image,blue,q);
3052 q+=GetPixelChannels(image);
3054 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3061 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3062 #pragma omp critical (MagickCore_ModulateImage)
3064 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3065 if (proceed == MagickFalse)
3069 image_view=DestroyCacheView(image_view);
3074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3078 % N e g a t e I m a g e %
3082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3084 % NegateImage() negates the colors in the reference image. The grayscale
3085 % option means that only grayscale values within the image are negated.
3087 % The format of the NegateImage method is:
3089 % MagickBooleanType NegateImage(Image *image,
3090 % const MagickBooleanType grayscale,ExceptionInfo *exception)
3092 % A description of each parameter follows:
3094 % o image: the image.
3096 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3098 % o exception: return any errors or warnings in this structure.
3101 MagickExport MagickBooleanType NegateImage(Image *image,
3102 const MagickBooleanType grayscale,ExceptionInfo *exception)
3104 #define NegateImageTag "Negate/Image"
3121 assert(image != (Image *) NULL);
3122 assert(image->signature == MagickSignature);
3123 if (image->debug != MagickFalse)
3124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3125 if (image->storage_class == PseudoClass)
3130 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3131 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3133 for (i=0; i < (ssize_t) image->colors; i++)
3135 if (grayscale != MagickFalse)
3136 if ((image->colormap[i].red != image->colormap[i].green) ||
3137 (image->colormap[i].green != image->colormap[i].blue))
3139 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3140 image->colormap[i].red=(Quantum) QuantumRange-
3141 image->colormap[i].red;
3142 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3143 image->colormap[i].green=(Quantum) QuantumRange-
3144 image->colormap[i].green;
3145 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3146 image->colormap[i].blue=(Quantum) QuantumRange-
3147 image->colormap[i].blue;
3155 image_view=AcquireCacheView(image);
3156 if (grayscale != MagickFalse)
3158 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3159 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3161 for (y=0; y < (ssize_t) image->rows; y++)
3172 if (status == MagickFalse)
3174 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3176 if (q == (const Quantum *) NULL)
3181 for (x=0; x < (ssize_t) image->columns; x++)
3186 if (IsPixelGray(image,q) != MagickFalse)
3188 q+=GetPixelChannels(image);
3191 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3196 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3197 if ((traits & UpdatePixelTrait) != 0)
3198 q[i]=QuantumRange-q[i];
3200 q+=GetPixelChannels(image);
3202 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3203 if (sync == MagickFalse)
3205 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3210 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3211 #pragma omp critical (MagickCore_NegateImage)
3213 proceed=SetImageProgress(image,NegateImageTag,progress++,
3215 if (proceed == MagickFalse)
3219 image_view=DestroyCacheView(image_view);
3225 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3226 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3228 for (y=0; y < (ssize_t) image->rows; y++)
3236 if (status == MagickFalse)
3238 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3239 if (q == (const Quantum *) NULL)
3244 for (x=0; x < (ssize_t) image->columns; x++)
3249 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3254 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3255 if ((traits & UpdatePixelTrait) != 0)
3256 q[i]=QuantumRange-q[i];
3258 q+=GetPixelChannels(image);
3260 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3262 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3267 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3268 #pragma omp critical (MagickCore_NegateImage)
3270 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3271 if (proceed == MagickFalse)
3275 image_view=DestroyCacheView(image_view);
3280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3284 % N o r m a l i z e I m a g e %
3288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3290 % NormalizeImage() enhances the contrast of a color image by mapping the
3291 % darkest 2 percent of all pixel to black and the brightest 1 percent to white.
3293 % The format of the NormalizeImage method is:
3295 % MagickBooleanType NormalizeImage(Image *image)
3297 % A description of each parameter follows:
3299 % o image: the image.
3302 MagickExport MagickBooleanType NormalizeImage(Image *image)
3308 black_point=(double) image->columns*image->rows*0.0015;
3309 white_point=(double) image->columns*image->rows*0.9995;
3310 return(ContrastStretchImage(image,black_point,white_point));
3314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3318 % S i g m o i d a l C o n t r a s t I m a g e %
3322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3324 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3325 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3326 % sigmoidal transfer function without saturating highlights or shadows.
3327 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3328 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3329 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3330 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3333 % The format of the SigmoidalContrastImage method is:
3335 % MagickBooleanType SigmoidalContrastImage(Image *image,
3336 % const MagickBooleanType sharpen,const char *levels)
3338 % A description of each parameter follows:
3340 % o image: the image.
3342 % o sharpen: Increase or decrease image contrast.
3344 % o alpha: strength of the contrast, the larger the number the more
3345 % 'threshold-like' it becomes.
3347 % o beta: midpoint of the function as a color value 0 to QuantumRange.
3350 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3351 const MagickBooleanType sharpen,const double contrast,const double midpoint)
3353 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3377 Allocate and initialize sigmoidal maps.
3379 assert(image != (Image *) NULL);
3380 assert(image->signature == MagickSignature);
3381 if (image->debug != MagickFalse)
3382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3383 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3384 sizeof(*sigmoidal_map));
3385 if (sigmoidal_map == (MagickRealType *) NULL)
3386 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3388 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3390 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3392 for (i=0; i <= (ssize_t) MaxMap; i++)
3394 if (sharpen != MagickFalse)
3396 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3397 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3398 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3399 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3400 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3401 (double) QuantumRange)))))+0.5));
3404 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3405 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3406 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3407 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3408 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3409 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3410 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3411 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3412 (double) QuantumRange*contrast))))))/contrast)));
3414 if (image->storage_class == PseudoClass)
3417 Sigmoidal-contrast enhance colormap.
3419 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3420 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3422 for (i=0; i < (ssize_t) image->colors; i++)
3424 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3425 image->colormap[i].red=ClampToQuantum(sigmoidal_map[
3426 ScaleQuantumToMap(image->colormap[i].red)]);
3427 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3428 image->colormap[i].green=ClampToQuantum(sigmoidal_map[
3429 ScaleQuantumToMap(image->colormap[i].green)]);
3430 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3431 image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
3432 ScaleQuantumToMap(image->colormap[i].blue)]);
3433 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3434 image->colormap[i].alpha=ClampToQuantum(sigmoidal_map[
3435 ScaleQuantumToMap(image->colormap[i].alpha)]);
3439 Sigmoidal-contrast enhance image.
3443 exception=(&image->exception);
3444 image_view=AcquireCacheView(image);
3445 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3446 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3448 for (y=0; y < (ssize_t) image->rows; y++)
3456 if (status == MagickFalse)
3458 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3459 if (q == (const Quantum *) NULL)
3464 for (x=0; x < (ssize_t) image->columns; x++)
3466 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3467 SetPixelRed(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
3468 GetPixelRed(image,q))]),q);
3469 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3470 SetPixelGreen(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
3471 GetPixelGreen(image,q))]),q);
3472 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3473 SetPixelBlue(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
3474 GetPixelBlue(image,q))]),q);
3475 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3476 (image->colorspace == CMYKColorspace))
3477 SetPixelBlack(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
3478 GetPixelBlack(image,q))]),q);
3479 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3480 SetPixelAlpha(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
3481 GetPixelAlpha(image,q))]),q);
3482 q+=GetPixelChannels(image);
3484 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3486 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3491 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3492 #pragma omp critical (MagickCore_SigmoidalContrastImage)
3494 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3496 if (proceed == MagickFalse)
3500 image_view=DestroyCacheView(image_view);
3501 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);