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,i);
264 channel=GetPixelChannelMapChannel(image,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,
343 % ExceptionInfo *exception)
345 % A description of each parameter follows:
347 % o image: the image.
349 % o threshold: define the threshold values.
351 % o exception: return any errors or warnings in this structure.
353 % Aside: You can get the same results as operator using LevelImages()
354 % with the 'threshold' value for both the black_point and the white_point.
357 MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold,
358 ExceptionInfo *exception)
360 #define ThresholdImageTag "Threshold/Image"
374 assert(image != (Image *) NULL);
375 assert(image->signature == MagickSignature);
376 if (image->debug != MagickFalse)
377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
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,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,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,ExceptionInfo *exception)
606 % A description of each parameter follows:
608 % o image: the image.
610 % o exception: return any errors or warnings in this structure.
614 static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
616 #if defined(MAGICKCORE_HDRI_SUPPORT)
619 if (quantum >= QuantumRange)
620 return(QuantumRange);
627 MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
629 #define ClampImageTag "Clamp/Image"
643 assert(image != (Image *) NULL);
644 assert(image->signature == MagickSignature);
645 if (image->debug != MagickFalse)
646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
647 if (image->storage_class == PseudoClass)
656 for (i=0; i < (ssize_t) image->colors; i++)
658 q->red=ClampToUnsignedQuantum(q->red);
659 q->green=ClampToUnsignedQuantum(q->green);
660 q->blue=ClampToUnsignedQuantum(q->blue);
661 q->alpha=ClampToUnsignedQuantum(q->alpha);
664 return(SyncImage(image,exception));
671 image_view=AcquireCacheView(image);
672 #if defined(MAGICKCORE_OPENMP_SUPPORT)
673 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
675 for (y=0; y < (ssize_t) image->rows; y++)
683 if (status == MagickFalse)
685 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
686 if (q == (Quantum *) NULL)
691 for (x=0; x < (ssize_t) image->columns; x++)
696 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
701 traits=GetPixelChannelMapTraits(image,i);
702 if (traits == UndefinedPixelTrait)
704 q[i]=ClampToUnsignedQuantum(q[i]);
706 q+=GetPixelChannels(image);
708 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
710 if (image->progress_monitor != (MagickProgressMonitor) NULL)
715 #if defined(MAGICKCORE_OPENMP_SUPPORT)
716 #pragma omp critical (MagickCore_ClampImage)
718 proceed=SetImageProgress(image,ClampImageTag,progress++,
720 if (proceed == MagickFalse)
724 image_view=DestroyCacheView(image_view);
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % D e s t r o y T h r e s h o l d M a p %
737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 % DestroyThresholdMap() de-allocate the given ThresholdMap
741 % The format of the ListThresholdMaps method is:
743 % ThresholdMap *DestroyThresholdMap(Threshold *map)
745 % A description of each parameter follows.
747 % o map: Pointer to the Threshold map to destroy
750 MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
752 assert(map != (ThresholdMap *) NULL);
753 if (map->map_id != (char *) NULL)
754 map->map_id=DestroyString(map->map_id);
755 if (map->description != (char *) NULL)
756 map->description=DestroyString(map->description);
757 if (map->levels != (ssize_t *) NULL)
758 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
759 map=(ThresholdMap *) RelinquishMagickMemory(map);
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % G e t T h r e s h o l d M a p %
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % GetThresholdMap() loads and searches one or more threshold map files for the
775 % map matching the given name or alias.
777 % The format of the GetThresholdMap method is:
779 % ThresholdMap *GetThresholdMap(const char *map_id,
780 % ExceptionInfo *exception)
782 % A description of each parameter follows.
784 % o map_id: ID of the map to look for.
786 % o exception: return any errors or warnings in this structure.
789 MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
790 ExceptionInfo *exception)
801 map=(ThresholdMap *)NULL;
802 options=GetConfigureOptions(ThresholdsFilename,exception);
803 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
804 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
805 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
806 GetStringInfoPath(option),map_id,exception);
807 options=DestroyConfigureOptions(options);
812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 + G e t T h r e s h o l d M a p F i l e %
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 % GetThresholdMapFile() look for a given threshold map name or alias in the
823 % given XML file data, and return the allocated the map when found.
825 % The format of the ListThresholdMaps method is:
827 % ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
828 % const char *map_id,ExceptionInfo *exception)
830 % A description of each parameter follows.
832 % o xml: The threshold map list in XML format.
834 % o filename: The threshold map XML filename.
836 % o map_id: ID of the map to look for in XML list.
838 % o exception: return any errors or warnings in this structure.
841 static ThresholdMap *GetThresholdMapFile(const char *xml,
842 const char *filename,const char *map_id,ExceptionInfo *exception)
866 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
867 "Loading threshold map file \"%s\" ...",filename);
868 map=(ThresholdMap *) NULL;
869 thresholds=NewXMLTree(xml,exception);
870 if (thresholds == (XMLTreeInfo *) NULL)
872 for (threshold=GetXMLTreeChild(thresholds,"threshold");
873 threshold != (XMLTreeInfo *) NULL;
874 threshold=GetNextXMLTreeTag(threshold))
876 attribute=GetXMLTreeAttribute(threshold,"map");
877 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
879 attribute=GetXMLTreeAttribute(threshold,"alias");
880 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
883 if (threshold == (XMLTreeInfo *) NULL)
885 description=GetXMLTreeChild(threshold,"description");
886 if (description == (XMLTreeInfo *) NULL)
888 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
889 "XmlMissingElement", "<description>, map \"%s\"",map_id);
890 thresholds=DestroyXMLTree(thresholds);
893 levels=GetXMLTreeChild(threshold,"levels");
894 if (levels == (XMLTreeInfo *) NULL)
896 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
897 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
898 thresholds=DestroyXMLTree(thresholds);
901 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
902 if (map == (ThresholdMap *) NULL)
903 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
904 map->map_id=(char *) NULL;
905 map->description=(char *) NULL;
906 map->levels=(ssize_t *) NULL;
907 attribute=GetXMLTreeAttribute(threshold,"map");
908 if (attribute != (char *) NULL)
909 map->map_id=ConstantString(attribute);
910 content=GetXMLTreeContent(description);
911 if (content != (char *) NULL)
912 map->description=ConstantString(content);
913 attribute=GetXMLTreeAttribute(levels,"width");
914 if (attribute == (char *) NULL)
916 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
917 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
918 thresholds=DestroyXMLTree(thresholds);
919 map=DestroyThresholdMap(map);
922 map->width=StringToUnsignedLong(attribute);
925 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
926 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
927 thresholds=DestroyXMLTree(thresholds);
928 map=DestroyThresholdMap(map);
931 attribute=GetXMLTreeAttribute(levels,"height");
932 if (attribute == (char *) NULL)
934 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
935 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
936 thresholds=DestroyXMLTree(thresholds);
937 map=DestroyThresholdMap(map);
940 map->height=StringToUnsignedLong(attribute);
941 if (map->height == 0)
943 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
944 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
945 thresholds=DestroyXMLTree(thresholds);
946 map=DestroyThresholdMap(map);
949 attribute=GetXMLTreeAttribute(levels,"divisor");
950 if (attribute == (char *) NULL)
952 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
953 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
954 thresholds=DestroyXMLTree(thresholds);
955 map=DestroyThresholdMap(map);
958 map->divisor=(ssize_t) StringToLong(attribute);
959 if (map->divisor < 2)
961 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
962 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
963 thresholds=DestroyXMLTree(thresholds);
964 map=DestroyThresholdMap(map);
967 content=GetXMLTreeContent(levels);
968 if (content == (char *) NULL)
970 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
971 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
972 thresholds=DestroyXMLTree(thresholds);
973 map=DestroyThresholdMap(map);
976 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
977 sizeof(*map->levels));
978 if (map->levels == (ssize_t *) NULL)
979 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
980 for (i=0; i < (ssize_t) (map->width*map->height); i++)
982 map->levels[i]=(ssize_t) strtol(content,&p,10);
985 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
986 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
987 thresholds=DestroyXMLTree(thresholds);
988 map=DestroyThresholdMap(map);
991 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
993 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
994 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
995 (double) map->levels[i],map_id);
996 thresholds=DestroyXMLTree(thresholds);
997 map=DestroyThresholdMap(map);
1002 value=(double) strtol(content,&p,10);
1006 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1007 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1008 thresholds=DestroyXMLTree(thresholds);
1009 map=DestroyThresholdMap(map);
1012 thresholds=DestroyXMLTree(thresholds);
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1021 + L i s t T h r e s h o l d M a p F i l e %
1025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 % ListThresholdMapFile() lists the threshold maps and their descriptions
1028 % in the given XML file data.
1030 % The format of the ListThresholdMaps method is:
1032 % MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1033 % const char *filename,ExceptionInfo *exception)
1035 % A description of each parameter follows.
1037 % o file: An pointer to the output FILE.
1039 % o xml: The threshold map list in XML format.
1041 % o filename: The threshold map XML filename.
1043 % o exception: return any errors or warnings in this structure.
1046 MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1047 const char *filename,ExceptionInfo *exception)
1059 assert( xml != (char *)NULL );
1060 assert( file != (FILE *)NULL );
1061 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1062 "Loading threshold map file \"%s\" ...",filename);
1063 thresholds=NewXMLTree(xml,exception);
1064 if ( thresholds == (XMLTreeInfo *)NULL )
1065 return(MagickFalse);
1066 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1067 (void) FormatLocaleFile(file,
1068 "----------------------------------------------------\n");
1069 threshold=GetXMLTreeChild(thresholds,"threshold");
1070 for ( ; threshold != (XMLTreeInfo *) NULL;
1071 threshold=GetNextXMLTreeTag(threshold))
1073 map=GetXMLTreeAttribute(threshold,"map");
1074 if (map == (char *) NULL)
1076 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1077 "XmlMissingAttribute", "<map>");
1078 thresholds=DestroyXMLTree(thresholds);
1079 return(MagickFalse);
1081 alias=GetXMLTreeAttribute(threshold,"alias");
1082 description=GetXMLTreeChild(threshold,"description");
1083 if (description == (XMLTreeInfo *) NULL)
1085 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1086 "XmlMissingElement", "<description>, map \"%s\"",map);
1087 thresholds=DestroyXMLTree(thresholds);
1088 return(MagickFalse);
1090 content=GetXMLTreeContent(description);
1091 if (content == (char *) NULL)
1093 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1094 "XmlMissingContent", "<description>, map \"%s\"", map);
1095 thresholds=DestroyXMLTree(thresholds);
1096 return(MagickFalse);
1098 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1101 thresholds=DestroyXMLTree(thresholds);
1106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1110 % L i s t T h r e s h o l d M a p s %
1114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1116 % ListThresholdMaps() lists the threshold maps and their descriptions
1117 % as defined by "threshold.xml" to a file.
1119 % The format of the ListThresholdMaps method is:
1121 % MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1123 % A description of each parameter follows.
1125 % o file: An pointer to the output FILE.
1127 % o exception: return any errors or warnings in this structure.
1130 MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1131 ExceptionInfo *exception)
1143 if (file == (FILE *) NULL)
1145 options=GetConfigureOptions(ThresholdsFilename,exception);
1146 (void) FormatLocaleFile(file,
1147 "\n Threshold Maps for Ordered Dither Operations\n");
1148 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1149 (const StringInfo *) NULL)
1151 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
1152 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1153 GetStringInfoPath(option),exception);
1155 options=DestroyConfigureOptions(options);
1156 return(status != 0 ? MagickTrue : MagickFalse);
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164 % O r d e r e d P o s t e r i z e I m a g e %
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1170 % OrderedPosterizeImage() will perform a ordered dither based on a number
1171 % of pre-defined dithering threshold maps, but over multiple intensity
1172 % levels, which can be different for different channels, according to the
1175 % The format of the OrderedPosterizeImage method is:
1177 % MagickBooleanType OrderedPosterizeImage(Image *image,
1178 % const char *threshold_map,ExceptionInfo *exception)
1180 % A description of each parameter follows:
1182 % o image: the image.
1184 % o threshold_map: A string containing the name of the threshold dither
1185 % map to use, followed by zero or more numbers representing the number
1186 % of color levels tho dither between.
1188 % Any level number less than 2 will be equivalent to 2, and means only
1189 % binary dithering will be applied to each color channel.
1191 % No numbers also means a 2 level (bitmap) dither will be applied to all
1192 % channels, while a single number is the number of levels applied to each
1193 % channel in sequence. More numbers will be applied in turn to each of
1194 % the color channels.
1196 % For example: "o3x3,6" will generate a 6 level posterization of the
1197 % image with a ordered 3x3 diffused pixel dither being applied between
1198 % each level. While checker,8,8,4 will produce a 332 colormaped image
1199 % with only a single checkerboard hash pattern (50% grey) between each
1200 % color level, to basically double the number of color levels with
1201 % a bare minimim of dithering.
1203 % o exception: return any errors or warnings in this structure.
1206 MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1207 const char *threshold_map,ExceptionInfo *exception)
1209 #define DitherImageTag "Dither/Image"
1215 token[MaxTextExtent];
1227 levels[CompositePixelChannel];
1238 assert(image != (Image *) NULL);
1239 assert(image->signature == MagickSignature);
1240 if (image->debug != MagickFalse)
1241 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1242 assert(exception != (ExceptionInfo *) NULL);
1243 assert(exception->signature == MagickSignature);
1244 if (threshold_map == (const char *) NULL)
1246 p=(char *) threshold_map;
1247 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1251 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1254 if ((p-threshold_map) >= (MaxTextExtent-1))
1256 token[p-threshold_map]=(*p);
1259 token[p-threshold_map]='\0';
1260 map=GetThresholdMap(token,exception);
1261 if (map == (ThresholdMap *) NULL)
1263 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1264 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
1265 return(MagickFalse);
1267 for (i=0; i < MaxPixelChannels; i++)
1269 p=strchr((char *) threshold_map,',');
1270 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1271 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1273 GetMagickToken(p,&p,token);
1275 GetMagickToken(p,&p,token);
1276 levels[i]=StringToDouble(token,(char **) NULL);
1278 for (i=0; i < MaxPixelChannels; i++)
1279 if (fabs(levels[i]) >= 1)
1281 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1282 return(MagickFalse);
1285 image_view=AcquireCacheView(image);
1286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1287 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1289 for (y=0; y < (ssize_t) image->rows; y++)
1297 if (status == MagickFalse)
1299 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1300 if (q == (Quantum *) NULL)
1305 for (x=0; x < (ssize_t) image->columns; x++)
1314 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1323 traits=GetPixelChannelMapTraits(image,i);
1324 if ((traits & UpdatePixelTrait) == 0)
1326 if (fabs(levels[n++]) < MagickEpsilon)
1328 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1329 level=threshold/(map->divisor-1);
1330 threshold-=level*(map->divisor-1);
1331 q[i]=RoundToQuantum((level+(threshold >= map->levels[(x % map->width)+
1332 map->width*(y % map->height)]))*QuantumRange/levels[n]);
1335 q+=GetPixelChannels(image);
1337 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1339 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1344 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1345 #pragma omp critical (MagickCore_OrderedPosterizeImage)
1347 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1348 if (proceed == MagickFalse)
1352 image_view=DestroyCacheView(image_view);
1353 map=DestroyThresholdMap(map);
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362 % R a n d o m T h r e s h o l d I m a g e %
1366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 % RandomThresholdImage() changes the value of individual pixels based on the
1369 % intensity of each pixel compared to a random threshold. The result is a
1370 % low-contrast, two color image.
1372 % The format of the RandomThresholdImage method is:
1374 % MagickBooleanType RandomThresholdImage(Image *image,
1375 % const char *thresholds,ExceptionInfo *exception)
1377 % A description of each parameter follows:
1379 % o image: the image.
1381 % o thresholds: a geometry string containing low,high thresholds. If the
1382 % string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1383 % is performed instead.
1385 % o exception: return any errors or warnings in this structure.
1388 MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1389 const char *thresholds,ExceptionInfo *exception)
1391 #define ThresholdImageTag "Threshold/Image"
1416 **restrict random_info;
1421 assert(image != (Image *) NULL);
1422 assert(image->signature == MagickSignature);
1423 if (image->debug != MagickFalse)
1424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1425 assert(exception != (ExceptionInfo *) NULL);
1426 assert(exception->signature == MagickSignature);
1427 if (thresholds == (const char *) NULL)
1429 GetPixelInfo(image,&threshold);
1431 max_threshold=(MagickRealType) QuantumRange;
1432 flags=ParseGeometry(thresholds,&geometry_info);
1433 min_threshold=geometry_info.rho;
1434 max_threshold=geometry_info.sigma;
1435 if ((flags & SigmaValue) == 0)
1436 max_threshold=min_threshold;
1437 if (strchr(thresholds,'%') != (char *) NULL)
1439 max_threshold*=(MagickRealType) (0.01*QuantumRange);
1440 min_threshold*=(MagickRealType) (0.01*QuantumRange);
1443 Random threshold image.
1447 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1448 return(MagickFalse);
1449 random_info=AcquireRandomInfoThreadSet();
1450 image_view=AcquireCacheView(image);
1451 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1452 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1454 for (y=0; y < (ssize_t) image->rows; y++)
1457 id = GetOpenMPThreadId();
1465 if (status == MagickFalse)
1467 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1468 if (q == (Quantum *) NULL)
1473 for (x=0; x < (ssize_t) image->columns; x++)
1478 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1486 traits=GetPixelChannelMapTraits(image,i);
1487 if ((traits & UpdatePixelTrait) == 0)
1489 if ((MagickRealType) q[i] < min_threshold)
1490 threshold=min_threshold;
1492 if ((MagickRealType) q[i] > max_threshold)
1493 threshold=max_threshold;
1495 threshold=(MagickRealType) (QuantumRange*
1496 GetPseudoRandomValue(random_info[id]));
1497 q[i]=(Quantum) ((MagickRealType) q[i] <= threshold ? 0 :
1500 q+=GetPixelChannels(image);
1502 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1504 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1509 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1510 #pragma omp critical (MagickCore_RandomThresholdImage)
1512 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1514 if (proceed == MagickFalse)
1518 image_view=DestroyCacheView(image_view);
1519 random_info=DestroyRandomInfoThreadSet(random_info);
1524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 % W h i t e T h r e s h o l d I m a g e %
1532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1534 % WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
1535 % the threshold into white while leaving all pixels at or below the threshold
1538 % The format of the WhiteThresholdImage method is:
1540 % MagickBooleanType WhiteThresholdImage(Image *image,
1541 % const char *threshold,ExceptionInfo *exception)
1543 % A description of each parameter follows:
1545 % o image: the image.
1547 % o threshold: Define the threshold value.
1549 % o exception: return any errors or warnings in this structure.
1552 MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
1553 const char *thresholds,ExceptionInfo *exception)
1555 #define ThresholdImageTag "Threshold/Image"
1581 assert(image != (Image *) NULL);
1582 assert(image->signature == MagickSignature);
1583 if (image->debug != MagickFalse)
1584 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1585 if (thresholds == (const char *) NULL)
1587 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1588 return(MagickFalse);
1589 flags=ParseGeometry(thresholds,&geometry_info);
1590 for (i=0; i < 5; i++)
1591 threshold[i]=geometry_info.rho;
1592 if ((flags & SigmaValue) != 0)
1593 threshold[1]=geometry_info.sigma;
1594 if ((flags & XiValue) != 0)
1595 threshold[2]=geometry_info.xi;
1596 if ((flags & PsiValue) != 0)
1597 threshold[3]=geometry_info.psi;
1598 if ((flags & ChiValue) != 0)
1599 threshold[4]=geometry_info.chi;
1600 if ((flags & PercentValue) != 0)
1601 for (i=0; i < 5; i++)
1602 threshold[i]*=(QuantumRange/100.0);
1604 White threshold image.
1608 image_view=AcquireCacheView(image);
1609 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1610 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1612 for (y=0; y < (ssize_t) image->rows; y++)
1620 if (status == MagickFalse)
1622 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1623 if (q == (Quantum *) NULL)
1628 for (x=0; x < (ssize_t) image->columns; x++)
1637 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1642 traits=GetPixelChannelMapTraits(image,i);
1643 if ((traits & UpdatePixelTrait) == 0)
1645 if ((MagickRealType) q[i] > threshold[n++ % 5])
1648 q+=GetPixelChannels(image);
1650 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1652 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1657 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1658 #pragma omp critical (MagickCore_WhiteThresholdImage)
1660 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1662 if (proceed == MagickFalse)
1666 image_view=DestroyCacheView(image_view);