2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP AAA IIIII N N TTTTT %
8 % PPPP AAAAA I N N N T %
13 % Methods to Paint on an Image %
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/color.h"
44 #include "MagickCore/color-private.h"
45 #include "MagickCore/colorspace-private.h"
46 #include "MagickCore/composite.h"
47 #include "MagickCore/composite-private.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/draw-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/gem.h"
53 #include "MagickCore/gem-private.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/paint.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/statistic.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/thread-private.h"
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 % F l o o d f i l l P a i n t I m a g e %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % FloodfillPaintImage() changes the color value of any pixel that matches
74 % target and is an immediate neighbor. If the method FillToBorderMethod is
75 % specified, the color value is changed for any neighbor pixel that does not
76 % match the bordercolor member of image.
78 % By default target must match a particular pixel color exactly. However,
79 % in many cases two colors may differ by a small amount. The fuzz member of
80 % image defines how much tolerance is acceptable to consider two colors as
81 % the same. For example, set fuzz to 10 and the color red at intensities of
82 % 100 and 102 respectively are now interpreted as the same color for the
83 % purposes of the floodfill.
85 % The format of the FloodfillPaintImage method is:
87 % MagickBooleanType FloodfillPaintImage(Image *image,
88 % const DrawInfo *draw_info,const PixelInfo target,
89 % const ssize_t x_offset,const ssize_t y_offset,
90 % const MagickBooleanType invert,ExceptionInfo *exception)
92 % A description of each parameter follows:
96 % o draw_info: the draw info.
98 % o target: the RGB value of the target color.
100 % o x_offset,y_offset: the starting location of the operation.
102 % o invert: paint any pixel that does not match the target color.
104 % o exception: return any errors or warnings in this structure.
107 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
108 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
109 const ssize_t y_offset,const MagickBooleanType invert,
110 ExceptionInfo *exception)
112 #define MaxStacksize (1UL << 15)
113 #define PushSegmentStack(up,left,right,delta) \
115 if (s >= (segment_stack+MaxStacksize)) \
116 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
119 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
121 s->x1=(double) (left); \
122 s->y1=(double) (up); \
123 s->x2=(double) (right); \
124 s->y2=(double) (delta); \
160 Check boundary conditions.
162 assert(image != (Image *) NULL);
163 assert(image->signature == MagickSignature);
164 if (image->debug != MagickFalse)
165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
166 assert(draw_info != (DrawInfo *) NULL);
167 assert(draw_info->signature == MagickSignature);
168 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
170 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
172 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
174 if (IsGrayColorspace(image->colorspace) != MagickFalse)
175 (void) TransformImageColorspace(image,sRGBColorspace,exception);
176 if ((image->matte == MagickFalse) && (draw_info->fill.matte != MagickFalse))
177 (void) SetImageAlpha(image,OpaqueAlpha,exception);
181 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
183 if (floodplane_image == (Image *) NULL)
185 floodplane_image->colorspace=GRAYColorspace;
186 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,exception);
187 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
188 sizeof(*segment_stack));
189 if (segment_stack == (SegmentInfo *) NULL)
191 floodplane_image=DestroyImage(floodplane_image);
192 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
196 Push initial segment on stack.
203 PushSegmentStack(y,x,x,1);
204 PushSegmentStack(y+1,x,x,-1);
205 GetPixelInfo(image,&pixel);
206 image_view=AcquireVirtualCacheView(image,exception);
207 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
208 while (s > segment_stack)
210 register const Quantum
220 Pop segment off stack.
225 offset=(ssize_t) s->y2;
226 y=(ssize_t) s->y1+offset;
228 Recolor neighboring pixels.
230 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
231 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
233 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
235 p+=x1*GetPixelChannels(image);
236 q+=x1*GetPixelChannels(floodplane_image);
237 for (x=x1; x >= 0; x--)
239 if (GetPixelGray(floodplane_image,q) != 0)
241 GetPixelInfoPixel(image,p,&pixel);
242 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
244 SetPixelGray(floodplane_image,QuantumRange,q);
245 p-=GetPixelChannels(image);
246 q-=GetPixelChannels(floodplane_image);
248 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
250 skip=x >= x1 ? MagickTrue : MagickFalse;
251 if (skip == MagickFalse)
255 PushSegmentStack(y,start,x1-1,-offset);
260 if (skip == MagickFalse)
262 if (x < (ssize_t) image->columns)
264 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
266 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
268 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
270 for ( ; x < (ssize_t) image->columns; x++)
272 if (GetPixelGray(floodplane_image,q) != 0)
274 GetPixelInfoPixel(image,p,&pixel);
275 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
277 SetPixelGray(floodplane_image,QuantumRange,q);
278 p+=GetPixelChannels(image);
279 q+=GetPixelChannels(floodplane_image);
281 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
282 if (status == MagickFalse)
285 PushSegmentStack(y,start,x-1,offset);
287 PushSegmentStack(y,x2+1,x-1,-offset);
293 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
295 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
297 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
299 for ( ; x <= x2; x++)
301 if (GetPixelGray(floodplane_image,q) != 0)
303 GetPixelInfoPixel(image,p,&pixel);
304 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
306 p+=GetPixelChannels(image);
307 q+=GetPixelChannels(floodplane_image);
313 for (y=0; y < (ssize_t) image->rows; y++)
315 register const Quantum
325 Tile fill color onto floodplane.
327 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
331 for (x=0; x < (ssize_t) image->columns; x++)
333 if (GetPixelGray(floodplane_image,p) != 0)
335 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
336 SetPixelInfoPixel(image,&fill_color,q);
338 p+=GetPixelChannels(floodplane_image);
339 q+=GetPixelChannels(image);
341 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
344 floodplane_view=DestroyCacheView(floodplane_view);
345 image_view=DestroyCacheView(image_view);
346 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
347 floodplane_image=DestroyImage(floodplane_image);
348 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 + G r a d i e n t I m a g e %
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 % GradientImage() applies a continuously smooth color transitions along a
363 % vector from one color to another.
365 % Note, the interface of this method will change in the future to support
366 % more than one transistion.
368 % The format of the GradientImage method is:
370 % MagickBooleanType GradientImage(Image *image,const GradientType type,
371 % const SpreadMethod method,const PixelInfo *start_color,
372 % const PixelInfo *stop_color,ExceptionInfo *exception)
374 % A description of each parameter follows:
376 % o image: the image.
378 % o type: the gradient type: linear or radial.
380 % o spread: the gradient spread meathod: pad, reflect, or repeat.
382 % o start_color: the start color.
384 % o stop_color: the stop color.
386 % o exception: return any errors or warnings in this structure.
390 static inline double MagickMax(const double x,const double y)
392 return(x > y ? x : y);
395 MagickExport MagickBooleanType GradientImage(Image *image,
396 const GradientType type,const SpreadMethod method,
397 const PixelInfo *start_color,const PixelInfo *stop_color,
398 ExceptionInfo *exception)
413 Set gradient start-stop end points.
415 assert(image != (const Image *) NULL);
416 assert(image->signature == MagickSignature);
417 if (image->debug != MagickFalse)
418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
419 assert(start_color != (const PixelInfo *) NULL);
420 assert(stop_color != (const PixelInfo *) NULL);
421 draw_info=AcquireDrawInfo();
422 gradient=(&draw_info->gradient);
424 gradient->bounding_box.width=image->columns;
425 gradient->bounding_box.height=image->rows;
426 gradient->gradient_vector.x2=(double) image->columns-1.0;
427 gradient->gradient_vector.y2=(double) image->rows-1.0;
428 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
429 gradient->gradient_vector.x2=0.0;
430 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
431 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
432 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
433 gradient->spread=method;
435 Define the gradient to fill between the stops.
437 gradient->number_stops=2;
438 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
439 sizeof(*gradient->stops));
440 if (gradient->stops == (StopInfo *) NULL)
441 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
443 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
444 sizeof(*gradient->stops));
445 for (i=0; i < (ssize_t) gradient->number_stops; i++)
446 GetPixelInfo(image,&gradient->stops[i].color);
447 gradient->stops[0].color=(*start_color);
448 gradient->stops[0].offset=0.0;
449 gradient->stops[1].color=(*stop_color);
450 gradient->stops[1].offset=1.0;
452 Draw a gradient on the image.
454 status=DrawGradientImage(image,draw_info,exception);
455 draw_info=DestroyDrawInfo(draw_info);
456 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
457 image->matte=MagickFalse;
458 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
459 (IsPixelInfoGray(stop_color) != MagickFalse))
460 image->type=GrayscaleType;
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % O i l P a i n t I m a g e %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % OilPaintImage() applies a special effect filter that simulates an oil
476 % painting. Each pixel is replaced by the most frequent color occurring
477 % in a circular region defined by radius.
479 % The format of the OilPaintImage method is:
481 % Image *OilPaintImage(const Image *image,const double radius,
482 % const double sigma,ExceptionInfo *exception)
484 % A description of each parameter follows:
486 % o image: the image.
488 % o radius: the radius of the circular neighborhood.
490 % o sigma: the standard deviation of the Gaussian, in pixels.
492 % o exception: return any errors or warnings in this structure.
496 static size_t **DestroyHistogramThreadSet(size_t **histogram)
501 assert(histogram != (size_t **) NULL);
502 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
503 if (histogram[i] != (size_t *) NULL)
504 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
505 histogram=(size_t **) RelinquishMagickMemory(histogram);
509 static size_t **AcquireHistogramThreadSet(const size_t count)
518 number_threads=GetOpenMPMaximumThreads();
519 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
520 if (histogram == (size_t **) NULL)
521 return((size_t **) NULL);
522 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
523 for (i=0; i < (ssize_t) number_threads; i++)
525 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
526 if (histogram[i] == (size_t *) NULL)
527 return(DestroyHistogramThreadSet(histogram));
532 MagickExport Image *OilPaintImage(const Image *image,const double radius,
533 const double sigma,ExceptionInfo *exception)
535 #define NumberPaintBins 256
536 #define OilPaintImageTag "OilPaint/Image"
560 Initialize painted image attributes.
562 assert(image != (const Image *) NULL);
563 assert(image->signature == MagickSignature);
564 if (image->debug != MagickFalse)
565 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
566 assert(exception != (ExceptionInfo *) NULL);
567 assert(exception->signature == MagickSignature);
568 width=GetOptimalKernelWidth2D(radius,sigma);
569 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
570 if (paint_image == (Image *) NULL)
571 return((Image *) NULL);
572 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
574 paint_image=DestroyImage(paint_image);
575 return((Image *) NULL);
577 histograms=AcquireHistogramThreadSet(NumberPaintBins);
578 if (histograms == (size_t **) NULL)
580 paint_image=DestroyImage(paint_image);
581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
588 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
589 GetPixelChannels(image)*(width/2L);
590 image_view=AcquireVirtualCacheView(image,exception);
591 paint_view=AcquireAuthenticCacheView(paint_image,exception);
592 #if defined(MAGICKCORE_OPENMP_SUPPORT)
593 #pragma omp parallel for schedule(static,4) shared(progress,status)
595 for (y=0; y < (ssize_t) image->rows; y++)
597 register const Quantum
609 if (status == MagickFalse)
611 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
612 (width/2L),image->columns+width,width,exception);
613 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
615 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
620 histogram=histograms[GetOpenMPThreadId()];
621 for (x=0; x < (ssize_t) image->columns; x++)
637 Assign most frequent color.
642 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
643 for (v=0; v < (ssize_t) width; v++)
645 for (u=0; u < (ssize_t) width; u++)
647 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
648 GetPixelChannels(image)*(u+k)));
650 if (histogram[n] > count)
656 k+=(ssize_t) (image->columns+width);
658 if (GetPixelMask(image,p) != 0)
660 p+=GetPixelChannels(image);
661 q+=GetPixelChannels(paint_image);
664 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
673 channel=GetPixelChannelMapChannel(image,i);
674 traits=GetPixelChannelMapTraits(image,channel);
675 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
676 if ((traits == UndefinedPixelTrait) ||
677 (paint_traits == UndefinedPixelTrait))
679 if ((paint_traits & CopyPixelTrait) != 0)
681 SetPixelChannel(paint_image,channel,p[center+i],q);
684 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
686 p+=GetPixelChannels(image);
687 q+=GetPixelChannels(paint_image);
689 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
691 if (image->progress_monitor != (MagickProgressMonitor) NULL)
696 #if defined(MAGICKCORE_OPENMP_SUPPORT)
697 #pragma omp critical (MagickCore_OilPaintImage)
699 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
700 if (proceed == MagickFalse)
704 paint_view=DestroyCacheView(paint_view);
705 image_view=DestroyCacheView(image_view);
706 histograms=DestroyHistogramThreadSet(histograms);
707 if (status == MagickFalse)
708 paint_image=DestroyImage(paint_image);
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % O p a q u e P a i n t I m a g e %
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 % OpaquePaintImage() changes any pixel that matches color with the color
726 % By default color must match a particular pixel color exactly. However, in
727 % many cases two colors may differ by a small amount. Fuzz defines how much
728 % tolerance is acceptable to consider two colors as the same. For example,
729 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
730 % are now interpreted as the same color.
732 % The format of the OpaquePaintImage method is:
734 % MagickBooleanType OpaquePaintImage(Image *image,
735 % const PixelInfo *target,const PixelInfo *fill,
736 % const MagickBooleanType invert,ExceptionInfo *exception)
738 % A description of each parameter follows:
740 % o image: the image.
742 % o target: the RGB value of the target color.
744 % o fill: the replacement color.
746 % o invert: paint any pixel that does not match the target color.
748 % o exception: return any errors or warnings in this structure.
751 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
752 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
753 ExceptionInfo *exception)
755 #define OpaquePaintImageTag "Opaque/Image"
772 assert(image != (Image *) NULL);
773 assert(image->signature == MagickSignature);
774 assert(target != (PixelInfo *) NULL);
775 assert(fill != (PixelInfo *) NULL);
776 if (image->debug != MagickFalse)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
778 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
780 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
781 (IsPixelInfoGray(fill) != MagickFalse))
782 (void) TransformImageColorspace(image,sRGBColorspace,exception);
783 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
784 (void) SetImageAlpha(image,OpaqueAlpha,exception);
786 Make image color opaque.
790 GetPixelInfo(image,&zero);
791 image_view=AcquireAuthenticCacheView(image,exception);
792 #if defined(MAGICKCORE_OPENMP_SUPPORT)
793 #pragma omp parallel for schedule(static,4) shared(progress,status)
795 for (y=0; y < (ssize_t) image->rows; y++)
806 if (status == MagickFalse)
808 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
809 if (q == (Quantum *) NULL)
815 for (x=0; x < (ssize_t) image->columns; x++)
817 GetPixelInfoPixel(image,q,&pixel);
818 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
819 SetPixelInfoPixel(image,fill,q);
820 q+=GetPixelChannels(image);
822 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
829 #if defined(MAGICKCORE_OPENMP_SUPPORT)
830 #pragma omp critical (MagickCore_OpaquePaintImage)
832 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
834 if (proceed == MagickFalse)
838 image_view=DestroyCacheView(image_view);
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 % T r a n s p a r e n t P a i n t I m a g e %
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % TransparentPaintImage() changes the opacity value associated with any pixel
854 % that matches color to the value defined by opacity.
856 % By default color must match a particular pixel color exactly. However, in
857 % many cases two colors may differ by a small amount. Fuzz defines how much
858 % tolerance is acceptable to consider two colors as the same. For example,
859 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
860 % are now interpreted as the same color.
862 % The format of the TransparentPaintImage method is:
864 % MagickBooleanType TransparentPaintImage(Image *image,
865 % const PixelInfo *target,const Quantum opacity,
866 % const MagickBooleanType invert,ExceptionInfo *exception)
868 % A description of each parameter follows:
870 % o image: the image.
872 % o target: the target color.
874 % o opacity: the replacement opacity value.
876 % o invert: paint any pixel that does not match the target color.
878 % o exception: return any errors or warnings in this structure.
881 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
882 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
883 ExceptionInfo *exception)
885 #define TransparentPaintImageTag "Transparent/Image"
902 assert(image != (Image *) NULL);
903 assert(image->signature == MagickSignature);
904 assert(target != (PixelInfo *) NULL);
905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
907 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
909 if (image->matte == MagickFalse)
910 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
912 Make image color transparent.
916 GetPixelInfo(image,&zero);
917 image_view=AcquireAuthenticCacheView(image,exception);
918 #if defined(MAGICKCORE_OPENMP_SUPPORT)
919 #pragma omp parallel for schedule(static,4) shared(progress,status)
921 for (y=0; y < (ssize_t) image->rows; y++)
932 if (status == MagickFalse)
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
935 if (q == (Quantum *) NULL)
941 for (x=0; x < (ssize_t) image->columns; x++)
943 GetPixelInfoPixel(image,q,&pixel);
944 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
945 SetPixelAlpha(image,opacity,q);
946 q+=GetPixelChannels(image);
948 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
950 if (image->progress_monitor != (MagickProgressMonitor) NULL)
955 #if defined(MAGICKCORE_OPENMP_SUPPORT)
956 #pragma omp critical (MagickCore_TransparentPaintImage)
958 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
960 if (proceed == MagickFalse)
964 image_view=DestroyCacheView(image_view);
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % T r a n s p a r e n t P a i n t I m a g e C h r o m a %
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % TransparentPaintImageChroma() changes the opacity value associated with any
980 % pixel that matches color to the value defined by opacity.
982 % As there is one fuzz value for the all the channels, TransparentPaintImage()
983 % is not suitable for the operations like chroma, where the tolerance for
984 % similarity of two color component (RGB) can be different. Thus we define
985 % this method to take two target pixels (one low and one high) and all the
986 % pixels of an image which are lying between these two pixels are made
989 % The format of the TransparentPaintImageChroma method is:
991 % MagickBooleanType TransparentPaintImageChroma(Image *image,
992 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
993 % const MagickBooleanType invert,ExceptionInfo *exception)
995 % A description of each parameter follows:
997 % o image: the image.
999 % o low: the low target color.
1001 % o high: the high target color.
1003 % o opacity: the replacement opacity value.
1005 % o invert: paint any pixel that does not match the target color.
1007 % o exception: return any errors or warnings in this structure.
1010 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1011 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1012 const MagickBooleanType invert,ExceptionInfo *exception)
1014 #define TransparentPaintImageTag "Transparent/Image"
1028 assert(image != (Image *) NULL);
1029 assert(image->signature == MagickSignature);
1030 assert(high != (PixelInfo *) NULL);
1031 assert(low != (PixelInfo *) NULL);
1032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1034 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1035 return(MagickFalse);
1036 if (image->matte == MagickFalse)
1037 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1039 Make image color transparent.
1043 image_view=AcquireAuthenticCacheView(image,exception);
1044 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1045 #pragma omp parallel for schedule(static,4) shared(progress,status)
1047 for (y=0; y < (ssize_t) image->rows; y++)
1061 if (status == MagickFalse)
1063 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1064 if (q == (Quantum *) NULL)
1069 GetPixelInfo(image,&pixel);
1070 for (x=0; x < (ssize_t) image->columns; x++)
1072 GetPixelInfoPixel(image,q,&pixel);
1073 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1074 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1075 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1077 if (match != invert)
1078 SetPixelAlpha(image,opacity,q);
1079 q+=GetPixelChannels(image);
1081 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1083 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1088 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1089 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1091 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1093 if (proceed == MagickFalse)
1097 image_view=DestroyCacheView(image_view);