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"
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 % F l o o d f i l l P a i n t I m a g e %
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % FloodfillPaintImage() changes the color value of any pixel that matches
72 % target and is an immediate neighbor. If the method FillToBorderMethod is
73 % specified, the color value is changed for any neighbor pixel that does not
74 % match the bordercolor member of image.
76 % By default target must match a particular pixel color exactly.
77 % However, in many cases two colors may differ by a small amount. The
78 % fuzz member of image defines how much tolerance is acceptable to
79 % consider two colors as the same. For example, set fuzz to 10 and the
80 % color red at intensities of 100 and 102 respectively are now
81 % interpreted as the same color for the purposes of the floodfill.
83 % The format of the FloodfillPaintImage method is:
85 % MagickBooleanType FloodfillPaintImage(Image *image,
86 % const ChannelType channel,const DrawInfo *draw_info,
87 % const MagickPixelPacket target,const ssize_t x_offset,const ssize_t y_offset,
88 % const MagickBooleanType invert)
90 % A description of each parameter follows:
94 % o channel: the channel(s).
96 % o draw_info: the draw info.
98 % o target: the RGB value of the target color.
100 % o x_offset,y_offset: the starting location of the operation.
102 % o invert: paint any pixel that does not match the target color.
105 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
106 const ChannelType channel,const DrawInfo *draw_info,
107 const MagickPixelPacket *target,const ssize_t x_offset,const ssize_t y_offset,
108 const MagickBooleanType invert)
110 #define MaxStacksize (1UL << 15)
111 #define PushSegmentStack(up,left,right,delta) \
113 if (s >= (segment_stack+MaxStacksize)) \
114 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
117 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
119 s->x1=(double) (left); \
120 s->y1=(double) (up); \
121 s->x2=(double) (right); \
122 s->y2=(double) (delta); \
159 Check boundary conditions.
161 assert(image != (Image *) NULL);
162 assert(image->signature == MagickSignature);
163 if (image->debug != MagickFalse)
164 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
165 assert(draw_info != (DrawInfo *) NULL);
166 assert(draw_info->signature == MagickSignature);
167 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
169 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
171 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
173 if (image->matte == MagickFalse)
174 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
178 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
179 if (floodplane_image == (Image *) NULL)
181 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel);
182 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
183 sizeof(*segment_stack));
184 if (segment_stack == (SegmentInfo *) NULL)
186 floodplane_image=DestroyImage(floodplane_image);
187 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
191 Push initial segment on stack.
193 exception=(&image->exception);
198 PushSegmentStack(y,x,x,1);
199 PushSegmentStack(y+1,x,x,-1);
200 GetMagickPixelPacket(image,&fill);
201 GetMagickPixelPacket(image,&pixel);
202 while (s > segment_stack)
204 register const IndexPacket
207 register const PixelPacket
217 Pop segment off stack.
222 offset=(ssize_t) s->y2;
223 y=(ssize_t) s->y1+offset;
225 Recolor neighboring pixels.
227 p=GetVirtualPixels(image,0,y,(size_t) (x1+1),1,exception);
228 q=GetAuthenticPixels(floodplane_image,0,y,(size_t) (x1+1),1,
230 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
232 indexes=GetVirtualIndexQueue(image);
235 for (x=x1; x >= 0; x--)
237 if (q->opacity == (Quantum) TransparentOpacity)
239 SetMagickPixelPacket(image,p,indexes+x,&pixel);
240 if (IsMagickColorSimilar(&pixel,target) == invert)
242 q->opacity=(Quantum) TransparentOpacity;
246 if (SyncAuthenticPixels(floodplane_image,exception) == MagickFalse)
248 skip=x >= x1 ? MagickTrue : MagickFalse;
249 if (skip == MagickFalse)
253 PushSegmentStack(y,start,x1-1,-offset);
258 if (skip == MagickFalse)
260 if (x < (ssize_t) image->columns)
262 p=GetVirtualPixels(image,x,y,image->columns-x,1,exception);
263 q=GetAuthenticPixels(floodplane_image,x,y,image->columns-x,1,
265 if ((p == (const PixelPacket *) NULL) ||
266 (q == (PixelPacket *) NULL))
268 indexes=GetVirtualIndexQueue(image);
269 for ( ; x < (ssize_t) image->columns; x++)
271 if (q->opacity == (Quantum) TransparentOpacity)
273 SetMagickPixelPacket(image,p,indexes+x,&pixel);
274 if (IsMagickColorSimilar(&pixel,target) == invert)
276 q->opacity=(Quantum) TransparentOpacity;
280 if (SyncAuthenticPixels(floodplane_image,exception) == MagickFalse)
283 PushSegmentStack(y,start,x-1,offset);
285 PushSegmentStack(y,x2+1,x-1,-offset);
291 p=GetVirtualPixels(image,x,y,(size_t) (x2-x+1),1,exception);
292 q=GetAuthenticPixels(floodplane_image,x,y,(size_t) (x2-x+1),1,
294 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
296 indexes=GetVirtualIndexQueue(image);
297 for ( ; x <= x2; x++)
299 if (q->opacity == (Quantum) TransparentOpacity)
301 SetMagickPixelPacket(image,p,indexes+x,&pixel);
302 if (IsMagickColorSimilar(&pixel,target) != invert)
311 for (y=0; y < (ssize_t) image->rows; y++)
313 register const PixelPacket
326 Tile fill color onto floodplane.
328 p=GetVirtualPixels(floodplane_image,0,y,image->columns,1,exception);
329 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
330 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
332 indexes=GetAuthenticIndexQueue(image);
333 for (x=0; x < (ssize_t) image->columns; x++)
335 if (p->opacity != OpaqueOpacity)
337 (void) GetFillColor(draw_info,x,y,&fill_color);
338 SetMagickPixelPacket(image,&fill_color,(IndexPacket *) NULL,&fill);
339 if (image->colorspace == CMYKColorspace)
340 ConvertRGBToCMYK(&fill);
341 if ((channel & RedChannel) != 0)
342 q->red=ClampToQuantum(fill.red);
343 if ((channel & GreenChannel) != 0)
344 q->green=ClampToQuantum(fill.green);
345 if ((channel & BlueChannel) != 0)
346 q->blue=ClampToQuantum(fill.blue);
347 if ((channel & OpacityChannel) != 0)
348 q->opacity=ClampToQuantum(fill.opacity);
349 if (((channel & IndexChannel) != 0) &&
350 (image->colorspace == CMYKColorspace))
351 indexes[x]=ClampToQuantum(fill.index);
356 if (SyncAuthenticPixels(image,exception) == MagickFalse)
359 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
360 floodplane_image=DestroyImage(floodplane_image);
361 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 + G r a d i e n t I m a g e %
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 % GradientImage() applies a continuously smooth color transitions along a
376 % vector from one color to another.
378 % Note, the interface of this method will change in the future to support
379 % more than one transistion.
381 % The format of the GradientImage method is:
383 % MagickBooleanType GradientImage(Image *image,const GradientType type,
384 % const SpreadMethod method,const PixelPacket *start_color,
385 % const PixelPacket *stop_color)
387 % A description of each parameter follows:
389 % o image: the image.
391 % o type: the gradient type: linear or radial.
393 % o spread: the gradient spread meathod: pad, reflect, or repeat.
395 % o start_color: the start color.
397 % o stop_color: the stop color.
399 % This provides a good example of making use of the DrawGradientImage
400 % function and the gradient structure in draw_info.
403 static inline double MagickMax(const double x,const double y)
405 return(x > y ? x : y);
408 MagickExport MagickBooleanType GradientImage(Image *image,
409 const GradientType type,const SpreadMethod method,
410 const PixelPacket *start_color,const PixelPacket *stop_color)
425 Set gradient start-stop end points.
427 assert(image != (const Image *) NULL);
428 assert(image->signature == MagickSignature);
429 if (image->debug != MagickFalse)
430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
431 assert(start_color != (const PixelPacket *) NULL);
432 assert(stop_color != (const PixelPacket *) NULL);
433 draw_info=AcquireDrawInfo();
434 gradient=(&draw_info->gradient);
436 gradient->bounding_box.width=image->columns;
437 gradient->bounding_box.height=image->rows;
438 gradient->gradient_vector.x2=(double) image->columns-1.0;
439 gradient->gradient_vector.y2=(double) image->rows-1.0;
440 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
441 gradient->gradient_vector.x2=0.0;
442 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
443 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
444 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
445 gradient->spread=method;
447 Define the gradient to fill between the stops.
449 gradient->number_stops=2;
450 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
451 sizeof(*gradient->stops));
452 if (gradient->stops == (StopInfo *) NULL)
453 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
455 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
456 sizeof(*gradient->stops));
457 for (i=0; i < (ssize_t) gradient->number_stops; i++)
458 GetMagickPixelPacket(image,&gradient->stops[i].color);
459 SetMagickPixelPacket(image,start_color,(IndexPacket *) NULL,
460 &gradient->stops[0].color);
461 gradient->stops[0].offset=0.0;
462 SetMagickPixelPacket(image,stop_color,(IndexPacket *) NULL,
463 &gradient->stops[1].color);
464 gradient->stops[1].offset=1.0;
466 Draw a gradient on the image.
468 status=DrawGradientImage(image,draw_info);
469 draw_info=DestroyDrawInfo(draw_info);
470 if ((start_color->opacity == OpaqueOpacity) &&
471 (stop_color->opacity == OpaqueOpacity))
472 image->matte=MagickFalse;
473 if ((IsGrayPixel(start_color) != MagickFalse) &&
474 (IsGrayPixel(stop_color) != MagickFalse))
475 image->type=GrayscaleType;
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % O i l P a i n t I m a g e %
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % OilPaintImage() applies a special effect filter that simulates an oil
491 % painting. Each pixel is replaced by the most frequent color occurring
492 % in a circular region defined by radius.
494 % The format of the OilPaintImage method is:
496 % Image *OilPaintImage(const Image *image,const double radius,
497 % ExceptionInfo *exception)
499 % A description of each parameter follows:
501 % o image: the image.
503 % o radius: the radius of the circular neighborhood.
505 % o exception: return any errors or warnings in this structure.
509 static size_t **DestroyHistogramThreadSet(size_t **histogram)
514 assert(histogram != (size_t **) NULL);
515 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
516 if (histogram[i] != (size_t *) NULL)
517 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
518 histogram=(size_t **) RelinquishAlignedMemory(histogram);
522 static size_t **AcquireHistogramThreadSet(const size_t count)
531 number_threads=GetOpenMPMaximumThreads();
532 histogram=(size_t **) AcquireAlignedMemory(number_threads,
534 if (histogram == (size_t **) NULL)
535 return((size_t **) NULL);
536 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
537 for (i=0; i < (ssize_t) number_threads; i++)
539 histogram[i]=(size_t *) AcquireQuantumMemory(count,
540 sizeof(**histogram));
541 if (histogram[i] == (size_t *) NULL)
542 return(DestroyHistogramThreadSet(histogram));
547 MagickExport Image *OilPaintImage(const Image *image,const double radius,
548 ExceptionInfo *exception)
550 #define NumberPaintBins 256
551 #define OilPaintImageTag "OilPaint/Image"
567 **restrict histograms,
574 Initialize painted image attributes.
576 assert(image != (const Image *) NULL);
577 assert(image->signature == MagickSignature);
578 if (image->debug != MagickFalse)
579 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
580 assert(exception != (ExceptionInfo *) NULL);
581 assert(exception->signature == MagickSignature);
582 width=GetOptimalKernelWidth2D(radius,0.5);
583 if ((image->columns < width) || (image->rows < width))
584 ThrowImageException(OptionError,"ImageSmallerThanRadius");
585 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
586 if (paint_image == (Image *) NULL)
587 return((Image *) NULL);
588 if (SetImageStorageClass(paint_image,DirectClass) == MagickFalse)
590 InheritException(exception,&paint_image->exception);
591 paint_image=DestroyImage(paint_image);
592 return((Image *) NULL);
594 histograms=AcquireHistogramThreadSet(NumberPaintBins);
595 if (histograms == (size_t **) NULL)
597 paint_image=DestroyImage(paint_image);
598 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
605 image_view=AcquireCacheView(image);
606 paint_view=AcquireCacheView(paint_image);
607 #if defined(MAGICKCORE_OPENMP_SUPPORT)
608 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
610 for (y=0; y < (ssize_t) image->rows; y++)
612 register const IndexPacket
615 register const PixelPacket
619 *restrict paint_indexes;
630 if (status == MagickFalse)
632 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t) (width/
633 2L),image->columns+width,width,exception);
634 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
636 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
641 indexes=GetCacheViewVirtualIndexQueue(image_view);
642 paint_indexes=GetCacheViewAuthenticIndexQueue(paint_view);
643 histogram=histograms[GetOpenMPThreadId()];
644 for (x=0; x < (ssize_t) image->columns; x++)
659 Assign most frequent color.
664 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
665 for (v=0; v < (ssize_t) width; v++)
667 for (u=0; u < (ssize_t) width; u++)
669 k=(ssize_t) ScaleQuantumToChar(PixelIntensityToQuantum(p+u+i));
671 if (histogram[k] > count)
677 i+=(ssize_t) (image->columns+width);
680 if (image->colorspace == CMYKColorspace)
681 paint_indexes[x]=indexes[x+j];
685 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
687 if (image->progress_monitor != (MagickProgressMonitor) NULL)
692 #if defined(MAGICKCORE_OPENMP_SUPPORT)
693 #pragma omp critical (MagickCore_OilPaintImage)
695 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
696 if (proceed == MagickFalse)
700 paint_view=DestroyCacheView(paint_view);
701 image_view=DestroyCacheView(image_view);
702 histograms=DestroyHistogramThreadSet(histograms);
703 if (status == MagickFalse)
704 paint_image=DestroyImage(paint_image);
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % O p a q u e P a i n t I m a g e %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % OpaquePaintImage() changes any pixel that matches color with the color
722 % By default color must match a particular pixel color exactly. However,
723 % in many cases two colors may differ by a small amount. Fuzz defines
724 % how much tolerance is acceptable to consider two colors as the same.
725 % For example, set fuzz to 10 and the color red at intensities of 100 and
726 % 102 respectively are now interpreted as the same color.
728 % The format of the OpaquePaintImage method is:
730 % MagickBooleanType OpaquePaintImage(Image *image,
731 % const PixelPacket *target,const PixelPacket *fill,
732 % const MagickBooleanType invert)
733 % MagickBooleanType OpaquePaintImageChannel(Image *image,
734 % const ChannelType channel,const PixelPacket *target,
735 % const PixelPacket *fill,const MagickBooleanType invert)
737 % A description of each parameter follows:
739 % o image: the image.
741 % o channel: the channel(s).
743 % o target: the RGB value of the target color.
745 % o fill: the replacement color.
747 % o invert: paint any pixel that does not match the target color.
751 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
752 const MagickPixelPacket *target,const MagickPixelPacket *fill,
753 const MagickBooleanType invert)
755 return(OpaquePaintImageChannel(image,AllChannels,target,fill,invert));
758 MagickExport MagickBooleanType OpaquePaintImageChannel(Image *image,
759 const ChannelType channel,const MagickPixelPacket *target,
760 const MagickPixelPacket *fill,const MagickBooleanType invert)
762 #define OpaquePaintImageTag "Opaque/Image"
782 assert(image != (Image *) NULL);
783 assert(image->signature == MagickSignature);
784 assert(target != (MagickPixelPacket *) NULL);
785 assert(fill != (MagickPixelPacket *) NULL);
786 if (image->debug != MagickFalse)
787 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
788 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
791 Make image color opaque.
795 exception=(&image->exception);
796 GetMagickPixelPacket(image,&zero);
797 image_view=AcquireCacheView(image);
798 #if defined(MAGICKCORE_OPENMP_SUPPORT)
799 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
801 for (y=0; y < (ssize_t) image->rows; y++)
815 if (status == MagickFalse)
817 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
818 if (q == (PixelPacket *) NULL)
823 indexes=GetCacheViewAuthenticIndexQueue(image_view);
825 for (x=0; x < (ssize_t) image->columns; x++)
827 SetMagickPixelPacket(image,q,indexes+x,&pixel);
828 if (IsMagickColorSimilar(&pixel,target) != invert)
830 if ((channel & RedChannel) != 0)
831 q->red=ClampToQuantum(fill->red);
832 if ((channel & GreenChannel) != 0)
833 q->green=ClampToQuantum(fill->green);
834 if ((channel & BlueChannel) != 0)
835 q->blue=ClampToQuantum(fill->blue);
836 if ((channel & OpacityChannel) != 0)
837 q->opacity=ClampToQuantum(fill->opacity);
838 if (((channel & IndexChannel) != 0) &&
839 (image->colorspace == CMYKColorspace))
840 indexes[x]=ClampToQuantum(fill->index);
844 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
846 if (image->progress_monitor != (MagickProgressMonitor) NULL)
851 #if defined(MAGICKCORE_OPENMP_SUPPORT)
852 #pragma omp critical (MagickCore_OpaquePaintImageChannel)
854 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
856 if (proceed == MagickFalse)
860 image_view=DestroyCacheView(image_view);
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 % T r a n s p a r e n t P a i n t I m a g e %
873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 % TransparentPaintImage() changes the opacity value associated with any pixel
876 % that matches color to the value defined by opacity.
878 % By default color must match a particular pixel color exactly. However,
879 % in many cases two colors may differ by a small amount. Fuzz defines
880 % how much tolerance is acceptable to consider two colors as the same.
881 % For example, set fuzz to 10 and the color red at intensities of 100 and
882 % 102 respectively are now interpreted as the same color.
884 % The format of the TransparentPaintImage method is:
886 % MagickBooleanType TransparentPaintImage(Image *image,
887 % const MagickPixelPacket *target,const Quantum opacity,
888 % const MagickBooleanType invert)
890 % A description of each parameter follows:
892 % o image: the image.
894 % o target: the target color.
896 % o opacity: the replacement opacity value.
898 % o invert: paint any pixel that does not match the target color.
901 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
902 const MagickPixelPacket *target,const Quantum opacity,
903 const MagickBooleanType invert)
905 #define TransparentPaintImageTag "Transparent/Image"
925 assert(image != (Image *) NULL);
926 assert(image->signature == MagickSignature);
927 assert(target != (MagickPixelPacket *) NULL);
928 if (image->debug != MagickFalse)
929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
930 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
932 if (image->matte == MagickFalse)
933 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
935 Make image color transparent.
939 exception=(&image->exception);
940 GetMagickPixelPacket(image,&zero);
941 image_view=AcquireCacheView(image);
942 #if defined(MAGICKCORE_OPENMP_SUPPORT)
943 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
945 for (y=0; y < (ssize_t) image->rows; y++)
959 if (status == MagickFalse)
961 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
962 if (q == (PixelPacket *) NULL)
967 indexes=GetCacheViewAuthenticIndexQueue(image_view);
969 for (x=0; x < (ssize_t) image->columns; x++)
971 SetMagickPixelPacket(image,q,indexes+x,&pixel);
972 if (IsMagickColorSimilar(&pixel,target) != invert)
976 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
978 if (image->progress_monitor != (MagickProgressMonitor) NULL)
983 #if defined(MAGICKCORE_OPENMP_SUPPORT)
984 #pragma omp critical (MagickCore_TransparentPaintImage)
986 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
988 if (proceed == MagickFalse)
992 image_view=DestroyCacheView(image_view);
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001 % 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 %
1005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007 % TransparentPaintImageChroma() changes the opacity value associated with any
1008 % pixel that matches color to the value defined by opacity.
1010 % As there is one fuzz value for the all the channels, the
1011 % TransparentPaintImage() API is not suitable for the operations like chroma,
1012 % where the tolerance for similarity of two color component (RGB) can be
1013 % different, Thus we define this method take two target pixels (one
1014 % low and one hight) and all the pixels of an image which are lying between
1015 % these two pixels are made transparent.
1017 % The format of the TransparentPaintImage method is:
1019 % MagickBooleanType TransparentPaintImage(Image *image,
1020 % const MagickPixelPacket *low,const MagickPixelPacket *hight,
1021 % const Quantum opacity,const MagickBooleanType invert)
1023 % A description of each parameter follows:
1025 % o image: the image.
1027 % o low: the low target color.
1029 % o high: the high target color.
1031 % o opacity: the replacement opacity value.
1033 % o invert: paint any pixel that does not match the target color.
1036 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1037 const MagickPixelPacket *low,const MagickPixelPacket *high,
1038 const Quantum opacity,const MagickBooleanType invert)
1040 #define TransparentPaintImageTag "Transparent/Image"
1057 assert(image != (Image *) NULL);
1058 assert(image->signature == MagickSignature);
1059 assert(high != (MagickPixelPacket *) NULL);
1060 assert(low != (MagickPixelPacket *) NULL);
1061 if (image->debug != MagickFalse)
1062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1063 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1064 return(MagickFalse);
1065 if (image->matte == MagickFalse)
1066 (void) SetImageAlphaChannel(image,ResetAlphaChannel);
1068 Make image color transparent.
1072 exception=(&image->exception);
1073 image_view=AcquireCacheView(image);
1074 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1075 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1077 for (y=0; y < (ssize_t) image->rows; y++)
1085 register IndexPacket
1091 register PixelPacket
1094 if (status == MagickFalse)
1096 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1097 if (q == (PixelPacket *) NULL)
1102 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1103 GetMagickPixelPacket(image,&pixel);
1104 for (x=0; x < (ssize_t) image->columns; x++)
1106 SetMagickPixelPacket(image,q,indexes+x,&pixel);
1107 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1108 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1109 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1110 MagickTrue : MagickFalse;
1111 if (match != invert)
1115 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1117 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1122 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1123 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1125 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1127 if (proceed == MagickFalse)
1131 image_view=DestroyCacheView(image_view);