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 (1UL << 17)
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,RGBColorspace,exception);
177 if ((image->alpha_trait != BlendPixelTrait) && (draw_info->fill.alpha_trait == BlendPixelTrait))
178 (void) SetImageAlpha(image,OpaqueAlpha,exception);
182 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
184 if (floodplane_image == (Image *) NULL)
186 floodplane_image->alpha_trait=UndefinedPixelTrait;
187 floodplane_image->colorspace=GRAYColorspace;
188 (void) QueryColorCompliance("#000",AllCompliance,
189 &floodplane_image->background_color,exception);
190 (void) SetImageBackgroundColor(floodplane_image,exception);
191 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
192 sizeof(*segment_stack));
193 if (segment_stack == (SegmentInfo *) NULL)
195 floodplane_image=DestroyImage(floodplane_image);
196 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
200 Push initial segment on stack.
207 PushSegmentStack(y,x,x,1);
208 PushSegmentStack(y+1,x,x,-1);
209 GetPixelInfo(image,&pixel);
210 image_view=AcquireVirtualCacheView(image,exception);
211 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
212 while (s > segment_stack)
214 register const Quantum
224 Pop segment off stack.
229 offset=(ssize_t) s->y2;
230 y=(ssize_t) s->y1+offset;
232 Recolor neighboring pixels.
234 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
235 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
237 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
239 p+=x1*GetPixelChannels(image);
240 q+=x1*GetPixelChannels(floodplane_image);
241 for (x=x1; x >= 0; x--)
243 if (GetPixelGray(floodplane_image,q) != 0)
245 GetPixelInfoPixel(image,p,&pixel);
246 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
248 SetPixelGray(floodplane_image,QuantumRange,q);
249 p-=GetPixelChannels(image);
250 q-=GetPixelChannels(floodplane_image);
252 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
254 skip=x >= x1 ? MagickTrue : MagickFalse;
255 if (skip == MagickFalse)
259 PushSegmentStack(y,start,x1-1,-offset);
264 if (skip == MagickFalse)
266 if (x < (ssize_t) image->columns)
268 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
270 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
272 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
274 for ( ; x < (ssize_t) image->columns; x++)
276 if (GetPixelGray(floodplane_image,q) != 0)
278 GetPixelInfoPixel(image,p,&pixel);
279 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
281 SetPixelGray(floodplane_image,QuantumRange,q);
282 p+=GetPixelChannels(image);
283 q+=GetPixelChannels(floodplane_image);
285 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
286 if (status == MagickFalse)
289 PushSegmentStack(y,start,x-1,offset);
291 PushSegmentStack(y,x2+1,x-1,-offset);
297 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
299 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
301 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
303 for ( ; x <= x2; x++)
305 if (GetPixelGray(floodplane_image,q) != 0)
307 GetPixelInfoPixel(image,p,&pixel);
308 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
310 p+=GetPixelChannels(image);
311 q+=GetPixelChannels(floodplane_image);
317 for (y=0; y < (ssize_t) image->rows; y++)
319 register const Quantum
329 Tile fill color onto floodplane.
331 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
332 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
333 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
335 for (x=0; x < (ssize_t) image->columns; x++)
337 if (GetPixelGray(floodplane_image,p) != 0)
339 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
340 SetPixelInfoPixel(image,&fill_color,q);
342 p+=GetPixelChannels(floodplane_image);
343 q+=GetPixelChannels(image);
345 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
348 floodplane_view=DestroyCacheView(floodplane_view);
349 image_view=DestroyCacheView(image_view);
350 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
351 floodplane_image=DestroyImage(floodplane_image);
352 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 + G r a d i e n t I m a g e %
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 % GradientImage() applies a continuously smooth color transitions along a
367 % vector from one color to another.
369 % Note, the interface of this method will change in the future to support
370 % more than one transistion.
372 % The format of the GradientImage method is:
374 % MagickBooleanType GradientImage(Image *image,const GradientType type,
375 % const SpreadMethod method,const PixelInfo *start_color,
376 % const PixelInfo *stop_color,ExceptionInfo *exception)
378 % A description of each parameter follows:
380 % o image: the image.
382 % o type: the gradient type: linear or radial.
384 % o spread: the gradient spread meathod: pad, reflect, or repeat.
386 % o start_color: the start color.
388 % o stop_color: the stop color.
390 % o exception: return any errors or warnings in this structure.
394 static inline double MagickMax(const double x,const double y)
396 return(x > y ? x : y);
399 MagickExport MagickBooleanType GradientImage(Image *image,
400 const GradientType type,const SpreadMethod method,
401 const PixelInfo *start_color,const PixelInfo *stop_color,
402 ExceptionInfo *exception)
417 Set gradient start-stop end points.
419 assert(image != (const Image *) NULL);
420 assert(image->signature == MagickSignature);
421 if (image->debug != MagickFalse)
422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
423 assert(start_color != (const PixelInfo *) NULL);
424 assert(stop_color != (const PixelInfo *) NULL);
425 draw_info=AcquireDrawInfo();
426 gradient=(&draw_info->gradient);
428 gradient->bounding_box.width=image->columns;
429 gradient->bounding_box.height=image->rows;
430 gradient->gradient_vector.x2=(double) image->columns-1.0;
431 gradient->gradient_vector.y2=(double) image->rows-1.0;
432 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
433 gradient->gradient_vector.x2=0.0;
434 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
435 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
436 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
437 gradient->spread=method;
439 Define the gradient to fill between the stops.
441 gradient->number_stops=2;
442 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
443 sizeof(*gradient->stops));
444 if (gradient->stops == (StopInfo *) NULL)
445 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
447 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
448 sizeof(*gradient->stops));
449 for (i=0; i < (ssize_t) gradient->number_stops; i++)
450 GetPixelInfo(image,&gradient->stops[i].color);
451 gradient->stops[0].color=(*start_color);
452 gradient->stops[0].offset=0.0;
453 gradient->stops[1].color=(*stop_color);
454 gradient->stops[1].offset=1.0;
456 Draw a gradient on the image.
458 (void) SetImageColorspace(image,start_color->colorspace,exception);
459 status=DrawGradientImage(image,draw_info,exception);
460 draw_info=DestroyDrawInfo(draw_info);
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) GetMagickResourceLimit(ThreadResource); 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=(size_t) GetMagickResourceLimit(ThreadResource);
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"
561 Initialize painted image attributes.
563 assert(image != (const Image *) NULL);
564 assert(image->signature == MagickSignature);
565 if (image->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567 assert(exception != (ExceptionInfo *) NULL);
568 assert(exception->signature == MagickSignature);
569 width=GetOptimalKernelWidth2D(radius,sigma);
570 linear_image=CloneImage(image,0,0,MagickTrue,exception);
571 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
572 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
574 if (linear_image != (Image *) NULL)
575 linear_image=DestroyImage(linear_image);
576 if (paint_image != (Image *) NULL)
577 linear_image=DestroyImage(paint_image);
578 return((Image *) NULL);
580 if (image->colorspace == sRGBColorspace)
581 (void) TransformImageColorspace(linear_image,sRGBColorspace,exception);
582 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
584 linear_image=DestroyImage(linear_image);
585 paint_image=DestroyImage(paint_image);
586 return((Image *) NULL);
588 histograms=AcquireHistogramThreadSet(NumberPaintBins);
589 if (histograms == (size_t **) NULL)
591 linear_image=DestroyImage(linear_image);
592 paint_image=DestroyImage(paint_image);
593 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
600 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
601 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
602 image_view=AcquireVirtualCacheView(linear_image,exception);
603 paint_view=AcquireAuthenticCacheView(paint_image,exception);
604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
605 #pragma omp parallel for schedule(static,4) shared(progress,status) \
606 magick_threads(linear_image,paint_image,linear_image->rows,1)
608 for (y=0; y < (ssize_t) linear_image->rows; y++)
610 register const Quantum
622 if (status == MagickFalse)
624 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
625 (width/2L),linear_image->columns+width,width,exception);
626 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
628 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
633 histogram=histograms[GetOpenMPThreadId()];
634 for (x=0; x < (ssize_t) linear_image->columns; x++)
650 Assign most frequent color.
655 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
656 for (v=0; v < (ssize_t) width; v++)
658 for (u=0; u < (ssize_t) width; u++)
660 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
661 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
663 if (histogram[n] > count)
669 k+=(ssize_t) (linear_image->columns+width);
671 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
680 channel=GetPixelChannelChannel(linear_image,i);
681 traits=GetPixelChannelTraits(linear_image,channel);
682 paint_traits=GetPixelChannelTraits(paint_image,channel);
683 if ((traits == UndefinedPixelTrait) ||
684 (paint_traits == UndefinedPixelTrait))
686 if (((paint_traits & CopyPixelTrait) != 0) ||
687 (GetPixelMask(linear_image,p) != 0))
689 SetPixelChannel(paint_image,channel,p[center+i],q);
692 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
695 p+=GetPixelChannels(linear_image);
696 q+=GetPixelChannels(paint_image);
698 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
700 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
705 #if defined(MAGICKCORE_OPENMP_SUPPORT)
706 #pragma omp critical (MagickCore_OilPaintImage)
708 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
710 if (proceed == MagickFalse)
714 paint_view=DestroyCacheView(paint_view);
715 image_view=DestroyCacheView(image_view);
716 histograms=DestroyHistogramThreadSet(histograms);
717 linear_image=DestroyImage(linear_image);
718 if (image->colorspace == sRGBColorspace)
719 (void) TransformImageColorspace(paint_image,sRGBColorspace,exception);
720 if (status == MagickFalse)
721 paint_image=DestroyImage(paint_image);
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % O p a q u e P a i n t I m a g e %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % OpaquePaintImage() changes any pixel that matches color with the color
739 % By default color must match a particular pixel color exactly. However, in
740 % many cases two colors may differ by a small amount. Fuzz defines how much
741 % tolerance is acceptable to consider two colors as the same. For example,
742 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
743 % are now interpreted as the same color.
745 % The format of the OpaquePaintImage method is:
747 % MagickBooleanType OpaquePaintImage(Image *image,
748 % const PixelInfo *target,const PixelInfo *fill,
749 % const MagickBooleanType invert,ExceptionInfo *exception)
751 % A description of each parameter follows:
753 % o image: the image.
755 % o target: the RGB value of the target color.
757 % o fill: the replacement color.
759 % o invert: paint any pixel that does not match the target color.
761 % o exception: return any errors or warnings in this structure.
764 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
765 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
766 ExceptionInfo *exception)
768 #define OpaquePaintImageTag "Opaque/Image"
785 assert(image != (Image *) NULL);
786 assert(image->signature == MagickSignature);
787 assert(target != (PixelInfo *) NULL);
788 assert(fill != (PixelInfo *) NULL);
789 if (image->debug != MagickFalse)
790 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
791 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
793 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
794 (IsPixelInfoGray(fill) == MagickFalse))
795 (void) TransformImageColorspace(image,RGBColorspace,exception);
796 if ((fill->alpha_trait == BlendPixelTrait) && (image->alpha_trait != BlendPixelTrait))
797 (void) SetImageAlpha(image,OpaqueAlpha,exception);
799 Make image color opaque.
803 GetPixelInfo(image,&zero);
804 image_view=AcquireAuthenticCacheView(image,exception);
805 #if defined(MAGICKCORE_OPENMP_SUPPORT)
806 #pragma omp parallel for schedule(static,4) shared(progress,status) \
807 magick_threads(image,image,image->rows,1)
809 for (y=0; y < (ssize_t) image->rows; y++)
820 if (status == MagickFalse)
822 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
823 if (q == (Quantum *) NULL)
829 for (x=0; x < (ssize_t) image->columns; x++)
831 GetPixelInfoPixel(image,q,&pixel);
832 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
833 SetPixelInfoPixel(image,fill,q);
834 q+=GetPixelChannels(image);
836 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
838 if (image->progress_monitor != (MagickProgressMonitor) NULL)
843 #if defined(MAGICKCORE_OPENMP_SUPPORT)
844 #pragma omp critical (MagickCore_OpaquePaintImage)
846 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
848 if (proceed == MagickFalse)
852 image_view=DestroyCacheView(image_view);
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % T r a n s p a r e n t P a i n t I m a g e %
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % TransparentPaintImage() changes the opacity value associated with any pixel
868 % that matches color to the value defined by opacity.
870 % By default color must match a particular pixel color exactly. However, in
871 % many cases two colors may differ by a small amount. Fuzz defines how much
872 % tolerance is acceptable to consider two colors as the same. For example,
873 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
874 % are now interpreted as the same color.
876 % The format of the TransparentPaintImage method is:
878 % MagickBooleanType TransparentPaintImage(Image *image,
879 % const PixelInfo *target,const Quantum opacity,
880 % const MagickBooleanType invert,ExceptionInfo *exception)
882 % A description of each parameter follows:
884 % o image: the image.
886 % o target: the target color.
888 % o opacity: the replacement opacity value.
890 % o invert: paint any pixel that does not match the target color.
892 % o exception: return any errors or warnings in this structure.
895 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
896 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
897 ExceptionInfo *exception)
899 #define TransparentPaintImageTag "Transparent/Image"
916 assert(image != (Image *) NULL);
917 assert(image->signature == MagickSignature);
918 assert(target != (PixelInfo *) NULL);
919 if (image->debug != MagickFalse)
920 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
921 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
923 if (image->alpha_trait != BlendPixelTrait)
924 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
926 Make image color transparent.
930 GetPixelInfo(image,&zero);
931 image_view=AcquireAuthenticCacheView(image,exception);
932 #if defined(MAGICKCORE_OPENMP_SUPPORT)
933 #pragma omp parallel for schedule(static,4) shared(progress,status) \
934 magick_threads(image,image,image->rows,1)
936 for (y=0; y < (ssize_t) image->rows; y++)
947 if (status == MagickFalse)
949 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
950 if (q == (Quantum *) NULL)
956 for (x=0; x < (ssize_t) image->columns; x++)
958 GetPixelInfoPixel(image,q,&pixel);
959 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
960 SetPixelAlpha(image,opacity,q);
961 q+=GetPixelChannels(image);
963 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
965 if (image->progress_monitor != (MagickProgressMonitor) NULL)
970 #if defined(MAGICKCORE_OPENMP_SUPPORT)
971 #pragma omp critical (MagickCore_TransparentPaintImage)
973 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
975 if (proceed == MagickFalse)
979 image_view=DestroyCacheView(image_view);
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 % 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 %
992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994 % TransparentPaintImageChroma() changes the opacity value associated with any
995 % pixel that matches color to the value defined by opacity.
997 % As there is one fuzz value for the all the channels, TransparentPaintImage()
998 % is not suitable for the operations like chroma, where the tolerance for
999 % similarity of two color component (RGB) can be different. Thus we define
1000 % this method to take two target pixels (one low and one high) and all the
1001 % pixels of an image which are lying between these two pixels are made
1004 % The format of the TransparentPaintImageChroma method is:
1006 % MagickBooleanType TransparentPaintImageChroma(Image *image,
1007 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1008 % const MagickBooleanType invert,ExceptionInfo *exception)
1010 % A description of each parameter follows:
1012 % o image: the image.
1014 % o low: the low target color.
1016 % o high: the high target color.
1018 % o opacity: the replacement opacity value.
1020 % o invert: paint any pixel that does not match the target color.
1022 % o exception: return any errors or warnings in this structure.
1025 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1026 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1027 const MagickBooleanType invert,ExceptionInfo *exception)
1029 #define TransparentPaintImageTag "Transparent/Image"
1043 assert(image != (Image *) NULL);
1044 assert(image->signature == MagickSignature);
1045 assert(high != (PixelInfo *) NULL);
1046 assert(low != (PixelInfo *) NULL);
1047 if (image->debug != MagickFalse)
1048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1049 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1050 return(MagickFalse);
1051 if (image->alpha_trait != BlendPixelTrait)
1052 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1054 Make image color transparent.
1058 image_view=AcquireAuthenticCacheView(image,exception);
1059 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1060 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1061 magick_threads(image,image,image->rows,1)
1063 for (y=0; y < (ssize_t) image->rows; y++)
1077 if (status == MagickFalse)
1079 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1080 if (q == (Quantum *) NULL)
1085 GetPixelInfo(image,&pixel);
1086 for (x=0; x < (ssize_t) image->columns; x++)
1088 GetPixelInfoPixel(image,q,&pixel);
1089 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1090 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1091 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1093 if (match != invert)
1094 SetPixelAlpha(image,opacity,q);
1095 q+=GetPixelChannels(image);
1097 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1099 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1104 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1105 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1107 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1109 if (proceed == MagickFalse)
1113 image_view=DestroyCacheView(image_view);