2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT H H RRRR EEEEE SSSSS H H OOO L DDDD %
7 % T H H R R E SS H H O O L D D %
8 % T HHHHH RRRR EEE SSS HHHHH O O L D D %
9 % T H H R R E SS H H O O L D D %
10 % T H H R R EEEEE SSSSS H H OOO LLLLL DDDD %
13 % MagickCore Image Threshold Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/configure.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/decorate.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/enhance.h"
57 #include "MagickCore/exception.h"
58 #include "MagickCore/exception-private.h"
59 #include "MagickCore/effect.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/gem.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/image-private.h"
64 #include "MagickCore/list.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/memory_.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/montage.h"
70 #include "MagickCore/option.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/quantize.h"
73 #include "MagickCore/quantum.h"
74 #include "MagickCore/random_.h"
75 #include "MagickCore/random-private.h"
76 #include "MagickCore/resize.h"
77 #include "MagickCore/resource_.h"
78 #include "MagickCore/segment.h"
79 #include "MagickCore/shear.h"
80 #include "MagickCore/signature-private.h"
81 #include "MagickCore/string_.h"
82 #include "MagickCore/string-private.h"
83 #include "MagickCore/thread-private.h"
84 #include "MagickCore/threshold.h"
85 #include "MagickCore/token.h"
86 #include "MagickCore/transform.h"
87 #include "MagickCore/xml-tree.h"
88 #include "MagickCore/xml-tree-private.h"
93 #define ThresholdsFilename "thresholds.xml"
114 Forward declarations.
117 *GetThresholdMapFile(const char *,const char *,const char *,ExceptionInfo *);
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % A d a p t i v e T h r e s h o l d I m a g e %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % AdaptiveThresholdImage() selects an individual threshold for each pixel
131 % based on the range of intensity values in its local neighborhood. This
132 % allows for thresholding of an image whose global intensity histogram
133 % doesn't contain distinctive peaks.
135 % The format of the AdaptiveThresholdImage method is:
137 % Image *AdaptiveThresholdImage(const Image *image,const size_t width,
138 % const size_t height,const double bias,ExceptionInfo *exception)
140 % A description of each parameter follows:
142 % o image: the image.
144 % o width: the width of the local neighborhood.
146 % o height: the height of the local neighborhood.
148 % o bias: the mean bias.
150 % o exception: return any errors or warnings in this structure.
153 MagickExport Image *AdaptiveThresholdImage(const Image *image,
154 const size_t width,const size_t height,const double bias,
155 ExceptionInfo *exception)
157 #define AdaptiveThresholdImageTag "AdaptiveThreshold/Image"
179 Initialize threshold image attributes.
181 assert(image != (Image *) NULL);
182 assert(image->signature == MagickSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickSignature);
187 if ((width % 2) == 0)
188 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
189 threshold_image=CloneImage(image,image->columns,image->rows,MagickTrue,
191 if (threshold_image == (Image *) NULL)
192 return((Image *) NULL);
193 status=SetImageStorageClass(threshold_image,DirectClass,exception);
194 if (status == MagickFalse)
196 threshold_image=DestroyImage(threshold_image);
197 return((Image *) NULL);
204 number_pixels=(MagickSizeType) width*height;
205 image_view=AcquireVirtualCacheView(image,exception);
206 threshold_view=AcquireAuthenticCacheView(threshold_image,exception);
207 #if defined(MAGICKCORE_OPENMP_SUPPORT)
208 #pragma omp parallel for schedule(static,4) shared(progress,status) \
209 dynamic_number_threads(image,image->columns,image->rows,1)
211 for (y=0; y < (ssize_t) image->rows; y++)
213 register const Quantum
225 if (status == MagickFalse)
227 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
228 (height/2L),image->columns+width,height,exception);
229 q=QueueCacheViewAuthenticPixels(threshold_view,0,y,threshold_image->columns,
231 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
236 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(height/2L)+
237 GetPixelChannels(image)*(width/2);
238 for (x=0; x < (ssize_t) image->columns; x++)
243 if (GetPixelMask(image,p) != 0)
245 p+=GetPixelChannels(image);
246 q+=GetPixelChannels(threshold_image);
249 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
262 register const Quantum
271 channel=GetPixelChannelMapChannel(image,i);
272 traits=GetPixelChannelMapTraits(image,channel);
273 threshold_traits=GetPixelChannelMapTraits(threshold_image,channel);
274 if ((traits == UndefinedPixelTrait) ||
275 (threshold_traits == UndefinedPixelTrait))
277 if ((threshold_traits & CopyPixelTrait) != 0)
279 SetPixelChannel(threshold_image,channel,p[center+i],q);
284 for (v=0; v < (ssize_t) height; v++)
286 for (u=0; u < (ssize_t) width; u++)
289 pixels+=GetPixelChannels(image);
291 pixels+=image->columns*GetPixelChannels(image);
293 mean=(MagickRealType) (pixel/number_pixels+bias);
294 SetPixelChannel(threshold_image,channel,(Quantum) ((MagickRealType)
295 p[center+i] <= mean ? 0 : QuantumRange),q);
297 p+=GetPixelChannels(image);
298 q+=GetPixelChannels(threshold_image);
300 if (SyncCacheViewAuthenticPixels(threshold_view,exception) == MagickFalse)
302 if (image->progress_monitor != (MagickProgressMonitor) NULL)
307 #if defined(MAGICKCORE_OPENMP_SUPPORT)
308 #pragma omp critical (MagickCore_AdaptiveThresholdImage)
310 proceed=SetImageProgress(image,AdaptiveThresholdImageTag,progress++,
312 if (proceed == MagickFalse)
316 threshold_image->type=image->type;
317 threshold_view=DestroyCacheView(threshold_view);
318 image_view=DestroyCacheView(image_view);
319 if (status == MagickFalse)
320 threshold_image=DestroyImage(threshold_image);
321 return(threshold_image);
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % B i l e v e l I m a g e %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % BilevelImage() changes the value of individual pixels based on the
336 % intensity of each pixel channel. The result is a high-contrast image.
338 % More precisely each channel value of the image is 'thresholded' so that if
339 % it is equal to or less than the given value it is set to zero, while any
340 % value greater than that give is set to it maximum or QuantumRange.
342 % This function is what is used to implement the "-threshold" operator for
343 % the command line API.
345 % If the default channel setting is given the image is thresholded using just
346 % the gray 'intensity' of the image, rather than the individual channels.
348 % The format of the BilevelImage method is:
350 % MagickBooleanType BilevelImage(Image *image,const double threshold,
351 % ExceptionInfo *exception)
353 % A description of each parameter follows:
355 % o image: the image.
357 % o threshold: define the threshold values.
359 % o exception: return any errors or warnings in this structure.
361 % Aside: You can get the same results as operator using LevelImages()
362 % with the 'threshold' value for both the black_point and the white_point.
365 MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold,
366 ExceptionInfo *exception)
368 #define ThresholdImageTag "Threshold/Image"
382 assert(image != (Image *) NULL);
383 assert(image->signature == MagickSignature);
384 if (image->debug != MagickFalse)
385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
386 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
389 Bilevel threshold image.
393 image_view=AcquireAuthenticCacheView(image,exception);
394 #if defined(MAGICKCORE_OPENMP_SUPPORT)
395 #pragma omp parallel for schedule(static,8) shared(progress,status) \
396 dynamic_number_threads(image,image->columns,image->rows,1)
398 for (y=0; y < (ssize_t) image->rows; y++)
406 if (status == MagickFalse)
408 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
409 if (q == (Quantum *) NULL)
414 for (x=0; x < (ssize_t) image->columns; x++)
422 if (GetPixelMask(image,q) != 0)
424 q+=GetPixelChannels(image);
427 pixel=(double) GetPixelIntensity(image,q);
428 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
436 channel=GetPixelChannelMapChannel(image,i);
437 traits=GetPixelChannelMapTraits(image,channel);
438 if ((traits & UpdatePixelTrait) == 0)
440 if (image->channel_mask != DefaultChannels)
442 q[i]=(Quantum) (pixel <= threshold ? 0 : QuantumRange);
444 q+=GetPixelChannels(image);
446 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
448 if (image->progress_monitor != (MagickProgressMonitor) NULL)
453 #if defined(MAGICKCORE_OPENMP_SUPPORT)
454 #pragma omp critical (MagickCore_BilevelImage)
456 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
458 if (proceed == MagickFalse)
462 image_view=DestroyCacheView(image_view);
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 % B l a c k T h r e s h o l d I m a g e %
475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % BlackThresholdImage() is like ThresholdImage() but forces all pixels below
478 % the threshold into black while leaving all pixels at or above the threshold
481 % The format of the BlackThresholdImage method is:
483 % MagickBooleanType BlackThresholdImage(Image *image,
484 % const char *threshold,ExceptionInfo *exception)
486 % A description of each parameter follows:
488 % o image: the image.
490 % o threshold: define the threshold value.
492 % o exception: return any errors or warnings in this structure.
495 MagickExport MagickBooleanType BlackThresholdImage(Image *image,
496 const char *thresholds,ExceptionInfo *exception)
498 #define ThresholdImageTag "Threshold/Image"
521 assert(image != (Image *) NULL);
522 assert(image->signature == MagickSignature);
523 if (image->debug != MagickFalse)
524 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
525 if (thresholds == (const char *) NULL)
527 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
529 if (IsGrayColorspace(image->colorspace) != MagickFalse)
530 (void) TransformImageColorspace(image,RGBColorspace,exception);
531 GetPixelInfo(image,&threshold);
532 flags=ParseGeometry(thresholds,&geometry_info);
533 threshold.red=geometry_info.rho;
534 threshold.green=geometry_info.rho;
535 threshold.blue=geometry_info.rho;
536 threshold.black=geometry_info.rho;
537 threshold.alpha=100.0;
538 if ((flags & SigmaValue) != 0)
539 threshold.green=geometry_info.sigma;
540 if ((flags & XiValue) != 0)
541 threshold.blue=geometry_info.xi;
542 if ((flags & PsiValue) != 0)
543 threshold.alpha=geometry_info.psi;
544 if (threshold.colorspace == CMYKColorspace)
546 if ((flags & PsiValue) != 0)
547 threshold.black=geometry_info.psi;
548 if ((flags & ChiValue) != 0)
549 threshold.alpha=geometry_info.chi;
551 if ((flags & PercentValue) != 0)
553 threshold.red*=(QuantumRange/100.0);
554 threshold.green*=(QuantumRange/100.0);
555 threshold.blue*=(QuantumRange/100.0);
556 threshold.black*=(QuantumRange/100.0);
557 threshold.alpha*=(QuantumRange/100.0);
560 White threshold image.
564 image_view=AcquireAuthenticCacheView(image,exception);
565 #if defined(MAGICKCORE_OPENMP_SUPPORT)
566 #pragma omp parallel for schedule(static,8) shared(progress,status) \
567 dynamic_number_threads(image,image->columns,image->rows,1)
569 for (y=0; y < (ssize_t) image->rows; y++)
577 if (status == MagickFalse)
579 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
580 if (q == (Quantum *) NULL)
585 for (x=0; x < (ssize_t) image->columns; x++)
590 if (GetPixelMask(image,q) != 0)
592 q+=GetPixelChannels(image);
595 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
603 channel=GetPixelChannelMapChannel(image,i);
604 traits=GetPixelChannelMapTraits(image,channel);
605 if ((traits & UpdatePixelTrait) == 0)
607 if ((double) q[i] <= GetPixelInfoChannel(&threshold,channel))
610 q+=GetPixelChannels(image);
612 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
614 if (image->progress_monitor != (MagickProgressMonitor) NULL)
619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
620 #pragma omp critical (MagickCore_BlackThresholdImage)
622 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
624 if (proceed == MagickFalse)
628 image_view=DestroyCacheView(image_view);
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % C l a m p I m a g e %
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % ClampImage() restricts the color range from 0 to the quantum depth.
645 % The format of the ClampImage method is:
647 % MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
649 % A description of each parameter follows:
651 % o image: the image.
653 % o exception: return any errors or warnings in this structure.
657 static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
659 #if defined(MAGICKCORE_HDRI_SUPPORT)
662 if (quantum >= QuantumRange)
663 return(QuantumRange);
670 MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
672 #define ClampImageTag "Clamp/Image"
686 assert(image != (Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 if (image->storage_class == PseudoClass)
699 for (i=0; i < (ssize_t) image->colors; i++)
701 q->red=(double) ClampToUnsignedQuantum(ClampToQuantum(q->red));
702 q->green=(double) ClampToUnsignedQuantum(ClampToQuantum(q->green));
703 q->blue=(double) ClampToUnsignedQuantum(ClampToQuantum(q->blue));
704 q->alpha=(double) ClampToUnsignedQuantum(ClampToQuantum(q->alpha));
707 return(SyncImage(image,exception));
714 image_view=AcquireAuthenticCacheView(image,exception);
715 #if defined(MAGICKCORE_OPENMP_SUPPORT)
716 #pragma omp parallel for schedule(static,8) shared(progress,status) \
717 dynamic_number_threads(image,image->columns,image->rows,1)
719 for (y=0; y < (ssize_t) image->rows; y++)
727 if (status == MagickFalse)
729 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
730 if (q == (Quantum *) NULL)
735 for (x=0; x < (ssize_t) image->columns; x++)
740 if (GetPixelMask(image,q) != 0)
742 q+=GetPixelChannels(image);
745 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
753 channel=GetPixelChannelMapChannel(image,i);
754 traits=GetPixelChannelMapTraits(image,channel);
755 if (traits == UndefinedPixelTrait)
757 q[i]=ClampToUnsignedQuantum(q[i]);
759 q+=GetPixelChannels(image);
761 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
763 if (image->progress_monitor != (MagickProgressMonitor) NULL)
768 #if defined(MAGICKCORE_OPENMP_SUPPORT)
769 #pragma omp critical (MagickCore_ClampImage)
771 proceed=SetImageProgress(image,ClampImageTag,progress++,image->rows);
772 if (proceed == MagickFalse)
776 image_view=DestroyCacheView(image_view);
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 % D e s t r o y T h r e s h o l d M a p %
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % DestroyThresholdMap() de-allocate the given ThresholdMap
793 % The format of the ListThresholdMaps method is:
795 % ThresholdMap *DestroyThresholdMap(Threshold *map)
797 % A description of each parameter follows.
799 % o map: Pointer to the Threshold map to destroy
802 MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
804 assert(map != (ThresholdMap *) NULL);
805 if (map->map_id != (char *) NULL)
806 map->map_id=DestroyString(map->map_id);
807 if (map->description != (char *) NULL)
808 map->description=DestroyString(map->description);
809 if (map->levels != (ssize_t *) NULL)
810 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
811 map=(ThresholdMap *) RelinquishMagickMemory(map);
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % G e t T h r e s h o l d M a p %
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % GetThresholdMap() loads and searches one or more threshold map files for the
827 % map matching the given name or alias.
829 % The format of the GetThresholdMap method is:
831 % ThresholdMap *GetThresholdMap(const char *map_id,
832 % ExceptionInfo *exception)
834 % A description of each parameter follows.
836 % o map_id: ID of the map to look for.
838 % o exception: return any errors or warnings in this structure.
841 MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
842 ExceptionInfo *exception)
853 map=(ThresholdMap *)NULL;
854 options=GetConfigureOptions(ThresholdsFilename,exception);
855 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
856 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
857 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
858 GetStringInfoPath(option),map_id,exception);
859 options=DestroyConfigureOptions(options);
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 + G e t T h r e s h o l d M a p F i l e %
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % GetThresholdMapFile() look for a given threshold map name or alias in the
875 % given XML file data, and return the allocated the map when found.
877 % The format of the ListThresholdMaps method is:
879 % ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
880 % const char *map_id,ExceptionInfo *exception)
882 % A description of each parameter follows.
884 % o xml: The threshold map list in XML format.
886 % o filename: The threshold map XML filename.
888 % o map_id: ID of the map to look for in XML list.
890 % o exception: return any errors or warnings in this structure.
893 static ThresholdMap *GetThresholdMapFile(const char *xml,
894 const char *filename,const char *map_id,ExceptionInfo *exception)
918 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
919 "Loading threshold map file \"%s\" ...",filename);
920 map=(ThresholdMap *) NULL;
921 thresholds=NewXMLTree(xml,exception);
922 if (thresholds == (XMLTreeInfo *) NULL)
924 for (threshold=GetXMLTreeChild(thresholds,"threshold");
925 threshold != (XMLTreeInfo *) NULL;
926 threshold=GetNextXMLTreeTag(threshold))
928 attribute=GetXMLTreeAttribute(threshold,"map");
929 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
931 attribute=GetXMLTreeAttribute(threshold,"alias");
932 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
935 if (threshold == (XMLTreeInfo *) NULL)
937 description=GetXMLTreeChild(threshold,"description");
938 if (description == (XMLTreeInfo *) NULL)
940 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
941 "XmlMissingElement", "<description>, map \"%s\"",map_id);
942 thresholds=DestroyXMLTree(thresholds);
945 levels=GetXMLTreeChild(threshold,"levels");
946 if (levels == (XMLTreeInfo *) NULL)
948 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
949 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
950 thresholds=DestroyXMLTree(thresholds);
953 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
954 if (map == (ThresholdMap *) NULL)
955 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
956 map->map_id=(char *) NULL;
957 map->description=(char *) NULL;
958 map->levels=(ssize_t *) NULL;
959 attribute=GetXMLTreeAttribute(threshold,"map");
960 if (attribute != (char *) NULL)
961 map->map_id=ConstantString(attribute);
962 content=GetXMLTreeContent(description);
963 if (content != (char *) NULL)
964 map->description=ConstantString(content);
965 attribute=GetXMLTreeAttribute(levels,"width");
966 if (attribute == (char *) NULL)
968 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
969 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
970 thresholds=DestroyXMLTree(thresholds);
971 map=DestroyThresholdMap(map);
974 map->width=StringToUnsignedLong(attribute);
977 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
978 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
979 thresholds=DestroyXMLTree(thresholds);
980 map=DestroyThresholdMap(map);
983 attribute=GetXMLTreeAttribute(levels,"height");
984 if (attribute == (char *) NULL)
986 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
987 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
988 thresholds=DestroyXMLTree(thresholds);
989 map=DestroyThresholdMap(map);
992 map->height=StringToUnsignedLong(attribute);
993 if (map->height == 0)
995 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
996 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
997 thresholds=DestroyXMLTree(thresholds);
998 map=DestroyThresholdMap(map);
1001 attribute=GetXMLTreeAttribute(levels,"divisor");
1002 if (attribute == (char *) NULL)
1004 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1005 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
1006 thresholds=DestroyXMLTree(thresholds);
1007 map=DestroyThresholdMap(map);
1010 map->divisor=(ssize_t) StringToLong(attribute);
1011 if (map->divisor < 2)
1013 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1014 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
1015 thresholds=DestroyXMLTree(thresholds);
1016 map=DestroyThresholdMap(map);
1019 content=GetXMLTreeContent(levels);
1020 if (content == (char *) NULL)
1022 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1023 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
1024 thresholds=DestroyXMLTree(thresholds);
1025 map=DestroyThresholdMap(map);
1028 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
1029 sizeof(*map->levels));
1030 if (map->levels == (ssize_t *) NULL)
1031 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
1032 for (i=0; i < (ssize_t) (map->width*map->height); i++)
1034 map->levels[i]=(ssize_t) strtol(content,&p,10);
1037 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1038 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
1039 thresholds=DestroyXMLTree(thresholds);
1040 map=DestroyThresholdMap(map);
1043 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
1045 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1046 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
1047 (double) map->levels[i],map_id);
1048 thresholds=DestroyXMLTree(thresholds);
1049 map=DestroyThresholdMap(map);
1054 value=(double) strtol(content,&p,10);
1058 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1059 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1060 thresholds=DestroyXMLTree(thresholds);
1061 map=DestroyThresholdMap(map);
1064 thresholds=DestroyXMLTree(thresholds);
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 + L i s t T h r e s h o l d M a p F i l e %
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079 % ListThresholdMapFile() lists the threshold maps and their descriptions
1080 % in the given XML file data.
1082 % The format of the ListThresholdMaps method is:
1084 % MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1085 % const char *filename,ExceptionInfo *exception)
1087 % A description of each parameter follows.
1089 % o file: An pointer to the output FILE.
1091 % o xml: The threshold map list in XML format.
1093 % o filename: The threshold map XML filename.
1095 % o exception: return any errors or warnings in this structure.
1098 MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1099 const char *filename,ExceptionInfo *exception)
1111 assert( xml != (char *)NULL );
1112 assert( file != (FILE *)NULL );
1113 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1114 "Loading threshold map file \"%s\" ...",filename);
1115 thresholds=NewXMLTree(xml,exception);
1116 if ( thresholds == (XMLTreeInfo *)NULL )
1117 return(MagickFalse);
1118 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1119 (void) FormatLocaleFile(file,
1120 "----------------------------------------------------\n");
1121 threshold=GetXMLTreeChild(thresholds,"threshold");
1122 for ( ; threshold != (XMLTreeInfo *) NULL;
1123 threshold=GetNextXMLTreeTag(threshold))
1125 map=GetXMLTreeAttribute(threshold,"map");
1126 if (map == (char *) NULL)
1128 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1129 "XmlMissingAttribute", "<map>");
1130 thresholds=DestroyXMLTree(thresholds);
1131 return(MagickFalse);
1133 alias=GetXMLTreeAttribute(threshold,"alias");
1134 description=GetXMLTreeChild(threshold,"description");
1135 if (description == (XMLTreeInfo *) NULL)
1137 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1138 "XmlMissingElement", "<description>, map \"%s\"",map);
1139 thresholds=DestroyXMLTree(thresholds);
1140 return(MagickFalse);
1142 content=GetXMLTreeContent(description);
1143 if (content == (char *) NULL)
1145 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1146 "XmlMissingContent", "<description>, map \"%s\"", map);
1147 thresholds=DestroyXMLTree(thresholds);
1148 return(MagickFalse);
1150 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1153 thresholds=DestroyXMLTree(thresholds);
1158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1162 % L i s t T h r e s h o l d M a p s %
1166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1168 % ListThresholdMaps() lists the threshold maps and their descriptions
1169 % as defined by "threshold.xml" to a file.
1171 % The format of the ListThresholdMaps method is:
1173 % MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1175 % A description of each parameter follows.
1177 % o file: An pointer to the output FILE.
1179 % o exception: return any errors or warnings in this structure.
1182 MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1183 ExceptionInfo *exception)
1195 if (file == (FILE *) NULL)
1197 options=GetConfigureOptions(ThresholdsFilename,exception);
1198 (void) FormatLocaleFile(file,
1199 "\n Threshold Maps for Ordered Dither Operations\n");
1200 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1201 (const StringInfo *) NULL)
1203 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
1204 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1205 GetStringInfoPath(option),exception);
1207 options=DestroyConfigureOptions(options);
1208 return(status != 0 ? MagickTrue : MagickFalse);
1212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216 % O r d e r e d P o s t e r i z e I m a g e %
1220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222 % OrderedPosterizeImage() will perform a ordered dither based on a number
1223 % of pre-defined dithering threshold maps, but over multiple intensity
1224 % levels, which can be different for different channels, according to the
1227 % The format of the OrderedPosterizeImage method is:
1229 % MagickBooleanType OrderedPosterizeImage(Image *image,
1230 % const char *threshold_map,ExceptionInfo *exception)
1232 % A description of each parameter follows:
1234 % o image: the image.
1236 % o threshold_map: A string containing the name of the threshold dither
1237 % map to use, followed by zero or more numbers representing the number
1238 % of color levels tho dither between.
1240 % Any level number less than 2 will be equivalent to 2, and means only
1241 % binary dithering will be applied to each color channel.
1243 % No numbers also means a 2 level (bitmap) dither will be applied to all
1244 % channels, while a single number is the number of levels applied to each
1245 % channel in sequence. More numbers will be applied in turn to each of
1246 % the color channels.
1248 % For example: "o3x3,6" will generate a 6 level posterization of the
1249 % image with a ordered 3x3 diffused pixel dither being applied between
1250 % each level. While checker,8,8,4 will produce a 332 colormaped image
1251 % with only a single checkerboard hash pattern (50% grey) between each
1252 % color level, to basically double the number of color levels with
1253 % a bare minimim of dithering.
1255 % o exception: return any errors or warnings in this structure.
1258 MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1259 const char *threshold_map,ExceptionInfo *exception)
1261 #define DitherImageTag "Dither/Image"
1267 token[MaxTextExtent];
1279 levels[CompositePixelChannel];
1290 assert(image != (Image *) NULL);
1291 assert(image->signature == MagickSignature);
1292 if (image->debug != MagickFalse)
1293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1294 assert(exception != (ExceptionInfo *) NULL);
1295 assert(exception->signature == MagickSignature);
1296 if (threshold_map == (const char *) NULL)
1298 p=(char *) threshold_map;
1299 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1303 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1306 if ((p-threshold_map) >= (MaxTextExtent-1))
1308 token[p-threshold_map]=(*p);
1311 token[p-threshold_map]='\0';
1312 map=GetThresholdMap(token,exception);
1313 if (map == (ThresholdMap *) NULL)
1315 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1316 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
1317 return(MagickFalse);
1319 for (i=0; i < MaxPixelChannels; i++)
1321 p=strchr((char *) threshold_map,',');
1322 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1323 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1325 GetMagickToken(p,&p,token);
1327 GetMagickToken(p,&p,token);
1328 levels[i]=StringToDouble(token,(char **) NULL);
1330 for (i=0; i < MaxPixelChannels; i++)
1331 if (fabs(levels[i]) >= 1)
1333 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1334 return(MagickFalse);
1337 image_view=AcquireAuthenticCacheView(image,exception);
1338 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1339 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1340 dynamic_number_threads(image,image->columns,image->rows,1)
1342 for (y=0; y < (ssize_t) image->rows; y++)
1350 if (status == MagickFalse)
1352 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1353 if (q == (Quantum *) NULL)
1358 for (x=0; x < (ssize_t) image->columns; x++)
1367 if (GetPixelMask(image,q) != 0)
1369 q+=GetPixelChannels(image);
1372 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1384 channel=GetPixelChannelMapChannel(image,i);
1385 traits=GetPixelChannelMapTraits(image,channel);
1386 if ((traits & UpdatePixelTrait) == 0)
1388 if (fabs(levels[n++]) < MagickEpsilon)
1390 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1391 level=threshold/(map->divisor-1);
1392 threshold-=level*(map->divisor-1);
1393 q[i]=ClampToQuantum((double) (level+(threshold >=
1394 map->levels[(x % map->width)+map->width*(y % map->height)]))*
1395 QuantumRange/levels[n]);
1398 q+=GetPixelChannels(image);
1400 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1402 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1407 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1408 #pragma omp critical (MagickCore_OrderedPosterizeImage)
1410 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1411 if (proceed == MagickFalse)
1415 image_view=DestroyCacheView(image_view);
1416 map=DestroyThresholdMap(map);
1421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1425 % R a n d o m T h r e s h o l d I m a g e %
1429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 % RandomThresholdImage() changes the value of individual pixels based on the
1432 % intensity of each pixel compared to a random threshold. The result is a
1433 % low-contrast, two color image.
1435 % The format of the RandomThresholdImage method is:
1437 % MagickBooleanType RandomThresholdImage(Image *image,
1438 % const char *thresholds,ExceptionInfo *exception)
1440 % A description of each parameter follows:
1442 % o image: the image.
1444 % o thresholds: a geometry string containing low,high thresholds. If the
1445 % string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1446 % is performed instead.
1448 % o exception: return any errors or warnings in this structure.
1451 MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1452 const char *thresholds,ExceptionInfo *exception)
1454 #define ThresholdImageTag "Threshold/Image"
1479 **restrict random_info;
1487 assert(image != (Image *) NULL);
1488 assert(image->signature == MagickSignature);
1489 if (image->debug != MagickFalse)
1490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1491 assert(exception != (ExceptionInfo *) NULL);
1492 assert(exception->signature == MagickSignature);
1493 if (thresholds == (const char *) NULL)
1495 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1496 return(MagickFalse);
1497 GetPixelInfo(image,&threshold);
1499 max_threshold=(MagickRealType) QuantumRange;
1500 flags=ParseGeometry(thresholds,&geometry_info);
1501 min_threshold=geometry_info.rho;
1502 max_threshold=geometry_info.sigma;
1503 if ((flags & SigmaValue) == 0)
1504 max_threshold=min_threshold;
1505 if (strchr(thresholds,'%') != (char *) NULL)
1507 max_threshold*=(MagickRealType) (0.01*QuantumRange);
1508 min_threshold*=(MagickRealType) (0.01*QuantumRange);
1511 Random threshold image.
1515 random_info=AcquireRandomInfoThreadSet();
1516 key=GetRandomSecretKey(random_info[0]);
1517 image_view=AcquireAuthenticCacheView(image,exception);
1518 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1519 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1520 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
1522 for (y=0; y < (ssize_t) image->rows; y++)
1525 id = GetOpenMPThreadId();
1533 if (status == MagickFalse)
1535 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1536 if (q == (Quantum *) NULL)
1541 for (x=0; x < (ssize_t) image->columns; x++)
1546 if (GetPixelMask(image,q) != 0)
1548 q+=GetPixelChannels(image);
1551 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1562 channel=GetPixelChannelMapChannel(image,i);
1563 traits=GetPixelChannelMapTraits(image,channel);
1564 if ((traits & UpdatePixelTrait) == 0)
1566 if ((MagickRealType) q[i] < min_threshold)
1567 threshold=min_threshold;
1569 if ((MagickRealType) q[i] > max_threshold)
1570 threshold=max_threshold;
1572 threshold=(MagickRealType) (QuantumRange*
1573 GetPseudoRandomValue(random_info[id]));
1574 q[i]=(MagickRealType) q[i] <= threshold ? 0 : QuantumRange;
1576 q+=GetPixelChannels(image);
1578 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1580 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1585 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1586 #pragma omp critical (MagickCore_RandomThresholdImage)
1588 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1590 if (proceed == MagickFalse)
1594 image_view=DestroyCacheView(image_view);
1595 random_info=DestroyRandomInfoThreadSet(random_info);
1600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1604 % W h i t e T h r e s h o l d I m a g e %
1608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1610 % WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
1611 % the threshold into white while leaving all pixels at or below the threshold
1614 % The format of the WhiteThresholdImage method is:
1616 % MagickBooleanType WhiteThresholdImage(Image *image,
1617 % const char *threshold,ExceptionInfo *exception)
1619 % A description of each parameter follows:
1621 % o image: the image.
1623 % o threshold: Define the threshold value.
1625 % o exception: return any errors or warnings in this structure.
1628 MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
1629 const char *thresholds,ExceptionInfo *exception)
1631 #define ThresholdImageTag "Threshold/Image"
1654 assert(image != (Image *) NULL);
1655 assert(image->signature == MagickSignature);
1656 if (image->debug != MagickFalse)
1657 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1658 if (thresholds == (const char *) NULL)
1660 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1661 return(MagickFalse);
1662 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1663 (void) TransformImageColorspace(image,RGBColorspace,exception);
1664 GetPixelInfo(image,&threshold);
1665 flags=ParseGeometry(thresholds,&geometry_info);
1666 threshold.red=geometry_info.rho;
1667 threshold.green=geometry_info.rho;
1668 threshold.blue=geometry_info.rho;
1669 threshold.black=geometry_info.rho;
1670 threshold.alpha=100.0;
1671 if ((flags & SigmaValue) != 0)
1672 threshold.green=geometry_info.sigma;
1673 if ((flags & XiValue) != 0)
1674 threshold.blue=geometry_info.xi;
1675 if ((flags & PsiValue) != 0)
1676 threshold.alpha=geometry_info.psi;
1677 if (threshold.colorspace == CMYKColorspace)
1679 if ((flags & PsiValue) != 0)
1680 threshold.black=geometry_info.psi;
1681 if ((flags & ChiValue) != 0)
1682 threshold.alpha=geometry_info.chi;
1684 if ((flags & PercentValue) != 0)
1686 threshold.red*=(QuantumRange/100.0);
1687 threshold.green*=(QuantumRange/100.0);
1688 threshold.blue*=(QuantumRange/100.0);
1689 threshold.black*=(QuantumRange/100.0);
1690 threshold.alpha*=(QuantumRange/100.0);
1693 White threshold image.
1697 image_view=AcquireAuthenticCacheView(image,exception);
1698 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1699 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1700 dynamic_number_threads(image,image->columns,image->rows,1)
1702 for (y=0; y < (ssize_t) image->rows; y++)
1710 if (status == MagickFalse)
1712 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1713 if (q == (Quantum *) NULL)
1718 for (x=0; x < (ssize_t) image->columns; x++)
1723 if (GetPixelMask(image,q) != 0)
1725 q+=GetPixelChannels(image);
1728 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1736 channel=GetPixelChannelMapChannel(image,i);
1737 traits=GetPixelChannelMapTraits(image,channel);
1738 if ((traits & UpdatePixelTrait) == 0)
1740 if ((double) q[i] > GetPixelInfoChannel(&threshold,channel))
1743 q+=GetPixelChannels(image);
1745 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1747 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1752 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1753 #pragma omp critical (MagickCore_WhiteThresholdImage)
1755 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1757 if (proceed == MagickFalse)
1761 image_view=DestroyCacheView(image_view);