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-2011 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,
88 % const ssize_t y_offset,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); \
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 image_view=AcquireCacheView(image);
207 floodplane_view=AcquireCacheView(floodplane_image);
208 while (s > segment_stack)
210 register const IndexPacket
213 register const PixelPacket
223 Pop segment off stack.
228 offset=(ssize_t) s->y2;
229 y=(ssize_t) s->y1+offset;
231 Recolor neighboring pixels.
233 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
234 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
236 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
238 indexes=GetCacheViewVirtualIndexQueue(image_view);
241 for (x=x1; x >= 0; x--)
243 if (q->opacity == (Quantum) TransparentOpacity)
245 SetMagickPixelPacket(image,p,indexes+x,&pixel);
246 if (IsMagickColorSimilar(&pixel,target) == invert)
248 q->opacity=(Quantum) TransparentOpacity;
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,
271 image->columns-x,1,exception);
272 if ((p == (const PixelPacket *) NULL) ||
273 (q == (PixelPacket *) NULL))
275 indexes=GetCacheViewVirtualIndexQueue(image_view);
276 for ( ; x < (ssize_t) image->columns; x++)
278 if (q->opacity == (Quantum) TransparentOpacity)
280 SetMagickPixelPacket(image,p,indexes+x,&pixel);
281 if (IsMagickColorSimilar(&pixel,target) == invert)
283 q->opacity=(Quantum) TransparentOpacity;
287 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
290 PushSegmentStack(y,start,x-1,offset);
292 PushSegmentStack(y,x2+1,x-1,-offset);
298 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
300 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
302 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
304 indexes=GetCacheViewVirtualIndexQueue(image_view);
305 for ( ; x <= x2; x++)
307 if (q->opacity == (Quantum) TransparentOpacity)
309 SetMagickPixelPacket(image,p,indexes+x,&pixel);
310 if (IsMagickColorSimilar(&pixel,target) != invert)
319 for (y=0; y < (ssize_t) image->rows; y++)
321 register const PixelPacket
334 Tile fill color onto floodplane.
336 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
338 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
339 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
341 indexes=GetCacheViewAuthenticIndexQueue(image_view);
342 for (x=0; x < (ssize_t) image->columns; x++)
344 if (p->opacity != OpaqueOpacity)
346 (void) GetFillColor(draw_info,x,y,&fill_color);
347 SetMagickPixelPacket(image,&fill_color,(IndexPacket *) NULL,&fill);
348 if (image->colorspace == CMYKColorspace)
349 ConvertRGBToCMYK(&fill);
350 if ((channel & RedChannel) != 0)
351 q->red=ClampToQuantum(fill.red);
352 if ((channel & GreenChannel) != 0)
353 q->green=ClampToQuantum(fill.green);
354 if ((channel & BlueChannel) != 0)
355 q->blue=ClampToQuantum(fill.blue);
356 if ((channel & OpacityChannel) != 0)
357 q->opacity=ClampToQuantum(fill.opacity);
358 if (((channel & IndexChannel) != 0) &&
359 (image->colorspace == CMYKColorspace))
360 indexes[x]=ClampToQuantum(fill.index);
365 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
368 floodplane_view=DestroyCacheView(floodplane_view);
369 image_view=DestroyCacheView(image_view);
370 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
371 floodplane_image=DestroyImage(floodplane_image);
372 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 + G r a d i e n t I m a g e %
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % GradientImage() applies a continuously smooth color transitions along a
387 % vector from one color to another.
389 % Note, the interface of this method will change in the future to support
390 % more than one transistion.
392 % The format of the GradientImage method is:
394 % MagickBooleanType GradientImage(Image *image,const GradientType type,
395 % const SpreadMethod method,const PixelPacket *start_color,
396 % const PixelPacket *stop_color)
398 % A description of each parameter follows:
400 % o image: the image.
402 % o type: the gradient type: linear or radial.
404 % o spread: the gradient spread meathod: pad, reflect, or repeat.
406 % o start_color: the start color.
408 % o stop_color: the stop color.
410 % This provides a good example of making use of the DrawGradientImage
411 % function and the gradient structure in draw_info.
414 static inline double MagickMax(const double x,const double y)
416 return(x > y ? x : y);
419 MagickExport MagickBooleanType GradientImage(Image *image,
420 const GradientType type,const SpreadMethod method,
421 const PixelPacket *start_color,const PixelPacket *stop_color)
436 Set gradient start-stop end points.
438 assert(image != (const Image *) NULL);
439 assert(image->signature == MagickSignature);
440 if (image->debug != MagickFalse)
441 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
442 assert(start_color != (const PixelPacket *) NULL);
443 assert(stop_color != (const PixelPacket *) NULL);
444 draw_info=AcquireDrawInfo();
445 gradient=(&draw_info->gradient);
447 gradient->bounding_box.width=image->columns;
448 gradient->bounding_box.height=image->rows;
449 gradient->gradient_vector.x2=(double) image->columns-1.0;
450 gradient->gradient_vector.y2=(double) image->rows-1.0;
451 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
452 gradient->gradient_vector.x2=0.0;
453 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
454 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
455 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
456 gradient->spread=method;
458 Define the gradient to fill between the stops.
460 gradient->number_stops=2;
461 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
462 sizeof(*gradient->stops));
463 if (gradient->stops == (StopInfo *) NULL)
464 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
466 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
467 sizeof(*gradient->stops));
468 for (i=0; i < (ssize_t) gradient->number_stops; i++)
469 GetMagickPixelPacket(image,&gradient->stops[i].color);
470 SetMagickPixelPacket(image,start_color,(IndexPacket *) NULL,
471 &gradient->stops[0].color);
472 gradient->stops[0].offset=0.0;
473 SetMagickPixelPacket(image,stop_color,(IndexPacket *) NULL,
474 &gradient->stops[1].color);
475 gradient->stops[1].offset=1.0;
477 Draw a gradient on the image.
479 status=DrawGradientImage(image,draw_info);
480 draw_info=DestroyDrawInfo(draw_info);
481 if ((start_color->opacity == OpaqueOpacity) &&
482 (stop_color->opacity == OpaqueOpacity))
483 image->matte=MagickFalse;
484 if ((IsGrayPixel(start_color) != MagickFalse) &&
485 (IsGrayPixel(stop_color) != MagickFalse))
486 image->type=GrayscaleType;
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 % O i l P a i n t I m a g e %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501 % OilPaintImage() applies a special effect filter that simulates an oil
502 % painting. Each pixel is replaced by the most frequent color occurring
503 % in a circular region defined by radius.
505 % The format of the OilPaintImage method is:
507 % Image *OilPaintImage(const Image *image,const double radius,
508 % ExceptionInfo *exception)
510 % A description of each parameter follows:
512 % o image: the image.
514 % o radius: the radius of the circular neighborhood.
516 % o exception: return any errors or warnings in this structure.
520 static size_t **DestroyHistogramThreadSet(size_t **histogram)
525 assert(histogram != (size_t **) NULL);
526 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
527 if (histogram[i] != (size_t *) NULL)
528 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
529 histogram=(size_t **) RelinquishMagickMemory(histogram);
533 static size_t **AcquireHistogramThreadSet(const size_t count)
542 number_threads=GetOpenMPMaximumThreads();
543 histogram=(size_t **) AcquireQuantumMemory(number_threads,
545 if (histogram == (size_t **) NULL)
546 return((size_t **) NULL);
547 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
548 for (i=0; i < (ssize_t) number_threads; i++)
550 histogram[i]=(size_t *) AcquireQuantumMemory(count,
551 sizeof(**histogram));
552 if (histogram[i] == (size_t *) NULL)
553 return(DestroyHistogramThreadSet(histogram));
558 MagickExport Image *OilPaintImage(const Image *image,const double radius,
559 ExceptionInfo *exception)
561 #define NumberPaintBins 256
562 #define OilPaintImageTag "OilPaint/Image"
578 **restrict histograms,
585 Initialize painted image attributes.
587 assert(image != (const Image *) NULL);
588 assert(image->signature == MagickSignature);
589 if (image->debug != MagickFalse)
590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
591 assert(exception != (ExceptionInfo *) NULL);
592 assert(exception->signature == MagickSignature);
593 width=GetOptimalKernelWidth2D(radius,0.5);
594 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
595 if (paint_image == (Image *) NULL)
596 return((Image *) NULL);
597 if (SetImageStorageClass(paint_image,DirectClass) == MagickFalse)
599 InheritException(exception,&paint_image->exception);
600 paint_image=DestroyImage(paint_image);
601 return((Image *) NULL);
603 histograms=AcquireHistogramThreadSet(NumberPaintBins);
604 if (histograms == (size_t **) NULL)
606 paint_image=DestroyImage(paint_image);
607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
614 image_view=AcquireCacheView(image);
615 paint_view=AcquireCacheView(paint_image);
616 #if defined(MAGICKCORE_OPENMP_SUPPORT)
617 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
619 for (y=0; y < (ssize_t) image->rows; y++)
621 register const IndexPacket
624 register const PixelPacket
628 *restrict paint_indexes;
639 if (status == MagickFalse)
641 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
642 (width/2L),image->columns+width,width,exception);
643 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
645 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
650 indexes=GetCacheViewVirtualIndexQueue(image_view);
651 paint_indexes=GetCacheViewAuthenticIndexQueue(paint_view);
652 histogram=histograms[GetOpenMPThreadId()];
653 for (x=0; x < (ssize_t) image->columns; x++)
668 Assign most frequent color.
673 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
674 for (v=0; v < (ssize_t) width; v++)
676 for (u=0; u < (ssize_t) width; u++)
678 k=(ssize_t) ScaleQuantumToChar(PixelIntensityToQuantum(p+u+i));
680 if (histogram[k] > count)
686 i+=(ssize_t) (image->columns+width);
689 if (image->colorspace == CMYKColorspace)
690 paint_indexes[x]=indexes[x+j];
694 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
696 if (image->progress_monitor != (MagickProgressMonitor) NULL)
701 #if defined(MAGICKCORE_OPENMP_SUPPORT)
702 #pragma omp critical (MagickCore_OilPaintImage)
704 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
705 if (proceed == MagickFalse)
709 paint_view=DestroyCacheView(paint_view);
710 image_view=DestroyCacheView(image_view);
711 histograms=DestroyHistogramThreadSet(histograms);
712 if (status == MagickFalse)
713 paint_image=DestroyImage(paint_image);
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 % O p a q u e P a i n t I m a g e %
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % OpaquePaintImage() changes any pixel that matches color with the color
731 % By default color must match a particular pixel color exactly. However,
732 % in many cases two colors may differ by a small amount. Fuzz defines
733 % how much tolerance is acceptable to consider two colors as the same.
734 % For example, set fuzz to 10 and the color red at intensities of 100 and
735 % 102 respectively are now interpreted as the same color.
737 % The format of the OpaquePaintImage method is:
739 % MagickBooleanType OpaquePaintImage(Image *image,
740 % const PixelPacket *target,const PixelPacket *fill,
741 % const MagickBooleanType invert)
742 % MagickBooleanType OpaquePaintImageChannel(Image *image,
743 % const ChannelType channel,const PixelPacket *target,
744 % const PixelPacket *fill,const MagickBooleanType invert)
746 % A description of each parameter follows:
748 % o image: the image.
750 % o channel: the channel(s).
752 % o target: the RGB value of the target color.
754 % o fill: the replacement color.
756 % o invert: paint any pixel that does not match the target color.
760 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
761 const MagickPixelPacket *target,const MagickPixelPacket *fill,
762 const MagickBooleanType invert)
764 return(OpaquePaintImageChannel(image,AllChannels,target,fill,invert));
767 MagickExport MagickBooleanType OpaquePaintImageChannel(Image *image,
768 const ChannelType channel,const MagickPixelPacket *target,
769 const MagickPixelPacket *fill,const MagickBooleanType invert)
771 #define OpaquePaintImageTag "Opaque/Image"
791 assert(image != (Image *) NULL);
792 assert(image->signature == MagickSignature);
793 assert(target != (MagickPixelPacket *) NULL);
794 assert(fill != (MagickPixelPacket *) NULL);
795 if (image->debug != MagickFalse)
796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
797 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
800 Make image color opaque.
804 exception=(&image->exception);
805 GetMagickPixelPacket(image,&zero);
806 image_view=AcquireCacheView(image);
807 #if defined(MAGICKCORE_OPENMP_SUPPORT)
808 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
810 for (y=0; y < (ssize_t) image->rows; y++)
824 if (status == MagickFalse)
826 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
827 if (q == (PixelPacket *) NULL)
832 indexes=GetCacheViewAuthenticIndexQueue(image_view);
834 for (x=0; x < (ssize_t) image->columns; x++)
836 SetMagickPixelPacket(image,q,indexes+x,&pixel);
837 if (IsMagickColorSimilar(&pixel,target) != invert)
839 if ((channel & RedChannel) != 0)
840 q->red=ClampToQuantum(fill->red);
841 if ((channel & GreenChannel) != 0)
842 q->green=ClampToQuantum(fill->green);
843 if ((channel & BlueChannel) != 0)
844 q->blue=ClampToQuantum(fill->blue);
845 if ((channel & OpacityChannel) != 0)
846 q->opacity=ClampToQuantum(fill->opacity);
847 if (((channel & IndexChannel) != 0) &&
848 (image->colorspace == CMYKColorspace))
849 indexes[x]=ClampToQuantum(fill->index);
853 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
855 if (image->progress_monitor != (MagickProgressMonitor) NULL)
860 #if defined(MAGICKCORE_OPENMP_SUPPORT)
861 #pragma omp critical (MagickCore_OpaquePaintImageChannel)
863 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
865 if (proceed == MagickFalse)
869 image_view=DestroyCacheView(image_view);
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % T r a n s p a r e n t P a i n t I m a g e %
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % TransparentPaintImage() changes the opacity value associated with any pixel
885 % that matches color to the value defined by opacity.
887 % By default color must match a particular pixel color exactly. However,
888 % in many cases two colors may differ by a small amount. Fuzz defines
889 % how much tolerance is acceptable to consider two colors as the same.
890 % For example, set fuzz to 10 and the color red at intensities of 100 and
891 % 102 respectively are now interpreted as the same color.
893 % The format of the TransparentPaintImage method is:
895 % MagickBooleanType TransparentPaintImage(Image *image,
896 % const MagickPixelPacket *target,const Quantum opacity,
897 % const MagickBooleanType invert)
899 % A description of each parameter follows:
901 % o image: the image.
903 % o target: the target color.
905 % o opacity: the replacement opacity value.
907 % o invert: paint any pixel that does not match the target color.
910 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
911 const MagickPixelPacket *target,const Quantum opacity,
912 const MagickBooleanType invert)
914 #define TransparentPaintImageTag "Transparent/Image"
934 assert(image != (Image *) NULL);
935 assert(image->signature == MagickSignature);
936 assert(target != (MagickPixelPacket *) NULL);
937 if (image->debug != MagickFalse)
938 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
939 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
941 if (image->matte == MagickFalse)
942 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
944 Make image color transparent.
948 exception=(&image->exception);
949 GetMagickPixelPacket(image,&zero);
950 image_view=AcquireCacheView(image);
951 #if defined(MAGICKCORE_OPENMP_SUPPORT)
952 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
954 for (y=0; y < (ssize_t) image->rows; y++)
968 if (status == MagickFalse)
970 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
971 if (q == (PixelPacket *) NULL)
976 indexes=GetCacheViewAuthenticIndexQueue(image_view);
978 for (x=0; x < (ssize_t) image->columns; x++)
980 SetMagickPixelPacket(image,q,indexes+x,&pixel);
981 if (IsMagickColorSimilar(&pixel,target) != invert)
985 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
987 if (image->progress_monitor != (MagickProgressMonitor) NULL)
992 #if defined(MAGICKCORE_OPENMP_SUPPORT)
993 #pragma omp critical (MagickCore_TransparentPaintImage)
995 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
997 if (proceed == MagickFalse)
1001 image_view=DestroyCacheView(image_view);
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 % 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 %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016 % TransparentPaintImageChroma() changes the opacity value associated with any
1017 % pixel that matches color to the value defined by opacity.
1019 % As there is one fuzz value for the all the channels, the
1020 % TransparentPaintImage() API is not suitable for the operations like chroma,
1021 % where the tolerance for similarity of two color component (RGB) can be
1022 % different, Thus we define this method take two target pixels (one
1023 % low and one hight) and all the pixels of an image which are lying between
1024 % these two pixels are made transparent.
1026 % The format of the TransparentPaintImage method is:
1028 % MagickBooleanType TransparentPaintImage(Image *image,
1029 % const MagickPixelPacket *low,const MagickPixelPacket *hight,
1030 % const Quantum opacity,const MagickBooleanType invert)
1032 % A description of each parameter follows:
1034 % o image: the image.
1036 % o low: the low target color.
1038 % o high: the high target color.
1040 % o opacity: the replacement opacity value.
1042 % o invert: paint any pixel that does not match the target color.
1045 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1046 const MagickPixelPacket *low,const MagickPixelPacket *high,
1047 const Quantum opacity,const MagickBooleanType invert)
1049 #define TransparentPaintImageTag "Transparent/Image"
1066 assert(image != (Image *) NULL);
1067 assert(image->signature == MagickSignature);
1068 assert(high != (MagickPixelPacket *) NULL);
1069 assert(low != (MagickPixelPacket *) NULL);
1070 if (image->debug != MagickFalse)
1071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1072 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1073 return(MagickFalse);
1074 if (image->matte == MagickFalse)
1075 (void) SetImageAlphaChannel(image,ResetAlphaChannel);
1077 Make image color transparent.
1081 exception=(&image->exception);
1082 image_view=AcquireCacheView(image);
1083 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1084 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1086 for (y=0; y < (ssize_t) image->rows; y++)
1094 register IndexPacket
1100 register PixelPacket
1103 if (status == MagickFalse)
1105 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1106 if (q == (PixelPacket *) NULL)
1111 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1112 GetMagickPixelPacket(image,&pixel);
1113 for (x=0; x < (ssize_t) image->columns; x++)
1115 SetMagickPixelPacket(image,q,indexes+x,&pixel);
1116 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1117 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1118 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1119 MagickTrue : MagickFalse;
1120 if (match != invert)
1124 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1126 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1132 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1134 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1136 if (proceed == MagickFalse)
1140 image_view=DestroyCacheView(image_view);