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++)
419 if (GetPixelMask(image,q) != 0)
421 q+=GetPixelChannels(image);
424 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
432 channel=GetPixelChannelMapChannel(image,i);
433 traits=GetPixelChannelMapTraits(image,channel);
434 if ((traits & UpdatePixelTrait) == 0)
436 q[i]=(Quantum) ((MagickRealType) q[i] <= threshold ? 0 : QuantumRange);
438 q+=GetPixelChannels(image);
440 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
442 if (image->progress_monitor != (MagickProgressMonitor) NULL)
447 #if defined(MAGICKCORE_OPENMP_SUPPORT)
448 #pragma omp critical (MagickCore_BilevelImage)
450 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
452 if (proceed == MagickFalse)
456 image_view=DestroyCacheView(image_view);
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 % B l a c k T h r e s h o l d I m a g e %
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 % BlackThresholdImage() is like ThresholdImage() but forces all pixels below
472 % the threshold into black while leaving all pixels at or above the threshold
475 % The format of the BlackThresholdImage method is:
477 % MagickBooleanType BlackThresholdImage(Image *image,
478 % const char *threshold,ExceptionInfo *exception)
480 % A description of each parameter follows:
482 % o image: the image.
484 % o threshold: define the threshold value.
486 % o exception: return any errors or warnings in this structure.
489 MagickExport MagickBooleanType BlackThresholdImage(Image *image,
490 const char *thresholds,ExceptionInfo *exception)
492 #define ThresholdImageTag "Threshold/Image"
515 assert(image != (Image *) NULL);
516 assert(image->signature == MagickSignature);
517 if (image->debug != MagickFalse)
518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
519 if (thresholds == (const char *) NULL)
521 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
523 if (IsGrayColorspace(image->colorspace) != MagickFalse)
524 (void) TransformImageColorspace(image,sRGBColorspace,exception);
525 GetPixelInfo(image,&threshold);
526 flags=ParseGeometry(thresholds,&geometry_info);
527 threshold.red=geometry_info.rho;
528 threshold.green=geometry_info.rho;
529 threshold.blue=geometry_info.rho;
530 threshold.black=geometry_info.rho;
531 threshold.alpha=100.0;
532 if ((flags & SigmaValue) != 0)
533 threshold.green=geometry_info.sigma;
534 if ((flags & XiValue) != 0)
535 threshold.blue=geometry_info.xi;
536 if ((flags & PsiValue) != 0)
537 threshold.alpha=geometry_info.psi;
538 if (threshold.colorspace == CMYKColorspace)
540 if ((flags & PsiValue) != 0)
541 threshold.black=geometry_info.psi;
542 if ((flags & ChiValue) != 0)
543 threshold.alpha=geometry_info.chi;
545 if ((flags & PercentValue) != 0)
547 threshold.red*=(QuantumRange/100.0);
548 threshold.green*=(QuantumRange/100.0);
549 threshold.blue*=(QuantumRange/100.0);
550 threshold.black*=(QuantumRange/100.0);
551 threshold.alpha*=(QuantumRange/100.0);
554 White threshold image.
558 image_view=AcquireAuthenticCacheView(image,exception);
559 #if defined(MAGICKCORE_OPENMP_SUPPORT)
560 #pragma omp parallel for schedule(static,8) shared(progress,status) \
561 dynamic_number_threads(image,image->columns,image->rows,1)
563 for (y=0; y < (ssize_t) image->rows; y++)
571 if (status == MagickFalse)
573 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
574 if (q == (Quantum *) NULL)
579 for (x=0; x < (ssize_t) image->columns; x++)
581 if (GetPixelMask(image,q) != 0)
583 q+=GetPixelChannels(image);
586 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
594 channel=GetPixelChannelMapChannel(image,i);
595 traits=GetPixelChannelMapTraits(image,channel);
596 if ((traits & UpdatePixelTrait) == 0)
598 if ((double) q[i] <= GetPixelInfoChannel(&threshold,channel))
601 q+=GetPixelChannels(image);
603 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
605 if (image->progress_monitor != (MagickProgressMonitor) NULL)
610 #if defined(MAGICKCORE_OPENMP_SUPPORT)
611 #pragma omp critical (MagickCore_BlackThresholdImage)
613 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
615 if (proceed == MagickFalse)
619 image_view=DestroyCacheView(image_view);
624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 % C l a m p I m a g e %
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % ClampImage() restricts the color range from 0 to the quantum depth.
636 % The format of the ClampImage method is:
638 % MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
640 % A description of each parameter follows:
642 % o image: the image.
644 % o exception: return any errors or warnings in this structure.
648 static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
650 #if defined(MAGICKCORE_HDRI_SUPPORT)
653 if (quantum >= QuantumRange)
654 return(QuantumRange);
661 MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
663 #define ClampImageTag "Clamp/Image"
677 assert(image != (Image *) NULL);
678 assert(image->signature == MagickSignature);
679 if (image->debug != MagickFalse)
680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
681 if (image->storage_class == PseudoClass)
690 for (i=0; i < (ssize_t) image->colors; i++)
692 q->red=(double) ClampToUnsignedQuantum(ClampToQuantum(q->red));
693 q->green=(double) ClampToUnsignedQuantum(ClampToQuantum(q->green));
694 q->blue=(double) ClampToUnsignedQuantum(ClampToQuantum(q->blue));
695 q->alpha=(double) ClampToUnsignedQuantum(ClampToQuantum(q->alpha));
698 return(SyncImage(image,exception));
705 image_view=AcquireAuthenticCacheView(image,exception);
706 #if defined(MAGICKCORE_OPENMP_SUPPORT)
707 #pragma omp parallel for schedule(static,8) shared(progress,status) \
708 dynamic_number_threads(image,image->columns,image->rows,1)
710 for (y=0; y < (ssize_t) image->rows; y++)
718 if (status == MagickFalse)
720 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
721 if (q == (Quantum *) NULL)
726 for (x=0; x < (ssize_t) image->columns; x++)
731 if (GetPixelMask(image,q) != 0)
733 q+=GetPixelChannels(image);
736 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
744 channel=GetPixelChannelMapChannel(image,i);
745 traits=GetPixelChannelMapTraits(image,channel);
746 if (traits == UndefinedPixelTrait)
748 q[i]=ClampToUnsignedQuantum(q[i]);
750 q+=GetPixelChannels(image);
752 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
754 if (image->progress_monitor != (MagickProgressMonitor) NULL)
759 #if defined(MAGICKCORE_OPENMP_SUPPORT)
760 #pragma omp critical (MagickCore_ClampImage)
762 proceed=SetImageProgress(image,ClampImageTag,progress++,image->rows);
763 if (proceed == MagickFalse)
767 image_view=DestroyCacheView(image_view);
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % D e s t r o y T h r e s h o l d M a p %
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 % DestroyThresholdMap() de-allocate the given ThresholdMap
784 % The format of the ListThresholdMaps method is:
786 % ThresholdMap *DestroyThresholdMap(Threshold *map)
788 % A description of each parameter follows.
790 % o map: Pointer to the Threshold map to destroy
793 MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
795 assert(map != (ThresholdMap *) NULL);
796 if (map->map_id != (char *) NULL)
797 map->map_id=DestroyString(map->map_id);
798 if (map->description != (char *) NULL)
799 map->description=DestroyString(map->description);
800 if (map->levels != (ssize_t *) NULL)
801 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
802 map=(ThresholdMap *) RelinquishMagickMemory(map);
807 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % G e t T h r e s h o l d M a p %
815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 % GetThresholdMap() loads and searches one or more threshold map files for the
818 % map matching the given name or alias.
820 % The format of the GetThresholdMap method is:
822 % ThresholdMap *GetThresholdMap(const char *map_id,
823 % ExceptionInfo *exception)
825 % A description of each parameter follows.
827 % o map_id: ID of the map to look for.
829 % o exception: return any errors or warnings in this structure.
832 MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
833 ExceptionInfo *exception)
844 map=(ThresholdMap *)NULL;
845 options=GetConfigureOptions(ThresholdsFilename,exception);
846 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
847 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
848 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
849 GetStringInfoPath(option),map_id,exception);
850 options=DestroyConfigureOptions(options);
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 + G e t T h r e s h o l d M a p F i l e %
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % GetThresholdMapFile() look for a given threshold map name or alias in the
866 % given XML file data, and return the allocated the map when found.
868 % The format of the ListThresholdMaps method is:
870 % ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
871 % const char *map_id,ExceptionInfo *exception)
873 % A description of each parameter follows.
875 % o xml: The threshold map list in XML format.
877 % o filename: The threshold map XML filename.
879 % o map_id: ID of the map to look for in XML list.
881 % o exception: return any errors or warnings in this structure.
884 static ThresholdMap *GetThresholdMapFile(const char *xml,
885 const char *filename,const char *map_id,ExceptionInfo *exception)
909 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
910 "Loading threshold map file \"%s\" ...",filename);
911 map=(ThresholdMap *) NULL;
912 thresholds=NewXMLTree(xml,exception);
913 if (thresholds == (XMLTreeInfo *) NULL)
915 for (threshold=GetXMLTreeChild(thresholds,"threshold");
916 threshold != (XMLTreeInfo *) NULL;
917 threshold=GetNextXMLTreeTag(threshold))
919 attribute=GetXMLTreeAttribute(threshold,"map");
920 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
922 attribute=GetXMLTreeAttribute(threshold,"alias");
923 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
926 if (threshold == (XMLTreeInfo *) NULL)
928 description=GetXMLTreeChild(threshold,"description");
929 if (description == (XMLTreeInfo *) NULL)
931 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
932 "XmlMissingElement", "<description>, map \"%s\"",map_id);
933 thresholds=DestroyXMLTree(thresholds);
936 levels=GetXMLTreeChild(threshold,"levels");
937 if (levels == (XMLTreeInfo *) NULL)
939 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
940 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
941 thresholds=DestroyXMLTree(thresholds);
944 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
945 if (map == (ThresholdMap *) NULL)
946 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
947 map->map_id=(char *) NULL;
948 map->description=(char *) NULL;
949 map->levels=(ssize_t *) NULL;
950 attribute=GetXMLTreeAttribute(threshold,"map");
951 if (attribute != (char *) NULL)
952 map->map_id=ConstantString(attribute);
953 content=GetXMLTreeContent(description);
954 if (content != (char *) NULL)
955 map->description=ConstantString(content);
956 attribute=GetXMLTreeAttribute(levels,"width");
957 if (attribute == (char *) NULL)
959 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
960 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
961 thresholds=DestroyXMLTree(thresholds);
962 map=DestroyThresholdMap(map);
965 map->width=StringToUnsignedLong(attribute);
968 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
969 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
970 thresholds=DestroyXMLTree(thresholds);
971 map=DestroyThresholdMap(map);
974 attribute=GetXMLTreeAttribute(levels,"height");
975 if (attribute == (char *) NULL)
977 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
978 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
979 thresholds=DestroyXMLTree(thresholds);
980 map=DestroyThresholdMap(map);
983 map->height=StringToUnsignedLong(attribute);
984 if (map->height == 0)
986 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
987 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
988 thresholds=DestroyXMLTree(thresholds);
989 map=DestroyThresholdMap(map);
992 attribute=GetXMLTreeAttribute(levels,"divisor");
993 if (attribute == (char *) NULL)
995 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
996 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
997 thresholds=DestroyXMLTree(thresholds);
998 map=DestroyThresholdMap(map);
1001 map->divisor=(ssize_t) StringToLong(attribute);
1002 if (map->divisor < 2)
1004 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1005 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
1006 thresholds=DestroyXMLTree(thresholds);
1007 map=DestroyThresholdMap(map);
1010 content=GetXMLTreeContent(levels);
1011 if (content == (char *) NULL)
1013 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1014 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
1015 thresholds=DestroyXMLTree(thresholds);
1016 map=DestroyThresholdMap(map);
1019 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
1020 sizeof(*map->levels));
1021 if (map->levels == (ssize_t *) NULL)
1022 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
1023 for (i=0; i < (ssize_t) (map->width*map->height); i++)
1025 map->levels[i]=(ssize_t) strtol(content,&p,10);
1028 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1029 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
1030 thresholds=DestroyXMLTree(thresholds);
1031 map=DestroyThresholdMap(map);
1034 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
1036 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1037 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
1038 (double) map->levels[i],map_id);
1039 thresholds=DestroyXMLTree(thresholds);
1040 map=DestroyThresholdMap(map);
1045 value=(double) strtol(content,&p,10);
1049 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1050 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1051 thresholds=DestroyXMLTree(thresholds);
1052 map=DestroyThresholdMap(map);
1055 thresholds=DestroyXMLTree(thresholds);
1060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064 + L i s t T h r e s h o l d M a p F i l e %
1068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 % ListThresholdMapFile() lists the threshold maps and their descriptions
1071 % in the given XML file data.
1073 % The format of the ListThresholdMaps method is:
1075 % MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1076 % const char *filename,ExceptionInfo *exception)
1078 % A description of each parameter follows.
1080 % o file: An pointer to the output FILE.
1082 % o xml: The threshold map list in XML format.
1084 % o filename: The threshold map XML filename.
1086 % o exception: return any errors or warnings in this structure.
1089 MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1090 const char *filename,ExceptionInfo *exception)
1102 assert( xml != (char *)NULL );
1103 assert( file != (FILE *)NULL );
1104 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1105 "Loading threshold map file \"%s\" ...",filename);
1106 thresholds=NewXMLTree(xml,exception);
1107 if ( thresholds == (XMLTreeInfo *)NULL )
1108 return(MagickFalse);
1109 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1110 (void) FormatLocaleFile(file,
1111 "----------------------------------------------------\n");
1112 threshold=GetXMLTreeChild(thresholds,"threshold");
1113 for ( ; threshold != (XMLTreeInfo *) NULL;
1114 threshold=GetNextXMLTreeTag(threshold))
1116 map=GetXMLTreeAttribute(threshold,"map");
1117 if (map == (char *) NULL)
1119 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1120 "XmlMissingAttribute", "<map>");
1121 thresholds=DestroyXMLTree(thresholds);
1122 return(MagickFalse);
1124 alias=GetXMLTreeAttribute(threshold,"alias");
1125 description=GetXMLTreeChild(threshold,"description");
1126 if (description == (XMLTreeInfo *) NULL)
1128 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1129 "XmlMissingElement", "<description>, map \"%s\"",map);
1130 thresholds=DestroyXMLTree(thresholds);
1131 return(MagickFalse);
1133 content=GetXMLTreeContent(description);
1134 if (content == (char *) NULL)
1136 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1137 "XmlMissingContent", "<description>, map \"%s\"", map);
1138 thresholds=DestroyXMLTree(thresholds);
1139 return(MagickFalse);
1141 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1144 thresholds=DestroyXMLTree(thresholds);
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153 % L i s t T h r e s h o l d M a p s %
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159 % ListThresholdMaps() lists the threshold maps and their descriptions
1160 % as defined by "threshold.xml" to a file.
1162 % The format of the ListThresholdMaps method is:
1164 % MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1166 % A description of each parameter follows.
1168 % o file: An pointer to the output FILE.
1170 % o exception: return any errors or warnings in this structure.
1173 MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1174 ExceptionInfo *exception)
1186 if (file == (FILE *) NULL)
1188 options=GetConfigureOptions(ThresholdsFilename,exception);
1189 (void) FormatLocaleFile(file,
1190 "\n Threshold Maps for Ordered Dither Operations\n");
1191 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1192 (const StringInfo *) NULL)
1194 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
1195 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1196 GetStringInfoPath(option),exception);
1198 options=DestroyConfigureOptions(options);
1199 return(status != 0 ? MagickTrue : MagickFalse);
1203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207 % O r d e r e d P o s t e r i z e I m a g e %
1211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1213 % OrderedPosterizeImage() will perform a ordered dither based on a number
1214 % of pre-defined dithering threshold maps, but over multiple intensity
1215 % levels, which can be different for different channels, according to the
1218 % The format of the OrderedPosterizeImage method is:
1220 % MagickBooleanType OrderedPosterizeImage(Image *image,
1221 % const char *threshold_map,ExceptionInfo *exception)
1223 % A description of each parameter follows:
1225 % o image: the image.
1227 % o threshold_map: A string containing the name of the threshold dither
1228 % map to use, followed by zero or more numbers representing the number
1229 % of color levels tho dither between.
1231 % Any level number less than 2 will be equivalent to 2, and means only
1232 % binary dithering will be applied to each color channel.
1234 % No numbers also means a 2 level (bitmap) dither will be applied to all
1235 % channels, while a single number is the number of levels applied to each
1236 % channel in sequence. More numbers will be applied in turn to each of
1237 % the color channels.
1239 % For example: "o3x3,6" will generate a 6 level posterization of the
1240 % image with a ordered 3x3 diffused pixel dither being applied between
1241 % each level. While checker,8,8,4 will produce a 332 colormaped image
1242 % with only a single checkerboard hash pattern (50% grey) between each
1243 % color level, to basically double the number of color levels with
1244 % a bare minimim of dithering.
1246 % o exception: return any errors or warnings in this structure.
1249 MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1250 const char *threshold_map,ExceptionInfo *exception)
1252 #define DitherImageTag "Dither/Image"
1258 token[MaxTextExtent];
1270 levels[CompositePixelChannel];
1281 assert(image != (Image *) NULL);
1282 assert(image->signature == MagickSignature);
1283 if (image->debug != MagickFalse)
1284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1285 assert(exception != (ExceptionInfo *) NULL);
1286 assert(exception->signature == MagickSignature);
1287 if (threshold_map == (const char *) NULL)
1289 p=(char *) threshold_map;
1290 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1294 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1297 if ((p-threshold_map) >= (MaxTextExtent-1))
1299 token[p-threshold_map]=(*p);
1302 token[p-threshold_map]='\0';
1303 map=GetThresholdMap(token,exception);
1304 if (map == (ThresholdMap *) NULL)
1306 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1307 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
1308 return(MagickFalse);
1310 for (i=0; i < MaxPixelChannels; i++)
1312 p=strchr((char *) threshold_map,',');
1313 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1314 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1316 GetMagickToken(p,&p,token);
1318 GetMagickToken(p,&p,token);
1319 levels[i]=StringToDouble(token,(char **) NULL);
1321 for (i=0; i < MaxPixelChannels; i++)
1322 if (fabs(levels[i]) >= 1)
1324 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1325 return(MagickFalse);
1328 image_view=AcquireAuthenticCacheView(image,exception);
1329 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1330 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1331 dynamic_number_threads(image,image->columns,image->rows,1)
1333 for (y=0; y < (ssize_t) image->rows; y++)
1341 if (status == MagickFalse)
1343 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1344 if (q == (Quantum *) NULL)
1349 for (x=0; x < (ssize_t) image->columns; x++)
1358 if (GetPixelMask(image,q) != 0)
1360 q+=GetPixelChannels(image);
1363 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1375 channel=GetPixelChannelMapChannel(image,i);
1376 traits=GetPixelChannelMapTraits(image,channel);
1377 if ((traits & UpdatePixelTrait) == 0)
1379 if (fabs(levels[n++]) < MagickEpsilon)
1381 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1382 level=threshold/(map->divisor-1);
1383 threshold-=level*(map->divisor-1);
1384 q[i]=RoundToQuantum((MagickRealType) (level+(threshold >=
1385 map->levels[(x % map->width)+map->width*(y % map->height)]))*
1386 QuantumRange/levels[n]);
1389 q+=GetPixelChannels(image);
1391 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1393 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1398 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1399 #pragma omp critical (MagickCore_OrderedPosterizeImage)
1401 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1402 if (proceed == MagickFalse)
1406 image_view=DestroyCacheView(image_view);
1407 map=DestroyThresholdMap(map);
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 % R a n d o m T h r e s h o l d I m a g e %
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422 % RandomThresholdImage() changes the value of individual pixels based on the
1423 % intensity of each pixel compared to a random threshold. The result is a
1424 % low-contrast, two color image.
1426 % The format of the RandomThresholdImage method is:
1428 % MagickBooleanType RandomThresholdImage(Image *image,
1429 % const char *thresholds,ExceptionInfo *exception)
1431 % A description of each parameter follows:
1433 % o image: the image.
1435 % o thresholds: a geometry string containing low,high thresholds. If the
1436 % string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1437 % is performed instead.
1439 % o exception: return any errors or warnings in this structure.
1442 MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1443 const char *thresholds,ExceptionInfo *exception)
1445 #define ThresholdImageTag "Threshold/Image"
1470 **restrict random_info;
1478 assert(image != (Image *) NULL);
1479 assert(image->signature == MagickSignature);
1480 if (image->debug != MagickFalse)
1481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1482 assert(exception != (ExceptionInfo *) NULL);
1483 assert(exception->signature == MagickSignature);
1484 if (thresholds == (const char *) NULL)
1486 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1487 return(MagickFalse);
1488 GetPixelInfo(image,&threshold);
1490 max_threshold=(MagickRealType) QuantumRange;
1491 flags=ParseGeometry(thresholds,&geometry_info);
1492 min_threshold=geometry_info.rho;
1493 max_threshold=geometry_info.sigma;
1494 if ((flags & SigmaValue) == 0)
1495 max_threshold=min_threshold;
1496 if (strchr(thresholds,'%') != (char *) NULL)
1498 max_threshold*=(MagickRealType) (0.01*QuantumRange);
1499 min_threshold*=(MagickRealType) (0.01*QuantumRange);
1502 Random threshold image.
1506 random_info=AcquireRandomInfoThreadSet();
1507 key=GetRandomSecretKey(random_info[0]);
1508 image_view=AcquireAuthenticCacheView(image,exception);
1509 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1510 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1511 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
1513 for (y=0; y < (ssize_t) image->rows; y++)
1516 id = GetOpenMPThreadId();
1524 if (status == MagickFalse)
1526 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1527 if (q == (Quantum *) NULL)
1532 for (x=0; x < (ssize_t) image->columns; x++)
1537 if (GetPixelMask(image,q) != 0)
1539 q+=GetPixelChannels(image);
1542 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1553 channel=GetPixelChannelMapChannel(image,i);
1554 traits=GetPixelChannelMapTraits(image,channel);
1555 if ((traits & UpdatePixelTrait) == 0)
1557 if ((MagickRealType) q[i] < min_threshold)
1558 threshold=min_threshold;
1560 if ((MagickRealType) q[i] > max_threshold)
1561 threshold=max_threshold;
1563 threshold=(MagickRealType) (QuantumRange*
1564 GetPseudoRandomValue(random_info[id]));
1565 q[i]=(MagickRealType) q[i] <= threshold ? 0 : QuantumRange;
1567 q+=GetPixelChannels(image);
1569 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1571 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1576 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1577 #pragma omp critical (MagickCore_RandomThresholdImage)
1579 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1581 if (proceed == MagickFalse)
1585 image_view=DestroyCacheView(image_view);
1586 random_info=DestroyRandomInfoThreadSet(random_info);
1591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1595 % W h i t e T h r e s h o l d I m a g e %
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601 % WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
1602 % the threshold into white while leaving all pixels at or below the threshold
1605 % The format of the WhiteThresholdImage method is:
1607 % MagickBooleanType WhiteThresholdImage(Image *image,
1608 % const char *threshold,ExceptionInfo *exception)
1610 % A description of each parameter follows:
1612 % o image: the image.
1614 % o threshold: Define the threshold value.
1616 % o exception: return any errors or warnings in this structure.
1619 MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
1620 const char *thresholds,ExceptionInfo *exception)
1622 #define ThresholdImageTag "Threshold/Image"
1645 assert(image != (Image *) NULL);
1646 assert(image->signature == MagickSignature);
1647 if (image->debug != MagickFalse)
1648 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1649 if (thresholds == (const char *) NULL)
1651 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1652 return(MagickFalse);
1653 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1654 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1655 GetPixelInfo(image,&threshold);
1656 flags=ParseGeometry(thresholds,&geometry_info);
1657 threshold.red=geometry_info.rho;
1658 threshold.green=geometry_info.rho;
1659 threshold.blue=geometry_info.rho;
1660 threshold.black=geometry_info.rho;
1661 threshold.alpha=100.0;
1662 if ((flags & SigmaValue) != 0)
1663 threshold.green=geometry_info.sigma;
1664 if ((flags & XiValue) != 0)
1665 threshold.blue=geometry_info.xi;
1666 if ((flags & PsiValue) != 0)
1667 threshold.alpha=geometry_info.psi;
1668 if (threshold.colorspace == CMYKColorspace)
1670 if ((flags & PsiValue) != 0)
1671 threshold.black=geometry_info.psi;
1672 if ((flags & ChiValue) != 0)
1673 threshold.alpha=geometry_info.chi;
1675 if ((flags & PercentValue) != 0)
1677 threshold.red*=(QuantumRange/100.0);
1678 threshold.green*=(QuantumRange/100.0);
1679 threshold.blue*=(QuantumRange/100.0);
1680 threshold.black*=(QuantumRange/100.0);
1681 threshold.alpha*=(QuantumRange/100.0);
1684 White threshold image.
1688 image_view=AcquireAuthenticCacheView(image,exception);
1689 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1690 #pragma omp parallel for schedule(static,8) shared(progress,status) \
1691 dynamic_number_threads(image,image->columns,image->rows,1)
1693 for (y=0; y < (ssize_t) image->rows; y++)
1701 if (status == MagickFalse)
1703 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1704 if (q == (Quantum *) NULL)
1709 for (x=0; x < (ssize_t) image->columns; x++)
1714 if (GetPixelMask(image,q) != 0)
1716 q+=GetPixelChannels(image);
1719 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1727 channel=GetPixelChannelMapChannel(image,i);
1728 traits=GetPixelChannelMapTraits(image,channel);
1729 if ((traits & UpdatePixelTrait) == 0)
1731 if ((double) q[i] > GetPixelInfoChannel(&threshold,channel))
1734 q+=GetPixelChannels(image);
1736 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1738 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1743 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1744 #pragma omp critical (MagickCore_WhiteThresholdImage)
1746 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1748 if (proceed == MagickFalse)
1752 image_view=DestroyCacheView(image_view);