MagickCore 7.1.1-43
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
visual-effects.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% V V IIIII SSSSS U U AAA L %
7% V V I SS U U A A L %
8% V V I SSS U U AAAAA L %
9% V V I SS U U A A L %
10% V IIIII SSSSS UUU A A LLLLL %
11% %
12% EEEEE FFFFF FFFFF EEEEE CCCC TTTTT SSSSS %
13% E F F E C T SS %
14% EEE FFF FFF EEE C T SSS %
15% E F F E C T SS %
16% EEEEE F F EEEEE CCCC T SSSSS %
17% %
18% %
19% MagickCore Image Special Effects Methods %
20% %
21% Software Design %
22% Cristy %
23% October 1996 %
24% %
25% %
26% %
27% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
28% dedicated to making software imaging solutions freely available. %
29% %
30% You may not use this file except in compliance with the License. You may %
31% obtain a copy of the License at %
32% %
33% https://imagemagick.org/script/license.php %
34% %
35% Unless required by applicable law or agreed to in writing, software %
36% distributed under the License is distributed on an "AS IS" BASIS, %
37% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
38% See the License for the specific language governing permissions and %
39% limitations under the License. %
40% %
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42%
43%
44%
45*/
46␌
47/*
48 Include declarations.
49*/
50#include "MagickCore/studio.h"
51#include "MagickCore/accelerate-private.h"
52#include "MagickCore/annotate.h"
53#include "MagickCore/artifact.h"
54#include "MagickCore/attribute.h"
55#include "MagickCore/cache.h"
56#include "MagickCore/cache-private.h"
57#include "MagickCore/cache-view.h"
58#include "MagickCore/channel.h"
59#include "MagickCore/color.h"
60#include "MagickCore/color-private.h"
61#include "MagickCore/colorspace-private.h"
62#include "MagickCore/composite.h"
63#include "MagickCore/decorate.h"
64#include "MagickCore/distort.h"
65#include "MagickCore/draw.h"
66#include "MagickCore/effect.h"
67#include "MagickCore/enhance.h"
68#include "MagickCore/exception.h"
69#include "MagickCore/exception-private.h"
70#include "MagickCore/gem.h"
71#include "MagickCore/gem-private.h"
72#include "MagickCore/geometry.h"
73#include "MagickCore/layer.h"
74#include "MagickCore/list.h"
75#include "MagickCore/log.h"
76#include "MagickCore/image.h"
77#include "MagickCore/image-private.h"
78#include "MagickCore/magick.h"
79#include "MagickCore/memory_.h"
80#include "MagickCore/memory-private.h"
81#include "MagickCore/monitor.h"
82#include "MagickCore/monitor-private.h"
83#include "MagickCore/option.h"
84#include "MagickCore/pixel.h"
85#include "MagickCore/pixel-accessor.h"
86#include "MagickCore/property.h"
87#include "MagickCore/quantum.h"
88#include "MagickCore/quantum-private.h"
89#include "MagickCore/random_.h"
90#include "MagickCore/random-private.h"
91#include "MagickCore/resample.h"
92#include "MagickCore/resample-private.h"
93#include "MagickCore/resize.h"
94#include "MagickCore/resource_.h"
95#include "MagickCore/splay-tree.h"
96#include "MagickCore/statistic.h"
97#include "MagickCore/string_.h"
98#include "MagickCore/string-private.h"
99#include "MagickCore/thread-private.h"
100#include "MagickCore/threshold.h"
101#include "MagickCore/transform.h"
102#include "MagickCore/transform-private.h"
103#include "MagickCore/utility.h"
104#include "MagickCore/visual-effects.h"
105␌
106/*
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108% %
109% %
110% %
111% A d d N o i s e I m a g e %
112% %
113% %
114% %
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116%
117% AddNoiseImage() adds random noise to the image.
118%
119% The format of the AddNoiseImage method is:
120%
121% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
122% const double attenuate,ExceptionInfo *exception)
123%
124% A description of each parameter follows:
125%
126% o image: the image.
127%
128% o channel: the channel type.
129%
130% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
131% Impulse, Laplacian, or Poisson.
132%
133% o attenuate: attenuate the random distribution.
134%
135% o exception: return any errors or warnings in this structure.
136%
137*/
138MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
139 const double attenuate,ExceptionInfo *exception)
140{
141#define AddNoiseImageTag "AddNoise/Image"
142
144 *image_view,
145 *noise_view;
146
147 Image
148 *noise_image;
149
150 MagickBooleanType
151 status;
152
153 MagickOffsetType
154 progress;
155
157 **magick_restrict random_info;
158
159 ssize_t
160 y;
161
162#if defined(MAGICKCORE_OPENMP_SUPPORT)
163 unsigned long
164 key;
165#endif
166
167 /*
168 Initialize noise image attributes.
169 */
170 assert(image != (const Image *) NULL);
171 assert(image->signature == MagickCoreSignature);
172 assert(exception != (ExceptionInfo *) NULL);
173 assert(exception->signature == MagickCoreSignature);
174 if (IsEventLogging() != MagickFalse)
175 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
176 noise_image=CloneImage(image,0,0,MagickTrue,exception);
177 if (noise_image == (Image *) NULL)
178 return((Image *) NULL);
179 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
180 {
181 noise_image=DestroyImage(noise_image);
182 return((Image *) NULL);
183 }
184 /*
185 Add noise in each row.
186 */
187 status=MagickTrue;
188 progress=0;
189 random_info=AcquireRandomInfoTLS();
190 image_view=AcquireVirtualCacheView(image,exception);
191 noise_view=AcquireAuthenticCacheView(noise_image,exception);
192#if defined(MAGICKCORE_OPENMP_SUPPORT)
193 key=GetRandomSecretKey(random_info[0]);
194 #pragma omp parallel for schedule(static) shared(progress,status) \
195 magick_number_threads(image,noise_image,image->rows,key == ~0UL ? 0 : 2)
196#endif
197 for (y=0; y < (ssize_t) image->rows; y++)
198 {
199 const int
200 id = GetOpenMPThreadId();
201
202 MagickBooleanType
203 sync;
204
205 const Quantum
206 *magick_restrict p;
207
208 ssize_t
209 x;
210
211 Quantum
212 *magick_restrict q;
213
214 if (status == MagickFalse)
215 continue;
216 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
217 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
218 exception);
219 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
220 {
221 status=MagickFalse;
222 continue;
223 }
224 for (x=0; x < (ssize_t) image->columns; x++)
225 {
226 ssize_t
227 i;
228
229 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
230 {
231 PixelChannel channel = GetPixelChannelChannel(image,i);
232 PixelTrait traits = GetPixelChannelTraits(image,channel);
233 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
234 if ((traits == UndefinedPixelTrait) ||
235 (noise_traits == UndefinedPixelTrait))
236 continue;
237 if ((noise_traits & CopyPixelTrait) != 0)
238 {
239 SetPixelChannel(noise_image,channel,p[i],q);
240 continue;
241 }
242 SetPixelChannel(noise_image,channel,ClampToQuantum(
243 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
244 q);
245 }
246 p+=(ptrdiff_t) GetPixelChannels(image);
247 q+=(ptrdiff_t) GetPixelChannels(noise_image);
248 }
249 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
250 if (sync == MagickFalse)
251 status=MagickFalse;
252 if (image->progress_monitor != (MagickProgressMonitor) NULL)
253 {
254 MagickBooleanType
255 proceed;
256
257#if defined(MAGICKCORE_OPENMP_SUPPORT)
258 #pragma omp atomic
259#endif
260 progress++;
261 proceed=SetImageProgress(image,AddNoiseImageTag,progress,image->rows);
262 if (proceed == MagickFalse)
263 status=MagickFalse;
264 }
265 }
266 noise_view=DestroyCacheView(noise_view);
267 image_view=DestroyCacheView(image_view);
268 random_info=DestroyRandomInfoTLS(random_info);
269 if (status == MagickFalse)
270 noise_image=DestroyImage(noise_image);
271 return(noise_image);
272}
273␌
274/*
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276% %
277% %
278% %
279% B l u e S h i f t I m a g e %
280% %
281% %
282% %
283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284%
285% BlueShiftImage() mutes the colors of the image to simulate a scene at
286% nighttime in the moonlight.
287%
288% The format of the BlueShiftImage method is:
289%
290% Image *BlueShiftImage(const Image *image,const double factor,
291% ExceptionInfo *exception)
292%
293% A description of each parameter follows:
294%
295% o image: the image.
296%
297% o factor: the shift factor.
298%
299% o exception: return any errors or warnings in this structure.
300%
301*/
302MagickExport Image *BlueShiftImage(const Image *image,const double factor,
303 ExceptionInfo *exception)
304{
305#define BlueShiftImageTag "BlueShift/Image"
306
308 *image_view,
309 *shift_view;
310
311 Image
312 *shift_image;
313
314 MagickBooleanType
315 status;
316
317 MagickOffsetType
318 progress;
319
320 ssize_t
321 y;
322
323 /*
324 Allocate blue shift image.
325 */
326 assert(image != (const Image *) NULL);
327 assert(image->signature == MagickCoreSignature);
328 assert(exception != (ExceptionInfo *) NULL);
329 assert(exception->signature == MagickCoreSignature);
330 if (IsEventLogging() != MagickFalse)
331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
332 shift_image=CloneImage(image,0,0,MagickTrue,exception);
333 if (shift_image == (Image *) NULL)
334 return((Image *) NULL);
335 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
336 {
337 shift_image=DestroyImage(shift_image);
338 return((Image *) NULL);
339 }
340 /*
341 Blue-shift DirectClass image.
342 */
343 status=MagickTrue;
344 progress=0;
345 image_view=AcquireVirtualCacheView(image,exception);
346 shift_view=AcquireAuthenticCacheView(shift_image,exception);
347#if defined(MAGICKCORE_OPENMP_SUPPORT)
348 #pragma omp parallel for schedule(static) shared(progress,status) \
349 magick_number_threads(image,shift_image,image->rows,1)
350#endif
351 for (y=0; y < (ssize_t) image->rows; y++)
352 {
353 MagickBooleanType
354 sync;
355
357 pixel;
358
359 Quantum
360 quantum;
361
362 const Quantum
363 *magick_restrict p;
364
365 ssize_t
366 x;
367
368 Quantum
369 *magick_restrict q;
370
371 if (status == MagickFalse)
372 continue;
373 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
374 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
375 exception);
376 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
377 {
378 status=MagickFalse;
379 continue;
380 }
381 for (x=0; x < (ssize_t) image->columns; x++)
382 {
383 quantum=GetPixelRed(image,p);
384 if (GetPixelGreen(image,p) < quantum)
385 quantum=GetPixelGreen(image,p);
386 if (GetPixelBlue(image,p) < quantum)
387 quantum=GetPixelBlue(image,p);
388 pixel.red=0.5*((double) GetPixelRed(image,p)+factor*(double) quantum);
389 pixel.green=0.5*((double) GetPixelGreen(image,p)+factor*(double) quantum);
390 pixel.blue=0.5*((double) GetPixelBlue(image,p)+factor*(double) quantum);
391 quantum=GetPixelRed(image,p);
392 if (GetPixelGreen(image,p) > quantum)
393 quantum=GetPixelGreen(image,p);
394 if (GetPixelBlue(image,p) > quantum)
395 quantum=GetPixelBlue(image,p);
396 pixel.red=0.5*(pixel.red+factor*(double) quantum);
397 pixel.green=0.5*(pixel.green+factor*(double) quantum);
398 pixel.blue=0.5*(pixel.blue+factor*(double) quantum);
399 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
400 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
401 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
402 p+=(ptrdiff_t) GetPixelChannels(image);
403 q+=(ptrdiff_t) GetPixelChannels(shift_image);
404 }
405 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
406 if (sync == MagickFalse)
407 status=MagickFalse;
408 if (image->progress_monitor != (MagickProgressMonitor) NULL)
409 {
410 MagickBooleanType
411 proceed;
412
413#if defined(MAGICKCORE_OPENMP_SUPPORT)
414 #pragma omp atomic
415#endif
416 progress++;
417 proceed=SetImageProgress(image,BlueShiftImageTag,progress,image->rows);
418 if (proceed == MagickFalse)
419 status=MagickFalse;
420 }
421 }
422 image_view=DestroyCacheView(image_view);
423 shift_view=DestroyCacheView(shift_view);
424 if (status == MagickFalse)
425 shift_image=DestroyImage(shift_image);
426 return(shift_image);
427}
428␌
429/*
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431% %
432% %
433% %
434% C h a r c o a l I m a g e %
435% %
436% %
437% %
438%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439%
440% CharcoalImage() creates a new image that is a copy of an existing one with
441% the edge highlighted. It allocates the memory necessary for the new Image
442% structure and returns a pointer to the new image.
443%
444% The format of the CharcoalImage method is:
445%
446% Image *CharcoalImage(const Image *image,const double radius,
447% const double sigma,ExceptionInfo *exception)
448%
449% A description of each parameter follows:
450%
451% o image: the image.
452%
453% o radius: the radius of the pixel neighborhood.
454%
455% o sigma: the standard deviation of the Gaussian, in pixels.
456%
457% o exception: return any errors or warnings in this structure.
458%
459*/
460MagickExport Image *CharcoalImage(const Image *image,const double radius,
461 const double sigma,ExceptionInfo *exception)
462{
463 Image
464 *charcoal_image,
465 *edge_image;
466
467 MagickBooleanType
468 status;
469
470 assert(image != (Image *) NULL);
471 assert(image->signature == MagickCoreSignature);
472 assert(exception != (ExceptionInfo *) NULL);
473 assert(exception->signature == MagickCoreSignature);
474 if (IsEventLogging() != MagickFalse)
475 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
476 edge_image=EdgeImage(image,radius,exception);
477 if (edge_image == (Image *) NULL)
478 return((Image *) NULL);
479 edge_image->alpha_trait=UndefinedPixelTrait;
480 charcoal_image=(Image *) NULL;
481 status=ClampImage(edge_image,exception);
482 if (status != MagickFalse)
483 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
484 edge_image=DestroyImage(edge_image);
485 if (charcoal_image == (Image *) NULL)
486 return((Image *) NULL);
487 status=NormalizeImage(charcoal_image,exception);
488 if (status != MagickFalse)
489 status=NegateImage(charcoal_image,MagickFalse,exception);
490 if (status != MagickFalse)
491 status=GrayscaleImage(charcoal_image,image->intensity,exception);
492 if (status == MagickFalse)
493 charcoal_image=DestroyImage(charcoal_image);
494 return(charcoal_image);
495}
496␌
497/*
498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499% %
500% %
501% %
502% C o l o r i z e I m a g e %
503% %
504% %
505% %
506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507%
508% ColorizeImage() blends the fill color with each pixel in the image.
509% A percentage blend is specified with opacity. Control the application
510% of different color components by specifying a different percentage for
511% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
512%
513% The format of the ColorizeImage method is:
514%
515% Image *ColorizeImage(const Image *image,const char *blend,
516% const PixelInfo *colorize,ExceptionInfo *exception)
517%
518% A description of each parameter follows:
519%
520% o image: the image.
521%
522% o blend: A character string indicating the level of blending as a
523% percentage.
524%
525% o colorize: A color value.
526%
527% o exception: return any errors or warnings in this structure.
528%
529*/
530MagickExport Image *ColorizeImage(const Image *image,const char *blend,
531 const PixelInfo *colorize,ExceptionInfo *exception)
532{
533#define ColorizeImageTag "Colorize/Image"
534#define Colorize(pixel,blend_percentage,colorize) \
535 ((((double) pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
536
538 *image_view;
539
541 geometry_info;
542
543 Image
544 *colorize_image;
545
546 MagickBooleanType
547 status;
548
549 MagickOffsetType
550 progress;
551
552 MagickStatusType
553 flags;
554
556 blend_percentage;
557
558 ssize_t
559 y;
560
561 /*
562 Allocate colorized image.
563 */
564 assert(image != (const Image *) NULL);
565 assert(image->signature == MagickCoreSignature);
566 assert(exception != (ExceptionInfo *) NULL);
567 assert(exception->signature == MagickCoreSignature);
568 if (IsEventLogging() != MagickFalse)
569 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
570 colorize_image=CloneImage(image,0,0,MagickTrue,exception);
571 if (colorize_image == (Image *) NULL)
572 return((Image *) NULL);
573 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
574 {
575 colorize_image=DestroyImage(colorize_image);
576 return((Image *) NULL);
577 }
578 if ((IsGrayColorspace(colorize_image->colorspace) != MagickFalse) ||
579 (IsPixelInfoGray(colorize) != MagickFalse))
580 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
581 if ((colorize_image->alpha_trait == UndefinedPixelTrait) &&
582 (colorize->alpha_trait != UndefinedPixelTrait))
583 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
584 if (blend == (const char *) NULL)
585 return(colorize_image);
586 GetPixelInfo(colorize_image,&blend_percentage);
587 flags=ParseGeometry(blend,&geometry_info);
588 blend_percentage.red=geometry_info.rho;
589 blend_percentage.green=geometry_info.rho;
590 blend_percentage.blue=geometry_info.rho;
591 blend_percentage.black=geometry_info.rho;
592 blend_percentage.alpha=(MagickRealType) TransparentAlpha;
593 if ((flags & SigmaValue) != 0)
594 blend_percentage.green=geometry_info.sigma;
595 if ((flags & XiValue) != 0)
596 blend_percentage.blue=geometry_info.xi;
597 if ((flags & PsiValue) != 0)
598 blend_percentage.alpha=geometry_info.psi;
599 if (blend_percentage.colorspace == CMYKColorspace)
600 {
601 if ((flags & PsiValue) != 0)
602 blend_percentage.black=geometry_info.psi;
603 if ((flags & ChiValue) != 0)
604 blend_percentage.alpha=geometry_info.chi;
605 }
606 /*
607 Colorize DirectClass image.
608 */
609 status=MagickTrue;
610 progress=0;
611 image_view=AcquireAuthenticCacheView(colorize_image,exception);
612#if defined(MAGICKCORE_OPENMP_SUPPORT)
613 #pragma omp parallel for schedule(static) shared(progress,status) \
614 magick_number_threads(colorize_image,colorize_image,colorize_image->rows,2)
615#endif
616 for (y=0; y < (ssize_t) colorize_image->rows; y++)
617 {
618 MagickBooleanType
619 sync;
620
621 Quantum
622 *magick_restrict q;
623
624 ssize_t
625 x;
626
627 if (status == MagickFalse)
628 continue;
629 q=GetCacheViewAuthenticPixels(image_view,0,y,colorize_image->columns,1,
630 exception);
631 if (q == (Quantum *) NULL)
632 {
633 status=MagickFalse;
634 continue;
635 }
636 for (x=0; x < (ssize_t) colorize_image->columns; x++)
637 {
638 ssize_t
639 i;
640
641 for (i=0; i < (ssize_t) GetPixelChannels(colorize_image); i++)
642 {
643 PixelTrait traits = GetPixelChannelTraits(colorize_image,
644 (PixelChannel) i);
645 if (traits == UndefinedPixelTrait)
646 continue;
647 if ((traits & CopyPixelTrait) != 0)
648 continue;
649 SetPixelChannel(colorize_image,(PixelChannel) i,ClampToQuantum(
650 Colorize(q[i],GetPixelInfoChannel(&blend_percentage,(PixelChannel) i),
651 GetPixelInfoChannel(colorize,(PixelChannel) i))),q);
652 }
653 q+=(ptrdiff_t) GetPixelChannels(colorize_image);
654 }
655 sync=SyncCacheViewAuthenticPixels(image_view,exception);
656 if (sync == MagickFalse)
657 status=MagickFalse;
658 if (image->progress_monitor != (MagickProgressMonitor) NULL)
659 {
660 MagickBooleanType
661 proceed;
662
663#if defined(MAGICKCORE_OPENMP_SUPPORT)
664 #pragma omp atomic
665#endif
666 progress++;
667 proceed=SetImageProgress(image,ColorizeImageTag,progress,
668 colorize_image->rows);
669 if (proceed == MagickFalse)
670 status=MagickFalse;
671 }
672 }
673 image_view=DestroyCacheView(image_view);
674 if (status == MagickFalse)
675 colorize_image=DestroyImage(colorize_image);
676 return(colorize_image);
677}
678␌
679/*
680%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681% %
682% %
683% %
684% C o l o r M a t r i x I m a g e %
685% %
686% %
687% %
688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689%
690% ColorMatrixImage() applies color transformation to an image. This method
691% permits saturation changes, hue rotation, luminance to alpha, and various
692% other effects. Although variable-sized transformation matrices can be used,
693% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
694% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
695% except offsets are in column 6 rather than 5 (in support of CMYKA images)
696% and offsets are normalized (divide Flash offset by 255).
697%
698% The format of the ColorMatrixImage method is:
699%
700% Image *ColorMatrixImage(const Image *image,
701% const KernelInfo *color_matrix,ExceptionInfo *exception)
702%
703% A description of each parameter follows:
704%
705% o image: the image.
706%
707% o color_matrix: the color matrix.
708%
709% o exception: return any errors or warnings in this structure.
710%
711*/
712/* FUTURE: modify to make use of a MagickMatrix Multiply function
713 That should be provided in "matrix.c"
714 (ASIDE: actually distorts should do this too but currently doesn't)
715*/
716
717MagickExport Image *ColorMatrixImage(const Image *image,
718 const KernelInfo *color_matrix,ExceptionInfo *exception)
719{
720#define ColorMatrixImageTag "ColorMatrix/Image"
721
723 *color_view,
724 *image_view;
725
726 double
727 ColorMatrix[6][6] =
728 {
729 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
730 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
731 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
732 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
733 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
734 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
735 };
736
737 Image
738 *color_image;
739
740 MagickBooleanType
741 status;
742
743 MagickOffsetType
744 progress;
745
746 ssize_t
747 i;
748
749 ssize_t
750 u,
751 v,
752 y;
753
754 /*
755 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
756 */
757 assert(image != (Image *) NULL);
758 assert(image->signature == MagickCoreSignature);
759 assert(exception != (ExceptionInfo *) NULL);
760 assert(exception->signature == MagickCoreSignature);
761 if (IsEventLogging() != MagickFalse)
762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
763 i=0;
764 for (v=0; v < (ssize_t) color_matrix->height; v++)
765 for (u=0; u < (ssize_t) color_matrix->width; u++)
766 {
767 if ((v < 6) && (u < 6))
768 ColorMatrix[v][u]=color_matrix->values[i];
769 i++;
770 }
771 /*
772 Initialize color image.
773 */
774 color_image=CloneImage(image,0,0,MagickTrue,exception);
775 if (color_image == (Image *) NULL)
776 return((Image *) NULL);
777 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
778 {
779 color_image=DestroyImage(color_image);
780 return((Image *) NULL);
781 }
782 if (image->debug != MagickFalse)
783 {
784 char
785 format[MagickPathExtent],
786 *message;
787
788 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
789 " ColorMatrix image with color matrix:");
790 message=AcquireString("");
791 for (v=0; v < 6; v++)
792 {
793 *message='\0';
794 (void) FormatLocaleString(format,MagickPathExtent,"%.20g: ",(double) v);
795 (void) ConcatenateString(&message,format);
796 for (u=0; u < 6; u++)
797 {
798 (void) FormatLocaleString(format,MagickPathExtent,"%+f ",
799 ColorMatrix[v][u]);
800 (void) ConcatenateString(&message,format);
801 }
802 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
803 }
804 message=DestroyString(message);
805 }
806 /*
807 Apply the ColorMatrix to image.
808 */
809 status=MagickTrue;
810 progress=0;
811 image_view=AcquireVirtualCacheView(image,exception);
812 color_view=AcquireAuthenticCacheView(color_image,exception);
813#if defined(MAGICKCORE_OPENMP_SUPPORT)
814 #pragma omp parallel for schedule(static) shared(progress,status) \
815 magick_number_threads(image,color_image,image->rows,1)
816#endif
817 for (y=0; y < (ssize_t) image->rows; y++)
818 {
820 pixel;
821
822 const Quantum
823 *magick_restrict p;
824
825 Quantum
826 *magick_restrict q;
827
828 ssize_t
829 x;
830
831 if (status == MagickFalse)
832 continue;
833 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
834 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
835 exception);
836 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
837 {
838 status=MagickFalse;
839 continue;
840 }
841 GetPixelInfo(image,&pixel);
842 for (x=0; x < (ssize_t) image->columns; x++)
843 {
844 ssize_t
845 h;
846
847 size_t
848 height;
849
850 GetPixelInfoPixel(image,p,&pixel);
851 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
852 for (h=0; h < (ssize_t) height; h++)
853 {
854 double
855 sum;
856
857 sum=ColorMatrix[h][0]*(double) GetPixelRed(image,p)+ColorMatrix[h][1]*
858 (double) GetPixelGreen(image,p)+ColorMatrix[h][2]*(double)
859 GetPixelBlue(image,p);
860 if (image->colorspace == CMYKColorspace)
861 sum+=ColorMatrix[h][3]*(double) GetPixelBlack(image,p);
862 if (image->alpha_trait != UndefinedPixelTrait)
863 sum+=ColorMatrix[h][4]*(double) GetPixelAlpha(image,p);
864 sum+=(double) QuantumRange*ColorMatrix[h][5];
865 switch (h)
866 {
867 case 0: pixel.red=sum; break;
868 case 1: pixel.green=sum; break;
869 case 2: pixel.blue=sum; break;
870 case 3: pixel.black=sum; break;
871 case 4: pixel.alpha=sum; break;
872 default: break;
873 }
874 }
875 SetPixelViaPixelInfo(color_image,&pixel,q);
876 p+=(ptrdiff_t) GetPixelChannels(image);
877 q+=(ptrdiff_t) GetPixelChannels(color_image);
878 }
879 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
880 status=MagickFalse;
881 if (image->progress_monitor != (MagickProgressMonitor) NULL)
882 {
883 MagickBooleanType
884 proceed;
885
886#if defined(MAGICKCORE_OPENMP_SUPPORT)
887 #pragma omp atomic
888#endif
889 progress++;
890 proceed=SetImageProgress(image,ColorMatrixImageTag,progress,
891 image->rows);
892 if (proceed == MagickFalse)
893 status=MagickFalse;
894 }
895 }
896 color_view=DestroyCacheView(color_view);
897 image_view=DestroyCacheView(image_view);
898 if (status == MagickFalse)
899 color_image=DestroyImage(color_image);
900 return(color_image);
901}
902␌
903/*
904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905% %
906% %
907% %
908% I m p l o d e I m a g e %
909% %
910% %
911% %
912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913%
914% ImplodeImage() creates a new image that is a copy of an existing
915% one with the image pixels "implode" by the specified percentage. It
916% allocates the memory necessary for the new Image structure and returns a
917% pointer to the new image.
918%
919% The format of the ImplodeImage method is:
920%
921% Image *ImplodeImage(const Image *image,const double amount,
922% const PixelInterpolateMethod method,ExceptionInfo *exception)
923%
924% A description of each parameter follows:
925%
926% o implode_image: Method ImplodeImage returns a pointer to the image
927% after it is implode. A null image is returned if there is a memory
928% shortage.
929%
930% o image: the image.
931%
932% o amount: Define the extent of the implosion.
933%
934% o method: the pixel interpolation method.
935%
936% o exception: return any errors or warnings in this structure.
937%
938*/
939MagickExport Image *ImplodeImage(const Image *image,const double amount,
940 const PixelInterpolateMethod method,ExceptionInfo *exception)
941{
942#define ImplodeImageTag "Implode/Image"
943
945 *canvas_view,
946 *implode_view,
947 *interpolate_view;
948
949 double
950 radius;
951
952 Image
953 *canvas_image,
954 *implode_image;
955
956 MagickBooleanType
957 status;
958
959 MagickOffsetType
960 progress;
961
963 center,
964 scale;
965
966 ssize_t
967 y;
968
969 /*
970 Initialize implode image attributes.
971 */
972 assert(image != (Image *) NULL);
973 assert(image->signature == MagickCoreSignature);
974 assert(exception != (ExceptionInfo *) NULL);
975 assert(exception->signature == MagickCoreSignature);
976 if (IsEventLogging() != MagickFalse)
977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
978 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
979 if (canvas_image == (Image *) NULL)
980 return((Image *) NULL);
981 if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
982 (canvas_image->background_color.alpha != (double) OpaqueAlpha))
983 (void) SetImageAlphaChannel(canvas_image,OpaqueAlphaChannel,exception);
984 implode_image=CloneImage(canvas_image,0,0,MagickTrue,exception);
985 if (implode_image == (Image *) NULL)
986 {
987 canvas_image=DestroyImage(canvas_image);
988 return((Image *) NULL);
989 }
990 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
991 {
992 canvas_image=DestroyImage(canvas_image);
993 implode_image=DestroyImage(implode_image);
994 return((Image *) NULL);
995 }
996 /*
997 Compute scaling factor.
998 */
999 scale.x=1.0;
1000 scale.y=1.0;
1001 center.x=0.5*canvas_image->columns;
1002 center.y=0.5*canvas_image->rows;
1003 radius=center.x;
1004 if (canvas_image->columns > canvas_image->rows)
1005 scale.y=(double) canvas_image->columns*PerceptibleReciprocal((double)
1006 canvas_image->rows);
1007 else
1008 if (canvas_image->columns < canvas_image->rows)
1009 {
1010 scale.x=(double) canvas_image->rows*PerceptibleReciprocal((double)
1011 canvas_image->columns);
1012 radius=center.y;
1013 }
1014 /*
1015 Implode image.
1016 */
1017 status=MagickTrue;
1018 progress=0;
1019 canvas_view=AcquireVirtualCacheView(canvas_image,exception);
1020 interpolate_view=AcquireVirtualCacheView(canvas_image,exception);
1021 implode_view=AcquireAuthenticCacheView(implode_image,exception);
1022#if defined(MAGICKCORE_OPENMP_SUPPORT)
1023 #pragma omp parallel for schedule(static) shared(progress,status) \
1024 magick_number_threads(canvas_image,implode_image,canvas_image->rows,1)
1025#endif
1026 for (y=0; y < (ssize_t) canvas_image->rows; y++)
1027 {
1028 const Quantum
1029 *magick_restrict p;
1030
1031 double
1032 distance;
1033
1034 PointInfo
1035 delta;
1036
1037 ssize_t
1038 x;
1039
1040 Quantum
1041 *magick_restrict q;
1042
1043 if (status == MagickFalse)
1044 continue;
1045 p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas_image->columns,1,
1046 exception);
1047 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
1048 exception);
1049 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1050 {
1051 status=MagickFalse;
1052 continue;
1053 }
1054 delta.y=scale.y*((double) y-center.y);
1055 for (x=0; x < (ssize_t) canvas_image->columns; x++)
1056 {
1057 ssize_t
1058 i;
1059
1060 /*
1061 Determine if the pixel is within an ellipse.
1062 */
1063 delta.x=scale.x*((double) x-center.x);
1064 distance=delta.x*delta.x+delta.y*delta.y;
1065 if (distance >= (radius*radius))
1066 for (i=0; i < (ssize_t) GetPixelChannels(canvas_image); i++)
1067 {
1068 PixelChannel channel = GetPixelChannelChannel(canvas_image,i);
1069 PixelTrait traits = GetPixelChannelTraits(canvas_image,channel);
1070 PixelTrait implode_traits = GetPixelChannelTraits(implode_image,
1071 channel);
1072 if ((traits == UndefinedPixelTrait) ||
1073 (implode_traits == UndefinedPixelTrait))
1074 continue;
1075 SetPixelChannel(implode_image,channel,p[i],q);
1076 }
1077 else
1078 {
1079 double
1080 factor;
1081
1082 PointInfo
1083 offset;
1084
1085 /*
1086 Implode the pixel.
1087 */
1088 factor=1.0;
1089 if (distance > 0.0)
1090 factor=pow(sin(MagickPI*sqrt(distance)*
1091 PerceptibleReciprocal(radius)/2.0),-amount);
1092 offset.x=factor*delta.x*PerceptibleReciprocal(scale.x)+center.x;
1093 offset.y=factor*delta.y*PerceptibleReciprocal(scale.y)+center.y;
1094 if ((IsValidPixelOffset(offset.x,image->columns) != MagickFalse) &&
1095 (IsValidPixelOffset(offset.y,image->rows) != MagickFalse))
1096 status=InterpolatePixelChannels(canvas_image,interpolate_view,
1097 implode_image,method,offset.x,offset.y,q,exception);
1098 if (status == MagickFalse)
1099 break;
1100 }
1101 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
1102 q+=(ptrdiff_t) GetPixelChannels(implode_image);
1103 }
1104 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
1105 status=MagickFalse;
1106 if (canvas_image->progress_monitor != (MagickProgressMonitor) NULL)
1107 {
1108 MagickBooleanType
1109 proceed;
1110
1111#if defined(MAGICKCORE_OPENMP_SUPPORT)
1112 #pragma omp atomic
1113#endif
1114 progress++;
1115 proceed=SetImageProgress(canvas_image,ImplodeImageTag,progress,
1116 canvas_image->rows);
1117 if (proceed == MagickFalse)
1118 status=MagickFalse;
1119 }
1120 }
1121 implode_view=DestroyCacheView(implode_view);
1122 interpolate_view=DestroyCacheView(interpolate_view);
1123 canvas_view=DestroyCacheView(canvas_view);
1124 canvas_image=DestroyImage(canvas_image);
1125 if (status == MagickFalse)
1126 implode_image=DestroyImage(implode_image);
1127 return(implode_image);
1128}
1129␌
1130/*
1131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1132% %
1133% %
1134% %
1135% M o r p h I m a g e s %
1136% %
1137% %
1138% %
1139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1140%
1141% The MorphImages() method requires a minimum of two images. The first
1142% image is transformed into the second by a number of intervening images
1143% as specified by frames.
1144%
1145% The format of the MorphImage method is:
1146%
1147% Image *MorphImages(const Image *image,const size_t number_frames,
1148% ExceptionInfo *exception)
1149%
1150% A description of each parameter follows:
1151%
1152% o image: the image.
1153%
1154% o number_frames: Define the number of in-between image to generate.
1155% The more in-between frames, the smoother the morph.
1156%
1157% o exception: return any errors or warnings in this structure.
1158%
1159*/
1160MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
1161 ExceptionInfo *exception)
1162{
1163#define MorphImageTag "Morph/Image"
1164
1165 double
1166 alpha,
1167 beta;
1168
1169 Image
1170 *morph_image,
1171 *morph_images;
1172
1173 MagickBooleanType
1174 status;
1175
1176 MagickOffsetType
1177 scene;
1178
1179 const Image
1180 *next;
1181
1182 ssize_t
1183 n;
1184
1185 ssize_t
1186 y;
1187
1188 /*
1189 Clone first frame in sequence.
1190 */
1191 assert(image != (Image *) NULL);
1192 assert(image->signature == MagickCoreSignature);
1193 assert(exception != (ExceptionInfo *) NULL);
1194 assert(exception->signature == MagickCoreSignature);
1195 if (IsEventLogging() != MagickFalse)
1196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1197 morph_images=CloneImage(image,0,0,MagickTrue,exception);
1198 if (morph_images == (Image *) NULL)
1199 return((Image *) NULL);
1200 if (GetNextImageInList(image) == (Image *) NULL)
1201 {
1202 /*
1203 Morph single image.
1204 */
1205 for (n=1; n < (ssize_t) number_frames; n++)
1206 {
1207 morph_image=CloneImage(image,0,0,MagickTrue,exception);
1208 if (morph_image == (Image *) NULL)
1209 {
1210 morph_images=DestroyImageList(morph_images);
1211 return((Image *) NULL);
1212 }
1213 AppendImageToList(&morph_images,morph_image);
1214 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1215 {
1216 MagickBooleanType
1217 proceed;
1218
1219 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) n,
1220 number_frames);
1221 if (proceed == MagickFalse)
1222 status=MagickFalse;
1223 }
1224 }
1225 return(GetFirstImageInList(morph_images));
1226 }
1227 /*
1228 Morph image sequence.
1229 */
1230 status=MagickTrue;
1231 scene=0;
1232 next=image;
1233 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
1234 {
1235 for (n=0; n < (ssize_t) number_frames; n++)
1236 {
1237 CacheView
1238 *image_view,
1239 *morph_view;
1240
1241 beta=(double) (n+1.0)/(double) (number_frames+1.0);
1242 alpha=1.0-beta;
1243 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
1244 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
1245 GetNextImageInList(next)->rows+0.5),next->filter,exception);
1246 if (morph_image == (Image *) NULL)
1247 {
1248 morph_images=DestroyImageList(morph_images);
1249 return((Image *) NULL);
1250 }
1251 status=SetImageStorageClass(morph_image,DirectClass,exception);
1252 if (status == MagickFalse)
1253 {
1254 morph_image=DestroyImage(morph_image);
1255 return((Image *) NULL);
1256 }
1257 AppendImageToList(&morph_images,morph_image);
1258 morph_images=GetLastImageInList(morph_images);
1259 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
1260 morph_images->rows,GetNextImageInList(next)->filter,exception);
1261 if (morph_image == (Image *) NULL)
1262 {
1263 morph_images=DestroyImageList(morph_images);
1264 return((Image *) NULL);
1265 }
1266 image_view=AcquireVirtualCacheView(morph_image,exception);
1267 morph_view=AcquireAuthenticCacheView(morph_images,exception);
1268#if defined(MAGICKCORE_OPENMP_SUPPORT)
1269 #pragma omp parallel for schedule(static) shared(status) \
1270 magick_number_threads(morph_image,morph_image,morph_image->rows,2)
1271#endif
1272 for (y=0; y < (ssize_t) morph_images->rows; y++)
1273 {
1274 MagickBooleanType
1275 sync;
1276
1277 const Quantum
1278 *magick_restrict p;
1279
1280 ssize_t
1281 x;
1282
1283 Quantum
1284 *magick_restrict q;
1285
1286 if (status == MagickFalse)
1287 continue;
1288 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
1289 exception);
1290 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
1291 exception);
1292 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1293 {
1294 status=MagickFalse;
1295 continue;
1296 }
1297 for (x=0; x < (ssize_t) morph_images->columns; x++)
1298 {
1299 ssize_t
1300 i;
1301
1302 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
1303 {
1304 PixelChannel channel = GetPixelChannelChannel(morph_image,i);
1305 PixelTrait traits = GetPixelChannelTraits(morph_image,channel);
1306 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
1307 if ((traits == UndefinedPixelTrait) ||
1308 (morph_traits == UndefinedPixelTrait))
1309 continue;
1310 if ((morph_traits & CopyPixelTrait) != 0)
1311 {
1312 SetPixelChannel(morph_image,channel,p[i],q);
1313 continue;
1314 }
1315 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*(double)
1316 GetPixelChannel(morph_images,channel,q)+beta*(double) p[i]),q);
1317 }
1318 p+=(ptrdiff_t) GetPixelChannels(morph_image);
1319 q+=(ptrdiff_t) GetPixelChannels(morph_images);
1320 }
1321 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
1322 if (sync == MagickFalse)
1323 status=MagickFalse;
1324 }
1325 morph_view=DestroyCacheView(morph_view);
1326 image_view=DestroyCacheView(image_view);
1327 morph_image=DestroyImage(morph_image);
1328 }
1329 if (n < (ssize_t) number_frames)
1330 break;
1331 /*
1332 Clone last frame in sequence.
1333 */
1334 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
1335 if (morph_image == (Image *) NULL)
1336 {
1337 morph_images=DestroyImageList(morph_images);
1338 return((Image *) NULL);
1339 }
1340 AppendImageToList(&morph_images,morph_image);
1341 morph_images=GetLastImageInList(morph_images);
1342 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1343 {
1344 MagickBooleanType
1345 proceed;
1346
1347 proceed=SetImageProgress(image,MorphImageTag,scene,
1348 GetImageListLength(image));
1349 if (proceed == MagickFalse)
1350 status=MagickFalse;
1351 }
1352 scene++;
1353 }
1354 if (GetNextImageInList(next) != (Image *) NULL)
1355 {
1356 morph_images=DestroyImageList(morph_images);
1357 return((Image *) NULL);
1358 }
1359 return(GetFirstImageInList(morph_images));
1360}
1361␌
1362/*
1363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364% %
1365% %
1366% %
1367% P l a s m a I m a g e %
1368% %
1369% %
1370% %
1371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372%
1373% PlasmaImage() initializes an image with plasma fractal values. The image
1374% must be initialized with a base color and the random number generator
1375% seeded before this method is called.
1376%
1377% The format of the PlasmaImage method is:
1378%
1379% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
1380% size_t attenuate,size_t depth,ExceptionInfo *exception)
1381%
1382% A description of each parameter follows:
1383%
1384% o image: the image.
1385%
1386% o segment: Define the region to apply plasma fractals values.
1387%
1388% o attenuate: Define the plasma attenuation factor.
1389%
1390% o depth: Limit the plasma recursion depth.
1391%
1392% o exception: return any errors or warnings in this structure.
1393%
1394*/
1395
1396static inline Quantum PlasmaPixel(RandomInfo *magick_restrict random_info,
1397 const double pixel,const double noise)
1398{
1399 MagickRealType
1400 plasma;
1401
1402 plasma=pixel+noise*GetPseudoRandomValue(random_info)-noise/2.0;
1403 return(ClampToQuantum(plasma));
1404}
1405
1406static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
1407 CacheView *u_view,CacheView *v_view,RandomInfo *magick_restrict random_info,
1408 const SegmentInfo *magick_restrict segment,size_t attenuate,size_t depth,
1409 ExceptionInfo *exception)
1410{
1411 double
1412 plasma;
1413
1414 MagickStatusType
1415 status;
1416
1417 const Quantum
1418 *magick_restrict u,
1419 *magick_restrict v;
1420
1421 Quantum
1422 *magick_restrict q;
1423
1424 ssize_t
1425 i;
1426
1427 ssize_t
1428 x,
1429 x_mid,
1430 y,
1431 y_mid;
1432
1433 if ((fabs(segment->x2-segment->x1) < MagickEpsilon) &&
1434 (fabs(segment->y2-segment->y1) < MagickEpsilon))
1435 return(MagickTrue);
1436 if (depth != 0)
1437 {
1439 local_info;
1440
1441 /*
1442 Divide the area into quadrants and recurse.
1443 */
1444 depth--;
1445 attenuate++;
1446 x_mid=CastDoubleToLong(ceil((segment->x1+segment->x2)/2-0.5));
1447 y_mid=CastDoubleToLong(ceil((segment->y1+segment->y2)/2-0.5));
1448 local_info=(*segment);
1449 local_info.x2=(double) x_mid;
1450 local_info.y2=(double) y_mid;
1451 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
1452 &local_info,attenuate,depth,exception);
1453 local_info=(*segment);
1454 local_info.y1=(double) y_mid;
1455 local_info.x2=(double) x_mid;
1456 status&=(MagickStatusType) PlasmaImageProxy(image,image_view,u_view,
1457 v_view,random_info,&local_info,attenuate,depth,exception);
1458 local_info=(*segment);
1459 local_info.x1=(double) x_mid;
1460 local_info.y2=(double) y_mid;
1461 status&=(MagickStatusType) PlasmaImageProxy(image,image_view,u_view,
1462 v_view,random_info,&local_info,attenuate,depth,exception);
1463 local_info=(*segment);
1464 local_info.x1=(double) x_mid;
1465 local_info.y1=(double) y_mid;
1466 status&=(MagickStatusType) PlasmaImageProxy(image,image_view,u_view,
1467 v_view,random_info,&local_info,attenuate,depth,exception);
1468 return(status == 0 ? MagickFalse : MagickTrue);
1469 }
1470 x_mid=CastDoubleToLong(ceil((segment->x1+segment->x2)/2-0.5));
1471 y_mid=CastDoubleToLong(ceil((segment->y1+segment->y2)/2-0.5));
1472 if ((fabs(segment->x1-x_mid) < MagickEpsilon) &&
1473 (fabs(segment->x2-x_mid) < MagickEpsilon) &&
1474 (fabs(segment->y1-y_mid) < MagickEpsilon) &&
1475 (fabs(segment->y2-y_mid) < MagickEpsilon))
1476 return(MagickFalse);
1477 /*
1478 Average pixels and apply plasma.
1479 */
1480 status=MagickTrue;
1481 plasma=(double) QuantumRange/(2.0*attenuate);
1482 if ((fabs(segment->x1-x_mid) >= MagickEpsilon) ||
1483 (fabs(segment->x2-x_mid) >= MagickEpsilon))
1484 {
1485 /*
1486 Left pixel.
1487 */
1488 x=CastDoubleToLong(ceil(segment->x1-0.5));
1489 u=GetCacheViewVirtualPixels(u_view,x,CastDoubleToLong(ceil(
1490 segment->y1-0.5)),1,1,exception);
1491 v=GetCacheViewVirtualPixels(v_view,x,CastDoubleToLong(ceil(
1492 segment->y2-0.5)),1,1,exception);
1493 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
1494 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
1495 (q == (Quantum *) NULL))
1496 return(MagickTrue);
1497 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1498 {
1499 PixelChannel channel = GetPixelChannelChannel(image,i);
1500 PixelTrait traits = GetPixelChannelTraits(image,channel);
1501 if (traits == UndefinedPixelTrait)
1502 continue;
1503 q[i]=PlasmaPixel(random_info,((double) u[i]+(double) v[i])/2.0,plasma);
1504 }
1505 status=SyncCacheViewAuthenticPixels(image_view,exception);
1506 if (fabs(segment->x1-segment->x2) >= MagickEpsilon)
1507 {
1508 /*
1509 Right pixel.
1510 */
1511 x=CastDoubleToLong(ceil(segment->x2-0.5));
1512 u=GetCacheViewVirtualPixels(u_view,x,CastDoubleToLong(ceil(
1513 segment->y1-0.5)),1,1,exception);
1514 v=GetCacheViewVirtualPixels(v_view,x,CastDoubleToLong(ceil(
1515 segment->y2-0.5)),1,1,exception);
1516 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
1517 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
1518 (q == (Quantum *) NULL))
1519 return(MagickFalse);
1520 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1521 {
1522 PixelChannel channel = GetPixelChannelChannel(image,i);
1523 PixelTrait traits = GetPixelChannelTraits(image,channel);
1524 if (traits == UndefinedPixelTrait)
1525 continue;
1526 q[i]=PlasmaPixel(random_info,((double) u[i]+(double) v[i])/2.0,
1527 plasma);
1528 }
1529 status=SyncCacheViewAuthenticPixels(image_view,exception);
1530 }
1531 }
1532 if ((fabs(segment->y1-y_mid) >= MagickEpsilon) ||
1533 (fabs(segment->y2-y_mid) >= MagickEpsilon))
1534 {
1535 if ((fabs(segment->x1-x_mid) >= MagickEpsilon) ||
1536 (fabs(segment->y2-y_mid) >= MagickEpsilon))
1537 {
1538 /*
1539 Bottom pixel.
1540 */
1541 y=CastDoubleToLong(ceil(segment->y2-0.5));
1542 u=GetCacheViewVirtualPixels(u_view,CastDoubleToLong(ceil(
1543 segment->x1-0.5)),y,1,1,exception);
1544 v=GetCacheViewVirtualPixels(v_view,CastDoubleToLong(ceil(
1545 segment->x2-0.5)),y,1,1,exception);
1546 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
1547 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
1548 (q == (Quantum *) NULL))
1549 return(MagickTrue);
1550 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1551 {
1552 PixelChannel channel = GetPixelChannelChannel(image,i);
1553 PixelTrait traits = GetPixelChannelTraits(image,channel);
1554 if (traits == UndefinedPixelTrait)
1555 continue;
1556 q[i]=PlasmaPixel(random_info,((double) u[i]+(double) v[i])/2.0,
1557 plasma);
1558 }
1559 status=SyncCacheViewAuthenticPixels(image_view,exception);
1560 }
1561 if (fabs(segment->y1-segment->y2) >= MagickEpsilon)
1562 {
1563 /*
1564 Top pixel.
1565 */
1566 y=CastDoubleToLong(ceil(segment->y1-0.5));
1567 u=GetCacheViewVirtualPixels(u_view,CastDoubleToLong(ceil(
1568 segment->x1-0.5)),y,1,1,exception);
1569 v=GetCacheViewVirtualPixels(v_view,CastDoubleToLong(ceil(
1570 segment->x2-0.5)),y,1,1,exception);
1571 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
1572 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
1573 (q == (Quantum *) NULL))
1574 return(MagickTrue);
1575 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1576 {
1577 PixelChannel channel = GetPixelChannelChannel(image,i);
1578 PixelTrait traits = GetPixelChannelTraits(image,channel);
1579 if (traits == UndefinedPixelTrait)
1580 continue;
1581 q[i]=PlasmaPixel(random_info,((double) u[i]+(double) v[i])/2.0,
1582 plasma);
1583 }
1584 status=SyncCacheViewAuthenticPixels(image_view,exception);
1585 }
1586 }
1587 if ((fabs(segment->x1-segment->x2) >= MagickEpsilon) ||
1588 (fabs(segment->y1-segment->y2) >= MagickEpsilon))
1589 {
1590 /*
1591 Middle pixel.
1592 */
1593 x=CastDoubleToLong(ceil(segment->x1-0.5));
1594 y=CastDoubleToLong(ceil(segment->y1-0.5));
1595 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
1596 x=CastDoubleToLong(ceil(segment->x2-0.5));
1597 y=CastDoubleToLong(ceil(segment->y2-0.5));
1598 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
1599 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
1600 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
1601 (q == (Quantum *) NULL))
1602 return(MagickTrue);
1603 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1604 {
1605 PixelChannel channel = GetPixelChannelChannel(image,i);
1606 PixelTrait traits = GetPixelChannelTraits(image,channel);
1607 if (traits == UndefinedPixelTrait)
1608 continue;
1609 q[i]=PlasmaPixel(random_info,((double) u[i]+(double) v[i])/2.0,plasma);
1610 }
1611 status=SyncCacheViewAuthenticPixels(image_view,exception);
1612 }
1613 if ((fabs(segment->x2-segment->x1) < 3.0) &&
1614 (fabs(segment->y2-segment->y1) < 3.0))
1615 return(status == 0 ? MagickFalse : MagickTrue);
1616 return(MagickFalse);
1617}
1618
1619MagickExport MagickBooleanType PlasmaImage(Image *image,
1620 const SegmentInfo *segment,size_t attenuate,size_t depth,
1621 ExceptionInfo *exception)
1622{
1623 CacheView
1624 *image_view,
1625 *u_view,
1626 *v_view;
1627
1628 MagickBooleanType
1629 status;
1630
1632 *random_info;
1633
1634 assert(image != (Image *) NULL);
1635 assert(image->signature == MagickCoreSignature);
1636 if (IsEventLogging() != MagickFalse)
1637 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1638 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1639 return(MagickFalse);
1640 image_view=AcquireAuthenticCacheView(image,exception);
1641 u_view=AcquireVirtualCacheView(image,exception);
1642 v_view=AcquireVirtualCacheView(image,exception);
1643 random_info=AcquireRandomInfo();
1644 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
1645 attenuate,depth,exception);
1646 random_info=DestroyRandomInfo(random_info);
1647 v_view=DestroyCacheView(v_view);
1648 u_view=DestroyCacheView(u_view);
1649 image_view=DestroyCacheView(image_view);
1650 return(status);
1651}
1652␌
1653/*
1654%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655% %
1656% %
1657% %
1658% P o l a r o i d I m a g e %
1659% %
1660% %
1661% %
1662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1663%
1664% PolaroidImage() simulates a Polaroid picture.
1665%
1666% The format of the PolaroidImage method is:
1667%
1668% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
1669% const char *caption,const double angle,
1670% const PixelInterpolateMethod method,ExceptionInfo exception)
1671%
1672% A description of each parameter follows:
1673%
1674% o image: the image.
1675%
1676% o draw_info: the draw info.
1677%
1678% o caption: the Polaroid caption.
1679%
1680% o angle: Apply the effect along this angle.
1681%
1682% o method: the pixel interpolation method.
1683%
1684% o exception: return any errors or warnings in this structure.
1685%
1686*/
1687MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
1688 const char *caption,const double angle,const PixelInterpolateMethod method,
1689 ExceptionInfo *exception)
1690{
1691 Image
1692 *bend_image,
1693 *caption_image,
1694 *flop_image,
1695 *picture_image,
1696 *polaroid_image,
1697 *rotate_image,
1698 *trim_image;
1699
1700 size_t
1701 height;
1702
1703 ssize_t
1704 quantum;
1705
1706 /*
1707 Simulate a Polaroid picture.
1708 */
1709 assert(image != (Image *) NULL);
1710 assert(image->signature == MagickCoreSignature);
1711 assert(exception != (ExceptionInfo *) NULL);
1712 assert(exception->signature == MagickCoreSignature);
1713 if (IsEventLogging() != MagickFalse)
1714 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1715 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
1716 image->rows)/25.0,10.0);
1717 height=(size_t) ((ssize_t) image->rows+2*quantum);
1718 caption_image=(Image *) NULL;
1719 if (caption != (const char *) NULL)
1720 {
1721 char
1722 *text;
1723
1724 /*
1725 Generate caption image.
1726 */
1727 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
1728 if (caption_image == (Image *) NULL)
1729 return((Image *) NULL);
1730 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
1731 exception);
1732 if (text != (char *) NULL)
1733 {
1734 char
1735 geometry[MagickPathExtent];
1736
1737 DrawInfo
1738 *annotate_info;
1739
1740 MagickBooleanType
1741 status;
1742
1743 ssize_t
1744 count;
1745
1747 metrics;
1748
1749 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
1750 (void) CloneString(&annotate_info->text,text);
1751 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,
1752 &metrics,&text,exception);
1753 status=SetImageExtent(caption_image,image->columns,(size_t)
1754 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
1755 if (status == MagickFalse)
1756 caption_image=DestroyImage(caption_image);
1757 else
1758 {
1759 caption_image->background_color=image->border_color;
1760 (void) SetImageBackgroundColor(caption_image,exception);
1761 (void) CloneString(&annotate_info->text,text);
1762 (void) FormatLocaleString(geometry,MagickPathExtent,"+0+%.20g",
1763 metrics.ascent);
1764 if (annotate_info->gravity == UndefinedGravity)
1765 (void) CloneString(&annotate_info->geometry,geometry);
1766 (void) AnnotateImage(caption_image,annotate_info,exception);
1767 height+=caption_image->rows;
1768 }
1769 annotate_info=DestroyDrawInfo(annotate_info);
1770 text=DestroyString(text);
1771 }
1772 }
1773 picture_image=CloneImage(image,(size_t) ((ssize_t) image->columns+2*quantum),
1774 height,MagickTrue,exception);
1775 if (picture_image == (Image *) NULL)
1776 {
1777 if (caption_image != (Image *) NULL)
1778 caption_image=DestroyImage(caption_image);
1779 return((Image *) NULL);
1780 }
1781 picture_image->background_color=image->border_color;
1782 (void) SetImageBackgroundColor(picture_image,exception);
1783 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
1784 quantum,exception);
1785 if (caption_image != (Image *) NULL)
1786 {
1787 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
1788 MagickTrue,quantum,((ssize_t) image->rows+3*quantum/2),exception);
1789 caption_image=DestroyImage(caption_image);
1790 }
1791 (void) QueryColorCompliance("none",AllCompliance,
1792 &picture_image->background_color,exception);
1793 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
1794 rotate_image=RotateImage(picture_image,90.0,exception);
1795 picture_image=DestroyImage(picture_image);
1796 if (rotate_image == (Image *) NULL)
1797 return((Image *) NULL);
1798 picture_image=rotate_image;
1799 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
1800 picture_image->columns,method,exception);
1801 picture_image=DestroyImage(picture_image);
1802 if (bend_image == (Image *) NULL)
1803 return((Image *) NULL);
1804 picture_image=bend_image;
1805 rotate_image=RotateImage(picture_image,-90.0,exception);
1806 picture_image=DestroyImage(picture_image);
1807 if (rotate_image == (Image *) NULL)
1808 return((Image *) NULL);
1809 picture_image=rotate_image;
1810 picture_image->background_color=image->background_color;
1811 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
1812 exception);
1813 if (polaroid_image == (Image *) NULL)
1814 {
1815 picture_image=DestroyImage(picture_image);
1816 return(picture_image);
1817 }
1818 flop_image=FlopImage(polaroid_image,exception);
1819 polaroid_image=DestroyImage(polaroid_image);
1820 if (flop_image == (Image *) NULL)
1821 {
1822 picture_image=DestroyImage(picture_image);
1823 return(picture_image);
1824 }
1825 polaroid_image=flop_image;
1826 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
1827 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
1828 picture_image=DestroyImage(picture_image);
1829 (void) QueryColorCompliance("none",AllCompliance,
1830 &polaroid_image->background_color,exception);
1831 rotate_image=RotateImage(polaroid_image,angle,exception);
1832 polaroid_image=DestroyImage(polaroid_image);
1833 if (rotate_image == (Image *) NULL)
1834 return((Image *) NULL);
1835 polaroid_image=rotate_image;
1836 trim_image=TrimImage(polaroid_image,exception);
1837 polaroid_image=DestroyImage(polaroid_image);
1838 if (trim_image == (Image *) NULL)
1839 return((Image *) NULL);
1840 polaroid_image=trim_image;
1841 return(polaroid_image);
1842}
1843␌
1844/*
1845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846% %
1847% %
1848% %
1849% S e p i a T o n e I m a g e %
1850% %
1851% %
1852% %
1853%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1854%
1855% MagickSepiaToneImage() applies a special effect to the image, similar to the
1856% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
1857% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
1858% threshold of 80% is a good starting point for a reasonable tone.
1859%
1860% The format of the SepiaToneImage method is:
1861%
1862% Image *SepiaToneImage(const Image *image,const double threshold,
1863% ExceptionInfo *exception)
1864%
1865% A description of each parameter follows:
1866%
1867% o image: the image.
1868%
1869% o threshold: the tone threshold.
1870%
1871% o exception: return any errors or warnings in this structure.
1872%
1873*/
1874MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
1875 ExceptionInfo *exception)
1876{
1877#define SepiaToneImageTag "SepiaTone/Image"
1878
1879 CacheView
1880 *image_view,
1881 *sepia_view;
1882
1883 Image
1884 *sepia_image;
1885
1886 MagickBooleanType
1887 status;
1888
1889 MagickOffsetType
1890 progress;
1891
1892 ssize_t
1893 y;
1894
1895 /*
1896 Initialize sepia-toned image attributes.
1897 */
1898 assert(image != (const Image *) NULL);
1899 assert(image->signature == MagickCoreSignature);
1900 assert(exception != (ExceptionInfo *) NULL);
1901 assert(exception->signature == MagickCoreSignature);
1902 if (IsEventLogging() != MagickFalse)
1903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1904 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
1905 if (sepia_image == (Image *) NULL)
1906 return((Image *) NULL);
1907 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
1908 {
1909 sepia_image=DestroyImage(sepia_image);
1910 return((Image *) NULL);
1911 }
1912 /*
1913 Tone each row of the image.
1914 */
1915 status=MagickTrue;
1916 progress=0;
1917 image_view=AcquireVirtualCacheView(image,exception);
1918 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
1919#if defined(MAGICKCORE_OPENMP_SUPPORT)
1920 #pragma omp parallel for schedule(static) shared(progress,status) \
1921 magick_number_threads(image,sepia_image,image->rows,1)
1922#endif
1923 for (y=0; y < (ssize_t) image->rows; y++)
1924 {
1925 const Quantum
1926 *magick_restrict p;
1927
1928 ssize_t
1929 x;
1930
1931 Quantum
1932 *magick_restrict q;
1933
1934 if (status == MagickFalse)
1935 continue;
1936 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1937 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
1938 exception);
1939 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1940 {
1941 status=MagickFalse;
1942 continue;
1943 }
1944 for (x=0; x < (ssize_t) image->columns; x++)
1945 {
1946 double
1947 intensity,
1948 tone;
1949
1950 intensity=GetPixelIntensity(image,p);
1951 tone=intensity > threshold ? (double) QuantumRange : intensity+
1952 (double) QuantumRange-threshold;
1953 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
1954 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
1955 intensity+(double) QuantumRange-7.0*threshold/6.0;
1956 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
1957 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
1958 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
1959 tone=threshold/7.0;
1960 if ((double) GetPixelGreen(image,q) < tone)
1961 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
1962 if ((double) GetPixelBlue(image,q) < tone)
1963 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
1964 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
1965 p+=(ptrdiff_t) GetPixelChannels(image);
1966 q+=(ptrdiff_t) GetPixelChannels(sepia_image);
1967 }
1968 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
1969 status=MagickFalse;
1970 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1971 {
1972 MagickBooleanType
1973 proceed;
1974
1975#if defined(MAGICKCORE_OPENMP_SUPPORT)
1976 #pragma omp atomic
1977#endif
1978 progress++;
1979 proceed=SetImageProgress(image,SepiaToneImageTag,progress,image->rows);
1980 if (proceed == MagickFalse)
1981 status=MagickFalse;
1982 }
1983 }
1984 sepia_view=DestroyCacheView(sepia_view);
1985 image_view=DestroyCacheView(image_view);
1986 (void) NormalizeImage(sepia_image,exception);
1987 (void) ContrastImage(sepia_image,MagickTrue,exception);
1988 if (status == MagickFalse)
1989 sepia_image=DestroyImage(sepia_image);
1990 return(sepia_image);
1991}
1992␌
1993/*
1994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995% %
1996% %
1997% %
1998% S h a d o w I m a g e %
1999% %
2000% %
2001% %
2002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2003%
2004% ShadowImage() simulates a shadow from the specified image and returns it.
2005%
2006% The format of the ShadowImage method is:
2007%
2008% Image *ShadowImage(const Image *image,const double alpha,
2009% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
2010% ExceptionInfo *exception)
2011%
2012% A description of each parameter follows:
2013%
2014% o image: the image.
2015%
2016% o alpha: percentage transparency.
2017%
2018% o sigma: the standard deviation of the Gaussian, in pixels.
2019%
2020% o x_offset: the shadow x-offset.
2021%
2022% o y_offset: the shadow y-offset.
2023%
2024% o exception: return any errors or warnings in this structure.
2025%
2026*/
2027MagickExport Image *ShadowImage(const Image *image,const double alpha,
2028 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
2029 ExceptionInfo *exception)
2030{
2031#define ShadowImageTag "Shadow/Image"
2032
2033 CacheView
2034 *image_view;
2035
2036 ChannelType
2037 channel_mask;
2038
2039 Image
2040 *border_image,
2041 *clone_image,
2042 *shadow_image;
2043
2044 MagickBooleanType
2045 status;
2046
2047 PixelInfo
2048 background_color;
2049
2051 border_info;
2052
2053 ssize_t
2054 y;
2055
2056 assert(image != (Image *) NULL);
2057 assert(image->signature == MagickCoreSignature);
2058 assert(exception != (ExceptionInfo *) NULL);
2059 assert(exception->signature == MagickCoreSignature);
2060 if (IsEventLogging() != MagickFalse)
2061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2062 clone_image=CloneImage(image,0,0,MagickTrue,exception);
2063 if (clone_image == (Image *) NULL)
2064 return((Image *) NULL);
2065 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2066 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
2067 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
2068 exception);
2069 border_info.width=CastDoubleToUnsigned(2.0*sigma+0.5);
2070 border_info.height=CastDoubleToUnsigned(2.0*sigma+0.5);
2071 border_info.x=0;
2072 border_info.y=0;
2073 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
2074 exception);
2075 clone_image->alpha_trait=BlendPixelTrait;
2076 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
2077 clone_image=DestroyImage(clone_image);
2078 if (border_image == (Image *) NULL)
2079 return((Image *) NULL);
2080 if (border_image->alpha_trait == UndefinedPixelTrait)
2081 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
2082 /*
2083 Shadow image.
2084 */
2085 status=MagickTrue;
2086 background_color=border_image->background_color;
2087 background_color.alpha_trait=BlendPixelTrait;
2088 image_view=AcquireAuthenticCacheView(border_image,exception);
2089 for (y=0; y < (ssize_t) border_image->rows; y++)
2090 {
2091 Quantum
2092 *magick_restrict q;
2093
2094 ssize_t
2095 x;
2096
2097 if (status == MagickFalse)
2098 continue;
2099 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
2100 exception);
2101 if (q == (Quantum *) NULL)
2102 {
2103 status=MagickFalse;
2104 continue;
2105 }
2106 for (x=0; x < (ssize_t) border_image->columns; x++)
2107 {
2108 if (border_image->alpha_trait != UndefinedPixelTrait)
2109 background_color.alpha=(double) GetPixelAlpha(border_image,q)*alpha/
2110 100.0;
2111 SetPixelViaPixelInfo(border_image,&background_color,q);
2112 q+=(ptrdiff_t) GetPixelChannels(border_image);
2113 }
2114 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2115 status=MagickFalse;
2116 }
2117 image_view=DestroyCacheView(image_view);
2118 if (status == MagickFalse)
2119 {
2120 border_image=DestroyImage(border_image);
2121 return((Image *) NULL);
2122 }
2123 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
2124 shadow_image=BlurImage(border_image,0.0,sigma,exception);
2125 border_image=DestroyImage(border_image);
2126 if (shadow_image == (Image *) NULL)
2127 return((Image *) NULL);
2128 (void) SetPixelChannelMask(shadow_image,channel_mask);
2129 if (shadow_image->page.width == 0)
2130 shadow_image->page.width=shadow_image->columns;
2131 if (shadow_image->page.height == 0)
2132 shadow_image->page.height=shadow_image->rows;
2133 shadow_image->page.width=(size_t) ((ssize_t) shadow_image->page.width+
2134 x_offset-(ssize_t) border_info.width);
2135 shadow_image->page.height=(size_t) ((ssize_t) shadow_image->page.height+
2136 y_offset-(ssize_t) border_info.height);
2137 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
2138 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
2139 return(shadow_image);
2140}
2141␌
2142/*
2143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2144% %
2145% %
2146% %
2147% S k e t c h I m a g e %
2148% %
2149% %
2150% %
2151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2152%
2153% SketchImage() simulates a pencil sketch. We convolve the image with a
2154% Gaussian operator of the given radius and standard deviation (sigma). For
2155% reasonable results, radius should be larger than sigma. Use a radius of 0
2156% and SketchImage() selects a suitable radius for you. Angle gives the angle
2157% of the sketch.
2158%
2159% The format of the SketchImage method is:
2160%
2161% Image *SketchImage(const Image *image,const double radius,
2162% const double sigma,const double angle,ExceptionInfo *exception)
2163%
2164% A description of each parameter follows:
2165%
2166% o image: the image.
2167%
2168% o radius: the radius of the Gaussian, in pixels, not counting the
2169% center pixel.
2170%
2171% o sigma: the standard deviation of the Gaussian, in pixels.
2172%
2173% o angle: apply the effect along this angle.
2174%
2175% o exception: return any errors or warnings in this structure.
2176%
2177*/
2178MagickExport Image *SketchImage(const Image *image,const double radius,
2179 const double sigma,const double angle,ExceptionInfo *exception)
2180{
2181 CacheView
2182 *random_view;
2183
2184 Image
2185 *blend_image,
2186 *blur_image,
2187 *dodge_image,
2188 *random_image,
2189 *sketch_image;
2190
2191 MagickBooleanType
2192 status;
2193
2195 **magick_restrict random_info;
2196
2197 ssize_t
2198 y;
2199
2200#if defined(MAGICKCORE_OPENMP_SUPPORT)
2201 unsigned long
2202 key;
2203#endif
2204
2205 /*
2206 Sketch image.
2207 */
2208 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
2209 MagickTrue,exception);
2210 if (random_image == (Image *) NULL)
2211 return((Image *) NULL);
2212 status=MagickTrue;
2213 random_info=AcquireRandomInfoTLS();
2214 random_view=AcquireAuthenticCacheView(random_image,exception);
2215#if defined(MAGICKCORE_OPENMP_SUPPORT)
2216 key=GetRandomSecretKey(random_info[0]);
2217 #pragma omp parallel for schedule(static) shared(status) \
2218 magick_number_threads(random_image,random_image,random_image->rows,key == ~0UL ? 0 : 2)
2219#endif
2220 for (y=0; y < (ssize_t) random_image->rows; y++)
2221 {
2222 const int
2223 id = GetOpenMPThreadId();
2224
2225 Quantum
2226 *magick_restrict q;
2227
2228 ssize_t
2229 x;
2230
2231 if (status == MagickFalse)
2232 continue;
2233 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
2234 exception);
2235 if (q == (Quantum *) NULL)
2236 {
2237 status=MagickFalse;
2238 continue;
2239 }
2240 for (x=0; x < (ssize_t) random_image->columns; x++)
2241 {
2242 double
2243 value;
2244
2245 ssize_t
2246 i;
2247
2248 value=GetPseudoRandomValue(random_info[id]);
2249 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
2250 {
2251 PixelChannel channel = GetPixelChannelChannel(image,i);
2252 PixelTrait traits = GetPixelChannelTraits(image,channel);
2253 if (traits == UndefinedPixelTrait)
2254 continue;
2255 q[i]=ClampToQuantum((double) QuantumRange*value);
2256 }
2257 q+=(ptrdiff_t) GetPixelChannels(random_image);
2258 }
2259 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
2260 status=MagickFalse;
2261 }
2262 random_view=DestroyCacheView(random_view);
2263 random_info=DestroyRandomInfoTLS(random_info);
2264 if (status == MagickFalse)
2265 {
2266 random_image=DestroyImage(random_image);
2267 return(random_image);
2268 }
2269 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
2270 random_image=DestroyImage(random_image);
2271 if (blur_image == (Image *) NULL)
2272 return((Image *) NULL);
2273 dodge_image=EdgeImage(blur_image,radius,exception);
2274 blur_image=DestroyImage(blur_image);
2275 if (dodge_image == (Image *) NULL)
2276 return((Image *) NULL);
2277 status=ClampImage(dodge_image,exception);
2278 if (status != MagickFalse)
2279 status=NormalizeImage(dodge_image,exception);
2280 if (status != MagickFalse)
2281 status=NegateImage(dodge_image,MagickFalse,exception);
2282 if (status != MagickFalse)
2283 status=TransformImage(&dodge_image,(char *) NULL,"50%",exception);
2284 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
2285 if (sketch_image == (Image *) NULL)
2286 {
2287 dodge_image=DestroyImage(dodge_image);
2288 return((Image *) NULL);
2289 }
2290 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
2291 MagickTrue,0,0,exception);
2292 dodge_image=DestroyImage(dodge_image);
2293 blend_image=CloneImage(image,0,0,MagickTrue,exception);
2294 if (blend_image == (Image *) NULL)
2295 {
2296 sketch_image=DestroyImage(sketch_image);
2297 return((Image *) NULL);
2298 }
2299 if (blend_image->alpha_trait != BlendPixelTrait)
2300 (void) SetImageAlpha(blend_image,TransparentAlpha,exception);
2301 (void) SetImageArtifact(blend_image,"compose:args","20x80");
2302 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
2303 0,0,exception);
2304 blend_image=DestroyImage(blend_image);
2305 return(sketch_image);
2306}
2307␌
2308/*
2309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2310% %
2311% %
2312% %
2313% S o l a r i z e I m a g e %
2314% %
2315% %
2316% %
2317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2318%
2319% SolarizeImage() applies a special effect to the image, similar to the effect
2320% achieved in a photo darkroom by selectively exposing areas of photo
2321% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
2322% measure of the extent of the solarization.
2323%
2324% The format of the SolarizeImage method is:
2325%
2326% MagickBooleanType SolarizeImage(Image *image,const double threshold,
2327% ExceptionInfo *exception)
2328%
2329% A description of each parameter follows:
2330%
2331% o image: the image.
2332%
2333% o threshold: Define the extent of the solarization.
2334%
2335% o exception: return any errors or warnings in this structure.
2336%
2337*/
2338MagickExport MagickBooleanType SolarizeImage(Image *image,
2339 const double threshold,ExceptionInfo *exception)
2340{
2341#define SolarizeImageTag "Solarize/Image"
2342
2343 CacheView
2344 *image_view;
2345
2346 MagickBooleanType
2347 status;
2348
2349 MagickOffsetType
2350 progress;
2351
2352 ssize_t
2353 y;
2354
2355 assert(image != (Image *) NULL);
2356 assert(image->signature == MagickCoreSignature);
2357 if (IsEventLogging() != MagickFalse)
2358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2359 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2360 (void) SetImageColorspace(image,sRGBColorspace,exception);
2361 if (image->storage_class == PseudoClass)
2362 {
2363 ssize_t
2364 i;
2365
2366 /*
2367 Solarize colormap.
2368 */
2369 for (i=0; i < (ssize_t) image->colors; i++)
2370 {
2371 if ((double) image->colormap[i].red > threshold)
2372 image->colormap[i].red=(double) QuantumRange-image->colormap[i].red;
2373 if ((double) image->colormap[i].green > threshold)
2374 image->colormap[i].green=(double) QuantumRange-
2375 image->colormap[i].green;
2376 if ((double) image->colormap[i].blue > threshold)
2377 image->colormap[i].blue=(double) QuantumRange-image->colormap[i].blue;
2378 }
2379 return(SyncImage(image,exception));
2380 }
2381 /*
2382 Solarize image.
2383 */
2384 status=MagickTrue;
2385 progress=0;
2386 image_view=AcquireAuthenticCacheView(image,exception);
2387#if defined(MAGICKCORE_OPENMP_SUPPORT)
2388 #pragma omp parallel for schedule(static) shared(progress,status) \
2389 magick_number_threads(image,image,image->rows,2)
2390#endif
2391 for (y=0; y < (ssize_t) image->rows; y++)
2392 {
2393 ssize_t
2394 x;
2395
2396 Quantum
2397 *magick_restrict q;
2398
2399 if (status == MagickFalse)
2400 continue;
2401 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2402 if (q == (Quantum *) NULL)
2403 {
2404 status=MagickFalse;
2405 continue;
2406 }
2407 for (x=0; x < (ssize_t) image->columns; x++)
2408 {
2409 ssize_t
2410 i;
2411
2412 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2413 {
2414 PixelChannel channel = GetPixelChannelChannel(image,i);
2415 PixelTrait traits = GetPixelChannelTraits(image,channel);
2416 if ((traits & UpdatePixelTrait) == 0)
2417 continue;
2418 if ((double) q[i] > threshold)
2419 q[i]=QuantumRange-q[i];
2420 }
2421 q+=(ptrdiff_t) GetPixelChannels(image);
2422 }
2423 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2424 status=MagickFalse;
2425 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2426 {
2427 MagickBooleanType
2428 proceed;
2429
2430#if defined(MAGICKCORE_OPENMP_SUPPORT)
2431 #pragma omp atomic
2432#endif
2433 progress++;
2434 proceed=SetImageProgress(image,SolarizeImageTag,progress,image->rows);
2435 if (proceed == MagickFalse)
2436 status=MagickFalse;
2437 }
2438 }
2439 image_view=DestroyCacheView(image_view);
2440 return(status);
2441}
2442␌
2443/*
2444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2445% %
2446% %
2447% %
2448% S t e g a n o I m a g e %
2449% %
2450% %
2451% %
2452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2453%
2454% SteganoImage() hides a digital watermark within the image. Recover
2455% the hidden watermark later to prove that the authenticity of an image.
2456% Offset defines the start position within the image to hide the watermark.
2457%
2458% The format of the SteganoImage method is:
2459%
2460% Image *SteganoImage(const Image *image,Image *watermark,
2461% ExceptionInfo *exception)
2462%
2463% A description of each parameter follows:
2464%
2465% o image: the image.
2466%
2467% o watermark: the watermark image.
2468%
2469% o exception: return any errors or warnings in this structure.
2470%
2471*/
2472MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
2473 ExceptionInfo *exception)
2474{
2475#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
2476#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
2477 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
2478#define SteganoImageTag "Stegano/Image"
2479
2480 CacheView
2481 *stegano_view,
2482 *watermark_view;
2483
2484 Image
2485 *stegano_image;
2486
2487 int
2488 c;
2489
2490 MagickBooleanType
2491 status;
2492
2493 PixelInfo
2494 pixel;
2495
2496 Quantum
2497 *q;
2498
2499 ssize_t
2500 x;
2501
2502 size_t
2503 depth,
2504 one;
2505
2506 ssize_t
2507 i,
2508 j,
2509 k,
2510 y;
2511
2512 /*
2513 Initialize steganographic image attributes.
2514 */
2515 assert(image != (const Image *) NULL);
2516 assert(image->signature == MagickCoreSignature);
2517 assert(watermark != (const Image *) NULL);
2518 assert(watermark->signature == MagickCoreSignature);
2519 assert(exception != (ExceptionInfo *) NULL);
2520 assert(exception->signature == MagickCoreSignature);
2521 if (IsEventLogging() != MagickFalse)
2522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2523 one=1UL;
2524 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
2525 if (stegano_image == (Image *) NULL)
2526 return((Image *) NULL);
2527 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
2528 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
2529 {
2530 stegano_image=DestroyImage(stegano_image);
2531 return((Image *) NULL);
2532 }
2533 /*
2534 Hide watermark in low-order bits of image.
2535 */
2536 c=0;
2537 i=0;
2538 j=0;
2539 depth=stegano_image->depth;
2540 k=stegano_image->offset;
2541 status=MagickTrue;
2542 watermark_view=AcquireVirtualCacheView(watermark,exception);
2543 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
2544 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
2545 {
2546 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
2547 {
2548 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
2549 {
2550 ssize_t
2551 offset;
2552
2553 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
2554 exception);
2555 offset=k/(ssize_t) stegano_image->columns;
2556 if (offset >= (ssize_t) stegano_image->rows)
2557 break;
2558 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
2559 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
2560 exception);
2561 if (q == (Quantum *) NULL)
2562 break;
2563 switch (c)
2564 {
2565 case 0:
2566 {
2567 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
2568 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2569 break;
2570 }
2571 case 1:
2572 {
2573 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
2574 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2575 break;
2576 }
2577 case 2:
2578 {
2579 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
2580 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2581 break;
2582 }
2583 }
2584 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
2585 break;
2586 c++;
2587 if (c == 3)
2588 c=0;
2589 k++;
2590 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
2591 k=0;
2592 if (k == stegano_image->offset)
2593 j++;
2594 }
2595 }
2596 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2597 {
2598 MagickBooleanType
2599 proceed;
2600
2601 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
2602 depth-i,depth);
2603 if (proceed == MagickFalse)
2604 status=MagickFalse;
2605 }
2606 }
2607 stegano_view=DestroyCacheView(stegano_view);
2608 watermark_view=DestroyCacheView(watermark_view);
2609 if (status == MagickFalse)
2610 stegano_image=DestroyImage(stegano_image);
2611 return(stegano_image);
2612}
2613␌
2614/*
2615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2616% %
2617% %
2618% %
2619% S t e r e o A n a g l y p h I m a g e %
2620% %
2621% %
2622% %
2623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2624%
2625% StereoAnaglyphImage() combines two images and produces a single image that
2626% is the composite of a left and right image of a stereo pair. Special
2627% red-green stereo glasses are required to view this effect.
2628%
2629% The format of the StereoAnaglyphImage method is:
2630%
2631% Image *StereoImage(const Image *left_image,const Image *right_image,
2632% ExceptionInfo *exception)
2633% Image *StereoAnaglyphImage(const Image *left_image,
2634% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
2635% ExceptionInfo *exception)
2636%
2637% A description of each parameter follows:
2638%
2639% o left_image: the left image.
2640%
2641% o right_image: the right image.
2642%
2643% o exception: return any errors or warnings in this structure.
2644%
2645% o x_offset: amount, in pixels, by which the left image is offset to the
2646% right of the right image.
2647%
2648% o y_offset: amount, in pixels, by which the left image is offset to the
2649% bottom of the right image.
2650%
2651%
2652*/
2653MagickExport Image *StereoImage(const Image *left_image,
2654 const Image *right_image,ExceptionInfo *exception)
2655{
2656 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
2657}
2658
2659MagickExport Image *StereoAnaglyphImage(const Image *left_image,
2660 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
2661 ExceptionInfo *exception)
2662{
2663#define StereoImageTag "Stereo/Image"
2664
2665 const Image
2666 *image;
2667
2668 Image
2669 *stereo_image;
2670
2671 MagickBooleanType
2672 status;
2673
2674 ssize_t
2675 y;
2676
2677 assert(left_image != (const Image *) NULL);
2678 assert(left_image->signature == MagickCoreSignature);
2679 assert(right_image != (const Image *) NULL);
2680 assert(right_image->signature == MagickCoreSignature);
2681 assert(exception != (ExceptionInfo *) NULL);
2682 assert(exception->signature == MagickCoreSignature);
2683 if (IsEventLogging() != MagickFalse)
2684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2685 left_image->filename);
2686 image=left_image;
2687 if ((left_image->columns != right_image->columns) ||
2688 (left_image->rows != right_image->rows))
2689 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
2690 /*
2691 Initialize stereo image attributes.
2692 */
2693 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
2694 MagickTrue,exception);
2695 if (stereo_image == (Image *) NULL)
2696 return((Image *) NULL);
2697 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
2698 {
2699 stereo_image=DestroyImage(stereo_image);
2700 return((Image *) NULL);
2701 }
2702 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
2703 /*
2704 Copy left image to red channel and right image to blue channel.
2705 */
2706 status=MagickTrue;
2707 for (y=0; y < (ssize_t) stereo_image->rows; y++)
2708 {
2709 const Quantum
2710 *magick_restrict p,
2711 *magick_restrict q;
2712
2713 ssize_t
2714 x;
2715
2716 Quantum
2717 *magick_restrict r;
2718
2719 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
2720 exception);
2721 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
2722 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
2723 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
2724 (r == (Quantum *) NULL))
2725 break;
2726 for (x=0; x < (ssize_t) stereo_image->columns; x++)
2727 {
2728 SetPixelRed(stereo_image,GetPixelRed(left_image,p),r);
2729 SetPixelGreen(stereo_image,GetPixelGreen(right_image,q),r);
2730 SetPixelBlue(stereo_image,GetPixelBlue(right_image,q),r);
2731 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
2732 SetPixelAlpha(stereo_image,(GetPixelAlpha(left_image,p)+
2733 GetPixelAlpha(right_image,q))/2,r);
2734 p+=(ptrdiff_t) GetPixelChannels(left_image);
2735 q+=(ptrdiff_t) GetPixelChannels(right_image);
2736 r+=(ptrdiff_t) GetPixelChannels(stereo_image);
2737 }
2738 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
2739 break;
2740 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2741 {
2742 MagickBooleanType
2743 proceed;
2744
2745 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
2746 stereo_image->rows);
2747 if (proceed == MagickFalse)
2748 status=MagickFalse;
2749 }
2750 }
2751 if (status == MagickFalse)
2752 stereo_image=DestroyImage(stereo_image);
2753 return(stereo_image);
2754}
2755␌
2756/*
2757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2758% %
2759% %
2760% %
2761% S w i r l I m a g e %
2762% %
2763% %
2764% %
2765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2766%
2767% SwirlImage() swirls the pixels about the center of the image, where
2768% degrees indicates the sweep of the arc through which each pixel is moved.
2769% You get a more dramatic effect as the degrees move from 1 to 360.
2770%
2771% The format of the SwirlImage method is:
2772%
2773% Image *SwirlImage(const Image *image,double degrees,
2774% const PixelInterpolateMethod method,ExceptionInfo *exception)
2775%
2776% A description of each parameter follows:
2777%
2778% o image: the image.
2779%
2780% o degrees: Define the tightness of the swirling effect.
2781%
2782% o method: the pixel interpolation method.
2783%
2784% o exception: return any errors or warnings in this structure.
2785%
2786*/
2787MagickExport Image *SwirlImage(const Image *image,double degrees,
2788 const PixelInterpolateMethod method,ExceptionInfo *exception)
2789{
2790#define SwirlImageTag "Swirl/Image"
2791
2792 CacheView
2793 *canvas_view,
2794 *interpolate_view,
2795 *swirl_view;
2796
2797 double
2798 radius;
2799
2800 Image
2801 *canvas_image,
2802 *swirl_image;
2803
2804 MagickBooleanType
2805 status;
2806
2807 MagickOffsetType
2808 progress;
2809
2810 PointInfo
2811 center,
2812 scale;
2813
2814 ssize_t
2815 y;
2816
2817 /*
2818 Initialize swirl image attributes.
2819 */
2820 assert(image != (const Image *) NULL);
2821 assert(image->signature == MagickCoreSignature);
2822 assert(exception != (ExceptionInfo *) NULL);
2823 assert(exception->signature == MagickCoreSignature);
2824 if (IsEventLogging() != MagickFalse)
2825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2826 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
2827 if (canvas_image == (Image *) NULL)
2828 return((Image *) NULL);
2829 swirl_image=CloneImage(canvas_image,0,0,MagickTrue,exception);
2830 if (swirl_image == (Image *) NULL)
2831 {
2832 canvas_image=DestroyImage(canvas_image);
2833 return((Image *) NULL);
2834 }
2835 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
2836 {
2837 canvas_image=DestroyImage(canvas_image);
2838 swirl_image=DestroyImage(swirl_image);
2839 return((Image *) NULL);
2840 }
2841 if (swirl_image->background_color.alpha_trait != UndefinedPixelTrait)
2842 (void) SetImageAlphaChannel(swirl_image,OnAlphaChannel,exception);
2843 /*
2844 Compute scaling factor.
2845 */
2846 center.x=(double) canvas_image->columns/2.0;
2847 center.y=(double) canvas_image->rows/2.0;
2848 radius=MagickMax(center.x,center.y);
2849 scale.x=1.0;
2850 scale.y=1.0;
2851 if (canvas_image->columns > canvas_image->rows)
2852 scale.y=(double) canvas_image->columns/(double) canvas_image->rows;
2853 else
2854 if (canvas_image->columns < canvas_image->rows)
2855 scale.x=(double) canvas_image->rows/(double) canvas_image->columns;
2856 degrees=(double) DegreesToRadians(degrees);
2857 /*
2858 Swirl image.
2859 */
2860 status=MagickTrue;
2861 progress=0;
2862 canvas_view=AcquireVirtualCacheView(canvas_image,exception);
2863 interpolate_view=AcquireVirtualCacheView(image,exception);
2864 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
2865#if defined(MAGICKCORE_OPENMP_SUPPORT)
2866 #pragma omp parallel for schedule(static) shared(progress,status) \
2867 magick_number_threads(canvas_image,swirl_image,canvas_image->rows,1)
2868#endif
2869 for (y=0; y < (ssize_t) canvas_image->rows; y++)
2870 {
2871 double
2872 distance;
2873
2874 PointInfo
2875 delta;
2876
2877 const Quantum
2878 *magick_restrict p;
2879
2880 ssize_t
2881 x;
2882
2883 Quantum
2884 *magick_restrict q;
2885
2886 if (status == MagickFalse)
2887 continue;
2888 p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas_image->columns,1,
2889 exception);
2890 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
2891 exception);
2892 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2893 {
2894 status=MagickFalse;
2895 continue;
2896 }
2897 delta.y=scale.y*(double) (y-center.y);
2898 for (x=0; x < (ssize_t) canvas_image->columns; x++)
2899 {
2900 /*
2901 Determine if the pixel is within an ellipse.
2902 */
2903 delta.x=scale.x*(double) (x-center.x);
2904 distance=delta.x*delta.x+delta.y*delta.y;
2905 if (distance >= (radius*radius))
2906 {
2907 ssize_t
2908 i;
2909
2910 for (i=0; i < (ssize_t) GetPixelChannels(canvas_image); i++)
2911 {
2912 PixelChannel channel = GetPixelChannelChannel(canvas_image,i);
2913 PixelTrait traits = GetPixelChannelTraits(canvas_image,channel);
2914 PixelTrait swirl_traits = GetPixelChannelTraits(swirl_image,
2915 channel);
2916 if ((traits == UndefinedPixelTrait) ||
2917 (swirl_traits == UndefinedPixelTrait))
2918 continue;
2919 SetPixelChannel(swirl_image,channel,p[i],q);
2920 }
2921 }
2922 else
2923 {
2924 double
2925 cosine,
2926 factor,
2927 sine;
2928
2929 /*
2930 Swirl the pixel.
2931 */
2932 factor=1.0-sqrt((double) distance)/radius;
2933 sine=sin((double) (degrees*factor*factor));
2934 cosine=cos((double) (degrees*factor*factor));
2935 status=InterpolatePixelChannels(canvas_image,interpolate_view,
2936 swirl_image,method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),
2937 (double) ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,
2938 exception);
2939 if (status == MagickFalse)
2940 break;
2941 }
2942 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
2943 q+=(ptrdiff_t) GetPixelChannels(swirl_image);
2944 }
2945 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
2946 status=MagickFalse;
2947 if (canvas_image->progress_monitor != (MagickProgressMonitor) NULL)
2948 {
2949 MagickBooleanType
2950 proceed;
2951
2952#if defined(MAGICKCORE_OPENMP_SUPPORT)
2953 #pragma omp atomic
2954#endif
2955 progress++;
2956 proceed=SetImageProgress(canvas_image,SwirlImageTag,progress,
2957 canvas_image->rows);
2958 if (proceed == MagickFalse)
2959 status=MagickFalse;
2960 }
2961 }
2962 swirl_view=DestroyCacheView(swirl_view);
2963 interpolate_view=DestroyCacheView(interpolate_view);
2964 canvas_view=DestroyCacheView(canvas_view);
2965 canvas_image=DestroyImage(canvas_image);
2966 if (status == MagickFalse)
2967 swirl_image=DestroyImage(swirl_image);
2968 return(swirl_image);
2969}
2970␌
2971/*
2972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2973% %
2974% %
2975% %
2976% T i n t I m a g e %
2977% %
2978% %
2979% %
2980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2981%
2982% TintImage() applies a color vector to each pixel in the image. The length
2983% of the vector is 0 for black and white and at its maximum for the midtones.
2984% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
2985%
2986% The format of the TintImage method is:
2987%
2988% Image *TintImage(const Image *image,const char *blend,
2989% const PixelInfo *tint,ExceptionInfo *exception)
2990%
2991% A description of each parameter follows:
2992%
2993% o image: the image.
2994%
2995% o blend: A color value used for tinting.
2996%
2997% o tint: A color value used for tinting.
2998%
2999% o exception: return any errors or warnings in this structure.
3000%
3001*/
3002MagickExport Image *TintImage(const Image *image,const char *blend,
3003 const PixelInfo *tint,ExceptionInfo *exception)
3004{
3005#define TintImageTag "Tint/Image"
3006
3007 CacheView
3008 *image_view,
3009 *tint_view;
3010
3011 double
3012 intensity;
3013
3015 geometry_info;
3016
3017 Image
3018 *tint_image;
3019
3020 MagickBooleanType
3021 status;
3022
3023 MagickOffsetType
3024 progress;
3025
3026 PixelInfo
3027 color_vector;
3028
3029 MagickStatusType
3030 flags;
3031
3032 ssize_t
3033 y;
3034
3035 /*
3036 Allocate tint image.
3037 */
3038 assert(image != (const Image *) NULL);
3039 assert(image->signature == MagickCoreSignature);
3040 assert(exception != (ExceptionInfo *) NULL);
3041 assert(exception->signature == MagickCoreSignature);
3042 if (IsEventLogging() != MagickFalse)
3043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3044 tint_image=CloneImage(image,0,0,MagickTrue,exception);
3045 if (tint_image == (Image *) NULL)
3046 return((Image *) NULL);
3047 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
3048 {
3049 tint_image=DestroyImage(tint_image);
3050 return((Image *) NULL);
3051 }
3052 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
3053 (IsPixelInfoGray(tint) == MagickFalse))
3054 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
3055 if (blend == (const char *) NULL)
3056 return(tint_image);
3057 /*
3058 Determine RGB values of the color.
3059 */
3060 GetPixelInfo(image,&color_vector);
3061 flags=ParseGeometry(blend,&geometry_info);
3062 color_vector.red=geometry_info.rho;
3063 color_vector.green=geometry_info.rho;
3064 color_vector.blue=geometry_info.rho;
3065 color_vector.alpha=(MagickRealType) OpaqueAlpha;
3066 if ((flags & SigmaValue) != 0)
3067 color_vector.green=geometry_info.sigma;
3068 if ((flags & XiValue) != 0)
3069 color_vector.blue=geometry_info.xi;
3070 if ((flags & PsiValue) != 0)
3071 color_vector.alpha=geometry_info.psi;
3072 if (image->colorspace == CMYKColorspace)
3073 {
3074 color_vector.black=geometry_info.rho;
3075 if ((flags & PsiValue) != 0)
3076 color_vector.black=geometry_info.psi;
3077 if ((flags & ChiValue) != 0)
3078 color_vector.alpha=geometry_info.chi;
3079 }
3080 intensity=(double) GetPixelInfoIntensity((const Image *) NULL,tint);
3081 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
3082 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
3083 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
3084 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
3085 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
3086 /*
3087 Tint image.
3088 */
3089 status=MagickTrue;
3090 progress=0;
3091 image_view=AcquireVirtualCacheView(image,exception);
3092 tint_view=AcquireAuthenticCacheView(tint_image,exception);
3093#if defined(MAGICKCORE_OPENMP_SUPPORT)
3094 #pragma omp parallel for schedule(static) shared(progress,status) \
3095 magick_number_threads(image,tint_image,image->rows,1)
3096#endif
3097 for (y=0; y < (ssize_t) image->rows; y++)
3098 {
3099 const Quantum
3100 *magick_restrict p;
3101
3102 Quantum
3103 *magick_restrict q;
3104
3105 ssize_t
3106 x;
3107
3108 if (status == MagickFalse)
3109 continue;
3110 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3111 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
3112 exception);
3113 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3114 {
3115 status=MagickFalse;
3116 continue;
3117 }
3118 for (x=0; x < (ssize_t) image->columns; x++)
3119 {
3120 PixelInfo
3121 pixel;
3122
3123 double
3124 weight;
3125
3126 GetPixelInfo(image,&pixel);
3127 weight=QuantumScale*(double) GetPixelRed(image,p)-0.5;
3128 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
3129 (1.0-(4.0*(weight*weight)));
3130 weight=QuantumScale*(double) GetPixelGreen(image,p)-0.5;
3131 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
3132 (1.0-(4.0*(weight*weight)));
3133 weight=QuantumScale*(double) GetPixelBlue(image,p)-0.5;
3134 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
3135 (1.0-(4.0*(weight*weight)));
3136 weight=QuantumScale*(double) GetPixelBlack(image,p)-0.5;
3137 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
3138 (1.0-(4.0*(weight*weight)));
3139 pixel.alpha=(MagickRealType) GetPixelAlpha(image,p);
3140 SetPixelViaPixelInfo(tint_image,&pixel,q);
3141 p+=(ptrdiff_t) GetPixelChannels(image);
3142 q+=(ptrdiff_t) GetPixelChannels(tint_image);
3143 }
3144 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
3145 status=MagickFalse;
3146 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3147 {
3148 MagickBooleanType
3149 proceed;
3150
3151#if defined(MAGICKCORE_OPENMP_SUPPORT)
3152 #pragma omp atomic
3153#endif
3154 progress++;
3155 proceed=SetImageProgress(image,TintImageTag,progress,image->rows);
3156 if (proceed == MagickFalse)
3157 status=MagickFalse;
3158 }
3159 }
3160 tint_view=DestroyCacheView(tint_view);
3161 image_view=DestroyCacheView(image_view);
3162 if (status == MagickFalse)
3163 tint_image=DestroyImage(tint_image);
3164 return(tint_image);
3165}
3166␌
3167/*
3168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3169% %
3170% %
3171% %
3172% V i g n e t t e I m a g e %
3173% %
3174% %
3175% %
3176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3177%
3178% VignetteImage() softens the edges of the image in vignette style.
3179%
3180% The format of the VignetteImage method is:
3181%
3182% Image *VignetteImage(const Image *image,const double radius,
3183% const double sigma,const ssize_t x,const ssize_t y,
3184% ExceptionInfo *exception)
3185%
3186% A description of each parameter follows:
3187%
3188% o image: the image.
3189%
3190% o radius: the radius of the pixel neighborhood.
3191%
3192% o sigma: the standard deviation of the Gaussian, in pixels.
3193%
3194% o x, y: Define the x and y ellipse offset.
3195%
3196% o exception: return any errors or warnings in this structure.
3197%
3198*/
3199MagickExport Image *VignetteImage(const Image *image,const double radius,
3200 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
3201{
3202 char
3203 ellipse[MagickPathExtent];
3204
3205 DrawInfo
3206 *draw_info;
3207
3208 Image
3209 *canvas,
3210 *blur_image,
3211 *oval_image,
3212 *vignette_image;
3213
3214 assert(image != (Image *) NULL);
3215 assert(image->signature == MagickCoreSignature);
3216 assert(exception != (ExceptionInfo *) NULL);
3217 assert(exception->signature == MagickCoreSignature);
3218 if (IsEventLogging() != MagickFalse)
3219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3220 canvas=CloneImage(image,0,0,MagickTrue,exception);
3221 if (canvas == (Image *) NULL)
3222 return((Image *) NULL);
3223 if (SetImageStorageClass(canvas,DirectClass,exception) == MagickFalse)
3224 {
3225 canvas=DestroyImage(canvas);
3226 return((Image *) NULL);
3227 }
3228 canvas->alpha_trait=BlendPixelTrait;
3229 oval_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
3230 exception);
3231 if (oval_image == (Image *) NULL)
3232 {
3233 canvas=DestroyImage(canvas);
3234 return((Image *) NULL);
3235 }
3236 (void) QueryColorCompliance("#000000",AllCompliance,
3237 &oval_image->background_color,exception);
3238 (void) SetImageBackgroundColor(oval_image,exception);
3239 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
3240 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
3241 exception);
3242 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
3243 exception);
3244 (void) FormatLocaleString(ellipse,MagickPathExtent,"ellipse %g,%g,%g,%g,"
3245 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
3246 image->rows/2.0-y);
3247 draw_info->primitive=AcquireString(ellipse);
3248 (void) DrawImage(oval_image,draw_info,exception);
3249 draw_info=DestroyDrawInfo(draw_info);
3250 blur_image=BlurImage(oval_image,radius,sigma,exception);
3251 oval_image=DestroyImage(oval_image);
3252 if (blur_image == (Image *) NULL)
3253 {
3254 canvas=DestroyImage(canvas);
3255 return((Image *) NULL);
3256 }
3257 blur_image->alpha_trait=UndefinedPixelTrait;
3258 (void) CompositeImage(canvas,blur_image,IntensityCompositeOp,MagickTrue,
3259 0,0,exception);
3260 blur_image=DestroyImage(blur_image);
3261 vignette_image=MergeImageLayers(canvas,FlattenLayer,exception);
3262 canvas=DestroyImage(canvas);
3263 if (vignette_image != (Image *) NULL)
3264 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
3265 return(vignette_image);
3266}
3267␌
3268/*
3269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3270% %
3271% %
3272% %
3273% W a v e I m a g e %
3274% %
3275% %
3276% %
3277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3278%
3279% WaveImage() creates a "ripple" effect in the image by shifting the pixels
3280% vertically along a sine wave whose amplitude and wavelength is specified
3281% by the given parameters.
3282%
3283% The format of the WaveImage method is:
3284%
3285% Image *WaveImage(const Image *image,const double amplitude,
3286% const double wave_length,const PixelInterpolateMethod method,
3287% ExceptionInfo *exception)
3288%
3289% A description of each parameter follows:
3290%
3291% o image: the image.
3292%
3293% o amplitude, wave_length: Define the amplitude and wave length of the
3294% sine wave.
3295%
3296% o interpolate: the pixel interpolation method.
3297%
3298% o exception: return any errors or warnings in this structure.
3299%
3300*/
3301MagickExport Image *WaveImage(const Image *image,const double amplitude,
3302 const double wave_length,const PixelInterpolateMethod method,
3303 ExceptionInfo *exception)
3304{
3305#define WaveImageTag "Wave/Image"
3306
3307 CacheView
3308 *canvas_image_view,
3309 *wave_view;
3310
3311 float
3312 *sine_map;
3313
3314 Image
3315 *canvas_image,
3316 *wave_image;
3317
3318 MagickBooleanType
3319 status;
3320
3321 MagickOffsetType
3322 progress;
3323
3324 ssize_t
3325 i;
3326
3327 ssize_t
3328 y;
3329
3330 /*
3331 Initialize wave image attributes.
3332 */
3333 assert(image != (Image *) NULL);
3334 assert(image->signature == MagickCoreSignature);
3335 assert(exception != (ExceptionInfo *) NULL);
3336 assert(exception->signature == MagickCoreSignature);
3337 if (IsEventLogging() != MagickFalse)
3338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3339 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
3340 if (canvas_image == (Image *) NULL)
3341 return((Image *) NULL);
3342 if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
3343 (canvas_image->background_color.alpha != (double) OpaqueAlpha))
3344 (void) SetImageAlpha(canvas_image,OpaqueAlpha,exception);
3345 wave_image=CloneImage(canvas_image,canvas_image->columns,(size_t)
3346 (canvas_image->rows+2.0*fabs(amplitude)),MagickTrue,exception);
3347 if (wave_image == (Image *) NULL)
3348 {
3349 canvas_image=DestroyImage(canvas_image);
3350 return((Image *) NULL);
3351 }
3352 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
3353 {
3354 canvas_image=DestroyImage(canvas_image);
3355 wave_image=DestroyImage(wave_image);
3356 return((Image *) NULL);
3357 }
3358 /*
3359 Allocate sine map.
3360 */
3361 sine_map=(float *) AcquireQuantumMemory((size_t) wave_image->columns,
3362 sizeof(*sine_map));
3363 if (sine_map == (float *) NULL)
3364 {
3365 canvas_image=DestroyImage(canvas_image);
3366 wave_image=DestroyImage(wave_image);
3367 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3368 }
3369 for (i=0; i < (ssize_t) wave_image->columns; i++)
3370 sine_map[i]=(float) (fabs(amplitude)+amplitude*sin((double)
3371 ((2.0*MagickPI*i)*(double) PerceptibleReciprocal(wave_length))));
3372 /*
3373 Wave image.
3374 */
3375 status=MagickTrue;
3376 progress=0;
3377 canvas_image_view=AcquireVirtualCacheView(canvas_image,exception);
3378 wave_view=AcquireAuthenticCacheView(wave_image,exception);
3379 (void) SetCacheViewVirtualPixelMethod(canvas_image_view,
3380 BackgroundVirtualPixelMethod);
3381#if defined(MAGICKCORE_OPENMP_SUPPORT)
3382 #pragma omp parallel for schedule(static) shared(progress,status) \
3383 magick_number_threads(canvas_image,wave_image,wave_image->rows,1)
3384#endif
3385 for (y=0; y < (ssize_t) wave_image->rows; y++)
3386 {
3387 const Quantum
3388 *magick_restrict p;
3389
3390 Quantum
3391 *magick_restrict q;
3392
3393 ssize_t
3394 x;
3395
3396 if (status == MagickFalse)
3397 continue;
3398 p=GetCacheViewVirtualPixels(canvas_image_view,0,y,canvas_image->columns,1,
3399 exception);
3400 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
3401 exception);
3402 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3403 {
3404 status=MagickFalse;
3405 continue;
3406 }
3407 for (x=0; x < (ssize_t) wave_image->columns; x++)
3408 {
3409 status=InterpolatePixelChannels(canvas_image,canvas_image_view,
3410 wave_image,method,(double) x,(double) (y-sine_map[x]),q,exception);
3411 if (status == MagickFalse)
3412 break;
3413 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
3414 q+=(ptrdiff_t) GetPixelChannels(wave_image);
3415 }
3416 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
3417 status=MagickFalse;
3418 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3419 {
3420 MagickBooleanType
3421 proceed;
3422
3423#if defined(MAGICKCORE_OPENMP_SUPPORT)
3424 #pragma omp atomic
3425#endif
3426 progress++;
3427 proceed=SetImageProgress(canvas_image,WaveImageTag,progress,
3428 canvas_image->rows);
3429 if (proceed == MagickFalse)
3430 status=MagickFalse;
3431 }
3432 }
3433 wave_view=DestroyCacheView(wave_view);
3434 canvas_image_view=DestroyCacheView(canvas_image_view);
3435 canvas_image=DestroyImage(canvas_image);
3436 sine_map=(float *) RelinquishMagickMemory(sine_map);
3437 if (status == MagickFalse)
3438 wave_image=DestroyImage(wave_image);
3439 return(wave_image);
3440}
3441␌
3442/*
3443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444% %
3445% %
3446% %
3447% W a v e l e t D e n o i s e I m a g e %
3448% %
3449% %
3450% %
3451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3452%
3453% WaveletDenoiseImage() removes noise from the image using a wavelet
3454% transform. The wavelet transform is a fast hierarchical scheme for
3455% processing an image using a set of consecutive lowpass and high_pass filters,
3456% followed by a decimation. This results in a decomposition into different
3457% scales which can be regarded as different “frequency bands”, determined by
3458% the mother wavelet. Adapted from dcraw.c by David Coffin.
3459%
3460% The format of the WaveletDenoiseImage method is:
3461%
3462% Image *WaveletDenoiseImage(const Image *image,const double threshold,
3463% const double softness,ExceptionInfo *exception)
3464%
3465% A description of each parameter follows:
3466%
3467% o image: the image.
3468%
3469% o threshold: set the threshold for smoothing.
3470%
3471% o softness: attenuate the smoothing threshold.
3472%
3473% o exception: return any errors or warnings in this structure.
3474%
3475*/
3476
3477static inline void HatTransform(const float *magick_restrict pixels,
3478 const size_t stride,const size_t extent,const size_t scale,float *kernel)
3479{
3480 const float
3481 *magick_restrict p,
3482 *magick_restrict q,
3483 *magick_restrict r;
3484
3485 ssize_t
3486 i;
3487
3488 p=pixels;
3489 q=pixels+scale*stride;
3490 r=pixels+scale*stride;
3491 for (i=0; i < (ssize_t) scale; i++)
3492 {
3493 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
3494 p+=(ptrdiff_t) stride;
3495 q-=stride;
3496 r+=(ptrdiff_t) stride;
3497 }
3498 for ( ; i < (ssize_t) (extent-scale); i++)
3499 {
3500 kernel[i]=0.25f*(2.0f*(*p)+*(p-scale*stride)+*(p+scale*stride));
3501 p+=(ptrdiff_t) stride;
3502 }
3503 q=p-scale*stride;
3504 r=pixels+stride*(extent-2);
3505 for ( ; i < (ssize_t) extent; i++)
3506 {
3507 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
3508 p+=(ptrdiff_t) stride;
3509 q+=(ptrdiff_t) stride;
3510 r-=stride;
3511 }
3512}
3513
3514MagickExport Image *WaveletDenoiseImage(const Image *image,
3515 const double threshold,const double softness,ExceptionInfo *exception)
3516{
3517 CacheView
3518 *image_view,
3519 *noise_view;
3520
3521 float
3522 *kernel,
3523 *pixels;
3524
3525 Image
3526 *noise_image;
3527
3528 MagickBooleanType
3529 status;
3530
3531 MagickSizeType
3532 number_pixels;
3533
3535 *pixels_info;
3536
3537 size_t
3538 number_levels = 5;
3539
3540 ssize_t
3541 channel;
3542
3543 static const float
3544 noise_levels[] = { 0.8002f, 0.2735f, 0.1202f, 0.0585f, 0.0291f, 0.0152f,
3545 0.0080f, 0.0044f };
3546
3547 /*
3548 Initialize noise image attributes.
3549 */
3550 assert(image != (const Image *) NULL);
3551 assert(image->signature == MagickCoreSignature);
3552 assert(exception != (ExceptionInfo *) NULL);
3553 assert(exception->signature == MagickCoreSignature);
3554 if (IsEventLogging() != MagickFalse)
3555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3556#if defined(MAGICKCORE_OPENCL_SUPPORT)
3557 noise_image=AccelerateWaveletDenoiseImage(image,threshold,exception);
3558 if (noise_image != (Image *) NULL)
3559 return(noise_image);
3560#endif
3561 noise_image=CloneImage(image,0,0,MagickTrue,exception);
3562 if (noise_image == (Image *) NULL)
3563 return((Image *) NULL);
3564 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
3565 {
3566 noise_image=DestroyImage(noise_image);
3567 return((Image *) NULL);
3568 }
3569 if (AcquireMagickResource(WidthResource,4*image->columns) == MagickFalse)
3570 {
3571 noise_image=DestroyImage(noise_image);
3572 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3573 }
3574 pixels_info=AcquireVirtualMemory(4*image->columns,image->rows*
3575 sizeof(*pixels));
3576 kernel=(float *) AcquireQuantumMemory(MagickMax(image->rows,image->columns)+1,
3577 GetOpenMPMaximumThreads()*sizeof(*kernel));
3578 if ((pixels_info == (MemoryInfo *) NULL) || (kernel == (float *) NULL))
3579 {
3580 if (kernel != (float *) NULL)
3581 kernel=(float *) RelinquishMagickMemory(kernel);
3582 if (pixels_info != (MemoryInfo *) NULL)
3583 pixels_info=RelinquishVirtualMemory(pixels_info);
3584 noise_image=DestroyImage(noise_image);
3585 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3586 }
3587 pixels=(float *) GetVirtualMemoryBlob(pixels_info);
3588 status=MagickTrue;
3589 number_pixels=(MagickSizeType) image->columns*image->rows;
3590 while ((number_levels > 0) &&
3591 ((1UL << (number_levels-1)) >= MagickMin(image->columns,image->rows)))
3592 number_levels--;
3593 image_view=AcquireAuthenticCacheView(image,exception);
3594 noise_view=AcquireAuthenticCacheView(noise_image,exception);
3595 for (channel=0; channel < (ssize_t) GetPixelChannels(image); channel++)
3596 {
3597 ssize_t
3598 i;
3599
3600 size_t
3601 high_pass,
3602 low_pass;
3603
3604 ssize_t
3605 level,
3606 y;
3607
3608 PixelChannel
3609 pixel_channel;
3610
3611 PixelTrait
3612 traits;
3613
3614 if (status == MagickFalse)
3615 continue;
3616 traits=GetPixelChannelTraits(image,(PixelChannel) channel);
3617 if (traits == UndefinedPixelTrait)
3618 continue;
3619 pixel_channel=GetPixelChannelChannel(image,channel);
3620 if ((pixel_channel != RedPixelChannel) &&
3621 (pixel_channel != GreenPixelChannel) &&
3622 (pixel_channel != BluePixelChannel))
3623 continue;
3624 /*
3625 Copy channel from image to wavelet pixel array.
3626 */
3627 i=0;
3628 for (y=0; y < (ssize_t) image->rows; y++)
3629 {
3630 const Quantum
3631 *magick_restrict p;
3632
3633 ssize_t
3634 x;
3635
3636 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3637 if (p == (const Quantum *) NULL)
3638 {
3639 status=MagickFalse;
3640 break;
3641 }
3642 for (x=0; x < (ssize_t) image->columns; x++)
3643 {
3644 pixels[i++]=(float) p[channel];
3645 p+=(ptrdiff_t) GetPixelChannels(image);
3646 }
3647 }
3648 /*
3649 Low pass filter outputs are called approximation kernel & high pass
3650 filters are referred to as detail kernel. The detail kernel
3651 have high values in the noisy parts of the signal.
3652 */
3653 high_pass=0;
3654 for (level=0; level < (ssize_t) number_levels; level++)
3655 {
3656 double
3657 magnitude;
3658
3659 ssize_t
3660 x;
3661
3662 low_pass=(size_t) (number_pixels*((level & 0x01)+1));
3663#if defined(MAGICKCORE_OPENMP_SUPPORT)
3664 #pragma omp parallel for schedule(static,1) \
3665 magick_number_threads(image,image,image->rows,1)
3666#endif
3667 for (y=0; y < (ssize_t) image->rows; y++)
3668 {
3669 const int
3670 id = GetOpenMPThreadId();
3671
3672 float
3673 *magick_restrict p,
3674 *magick_restrict q;
3675
3676 ssize_t
3677 c;
3678
3679 p=kernel+id*(ssize_t) image->columns;
3680 q=pixels+y*(ssize_t) image->columns;
3681 HatTransform(q+high_pass,1,image->columns,((size_t) 1UL << level),p);
3682 q+=(ptrdiff_t) low_pass;
3683 for (c=0; c < (ssize_t) image->columns; c++)
3684 *q++=(*p++);
3685 }
3686#if defined(MAGICKCORE_OPENMP_SUPPORT)
3687 #pragma omp parallel for schedule(static,1) \
3688 magick_number_threads(image,image,image->columns,1)
3689#endif
3690 for (x=0; x < (ssize_t) image->columns; x++)
3691 {
3692 const int
3693 id = GetOpenMPThreadId();
3694
3695 float
3696 *magick_restrict p,
3697 *magick_restrict q;
3698
3699 ssize_t
3700 r;
3701
3702 p=kernel+id*(ssize_t) image->rows;
3703 q=pixels+x+low_pass;
3704 HatTransform(q,image->columns,image->rows,((size_t) 1UL << level),p);
3705 for (r=0; r < (ssize_t) image->rows; r++)
3706 {
3707 *q=(*p++);
3708 q+=(ptrdiff_t) image->columns;
3709 }
3710 }
3711 /*
3712 To threshold, each coefficient is compared to a threshold value and
3713 attenuated / shrunk by some factor.
3714 */
3715 magnitude=threshold*(double) noise_levels[level];
3716 for (i=0; i < (ssize_t) number_pixels; ++i)
3717 {
3718 pixels[(ssize_t) high_pass+i]-=pixels[(ssize_t) low_pass+i];
3719 if ((double) pixels[(ssize_t) high_pass+i] < -magnitude)
3720 pixels[(ssize_t) high_pass+i]+=(float) (magnitude-softness*magnitude);
3721 else
3722 if ((double) pixels[(ssize_t) high_pass+i] > magnitude)
3723 pixels[(ssize_t) high_pass+i]-=(float) (magnitude-softness*
3724 magnitude);
3725 else
3726 pixels[(ssize_t) high_pass+i]*=(float) softness;
3727 if (high_pass != 0)
3728 pixels[i]+=pixels[(ssize_t) high_pass+i];
3729 }
3730 high_pass=low_pass;
3731 }
3732 /*
3733 Reconstruct image from the thresholded wavelet kernel.
3734 */
3735 i=0;
3736 for (y=0; y < (ssize_t) image->rows; y++)
3737 {
3738 MagickBooleanType
3739 sync;
3740
3741 Quantum
3742 *magick_restrict q;
3743
3744 ssize_t
3745 x;
3746
3747 ssize_t
3748 offset;
3749
3750 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
3751 exception);
3752 if (q == (Quantum *) NULL)
3753 {
3754 status=MagickFalse;
3755 break;
3756 }
3757 offset=GetPixelChannelOffset(noise_image,pixel_channel);
3758 for (x=0; x < (ssize_t) image->columns; x++)
3759 {
3760 MagickRealType
3761 pixel;
3762
3763 pixel=(MagickRealType) pixels[i]+(MagickRealType)
3764 pixels[(ssize_t) low_pass+i];
3765 q[offset]=ClampToQuantum(pixel);
3766 i++;
3767 q+=(ptrdiff_t) GetPixelChannels(noise_image);
3768 }
3769 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
3770 if (sync == MagickFalse)
3771 status=MagickFalse;
3772 }
3773 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3774 {
3775 MagickBooleanType
3776 proceed;
3777
3778 proceed=SetImageProgress(image,AddNoiseImageTag,(MagickOffsetType)
3779 channel,GetPixelChannels(image));
3780 if (proceed == MagickFalse)
3781 status=MagickFalse;
3782 }
3783 }
3784 noise_view=DestroyCacheView(noise_view);
3785 image_view=DestroyCacheView(image_view);
3786 kernel=(float *) RelinquishMagickMemory(kernel);
3787 pixels_info=RelinquishVirtualMemory(pixels_info);
3788 if (status == MagickFalse)
3789 noise_image=DestroyImage(noise_image);
3790 return(noise_image);
3791}