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-2016 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/artifact.h"
44 #include "MagickCore/channel.h"
45 #include "MagickCore/color.h"
46 #include "MagickCore/color-private.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/composite.h"
49 #include "MagickCore/composite-private.h"
50 #include "MagickCore/draw.h"
51 #include "MagickCore/draw-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/gem.h"
55 #include "MagickCore/gem-private.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/option.h"
59 #include "MagickCore/paint.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/resource_.h"
62 #include "MagickCore/statistic.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
65 #include "MagickCore/thread-private.h"
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % F l o o d f i l l P a i n t I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % FloodfillPaintImage() changes the color value of any pixel that matches
79 % target and is an immediate neighbor. If the method FillToBorderMethod is
80 % specified, the color value is changed for any neighbor pixel that does not
81 % match the bordercolor member of image.
83 % By default target must match a particular pixel color exactly. However,
84 % in many cases two colors may differ by a small amount. The fuzz member of
85 % image defines how much tolerance is acceptable to consider two colors as
86 % the same. For example, set fuzz to 10 and the color red at intensities of
87 % 100 and 102 respectively are now interpreted as the same color for the
88 % purposes of the floodfill.
90 % The format of the FloodfillPaintImage method is:
92 % MagickBooleanType FloodfillPaintImage(Image *image,
93 % const DrawInfo *draw_info,const PixelInfo target,
94 % const ssize_t x_offset,const ssize_t y_offset,
95 % const MagickBooleanType invert,ExceptionInfo *exception)
97 % A description of each parameter follows:
101 % o draw_info: the draw info.
103 % o target: the RGB value of the target color.
105 % o x_offset,y_offset: the starting location of the operation.
107 % o invert: paint any pixel that does not match the target color.
109 % o exception: return any errors or warnings in this structure.
112 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
113 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
114 const ssize_t y_offset,const MagickBooleanType invert,
115 ExceptionInfo *exception)
117 #define MaxStacksize 524288UL
118 #define PushSegmentStack(up,left,right,delta) \
120 if (s >= (segment_stack+MaxStacksize)) \
121 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
124 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
126 s->x1=(double) (left); \
127 s->y1=(double) (up); \
128 s->x2=(double) (right); \
129 s->y2=(double) (delta); \
167 Check boundary conditions.
169 assert(image != (Image *) NULL);
170 assert(image->signature == MagickCoreSignature);
171 if (image->debug != MagickFalse)
172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
173 assert(draw_info != (DrawInfo *) NULL);
174 assert(draw_info->signature == MagickCoreSignature);
175 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
177 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
179 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
181 if (IsGrayColorspace(image->colorspace) != MagickFalse)
182 (void) SetImageColorspace(image,sRGBColorspace,exception);
183 if ((image->alpha_trait == UndefinedPixelTrait) &&
184 (draw_info->fill.alpha_trait != UndefinedPixelTrait))
185 (void) SetImageAlpha(image,OpaqueAlpha,exception);
189 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
191 if (floodplane_image == (Image *) NULL)
193 floodplane_image->alpha_trait=UndefinedPixelTrait;
194 floodplane_image->colorspace=GRAYColorspace;
195 (void) QueryColorCompliance("#000",AllCompliance,
196 &floodplane_image->background_color,exception);
197 (void) SetImageBackgroundColor(floodplane_image,exception);
198 segment_info=AcquireVirtualMemory(MaxStacksize,sizeof(*segment_stack));
199 if (segment_info == (MemoryInfo *) NULL)
201 floodplane_image=DestroyImage(floodplane_image);
202 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
205 segment_stack=(SegmentInfo *) GetVirtualMemoryBlob(segment_info);
207 Push initial segment on stack.
212 PushSegmentStack(y_offset,x_offset,x_offset,1);
213 PushSegmentStack(y_offset+1,x_offset,x_offset,-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 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.
408 MagickExport MagickBooleanType GradientImage(Image *image,
409 const GradientType type,const SpreadMethod method,const StopInfo *stops,
410 const size_t number_stops,ExceptionInfo *exception)
425 Set gradient start-stop end points.
427 assert(image != (const Image *) NULL);
428 assert(image->signature == MagickCoreSignature);
429 if (image->debug != MagickFalse)
430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
431 assert(stops != (const StopInfo *) NULL);
432 assert(number_stops > 0);
433 draw_info=AcquireDrawInfo();
434 gradient=(&draw_info->gradient);
436 gradient->bounding_box.width=image->columns;
437 gradient->bounding_box.height=image->rows;
438 artifact=GetImageArtifact(image,"gradient:bounding-box");
439 if (artifact != (const char *) NULL)
440 (void) ParseAbsoluteGeometry(artifact,&gradient->bounding_box);
441 gradient->gradient_vector.x2=(double) image->columns;
442 gradient->gradient_vector.y2=(double) image->rows;
443 artifact=GetImageArtifact(image,"gradient:direction");
444 if (artifact != (const char *) NULL)
449 direction=(GravityType) ParseCommandOption(MagickGravityOptions,
450 MagickFalse,artifact);
453 case NorthWestGravity:
455 gradient->gradient_vector.x1=(double) image->columns;
456 gradient->gradient_vector.y1=(double) image->rows;
457 gradient->gradient_vector.x2=0.0;
458 gradient->gradient_vector.y2=0.0;
463 gradient->gradient_vector.x1=0.0;
464 gradient->gradient_vector.y1=(double) image->rows;
465 gradient->gradient_vector.x2=0.0;
466 gradient->gradient_vector.y2=0.0;
469 case NorthEastGravity:
471 gradient->gradient_vector.x1=0.0;
472 gradient->gradient_vector.y1=(double) image->rows;
473 gradient->gradient_vector.x2=(double) image->columns;
474 gradient->gradient_vector.y2=0.0;
479 gradient->gradient_vector.x1=(double) image->columns;
480 gradient->gradient_vector.y1=0.0;
481 gradient->gradient_vector.x2=0.0;
482 gradient->gradient_vector.y2=0.0;
487 gradient->gradient_vector.x1=0.0;
488 gradient->gradient_vector.y1=0.0;
489 gradient->gradient_vector.x2=(double) image->columns;
490 gradient->gradient_vector.y2=0.0;
493 case SouthWestGravity:
495 gradient->gradient_vector.x1=(double) image->columns;
496 gradient->gradient_vector.y1=0.0;
497 gradient->gradient_vector.x2=0.0;
498 gradient->gradient_vector.y2=(double) image->rows;
503 gradient->gradient_vector.x1=0.0;
504 gradient->gradient_vector.y1=0.0;
505 gradient->gradient_vector.x2=0.0;
506 gradient->gradient_vector.y2=(double) image->columns;
509 case SouthEastGravity:
511 gradient->gradient_vector.x1=0.0;
512 gradient->gradient_vector.y1=0.0;
513 gradient->gradient_vector.x2=(double) image->columns;
514 gradient->gradient_vector.y2=(double) image->rows;
521 artifact=GetImageArtifact(image,"gradient:angle");
522 if (artifact != (const char *) NULL)
523 gradient->angle=StringToDouble(artifact,(char **) NULL);
524 artifact=GetImageArtifact(image,"gradient:vector");
525 if (artifact != (const char *) NULL)
526 (void) sscanf(artifact,"%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf",
527 &gradient->gradient_vector.x1,&gradient->gradient_vector.y1,
528 &gradient->gradient_vector.x2,&gradient->gradient_vector.y2);
529 if ((GetImageArtifact(image,"gradient:angle") == (const char *) NULL) &&
530 (GetImageArtifact(image,"gradient:direction") == (const char *) NULL) &&
531 (GetImageArtifact(image,"gradient:extent") == (const char *) NULL) &&
532 (GetImageArtifact(image,"gradient:vector") == (const char *) NULL))
533 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
534 gradient->gradient_vector.x2=0.0;
535 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
536 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
537 artifact=GetImageArtifact(image,"gradient:center");
538 if (artifact != (const char *) NULL)
539 (void) sscanf(artifact,"%lf%*[ ,]%lf",&gradient->center.x,
540 &gradient->center.y);
541 artifact=GetImageArtifact(image,"gradient:angle");
542 if ((type == LinearGradient) && (artifact != (const char *) NULL))
550 Reference https://drafts.csswg.org/css-images-3/#linear-gradients.
552 sine=sin((double) DegreesToRadians(gradient->angle-90.0));
553 cosine=cos((double) DegreesToRadians(gradient->angle-90.0));
554 distance=fabs((double) image->columns*cosine)+
555 fabs((double) image->rows*sine);
556 gradient->gradient_vector.x1=0.5*(image->columns-distance*cosine);
557 gradient->gradient_vector.y1=0.5*(image->rows-distance*sine);
558 gradient->gradient_vector.x2=0.5*(image->columns+distance*cosine);
559 gradient->gradient_vector.y2=0.5*(image->rows+distance*sine);
561 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
562 gradient->radii.y=gradient->radii.x;
563 artifact=GetImageArtifact(image,"gradient:extent");
564 if (artifact != (const char *) NULL)
566 if (LocaleCompare(artifact,"Circle") == 0)
568 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
569 gradient->radii.y=gradient->radii.x;
571 if (LocaleCompare(artifact,"Diagonal") == 0)
573 gradient->radii.x=(double) (sqrt(image->columns*image->columns+
574 image->rows*image->rows))/2.0;
575 gradient->radii.y=gradient->radii.x;
577 if (LocaleCompare(artifact,"Ellipse") == 0)
579 gradient->radii.x=(double) image->columns/2.0;
580 gradient->radii.y=(double) image->rows/2.0;
582 if (LocaleCompare(artifact,"Maximum") == 0)
584 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
585 gradient->radii.y=gradient->radii.x;
587 if (LocaleCompare(artifact,"Minimum") == 0)
589 gradient->radii.x=(double) (MagickMin(image->columns,image->rows))/
591 gradient->radii.y=gradient->radii.x;
594 artifact=GetImageArtifact(image,"gradient:radii");
595 if (artifact != (const char *) NULL)
596 (void) sscanf(artifact,"%lf%*[ ,]%lf",&gradient->radii.x,
598 gradient->radius=MagickMax(gradient->radii.x,gradient->radii.y);
599 gradient->spread=method;
601 Define the gradient to fill between the stops.
603 gradient->number_stops=number_stops;
604 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
605 sizeof(*gradient->stops));
606 if (gradient->stops == (StopInfo *) NULL)
607 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
609 (void) CopyMagickMemory(gradient->stops,stops,(size_t) number_stops*
612 Draw a gradient on the image.
614 status=DrawGradientImage(image,draw_info,exception);
615 draw_info=DestroyDrawInfo(draw_info);
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % O i l P a i n t I m a g e %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % OilPaintImage() applies a special effect filter that simulates an oil
631 % painting. Each pixel is replaced by the most frequent color occurring
632 % in a circular region defined by radius.
634 % The format of the OilPaintImage method is:
636 % Image *OilPaintImage(const Image *image,const double radius,
637 % const double sigma,ExceptionInfo *exception)
639 % A description of each parameter follows:
641 % o image: the image.
643 % o radius: the radius of the circular neighborhood.
645 % o sigma: the standard deviation of the Gaussian, in pixels.
647 % o exception: return any errors or warnings in this structure.
651 static size_t **DestroyHistogramThreadSet(size_t **histogram)
656 assert(histogram != (size_t **) NULL);
657 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
658 if (histogram[i] != (size_t *) NULL)
659 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
660 histogram=(size_t **) RelinquishMagickMemory(histogram);
664 static size_t **AcquireHistogramThreadSet(const size_t count)
673 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
674 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
675 if (histogram == (size_t **) NULL)
676 return((size_t **) NULL);
677 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
678 for (i=0; i < (ssize_t) number_threads; i++)
680 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
681 if (histogram[i] == (size_t *) NULL)
682 return(DestroyHistogramThreadSet(histogram));
687 MagickExport Image *OilPaintImage(const Image *image,const double radius,
688 const double sigma,ExceptionInfo *exception)
690 #define NumberPaintBins 256
691 #define OilPaintImageTag "OilPaint/Image"
716 Initialize painted image attributes.
718 assert(image != (const Image *) NULL);
719 assert(image->signature == MagickCoreSignature);
720 if (image->debug != MagickFalse)
721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
722 assert(exception != (ExceptionInfo *) NULL);
723 assert(exception->signature == MagickCoreSignature);
724 width=GetOptimalKernelWidth2D(radius,sigma);
725 linear_image=CloneImage(image,0,0,MagickTrue,exception);
726 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
727 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
729 if (linear_image != (Image *) NULL)
730 linear_image=DestroyImage(linear_image);
731 if (paint_image != (Image *) NULL)
732 linear_image=DestroyImage(paint_image);
733 return((Image *) NULL);
735 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
737 linear_image=DestroyImage(linear_image);
738 paint_image=DestroyImage(paint_image);
739 return((Image *) NULL);
741 histograms=AcquireHistogramThreadSet(NumberPaintBins);
742 if (histograms == (size_t **) NULL)
744 linear_image=DestroyImage(linear_image);
745 paint_image=DestroyImage(paint_image);
746 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
753 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
754 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
755 image_view=AcquireVirtualCacheView(linear_image,exception);
756 paint_view=AcquireAuthenticCacheView(paint_image,exception);
757 #if defined(MAGICKCORE_OPENMP_SUPPORT)
758 #pragma omp parallel for schedule(static,4) shared(progress,status) \
759 magick_threads(linear_image,paint_image,linear_image->rows,1)
761 for (y=0; y < (ssize_t) linear_image->rows; y++)
763 register const Quantum
775 if (status == MagickFalse)
777 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
778 (width/2L),linear_image->columns+width,width,exception);
779 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
781 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
786 histogram=histograms[GetOpenMPThreadId()];
787 for (x=0; x < (ssize_t) linear_image->columns; x++)
803 Assign most frequent color.
808 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
809 for (v=0; v < (ssize_t) width; v++)
811 for (u=0; u < (ssize_t) width; u++)
813 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
814 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
816 if (histogram[n] > count)
822 k+=(ssize_t) (linear_image->columns+width);
824 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
826 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
827 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
828 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
829 if ((traits == UndefinedPixelTrait) ||
830 (paint_traits == UndefinedPixelTrait))
832 if (((paint_traits & CopyPixelTrait) != 0) ||
833 (GetPixelWriteMask(linear_image,p) == 0))
835 SetPixelChannel(paint_image,channel,p[center+i],q);
838 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
841 p+=GetPixelChannels(linear_image);
842 q+=GetPixelChannels(paint_image);
844 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
846 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
851 #if defined(MAGICKCORE_OPENMP_SUPPORT)
852 #pragma omp critical (MagickCore_OilPaintImage)
854 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
856 if (proceed == MagickFalse)
860 paint_view=DestroyCacheView(paint_view);
861 image_view=DestroyCacheView(image_view);
862 histograms=DestroyHistogramThreadSet(histograms);
863 linear_image=DestroyImage(linear_image);
864 if (status == MagickFalse)
865 paint_image=DestroyImage(paint_image);
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % O p a q u e P a i n t I m a g e %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % OpaquePaintImage() changes any pixel that matches color with the color
881 % defined by fill argument.
883 % By default color must match a particular pixel color exactly. However, in
884 % many cases two colors may differ by a small amount. Fuzz defines how much
885 % tolerance is acceptable to consider two colors as the same. For example,
886 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
887 % are now interpreted as the same color.
889 % The format of the OpaquePaintImage method is:
891 % MagickBooleanType OpaquePaintImage(Image *image,const PixelInfo *target,
892 % const PixelInfo *fill,const MagickBooleanType invert,
893 % ExceptionInfo *exception)
895 % A description of each parameter follows:
897 % o image: the image.
899 % o target: the RGB value of the target color.
901 % o fill: the replacement color.
903 % o invert: paint any pixel that does not match the target color.
905 % o exception: return any errors or warnings in this structure.
908 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
909 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
910 ExceptionInfo *exception)
912 #define OpaquePaintImageTag "Opaque/Image"
931 assert(image != (Image *) NULL);
932 assert(image->signature == MagickCoreSignature);
933 assert(target != (PixelInfo *) NULL);
934 assert(fill != (PixelInfo *) NULL);
935 if (image->debug != MagickFalse)
936 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
937 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
939 ConformPixelInfo(image,fill,&conform_fill,exception);
940 ConformPixelInfo(image,target,&conform_target,exception);
942 Make image color opaque.
946 GetPixelInfo(image,&zero);
947 image_view=AcquireAuthenticCacheView(image,exception);
948 #if defined(MAGICKCORE_OPENMP_SUPPORT)
949 #pragma omp parallel for schedule(static,4) shared(progress,status) \
950 magick_threads(image,image,image->rows,1)
952 for (y=0; y < (ssize_t) image->rows; y++)
963 if (status == MagickFalse)
965 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
966 if (q == (Quantum *) NULL)
972 for (x=0; x < (ssize_t) image->columns; x++)
974 if (GetPixelWriteMask(image,q) == 0)
976 q+=GetPixelChannels(image);
979 GetPixelInfoPixel(image,q,&pixel);
980 if (IsFuzzyEquivalencePixelInfo(&pixel,&conform_target) != invert)
981 SetPixelViaPixelInfo(image,&conform_fill,q);
982 q+=GetPixelChannels(image);
984 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
986 if (image->progress_monitor != (MagickProgressMonitor) NULL)
991 #if defined(MAGICKCORE_OPENMP_SUPPORT)
992 #pragma omp critical (MagickCore_OpaquePaintImage)
994 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
996 if (proceed == MagickFalse)
1000 image_view=DestroyCacheView(image_view);
1005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 % T r a n s p a r e n t P a i n t I m a g e %
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 % TransparentPaintImage() changes the opacity value associated with any pixel
1016 % that matches color to the value defined by opacity.
1018 % By default color must match a particular pixel color exactly. However, in
1019 % many cases two colors may differ by a small amount. Fuzz defines how much
1020 % tolerance is acceptable to consider two colors as the same. For example,
1021 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
1022 % are now interpreted as the same color.
1024 % The format of the TransparentPaintImage method is:
1026 % MagickBooleanType TransparentPaintImage(Image *image,
1027 % const PixelInfo *target,const Quantum opacity,
1028 % const MagickBooleanType invert,ExceptionInfo *exception)
1030 % A description of each parameter follows:
1032 % o image: the image.
1034 % o target: the target color.
1036 % o opacity: the replacement opacity value.
1038 % o invert: paint any pixel that does not match the target color.
1040 % o exception: return any errors or warnings in this structure.
1043 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
1044 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
1045 ExceptionInfo *exception)
1047 #define TransparentPaintImageTag "Transparent/Image"
1064 assert(image != (Image *) NULL);
1065 assert(image->signature == MagickCoreSignature);
1066 assert(target != (PixelInfo *) NULL);
1067 if (image->debug != MagickFalse)
1068 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1069 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1070 return(MagickFalse);
1071 if (image->alpha_trait == UndefinedPixelTrait)
1072 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1074 Make image color transparent.
1078 GetPixelInfo(image,&zero);
1079 image_view=AcquireAuthenticCacheView(image,exception);
1080 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1081 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1082 magick_threads(image,image,image->rows,1)
1084 for (y=0; y < (ssize_t) image->rows; y++)
1095 if (status == MagickFalse)
1097 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1098 if (q == (Quantum *) NULL)
1104 for (x=0; x < (ssize_t) image->columns; x++)
1106 if (GetPixelWriteMask(image,q) == 0)
1108 q+=GetPixelChannels(image);
1111 GetPixelInfoPixel(image,q,&pixel);
1112 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
1113 SetPixelAlpha(image,opacity,q);
1114 q+=GetPixelChannels(image);
1116 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1118 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1123 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1124 #pragma omp critical (MagickCore_TransparentPaintImage)
1126 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1128 if (proceed == MagickFalse)
1132 image_view=DestroyCacheView(image_view);
1137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1141 % 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 %
1145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1147 % TransparentPaintImageChroma() changes the opacity value associated with any
1148 % pixel that matches color to the value defined by opacity.
1150 % As there is one fuzz value for the all the channels, TransparentPaintImage()
1151 % is not suitable for the operations like chroma, where the tolerance for
1152 % similarity of two color component (RGB) can be different. Thus we define
1153 % this method to take two target pixels (one low and one high) and all the
1154 % pixels of an image which are lying between these two pixels are made
1157 % The format of the TransparentPaintImageChroma method is:
1159 % MagickBooleanType TransparentPaintImageChroma(Image *image,
1160 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1161 % const MagickBooleanType invert,ExceptionInfo *exception)
1163 % A description of each parameter follows:
1165 % o image: the image.
1167 % o low: the low target color.
1169 % o high: the high target color.
1171 % o opacity: the replacement opacity value.
1173 % o invert: paint any pixel that does not match the target color.
1175 % o exception: return any errors or warnings in this structure.
1178 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1179 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1180 const MagickBooleanType invert,ExceptionInfo *exception)
1182 #define TransparentPaintImageTag "Transparent/Image"
1196 assert(image != (Image *) NULL);
1197 assert(image->signature == MagickCoreSignature);
1198 assert(high != (PixelInfo *) NULL);
1199 assert(low != (PixelInfo *) NULL);
1200 if (image->debug != MagickFalse)
1201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1202 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1203 return(MagickFalse);
1204 if (image->alpha_trait == UndefinedPixelTrait)
1205 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1207 Make image color transparent.
1211 image_view=AcquireAuthenticCacheView(image,exception);
1212 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1213 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1214 magick_threads(image,image,image->rows,1)
1216 for (y=0; y < (ssize_t) image->rows; y++)
1230 if (status == MagickFalse)
1232 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1233 if (q == (Quantum *) NULL)
1238 GetPixelInfo(image,&pixel);
1239 for (x=0; x < (ssize_t) image->columns; x++)
1241 if (GetPixelWriteMask(image,q) == 0)
1243 q+=GetPixelChannels(image);
1246 GetPixelInfoPixel(image,q,&pixel);
1247 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1248 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1249 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1251 if (match != invert)
1252 SetPixelAlpha(image,opacity,q);
1253 q+=GetPixelChannels(image);
1255 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1257 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1265 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1267 if (proceed == MagickFalse)
1271 image_view=DestroyCacheView(image_view);