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-2013 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/resource_.h"
59 #include "MagickCore/statistic.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/thread-private.h"
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 % F l o o d f i l l P a i n t I m a g e %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % FloodfillPaintImage() changes the color value of any pixel that matches
75 % target and is an immediate neighbor. If the method FillToBorderMethod is
76 % specified, the color value is changed for any neighbor pixel that does not
77 % match the bordercolor member of image.
79 % By default target must match a particular pixel color exactly. However,
80 % in many cases two colors may differ by a small amount. The fuzz member of
81 % image defines how much tolerance is acceptable to consider two colors as
82 % the same. For example, set fuzz to 10 and the color red at intensities of
83 % 100 and 102 respectively are now interpreted as the same color for the
84 % purposes of the floodfill.
86 % The format of the FloodfillPaintImage method is:
88 % MagickBooleanType FloodfillPaintImage(Image *image,
89 % const DrawInfo *draw_info,const PixelInfo target,
90 % const ssize_t x_offset,const ssize_t y_offset,
91 % const MagickBooleanType invert,ExceptionInfo *exception)
93 % A description of each parameter follows:
97 % o draw_info: the draw info.
99 % o target: the RGB value of the target color.
101 % o x_offset,y_offset: the starting location of the operation.
103 % o invert: paint any pixel that does not match the target color.
105 % o exception: return any errors or warnings in this structure.
108 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
109 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
110 const ssize_t y_offset,const MagickBooleanType invert,
111 ExceptionInfo *exception)
113 #define MaxStacksize 131072UL
114 #define PushSegmentStack(up,left,right,delta) \
116 if (s >= (segment_stack+MaxStacksize)) \
117 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
120 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
122 s->x1=(double) (left); \
123 s->y1=(double) (up); \
124 s->x2=(double) (right); \
125 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 (IsGrayColorspace(image->colorspace) != MagickFalse)
176 (void) TransformImageColorspace(image,sRGBColorspace,exception);
177 if ((image->alpha_trait != BlendPixelTrait) &&
178 (draw_info->fill.alpha_trait == BlendPixelTrait))
179 (void) SetImageAlpha(image,OpaqueAlpha,exception);
183 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
185 if (floodplane_image == (Image *) NULL)
187 floodplane_image->alpha_trait=UndefinedPixelTrait;
188 floodplane_image->colorspace=GRAYColorspace;
189 (void) QueryColorCompliance("#000",AllCompliance,
190 &floodplane_image->background_color,exception);
191 (void) SetImageBackgroundColor(floodplane_image,exception);
192 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
193 sizeof(*segment_stack));
194 if (segment_stack == (SegmentInfo *) NULL)
196 floodplane_image=DestroyImage(floodplane_image);
197 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
201 Push initial segment on stack.
208 PushSegmentStack(y,x,x,1);
209 PushSegmentStack(y+1,x,x,-1);
210 GetPixelInfo(image,&pixel);
211 image_view=AcquireVirtualCacheView(image,exception);
212 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
213 while (s > segment_stack)
215 register const Quantum
225 Pop segment off stack.
230 offset=(ssize_t) s->y2;
231 y=(ssize_t) s->y1+offset;
233 Recolor neighboring pixels.
235 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
236 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
238 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
240 p+=x1*GetPixelChannels(image);
241 q+=x1*GetPixelChannels(floodplane_image);
242 for (x=x1; x >= 0; x--)
244 if (GetPixelGray(floodplane_image,q) != 0)
246 GetPixelInfoPixel(image,p,&pixel);
247 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
249 SetPixelGray(floodplane_image,QuantumRange,q);
250 p-=GetPixelChannels(image);
251 q-=GetPixelChannels(floodplane_image);
253 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
255 skip=x >= x1 ? MagickTrue : MagickFalse;
256 if (skip == MagickFalse)
260 PushSegmentStack(y,start,x1-1,-offset);
265 if (skip == MagickFalse)
267 if (x < (ssize_t) image->columns)
269 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
271 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
273 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
275 for ( ; x < (ssize_t) image->columns; x++)
277 if (GetPixelGray(floodplane_image,q) != 0)
279 GetPixelInfoPixel(image,p,&pixel);
280 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
282 SetPixelGray(floodplane_image,QuantumRange,q);
283 p+=GetPixelChannels(image);
284 q+=GetPixelChannels(floodplane_image);
286 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
287 if (status == MagickFalse)
290 PushSegmentStack(y,start,x-1,offset);
292 PushSegmentStack(y,x2+1,x-1,-offset);
298 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
300 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
302 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
304 for ( ; x <= x2; x++)
306 if (GetPixelGray(floodplane_image,q) != 0)
308 GetPixelInfoPixel(image,p,&pixel);
309 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
311 p+=GetPixelChannels(image);
312 q+=GetPixelChannels(floodplane_image);
318 for (y=0; y < (ssize_t) image->rows; y++)
320 register const Quantum
330 Tile fill color onto floodplane.
332 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
333 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
334 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
336 for (x=0; x < (ssize_t) image->columns; x++)
338 if (GetPixelGray(floodplane_image,p) != 0)
340 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
341 SetPixelInfoPixel(image,&fill_color,q);
343 p+=GetPixelChannels(floodplane_image);
344 q+=GetPixelChannels(image);
346 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
349 floodplane_view=DestroyCacheView(floodplane_view);
350 image_view=DestroyCacheView(image_view);
351 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
352 floodplane_image=DestroyImage(floodplane_image);
353 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 + G r a d i e n t I m a g e %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % GradientImage() applies a continuously smooth color transitions along a
368 % vector from one color to another.
370 % Note, the interface of this method will change in the future to support
371 % more than one transistion.
373 % The format of the GradientImage method is:
375 % MagickBooleanType GradientImage(Image *image,const GradientType type,
376 % const SpreadMethod method,const PixelInfo *start_color,
377 % const PixelInfo *stop_color,ExceptionInfo *exception)
379 % A description of each parameter follows:
381 % o image: the image.
383 % o type: the gradient type: linear or radial.
385 % o spread: the gradient spread meathod: pad, reflect, or repeat.
387 % o start_color: the start color.
389 % o stop_color: the stop color.
391 % o exception: return any errors or warnings in this structure.
395 static inline double MagickMax(const double x,const double y)
397 return(x > y ? x : y);
400 MagickExport MagickBooleanType GradientImage(Image *image,
401 const GradientType type,const SpreadMethod method,
402 const PixelInfo *start_color,const PixelInfo *stop_color,
403 ExceptionInfo *exception)
418 Set gradient start-stop end points.
420 assert(image != (const Image *) NULL);
421 assert(image->signature == MagickSignature);
422 if (image->debug != MagickFalse)
423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
424 assert(start_color != (const PixelInfo *) NULL);
425 assert(stop_color != (const PixelInfo *) NULL);
426 draw_info=AcquireDrawInfo();
427 gradient=(&draw_info->gradient);
429 gradient->bounding_box.width=image->columns;
430 gradient->bounding_box.height=image->rows;
431 gradient->gradient_vector.x2=(double) image->columns-1.0;
432 gradient->gradient_vector.y2=(double) image->rows-1.0;
433 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
434 gradient->gradient_vector.x2=0.0;
435 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
436 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
437 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
438 gradient->spread=method;
440 Define the gradient to fill between the stops.
442 gradient->number_stops=2;
443 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
444 sizeof(*gradient->stops));
445 if (gradient->stops == (StopInfo *) NULL)
446 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
448 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
449 sizeof(*gradient->stops));
450 for (i=0; i < (ssize_t) gradient->number_stops; i++)
451 GetPixelInfo(image,&gradient->stops[i].color);
452 gradient->stops[0].color=(*start_color);
453 gradient->stops[0].offset=0.0;
454 gradient->stops[1].color=(*stop_color);
455 gradient->stops[1].offset=1.0;
457 Draw a gradient on the image.
459 (void) SetImageColorspace(image,start_color->colorspace,exception);
460 status=DrawGradientImage(image,draw_info,exception);
461 draw_info=DestroyDrawInfo(draw_info);
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 % O i l P a i n t I m a g e %
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % OilPaintImage() applies a special effect filter that simulates an oil
477 % painting. Each pixel is replaced by the most frequent color occurring
478 % in a circular region defined by radius.
480 % The format of the OilPaintImage method is:
482 % Image *OilPaintImage(const Image *image,const double radius,
483 % const double sigma,ExceptionInfo *exception)
485 % A description of each parameter follows:
487 % o image: the image.
489 % o radius: the radius of the circular neighborhood.
491 % o sigma: the standard deviation of the Gaussian, in pixels.
493 % o exception: return any errors or warnings in this structure.
497 static size_t **DestroyHistogramThreadSet(size_t **histogram)
502 assert(histogram != (size_t **) NULL);
503 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
504 if (histogram[i] != (size_t *) NULL)
505 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
506 histogram=(size_t **) RelinquishMagickMemory(histogram);
510 static size_t **AcquireHistogramThreadSet(const size_t count)
519 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
520 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
521 if (histogram == (size_t **) NULL)
522 return((size_t **) NULL);
523 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
524 for (i=0; i < (ssize_t) number_threads; i++)
526 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
527 if (histogram[i] == (size_t *) NULL)
528 return(DestroyHistogramThreadSet(histogram));
533 MagickExport Image *OilPaintImage(const Image *image,const double radius,
534 const double sigma,ExceptionInfo *exception)
536 #define NumberPaintBins 256
537 #define OilPaintImageTag "OilPaint/Image"
562 Initialize painted image attributes.
564 assert(image != (const Image *) NULL);
565 assert(image->signature == MagickSignature);
566 if (image->debug != MagickFalse)
567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
568 assert(exception != (ExceptionInfo *) NULL);
569 assert(exception->signature == MagickSignature);
570 width=GetOptimalKernelWidth2D(radius,sigma);
571 linear_image=CloneImage(image,0,0,MagickTrue,exception);
572 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
573 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
575 if (linear_image != (Image *) NULL)
576 linear_image=DestroyImage(linear_image);
577 if (paint_image != (Image *) NULL)
578 linear_image=DestroyImage(paint_image);
579 return((Image *) NULL);
581 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
583 linear_image=DestroyImage(linear_image);
584 paint_image=DestroyImage(paint_image);
585 return((Image *) NULL);
587 histograms=AcquireHistogramThreadSet(NumberPaintBins);
588 if (histograms == (size_t **) NULL)
590 linear_image=DestroyImage(linear_image);
591 paint_image=DestroyImage(paint_image);
592 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
599 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
600 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
601 image_view=AcquireVirtualCacheView(linear_image,exception);
602 paint_view=AcquireAuthenticCacheView(paint_image,exception);
603 #if defined(MAGICKCORE_OPENMP_SUPPORT)
604 #pragma omp parallel for schedule(static,4) shared(progress,status) \
605 magick_threads(linear_image,paint_image,linear_image->rows,1)
607 for (y=0; y < (ssize_t) linear_image->rows; y++)
609 register const Quantum
621 if (status == MagickFalse)
623 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
624 (width/2L),linear_image->columns+width,width,exception);
625 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
627 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
632 histogram=histograms[GetOpenMPThreadId()];
633 for (x=0; x < (ssize_t) linear_image->columns; x++)
649 Assign most frequent color.
654 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
655 for (v=0; v < (ssize_t) width; v++)
657 for (u=0; u < (ssize_t) width; u++)
659 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
660 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
662 if (histogram[n] > count)
668 k+=(ssize_t) (linear_image->columns+width);
670 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
672 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
673 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
674 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
675 if ((traits == UndefinedPixelTrait) ||
676 (paint_traits == UndefinedPixelTrait))
678 if (((paint_traits & CopyPixelTrait) != 0) ||
679 (GetPixelMask(linear_image,p) != 0))
681 SetPixelChannel(paint_image,channel,p[center+i],q);
684 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
687 p+=GetPixelChannels(linear_image);
688 q+=GetPixelChannels(paint_image);
690 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
692 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
697 #if defined(MAGICKCORE_OPENMP_SUPPORT)
698 #pragma omp critical (MagickCore_OilPaintImage)
700 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
702 if (proceed == MagickFalse)
706 paint_view=DestroyCacheView(paint_view);
707 image_view=DestroyCacheView(image_view);
708 histograms=DestroyHistogramThreadSet(histograms);
709 linear_image=DestroyImage(linear_image);
710 if (status == MagickFalse)
711 paint_image=DestroyImage(paint_image);
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % O p a q u e P a i n t I m a g e %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % OpaquePaintImage() changes any pixel that matches color with the color
729 % By default color must match a particular pixel color exactly. However, in
730 % many cases two colors may differ by a small amount. Fuzz defines how much
731 % tolerance is acceptable to consider two colors as the same. For example,
732 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
733 % are now interpreted as the same color.
735 % The format of the OpaquePaintImage method is:
737 % MagickBooleanType OpaquePaintImage(Image *image,
738 % const PixelInfo *target,const PixelInfo *fill,
739 % const MagickBooleanType invert,ExceptionInfo *exception)
741 % A description of each parameter follows:
743 % o image: the image.
745 % o target: the RGB value of the target color.
747 % o fill: the replacement color.
749 % o invert: paint any pixel that does not match the target color.
751 % o exception: return any errors or warnings in this structure.
754 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
755 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
756 ExceptionInfo *exception)
758 #define OpaquePaintImageTag "Opaque/Image"
775 assert(image != (Image *) NULL);
776 assert(image->signature == MagickSignature);
777 assert(target != (PixelInfo *) NULL);
778 assert(fill != (PixelInfo *) NULL);
779 if (image->debug != MagickFalse)
780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
781 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
783 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
784 (IsPixelInfoGray(fill) == MagickFalse))
785 (void) TransformImageColorspace(image,sRGBColorspace,exception);
786 if ((fill->alpha_trait == BlendPixelTrait) &&
787 (image->alpha_trait != BlendPixelTrait))
788 (void) SetImageAlpha(image,OpaqueAlpha,exception);
790 Make image color opaque.
794 GetPixelInfo(image,&zero);
795 image_view=AcquireAuthenticCacheView(image,exception);
796 #if defined(MAGICKCORE_OPENMP_SUPPORT)
797 #pragma omp parallel for schedule(static,4) shared(progress,status) \
798 magick_threads(image,image,image->rows,1)
800 for (y=0; y < (ssize_t) image->rows; y++)
811 if (status == MagickFalse)
813 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
814 if (q == (Quantum *) NULL)
820 for (x=0; x < (ssize_t) image->columns; x++)
822 GetPixelInfoPixel(image,q,&pixel);
823 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
824 SetPixelInfoPixel(image,fill,q);
825 q+=GetPixelChannels(image);
827 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
829 if (image->progress_monitor != (MagickProgressMonitor) NULL)
834 #if defined(MAGICKCORE_OPENMP_SUPPORT)
835 #pragma omp critical (MagickCore_OpaquePaintImage)
837 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
839 if (proceed == MagickFalse)
843 image_view=DestroyCacheView(image_view);
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 % T r a n s p a r e n t P a i n t I m a g e %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % TransparentPaintImage() changes the opacity value associated with any pixel
859 % that matches color to the value defined by opacity.
861 % By default color must match a particular pixel color exactly. However, in
862 % many cases two colors may differ by a small amount. Fuzz defines how much
863 % tolerance is acceptable to consider two colors as the same. For example,
864 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
865 % are now interpreted as the same color.
867 % The format of the TransparentPaintImage method is:
869 % MagickBooleanType TransparentPaintImage(Image *image,
870 % const PixelInfo *target,const Quantum opacity,
871 % const MagickBooleanType invert,ExceptionInfo *exception)
873 % A description of each parameter follows:
875 % o image: the image.
877 % o target: the target color.
879 % o opacity: the replacement opacity value.
881 % o invert: paint any pixel that does not match the target color.
883 % o exception: return any errors or warnings in this structure.
886 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
887 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
888 ExceptionInfo *exception)
890 #define TransparentPaintImageTag "Transparent/Image"
907 assert(image != (Image *) NULL);
908 assert(image->signature == MagickSignature);
909 assert(target != (PixelInfo *) NULL);
910 if (image->debug != MagickFalse)
911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
912 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
914 if (image->alpha_trait != BlendPixelTrait)
915 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
917 Make image color transparent.
921 GetPixelInfo(image,&zero);
922 image_view=AcquireAuthenticCacheView(image,exception);
923 #if defined(MAGICKCORE_OPENMP_SUPPORT)
924 #pragma omp parallel for schedule(static,4) shared(progress,status) \
925 magick_threads(image,image,image->rows,1)
927 for (y=0; y < (ssize_t) image->rows; y++)
938 if (status == MagickFalse)
940 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
941 if (q == (Quantum *) NULL)
947 for (x=0; x < (ssize_t) image->columns; x++)
949 GetPixelInfoPixel(image,q,&pixel);
950 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
951 SetPixelAlpha(image,opacity,q);
952 q+=GetPixelChannels(image);
954 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
956 if (image->progress_monitor != (MagickProgressMonitor) NULL)
961 #if defined(MAGICKCORE_OPENMP_SUPPORT)
962 #pragma omp critical (MagickCore_TransparentPaintImage)
964 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
966 if (proceed == MagickFalse)
970 image_view=DestroyCacheView(image_view);
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % 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 %
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 % TransparentPaintImageChroma() changes the opacity value associated with any
986 % pixel that matches color to the value defined by opacity.
988 % As there is one fuzz value for the all the channels, TransparentPaintImage()
989 % is not suitable for the operations like chroma, where the tolerance for
990 % similarity of two color component (RGB) can be different. Thus we define
991 % this method to take two target pixels (one low and one high) and all the
992 % pixels of an image which are lying between these two pixels are made
995 % The format of the TransparentPaintImageChroma method is:
997 % MagickBooleanType TransparentPaintImageChroma(Image *image,
998 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
999 % const MagickBooleanType invert,ExceptionInfo *exception)
1001 % A description of each parameter follows:
1003 % o image: the image.
1005 % o low: the low target color.
1007 % o high: the high target color.
1009 % o opacity: the replacement opacity value.
1011 % o invert: paint any pixel that does not match the target color.
1013 % o exception: return any errors or warnings in this structure.
1016 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1017 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1018 const MagickBooleanType invert,ExceptionInfo *exception)
1020 #define TransparentPaintImageTag "Transparent/Image"
1034 assert(image != (Image *) NULL);
1035 assert(image->signature == MagickSignature);
1036 assert(high != (PixelInfo *) NULL);
1037 assert(low != (PixelInfo *) NULL);
1038 if (image->debug != MagickFalse)
1039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1040 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1041 return(MagickFalse);
1042 if (image->alpha_trait != BlendPixelTrait)
1043 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1045 Make image color transparent.
1049 image_view=AcquireAuthenticCacheView(image,exception);
1050 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1051 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1052 magick_threads(image,image,image->rows,1)
1054 for (y=0; y < (ssize_t) image->rows; y++)
1068 if (status == MagickFalse)
1070 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1071 if (q == (Quantum *) NULL)
1076 GetPixelInfo(image,&pixel);
1077 for (x=0; x < (ssize_t) image->columns; x++)
1079 GetPixelInfoPixel(image,q,&pixel);
1080 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1081 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1082 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1084 if (match != invert)
1085 SetPixelAlpha(image,opacity,q);
1086 q+=GetPixelChannels(image);
1088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1090 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1095 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1096 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1098 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1100 if (proceed == MagickFalse)
1104 image_view=DestroyCacheView(image_view);