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-2010 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 "magick/studio.h"
44 #include "magick/artifact.h"
45 #include "magick/cache.h"
46 #include "magick/cache-view.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/composite-private.h"
51 #include "magick/enhance.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/fx.h"
55 #include "magick/gem.h"
56 #include "magick/geometry.h"
57 #include "magick/histogram.h"
58 #include "magick/image.h"
59 #include "magick/image-private.h"
60 #include "magick/memory_.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/option.h"
64 #include "magick/quantum.h"
65 #include "magick/quantum-private.h"
66 #include "magick/resample.h"
67 #include "magick/resample-private.h"
68 #include "magick/statistic.h"
69 #include "magick/string_.h"
70 #include "magick/string-private.h"
71 #include "magick/thread-private.h"
72 #include "magick/token.h"
73 #include "magick/xml-tree.h"
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % A u t o G a m m a I m a g e %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % AutoGammaImage() extract the 'mean' from the image and adjust the image
87 % to try make set its gamma appropriatally.
89 % The format of the AutoGammaImage method is:
91 % MagickBooleanType AutoGammaImage(Image *image)
92 % MagickBooleanType AutoGammaImageChannel(Image *image,
93 % const ChannelType channel)
95 % A description of each parameter follows:
97 % o image: The image to auto-level
99 % o channel: The channels to auto-level. If the special 'SyncChannels'
100 % flag is set all given channels is adjusted in the same way using the
101 % mean average of those channels.
105 MagickExport MagickBooleanType AutoGammaImage(Image *image)
107 return(AutoGammaImageChannel(image,DefaultChannels));
110 MagickExport MagickBooleanType AutoGammaImageChannel(Image *image,
111 const ChannelType channel)
117 mean,sans,gamma,logmean;
121 if ((channel & SyncChannels) != 0 )
124 Apply gamma correction equally accross all given channels
126 (void) GetImageChannelMean(image,channel,&mean,&sans,&image->exception);
127 gamma=log(mean*QuantumScale)/logmean;
128 return LevelImageChannel(image, channel,
129 0.0, (double)QuantumRange, gamma);
133 auto-gamma each channel separateally
136 if ((channel & RedChannel) != 0)
138 (void) GetImageChannelMean(image,RedChannel,&mean,&sans,
140 gamma=log(mean*QuantumScale)/logmean;
141 status = status && LevelImageChannel(image, RedChannel,
142 0.0, (double)QuantumRange, gamma);
144 if ((channel & GreenChannel) != 0)
146 (void) GetImageChannelMean(image,GreenChannel,&mean,&sans,
148 gamma=log(mean*QuantumScale)/logmean;
149 status = status && LevelImageChannel(image, GreenChannel,
150 0.0, (double)QuantumRange, gamma);
152 if ((channel & BlueChannel) != 0)
154 (void) GetImageChannelMean(image,BlueChannel,&mean,&sans,
156 gamma=log(mean*QuantumScale)/logmean;
157 status = status && LevelImageChannel(image, BlueChannel,
158 0.0, (double)QuantumRange, gamma);
160 if (((channel & OpacityChannel) != 0) &&
161 (image->matte == MagickTrue))
163 (void) GetImageChannelMean(image,OpacityChannel,&mean,&sans,
165 gamma=log(mean*QuantumScale)/logmean;
166 status = status && LevelImageChannel(image, OpacityChannel,
167 0.0, (double)QuantumRange, gamma);
169 if (((channel & IndexChannel) != 0) &&
170 (image->colorspace == CMYKColorspace))
172 (void) GetImageChannelMean(image,IndexChannel,&mean,&sans,
174 gamma=log(mean*QuantumScale)/logmean;
175 status = status && LevelImageChannel(image, IndexChannel,
176 0.0, (double)QuantumRange, gamma);
178 return(status != 0 ? MagickTrue : MagickFalse);
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % A u t o L e v e l I m a g e %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % AutoLevelImage() adjusts the levels of a particular image channel by
193 % scaling the minimum and maximum values to the full quantum range.
195 % The format of the LevelImage method is:
197 % MagickBooleanType AutoLevelImage(Image *image)
198 % MagickBooleanType AutoLevelImageChannel(Image *image,
199 % const ChannelType channel)
201 % A description of each parameter follows:
203 % o image: The image to auto-level
205 % o channel: The channels to auto-level. If the special 'SyncChannels'
206 % flag is set the min/max/mean value of all given channels is used for
207 % all given channels, to all channels in the same way.
211 MagickExport MagickBooleanType AutoLevelImage(Image *image)
213 return(AutoLevelImageChannel(image,DefaultChannels));
216 MagickExport MagickBooleanType AutoLevelImageChannel(Image *image,
217 const ChannelType channel)
220 This is simply a convenience function around a Min/Max Histogram Stretch
222 return MinMaxStretchImage(image, channel, 0.0, 0.0);
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 % B r i g h t n e s s C o n t r a s t I m a g e %
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % Use BrightnessContrastImage() to change the brightness and/or contrast of
237 % an image. It converts the brightness and contrast parameters into slope
238 % and intercept and calls a polynomical function to apply to the image.
240 % The format of the BrightnessContrastImage method is:
242 % MagickBooleanType BrightnessContrastImage(Image *image,
243 % const double brightness,const double contrast)
244 % MagickBooleanType BrightnessContrastImageChannel(Image *image,
245 % const ChannelType channel,const double brightness,
246 % const double contrast)
248 % A description of each parameter follows:
250 % o image: the image.
252 % o channel: the channel.
254 % o brightness: the brightness percent (-100 .. 100).
256 % o contrast: the contrast percent (-100 .. 100).
260 MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
261 const double brightness,const double contrast)
266 status=BrightnessContrastImageChannel(image,DefaultChannels,brightness,
271 MagickExport MagickBooleanType BrightnessContrastImageChannel(Image *image,
272 const ChannelType channel,const double brightness,const double contrast)
274 #define BrightnessContastImageTag "BrightnessContast/Image"
286 Compute slope and intercept.
288 assert(image != (Image *) NULL);
289 assert(image->signature == MagickSignature);
290 if (image->debug != MagickFalse)
291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
293 slope=tan(MagickPI*(alpha/100.0+1.0)/4.0);
296 intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
297 coefficients[0]=slope;
298 coefficients[1]=intercept;
299 status=FunctionImageChannel(image,channel,PolynomialFunction,2,coefficients,
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % C o l o r D e c i s i o n L i s t I m a g e %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 % ColorDecisionListImage() accepts a lightweight Color Correction Collection
316 % (CCC) file which solely contains one or more color corrections and applies
317 % the correction to the image. Here is a sample CCC file:
319 % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
320 % <ColorCorrection id="cc03345">
322 % <Slope> 0.9 1.2 0.5 </Slope>
323 % <Offset> 0.4 -0.5 0.6 </Offset>
324 % <Power> 1.0 0.8 1.5 </Power>
327 % <Saturation> 0.85 </Saturation>
330 % </ColorCorrectionCollection>
332 % which includes the slop, offset, and power for each of the RGB channels
333 % as well as the saturation.
335 % The format of the ColorDecisionListImage method is:
337 % MagickBooleanType ColorDecisionListImage(Image *image,
338 % const char *color_correction_collection)
340 % A description of each parameter follows:
342 % o image: the image.
344 % o color_correction_collection: the color correction collection in XML.
347 MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
348 const char *color_correction_collection)
350 #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
352 typedef struct _Correction
360 typedef struct _ColorCorrection
375 token[MaxTextExtent];
409 Allocate and initialize cdl maps.
411 assert(image != (Image *) NULL);
412 assert(image->signature == MagickSignature);
413 if (image->debug != MagickFalse)
414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
415 if (color_correction_collection == (const char *) NULL)
417 ccc=NewXMLTree((const char *) color_correction_collection,&image->exception);
418 if (ccc == (XMLTreeInfo *) NULL)
420 cc=GetXMLTreeChild(ccc,"ColorCorrection");
421 if (cc == (XMLTreeInfo *) NULL)
423 ccc=DestroyXMLTree(ccc);
426 color_correction.red.slope=1.0;
427 color_correction.red.offset=0.0;
428 color_correction.red.power=1.0;
429 color_correction.green.slope=1.0;
430 color_correction.green.offset=0.0;
431 color_correction.green.power=1.0;
432 color_correction.blue.slope=1.0;
433 color_correction.blue.offset=0.0;
434 color_correction.blue.power=1.0;
435 color_correction.saturation=0.0;
436 sop=GetXMLTreeChild(cc,"SOPNode");
437 if (sop != (XMLTreeInfo *) NULL)
444 slope=GetXMLTreeChild(sop,"Slope");
445 if (slope != (XMLTreeInfo *) NULL)
447 content=GetXMLTreeContent(slope);
448 p=(const char *) content;
449 for (i=0; (*p != '\0') && (i < 3); i++)
451 GetMagickToken(p,&p,token);
453 GetMagickToken(p,&p,token);
456 case 0: color_correction.red.slope=StringToDouble(token); break;
457 case 1: color_correction.green.slope=StringToDouble(token); break;
458 case 2: color_correction.blue.slope=StringToDouble(token); break;
462 offset=GetXMLTreeChild(sop,"Offset");
463 if (offset != (XMLTreeInfo *) NULL)
465 content=GetXMLTreeContent(offset);
466 p=(const char *) content;
467 for (i=0; (*p != '\0') && (i < 3); i++)
469 GetMagickToken(p,&p,token);
471 GetMagickToken(p,&p,token);
474 case 0: color_correction.red.offset=StringToDouble(token); break;
475 case 1: color_correction.green.offset=StringToDouble(token); break;
476 case 2: color_correction.blue.offset=StringToDouble(token); break;
480 power=GetXMLTreeChild(sop,"Power");
481 if (power != (XMLTreeInfo *) NULL)
483 content=GetXMLTreeContent(power);
484 p=(const char *) content;
485 for (i=0; (*p != '\0') && (i < 3); i++)
487 GetMagickToken(p,&p,token);
489 GetMagickToken(p,&p,token);
492 case 0: color_correction.red.power=StringToDouble(token); break;
493 case 1: color_correction.green.power=StringToDouble(token); break;
494 case 2: color_correction.blue.power=StringToDouble(token); break;
499 sat=GetXMLTreeChild(cc,"SATNode");
500 if (sat != (XMLTreeInfo *) NULL)
505 saturation=GetXMLTreeChild(sat,"Saturation");
506 if (saturation != (XMLTreeInfo *) NULL)
508 content=GetXMLTreeContent(saturation);
509 p=(const char *) content;
510 GetMagickToken(p,&p,token);
511 color_correction.saturation=StringToDouble(token);
514 ccc=DestroyXMLTree(ccc);
515 if (image->debug != MagickFalse)
517 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
518 " Color Correction Collection:");
519 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
520 " color_correction.red.slope: %g",color_correction.red.slope);
521 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
522 " color_correction.red.offset: %g",color_correction.red.offset);
523 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
524 " color_correction.red.power: %g",color_correction.red.power);
525 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
526 " color_correction.green.slope: %g",color_correction.green.slope);
527 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
528 " color_correction.green.offset: %g",color_correction.green.offset);
529 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
530 " color_correction.green.power: %g",color_correction.green.power);
531 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
532 " color_correction.blue.slope: %g",color_correction.blue.slope);
533 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
534 " color_correction.blue.offset: %g",color_correction.blue.offset);
535 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
536 " color_correction.blue.power: %g",color_correction.blue.power);
537 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
538 " color_correction.saturation: %g",color_correction.saturation);
540 cdl_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
541 if (cdl_map == (PixelPacket *) NULL)
542 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
544 #if defined(MAGICKCORE_OPENMP_SUPPORT)
545 #pragma omp parallel for schedule(dynamic,4)
547 for (i=0; i <= (ssize_t) MaxMap; i++)
549 cdl_map[i].red=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
550 MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
551 color_correction.red.offset,color_correction.red.power)))));
552 cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
553 MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
554 color_correction.green.offset,color_correction.green.power)))));
555 cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
556 MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
557 color_correction.blue.offset,color_correction.blue.power)))));
559 if (image->storage_class == PseudoClass)
562 Apply transfer function to colormap.
564 #if defined(MAGICKCORE_OPENMP_SUPPORT)
565 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
567 for (i=0; i < (ssize_t) image->colors; i++)
572 luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
573 0.0722*image->colormap[i].blue;
574 image->colormap[i].red=ClampToQuantum(luma+color_correction.saturation*
575 cdl_map[ScaleQuantumToMap(image->colormap[i].red)].red-luma);
576 image->colormap[i].green=ClampToQuantum(luma+
577 color_correction.saturation*cdl_map[ScaleQuantumToMap(
578 image->colormap[i].green)].green-luma);
579 image->colormap[i].blue=ClampToQuantum(luma+color_correction.saturation*
580 cdl_map[ScaleQuantumToMap(image->colormap[i].blue)].blue-luma);
584 Apply transfer function to image.
588 exception=(&image->exception);
589 image_view=AcquireCacheView(image);
590 #if defined(MAGICKCORE_OPENMP_SUPPORT)
591 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
593 for (y=0; y < (ssize_t) image->rows; y++)
604 if (status == MagickFalse)
606 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
607 if (q == (PixelPacket *) NULL)
612 for (x=0; x < (ssize_t) image->columns; x++)
614 luma=0.2126*q->red+0.7152*q->green+0.0722*q->blue;
615 q->red=ClampToQuantum(luma+color_correction.saturation*
616 (cdl_map[ScaleQuantumToMap(q->red)].red-luma));
617 q->green=ClampToQuantum(luma+color_correction.saturation*
618 (cdl_map[ScaleQuantumToMap(q->green)].green-luma));
619 q->blue=ClampToQuantum(luma+color_correction.saturation*
620 (cdl_map[ScaleQuantumToMap(q->blue)].blue-luma));
623 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
625 if (image->progress_monitor != (MagickProgressMonitor) NULL)
630 #if defined(MAGICKCORE_OPENMP_SUPPORT)
631 #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
633 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
634 progress++,image->rows);
635 if (proceed == MagickFalse)
639 image_view=DestroyCacheView(image_view);
640 cdl_map=(PixelPacket *) RelinquishMagickMemory(cdl_map);
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % C l u t I m a g e %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % ClutImage() replaces each color value in the given image, by using it as an
656 % index to lookup a replacement color value in a Color Look UP Table in the
657 % form of an image. The values are extracted along a diagonal of the CLUT
658 % image so either a horizontal or vertial gradient image can be used.
660 % Typically this is used to either re-color a gray-scale image according to a
661 % color gradient in the CLUT image, or to perform a freeform histogram
662 % (level) adjustment according to the (typically gray-scale) gradient in the
665 % When the 'channel' mask includes the matte/alpha transparency channel but
666 % one image has no such channel it is assumed that that image is a simple
667 % gray-scale image that will effect the alpha channel values, either for
668 % gray-scale coloring (with transparent or semi-transparent colors), or
669 % a histogram adjustment of existing alpha channel values. If both images
670 % have matte channels, direct and normal indexing is applied, which is rarely
673 % The format of the ClutImage method is:
675 % MagickBooleanType ClutImage(Image *image,Image *clut_image)
676 % MagickBooleanType ClutImageChannel(Image *image,
677 % const ChannelType channel,Image *clut_image)
679 % A description of each parameter follows:
681 % o image: the image, which is replaced by indexed CLUT values
683 % o clut_image: the color lookup table image for replacement color values.
685 % o channel: the channel.
689 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image)
691 return(ClutImageChannel(image,DefaultChannels,clut_image));
694 MagickExport MagickBooleanType ClutImageChannel(Image *image,
695 const ChannelType channel,const Image *clut_image)
697 #define ClutImageTag "Clut/Image"
715 **restrict resample_filter;
721 assert(image != (Image *) NULL);
722 assert(image->signature == MagickSignature);
723 if (image->debug != MagickFalse)
724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
725 assert(clut_image != (Image *) NULL);
726 assert(clut_image->signature == MagickSignature);
727 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
734 GetMagickPixelPacket(clut_image,&zero);
735 adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
736 exception=(&image->exception);
737 resample_filter=AcquireResampleFilterThreadSet(clut_image,
738 UndefinedVirtualPixelMethod,MagickTrue,exception);
739 image_view=AcquireCacheView(image);
740 #if defined(MAGICKCORE_OPENMP_SUPPORT)
741 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
743 for (y=0; y < (ssize_t) image->rows; y++)
760 if (status == MagickFalse)
762 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
763 if (q == (PixelPacket *) NULL)
768 indexes=GetCacheViewAuthenticIndexQueue(image_view);
770 id=GetOpenMPThreadId();
771 for (x=0; x < (ssize_t) image->columns; x++)
776 Apply OpacityChannel BEFORE the color channels. Do not re-order.
778 The handling special case 2 (coloring gray-scale), requires access to
779 the unmodified colors of the original image to determine the index
780 value. As such alpha/matte channel handling must be performed BEFORE,
781 any of the color channels are modified.
784 if ((channel & OpacityChannel) != 0)
786 if (clut_image->matte == MagickFalse)
789 A gray-scale LUT replacement for an image alpha channel.
791 (void) ResamplePixelColor(resample_filter[id],QuantumScale*
792 GetAlphaPixelComponent(q)*(clut_image->columns+adjust),
793 QuantumScale*GetAlphaPixelComponent(q)*(clut_image->rows+
795 q->opacity=(Quantum) (QuantumRange-MagickPixelIntensityToQuantum(
799 if (image->matte == MagickFalse)
802 A greyscale image being colored by a LUT with transparency.
804 (void) ResamplePixelColor(resample_filter[id],QuantumScale*
805 PixelIntensity(q)*(clut_image->columns-adjust),QuantumScale*
806 PixelIntensity(q)*(clut_image->rows-adjust),&pixel);
807 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
812 Direct alpha channel lookup.
814 (void) ResamplePixelColor(resample_filter[id],QuantumScale*
815 q->opacity*(clut_image->columns-adjust),QuantumScale*
816 q->opacity* (clut_image->rows-adjust),&pixel);
817 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
820 if ((channel & RedChannel) != 0)
822 (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->red*
823 (clut_image->columns-adjust),QuantumScale*q->red*
824 (clut_image->rows-adjust),&pixel);
825 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
827 if ((channel & GreenChannel) != 0)
829 (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->green*
830 (clut_image->columns-adjust),QuantumScale*q->green*
831 (clut_image->rows-adjust),&pixel);
832 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
834 if ((channel & BlueChannel) != 0)
836 (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->blue*
837 (clut_image->columns-adjust),QuantumScale*q->blue*
838 (clut_image->rows-adjust),&pixel);
839 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
841 if (((channel & IndexChannel) != 0) &&
842 (image->colorspace == CMYKColorspace))
844 (void) ResamplePixelColor(resample_filter[id],QuantumScale*indexes[x]*
845 (clut_image->columns-adjust),QuantumScale*indexes[x]*
846 (clut_image->rows-adjust),&pixel);
847 indexes[x]=ClampToQuantum(pixel.index);
851 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
853 if (image->progress_monitor != (MagickProgressMonitor) NULL)
858 #if defined(MAGICKCORE_OPENMP_SUPPORT)
859 #pragma omp critical (MagickCore_ClutImageChannel)
861 proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
862 if (proceed == MagickFalse)
866 image_view=DestroyCacheView(image_view);
867 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
869 Enable alpha channel if CLUT image could enable it.
871 if ((clut_image->matte != MagickFalse) && ((channel & OpacityChannel) != 0))
872 (void) SetImageAlphaChannel(image,ActivateAlphaChannel);
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 % C o n t r a s t I m a g e %
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887 % ContrastImage() enhances the intensity differences between the lighter and
888 % darker elements of the image. Set sharpen to a MagickTrue to increase the
889 % image contrast otherwise the contrast is reduced.
891 % The format of the ContrastImage method is:
893 % MagickBooleanType ContrastImage(Image *image,
894 % const MagickBooleanType sharpen)
896 % A description of each parameter follows:
898 % o image: the image.
900 % o sharpen: Increase or decrease image contrast.
904 static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
912 Enhance contrast: dark color become darker, light color become lighter.
914 assert(red != (Quantum *) NULL);
915 assert(green != (Quantum *) NULL);
916 assert(blue != (Quantum *) NULL);
920 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
921 brightness+=0.5*sign*(0.5*(sin(MagickPI*(brightness-0.5))+1.0)-brightness);
922 if (brightness > 1.0)
925 if (brightness < 0.0)
927 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
930 MagickExport MagickBooleanType ContrastImage(Image *image,
931 const MagickBooleanType sharpen)
933 #define ContrastImageTag "Contrast/Image"
956 assert(image != (Image *) NULL);
957 assert(image->signature == MagickSignature);
958 if (image->debug != MagickFalse)
959 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
960 sign=sharpen != MagickFalse ? 1 : -1;
961 if (image->storage_class == PseudoClass)
964 Contrast enhance colormap.
966 for (i=0; i < (ssize_t) image->colors; i++)
967 Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
968 &image->colormap[i].blue);
971 Contrast enhance image.
975 exception=(&image->exception);
976 image_view=AcquireCacheView(image);
977 #if defined(MAGICKCORE_OPENMP_SUPPORT)
978 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
980 for (y=0; y < (ssize_t) image->rows; y++)
988 if (status == MagickFalse)
990 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
991 if (q == (PixelPacket *) NULL)
996 for (x=0; x < (ssize_t) image->columns; x++)
998 Contrast(sign,&q->red,&q->green,&q->blue);
1001 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1003 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1008 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1009 #pragma omp critical (MagickCore_ContrastImage)
1011 proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
1012 if (proceed == MagickFalse)
1016 image_view=DestroyCacheView(image_view);
1021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1025 % C o n t r a s t S t r e t c h I m a g e %
1029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1031 % The ContrastStretchImage() is a simple image enhancement technique that
1032 % attempts to improve the contrast in an image by `stretching' the range of
1033 % intensity values it contains to span a desired range of values. It differs
1034 % from the more sophisticated histogram equalization in that it can only
1035 % apply % a linear scaling function to the image pixel values. As a result
1036 % the `enhancement' is less harsh.
1038 % The format of the ContrastStretchImage method is:
1040 % MagickBooleanType ContrastStretchImage(Image *image,
1041 % const char *levels)
1042 % MagickBooleanType ContrastStretchImageChannel(Image *image,
1043 % const size_t channel,const double black_point,
1044 % const double white_point)
1046 % A description of each parameter follows:
1048 % o image: the image.
1050 % o channel: the channel.
1052 % o black_point: the black point.
1054 % o white_point: the white point.
1056 % o levels: Specify the levels where the black and white points have the
1057 % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
1061 MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1080 if (levels == (char *) NULL)
1081 return(MagickFalse);
1082 flags=ParseGeometry(levels,&geometry_info);
1083 black_point=geometry_info.rho;
1084 white_point=(double) image->columns*image->rows;
1085 if ((flags & SigmaValue) != 0)
1086 white_point=geometry_info.sigma;
1087 if ((flags & PercentValue) != 0)
1089 black_point*=(double) QuantumRange/100.0;
1090 white_point*=(double) QuantumRange/100.0;
1092 if ((flags & SigmaValue) == 0)
1093 white_point=(double) image->columns*image->rows-black_point;
1094 status=ContrastStretchImageChannel(image,DefaultChannels,black_point,
1099 MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
1100 const ChannelType channel,const double black_point,const double white_point)
1102 #define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
1103 #define ContrastStretchImageTag "ContrastStretch/Image"
1133 Allocate histogram and stretch map.
1135 assert(image != (Image *) NULL);
1136 assert(image->signature == MagickSignature);
1137 if (image->debug != MagickFalse)
1138 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1139 histogram=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
1140 sizeof(*histogram));
1141 stretch_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
1142 sizeof(*stretch_map));
1143 if ((histogram == (MagickPixelPacket *) NULL) ||
1144 (stretch_map == (MagickPixelPacket *) NULL))
1145 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1151 exception=(&image->exception);
1152 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
1153 image_view=AcquireCacheView(image);
1154 for (y=0; y < (ssize_t) image->rows; y++)
1156 register const PixelPacket
1159 register IndexPacket
1165 if (status == MagickFalse)
1167 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1168 if (p == (const PixelPacket *) NULL)
1173 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1174 if (channel == DefaultChannels)
1175 for (x=0; x < (ssize_t) image->columns; x++)
1180 intensity=PixelIntensityToQuantum(p);
1181 histogram[ScaleQuantumToMap(intensity)].red++;
1182 histogram[ScaleQuantumToMap(intensity)].green++;
1183 histogram[ScaleQuantumToMap(intensity)].blue++;
1184 histogram[ScaleQuantumToMap(intensity)].index++;
1188 for (x=0; x < (ssize_t) image->columns; x++)
1190 if ((channel & RedChannel) != 0)
1191 histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++;
1192 if ((channel & GreenChannel) != 0)
1193 histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++;
1194 if ((channel & BlueChannel) != 0)
1195 histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++;
1196 if ((channel & OpacityChannel) != 0)
1197 histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++;
1198 if (((channel & IndexChannel) != 0) &&
1199 (image->colorspace == CMYKColorspace))
1200 histogram[ScaleQuantumToMap(indexes[x])].index++;
1205 Find the histogram boundaries by locating the black/white levels.
1208 white.red=MaxRange(QuantumRange);
1209 if ((channel & RedChannel) != 0)
1212 for (i=0; i <= (ssize_t) MaxMap; i++)
1214 intensity+=histogram[i].red;
1215 if (intensity > black_point)
1218 black.red=(MagickRealType) i;
1220 for (i=(ssize_t) MaxMap; i != 0; i--)
1222 intensity+=histogram[i].red;
1223 if (intensity > ((double) image->columns*image->rows-white_point))
1226 white.red=(MagickRealType) i;
1229 white.green=MaxRange(QuantumRange);
1230 if ((channel & GreenChannel) != 0)
1233 for (i=0; i <= (ssize_t) MaxMap; i++)
1235 intensity+=histogram[i].green;
1236 if (intensity > black_point)
1239 black.green=(MagickRealType) i;
1241 for (i=(ssize_t) MaxMap; i != 0; i--)
1243 intensity+=histogram[i].green;
1244 if (intensity > ((double) image->columns*image->rows-white_point))
1247 white.green=(MagickRealType) i;
1250 white.blue=MaxRange(QuantumRange);
1251 if ((channel & BlueChannel) != 0)
1254 for (i=0; i <= (ssize_t) MaxMap; i++)
1256 intensity+=histogram[i].blue;
1257 if (intensity > black_point)
1260 black.blue=(MagickRealType) i;
1262 for (i=(ssize_t) MaxMap; i != 0; i--)
1264 intensity+=histogram[i].blue;
1265 if (intensity > ((double) image->columns*image->rows-white_point))
1268 white.blue=(MagickRealType) i;
1271 white.opacity=MaxRange(QuantumRange);
1272 if ((channel & OpacityChannel) != 0)
1275 for (i=0; i <= (ssize_t) MaxMap; i++)
1277 intensity+=histogram[i].opacity;
1278 if (intensity > black_point)
1281 black.opacity=(MagickRealType) i;
1283 for (i=(ssize_t) MaxMap; i != 0; i--)
1285 intensity+=histogram[i].opacity;
1286 if (intensity > ((double) image->columns*image->rows-white_point))
1289 white.opacity=(MagickRealType) i;
1292 white.index=MaxRange(QuantumRange);
1293 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
1296 for (i=0; i <= (ssize_t) MaxMap; i++)
1298 intensity+=histogram[i].index;
1299 if (intensity > black_point)
1302 black.index=(MagickRealType) i;
1304 for (i=(ssize_t) MaxMap; i != 0; i--)
1306 intensity+=histogram[i].index;
1307 if (intensity > ((double) image->columns*image->rows-white_point))
1310 white.index=(MagickRealType) i;
1312 histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram);
1314 Stretch the histogram to create the stretched image mapping.
1316 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map));
1317 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1318 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1320 for (i=0; i <= (ssize_t) MaxMap; i++)
1322 if ((channel & RedChannel) != 0)
1324 if (i < (ssize_t) black.red)
1325 stretch_map[i].red=0.0;
1327 if (i > (ssize_t) white.red)
1328 stretch_map[i].red=(MagickRealType) QuantumRange;
1330 if (black.red != white.red)
1331 stretch_map[i].red=(MagickRealType) ScaleMapToQuantum(
1332 (MagickRealType) (MaxMap*(i-black.red)/(white.red-black.red)));
1334 if ((channel & GreenChannel) != 0)
1336 if (i < (ssize_t) black.green)
1337 stretch_map[i].green=0.0;
1339 if (i > (ssize_t) white.green)
1340 stretch_map[i].green=(MagickRealType) QuantumRange;
1342 if (black.green != white.green)
1343 stretch_map[i].green=(MagickRealType) ScaleMapToQuantum(
1344 (MagickRealType) (MaxMap*(i-black.green)/(white.green-
1347 if ((channel & BlueChannel) != 0)
1349 if (i < (ssize_t) black.blue)
1350 stretch_map[i].blue=0.0;
1352 if (i > (ssize_t) white.blue)
1353 stretch_map[i].blue=(MagickRealType) QuantumRange;
1355 if (black.blue != white.blue)
1356 stretch_map[i].blue=(MagickRealType) ScaleMapToQuantum(
1357 (MagickRealType) (MaxMap*(i-black.blue)/(white.blue-
1360 if ((channel & OpacityChannel) != 0)
1362 if (i < (ssize_t) black.opacity)
1363 stretch_map[i].opacity=0.0;
1365 if (i > (ssize_t) white.opacity)
1366 stretch_map[i].opacity=(MagickRealType) QuantumRange;
1368 if (black.opacity != white.opacity)
1369 stretch_map[i].opacity=(MagickRealType) ScaleMapToQuantum(
1370 (MagickRealType) (MaxMap*(i-black.opacity)/(white.opacity-
1373 if (((channel & IndexChannel) != 0) &&
1374 (image->colorspace == CMYKColorspace))
1376 if (i < (ssize_t) black.index)
1377 stretch_map[i].index=0.0;
1379 if (i > (ssize_t) white.index)
1380 stretch_map[i].index=(MagickRealType) QuantumRange;
1382 if (black.index != white.index)
1383 stretch_map[i].index=(MagickRealType) ScaleMapToQuantum(
1384 (MagickRealType) (MaxMap*(i-black.index)/(white.index-
1391 if (((channel & OpacityChannel) != 0) || (((channel & IndexChannel) != 0) &&
1392 (image->colorspace == CMYKColorspace)))
1393 image->storage_class=DirectClass;
1394 if (image->storage_class == PseudoClass)
1399 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1400 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1402 for (i=0; i < (ssize_t) image->colors; i++)
1404 if ((channel & RedChannel) != 0)
1406 if (black.red != white.red)
1407 image->colormap[i].red=ClampToQuantum(stretch_map[
1408 ScaleQuantumToMap(image->colormap[i].red)].red);
1410 if ((channel & GreenChannel) != 0)
1412 if (black.green != white.green)
1413 image->colormap[i].green=ClampToQuantum(stretch_map[
1414 ScaleQuantumToMap(image->colormap[i].green)].green);
1416 if ((channel & BlueChannel) != 0)
1418 if (black.blue != white.blue)
1419 image->colormap[i].blue=ClampToQuantum(stretch_map[
1420 ScaleQuantumToMap(image->colormap[i].blue)].blue);
1422 if ((channel & OpacityChannel) != 0)
1424 if (black.opacity != white.opacity)
1425 image->colormap[i].opacity=ClampToQuantum(stretch_map[
1426 ScaleQuantumToMap(image->colormap[i].opacity)].opacity);
1435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1436 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1438 for (y=0; y < (ssize_t) image->rows; y++)
1440 register IndexPacket
1446 register PixelPacket
1449 if (status == MagickFalse)
1451 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1452 if (q == (PixelPacket *) NULL)
1457 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1458 for (x=0; x < (ssize_t) image->columns; x++)
1460 if ((channel & RedChannel) != 0)
1462 if (black.red != white.red)
1463 q->red=ClampToQuantum(stretch_map[ScaleQuantumToMap(q->red)].red);
1465 if ((channel & GreenChannel) != 0)
1467 if (black.green != white.green)
1468 q->green=ClampToQuantum(stretch_map[ScaleQuantumToMap(
1471 if ((channel & BlueChannel) != 0)
1473 if (black.blue != white.blue)
1474 q->blue=ClampToQuantum(stretch_map[ScaleQuantumToMap(
1477 if ((channel & OpacityChannel) != 0)
1479 if (black.opacity != white.opacity)
1480 q->opacity=ClampToQuantum(stretch_map[ScaleQuantumToMap(
1481 q->opacity)].opacity);
1483 if (((channel & IndexChannel) != 0) &&
1484 (image->colorspace == CMYKColorspace))
1486 if (black.index != white.index)
1487 indexes[x]=(IndexPacket) ClampToQuantum(stretch_map[
1488 ScaleQuantumToMap(indexes[x])].index);
1492 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1494 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1499 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1500 #pragma omp critical (MagickCore_ContrastStretchImageChannel)
1502 proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
1504 if (proceed == MagickFalse)
1508 image_view=DestroyCacheView(image_view);
1509 stretch_map=(MagickPixelPacket *) RelinquishMagickMemory(stretch_map);
1514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1518 % E n h a n c e I m a g e %
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % EnhanceImage() applies a digital filter that improves the quality of a
1527 % The format of the EnhanceImage method is:
1529 % Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1531 % A description of each parameter follows:
1533 % o image: the image.
1535 % o exception: return any errors or warnings in this structure.
1538 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
1540 #define Enhance(weight) \
1541 mean=((MagickRealType) r->red+pixel.red)/2; \
1542 distance=(MagickRealType) r->red-(MagickRealType) pixel.red; \
1543 distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
1544 mean)*distance*distance; \
1545 mean=((MagickRealType) r->green+pixel.green)/2; \
1546 distance=(MagickRealType) r->green-(MagickRealType) pixel.green; \
1547 distance_squared+=4.0*distance*distance; \
1548 mean=((MagickRealType) r->blue+pixel.blue)/2; \
1549 distance=(MagickRealType) r->blue-(MagickRealType) pixel.blue; \
1550 distance_squared+=QuantumScale*(3.0*((MagickRealType) \
1551 QuantumRange+1.0)-1.0-mean)*distance*distance; \
1552 mean=((MagickRealType) r->opacity+pixel.opacity)/2; \
1553 distance=(MagickRealType) r->opacity-(MagickRealType) pixel.opacity; \
1554 distance_squared+=QuantumScale*(3.0*((MagickRealType) \
1555 QuantumRange+1.0)-1.0-mean)*distance*distance; \
1556 if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
1557 QuantumRange/25.0f)) \
1559 aggregate.red+=(weight)*r->red; \
1560 aggregate.green+=(weight)*r->green; \
1561 aggregate.blue+=(weight)*r->blue; \
1562 aggregate.opacity+=(weight)*r->opacity; \
1563 total_weight+=(weight); \
1566 #define EnhanceImageTag "Enhance/Image"
1588 Initialize enhanced image attributes.
1590 assert(image != (const Image *) NULL);
1591 assert(image->signature == MagickSignature);
1592 if (image->debug != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 assert(exception != (ExceptionInfo *) NULL);
1595 assert(exception->signature == MagickSignature);
1596 if ((image->columns < 5) || (image->rows < 5))
1597 return((Image *) NULL);
1598 enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1600 if (enhance_image == (Image *) NULL)
1601 return((Image *) NULL);
1602 if (SetImageStorageClass(enhance_image,DirectClass) == MagickFalse)
1604 InheritException(exception,&enhance_image->exception);
1605 enhance_image=DestroyImage(enhance_image);
1606 return((Image *) NULL);
1613 (void) ResetMagickMemory(&zero,0,sizeof(zero));
1614 image_view=AcquireCacheView(image);
1615 enhance_view=AcquireCacheView(enhance_image);
1616 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1617 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1619 for (y=0; y < (ssize_t) image->rows; y++)
1621 register const PixelPacket
1627 register PixelPacket
1631 Read another scan line.
1633 if (status == MagickFalse)
1635 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1636 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1638 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1643 for (x=0; x < (ssize_t) image->columns; x++)
1657 register const PixelPacket
1661 Compute weighted average of target pixel color components.
1665 r=p+2*(image->columns+4)+2;
1668 Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0);
1669 r=p+(image->columns+4);
1670 Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0);
1671 r=p+2*(image->columns+4);
1672 Enhance(10.0); Enhance(40.0); Enhance(80.0); Enhance(40.0); Enhance(10.0);
1673 r=p+3*(image->columns+4);
1674 Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0);
1675 r=p+4*(image->columns+4);
1676 Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0);
1677 q->red=(Quantum) ((aggregate.red+(total_weight/2)-1)/total_weight);
1678 q->green=(Quantum) ((aggregate.green+(total_weight/2)-1)/total_weight);
1679 q->blue=(Quantum) ((aggregate.blue+(total_weight/2)-1)/total_weight);
1680 q->opacity=(Quantum) ((aggregate.opacity+(total_weight/2)-1)/
1685 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1687 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1692 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1693 #pragma omp critical (MagickCore_EnhanceImage)
1695 proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
1696 if (proceed == MagickFalse)
1700 enhance_view=DestroyCacheView(enhance_view);
1701 image_view=DestroyCacheView(image_view);
1702 return(enhance_image);
1706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % E q u a l i z e I m a g e %
1714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1716 % EqualizeImage() applies a histogram equalization to the image.
1718 % The format of the EqualizeImage method is:
1720 % MagickBooleanType EqualizeImage(Image *image)
1721 % MagickBooleanType EqualizeImageChannel(Image *image,
1722 % const ChannelType channel)
1724 % A description of each parameter follows:
1726 % o image: the image.
1728 % o channel: the channel.
1732 MagickExport MagickBooleanType EqualizeImage(Image *image)
1734 return(EqualizeImageChannel(image,DefaultChannels));
1737 MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
1738 const ChannelType channel)
1740 #define EqualizeImageTag "Equalize/Image"
1769 Allocate and initialize histogram arrays.
1771 assert(image != (Image *) NULL);
1772 assert(image->signature == MagickSignature);
1773 if (image->debug != MagickFalse)
1774 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1775 equalize_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
1776 sizeof(*equalize_map));
1777 histogram=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
1778 sizeof(*histogram));
1779 map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
1780 if ((equalize_map == (MagickPixelPacket *) NULL) ||
1781 (histogram == (MagickPixelPacket *) NULL) ||
1782 (map == (MagickPixelPacket *) NULL))
1784 if (map != (MagickPixelPacket *) NULL)
1785 map=(MagickPixelPacket *) RelinquishMagickMemory(map);
1786 if (histogram != (MagickPixelPacket *) NULL)
1787 histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram);
1788 if (equalize_map != (MagickPixelPacket *) NULL)
1789 equalize_map=(MagickPixelPacket *) RelinquishMagickMemory(equalize_map);
1790 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1796 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
1797 exception=(&image->exception);
1798 for (y=0; y < (ssize_t) image->rows; y++)
1800 register const IndexPacket
1803 register const PixelPacket
1809 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1810 if (p == (const PixelPacket *) NULL)
1812 indexes=GetVirtualIndexQueue(image);
1813 for (x=0; x < (ssize_t) image->columns; x++)
1815 if ((channel & RedChannel) != 0)
1816 histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++;
1817 if ((channel & GreenChannel) != 0)
1818 histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++;
1819 if ((channel & BlueChannel) != 0)
1820 histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++;
1821 if ((channel & OpacityChannel) != 0)
1822 histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++;
1823 if (((channel & IndexChannel) != 0) &&
1824 (image->colorspace == CMYKColorspace))
1825 histogram[ScaleQuantumToMap(indexes[x])].index++;
1830 Integrate the histogram to get the equalization map.
1832 (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
1833 for (i=0; i <= (ssize_t) MaxMap; i++)
1835 if ((channel & RedChannel) != 0)
1836 intensity.red+=histogram[i].red;
1837 if ((channel & GreenChannel) != 0)
1838 intensity.green+=histogram[i].green;
1839 if ((channel & BlueChannel) != 0)
1840 intensity.blue+=histogram[i].blue;
1841 if ((channel & OpacityChannel) != 0)
1842 intensity.opacity+=histogram[i].opacity;
1843 if (((channel & IndexChannel) != 0) &&
1844 (image->colorspace == CMYKColorspace))
1845 intensity.index+=histogram[i].index;
1849 white=map[(int) MaxMap];
1850 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
1851 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1852 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1854 for (i=0; i <= (ssize_t) MaxMap; i++)
1856 if (((channel & RedChannel) != 0) && (white.red != black.red))
1857 equalize_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1858 ((MaxMap*(map[i].red-black.red))/(white.red-black.red)));
1859 if (((channel & GreenChannel) != 0) && (white.green != black.green))
1860 equalize_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1861 ((MaxMap*(map[i].green-black.green))/(white.green-black.green)));
1862 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
1863 equalize_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1864 ((MaxMap*(map[i].blue-black.blue))/(white.blue-black.blue)));
1865 if (((channel & OpacityChannel) != 0) && (white.opacity != black.opacity))
1866 equalize_map[i].opacity=(MagickRealType) ScaleMapToQuantum(
1867 (MagickRealType) ((MaxMap*(map[i].opacity-black.opacity))/
1868 (white.opacity-black.opacity)));
1869 if ((((channel & IndexChannel) != 0) &&
1870 (image->colorspace == CMYKColorspace)) &&
1871 (white.index != black.index))
1872 equalize_map[i].index=(MagickRealType) ScaleMapToQuantum((MagickRealType)
1873 ((MaxMap*(map[i].index-black.index))/(white.index-black.index)));
1875 histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram);
1876 map=(MagickPixelPacket *) RelinquishMagickMemory(map);
1877 if (image->storage_class == PseudoClass)
1882 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1883 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1885 for (i=0; i < (ssize_t) image->colors; i++)
1887 if (((channel & RedChannel) != 0) && (white.red != black.red))
1888 image->colormap[i].red=ClampToQuantum(equalize_map[
1889 ScaleQuantumToMap(image->colormap[i].red)].red);
1890 if (((channel & GreenChannel) != 0) && (white.green != black.green))
1891 image->colormap[i].green=ClampToQuantum(equalize_map[
1892 ScaleQuantumToMap(image->colormap[i].green)].green);
1893 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
1894 image->colormap[i].blue=ClampToQuantum(equalize_map[
1895 ScaleQuantumToMap(image->colormap[i].blue)].blue);
1896 if (((channel & OpacityChannel) != 0) &&
1897 (white.opacity != black.opacity))
1898 image->colormap[i].opacity=ClampToQuantum(equalize_map[
1899 ScaleQuantumToMap(image->colormap[i].opacity)].opacity);
1907 exception=(&image->exception);
1908 image_view=AcquireCacheView(image);
1909 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1910 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1912 for (y=0; y < (ssize_t) image->rows; y++)
1914 register IndexPacket
1920 register PixelPacket
1923 if (status == MagickFalse)
1925 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1926 if (q == (PixelPacket *) NULL)
1931 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1932 for (x=0; x < (ssize_t) image->columns; x++)
1934 if (((channel & RedChannel) != 0) && (white.red != black.red))
1935 q->red=ClampToQuantum(equalize_map[ScaleQuantumToMap(q->red)].red);
1936 if (((channel & GreenChannel) != 0) && (white.green != black.green))
1937 q->green=ClampToQuantum(equalize_map[ScaleQuantumToMap(
1939 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
1940 q->blue=ClampToQuantum(equalize_map[ScaleQuantumToMap(q->blue)].blue);
1941 if (((channel & OpacityChannel) != 0) && (white.opacity != black.opacity))
1942 q->opacity=ClampToQuantum(equalize_map[ScaleQuantumToMap(
1943 q->opacity)].opacity);
1944 if ((((channel & IndexChannel) != 0) &&
1945 (image->colorspace == CMYKColorspace)) &&
1946 (white.index != black.index))
1947 indexes[x]=ClampToQuantum(equalize_map[ScaleQuantumToMap(
1948 indexes[x])].index);
1951 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1958 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1959 #pragma omp critical (MagickCore_EqualizeImageChannel)
1961 proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
1962 if (proceed == MagickFalse)
1966 image_view=DestroyCacheView(image_view);
1967 equalize_map=(MagickPixelPacket *) RelinquishMagickMemory(equalize_map);
1972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1976 % G a m m a I m a g e %
1980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1982 % GammaImage() gamma-corrects a particular image channel. The same
1983 % image viewed on different devices will have perceptual differences in the
1984 % way the image's intensities are represented on the screen. Specify
1985 % individual gamma levels for the red, green, and blue channels, or adjust
1986 % all three with the gamma parameter. Values typically range from 0.8 to 2.3.
1988 % You can also reduce the influence of a particular channel with a gamma
1991 % The format of the GammaImage method is:
1993 % MagickBooleanType GammaImage(Image *image,const double gamma)
1994 % MagickBooleanType GammaImageChannel(Image *image,
1995 % const ChannelType channel,const double gamma)
1997 % A description of each parameter follows:
1999 % o image: the image.
2001 % o channel: the channel.
2003 % o gamma: the image gamma.
2006 MagickExport MagickBooleanType GammaImage(Image *image,const char *level)
2018 assert(image != (Image *) NULL);
2019 assert(image->signature == MagickSignature);
2020 if (image->debug != MagickFalse)
2021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2022 if (level == (char *) NULL)
2023 return(MagickFalse);
2024 flags=ParseGeometry(level,&geometry_info);
2025 gamma.red=geometry_info.rho;
2026 gamma.green=geometry_info.sigma;
2027 if ((flags & SigmaValue) == 0)
2028 gamma.green=gamma.red;
2029 gamma.blue=geometry_info.xi;
2030 if ((flags & XiValue) == 0)
2031 gamma.blue=gamma.red;
2032 if ((gamma.red == 1.0) && (gamma.green == 1.0) && (gamma.blue == 1.0))
2034 if ((gamma.red == gamma.green) && (gamma.green == gamma.blue))
2035 status=GammaImageChannel(image,(const ChannelType) (RedChannel |
2036 GreenChannel | BlueChannel),(double) gamma.red);
2039 status=GammaImageChannel(image,RedChannel,(double) gamma.red);
2040 status|=GammaImageChannel(image,GreenChannel,(double) gamma.green);
2041 status|=GammaImageChannel(image,BlueChannel,(double) gamma.blue);
2043 return(status != 0 ? MagickTrue : MagickFalse);
2046 MagickExport MagickBooleanType GammaImageChannel(Image *image,
2047 const ChannelType channel,const double gamma)
2049 #define GammaCorrectImageTag "GammaCorrect/Image"
2073 Allocate and initialize gamma maps.
2075 assert(image != (Image *) NULL);
2076 assert(image->signature == MagickSignature);
2077 if (image->debug != MagickFalse)
2078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2081 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map));
2082 if (gamma_map == (Quantum *) NULL)
2083 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2085 (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
2087 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2088 #pragma omp parallel for schedule(dynamic,4)
2090 for (i=0; i <= (ssize_t) MaxMap; i++)
2091 gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
2092 MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
2093 if (image->storage_class == PseudoClass)
2096 Gamma-correct colormap.
2098 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2099 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2101 for (i=0; i < (ssize_t) image->colors; i++)
2103 if ((channel & RedChannel) != 0)
2104 image->colormap[i].red=gamma_map[
2105 ScaleQuantumToMap(image->colormap[i].red)];
2106 if ((channel & GreenChannel) != 0)
2107 image->colormap[i].green=gamma_map[
2108 ScaleQuantumToMap(image->colormap[i].green)];
2109 if ((channel & BlueChannel) != 0)
2110 image->colormap[i].blue=gamma_map[
2111 ScaleQuantumToMap(image->colormap[i].blue)];
2112 if ((channel & OpacityChannel) != 0)
2114 if (image->matte == MagickFalse)
2115 image->colormap[i].opacity=gamma_map[
2116 ScaleQuantumToMap(image->colormap[i].opacity)];
2118 image->colormap[i].opacity=(Quantum) QuantumRange-
2119 gamma_map[ScaleQuantumToMap((Quantum) (QuantumRange-
2120 image->colormap[i].opacity))];
2125 Gamma-correct image.
2129 exception=(&image->exception);
2130 image_view=AcquireCacheView(image);
2131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2132 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2134 for (y=0; y < (ssize_t) image->rows; y++)
2136 register IndexPacket
2142 register PixelPacket
2145 if (status == MagickFalse)
2147 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2148 if (q == (PixelPacket *) NULL)
2153 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2154 for (x=0; x < (ssize_t) image->columns; x++)
2156 if (channel == DefaultChannels)
2158 q->red=gamma_map[ScaleQuantumToMap(q->red)];
2159 q->green=gamma_map[ScaleQuantumToMap(q->green)];
2160 q->blue=gamma_map[ScaleQuantumToMap(q->blue)];
2164 if ((channel & RedChannel) != 0)
2165 q->red=gamma_map[ScaleQuantumToMap(q->red)];
2166 if ((channel & GreenChannel) != 0)
2167 q->green=gamma_map[ScaleQuantumToMap(q->green)];
2168 if ((channel & BlueChannel) != 0)
2169 q->blue=gamma_map[ScaleQuantumToMap(q->blue)];
2170 if ((channel & OpacityChannel) != 0)
2172 if (image->matte == MagickFalse)
2173 q->opacity=gamma_map[ScaleQuantumToMap(q->opacity)];
2175 q->opacity=(Quantum) QuantumRange-gamma_map[
2176 ScaleQuantumToMap((Quantum) GetAlphaPixelComponent(q))];
2181 if (((channel & IndexChannel) != 0) &&
2182 (image->colorspace == CMYKColorspace))
2183 for (x=0; x < (ssize_t) image->columns; x++)
2184 indexes[x]=gamma_map[ScaleQuantumToMap(indexes[x])];
2185 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2187 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2192 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2193 #pragma omp critical (MagickCore_GammaImageChannel)
2195 proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
2197 if (proceed == MagickFalse)
2201 image_view=DestroyCacheView(image_view);
2202 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
2203 if (image->gamma != 0.0)
2204 image->gamma*=gamma;
2209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2213 % H a l d C l u t I m a g e %
2217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2219 % HaldClutImage() applies a Hald color lookup table to the image. A Hald
2220 % color lookup table is a 3-dimensional color cube mapped to 2 dimensions.
2221 % Create it with the HALD coder. You can apply any color transformation to
2222 % the Hald image and then use this method to apply the transform to the
2225 % The format of the HaldClutImage method is:
2227 % MagickBooleanType HaldClutImage(Image *image,Image *hald_image)
2228 % MagickBooleanType HaldClutImageChannel(Image *image,
2229 % const ChannelType channel,Image *hald_image)
2231 % A description of each parameter follows:
2233 % o image: the image, which is replaced by indexed CLUT values
2235 % o hald_image: the color lookup table image for replacement color values.
2237 % o channel: the channel.
2241 static inline size_t MagickMin(const size_t x,const size_t y)
2248 MagickExport MagickBooleanType HaldClutImage(Image *image,
2249 const Image *hald_image)
2251 return(HaldClutImageChannel(image,DefaultChannels,hald_image));
2254 MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
2255 const ChannelType channel,const Image *hald_image)
2257 #define HaldClutImageTag "Clut/Image"
2259 typedef struct _HaldInfo
2286 **restrict resample_filter;
2296 assert(image != (Image *) NULL);
2297 assert(image->signature == MagickSignature);
2298 if (image->debug != MagickFalse)
2299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2300 assert(hald_image != (Image *) NULL);
2301 assert(hald_image->signature == MagickSignature);
2302 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2303 return(MagickFalse);
2304 if (image->matte == MagickFalse)
2305 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2311 length=MagickMin(hald_image->columns,hald_image->rows);
2312 for (level=2; (level*level*level) < length; level++) ;
2314 cube_size=level*level;
2315 width=(double) hald_image->columns;
2316 GetMagickPixelPacket(hald_image,&zero);
2317 exception=(&image->exception);
2318 resample_filter=AcquireResampleFilterThreadSet(hald_image,
2319 UndefinedVirtualPixelMethod,MagickTrue,exception);
2320 image_view=AcquireCacheView(image);
2321 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2322 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2324 for (y=0; y < (ssize_t) image->rows; y++)
2342 register IndexPacket
2348 register PixelPacket
2351 if (status == MagickFalse)
2353 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2354 if (q == (PixelPacket *) NULL)
2359 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2365 id=GetOpenMPThreadId();
2366 for (x=0; x < (ssize_t) image->columns; x++)
2368 point.x=QuantumScale*(level-1.0)*q->red;
2369 point.y=QuantumScale*(level-1.0)*q->green;
2370 point.z=QuantumScale*(level-1.0)*q->blue;
2371 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2372 point.x-=floor(point.x);
2373 point.y-=floor(point.y);
2374 point.z-=floor(point.z);
2375 (void) ResamplePixelColor(resample_filter[id],fmod(offset,width),
2376 floor(offset/width),&pixel1);
2377 (void) ResamplePixelColor(resample_filter[id],fmod(offset+level,width),
2378 floor((offset+level)/width),&pixel2);
2379 MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2,
2380 pixel2.opacity,point.y,&pixel3);
2382 (void) ResamplePixelColor(resample_filter[id],fmod(offset,width),
2383 floor(offset/width),&pixel1);
2384 (void) ResamplePixelColor(resample_filter[id],fmod(offset+level,width),
2385 floor((offset+level)/width),&pixel2);
2386 MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2,
2387 pixel2.opacity,point.y,&pixel4);
2388 MagickPixelCompositeAreaBlend(&pixel3,pixel3.opacity,&pixel4,
2389 pixel4.opacity,point.z,&pixel);
2390 if ((channel & RedChannel) != 0)
2391 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
2392 if ((channel & GreenChannel) != 0)
2393 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
2394 if ((channel & BlueChannel) != 0)
2395 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
2396 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
2397 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
2398 if (((channel & IndexChannel) != 0) &&
2399 (image->colorspace == CMYKColorspace))
2400 indexes[x]=ClampToQuantum(pixel.index);
2403 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2405 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2410 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2411 #pragma omp critical (MagickCore_HaldClutImageChannel)
2413 proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
2414 if (proceed == MagickFalse)
2418 image_view=DestroyCacheView(image_view);
2419 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
2424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2428 % L e v e l I m a g e %
2432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2434 % LevelImage() adjusts the levels of a particular image channel by
2435 % scaling the colors falling between specified white and black points to
2436 % the full available quantum range.
2438 % The parameters provided represent the black, and white points. The black
2439 % point specifies the darkest color in the image. Colors darker than the
2440 % black point are set to zero. White point specifies the lightest color in
2441 % the image. Colors brighter than the white point are set to the maximum
2444 % If a '!' flag is given, map black and white colors to the given levels
2445 % rather than mapping those levels to black and white. See
2446 % LevelizeImageChannel() and LevelizeImageChannel(), below.
2448 % Gamma specifies a gamma correction to apply to the image.
2450 % The format of the LevelImage method is:
2452 % MagickBooleanType LevelImage(Image *image,const char *levels)
2454 % A description of each parameter follows:
2456 % o image: the image.
2458 % o levels: Specify the levels where the black and white points have the
2459 % range of 0-QuantumRange, and gamma has the range 0-10 (e.g. 10x90%+2).
2460 % A '!' flag inverts the re-mapping.
2464 MagickExport MagickBooleanType LevelImage(Image *image,const char *levels)
2483 if (levels == (char *) NULL)
2484 return(MagickFalse);
2485 flags=ParseGeometry(levels,&geometry_info);
2486 black_point=geometry_info.rho;
2487 white_point=(double) QuantumRange;
2488 if ((flags & SigmaValue) != 0)
2489 white_point=geometry_info.sigma;
2491 if ((flags & XiValue) != 0)
2492 gamma=geometry_info.xi;
2493 if ((flags & PercentValue) != 0)
2495 black_point*=(double) image->columns*image->rows/100.0;
2496 white_point*=(double) image->columns*image->rows/100.0;
2498 if ((flags & SigmaValue) == 0)
2499 white_point=(double) QuantumRange-black_point;
2500 if ((flags & AspectValue ) == 0)
2501 status=LevelImageChannel(image,DefaultChannels,black_point,white_point,
2504 status=LevelizeImage(image,black_point,white_point,gamma);
2509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2513 % L e v e l i z e I m a g e %
2517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2519 % LevelizeImage() applies the normal level operation to the image, spreading
2520 % out the values between the black and white points over the entire range of
2521 % values. Gamma correction is also applied after the values has been mapped.
2523 % It is typically used to improve image contrast, or to provide a controlled
2524 % linear threshold for the image. If the black and white points are set to
2525 % the minimum and maximum values found in the image, the image can be
2526 % normalized. or by swapping black and white values, negate the image.
2528 % The format of the LevelizeImage method is:
2530 % MagickBooleanType LevelizeImage(Image *image,const double black_point,
2531 % const double white_point,const double gamma)
2532 % MagickBooleanType LevelizeImageChannel(Image *image,
2533 % const ChannelType channel,const double black_point,
2534 % const double white_point,const double gamma)
2536 % A description of each parameter follows:
2538 % o image: the image.
2540 % o channel: the channel.
2542 % o black_point: The level which is to be mapped to zero (black)
2544 % o white_point: The level which is to be mapped to QuantiumRange (white)
2546 % o gamma: adjust gamma by this factor before mapping values.
2547 % use 1.0 for purely linear stretching of image color values
2551 MagickExport MagickBooleanType LevelizeImage(Image *image,
2552 const double black_point,const double white_point,const double gamma)
2557 status=LevelizeImageChannel(image,DefaultChannels,black_point,white_point,
2562 MagickExport MagickBooleanType LevelImageChannel(Image *image,
2563 const ChannelType channel,const double black_point,const double white_point,
2566 #define LevelImageTag "Level/Image"
2567 #define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
2568 pow(scale*((double) (x)-black_point),1.0/gamma)))
2592 Allocate and initialize levels map.
2594 assert(image != (Image *) NULL);
2595 assert(image->signature == MagickSignature);
2596 if (image->debug != MagickFalse)
2597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2598 scale = (white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
2599 if (image->storage_class == PseudoClass)
2600 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2601 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2603 for (i=0; i < (ssize_t) image->colors; i++)
2608 if ((channel & RedChannel) != 0)
2609 image->colormap[i].red=LevelQuantum(image->colormap[i].red);
2610 if ((channel & GreenChannel) != 0)
2611 image->colormap[i].green=LevelQuantum(image->colormap[i].green);
2612 if ((channel & BlueChannel) != 0)
2613 image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
2614 if ((channel & OpacityChannel) != 0)
2615 image->colormap[i].opacity=LevelQuantum(image->colormap[i].opacity);
2622 exception=(&image->exception);
2623 image_view=AcquireCacheView(image);
2624 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2625 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2627 for (y=0; y < (ssize_t) image->rows; y++)
2629 register IndexPacket
2635 register PixelPacket
2638 if (status == MagickFalse)
2640 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2641 if (q == (PixelPacket *) NULL)
2646 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2647 for (x=0; x < (ssize_t) image->columns; x++)
2649 if ((channel & RedChannel) != 0)
2650 q->red=LevelQuantum(q->red);
2651 if ((channel & GreenChannel) != 0)
2652 q->green=LevelQuantum(q->green);
2653 if ((channel & BlueChannel) != 0)
2654 q->blue=LevelQuantum(q->blue);
2655 if (((channel & OpacityChannel) != 0) &&
2656 (image->matte == MagickTrue))
2657 q->opacity=(Quantum) (QuantumRange-LevelQuantum(QuantumRange-
2659 if (((channel & IndexChannel) != 0) &&
2660 (image->colorspace == CMYKColorspace))
2661 indexes[x]=LevelQuantum(indexes[x]);
2664 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2666 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2671 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2672 #pragma omp critical (MagickCore_LevelImageChannel)
2674 proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
2675 if (proceed == MagickFalse)
2679 image_view=DestroyCacheView(image_view);
2684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2688 % L e v e l i z e I m a g e C h a n n e l %
2692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2694 % LevelizeImageChannel() applies the reversed LevelImage() operation to just
2695 % the specific channels specified. It compresses the full range of color
2696 % values, so that they lie between the given black and white points. Gamma is
2697 % applied before the values are mapped.
2699 % LevelizeImageChannel() can be called with by using a +level command line
2700 % API option, or using a '!' on a -level or LevelImage() geometry string.
2702 % It can be used for example de-contrast a greyscale image to the exact
2703 % levels specified. Or by using specific levels for each channel of an image
2704 % you can convert a gray-scale image to any linear color gradient, according
2707 % The format of the LevelizeImageChannel method is:
2709 % MagickBooleanType LevelizeImageChannel(Image *image,
2710 % const ChannelType channel,const char *levels)
2712 % A description of each parameter follows:
2714 % o image: the image.
2716 % o channel: the channel.
2718 % o black_point: The level to map zero (black) to.
2720 % o white_point: The level to map QuantiumRange (white) to.
2722 % o gamma: adjust gamma by this factor before mapping values.
2725 MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
2726 const ChannelType channel,const double black_point,const double white_point,
2729 #define LevelizeImageTag "Levelize/Image"
2730 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
2731 pow((double)(QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
2753 Allocate and initialize levels map.
2755 assert(image != (Image *) NULL);
2756 assert(image->signature == MagickSignature);
2757 if (image->debug != MagickFalse)
2758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2759 if (image->storage_class == PseudoClass)
2760 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2761 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2763 for (i=0; i < (ssize_t) image->colors; i++)
2768 if ((channel & RedChannel) != 0)
2769 image->colormap[i].red=LevelizeValue(image->colormap[i].red);
2770 if ((channel & GreenChannel) != 0)
2771 image->colormap[i].green=LevelizeValue(image->colormap[i].green);
2772 if ((channel & BlueChannel) != 0)
2773 image->colormap[i].blue=LevelizeValue(image->colormap[i].blue);
2774 if ((channel & OpacityChannel) != 0)
2775 image->colormap[i].opacity=LevelizeValue(image->colormap[i].opacity);
2782 exception=(&image->exception);
2783 image_view=AcquireCacheView(image);
2784 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2785 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2787 for (y=0; y < (ssize_t) image->rows; y++)
2789 register IndexPacket
2795 register PixelPacket
2798 if (status == MagickFalse)
2800 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2801 if (q == (PixelPacket *) NULL)
2806 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2807 for (x=0; x < (ssize_t) image->columns; x++)
2809 if ((channel & RedChannel) != 0)
2810 q->red=LevelizeValue(q->red);
2811 if ((channel & GreenChannel) != 0)
2812 q->green=LevelizeValue(q->green);
2813 if ((channel & BlueChannel) != 0)
2814 q->blue=LevelizeValue(q->blue);
2815 if (((channel & OpacityChannel) != 0) &&
2816 (image->matte == MagickTrue))
2817 q->opacity=LevelizeValue(q->opacity);
2818 if (((channel & IndexChannel) != 0) &&
2819 (image->colorspace == CMYKColorspace))
2820 indexes[x]=LevelizeValue(indexes[x]);
2823 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2825 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2830 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2831 #pragma omp critical (MagickCore_LevelizeImageChannel)
2833 proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
2834 if (proceed == MagickFalse)
2842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2846 % L e v e l I m a g e C o l o r s %
2850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2852 % LevelImageColor() maps the given color to "black" and "white" values,
2853 % linearly spreading out the colors, and level values on a channel by channel
2854 % bases, as per LevelImage(). The given colors allows you to specify
2855 % different level ranges for each of the color channels seperatally.
2857 % If the boolean 'invert' is set true the image values will modifyed in the
2858 % reverse direction. That is any existing "black" and "white" colors in the
2859 % image will become the color values given, with all other values compressed
2860 % appropriatally. This effectivally maps a greyscale gradient into the given
2863 % The format of the LevelColorsImageChannel method is:
2865 % MagickBooleanType LevelColorsImage(Image *image,
2866 % const MagickPixelPacket *black_color,
2867 % const MagickPixelPacket *white_color,const MagickBooleanType invert)
2868 % MagickBooleanType LevelColorsImageChannel(Image *image,
2869 % const ChannelType channel,const MagickPixelPacket *black_color,
2870 % const MagickPixelPacket *white_color,const MagickBooleanType invert)
2872 % A description of each parameter follows:
2874 % o image: the image.
2876 % o channel: the channel.
2878 % o black_color: The color to map black to/from
2880 % o white_point: The color to map white to/from
2882 % o invert: if true map the colors (levelize), rather than from (level)
2886 MagickExport MagickBooleanType LevelColorsImage(Image *image,
2887 const MagickPixelPacket *black_color,const MagickPixelPacket *white_color,
2888 const MagickBooleanType invert)
2893 status=LevelColorsImageChannel(image,DefaultChannels,black_color,white_color,
2898 MagickExport MagickBooleanType LevelColorsImageChannel(Image *image,
2899 const ChannelType channel,const MagickPixelPacket *black_color,
2900 const MagickPixelPacket *white_color,const MagickBooleanType invert)
2906 Allocate and initialize levels map.
2908 assert(image != (Image *) NULL);
2909 assert(image->signature == MagickSignature);
2910 if (image->debug != MagickFalse)
2911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2913 if (invert == MagickFalse)
2915 if ((channel & RedChannel) != 0)
2916 status|=LevelImageChannel(image,RedChannel,
2917 black_color->red,white_color->red,(double) 1.0);
2918 if ((channel & GreenChannel) != 0)
2919 status|=LevelImageChannel(image,GreenChannel,
2920 black_color->green,white_color->green,(double) 1.0);
2921 if ((channel & BlueChannel) != 0)
2922 status|=LevelImageChannel(image,BlueChannel,
2923 black_color->blue,white_color->blue,(double) 1.0);
2924 if (((channel & OpacityChannel) != 0) &&
2925 (image->matte == MagickTrue))
2926 status|=LevelImageChannel(image,OpacityChannel,
2927 black_color->opacity,white_color->opacity,(double) 1.0);
2928 if (((channel & IndexChannel) != 0) &&
2929 (image->colorspace == CMYKColorspace))
2930 status|=LevelImageChannel(image,IndexChannel,
2931 black_color->index,white_color->index,(double) 1.0);
2935 if ((channel & RedChannel) != 0)
2936 status|=LevelizeImageChannel(image,RedChannel,
2937 black_color->red,white_color->red,(double) 1.0);
2938 if ((channel & GreenChannel) != 0)
2939 status|=LevelizeImageChannel(image,GreenChannel,
2940 black_color->green,white_color->green,(double) 1.0);
2941 if ((channel & BlueChannel) != 0)
2942 status|=LevelizeImageChannel(image,BlueChannel,
2943 black_color->blue,white_color->blue,(double) 1.0);
2944 if (((channel & OpacityChannel) != 0) &&
2945 (image->matte == MagickTrue))
2946 status|=LevelizeImageChannel(image,OpacityChannel,
2947 black_color->opacity,white_color->opacity,(double) 1.0);
2948 if (((channel & IndexChannel) != 0) &&
2949 (image->colorspace == CMYKColorspace))
2950 status|=LevelizeImageChannel(image,IndexChannel,
2951 black_color->index,white_color->index,(double) 1.0);
2953 return(status == 0 ? MagickFalse : MagickTrue);
2957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2961 % L i n e a r S t r e t c h I m a g e %
2965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2967 % The LinearStretchImage() discards any pixels below the black point and
2968 % above the white point and levels the remaining pixels.
2970 % The format of the LinearStretchImage method is:
2972 % MagickBooleanType LinearStretchImage(Image *image,
2973 % const double black_point,const double white_point)
2975 % A description of each parameter follows:
2977 % o image: the image.
2979 % o black_point: the black point.
2981 % o white_point: the white point.
2984 MagickExport MagickBooleanType LinearStretchImage(Image *image,
2985 const double black_point,const double white_point)
2987 #define LinearStretchImageTag "LinearStretch/Image"
3008 Allocate histogram and linear map.
3010 assert(image != (Image *) NULL);
3011 assert(image->signature == MagickSignature);
3012 histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3013 sizeof(*histogram));
3014 if (histogram == (MagickRealType *) NULL)
3015 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3020 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
3021 exception=(&image->exception);
3022 for (y=0; y < (ssize_t) image->rows; y++)
3024 register const PixelPacket
3030 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3031 if (p == (const PixelPacket *) NULL)
3033 for (x=(ssize_t) image->columns-1; x >= 0; x--)
3035 histogram[ScaleQuantumToMap(PixelIntensityToQuantum(p))]++;
3040 Find the histogram boundaries by locating the black and white point levels.
3042 number_pixels=(MagickSizeType) image->columns*image->rows;
3044 for (black=0; black < (ssize_t) MaxMap; black++)
3046 intensity+=histogram[black];
3047 if (intensity >= black_point)
3051 for (white=(ssize_t) MaxMap; white != 0; white--)
3053 intensity+=histogram[white];
3054 if (intensity >= white_point)
3057 histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
3058 status=LevelImageChannel(image,DefaultChannels,(double) black,(double) white,
3064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3068 % M o d u l a t e I m a g e %
3072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3074 % ModulateImage() lets you control the brightness, saturation, and hue
3075 % of an image. Modulate represents the brightness, saturation, and hue
3076 % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
3077 % modulation is lightness, saturation, and hue. And if the colorspace is
3078 % HWB, use blackness, whiteness, and hue.
3080 % The format of the ModulateImage method is:
3082 % MagickBooleanType ModulateImage(Image *image,const char *modulate)
3084 % A description of each parameter follows:
3086 % o image: the image.
3088 % o modulate: Define the percent change in brightness, saturation, and
3093 static void ModulateHSB(const double percent_hue,
3094 const double percent_saturation,const double percent_brightness,
3095 Quantum *red,Quantum *green,Quantum *blue)
3103 Increase or decrease color brightness, saturation, or hue.
3105 assert(red != (Quantum *) NULL);
3106 assert(green != (Quantum *) NULL);
3107 assert(blue != (Quantum *) NULL);
3108 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
3109 hue+=0.5*(0.01*percent_hue-1.0);
3114 saturation*=0.01*percent_saturation;
3115 brightness*=0.01*percent_brightness;
3116 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
3119 static void ModulateHSL(const double percent_hue,
3120 const double percent_saturation,const double percent_lightness,
3121 Quantum *red,Quantum *green,Quantum *blue)
3129 Increase or decrease color lightness, saturation, or hue.
3131 assert(red != (Quantum *) NULL);
3132 assert(green != (Quantum *) NULL);
3133 assert(blue != (Quantum *) NULL);
3134 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
3135 hue+=0.5*(0.01*percent_hue-1.0);
3140 saturation*=0.01*percent_saturation;
3141 lightness*=0.01*percent_lightness;
3142 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
3145 static void ModulateHWB(const double percent_hue,const double percent_whiteness, const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue)
3153 Increase or decrease color blackness, whiteness, or hue.
3155 assert(red != (Quantum *) NULL);
3156 assert(green != (Quantum *) NULL);
3157 assert(blue != (Quantum *) NULL);
3158 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
3159 hue+=0.5*(0.01*percent_hue-1.0);
3164 blackness*=0.01*percent_blackness;
3165 whiteness*=0.01*percent_whiteness;
3166 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
3169 MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
3171 #define ModulateImageTag "Modulate/Image"
3209 Initialize modulate table.
3211 assert(image != (Image *) NULL);
3212 assert(image->signature == MagickSignature);
3213 if (image->debug != MagickFalse)
3214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3215 if (modulate == (char *) NULL)
3216 return(MagickFalse);
3217 flags=ParseGeometry(modulate,&geometry_info);
3218 percent_brightness=geometry_info.rho;
3219 percent_saturation=geometry_info.sigma;
3220 if ((flags & SigmaValue) == 0)
3221 percent_saturation=100.0;
3222 percent_hue=geometry_info.xi;
3223 if ((flags & XiValue) == 0)
3225 colorspace=UndefinedColorspace;
3226 artifact=GetImageArtifact(image,"modulate:colorspace");
3227 if (artifact != (const char *) NULL)
3228 colorspace=(ColorspaceType) ParseMagickOption(MagickColorspaceOptions,
3229 MagickFalse,artifact);
3230 if (image->storage_class == PseudoClass)
3235 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3236 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3238 for (i=0; i < (ssize_t) image->colors; i++)
3243 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3244 &image->colormap[i].red,&image->colormap[i].green,
3245 &image->colormap[i].blue);
3251 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3252 &image->colormap[i].red,&image->colormap[i].green,
3253 &image->colormap[i].blue);
3258 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3259 &image->colormap[i].red,&image->colormap[i].green,
3260 &image->colormap[i].blue);
3270 exception=(&image->exception);
3271 image_view=AcquireCacheView(image);
3272 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3273 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3275 for (y=0; y < (ssize_t) image->rows; y++)
3280 register PixelPacket
3283 if (status == MagickFalse)
3285 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3286 if (q == (PixelPacket *) NULL)
3291 for (x=0; x < (ssize_t) image->columns; x++)
3297 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3298 &q->red,&q->green,&q->blue);
3304 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3305 &q->red,&q->green,&q->blue);
3310 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3311 &q->red,&q->green,&q->blue);
3317 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3319 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3324 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3325 #pragma omp critical (MagickCore_ModulateImage)
3327 proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
3328 if (proceed == MagickFalse)
3332 image_view=DestroyCacheView(image_view);
3337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3341 % N e g a t e I m a g e %
3345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3347 % NegateImage() negates the colors in the reference image. The grayscale
3348 % option means that only grayscale values within the image are negated.
3350 % The format of the NegateImageChannel method is:
3352 % MagickBooleanType NegateImage(Image *image,
3353 % const MagickBooleanType grayscale)
3354 % MagickBooleanType NegateImageChannel(Image *image,
3355 % const ChannelType channel,const MagickBooleanType grayscale)
3357 % A description of each parameter follows:
3359 % o image: the image.
3361 % o channel: the channel.
3363 % o grayscale: If MagickTrue, only negate grayscale pixels within the image.
3367 MagickExport MagickBooleanType NegateImage(Image *image,
3368 const MagickBooleanType grayscale)
3373 status=NegateImageChannel(image,DefaultChannels,grayscale);
3377 MagickExport MagickBooleanType NegateImageChannel(Image *image,
3378 const ChannelType channel,const MagickBooleanType grayscale)
3380 #define NegateImageTag "Negate/Image"
3400 assert(image != (Image *) NULL);
3401 assert(image->signature == MagickSignature);
3402 if (image->debug != MagickFalse)
3403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3404 if (image->storage_class == PseudoClass)
3409 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3410 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3412 for (i=0; i < (ssize_t) image->colors; i++)
3414 if (grayscale != MagickFalse)
3415 if ((image->colormap[i].red != image->colormap[i].green) ||
3416 (image->colormap[i].green != image->colormap[i].blue))
3418 if ((channel & RedChannel) != 0)
3419 image->colormap[i].red=(Quantum) QuantumRange-
3420 image->colormap[i].red;
3421 if ((channel & GreenChannel) != 0)
3422 image->colormap[i].green=(Quantum) QuantumRange-
3423 image->colormap[i].green;
3424 if ((channel & BlueChannel) != 0)
3425 image->colormap[i].blue=(Quantum) QuantumRange-
3426 image->colormap[i].blue;
3434 exception=(&image->exception);
3435 image_view=AcquireCacheView(image);
3436 if (grayscale != MagickFalse)
3438 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3439 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3441 for (y=0; y < (ssize_t) image->rows; y++)
3446 register IndexPacket
3452 register PixelPacket
3455 if (status == MagickFalse)
3457 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3459 if (q == (PixelPacket *) NULL)
3464 indexes=GetCacheViewAuthenticIndexQueue(image_view);
3465 for (x=0; x < (ssize_t) image->columns; x++)
3467 if ((q->red != q->green) || (q->green != q->blue))
3472 if ((channel & RedChannel) != 0)
3473 q->red=(Quantum) QuantumRange-q->red;
3474 if ((channel & GreenChannel) != 0)
3475 q->green=(Quantum) QuantumRange-q->green;
3476 if ((channel & BlueChannel) != 0)
3477 q->blue=(Quantum) QuantumRange-q->blue;
3478 if ((channel & OpacityChannel) != 0)
3479 q->opacity=(Quantum) QuantumRange-q->opacity;
3480 if (((channel & IndexChannel) != 0) &&
3481 (image->colorspace == CMYKColorspace))
3482 indexes[x]=(IndexPacket) QuantumRange-indexes[x];
3485 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3486 if (sync == MagickFalse)
3488 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3494 #pragma omp critical (MagickCore_NegateImageChannel)
3496 proceed=SetImageProgress(image,NegateImageTag,progress++,
3498 if (proceed == MagickFalse)
3502 image_view=DestroyCacheView(image_view);
3508 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3509 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3511 for (y=0; y < (ssize_t) image->rows; y++)
3513 register IndexPacket
3519 register PixelPacket
3522 if (status == MagickFalse)
3524 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3525 if (q == (PixelPacket *) NULL)
3530 indexes=GetCacheViewAuthenticIndexQueue(image_view);
3531 for (x=0; x < (ssize_t) image->columns; x++)
3533 if ((channel & RedChannel) != 0)
3534 q->red=(Quantum) QuantumRange-q->red;
3535 if ((channel & GreenChannel) != 0)
3536 q->green=(Quantum) QuantumRange-q->green;
3537 if ((channel & BlueChannel) != 0)
3538 q->blue=(Quantum) QuantumRange-q->blue;
3539 if ((channel & OpacityChannel) != 0)
3540 q->opacity=(Quantum) QuantumRange-q->opacity;
3541 if (((channel & IndexChannel) != 0) &&
3542 (image->colorspace == CMYKColorspace))
3543 indexes[x]=(IndexPacket) QuantumRange-indexes[x];
3546 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3548 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3553 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3554 #pragma omp critical (MagickCore_NegateImageChannel)
3556 proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
3557 if (proceed == MagickFalse)
3561 image_view=DestroyCacheView(image_view);
3566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3570 % N o r m a l i z e I m a g e %
3574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3576 % The NormalizeImage() method enhances the contrast of a color image by
3577 % mapping the darkest 2 percent of all pixel to black and the brightest
3578 % 1 percent to white.
3580 % The format of the NormalizeImage method is:
3582 % MagickBooleanType NormalizeImage(Image *image)
3583 % MagickBooleanType NormalizeImageChannel(Image *image,
3584 % const ChannelType channel)
3586 % A description of each parameter follows:
3588 % o image: the image.
3590 % o channel: the channel.
3594 MagickExport MagickBooleanType NormalizeImage(Image *image)
3599 status=NormalizeImageChannel(image,DefaultChannels);
3603 MagickExport MagickBooleanType NormalizeImageChannel(Image *image,
3604 const ChannelType channel)
3610 black_point=(double) image->columns*image->rows*0.0015;
3611 white_point=(double) image->columns*image->rows*0.9995;
3612 return(ContrastStretchImageChannel(image,channel,black_point,white_point));
3616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3620 % S i g m o i d a l C o n t r a s t I m a g e %
3624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3626 % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear
3627 % sigmoidal contrast algorithm. Increase the contrast of the image using a
3628 % sigmoidal transfer function without saturating highlights or shadows.
3629 % Contrast indicates how much to increase the contrast (0 is none; 3 is
3630 % typical; 20 is pushing it); mid-point indicates where midtones fall in the
3631 % resultant image (0 is white; 50% is middle-gray; 100% is black). Set
3632 % sharpen to MagickTrue to increase the image contrast otherwise the contrast
3635 % The format of the SigmoidalContrastImage method is:
3637 % MagickBooleanType SigmoidalContrastImage(Image *image,
3638 % const MagickBooleanType sharpen,const char *levels)
3639 % MagickBooleanType SigmoidalContrastImageChannel(Image *image,
3640 % const ChannelType channel,const MagickBooleanType sharpen,
3641 % const double contrast,const double midpoint)
3643 % A description of each parameter follows:
3645 % o image: the image.
3647 % o channel: the channel.
3649 % o sharpen: Increase or decrease image contrast.
3651 % o contrast: control the "shoulder" of the contast curve.
3653 % o midpoint: control the "toe" of the contast curve.
3657 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
3658 const MagickBooleanType sharpen,const char *levels)
3669 flags=ParseGeometry(levels,&geometry_info);
3670 if ((flags & SigmaValue) == 0)
3671 geometry_info.sigma=1.0*QuantumRange/2.0;
3672 if ((flags & PercentValue) != 0)
3673 geometry_info.sigma=1.0*QuantumRange*geometry_info.sigma/100.0;
3674 status=SigmoidalContrastImageChannel(image,DefaultChannels,sharpen,
3675 geometry_info.rho,geometry_info.sigma);
3679 MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
3680 const ChannelType channel,const MagickBooleanType sharpen,
3681 const double contrast,const double midpoint)
3683 #define SigmoidalContrastImageTag "SigmoidalContrast/Image"
3707 Allocate and initialize sigmoidal maps.
3709 assert(image != (Image *) NULL);
3710 assert(image->signature == MagickSignature);
3711 if (image->debug != MagickFalse)
3712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3713 sigmoidal_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
3714 sizeof(*sigmoidal_map));
3715 if (sigmoidal_map == (MagickRealType *) NULL)
3716 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3718 (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
3719 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3720 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3722 for (i=0; i <= (ssize_t) MaxMap; i++)
3724 if (sharpen != MagickFalse)
3726 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3727 (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
3728 (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
3729 (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
3730 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
3731 (double) QuantumRange)))))+0.5));
3734 sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
3735 (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
3736 (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
3737 (1.0+exp(contrast*(midpoint/(double) QuantumRange-1.0))))-(1.0/
3738 (1.0+exp(midpoint/(double) QuantumRange*contrast))))))/
3739 (1.0/(1.0+exp(midpoint/(double) QuantumRange*contrast))+
3740 ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
3741 (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
3742 (double) QuantumRange*contrast))))))/contrast)));
3744 if (image->storage_class == PseudoClass)
3747 Sigmoidal-contrast enhance colormap.
3749 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3750 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3752 for (i=0; i < (ssize_t) image->colors; i++)
3754 if ((channel & RedChannel) != 0)
3755 image->colormap[i].red=ClampToQuantum(sigmoidal_map[
3756 ScaleQuantumToMap(image->colormap[i].red)]);
3757 if ((channel & GreenChannel) != 0)
3758 image->colormap[i].green=ClampToQuantum(sigmoidal_map[
3759 ScaleQuantumToMap(image->colormap[i].green)]);
3760 if ((channel & BlueChannel) != 0)
3761 image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
3762 ScaleQuantumToMap(image->colormap[i].blue)]);
3763 if ((channel & OpacityChannel) != 0)
3764 image->colormap[i].opacity=ClampToQuantum(sigmoidal_map[
3765 ScaleQuantumToMap(image->colormap[i].opacity)]);
3769 Sigmoidal-contrast enhance image.
3773 exception=(&image->exception);
3774 image_view=AcquireCacheView(image);
3775 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3776 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3778 for (y=0; y < (ssize_t) image->rows; y++)
3780 register IndexPacket
3786 register PixelPacket
3789 if (status == MagickFalse)
3791 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3792 if (q == (PixelPacket *) NULL)
3797 indexes=GetCacheViewAuthenticIndexQueue(image_view);
3798 for (x=0; x < (ssize_t) image->columns; x++)
3800 if ((channel & RedChannel) != 0)
3801 q->red=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->red)]);
3802 if ((channel & GreenChannel) != 0)
3803 q->green=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->green)]);
3804 if ((channel & BlueChannel) != 0)
3805 q->blue=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->blue)]);
3806 if ((channel & OpacityChannel) != 0)
3807 q->opacity=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q->opacity)]);
3808 if (((channel & IndexChannel) != 0) &&
3809 (image->colorspace == CMYKColorspace))
3810 indexes[x]=(IndexPacket) ClampToQuantum(sigmoidal_map[
3811 ScaleQuantumToMap(indexes[x])]);
3814 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3816 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3821 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3822 #pragma omp critical (MagickCore_SigmoidalContrastImageChannel)
3824 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
3826 if (proceed == MagickFalse)
3830 image_view=DestroyCacheView(image_view);
3831 sigmoidal_map=(MagickRealType *) RelinquishMagickMemory(sigmoidal_map);