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-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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/string_.h"
59 #include "MagickCore/thread-private.h"
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 % F l o o d f i l l P a i n t I m a g e %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % FloodfillPaintImage() changes the color value of any pixel that matches
73 % target and is an immediate neighbor. If the method FillToBorderMethod is
74 % specified, the color value is changed for any neighbor pixel that does not
75 % match the bordercolor member of image.
77 % By default target must match a particular pixel color exactly. However,
78 % in many cases two colors may differ by a small amount. The fuzz member of
79 % image defines how much tolerance is acceptable to consider two colors as
80 % the same. For example, set fuzz to 10 and the color red at intensities of
81 % 100 and 102 respectively are now interpreted as the same color for the
82 % purposes of the floodfill.
84 % The format of the FloodfillPaintImage method is:
86 % MagickBooleanType FloodfillPaintImage(Image *image,
87 % const DrawInfo *draw_info,const PixelInfo target,
88 % const ssize_t x_offset,const ssize_t y_offset,
89 % const MagickBooleanType invert,ExceptionInfo *exception)
91 % A description of each parameter follows:
95 % o draw_info: the draw info.
97 % o target: the RGB value of the target color.
99 % o x_offset,y_offset: the starting location of the operation.
101 % o invert: paint any pixel that does not match the target color.
103 % o exception: return any errors or warnings in this structure.
106 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
107 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
108 const ssize_t y_offset,const MagickBooleanType invert,
109 ExceptionInfo *exception)
111 #define MaxStacksize (1UL << 15)
112 #define PushSegmentStack(up,left,right,delta) \
114 if (s >= (segment_stack+MaxStacksize)) \
115 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
118 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
120 s->x1=(double) (left); \
121 s->y1=(double) (up); \
122 s->x2=(double) (right); \
123 s->y2=(double) (delta); \
161 Check boundary conditions.
163 assert(image != (Image *) NULL);
164 assert(image->signature == MagickSignature);
165 if (image->debug != MagickFalse)
166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167 assert(draw_info != (DrawInfo *) NULL);
168 assert(draw_info->signature == MagickSignature);
169 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
171 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
175 if (image->matte == MagickFalse)
176 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
180 floodplane_image=CloneImage(image,0,0,MagickTrue,exception);
181 if (floodplane_image == (Image *) NULL)
183 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
184 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
185 sizeof(*segment_stack));
186 if (segment_stack == (SegmentInfo *) NULL)
188 floodplane_image=DestroyImage(floodplane_image);
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
193 Push initial segment on stack.
196 fill_color.black=0.0;
197 fill_color.index=0.0;
202 PushSegmentStack(y,x,x,1);
203 PushSegmentStack(y+1,x,x,-1);
204 GetPixelInfo(image,&pixel);
205 image_view=AcquireCacheView(image);
206 floodplane_view=AcquireCacheView(floodplane_image);
207 while (s > segment_stack)
209 register const Quantum
219 Pop segment off stack.
224 offset=(ssize_t) s->y2;
225 y=(ssize_t) s->y1+offset;
227 Recolor neighboring pixels.
229 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
230 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
232 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
234 p+=x1*GetPixelChannels(image);
235 q+=x1*GetPixelChannels(floodplane_image);
236 for (x=x1; x >= 0; x--)
238 if (GetPixelAlpha(image,q) == TransparentAlpha)
240 GetPixelInfoPixel(image,p,&pixel);
241 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
243 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
244 p-=GetPixelChannels(image);
245 q-=GetPixelChannels(floodplane_image);
247 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
249 skip=x >= x1 ? MagickTrue : MagickFalse;
250 if (skip == MagickFalse)
254 PushSegmentStack(y,start,x1-1,-offset);
259 if (skip == MagickFalse)
261 if (x < (ssize_t) image->columns)
263 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
265 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
266 image->columns-x,1,exception);
267 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
269 for ( ; x < (ssize_t) image->columns; x++)
271 if (GetPixelAlpha(image,q) == TransparentAlpha)
273 GetPixelInfoPixel(image,p,&pixel);
274 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
276 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
277 p+=GetPixelChannels(image);
278 q+=GetPixelChannels(floodplane_image);
280 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
281 if (status == MagickFalse)
284 PushSegmentStack(y,start,x-1,offset);
286 PushSegmentStack(y,x2+1,x-1,-offset);
292 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
294 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
296 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
298 for ( ; x <= x2; x++)
300 if (GetPixelAlpha(image,q) == TransparentAlpha)
302 GetPixelInfoPixel(image,p,&pixel);
303 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
305 p+=GetPixelChannels(image);
306 q+=GetPixelChannels(floodplane_image);
312 for (y=0; y < (ssize_t) image->rows; y++)
314 register const Quantum
324 Tile fill color onto floodplane.
326 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
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 (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
335 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
336 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
337 SetPixelRed(image,ClampToQuantum(fill_color.red),q);
338 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
339 SetPixelGreen(image,ClampToQuantum(fill_color.green),q);
340 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
341 SetPixelBlue(image,ClampToQuantum(fill_color.blue),q);
342 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
343 SetPixelBlack(image,ClampToQuantum(fill_color.black),q);
344 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
345 SetPixelAlpha(image,ClampToQuantum(fill_color.alpha),q);
347 p+=GetPixelChannels(floodplane_image);
348 q+=GetPixelChannels(image);
350 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
353 floodplane_view=DestroyCacheView(floodplane_view);
354 image_view=DestroyCacheView(image_view);
355 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
356 floodplane_image=DestroyImage(floodplane_image);
357 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 + G r a d i e n t I m a g e %
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 % GradientImage() applies a continuously smooth color transitions along a
372 % vector from one color to another.
374 % Note, the interface of this method will change in the future to support
375 % more than one transistion.
377 % The format of the GradientImage method is:
379 % MagickBooleanType GradientImage(Image *image,const GradientType type,
380 % const SpreadMethod method,const PixelInfo *start_color,
381 % const PixelInfo *stop_color,ExceptionInfo *exception)
383 % A description of each parameter follows:
385 % o image: the image.
387 % o type: the gradient type: linear or radial.
389 % o spread: the gradient spread meathod: pad, reflect, or repeat.
391 % o start_color: the start color.
393 % o stop_color: the stop color.
395 % o exception: return any errors or warnings in this structure.
399 static inline double MagickMax(const double x,const double y)
401 return(x > y ? x : y);
404 MagickExport MagickBooleanType GradientImage(Image *image,
405 const GradientType type,const SpreadMethod method,
406 const PixelInfo *start_color,const PixelInfo *stop_color,
407 ExceptionInfo *exception)
422 Set gradient start-stop end points.
424 assert(image != (const Image *) NULL);
425 assert(image->signature == MagickSignature);
426 if (image->debug != MagickFalse)
427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
428 assert(start_color != (const PixelInfo *) NULL);
429 assert(stop_color != (const PixelInfo *) NULL);
430 draw_info=AcquireDrawInfo();
431 gradient=(&draw_info->gradient);
433 gradient->bounding_box.width=image->columns;
434 gradient->bounding_box.height=image->rows;
435 gradient->gradient_vector.x2=(double) image->columns-1.0;
436 gradient->gradient_vector.y2=(double) image->rows-1.0;
437 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
438 gradient->gradient_vector.x2=0.0;
439 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
440 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
441 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
442 gradient->spread=method;
444 Define the gradient to fill between the stops.
446 gradient->number_stops=2;
447 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
448 sizeof(*gradient->stops));
449 if (gradient->stops == (StopInfo *) NULL)
450 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
452 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
453 sizeof(*gradient->stops));
454 for (i=0; i < (ssize_t) gradient->number_stops; i++)
455 GetPixelInfo(image,&gradient->stops[i].color);
456 gradient->stops[0].color=(*start_color);
457 gradient->stops[0].offset=0.0;
458 gradient->stops[1].color=(*stop_color);
459 gradient->stops[1].offset=1.0;
461 Draw a gradient on the image.
463 status=DrawGradientImage(image,draw_info,exception);
464 draw_info=DestroyDrawInfo(draw_info);
465 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
466 image->matte=MagickFalse;
467 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
468 (IsPixelInfoGray(stop_color) != MagickFalse))
469 image->type=GrayscaleType;
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % O i l P a i n t I m a g e %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % OilPaintImage() applies a special effect filter that simulates an oil
485 % painting. Each pixel is replaced by the most frequent color occurring
486 % in a circular region defined by radius.
488 % The format of the OilPaintImage method is:
490 % Image *OilPaintImage(const Image *image,const double radius,
491 % const double sigma,ExceptionInfo *exception)
493 % A description of each parameter follows:
495 % o image: the image.
497 % o radius: the radius of the circular neighborhood.
499 % o sigma: the standard deviation of the Gaussian, in pixels.
501 % o exception: return any errors or warnings in this structure.
505 static size_t **DestroyHistogramThreadSet(size_t **histogram)
510 assert(histogram != (size_t **) NULL);
511 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
512 if (histogram[i] != (size_t *) NULL)
513 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
514 histogram=(size_t **) RelinquishMagickMemory(histogram);
518 static size_t **AcquireHistogramThreadSet(const size_t count)
527 number_threads=GetOpenMPMaximumThreads();
528 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
529 if (histogram == (size_t **) NULL)
530 return((size_t **) NULL);
531 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
532 for (i=0; i < (ssize_t) number_threads; i++)
534 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
535 if (histogram[i] == (size_t *) NULL)
536 return(DestroyHistogramThreadSet(histogram));
541 MagickExport Image *OilPaintImage(const Image *image,const double radius,
542 const double sigma,ExceptionInfo *exception)
544 #define NumberPaintBins 256
545 #define OilPaintImageTag "OilPaint/Image"
568 Initialize painted image attributes.
570 assert(image != (const Image *) NULL);
571 assert(image->signature == MagickSignature);
572 if (image->debug != MagickFalse)
573 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
574 assert(exception != (ExceptionInfo *) NULL);
575 assert(exception->signature == MagickSignature);
576 width=GetOptimalKernelWidth2D(radius,sigma);
577 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
578 if (paint_image == (Image *) NULL)
579 return((Image *) NULL);
580 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
582 paint_image=DestroyImage(paint_image);
583 return((Image *) NULL);
585 histograms=AcquireHistogramThreadSet(NumberPaintBins);
586 if (histograms == (size_t **) NULL)
588 paint_image=DestroyImage(paint_image);
589 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
596 image_view=AcquireCacheView(image);
597 paint_view=AcquireCacheView(paint_image);
598 #if defined(MAGICKCORE_OPENMP_SUPPORT)
599 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
601 for (y=0; y < (ssize_t) image->rows; y++)
603 register const Quantum
615 if (status == MagickFalse)
617 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
618 (width/2L),image->columns+width,width,exception);
619 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
621 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
626 histogram=histograms[GetOpenMPThreadId()];
627 for (x=0; x < (ssize_t) image->columns; x++)
642 Assign most frequent color.
647 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
648 for (v=0; v < (ssize_t) width; v++)
650 for (u=0; u < (ssize_t) width; u++)
652 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
653 GetPixelChannels(image)*(u+i)));
655 if (histogram[k] > count)
661 i+=(ssize_t) (image->columns+width);
663 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
664 SetPixelRed(paint_image,GetPixelRed(image,p+j*
665 GetPixelChannels(image)),q);
666 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
667 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
668 GetPixelChannels(image)),q);
669 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
670 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
671 GetPixelChannels(image)),q);
672 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
673 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
674 GetPixelChannels(image)),q);
675 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
676 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
677 GetPixelChannels(image)),q);
678 p+=GetPixelChannels(image);
679 q+=GetPixelChannels(paint_image);
681 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
683 if (image->progress_monitor != (MagickProgressMonitor) NULL)
688 #if defined(MAGICKCORE_OPENMP_SUPPORT)
689 #pragma omp critical (MagickCore_OilPaintImage)
691 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
692 if (proceed == MagickFalse)
696 paint_view=DestroyCacheView(paint_view);
697 image_view=DestroyCacheView(image_view);
698 histograms=DestroyHistogramThreadSet(histograms);
699 if (status == MagickFalse)
700 paint_image=DestroyImage(paint_image);
705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % O p a q u e P a i n t I m a g e %
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 % OpaquePaintImage() changes any pixel that matches color with the color
718 % By default color must match a particular pixel color exactly. However, in
719 % many cases two colors may differ by a small amount. Fuzz defines how much
720 % tolerance is acceptable to consider two colors as the same. For example,
721 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
722 % are now interpreted as the same color.
724 % The format of the OpaquePaintImage method is:
726 % MagickBooleanType OpaquePaintImage(Image *image,
727 % const PixelInfo *target,const PixelInfo *fill,
728 % const MagickBooleanType invert,ExceptionInfo *exception)
730 % A description of each parameter follows:
732 % o image: the image.
734 % o target: the RGB value of the target color.
736 % o fill: the replacement color.
738 % o invert: paint any pixel that does not match the target color.
740 % o exception: return any errors or warnings in this structure.
743 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
744 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
745 ExceptionInfo *exception)
747 #define OpaquePaintImageTag "Opaque/Image"
764 assert(image != (Image *) NULL);
765 assert(image->signature == MagickSignature);
766 assert(target != (PixelInfo *) NULL);
767 assert(fill != (PixelInfo *) NULL);
768 if (image->debug != MagickFalse)
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
770 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
773 Make image color opaque.
777 GetPixelInfo(image,&zero);
778 image_view=AcquireCacheView(image);
779 #if defined(MAGICKCORE_OPENMP_SUPPORT)
780 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
782 for (y=0; y < (ssize_t) image->rows; y++)
793 if (status == MagickFalse)
795 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
796 if (q == (Quantum *) NULL)
802 for (x=0; x < (ssize_t) image->columns; x++)
804 GetPixelInfoPixel(image,q,&pixel);
805 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
807 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
808 SetPixelRed(image,ClampToQuantum(fill->red),q);
809 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
810 SetPixelGreen(image,ClampToQuantum(fill->green),q);
811 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
812 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
813 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
814 SetPixelBlack(image,ClampToQuantum(fill->black),q);
815 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
816 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
818 q+=GetPixelChannels(image);
820 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
822 if (image->progress_monitor != (MagickProgressMonitor) NULL)
827 #if defined(MAGICKCORE_OPENMP_SUPPORT)
828 #pragma omp critical (MagickCore_OpaquePaintImage)
830 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
832 if (proceed == MagickFalse)
836 image_view=DestroyCacheView(image_view);
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 % T r a n s p a r e n t P a i n t I m a g e %
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 % TransparentPaintImage() changes the opacity value associated with any pixel
852 % that matches color to the value defined by opacity.
854 % By default color must match a particular pixel color exactly. However, in
855 % many cases two colors may differ by a small amount. Fuzz defines how much
856 % tolerance is acceptable to consider two colors as the same. For example,
857 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
858 % are now interpreted as the same color.
860 % The format of the TransparentPaintImage method is:
862 % MagickBooleanType TransparentPaintImage(Image *image,
863 % const PixelInfo *target,const Quantum opacity,
864 % const MagickBooleanType invert,ExceptionInfo *exception)
866 % A description of each parameter follows:
868 % o image: the image.
870 % o target: the target color.
872 % o opacity: the replacement opacity value.
874 % o invert: paint any pixel that does not match the target color.
876 % o exception: return any errors or warnings in this structure.
879 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
880 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
881 ExceptionInfo *exception)
883 #define TransparentPaintImageTag "Transparent/Image"
900 assert(image != (Image *) NULL);
901 assert(image->signature == MagickSignature);
902 assert(target != (PixelInfo *) NULL);
903 if (image->debug != MagickFalse)
904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
905 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
907 if (image->matte == MagickFalse)
908 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
910 Make image color transparent.
914 GetPixelInfo(image,&zero);
915 image_view=AcquireCacheView(image);
916 #if defined(MAGICKCORE_OPENMP_SUPPORT)
917 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
919 for (y=0; y < (ssize_t) image->rows; y++)
930 if (status == MagickFalse)
932 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
933 if (q == (Quantum *) NULL)
939 for (x=0; x < (ssize_t) image->columns; x++)
941 GetPixelInfoPixel(image,q,&pixel);
942 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
943 SetPixelAlpha(image,opacity,q);
944 q+=GetPixelChannels(image);
946 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
948 if (image->progress_monitor != (MagickProgressMonitor) NULL)
953 #if defined(MAGICKCORE_OPENMP_SUPPORT)
954 #pragma omp critical (MagickCore_TransparentPaintImage)
956 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
958 if (proceed == MagickFalse)
962 image_view=DestroyCacheView(image_view);
967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971 % 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 %
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977 % TransparentPaintImageChroma() changes the opacity value associated with any
978 % pixel that matches color to the value defined by opacity.
980 % As there is one fuzz value for the all the channels, TransparentPaintImage()
981 % is not suitable for the operations like chroma, where the tolerance for
982 % similarity of two color component (RGB) can be different. Thus we define
983 % this method to take two target pixels (one low and one high) and all the
984 % pixels of an image which are lying between these two pixels are made
987 % The format of the TransparentPaintImageChroma method is:
989 % MagickBooleanType TransparentPaintImageChroma(Image *image,
990 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
991 % const MagickBooleanType invert,ExceptionInfo *exception)
993 % A description of each parameter follows:
995 % o image: the image.
997 % o low: the low target color.
999 % o high: the high target color.
1001 % o opacity: the replacement opacity value.
1003 % o invert: paint any pixel that does not match the target color.
1005 % o exception: return any errors or warnings in this structure.
1008 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1009 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1010 const MagickBooleanType invert,ExceptionInfo *exception)
1012 #define TransparentPaintImageTag "Transparent/Image"
1026 assert(image != (Image *) NULL);
1027 assert(image->signature == MagickSignature);
1028 assert(high != (PixelInfo *) NULL);
1029 assert(low != (PixelInfo *) NULL);
1030 if (image->debug != MagickFalse)
1031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1032 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1033 return(MagickFalse);
1034 if (image->matte == MagickFalse)
1035 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1037 Make image color transparent.
1041 image_view=AcquireCacheView(image);
1042 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1043 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1045 for (y=0; y < (ssize_t) image->rows; y++)
1059 if (status == MagickFalse)
1061 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1062 if (q == (Quantum *) NULL)
1067 GetPixelInfo(image,&pixel);
1068 for (x=0; x < (ssize_t) image->columns; x++)
1070 GetPixelInfoPixel(image,q,&pixel);
1071 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1072 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1073 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1075 if (match != invert)
1076 SetPixelAlpha(image,opacity,q);
1077 q+=GetPixelChannels(image);
1079 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1081 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1086 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1087 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1089 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1091 if (proceed == MagickFalse)
1095 image_view=DestroyCacheView(image_view);