43#include "MagickCore/studio.h"
44#include "MagickCore/accelerate-private.h"
45#include "MagickCore/artifact.h"
46#include "MagickCore/attribute.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/cache-private.h"
49#include "MagickCore/cache-view.h"
50#include "MagickCore/channel.h"
51#include "MagickCore/color.h"
52#include "MagickCore/color-private.h"
53#include "MagickCore/colorspace.h"
54#include "MagickCore/colorspace-private.h"
55#include "MagickCore/composite-private.h"
56#include "MagickCore/enhance.h"
57#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/fx.h"
60#include "MagickCore/gem.h"
61#include "MagickCore/gem-private.h"
62#include "MagickCore/geometry.h"
63#include "MagickCore/histogram.h"
64#include "MagickCore/image.h"
65#include "MagickCore/image-private.h"
66#include "MagickCore/memory_.h"
67#include "MagickCore/monitor.h"
68#include "MagickCore/monitor-private.h"
69#include "MagickCore/option.h"
70#include "MagickCore/pixel.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/pixel-private.h"
73#include "MagickCore/property.h"
74#include "MagickCore/quantum.h"
75#include "MagickCore/quantum-private.h"
76#include "MagickCore/resample.h"
77#include "MagickCore/resample-private.h"
78#include "MagickCore/resource_.h"
79#include "MagickCore/statistic.h"
80#include "MagickCore/string_.h"
81#include "MagickCore/string-private.h"
82#include "MagickCore/thread-private.h"
83#include "MagickCore/threshold.h"
84#include "MagickCore/token.h"
85#include "MagickCore/xml-tree.h"
86#include "MagickCore/xml-tree-private.h"
113MagickExport MagickBooleanType AutoGammaImage(Image *image,
114 ExceptionInfo *exception)
129 if (image->channel_mask == AllChannels)
134 (void) GetImageMean(image,&mean,&sans,exception);
135 gamma=log(mean*QuantumScale)/log_mean;
136 return(LevelImage(image,0.0,(
double) QuantumRange,gamma,exception));
142 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
147 PixelChannel channel = GetPixelChannelChannel(image,i);
148 PixelTrait traits = GetPixelChannelTraits(image,channel);
149 if ((traits & UpdatePixelTrait) == 0)
151 channel_mask=SetImageChannelMask(image,(ChannelType) (1UL << i));
152 status=GetImageMean(image,&mean,&sans,exception);
153 gamma=log(mean*QuantumScale)/log_mean;
154 status&=(MagickStatusType) LevelImage(image,0.0,(
double) QuantumRange,gamma,
156 (void) SetImageChannelMask(image,channel_mask);
157 if (status == MagickFalse)
160 return(status != 0 ? MagickTrue : MagickFalse);
188MagickExport MagickBooleanType AutoLevelImage(Image *image,
189 ExceptionInfo *exception)
191 return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
225MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
226 const double brightness,
const double contrast,ExceptionInfo *exception)
228#define BrightnessContrastImageTag "BrightnessContrast/Image"
241 assert(image != (Image *) NULL);
242 assert(image->signature == MagickCoreSignature);
243 if (IsEventLogging() != MagickFalse)
244 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
245 slope=100.0*MagickSafeReciprocal(100.0-contrast);
247 slope=0.01*contrast+1.0;
248 intercept=(0.01*brightness-0.5)*slope+0.5;
249 coefficients[0]=slope;
250 coefficients[1]=intercept;
251 status=FunctionImage(image,PolynomialFunction,2,coefficients,exception);
303static void ClipCLAHEHistogram(
const double clip_limit,
const size_t number_bins,
306#define NumberCLAHEGrays (65536)
318 if (number_bins == 0)
321 for (i=0; i < (ssize_t) number_bins; i++)
322 if (histogram[i] > clip_limit)
323 cumulative_excess+=(ssize_t) (histogram[i]-clip_limit);
327 step=cumulative_excess/(ssize_t) number_bins;
328 excess=(ssize_t) (clip_limit-step);
329 for (i=0; i < (ssize_t) number_bins; i++)
331 if ((
double) histogram[i] > clip_limit)
332 histogram[i]=(size_t) clip_limit;
334 if ((ssize_t) histogram[i] > excess)
336 cumulative_excess-=(ssize_t) histogram[i]-excess;
337 histogram[i]=(size_t) clip_limit;
341 cumulative_excess-=step;
342 histogram[i]+=(size_t) step;
356 previous_excess=cumulative_excess;
358 q=histogram+number_bins;
359 while ((cumulative_excess != 0) && (p < q))
361 step=(ssize_t) number_bins/cumulative_excess;
364 for (p=histogram; (p < q) && (cumulative_excess != 0); p+=(ptrdiff_t) step)
365 if ((
double) *p < clip_limit)
372 }
while ((cumulative_excess != 0) && (cumulative_excess < previous_excess));
375static void GenerateCLAHEHistogram(
const RectangleInfo *clahe_info,
376 const RectangleInfo *tile_info,
const size_t number_bins,
377 const unsigned short *lut,
const unsigned short *pixels,
size_t *histogram)
388 for (i=0; i < (ssize_t) number_bins; i++)
391 for (i=0; i < (ssize_t) tile_info->height; i++)
396 q=p+tile_info->width;
398 histogram[lut[*p++]]++;
399 q+=(ptrdiff_t) clahe_info->width;
400 p=q-tile_info->width;
404static void InterpolateCLAHE(
const RectangleInfo *clahe_info,
const size_t *Q12,
405 const size_t *Q22,
const size_t *Q11,
const size_t *Q21,
406 const RectangleInfo *tile,
const unsigned short *lut,
unsigned short *pixels)
417 for (y=(ssize_t) tile->height; y > 0; y--)
422 for (x=(ssize_t) tile->width; x > 0; x--)
424 intensity=lut[*pixels];
425 *pixels++=(
unsigned short) (MagickSafeReciprocal((
double) tile->width*
426 tile->height)*(y*((
double) x*Q12[intensity]+((
double) tile->width-x)*
427 Q22[intensity])+((
double) tile->height-y)*((
double) x*Q11[intensity]+
428 ((
double) tile->width-x)*Q21[intensity])));
430 pixels+=(clahe_info->width-tile->width);
434static void GenerateCLAHELut(
const RangeInfo *range_info,
435 const size_t number_bins,
unsigned short *lut)
446 delta=(
unsigned short) ((range_info->max-range_info->min)/number_bins+1);
447 for (i=(ssize_t) range_info->min; i <= (ssize_t) range_info->max; i++)
448 lut[i]=(
unsigned short) ((i-range_info->min)/delta);
451static void MapCLAHEHistogram(
const RangeInfo *range_info,
452 const size_t number_bins,
const size_t number_pixels,
size_t *histogram)
464 scale=(double) (range_info->max-range_info->min)/number_pixels;
466 for (i=0; i < (ssize_t) number_bins; i++)
469 histogram[i]=(size_t) (range_info->min+scale*sum);
470 if (histogram[i] > range_info->max)
471 histogram[i]=range_info->max;
475static MagickBooleanType CLAHE(
const RectangleInfo *clahe_info,
476 const RectangleInfo *tile_info,
const RangeInfo *range_info,
477 const size_t number_bins,
const double clip_limit,
unsigned short *pixels)
496 if (clip_limit == 1.0)
498 tile_cache=AcquireVirtualMemory((
size_t) clahe_info->x*number_bins,(
size_t)
499 clahe_info->y*
sizeof(*tiles));
500 if (tile_cache == (MemoryInfo *) NULL)
502 lut=(
unsigned short *) AcquireQuantumMemory(NumberCLAHEGrays,
sizeof(*lut));
503 if (lut == (
unsigned short *) NULL)
505 tile_cache=RelinquishVirtualMemory(tile_cache);
508 tiles=(
size_t *) GetVirtualMemoryBlob(tile_cache);
509 limit=(size_t) (clip_limit*((
double) tile_info->width*tile_info->height)/
516 GenerateCLAHELut(range_info,number_bins,lut);
518 for (y=0; y < (ssize_t) clahe_info->y; y++)
523 for (x=0; x < (ssize_t) clahe_info->x; x++)
528 histogram=tiles+((ssize_t) number_bins*(y*clahe_info->x+x));
529 GenerateCLAHEHistogram(clahe_info,tile_info,number_bins,lut,p,histogram);
530 ClipCLAHEHistogram((
double) limit,number_bins,histogram);
531 MapCLAHEHistogram(range_info,number_bins,tile_info->width*
532 tile_info->height,histogram);
533 p+=(ptrdiff_t) tile_info->width;
535 p+=CastDoubleToPtrdiffT((
double) clahe_info->width*(tile_info->height-1));
541 for (y=0; y <= (ssize_t) clahe_info->y; y++)
552 tile.height=tile_info->height;
560 tile.height=tile_info->height >> 1;
565 if (y == (ssize_t) clahe_info->y)
570 tile.height=(tile_info->height+1) >> 1;
571 tile.y=clahe_info->y-1;
574 for (x=0; x <= (ssize_t) clahe_info->x; x++)
582 tile.width=tile_info->width;
590 tile.width=tile_info->width >> 1;
595 if (x == (ssize_t) clahe_info->x)
600 tile.width=(tile_info->width+1) >> 1;
601 tile.x=clahe_info->x-1;
604 Q12=(double) number_bins*(tile.y*clahe_info->x+tile.x);
605 Q22=(double) number_bins*(tile.y*clahe_info->x+offset.x);
606 Q11=(double) number_bins*(offset.y*clahe_info->x+tile.x);
607 Q21=(double) number_bins*(offset.y*clahe_info->x+offset.x);
608 InterpolateCLAHE(clahe_info,tiles+CastDoubleToPtrdiffT(Q12),
609 tiles+CastDoubleToPtrdiffT(Q22),tiles+CastDoubleToPtrdiffT(Q11),
610 tiles+CastDoubleToPtrdiffT(Q21),&tile,lut,p);
611 p+=(ptrdiff_t) tile.width;
613 p+=CastDoubleToPtrdiffT((
double) clahe_info->width*(tile.height-1));
615 lut=(
unsigned short *) RelinquishMagickMemory(lut);
616 tile_cache=RelinquishVirtualMemory(tile_cache);
620MagickExport MagickBooleanType CLAHEImage(Image *image,
const size_t width,
621 const size_t height,
const size_t number_bins,
const double clip_limit,
622 ExceptionInfo *exception)
624#define CLAHEImageTag "CLAHE/Image"
660 assert(image != (Image *) NULL);
661 assert(image->signature == MagickCoreSignature);
662 if (IsEventLogging() != MagickFalse)
663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
665 range_info.max=NumberCLAHEGrays-1;
666 tile_info.width=width;
667 if (tile_info.width == 0)
668 tile_info.width=image->columns >> 3;
669 if (tile_info.width < 2)
671 tile_info.height=height;
672 if (tile_info.height == 0)
673 tile_info.height=image->rows >> 3;
674 if (tile_info.height < 2)
677 if ((image->columns % tile_info.width) != 0)
678 tile_info.x=(ssize_t) (tile_info.width-(image->columns % tile_info.width));
680 if ((image->rows % tile_info.height) != 0)
681 tile_info.y=(ssize_t) (tile_info.height-(image->rows % tile_info.height));
682 clahe_info.width=(size_t) ((ssize_t) image->columns+tile_info.x);
683 clahe_info.height=(size_t) ((ssize_t) image->rows+tile_info.y);
684 clahe_info.x=(ssize_t) (clahe_info.width/tile_info.width);
685 clahe_info.y=(ssize_t) (clahe_info.height/tile_info.height);
686 pixel_cache=AcquireVirtualMemory(clahe_info.width,clahe_info.height*
688 if (pixel_cache == (MemoryInfo *) NULL)
689 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
691 pixels=(
unsigned short *) GetVirtualMemoryBlob(pixel_cache);
692 colorspace=image->colorspace;
693 if (TransformImageColorspace(image,LabColorspace,exception) == MagickFalse)
695 pixel_cache=RelinquishVirtualMemory(pixel_cache);
701 image_view=AcquireVirtualCacheView(image,exception);
705 for (y=0; y < (ssize_t) clahe_info.height; y++)
713 if (status == MagickFalse)
715 p=GetCacheViewVirtualPixels(image_view,-(tile_info.x >> 1),y-
716 (tile_info.y >> 1),clahe_info.width,1,exception);
717 if (p == (
const Quantum *) NULL)
722 for (x=0; x < (ssize_t) clahe_info.width; x++)
724 pixels[n++]=ScaleQuantumToShort(p[0]);
725 p+=(ptrdiff_t) GetPixelChannels(image);
727 if (image->progress_monitor != (MagickProgressMonitor) NULL)
733 proceed=SetImageProgress(image,CLAHEImageTag,progress,2*
734 GetPixelChannels(image));
735 if (proceed == MagickFalse)
739 image_view=DestroyCacheView(image_view);
740 status=CLAHE(&clahe_info,&tile_info,&range_info,number_bins == 0 ?
741 (
size_t) 128 : MagickMin(number_bins,256),clip_limit,pixels);
742 if (status == MagickFalse)
743 (void) ThrowMagickException(exception,GetMagickModule(),
744 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
748 image_view=AcquireAuthenticCacheView(image,exception);
749 n=clahe_info.width*(size_t) (tile_info.y/2);
750 for (y=0; y < (ssize_t) image->rows; y++)
758 if (status == MagickFalse)
760 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
761 if (q == (Quantum *) NULL)
766 n+=(size_t) (tile_info.x/2);
767 for (x=0; x < (ssize_t) image->columns; x++)
769 q[0]=ScaleShortToQuantum(pixels[n++]);
770 q+=(ptrdiff_t) GetPixelChannels(image);
772 n+=(size_t) ((ssize_t) clahe_info.width-(ssize_t) image->columns-
774 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
776 if (image->progress_monitor != (MagickProgressMonitor) NULL)
782 proceed=SetImageProgress(image,CLAHEImageTag,progress,2*
783 GetPixelChannels(image));
784 if (proceed == MagickFalse)
788 image_view=DestroyCacheView(image_view);
789 pixel_cache=RelinquishVirtualMemory(pixel_cache);
790 if (TransformImageColorspace(image,colorspace,exception) == MagickFalse)
840MagickExport MagickBooleanType ClutImage(Image *image,
const Image *clut_image,
841 const PixelInterpolateMethod method,ExceptionInfo *exception)
843#define ClutImageTag "Clut/Image"
863 assert(image != (Image *) NULL);
864 assert(image->signature == MagickCoreSignature);
865 assert(clut_image != (Image *) NULL);
866 assert(clut_image->signature == MagickCoreSignature);
867 if (IsEventLogging() != MagickFalse)
868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
869 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
871 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
872 (IsGrayColorspace(clut_image->colorspace) == MagickFalse))
873 (void) SetImageColorspace(image,sRGBColorspace,exception);
874 clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*clut_map));
875 if (clut_map == (PixelInfo *) NULL)
876 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
883 adjust=(ssize_t) (method == IntegerInterpolatePixel ? 0 : 1);
884 clut_view=AcquireVirtualCacheView(clut_image,exception);
885 for (i=0; i <= (ssize_t) MaxMap; i++)
887 GetPixelInfo(clut_image,clut_map+i);
888 status=InterpolatePixelInfo(clut_image,clut_view,method,(
double) i*
889 ((
double) clut_image->columns-adjust)/MaxMap,(
double) i*
890 ((
double) clut_image->rows-adjust)/MaxMap,clut_map+i,exception);
891 if (status == MagickFalse)
894 clut_view=DestroyCacheView(clut_view);
895 image_view=AcquireAuthenticCacheView(image,exception);
896#if defined(MAGICKCORE_OPENMP_SUPPORT)
897 #pragma omp parallel for schedule(static) shared(progress,status) \
898 magick_number_threads(image,image,image->rows,1)
900 for (y=0; y < (ssize_t) image->rows; y++)
911 if (status == MagickFalse)
913 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
914 if (q == (Quantum *) NULL)
919 GetPixelInfo(image,&pixel);
920 for (x=0; x < (ssize_t) image->columns; x++)
925 GetPixelInfoPixel(image,q,&pixel);
926 traits=GetPixelChannelTraits(image,RedPixelChannel);
927 if ((traits & UpdatePixelTrait) != 0)
928 pixel.red=clut_map[ScaleQuantumToMap(ClampToQuantum(
930 traits=GetPixelChannelTraits(image,GreenPixelChannel);
931 if ((traits & UpdatePixelTrait) != 0)
932 pixel.green=clut_map[ScaleQuantumToMap(ClampToQuantum(
933 pixel.green))].green;
934 traits=GetPixelChannelTraits(image,BluePixelChannel);
935 if ((traits & UpdatePixelTrait) != 0)
936 pixel.blue=clut_map[ScaleQuantumToMap(ClampToQuantum(
938 traits=GetPixelChannelTraits(image,BlackPixelChannel);
939 if ((traits & UpdatePixelTrait) != 0)
940 pixel.black=clut_map[ScaleQuantumToMap(ClampToQuantum(
941 pixel.black))].black;
942 traits=GetPixelChannelTraits(image,AlphaPixelChannel);
943 if ((traits & UpdatePixelTrait) != 0)
944 pixel.alpha=clut_map[ScaleQuantumToMap(ClampToQuantum(
945 pixel.alpha))].alpha;
946 SetPixelViaPixelInfo(image,&pixel,q);
947 q+=(ptrdiff_t) GetPixelChannels(image);
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
956#if defined(MAGICKCORE_OPENMP_SUPPORT)
960 proceed=SetImageProgress(image,ClutImageTag,progress,image->rows);
961 if (proceed == MagickFalse)
965 image_view=DestroyCacheView(image_view);
966 clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
967 if ((clut_image->alpha_trait != UndefinedPixelTrait) &&
968 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
969 (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
1018MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
1019 const char *color_correction_collection,ExceptionInfo *exception)
1021#define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
1023 typedef struct _Correction
1031 typedef struct _ColorCorrection
1046 token[MagickPathExtent];
1079 assert(image != (Image *) NULL);
1080 assert(image->signature == MagickCoreSignature);
1081 if (IsEventLogging() != MagickFalse)
1082 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1083 if (color_correction_collection == (
const char *) NULL)
1084 return(MagickFalse);
1085 ccc=NewXMLTree((
const char *) color_correction_collection,exception);
1086 if (ccc == (XMLTreeInfo *) NULL)
1087 return(MagickFalse);
1088 cc=GetXMLTreeChild(ccc,
"ColorCorrection");
1089 if (cc == (XMLTreeInfo *) NULL)
1091 ccc=DestroyXMLTree(ccc);
1092 return(MagickFalse);
1094 color_correction.red.slope=1.0;
1095 color_correction.red.offset=0.0;
1096 color_correction.red.power=1.0;
1097 color_correction.green.slope=1.0;
1098 color_correction.green.offset=0.0;
1099 color_correction.green.power=1.0;
1100 color_correction.blue.slope=1.0;
1101 color_correction.blue.offset=0.0;
1102 color_correction.blue.power=1.0;
1103 color_correction.saturation=0.0;
1104 sop=GetXMLTreeChild(cc,
"SOPNode");
1105 if (sop != (XMLTreeInfo *) NULL)
1112 slope=GetXMLTreeChild(sop,
"Slope");
1113 if (slope != (XMLTreeInfo *) NULL)
1115 content=GetXMLTreeContent(slope);
1116 p=(
const char *) content;
1117 for (i=0; (*p !=
'\0') && (i < 3); i++)
1119 (void) GetNextToken(p,&p,MagickPathExtent,token);
1121 (void) GetNextToken(p,&p,MagickPathExtent,token);
1126 color_correction.red.slope=StringToDouble(token,(
char **) NULL);
1131 color_correction.green.slope=StringToDouble(token,
1137 color_correction.blue.slope=StringToDouble(token,
1144 offset=GetXMLTreeChild(sop,
"Offset");
1145 if (offset != (XMLTreeInfo *) NULL)
1147 content=GetXMLTreeContent(offset);
1148 p=(
const char *) content;
1149 for (i=0; (*p !=
'\0') && (i < 3); i++)
1151 (void) GetNextToken(p,&p,MagickPathExtent,token);
1153 (void) GetNextToken(p,&p,MagickPathExtent,token);
1158 color_correction.red.offset=StringToDouble(token,
1164 color_correction.green.offset=StringToDouble(token,
1170 color_correction.blue.offset=StringToDouble(token,
1177 power=GetXMLTreeChild(sop,
"Power");
1178 if (power != (XMLTreeInfo *) NULL)
1180 content=GetXMLTreeContent(power);
1181 p=(
const char *) content;
1182 for (i=0; (*p !=
'\0') && (i < 3); i++)
1184 (void) GetNextToken(p,&p,MagickPathExtent,token);
1186 (void) GetNextToken(p,&p,MagickPathExtent,token);
1191 color_correction.red.power=StringToDouble(token,(
char **) NULL);
1196 color_correction.green.power=StringToDouble(token,
1202 color_correction.blue.power=StringToDouble(token,
1210 sat=GetXMLTreeChild(cc,
"SATNode");
1211 if (sat != (XMLTreeInfo *) NULL)
1216 saturation=GetXMLTreeChild(sat,
"Saturation");
1217 if (saturation != (XMLTreeInfo *) NULL)
1219 content=GetXMLTreeContent(saturation);
1220 p=(
const char *) content;
1221 (void) GetNextToken(p,&p,MagickPathExtent,token);
1222 color_correction.saturation=StringToDouble(token,(
char **) NULL);
1225 ccc=DestroyXMLTree(ccc);
1226 if (image->debug != MagickFalse)
1228 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1229 " Color Correction Collection:");
1230 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1231 " color_correction.red.slope: %g",color_correction.red.slope);
1232 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1233 " color_correction.red.offset: %g",color_correction.red.offset);
1234 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1235 " color_correction.red.power: %g",color_correction.red.power);
1236 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1237 " color_correction.green.slope: %g",color_correction.green.slope);
1238 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1239 " color_correction.green.offset: %g",color_correction.green.offset);
1240 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1241 " color_correction.green.power: %g",color_correction.green.power);
1242 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1243 " color_correction.blue.slope: %g",color_correction.blue.slope);
1244 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1245 " color_correction.blue.offset: %g",color_correction.blue.offset);
1246 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1247 " color_correction.blue.power: %g",color_correction.blue.power);
1248 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1249 " color_correction.saturation: %g",color_correction.saturation);
1251 cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*cdl_map));
1252 if (cdl_map == (PixelInfo *) NULL)
1253 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1255 for (i=0; i <= (ssize_t) MaxMap; i++)
1257 cdl_map[i].red=(double) ScaleMapToQuantum((
double)
1258 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
1259 color_correction.red.offset,color_correction.red.power))));
1260 cdl_map[i].green=(double) ScaleMapToQuantum((
double)
1261 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
1262 color_correction.green.offset,color_correction.green.power))));
1263 cdl_map[i].blue=(double) ScaleMapToQuantum((
double)
1264 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
1265 color_correction.blue.offset,color_correction.blue.power))));
1267 if (image->storage_class == PseudoClass)
1269 for (i=0; i < (ssize_t) image->colors; i++)
1277 luma=0.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
1278 0.07217*image->colormap[i].blue;
1279 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
1280 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-luma;
1281 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
1282 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-
1284 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
1285 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-luma;
1287 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
1288 (void) SyncImage(image, exception);
1296 image_view=AcquireAuthenticCacheView(image,exception);
1297#if defined(MAGICKCORE_OPENMP_SUPPORT)
1298 #pragma omp parallel for schedule(static) shared(progress,status) \
1299 magick_number_threads(image,image,image->rows,1)
1301 for (y=0; y < (ssize_t) image->rows; y++)
1312 if (status == MagickFalse)
1314 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1315 if (q == (Quantum *) NULL)
1320 for (x=0; x < (ssize_t) image->columns; x++)
1322 luma=0.21267*(double) GetPixelRed(image,q)+0.71526*(double)
1323 GetPixelGreen(image,q)+0.07217*(double) GetPixelBlue(image,q);
1324 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
1325 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
1326 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
1327 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
1328 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
1329 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
1330 q+=(ptrdiff_t) GetPixelChannels(image);
1332 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1334 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1339#if defined(MAGICKCORE_OPENMP_SUPPORT)
1343 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
1344 progress,image->rows);
1345 if (proceed == MagickFalse)
1349 image_view=DestroyCacheView(image_view);
1350 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
1384static inline void Contrast(
const int sign,
double *red,
double *green,
1395 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
1396 brightness+=0.5*sign*(0.5*(sin((
double) (MagickPI*(brightness-0.5)))+1.0)-
1398 if (brightness > 1.0)
1401 if (brightness < 0.0)
1403 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
1406MagickExport MagickBooleanType ContrastImage(Image *image,
1407 const MagickBooleanType sharpen,ExceptionInfo *exception)
1409#define ContrastImageTag "Contrast/Image"
1429 assert(image != (Image *) NULL);
1430 assert(image->signature == MagickCoreSignature);
1431#if defined(MAGICKCORE_OPENCL_SUPPORT)
1432 if (AccelerateContrastImage(image,sharpen,exception) != MagickFalse)
1435 if (IsEventLogging() != MagickFalse)
1436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1437 sign=sharpen != MagickFalse ? 1 : -1;
1438 if (image->storage_class == PseudoClass)
1443 for (i=0; i < (ssize_t) image->colors; i++)
1450 red=(double) image->colormap[i].red;
1451 green=(double) image->colormap[i].green;
1452 blue=(double) image->colormap[i].blue;
1453 Contrast(sign,&red,&green,&blue);
1454 image->colormap[i].red=(MagickRealType) red;
1455 image->colormap[i].green=(MagickRealType) green;
1456 image->colormap[i].blue=(MagickRealType) blue;
1464 image_view=AcquireAuthenticCacheView(image,exception);
1465#if defined(MAGICKCORE_OPENMP_SUPPORT)
1466 #pragma omp parallel for schedule(static) shared(progress,status) \
1467 magick_number_threads(image,image,image->rows,1)
1469 for (y=0; y < (ssize_t) image->rows; y++)
1482 if (status == MagickFalse)
1484 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1485 if (q == (Quantum *) NULL)
1490 for (x=0; x < (ssize_t) image->columns; x++)
1492 red=(double) GetPixelRed(image,q);
1493 green=(double) GetPixelGreen(image,q);
1494 blue=(double) GetPixelBlue(image,q);
1495 Contrast(sign,&red,&green,&blue);
1496 SetPixelRed(image,ClampToQuantum(red),q);
1497 SetPixelGreen(image,ClampToQuantum(green),q);
1498 SetPixelBlue(image,ClampToQuantum(blue),q);
1499 q+=(ptrdiff_t) GetPixelChannels(image);
1501 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1503 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1508#if defined(MAGICKCORE_OPENMP_SUPPORT)
1512 proceed=SetImageProgress(image,ContrastImageTag,progress,image->rows);
1513 if (proceed == MagickFalse)
1517 image_view=DestroyCacheView(image_view);
1558MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1559 const double black_point,
const double white_point,ExceptionInfo *exception)
1561#define ContrastStretchImageTag "ContrastStretch/Image"
1567 property[MagickPathExtent];
1595 assert(image != (Image *) NULL);
1596 assert(image->signature == MagickCoreSignature);
1597 if (IsEventLogging() != MagickFalse)
1598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1599 type=IdentifyImageType(image,exception);
1600 if (IsGrayImageType(type) != MagickFalse)
1601 (void) SetImageColorspace(image,GRAYColorspace,exception);
1602 black=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*black));
1603 white=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*white));
1604 stretch_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1605 sizeof(*stretch_map));
1606 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1607 sizeof(*histogram));
1608 if ((black == (Quantum *) NULL) || (white == (Quantum *) NULL) ||
1609 (stretch_map == (Quantum *) NULL) || (histogram == (
double *) NULL))
1611 if (histogram != (
double *) NULL)
1612 histogram=(
double *) RelinquishMagickMemory(histogram);
1613 if (stretch_map != (Quantum *) NULL)
1614 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1615 if (white != (Quantum *) NULL)
1616 white=(Quantum *) RelinquishMagickMemory(white);
1617 if (black != (Quantum *) NULL)
1618 black=(Quantum *) RelinquishMagickMemory(black);
1619 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1626 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1627 sizeof(*histogram));
1628 image_view=AcquireVirtualCacheView(image,exception);
1629 for (y=0; y < (ssize_t) image->rows; y++)
1637 if (status == MagickFalse)
1639 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1640 if (p == (
const Quantum *) NULL)
1645 for (x=0; x < (ssize_t) image->columns; x++)
1650 pixel=GetPixelIntensity(image,p);
1651 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1653 if (image->channel_mask != AllChannels)
1654 pixel=(double) p[i];
1655 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
1656 ClampToQuantum(pixel))+(size_t) i]++;
1658 p+=(ptrdiff_t) GetPixelChannels(image);
1661 image_view=DestroyCacheView(image_view);
1665 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1673 black[i]=(Quantum) 0;
1674 white[i]=(Quantum) ScaleQuantumToMap(QuantumRange);
1676 for (j=0; j <= (ssize_t) MaxMap; j++)
1678 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1679 if (intensity > black_point)
1682 black[i]=(Quantum) j;
1684 for (j=(ssize_t) MaxMap; j != 0; j--)
1686 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1687 if (intensity > ((
double) image->columns*image->rows-white_point))
1690 white[i]=(Quantum) j;
1692 histogram=(
double *) RelinquishMagickMemory(histogram);
1696 (void) memset(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1697 sizeof(*stretch_map));
1698 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1703 for (j=0; j <= (ssize_t) MaxMap; j++)
1708 gamma=MagickSafeReciprocal(white[i]-black[i]);
1709 if (j < (ssize_t) black[i])
1710 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=(Quantum) 0;
1712 if (j > (ssize_t) white[i])
1713 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=QuantumRange;
1715 if (black[i] != white[i])
1716 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=
1717 ScaleMapToQuantum((
double) (MaxMap*gamma*(j-(
double) black[i])));
1720 if (image->storage_class == PseudoClass)
1728 for (j=0; j < (ssize_t) image->colors; j++)
1730 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1732 i=GetPixelChannelOffset(image,RedPixelChannel);
1733 image->colormap[j].red=(MagickRealType) stretch_map[
1734 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1735 image->colormap[j].red))+(size_t) i];
1737 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1739 i=GetPixelChannelOffset(image,GreenPixelChannel);
1740 image->colormap[j].green=(MagickRealType) stretch_map[
1741 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1742 image->colormap[j].green))+(size_t) i];
1744 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1746 i=GetPixelChannelOffset(image,BluePixelChannel);
1747 image->colormap[j].blue=(MagickRealType) stretch_map[
1748 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1749 image->colormap[j].blue))+(size_t) i];
1751 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1753 i=GetPixelChannelOffset(image,AlphaPixelChannel);
1754 image->colormap[j].alpha=(MagickRealType) stretch_map[
1755 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1756 image->colormap[j].alpha))+(size_t) i];
1765 image_view=AcquireAuthenticCacheView(image,exception);
1766#if defined(MAGICKCORE_OPENMP_SUPPORT)
1767 #pragma omp parallel for schedule(static) shared(progress,status) \
1768 magick_number_threads(image,image,image->rows,1)
1770 for (y=0; y < (ssize_t) image->rows; y++)
1778 if (status == MagickFalse)
1780 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1781 if (q == (Quantum *) NULL)
1786 for (x=0; x < (ssize_t) image->columns; x++)
1791 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1793 PixelChannel channel = GetPixelChannelChannel(image,j);
1794 PixelTrait traits = GetPixelChannelTraits(image,channel);
1795 if ((traits & UpdatePixelTrait) == 0)
1797 if (black[j] == white[j])
1799 q[j]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1800 ScaleQuantumToMap(q[j])+(
size_t) j]);
1802 q+=(ptrdiff_t) GetPixelChannels(image);
1804 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1806 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1811#if defined(MAGICKCORE_OPENMP_SUPPORT)
1815 proceed=SetImageProgress(image,ContrastStretchImageTag,progress,
1817 if (proceed == MagickFalse)
1821 image_view=DestroyCacheView(image_view);
1822 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*
1823 QuantumScale*GetPixelIntensity(image,black),100.0*QuantumScale*
1824 GetPixelIntensity(image,white));
1825 (void) SetImageProperty(image,
"histogram:contrast-stretch",property,
1827 white=(Quantum *) RelinquishMagickMemory(white);
1828 black=(Quantum *) RelinquishMagickMemory(black);
1829 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1858MagickExport Image *EnhanceImage(
const Image *image,ExceptionInfo *exception)
1860#define EnhanceImageTag "Enhance/Image"
1861#define EnhancePixel(weight) \
1862 mean=QuantumScale*((double) GetPixelRed(image,r)+pixel.red)/2.0; \
1863 distance=QuantumScale*((double) GetPixelRed(image,r)-pixel.red); \
1864 distance_squared=(4.0+mean)*distance*distance; \
1865 mean=QuantumScale*((double) GetPixelGreen(image,r)+pixel.green)/2.0; \
1866 distance=QuantumScale*((double) GetPixelGreen(image,r)-pixel.green); \
1867 distance_squared+=(7.0-mean)*distance*distance; \
1868 mean=QuantumScale*((double) GetPixelBlue(image,r)+pixel.blue)/2.0; \
1869 distance=QuantumScale*((double) GetPixelBlue(image,r)-pixel.blue); \
1870 distance_squared+=(5.0-mean)*distance*distance; \
1871 mean=QuantumScale*((double) GetPixelBlack(image,r)+pixel.black)/2.0; \
1872 distance=QuantumScale*((double) GetPixelBlack(image,r)-pixel.black); \
1873 distance_squared+=(5.0-mean)*distance*distance; \
1874 mean=QuantumScale*((double) GetPixelAlpha(image,r)+pixel.alpha)/2.0; \
1875 distance=QuantumScale*((double) GetPixelAlpha(image,r)-pixel.alpha); \
1876 distance_squared+=(5.0-mean)*distance*distance; \
1877 if (distance_squared < 0.069) \
1879 aggregate.red+=(weight)*(double) GetPixelRed(image,r); \
1880 aggregate.green+=(weight)*(double) GetPixelGreen(image,r); \
1881 aggregate.blue+=(weight)*(double) GetPixelBlue(image,r); \
1882 aggregate.black+=(weight)*(double) GetPixelBlack(image,r); \
1883 aggregate.alpha+=(weight)*(double) GetPixelAlpha(image,r); \
1884 total_weight+=(weight); \
1886 r+=(ptrdiff_t) GetPixelChannels(image);
1907 assert(image != (
const Image *) NULL);
1908 assert(image->signature == MagickCoreSignature);
1909 if (IsEventLogging() != MagickFalse)
1910 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1911 assert(exception != (ExceptionInfo *) NULL);
1912 assert(exception->signature == MagickCoreSignature);
1913 enhance_image=CloneImage(image,0,0,MagickTrue,
1915 if (enhance_image == (Image *) NULL)
1916 return((Image *) NULL);
1917 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1919 enhance_image=DestroyImage(enhance_image);
1920 return((Image *) NULL);
1927 image_view=AcquireVirtualCacheView(image,exception);
1928 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1929#if defined(MAGICKCORE_OPENMP_SUPPORT)
1930 #pragma omp parallel for schedule(static) shared(progress,status) \
1931 magick_number_threads(image,enhance_image,image->rows,1)
1933 for (y=0; y < (ssize_t) image->rows; y++)
1950 if (status == MagickFalse)
1952 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1953 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1955 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
1960 center=(ssize_t) GetPixelChannels(image)*(2*((ssize_t) image->columns+4)+2);
1961 GetPixelInfo(image,&pixel);
1962 for (x=0; x < (ssize_t) image->columns; x++)
1976 GetPixelInfo(image,&aggregate);
1978 GetPixelInfoPixel(image,p+center,&pixel);
1980 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1981 EnhancePixel(8.0); EnhancePixel(5.0);
1982 r=p+GetPixelChannels(image)*(image->columns+4);
1983 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1984 EnhancePixel(20.0); EnhancePixel(8.0);
1985 r=p+2*GetPixelChannels(image)*(image->columns+4);
1986 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1987 EnhancePixel(40.0); EnhancePixel(10.0);
1988 r=p+3*GetPixelChannels(image)*(image->columns+4);
1989 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1990 EnhancePixel(20.0); EnhancePixel(8.0);
1991 r=p+4*GetPixelChannels(image)*(image->columns+4);
1992 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1993 EnhancePixel(8.0); EnhancePixel(5.0);
1994 if (total_weight > MagickEpsilon)
1996 pixel.red=((aggregate.red+total_weight/2.0)/total_weight);
1997 pixel.green=((aggregate.green+total_weight/2.0)/total_weight);
1998 pixel.blue=((aggregate.blue+total_weight/2.0)/total_weight);
1999 pixel.black=((aggregate.black+total_weight/2.0)/total_weight);
2000 pixel.alpha=((aggregate.alpha+total_weight/2.0)/total_weight);
2002 SetPixelViaPixelInfo(enhance_image,&pixel,q);
2003 p+=(ptrdiff_t) GetPixelChannels(image);
2004 q+=(ptrdiff_t) GetPixelChannels(enhance_image);
2006 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
2008 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2013#if defined(MAGICKCORE_OPENMP_SUPPORT)
2017 proceed=SetImageProgress(image,EnhanceImageTag,progress,image->rows);
2018 if (proceed == MagickFalse)
2022 enhance_view=DestroyCacheView(enhance_view);
2023 image_view=DestroyCacheView(image_view);
2024 if (status == MagickFalse)
2025 enhance_image=DestroyImage(enhance_image);
2026 return(enhance_image);
2053MagickExport MagickBooleanType EqualizeImage(Image *image,
2054 ExceptionInfo *exception)
2056#define EqualizeImageTag "Equalize/Image"
2062 black[2*CompositePixelChannel+1],
2066 white[2*CompositePixelChannel+1];
2083 assert(image != (Image *) NULL);
2084 assert(image->signature == MagickCoreSignature);
2085#if defined(MAGICKCORE_OPENCL_SUPPORT)
2086 if (AccelerateEqualizeImage(image,exception) != MagickFalse)
2089 if (IsEventLogging() != MagickFalse)
2090 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2091 equalize_map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2092 sizeof(*equalize_map));
2093 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2094 sizeof(*histogram));
2095 map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
sizeof(*map));
2096 if ((equalize_map == (
double *) NULL) || (histogram == (
double *) NULL) ||
2097 (map == (
double *) NULL))
2099 if (map != (
double *) NULL)
2100 map=(
double *) RelinquishMagickMemory(map);
2101 if (histogram != (
double *) NULL)
2102 histogram=(
double *) RelinquishMagickMemory(histogram);
2103 if (equalize_map != (
double *) NULL)
2104 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2105 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2112 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
2113 sizeof(*histogram));
2114 image_view=AcquireVirtualCacheView(image,exception);
2115 for (y=0; y < (ssize_t) image->rows; y++)
2123 if (status == MagickFalse)
2125 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2126 if (p == (
const Quantum *) NULL)
2131 for (x=0; x < (ssize_t) image->columns; x++)
2133 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2138 intensity=(double) p[i];
2139 if ((image->channel_mask & SyncChannels) != 0)
2140 intensity=GetPixelIntensity(image,p);
2141 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
2142 ClampToQuantum(intensity))+(size_t) i]++;
2144 p+=(ptrdiff_t) GetPixelChannels(image);
2147 image_view=DestroyCacheView(image_view);
2151 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2160 for (j=0; j <= (ssize_t) MaxMap; j++)
2162 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
2163 map[(ssize_t) GetPixelChannels(image)*j+i]=intensity;
2166 (void) memset(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
2167 sizeof(*equalize_map));
2168 (void) memset(black,0,
sizeof(*black));
2169 (void) memset(white,0,
sizeof(*white));
2170 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2176 white[i]=map[GetPixelChannels(image)*MaxMap+(size_t) i];
2177 if (black[i] != white[i])
2178 for (j=0; j <= (ssize_t) MaxMap; j++)
2179 equalize_map[GetPixelChannels(image)*(size_t) j+(
size_t) i]=(double)
2180 ScaleMapToQuantum((
double) ((MaxMap*(map[GetPixelChannels(image)*
2181 (
size_t) j+(
size_t) i]-black[i]))/(white[i]-black[i])));
2183 histogram=(
double *) RelinquishMagickMemory(histogram);
2184 map=(
double *) RelinquishMagickMemory(map);
2185 if (image->storage_class == PseudoClass)
2193 for (j=0; j < (ssize_t) image->colors; j++)
2195 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2197 PixelChannel channel = GetPixelChannelChannel(image,
2199 if (black[channel] != white[channel])
2200 image->colormap[j].red=equalize_map[(ssize_t)
2201 GetPixelChannels(image)*ScaleQuantumToMap(
2202 ClampToQuantum(image->colormap[j].red))+channel];
2204 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2206 PixelChannel channel = GetPixelChannelChannel(image,
2208 if (black[channel] != white[channel])
2209 image->colormap[j].green=equalize_map[(ssize_t)
2210 GetPixelChannels(image)*ScaleQuantumToMap(
2211 ClampToQuantum(image->colormap[j].green))+channel];
2213 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2215 PixelChannel channel = GetPixelChannelChannel(image,
2217 if (black[channel] != white[channel])
2218 image->colormap[j].blue=equalize_map[(ssize_t)
2219 GetPixelChannels(image)*ScaleQuantumToMap(
2220 ClampToQuantum(image->colormap[j].blue))+channel];
2222 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2224 PixelChannel channel = GetPixelChannelChannel(image,
2226 if (black[channel] != white[channel])
2227 image->colormap[j].alpha=equalize_map[(ssize_t)
2228 GetPixelChannels(image)*ScaleQuantumToMap(
2229 ClampToQuantum(image->colormap[j].alpha))+channel];
2237 image_view=AcquireAuthenticCacheView(image,exception);
2238#if defined(MAGICKCORE_OPENMP_SUPPORT)
2239 #pragma omp parallel for schedule(static) shared(progress,status) \
2240 magick_number_threads(image,image,image->rows,1)
2242 for (y=0; y < (ssize_t) image->rows; y++)
2250 if (status == MagickFalse)
2252 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2253 if (q == (Quantum *) NULL)
2258 for (x=0; x < (ssize_t) image->columns; x++)
2263 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2265 PixelChannel channel = GetPixelChannelChannel(image,j);
2266 PixelTrait traits = GetPixelChannelTraits(image,channel);
2267 if (((traits & UpdatePixelTrait) == 0) || (black[j] == white[j]))
2269 q[j]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
2270 ScaleQuantumToMap(q[j])+(
size_t) j]);
2272 q+=(ptrdiff_t) GetPixelChannels(image);
2274 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2276 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2281#if defined(MAGICKCORE_OPENMP_SUPPORT)
2285 proceed=SetImageProgress(image,EqualizeImageTag,progress,image->rows);
2286 if (proceed == MagickFalse)
2290 image_view=DestroyCacheView(image_view);
2291 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2330static inline double gamma_pow(
const double value,
const double gamma)
2332 return(value < 0.0 ? value : pow(value,gamma));
2335MagickExport MagickBooleanType GammaImage(Image *image,
const double gamma,
2336 ExceptionInfo *exception)
2338#define GammaImageTag "Gamma/Image"
2361 assert(image != (Image *) NULL);
2362 assert(image->signature == MagickCoreSignature);
2363 if (IsEventLogging() != MagickFalse)
2364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2367 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*gamma_map));
2368 if (gamma_map == (Quantum *) NULL)
2369 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2371 (void) memset(gamma_map,0,(MaxMap+1)*
sizeof(*gamma_map));
2373 for (i=0; i <= (ssize_t) MaxMap; i++)
2374 gamma_map[i]=ScaleMapToQuantum((
double) (MaxMap*pow((
double) i/
2375 MaxMap,MagickSafeReciprocal(gamma))));
2376 if (image->storage_class == PseudoClass)
2377 for (i=0; i < (ssize_t) image->colors; i++)
2382 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2383 image->colormap[i].red=(double) gamma_map[ScaleQuantumToMap(
2384 ClampToQuantum(image->colormap[i].red))];
2385 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2386 image->colormap[i].green=(double) gamma_map[ScaleQuantumToMap(
2387 ClampToQuantum(image->colormap[i].green))];
2388 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2389 image->colormap[i].blue=(double) gamma_map[ScaleQuantumToMap(
2390 ClampToQuantum(image->colormap[i].blue))];
2391 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2392 image->colormap[i].alpha=(double) gamma_map[ScaleQuantumToMap(
2393 ClampToQuantum(image->colormap[i].alpha))];
2400 image_view=AcquireAuthenticCacheView(image,exception);
2401#if defined(MAGICKCORE_OPENMP_SUPPORT)
2402 #pragma omp parallel for schedule(static) shared(progress,status) \
2403 magick_number_threads(image,image,image->rows,1)
2405 for (y=0; y < (ssize_t) image->rows; y++)
2413 if (status == MagickFalse)
2415 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2416 if (q == (Quantum *) NULL)
2421 for (x=0; x < (ssize_t) image->columns; x++)
2426 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2428 PixelChannel channel = GetPixelChannelChannel(image,j);
2429 PixelTrait traits = GetPixelChannelTraits(image,channel);
2430 if ((traits & UpdatePixelTrait) == 0)
2432 q[j]=gamma_map[ScaleQuantumToMap(ClampToQuantum((MagickRealType)
2435 q+=(ptrdiff_t) GetPixelChannels(image);
2437 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2439 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2444#if defined(MAGICKCORE_OPENMP_SUPPORT)
2448 proceed=SetImageProgress(image,GammaImageTag,progress,image->rows);
2449 if (proceed == MagickFalse)
2453 image_view=DestroyCacheView(image_view);
2454 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
2455 if (image->gamma != 0.0)
2456 image->gamma*=gamma;
2487MagickExport MagickBooleanType GrayscaleImage(Image *image,
2488 const PixelIntensityMethod method,ExceptionInfo *exception)
2490#define GrayscaleImageTag "Grayscale/Image"
2504 assert(image != (Image *) NULL);
2505 assert(image->signature == MagickCoreSignature);
2506 if (IsEventLogging() != MagickFalse)
2507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2508 if (image->storage_class == PseudoClass)
2510 if (SyncImage(image,exception) == MagickFalse)
2511 return(MagickFalse);
2512 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2513 return(MagickFalse);
2515#if defined(MAGICKCORE_OPENCL_SUPPORT)
2516 if (AccelerateGrayscaleImage(image,method,exception) != MagickFalse)
2518 image->intensity=method;
2519 image->type=GrayscaleType;
2520 if ((method == Rec601LuminancePixelIntensityMethod) ||
2521 (method == Rec709LuminancePixelIntensityMethod))
2522 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2523 return(SetImageColorspace(image,GRAYColorspace,exception));
2531 image_view=AcquireAuthenticCacheView(image,exception);
2532#if defined(MAGICKCORE_OPENMP_SUPPORT)
2533 #pragma omp parallel for schedule(static) shared(progress,status) \
2534 magick_number_threads(image,image,image->rows,1)
2536 for (y=0; y < (ssize_t) image->rows; y++)
2544 if (status == MagickFalse)
2546 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2547 if (q == (Quantum *) NULL)
2552 for (x=0; x < (ssize_t) image->columns; x++)
2560 red=(MagickRealType) GetPixelRed(image,q);
2561 green=(MagickRealType) GetPixelGreen(image,q);
2562 blue=(MagickRealType) GetPixelBlue(image,q);
2566 case AveragePixelIntensityMethod:
2568 intensity=(red+green+blue)/3.0;
2571 case BrightnessPixelIntensityMethod:
2573 intensity=MagickMax(MagickMax(red,green),blue);
2576 case LightnessPixelIntensityMethod:
2578 intensity=(MagickMin(MagickMin(red,green),blue)+
2579 MagickMax(MagickMax(red,green),blue))/2.0;
2582 case MSPixelIntensityMethod:
2584 intensity=(MagickRealType) (((
double) red*red+green*green+
2588 case Rec601LumaPixelIntensityMethod:
2590 if (image->colorspace == RGBColorspace)
2592 red=EncodePixelGamma(red);
2593 green=EncodePixelGamma(green);
2594 blue=EncodePixelGamma(blue);
2596 intensity=0.298839*red+0.586811*green+0.114350*blue;
2599 case Rec601LuminancePixelIntensityMethod:
2601 if (image->colorspace == sRGBColorspace)
2603 red=DecodePixelGamma(red);
2604 green=DecodePixelGamma(green);
2605 blue=DecodePixelGamma(blue);
2607 intensity=0.298839*red+0.586811*green+0.114350*blue;
2610 case Rec709LumaPixelIntensityMethod:
2613 if (image->colorspace == RGBColorspace)
2615 red=EncodePixelGamma(red);
2616 green=EncodePixelGamma(green);
2617 blue=EncodePixelGamma(blue);
2619 intensity=0.212656*red+0.715158*green+0.072186*blue;
2622 case Rec709LuminancePixelIntensityMethod:
2624 if (image->colorspace == sRGBColorspace)
2626 red=DecodePixelGamma(red);
2627 green=DecodePixelGamma(green);
2628 blue=DecodePixelGamma(blue);
2630 intensity=0.212656*red+0.715158*green+0.072186*blue;
2633 case RMSPixelIntensityMethod:
2635 intensity=(MagickRealType) (sqrt((
double) red*red+green*green+
2636 blue*blue)/sqrt(3.0));
2640 SetPixelGray(image,ClampToQuantum(intensity),q);
2641 q+=(ptrdiff_t) GetPixelChannels(image);
2643 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2645 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2650#if defined(MAGICKCORE_OPENMP_SUPPORT)
2654 proceed=SetImageProgress(image,GrayscaleImageTag,progress,image->rows);
2655 if (proceed == MagickFalse)
2659 image_view=DestroyCacheView(image_view);
2660 image->intensity=method;
2661 image->type=GrayscaleType;
2662 if ((method == Rec601LuminancePixelIntensityMethod) ||
2663 (method == Rec709LuminancePixelIntensityMethod))
2664 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2665 return(SetImageColorspace(image,GRAYColorspace,exception));
2699MagickExport MagickBooleanType HaldClutImage(Image *image,
2700 const Image *hald_image,ExceptionInfo *exception)
2702#define HaldClutImageTag "Clut/Image"
2704 typedef struct _HaldInfo
2736 assert(image != (Image *) NULL);
2737 assert(image->signature == MagickCoreSignature);
2738 if (IsEventLogging() != MagickFalse)
2739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2740 assert(hald_image != (Image *) NULL);
2741 assert(hald_image->signature == MagickCoreSignature);
2742 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2743 return(MagickFalse);
2744 if ((image->alpha_trait & BlendPixelTrait) == 0)
2745 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2746 if (image->colorspace != hald_image->colorspace)
2747 (void) SetImageColorspace(image,hald_image->colorspace,exception);
2753 length=(size_t) MagickMin((MagickRealType) hald_image->columns,
2754 (MagickRealType) hald_image->rows);
2755 for (level=2; (level*level*level) < length; level++) ;
2757 cube_size=level*level;
2758 width=(double) hald_image->columns;
2759 GetPixelInfo(hald_image,&zero);
2760 hald_view=AcquireVirtualCacheView(hald_image,exception);
2761 image_view=AcquireAuthenticCacheView(image,exception);
2762#if defined(MAGICKCORE_OPENMP_SUPPORT)
2763 #pragma omp parallel for schedule(static) shared(progress,status) \
2764 magick_number_threads(image,image,image->rows,1)
2766 for (y=0; y < (ssize_t) image->rows; y++)
2774 if (status == MagickFalse)
2776 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2777 if (q == (Quantum *) NULL)
2782 for (x=0; x < (ssize_t) image->columns; x++)
2789 point = { 0, 0, 0 };
2798 point.x=QuantumScale*(level-1.0)*(
double) GetPixelRed(image,q);
2799 point.y=QuantumScale*(level-1.0)*(
double) GetPixelGreen(image,q);
2800 point.z=QuantumScale*(level-1.0)*(
double) GetPixelBlue(image,q);
2801 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2802 point.x-=floor(point.x);
2803 point.y-=floor(point.y);
2804 point.z-=floor(point.z);
2805 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2806 fmod(offset,width),floor(offset/width),&pixel1,exception);
2807 if (status == MagickFalse)
2809 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2810 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2811 if (status == MagickFalse)
2814 if (hald_image->interpolate == NearestInterpolatePixel)
2815 area=(point.y < 0.5) ? 0.0 : 1.0;
2816 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2819 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2820 fmod(offset,width),floor(offset/width),&pixel1,exception);
2821 if (status == MagickFalse)
2823 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2824 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2825 if (status == MagickFalse)
2827 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2830 if (hald_image->interpolate == NearestInterpolatePixel)
2831 area=(point.z < 0.5)? 0.0 : 1.0;
2832 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2834 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2835 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2836 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2837 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2838 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2839 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2840 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2841 (image->colorspace == CMYKColorspace))
2842 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2843 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2844 (image->alpha_trait != UndefinedPixelTrait))
2845 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2846 q+=(ptrdiff_t) GetPixelChannels(image);
2848 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2850 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2855#if defined(MAGICKCORE_OPENMP_SUPPORT)
2859 proceed=SetImageProgress(image,HaldClutImageTag,progress,image->rows);
2860 if (proceed == MagickFalse)
2864 hald_view=DestroyCacheView(hald_view);
2865 image_view=DestroyCacheView(image_view);
2913static inline double LevelPixel(
const double black_point,
2914 const double white_point,
const double gamma,
const double pixel)
2920 scale=MagickSafeReciprocal(white_point-black_point);
2921 level_pixel=(double) QuantumRange*gamma_pow(scale*((
double) pixel-(
double)
2922 black_point),MagickSafeReciprocal(gamma));
2923 return(level_pixel);
2926MagickExport MagickBooleanType LevelImage(Image *image,
const double black_point,
2927 const double white_point,
const double gamma,ExceptionInfo *exception)
2929#define LevelImageTag "Level/Image"
2947 assert(image != (Image *) NULL);
2948 assert(image->signature == MagickCoreSignature);
2949 if (IsEventLogging() != MagickFalse)
2950 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2951 if (image->storage_class == PseudoClass)
2952 for (i=0; i < (ssize_t) image->colors; i++)
2957 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2958 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2959 white_point,gamma,image->colormap[i].red));
2960 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2961 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2962 white_point,gamma,image->colormap[i].green));
2963 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2964 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2965 white_point,gamma,image->colormap[i].blue));
2966 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2967 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2968 white_point,gamma,image->colormap[i].alpha));
2975 image_view=AcquireAuthenticCacheView(image,exception);
2976#if defined(MAGICKCORE_OPENMP_SUPPORT)
2977 #pragma omp parallel for schedule(static) shared(progress,status) \
2978 magick_number_threads(image,image,image->rows,1)
2980 for (y=0; y < (ssize_t) image->rows; y++)
2988 if (status == MagickFalse)
2990 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2991 if (q == (Quantum *) NULL)
2996 for (x=0; x < (ssize_t) image->columns; x++)
3001 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
3003 PixelChannel channel = GetPixelChannelChannel(image,j);
3004 PixelTrait traits = GetPixelChannelTraits(image,channel);
3005 if ((traits & UpdatePixelTrait) == 0)
3007 q[j]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
3010 q+=(ptrdiff_t) GetPixelChannels(image);
3012 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3014 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3019#if defined(MAGICKCORE_OPENMP_SUPPORT)
3023 proceed=SetImageProgress(image,LevelImageTag,progress,image->rows);
3024 if (proceed == MagickFalse)
3028 image_view=DestroyCacheView(image_view);
3029 (void) ClampImage(image,exception);
3075MagickExport MagickBooleanType LevelizeImage(Image *image,
3076 const double black_point,
const double white_point,
const double gamma,
3077 ExceptionInfo *exception)
3079#define LevelizeImageTag "Levelize/Image"
3080#define LevelizeValue(x) ClampToQuantum(((MagickRealType) gamma_pow((double) \
3081 (QuantumScale*((double) x)),gamma))*(white_point-black_point)+black_point)
3101 assert(image != (Image *) NULL);
3102 assert(image->signature == MagickCoreSignature);
3103 if (IsEventLogging() != MagickFalse)
3104 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3105 if (image->storage_class == PseudoClass)
3106 for (i=0; i < (ssize_t) image->colors; i++)
3111 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3112 image->colormap[i].red=(double) LevelizeValue(image->colormap[i].red);
3113 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3114 image->colormap[i].green=(double) LevelizeValue(
3115 image->colormap[i].green);
3116 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3117 image->colormap[i].blue=(double) LevelizeValue(image->colormap[i].blue);
3118 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3119 image->colormap[i].alpha=(double) LevelizeValue(
3120 image->colormap[i].alpha);
3127 image_view=AcquireAuthenticCacheView(image,exception);
3128#if defined(MAGICKCORE_OPENMP_SUPPORT)
3129 #pragma omp parallel for schedule(static) shared(progress,status) \
3130 magick_number_threads(image,image,image->rows,1)
3132 for (y=0; y < (ssize_t) image->rows; y++)
3140 if (status == MagickFalse)
3142 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3143 if (q == (Quantum *) NULL)
3148 for (x=0; x < (ssize_t) image->columns; x++)
3153 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
3155 PixelChannel channel = GetPixelChannelChannel(image,j);
3156 PixelTrait traits = GetPixelChannelTraits(image,channel);
3157 if ((traits & UpdatePixelTrait) == 0)
3159 q[j]=LevelizeValue(q[j]);
3161 q+=(ptrdiff_t) GetPixelChannels(image);
3163 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3165 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3170#if defined(MAGICKCORE_OPENMP_SUPPORT)
3174 proceed=SetImageProgress(image,LevelizeImageTag,progress,image->rows);
3175 if (proceed == MagickFalse)
3179 image_view=DestroyCacheView(image_view);
3224MagickExport MagickBooleanType LevelImageColors(Image *image,
3225 const PixelInfo *black_color,
const PixelInfo *white_color,
3226 const MagickBooleanType invert,ExceptionInfo *exception)
3237 assert(image != (Image *) NULL);
3238 assert(image->signature == MagickCoreSignature);
3239 if (IsEventLogging() != MagickFalse)
3240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3241 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
3242 ((IsGrayColorspace(black_color->colorspace) == MagickFalse) ||
3243 (IsGrayColorspace(white_color->colorspace) == MagickFalse)))
3244 (void) SetImageColorspace(image,sRGBColorspace,exception);
3246 if (invert == MagickFalse)
3248 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3250 channel_mask=SetImageChannelMask(image,RedChannel);
3251 status&=(MagickStatusType) LevelImage(image,black_color->red,
3252 white_color->red,1.0,exception);
3253 (void) SetImageChannelMask(image,channel_mask);
3255 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3257 channel_mask=SetImageChannelMask(image,GreenChannel);
3258 status&=(MagickStatusType) LevelImage(image,black_color->green,
3259 white_color->green,1.0,exception);
3260 (void) SetImageChannelMask(image,channel_mask);
3262 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3264 channel_mask=SetImageChannelMask(image,BlueChannel);
3265 status&=(MagickStatusType) LevelImage(image,black_color->blue,
3266 white_color->blue,1.0,exception);
3267 (void) SetImageChannelMask(image,channel_mask);
3269 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3270 (image->colorspace == CMYKColorspace))
3272 channel_mask=SetImageChannelMask(image,BlackChannel);
3273 status&=(MagickStatusType) LevelImage(image,black_color->black,
3274 white_color->black,1.0,exception);
3275 (void) SetImageChannelMask(image,channel_mask);
3277 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3278 (image->alpha_trait != UndefinedPixelTrait))
3280 channel_mask=SetImageChannelMask(image,AlphaChannel);
3281 status&=(MagickStatusType) LevelImage(image,black_color->alpha,
3282 white_color->alpha,1.0,exception);
3283 (void) SetImageChannelMask(image,channel_mask);
3288 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3290 channel_mask=SetImageChannelMask(image,RedChannel);
3291 status&=(MagickStatusType) LevelizeImage(image,black_color->red,
3292 white_color->red,1.0,exception);
3293 (void) SetImageChannelMask(image,channel_mask);
3295 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3297 channel_mask=SetImageChannelMask(image,GreenChannel);
3298 status&=(MagickStatusType) LevelizeImage(image,black_color->green,
3299 white_color->green,1.0,exception);
3300 (void) SetImageChannelMask(image,channel_mask);
3302 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3304 channel_mask=SetImageChannelMask(image,BlueChannel);
3305 status&=(MagickStatusType) LevelizeImage(image,black_color->blue,
3306 white_color->blue,1.0,exception);
3307 (void) SetImageChannelMask(image,channel_mask);
3309 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3310 (image->colorspace == CMYKColorspace))
3312 channel_mask=SetImageChannelMask(image,BlackChannel);
3313 status&=(MagickStatusType) LevelizeImage(image,black_color->black,
3314 white_color->black,1.0,exception);
3315 (void) SetImageChannelMask(image,channel_mask);
3317 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3318 (image->alpha_trait != UndefinedPixelTrait))
3320 channel_mask=SetImageChannelMask(image,AlphaChannel);
3321 status&=(MagickStatusType) LevelizeImage(image,black_color->alpha,
3322 white_color->alpha,1.0,exception);
3323 (void) SetImageChannelMask(image,channel_mask);
3326 return(status != 0 ? MagickTrue : MagickFalse);
3360MagickExport MagickBooleanType LinearStretchImage(Image *image,
3361 const double black_point,
const double white_point,ExceptionInfo *exception)
3363#define LinearStretchImageTag "LinearStretch/Image"
3369 property[MagickPathExtent];
3386 assert(image != (Image *) NULL);
3387 assert(image->signature == MagickCoreSignature);
3388 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*histogram));
3389 if (histogram == (
double *) NULL)
3390 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
3395 (void) memset(histogram,0,(MaxMap+1)*
sizeof(*histogram));
3396 image_view=AcquireVirtualCacheView(image,exception);
3397 for (y=0; y < (ssize_t) image->rows; y++)
3405 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3406 if (p == (
const Quantum *) NULL)
3408 for (x=0; x < (ssize_t) image->columns; x++)
3410 intensity=GetPixelIntensity(image,p);
3411 histogram[ScaleQuantumToMap(ClampToQuantum(intensity))]++;
3412 p+=(ptrdiff_t) GetPixelChannels(image);
3415 image_view=DestroyCacheView(image_view);
3420 for (black=0; black < (ssize_t) MaxMap; black++)
3422 intensity+=histogram[black];
3423 if (intensity >= black_point)
3427 for (white=(ssize_t) MaxMap; white != 0; white--)
3429 intensity+=histogram[white];
3430 if (intensity >= white_point)
3433 histogram=(
double *) RelinquishMagickMemory(histogram);
3434 status=LevelImage(image,(
double) ScaleMapToQuantum((MagickRealType) black),
3435 (
double) ScaleMapToQuantum((MagickRealType) white),1.0,exception);
3436 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*black/
3437 MaxMap,100.0*white/MaxMap);
3438 (void) SetImageProperty(image,
"histogram:linear-stretch",property,exception);
3474static inline void ModulateHCL(
const double percent_hue,
3475 const double percent_chroma,
const double percent_luma,
double *red,
3476 double *green,
double *blue)
3486 ConvertRGBToHCL(*red,*green,*blue,&hue,&chroma,&luma);
3487 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3488 chroma*=0.01*percent_chroma;
3489 luma*=0.01*percent_luma;
3490 ConvertHCLToRGB(hue,chroma,luma,red,green,blue);
3493static inline void ModulateHCLp(
const double percent_hue,
3494 const double percent_chroma,
const double percent_luma,
double *red,
3495 double *green,
double *blue)
3505 ConvertRGBToHCLp(*red,*green,*blue,&hue,&chroma,&luma);
3506 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3507 chroma*=0.01*percent_chroma;
3508 luma*=0.01*percent_luma;
3509 ConvertHCLpToRGB(hue,chroma,luma,red,green,blue);
3512static inline void ModulateHSB(
const double percent_hue,
3513 const double percent_saturation,
const double percent_brightness,
double *red,
3514 double *green,
double *blue)
3524 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
3525 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3526 saturation*=0.01*percent_saturation;
3527 brightness*=0.01*percent_brightness;
3528 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
3531static inline void ModulateHSI(
const double percent_hue,
3532 const double percent_saturation,
const double percent_intensity,
double *red,
3533 double *green,
double *blue)
3543 ConvertRGBToHSI(*red,*green,*blue,&hue,&saturation,&intensity);
3544 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3545 saturation*=0.01*percent_saturation;
3546 intensity*=0.01*percent_intensity;
3547 ConvertHSIToRGB(hue,saturation,intensity,red,green,blue);
3550static inline void ModulateHSL(
const double percent_hue,
3551 const double percent_saturation,
const double percent_lightness,
double *red,
3552 double *green,
double *blue)
3562 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
3563 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3564 saturation*=0.01*percent_saturation;
3565 lightness*=0.01*percent_lightness;
3566 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
3569static inline void ModulateHSV(
const double percent_hue,
3570 const double percent_saturation,
const double percent_value,
double *red,
3571 double *green,
double *blue)
3581 ConvertRGBToHSV(*red,*green,*blue,&hue,&saturation,&value);
3582 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3583 saturation*=0.01*percent_saturation;
3584 value*=0.01*percent_value;
3585 ConvertHSVToRGB(hue,saturation,value,red,green,blue);
3588static inline void ModulateHWB(
const double percent_hue,
3589 const double percent_whiteness,
const double percent_blackness,
double *red,
3590 double *green,
double *blue)
3600 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
3601 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3602 blackness*=0.01*percent_blackness;
3603 whiteness*=0.01*percent_whiteness;
3604 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
3607static inline void ModulateLCHab(
const double percent_luma,
3608 const double percent_chroma,
const double percent_hue,
3609 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3619 ConvertRGBToLCHab(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3620 luma*=0.01*percent_luma;
3621 chroma*=0.01*percent_chroma;
3622 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3623 ConvertLCHabToRGB(luma,chroma,hue,illuminant,red,green,blue);
3626static inline void ModulateLCHuv(
const double percent_luma,
3627 const double percent_chroma,
const double percent_hue,
3628 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3638 ConvertRGBToLCHuv(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3639 luma*=0.01*percent_luma;
3640 chroma*=0.01*percent_chroma;
3641 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3642 ConvertLCHuvToRGB(luma,chroma,hue,illuminant,red,green,blue);
3645MagickExport MagickBooleanType ModulateImage(Image *image,
const char *modulate,
3646 ExceptionInfo *exception)
3648#define ModulateImageTag "Modulate/Image"
3654 colorspace = UndefinedColorspace;
3660 percent_brightness = 100.0,
3661 percent_hue = 100.0,
3662 percent_saturation = 100.0;
3668 illuminant = D65Illuminant;
3688 assert(image != (Image *) NULL);
3689 assert(image->signature == MagickCoreSignature);
3690 if (IsEventLogging() != MagickFalse)
3691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3692 if (modulate == (
char *) NULL)
3693 return(MagickFalse);
3694 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
3695 (void) SetImageColorspace(image,sRGBColorspace,exception);
3696 flags=ParseGeometry(modulate,&geometry_info);
3697 if ((flags & RhoValue) != 0)
3698 percent_brightness=geometry_info.rho;
3699 if ((flags & SigmaValue) != 0)
3700 percent_saturation=geometry_info.sigma;
3701 if ((flags & XiValue) != 0)
3702 percent_hue=geometry_info.xi;
3703 artifact=GetImageArtifact(image,
"modulate:colorspace");
3704 if (artifact != (
const char *) NULL)
3705 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
3706 MagickFalse,artifact);
3707 artifact=GetImageArtifact(image,
"color:illuminant");
3708 if (artifact != (
const char *) NULL)
3713 illuminant_type=ParseCommandOption(MagickIlluminantOptions,MagickFalse,
3715 if (illuminant_type < 0)
3717 illuminant=UndefinedIlluminant;
3718 colorspace=UndefinedColorspace;
3721 illuminant=(IlluminantType) illuminant_type;
3723 if (image->storage_class == PseudoClass)
3724 for (i=0; i < (ssize_t) image->colors; i++)
3734 red=(double) image->colormap[i].red;
3735 green=(double) image->colormap[i].green;
3736 blue=(double) image->colormap[i].blue;
3741 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3745 case HCLpColorspace:
3747 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3753 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3759 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3766 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3772 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3778 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3783 case LCHabColorspace:
3785 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3786 illuminant,&red,&green,&blue);
3789 case LCHuvColorspace:
3791 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3792 illuminant,&red,&green,&blue);
3796 image->colormap[i].red=red;
3797 image->colormap[i].green=green;
3798 image->colormap[i].blue=blue;
3803#if defined(MAGICKCORE_OPENCL_SUPPORT)
3804 if (AccelerateModulateImage(image,percent_brightness,percent_hue,
3805 percent_saturation,colorspace,exception) != MagickFalse)
3810 image_view=AcquireAuthenticCacheView(image,exception);
3811#if defined(MAGICKCORE_OPENMP_SUPPORT)
3812 #pragma omp parallel for schedule(static) shared(progress,status) \
3813 magick_number_threads(image,image,image->rows,1)
3815 for (y=0; y < (ssize_t) image->rows; y++)
3823 if (status == MagickFalse)
3825 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3826 if (q == (Quantum *) NULL)
3831 for (x=0; x < (ssize_t) image->columns; x++)
3838 red=(double) GetPixelRed(image,q);
3839 green=(double) GetPixelGreen(image,q);
3840 blue=(double) GetPixelBlue(image,q);
3845 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3849 case HCLpColorspace:
3851 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3857 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3863 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3870 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3876 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3882 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3887 case LCHabColorspace:
3889 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3890 illuminant,&red,&green,&blue);
3893 case LCHuvColorspace:
3895 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3896 illuminant,&red,&green,&blue);
3900 SetPixelRed(image,ClampToQuantum(red),q);
3901 SetPixelGreen(image,ClampToQuantum(green),q);
3902 SetPixelBlue(image,ClampToQuantum(blue),q);
3903 q+=(ptrdiff_t) GetPixelChannels(image);
3905 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3907 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3912#if defined(MAGICKCORE_OPENMP_SUPPORT)
3916 proceed=SetImageProgress(image,ModulateImageTag,progress,image->rows);
3917 if (proceed == MagickFalse)
3921 image_view=DestroyCacheView(image_view);
3953MagickExport MagickBooleanType NegateImage(Image *image,
3954 const MagickBooleanType grayscale,ExceptionInfo *exception)
3956#define NegateImageTag "Negate/Image"
3973 assert(image != (Image *) NULL);
3974 assert(image->signature == MagickCoreSignature);
3975 if (IsEventLogging() != MagickFalse)
3976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3977 if (image->storage_class == PseudoClass)
3978 for (i=0; i < (ssize_t) image->colors; i++)
3983 if (grayscale != MagickFalse)
3984 if ((image->colormap[i].red != image->colormap[i].green) ||
3985 (image->colormap[i].green != image->colormap[i].blue))
3987 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3988 image->colormap[i].red=(double) QuantumRange-image->colormap[i].red;
3989 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3990 image->colormap[i].green=(double) QuantumRange-image->colormap[i].green;
3991 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3992 image->colormap[i].blue=(double) QuantumRange-image->colormap[i].blue;
3999 image_view=AcquireAuthenticCacheView(image,exception);
4000 if( grayscale != MagickFalse )
4002 for (y=0; y < (ssize_t) image->rows; y++)
4013 if (status == MagickFalse)
4015 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4017 if (q == (Quantum *) NULL)
4022 for (x=0; x < (ssize_t) image->columns; x++)
4027 if (IsPixelGray(image,q) == MagickFalse)
4029 q+=(ptrdiff_t) GetPixelChannels(image);
4032 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4034 PixelChannel channel = GetPixelChannelChannel(image,j);
4035 PixelTrait traits = GetPixelChannelTraits(image,channel);
4036 if ((traits & UpdatePixelTrait) == 0)
4038 q[j]=QuantumRange-q[j];
4040 q+=(ptrdiff_t) GetPixelChannels(image);
4042 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4043 if (sync == MagickFalse)
4045 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4051 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4052 if (proceed == MagickFalse)
4056 image_view=DestroyCacheView(image_view);
4062#if defined(MAGICKCORE_OPENMP_SUPPORT)
4063 #pragma omp parallel for schedule(static) shared(progress,status) \
4064 magick_number_threads(image,image,image->rows,1)
4066 for (y=0; y < (ssize_t) image->rows; y++)
4074 if (status == MagickFalse)
4076 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4077 if (q == (Quantum *) NULL)
4082 for (x=0; x < (ssize_t) image->columns; x++)
4087 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4089 PixelChannel channel = GetPixelChannelChannel(image,j);
4090 PixelTrait traits = GetPixelChannelTraits(image,channel);
4091 if ((traits & UpdatePixelTrait) == 0)
4093 q[j]=QuantumRange-q[j];
4095 q+=(ptrdiff_t) GetPixelChannels(image);
4097 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4099 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4104#if defined(MAGICKCORE_OPENMP_SUPPORT)
4108 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4109 if (proceed == MagickFalse)
4113 image_view=DestroyCacheView(image_view);
4143MagickExport MagickBooleanType NormalizeImage(Image *image,
4144 ExceptionInfo *exception)
4150 black_point=0.02*image->columns*image->rows;
4151 white_point=0.99*image->columns*image->rows;
4152 return(ContrastStretchImage(image,black_point,white_point,exception));
4219#if defined(MAGICKCORE_HAVE_ATANH)
4220#define Sigmoidal(a,b,x) ( tanh((0.5*(a))*((x)-(b))) )
4222#define Sigmoidal(a,b,x) ( 1.0/(1.0+exp((a)*((b)-(x)))) )
4241#define ScaledSigmoidal(a,b,x) ( \
4242 (Sigmoidal((a),(b),(x))-Sigmoidal((a),(b),0.0)) / \
4243 (Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0)) )
4253static inline double InverseScaledSigmoidal(
const double a,
const double b,
4256 const double sig0=Sigmoidal(a,b,0.0);
4257 const double sig1=Sigmoidal(a,b,1.0);
4258 const double argument=(sig1-sig0)*x+sig0;
4259 const double clamped=
4261#if defined(MAGICKCORE_HAVE_ATANH)
4262 argument < -1+MagickEpsilon
4266 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4268 return(b+(2.0/a)*atanh(clamped));
4270 argument < MagickEpsilon
4274 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4276 return(b-log(1.0/clamped-1.0)/a);
4280MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
4281 const MagickBooleanType sharpen,
const double contrast,
const double midpoint,
4282 ExceptionInfo *exception)
4284#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
4285#define ScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4286 ScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale*((double) x))) )
4287#define InverseScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4288 InverseScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale* \
4306 assert(image != (Image *) NULL);
4307 assert(image->signature == MagickCoreSignature);
4308 if (IsEventLogging() != MagickFalse)
4309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4314 if (contrast < MagickEpsilon)
4319 if (image->storage_class == PseudoClass)
4324 if( sharpen != MagickFalse )
4325 for (i=0; i < (ssize_t) image->colors; i++)
4327 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4328 image->colormap[i].red=(MagickRealType) ScaledSig(
4329 image->colormap[i].red);
4330 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4331 image->colormap[i].green=(MagickRealType) ScaledSig(
4332 image->colormap[i].green);
4333 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4334 image->colormap[i].blue=(MagickRealType) ScaledSig(
4335 image->colormap[i].blue);
4336 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4337 image->colormap[i].alpha=(MagickRealType) ScaledSig(
4338 image->colormap[i].alpha);
4341 for (i=0; i < (ssize_t) image->colors; i++)
4343 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4344 image->colormap[i].red=(MagickRealType) InverseScaledSig(
4345 image->colormap[i].red);
4346 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4347 image->colormap[i].green=(MagickRealType) InverseScaledSig(
4348 image->colormap[i].green);
4349 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4350 image->colormap[i].blue=(MagickRealType) InverseScaledSig(
4351 image->colormap[i].blue);
4352 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4353 image->colormap[i].alpha=(MagickRealType) InverseScaledSig(
4354 image->colormap[i].alpha);
4362 image_view=AcquireAuthenticCacheView(image,exception);
4363#if defined(MAGICKCORE_OPENMP_SUPPORT)
4364 #pragma omp parallel for schedule(static) shared(progress,status) \
4365 magick_number_threads(image,image,image->rows,1)
4367 for (y=0; y < (ssize_t) image->rows; y++)
4375 if (status == MagickFalse)
4377 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4378 if (q == (Quantum *) NULL)
4383 for (x=0; x < (ssize_t) image->columns; x++)
4388 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4390 PixelChannel channel = GetPixelChannelChannel(image,i);
4391 PixelTrait traits = GetPixelChannelTraits(image,channel);
4392 if ((traits & UpdatePixelTrait) == 0)
4394 if( sharpen != MagickFalse )
4395 q[i]=ScaledSig(q[i]);
4397 q[i]=InverseScaledSig(q[i]);
4399 q+=(ptrdiff_t) GetPixelChannels(image);
4401 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4403 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4408#if defined(MAGICKCORE_OPENMP_SUPPORT)
4412 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress,
4414 if (proceed == MagickFalse)
4418 image_view=DestroyCacheView(image_view);
4448MagickExport MagickBooleanType WhiteBalanceImage(Image *image,
4449 ExceptionInfo *exception)
4451#define WhiteBalanceImageTag "WhiteBalance/Image"
4475 assert(image != (Image *) NULL);
4476 assert(image->signature == MagickCoreSignature);
4477 if (IsEventLogging() != MagickFalse)
4478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4479 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
4480 return(MagickFalse);
4481 status=TransformImageColorspace(image,LabColorspace,exception);
4484 image_view=AcquireAuthenticCacheView(image,exception);
4485 for (y=0; y < (ssize_t) image->rows; y++)
4493 if (status == MagickFalse)
4495 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4496 if (p == (Quantum *) NULL)
4501 for (x=0; x < (ssize_t) image->columns; x++)
4503 a_mean+=QuantumScale*(double) GetPixela(image,p)-0.5;
4504 b_mean+=QuantumScale*(double) GetPixelb(image,p)-0.5;
4505 p+=(ptrdiff_t) GetPixelChannels(image);
4508 a_mean/=((double) image->columns*image->rows);
4509 b_mean/=((double) image->columns*image->rows);
4511#if defined(MAGICKCORE_OPENMP_SUPPORT)
4512 #pragma omp parallel for schedule(static) shared(progress,status) \
4513 magick_number_threads(image,image,image->rows,1)
4515 for (y=0; y < (ssize_t) image->rows; y++)
4523 if (status == MagickFalse)
4525 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4526 if (q == (Quantum *) NULL)
4531 for (x=0; x < (ssize_t) image->columns; x++)
4540 a=(double) GetPixela(image,q)-1.1*(double) GetPixelL(image,q)*a_mean;
4541 b=(double) GetPixelb(image,q)-1.1*(double) GetPixelL(image,q)*b_mean;
4542 SetPixela(image,ClampToQuantum(a),q);
4543 SetPixelb(image,ClampToQuantum(b),q);
4544 q+=(ptrdiff_t) GetPixelChannels(image);
4546 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4548 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4553#if defined(MAGICKCORE_OPENMP_SUPPORT)
4557 proceed=SetImageProgress(image,WhiteBalanceImageTag,progress,
4559 if (proceed == MagickFalse)
4563 image_view=DestroyCacheView(image_view);
4564 artifact=GetImageArtifact(image,
"white-balance:vibrance");
4565 if (artifact != (
const char *) NULL)
4582 flags=ParseGeometry(artifact,&geometry_info);
4583 if ((flags & RhoValue) != 0)
4584 black_point=geometry_info.rho;
4585 if ((flags & PercentValue) != 0)
4586 black_point*=((double) QuantumRange/100.0);
4587 channel_mask=SetImageChannelMask(image,(ChannelType) (aChannel |
4589 status&=(MagickStatusType) LevelImage(image,black_point,(
double)
4590 QuantumRange-black_point,1.0,exception);
4591 (void) SetImageChannelMask(image,channel_mask);
4593 status&=(MagickStatusType) TransformImageColorspace(image,sRGBColorspace,
4595 return(status != 0 ? MagickTrue : MagickFalse);