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-2012 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/statistic.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/thread-private.h"
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 % F l o o d f i l l P a i n t I m a g e %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % FloodfillPaintImage() changes the color value of any pixel that matches
74 % target and is an immediate neighbor. If the method FillToBorderMethod is
75 % specified, the color value is changed for any neighbor pixel that does not
76 % match the bordercolor member of image.
78 % By default target must match a particular pixel color exactly. However,
79 % in many cases two colors may differ by a small amount. The fuzz member of
80 % image defines how much tolerance is acceptable to consider two colors as
81 % the same. For example, set fuzz to 10 and the color red at intensities of
82 % 100 and 102 respectively are now interpreted as the same color for the
83 % purposes of the floodfill.
85 % The format of the FloodfillPaintImage method is:
87 % MagickBooleanType FloodfillPaintImage(Image *image,
88 % const DrawInfo *draw_info,const PixelInfo target,
89 % const ssize_t x_offset,const ssize_t y_offset,
90 % const MagickBooleanType invert,ExceptionInfo *exception)
92 % A description of each parameter follows:
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.
104 % o exception: return any errors or warnings in this structure.
107 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
108 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
109 const ssize_t y_offset,const MagickBooleanType invert,
110 ExceptionInfo *exception)
112 #define MaxStacksize (1UL << 15)
113 #define PushSegmentStack(up,left,right,delta) \
115 if (s >= (segment_stack+MaxStacksize)) \
116 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
119 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
121 s->x1=(double) (left); \
122 s->y1=(double) (up); \
123 s->x2=(double) (right); \
124 s->y2=(double) (delta); \
160 Check boundary conditions.
162 assert(image != (Image *) NULL);
163 assert(image->signature == MagickSignature);
164 if (image->debug != MagickFalse)
165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
166 assert(draw_info != (DrawInfo *) NULL);
167 assert(draw_info->signature == MagickSignature);
168 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
170 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
172 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
174 if (image->matte == MagickFalse)
175 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
179 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
181 if (floodplane_image == (Image *) NULL)
183 floodplane_image->colorspace=GRAYColorspace;
184 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,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,&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 (GetPixelGray(floodplane_image,q) != 0)
239 GetPixelInfoPixel(image,p,&pixel);
240 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
242 SetPixelGray(floodplane_image,QuantumRange,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,image->columns-
266 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
268 for ( ; x < (ssize_t) image->columns; x++)
270 if (GetPixelGray(floodplane_image,q) != 0)
272 GetPixelInfoPixel(image,p,&pixel);
273 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
275 SetPixelGray(floodplane_image,QuantumRange,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 (GetPixelGray(floodplane_image,q) != 0)
301 GetPixelInfoPixel(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,exception);
326 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
329 for (x=0; x < (ssize_t) image->columns; x++)
331 if (GetPixelGray(floodplane_image,p) != 0)
333 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
334 SetPixelInfoPixel(image,&fill_color,q);
336 p+=GetPixelChannels(floodplane_image);
337 q+=GetPixelChannels(image);
339 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
342 floodplane_view=DestroyCacheView(floodplane_view);
343 image_view=DestroyCacheView(image_view);
344 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
345 floodplane_image=DestroyImage(floodplane_image);
346 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 + G r a d i e n t I m a g e %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % GradientImage() applies a continuously smooth color transitions along a
361 % vector from one color to another.
363 % Note, the interface of this method will change in the future to support
364 % more than one transistion.
366 % The format of the GradientImage method is:
368 % MagickBooleanType GradientImage(Image *image,const GradientType type,
369 % const SpreadMethod method,const PixelInfo *start_color,
370 % const PixelInfo *stop_color,ExceptionInfo *exception)
372 % A description of each parameter follows:
374 % o image: the image.
376 % o type: the gradient type: linear or radial.
378 % o spread: the gradient spread meathod: pad, reflect, or repeat.
380 % o start_color: the start color.
382 % o stop_color: the stop color.
384 % o exception: return any errors or warnings in this structure.
388 static inline double MagickMax(const double x,const double y)
390 return(x > y ? x : y);
393 MagickExport MagickBooleanType GradientImage(Image *image,
394 const GradientType type,const SpreadMethod method,
395 const PixelInfo *start_color,const PixelInfo *stop_color,
396 ExceptionInfo *exception)
411 Set gradient start-stop end points.
413 assert(image != (const Image *) NULL);
414 assert(image->signature == MagickSignature);
415 if (image->debug != MagickFalse)
416 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
417 assert(start_color != (const PixelInfo *) NULL);
418 assert(stop_color != (const PixelInfo *) NULL);
419 draw_info=AcquireDrawInfo();
420 gradient=(&draw_info->gradient);
422 gradient->bounding_box.width=image->columns;
423 gradient->bounding_box.height=image->rows;
424 gradient->gradient_vector.x2=(double) image->columns-1.0;
425 gradient->gradient_vector.y2=(double) image->rows-1.0;
426 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
427 gradient->gradient_vector.x2=0.0;
428 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
429 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
430 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
431 gradient->spread=method;
433 Define the gradient to fill between the stops.
435 gradient->number_stops=2;
436 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
437 sizeof(*gradient->stops));
438 if (gradient->stops == (StopInfo *) NULL)
439 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
441 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
442 sizeof(*gradient->stops));
443 for (i=0; i < (ssize_t) gradient->number_stops; i++)
444 GetPixelInfo(image,&gradient->stops[i].color);
445 gradient->stops[0].color=(*start_color);
446 gradient->stops[0].offset=0.0;
447 gradient->stops[1].color=(*stop_color);
448 gradient->stops[1].offset=1.0;
450 Draw a gradient on the image.
452 status=DrawGradientImage(image,draw_info,exception);
453 draw_info=DestroyDrawInfo(draw_info);
454 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
455 image->matte=MagickFalse;
456 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
457 (IsPixelInfoGray(stop_color) != MagickFalse))
458 image->type=GrayscaleType;
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % O i l P a i n t I m a g e %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % OilPaintImage() applies a special effect filter that simulates an oil
474 % painting. Each pixel is replaced by the most frequent color occurring
475 % in a circular region defined by radius.
477 % The format of the OilPaintImage method is:
479 % Image *OilPaintImage(const Image *image,const double radius,
480 % const double sigma,ExceptionInfo *exception)
482 % A description of each parameter follows:
484 % o image: the image.
486 % o radius: the radius of the circular neighborhood.
488 % o sigma: the standard deviation of the Gaussian, in pixels.
490 % o exception: return any errors or warnings in this structure.
494 static size_t **DestroyHistogramThreadSet(size_t **histogram)
499 assert(histogram != (size_t **) NULL);
500 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
501 if (histogram[i] != (size_t *) NULL)
502 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
503 histogram=(size_t **) RelinquishMagickMemory(histogram);
507 static size_t **AcquireHistogramThreadSet(const size_t count)
516 number_threads=GetOpenMPMaximumThreads();
517 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
518 if (histogram == (size_t **) NULL)
519 return((size_t **) NULL);
520 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
521 for (i=0; i < (ssize_t) number_threads; i++)
523 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
524 if (histogram[i] == (size_t *) NULL)
525 return(DestroyHistogramThreadSet(histogram));
530 MagickExport Image *OilPaintImage(const Image *image,const double radius,
531 const double sigma,ExceptionInfo *exception)
533 #define NumberPaintBins 256
534 #define OilPaintImageTag "OilPaint/Image"
557 Initialize painted image attributes.
559 assert(image != (const Image *) NULL);
560 assert(image->signature == MagickSignature);
561 if (image->debug != MagickFalse)
562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
563 assert(exception != (ExceptionInfo *) NULL);
564 assert(exception->signature == MagickSignature);
565 width=GetOptimalKernelWidth2D(radius,sigma);
566 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
567 if (paint_image == (Image *) NULL)
568 return((Image *) NULL);
569 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
571 paint_image=DestroyImage(paint_image);
572 return((Image *) NULL);
574 histograms=AcquireHistogramThreadSet(NumberPaintBins);
575 if (histograms == (size_t **) NULL)
577 paint_image=DestroyImage(paint_image);
578 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
585 image_view=AcquireCacheView(image);
586 paint_view=AcquireCacheView(paint_image);
587 #if defined(MAGICKCORE_OPENMP_SUPPORT)
588 #pragma omp parallel for schedule(static,4) shared(progress,status)
590 for (y=0; y < (ssize_t) image->rows; y++)
592 register const Quantum
604 if (status == MagickFalse)
606 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
607 (width/2L),image->columns+width,width,exception);
608 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
610 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
615 histogram=histograms[GetOpenMPThreadId()];
616 for (x=0; x < (ssize_t) image->columns; x++)
631 Assign most frequent color.
636 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
637 for (v=0; v < (ssize_t) width; v++)
639 for (u=0; u < (ssize_t) width; u++)
641 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
642 GetPixelChannels(image)*(u+i)));
644 if (histogram[k] > count)
650 i+=(ssize_t) (image->columns+width);
652 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
653 SetPixelRed(paint_image,GetPixelRed(image,p+j*
654 GetPixelChannels(image)),q);
655 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
656 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
657 GetPixelChannels(image)),q);
658 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
659 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
660 GetPixelChannels(image)),q);
661 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
662 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
663 GetPixelChannels(image)),q);
664 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
665 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
666 GetPixelChannels(image)),q);
667 p+=GetPixelChannels(image);
668 q+=GetPixelChannels(paint_image);
670 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
672 if (image->progress_monitor != (MagickProgressMonitor) NULL)
677 #if defined(MAGICKCORE_OPENMP_SUPPORT)
678 #pragma omp critical (MagickCore_OilPaintImage)
680 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
681 if (proceed == MagickFalse)
685 paint_view=DestroyCacheView(paint_view);
686 image_view=DestroyCacheView(image_view);
687 histograms=DestroyHistogramThreadSet(histograms);
688 if (status == MagickFalse)
689 paint_image=DestroyImage(paint_image);
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 % O p a q u e P a i n t I m a g e %
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704 % OpaquePaintImage() changes any pixel that matches color with the color
707 % By default color must match a particular pixel color exactly. However, in
708 % many cases two colors may differ by a small amount. Fuzz defines how much
709 % tolerance is acceptable to consider two colors as the same. For example,
710 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
711 % are now interpreted as the same color.
713 % The format of the OpaquePaintImage method is:
715 % MagickBooleanType OpaquePaintImage(Image *image,
716 % const PixelInfo *target,const PixelInfo *fill,
717 % const MagickBooleanType invert,ExceptionInfo *exception)
719 % A description of each parameter follows:
721 % o image: the image.
723 % o target: the RGB value of the target color.
725 % o fill: the replacement color.
727 % o invert: paint any pixel that does not match the target color.
729 % o exception: return any errors or warnings in this structure.
732 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
733 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
734 ExceptionInfo *exception)
736 #define OpaquePaintImageTag "Opaque/Image"
753 assert(image != (Image *) NULL);
754 assert(image->signature == MagickSignature);
755 assert(target != (PixelInfo *) NULL);
756 assert(fill != (PixelInfo *) NULL);
757 if (image->debug != MagickFalse)
758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
759 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
762 Make image color opaque.
766 GetPixelInfo(image,&zero);
767 image_view=AcquireCacheView(image);
768 #if defined(MAGICKCORE_OPENMP_SUPPORT)
769 #pragma omp parallel for schedule(static,4) shared(progress,status)
771 for (y=0; y < (ssize_t) image->rows; y++)
782 if (status == MagickFalse)
784 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
785 if (q == (Quantum *) NULL)
791 for (x=0; x < (ssize_t) image->columns; x++)
793 GetPixelInfoPixel(image,q,&pixel);
794 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
795 SetPixelInfoPixel(image,fill,q);
796 q+=GetPixelChannels(image);
798 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
800 if (image->progress_monitor != (MagickProgressMonitor) NULL)
805 #if defined(MAGICKCORE_OPENMP_SUPPORT)
806 #pragma omp critical (MagickCore_OpaquePaintImage)
808 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
810 if (proceed == MagickFalse)
814 image_view=DestroyCacheView(image_view);
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % T r a n s p a r e n t P a i n t I m a g e %
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % TransparentPaintImage() changes the opacity value associated with any pixel
830 % that matches color to the value defined by opacity.
832 % By default color must match a particular pixel color exactly. However, in
833 % many cases two colors may differ by a small amount. Fuzz defines how much
834 % tolerance is acceptable to consider two colors as the same. For example,
835 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
836 % are now interpreted as the same color.
838 % The format of the TransparentPaintImage method is:
840 % MagickBooleanType TransparentPaintImage(Image *image,
841 % const PixelInfo *target,const Quantum opacity,
842 % const MagickBooleanType invert,ExceptionInfo *exception)
844 % A description of each parameter follows:
846 % o image: the image.
848 % o target: the target color.
850 % o opacity: the replacement opacity value.
852 % o invert: paint any pixel that does not match the target color.
854 % o exception: return any errors or warnings in this structure.
857 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
858 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
859 ExceptionInfo *exception)
861 #define TransparentPaintImageTag "Transparent/Image"
878 assert(image != (Image *) NULL);
879 assert(image->signature == MagickSignature);
880 assert(target != (PixelInfo *) NULL);
881 if (image->debug != MagickFalse)
882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
883 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
885 if (image->matte == MagickFalse)
886 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
888 Make image color transparent.
892 GetPixelInfo(image,&zero);
893 image_view=AcquireCacheView(image);
894 #if defined(MAGICKCORE_OPENMP_SUPPORT)
895 #pragma omp parallel for schedule(static,4) shared(progress,status)
897 for (y=0; y < (ssize_t) image->rows; y++)
908 if (status == MagickFalse)
910 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
911 if (q == (Quantum *) NULL)
917 for (x=0; x < (ssize_t) image->columns; x++)
919 GetPixelInfoPixel(image,q,&pixel);
920 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
921 SetPixelAlpha(image,opacity,q);
922 q+=GetPixelChannels(image);
924 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
926 if (image->progress_monitor != (MagickProgressMonitor) NULL)
931 #if defined(MAGICKCORE_OPENMP_SUPPORT)
932 #pragma omp critical (MagickCore_TransparentPaintImage)
934 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
936 if (proceed == MagickFalse)
940 image_view=DestroyCacheView(image_view);
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % 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 %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % TransparentPaintImageChroma() changes the opacity value associated with any
956 % pixel that matches color to the value defined by opacity.
958 % As there is one fuzz value for the all the channels, TransparentPaintImage()
959 % is not suitable for the operations like chroma, where the tolerance for
960 % similarity of two color component (RGB) can be different. Thus we define
961 % this method to take two target pixels (one low and one high) and all the
962 % pixels of an image which are lying between these two pixels are made
965 % The format of the TransparentPaintImageChroma method is:
967 % MagickBooleanType TransparentPaintImageChroma(Image *image,
968 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
969 % const MagickBooleanType invert,ExceptionInfo *exception)
971 % A description of each parameter follows:
973 % o image: the image.
975 % o low: the low target color.
977 % o high: the high target color.
979 % o opacity: the replacement opacity value.
981 % o invert: paint any pixel that does not match the target color.
983 % o exception: return any errors or warnings in this structure.
986 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
987 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
988 const MagickBooleanType invert,ExceptionInfo *exception)
990 #define TransparentPaintImageTag "Transparent/Image"
1004 assert(image != (Image *) NULL);
1005 assert(image->signature == MagickSignature);
1006 assert(high != (PixelInfo *) NULL);
1007 assert(low != (PixelInfo *) NULL);
1008 if (image->debug != MagickFalse)
1009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1010 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1011 return(MagickFalse);
1012 if (image->matte == MagickFalse)
1013 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1015 Make image color transparent.
1019 image_view=AcquireCacheView(image);
1020 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1021 #pragma omp parallel for schedule(static,4) shared(progress,status)
1023 for (y=0; y < (ssize_t) image->rows; y++)
1037 if (status == MagickFalse)
1039 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1040 if (q == (Quantum *) NULL)
1045 GetPixelInfo(image,&pixel);
1046 for (x=0; x < (ssize_t) image->columns; x++)
1048 GetPixelInfoPixel(image,q,&pixel);
1049 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1050 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1051 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1053 if (match != invert)
1054 SetPixelAlpha(image,opacity,q);
1055 q+=GetPixelChannels(image);
1057 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1059 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1065 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1067 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1069 if (proceed == MagickFalse)
1073 image_view=DestroyCacheView(image_view);