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-2010 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 ssize_t x_offset,const ssize_t 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 ssize_t x_offset,const ssize_t 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)) < (ssize_t) 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 >= (ssize_t) image->columns))
173 if ((y_offset < 0) || (y_offset >= (ssize_t) 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.
226 offset=(ssize_t) s->y2;
227 y=(ssize_t) s->y1+offset;
229 Recolor neighboring pixels.
231 p=GetVirtualPixels(image,0,y,(size_t) (x1+1),1,exception);
232 q=GetAuthenticPixels(floodplane_image,0,y,(size_t) (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 < (ssize_t) 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 < (ssize_t) 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,(size_t) (x2-x+1),1,exception);
296 q=GetAuthenticPixels(floodplane_image,x,y,(size_t) (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 < (ssize_t) 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 < (ssize_t) 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=ClampToQuantum(fill.red);
347 if ((channel & GreenChannel) != 0)
348 q->green=ClampToQuantum(fill.green);
349 if ((channel & BlueChannel) != 0)
350 q->blue=ClampToQuantum(fill.blue);
351 if ((channel & OpacityChannel) != 0)
352 q->opacity=ClampToQuantum(fill.opacity);
353 if (((channel & IndexChannel) != 0) &&
354 (image->colorspace == CMYKColorspace))
355 indexes[x]=ClampToQuantum(fill.index);
360 if (SyncAuthenticPixels(image,exception) == MagickFalse)
363 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
364 floodplane_image=DestroyImage(floodplane_image);
365 return(y == (ssize_t) 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 < (ssize_t) 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 size_t **DestroyHistogramThreadSet(size_t **histogram)
512 assert(histogram != (size_t **) NULL);
513 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
514 if (histogram[i] != (size_t *) NULL)
515 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
516 histogram=(size_t **) RelinquishAlignedMemory(histogram);
520 static size_t **AcquireHistogramThreadSet(const size_t count)
529 number_threads=GetOpenMPMaximumThreads();
530 histogram=(size_t **) AcquireAlignedMemory(number_threads,
532 if (histogram == (size_t **) NULL)
533 return((size_t **) NULL);
534 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
535 for (i=0; i < (ssize_t) number_threads; i++)
537 histogram[i]=(size_t *) AcquireQuantumMemory(count,
538 sizeof(**histogram));
539 if (histogram[i] == (size_t *) 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"
565 **restrict histograms,
572 Initialize painted image attributes.
574 assert(image != (const Image *) NULL);
575 assert(image->signature == MagickSignature);
576 if (image->debug != MagickFalse)
577 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
578 assert(exception != (ExceptionInfo *) NULL);
579 assert(exception->signature == MagickSignature);
580 width=GetOptimalKernelWidth2D(radius,0.5);
581 if ((image->columns < width) || (image->rows < width))
582 ThrowImageException(OptionError,"ImageSmallerThanRadius");
583 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
584 if (paint_image == (Image *) NULL)
585 return((Image *) NULL);
586 if (SetImageStorageClass(paint_image,DirectClass) == MagickFalse)
588 InheritException(exception,&paint_image->exception);
589 paint_image=DestroyImage(paint_image);
590 return((Image *) NULL);
592 histograms=AcquireHistogramThreadSet(NumberPaintBins);
593 if (histograms == (size_t **) NULL)
595 paint_image=DestroyImage(paint_image);
596 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
603 image_view=AcquireCacheView(image);
604 paint_view=AcquireCacheView(paint_image);
605 #if defined(MAGICKCORE_OPENMP_SUPPORT)
606 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
608 for (y=0; y < (ssize_t) image->rows; y++)
610 register const IndexPacket
613 register const PixelPacket
617 *restrict paint_indexes;
628 if (status == MagickFalse)
630 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t) (width/
631 2L),image->columns+width,width,exception);
632 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
634 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
639 indexes=GetCacheViewVirtualIndexQueue(image_view);
640 paint_indexes=GetCacheViewAuthenticIndexQueue(paint_view);
641 histogram=histograms[GetOpenMPThreadId()];
642 for (x=0; x < (ssize_t) image->columns; x++)
657 Assign most frequent color.
662 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
663 for (v=0; v < (ssize_t) width; v++)
665 for (u=0; u < (ssize_t) width; u++)
667 k=(ssize_t) ScaleQuantumToChar(PixelIntensityToQuantum(p+u+i));
669 if (histogram[k] > count)
675 i+=(ssize_t) (image->columns+width);
678 if (image->colorspace == CMYKColorspace)
679 paint_indexes[x]=indexes[x+j];
683 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
685 if (image->progress_monitor != (MagickProgressMonitor) NULL)
690 #if defined(MAGICKCORE_OPENMP_SUPPORT)
691 #pragma omp critical (MagickCore_OilPaintImage)
693 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
694 if (proceed == MagickFalse)
698 paint_view=DestroyCacheView(paint_view);
699 image_view=DestroyCacheView(image_view);
700 histograms=DestroyHistogramThreadSet(histograms);
701 if (status == MagickFalse)
702 paint_image=DestroyImage(paint_image);
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 % O p a q u e P a i n t I m a g e %
715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % OpaquePaintImage() changes any pixel that matches color with the color
720 % By default color must match a particular pixel color exactly. However,
721 % in many cases two colors may differ by a small amount. Fuzz defines
722 % how much tolerance is acceptable to consider two colors as the same.
723 % For example, set fuzz to 10 and the color red at intensities of 100 and
724 % 102 respectively are now interpreted as the same color.
726 % The format of the OpaquePaintImage method is:
728 % MagickBooleanType OpaquePaintImage(Image *image,
729 % const PixelPacket *target,const PixelPacket *fill,
730 % const MagickBooleanType invert)
731 % MagickBooleanType OpaquePaintImageChannel(Image *image,
732 % const ChannelType channel,const PixelPacket *target,
733 % const PixelPacket *fill,const MagickBooleanType invert)
735 % A description of each parameter follows:
737 % o image: the image.
739 % o channel: the channel(s).
741 % o target: the RGB value of the target color.
743 % o fill: the replacement color.
745 % o invert: paint any pixel that does not match the target color.
749 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
750 const MagickPixelPacket *target,const MagickPixelPacket *fill,
751 const MagickBooleanType invert)
753 return(OpaquePaintImageChannel(image,AllChannels,target,fill,invert));
756 MagickExport MagickBooleanType OpaquePaintImageChannel(Image *image,
757 const ChannelType channel,const MagickPixelPacket *target,
758 const MagickPixelPacket *fill,const MagickBooleanType invert)
760 #define OpaquePaintImageTag "Opaque/Image"
780 assert(image != (Image *) NULL);
781 assert(image->signature == MagickSignature);
782 assert(target != (MagickPixelPacket *) NULL);
783 assert(fill != (MagickPixelPacket *) NULL);
784 if (image->debug != MagickFalse)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
786 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
789 Make image color opaque.
793 exception=(&image->exception);
794 GetMagickPixelPacket(image,&zero);
795 image_view=AcquireCacheView(image);
796 #if defined(MAGICKCORE_OPENMP_SUPPORT)
797 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
799 for (y=0; y < (ssize_t) image->rows; y++)
813 if (status == MagickFalse)
815 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
816 if (q == (PixelPacket *) NULL)
821 indexes=GetCacheViewAuthenticIndexQueue(image_view);
823 for (x=0; x < (ssize_t) image->columns; x++)
825 SetMagickPixelPacket(image,q,indexes+x,&pixel);
826 if (IsMagickColorSimilar(&pixel,target) != invert)
828 if ((channel & RedChannel) != 0)
829 q->red=ClampToQuantum(fill->red);
830 if ((channel & GreenChannel) != 0)
831 q->green=ClampToQuantum(fill->green);
832 if ((channel & BlueChannel) != 0)
833 q->blue=ClampToQuantum(fill->blue);
834 if ((channel & OpacityChannel) != 0)
835 q->opacity=ClampToQuantum(fill->opacity);
836 if (((channel & IndexChannel) != 0) &&
837 (image->colorspace == CMYKColorspace))
838 indexes[x]=ClampToQuantum(fill->index);
842 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
844 if (image->progress_monitor != (MagickProgressMonitor) NULL)
849 #if defined(MAGICKCORE_OPENMP_SUPPORT)
850 #pragma omp critical (MagickCore_OpaquePaintImageChannel)
852 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
854 if (proceed == MagickFalse)
858 image_view=DestroyCacheView(image_view);
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % T r a n s p a r e n t P a i n t I m a g e %
871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 % TransparentPaintImage() changes the opacity value associated with any pixel
874 % that matches color to the value defined by opacity.
876 % By default color must match a particular pixel color exactly. However,
877 % in many cases two colors may differ by a small amount. Fuzz defines
878 % how much tolerance is acceptable to consider two colors as the same.
879 % For example, set fuzz to 10 and the color red at intensities of 100 and
880 % 102 respectively are now interpreted as the same color.
882 % The format of the TransparentPaintImage method is:
884 % MagickBooleanType TransparentPaintImage(Image *image,
885 % const MagickPixelPacket *target,const Quantum opacity,
886 % const MagickBooleanType invert)
888 % A description of each parameter follows:
890 % o image: the image.
892 % o target: the target color.
894 % o opacity: the replacement opacity value.
896 % o invert: paint any pixel that does not match the target color.
899 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
900 const MagickPixelPacket *target,const Quantum opacity,
901 const MagickBooleanType invert)
903 #define TransparentPaintImageTag "Transparent/Image"
923 assert(image != (Image *) NULL);
924 assert(image->signature == MagickSignature);
925 assert(target != (MagickPixelPacket *) NULL);
926 if (image->debug != MagickFalse)
927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
928 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
930 if (image->matte == MagickFalse)
931 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
933 Make image color transparent.
937 exception=(&image->exception);
938 GetMagickPixelPacket(image,&zero);
939 image_view=AcquireCacheView(image);
940 #if defined(MAGICKCORE_OPENMP_SUPPORT)
941 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
943 for (y=0; y < (ssize_t) image->rows; y++)
957 if (status == MagickFalse)
959 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
960 if (q == (PixelPacket *) NULL)
965 indexes=GetCacheViewAuthenticIndexQueue(image_view);
967 for (x=0; x < (ssize_t) image->columns; x++)
969 SetMagickPixelPacket(image,q,indexes+x,&pixel);
970 if (IsMagickColorSimilar(&pixel,target) != invert)
974 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
976 if (image->progress_monitor != (MagickProgressMonitor) NULL)
981 #if defined(MAGICKCORE_OPENMP_SUPPORT)
982 #pragma omp critical (MagickCore_TransparentPaintImage)
984 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
986 if (proceed == MagickFalse)
990 image_view=DestroyCacheView(image_view);
995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
999 % 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 %
1003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 % TransparentPaintImageChroma() changes the opacity value associated with any
1006 % pixel that matches color to the value defined by opacity.
1008 % As there is one fuzz value for the all the channels, the
1009 % TransparentPaintImage() API is not suitable for the operations like chroma,
1010 % where the tolerance for similarity of two color component (RGB) can be
1011 % different, Thus we define this method take two target pixels (one
1012 % low and one hight) and all the pixels of an image which are lying between
1013 % these two pixels are made transparent.
1015 % The format of the TransparentPaintImage method is:
1017 % MagickBooleanType TransparentPaintImage(Image *image,
1018 % const MagickPixelPacket *low,const MagickPixelPacket *hight,
1019 % const Quantum opacity,const MagickBooleanType invert)
1021 % A description of each parameter follows:
1023 % o image: the image.
1025 % o low: the low target color.
1027 % o high: the high target color.
1029 % o opacity: the replacement opacity value.
1031 % o invert: paint any pixel that does not match the target color.
1034 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1035 const MagickPixelPacket *low,const MagickPixelPacket *high,
1036 const Quantum opacity,const MagickBooleanType invert)
1038 #define TransparentPaintImageTag "Transparent/Image"
1055 assert(image != (Image *) NULL);
1056 assert(image->signature == MagickSignature);
1057 assert(high != (MagickPixelPacket *) NULL);
1058 assert(low != (MagickPixelPacket *) NULL);
1059 if (image->debug != MagickFalse)
1060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1061 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1062 return(MagickFalse);
1063 if (image->matte == MagickFalse)
1064 (void) SetImageAlphaChannel(image,ResetAlphaChannel);
1066 Make image color transparent.
1070 exception=(&image->exception);
1071 image_view=AcquireCacheView(image);
1072 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1073 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1075 for (y=0; y < (ssize_t) image->rows; y++)
1083 register IndexPacket
1089 register PixelPacket
1092 if (status == MagickFalse)
1094 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1095 if (q == (PixelPacket *) NULL)
1100 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1101 GetMagickPixelPacket(image,&pixel);
1102 for (x=0; x < (ssize_t) image->columns; x++)
1104 SetMagickPixelPacket(image,q,indexes+x,&pixel);
1105 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1106 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1107 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1108 MagickTrue : MagickFalse;
1109 if (match != invert)
1113 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1115 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1120 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1121 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1123 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1125 if (proceed == MagickFalse)
1129 image_view=DestroyCacheView(image_view);