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)
1268 for (i=0; i < (ssize_t) image->colors; i++)
1276 luma=0.21267*image->colormap[i].red+0.71526*image->colormap[i].green+
1277 0.07217*image->colormap[i].blue;
1278 image->colormap[i].red=luma+color_correction.saturation*cdl_map[
1279 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-luma;
1280 image->colormap[i].green=luma+color_correction.saturation*cdl_map[
1281 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-luma;
1282 image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
1283 ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-luma;
1290 image_view=AcquireAuthenticCacheView(image,exception);
1291#if defined(MAGICKCORE_OPENMP_SUPPORT)
1292 #pragma omp parallel for schedule(static) shared(progress,status) \
1293 magick_number_threads(image,image,image->rows,1)
1295 for (y=0; y < (ssize_t) image->rows; y++)
1306 if (status == MagickFalse)
1308 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1309 if (q == (Quantum *) NULL)
1314 for (x=0; x < (ssize_t) image->columns; x++)
1316 luma=0.21267*(double) GetPixelRed(image,q)+0.71526*(double)
1317 GetPixelGreen(image,q)+0.07217*(double) GetPixelBlue(image,q);
1318 SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation*
1319 (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q);
1320 SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation*
1321 (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q);
1322 SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation*
1323 (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q);
1324 q+=(ptrdiff_t) GetPixelChannels(image);
1326 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1328 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1333#if defined(MAGICKCORE_OPENMP_SUPPORT)
1337 proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
1338 progress,image->rows);
1339 if (proceed == MagickFalse)
1343 image_view=DestroyCacheView(image_view);
1344 cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
1378static inline void Contrast(
const int sign,
double *red,
double *green,
1389 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
1390 brightness+=0.5*sign*(0.5*(sin((
double) (MagickPI*(brightness-0.5)))+1.0)-
1392 if (brightness > 1.0)
1395 if (brightness < 0.0)
1397 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
1400MagickExport MagickBooleanType ContrastImage(Image *image,
1401 const MagickBooleanType sharpen,ExceptionInfo *exception)
1403#define ContrastImageTag "Contrast/Image"
1423 assert(image != (Image *) NULL);
1424 assert(image->signature == MagickCoreSignature);
1425#if defined(MAGICKCORE_OPENCL_SUPPORT)
1426 if (AccelerateContrastImage(image,sharpen,exception) != MagickFalse)
1429 if (IsEventLogging() != MagickFalse)
1430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1431 sign=sharpen != MagickFalse ? 1 : -1;
1432 if (image->storage_class == PseudoClass)
1437 for (i=0; i < (ssize_t) image->colors; i++)
1444 red=(double) image->colormap[i].red;
1445 green=(double) image->colormap[i].green;
1446 blue=(double) image->colormap[i].blue;
1447 Contrast(sign,&red,&green,&blue);
1448 image->colormap[i].red=(MagickRealType) red;
1449 image->colormap[i].green=(MagickRealType) green;
1450 image->colormap[i].blue=(MagickRealType) blue;
1458 image_view=AcquireAuthenticCacheView(image,exception);
1459#if defined(MAGICKCORE_OPENMP_SUPPORT)
1460 #pragma omp parallel for schedule(static) shared(progress,status) \
1461 magick_number_threads(image,image,image->rows,1)
1463 for (y=0; y < (ssize_t) image->rows; y++)
1476 if (status == MagickFalse)
1478 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1479 if (q == (Quantum *) NULL)
1484 for (x=0; x < (ssize_t) image->columns; x++)
1486 red=(double) GetPixelRed(image,q);
1487 green=(double) GetPixelGreen(image,q);
1488 blue=(double) GetPixelBlue(image,q);
1489 Contrast(sign,&red,&green,&blue);
1490 SetPixelRed(image,ClampToQuantum(red),q);
1491 SetPixelGreen(image,ClampToQuantum(green),q);
1492 SetPixelBlue(image,ClampToQuantum(blue),q);
1493 q+=(ptrdiff_t) GetPixelChannels(image);
1495 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1497 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1502#if defined(MAGICKCORE_OPENMP_SUPPORT)
1506 proceed=SetImageProgress(image,ContrastImageTag,progress,image->rows);
1507 if (proceed == MagickFalse)
1511 image_view=DestroyCacheView(image_view);
1552MagickExport MagickBooleanType ContrastStretchImage(Image *image,
1553 const double black_point,
const double white_point,ExceptionInfo *exception)
1555#define ContrastStretchImageTag "ContrastStretch/Image"
1561 property[MagickPathExtent];
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 if (IsEventLogging() != MagickFalse)
1592 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1593 type=IdentifyImageType(image,exception);
1594 if (IsGrayImageType(type) != MagickFalse)
1595 (void) SetImageColorspace(image,GRAYColorspace,exception);
1596 black=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*black));
1597 white=(Quantum *) AcquireQuantumMemory(MaxPixelChannels,
sizeof(*white));
1598 stretch_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1599 sizeof(*stretch_map));
1600 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
1601 sizeof(*histogram));
1602 if ((black == (Quantum *) NULL) || (white == (Quantum *) NULL) ||
1603 (stretch_map == (Quantum *) NULL) || (histogram == (
double *) NULL))
1605 if (histogram != (
double *) NULL)
1606 histogram=(
double *) RelinquishMagickMemory(histogram);
1607 if (stretch_map != (Quantum *) NULL)
1608 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1609 if (white != (Quantum *) NULL)
1610 white=(Quantum *) RelinquishMagickMemory(white);
1611 if (black != (Quantum *) NULL)
1612 black=(Quantum *) RelinquishMagickMemory(black);
1613 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1620 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1621 sizeof(*histogram));
1622 image_view=AcquireVirtualCacheView(image,exception);
1623 for (y=0; y < (ssize_t) image->rows; y++)
1631 if (status == MagickFalse)
1633 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1634 if (p == (
const Quantum *) NULL)
1639 for (x=0; x < (ssize_t) image->columns; x++)
1644 pixel=GetPixelIntensity(image,p);
1645 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1647 if (image->channel_mask != AllChannels)
1648 pixel=(double) p[i];
1649 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
1650 ClampToQuantum(pixel))+(size_t) i]++;
1652 p+=(ptrdiff_t) GetPixelChannels(image);
1655 image_view=DestroyCacheView(image_view);
1659 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1667 black[i]=(Quantum) 0;
1668 white[i]=(Quantum) ScaleQuantumToMap(QuantumRange);
1670 for (j=0; j <= (ssize_t) MaxMap; j++)
1672 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1673 if (intensity > black_point)
1676 black[i]=(Quantum) j;
1678 for (j=(ssize_t) MaxMap; j != 0; j--)
1680 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
1681 if (intensity > ((
double) image->columns*image->rows-white_point))
1684 white[i]=(Quantum) j;
1686 histogram=(
double *) RelinquishMagickMemory(histogram);
1690 (void) memset(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
1691 sizeof(*stretch_map));
1692 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1697 for (j=0; j <= (ssize_t) MaxMap; j++)
1702 gamma=MagickSafeReciprocal(white[i]-black[i]);
1703 if (j < (ssize_t) black[i])
1704 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=(Quantum) 0;
1706 if (j > (ssize_t) white[i])
1707 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=QuantumRange;
1709 if (black[i] != white[i])
1710 stretch_map[(ssize_t) GetPixelChannels(image)*j+i]=
1711 ScaleMapToQuantum((
double) (MaxMap*gamma*(j-(
double) black[i])));
1714 if (image->storage_class == PseudoClass)
1722 for (j=0; j < (ssize_t) image->colors; j++)
1724 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1726 i=GetPixelChannelOffset(image,RedPixelChannel);
1727 image->colormap[j].red=(MagickRealType) stretch_map[
1728 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1729 image->colormap[j].red))+(size_t) i];
1731 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1733 i=GetPixelChannelOffset(image,GreenPixelChannel);
1734 image->colormap[j].green=(MagickRealType) stretch_map[
1735 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1736 image->colormap[j].green))+(size_t) i];
1738 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1740 i=GetPixelChannelOffset(image,BluePixelChannel);
1741 image->colormap[j].blue=(MagickRealType) stretch_map[
1742 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1743 image->colormap[j].blue))+(size_t) i];
1745 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1747 i=GetPixelChannelOffset(image,AlphaPixelChannel);
1748 image->colormap[j].alpha=(MagickRealType) stretch_map[
1749 GetPixelChannels(image)*ScaleQuantumToMap(ClampToQuantum(
1750 image->colormap[j].alpha))+(size_t) i];
1759 image_view=AcquireAuthenticCacheView(image,exception);
1760#if defined(MAGICKCORE_OPENMP_SUPPORT)
1761 #pragma omp parallel for schedule(static) shared(progress,status) \
1762 magick_number_threads(image,image,image->rows,1)
1764 for (y=0; y < (ssize_t) image->rows; y++)
1772 if (status == MagickFalse)
1774 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1775 if (q == (Quantum *) NULL)
1780 for (x=0; x < (ssize_t) image->columns; x++)
1785 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1787 PixelChannel channel = GetPixelChannelChannel(image,j);
1788 PixelTrait traits = GetPixelChannelTraits(image,channel);
1789 if ((traits & UpdatePixelTrait) == 0)
1791 if (black[j] == white[j])
1793 q[j]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
1794 ScaleQuantumToMap(q[j])+(
size_t) j]);
1796 q+=(ptrdiff_t) GetPixelChannels(image);
1798 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1800 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1805#if defined(MAGICKCORE_OPENMP_SUPPORT)
1809 proceed=SetImageProgress(image,ContrastStretchImageTag,progress,
1811 if (proceed == MagickFalse)
1815 image_view=DestroyCacheView(image_view);
1816 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*
1817 QuantumScale*GetPixelIntensity(image,black),100.0*QuantumScale*
1818 GetPixelIntensity(image,white));
1819 (void) SetImageProperty(image,
"histogram:contrast-stretch",property,
1821 white=(Quantum *) RelinquishMagickMemory(white);
1822 black=(Quantum *) RelinquishMagickMemory(black);
1823 stretch_map=(Quantum *) RelinquishMagickMemory(stretch_map);
1852MagickExport Image *EnhanceImage(
const Image *image,ExceptionInfo *exception)
1854#define EnhanceImageTag "Enhance/Image"
1855#define EnhancePixel(weight) \
1856 mean=QuantumScale*((double) GetPixelRed(image,r)+pixel.red)/2.0; \
1857 distance=QuantumScale*((double) GetPixelRed(image,r)-pixel.red); \
1858 distance_squared=(4.0+mean)*distance*distance; \
1859 mean=QuantumScale*((double) GetPixelGreen(image,r)+pixel.green)/2.0; \
1860 distance=QuantumScale*((double) GetPixelGreen(image,r)-pixel.green); \
1861 distance_squared+=(7.0-mean)*distance*distance; \
1862 mean=QuantumScale*((double) GetPixelBlue(image,r)+pixel.blue)/2.0; \
1863 distance=QuantumScale*((double) GetPixelBlue(image,r)-pixel.blue); \
1864 distance_squared+=(5.0-mean)*distance*distance; \
1865 mean=QuantumScale*((double) GetPixelBlack(image,r)+pixel.black)/2.0; \
1866 distance=QuantumScale*((double) GetPixelBlack(image,r)-pixel.black); \
1867 distance_squared+=(5.0-mean)*distance*distance; \
1868 mean=QuantumScale*((double) GetPixelAlpha(image,r)+pixel.alpha)/2.0; \
1869 distance=QuantumScale*((double) GetPixelAlpha(image,r)-pixel.alpha); \
1870 distance_squared+=(5.0-mean)*distance*distance; \
1871 if (distance_squared < 0.069) \
1873 aggregate.red+=(weight)*(double) GetPixelRed(image,r); \
1874 aggregate.green+=(weight)*(double) GetPixelGreen(image,r); \
1875 aggregate.blue+=(weight)*(double) GetPixelBlue(image,r); \
1876 aggregate.black+=(weight)*(double) GetPixelBlack(image,r); \
1877 aggregate.alpha+=(weight)*(double) GetPixelAlpha(image,r); \
1878 total_weight+=(weight); \
1880 r+=(ptrdiff_t) GetPixelChannels(image);
1901 assert(image != (
const Image *) NULL);
1902 assert(image->signature == MagickCoreSignature);
1903 if (IsEventLogging() != MagickFalse)
1904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1905 assert(exception != (ExceptionInfo *) NULL);
1906 assert(exception->signature == MagickCoreSignature);
1907 enhance_image=CloneImage(image,0,0,MagickTrue,
1909 if (enhance_image == (Image *) NULL)
1910 return((Image *) NULL);
1911 if (SetImageStorageClass(enhance_image,DirectClass,exception) == MagickFalse)
1913 enhance_image=DestroyImage(enhance_image);
1914 return((Image *) NULL);
1921 image_view=AcquireVirtualCacheView(image,exception);
1922 enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
1923#if defined(MAGICKCORE_OPENMP_SUPPORT)
1924 #pragma omp parallel for schedule(static) shared(progress,status) \
1925 magick_number_threads(image,enhance_image,image->rows,1)
1927 for (y=0; y < (ssize_t) image->rows; y++)
1944 if (status == MagickFalse)
1946 p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception);
1947 q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1,
1949 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
1954 center=(ssize_t) GetPixelChannels(image)*(2*((ssize_t) image->columns+4)+2);
1955 GetPixelInfo(image,&pixel);
1956 for (x=0; x < (ssize_t) image->columns; x++)
1970 GetPixelInfo(image,&aggregate);
1972 GetPixelInfoPixel(image,p+center,&pixel);
1974 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1975 EnhancePixel(8.0); EnhancePixel(5.0);
1976 r=p+GetPixelChannels(image)*(image->columns+4);
1977 EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0);
1978 EnhancePixel(20.0); EnhancePixel(8.0);
1979 r=p+2*GetPixelChannels(image)*(image->columns+4);
1980 EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0);
1981 EnhancePixel(40.0); EnhancePixel(10.0);
1982 r=p+3*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+4*GetPixelChannels(image)*(image->columns+4);
1986 EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
1987 EnhancePixel(8.0); EnhancePixel(5.0);
1988 if (total_weight > MagickEpsilon)
1990 pixel.red=((aggregate.red+total_weight/2.0)/total_weight);
1991 pixel.green=((aggregate.green+total_weight/2.0)/total_weight);
1992 pixel.blue=((aggregate.blue+total_weight/2.0)/total_weight);
1993 pixel.black=((aggregate.black+total_weight/2.0)/total_weight);
1994 pixel.alpha=((aggregate.alpha+total_weight/2.0)/total_weight);
1996 SetPixelViaPixelInfo(enhance_image,&pixel,q);
1997 p+=(ptrdiff_t) GetPixelChannels(image);
1998 q+=(ptrdiff_t) GetPixelChannels(enhance_image);
2000 if (SyncCacheViewAuthenticPixels(enhance_view,exception) == MagickFalse)
2002 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2007#if defined(MAGICKCORE_OPENMP_SUPPORT)
2011 proceed=SetImageProgress(image,EnhanceImageTag,progress,image->rows);
2012 if (proceed == MagickFalse)
2016 enhance_view=DestroyCacheView(enhance_view);
2017 image_view=DestroyCacheView(image_view);
2018 if (status == MagickFalse)
2019 enhance_image=DestroyImage(enhance_image);
2020 return(enhance_image);
2047MagickExport MagickBooleanType EqualizeImage(Image *image,
2048 ExceptionInfo *exception)
2050#define EqualizeImageTag "Equalize/Image"
2056 black[2*CompositePixelChannel+1],
2060 white[2*CompositePixelChannel+1];
2077 assert(image != (Image *) NULL);
2078 assert(image->signature == MagickCoreSignature);
2079#if defined(MAGICKCORE_OPENCL_SUPPORT)
2080 if (AccelerateEqualizeImage(image,exception) != MagickFalse)
2083 if (IsEventLogging() != MagickFalse)
2084 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2085 equalize_map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2086 sizeof(*equalize_map));
2087 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
2088 sizeof(*histogram));
2089 map=(
double *) AcquireQuantumMemory(MaxMap+1UL,MaxPixelChannels*
sizeof(*map));
2090 if ((equalize_map == (
double *) NULL) || (histogram == (
double *) NULL) ||
2091 (map == (
double *) NULL))
2093 if (map != (
double *) NULL)
2094 map=(
double *) RelinquishMagickMemory(map);
2095 if (histogram != (
double *) NULL)
2096 histogram=(
double *) RelinquishMagickMemory(histogram);
2097 if (equalize_map != (
double *) NULL)
2098 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2099 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2106 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
2107 sizeof(*histogram));
2108 image_view=AcquireVirtualCacheView(image,exception);
2109 for (y=0; y < (ssize_t) image->rows; y++)
2117 if (status == MagickFalse)
2119 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2120 if (p == (
const Quantum *) NULL)
2125 for (x=0; x < (ssize_t) image->columns; x++)
2127 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2132 intensity=(double) p[i];
2133 if ((image->channel_mask & SyncChannels) != 0)
2134 intensity=GetPixelIntensity(image,p);
2135 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
2136 ClampToQuantum(intensity))+(size_t) i]++;
2138 p+=(ptrdiff_t) GetPixelChannels(image);
2141 image_view=DestroyCacheView(image_view);
2145 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2154 for (j=0; j <= (ssize_t) MaxMap; j++)
2156 intensity+=histogram[(ssize_t) GetPixelChannels(image)*j+i];
2157 map[(ssize_t) GetPixelChannels(image)*j+i]=intensity;
2160 (void) memset(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
2161 sizeof(*equalize_map));
2162 (void) memset(black,0,
sizeof(*black));
2163 (void) memset(white,0,
sizeof(*white));
2164 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2170 white[i]=map[GetPixelChannels(image)*MaxMap+(size_t) i];
2171 if (black[i] != white[i])
2172 for (j=0; j <= (ssize_t) MaxMap; j++)
2173 equalize_map[GetPixelChannels(image)*(size_t) j+(
size_t) i]=(double)
2174 ScaleMapToQuantum((
double) ((MaxMap*(map[GetPixelChannels(image)*
2175 (
size_t) j+(
size_t) i]-black[i]))/(white[i]-black[i])));
2177 histogram=(
double *) RelinquishMagickMemory(histogram);
2178 map=(
double *) RelinquishMagickMemory(map);
2179 if (image->storage_class == PseudoClass)
2187 for (j=0; j < (ssize_t) image->colors; j++)
2189 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2191 PixelChannel channel = GetPixelChannelChannel(image,
2193 if (black[channel] != white[channel])
2194 image->colormap[j].red=equalize_map[(ssize_t)
2195 GetPixelChannels(image)*ScaleQuantumToMap(
2196 ClampToQuantum(image->colormap[j].red))+channel];
2198 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2200 PixelChannel channel = GetPixelChannelChannel(image,
2202 if (black[channel] != white[channel])
2203 image->colormap[j].green=equalize_map[(ssize_t)
2204 GetPixelChannels(image)*ScaleQuantumToMap(
2205 ClampToQuantum(image->colormap[j].green))+channel];
2207 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2209 PixelChannel channel = GetPixelChannelChannel(image,
2211 if (black[channel] != white[channel])
2212 image->colormap[j].blue=equalize_map[(ssize_t)
2213 GetPixelChannels(image)*ScaleQuantumToMap(
2214 ClampToQuantum(image->colormap[j].blue))+channel];
2216 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2218 PixelChannel channel = GetPixelChannelChannel(image,
2220 if (black[channel] != white[channel])
2221 image->colormap[j].alpha=equalize_map[(ssize_t)
2222 GetPixelChannels(image)*ScaleQuantumToMap(
2223 ClampToQuantum(image->colormap[j].alpha))+channel];
2231 image_view=AcquireAuthenticCacheView(image,exception);
2232#if defined(MAGICKCORE_OPENMP_SUPPORT)
2233 #pragma omp parallel for schedule(static) shared(progress,status) \
2234 magick_number_threads(image,image,image->rows,1)
2236 for (y=0; y < (ssize_t) image->rows; y++)
2244 if (status == MagickFalse)
2246 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2247 if (q == (Quantum *) NULL)
2252 for (x=0; x < (ssize_t) image->columns; x++)
2257 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2259 PixelChannel channel = GetPixelChannelChannel(image,j);
2260 PixelTrait traits = GetPixelChannelTraits(image,channel);
2261 if (((traits & UpdatePixelTrait) == 0) || (black[j] == white[j]))
2263 q[j]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
2264 ScaleQuantumToMap(q[j])+(
size_t) j]);
2266 q+=(ptrdiff_t) GetPixelChannels(image);
2268 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2270 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2275#if defined(MAGICKCORE_OPENMP_SUPPORT)
2279 proceed=SetImageProgress(image,EqualizeImageTag,progress,image->rows);
2280 if (proceed == MagickFalse)
2284 image_view=DestroyCacheView(image_view);
2285 equalize_map=(
double *) RelinquishMagickMemory(equalize_map);
2324static inline double gamma_pow(
const double value,
const double gamma)
2326 return(value < 0.0 ? value : pow(value,gamma));
2329MagickExport MagickBooleanType GammaImage(Image *image,
const double gamma,
2330 ExceptionInfo *exception)
2332#define GammaImageTag "Gamma/Image"
2355 assert(image != (Image *) NULL);
2356 assert(image->signature == MagickCoreSignature);
2357 if (IsEventLogging() != MagickFalse)
2358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2361 gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*gamma_map));
2362 if (gamma_map == (Quantum *) NULL)
2363 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2365 (void) memset(gamma_map,0,(MaxMap+1)*
sizeof(*gamma_map));
2367 for (i=0; i <= (ssize_t) MaxMap; i++)
2368 gamma_map[i]=ScaleMapToQuantum((
double) (MaxMap*pow((
double) i/
2369 MaxMap,MagickSafeReciprocal(gamma))));
2370 if (image->storage_class == PseudoClass)
2371 for (i=0; i < (ssize_t) image->colors; i++)
2376 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2377 image->colormap[i].red=(double) gamma_map[ScaleQuantumToMap(
2378 ClampToQuantum(image->colormap[i].red))];
2379 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2380 image->colormap[i].green=(double) gamma_map[ScaleQuantumToMap(
2381 ClampToQuantum(image->colormap[i].green))];
2382 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2383 image->colormap[i].blue=(double) gamma_map[ScaleQuantumToMap(
2384 ClampToQuantum(image->colormap[i].blue))];
2385 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2386 image->colormap[i].alpha=(double) gamma_map[ScaleQuantumToMap(
2387 ClampToQuantum(image->colormap[i].alpha))];
2394 image_view=AcquireAuthenticCacheView(image,exception);
2395#if defined(MAGICKCORE_OPENMP_SUPPORT)
2396 #pragma omp parallel for schedule(static) shared(progress,status) \
2397 magick_number_threads(image,image,image->rows,1)
2399 for (y=0; y < (ssize_t) image->rows; y++)
2407 if (status == MagickFalse)
2409 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2410 if (q == (Quantum *) NULL)
2415 for (x=0; x < (ssize_t) image->columns; x++)
2420 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2422 PixelChannel channel = GetPixelChannelChannel(image,j);
2423 PixelTrait traits = GetPixelChannelTraits(image,channel);
2424 if ((traits & UpdatePixelTrait) == 0)
2426 q[j]=gamma_map[ScaleQuantumToMap(ClampToQuantum((MagickRealType)
2429 q+=(ptrdiff_t) GetPixelChannels(image);
2431 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2433 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2438#if defined(MAGICKCORE_OPENMP_SUPPORT)
2442 proceed=SetImageProgress(image,GammaImageTag,progress,image->rows);
2443 if (proceed == MagickFalse)
2447 image_view=DestroyCacheView(image_view);
2448 gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map);
2449 if (image->gamma != 0.0)
2450 image->gamma*=gamma;
2481MagickExport MagickBooleanType GrayscaleImage(Image *image,
2482 const PixelIntensityMethod method,ExceptionInfo *exception)
2484#define GrayscaleImageTag "Grayscale/Image"
2498 assert(image != (Image *) NULL);
2499 assert(image->signature == MagickCoreSignature);
2500 if (IsEventLogging() != MagickFalse)
2501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2502 if (image->storage_class == PseudoClass)
2504 if (SyncImage(image,exception) == MagickFalse)
2505 return(MagickFalse);
2506 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2507 return(MagickFalse);
2509#if defined(MAGICKCORE_OPENCL_SUPPORT)
2510 if (AccelerateGrayscaleImage(image,method,exception) != MagickFalse)
2512 image->intensity=method;
2513 image->type=GrayscaleType;
2514 if ((method == Rec601LuminancePixelIntensityMethod) ||
2515 (method == Rec709LuminancePixelIntensityMethod))
2516 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2517 return(SetImageColorspace(image,GRAYColorspace,exception));
2525 image_view=AcquireAuthenticCacheView(image,exception);
2526#if defined(MAGICKCORE_OPENMP_SUPPORT)
2527 #pragma omp parallel for schedule(static) shared(progress,status) \
2528 magick_number_threads(image,image,image->rows,1)
2530 for (y=0; y < (ssize_t) image->rows; y++)
2538 if (status == MagickFalse)
2540 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2541 if (q == (Quantum *) NULL)
2546 for (x=0; x < (ssize_t) image->columns; x++)
2554 red=(MagickRealType) GetPixelRed(image,q);
2555 green=(MagickRealType) GetPixelGreen(image,q);
2556 blue=(MagickRealType) GetPixelBlue(image,q);
2560 case AveragePixelIntensityMethod:
2562 intensity=(red+green+blue)/3.0;
2565 case BrightnessPixelIntensityMethod:
2567 intensity=MagickMax(MagickMax(red,green),blue);
2570 case LightnessPixelIntensityMethod:
2572 intensity=(MagickMin(MagickMin(red,green),blue)+
2573 MagickMax(MagickMax(red,green),blue))/2.0;
2576 case MSPixelIntensityMethod:
2578 intensity=(MagickRealType) (((
double) red*red+green*green+
2582 case Rec601LumaPixelIntensityMethod:
2584 if (image->colorspace == RGBColorspace)
2586 red=EncodePixelGamma(red);
2587 green=EncodePixelGamma(green);
2588 blue=EncodePixelGamma(blue);
2590 intensity=0.298839*red+0.586811*green+0.114350*blue;
2593 case Rec601LuminancePixelIntensityMethod:
2595 if (image->colorspace == sRGBColorspace)
2597 red=DecodePixelGamma(red);
2598 green=DecodePixelGamma(green);
2599 blue=DecodePixelGamma(blue);
2601 intensity=0.298839*red+0.586811*green+0.114350*blue;
2604 case Rec709LumaPixelIntensityMethod:
2607 if (image->colorspace == RGBColorspace)
2609 red=EncodePixelGamma(red);
2610 green=EncodePixelGamma(green);
2611 blue=EncodePixelGamma(blue);
2613 intensity=0.212656*red+0.715158*green+0.072186*blue;
2616 case Rec709LuminancePixelIntensityMethod:
2618 if (image->colorspace == sRGBColorspace)
2620 red=DecodePixelGamma(red);
2621 green=DecodePixelGamma(green);
2622 blue=DecodePixelGamma(blue);
2624 intensity=0.212656*red+0.715158*green+0.072186*blue;
2627 case RMSPixelIntensityMethod:
2629 intensity=(MagickRealType) (sqrt((
double) red*red+green*green+
2630 blue*blue)/sqrt(3.0));
2634 SetPixelGray(image,ClampToQuantum(intensity),q);
2635 q+=(ptrdiff_t) GetPixelChannels(image);
2637 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2639 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2644#if defined(MAGICKCORE_OPENMP_SUPPORT)
2648 proceed=SetImageProgress(image,GrayscaleImageTag,progress,image->rows);
2649 if (proceed == MagickFalse)
2653 image_view=DestroyCacheView(image_view);
2654 image->intensity=method;
2655 image->type=GrayscaleType;
2656 if ((method == Rec601LuminancePixelIntensityMethod) ||
2657 (method == Rec709LuminancePixelIntensityMethod))
2658 return(SetImageColorspace(image,LinearGRAYColorspace,exception));
2659 return(SetImageColorspace(image,GRAYColorspace,exception));
2693MagickExport MagickBooleanType HaldClutImage(Image *image,
2694 const Image *hald_image,ExceptionInfo *exception)
2696#define HaldClutImageTag "Clut/Image"
2698 typedef struct _HaldInfo
2730 assert(image != (Image *) NULL);
2731 assert(image->signature == MagickCoreSignature);
2732 if (IsEventLogging() != MagickFalse)
2733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2734 assert(hald_image != (Image *) NULL);
2735 assert(hald_image->signature == MagickCoreSignature);
2736 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2737 return(MagickFalse);
2738 if ((image->alpha_trait & BlendPixelTrait) == 0)
2739 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2740 if (image->colorspace != hald_image->colorspace)
2741 (void) SetImageColorspace(image,hald_image->colorspace,exception);
2747 length=(size_t) MagickMin((MagickRealType) hald_image->columns,
2748 (MagickRealType) hald_image->rows);
2749 for (level=2; (level*level*level) < length; level++) ;
2751 cube_size=level*level;
2752 width=(double) hald_image->columns;
2753 GetPixelInfo(hald_image,&zero);
2754 hald_view=AcquireVirtualCacheView(hald_image,exception);
2755 image_view=AcquireAuthenticCacheView(image,exception);
2756#if defined(MAGICKCORE_OPENMP_SUPPORT)
2757 #pragma omp parallel for schedule(static) shared(progress,status) \
2758 magick_number_threads(image,image,image->rows,1)
2760 for (y=0; y < (ssize_t) image->rows; y++)
2768 if (status == MagickFalse)
2770 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2771 if (q == (Quantum *) NULL)
2776 for (x=0; x < (ssize_t) image->columns; x++)
2783 point = { 0, 0, 0 };
2792 point.x=QuantumScale*(level-1.0)*(
double) GetPixelRed(image,q);
2793 point.y=QuantumScale*(level-1.0)*(
double) GetPixelGreen(image,q);
2794 point.z=QuantumScale*(level-1.0)*(
double) GetPixelBlue(image,q);
2795 offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
2796 point.x-=floor(point.x);
2797 point.y-=floor(point.y);
2798 point.z-=floor(point.z);
2799 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2800 fmod(offset,width),floor(offset/width),&pixel1,exception);
2801 if (status == MagickFalse)
2803 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2804 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2805 if (status == MagickFalse)
2808 if (hald_image->interpolate == NearestInterpolatePixel)
2809 area=(point.y < 0.5) ? 0.0 : 1.0;
2810 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2813 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2814 fmod(offset,width),floor(offset/width),&pixel1,exception);
2815 if (status == MagickFalse)
2817 status=InterpolatePixelInfo(hald_image,hald_view,hald_image->interpolate,
2818 fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
2819 if (status == MagickFalse)
2821 CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
2824 if (hald_image->interpolate == NearestInterpolatePixel)
2825 area=(point.z < 0.5)? 0.0 : 1.0;
2826 CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
2828 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2829 SetPixelRed(image,ClampToQuantum(pixel.red),q);
2830 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2831 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
2832 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2833 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
2834 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2835 (image->colorspace == CMYKColorspace))
2836 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
2837 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
2838 (image->alpha_trait != UndefinedPixelTrait))
2839 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
2840 q+=(ptrdiff_t) GetPixelChannels(image);
2842 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2844 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2849#if defined(MAGICKCORE_OPENMP_SUPPORT)
2853 proceed=SetImageProgress(image,HaldClutImageTag,progress,image->rows);
2854 if (proceed == MagickFalse)
2858 hald_view=DestroyCacheView(hald_view);
2859 image_view=DestroyCacheView(image_view);
2907static inline double LevelPixel(
const double black_point,
2908 const double white_point,
const double gamma,
const double pixel)
2914 scale=MagickSafeReciprocal(white_point-black_point);
2915 level_pixel=(double) QuantumRange*gamma_pow(scale*((
double) pixel-(
double)
2916 black_point),MagickSafeReciprocal(gamma));
2917 return(level_pixel);
2920MagickExport MagickBooleanType LevelImage(Image *image,
const double black_point,
2921 const double white_point,
const double gamma,ExceptionInfo *exception)
2923#define LevelImageTag "Level/Image"
2941 assert(image != (Image *) NULL);
2942 assert(image->signature == MagickCoreSignature);
2943 if (IsEventLogging() != MagickFalse)
2944 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2945 if (image->storage_class == PseudoClass)
2946 for (i=0; i < (ssize_t) image->colors; i++)
2951 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2952 image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
2953 white_point,gamma,image->colormap[i].red));
2954 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2955 image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
2956 white_point,gamma,image->colormap[i].green));
2957 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2958 image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
2959 white_point,gamma,image->colormap[i].blue));
2960 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2961 image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
2962 white_point,gamma,image->colormap[i].alpha));
2969 image_view=AcquireAuthenticCacheView(image,exception);
2970#if defined(MAGICKCORE_OPENMP_SUPPORT)
2971 #pragma omp parallel for schedule(static) shared(progress,status) \
2972 magick_number_threads(image,image,image->rows,1)
2974 for (y=0; y < (ssize_t) image->rows; y++)
2982 if (status == MagickFalse)
2984 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2985 if (q == (Quantum *) NULL)
2990 for (x=0; x < (ssize_t) image->columns; x++)
2995 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2997 PixelChannel channel = GetPixelChannelChannel(image,j);
2998 PixelTrait traits = GetPixelChannelTraits(image,channel);
2999 if ((traits & UpdatePixelTrait) == 0)
3001 q[j]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
3004 q+=(ptrdiff_t) GetPixelChannels(image);
3006 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3008 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3013#if defined(MAGICKCORE_OPENMP_SUPPORT)
3017 proceed=SetImageProgress(image,LevelImageTag,progress,image->rows);
3018 if (proceed == MagickFalse)
3022 image_view=DestroyCacheView(image_view);
3023 (void) ClampImage(image,exception);
3069MagickExport MagickBooleanType LevelizeImage(Image *image,
3070 const double black_point,
const double white_point,
const double gamma,
3071 ExceptionInfo *exception)
3073#define LevelizeImageTag "Levelize/Image"
3074#define LevelizeValue(x) ClampToQuantum(((MagickRealType) gamma_pow((double) \
3075 (QuantumScale*((double) x)),gamma))*(white_point-black_point)+black_point)
3095 assert(image != (Image *) NULL);
3096 assert(image->signature == MagickCoreSignature);
3097 if (IsEventLogging() != MagickFalse)
3098 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3099 if (image->storage_class == PseudoClass)
3100 for (i=0; i < (ssize_t) image->colors; i++)
3105 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3106 image->colormap[i].red=(double) LevelizeValue(image->colormap[i].red);
3107 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3108 image->colormap[i].green=(double) LevelizeValue(
3109 image->colormap[i].green);
3110 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3111 image->colormap[i].blue=(double) LevelizeValue(image->colormap[i].blue);
3112 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3113 image->colormap[i].alpha=(double) LevelizeValue(
3114 image->colormap[i].alpha);
3121 image_view=AcquireAuthenticCacheView(image,exception);
3122#if defined(MAGICKCORE_OPENMP_SUPPORT)
3123 #pragma omp parallel for schedule(static) shared(progress,status) \
3124 magick_number_threads(image,image,image->rows,1)
3126 for (y=0; y < (ssize_t) image->rows; y++)
3134 if (status == MagickFalse)
3136 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3137 if (q == (Quantum *) NULL)
3142 for (x=0; x < (ssize_t) image->columns; x++)
3147 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
3149 PixelChannel channel = GetPixelChannelChannel(image,j);
3150 PixelTrait traits = GetPixelChannelTraits(image,channel);
3151 if ((traits & UpdatePixelTrait) == 0)
3153 q[j]=LevelizeValue(q[j]);
3155 q+=(ptrdiff_t) GetPixelChannels(image);
3157 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3159 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3164#if defined(MAGICKCORE_OPENMP_SUPPORT)
3168 proceed=SetImageProgress(image,LevelizeImageTag,progress,image->rows);
3169 if (proceed == MagickFalse)
3173 image_view=DestroyCacheView(image_view);
3218MagickExport MagickBooleanType LevelImageColors(Image *image,
3219 const PixelInfo *black_color,
const PixelInfo *white_color,
3220 const MagickBooleanType invert,ExceptionInfo *exception)
3231 assert(image != (Image *) NULL);
3232 assert(image->signature == MagickCoreSignature);
3233 if (IsEventLogging() != MagickFalse)
3234 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3235 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
3236 ((IsGrayColorspace(black_color->colorspace) == MagickFalse) ||
3237 (IsGrayColorspace(white_color->colorspace) == MagickFalse)))
3238 (void) SetImageColorspace(image,sRGBColorspace,exception);
3240 if (invert == MagickFalse)
3242 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3244 channel_mask=SetImageChannelMask(image,RedChannel);
3245 status&=(MagickStatusType) LevelImage(image,black_color->red,
3246 white_color->red,1.0,exception);
3247 (void) SetImageChannelMask(image,channel_mask);
3249 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3251 channel_mask=SetImageChannelMask(image,GreenChannel);
3252 status&=(MagickStatusType) LevelImage(image,black_color->green,
3253 white_color->green,1.0,exception);
3254 (void) SetImageChannelMask(image,channel_mask);
3256 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3258 channel_mask=SetImageChannelMask(image,BlueChannel);
3259 status&=(MagickStatusType) LevelImage(image,black_color->blue,
3260 white_color->blue,1.0,exception);
3261 (void) SetImageChannelMask(image,channel_mask);
3263 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3264 (image->colorspace == CMYKColorspace))
3266 channel_mask=SetImageChannelMask(image,BlackChannel);
3267 status&=(MagickStatusType) LevelImage(image,black_color->black,
3268 white_color->black,1.0,exception);
3269 (void) SetImageChannelMask(image,channel_mask);
3271 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3272 (image->alpha_trait != UndefinedPixelTrait))
3274 channel_mask=SetImageChannelMask(image,AlphaChannel);
3275 status&=(MagickStatusType) LevelImage(image,black_color->alpha,
3276 white_color->alpha,1.0,exception);
3277 (void) SetImageChannelMask(image,channel_mask);
3282 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3284 channel_mask=SetImageChannelMask(image,RedChannel);
3285 status&=(MagickStatusType) LevelizeImage(image,black_color->red,
3286 white_color->red,1.0,exception);
3287 (void) SetImageChannelMask(image,channel_mask);
3289 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3291 channel_mask=SetImageChannelMask(image,GreenChannel);
3292 status&=(MagickStatusType) LevelizeImage(image,black_color->green,
3293 white_color->green,1.0,exception);
3294 (void) SetImageChannelMask(image,channel_mask);
3296 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3298 channel_mask=SetImageChannelMask(image,BlueChannel);
3299 status&=(MagickStatusType) LevelizeImage(image,black_color->blue,
3300 white_color->blue,1.0,exception);
3301 (void) SetImageChannelMask(image,channel_mask);
3303 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3304 (image->colorspace == CMYKColorspace))
3306 channel_mask=SetImageChannelMask(image,BlackChannel);
3307 status&=(MagickStatusType) LevelizeImage(image,black_color->black,
3308 white_color->black,1.0,exception);
3309 (void) SetImageChannelMask(image,channel_mask);
3311 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3312 (image->alpha_trait != UndefinedPixelTrait))
3314 channel_mask=SetImageChannelMask(image,AlphaChannel);
3315 status&=(MagickStatusType) LevelizeImage(image,black_color->alpha,
3316 white_color->alpha,1.0,exception);
3317 (void) SetImageChannelMask(image,channel_mask);
3320 return(status != 0 ? MagickTrue : MagickFalse);
3354MagickExport MagickBooleanType LinearStretchImage(Image *image,
3355 const double black_point,
const double white_point,ExceptionInfo *exception)
3357#define LinearStretchImageTag "LinearStretch/Image"
3363 property[MagickPathExtent];
3380 assert(image != (Image *) NULL);
3381 assert(image->signature == MagickCoreSignature);
3382 histogram=(
double *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*histogram));
3383 if (histogram == (
double *) NULL)
3384 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
3389 (void) memset(histogram,0,(MaxMap+1)*
sizeof(*histogram));
3390 image_view=AcquireVirtualCacheView(image,exception);
3391 for (y=0; y < (ssize_t) image->rows; y++)
3399 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3400 if (p == (
const Quantum *) NULL)
3402 for (x=0; x < (ssize_t) image->columns; x++)
3404 intensity=GetPixelIntensity(image,p);
3405 histogram[ScaleQuantumToMap(ClampToQuantum(intensity))]++;
3406 p+=(ptrdiff_t) GetPixelChannels(image);
3409 image_view=DestroyCacheView(image_view);
3414 for (black=0; black < (ssize_t) MaxMap; black++)
3416 intensity+=histogram[black];
3417 if (intensity >= black_point)
3421 for (white=(ssize_t) MaxMap; white != 0; white--)
3423 intensity+=histogram[white];
3424 if (intensity >= white_point)
3427 histogram=(
double *) RelinquishMagickMemory(histogram);
3428 status=LevelImage(image,(
double) ScaleMapToQuantum((MagickRealType) black),
3429 (
double) ScaleMapToQuantum((MagickRealType) white),1.0,exception);
3430 (void) FormatLocaleString(property,MagickPathExtent,
"%gx%g%%",100.0*black/
3431 MaxMap,100.0*white/MaxMap);
3432 (void) SetImageProperty(image,
"histogram:linear-stretch",property,exception);
3468static inline void ModulateHCL(
const double percent_hue,
3469 const double percent_chroma,
const double percent_luma,
double *red,
3470 double *green,
double *blue)
3480 ConvertRGBToHCL(*red,*green,*blue,&hue,&chroma,&luma);
3481 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3482 chroma*=0.01*percent_chroma;
3483 luma*=0.01*percent_luma;
3484 ConvertHCLToRGB(hue,chroma,luma,red,green,blue);
3487static inline void ModulateHCLp(
const double percent_hue,
3488 const double percent_chroma,
const double percent_luma,
double *red,
3489 double *green,
double *blue)
3499 ConvertRGBToHCLp(*red,*green,*blue,&hue,&chroma,&luma);
3500 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3501 chroma*=0.01*percent_chroma;
3502 luma*=0.01*percent_luma;
3503 ConvertHCLpToRGB(hue,chroma,luma,red,green,blue);
3506static inline void ModulateHSB(
const double percent_hue,
3507 const double percent_saturation,
const double percent_brightness,
double *red,
3508 double *green,
double *blue)
3518 ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
3519 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3520 saturation*=0.01*percent_saturation;
3521 brightness*=0.01*percent_brightness;
3522 ConvertHSBToRGB(hue,saturation,brightness,red,green,blue);
3525static inline void ModulateHSI(
const double percent_hue,
3526 const double percent_saturation,
const double percent_intensity,
double *red,
3527 double *green,
double *blue)
3537 ConvertRGBToHSI(*red,*green,*blue,&hue,&saturation,&intensity);
3538 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3539 saturation*=0.01*percent_saturation;
3540 intensity*=0.01*percent_intensity;
3541 ConvertHSIToRGB(hue,saturation,intensity,red,green,blue);
3544static inline void ModulateHSL(
const double percent_hue,
3545 const double percent_saturation,
const double percent_lightness,
double *red,
3546 double *green,
double *blue)
3556 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
3557 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3558 saturation*=0.01*percent_saturation;
3559 lightness*=0.01*percent_lightness;
3560 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
3563static inline void ModulateHSV(
const double percent_hue,
3564 const double percent_saturation,
const double percent_value,
double *red,
3565 double *green,
double *blue)
3575 ConvertRGBToHSV(*red,*green,*blue,&hue,&saturation,&value);
3576 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3577 saturation*=0.01*percent_saturation;
3578 value*=0.01*percent_value;
3579 ConvertHSVToRGB(hue,saturation,value,red,green,blue);
3582static inline void ModulateHWB(
const double percent_hue,
3583 const double percent_whiteness,
const double percent_blackness,
double *red,
3584 double *green,
double *blue)
3594 ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
3595 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3596 blackness*=0.01*percent_blackness;
3597 whiteness*=0.01*percent_whiteness;
3598 ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
3601static inline void ModulateLCHab(
const double percent_luma,
3602 const double percent_chroma,
const double percent_hue,
3603 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3613 ConvertRGBToLCHab(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3614 luma*=0.01*percent_luma;
3615 chroma*=0.01*percent_chroma;
3616 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3617 ConvertLCHabToRGB(luma,chroma,hue,illuminant,red,green,blue);
3620static inline void ModulateLCHuv(
const double percent_luma,
3621 const double percent_chroma,
const double percent_hue,
3622 const IlluminantType illuminant,
double *red,
double *green,
double *blue)
3632 ConvertRGBToLCHuv(*red,*green,*blue,illuminant,&luma,&chroma,&hue);
3633 luma*=0.01*percent_luma;
3634 chroma*=0.01*percent_chroma;
3635 hue+=fmod((percent_hue-100.0),200.0)/200.0;
3636 ConvertLCHuvToRGB(luma,chroma,hue,illuminant,red,green,blue);
3639MagickExport MagickBooleanType ModulateImage(Image *image,
const char *modulate,
3640 ExceptionInfo *exception)
3642#define ModulateImageTag "Modulate/Image"
3648 colorspace = UndefinedColorspace;
3654 percent_brightness = 100.0,
3655 percent_hue = 100.0,
3656 percent_saturation = 100.0;
3662 illuminant = D65Illuminant;
3682 assert(image != (Image *) NULL);
3683 assert(image->signature == MagickCoreSignature);
3684 if (IsEventLogging() != MagickFalse)
3685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3686 if (modulate == (
char *) NULL)
3687 return(MagickFalse);
3688 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
3689 (void) SetImageColorspace(image,sRGBColorspace,exception);
3690 flags=ParseGeometry(modulate,&geometry_info);
3691 if ((flags & RhoValue) != 0)
3692 percent_brightness=geometry_info.rho;
3693 if ((flags & SigmaValue) != 0)
3694 percent_saturation=geometry_info.sigma;
3695 if ((flags & XiValue) != 0)
3696 percent_hue=geometry_info.xi;
3697 artifact=GetImageArtifact(image,
"modulate:colorspace");
3698 if (artifact != (
const char *) NULL)
3699 colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
3700 MagickFalse,artifact);
3701 artifact=GetImageArtifact(image,
"color:illuminant");
3702 if (artifact != (
const char *) NULL)
3707 illuminant_type=ParseCommandOption(MagickIlluminantOptions,MagickFalse,
3709 if (illuminant_type < 0)
3711 illuminant=UndefinedIlluminant;
3712 colorspace=UndefinedColorspace;
3715 illuminant=(IlluminantType) illuminant_type;
3717 if (image->storage_class == PseudoClass)
3718 for (i=0; i < (ssize_t) image->colors; i++)
3728 red=(double) image->colormap[i].red;
3729 green=(double) image->colormap[i].green;
3730 blue=(double) image->colormap[i].blue;
3735 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3739 case HCLpColorspace:
3741 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3747 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3753 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3760 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3766 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3772 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3777 case LCHabColorspace:
3779 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3780 illuminant,&red,&green,&blue);
3783 case LCHuvColorspace:
3785 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3786 illuminant,&red,&green,&blue);
3790 image->colormap[i].red=red;
3791 image->colormap[i].green=green;
3792 image->colormap[i].blue=blue;
3797#if defined(MAGICKCORE_OPENCL_SUPPORT)
3798 if (AccelerateModulateImage(image,percent_brightness,percent_hue,
3799 percent_saturation,colorspace,exception) != MagickFalse)
3804 image_view=AcquireAuthenticCacheView(image,exception);
3805#if defined(MAGICKCORE_OPENMP_SUPPORT)
3806 #pragma omp parallel for schedule(static) shared(progress,status) \
3807 magick_number_threads(image,image,image->rows,1)
3809 for (y=0; y < (ssize_t) image->rows; y++)
3817 if (status == MagickFalse)
3819 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3820 if (q == (Quantum *) NULL)
3825 for (x=0; x < (ssize_t) image->columns; x++)
3832 red=(double) GetPixelRed(image,q);
3833 green=(double) GetPixelGreen(image,q);
3834 blue=(double) GetPixelBlue(image,q);
3839 ModulateHCL(percent_hue,percent_saturation,percent_brightness,
3843 case HCLpColorspace:
3845 ModulateHCLp(percent_hue,percent_saturation,percent_brightness,
3851 ModulateHSB(percent_hue,percent_saturation,percent_brightness,
3857 ModulateHSI(percent_hue,percent_saturation,percent_brightness,
3864 ModulateHSL(percent_hue,percent_saturation,percent_brightness,
3870 ModulateHSV(percent_hue,percent_saturation,percent_brightness,
3876 ModulateHWB(percent_hue,percent_saturation,percent_brightness,
3881 case LCHabColorspace:
3883 ModulateLCHab(percent_brightness,percent_saturation,percent_hue,
3884 illuminant,&red,&green,&blue);
3887 case LCHuvColorspace:
3889 ModulateLCHuv(percent_brightness,percent_saturation,percent_hue,
3890 illuminant,&red,&green,&blue);
3894 SetPixelRed(image,ClampToQuantum(red),q);
3895 SetPixelGreen(image,ClampToQuantum(green),q);
3896 SetPixelBlue(image,ClampToQuantum(blue),q);
3897 q+=(ptrdiff_t) GetPixelChannels(image);
3899 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3901 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3906#if defined(MAGICKCORE_OPENMP_SUPPORT)
3910 proceed=SetImageProgress(image,ModulateImageTag,progress,image->rows);
3911 if (proceed == MagickFalse)
3915 image_view=DestroyCacheView(image_view);
3947MagickExport MagickBooleanType NegateImage(Image *image,
3948 const MagickBooleanType grayscale,ExceptionInfo *exception)
3950#define NegateImageTag "Negate/Image"
3967 assert(image != (Image *) NULL);
3968 assert(image->signature == MagickCoreSignature);
3969 if (IsEventLogging() != MagickFalse)
3970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3971 if (image->storage_class == PseudoClass)
3972 for (i=0; i < (ssize_t) image->colors; i++)
3977 if (grayscale != MagickFalse)
3978 if ((image->colormap[i].red != image->colormap[i].green) ||
3979 (image->colormap[i].green != image->colormap[i].blue))
3981 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3982 image->colormap[i].red=(double) QuantumRange-image->colormap[i].red;
3983 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3984 image->colormap[i].green=(double) QuantumRange-image->colormap[i].green;
3985 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3986 image->colormap[i].blue=(double) QuantumRange-image->colormap[i].blue;
3993 image_view=AcquireAuthenticCacheView(image,exception);
3994 if( grayscale != MagickFalse )
3996 for (y=0; y < (ssize_t) image->rows; y++)
4007 if (status == MagickFalse)
4009 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4011 if (q == (Quantum *) NULL)
4016 for (x=0; x < (ssize_t) image->columns; x++)
4021 if (IsPixelGray(image,q) == MagickFalse)
4023 q+=(ptrdiff_t) GetPixelChannels(image);
4026 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4028 PixelChannel channel = GetPixelChannelChannel(image,j);
4029 PixelTrait traits = GetPixelChannelTraits(image,channel);
4030 if ((traits & UpdatePixelTrait) == 0)
4032 q[j]=QuantumRange-q[j];
4034 q+=(ptrdiff_t) GetPixelChannels(image);
4036 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4037 if (sync == MagickFalse)
4039 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4045 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4046 if (proceed == MagickFalse)
4050 image_view=DestroyCacheView(image_view);
4056#if defined(MAGICKCORE_OPENMP_SUPPORT)
4057 #pragma omp parallel for schedule(static) shared(progress,status) \
4058 magick_number_threads(image,image,image->rows,1)
4060 for (y=0; y < (ssize_t) image->rows; y++)
4068 if (status == MagickFalse)
4070 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4071 if (q == (Quantum *) NULL)
4076 for (x=0; x < (ssize_t) image->columns; x++)
4081 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
4083 PixelChannel channel = GetPixelChannelChannel(image,j);
4084 PixelTrait traits = GetPixelChannelTraits(image,channel);
4085 if ((traits & UpdatePixelTrait) == 0)
4087 q[j]=QuantumRange-q[j];
4089 q+=(ptrdiff_t) GetPixelChannels(image);
4091 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4093 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4098#if defined(MAGICKCORE_OPENMP_SUPPORT)
4102 proceed=SetImageProgress(image,NegateImageTag,progress,image->rows);
4103 if (proceed == MagickFalse)
4107 image_view=DestroyCacheView(image_view);
4137MagickExport MagickBooleanType NormalizeImage(Image *image,
4138 ExceptionInfo *exception)
4144 black_point=0.02*image->columns*image->rows;
4145 white_point=0.99*image->columns*image->rows;
4146 return(ContrastStretchImage(image,black_point,white_point,exception));
4213#if defined(MAGICKCORE_HAVE_ATANH)
4214#define Sigmoidal(a,b,x) ( tanh((0.5*(a))*((x)-(b))) )
4216#define Sigmoidal(a,b,x) ( 1.0/(1.0+exp((a)*((b)-(x)))) )
4235#define ScaledSigmoidal(a,b,x) ( \
4236 (Sigmoidal((a),(b),(x))-Sigmoidal((a),(b),0.0)) / \
4237 (Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0)) )
4247static inline double InverseScaledSigmoidal(
const double a,
const double b,
4250 const double sig0=Sigmoidal(a,b,0.0);
4251 const double sig1=Sigmoidal(a,b,1.0);
4252 const double argument=(sig1-sig0)*x+sig0;
4253 const double clamped=
4255#if defined(MAGICKCORE_HAVE_ATANH)
4256 argument < -1+MagickEpsilon
4260 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4262 return(b+(2.0/a)*atanh(clamped));
4264 argument < MagickEpsilon
4268 ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument )
4270 return(b-log(1.0/clamped-1.0)/a);
4274MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
4275 const MagickBooleanType sharpen,
const double contrast,
const double midpoint,
4276 ExceptionInfo *exception)
4278#define SigmoidalContrastImageTag "SigmoidalContrast/Image"
4279#define ScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4280 ScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale*((double) x))) )
4281#define InverseScaledSig(x) (ClampToQuantum((double) QuantumRange* \
4282 InverseScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale* \
4300 assert(image != (Image *) NULL);
4301 assert(image->signature == MagickCoreSignature);
4302 if (IsEventLogging() != MagickFalse)
4303 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4308 if (contrast < MagickEpsilon)
4313 if (image->storage_class == PseudoClass)
4318 if( sharpen != MagickFalse )
4319 for (i=0; i < (ssize_t) image->colors; i++)
4321 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4322 image->colormap[i].red=(MagickRealType) ScaledSig(
4323 image->colormap[i].red);
4324 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4325 image->colormap[i].green=(MagickRealType) ScaledSig(
4326 image->colormap[i].green);
4327 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4328 image->colormap[i].blue=(MagickRealType) ScaledSig(
4329 image->colormap[i].blue);
4330 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4331 image->colormap[i].alpha=(MagickRealType) ScaledSig(
4332 image->colormap[i].alpha);
4335 for (i=0; i < (ssize_t) image->colors; i++)
4337 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4338 image->colormap[i].red=(MagickRealType) InverseScaledSig(
4339 image->colormap[i].red);
4340 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4341 image->colormap[i].green=(MagickRealType) InverseScaledSig(
4342 image->colormap[i].green);
4343 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4344 image->colormap[i].blue=(MagickRealType) InverseScaledSig(
4345 image->colormap[i].blue);
4346 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4347 image->colormap[i].alpha=(MagickRealType) InverseScaledSig(
4348 image->colormap[i].alpha);
4356 image_view=AcquireAuthenticCacheView(image,exception);
4357#if defined(MAGICKCORE_OPENMP_SUPPORT)
4358 #pragma omp parallel for schedule(static) shared(progress,status) \
4359 magick_number_threads(image,image,image->rows,1)
4361 for (y=0; y < (ssize_t) image->rows; y++)
4369 if (status == MagickFalse)
4371 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4372 if (q == (Quantum *) NULL)
4377 for (x=0; x < (ssize_t) image->columns; x++)
4382 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4384 PixelChannel channel = GetPixelChannelChannel(image,i);
4385 PixelTrait traits = GetPixelChannelTraits(image,channel);
4386 if ((traits & UpdatePixelTrait) == 0)
4388 if( sharpen != MagickFalse )
4389 q[i]=ScaledSig(q[i]);
4391 q[i]=InverseScaledSig(q[i]);
4393 q+=(ptrdiff_t) GetPixelChannels(image);
4395 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4397 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4402#if defined(MAGICKCORE_OPENMP_SUPPORT)
4406 proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress,
4408 if (proceed == MagickFalse)
4412 image_view=DestroyCacheView(image_view);
4442MagickExport MagickBooleanType WhiteBalanceImage(Image *image,
4443 ExceptionInfo *exception)
4445#define WhiteBalanceImageTag "WhiteBalance/Image"
4469 assert(image != (Image *) NULL);
4470 assert(image->signature == MagickCoreSignature);
4471 if (IsEventLogging() != MagickFalse)
4472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4473 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
4474 return(MagickFalse);
4475 status=TransformImageColorspace(image,LabColorspace,exception);
4478 image_view=AcquireAuthenticCacheView(image,exception);
4479 for (y=0; y < (ssize_t) image->rows; y++)
4487 if (status == MagickFalse)
4489 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4490 if (p == (Quantum *) NULL)
4495 for (x=0; x < (ssize_t) image->columns; x++)
4497 a_mean+=QuantumScale*(double) GetPixela(image,p)-0.5;
4498 b_mean+=QuantumScale*(double) GetPixelb(image,p)-0.5;
4499 p+=(ptrdiff_t) GetPixelChannels(image);
4502 a_mean/=((double) image->columns*image->rows);
4503 b_mean/=((double) image->columns*image->rows);
4505#if defined(MAGICKCORE_OPENMP_SUPPORT)
4506 #pragma omp parallel for schedule(static) shared(progress,status) \
4507 magick_number_threads(image,image,image->rows,1)
4509 for (y=0; y < (ssize_t) image->rows; y++)
4517 if (status == MagickFalse)
4519 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4520 if (q == (Quantum *) NULL)
4525 for (x=0; x < (ssize_t) image->columns; x++)
4534 a=(double) GetPixela(image,q)-1.1*(double) GetPixelL(image,q)*a_mean;
4535 b=(double) GetPixelb(image,q)-1.1*(double) GetPixelL(image,q)*b_mean;
4536 SetPixela(image,ClampToQuantum(a),q);
4537 SetPixelb(image,ClampToQuantum(b),q);
4538 q+=(ptrdiff_t) GetPixelChannels(image);
4540 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4542 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4547#if defined(MAGICKCORE_OPENMP_SUPPORT)
4551 proceed=SetImageProgress(image,WhiteBalanceImageTag,progress,image->rows);
4552 if (proceed == MagickFalse)
4556 image_view=DestroyCacheView(image_view);
4557 artifact=GetImageArtifact(image,
"white-balance:vibrance");
4558 if (artifact != (
const char *) NULL)
4575 flags=ParseGeometry(artifact,&geometry_info);
4576 if ((flags & RhoValue) != 0)
4577 black_point=geometry_info.rho;
4578 if ((flags & PercentValue) != 0)
4579 black_point*=((double) QuantumRange/100.0);
4580 channel_mask=SetImageChannelMask(image,(ChannelType) (aChannel |
4582 status&=(MagickStatusType) LevelImage(image,black_point,(
double)
4583 QuantumRange-black_point,1.0,exception);
4584 (void) SetImageChannelMask(image,channel_mask);
4586 status&=(MagickStatusType) TransformImageColorspace(image,sRGBColorspace,
4588 return(status != 0 ? MagickTrue : MagickFalse);