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-2017 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 % https://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 for (y=0; y < (ssize_t) image->rows; y++)
325 register const Quantum
335 Tile fill color onto floodplane.
337 if (status == MagickFalse)
339 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
340 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
341 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
346 for (x=0; x < (ssize_t) image->columns; x++)
348 if (GetPixelGray(floodplane_image,p) != 0)
350 GetFillColor(draw_info,x,y,&fill_color,exception);
351 SetPixelViaPixelInfo(image,&fill_color,q);
353 p+=GetPixelChannels(floodplane_image);
354 q+=GetPixelChannels(image);
356 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
359 floodplane_view=DestroyCacheView(floodplane_view);
360 image_view=DestroyCacheView(image_view);
361 segment_info=RelinquishVirtualMemory(segment_info);
362 floodplane_image=DestroyImage(floodplane_image);
367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 + G r a d i e n t I m a g e %
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 % GradientImage() applies a continuously smooth color transitions along a
378 % vector from one color to another.
380 % Note, the interface of this method will change in the future to support
381 % more than one transistion.
383 % The format of the GradientImage method is:
385 % MagickBooleanType GradientImage(Image *image,const GradientType type,
386 % const SpreadMethod method,const PixelInfo *start_color,
387 % const PixelInfo *stop_color,ExceptionInfo *exception)
389 % A description of each parameter follows:
391 % o image: the image.
393 % o type: the gradient type: linear or radial.
395 % o spread: the gradient spread meathod: pad, reflect, or repeat.
397 % o start_color: the start color.
399 % o stop_color: the stop color.
401 % o exception: return any errors or warnings in this structure.
404 MagickExport MagickBooleanType GradientImage(Image *image,
405 const GradientType type,const SpreadMethod method,const StopInfo *stops,
406 const size_t number_stops,ExceptionInfo *exception)
421 Set gradient start-stop end points.
423 assert(image != (const Image *) NULL);
424 assert(image->signature == MagickCoreSignature);
425 if (image->debug != MagickFalse)
426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
427 assert(stops != (const StopInfo *) NULL);
428 assert(number_stops > 0);
429 draw_info=AcquireDrawInfo();
430 gradient=(&draw_info->gradient);
432 gradient->bounding_box.width=image->columns;
433 gradient->bounding_box.height=image->rows;
434 artifact=GetImageArtifact(image,"gradient:bounding-box");
435 if (artifact != (const char *) NULL)
436 (void) ParseAbsoluteGeometry(artifact,&gradient->bounding_box);
437 gradient->gradient_vector.x2=(double) image->columns-1;
438 gradient->gradient_vector.y2=(double) image->rows-1;
439 artifact=GetImageArtifact(image,"gradient:direction");
440 if (artifact != (const char *) NULL)
445 direction=(GravityType) ParseCommandOption(MagickGravityOptions,
446 MagickFalse,artifact);
449 case NorthWestGravity:
451 gradient->gradient_vector.x1=(double) image->columns-1;
452 gradient->gradient_vector.y1=(double) image->rows-1;
453 gradient->gradient_vector.x2=0.0;
454 gradient->gradient_vector.y2=0.0;
459 gradient->gradient_vector.x1=0.0;
460 gradient->gradient_vector.y1=(double) image->rows-1;
461 gradient->gradient_vector.x2=0.0;
462 gradient->gradient_vector.y2=0.0;
465 case NorthEastGravity:
467 gradient->gradient_vector.x1=0.0;
468 gradient->gradient_vector.y1=(double) image->rows-1;
469 gradient->gradient_vector.x2=(double) image->columns-1;
470 gradient->gradient_vector.y2=0.0;
475 gradient->gradient_vector.x1=(double) image->columns-1;
476 gradient->gradient_vector.y1=0.0;
477 gradient->gradient_vector.x2=0.0;
478 gradient->gradient_vector.y2=0.0;
483 gradient->gradient_vector.x1=0.0;
484 gradient->gradient_vector.y1=0.0;
485 gradient->gradient_vector.x2=(double) image->columns-1;
486 gradient->gradient_vector.y2=0.0;
489 case SouthWestGravity:
491 gradient->gradient_vector.x1=(double) image->columns-1;
492 gradient->gradient_vector.y1=0.0;
493 gradient->gradient_vector.x2=0.0;
494 gradient->gradient_vector.y2=(double) image->rows-1;
499 gradient->gradient_vector.x1=0.0;
500 gradient->gradient_vector.y1=0.0;
501 gradient->gradient_vector.x2=0.0;
502 gradient->gradient_vector.y2=(double) image->columns-1;
505 case SouthEastGravity:
507 gradient->gradient_vector.x1=0.0;
508 gradient->gradient_vector.y1=0.0;
509 gradient->gradient_vector.x2=(double) image->columns-1;
510 gradient->gradient_vector.y2=(double) image->rows-1;
517 artifact=GetImageArtifact(image,"gradient:angle");
518 if (artifact != (const char *) NULL)
519 gradient->angle=StringToDouble(artifact,(char **) NULL);
520 artifact=GetImageArtifact(image,"gradient:vector");
521 if (artifact != (const char *) NULL)
522 (void) sscanf(artifact,"%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf",
523 &gradient->gradient_vector.x1,&gradient->gradient_vector.y1,
524 &gradient->gradient_vector.x2,&gradient->gradient_vector.y2);
525 if ((GetImageArtifact(image,"gradient:angle") == (const char *) NULL) &&
526 (GetImageArtifact(image,"gradient:direction") == (const char *) NULL) &&
527 (GetImageArtifact(image,"gradient:extent") == (const char *) NULL) &&
528 (GetImageArtifact(image,"gradient:vector") == (const char *) NULL))
529 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
530 gradient->gradient_vector.x2=0.0;
531 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
532 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
533 artifact=GetImageArtifact(image,"gradient:center");
534 if (artifact != (const char *) NULL)
535 (void) sscanf(artifact,"%lf%*[ ,]%lf",&gradient->center.x,
536 &gradient->center.y);
537 artifact=GetImageArtifact(image,"gradient:angle");
538 if ((type == LinearGradient) && (artifact != (const char *) NULL))
546 Reference https://drafts.csswg.org/css-images-3/#linear-gradients.
548 sine=sin((double) DegreesToRadians(gradient->angle-90.0));
549 cosine=cos((double) DegreesToRadians(gradient->angle-90.0));
550 distance=fabs((double) image->columns*cosine)+
551 fabs((double) image->rows*sine);
552 gradient->gradient_vector.x1=0.5*(image->columns-distance*cosine);
553 gradient->gradient_vector.y1=0.5*(image->rows-distance*sine);
554 gradient->gradient_vector.x2=0.5*(image->columns+distance*cosine);
555 gradient->gradient_vector.y2=0.5*(image->rows+distance*sine);
557 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
558 gradient->radii.y=gradient->radii.x;
559 artifact=GetImageArtifact(image,"gradient:extent");
560 if (artifact != (const char *) NULL)
562 if (LocaleCompare(artifact,"Circle") == 0)
564 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
565 gradient->radii.y=gradient->radii.x;
567 if (LocaleCompare(artifact,"Diagonal") == 0)
569 gradient->radii.x=(double) (sqrt(image->columns*image->columns+
570 image->rows*image->rows))/2.0;
571 gradient->radii.y=gradient->radii.x;
573 if (LocaleCompare(artifact,"Ellipse") == 0)
575 gradient->radii.x=(double) image->columns/2.0;
576 gradient->radii.y=(double) image->rows/2.0;
578 if (LocaleCompare(artifact,"Maximum") == 0)
580 gradient->radii.x=(double) MagickMax(image->columns,image->rows)/2.0;
581 gradient->radii.y=gradient->radii.x;
583 if (LocaleCompare(artifact,"Minimum") == 0)
585 gradient->radii.x=(double) (MagickMin(image->columns,image->rows))/
587 gradient->radii.y=gradient->radii.x;
590 artifact=GetImageArtifact(image,"gradient:radii");
591 if (artifact != (const char *) NULL)
592 (void) sscanf(artifact,"%lf%*[ ,]%lf",&gradient->radii.x,
594 gradient->radius=MagickMax(gradient->radii.x,gradient->radii.y);
595 gradient->spread=method;
597 Define the gradient to fill between the stops.
599 gradient->number_stops=number_stops;
600 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
601 sizeof(*gradient->stops));
602 if (gradient->stops == (StopInfo *) NULL)
603 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
605 (void) CopyMagickMemory(gradient->stops,stops,(size_t) number_stops*
608 Draw a gradient on the image.
610 status=DrawGradientImage(image,draw_info,exception);
611 draw_info=DestroyDrawInfo(draw_info);
616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % O i l P a i n t I m a g e %
624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626 % OilPaintImage() applies a special effect filter that simulates an oil
627 % painting. Each pixel is replaced by the most frequent color occurring
628 % in a circular region defined by radius.
630 % The format of the OilPaintImage method is:
632 % Image *OilPaintImage(const Image *image,const double radius,
633 % const double sigma,ExceptionInfo *exception)
635 % A description of each parameter follows:
637 % o image: the image.
639 % o radius: the radius of the circular neighborhood.
641 % o sigma: the standard deviation of the Gaussian, in pixels.
643 % o exception: return any errors or warnings in this structure.
647 static size_t **DestroyHistogramThreadSet(size_t **histogram)
652 assert(histogram != (size_t **) NULL);
653 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
654 if (histogram[i] != (size_t *) NULL)
655 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
656 histogram=(size_t **) RelinquishMagickMemory(histogram);
660 static size_t **AcquireHistogramThreadSet(const size_t count)
669 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
670 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
671 if (histogram == (size_t **) NULL)
672 return((size_t **) NULL);
673 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
674 for (i=0; i < (ssize_t) number_threads; i++)
676 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
677 if (histogram[i] == (size_t *) NULL)
678 return(DestroyHistogramThreadSet(histogram));
683 MagickExport Image *OilPaintImage(const Image *image,const double radius,
684 const double sigma,ExceptionInfo *exception)
686 #define NumberPaintBins 256
687 #define OilPaintImageTag "OilPaint/Image"
712 Initialize painted image attributes.
714 assert(image != (const Image *) NULL);
715 assert(image->signature == MagickCoreSignature);
716 if (image->debug != MagickFalse)
717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
718 assert(exception != (ExceptionInfo *) NULL);
719 assert(exception->signature == MagickCoreSignature);
720 width=GetOptimalKernelWidth2D(radius,sigma);
721 linear_image=CloneImage(image,0,0,MagickTrue,exception);
722 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
723 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
725 if (linear_image != (Image *) NULL)
726 linear_image=DestroyImage(linear_image);
727 if (paint_image != (Image *) NULL)
728 linear_image=DestroyImage(paint_image);
729 return((Image *) NULL);
731 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
733 linear_image=DestroyImage(linear_image);
734 paint_image=DestroyImage(paint_image);
735 return((Image *) NULL);
737 histograms=AcquireHistogramThreadSet(NumberPaintBins);
738 if (histograms == (size_t **) NULL)
740 linear_image=DestroyImage(linear_image);
741 paint_image=DestroyImage(paint_image);
742 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
749 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
750 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
751 image_view=AcquireVirtualCacheView(linear_image,exception);
752 paint_view=AcquireAuthenticCacheView(paint_image,exception);
753 #if defined(MAGICKCORE_OPENMP_SUPPORT)
754 #pragma omp parallel for schedule(static,4) shared(progress,status) \
755 magick_threads(linear_image,paint_image,linear_image->rows,1)
757 for (y=0; y < (ssize_t) linear_image->rows; y++)
759 register const Quantum
771 if (status == MagickFalse)
773 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
774 (width/2L),linear_image->columns+width,width,exception);
775 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
777 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
782 histogram=histograms[GetOpenMPThreadId()];
783 for (x=0; x < (ssize_t) linear_image->columns; x++)
799 Assign most frequent color.
804 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
805 for (v=0; v < (ssize_t) width; v++)
807 for (u=0; u < (ssize_t) width; u++)
809 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
810 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
812 if (histogram[n] > count)
818 k+=(ssize_t) (linear_image->columns+width);
820 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
822 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
823 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
824 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
825 if ((traits == UndefinedPixelTrait) ||
826 (paint_traits == UndefinedPixelTrait))
828 if (((paint_traits & CopyPixelTrait) != 0) ||
829 (GetPixelWriteMask(linear_image,p) == 0))
831 SetPixelChannel(paint_image,channel,p[center+i],q);
834 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
837 p+=GetPixelChannels(linear_image);
838 q+=GetPixelChannels(paint_image);
840 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
842 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
847 #if defined(MAGICKCORE_OPENMP_SUPPORT)
848 #pragma omp critical (MagickCore_OilPaintImage)
850 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
852 if (proceed == MagickFalse)
856 paint_view=DestroyCacheView(paint_view);
857 image_view=DestroyCacheView(image_view);
858 histograms=DestroyHistogramThreadSet(histograms);
859 linear_image=DestroyImage(linear_image);
860 if (status == MagickFalse)
861 paint_image=DestroyImage(paint_image);
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % O p a q u e P a i n t I m a g e %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 % OpaquePaintImage() changes any pixel that matches color with the color
877 % defined by fill argument.
879 % By default color must match a particular pixel color exactly. However, in
880 % many cases two colors may differ by a small amount. Fuzz defines how much
881 % tolerance is acceptable to consider two colors as the same. For example,
882 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
883 % are now interpreted as the same color.
885 % The format of the OpaquePaintImage method is:
887 % MagickBooleanType OpaquePaintImage(Image *image,const PixelInfo *target,
888 % const PixelInfo *fill,const MagickBooleanType invert,
889 % ExceptionInfo *exception)
891 % A description of each parameter follows:
893 % o image: the image.
895 % o target: the RGB value of the target color.
897 % o fill: the replacement color.
899 % o invert: paint any pixel that does not match the target color.
901 % o exception: return any errors or warnings in this structure.
904 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
905 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
906 ExceptionInfo *exception)
908 #define OpaquePaintImageTag "Opaque/Image"
930 assert(image != (Image *) NULL);
931 assert(image->signature == MagickCoreSignature);
932 assert(target != (PixelInfo *) NULL);
933 assert(fill != (PixelInfo *) NULL);
934 if (image->debug != MagickFalse)
935 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
936 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
938 ConformPixelInfo(image,fill,&conform_fill,exception);
939 ConformPixelInfo(image,target,&conform_target,exception);
941 Make image color opaque.
945 GetPixelInfo(image,&zero);
946 image_view=AcquireAuthenticCacheView(image,exception);
947 #if defined(MAGICKCORE_OPENMP_SUPPORT)
948 #pragma omp parallel for schedule(static,4) shared(progress,status) \
949 magick_threads(image,image,image->rows,1)
951 for (y=0; y < (ssize_t) image->rows; y++)
962 if (status == MagickFalse)
964 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
965 if (q == (Quantum *) NULL)
971 for (x=0; x < (ssize_t) image->columns; x++)
973 if (GetPixelWriteMask(image,q) == 0)
975 q+=GetPixelChannels(image);
978 GetPixelInfoPixel(image,q,&pixel);
979 if (IsFuzzyEquivalencePixelInfo(&pixel,&conform_target) != invert)
981 channel_map=image->channel_map;
982 if ((channel_map[RedPixelChannel].traits & UpdatePixelTrait) != 0)
983 SetPixelRed(image,conform_fill.red,q);
984 if ((channel_map[GreenPixelChannel].traits & UpdatePixelTrait) != 0)
985 SetPixelGreen(image,conform_fill.green,q);
986 if ((channel_map[BluePixelChannel].traits & UpdatePixelTrait) != 0)
987 SetPixelBlue(image,conform_fill.blue,q);
988 if ((channel_map[BlackPixelChannel].traits & UpdatePixelTrait) != 0)
989 SetPixelBlack(image,conform_fill.black,q);
990 if ((channel_map[AlphaPixelChannel].traits & UpdatePixelTrait) != 0)
991 SetPixelAlpha(image,conform_fill.alpha,q);
993 q+=GetPixelChannels(image);
995 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
997 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1002 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1003 #pragma omp critical (MagickCore_OpaquePaintImage)
1005 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
1007 if (proceed == MagickFalse)
1011 image_view=DestroyCacheView(image_view);
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020 % T r a n s p a r e n t P a i n t I m a g e %
1024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1026 % TransparentPaintImage() changes the opacity value associated with any pixel
1027 % that matches color to the value defined by opacity.
1029 % By default color must match a particular pixel color exactly. However, in
1030 % many cases two colors may differ by a small amount. Fuzz defines how much
1031 % tolerance is acceptable to consider two colors as the same. For example,
1032 % set fuzz to 10 and the color red at intensities of 100 and 102 respectively
1033 % are now interpreted as the same color.
1035 % The format of the TransparentPaintImage method is:
1037 % MagickBooleanType TransparentPaintImage(Image *image,
1038 % const PixelInfo *target,const Quantum opacity,
1039 % const MagickBooleanType invert,ExceptionInfo *exception)
1041 % A description of each parameter follows:
1043 % o image: the image.
1045 % o target: the target color.
1047 % o opacity: the replacement opacity value.
1049 % o invert: paint any pixel that does not match the target color.
1051 % o exception: return any errors or warnings in this structure.
1054 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
1055 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
1056 ExceptionInfo *exception)
1058 #define TransparentPaintImageTag "Transparent/Image"
1075 assert(image != (Image *) NULL);
1076 assert(image->signature == MagickCoreSignature);
1077 assert(target != (PixelInfo *) NULL);
1078 if (image->debug != MagickFalse)
1079 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1080 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1081 return(MagickFalse);
1082 if (image->alpha_trait == UndefinedPixelTrait)
1083 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1085 Make image color transparent.
1089 GetPixelInfo(image,&zero);
1090 image_view=AcquireAuthenticCacheView(image,exception);
1091 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1092 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1093 magick_threads(image,image,image->rows,1)
1095 for (y=0; y < (ssize_t) image->rows; y++)
1106 if (status == MagickFalse)
1108 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1109 if (q == (Quantum *) NULL)
1115 for (x=0; x < (ssize_t) image->columns; x++)
1117 if (GetPixelWriteMask(image,q) == 0)
1119 q+=GetPixelChannels(image);
1122 GetPixelInfoPixel(image,q,&pixel);
1123 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
1124 SetPixelAlpha(image,opacity,q);
1125 q+=GetPixelChannels(image);
1127 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1129 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1134 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1135 #pragma omp critical (MagickCore_TransparentPaintImage)
1137 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1139 if (proceed == MagickFalse)
1143 image_view=DestroyCacheView(image_view);
1148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152 % 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 %
1156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1158 % TransparentPaintImageChroma() changes the opacity value associated with any
1159 % pixel that matches color to the value defined by opacity.
1161 % As there is one fuzz value for the all the channels, TransparentPaintImage()
1162 % is not suitable for the operations like chroma, where the tolerance for
1163 % similarity of two color component (RGB) can be different. Thus we define
1164 % this method to take two target pixels (one low and one high) and all the
1165 % pixels of an image which are lying between these two pixels are made
1168 % The format of the TransparentPaintImageChroma method is:
1170 % MagickBooleanType TransparentPaintImageChroma(Image *image,
1171 % const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1172 % const MagickBooleanType invert,ExceptionInfo *exception)
1174 % A description of each parameter follows:
1176 % o image: the image.
1178 % o low: the low target color.
1180 % o high: the high target color.
1182 % o opacity: the replacement opacity value.
1184 % o invert: paint any pixel that does not match the target color.
1186 % o exception: return any errors or warnings in this structure.
1189 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1190 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1191 const MagickBooleanType invert,ExceptionInfo *exception)
1193 #define TransparentPaintImageTag "Transparent/Image"
1207 assert(image != (Image *) NULL);
1208 assert(image->signature == MagickCoreSignature);
1209 assert(high != (PixelInfo *) NULL);
1210 assert(low != (PixelInfo *) NULL);
1211 if (image->debug != MagickFalse)
1212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1213 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1214 return(MagickFalse);
1215 if (image->alpha_trait == UndefinedPixelTrait)
1216 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1218 Make image color transparent.
1222 image_view=AcquireAuthenticCacheView(image,exception);
1223 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1224 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1225 magick_threads(image,image,image->rows,1)
1227 for (y=0; y < (ssize_t) image->rows; y++)
1241 if (status == MagickFalse)
1243 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1244 if (q == (Quantum *) NULL)
1249 GetPixelInfo(image,&pixel);
1250 for (x=0; x < (ssize_t) image->columns; x++)
1252 if (GetPixelWriteMask(image,q) == 0)
1254 q+=GetPixelChannels(image);
1257 GetPixelInfoPixel(image,q,&pixel);
1258 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1259 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1260 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1262 if (match != invert)
1263 SetPixelAlpha(image,opacity,q);
1264 q+=GetPixelChannels(image);
1266 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1268 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1273 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1274 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1276 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1278 if (proceed == MagickFalse)
1282 image_view=DestroyCacheView(image_view);