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 "MagickCore/studio.h"
43 #include "MagickCore/color.h"
44 #include "MagickCore/color-private.h"
45 #include "MagickCore/colorspace-private.h"
46 #include "MagickCore/composite.h"
47 #include "MagickCore/composite-private.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/draw-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/gem.h"
53 #include "MagickCore/gem-private.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/paint.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/thread-private.h"
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 % F l o o d f i l l P a i n t I m a g e %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % FloodfillPaintImage() changes the color value of any pixel that matches
73 % target and is an immediate neighbor. If the method FillToBorderMethod is
74 % specified, the color value is changed for any neighbor pixel that does not
75 % match the bordercolor member of image.
77 % By default target must match a particular pixel color exactly. However,
78 % in many cases two colors may differ by a small amount. The fuzz member of
79 % image defines how much tolerance is acceptable to consider two colors as
80 % the same. For example, set fuzz to 10 and the color red at intensities of
81 % 100 and 102 respectively are now interpreted as the same color for the
82 % purposes of the floodfill.
84 % The format of the FloodfillPaintImage method is:
86 % MagickBooleanType FloodfillPaintImage(Image *image,
87 % const DrawInfo *draw_info,const PixelInfo target,
88 % const ssize_t x_offset,const ssize_t y_offset,
89 % const MagickBooleanType invert,ExceptionInfo *exception)
91 % A description of each parameter follows:
95 % o draw_info: the draw info.
97 % o target: the RGB value of the target color.
99 % o x_offset,y_offset: the starting location of the operation.
101 % o invert: paint any pixel that does not match the target color.
103 % o exception: return any errors or warnings in this structure.
106 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
107 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
108 const ssize_t y_offset,const MagickBooleanType invert,
109 ExceptionInfo *exception)
111 #define MaxStacksize (1UL << 15)
112 #define PushSegmentStack(up,left,right,delta) \
114 if (s >= (segment_stack+MaxStacksize)) \
115 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
118 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
120 s->x1=(double) (left); \
121 s->y1=(double) (up); \
122 s->x2=(double) (right); \
123 s->y2=(double) (delta); \
162 Check boundary conditions.
164 assert(image != (Image *) NULL);
165 assert(image->signature == MagickSignature);
166 if (image->debug != MagickFalse)
167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
168 assert(draw_info != (DrawInfo *) NULL);
169 assert(draw_info->signature == MagickSignature);
170 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
172 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
174 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
176 if (image->matte == MagickFalse)
177 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
181 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
182 if (floodplane_image == (Image *) NULL)
184 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
185 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
186 sizeof(*segment_stack));
187 if (segment_stack == (SegmentInfo *) NULL)
189 floodplane_image=DestroyImage(floodplane_image);
190 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
194 Push initial segment on stack.
201 PushSegmentStack(y,x,x,1);
202 PushSegmentStack(y+1,x,x,-1);
203 GetPixelInfo(image,&fill);
204 GetPixelInfo(image,&pixel);
205 image_view=AcquireCacheView(image);
206 floodplane_view=AcquireCacheView(floodplane_image);
207 while (s > segment_stack)
209 register const Quantum
219 Pop segment off stack.
224 offset=(ssize_t) s->y2;
225 y=(ssize_t) s->y1+offset;
227 Recolor neighboring pixels.
229 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
230 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
232 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
234 p+=x1*GetPixelChannels(image);
235 q+=x1*GetPixelChannels(floodplane_image);
236 for (x=x1; x >= 0; x--)
238 if (GetPixelAlpha(image,q) == TransparentAlpha)
240 SetPixelInfo(image,p,&pixel);
241 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
243 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
244 p-=GetPixelChannels(image);
245 q-=GetPixelChannels(floodplane_image);
247 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
249 skip=x >= x1 ? MagickTrue : MagickFalse;
250 if (skip == MagickFalse)
254 PushSegmentStack(y,start,x1-1,-offset);
259 if (skip == MagickFalse)
261 if (x < (ssize_t) image->columns)
263 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
265 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
266 image->columns-x,1,exception);
267 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
269 for ( ; x < (ssize_t) image->columns; x++)
271 if (GetPixelAlpha(image,q) == TransparentAlpha)
273 SetPixelInfo(image,p,&pixel);
274 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
276 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
277 p+=GetPixelChannels(image);
278 q+=GetPixelChannels(floodplane_image);
280 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
281 if (status == MagickFalse)
284 PushSegmentStack(y,start,x-1,offset);
286 PushSegmentStack(y,x2+1,x-1,-offset);
292 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
294 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
296 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
298 for ( ; x <= x2; x++)
300 if (GetPixelAlpha(image,q) == TransparentAlpha)
302 SetPixelInfo(image,p,&pixel);
303 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
305 p+=GetPixelChannels(image);
306 q+=GetPixelChannels(floodplane_image);
312 for (y=0; y < (ssize_t) image->rows; y++)
314 register const Quantum
324 Tile fill color onto floodplane.
326 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
331 for (x=0; x < (ssize_t) image->columns; x++)
333 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
335 (void) GetFillColor(draw_info,x,y,&fill_color);
336 SetPixelInfoPacket(image,&fill_color,&fill);
337 if (image->colorspace == CMYKColorspace)
338 ConvertRGBToCMYK(&fill);
339 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
340 SetPixelRed(image,ClampToQuantum(fill.red),q);
341 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
342 SetPixelGreen(image,ClampToQuantum(fill.green),q);
343 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
344 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
345 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
346 SetPixelBlack(image,ClampToQuantum(fill.black),q);
347 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
348 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
350 p+=GetPixelChannels(floodplane_image);
351 q+=GetPixelChannels(image);
353 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
356 floodplane_view=DestroyCacheView(floodplane_view);
357 image_view=DestroyCacheView(image_view);
358 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
359 floodplane_image=DestroyImage(floodplane_image);
360 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 + G r a d i e n t I m a g e %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 % GradientImage() applies a continuously smooth color transitions along a
375 % vector from one color to another.
377 % Note, the interface of this method will change in the future to support
378 % more than one transistion.
380 % The format of the GradientImage method is:
382 % MagickBooleanType GradientImage(Image *image,const GradientType type,
383 % const SpreadMethod method,const PixelPacket *start_color,
384 % const PixelPacket *stop_color,ExceptionInfo *exception)
386 % A description of each parameter follows:
388 % o image: the image.
390 % o type: the gradient type: linear or radial.
392 % o spread: the gradient spread meathod: pad, reflect, or repeat.
394 % o start_color: the start color.
396 % o stop_color: the stop color.
398 % o exception: return any errors or warnings in this structure.
402 static inline double MagickMax(const double x,const double y)
404 return(x > y ? x : y);
407 MagickExport MagickBooleanType GradientImage(Image *image,
408 const GradientType type,const SpreadMethod method,
409 const PixelPacket *start_color,const PixelPacket *stop_color,
410 ExceptionInfo *exception)
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 GetPixelInfo(image,&gradient->stops[i].color);
459 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
460 gradient->stops[0].offset=0.0;
461 SetPixelInfoPacket(image,stop_color,&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->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
469 image->matte=MagickFalse;
470 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
471 (IsPixelPacketGray(stop_color) != MagickFalse))
472 image->type=GrayscaleType;
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % O i l P a i n t I m a g e %
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 % OilPaintImage() applies a special effect filter that simulates an oil
488 % painting. Each pixel is replaced by the most frequent color occurring
489 % in a circular region defined by radius.
491 % The format of the OilPaintImage method is:
493 % Image *OilPaintImage(const Image *image,const double radius,
494 % const double sigma,ExceptionInfo *exception)
496 % A description of each parameter follows:
498 % o image: the image.
500 % o radius: the radius of the circular neighborhood.
502 % o sigma: the standard deviation of the Gaussian, in pixels.
504 % o exception: return any errors or warnings in this structure.
508 static size_t **DestroyHistogramThreadSet(size_t **histogram)
513 assert(histogram != (size_t **) NULL);
514 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
515 if (histogram[i] != (size_t *) NULL)
516 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
517 histogram=(size_t **) RelinquishMagickMemory(histogram);
521 static size_t **AcquireHistogramThreadSet(const size_t count)
530 number_threads=GetOpenMPMaximumThreads();
531 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
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,sizeof(**histogram));
538 if (histogram[i] == (size_t *) NULL)
539 return(DestroyHistogramThreadSet(histogram));
544 MagickExport Image *OilPaintImage(const Image *image,const double radius,
545 const double sigma,ExceptionInfo *exception)
547 #define NumberPaintBins 256
548 #define OilPaintImageTag "OilPaint/Image"
571 Initialize painted image attributes.
573 assert(image != (const Image *) NULL);
574 assert(image->signature == MagickSignature);
575 if (image->debug != MagickFalse)
576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
577 assert(exception != (ExceptionInfo *) NULL);
578 assert(exception->signature == MagickSignature);
579 width=GetOptimalKernelWidth2D(radius,sigma);
580 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
581 if (paint_image == (Image *) NULL)
582 return((Image *) NULL);
583 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
585 paint_image=DestroyImage(paint_image);
586 return((Image *) NULL);
588 histograms=AcquireHistogramThreadSet(NumberPaintBins);
589 if (histograms == (size_t **) NULL)
591 paint_image=DestroyImage(paint_image);
592 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
599 image_view=AcquireCacheView(image);
600 paint_view=AcquireCacheView(paint_image);
601 #if defined(MAGICKCORE_OPENMP_SUPPORT)
602 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
604 for (y=0; y < (ssize_t) image->rows; y++)
606 register const Quantum
618 if (status == MagickFalse)
620 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
621 (width/2L),image->columns+width,width,exception);
622 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
624 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
629 histogram=histograms[GetOpenMPThreadId()];
630 for (x=0; x < (ssize_t) image->columns; x++)
645 Assign most frequent color.
650 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
651 for (v=0; v < (ssize_t) width; v++)
653 for (u=0; u < (ssize_t) width; u++)
655 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
656 GetPixelChannels(image)*(u+i)));
658 if (histogram[k] > count)
664 i+=(ssize_t) (image->columns+width);
666 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
667 SetPixelRed(paint_image,GetPixelRed(image,p+j*
668 GetPixelChannels(image)),q);
669 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
670 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
671 GetPixelChannels(image)),q);
672 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
673 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
674 GetPixelChannels(image)),q);
675 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
676 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
677 GetPixelChannels(image)),q);
678 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
679 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
680 GetPixelChannels(image)),q);
681 p+=GetPixelChannels(image);
682 q+=GetPixelChannels(paint_image);
684 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
686 if (image->progress_monitor != (MagickProgressMonitor) NULL)
691 #if defined(MAGICKCORE_OPENMP_SUPPORT)
692 #pragma omp critical (MagickCore_OilPaintImage)
694 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
695 if (proceed == MagickFalse)
699 paint_view=DestroyCacheView(paint_view);
700 image_view=DestroyCacheView(image_view);
701 histograms=DestroyHistogramThreadSet(histograms);
702 if (status == MagickFalse)
703 paint_image=DestroyImage(paint_image);
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712 % O p a q u e P a i n t I m a g e %
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 % OpaquePaintImage() changes any pixel that matches color with the color
721 % By default color must match a particular pixel color exactly. However, in
722 % many cases two colors may differ by a small amount. Fuzz defines how much
723 % tolerance is acceptable to consider two colors as the same. For example,
724 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
725 % are now interpreted as the same color.
727 % The format of the OpaquePaintImage method is:
729 % MagickBooleanType OpaquePaintImage(Image *image,
730 % const PixelPacket *target,const PixelPacket *fill,
731 % const MagickBooleanType invert,ExceptionInfo *exception)
733 % A description of each parameter follows:
735 % o image: the image.
737 % o target: the RGB value of the target color.
739 % o fill: the replacement color.
741 % o invert: paint any pixel that does not match the target color.
743 % o exception: return any errors or warnings in this structure.
746 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
747 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
748 ExceptionInfo *exception)
750 #define OpaquePaintImageTag "Opaque/Image"
767 assert(image != (Image *) NULL);
768 assert(image->signature == MagickSignature);
769 assert(target != (PixelInfo *) NULL);
770 assert(fill != (PixelInfo *) NULL);
771 if (image->debug != MagickFalse)
772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
773 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
776 Make image color opaque.
780 GetPixelInfo(image,&zero);
781 image_view=AcquireCacheView(image);
782 #if defined(MAGICKCORE_OPENMP_SUPPORT)
783 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
785 for (y=0; y < (ssize_t) image->rows; y++)
796 if (status == MagickFalse)
798 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
799 if (q == (Quantum *) NULL)
805 for (x=0; x < (ssize_t) image->columns; x++)
807 SetPixelInfo(image,q,&pixel);
808 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
810 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
811 SetPixelRed(image,ClampToQuantum(fill->red),q);
812 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
813 SetPixelGreen(image,ClampToQuantum(fill->green),q);
814 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
815 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
816 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
817 SetPixelBlack(image,ClampToQuantum(fill->black),q);
818 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
819 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
821 q+=GetPixelChannels(image);
823 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
825 if (image->progress_monitor != (MagickProgressMonitor) NULL)
830 #if defined(MAGICKCORE_OPENMP_SUPPORT)
831 #pragma omp critical (MagickCore_OpaquePaintImage)
833 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
835 if (proceed == MagickFalse)
839 image_view=DestroyCacheView(image_view);
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 % T r a n s p a r e n t P a i n t I m a g e %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % TransparentPaintImage() changes the opacity value associated with any pixel
855 % that matches color to the value defined by opacity.
857 % By default color must match a particular pixel color exactly. However, in
858 % many cases two colors may differ by a small amount. Fuzz defines how much
859 % tolerance is acceptable to consider two colors as the same. For example,
860 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
861 % are now interpreted as the same color.
863 % The format of the TransparentPaintImage method is:
865 % MagickBooleanType TransparentPaintImage(Image *image,
866 % const PixelInfo *target,const Quantum opacity,
867 % const MagickBooleanType invert,ExceptionInfo *exception)
869 % A description of each parameter follows:
871 % o image: the image.
873 % o target: the target color.
875 % o opacity: the replacement opacity value.
877 % o invert: paint any pixel that does not match the target color.
879 % o exception: return any errors or warnings in this structure.
882 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
883 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
884 ExceptionInfo *exception)
886 #define TransparentPaintImageTag "Transparent/Image"
903 assert(image != (Image *) NULL);
904 assert(image->signature == MagickSignature);
905 assert(target != (PixelInfo *) NULL);
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
908 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
910 if (image->matte == MagickFalse)
911 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
913 Make image color transparent.
917 GetPixelInfo(image,&zero);
918 image_view=AcquireCacheView(image);
919 #if defined(MAGICKCORE_OPENMP_SUPPORT)
920 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
922 for (y=0; y < (ssize_t) image->rows; y++)
933 if (status == MagickFalse)
935 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
936 if (q == (Quantum *) NULL)
942 for (x=0; x < (ssize_t) image->columns; x++)
944 SetPixelInfo(image,q,&pixel);
945 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
946 SetPixelAlpha(image,opacity,q);
947 q+=GetPixelChannels(image);
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
956 #if defined(MAGICKCORE_OPENMP_SUPPORT)
957 #pragma omp critical (MagickCore_TransparentPaintImage)
959 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
961 if (proceed == MagickFalse)
965 image_view=DestroyCacheView(image_view);
970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974 % 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 %
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 % TransparentPaintImageChroma() changes the opacity value associated with any
981 % pixel that matches color to the value defined by opacity.
983 % As there is one fuzz value for the all the channels, TransparentPaintImage()
984 % is not suitable for the operations like chroma, where the tolerance for
985 % similarity of two color component (RGB) can be different. Thus we define
986 % this method to take two target pixels (one low and one high) and all the
987 % pixels of an image which are lying between these two pixels are made
990 % The format of the TransparentPaintImageChroma method is:
992 % MagickBooleanType TransparentPaintImageChroma(Image *image,
993 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
994 % const MagickBooleanType invert,ExceptionInfo *exception)
996 % A description of each parameter follows:
998 % o image: the image.
1000 % o low: the low target color.
1002 % o high: the high target color.
1004 % o opacity: the replacement opacity value.
1006 % o invert: paint any pixel that does not match the target color.
1008 % o exception: return any errors or warnings in this structure.
1011 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1012 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1013 const MagickBooleanType invert,ExceptionInfo *exception)
1015 #define TransparentPaintImageTag "Transparent/Image"
1029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
1031 assert(high != (PixelInfo *) NULL);
1032 assert(low != (PixelInfo *) NULL);
1033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1036 return(MagickFalse);
1037 if (image->matte == MagickFalse)
1038 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1040 Make image color transparent.
1044 image_view=AcquireCacheView(image);
1045 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1046 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1048 for (y=0; y < (ssize_t) image->rows; y++)
1062 if (status == MagickFalse)
1064 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1065 if (q == (Quantum *) NULL)
1070 GetPixelInfo(image,&pixel);
1071 for (x=0; x < (ssize_t) image->columns; x++)
1073 SetPixelInfo(image,q,&pixel);
1074 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1075 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1076 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1078 if (match != invert)
1079 SetPixelAlpha(image,opacity,q);
1080 q+=GetPixelChannels(image);
1082 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1084 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1089 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1090 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1092 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1094 if (proceed == MagickFalse)
1098 image_view=DestroyCacheView(image_view);