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/monitor.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/paint.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/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. However,
77 % in many cases two colors may differ by a small amount. The fuzz member of
78 % image defines how much tolerance is acceptable to consider two colors as
79 % the same. For example, set fuzz to 10 and the color red at intensities of
80 % 100 and 102 respectively are now interpreted as the same color for the
81 % purposes of the floodfill.
83 % The format of the FloodfillPaintImage method is:
85 % MagickBooleanType FloodfillPaintImage(Image *image,
86 % const DrawInfo *draw_info,const PixelInfo target,
87 % const ssize_t x_offset,const ssize_t y_offset,
88 % const MagickBooleanType invert,ExceptionInfo *exception)
90 % A description of each parameter follows:
94 % o draw_info: the draw info.
96 % o target: the RGB value of the target color.
98 % o x_offset,y_offset: the starting location of the operation.
100 % o invert: paint any pixel that does not match the target color.
102 % o exception: return any errors or warnings in this structure.
105 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
106 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
107 const ssize_t y_offset,const MagickBooleanType invert,
108 ExceptionInfo *exception)
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); \
161 Check boundary conditions.
163 assert(image != (Image *) NULL);
164 assert(image->signature == MagickSignature);
165 if (image->debug != MagickFalse)
166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167 assert(draw_info != (DrawInfo *) NULL);
168 assert(draw_info->signature == MagickSignature);
169 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
171 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
175 if (image->matte == MagickFalse)
176 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
180 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
181 if (floodplane_image == (Image *) NULL)
183 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
184 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
185 sizeof(*segment_stack));
186 if (segment_stack == (SegmentInfo *) NULL)
188 floodplane_image=DestroyImage(floodplane_image);
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
193 Push initial segment on stack.
200 PushSegmentStack(y,x,x,1);
201 PushSegmentStack(y+1,x,x,-1);
202 GetPixelInfo(image,&fill);
203 GetPixelInfo(image,&pixel);
204 image_view=AcquireCacheView(image);
205 floodplane_view=AcquireCacheView(floodplane_image);
206 while (s > segment_stack)
208 register const Quantum
218 Pop segment off stack.
223 offset=(ssize_t) s->y2;
224 y=(ssize_t) s->y1+offset;
226 Recolor neighboring pixels.
228 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
229 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
231 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
233 p+=x1*GetPixelChannels(image);
234 q+=x1*GetPixelChannels(floodplane_image);
235 for (x=x1; x >= 0; x--)
237 if (GetPixelAlpha(image,q) == TransparentAlpha)
239 SetPixelInfo(image,p,&pixel);
240 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
242 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
243 p-=GetPixelChannels(image);
244 q-=GetPixelChannels(floodplane_image);
246 if (SyncCacheViewAuthenticPixels(floodplane_view,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=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
264 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
265 image->columns-x,1,exception);
266 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
268 for ( ; x < (ssize_t) image->columns; x++)
270 if (GetPixelAlpha(image,q) == TransparentAlpha)
272 SetPixelInfo(image,p,&pixel);
273 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
275 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
276 p+=GetPixelChannels(image);
277 q+=GetPixelChannels(floodplane_image);
279 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
280 if (status == MagickFalse)
283 PushSegmentStack(y,start,x-1,offset);
285 PushSegmentStack(y,x2+1,x-1,-offset);
291 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
293 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
295 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
297 for ( ; x <= x2; x++)
299 if (GetPixelAlpha(image,q) == TransparentAlpha)
301 SetPixelInfo(image,p,&pixel);
302 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
304 p+=GetPixelChannels(image);
305 q+=GetPixelChannels(floodplane_image);
311 for (y=0; y < (ssize_t) image->rows; y++)
313 register const Quantum
323 Tile fill color onto floodplane.
325 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
327 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
330 for (x=0; x < (ssize_t) image->columns; x++)
332 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
334 (void) GetFillColor(draw_info,x,y,&fill_color);
335 SetPixelInfoPacket(image,&fill_color,&fill);
336 if (image->colorspace == CMYKColorspace)
337 ConvertRGBToCMYK(&fill);
338 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
339 SetPixelRed(image,ClampToQuantum(fill.red),q);
340 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
341 SetPixelGreen(image,ClampToQuantum(fill.green),q);
342 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
343 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
344 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
345 SetPixelBlack(image,ClampToQuantum(fill.black),q);
346 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
347 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
349 p+=GetPixelChannels(floodplane_image);
350 q+=GetPixelChannels(image);
352 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
355 floodplane_view=DestroyCacheView(floodplane_view);
356 image_view=DestroyCacheView(image_view);
357 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
358 floodplane_image=DestroyImage(floodplane_image);
359 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 + G r a d i e n t I m a g e %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % GradientImage() applies a continuously smooth color transitions along a
374 % vector from one color to another.
376 % Note, the interface of this method will change in the future to support
377 % more than one transistion.
379 % The format of the GradientImage method is:
381 % MagickBooleanType GradientImage(Image *image,const GradientType type,
382 % const SpreadMethod method,const PixelPacket *start_color,
383 % const PixelPacket *stop_color,ExceptionInfo *exception)
385 % A description of each parameter follows:
387 % o image: the image.
389 % o type: the gradient type: linear or radial.
391 % o spread: the gradient spread meathod: pad, reflect, or repeat.
393 % o start_color: the start color.
395 % o stop_color: the stop color.
397 % o exception: return any errors or warnings in this structure.
401 static inline double MagickMax(const double x,const double y)
403 return(x > y ? x : y);
406 MagickExport MagickBooleanType GradientImage(Image *image,
407 const GradientType type,const SpreadMethod method,
408 const PixelPacket *start_color,const PixelPacket *stop_color,
409 ExceptionInfo *exception)
424 Set gradient start-stop end points.
426 assert(image != (const Image *) NULL);
427 assert(image->signature == MagickSignature);
428 if (image->debug != MagickFalse)
429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
430 assert(start_color != (const PixelPacket *) NULL);
431 assert(stop_color != (const PixelPacket *) NULL);
432 draw_info=AcquireDrawInfo();
433 gradient=(&draw_info->gradient);
435 gradient->bounding_box.width=image->columns;
436 gradient->bounding_box.height=image->rows;
437 gradient->gradient_vector.x2=(double) image->columns-1.0;
438 gradient->gradient_vector.y2=(double) image->rows-1.0;
439 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
440 gradient->gradient_vector.x2=0.0;
441 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
442 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
443 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
444 gradient->spread=method;
446 Define the gradient to fill between the stops.
448 gradient->number_stops=2;
449 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
450 sizeof(*gradient->stops));
451 if (gradient->stops == (StopInfo *) NULL)
452 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
454 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
455 sizeof(*gradient->stops));
456 for (i=0; i < (ssize_t) gradient->number_stops; i++)
457 GetPixelInfo(image,&gradient->stops[i].color);
458 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
459 gradient->stops[0].offset=0.0;
460 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
461 gradient->stops[1].offset=1.0;
463 Draw a gradient on the image.
465 status=DrawGradientImage(image,draw_info);
466 draw_info=DestroyDrawInfo(draw_info);
467 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
468 image->matte=MagickFalse;
469 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
470 (IsPixelPacketGray(stop_color) != MagickFalse))
471 image->type=GrayscaleType;
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % O i l P a i n t I m a g e %
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 % OilPaintImage() applies a special effect filter that simulates an oil
487 % painting. Each pixel is replaced by the most frequent color occurring
488 % in a circular region defined by radius.
490 % The format of the OilPaintImage method is:
492 % Image *OilPaintImage(const Image *image,const double radius,
493 % const double sigma,ExceptionInfo *exception)
495 % A description of each parameter follows:
497 % o image: the image.
499 % o radius: the radius of the circular neighborhood.
501 % o sigma: the standard deviation of the Gaussian, in pixels.
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 **) RelinquishMagickMemory(histogram);
520 static size_t **AcquireHistogramThreadSet(const size_t count)
529 number_threads=GetOpenMPMaximumThreads();
530 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
531 if (histogram == (size_t **) NULL)
532 return((size_t **) NULL);
533 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
534 for (i=0; i < (ssize_t) number_threads; i++)
536 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
537 if (histogram[i] == (size_t *) NULL)
538 return(DestroyHistogramThreadSet(histogram));
543 MagickExport Image *OilPaintImage(const Image *image,const double radius,
544 const double sigma,ExceptionInfo *exception)
546 #define NumberPaintBins 256
547 #define OilPaintImageTag "OilPaint/Image"
570 Initialize painted image attributes.
572 assert(image != (const Image *) NULL);
573 assert(image->signature == MagickSignature);
574 if (image->debug != MagickFalse)
575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
576 assert(exception != (ExceptionInfo *) NULL);
577 assert(exception->signature == MagickSignature);
578 width=GetOptimalKernelWidth2D(radius,sigma);
579 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
580 if (paint_image == (Image *) NULL)
581 return((Image *) NULL);
582 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
584 paint_image=DestroyImage(paint_image);
585 return((Image *) NULL);
587 histograms=AcquireHistogramThreadSet(NumberPaintBins);
588 if (histograms == (size_t **) NULL)
590 paint_image=DestroyImage(paint_image);
591 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
598 image_view=AcquireCacheView(image);
599 paint_view=AcquireCacheView(paint_image);
600 #if defined(MAGICKCORE_OPENMP_SUPPORT)
601 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
603 for (y=0; y < (ssize_t) image->rows; y++)
605 register const Quantum
617 if (status == MagickFalse)
619 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
620 (width/2L),image->columns+width,width,exception);
621 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
623 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
628 histogram=histograms[GetOpenMPThreadId()];
629 for (x=0; x < (ssize_t) image->columns; x++)
644 Assign most frequent color.
649 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
650 for (v=0; v < (ssize_t) width; v++)
652 for (u=0; u < (ssize_t) width; u++)
654 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
655 GetPixelChannels(image)*(u+i)));
657 if (histogram[k] > count)
663 i+=(ssize_t) (image->columns+width);
665 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
666 SetPixelRed(paint_image,GetPixelRed(image,p+j*
667 GetPixelChannels(image)),q);
668 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
669 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
670 GetPixelChannels(image)),q);
671 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
672 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
673 GetPixelChannels(image)),q);
674 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
675 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
676 GetPixelChannels(image)),q);
677 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
678 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
679 GetPixelChannels(image)),q);
680 p+=GetPixelChannels(image);
681 q+=GetPixelChannels(paint_image);
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, in
721 % many cases two colors may differ by a small amount. Fuzz defines how much
722 % tolerance is acceptable to consider two colors as the same. For example,
723 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
724 % 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,ExceptionInfo *exception)
732 % A description of each parameter follows:
734 % o image: the image.
736 % o target: the RGB value of the target color.
738 % o fill: the replacement color.
740 % o invert: paint any pixel that does not match the target color.
742 % o exception: return any errors or warnings in this structure.
745 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
746 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
747 ExceptionInfo *exception)
749 #define OpaquePaintImageTag "Opaque/Image"
766 assert(image != (Image *) NULL);
767 assert(image->signature == MagickSignature);
768 assert(target != (PixelInfo *) NULL);
769 assert(fill != (PixelInfo *) NULL);
770 if (image->debug != MagickFalse)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
772 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
775 Make image color opaque.
779 GetPixelInfo(image,&zero);
780 image_view=AcquireCacheView(image);
781 #if defined(MAGICKCORE_OPENMP_SUPPORT)
782 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
784 for (y=0; y < (ssize_t) image->rows; y++)
795 if (status == MagickFalse)
797 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
798 if (q == (Quantum *) NULL)
804 for (x=0; x < (ssize_t) image->columns; x++)
806 SetPixelInfo(image,q,&pixel);
807 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
809 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
810 SetPixelRed(image,ClampToQuantum(fill->red),q);
811 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
812 SetPixelGreen(image,ClampToQuantum(fill->green),q);
813 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
814 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
815 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
816 SetPixelBlack(image,ClampToQuantum(fill->black),q);
817 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
818 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
820 q+=GetPixelChannels(image);
822 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
829 #if defined(MAGICKCORE_OPENMP_SUPPORT)
830 #pragma omp critical (MagickCore_OpaquePaintImage)
832 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
834 if (proceed == MagickFalse)
838 image_view=DestroyCacheView(image_view);
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 % T r a n s p a r e n t P a i n t I m a g e %
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % TransparentPaintImage() changes the opacity value associated with any pixel
854 % that matches color to the value defined by opacity.
856 % By default color must match a particular pixel color exactly. However, in
857 % many cases two colors may differ by a small amount. Fuzz defines how much
858 % tolerance is acceptable to consider two colors as the same. For example,
859 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
860 % are now interpreted as the same color.
862 % The format of the TransparentPaintImage method is:
864 % MagickBooleanType TransparentPaintImage(Image *image,
865 % const PixelInfo *target,const Quantum opacity,
866 % const MagickBooleanType invert,ExceptionInfo *exception)
868 % A description of each parameter follows:
870 % o image: the image.
872 % o target: the target color.
874 % o opacity: the replacement opacity value.
876 % o invert: paint any pixel that does not match the target color.
878 % o exception: return any errors or warnings in this structure.
881 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
882 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
883 ExceptionInfo *exception)
885 #define TransparentPaintImageTag "Transparent/Image"
902 assert(image != (Image *) NULL);
903 assert(image->signature == MagickSignature);
904 assert(target != (PixelInfo *) NULL);
905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
907 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
909 if (image->matte == MagickFalse)
910 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
912 Make image color transparent.
916 GetPixelInfo(image,&zero);
917 image_view=AcquireCacheView(image);
918 #if defined(MAGICKCORE_OPENMP_SUPPORT)
919 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
921 for (y=0; y < (ssize_t) image->rows; y++)
932 if (status == MagickFalse)
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
935 if (q == (Quantum *) NULL)
941 for (x=0; x < (ssize_t) image->columns; x++)
943 SetPixelInfo(image,q,&pixel);
944 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
945 SetPixelAlpha(image,opacity,q);
946 q+=GetPixelChannels(image);
948 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
950 if (image->progress_monitor != (MagickProgressMonitor) NULL)
955 #if defined(MAGICKCORE_OPENMP_SUPPORT)
956 #pragma omp critical (MagickCore_TransparentPaintImage)
958 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
960 if (proceed == MagickFalse)
964 image_view=DestroyCacheView(image_view);
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % 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 %
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % TransparentPaintImageChroma() changes the opacity value associated with any
980 % pixel that matches color to the value defined by opacity.
982 % As there is one fuzz value for the all the channels, TransparentPaintImage()
983 % is not suitable for the operations like chroma, where the tolerance for
984 % similarity of two color component (RGB) can be different. Thus we define
985 % this method to take two target pixels (one low and one high) and all the
986 % pixels of an image which are lying between these two pixels are made
989 % The format of the TransparentPaintImageChroma method is:
991 % MagickBooleanType TransparentPaintImageChroma(Image *image,
992 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
993 % const MagickBooleanType invert,ExceptionInfo *exception)
995 % A description of each parameter follows:
997 % o image: the image.
999 % o low: the low target color.
1001 % o high: the high target color.
1003 % o opacity: the replacement opacity value.
1005 % o invert: paint any pixel that does not match the target color.
1007 % o exception: return any errors or warnings in this structure.
1010 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1011 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1012 const MagickBooleanType invert,ExceptionInfo *exception)
1014 #define TransparentPaintImageTag "Transparent/Image"
1028 assert(image != (Image *) NULL);
1029 assert(image->signature == MagickSignature);
1030 assert(high != (PixelInfo *) NULL);
1031 assert(low != (PixelInfo *) NULL);
1032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1034 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1035 return(MagickFalse);
1036 if (image->matte == MagickFalse)
1037 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1039 Make image color transparent.
1043 image_view=AcquireCacheView(image);
1044 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1045 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1047 for (y=0; y < (ssize_t) image->rows; y++)
1061 if (status == MagickFalse)
1063 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1064 if (q == (Quantum *) NULL)
1069 GetPixelInfo(image,&pixel);
1070 for (x=0; x < (ssize_t) image->columns; x++)
1072 SetPixelInfo(image,q,&pixel);
1073 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1074 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1075 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1077 if (match != invert)
1078 SetPixelAlpha(image,opacity,q);
1079 q+=GetPixelChannels(image);
1081 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1083 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1088 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1089 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1091 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1093 if (proceed == MagickFalse)
1097 image_view=DestroyCacheView(image_view);