]> granicus.if.org Git - imagemagick/blob - MagickCore/paint.c
(no commit message)
[imagemagick] / MagickCore / paint.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                      PPPP    AAA   IIIII  N   N  TTTTT                      %
7 %                      P   P  A   A    I    NN  N    T                        %
8 %                      PPPP   AAAAA    I    N N N    T                        %
9 %                      P      A   A    I    N  NN    T                        %
10 %                      P      A   A  IIIII  N   N    T                        %
11 %                                                                             %
12 %                                                                             %
13 %                        Methods to Paint on an Image                         %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1998                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40  Include declarations.
41 */
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/resource_.h"
59 #include "MagickCore/statistic.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/thread-private.h"
62 \f
63 /*
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %                                                                             %
66 %                                                                             %
67 %                                                                             %
68 %   F l o o d f i l l P a i n t I m a g e                                     %
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %
74 %  FloodfillPaintImage() changes the color value of any pixel that matches
75 %  target and is an immediate neighbor.  If the method FillToBorderMethod is
76 %  specified, the color value is changed for any neighbor pixel that does not
77 %  match the bordercolor member of image.
78 %
79 %  By default target must match a particular pixel color exactly.  However,
80 %  in many cases two colors may differ by a small amount.  The fuzz member of
81 %  image defines how much tolerance is acceptable to consider two colors as
82 %  the same.  For example, set fuzz to 10 and the color red at intensities of
83 %  100 and 102 respectively are now interpreted as the same color for the
84 %  purposes of the floodfill.
85 %
86 %  The format of the FloodfillPaintImage method is:
87 %
88 %      MagickBooleanType FloodfillPaintImage(Image *image,
89 %        const DrawInfo *draw_info,const PixelInfo target,
90 %        const ssize_t x_offset,const ssize_t y_offset,
91 %        const MagickBooleanType invert,ExceptionInfo *exception)
92 %
93 %  A description of each parameter follows:
94 %
95 %    o image: the image.
96 %
97 %    o draw_info: the draw info.
98 %
99 %    o target: the RGB value of the target color.
100 %
101 %    o x_offset,y_offset: the starting location of the operation.
102 %
103 %    o invert: paint any pixel that does not match the target color.
104 %
105 %    o exception: return any errors or warnings in this structure.
106 %
107 */
108 MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
109   const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
110   const ssize_t y_offset,const MagickBooleanType invert,
111   ExceptionInfo *exception)
112 {
113 #define MaxStacksize  131072UL
114 #define PushSegmentStack(up,left,right,delta) \
115 { \
116   if (s >= (segment_stack+MaxStacksize)) \
117     ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
118   else \
119     { \
120       if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
121         { \
122           s->x1=(double) (left); \
123           s->y1=(double) (up); \
124           s->x2=(double) (right); \
125           s->y2=(double) (delta); \
126           s++; \
127         } \
128     } \
129 }
130
131   CacheView
132     *floodplane_view,
133     *image_view;
134
135   Image
136     *floodplane_image;
137
138   MagickBooleanType
139     skip,
140     status;
141
142   PixelInfo
143     fill_color,
144     pixel;
145
146   register SegmentInfo
147     *s;
148
149   SegmentInfo
150     *segment_stack;
151
152   ssize_t
153     offset,
154     start,
155     x,
156     x1,
157     x2,
158     y;
159
160   /*
161     Check boundary conditions.
162   */
163   assert(image != (Image *) NULL);
164   assert(image->signature == MagickSignature);
165   if (image->debug != MagickFalse)
166     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167   assert(draw_info != (DrawInfo *) NULL);
168   assert(draw_info->signature == MagickSignature);
169   if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
170     return(MagickFalse);
171   if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
172     return(MagickFalse);
173   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
174     return(MagickFalse);
175   if (IsGrayColorspace(image->colorspace) != MagickFalse)
176     (void) TransformImageColorspace(image,RGBColorspace,exception);
177   if ((image->alpha_trait != BlendPixelTrait) && (draw_info->fill.alpha_trait == BlendPixelTrait))
178     (void) SetImageAlpha(image,OpaqueAlpha,exception);
179   /*
180     Set floodfill state.
181   */
182   floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
183     exception);
184   if (floodplane_image == (Image *) NULL)
185     return(MagickFalse);
186   floodplane_image->alpha_trait=UndefinedPixelTrait;
187   floodplane_image->colorspace=GRAYColorspace;
188   (void) QueryColorCompliance("#000",AllCompliance,
189     &floodplane_image->background_color,exception);
190   (void) SetImageBackgroundColor(floodplane_image,exception);
191   segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
192     sizeof(*segment_stack));
193   if (segment_stack == (SegmentInfo *) NULL)
194     {
195       floodplane_image=DestroyImage(floodplane_image);
196       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
197         image->filename);
198     }
199   /*
200     Push initial segment on stack.
201   */
202   status=MagickTrue;
203   x=x_offset;
204   y=y_offset;
205   start=0;
206   s=segment_stack;
207   PushSegmentStack(y,x,x,1);
208   PushSegmentStack(y+1,x,x,-1);
209   GetPixelInfo(image,&pixel);
210   image_view=AcquireVirtualCacheView(image,exception);
211   floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
212   while (s > segment_stack)
213   {
214     register const Quantum
215       *restrict p;
216
217     register Quantum
218       *restrict q;
219
220     register ssize_t
221       x;
222
223     /*
224       Pop segment off stack.
225     */
226     s--;
227     x1=(ssize_t) s->x1;
228     x2=(ssize_t) s->x2;
229     offset=(ssize_t) s->y2;
230     y=(ssize_t) s->y1+offset;
231     /*
232       Recolor neighboring pixels.
233     */
234     p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
235     q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
236       exception);
237     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
238       break;
239     p+=x1*GetPixelChannels(image);
240     q+=x1*GetPixelChannels(floodplane_image);
241     for (x=x1; x >= 0; x--)
242     {
243       if (GetPixelGray(floodplane_image,q) != 0)
244         break;
245       GetPixelInfoPixel(image,p,&pixel);
246       if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
247         break;
248       SetPixelGray(floodplane_image,QuantumRange,q);
249       p-=GetPixelChannels(image);
250       q-=GetPixelChannels(floodplane_image);
251     }
252     if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
253       break;
254     skip=x >= x1 ? MagickTrue : MagickFalse;
255     if (skip == MagickFalse)
256       {
257         start=x+1;
258         if (start < x1)
259           PushSegmentStack(y,start,x1-1,-offset);
260         x=x1+1;
261       }
262     do
263     {
264       if (skip == MagickFalse)
265         {
266           if (x < (ssize_t) image->columns)
267             {
268               p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
269                 exception);
270               q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
271                 x,1,exception);
272               if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
273                 break;
274               for ( ; x < (ssize_t) image->columns; x++)
275               {
276                 if (GetPixelGray(floodplane_image,q) != 0)
277                   break;
278                 GetPixelInfoPixel(image,p,&pixel);
279                 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
280                   break;
281                 SetPixelGray(floodplane_image,QuantumRange,q);
282                 p+=GetPixelChannels(image);
283                 q+=GetPixelChannels(floodplane_image);
284               }
285               status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
286               if (status == MagickFalse)
287                 break;
288             }
289           PushSegmentStack(y,start,x-1,offset);
290           if (x > (x2+1))
291             PushSegmentStack(y,x2+1,x-1,-offset);
292         }
293       skip=MagickFalse;
294       x++;
295       if (x <= x2)
296         {
297           p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
298             exception);
299           q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
300             exception);
301           if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
302             break;
303           for ( ; x <= x2; x++)
304           {
305             if (GetPixelGray(floodplane_image,q) != 0)
306               break;
307             GetPixelInfoPixel(image,p,&pixel);
308             if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
309               break;
310             p+=GetPixelChannels(image);
311             q+=GetPixelChannels(floodplane_image);
312           }
313         }
314       start=x;
315     } while (x <= x2);
316   }
317   for (y=0; y < (ssize_t) image->rows; y++)
318   {
319     register const Quantum
320       *restrict p;
321
322     register Quantum
323       *restrict q;
324
325     register ssize_t
326       x;
327
328     /*
329       Tile fill color onto floodplane.
330     */
331     p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
332     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
333     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
334       break;
335     for (x=0; x < (ssize_t) image->columns; x++)
336     {
337       if (GetPixelGray(floodplane_image,p) != 0)
338         {
339           (void) GetFillColor(draw_info,x,y,&fill_color,exception);
340           SetPixelInfoPixel(image,&fill_color,q);
341         }
342       p+=GetPixelChannels(floodplane_image);
343       q+=GetPixelChannels(image);
344     }
345     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
346       break;
347   }
348   floodplane_view=DestroyCacheView(floodplane_view);
349   image_view=DestroyCacheView(image_view);
350   segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
351   floodplane_image=DestroyImage(floodplane_image);
352   return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
353 }
354 \f
355 /*
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 %                                                                             %
358 %                                                                             %
359 %                                                                             %
360 +     G r a d i e n t I m a g e                                               %
361 %                                                                             %
362 %                                                                             %
363 %                                                                             %
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 %
366 %  GradientImage() applies a continuously smooth color transitions along a
367 %  vector from one color to another.
368 %
369 %  Note, the interface of this method will change in the future to support
370 %  more than one transistion.
371 %
372 %  The format of the GradientImage method is:
373 %
374 %      MagickBooleanType GradientImage(Image *image,const GradientType type,
375 %        const SpreadMethod method,const PixelInfo *start_color,
376 %        const PixelInfo *stop_color,ExceptionInfo *exception)
377 %
378 %  A description of each parameter follows:
379 %
380 %    o image: the image.
381 %
382 %    o type: the gradient type: linear or radial.
383 %
384 %    o spread: the gradient spread meathod: pad, reflect, or repeat.
385 %
386 %    o start_color: the start color.
387 %
388 %    o stop_color: the stop color.
389 %
390 %    o exception: return any errors or warnings in this structure.
391 %
392 */
393
394 static inline double MagickMax(const double x,const double y)
395 {
396   return(x > y ? x : y);
397 }
398
399 MagickExport MagickBooleanType GradientImage(Image *image,
400   const GradientType type,const SpreadMethod method,
401   const PixelInfo *start_color,const PixelInfo *stop_color,
402   ExceptionInfo *exception)
403 {
404   DrawInfo
405     *draw_info;
406
407   GradientInfo
408     *gradient;
409
410   MagickBooleanType
411     status;
412
413   register ssize_t
414     i;
415
416   /*
417     Set gradient start-stop end points.
418   */
419   assert(image != (const Image *) NULL);
420   assert(image->signature == MagickSignature);
421   if (image->debug != MagickFalse)
422     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
423   assert(start_color != (const PixelInfo *) NULL);
424   assert(stop_color != (const PixelInfo *) NULL);
425   draw_info=AcquireDrawInfo();
426   gradient=(&draw_info->gradient);
427   gradient->type=type;
428   gradient->bounding_box.width=image->columns;
429   gradient->bounding_box.height=image->rows;
430   gradient->gradient_vector.x2=(double) image->columns-1.0;
431   gradient->gradient_vector.y2=(double) image->rows-1.0;
432   if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
433     gradient->gradient_vector.x2=0.0;
434   gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
435   gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
436   gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
437   gradient->spread=method;
438   /*
439     Define the gradient to fill between the stops.
440   */
441   gradient->number_stops=2;
442   gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
443     sizeof(*gradient->stops));
444   if (gradient->stops == (StopInfo *) NULL)
445     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
446       image->filename);
447   (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
448     sizeof(*gradient->stops));
449   for (i=0; i < (ssize_t) gradient->number_stops; i++)
450     GetPixelInfo(image,&gradient->stops[i].color);
451   gradient->stops[0].color=(*start_color);
452   gradient->stops[0].offset=0.0;
453   gradient->stops[1].color=(*stop_color);
454   gradient->stops[1].offset=1.0;
455   /*
456     Draw a gradient on the image.
457   */
458   (void) SetImageColorspace(image,start_color->colorspace,exception);
459   status=DrawGradientImage(image,draw_info,exception);
460   draw_info=DestroyDrawInfo(draw_info);
461   return(status);
462 }
463 \f
464 /*
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 %                                                                             %
467 %                                                                             %
468 %                                                                             %
469 %     O i l P a i n t I m a g e                                               %
470 %                                                                             %
471 %                                                                             %
472 %                                                                             %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 %
475 %  OilPaintImage() applies a special effect filter that simulates an oil
476 %  painting.  Each pixel is replaced by the most frequent color occurring
477 %  in a circular region defined by radius.
478 %
479 %  The format of the OilPaintImage method is:
480 %
481 %      Image *OilPaintImage(const Image *image,const double radius,
482 %        const double sigma,ExceptionInfo *exception)
483 %
484 %  A description of each parameter follows:
485 %
486 %    o image: the image.
487 %
488 %    o radius: the radius of the circular neighborhood.
489 %
490 %    o sigma: the standard deviation of the Gaussian, in pixels.
491 %
492 %    o exception: return any errors or warnings in this structure.
493 %
494 */
495
496 static size_t **DestroyHistogramThreadSet(size_t **histogram)
497 {
498   register ssize_t
499     i;
500
501   assert(histogram != (size_t **) NULL);
502   for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
503     if (histogram[i] != (size_t *) NULL)
504       histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
505   histogram=(size_t **) RelinquishMagickMemory(histogram);
506   return(histogram);
507 }
508
509 static size_t **AcquireHistogramThreadSet(const size_t count)
510 {
511   register ssize_t
512     i;
513
514   size_t
515     **histogram,
516     number_threads;
517
518   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
519   histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
520   if (histogram == (size_t **) NULL)
521     return((size_t **) NULL);
522   (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
523   for (i=0; i < (ssize_t) number_threads; i++)
524   {
525     histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
526     if (histogram[i] == (size_t *) NULL)
527       return(DestroyHistogramThreadSet(histogram));
528   }
529   return(histogram);
530 }
531
532 MagickExport Image *OilPaintImage(const Image *image,const double radius,
533   const double sigma,ExceptionInfo *exception)
534 {
535 #define NumberPaintBins  256
536 #define OilPaintImageTag  "OilPaint/Image"
537
538   CacheView
539     *image_view,
540     *paint_view;
541
542   Image
543     *linear_image,
544     *paint_image;
545
546   MagickBooleanType
547     status;
548
549   MagickOffsetType
550     progress;
551
552   size_t
553     **histograms,
554     width;
555
556   ssize_t
557     center,
558     y;
559
560   /*
561     Initialize painted image attributes.
562   */
563   assert(image != (const Image *) NULL);
564   assert(image->signature == MagickSignature);
565   if (image->debug != MagickFalse)
566     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567   assert(exception != (ExceptionInfo *) NULL);
568   assert(exception->signature == MagickSignature);
569   width=GetOptimalKernelWidth2D(radius,sigma);
570   linear_image=CloneImage(image,0,0,MagickTrue,exception);
571   paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
572   if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
573     {
574       if (linear_image != (Image *) NULL)
575         linear_image=DestroyImage(linear_image);
576       if (paint_image != (Image *) NULL)
577         linear_image=DestroyImage(paint_image);
578       return((Image *) NULL);
579     }
580   if (image->colorspace == sRGBColorspace)
581     (void) TransformImageColorspace(linear_image,sRGBColorspace,exception);
582   if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
583     {
584       linear_image=DestroyImage(linear_image);
585       paint_image=DestroyImage(paint_image);
586       return((Image *) NULL);
587     }
588   histograms=AcquireHistogramThreadSet(NumberPaintBins);
589   if (histograms == (size_t **) NULL)
590     {
591       linear_image=DestroyImage(linear_image);
592       paint_image=DestroyImage(paint_image);
593       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
594     }
595   /*
596     Oil paint image.
597   */
598   status=MagickTrue;
599   progress=0;
600   center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
601     (width/2L)+GetPixelChannels(linear_image)*(width/2L);
602   image_view=AcquireVirtualCacheView(linear_image,exception);
603   paint_view=AcquireAuthenticCacheView(paint_image,exception);
604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
605   #pragma omp parallel for schedule(static,4) shared(progress,status) \
606     magick_threads(linear_image,paint_image,linear_image->rows,1)
607 #endif
608   for (y=0; y < (ssize_t) linear_image->rows; y++)
609   {
610     register const Quantum
611       *restrict p;
612
613     register Quantum
614       *restrict q;
615
616     register size_t
617       *histogram;
618
619     register ssize_t
620       x;
621
622     if (status == MagickFalse)
623       continue;
624     p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
625       (width/2L),linear_image->columns+width,width,exception);
626     q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
627       exception);
628     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
629       {
630         status=MagickFalse;
631         continue;
632       }
633     histogram=histograms[GetOpenMPThreadId()];
634     for (x=0; x < (ssize_t) linear_image->columns; x++)
635     {
636       register ssize_t
637         i,
638         u;
639
640       size_t
641         count;
642
643       ssize_t
644         j,
645         k,
646         n,
647         v;
648
649       /*
650         Assign most frequent color.
651       */
652       k=0;
653       j=0;
654       count=0;
655       (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
656       for (v=0; v < (ssize_t) width; v++)
657       {
658         for (u=0; u < (ssize_t) width; u++)
659         {
660           n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
661             linear_image,p+GetPixelChannels(linear_image)*(u+k))));
662           histogram[n]++;
663           if (histogram[n] > count)
664             {
665               j=k+u;
666               count=histogram[n];
667             }
668         }
669         k+=(ssize_t) (linear_image->columns+width);
670       }
671       for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
672       {
673         PixelChannel channel=GetPixelChannelChannel(linear_image,i);
674         PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
675         PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
676         if ((traits == UndefinedPixelTrait) ||
677             (paint_traits == UndefinedPixelTrait))
678           continue;
679         if (((paint_traits & CopyPixelTrait) != 0) ||
680             (GetPixelMask(linear_image,p) != 0))
681           {
682             SetPixelChannel(paint_image,channel,p[center+i],q);
683             continue;
684           }
685         SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
686           i],q);
687       }
688       p+=GetPixelChannels(linear_image);
689       q+=GetPixelChannels(paint_image);
690     }
691     if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
692       status=MagickFalse;
693     if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
694       {
695         MagickBooleanType
696           proceed;
697
698 #if defined(MAGICKCORE_OPENMP_SUPPORT)
699         #pragma omp critical (MagickCore_OilPaintImage)
700 #endif
701         proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
702           linear_image->rows);
703         if (proceed == MagickFalse)
704           status=MagickFalse;
705       }
706   }
707   paint_view=DestroyCacheView(paint_view);
708   image_view=DestroyCacheView(image_view);
709   histograms=DestroyHistogramThreadSet(histograms);
710   linear_image=DestroyImage(linear_image);
711   if (image->colorspace == sRGBColorspace)
712     (void) TransformImageColorspace(paint_image,sRGBColorspace,exception);
713   if (status == MagickFalse)
714     paint_image=DestroyImage(paint_image);
715   return(paint_image);
716 }
717 \f
718 /*
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 %                                                                             %
721 %                                                                             %
722 %                                                                             %
723 %     O p a q u e P a i n t I m a g e                                         %
724 %                                                                             %
725 %                                                                             %
726 %                                                                             %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 %
729 %  OpaquePaintImage() changes any pixel that matches color with the color
730 %  defined by fill.
731 %
732 %  By default color must match a particular pixel color exactly.  However, in
733 %  many cases two colors may differ by a small amount.  Fuzz defines how much
734 %  tolerance is acceptable to consider two colors as the same.  For example,
735 %  set fuzz to 10 and the color red at intensities of 100 and 102 respectively
736 %  are now interpreted as the same color.
737 %
738 %  The format of the OpaquePaintImage method is:
739 %
740 %      MagickBooleanType OpaquePaintImage(Image *image,
741 %        const PixelInfo *target,const PixelInfo *fill,
742 %        const MagickBooleanType invert,ExceptionInfo *exception)
743 %
744 %  A description of each parameter follows:
745 %
746 %    o image: the image.
747 %
748 %    o target: the RGB value of the target color.
749 %
750 %    o fill: the replacement color.
751 %
752 %    o invert: paint any pixel that does not match the target color.
753 %
754 %    o exception: return any errors or warnings in this structure.
755 %
756 */
757 MagickExport MagickBooleanType OpaquePaintImage(Image *image,
758   const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
759   ExceptionInfo *exception)
760 {
761 #define OpaquePaintImageTag  "Opaque/Image"
762
763   CacheView
764     *image_view;
765
766   MagickBooleanType
767     status;
768
769   MagickOffsetType
770     progress;
771
772   PixelInfo
773     zero;
774
775   ssize_t
776     y;
777
778   assert(image != (Image *) NULL);
779   assert(image->signature == MagickSignature);
780   assert(target != (PixelInfo *) NULL);
781   assert(fill != (PixelInfo *) NULL);
782   if (image->debug != MagickFalse)
783     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
784   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
785     return(MagickFalse);
786   if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
787       (IsPixelInfoGray(fill) == MagickFalse))
788     (void) TransformImageColorspace(image,RGBColorspace,exception);
789   if ((fill->alpha_trait == BlendPixelTrait) && (image->alpha_trait != BlendPixelTrait))
790     (void) SetImageAlpha(image,OpaqueAlpha,exception);
791   /*
792     Make image color opaque.
793   */
794   status=MagickTrue;
795   progress=0;
796   GetPixelInfo(image,&zero);
797   image_view=AcquireAuthenticCacheView(image,exception);
798 #if defined(MAGICKCORE_OPENMP_SUPPORT)
799   #pragma omp parallel for schedule(static,4) shared(progress,status) \
800     magick_threads(image,image,image->rows,1)
801 #endif
802   for (y=0; y < (ssize_t) image->rows; y++)
803   {
804     PixelInfo
805       pixel;
806
807     register Quantum
808       *restrict q;
809
810     register ssize_t
811       x;
812
813     if (status == MagickFalse)
814       continue;
815     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
816     if (q == (Quantum *) NULL)
817       {
818         status=MagickFalse;
819         continue;
820       }
821     pixel=zero;
822     for (x=0; x < (ssize_t) image->columns; x++)
823     {
824       GetPixelInfoPixel(image,q,&pixel);
825       if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
826         SetPixelInfoPixel(image,fill,q);
827       q+=GetPixelChannels(image);
828     }
829     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
830       status=MagickFalse;
831     if (image->progress_monitor != (MagickProgressMonitor) NULL)
832       {
833         MagickBooleanType
834           proceed;
835
836 #if defined(MAGICKCORE_OPENMP_SUPPORT)
837         #pragma omp critical (MagickCore_OpaquePaintImage)
838 #endif
839         proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
840           image->rows);
841         if (proceed == MagickFalse)
842           status=MagickFalse;
843       }
844   }
845   image_view=DestroyCacheView(image_view);
846   return(status);
847 }
848 \f
849 /*
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 %                                                                             %
852 %                                                                             %
853 %                                                                             %
854 %     T r a n s p a r e n t P a i n t I m a g e                               %
855 %                                                                             %
856 %                                                                             %
857 %                                                                             %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 %
860 %  TransparentPaintImage() changes the opacity value associated with any pixel
861 %  that matches color to the value defined by opacity.
862 %
863 %  By default color must match a particular pixel color exactly.  However, in
864 %  many cases two colors may differ by a small amount.  Fuzz defines how much
865 %  tolerance is acceptable to consider two colors as the same.  For example,
866 %  set fuzz to 10 and the color red at intensities of 100 and 102 respectively
867 %  are now interpreted as the same color.
868 %
869 %  The format of the TransparentPaintImage method is:
870 %
871 %      MagickBooleanType TransparentPaintImage(Image *image,
872 %        const PixelInfo *target,const Quantum opacity,
873 %        const MagickBooleanType invert,ExceptionInfo *exception)
874 %
875 %  A description of each parameter follows:
876 %
877 %    o image: the image.
878 %
879 %    o target: the target color.
880 %
881 %    o opacity: the replacement opacity value.
882 %
883 %    o invert: paint any pixel that does not match the target color.
884 %
885 %    o exception: return any errors or warnings in this structure.
886 %
887 */
888 MagickExport MagickBooleanType TransparentPaintImage(Image *image,
889   const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
890   ExceptionInfo *exception)
891 {
892 #define TransparentPaintImageTag  "Transparent/Image"
893
894   CacheView
895     *image_view;
896
897   MagickBooleanType
898     status;
899
900   MagickOffsetType
901     progress;
902
903   PixelInfo
904     zero;
905
906   ssize_t
907     y;
908
909   assert(image != (Image *) NULL);
910   assert(image->signature == MagickSignature);
911   assert(target != (PixelInfo *) NULL);
912   if (image->debug != MagickFalse)
913     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
914   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
915     return(MagickFalse);
916   if (image->alpha_trait != BlendPixelTrait)
917     (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
918   /*
919     Make image color transparent.
920   */
921   status=MagickTrue;
922   progress=0;
923   GetPixelInfo(image,&zero);
924   image_view=AcquireAuthenticCacheView(image,exception);
925 #if defined(MAGICKCORE_OPENMP_SUPPORT)
926   #pragma omp parallel for schedule(static,4) shared(progress,status) \
927     magick_threads(image,image,image->rows,1)
928 #endif
929   for (y=0; y < (ssize_t) image->rows; y++)
930   {
931     PixelInfo
932       pixel;
933
934     register ssize_t
935       x;
936
937     register Quantum
938       *restrict q;
939
940     if (status == MagickFalse)
941       continue;
942     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
943     if (q == (Quantum *) NULL)
944       {
945         status=MagickFalse;
946         continue;
947       }
948     pixel=zero;
949     for (x=0; x < (ssize_t) image->columns; x++)
950     {
951       GetPixelInfoPixel(image,q,&pixel);
952       if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
953         SetPixelAlpha(image,opacity,q);
954       q+=GetPixelChannels(image);
955     }
956     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
957       status=MagickFalse;
958     if (image->progress_monitor != (MagickProgressMonitor) NULL)
959       {
960         MagickBooleanType
961           proceed;
962
963 #if defined(MAGICKCORE_OPENMP_SUPPORT)
964         #pragma omp critical (MagickCore_TransparentPaintImage)
965 #endif
966         proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
967           image->rows);
968         if (proceed == MagickFalse)
969           status=MagickFalse;
970       }
971   }
972   image_view=DestroyCacheView(image_view);
973   return(status);
974 }
975 \f
976 /*
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 %                                                                             %
979 %                                                                             %
980 %                                                                             %
981 %     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                   %
982 %                                                                             %
983 %                                                                             %
984 %                                                                             %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 %
987 %  TransparentPaintImageChroma() changes the opacity value associated with any
988 %  pixel that matches color to the value defined by opacity.
989 %
990 %  As there is one fuzz value for the all the channels, TransparentPaintImage()
991 %  is not suitable for the operations like chroma, where the tolerance for
992 %  similarity of two color component (RGB) can be different. Thus we define
993 %  this method to take two target pixels (one low and one high) and all the
994 %  pixels of an image which are lying between these two pixels are made
995 %  transparent.
996 %
997 %  The format of the TransparentPaintImageChroma method is:
998 %
999 %      MagickBooleanType TransparentPaintImageChroma(Image *image,
1000 %        const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1001 %        const MagickBooleanType invert,ExceptionInfo *exception)
1002 %
1003 %  A description of each parameter follows:
1004 %
1005 %    o image: the image.
1006 %
1007 %    o low: the low target color.
1008 %
1009 %    o high: the high target color.
1010 %
1011 %    o opacity: the replacement opacity value.
1012 %
1013 %    o invert: paint any pixel that does not match the target color.
1014 %
1015 %    o exception: return any errors or warnings in this structure.
1016 %
1017 */
1018 MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
1019   const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1020   const MagickBooleanType invert,ExceptionInfo *exception)
1021 {
1022 #define TransparentPaintImageTag  "Transparent/Image"
1023
1024   CacheView
1025     *image_view;
1026
1027   MagickBooleanType
1028     status;
1029
1030   MagickOffsetType
1031     progress;
1032
1033   ssize_t
1034     y;
1035
1036   assert(image != (Image *) NULL);
1037   assert(image->signature == MagickSignature);
1038   assert(high != (PixelInfo *) NULL);
1039   assert(low != (PixelInfo *) NULL);
1040   if (image->debug != MagickFalse)
1041     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1042   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1043     return(MagickFalse);
1044   if (image->alpha_trait != BlendPixelTrait)
1045     (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1046   /*
1047     Make image color transparent.
1048   */
1049   status=MagickTrue;
1050   progress=0;
1051   image_view=AcquireAuthenticCacheView(image,exception);
1052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1053   #pragma omp parallel for schedule(static,4) shared(progress,status) \
1054     magick_threads(image,image,image->rows,1)
1055 #endif
1056   for (y=0; y < (ssize_t) image->rows; y++)
1057   {
1058     MagickBooleanType
1059       match;
1060
1061     PixelInfo
1062       pixel;
1063
1064     register Quantum
1065       *restrict q;
1066
1067     register ssize_t
1068       x;
1069
1070     if (status == MagickFalse)
1071       continue;
1072     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1073     if (q == (Quantum *) NULL)
1074       {
1075         status=MagickFalse;
1076         continue;
1077       }
1078     GetPixelInfo(image,&pixel);
1079     for (x=0; x < (ssize_t) image->columns; x++)
1080     {
1081       GetPixelInfoPixel(image,q,&pixel);
1082       match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1083         (pixel.green >= low->green) && (pixel.green <= high->green) &&
1084         (pixel.blue  >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1085         MagickFalse;
1086       if (match != invert)
1087         SetPixelAlpha(image,opacity,q);
1088       q+=GetPixelChannels(image);
1089     }
1090     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1091       status=MagickFalse;
1092     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1093       {
1094         MagickBooleanType
1095           proceed;
1096
1097 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1098         #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1099 #endif
1100         proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1101           image->rows);
1102         if (proceed == MagickFalse)
1103           status=MagickFalse;
1104       }
1105   }
1106   image_view=DestroyCacheView(image_view);
1107   return(status);
1108 }