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-2009 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 "magick/studio.h"
43 #include "magick/color.h"
44 #include "magick/color-private.h"
45 #include "magick/colorspace-private.h"
46 #include "magick/composite.h"
47 #include "magick/composite-private.h"
48 #include "magick/draw.h"
49 #include "magick/draw-private.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/gem.h"
53 #include "magick/monitor.h"
54 #include "magick/monitor-private.h"
55 #include "magick/paint.h"
56 #include "magick/pixel-private.h"
57 #include "magick/string_.h"
58 #include "magick/thread-private.h"
60 static inline double MagickMax(const double x,const double y)
62 return( x > y ? x : y);
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % F l o o d f i l l P a i n t I m a g e %
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % FloodfillPaintImage() changes the color value of any pixel that matches
76 % target and is an immediate neighbor. If the method FillToBorderMethod is
77 % specified, the color value is changed for any neighbor pixel that does not
78 % match the bordercolor member of image.
80 % By default target must match a particular pixel color exactly.
81 % However, in many cases two colors may differ by a small amount. The
82 % fuzz member of image defines how much tolerance is acceptable to
83 % consider two colors as the same. For example, set fuzz to 10 and the
84 % color red at intensities of 100 and 102 respectively are now
85 % interpreted as the same color for the purposes of the floodfill.
87 % The format of the FloodfillPaintImage method is:
89 % MagickBooleanType FloodfillPaintImage(Image *image,
90 % const ChannelType channel,const DrawInfo *draw_info,
91 % const MagickPixelPacket target,const long x_offset,const long y_offset,
92 % const MagickBooleanType invert)
94 % A description of each parameter follows:
98 % o channel: the channel(s).
100 % o draw_info: the draw info.
102 % o target: the RGB value of the target color.
104 % o x_offset,y_offset: the starting location of the operation.
106 % o invert: paint any pixel that does not match the target color.
109 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
110 const ChannelType channel,const DrawInfo *draw_info,
111 const MagickPixelPacket *target,const long x_offset,const long y_offset,
112 const MagickBooleanType invert)
114 #define MaxStacksize (1UL << 15)
115 #define PushSegmentStack(up,left,right,delta) \
117 if (s >= (segment_stack+MaxStacksize)) \
118 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
121 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (long) image->rows)) \
123 s->x1=(double) (left); \
124 s->y1=(double) (up); \
125 s->x2=(double) (right); \
126 s->y2=(double) (delta); \
163 Check boundary conditions.
165 assert(image != (Image *) NULL);
166 assert(image->signature == MagickSignature);
167 if (image->debug != MagickFalse)
168 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
169 assert(draw_info != (DrawInfo *) NULL);
170 assert(draw_info->signature == MagickSignature);
171 if ((x_offset < 0) || (x_offset >= (long) image->columns))
173 if ((y_offset < 0) || (y_offset >= (long) image->rows))
175 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
177 if (image->matte == MagickFalse)
178 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
182 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
183 if (floodplane_image == (Image *) NULL)
185 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel);
186 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
187 sizeof(*segment_stack));
188 if (segment_stack == (SegmentInfo *) NULL)
190 floodplane_image=DestroyImage(floodplane_image);
191 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
195 Push initial segment on stack.
197 exception=(&image->exception);
202 PushSegmentStack(y,x,x,1);
203 PushSegmentStack(y+1,x,x,-1);
204 GetMagickPixelPacket(image,&fill);
205 GetMagickPixelPacket(image,&pixel);
206 while (s > segment_stack)
208 register const IndexPacket
211 register const PixelPacket
221 Pop segment off stack.
227 y=(long) s->y1+offset;
229 Recolor neighboring pixels.
231 p=GetVirtualPixels(image,0,y,(unsigned long) (x1+1),1,exception);
232 q=GetAuthenticPixels(floodplane_image,0,y,(unsigned long) (x1+1),1,
234 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
236 indexes=GetVirtualIndexQueue(image);
239 for (x=x1; x >= 0; x--)
241 if (q->opacity == (Quantum) TransparentOpacity)
243 SetMagickPixelPacket(image,p,indexes+x,&pixel);
244 if (IsMagickColorSimilar(&pixel,target) == invert)
246 q->opacity=(Quantum) TransparentOpacity;
250 if (SyncAuthenticPixels(floodplane_image,exception) == MagickFalse)
252 skip=x >= x1 ? MagickTrue : MagickFalse;
253 if (skip == MagickFalse)
257 PushSegmentStack(y,start,x1-1,-offset);
262 if (skip == MagickFalse)
264 if (x < (long) image->columns)
266 p=GetVirtualPixels(image,x,y,image->columns-x,1,exception);
267 q=GetAuthenticPixels(floodplane_image,x,y,image->columns-x,1,
269 if ((p == (const PixelPacket *) NULL) ||
270 (q == (PixelPacket *) NULL))
272 indexes=GetVirtualIndexQueue(image);
273 for ( ; x < (long) image->columns; x++)
275 if (q->opacity == (Quantum) TransparentOpacity)
277 SetMagickPixelPacket(image,p,indexes+x,&pixel);
278 if (IsMagickColorSimilar(&pixel,target) == invert)
280 q->opacity=(Quantum) TransparentOpacity;
284 if (SyncAuthenticPixels(floodplane_image,exception) == MagickFalse)
287 PushSegmentStack(y,start,x-1,offset);
289 PushSegmentStack(y,x2+1,x-1,-offset);
295 p=GetVirtualPixels(image,x,y,(unsigned long) (x2-x+1),1,exception);
296 q=GetAuthenticPixels(floodplane_image,x,y,(unsigned long) (x2-x+1),1,
298 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
300 indexes=GetVirtualIndexQueue(image);
301 for ( ; x <= x2; x++)
303 if (q->opacity == (Quantum) TransparentOpacity)
305 SetMagickPixelPacket(image,p,indexes+x,&pixel);
306 if (IsMagickColorSimilar(&pixel,target) != invert)
315 for (y=0; y < (long) image->rows; y++)
317 register const PixelPacket
330 Tile fill color onto floodplane.
332 p=GetVirtualPixels(floodplane_image,0,y,image->columns,1,exception);
333 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
334 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
336 indexes=GetAuthenticIndexQueue(image);
337 for (x=0; x < (long) image->columns; x++)
339 if (p->opacity != OpaqueOpacity)
341 (void) GetFillColor(draw_info,x,y,&fill_color);
342 SetMagickPixelPacket(image,&fill_color,(IndexPacket *) NULL,&fill);
343 if (image->colorspace == CMYKColorspace)
344 ConvertRGBToCMYK(&fill);
345 if ((channel & RedChannel) != 0)
346 q->red=RoundToQuantum(fill.red);
347 if ((channel & GreenChannel) != 0)
348 q->green=RoundToQuantum(fill.green);
349 if ((channel & BlueChannel) != 0)
350 q->blue=RoundToQuantum(fill.blue);
351 if ((channel & OpacityChannel) != 0)
352 q->opacity=RoundToQuantum(fill.opacity);
353 if (((channel & IndexChannel) != 0) &&
354 (image->colorspace == CMYKColorspace))
355 indexes[x]=RoundToQuantum(fill.index);
360 if (SyncAuthenticPixels(image,exception) == MagickFalse)
363 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
364 floodplane_image=DestroyImage(floodplane_image);
365 return(y == (long) image->rows ? MagickTrue : MagickFalse);
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 + G r a d i e n t I m a g e %
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 % GradientImage() applies a continuously smooth color transitions along a
380 % vector from one color to another.
382 % Note, the interface of this method will change in the future to support
383 % more than one transistion.
385 % The format of the GradientImage method is:
387 % MagickBooleanType GradientImage(Image *image,const GradientType type,
388 % const SpreadMethod method,const PixelPacket *start_color,
389 % const PixelPacket *stop_color)
391 % A description of each parameter follows:
393 % o image: the image.
395 % o type: the gradient type: linear or radial.
397 % o spread: the gradient spread meathod: pad, reflect, or repeat.
399 % o start_color: the start color.
401 % o stop_color: the stop color.
403 % This provides a good example of making use of the DrawGradientImage
404 % function and the gradient structure in draw_info.
406 MagickExport MagickBooleanType GradientImage(Image *image,
407 const GradientType type,const SpreadMethod method,
408 const PixelPacket *start_color,const PixelPacket *stop_color)
423 Set gradient start-stop end points.
425 assert(image != (const Image *) NULL);
426 assert(image->signature == MagickSignature);
427 if (image->debug != MagickFalse)
428 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
429 assert(start_color != (const PixelPacket *) NULL);
430 assert(stop_color != (const PixelPacket *) NULL);
431 draw_info=AcquireDrawInfo();
432 gradient=(&draw_info->gradient);
434 gradient->bounding_box.width=image->columns;
435 gradient->bounding_box.height=image->rows;
436 gradient->gradient_vector.x2=(double) image->columns-1.0;
437 gradient->gradient_vector.y2=(double) image->rows-1.0;
438 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
439 gradient->gradient_vector.x2=0.0;
440 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
441 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
442 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
443 gradient->spread=method;
445 Define the gradient to fill between the stops.
447 gradient->number_stops=2;
448 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
449 sizeof(*gradient->stops));
450 if (gradient->stops == (StopInfo *) NULL)
451 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
453 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
454 sizeof(*gradient->stops));
455 for (i=0; i < (long) gradient->number_stops; i++)
456 GetMagickPixelPacket(image,&gradient->stops[i].color);
457 SetMagickPixelPacket(image,start_color,(IndexPacket *) NULL,
458 &gradient->stops[0].color);
459 gradient->stops[0].offset=0.0;
460 SetMagickPixelPacket(image,stop_color,(IndexPacket *) NULL,
461 &gradient->stops[1].color);
462 gradient->stops[1].offset=1.0;
464 Draw a gradient on the image.
466 status=DrawGradientImage(image,draw_info);
467 draw_info=DestroyDrawInfo(draw_info);
468 if ((start_color->opacity == OpaqueOpacity) &&
469 (stop_color->opacity == OpaqueOpacity))
470 image->matte=MagickFalse;
471 if ((IsGrayPixel(start_color) != MagickFalse) &&
472 (IsGrayPixel(stop_color) != MagickFalse))
473 image->type=GrayscaleType;
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % O i l P a i n t I m a g e %
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 % OilPaintImage() applies a special effect filter that simulates an oil
489 % painting. Each pixel is replaced by the most frequent color occurring
490 % in a circular region defined by radius.
492 % The format of the OilPaintImage method is:
494 % Image *OilPaintImage(const Image *image,const double radius,
495 % ExceptionInfo *exception)
497 % A description of each parameter follows:
499 % o image: the image.
501 % o radius: the radius of the circular neighborhood.
503 % o exception: return any errors or warnings in this structure.
507 static unsigned long **DestroyHistogramThreadSet(unsigned long **histogram)
512 assert(histogram != (unsigned long **) NULL);
513 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
514 if (histogram[i] != (unsigned long *) NULL)
515 histogram[i]=(unsigned long *) RelinquishMagickMemory(histogram[i]);
516 histogram=(unsigned long **) RelinquishAlignedMemory(histogram);
520 static unsigned long **AcquireHistogramThreadSet(const size_t count)
529 number_threads=GetOpenMPMaximumThreads();
530 histogram=(unsigned long **) AcquireAlignedMemory(number_threads,
532 if (histogram == (unsigned long **) NULL)
533 return((unsigned long **) NULL);
534 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
535 for (i=0; i < (long) number_threads; i++)
537 histogram[i]=(unsigned long *) AcquireQuantumMemory(count,
538 sizeof(**histogram));
539 if (histogram[i] == (unsigned long *) NULL)
540 return(DestroyHistogramThreadSet(histogram));
545 MagickExport Image *OilPaintImage(const Image *image,const double radius,
546 ExceptionInfo *exception)
548 #define NumberPaintBins 256
549 #define OilPaintImageTag "OilPaint/Image"
570 Initialize painted image attributes.
572 assert(image != (const Image *) NULL);
573 assert(image->signature == MagickSignature);
574 if (image->debug != MagickFalse)
575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
576 assert(exception != (ExceptionInfo *) NULL);
577 assert(exception->signature == MagickSignature);
578 width=GetOptimalKernelWidth2D(radius,0.5);
579 if ((image->columns < width) || (image->rows < width))
580 ThrowImageException(OptionError,"ImageSmallerThanRadius");
581 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
582 if (paint_image == (Image *) NULL)
583 return((Image *) NULL);
584 if (SetImageStorageClass(paint_image,DirectClass) == MagickFalse)
586 InheritException(exception,&paint_image->exception);
587 paint_image=DestroyImage(paint_image);
588 return((Image *) NULL);
590 histograms=AcquireHistogramThreadSet(NumberPaintBins);
591 if (histograms == (unsigned long **) NULL)
593 paint_image=DestroyImage(paint_image);
594 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
601 image_view=AcquireCacheView(image);
602 paint_view=AcquireCacheView(paint_image);
603 #if defined(MAGICKCORE_OPENMP_SUPPORT)
604 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
606 for (y=0; y < (long) image->rows; y++)
608 register const IndexPacket
611 register const PixelPacket
615 *__restrict paint_indexes;
623 register unsigned long
626 if (status == MagickFalse)
628 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
629 2L),image->columns+width,width,exception);
630 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
632 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
637 indexes=GetCacheViewVirtualIndexQueue(image_view);
638 paint_indexes=GetCacheViewAuthenticIndexQueue(paint_view);
639 histogram=histograms[GetOpenMPThreadId()];
640 for (x=0; x < (long) image->columns; x++)
655 Assign most frequent color.
660 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
661 for (v=0; v < (long) width; v++)
663 for (u=0; u < (long) width; u++)
665 k=(long) ScaleQuantumToChar(PixelIntensityToQuantum(p+u+i));
667 if (histogram[k] > count)
673 i+=image->columns+width;
676 if (image->colorspace == CMYKColorspace)
677 paint_indexes[x]=indexes[x+j];
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,
719 % in many cases two colors may differ by a small amount. Fuzz defines
720 % how much tolerance is acceptable to consider two colors as the same.
721 % For example, set fuzz to 10 and the color red at intensities of 100 and
722 % 102 respectively are now interpreted as the same color.
724 % The format of the OpaquePaintImage method is:
726 % MagickBooleanType OpaquePaintImage(Image *image,
727 % const PixelPacket *target,const PixelPacket *fill,
728 % const MagickBooleanType invert)
729 % MagickBooleanType OpaquePaintImageChannel(Image *image,
730 % const ChannelType channel,const PixelPacket *target,
731 % const PixelPacket *fill,const MagickBooleanType invert)
733 % A description of each parameter follows:
735 % o image: the image.
737 % o channel: the channel(s).
739 % o target: the RGB value of the target color.
741 % o fill: the replacement color.
743 % o invert: paint any pixel that does not match the target color.
747 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
748 const MagickPixelPacket *target,const MagickPixelPacket *fill,
749 const MagickBooleanType invert)
751 return(OpaquePaintImageChannel(image,AllChannels,target,fill,invert));
754 MagickExport MagickBooleanType OpaquePaintImageChannel(Image *image,
755 const ChannelType channel,const MagickPixelPacket *target,
756 const MagickPixelPacket *fill,const MagickBooleanType invert)
758 #define OpaquePaintImageTag "Opaque/Image"
776 assert(image != (Image *) NULL);
777 assert(image->signature == MagickSignature);
778 assert(target != (MagickPixelPacket *) NULL);
779 assert(fill != (MagickPixelPacket *) NULL);
780 if (image->debug != MagickFalse)
781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
782 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
785 Make image color opaque.
789 exception=(&image->exception);
790 GetMagickPixelPacket(image,&zero);
791 image_view=AcquireCacheView(image);
792 #if defined(MAGICKCORE_OPENMP_SUPPORT)
793 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
795 for (y=0; y < (long) image->rows; y++)
809 if (status == MagickFalse)
811 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
812 if (q == (PixelPacket *) NULL)
817 indexes=GetCacheViewAuthenticIndexQueue(image_view);
819 for (x=0; x < (long) image->columns; x++)
821 SetMagickPixelPacket(image,q,indexes+x,&pixel);
822 if (IsMagickColorSimilar(&pixel,target) != invert)
824 if ((channel & RedChannel) != 0)
825 q->red=RoundToQuantum(fill->red);
826 if ((channel & GreenChannel) != 0)
827 q->green=RoundToQuantum(fill->green);
828 if ((channel & BlueChannel) != 0)
829 q->blue=RoundToQuantum(fill->blue);
830 if ((channel & OpacityChannel) != 0)
831 q->opacity=RoundToQuantum(fill->opacity);
832 if (((channel & IndexChannel) != 0) &&
833 (image->colorspace == CMYKColorspace))
834 indexes[x]=RoundToQuantum(fill->index);
838 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
840 if (image->progress_monitor != (MagickProgressMonitor) NULL)
845 #if defined(MAGICKCORE_OPENMP_SUPPORT)
846 #pragma omp critical (MagickCore_OpaquePaintImageChannel)
848 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
850 if (proceed == MagickFalse)
854 image_view=DestroyCacheView(image_view);
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % T r a n s p a r e n t P a i n t I m a g e %
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 % TransparentPaintImage() changes the opacity value associated with any pixel
870 % that matches color to the value defined by opacity.
872 % By default color must match a particular pixel color exactly. However,
873 % in many cases two colors may differ by a small amount. Fuzz defines
874 % how much tolerance is acceptable to consider two colors as the same.
875 % For example, set fuzz to 10 and the color red at intensities of 100 and
876 % 102 respectively are now interpreted as the same color.
878 % The format of the TransparentPaintImage method is:
880 % MagickBooleanType TransparentPaintImage(Image *image,
881 % const MagickPixelPacket *target,const Quantum opacity,
882 % const MagickBooleanType invert)
884 % A description of each parameter follows:
886 % o image: the image.
888 % o target: the target color.
890 % o opacity: the replacement opacity value.
892 % o invert: paint any pixel that does not match the target color.
895 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
896 const MagickPixelPacket *target,const Quantum opacity,
897 const MagickBooleanType invert)
899 #define TransparentPaintImageTag "Transparent/Image"
917 assert(image != (Image *) NULL);
918 assert(image->signature == MagickSignature);
919 assert(target != (MagickPixelPacket *) NULL);
920 if (image->debug != MagickFalse)
921 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
922 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
924 if (image->matte == MagickFalse)
925 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
927 Make image color transparent.
931 exception=(&image->exception);
932 GetMagickPixelPacket(image,&zero);
933 image_view=AcquireCacheView(image);
934 #if defined(MAGICKCORE_OPENMP_SUPPORT)
935 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
937 for (y=0; y < (long) image->rows; y++)
951 if (status == MagickFalse)
953 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
954 if (q == (PixelPacket *) NULL)
959 indexes=GetCacheViewAuthenticIndexQueue(image_view);
961 for (x=0; x < (long) image->columns; x++)
963 SetMagickPixelPacket(image,q,indexes+x,&pixel);
964 if (IsMagickColorSimilar(&pixel,target) != invert)
968 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
970 if (image->progress_monitor != (MagickProgressMonitor) NULL)
975 #if defined(MAGICKCORE_OPENMP_SUPPORT)
976 #pragma omp critical (MagickCore_TransparentPaintImage)
978 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
980 if (proceed == MagickFalse)
984 image_view=DestroyCacheView(image_view);
989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % 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 %
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
999 % TransparentPaintImageChroma() changes the opacity value associated with any
1000 % pixel that matches color to the value defined by opacity.
1002 % As there is one fuzz value for the all the channels, the
1003 % TransparentPaintImage() API is not suitable for the operations like chroma,
1004 % where the tolerance for similarity of two color component (RGB) can be
1005 % different, Thus we define this method take two target pixels (one
1006 % low and one hight) and all the pixels of an image which are lying between
1007 % these two pixels are made transparent.
1009 % The format of the TransparentPaintImage method is:
1011 % MagickBooleanType TransparentPaintImage(Image *image,
1012 % const MagickPixelPacket *low,const MagickPixelPacket *hight,
1013 % const Quantum opacity,const MagickBooleanType invert)
1015 % A description of each parameter follows:
1017 % o image: the image.
1019 % o low: the low target color.
1021 % o high: the high target color.
1023 % o opacity: the replacement opacity value.
1025 % o invert: paint any pixel that does not match the target color.
1028 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1029 const MagickPixelPacket *low,const MagickPixelPacket *high,
1030 const Quantum opacity,const MagickBooleanType invert)
1032 #define TransparentPaintImageTag "Transparent/Image"
1047 assert(image != (Image *) NULL);
1048 assert(image->signature == MagickSignature);
1049 assert(high != (MagickPixelPacket *) NULL);
1050 assert(low != (MagickPixelPacket *) NULL);
1051 if (image->debug != MagickFalse)
1052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1053 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1054 return(MagickFalse);
1055 if (image->matte == MagickFalse)
1056 (void) SetImageAlphaChannel(image,ResetAlphaChannel);
1058 Make image color transparent.
1062 exception=(&image->exception);
1063 image_view=AcquireCacheView(image);
1064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1065 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1067 for (y=0; y < (long) image->rows; y++)
1075 register IndexPacket
1076 *__restrict indexes;
1081 register PixelPacket
1084 if (status == MagickFalse)
1086 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1087 if (q == (PixelPacket *) NULL)
1092 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1093 GetMagickPixelPacket(image,&pixel);
1094 for (x=0; x < (long) image->columns; x++)
1096 SetMagickPixelPacket(image,q,indexes+x,&pixel);
1097 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1098 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1099 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1100 MagickTrue : MagickFalse;
1101 if (match != invert)
1105 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1107 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1112 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1113 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1115 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1117 if (proceed == MagickFalse)
1121 image_view=DestroyCacheView(image_view);