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-2015 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/channel.h"
44 #include "MagickCore/color.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/colorspace-private.h"
47 #include "MagickCore/composite.h"
48 #include "MagickCore/composite-private.h"
49 #include "MagickCore/draw.h"
50 #include "MagickCore/draw-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/gem.h"
54 #include "MagickCore/gem-private.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/paint.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/resource_.h"
60 #include "MagickCore/statistic.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/thread-private.h"
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % F l o o d f i l l P a i n t I m a g e %
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % FloodfillPaintImage() changes the color value of any pixel that matches
76 % target and is an immediate neighbor. If the method FillToBorderMethod is
77 % specified, the color value is changed for any neighbor pixel that does not
78 % match the bordercolor member of image.
80 % By default target must match a particular pixel color exactly. However,
81 % in many cases two colors may differ by a small amount. The fuzz member of
82 % image defines how much tolerance is acceptable to consider two colors as
83 % the same. For example, set fuzz to 10 and the color red at intensities of
84 % 100 and 102 respectively are now interpreted as the same color for the
85 % purposes of the floodfill.
87 % The format of the FloodfillPaintImage method is:
89 % MagickBooleanType FloodfillPaintImage(Image *image,
90 % const DrawInfo *draw_info,const PixelInfo target,
91 % const ssize_t x_offset,const ssize_t y_offset,
92 % const MagickBooleanType invert,ExceptionInfo *exception)
94 % A description of each parameter follows:
98 % o draw_info: the draw info.
100 % o target: the RGB value of the target color.
102 % o x_offset,y_offset: the starting location of the operation.
104 % o invert: paint any pixel that does not match the target color.
106 % o exception: return any errors or warnings in this structure.
109 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
110 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
111 const ssize_t y_offset,const MagickBooleanType invert,
112 ExceptionInfo *exception)
114 #define MaxStacksize 262144UL
115 #define PushSegmentStack(up,left,right,delta) \
117 if (s >= (segment_stack+MaxStacksize)) \
118 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
121 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
123 s->x1=(double) (left); \
124 s->y1=(double) (up); \
125 s->x2=(double) (right); \
126 s->y2=(double) (delta); \
165 Check boundary conditions.
167 assert(image != (Image *) NULL);
168 assert(image->signature == MagickCoreSignature);
169 if (image->debug != MagickFalse)
170 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
171 assert(draw_info != (DrawInfo *) NULL);
172 assert(draw_info->signature == MagickCoreSignature);
173 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
175 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
177 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
179 if (IsGrayColorspace(image->colorspace) != MagickFalse)
180 (void) SetImageColorspace(image,sRGBColorspace,exception);
181 if ((image->alpha_trait == UndefinedPixelTrait) &&
182 (draw_info->fill.alpha_trait != UndefinedPixelTrait))
183 (void) SetImageAlpha(image,OpaqueAlpha,exception);
187 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
189 if (floodplane_image == (Image *) NULL)
191 floodplane_image->alpha_trait=UndefinedPixelTrait;
192 floodplane_image->colorspace=GRAYColorspace;
193 (void) QueryColorCompliance("#000",AllCompliance,
194 &floodplane_image->background_color,exception);
195 (void) SetImageBackgroundColor(floodplane_image,exception);
196 segment_info=AcquireVirtualMemory(MaxStacksize,sizeof(*segment_stack));
197 if (segment_info == (MemoryInfo *) NULL)
199 floodplane_image=DestroyImage(floodplane_image);
200 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
203 segment_stack=(SegmentInfo *) GetVirtualMemoryBlob(segment_info);
205 Push initial segment on stack.
212 PushSegmentStack(y,x,x,1);
213 PushSegmentStack(y+1,x,x,-1);
214 GetPixelInfo(image,&pixel);
215 image_view=AcquireVirtualCacheView(image,exception);
216 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
217 while (s > segment_stack)
219 register const Quantum
229 Pop segment off stack.
234 offset=(ssize_t) s->y2;
235 y=(ssize_t) s->y1+offset;
237 Recolor neighboring pixels.
239 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
240 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
242 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
244 p+=x1*GetPixelChannels(image);
245 q+=x1*GetPixelChannels(floodplane_image);
246 for (x=x1; x >= 0; x--)
248 if (GetPixelGray(floodplane_image,q) != 0)
250 GetPixelInfoPixel(image,p,&pixel);
251 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
253 SetPixelGray(floodplane_image,QuantumRange,q);
254 p-=GetPixelChannels(image);
255 q-=GetPixelChannels(floodplane_image);
257 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
259 skip=x >= x1 ? MagickTrue : MagickFalse;
260 if (skip == MagickFalse)
264 PushSegmentStack(y,start,x1-1,-offset);
269 if (skip == MagickFalse)
271 if (x < (ssize_t) image->columns)
273 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
275 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
277 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
279 for ( ; x < (ssize_t) image->columns; x++)
281 if (GetPixelGray(floodplane_image,q) != 0)
283 GetPixelInfoPixel(image,p,&pixel);
284 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
286 SetPixelGray(floodplane_image,QuantumRange,q);
287 p+=GetPixelChannels(image);
288 q+=GetPixelChannels(floodplane_image);
290 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
291 if (status == MagickFalse)
294 PushSegmentStack(y,start,x-1,offset);
296 PushSegmentStack(y,x2+1,x-1,-offset);
302 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
304 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
306 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
308 for ( ; x <= x2; x++)
310 if (GetPixelGray(floodplane_image,q) != 0)
312 GetPixelInfoPixel(image,p,&pixel);
313 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
315 p+=GetPixelChannels(image);
316 q+=GetPixelChannels(floodplane_image);
323 #if defined(MAGICKCORE_OPENMP_SUPPORT)
324 #pragma omp parallel for schedule(static,4) shared(status) \
325 magick_threads(floodplane_image,image,floodplane_image->rows,1)
327 for (y=0; y < (ssize_t) image->rows; y++)
329 register const Quantum
339 Tile fill color onto floodplane.
341 if (status == MagickFalse)
343 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
344 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
350 for (x=0; x < (ssize_t) image->columns; x++)
352 if (GetPixelGray(floodplane_image,p) != 0)
354 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
355 SetPixelViaPixelInfo(image,&fill_color,q);
357 p+=GetPixelChannels(floodplane_image);
358 q+=GetPixelChannels(image);
360 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
363 floodplane_view=DestroyCacheView(floodplane_view);
364 image_view=DestroyCacheView(image_view);
365 segment_info=RelinquishVirtualMemory(segment_info);
366 floodplane_image=DestroyImage(floodplane_image);
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 + G r a d i e n t I m a g e %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % GradientImage() applies a continuously smooth color transitions along a
382 % vector from one color to another.
384 % Note, the interface of this method will change in the future to support
385 % more than one transistion.
387 % The format of the GradientImage method is:
389 % MagickBooleanType GradientImage(Image *image,const GradientType type,
390 % const SpreadMethod method,const PixelInfo *start_color,
391 % const PixelInfo *stop_color,ExceptionInfo *exception)
393 % A description of each parameter follows:
395 % o image: the image.
397 % o type: the gradient type: linear or radial.
399 % o spread: the gradient spread meathod: pad, reflect, or repeat.
401 % o start_color: the start color.
403 % o stop_color: the stop color.
405 % o exception: return any errors or warnings in this structure.
409 MagickExport MagickBooleanType GradientImage(Image *image,
410 const GradientType type,const SpreadMethod method,
411 const StopInfo *stops,const size_t number_stops,
412 ExceptionInfo *exception)
424 Set gradient start-stop end points.
426 assert(image != (const Image *) NULL);
427 assert(image->signature == MagickCoreSignature);
428 if (image->debug != MagickFalse)
429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
430 assert(stops != (const StopInfo *) NULL);
431 assert(number_stops > 0);
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=number_stops;
449 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
450 sizeof(*gradient->stops));
451 if (gradient->stops == (StopInfo *) NULL)
452 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
454 (void) CopyMagickMemory(gradient->stops,stops,(size_t) number_stops*
457 Draw a gradient on the image.
459 status=DrawGradientImage(image,draw_info,exception);
460 draw_info=DestroyDrawInfo(draw_info);
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % O i l P a i n t I m a g e %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % OilPaintImage() applies a special effect filter that simulates an oil
476 % painting. Each pixel is replaced by the most frequent color occurring
477 % in a circular region defined by radius.
479 % The format of the OilPaintImage method is:
481 % Image *OilPaintImage(const Image *image,const double radius,
482 % const double sigma,ExceptionInfo *exception)
484 % A description of each parameter follows:
486 % o image: the image.
488 % o radius: the radius of the circular neighborhood.
490 % o sigma: the standard deviation of the Gaussian, in pixels.
492 % o exception: return any errors or warnings in this structure.
496 static size_t **DestroyHistogramThreadSet(size_t **histogram)
501 assert(histogram != (size_t **) NULL);
502 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
503 if (histogram[i] != (size_t *) NULL)
504 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
505 histogram=(size_t **) RelinquishMagickMemory(histogram);
509 static size_t **AcquireHistogramThreadSet(const size_t count)
518 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
519 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
520 if (histogram == (size_t **) NULL)
521 return((size_t **) NULL);
522 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
523 for (i=0; i < (ssize_t) number_threads; i++)
525 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
526 if (histogram[i] == (size_t *) NULL)
527 return(DestroyHistogramThreadSet(histogram));
532 MagickExport Image *OilPaintImage(const Image *image,const double radius,
533 const double sigma,ExceptionInfo *exception)
535 #define NumberPaintBins 256
536 #define OilPaintImageTag "OilPaint/Image"
561 Initialize painted image attributes.
563 assert(image != (const Image *) NULL);
564 assert(image->signature == MagickCoreSignature);
565 if (image->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567 assert(exception != (ExceptionInfo *) NULL);
568 assert(exception->signature == MagickCoreSignature);
569 width=GetOptimalKernelWidth2D(radius,sigma);
570 linear_image=CloneImage(image,0,0,MagickTrue,exception);
571 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
572 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
574 if (linear_image != (Image *) NULL)
575 linear_image=DestroyImage(linear_image);
576 if (paint_image != (Image *) NULL)
577 linear_image=DestroyImage(paint_image);
578 return((Image *) NULL);
580 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
582 linear_image=DestroyImage(linear_image);
583 paint_image=DestroyImage(paint_image);
584 return((Image *) NULL);
586 histograms=AcquireHistogramThreadSet(NumberPaintBins);
587 if (histograms == (size_t **) NULL)
589 linear_image=DestroyImage(linear_image);
590 paint_image=DestroyImage(paint_image);
591 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
598 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
599 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
600 image_view=AcquireVirtualCacheView(linear_image,exception);
601 paint_view=AcquireAuthenticCacheView(paint_image,exception);
602 #if defined(MAGICKCORE_OPENMP_SUPPORT)
603 #pragma omp parallel for schedule(static,4) shared(progress,status) \
604 magick_threads(linear_image,paint_image,linear_image->rows,1)
606 for (y=0; y < (ssize_t) linear_image->rows; y++)
608 register const Quantum
620 if (status == MagickFalse)
622 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
623 (width/2L),linear_image->columns+width,width,exception);
624 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
626 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
631 histogram=histograms[GetOpenMPThreadId()];
632 for (x=0; x < (ssize_t) linear_image->columns; x++)
648 Assign most frequent color.
653 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
654 for (v=0; v < (ssize_t) width; v++)
656 for (u=0; u < (ssize_t) width; u++)
658 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
659 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
661 if (histogram[n] > count)
667 k+=(ssize_t) (linear_image->columns+width);
669 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
671 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
672 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
673 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
674 if ((traits == UndefinedPixelTrait) ||
675 (paint_traits == UndefinedPixelTrait))
677 if (((paint_traits & CopyPixelTrait) != 0) ||
678 (GetPixelReadMask(linear_image,p) == 0))
680 SetPixelChannel(paint_image,channel,p[center+i],q);
683 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
686 p+=GetPixelChannels(linear_image);
687 q+=GetPixelChannels(paint_image);
689 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
691 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
696 #if defined(MAGICKCORE_OPENMP_SUPPORT)
697 #pragma omp critical (MagickCore_OilPaintImage)
699 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
701 if (proceed == MagickFalse)
705 paint_view=DestroyCacheView(paint_view);
706 image_view=DestroyCacheView(image_view);
707 histograms=DestroyHistogramThreadSet(histograms);
708 linear_image=DestroyImage(linear_image);
709 if (status == MagickFalse)
710 paint_image=DestroyImage(paint_image);
715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % O p a q u e P a i n t I m a g e %
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 % OpaquePaintImage() changes any pixel that matches color with the color
726 % defined by fill argument.
728 % By default color must match a particular pixel color exactly. However, in
729 % many cases two colors may differ by a small amount. Fuzz defines how much
730 % tolerance is acceptable to consider two colors as the same. For example,
731 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
732 % are now interpreted as the same color.
734 % The format of the OpaquePaintImage method is:
736 % MagickBooleanType OpaquePaintImage(Image *image,const PixelInfo *target,
737 % const PixelInfo *fill,const MagickBooleanType invert,
738 % ExceptionInfo *exception)
740 % A description of each parameter follows:
742 % o image: the image.
744 % o target: the RGB value of the target color.
746 % o fill: the replacement color.
748 % o invert: paint any pixel that does not match the target color.
750 % o exception: return any errors or warnings in this structure.
753 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
754 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
755 ExceptionInfo *exception)
757 #define OpaquePaintImageTag "Opaque/Image"
776 assert(image != (Image *) NULL);
777 assert(image->signature == MagickCoreSignature);
778 assert(target != (PixelInfo *) NULL);
779 assert(fill != (PixelInfo *) NULL);
780 if (image->debug != MagickFalse)
781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
782 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
784 ConformPixelInfo(image,fill,&conform_fill,exception);
785 ConformPixelInfo(image,target,&conform_target,exception);
787 Make image color opaque.
791 GetPixelInfo(image,&zero);
792 image_view=AcquireAuthenticCacheView(image,exception);
793 #if defined(MAGICKCORE_OPENMP_SUPPORT)
794 #pragma omp parallel for schedule(static,4) shared(progress,status) \
795 magick_threads(image,image,image->rows,1)
797 for (y=0; y < (ssize_t) image->rows; y++)
808 if (status == MagickFalse)
810 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
811 if (q == (Quantum *) NULL)
817 for (x=0; x < (ssize_t) image->columns; x++)
819 GetPixelInfoPixel(image,q,&pixel);
820 if (IsFuzzyEquivalencePixelInfo(&pixel,&conform_target) != invert)
821 SetPixelViaPixelInfo(image,&conform_fill,q);
822 q+=GetPixelChannels(image);
824 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
826 if (image->progress_monitor != (MagickProgressMonitor) NULL)
831 #if defined(MAGICKCORE_OPENMP_SUPPORT)
832 #pragma omp critical (MagickCore_OpaquePaintImage)
834 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
836 if (proceed == MagickFalse)
840 image_view=DestroyCacheView(image_view);
845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849 % T r a n s p a r e n t P a i n t I m a g e %
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % TransparentPaintImage() changes the opacity value associated with any pixel
856 % that matches color to the value defined by opacity.
858 % By default color must match a particular pixel color exactly. However, in
859 % many cases two colors may differ by a small amount. Fuzz defines how much
860 % tolerance is acceptable to consider two colors as the same. For example,
861 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
862 % are now interpreted as the same color.
864 % The format of the TransparentPaintImage method is:
866 % MagickBooleanType TransparentPaintImage(Image *image,
867 % const PixelInfo *target,const Quantum opacity,
868 % const MagickBooleanType invert,ExceptionInfo *exception)
870 % A description of each parameter follows:
872 % o image: the image.
874 % o target: the target color.
876 % o opacity: the replacement opacity value.
878 % o invert: paint any pixel that does not match the target color.
880 % o exception: return any errors or warnings in this structure.
883 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
884 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
885 ExceptionInfo *exception)
887 #define TransparentPaintImageTag "Transparent/Image"
904 assert(image != (Image *) NULL);
905 assert(image->signature == MagickCoreSignature);
906 assert(target != (PixelInfo *) NULL);
907 if (image->debug != MagickFalse)
908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
909 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
911 if (image->alpha_trait == UndefinedPixelTrait)
912 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
914 Make image color transparent.
918 GetPixelInfo(image,&zero);
919 image_view=AcquireAuthenticCacheView(image,exception);
920 #if defined(MAGICKCORE_OPENMP_SUPPORT)
921 #pragma omp parallel for schedule(static,4) shared(progress,status) \
922 magick_threads(image,image,image->rows,1)
924 for (y=0; y < (ssize_t) image->rows; y++)
935 if (status == MagickFalse)
937 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
938 if (q == (Quantum *) NULL)
944 for (x=0; x < (ssize_t) image->columns; x++)
946 GetPixelInfoPixel(image,q,&pixel);
947 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
948 SetPixelAlpha(image,opacity,q);
949 q+=GetPixelChannels(image);
951 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
958 #if defined(MAGICKCORE_OPENMP_SUPPORT)
959 #pragma omp critical (MagickCore_TransparentPaintImage)
961 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
963 if (proceed == MagickFalse)
967 image_view=DestroyCacheView(image_view);
972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % 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 %
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % TransparentPaintImageChroma() changes the opacity value associated with any
983 % pixel that matches color to the value defined by opacity.
985 % As there is one fuzz value for the all the channels, TransparentPaintImage()
986 % is not suitable for the operations like chroma, where the tolerance for
987 % similarity of two color component (RGB) can be different. Thus we define
988 % this method to take two target pixels (one low and one high) and all the
989 % pixels of an image which are lying between these two pixels are made
992 % The format of the TransparentPaintImageChroma method is:
994 % MagickBooleanType TransparentPaintImageChroma(Image *image,
995 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
996 % const MagickBooleanType invert,ExceptionInfo *exception)
998 % A description of each parameter follows:
1000 % o image: the image.
1002 % o low: the low target color.
1004 % o high: the high target color.
1006 % o opacity: the replacement opacity value.
1008 % o invert: paint any pixel that does not match the target color.
1010 % o exception: return any errors or warnings in this structure.
1013 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1014 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1015 const MagickBooleanType invert,ExceptionInfo *exception)
1017 #define TransparentPaintImageTag "Transparent/Image"
1031 assert(image != (Image *) NULL);
1032 assert(image->signature == MagickCoreSignature);
1033 assert(high != (PixelInfo *) NULL);
1034 assert(low != (PixelInfo *) NULL);
1035 if (image->debug != MagickFalse)
1036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1037 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1038 return(MagickFalse);
1039 if (image->alpha_trait == UndefinedPixelTrait)
1040 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1042 Make image color transparent.
1046 image_view=AcquireAuthenticCacheView(image,exception);
1047 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1048 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1049 magick_threads(image,image,image->rows,1)
1051 for (y=0; y < (ssize_t) image->rows; y++)
1065 if (status == MagickFalse)
1067 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1068 if (q == (Quantum *) NULL)
1073 GetPixelInfo(image,&pixel);
1074 for (x=0; x < (ssize_t) image->columns; x++)
1076 GetPixelInfoPixel(image,q,&pixel);
1077 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1078 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1079 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1081 if (match != invert)
1082 SetPixelAlpha(image,opacity,q);
1083 q+=GetPixelChannels(image);
1085 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1087 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1092 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1093 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1095 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1097 if (proceed == MagickFalse)
1101 image_view=DestroyCacheView(image_view);