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-2014 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 131072UL
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 == MagickSignature);
169 if (image->debug != MagickFalse)
170 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
171 assert(draw_info != (DrawInfo *) NULL);
172 assert(draw_info->signature == MagickSignature);
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 != BlendPixelTrait) &&
182 (draw_info->fill.alpha_trait == BlendPixelTrait))
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);
322 for (y=0; y < (ssize_t) image->rows; y++)
324 register const Quantum
334 Tile fill color onto floodplane.
336 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
337 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
338 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
340 for (x=0; x < (ssize_t) image->columns; x++)
342 if (GetPixelGray(floodplane_image,p) != 0)
344 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
345 SetPixelInfoPixel(image,&fill_color,q);
347 p+=GetPixelChannels(floodplane_image);
348 q+=GetPixelChannels(image);
350 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
353 floodplane_view=DestroyCacheView(floodplane_view);
354 image_view=DestroyCacheView(image_view);
355 segment_info=RelinquishVirtualMemory(segment_info);
356 floodplane_image=DestroyImage(floodplane_image);
357 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 + G r a d i e n t I m a g e %
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 % GradientImage() applies a continuously smooth color transitions along a
372 % vector from one color to another.
374 % Note, the interface of this method will change in the future to support
375 % more than one transistion.
377 % The format of the GradientImage method is:
379 % MagickBooleanType GradientImage(Image *image,const GradientType type,
380 % const SpreadMethod method,const PixelInfo *start_color,
381 % const PixelInfo *stop_color,ExceptionInfo *exception)
383 % A description of each parameter follows:
385 % o image: the image.
387 % o type: the gradient type: linear or radial.
389 % o spread: the gradient spread meathod: pad, reflect, or repeat.
391 % o start_color: the start color.
393 % o stop_color: the stop color.
395 % o exception: return any errors or warnings in this structure.
399 static inline double MagickMax(const double x,const double y)
401 return(x > y ? x : y);
404 MagickExport MagickBooleanType GradientImage(Image *image,
405 const GradientType type,const SpreadMethod method,
406 const PixelInfo *start_color,const PixelInfo *stop_color,
407 ExceptionInfo *exception)
422 Set gradient start-stop end points.
424 assert(image != (const Image *) NULL);
425 assert(image->signature == MagickSignature);
426 if (image->debug != MagickFalse)
427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
428 assert(start_color != (const PixelInfo *) NULL);
429 assert(stop_color != (const PixelInfo *) NULL);
430 draw_info=AcquireDrawInfo();
431 gradient=(&draw_info->gradient);
433 gradient->bounding_box.width=image->columns;
434 gradient->bounding_box.height=image->rows;
435 gradient->gradient_vector.x2=(double) image->columns-1.0;
436 gradient->gradient_vector.y2=(double) image->rows-1.0;
437 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
438 gradient->gradient_vector.x2=0.0;
439 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
440 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
441 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
442 gradient->spread=method;
444 Define the gradient to fill between the stops.
446 gradient->number_stops=2;
447 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
448 sizeof(*gradient->stops));
449 if (gradient->stops == (StopInfo *) NULL)
450 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
452 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
453 sizeof(*gradient->stops));
454 for (i=0; i < (ssize_t) gradient->number_stops; i++)
455 GetPixelInfo(image,&gradient->stops[i].color);
456 gradient->stops[0].color=(*start_color);
457 gradient->stops[0].offset=0.0;
458 gradient->stops[1].color=(*stop_color);
459 gradient->stops[1].offset=1.0;
461 Draw a gradient on the image.
463 (void) SetImageColorspace(image,start_color->colorspace,exception);
464 status=DrawGradientImage(image,draw_info,exception);
465 draw_info=DestroyDrawInfo(draw_info);
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % O i l P a i n t I m a g e %
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % OilPaintImage() applies a special effect filter that simulates an oil
481 % painting. Each pixel is replaced by the most frequent color occurring
482 % in a circular region defined by radius.
484 % The format of the OilPaintImage method is:
486 % Image *OilPaintImage(const Image *image,const double radius,
487 % const double sigma,ExceptionInfo *exception)
489 % A description of each parameter follows:
491 % o image: the image.
493 % o radius: the radius of the circular neighborhood.
495 % o sigma: the standard deviation of the Gaussian, in pixels.
497 % o exception: return any errors or warnings in this structure.
501 static size_t **DestroyHistogramThreadSet(size_t **histogram)
506 assert(histogram != (size_t **) NULL);
507 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
508 if (histogram[i] != (size_t *) NULL)
509 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
510 histogram=(size_t **) RelinquishMagickMemory(histogram);
514 static size_t **AcquireHistogramThreadSet(const size_t count)
523 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
524 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
525 if (histogram == (size_t **) NULL)
526 return((size_t **) NULL);
527 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
528 for (i=0; i < (ssize_t) number_threads; i++)
530 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
531 if (histogram[i] == (size_t *) NULL)
532 return(DestroyHistogramThreadSet(histogram));
537 MagickExport Image *OilPaintImage(const Image *image,const double radius,
538 const double sigma,ExceptionInfo *exception)
540 #define NumberPaintBins 256
541 #define OilPaintImageTag "OilPaint/Image"
566 Initialize painted image attributes.
568 assert(image != (const Image *) NULL);
569 assert(image->signature == MagickSignature);
570 if (image->debug != MagickFalse)
571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
572 assert(exception != (ExceptionInfo *) NULL);
573 assert(exception->signature == MagickSignature);
574 width=GetOptimalKernelWidth2D(radius,sigma);
575 linear_image=CloneImage(image,0,0,MagickTrue,exception);
576 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
577 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
579 if (linear_image != (Image *) NULL)
580 linear_image=DestroyImage(linear_image);
581 if (paint_image != (Image *) NULL)
582 linear_image=DestroyImage(paint_image);
583 return((Image *) NULL);
585 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
587 linear_image=DestroyImage(linear_image);
588 paint_image=DestroyImage(paint_image);
589 return((Image *) NULL);
591 histograms=AcquireHistogramThreadSet(NumberPaintBins);
592 if (histograms == (size_t **) NULL)
594 linear_image=DestroyImage(linear_image);
595 paint_image=DestroyImage(paint_image);
596 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
603 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
604 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
605 image_view=AcquireVirtualCacheView(linear_image,exception);
606 paint_view=AcquireAuthenticCacheView(paint_image,exception);
607 #if defined(MAGICKCORE_OPENMP_SUPPORT)
608 #pragma omp parallel for schedule(static,4) shared(progress,status) \
609 magick_threads(linear_image,paint_image,linear_image->rows,1)
611 for (y=0; y < (ssize_t) linear_image->rows; y++)
613 register const Quantum
625 if (status == MagickFalse)
627 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
628 (width/2L),linear_image->columns+width,width,exception);
629 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
631 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
636 histogram=histograms[GetOpenMPThreadId()];
637 for (x=0; x < (ssize_t) linear_image->columns; x++)
653 Assign most frequent color.
658 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
659 for (v=0; v < (ssize_t) width; v++)
661 for (u=0; u < (ssize_t) width; u++)
663 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
664 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
666 if (histogram[n] > count)
672 k+=(ssize_t) (linear_image->columns+width);
674 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
676 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
677 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
678 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
679 if ((traits == UndefinedPixelTrait) ||
680 (paint_traits == UndefinedPixelTrait))
682 if (((paint_traits & CopyPixelTrait) != 0) ||
683 (GetPixelReadMask(linear_image,p) == 0))
685 SetPixelChannel(paint_image,channel,p[center+i],q);
688 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
691 p+=GetPixelChannels(linear_image);
692 q+=GetPixelChannels(paint_image);
694 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
696 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
701 #if defined(MAGICKCORE_OPENMP_SUPPORT)
702 #pragma omp critical (MagickCore_OilPaintImage)
704 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
706 if (proceed == MagickFalse)
710 paint_view=DestroyCacheView(paint_view);
711 image_view=DestroyCacheView(image_view);
712 histograms=DestroyHistogramThreadSet(histograms);
713 linear_image=DestroyImage(linear_image);
714 if (status == MagickFalse)
715 paint_image=DestroyImage(paint_image);
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 % O p a q u e P a i n t I m a g e %
728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % OpaquePaintImage() changes any pixel that matches color with the color
733 % By default color must match a particular pixel color exactly. However, in
734 % many cases two colors may differ by a small amount. Fuzz defines how much
735 % tolerance is acceptable to consider two colors as the same. For example,
736 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
737 % are now interpreted as the same color.
739 % The format of the OpaquePaintImage method is:
741 % MagickBooleanType OpaquePaintImage(Image *image,
742 % const PixelInfo *target,const PixelInfo *fill,
743 % const MagickBooleanType invert,ExceptionInfo *exception)
745 % A description of each parameter follows:
747 % o image: the image.
749 % o target: the RGB value of the target color.
751 % o fill: the replacement color.
753 % o invert: paint any pixel that does not match the target color.
755 % o exception: return any errors or warnings in this structure.
758 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
759 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
760 ExceptionInfo *exception)
762 #define OpaquePaintImageTag "Opaque/Image"
779 assert(image != (Image *) NULL);
780 assert(image->signature == MagickSignature);
781 assert(target != (PixelInfo *) NULL);
782 assert(fill != (PixelInfo *) NULL);
783 if (image->debug != MagickFalse)
784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
785 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
787 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
788 (IsPixelInfoGray(fill) == MagickFalse))
789 (void) SetImageColorspace(image,sRGBColorspace,exception);
790 if ((fill->alpha_trait == BlendPixelTrait) &&
791 (image->alpha_trait != BlendPixelTrait))
792 (void) SetImageAlpha(image,OpaqueAlpha,exception);
794 Make image color opaque.
798 GetPixelInfo(image,&zero);
799 image_view=AcquireAuthenticCacheView(image,exception);
800 #if defined(MAGICKCORE_OPENMP_SUPPORT)
801 #pragma omp parallel for schedule(static,4) shared(progress,status) \
802 magick_threads(image,image,image->rows,1)
804 for (y=0; y < (ssize_t) image->rows; y++)
815 if (status == MagickFalse)
817 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
818 if (q == (Quantum *) NULL)
824 for (x=0; x < (ssize_t) image->columns; x++)
826 GetPixelInfoPixel(image,q,&pixel);
827 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
828 SetPixelInfoPixel(image,fill,q);
829 q+=GetPixelChannels(image);
831 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
833 if (image->progress_monitor != (MagickProgressMonitor) NULL)
838 #if defined(MAGICKCORE_OPENMP_SUPPORT)
839 #pragma omp critical (MagickCore_OpaquePaintImage)
841 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
843 if (proceed == MagickFalse)
847 image_view=DestroyCacheView(image_view);
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 % T r a n s p a r e n t P a i n t I m a g e %
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % TransparentPaintImage() changes the opacity value associated with any pixel
863 % that matches color to the value defined by opacity.
865 % By default color must match a particular pixel color exactly. However, in
866 % many cases two colors may differ by a small amount. Fuzz defines how much
867 % tolerance is acceptable to consider two colors as the same. For example,
868 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
869 % are now interpreted as the same color.
871 % The format of the TransparentPaintImage method is:
873 % MagickBooleanType TransparentPaintImage(Image *image,
874 % const PixelInfo *target,const Quantum opacity,
875 % const MagickBooleanType invert,ExceptionInfo *exception)
877 % A description of each parameter follows:
879 % o image: the image.
881 % o target: the target color.
883 % o opacity: the replacement opacity value.
885 % o invert: paint any pixel that does not match the target color.
887 % o exception: return any errors or warnings in this structure.
890 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
891 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
892 ExceptionInfo *exception)
894 #define TransparentPaintImageTag "Transparent/Image"
911 assert(image != (Image *) NULL);
912 assert(image->signature == MagickSignature);
913 assert(target != (PixelInfo *) NULL);
914 if (image->debug != MagickFalse)
915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
916 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
918 if (image->alpha_trait != BlendPixelTrait)
919 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
921 Make image color transparent.
925 GetPixelInfo(image,&zero);
926 image_view=AcquireAuthenticCacheView(image,exception);
927 #if defined(MAGICKCORE_OPENMP_SUPPORT)
928 #pragma omp parallel for schedule(static,4) shared(progress,status) \
929 magick_threads(image,image,image->rows,1)
931 for (y=0; y < (ssize_t) image->rows; y++)
942 if (status == MagickFalse)
944 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
945 if (q == (Quantum *) NULL)
951 for (x=0; x < (ssize_t) image->columns; x++)
953 GetPixelInfoPixel(image,q,&pixel);
954 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
955 SetPixelAlpha(image,opacity,q);
956 q+=GetPixelChannels(image);
958 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
960 if (image->progress_monitor != (MagickProgressMonitor) NULL)
965 #if defined(MAGICKCORE_OPENMP_SUPPORT)
966 #pragma omp critical (MagickCore_TransparentPaintImage)
968 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
970 if (proceed == MagickFalse)
974 image_view=DestroyCacheView(image_view);
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 % 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 %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % TransparentPaintImageChroma() changes the opacity value associated with any
990 % pixel that matches color to the value defined by opacity.
992 % As there is one fuzz value for the all the channels, TransparentPaintImage()
993 % is not suitable for the operations like chroma, where the tolerance for
994 % similarity of two color component (RGB) can be different. Thus we define
995 % this method to take two target pixels (one low and one high) and all the
996 % pixels of an image which are lying between these two pixels are made
999 % The format of the TransparentPaintImageChroma method is:
1001 % MagickBooleanType TransparentPaintImageChroma(Image *image,
1002 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1003 % const MagickBooleanType invert,ExceptionInfo *exception)
1005 % A description of each parameter follows:
1007 % o image: the image.
1009 % o low: the low target color.
1011 % o high: the high target color.
1013 % o opacity: the replacement opacity value.
1015 % o invert: paint any pixel that does not match the target color.
1017 % o exception: return any errors or warnings in this structure.
1020 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1021 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1022 const MagickBooleanType invert,ExceptionInfo *exception)
1024 #define TransparentPaintImageTag "Transparent/Image"
1038 assert(image != (Image *) NULL);
1039 assert(image->signature == MagickSignature);
1040 assert(high != (PixelInfo *) NULL);
1041 assert(low != (PixelInfo *) NULL);
1042 if (image->debug != MagickFalse)
1043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1044 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1045 return(MagickFalse);
1046 if (image->alpha_trait != BlendPixelTrait)
1047 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1049 Make image color transparent.
1053 image_view=AcquireAuthenticCacheView(image,exception);
1054 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1055 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1056 magick_threads(image,image,image->rows,1)
1058 for (y=0; y < (ssize_t) image->rows; y++)
1072 if (status == MagickFalse)
1074 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1075 if (q == (Quantum *) NULL)
1080 GetPixelInfo(image,&pixel);
1081 for (x=0; x < (ssize_t) image->columns; x++)
1083 GetPixelInfoPixel(image,q,&pixel);
1084 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1085 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1086 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1088 if (match != invert)
1089 SetPixelAlpha(image,opacity,q);
1090 q+=GetPixelChannels(image);
1092 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1094 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1099 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1100 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1102 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1104 if (proceed == MagickFalse)
1108 image_view=DestroyCacheView(image_view);