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,
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,
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*PerceptibleReciprocal(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,
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) (PerceptibleReciprocal((
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,
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));
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,
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=MagickMax(width,2);
667 if (tile_info.width == 0)
668 tile_info.width=image->columns >> 3;
669 tile_info.height=MagickMax(height,2);
670 if (tile_info.height == 0)
671 tile_info.height=image->rows >> 3;
673 if ((image->columns % tile_info.width) != 0)
674 tile_info.x=(ssize_t) (tile_info.width-(image->columns % tile_info.width));
676 if ((image->rows % tile_info.height) != 0)
677 tile_info.y=(ssize_t) (tile_info.height-(image->rows % tile_info.height));
678 clahe_info.width=(size_t) ((ssize_t) image->columns+tile_info.x);
679 clahe_info.height=(size_t) ((ssize_t) image->rows+tile_info.y);
680 clahe_info.x=(ssize_t) (clahe_info.width/tile_info.width);
681 clahe_info.y=(ssize_t) (clahe_info.height/tile_info.height);
682 pixel_cache=AcquireVirtualMemory(clahe_info.width,clahe_info.height*
685 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
687 pixels=(
unsigned short *) GetVirtualMemoryBlob(pixel_cache);
688 colorspace=image->colorspace;
689 if (TransformImageColorspace(image,LabColorspace,exception) == MagickFalse)
691 pixel_cache=RelinquishVirtualMemory(pixel_cache);
697 image_view=AcquireVirtualCacheView(image,exception);
701 for (y=0; y < (ssize_t) clahe_info.height; y++)
709 if (status == MagickFalse)
711 p=GetCacheViewVirtualPixels(image_view,-(tile_info.x >> 1),y-
712 (tile_info.y >> 1),clahe_info.width,1,exception);
713 if (p == (
const Quantum *) NULL)
718 for (x=0; x < (ssize_t) clahe_info.width; x++)
720 pixels[n++]=ScaleQuantumToShort(p[0]);
721 p+=(ptrdiff_t) GetPixelChannels(image);
723 if (image->progress_monitor != (MagickProgressMonitor) NULL)
729 proceed=SetImageProgress(image,CLAHEImageTag,progress,2*
730 GetPixelChannels(image));
731 if (proceed == MagickFalse)
735 image_view=DestroyCacheView(image_view);
736 status=CLAHE(&clahe_info,&tile_info,&range_info,number_bins == 0 ?
737 (
size_t) 128 : MagickMin(number_bins,256),clip_limit,pixels);
738 if (status == MagickFalse)
739 (void) ThrowMagickException(exception,GetMagickModule(),
740 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
744 image_view=AcquireAuthenticCacheView(image,exception);
745 n=clahe_info.width*(size_t) (tile_info.y/2);
746 for (y=0; y < (ssize_t) image->rows; y++)
754 if (status == MagickFalse)
756 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
757 if (q == (Quantum *) NULL)
762 n+=(size_t) (tile_info.x/2);
763 for (x=0; x < (ssize_t) image->columns; x++)
765 q[0]=ScaleShortToQuantum(pixels[n++]);
766 q+=(ptrdiff_t) GetPixelChannels(image);
768 n+=(size_t) ((ssize_t) clahe_info.width-(ssize_t) image->columns-
770 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
772 if (image->progress_monitor != (MagickProgressMonitor) NULL)
778 proceed=SetImageProgress(image,CLAHEImageTag,progress,2*
779 GetPixelChannels(image));
780 if (proceed == MagickFalse)
784 image_view=DestroyCacheView(image_view);
785 pixel_cache=RelinquishVirtualMemory(pixel_cache);
786 if (TransformImageColorspace(image,colorspace,exception) == MagickFalse)
836MagickExport MagickBooleanType ClutImage(
Image *image,
const Image *clut_image,
837 const PixelInterpolateMethod method,
ExceptionInfo *exception)
839#define ClutImageTag "Clut/Image"
859 assert(image != (
Image *) NULL);
860 assert(image->signature == MagickCoreSignature);
861 assert(clut_image != (
Image *) NULL);
862 assert(clut_image->signature == MagickCoreSignature);
863 if (IsEventLogging() != MagickFalse)
864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
865 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
867 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
868 (IsGrayColorspace(clut_image->colorspace) == MagickFalse))
869 (void) SetImageColorspace(image,sRGBColorspace,exception);
870 clut_map=(
PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*clut_map));
872 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
879 adjust=(ssize_t) (method == IntegerInterpolatePixel ? 0 : 1);
880 clut_view=AcquireVirtualCacheView(clut_image,exception);
881 for (i=0; i <= (ssize_t) MaxMap; i++)
883 GetPixelInfo(clut_image,clut_map+i);
884 status=InterpolatePixelInfo(clut_image,clut_view,method,(
double) i*
885 ((
double) clut_image->columns-adjust)/MaxMap,(
double) i*
886 ((
double) clut_image->rows-adjust)/MaxMap,clut_map+i,exception);
887 if (status == MagickFalse)
890 clut_view=DestroyCacheView(clut_view);
891 image_view=AcquireAuthenticCacheView(image,exception);
892#if defined(MAGICKCORE_OPENMP_SUPPORT)
893 #pragma omp parallel for schedule(static) shared(progress,status) \
894 magick_number_threads(image,image,image->rows,1)
896 for (y=0; y < (ssize_t) image->rows; y++)
907 if (status == MagickFalse)
909 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
910 if (q == (Quantum *) NULL)
915 GetPixelInfo(image,&pixel);
916 for (x=0; x < (ssize_t) image->columns; x++)
921 GetPixelInfoPixel(image,q,&pixel);
922 traits=GetPixelChannelTraits(image,RedPixelChannel);
923 if ((traits & UpdatePixelTrait) != 0)
924 pixel.red=clut_map[ScaleQuantumToMap(ClampToQuantum(
926 traits=GetPixelChannelTraits(image,GreenPixelChannel);
927 if ((traits & UpdatePixelTrait) != 0)
928 pixel.green=clut_map[ScaleQuantumToMap(ClampToQuantum(
929 pixel.green))].green;
930 traits=GetPixelChannelTraits(image,BluePixelChannel);
931 if ((traits & UpdatePixelTrait) != 0)
932 pixel.blue=clut_map[ScaleQuantumToMap(ClampToQuantum(
934 traits=GetPixelChannelTraits(image,BlackPixelChannel);
935 if ((traits & UpdatePixelTrait) != 0)
936 pixel.black=clut_map[ScaleQuantumToMap(ClampToQuantum(
937 pixel.black))].black;
938 traits=GetPixelChannelTraits(image,AlphaPixelChannel);
939 if ((traits & UpdatePixelTrait) != 0)
940 pixel.alpha=clut_map[ScaleQuantumToMap(ClampToQuantum(
941 pixel.alpha))].alpha;
942 SetPixelViaPixelInfo(image,&pixel,q);
943 q+=(ptrdiff_t) GetPixelChannels(image);
945 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
947 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952#if defined(MAGICKCORE_OPENMP_SUPPORT)
956 proceed=SetImageProgress(image,ClutImageTag,progress,image->rows);
957 if (proceed == MagickFalse)
961 image_view=DestroyCacheView(image_view);
962 clut_map=(
PixelInfo *) RelinquishMagickMemory(clut_map);
963 if ((clut_image->alpha_trait != UndefinedPixelTrait) &&
964 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
965 (
void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
1014MagickExport MagickBooleanType ColorDecisionListImage(
Image *image,
1015 const char *color_correction_collection,
ExceptionInfo *exception)
1017#define ColorDecisionListCorrectImageTag "ColorDecisionList/Image"
1019 typedef struct _Correction
1027 typedef struct _ColorCorrection
1042 token[MagickPathExtent];
1075 assert(image != (
Image *) NULL);
1076 assert(image->signature == MagickCoreSignature);
1077 if (IsEventLogging() != MagickFalse)
1078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1079 if (color_correction_collection == (
const char *) NULL)
1080 return(MagickFalse);
1081 ccc=NewXMLTree((
const char *) color_correction_collection,exception);
1083 return(MagickFalse);
1084 cc=GetXMLTreeChild(ccc,
"ColorCorrection");
1087 ccc=DestroyXMLTree(ccc);
1088 return(MagickFalse);
1090 color_correction.red.slope=1.0;
1091 color_correction.red.offset=0.0;
1092 color_correction.red.power=1.0;
1093 color_correction.green.slope=1.0;
1094 color_correction.green.offset=0.0;
1095 color_correction.green.power=1.0;
1096 color_correction.blue.slope=1.0;
1097 color_correction.blue.offset=0.0;
1098 color_correction.blue.power=1.0;
1099 color_correction.saturation=0.0;
1100 sop=GetXMLTreeChild(cc,
"SOPNode");
1108 slope=GetXMLTreeChild(sop,
"Slope");
1111 content=GetXMLTreeContent(slope);
1112 p=(
const char *) content;
1113 for (i=0; (*p !=
'\0') && (i < 3); i++)
1115 (void) GetNextToken(p,&p,MagickPathExtent,token);
1117 (void) GetNextToken(p,&p,MagickPathExtent,token);
1122 color_correction.red.slope=StringToDouble(token,(
char **) NULL);
1127 color_correction.green.slope=StringToDouble(token,
1133 color_correction.blue.slope=StringToDouble(token,
1140 offset=GetXMLTreeChild(sop,
"Offset");
1143 content=GetXMLTreeContent(offset);
1144 p=(
const char *) content;
1145 for (i=0; (*p !=
'\0') && (i < 3); i++)
1147 (void) GetNextToken(p,&p,MagickPathExtent,token);
1149 (void) GetNextToken(p,&p,MagickPathExtent,token);
1154 color_correction.red.offset=StringToDouble(token,
1160 color_correction.green.offset=StringToDouble(token,
1166 color_correction.blue.offset=StringToDouble(token,
1173 power=GetXMLTreeChild(sop,
"Power");
1176 content=GetXMLTreeContent(power);
1177 p=(
const char *) content;
1178 for (i=0; (*p !=
'\0') && (i < 3); i++)
1180 (void) GetNextToken(p,&p,MagickPathExtent,token);
1182 (void) GetNextToken(p,&p,MagickPathExtent,token);
1187 color_correction.red.power=StringToDouble(token,(
char **) NULL);
1192 color_correction.green.power=StringToDouble(token,
1198 color_correction.blue.power=StringToDouble(token,
1206 sat=GetXMLTreeChild(cc,
"SATNode");
1212 saturation=GetXMLTreeChild(sat,
"Saturation");
1215 content=GetXMLTreeContent(saturation);
1216 p=(
const char *) content;
1217 (void) GetNextToken(p,&p,MagickPathExtent,token);
1218 color_correction.saturation=StringToDouble(token,(
char **) NULL);
1221 ccc=DestroyXMLTree(ccc);
1222 if (image->debug != MagickFalse)
1224 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1225 " Color Correction Collection:");
1226 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1227 " color_correction.red.slope: %g",color_correction.red.slope);
1228 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1229 " color_correction.red.offset: %g",color_correction.red.offset);
1230 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1231 " color_correction.red.power: %g",color_correction.red.power);
1232 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1233 " color_correction.green.slope: %g",color_correction.green.slope);
1234 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1235 " color_correction.green.offset: %g",color_correction.green.offset);
1236 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1237 " color_correction.green.power: %g",color_correction.green.power);
1238 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1239 " color_correction.blue.slope: %g",color_correction.blue.slope);
1240 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1241 " color_correction.blue.offset: %g",color_correction.blue.offset);
1242 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1243 " color_correction.blue.power: %g",color_correction.blue.power);
1244 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1245 " color_correction.saturation: %g",color_correction.saturation);
1247 cdl_map=(
PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*cdl_map));
1249 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1251 for (i=0; i <= (ssize_t) MaxMap; i++)
1253 cdl_map[i].red=(double) ScaleMapToQuantum((
double)
1254 (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
1255 color_correction.red.offset,color_correction.red.power))));
1256 cdl_map[i].green=(double) ScaleMapToQuantum((
double)
1257 (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
1258 color_correction.green.offset,color_correction.green.power))));
1259 cdl_map[i].blue=(double) ScaleMapToQuantum((
double)
1260 (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
1261 color_correction.blue.offset,color_correction.blue.power))));
1263 if (image->storage_class == PseudoClass)
1264 for (i=0; i < (ssize_t) image->colors; i++)
1272 luma=0.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
1273 0.07217*image->colormap[i].blue;
1274 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
1275 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-luma;
1276 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
1277 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-luma;
1278 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
1279 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-luma;
1286 image_view=AcquireAuthenticCacheView(image,exception);
1287#if defined(MAGICKCORE_OPENMP_SUPPORT)
1288 #pragma omp parallel for schedule(static) shared(progress,status) \
1289 magick_number_threads(image,image,image->rows,1)
1291 for (y=0; y < (ssize_t) image->rows; y++)
1302 if (status == MagickFalse)
1304 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1305 if (q == (Quantum *) NULL)
1310 for (x=0; x < (ssize_t) image->columns; x++)
1312 luma=0.21267*(double) GetPixelRed(image,q)+0.71526*(double)
1313 GetPixelGreen(image,q)+0.07217*(double) GetPixelBlue(image,q);
1314 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
1315 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
1316 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
1317 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
1318 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
1319 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
1320 q+=(ptrdiff_t) GetPixelChannels(image);
1322 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1324 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1329#if defined(MAGICKCORE_OPENMP_SUPPORT)
1333 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
1334 progress,image->rows);
1335 if (proceed == MagickFalse)
1339 image_view=DestroyCacheView(image_view);
1340 cdl_map=(
PixelInfo *) RelinquishMagickMemory(cdl_map);
1374static inline void Contrast(
const int sign,
double *red,
double *green,
1385 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
1386 brightness+=0.5*sign*(0.5*(sin((
double) (MagickPI*(brightness-0.5)))+1.0)-
1388 if (brightness > 1.0)
1391 if (brightness < 0.0)
1393 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
1396MagickExport MagickBooleanType ContrastImage(
Image *image,
1399#define ContrastImageTag "Contrast/Image"
1419 assert(image != (
Image *) NULL);
1420 assert(image->signature == MagickCoreSignature);
1421#if defined(MAGICKCORE_OPENCL_SUPPORT)
1422 if (AccelerateContrastImage(image,sharpen,exception) != MagickFalse)
1425 if (IsEventLogging() != MagickFalse)
1426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1427 sign=sharpen != MagickFalse ? 1 : -1;
1428 if (image->storage_class == PseudoClass)
1433 for (i=0; i < (ssize_t) image->colors; i++)
1440 red=(double) image->colormap[i].red;
1441 green=(double) image->colormap[i].green;
1442 blue=(double) image->colormap[i].blue;
1443 Contrast(sign,&red,&green,&blue);
1444 image->colormap[i].red=(MagickRealType) red;
1445 image->colormap[i].green=(MagickRealType) green;
1446 image->colormap[i].blue=(MagickRealType) blue;
1454 image_view=AcquireAuthenticCacheView(image,exception);
1455#if defined(MAGICKCORE_OPENMP_SUPPORT)
1456 #pragma omp parallel for schedule(static) shared(progress,status) \
1457 magick_number_threads(image,image,image->rows,1)
1459 for (y=0; y < (ssize_t) image->rows; y++)
1472 if (status == MagickFalse)
1474 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1475 if (q == (Quantum *) NULL)
1480 for (x=0; x < (ssize_t) image->columns; x++)
1482 red=(double) GetPixelRed(image,q);
1483 green=(double) GetPixelGreen(image,q);
1484 blue=(double) GetPixelBlue(image,q);
1485 Contrast(sign,&red,&green,&blue);
1486 SetPixelRed(image,ClampToQuantum(red),q);
1487 SetPixelGreen(image,ClampToQuantum(green),q);
1488 SetPixelBlue(image,ClampToQuantum(blue),q);
1489 q+=(ptrdiff_t) GetPixelChannels(image);
1491 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1493 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1498#if defined(MAGICKCORE_OPENMP_SUPPORT)
1502 proceed=SetImageProgress(image,ContrastImageTag,progress,image->rows);
1503 if (proceed == MagickFalse)
1507 image_view=DestroyCacheView(image_view);
1548MagickExport MagickBooleanType ContrastStretchImage(
Image *image,
1549 const double black_point,
const double white_point,
ExceptionInfo *exception)
1551#define MaxRange(color) ((double) ScaleQuantumToMap((Quantum) (color)))
1552#define ContrastStretchImageTag "ContrastStretch/Image"
1558 property[MagickPathExtent];
1586 assert(image != (
Image *) NULL);
1587 assert(image->signature == MagickCoreSignature);
1588 if (IsEventLogging() != MagickFalse)
1589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1590 type=IdentifyImageType(image,exception);
1591 if (IsGrayImageType(type) != MagickFalse)
1592 (void) SetImageColorspace(image,GRAYColorspace,exception);
1593 black=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*black));
1594 white=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*white));
1595 stretch_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1596 sizeof(*stretch_map));
1597 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1598 sizeof(*histogram));
1599 if ((black == (Quantum *) NULL) || (white == (Quantum *) NULL) ||
1600 (stretch_map == (Quantum *) NULL) || (histogram == (
double *) NULL))
1602 if (histogram != (
double *) NULL)
1603 histogram=(
double *) RelinquishMagickMemory(histogram);
1604 if (stretch_map != (Quantum *) NULL)
1605 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1606 if (white != (Quantum *) NULL)
1607 white=(Quantum *) RelinquishMagickMemory(white);
1608 if (black != (Quantum *) NULL)
1609 black=(Quantum *) RelinquishMagickMemory(black);
1610 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1617 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1618 sizeof(*histogram));
1619 image_view=AcquireVirtualCacheView(image,exception);
1620 for (y=0; y < (ssize_t) image->rows; y++)
1628 if (status == MagickFalse)
1630 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1631 if (p == (
const Quantum *) NULL)
1636 for (x=0; x < (ssize_t) image->columns; x++)
1641 pixel=GetPixelIntensity(image,p);
1642 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1644 if (image->channel_mask != AllChannels)
1645 pixel=(double) p[i];
1646 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
1647 ClampToQuantum(pixel))+(size_t) i]++;
1649 p+=(ptrdiff_t) GetPixelChannels(image);
1652 image_view=DestroyCacheView(image_view);
1656 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1665 white[i]=MaxRange(QuantumRange);
1667 for (j=0; j <= (ssize_t) MaxMap; j++)
1669 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1670 if (intensity > black_point)
1673 black[i]=(Quantum) j;
1675 for (j=(ssize_t) MaxMap; j != 0; j--)
1677 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1678 if (intensity > ((
double) image->columns*image->rows-white_point))
1681 white[i]=(Quantum) j;
1683 histogram=(
double *) RelinquishMagickMemory(histogram);
1687 (void) memset(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1688 sizeof(*stretch_map));
1689 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1694 for (j=0; j <= (ssize_t) MaxMap; j++)
1699 gamma=PerceptibleReciprocal(white[i]-black[i]);
1700 if (j < (ssize_t) black[i])
1701 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=0.0;
1703 if (j > (ssize_t) white[i])
1704 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=QuantumRange;
1706 if (black[i] != white[i])
1707 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=
1708 ScaleMapToQuantum((
double) (MaxMap*gamma*(j-(
double) black[i])));
1711 if (image->storage_class == PseudoClass)
1719 for (j=0; j < (ssize_t) image->colors; j++)
1721 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1723 i=GetPixelChannelOffset(image,RedPixelChannel);
1724 image->colormap[j].red=(MagickRealType) stretch_map[
1725 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1726 image->colormap[j].red))+(size_t) i];
1728 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1730 i=GetPixelChannelOffset(image,GreenPixelChannel);
1731 image->colormap[j].green=(MagickRealType) stretch_map[
1732 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1733 image->colormap[j].green))+(size_t) i];
1735 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1737 i=GetPixelChannelOffset(image,BluePixelChannel);
1738 image->colormap[j].blue=(MagickRealType) stretch_map[
1739 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1740 image->colormap[j].blue))+(size_t) i];
1742 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1744 i=GetPixelChannelOffset(image,AlphaPixelChannel);
1745 image->colormap[j].alpha=(MagickRealType) stretch_map[
1746 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1747 image->colormap[j].alpha))+(size_t) i];
1756 image_view=AcquireAuthenticCacheView(image,exception);
1757#if defined(MAGICKCORE_OPENMP_SUPPORT)
1758 #pragma omp parallel for schedule(static) shared(progress,status) \
1759 magick_number_threads(image,image,image->rows,1)
1761 for (y=0; y < (ssize_t) image->rows; y++)
1769 if (status == MagickFalse)
1771 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1772 if (q == (Quantum *) NULL)
1777 for (x=0; x < (ssize_t) image->columns; x++)
1782 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1784 PixelChannel channel = GetPixelChannelChannel(image,j);
1785 PixelTrait traits = GetPixelChannelTraits(image,channel);
1786 if ((traits & UpdatePixelTrait) == 0)
1788 if (black[j] == white[j])
1790 q[j]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1791 ScaleQuantumToMap(q[j])+(
size_t) j]);
1793 q+=(ptrdiff_t) GetPixelChannels(image);
1795 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1797 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1802#if defined(MAGICKCORE_OPENMP_SUPPORT)
1806 proceed=SetImageProgress(image,ContrastStretchImageTag,progress,
1808 if (proceed == MagickFalse)
1812 image_view=DestroyCacheView(image_view);
1813 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*
1814 QuantumScale*GetPixelIntensity(image,black),100.0*QuantumScale*
1815 GetPixelIntensity(image,white));
1816 (void) SetImageProperty(image,
"histogram:contrast-stretch",property,
1818 white=(Quantum *) RelinquishMagickMemory(white);
1819 black=(Quantum *) RelinquishMagickMemory(black);
1820 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1851#define EnhanceImageTag "Enhance/Image"
1852#define EnhancePixel(weight) \
1853 mean=QuantumScale*((double) GetPixelRed(image,r)+pixel.red)/2.0; \
1854 distance=QuantumScale*((double) GetPixelRed(image,r)-pixel.red); \
1855 distance_squared=(4.0+mean)*distance*distance; \
1856 mean=QuantumScale*((double) GetPixelGreen(image,r)+pixel.green)/2.0; \
1857 distance=QuantumScale*((double) GetPixelGreen(image,r)-pixel.green); \
1858 distance_squared+=(7.0-mean)*distance*distance; \
1859 mean=QuantumScale*((double) GetPixelBlue(image,r)+pixel.blue)/2.0; \
1860 distance=QuantumScale*((double) GetPixelBlue(image,r)-pixel.blue); \
1861 distance_squared+=(5.0-mean)*distance*distance; \
1862 mean=QuantumScale*((double) GetPixelBlack(image,r)+pixel.black)/2.0; \
1863 distance=QuantumScale*((double) GetPixelBlack(image,r)-pixel.black); \
1864 distance_squared+=(5.0-mean)*distance*distance; \
1865 mean=QuantumScale*((double) GetPixelAlpha(image,r)+pixel.alpha)/2.0; \
1866 distance=QuantumScale*((double) GetPixelAlpha(image,r)-pixel.alpha); \
1867 distance_squared+=(5.0-mean)*distance*distance; \
1868 if (distance_squared < 0.069) \
1870 aggregate.red+=(weight)*(double) GetPixelRed(image,r); \
1871 aggregate.green+=(weight)*(double) GetPixelGreen(image,r); \
1872 aggregate.blue+=(weight)*(double) GetPixelBlue(image,r); \
1873 aggregate.black+=(weight)*(double) GetPixelBlack(image,r); \
1874 aggregate.alpha+=(weight)*(double) GetPixelAlpha(image,r); \
1875 total_weight+=(weight); \
1877 r+=(ptrdiff_t) GetPixelChannels(image);
1898 assert(image != (
const Image *) NULL);
1899 assert(image->signature == MagickCoreSignature);
1900 if (IsEventLogging() != MagickFalse)
1901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1903 assert(exception->signature == MagickCoreSignature);
1904 enhance_image=CloneImage(image,0,0,MagickTrue,
1906 if (enhance_image == (
Image *) NULL)
1907 return((
Image *) NULL);
1908 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1910 enhance_image=DestroyImage(enhance_image);
1911 return((
Image *) NULL);
1918 image_view=AcquireVirtualCacheView(image,exception);
1919 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1920#if defined(MAGICKCORE_OPENMP_SUPPORT)
1921 #pragma omp parallel for schedule(static) shared(progress,status) \
1922 magick_number_threads(image,enhance_image,image->rows,1)
1924 for (y=0; y < (ssize_t) image->rows; y++)
1941 if (status == MagickFalse)
1943 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1944 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1946 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
1951 center=(ssize_t) GetPixelChannels(image)*(2*((ssize_t) image->columns+4)+2);
1952 GetPixelInfo(image,&pixel);
1953 for (x=0; x < (ssize_t) image->columns; x++)
1967 GetPixelInfo(image,&aggregate);
1969 GetPixelInfoPixel(image,p+center,&pixel);
1971 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1972 EnhancePixel(8.0); EnhancePixel(5.0);
1973 r=p+GetPixelChannels(image)*(image->columns+4);
1974 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1975 EnhancePixel(20.0); EnhancePixel(8.0);
1976 r=p+2*GetPixelChannels(image)*(image->columns+4);
1977 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1978 EnhancePixel(40.0); EnhancePixel(10.0);
1979 r=p+3*GetPixelChannels(image)*(image->columns+4);
1980 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1981 EnhancePixel(20.0); EnhancePixel(8.0);
1982 r=p+4*GetPixelChannels(image)*(image->columns+4);
1983 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1984 EnhancePixel(8.0); EnhancePixel(5.0);
1985 if (total_weight > MagickEpsilon)
1987 pixel.red=((aggregate.red+total_weight/2.0)/total_weight);
1988 pixel.green=((aggregate.green+total_weight/2.0)/total_weight);
1989 pixel.blue=((aggregate.blue+total_weight/2.0)/total_weight);
1990 pixel.black=((aggregate.black+total_weight/2.0)/total_weight);
1991 pixel.alpha=((aggregate.alpha+total_weight/2.0)/total_weight);
1993 SetPixelViaPixelInfo(enhance_image,&pixel,q);
1994 p+=(ptrdiff_t) GetPixelChannels(image);
1995 q+=(ptrdiff_t) GetPixelChannels(enhance_image);
1997 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
1999 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2004#if defined(MAGICKCORE_OPENMP_SUPPORT)
2008 proceed=SetImageProgress(image,EnhanceImageTag,progress,image->rows);
2009 if (proceed == MagickFalse)
2013 enhance_view=DestroyCacheView(enhance_view);
2014 image_view=DestroyCacheView(image_view);
2015 if (status == MagickFalse)
2016 enhance_image=DestroyImage(enhance_image);
2017 return(enhance_image);
2044MagickExport MagickBooleanType EqualizeImage(
Image *image,
2047#define EqualizeImageTag "Equalize/Image"
2053 black[2*CompositePixelChannel+1],
2057 white[2*CompositePixelChannel+1];
2074 assert(image != (
Image *) NULL);
2075 assert(image->signature == MagickCoreSignature);
2076#if defined(MAGICKCORE_OPENCL_SUPPORT)
2077 if (AccelerateEqualizeImage(image,exception) != MagickFalse)
2080 if (IsEventLogging() != MagickFalse)
2081 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2082 equalize_map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2083 sizeof(*equalize_map));
2084 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2085 sizeof(*histogram));
2086 map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
sizeof(*map));
2087 if ((equalize_map == (
double *) NULL) || (histogram == (
double *) NULL) ||
2088 (map == (
double *) NULL))
2090 if (map != (
double *) NULL)
2091 map=(
double *) RelinquishMagickMemory(map);
2092 if (histogram != (
double *) NULL)
2093 histogram=(
double *) RelinquishMagickMemory(histogram);
2094 if (equalize_map != (
double *) NULL)
2095 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2096 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2103 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
2104 sizeof(*histogram));
2105 image_view=AcquireVirtualCacheView(image,exception);
2106 for (y=0; y < (ssize_t) image->rows; y++)
2114 if (status == MagickFalse)
2116 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2117 if (p == (
const Quantum *) NULL)
2122 for (x=0; x < (ssize_t) image->columns; x++)
2124 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2129 intensity=(double) p[i];
2130 if ((image->channel_mask & SyncChannels) != 0)
2131 intensity=GetPixelIntensity(image,p);
2132 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
2133 ClampToQuantum(intensity))+(size_t) i]++;
2135 p+=(ptrdiff_t) GetPixelChannels(image);
2138 image_view=DestroyCacheView(image_view);
2142 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2151 for (j=0; j <= (ssize_t) MaxMap; j++)
2153 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
2154 map[(ssize_t) GetPixelChannels(image)*j+i]=intensity;
2157 (void) memset(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
2158 sizeof(*equalize_map));
2159 (void) memset(black,0,
sizeof(*black));
2160 (void) memset(white,0,
sizeof(*white));
2161 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2167 white[i]=map[GetPixelChannels(image)*MaxMap+(size_t) i];
2168 if (black[i] != white[i])
2169 for (j=0; j <= (ssize_t) MaxMap; j++)
2170 equalize_map[GetPixelChannels(image)*(size_t) j+(
size_t) i]=(double)
2171 ScaleMapToQuantum((
double) ((MaxMap*(map[GetPixelChannels(image)*
2172 (size_t) j+(
size_t) i]-black[i]))/(white[i]-black[i])));
2174 histogram=(
double *) RelinquishMagickMemory(histogram);
2175 map=(
double *) RelinquishMagickMemory(map);
2176 if (image->storage_class == PseudoClass)
2184 for (j=0; j < (ssize_t) image->colors; j++)
2186 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2188 PixelChannel channel = GetPixelChannelChannel(image,
2190 if (black[channel] != white[channel])
2191 image->colormap[j].red=equalize_map[(ssize_t)
2192 GetPixelChannels(image)*ScaleQuantumToMap(
2193 ClampToQuantum(image->colormap[j].red))+channel];
2195 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2197 PixelChannel channel = GetPixelChannelChannel(image,
2199 if (black[channel] != white[channel])
2200 image->colormap[j].green=equalize_map[(ssize_t)
2201 GetPixelChannels(image)*ScaleQuantumToMap(
2202 ClampToQuantum(image->colormap[j].green))+channel];
2204 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2206 PixelChannel channel = GetPixelChannelChannel(image,
2208 if (black[channel] != white[channel])
2209 image->colormap[j].blue=equalize_map[(ssize_t)
2210 GetPixelChannels(image)*ScaleQuantumToMap(
2211 ClampToQuantum(image->colormap[j].blue))+channel];
2213 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2215 PixelChannel channel = GetPixelChannelChannel(image,
2217 if (black[channel] != white[channel])
2218 image->colormap[j].alpha=equalize_map[(ssize_t)
2219 GetPixelChannels(image)*ScaleQuantumToMap(
2220 ClampToQuantum(image->colormap[j].alpha))+channel];
2228 image_view=AcquireAuthenticCacheView(image,exception);
2229#if defined(MAGICKCORE_OPENMP_SUPPORT)
2230 #pragma omp parallel for schedule(static) shared(progress,status) \
2231 magick_number_threads(image,image,image->rows,1)
2233 for (y=0; y < (ssize_t) image->rows; y++)
2241 if (status == MagickFalse)
2243 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2244 if (q == (Quantum *) NULL)
2249 for (x=0; x < (ssize_t) image->columns; x++)
2254 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2256 PixelChannel channel = GetPixelChannelChannel(image,j);
2257 PixelTrait traits = GetPixelChannelTraits(image,channel);
2258 if (((traits & UpdatePixelTrait) == 0) || (black[j] == white[j]))
2260 q[j]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
2261 ScaleQuantumToMap(q[j])+(
size_t) j]);
2263 q+=(ptrdiff_t) GetPixelChannels(image);
2265 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2267 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2272#if defined(MAGICKCORE_OPENMP_SUPPORT)
2276 proceed=SetImageProgress(image,EqualizeImageTag,progress,image->rows);
2277 if (proceed == MagickFalse)
2281 image_view=DestroyCacheView(image_view);
2282 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2321static inline double gamma_pow(
const double value,
const double gamma)
2323 return(value < 0.0 ? value : pow(value,gamma));
2326MagickExport MagickBooleanType GammaImage(
Image *image,
const double gamma,
2329#define GammaImageTag "Gamma/Image"
2352 assert(image != (
Image *) NULL);
2353 assert(image->signature == MagickCoreSignature);
2354 if (IsEventLogging() != MagickFalse)
2355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2358 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*gamma_map));
2359 if (gamma_map == (Quantum *) NULL)
2360 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2362 (void) memset(gamma_map,0,(MaxMap+1)*
sizeof(*gamma_map));
2364 for (i=0; i <= (ssize_t) MaxMap; i++)
2365 gamma_map[i]=ScaleMapToQuantum((
double) (MaxMap*pow((
double) i/
2366 MaxMap,PerceptibleReciprocal(gamma))));
2367 if (image->storage_class == PseudoClass)
2368 for (i=0; i < (ssize_t) image->colors; i++)
2373 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2374 image->colormap[i].red=(
double) gamma_map[ScaleQuantumToMap(
2375 ClampToQuantum(image->colormap[i].red))];
2376 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2377 image->colormap[i].green=(double) gamma_map[ScaleQuantumToMap(
2378 ClampToQuantum(image->colormap[i].green))];
2379 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2380 image->colormap[i].blue=(
double) gamma_map[ScaleQuantumToMap(
2381 ClampToQuantum(image->colormap[i].blue))];
2382 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2383 image->colormap[i].alpha=(double) gamma_map[ScaleQuantumToMap(
2384 ClampToQuantum(image->colormap[i].alpha))];
2391 image_view=AcquireAuthenticCacheView(image,exception);
2392#if defined(MAGICKCORE_OPENMP_SUPPORT)
2393 #pragma omp parallel for schedule(static) shared(progress,status) \
2394 magick_number_threads(image,image,image->rows,1)
2396 for (y=0; y < (ssize_t) image->rows; y++)
2404 if (status == MagickFalse)
2406 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2407 if (q == (Quantum *) NULL)
2412 for (x=0; x < (ssize_t) image->columns; x++)
2417 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2419 PixelChannel channel = GetPixelChannelChannel(image,j);
2420 PixelTrait traits = GetPixelChannelTraits(image,channel);
2421 if ((traits & UpdatePixelTrait) == 0)
2423 q[j]=gamma_map[ScaleQuantumToMap(ClampToQuantum((MagickRealType)
2426 q+=(ptrdiff_t) GetPixelChannels(image);
2428 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2435#if defined(MAGICKCORE_OPENMP_SUPPORT)
2439 proceed=SetImageProgress(image,GammaImageTag,progress,image->rows);
2440 if (proceed == MagickFalse)
2444 image_view=DestroyCacheView(image_view);
2445 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
2446 if (image->gamma != 0.0)
2447 image->gamma*=gamma;
2478MagickExport MagickBooleanType GrayscaleImage(
Image *image,
2481#define GrayscaleImageTag "Grayscale/Image"
2495 assert(image != (
Image *) NULL);
2496 assert(image->signature == MagickCoreSignature);
2497 if (IsEventLogging() != MagickFalse)
2498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2499 if (image->storage_class == PseudoClass)
2501 if (SyncImage(image,exception) == MagickFalse)
2502 return(MagickFalse);
2503 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2504 return(MagickFalse);
2506#if defined(MAGICKCORE_OPENCL_SUPPORT)
2507 if (AccelerateGrayscaleImage(image,method,exception) != MagickFalse)
2509 image->intensity=method;
2510 image->type=GrayscaleType;
2511 if ((method == Rec601LuminancePixelIntensityMethod) ||
2512 (method == Rec709LuminancePixelIntensityMethod))
2513 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2514 return(SetImageColorspace(image,GRAYColorspace,exception));
2522 image_view=AcquireAuthenticCacheView(image,exception);
2523#if defined(MAGICKCORE_OPENMP_SUPPORT)
2524 #pragma omp parallel for schedule(static) shared(progress,status) \
2525 magick_number_threads(image,image,image->rows,1)
2527 for (y=0; y < (ssize_t) image->rows; y++)
2535 if (status == MagickFalse)
2537 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2538 if (q == (Quantum *) NULL)
2543 for (x=0; x < (ssize_t) image->columns; x++)
2551 red=(MagickRealType) GetPixelRed(image,q);
2552 green=(MagickRealType) GetPixelGreen(image,q);
2553 blue=(MagickRealType) GetPixelBlue(image,q);
2557 case AveragePixelIntensityMethod:
2559 intensity=(red+green+blue)/3.0;
2562 case BrightnessPixelIntensityMethod:
2564 intensity=MagickMax(MagickMax(red,green),blue);
2567 case LightnessPixelIntensityMethod:
2569 intensity=(MagickMin(MagickMin(red,green),blue)+
2570 MagickMax(MagickMax(red,green),blue))/2.0;
2573 case MSPixelIntensityMethod:
2575 intensity=(MagickRealType) (((
double) red*red+green*green+
2579 case Rec601LumaPixelIntensityMethod:
2581 if (image->colorspace == RGBColorspace)
2583 red=EncodePixelGamma(red);
2584 green=EncodePixelGamma(green);
2585 blue=EncodePixelGamma(blue);
2587 intensity=0.298839*red+0.586811*green+0.114350*blue;
2590 case Rec601LuminancePixelIntensityMethod:
2592 if (image->colorspace == sRGBColorspace)
2594 red=DecodePixelGamma(red);
2595 green=DecodePixelGamma(green);
2596 blue=DecodePixelGamma(blue);
2598 intensity=0.298839*red+0.586811*green+0.114350*blue;
2601 case Rec709LumaPixelIntensityMethod:
2604 if (image->colorspace == RGBColorspace)
2606 red=EncodePixelGamma(red);
2607 green=EncodePixelGamma(green);
2608 blue=EncodePixelGamma(blue);
2610 intensity=0.212656*red+0.715158*green+0.072186*blue;
2613 case Rec709LuminancePixelIntensityMethod:
2615 if (image->colorspace == sRGBColorspace)
2617 red=DecodePixelGamma(red);
2618 green=DecodePixelGamma(green);
2619 blue=DecodePixelGamma(blue);
2621 intensity=0.212656*red+0.715158*green+0.072186*blue;
2624 case RMSPixelIntensityMethod:
2626 intensity=(MagickRealType) (sqrt((
double) red*red+green*green+
2627 blue*blue)/sqrt(3.0));
2631 SetPixelGray(image,ClampToQuantum(intensity),q);
2632 q+=(ptrdiff_t) GetPixelChannels(image);
2634 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2636 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2641#if defined(MAGICKCORE_OPENMP_SUPPORT)
2645 proceed=SetImageProgress(image,GrayscaleImageTag,progress,image->rows);
2646 if (proceed == MagickFalse)
2650 image_view=DestroyCacheView(image_view);
2651 image->intensity=method;
2652 image->type=GrayscaleType;
2653 if ((method == Rec601LuminancePixelIntensityMethod) ||
2654 (method == Rec709LuminancePixelIntensityMethod))
2655 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2656 return(SetImageColorspace(image,GRAYColorspace,exception));
2690MagickExport MagickBooleanType HaldClutImage(
Image *image,
2693#define HaldClutImageTag "Clut/Image"
2695 typedef struct _HaldInfo
2727 assert(image != (
Image *) NULL);
2728 assert(image->signature == MagickCoreSignature);
2729 if (IsEventLogging() != MagickFalse)
2730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2731 assert(hald_image != (
Image *) NULL);
2732 assert(hald_image->signature == MagickCoreSignature);
2733 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2734 return(MagickFalse);
2735 if ((image->alpha_trait & BlendPixelTrait) == 0)
2736 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2737 if (image->colorspace != hald_image->colorspace)
2738 (void) SetImageColorspace(image,hald_image->colorspace,exception);
2744 length=(size_t) MagickMin((MagickRealType) hald_image->columns,
2745 (MagickRealType) hald_image->rows);
2746 for (level=2; (level*level*level) < length; level++) ;
2748 cube_size=level*level;
2749 width=(double) hald_image->columns;
2750 GetPixelInfo(hald_image,&zero);
2751 hald_view=AcquireVirtualCacheView(hald_image,exception);
2752 image_view=AcquireAuthenticCacheView(image,exception);
2753#if defined(MAGICKCORE_OPENMP_SUPPORT)
2754 #pragma omp parallel for schedule(static) shared(progress,status) \
2755 magick_number_threads(image,image,image->rows,1)
2757 for (y=0; y < (ssize_t) image->rows; y++)
2765 if (status == MagickFalse)
2767 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2768 if (q == (Quantum *) NULL)
2773 for (x=0; x < (ssize_t) image->columns; x++)
2780 point = { 0, 0, 0 };
2789 point.x=QuantumScale*(level-1.0)*(
double) GetPixelRed(image,q);
2790 point.y=QuantumScale*(level-1.0)*(
double) GetPixelGreen(image,q);
2791 point.z=QuantumScale*(level-1.0)*(
double) GetPixelBlue(image,q);
2792 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2793 point.x-=floor(point.x);
2794 point.y-=floor(point.y);
2795 point.z-=floor(point.z);
2796 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2797 fmod(offset,width),floor(offset/width),&pixel1,exception);
2798 if (status == MagickFalse)
2800 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2801 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2802 if (status == MagickFalse)
2805 if (hald_image->interpolate == NearestInterpolatePixel)
2806 area=(point.y < 0.5) ? 0.0 : 1.0;
2807 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2810 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2811 fmod(offset,width),floor(offset/width),&pixel1,exception);
2812 if (status == MagickFalse)
2814 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2815 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2816 if (status == MagickFalse)
2818 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2821 if (hald_image->interpolate == NearestInterpolatePixel)
2822 area=(point.z < 0.5)? 0.0 : 1.0;
2823 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2825 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2826 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2827 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2828 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2829 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2830 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2831 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2832 (image->colorspace == CMYKColorspace))
2833 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2834 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2835 (image->alpha_trait != UndefinedPixelTrait))
2836 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2837 q+=(ptrdiff_t) GetPixelChannels(image);
2839 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2841 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2846#if defined(MAGICKCORE_OPENMP_SUPPORT)
2850 proceed=SetImageProgress(image,HaldClutImageTag,progress,image->rows);
2851 if (proceed == MagickFalse)
2855 hald_view=DestroyCacheView(hald_view);
2856 image_view=DestroyCacheView(image_view);
2904static inline double LevelPixel(
const double black_point,
2905 const double white_point,
const double gamma,
const double pixel)
2911 scale=PerceptibleReciprocal(white_point-black_point);
2912 level_pixel=(double) QuantumRange*gamma_pow(scale*((
double) pixel-(
double)
2913 black_point),PerceptibleReciprocal(gamma));
2914 return(level_pixel);
2917MagickExport MagickBooleanType LevelImage(
Image *image,
const double black_point,
2918 const double white_point,
const double gamma,
ExceptionInfo *exception)
2920#define LevelImageTag "Level/Image"
2938 assert(image != (
Image *) NULL);
2939 assert(image->signature == MagickCoreSignature);
2940 if (IsEventLogging() != MagickFalse)
2941 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2942 if (image->storage_class == PseudoClass)
2943 for (i=0; i < (ssize_t) image->colors; i++)
2948 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2949 image->colormap[i].red=(
double) ClampToQuantum(LevelPixel(black_point,
2950 white_point,gamma,image->colormap[i].red));
2951 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2952 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2953 white_point,gamma,image->colormap[i].green));
2954 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2955 image->colormap[i].blue=(
double) ClampToQuantum(LevelPixel(black_point,
2956 white_point,gamma,image->colormap[i].blue));
2957 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2958 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2959 white_point,gamma,image->colormap[i].alpha));
2966 image_view=AcquireAuthenticCacheView(image,exception);
2967#if defined(MAGICKCORE_OPENMP_SUPPORT)
2968 #pragma omp parallel for schedule(static) shared(progress,status) \
2969 magick_number_threads(image,image,image->rows,1)
2971 for (y=0; y < (ssize_t) image->rows; y++)
2979 if (status == MagickFalse)
2981 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2982 if (q == (Quantum *) NULL)
2987 for (x=0; x < (ssize_t) image->columns; x++)
2992 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2994 PixelChannel channel = GetPixelChannelChannel(image,j);
2995 PixelTrait traits = GetPixelChannelTraits(image,channel);
2996 if ((traits & UpdatePixelTrait) == 0)
2998 q[j]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
3001 q+=(ptrdiff_t) GetPixelChannels(image);
3003 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3005 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3010#if defined(MAGICKCORE_OPENMP_SUPPORT)
3014 proceed=SetImageProgress(image,LevelImageTag,progress,image->rows);
3015 if (proceed == MagickFalse)
3019 image_view=DestroyCacheView(image_view);
3020 (void) ClampImage(image,exception);
3066MagickExport MagickBooleanType LevelizeImage(
Image *image,
3067 const double black_point,
const double white_point,
const double gamma,
3070#define LevelizeImageTag "Levelize/Image"
3071#define LevelizeValue(x) ClampToQuantum(((MagickRealType) gamma_pow((double) \
3072 (QuantumScale*((double) x)),gamma))*(white_point-black_point)+black_point)
3092 assert(image != (
Image *) NULL);
3093 assert(image->signature == MagickCoreSignature);
3094 if (IsEventLogging() != MagickFalse)
3095 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3096 if (image->storage_class == PseudoClass)
3097 for (i=0; i < (ssize_t) image->colors; i++)
3102 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3103 image->colormap[i].red=(
double) LevelizeValue(image->colormap[i].red);
3104 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3105 image->colormap[i].green=(double) LevelizeValue(
3106 image->colormap[i].green);
3107 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3108 image->colormap[i].blue=(
double) LevelizeValue(image->colormap[i].blue);
3109 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3110 image->colormap[i].alpha=(double) LevelizeValue(
3111 image->colormap[i].alpha);
3118 image_view=AcquireAuthenticCacheView(image,exception);
3119#if defined(MAGICKCORE_OPENMP_SUPPORT)
3120 #pragma omp parallel for schedule(static) shared(progress,status) \
3121 magick_number_threads(image,image,image->rows,1)
3123 for (y=0; y < (ssize_t) image->rows; y++)
3131 if (status == MagickFalse)
3133 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3134 if (q == (Quantum *) NULL)
3139 for (x=0; x < (ssize_t) image->columns; x++)
3144 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
3146 PixelChannel channel = GetPixelChannelChannel(image,j);
3147 PixelTrait traits = GetPixelChannelTraits(image,channel);
3148 if ((traits & UpdatePixelTrait) == 0)
3150 q[j]=LevelizeValue(q[j]);
3152 q+=(ptrdiff_t) GetPixelChannels(image);
3154 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3156 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3161#if defined(MAGICKCORE_OPENMP_SUPPORT)
3165 proceed=SetImageProgress(image,LevelizeImageTag,progress,image->rows);
3166 if (proceed == MagickFalse)
3170 image_view=DestroyCacheView(image_view);
3215MagickExport MagickBooleanType LevelImageColors(
Image *image,
3228 assert(image != (
Image *) NULL);
3229 assert(image->signature == MagickCoreSignature);
3230 if (IsEventLogging() != MagickFalse)
3231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3232 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
3233 ((IsGrayColorspace(black_color->colorspace) == MagickFalse) ||
3234 (IsGrayColorspace(white_color->colorspace) == MagickFalse)))
3235 (
void) SetImageColorspace(image,sRGBColorspace,exception);
3237 if (invert == MagickFalse)
3239 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3241 channel_mask=SetImageChannelMask(image,RedChannel);
3242 status&=(MagickStatusType) LevelImage(image,black_color->red,
3243 white_color->red,1.0,exception);
3244 (void) SetImageChannelMask(image,channel_mask);
3246 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3248 channel_mask=SetImageChannelMask(image,GreenChannel);
3249 status&=(MagickStatusType) LevelImage(image,black_color->green,
3250 white_color->green,1.0,exception);
3251 (void) SetImageChannelMask(image,channel_mask);
3253 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3255 channel_mask=SetImageChannelMask(image,BlueChannel);
3256 status&=(MagickStatusType) LevelImage(image,black_color->blue,
3257 white_color->blue,1.0,exception);
3258 (void) SetImageChannelMask(image,channel_mask);
3260 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3261 (image->colorspace == CMYKColorspace))
3263 channel_mask=SetImageChannelMask(image,BlackChannel);
3264 status&=(MagickStatusType) LevelImage(image,black_color->black,
3265 white_color->black,1.0,exception);
3266 (void) SetImageChannelMask(image,channel_mask);
3268 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3269 (image->alpha_trait != UndefinedPixelTrait))
3271 channel_mask=SetImageChannelMask(image,AlphaChannel);
3272 status&=(MagickStatusType) LevelImage(image,black_color->alpha,
3273 white_color->alpha,1.0,exception);
3274 (void) SetImageChannelMask(image,channel_mask);
3279 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3281 channel_mask=SetImageChannelMask(image,RedChannel);
3282 status&=(MagickStatusType) LevelizeImage(image,black_color->red,
3283 white_color->red,1.0,exception);
3284 (void) SetImageChannelMask(image,channel_mask);
3286 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3288 channel_mask=SetImageChannelMask(image,GreenChannel);
3289 status&=(MagickStatusType) LevelizeImage(image,black_color->green,
3290 white_color->green,1.0,exception);
3291 (void) SetImageChannelMask(image,channel_mask);
3293 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3295 channel_mask=SetImageChannelMask(image,BlueChannel);
3296 status&=(MagickStatusType) LevelizeImage(image,black_color->blue,
3297 white_color->blue,1.0,exception);
3298 (void) SetImageChannelMask(image,channel_mask);
3300 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3301 (image->colorspace == CMYKColorspace))
3303 channel_mask=SetImageChannelMask(image,BlackChannel);
3304 status&=(MagickStatusType) LevelizeImage(image,black_color->black,
3305 white_color->black,1.0,exception);
3306 (void) SetImageChannelMask(image,channel_mask);
3308 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3309 (image->alpha_trait != UndefinedPixelTrait))
3311 channel_mask=SetImageChannelMask(image,AlphaChannel);
3312 status&=(MagickStatusType) LevelizeImage(image,black_color->alpha,
3313 white_color->alpha,1.0,exception);
3314 (void) SetImageChannelMask(image,channel_mask);
3317 return(status != 0 ? MagickTrue : MagickFalse);
3351MagickExport MagickBooleanType LinearStretchImage(
Image *image,
3352 const double black_point,
const double white_point,
ExceptionInfo *exception)
3354#define LinearStretchImageTag "LinearStretch/Image"
3360 property[MagickPathExtent];
3377 assert(image != (
Image *) NULL);
3378 assert(image->signature == MagickCoreSignature);
3379 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*histogram));
3380 if (histogram == (
double *) NULL)
3381 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
3386 (void) memset(histogram,0,(MaxMap+1)*
sizeof(*histogram));
3387 image_view=AcquireVirtualCacheView(image,exception);
3388 for (y=0; y < (ssize_t) image->rows; y++)
3396 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3397 if (p == (
const Quantum *) NULL)
3399 for (x=0; x < (ssize_t) image->columns; x++)
3401 intensity=GetPixelIntensity(image,p);
3402 histogram[ScaleQuantumToMap(ClampToQuantum(intensity))]++;
3403 p+=(ptrdiff_t) GetPixelChannels(image);
3406 image_view=DestroyCacheView(image_view);
3411 for (black=0; black < (ssize_t) MaxMap; black++)
3413 intensity+=histogram[black];
3414 if (intensity >= black_point)
3418 for (white=(ssize_t) MaxMap; white != 0; white--)
3420 intensity+=histogram[white];
3421 if (intensity >= white_point)
3424 histogram=(
double *) RelinquishMagickMemory(histogram);
3425 status=LevelImage(image,(
double) ScaleMapToQuantum((MagickRealType) black),
3426 (
double) ScaleMapToQuantum((MagickRealType) white),1.0,exception);
3427 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*black/
3428 MaxMap,100.0*white/MaxMap);
3429 (void) SetImageProperty(image,
"histogram:linear-stretch",property,exception);
3465static inline void ModulateHCL(
const double percent_hue,
3466 const double percent_chroma,
const double percent_luma,
double *red,
3467 double *green,
double *blue)
3477 ConvertRGBToHCL(*red,*green,*blue,&hue,&chroma,&luma);
3478 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3479 chroma*=0.01*percent_chroma;
3480 luma*=0.01*percent_luma;
3481 ConvertHCLToRGB(hue,chroma,luma,red,green,blue);
3484static inline void ModulateHCLp(
const double percent_hue,
3485 const double percent_chroma,
const double percent_luma,
double *red,
3486 double *green,
double *blue)
3496 ConvertRGBToHCLp(*red,*green,*blue,&hue,&chroma,&luma);
3497 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3498 chroma*=0.01*percent_chroma;
3499 luma*=0.01*percent_luma;
3500 ConvertHCLpToRGB(hue,chroma,luma,red,green,blue);
3503static inline void ModulateHSB(
const double percent_hue,
3504 const double percent_saturation,
const double percent_brightness,
double *red,
3505 double *green,
double *blue)
3515 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
3516 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3517 saturation*=0.01*percent_saturation;
3518 brightness*=0.01*percent_brightness;
3519 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
3522static inline void ModulateHSI(
const double percent_hue,
3523 const double percent_saturation,
const double percent_intensity,
double *red,
3524 double *green,
double *blue)
3534 ConvertRGBToHSI(*red,*green,*blue,&hue,&saturation,&intensity);
3535 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3536 saturation*=0.01*percent_saturation;
3537 intensity*=0.01*percent_intensity;
3538 ConvertHSIToRGB(hue,saturation,intensity,red,green,blue);
3541static inline void ModulateHSL(
const double percent_hue,
3542 const double percent_saturation,
const double percent_lightness,
double *red,
3543 double *green,
double *blue)
3553 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
3554 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3555 saturation*=0.01*percent_saturation;
3556 lightness*=0.01*percent_lightness;
3557 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
3560static inline void ModulateHSV(
const double percent_hue,
3561 const double percent_saturation,
const double percent_value,
double *red,
3562 double *green,
double *blue)
3572 ConvertRGBToHSV(*red,*green,*blue,&hue,&saturation,&value);
3573 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3574 saturation*=0.01*percent_saturation;
3575 value*=0.01*percent_value;
3576 ConvertHSVToRGB(hue,saturation,value,red,green,blue);
3579static inline void ModulateHWB(
const double percent_hue,
3580 const double percent_whiteness,
const double percent_blackness,
double *red,
3581 double *green,
double *blue)
3591 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
3592 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3593 blackness*=0.01*percent_blackness;
3594 whiteness*=0.01*percent_whiteness;
3595 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
3598static inline void ModulateLCHab(
const double percent_luma,
3599 const double percent_chroma,
const double percent_hue,
3600 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3610 ConvertRGBToLCHab(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3611 luma*=0.01*percent_luma;
3612 chroma*=0.01*percent_chroma;
3613 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3614 ConvertLCHabToRGB(luma,chroma,hue,illuminant,red,green,blue);
3617static inline void ModulateLCHuv(
const double percent_luma,
3618 const double percent_chroma,
const double percent_hue,
3619 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3629 ConvertRGBToLCHuv(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3630 luma*=0.01*percent_luma;
3631 chroma*=0.01*percent_chroma;
3632 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3633 ConvertLCHuvToRGB(luma,chroma,hue,illuminant,red,green,blue);
3636MagickExport MagickBooleanType ModulateImage(
Image *image,
const char *modulate,
3639#define ModulateImageTag "Modulate/Image"
3645 colorspace = UndefinedColorspace;
3651 percent_brightness = 100.0,
3652 percent_hue = 100.0,
3653 percent_saturation = 100.0;
3659 illuminant = D65Illuminant;
3679 assert(image != (
Image *) NULL);
3680 assert(image->signature == MagickCoreSignature);
3681 if (IsEventLogging() != MagickFalse)
3682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3683 if (modulate == (
char *) NULL)
3684 return(MagickFalse);
3685 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
3686 (
void) SetImageColorspace(image,sRGBColorspace,exception);
3687 flags=ParseGeometry(modulate,&geometry_info);
3688 if ((flags & RhoValue) != 0)
3689 percent_brightness=geometry_info.rho;
3690 if ((flags & SigmaValue) != 0)
3691 percent_saturation=geometry_info.sigma;
3692 if ((flags & XiValue) != 0)
3693 percent_hue=geometry_info.xi;
3694 artifact=GetImageArtifact(image,
"modulate:colorspace");
3695 if (artifact != (
const char *) NULL)
3696 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
3697 MagickFalse,artifact);
3698 artifact=GetImageArtifact(image,
"color:illuminant");
3699 if (artifact != (
const char *) NULL)
3704 illuminant_type=ParseCommandOption(MagickIlluminantOptions,MagickFalse,
3706 if (illuminant_type < 0)
3708 illuminant=UndefinedIlluminant;
3709 colorspace=UndefinedColorspace;
3712 illuminant=(IlluminantType) illuminant_type;
3714 if (image->storage_class == PseudoClass)
3715 for (i=0; i < (ssize_t) image->colors; i++)
3725 red=(double) image->colormap[i].red;
3726 green=(double) image->colormap[i].green;
3727 blue=(double) image->colormap[i].blue;
3732 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3736 case HCLpColorspace:
3738 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3744 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3750 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3757 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3763 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3769 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3774 case LCHabColorspace:
3776 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3777 illuminant,&red,&green,&blue);
3780 case LCHuvColorspace:
3782 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3783 illuminant,&red,&green,&blue);
3787 image->colormap[i].red=red;
3788 image->colormap[i].green=green;
3789 image->colormap[i].blue=blue;
3794#if defined(MAGICKCORE_OPENCL_SUPPORT)
3795 if (AccelerateModulateImage(image,percent_brightness,percent_hue,
3796 percent_saturation,colorspace,exception) != MagickFalse)
3801 image_view=AcquireAuthenticCacheView(image,exception);
3802#if defined(MAGICKCORE_OPENMP_SUPPORT)
3803 #pragma omp parallel for schedule(static) shared(progress,status) \
3804 magick_number_threads(image,image,image->rows,1)
3806 for (y=0; y < (ssize_t) image->rows; y++)
3814 if (status == MagickFalse)
3816 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3817 if (q == (Quantum *) NULL)
3822 for (x=0; x < (ssize_t) image->columns; x++)
3829 red=(double) GetPixelRed(image,q);
3830 green=(double) GetPixelGreen(image,q);
3831 blue=(double) GetPixelBlue(image,q);
3836 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3840 case HCLpColorspace:
3842 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3848 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3854 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3861 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3867 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3873 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3878 case LCHabColorspace:
3880 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3881 illuminant,&red,&green,&blue);
3884 case LCHuvColorspace:
3886 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3887 illuminant,&red,&green,&blue);
3891 SetPixelRed(image,ClampToQuantum(red),q);
3892 SetPixelGreen(image,ClampToQuantum(green),q);
3893 SetPixelBlue(image,ClampToQuantum(blue),q);
3894 q+=(ptrdiff_t) GetPixelChannels(image);
3896 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3898 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3903#if defined(MAGICKCORE_OPENMP_SUPPORT)
3907 proceed=SetImageProgress(image,ModulateImageTag,progress,image->rows);
3908 if (proceed == MagickFalse)
3912 image_view=DestroyCacheView(image_view);
3944MagickExport MagickBooleanType NegateImage(
Image *image,
3947#define NegateImageTag "Negate/Image"
3964 assert(image != (
Image *) NULL);
3965 assert(image->signature == MagickCoreSignature);
3966 if (IsEventLogging() != MagickFalse)
3967 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3968 if (image->storage_class == PseudoClass)
3969 for (i=0; i < (ssize_t) image->colors; i++)
3974 if (grayscale != MagickFalse)
3975 if ((image->colormap[i].red != image->colormap[i].green) ||
3976 (image->colormap[i].green != image->colormap[i].blue))
3978 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3979 image->colormap[i].red=(double) QuantumRange-image->colormap[i].red;
3980 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3981 image->colormap[i].green=(double) QuantumRange-image->colormap[i].green;
3982 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3983 image->colormap[i].blue=(double) QuantumRange-image->colormap[i].blue;
3990 image_view=AcquireAuthenticCacheView(image,exception);
3991 if( grayscale != MagickFalse )
3993 for (y=0; y < (ssize_t) image->rows; y++)
4004 if (status == MagickFalse)
4006 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4008 if (q == (Quantum *) NULL)
4013 for (x=0; x < (ssize_t) image->columns; x++)
4018 if (IsPixelGray(image,q) == MagickFalse)
4020 q+=(ptrdiff_t) GetPixelChannels(image);
4023 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4025 PixelChannel channel = GetPixelChannelChannel(image,j);
4026 PixelTrait traits = GetPixelChannelTraits(image,channel);
4027 if ((traits & UpdatePixelTrait) == 0)
4029 q[j]=QuantumRange-q[j];
4031 q+=(ptrdiff_t) GetPixelChannels(image);
4033 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4034 if (sync == MagickFalse)
4036 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4042 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4043 if (proceed == MagickFalse)
4047 image_view=DestroyCacheView(image_view);
4053#if defined(MAGICKCORE_OPENMP_SUPPORT)
4054 #pragma omp parallel for schedule(static) shared(progress,status) \
4055 magick_number_threads(image,image,image->rows,1)
4057 for (y=0; y < (ssize_t) image->rows; y++)
4065 if (status == MagickFalse)
4067 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4068 if (q == (Quantum *) NULL)
4073 for (x=0; x < (ssize_t) image->columns; x++)
4078 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4080 PixelChannel channel = GetPixelChannelChannel(image,j);
4081 PixelTrait traits = GetPixelChannelTraits(image,channel);
4082 if ((traits & UpdatePixelTrait) == 0)
4084 q[j]=QuantumRange-q[j];
4086 q+=(ptrdiff_t) GetPixelChannels(image);
4088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4090 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4095#if defined(MAGICKCORE_OPENMP_SUPPORT)
4099 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4100 if (proceed == MagickFalse)
4104 image_view=DestroyCacheView(image_view);
4134MagickExport MagickBooleanType NormalizeImage(
Image *image,
4141 black_point=0.02*image->columns*image->rows;
4142 white_point=0.99*image->columns*image->rows;
4143 return(ContrastStretchImage(image,black_point,white_point,exception));
4210#if defined(MAGICKCORE_HAVE_ATANH)
4211#define Sigmoidal(a,b,x) ( tanh((0.5*(a))*((x)-(b))) )
4213#define Sigmoidal(a,b,x) ( 1.0/(1.0+exp((a)*((b)-(x)))) )
4232#define ScaledSigmoidal(a,b,x) ( \
4233 (Sigmoidal((a),(b),(x))-Sigmoidal((a),(b),0.0)) / \
4234 (Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0)) )
4244static inline double InverseScaledSigmoidal(
const double a,
const double b,
4247 const double sig0=Sigmoidal(a,b,0.0);
4248 const double sig1=Sigmoidal(a,b,1.0);
4249 const double argument=(sig1-sig0)*x+sig0;
4250 const double clamped=
4252#if defined(MAGICKCORE_HAVE_ATANH)
4253 argument < -1+MagickEpsilon
4257 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4259 return(b+(2.0/a)*atanh(clamped));
4261 argument < MagickEpsilon
4265 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4267 return(b-log(1.0/clamped-1.0)/a);
4271MagickExport MagickBooleanType SigmoidalContrastImage(
Image *image,
4272 const MagickBooleanType sharpen,
const double contrast,
const double midpoint,
4275#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
4276#define ScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4277 ScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale*((double) x))) )
4278#define InverseScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4279 InverseScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale* \
4297 assert(image != (
Image *) NULL);
4298 assert(image->signature == MagickCoreSignature);
4299 if (IsEventLogging() != MagickFalse)
4300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4305 if (contrast < MagickEpsilon)
4310 if (image->storage_class == PseudoClass)
4315 if( sharpen != MagickFalse )
4316 for (i=0; i < (ssize_t) image->colors; i++)
4318 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4319 image->colormap[i].red=(MagickRealType) ScaledSig(
4320 image->colormap[i].red);
4321 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4322 image->colormap[i].green=(MagickRealType) ScaledSig(
4323 image->colormap[i].green);
4324 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4325 image->colormap[i].blue=(MagickRealType) ScaledSig(
4326 image->colormap[i].blue);
4327 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4328 image->colormap[i].alpha=(MagickRealType) ScaledSig(
4329 image->colormap[i].alpha);
4332 for (i=0; i < (ssize_t) image->colors; i++)
4334 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4335 image->colormap[i].red=(MagickRealType) InverseScaledSig(
4336 image->colormap[i].red);
4337 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4338 image->colormap[i].green=(MagickRealType) InverseScaledSig(
4339 image->colormap[i].green);
4340 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4341 image->colormap[i].blue=(MagickRealType) InverseScaledSig(
4342 image->colormap[i].blue);
4343 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4344 image->colormap[i].alpha=(MagickRealType) InverseScaledSig(
4345 image->colormap[i].alpha);
4353 image_view=AcquireAuthenticCacheView(image,exception);
4354#if defined(MAGICKCORE_OPENMP_SUPPORT)
4355 #pragma omp parallel for schedule(static) shared(progress,status) \
4356 magick_number_threads(image,image,image->rows,1)
4358 for (y=0; y < (ssize_t) image->rows; y++)
4366 if (status == MagickFalse)
4368 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4369 if (q == (Quantum *) NULL)
4374 for (x=0; x < (ssize_t) image->columns; x++)
4379 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4381 PixelChannel channel = GetPixelChannelChannel(image,i);
4382 PixelTrait traits = GetPixelChannelTraits(image,channel);
4383 if ((traits & UpdatePixelTrait) == 0)
4385 if( sharpen != MagickFalse )
4386 q[i]=ScaledSig(q[i]);
4388 q[i]=InverseScaledSig(q[i]);
4390 q+=(ptrdiff_t) GetPixelChannels(image);
4392 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4394 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4399#if defined(MAGICKCORE_OPENMP_SUPPORT)
4403 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress,
4405 if (proceed == MagickFalse)
4409 image_view=DestroyCacheView(image_view);
4439MagickExport MagickBooleanType WhiteBalanceImage(
Image *image,
4442#define WhiteBalanceImageTag "WhiteBalance/Image"
4466 assert(image != (
Image *) NULL);
4467 assert(image->signature == MagickCoreSignature);
4468 if (IsEventLogging() != MagickFalse)
4469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4470 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
4471 return(MagickFalse);
4472 status=TransformImageColorspace(image,LabColorspace,exception);
4475 image_view=AcquireAuthenticCacheView(image,exception);
4476 for (y=0; y < (ssize_t) image->rows; y++)
4484 if (status == MagickFalse)
4486 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4487 if (p == (Quantum *) NULL)
4492 for (x=0; x < (ssize_t) image->columns; x++)
4494 a_mean+=QuantumScale*(double) GetPixela(image,p)-0.5;
4495 b_mean+=QuantumScale*(double) GetPixelb(image,p)-0.5;
4496 p+=(ptrdiff_t) GetPixelChannels(image);
4499 a_mean/=((double) image->columns*image->rows);
4500 b_mean/=((double) image->columns*image->rows);
4502#if defined(MAGICKCORE_OPENMP_SUPPORT)
4503 #pragma omp parallel for schedule(static) shared(progress,status) \
4504 magick_number_threads(image,image,image->rows,1)
4506 for (y=0; y < (ssize_t) image->rows; y++)
4514 if (status == MagickFalse)
4516 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4517 if (q == (Quantum *) NULL)
4522 for (x=0; x < (ssize_t) image->columns; x++)
4531 a=(double) GetPixela(image,q)-1.1*(double) GetPixelL(image,q)*a_mean;
4532 b=(double) GetPixelb(image,q)-1.1*(double) GetPixelL(image,q)*b_mean;
4533 SetPixela(image,ClampToQuantum(a),q);
4534 SetPixelb(image,ClampToQuantum(b),q);
4535 q+=(ptrdiff_t) GetPixelChannels(image);
4537 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4539 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4544#if defined(MAGICKCORE_OPENMP_SUPPORT)
4548 proceed=SetImageProgress(image,WhiteBalanceImageTag,progress,image->rows);
4549 if (proceed == MagickFalse)
4553 image_view=DestroyCacheView(image_view);
4554 artifact=GetImageArtifact(image,
"white-balance:vibrance");
4555 if (artifact != (
const char *) NULL)
4572 flags=ParseGeometry(artifact,&geometry_info);
4573 if ((flags & RhoValue) != 0)
4574 black_point=geometry_info.rho;
4575 if ((flags & PercentValue) != 0)
4576 black_point*=((double) QuantumRange/100.0);
4577 channel_mask=SetImageChannelMask(image,(ChannelType) (aChannel |
4579 status&=(MagickStatusType) LevelImage(image,black_point,(
double)
4580 QuantumRange-black_point,1.0,exception);
4581 (void) SetImageChannelMask(image,channel_mask);
4583 status&=(MagickStatusType) TransformImageColorspace(image,sRGBColorspace,
4585 return(status != 0 ? MagickTrue : MagickFalse);