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 ((target->matte != MagickFalse) && (image->matte == MagickFalse))
175 (void) SetImageAlpha(image,OpaqueAlpha,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->matte == MagickFalse) && (stop_color->matte == MagickFalse))
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"
558 Initialize painted image attributes.
560 assert(image != (const Image *) NULL);
561 assert(image->signature == MagickSignature);
562 if (image->debug != MagickFalse)
563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
564 assert(exception != (ExceptionInfo *) NULL);
565 assert(exception->signature == MagickSignature);
566 width=GetOptimalKernelWidth2D(radius,sigma);
567 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
568 if (paint_image == (Image *) NULL)
569 return((Image *) NULL);
570 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
572 paint_image=DestroyImage(paint_image);
573 return((Image *) NULL);
575 histograms=AcquireHistogramThreadSet(NumberPaintBins);
576 if (histograms == (size_t **) NULL)
578 paint_image=DestroyImage(paint_image);
579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
586 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
587 GetPixelChannels(image)*(width/2L);
588 image_view=AcquireCacheView(image);
589 paint_view=AcquireCacheView(paint_image);
590 #if defined(MAGICKCORE_OPENMP_SUPPORT)
591 #pragma omp parallel for schedule(static,4) shared(progress,status)
593 for (y=0; y < (ssize_t) image->rows; y++)
595 register const Quantum
607 if (status == MagickFalse)
609 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
610 (width/2L),image->columns+width,width,exception);
611 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
613 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
618 histogram=histograms[GetOpenMPThreadId()];
619 for (x=0; x < (ssize_t) image->columns; x++)
635 Assign most frequent color.
640 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
641 for (v=0; v < (ssize_t) width; v++)
643 for (u=0; u < (ssize_t) width; u++)
645 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
646 GetPixelChannels(image)*(u+k)));
648 if (histogram[n] > count)
654 k+=(ssize_t) (image->columns+width);
656 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
665 channel=GetPixelChannelMapChannel(image,i);
666 traits=GetPixelChannelMapTraits(image,channel);
667 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
668 if ((traits == UndefinedPixelTrait) ||
669 (paint_traits == UndefinedPixelTrait))
671 if (((paint_traits & CopyPixelTrait) != 0) ||
672 (GetPixelMask(image,p) != 0))
674 SetPixelChannel(paint_image,channel,p[center+i],q);
677 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
679 p+=GetPixelChannels(image);
680 q+=GetPixelChannels(paint_image);
682 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
684 if (image->progress_monitor != (MagickProgressMonitor) NULL)
689 #if defined(MAGICKCORE_OPENMP_SUPPORT)
690 #pragma omp critical (MagickCore_OilPaintImage)
692 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
693 if (proceed == MagickFalse)
697 paint_view=DestroyCacheView(paint_view);
698 image_view=DestroyCacheView(image_view);
699 histograms=DestroyHistogramThreadSet(histograms);
700 if (status == MagickFalse)
701 paint_image=DestroyImage(paint_image);
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 % O p a q u e P a i n t I m a g e %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % OpaquePaintImage() changes any pixel that matches color with the color
719 % By default color must match a particular pixel color exactly. However, in
720 % many cases two colors may differ by a small amount. Fuzz defines how much
721 % tolerance is acceptable to consider two colors as the same. For example,
722 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
723 % are now interpreted as the same color.
725 % The format of the OpaquePaintImage method is:
727 % MagickBooleanType OpaquePaintImage(Image *image,
728 % const PixelInfo *target,const PixelInfo *fill,
729 % const MagickBooleanType invert,ExceptionInfo *exception)
731 % A description of each parameter follows:
733 % o image: the image.
735 % o target: the RGB value of the target color.
737 % o fill: the replacement color.
739 % o invert: paint any pixel that does not match the target color.
741 % o exception: return any errors or warnings in this structure.
744 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
745 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
746 ExceptionInfo *exception)
748 #define OpaquePaintImageTag "Opaque/Image"
765 assert(image != (Image *) NULL);
766 assert(image->signature == MagickSignature);
767 assert(target != (PixelInfo *) NULL);
768 assert(fill != (PixelInfo *) NULL);
769 if (image->debug != MagickFalse)
770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
771 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
773 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
774 (void) SetImageAlpha(image,OpaqueAlpha,exception);
776 Make image color opaque.
780 GetPixelInfo(image,&zero);
781 image_view=AcquireCacheView(image);
782 #if defined(MAGICKCORE_OPENMP_SUPPORT)
783 #pragma omp parallel for schedule(static,4) shared(progress,status)
785 for (y=0; y < (ssize_t) image->rows; y++)
796 if (status == MagickFalse)
798 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
799 if (q == (Quantum *) NULL)
805 for (x=0; x < (ssize_t) image->columns; x++)
807 GetPixelInfoPixel(image,q,&pixel);
808 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
809 SetPixelInfoPixel(image,fill,q);
810 q+=GetPixelChannels(image);
812 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
814 if (image->progress_monitor != (MagickProgressMonitor) NULL)
819 #if defined(MAGICKCORE_OPENMP_SUPPORT)
820 #pragma omp critical (MagickCore_OpaquePaintImage)
822 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
824 if (proceed == MagickFalse)
828 image_view=DestroyCacheView(image_view);
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % T r a n s p a r e n t P a i n t I m a g e %
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843 % TransparentPaintImage() changes the opacity value associated with any pixel
844 % that matches color to the value defined by opacity.
846 % By default color must match a particular pixel color exactly. However, in
847 % many cases two colors may differ by a small amount. Fuzz defines how much
848 % tolerance is acceptable to consider two colors as the same. For example,
849 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
850 % are now interpreted as the same color.
852 % The format of the TransparentPaintImage method is:
854 % MagickBooleanType TransparentPaintImage(Image *image,
855 % const PixelInfo *target,const Quantum opacity,
856 % const MagickBooleanType invert,ExceptionInfo *exception)
858 % A description of each parameter follows:
860 % o image: the image.
862 % o target: the target color.
864 % o opacity: the replacement opacity value.
866 % o invert: paint any pixel that does not match the target color.
868 % o exception: return any errors or warnings in this structure.
871 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
872 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
873 ExceptionInfo *exception)
875 #define TransparentPaintImageTag "Transparent/Image"
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 assert(target != (PixelInfo *) NULL);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
899 if (image->matte == MagickFalse)
900 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
902 Make image color transparent.
906 GetPixelInfo(image,&zero);
907 image_view=AcquireCacheView(image);
908 #if defined(MAGICKCORE_OPENMP_SUPPORT)
909 #pragma omp parallel for schedule(static,4) shared(progress,status)
911 for (y=0; y < (ssize_t) image->rows; y++)
922 if (status == MagickFalse)
924 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
925 if (q == (Quantum *) NULL)
931 for (x=0; x < (ssize_t) image->columns; x++)
933 GetPixelInfoPixel(image,q,&pixel);
934 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
935 SetPixelAlpha(image,opacity,q);
936 q+=GetPixelChannels(image);
938 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
940 if (image->progress_monitor != (MagickProgressMonitor) NULL)
945 #if defined(MAGICKCORE_OPENMP_SUPPORT)
946 #pragma omp critical (MagickCore_TransparentPaintImage)
948 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
950 if (proceed == MagickFalse)
954 image_view=DestroyCacheView(image_view);
959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 % 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 %
967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % TransparentPaintImageChroma() changes the opacity value associated with any
970 % pixel that matches color to the value defined by opacity.
972 % As there is one fuzz value for the all the channels, TransparentPaintImage()
973 % is not suitable for the operations like chroma, where the tolerance for
974 % similarity of two color component (RGB) can be different. Thus we define
975 % this method to take two target pixels (one low and one high) and all the
976 % pixels of an image which are lying between these two pixels are made
979 % The format of the TransparentPaintImageChroma method is:
981 % MagickBooleanType TransparentPaintImageChroma(Image *image,
982 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
983 % const MagickBooleanType invert,ExceptionInfo *exception)
985 % A description of each parameter follows:
987 % o image: the image.
989 % o low: the low target color.
991 % o high: the high target color.
993 % o opacity: the replacement opacity value.
995 % o invert: paint any pixel that does not match the target color.
997 % o exception: return any errors or warnings in this structure.
1000 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1001 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1002 const MagickBooleanType invert,ExceptionInfo *exception)
1004 #define TransparentPaintImageTag "Transparent/Image"
1018 assert(image != (Image *) NULL);
1019 assert(image->signature == MagickSignature);
1020 assert(high != (PixelInfo *) NULL);
1021 assert(low != (PixelInfo *) NULL);
1022 if (image->debug != MagickFalse)
1023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1024 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1025 return(MagickFalse);
1026 if (image->matte == MagickFalse)
1027 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1029 Make image color transparent.
1033 image_view=AcquireCacheView(image);
1034 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1035 #pragma omp parallel for schedule(static,4) shared(progress,status)
1037 for (y=0; y < (ssize_t) image->rows; y++)
1051 if (status == MagickFalse)
1053 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1054 if (q == (Quantum *) NULL)
1059 GetPixelInfo(image,&pixel);
1060 for (x=0; x < (ssize_t) image->columns; x++)
1062 GetPixelInfoPixel(image,q,&pixel);
1063 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1064 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1065 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1067 if (match != invert)
1068 SetPixelAlpha(image,opacity,q);
1069 q+=GetPixelChannels(image);
1071 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1073 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1078 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1079 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1081 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1083 if (proceed == MagickFalse)
1087 image_view=DestroyCacheView(image_view);