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-2011 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/configure.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/draw.h"
55 #include "MagickCore/enhance.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/effect.h"
59 #include "MagickCore/fx.h"
60 #include "MagickCore/gem.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/monitor.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/montage.h"
69 #include "MagickCore/option.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/quantize.h"
72 #include "MagickCore/quantum.h"
73 #include "MagickCore/random_.h"
74 #include "MagickCore/random-private.h"
75 #include "MagickCore/resize.h"
76 #include "MagickCore/resource_.h"
77 #include "MagickCore/segment.h"
78 #include "MagickCore/shear.h"
79 #include "MagickCore/signature-private.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/transform.h"
86 #include "MagickCore/xml-tree.h"
87 #include "MagickCore/xml-tree-private.h"
92 #define ThresholdsFilename "thresholds.xml"
113 Forward declarations.
116 *GetThresholdMapFile(const char *,const char *,const char *,ExceptionInfo *);
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % A d a p t i v e T h r e s h o l d I m a g e %
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % AdaptiveThresholdImage() selects an individual threshold for each pixel
130 % based on the range of intensity values in its local neighborhood. This
131 % allows for thresholding of an image whose global intensity histogram
132 % doesn't contain distinctive peaks.
134 % The format of the AdaptiveThresholdImage method is:
136 % Image *AdaptiveThresholdImage(const Image *image,const size_t width,
137 % const size_t height,const double bias,ExceptionInfo *exception)
139 % A description of each parameter follows:
141 % o image: the image.
143 % o width: the width of the local neighborhood.
145 % o height: the height of the local neighborhood.
147 % o bias: the mean bias.
149 % o exception: return any errors or warnings in this structure.
152 MagickExport Image *AdaptiveThresholdImage(const Image *image,
153 const size_t width,const size_t height,const double bias,
154 ExceptionInfo *exception)
156 #define AdaptiveThresholdImageTag "AdaptiveThreshold/Image"
178 Initialize threshold image attributes.
180 assert(image != (Image *) NULL);
181 assert(image->signature == MagickSignature);
182 if (image->debug != MagickFalse)
183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
184 assert(exception != (ExceptionInfo *) NULL);
185 assert(exception->signature == MagickSignature);
186 if ((width % 2) == 0)
187 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
188 threshold_image=CloneImage(image,image->columns,image->rows,MagickTrue,
190 if (threshold_image == (Image *) NULL)
191 return((Image *) NULL);
192 status=SetImageStorageClass(threshold_image,DirectClass,exception);
193 if (status == MagickFalse)
195 threshold_image=DestroyImage(threshold_image);
196 return((Image *) NULL);
203 number_pixels=(MagickSizeType) width*height;
204 image_view=AcquireCacheView(image);
205 threshold_view=AcquireCacheView(threshold_image);
206 #if defined(MAGICKCORE_OPENMP_SUPPORT)
207 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
209 for (y=0; y < (ssize_t) image->rows; y++)
211 register const Quantum
223 if (status == MagickFalse)
225 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
226 (height/2L),image->columns+width,height,exception);
227 q=QueueCacheViewAuthenticPixels(threshold_view,0,y,threshold_image->columns,
229 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
234 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(height/2L)+
235 GetPixelChannels(image)*(width/2);
236 for (x=0; x < (ssize_t) image->columns; x++)
241 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
254 register const Quantum
263 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
264 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
265 threshold_traits=GetPixelChannelMapTraits(threshold_image,channel);
266 if ((traits == UndefinedPixelTrait) ||
267 (threshold_traits == UndefinedPixelTrait))
269 if ((threshold_traits & CopyPixelTrait) != 0)
271 SetPixelChannel(threshold_image,channel,p[center+i],q);
276 for (v=0; v < (ssize_t) height; v++)
278 for (u=0; u < (ssize_t) width; u++)
281 pixels+=GetPixelChannels(image);
283 pixels+=image->columns*GetPixelChannels(image);
285 mean=(MagickRealType) (pixel/number_pixels+bias);
286 SetPixelChannel(threshold_image,channel,(Quantum) ((MagickRealType)
287 p[center+i] <= mean ? 0 : QuantumRange),q);
289 p+=GetPixelChannels(image);
290 q+=GetPixelChannels(threshold_image);
292 if (SyncCacheViewAuthenticPixels(threshold_view,exception) == MagickFalse)
294 if (image->progress_monitor != (MagickProgressMonitor) NULL)
299 #if defined(MAGICKCORE_OPENMP_SUPPORT)
300 #pragma omp critical (MagickCore_AdaptiveThresholdImage)
302 proceed=SetImageProgress(image,AdaptiveThresholdImageTag,progress++,
304 if (proceed == MagickFalse)
308 threshold_image->type=image->type;
309 threshold_view=DestroyCacheView(threshold_view);
310 image_view=DestroyCacheView(image_view);
311 if (status == MagickFalse)
312 threshold_image=DestroyImage(threshold_image);
313 return(threshold_image);
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 % B i l e v e l I m a g e %
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % BilevelImage() changes the value of individual pixels based on the
328 % intensity of each pixel channel. The result is a high-contrast image.
330 % More precisely each channel value of the image is 'thresholded' so that if
331 % it is equal to or less than the given value it is set to zero, while any
332 % value greater than that give is set to it maximum or QuantumRange.
334 % This function is what is used to implement the "-threshold" operator for
335 % the command line API.
337 % If the default channel setting is given the image is thresholded using just
338 % the gray 'intensity' of the image, rather than the individual channels.
340 % The format of the BilevelImage method is:
342 % MagickBooleanType BilevelImage(Image *image,const double threshold)
344 % A description of each parameter follows:
346 % o image: the image.
348 % o threshold: define the threshold values.
350 % Aside: You can get the same results as operator using LevelImages()
351 % with the 'threshold' value for both the black_point and the white_point.
354 MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold)
356 #define ThresholdImageTag "Threshold/Image"
373 assert(image != (Image *) NULL);
374 assert(image->signature == MagickSignature);
375 if (image->debug != MagickFalse)
376 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
377 exception=(&image->exception);
378 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
381 Bilevel threshold image.
385 image_view=AcquireCacheView(image);
386 #if defined(MAGICKCORE_OPENMP_SUPPORT)
387 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
389 for (y=0; y < (ssize_t) image->rows; y++)
397 if (status == MagickFalse)
399 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
400 if (q == (Quantum *) NULL)
405 for (x=0; x < (ssize_t) image->columns; x++)
410 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
415 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
416 if ((traits & UpdatePixelTrait) != 0)
417 q[i]=(Quantum) ((MagickRealType) q[i] <= threshold ? 0 :
420 q+=GetPixelChannels(image);
422 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
424 if (image->progress_monitor != (MagickProgressMonitor) NULL)
429 #if defined(MAGICKCORE_OPENMP_SUPPORT)
430 #pragma omp critical (MagickCore_BilevelImage)
432 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
434 if (proceed == MagickFalse)
438 image_view=DestroyCacheView(image_view);
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 % B l a c k T h r e s h o l d I m a g e %
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 % BlackThresholdImage() is like ThresholdImage() but forces all pixels below
454 % the threshold into black while leaving all pixels at or above the threshold
457 % The format of the BlackThresholdImage method is:
459 % MagickBooleanType BlackThresholdImage(Image *image,
460 % const char *threshold,ExceptionInfo *exception)
462 % A description of each parameter follows:
464 % o image: the image.
466 % o threshold: define the threshold value.
468 % o exception: return any errors or warnings in this structure.
471 MagickExport MagickBooleanType BlackThresholdImage(Image *image,
472 const char *thresholds,ExceptionInfo *exception)
474 #define ThresholdImageTag "Threshold/Image"
500 assert(image != (Image *) NULL);
501 assert(image->signature == MagickSignature);
502 if (image->debug != MagickFalse)
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
504 if (thresholds == (const char *) NULL)
506 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
508 flags=ParseGeometry(thresholds,&geometry_info);
509 for (i=0; i < 5; i++)
510 threshold[i]=geometry_info.rho;
511 if ((flags & SigmaValue) != 0)
512 threshold[1]=geometry_info.sigma;
513 if ((flags & XiValue) != 0)
514 threshold[2]=geometry_info.xi;
515 if ((flags & PsiValue) != 0)
516 threshold[3]=geometry_info.psi;
517 if ((flags & ChiValue) != 0)
518 threshold[4]=geometry_info.chi;
519 if ((flags & PercentValue) != 0)
520 for (i=0; i < 5; i++)
521 threshold[i]*=(QuantumRange/100.0);
523 White threshold image.
527 image_view=AcquireCacheView(image);
528 #if defined(MAGICKCORE_OPENMP_SUPPORT)
529 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
531 for (y=0; y < (ssize_t) image->rows; y++)
539 if (status == MagickFalse)
541 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
542 if (q == (Quantum *) NULL)
547 for (x=0; x < (ssize_t) image->columns; x++)
556 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
561 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
562 if ((traits & UpdatePixelTrait) == 0)
564 if ((MagickRealType) q[i] < threshold[n++ % 5])
567 q+=GetPixelChannels(image);
569 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
571 if (image->progress_monitor != (MagickProgressMonitor) NULL)
576 #if defined(MAGICKCORE_OPENMP_SUPPORT)
577 #pragma omp critical (MagickCore_WhiteThresholdImage)
579 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
581 if (proceed == MagickFalse)
585 image_view=DestroyCacheView(image_view);
590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 % C l a m p I m a g e %
598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600 % ClampImage() restricts the color range from 0 to the quantum depth.
602 % The format of the ClampImage method is:
604 % MagickBooleanType ClampImage(Image *image)
606 % A description of each parameter follows:
608 % o image: the image.
612 static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
614 #if defined(MAGICKCORE_HDRI_SUPPORT)
617 if (quantum >= QuantumRange)
618 return(QuantumRange);
625 MagickExport MagickBooleanType ClampImage(Image *image)
627 #define ClampImageTag "Clamp/Image"
644 assert(image != (Image *) NULL);
645 assert(image->signature == MagickSignature);
646 if (image->debug != MagickFalse)
647 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
648 if (image->storage_class == PseudoClass)
657 for (i=0; i < (ssize_t) image->colors; i++)
659 q->red=ClampToUnsignedQuantum(q->red);
660 q->green=ClampToUnsignedQuantum(q->green);
661 q->blue=ClampToUnsignedQuantum(q->blue);
662 q->alpha=ClampToUnsignedQuantum(q->alpha);
665 return(SyncImage(image));
672 exception=(&image->exception);
673 image_view=AcquireCacheView(image);
674 #if defined(MAGICKCORE_OPENMP_SUPPORT)
675 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
677 for (y=0; y < (ssize_t) image->rows; y++)
685 if (status == MagickFalse)
687 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
688 if (q == (Quantum *) NULL)
693 for (x=0; x < (ssize_t) image->columns; x++)
698 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
703 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
704 if (traits == UndefinedPixelTrait)
706 q[i]=ClampToUnsignedQuantum(q[i]);
708 q+=GetPixelChannels(image);
710 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
712 if (image->progress_monitor != (MagickProgressMonitor) NULL)
717 #if defined(MAGICKCORE_OPENMP_SUPPORT)
718 #pragma omp critical (MagickCore_ClampImage)
720 proceed=SetImageProgress(image,ClampImageTag,progress++,
722 if (proceed == MagickFalse)
726 image_view=DestroyCacheView(image_view);
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % D e s t r o y T h r e s h o l d M a p %
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741 % DestroyThresholdMap() de-allocate the given ThresholdMap
743 % The format of the ListThresholdMaps method is:
745 % ThresholdMap *DestroyThresholdMap(Threshold *map)
747 % A description of each parameter follows.
749 % o map: Pointer to the Threshold map to destroy
752 MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
754 assert(map != (ThresholdMap *) NULL);
755 if (map->map_id != (char *) NULL)
756 map->map_id=DestroyString(map->map_id);
757 if (map->description != (char *) NULL)
758 map->description=DestroyString(map->description);
759 if (map->levels != (ssize_t *) NULL)
760 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
761 map=(ThresholdMap *) RelinquishMagickMemory(map);
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 % G e t T h r e s h o l d M a p %
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % GetThresholdMap() loads and searches one or more threshold map files for the
777 % map matching the given name or alias.
779 % The format of the GetThresholdMap method is:
781 % ThresholdMap *GetThresholdMap(const char *map_id,
782 % ExceptionInfo *exception)
784 % A description of each parameter follows.
786 % o map_id: ID of the map to look for.
788 % o exception: return any errors or warnings in this structure.
791 MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
792 ExceptionInfo *exception)
803 map=(ThresholdMap *)NULL;
804 options=GetConfigureOptions(ThresholdsFilename,exception);
805 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
806 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
807 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
808 GetStringInfoPath(option),map_id,exception);
809 options=DestroyConfigureOptions(options);
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 + G e t T h r e s h o l d M a p F i l e %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % GetThresholdMapFile() look for a given threshold map name or alias in the
825 % given XML file data, and return the allocated the map when found.
827 % The format of the ListThresholdMaps method is:
829 % ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
830 % const char *map_id,ExceptionInfo *exception)
832 % A description of each parameter follows.
834 % o xml: The threshold map list in XML format.
836 % o filename: The threshold map XML filename.
838 % o map_id: ID of the map to look for in XML list.
840 % o exception: return any errors or warnings in this structure.
843 static ThresholdMap *GetThresholdMapFile(const char *xml,
844 const char *filename,const char *map_id,ExceptionInfo *exception)
868 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
869 "Loading threshold map file \"%s\" ...",filename);
870 map=(ThresholdMap *) NULL;
871 thresholds=NewXMLTree(xml,exception);
872 if (thresholds == (XMLTreeInfo *) NULL)
874 for (threshold=GetXMLTreeChild(thresholds,"threshold");
875 threshold != (XMLTreeInfo *) NULL;
876 threshold=GetNextXMLTreeTag(threshold))
878 attribute=GetXMLTreeAttribute(threshold,"map");
879 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
881 attribute=GetXMLTreeAttribute(threshold,"alias");
882 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
885 if (threshold == (XMLTreeInfo *) NULL)
887 description=GetXMLTreeChild(threshold,"description");
888 if (description == (XMLTreeInfo *) NULL)
890 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
891 "XmlMissingElement", "<description>, map \"%s\"",map_id);
892 thresholds=DestroyXMLTree(thresholds);
895 levels=GetXMLTreeChild(threshold,"levels");
896 if (levels == (XMLTreeInfo *) NULL)
898 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
899 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
900 thresholds=DestroyXMLTree(thresholds);
903 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
904 if (map == (ThresholdMap *) NULL)
905 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
906 map->map_id=(char *) NULL;
907 map->description=(char *) NULL;
908 map->levels=(ssize_t *) NULL;
909 attribute=GetXMLTreeAttribute(threshold,"map");
910 if (attribute != (char *) NULL)
911 map->map_id=ConstantString(attribute);
912 content=GetXMLTreeContent(description);
913 if (content != (char *) NULL)
914 map->description=ConstantString(content);
915 attribute=GetXMLTreeAttribute(levels,"width");
916 if (attribute == (char *) NULL)
918 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
919 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
920 thresholds=DestroyXMLTree(thresholds);
921 map=DestroyThresholdMap(map);
924 map->width=StringToUnsignedLong(attribute);
927 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
928 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
929 thresholds=DestroyXMLTree(thresholds);
930 map=DestroyThresholdMap(map);
933 attribute=GetXMLTreeAttribute(levels,"height");
934 if (attribute == (char *) NULL)
936 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
937 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
938 thresholds=DestroyXMLTree(thresholds);
939 map=DestroyThresholdMap(map);
942 map->height=StringToUnsignedLong(attribute);
943 if (map->height == 0)
945 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
946 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
947 thresholds=DestroyXMLTree(thresholds);
948 map=DestroyThresholdMap(map);
951 attribute=GetXMLTreeAttribute(levels,"divisor");
952 if (attribute == (char *) NULL)
954 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
955 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
956 thresholds=DestroyXMLTree(thresholds);
957 map=DestroyThresholdMap(map);
960 map->divisor=(ssize_t) StringToLong(attribute);
961 if (map->divisor < 2)
963 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
964 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
965 thresholds=DestroyXMLTree(thresholds);
966 map=DestroyThresholdMap(map);
969 content=GetXMLTreeContent(levels);
970 if (content == (char *) NULL)
972 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
973 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
974 thresholds=DestroyXMLTree(thresholds);
975 map=DestroyThresholdMap(map);
978 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
979 sizeof(*map->levels));
980 if (map->levels == (ssize_t *) NULL)
981 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
982 for (i=0; i < (ssize_t) (map->width*map->height); i++)
984 map->levels[i]=(ssize_t) strtol(content,&p,10);
987 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
988 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
989 thresholds=DestroyXMLTree(thresholds);
990 map=DestroyThresholdMap(map);
993 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
995 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
996 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
997 (double) map->levels[i],map_id);
998 thresholds=DestroyXMLTree(thresholds);
999 map=DestroyThresholdMap(map);
1004 value=(double) strtol(content,&p,10);
1008 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1009 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1010 thresholds=DestroyXMLTree(thresholds);
1011 map=DestroyThresholdMap(map);
1014 thresholds=DestroyXMLTree(thresholds);
1019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 + L i s t T h r e s h o l d M a p F i l e %
1027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029 % ListThresholdMapFile() lists the threshold maps and their descriptions
1030 % in the given XML file data.
1032 % The format of the ListThresholdMaps method is:
1034 % MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1035 % const char *filename,ExceptionInfo *exception)
1037 % A description of each parameter follows.
1039 % o file: An pointer to the output FILE.
1041 % o xml: The threshold map list in XML format.
1043 % o filename: The threshold map XML filename.
1045 % o exception: return any errors or warnings in this structure.
1048 MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1049 const char *filename,ExceptionInfo *exception)
1061 assert( xml != (char *)NULL );
1062 assert( file != (FILE *)NULL );
1063 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1064 "Loading threshold map file \"%s\" ...",filename);
1065 thresholds=NewXMLTree(xml,exception);
1066 if ( thresholds == (XMLTreeInfo *)NULL )
1067 return(MagickFalse);
1068 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1069 (void) FormatLocaleFile(file,
1070 "----------------------------------------------------\n");
1071 threshold=GetXMLTreeChild(thresholds,"threshold");
1072 for ( ; threshold != (XMLTreeInfo *) NULL;
1073 threshold=GetNextXMLTreeTag(threshold))
1075 map=GetXMLTreeAttribute(threshold,"map");
1076 if (map == (char *) NULL)
1078 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1079 "XmlMissingAttribute", "<map>");
1080 thresholds=DestroyXMLTree(thresholds);
1081 return(MagickFalse);
1083 alias=GetXMLTreeAttribute(threshold,"alias");
1084 description=GetXMLTreeChild(threshold,"description");
1085 if (description == (XMLTreeInfo *) NULL)
1087 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1088 "XmlMissingElement", "<description>, map \"%s\"",map);
1089 thresholds=DestroyXMLTree(thresholds);
1090 return(MagickFalse);
1092 content=GetXMLTreeContent(description);
1093 if (content == (char *) NULL)
1095 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1096 "XmlMissingContent", "<description>, map \"%s\"", map);
1097 thresholds=DestroyXMLTree(thresholds);
1098 return(MagickFalse);
1100 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1103 thresholds=DestroyXMLTree(thresholds);
1108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1112 % L i s t T h r e s h o l d M a p s %
1116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1118 % ListThresholdMaps() lists the threshold maps and their descriptions
1119 % as defined by "threshold.xml" to a file.
1121 % The format of the ListThresholdMaps method is:
1123 % MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1125 % A description of each parameter follows.
1127 % o file: An pointer to the output FILE.
1129 % o exception: return any errors or warnings in this structure.
1132 MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1133 ExceptionInfo *exception)
1145 if (file == (FILE *) NULL)
1147 options=GetConfigureOptions(ThresholdsFilename,exception);
1148 (void) FormatLocaleFile(file,
1149 "\n Threshold Maps for Ordered Dither Operations\n");
1150 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1151 (const StringInfo *) NULL)
1153 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
1154 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1155 GetStringInfoPath(option),exception);
1157 options=DestroyConfigureOptions(options);
1158 return(status != 0 ? MagickTrue : MagickFalse);
1162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166 % O r d e r e d P o s t e r i z e I m a g e %
1170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 % OrderedPosterizeImage() will perform a ordered dither based on a number
1173 % of pre-defined dithering threshold maps, but over multiple intensity
1174 % levels, which can be different for different channels, according to the
1177 % The format of the OrderedPosterizeImage method is:
1179 % MagickBooleanType OrderedPosterizeImage(Image *image,
1180 % const char *threshold_map,ExceptionInfo *exception)
1182 % A description of each parameter follows:
1184 % o image: the image.
1186 % o threshold_map: A string containing the name of the threshold dither
1187 % map to use, followed by zero or more numbers representing the number
1188 % of color levels tho dither between.
1190 % Any level number less than 2 will be equivalent to 2, and means only
1191 % binary dithering will be applied to each color channel.
1193 % No numbers also means a 2 level (bitmap) dither will be applied to all
1194 % channels, while a single number is the number of levels applied to each
1195 % channel in sequence. More numbers will be applied in turn to each of
1196 % the color channels.
1198 % For example: "o3x3,6" will generate a 6 level posterization of the
1199 % image with a ordered 3x3 diffused pixel dither being applied between
1200 % each level. While checker,8,8,4 will produce a 332 colormaped image
1201 % with only a single checkerboard hash pattern (50% grey) between each
1202 % color level, to basically double the number of color levels with
1203 % a bare minimim of dithering.
1205 % o exception: return any errors or warnings in this structure.
1208 MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1209 const char *threshold_map,ExceptionInfo *exception)
1211 #define DitherImageTag "Dither/Image"
1217 token[MaxTextExtent];
1229 levels[MaxPixelChannels];
1240 assert(image != (Image *) NULL);
1241 assert(image->signature == MagickSignature);
1242 if (image->debug != MagickFalse)
1243 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1244 assert(exception != (ExceptionInfo *) NULL);
1245 assert(exception->signature == MagickSignature);
1246 if (threshold_map == (const char *) NULL)
1248 p=(char *) threshold_map;
1249 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1253 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1256 if ((p-threshold_map) >= (MaxTextExtent-1))
1258 token[p-threshold_map]=(*p);
1261 token[p-threshold_map]='\0';
1262 map=GetThresholdMap(token,exception);
1263 if (map == (ThresholdMap *) NULL)
1265 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1266 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
1267 return(MagickFalse);
1269 for (i=0; i < MaxPixelChannels; i++)
1271 p=strchr((char *) threshold_map,',');
1272 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1273 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1275 GetMagickToken(p,&p,token);
1277 GetMagickToken(p,&p,token);
1278 levels[i]=InterpretLocaleValue(token,(char **) NULL);
1280 for (i=0; i < MaxPixelChannels; i++)
1281 if (fabs(levels[i]) >= 1)
1283 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1284 return(MagickFalse);
1287 image_view=AcquireCacheView(image);
1288 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1289 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1291 for (y=0; y < (ssize_t) image->rows; y++)
1299 if (status == MagickFalse)
1301 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1302 if (q == (Quantum *) NULL)
1307 for (x=0; x < (ssize_t) image->columns; x++)
1316 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1325 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1326 if ((traits & UpdatePixelTrait) == 0)
1328 if (fabs(levels[n++]) < MagickEpsilon)
1330 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1331 level=threshold/(map->divisor-1);
1332 threshold-=level*(map->divisor-1);
1333 q[i]=RoundToQuantum((level+(threshold >= map->levels[(x % map->width)+
1334 map->width*(y % map->height)]))*QuantumRange/levels[n]);
1337 q+=GetPixelChannels(image);
1339 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1341 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1346 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1347 #pragma omp critical (MagickCore_OrderedPosterizeImage)
1349 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1350 if (proceed == MagickFalse)
1354 image_view=DestroyCacheView(image_view);
1355 map=DestroyThresholdMap(map);
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 % R a n d o m T h r e s h o l d I m a g e %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % RandomThresholdImage() changes the value of individual pixels based on the
1371 % intensity of each pixel compared to a random threshold. The result is a
1372 % low-contrast, two color image.
1374 % The format of the RandomThresholdImage method is:
1376 % MagickBooleanType RandomThresholdImage(Image *image,
1377 % const char *thresholds,ExceptionInfo *exception)
1379 % A description of each parameter follows:
1381 % o image: the image.
1383 % o thresholds: a geometry string containing low,high thresholds. If the
1384 % string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1385 % is performed instead.
1387 % o exception: return any errors or warnings in this structure.
1390 MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1391 const char *thresholds,ExceptionInfo *exception)
1393 #define ThresholdImageTag "Threshold/Image"
1418 **restrict random_info;
1423 assert(image != (Image *) NULL);
1424 assert(image->signature == MagickSignature);
1425 if (image->debug != MagickFalse)
1426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1427 assert(exception != (ExceptionInfo *) NULL);
1428 assert(exception->signature == MagickSignature);
1429 if (thresholds == (const char *) NULL)
1431 GetPixelInfo(image,&threshold);
1433 max_threshold=(MagickRealType) QuantumRange;
1434 flags=ParseGeometry(thresholds,&geometry_info);
1435 min_threshold=geometry_info.rho;
1436 max_threshold=geometry_info.sigma;
1437 if ((flags & SigmaValue) == 0)
1438 max_threshold=min_threshold;
1439 if (strchr(thresholds,'%') != (char *) NULL)
1441 max_threshold*=(MagickRealType) (0.01*QuantumRange);
1442 min_threshold*=(MagickRealType) (0.01*QuantumRange);
1445 Random threshold image.
1449 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1450 return(MagickFalse);
1451 random_info=AcquireRandomInfoThreadSet();
1452 image_view=AcquireCacheView(image);
1453 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1454 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1456 for (y=0; y < (ssize_t) image->rows; y++)
1459 id = GetOpenMPThreadId();
1467 if (status == MagickFalse)
1469 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1470 if (q == (Quantum *) NULL)
1475 for (x=0; x < (ssize_t) image->columns; x++)
1480 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1488 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1489 if ((traits & UpdatePixelTrait) == 0)
1491 if ((MagickRealType) q[i] < min_threshold)
1492 threshold=min_threshold;
1494 if ((MagickRealType) q[i] > max_threshold)
1495 threshold=max_threshold;
1497 threshold=(MagickRealType) (QuantumRange*
1498 GetPseudoRandomValue(random_info[id]));
1499 q[i]=(Quantum) ((MagickRealType) q[i] <= threshold ? 0 :
1502 q+=GetPixelChannels(image);
1504 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1506 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1511 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1512 #pragma omp critical (MagickCore_RandomThresholdImage)
1514 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1516 if (proceed == MagickFalse)
1520 image_view=DestroyCacheView(image_view);
1521 random_info=DestroyRandomInfoThreadSet(random_info);
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1530 % W h i t e T h r e s h o l d I m a g e %
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1536 % WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
1537 % the threshold into white while leaving all pixels at or below the threshold
1540 % The format of the WhiteThresholdImage method is:
1542 % MagickBooleanType WhiteThresholdImage(Image *image,
1543 % const char *threshold,ExceptionInfo *exception)
1545 % A description of each parameter follows:
1547 % o image: the image.
1549 % o threshold: Define the threshold value.
1551 % o exception: return any errors or warnings in this structure.
1554 MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
1555 const char *thresholds,ExceptionInfo *exception)
1557 #define ThresholdImageTag "Threshold/Image"
1583 assert(image != (Image *) NULL);
1584 assert(image->signature == MagickSignature);
1585 if (image->debug != MagickFalse)
1586 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1587 if (thresholds == (const char *) NULL)
1589 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1590 return(MagickFalse);
1591 flags=ParseGeometry(thresholds,&geometry_info);
1592 for (i=0; i < 5; i++)
1593 threshold[i]=geometry_info.rho;
1594 if ((flags & SigmaValue) != 0)
1595 threshold[1]=geometry_info.sigma;
1596 if ((flags & XiValue) != 0)
1597 threshold[2]=geometry_info.xi;
1598 if ((flags & PsiValue) != 0)
1599 threshold[3]=geometry_info.psi;
1600 if ((flags & ChiValue) != 0)
1601 threshold[4]=geometry_info.chi;
1602 if ((flags & PercentValue) != 0)
1603 for (i=0; i < 5; i++)
1604 threshold[i]*=(QuantumRange/100.0);
1606 White threshold image.
1610 image_view=AcquireCacheView(image);
1611 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1612 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1614 for (y=0; y < (ssize_t) image->rows; y++)
1622 if (status == MagickFalse)
1624 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1625 if (q == (Quantum *) NULL)
1630 for (x=0; x < (ssize_t) image->columns; x++)
1639 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1644 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1645 if ((traits & UpdatePixelTrait) == 0)
1647 if ((MagickRealType) q[i] > threshold[n++ % 5])
1650 q+=GetPixelChannels(image);
1652 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1654 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1659 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1660 #pragma omp critical (MagickCore_WhiteThresholdImage)
1662 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1664 if (proceed == MagickFalse)
1668 image_view=DestroyCacheView(image_view);