MagickCore 7.1.1
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,AcquireString(
1766 geometry));
1767 (void) AnnotateImage(caption_image,annotate_info,exception);
1768 height+=caption_image->rows;
1769 }
1770 annotate_info=DestroyDrawInfo(annotate_info);
1771 text=DestroyString(text);
1772 }
1773 }
1774 picture_image=CloneImage(image,(size_t) ((ssize_t) image->columns+2*quantum),
1775 height,MagickTrue,exception);
1776 if (picture_image == (Image *) NULL)
1777 {
1778 if (caption_image != (Image *) NULL)
1779 caption_image=DestroyImage(caption_image);
1780 return((Image *) NULL);
1781 }
1782 picture_image->background_color=image->border_color;
1783 (void) SetImageBackgroundColor(picture_image,exception);
1784 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
1785 quantum,exception);
1786 if (caption_image != (Image *) NULL)
1787 {
1788 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
1789 MagickTrue,quantum,((ssize_t) image->rows+3*quantum/2),exception);
1790 caption_image=DestroyImage(caption_image);
1791 }
1792 (void) QueryColorCompliance("none",AllCompliance,
1793 &picture_image->background_color,exception);
1794 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
1795 rotate_image=RotateImage(picture_image,90.0,exception);
1796 picture_image=DestroyImage(picture_image);
1797 if (rotate_image == (Image *) NULL)
1798 return((Image *) NULL);
1799 picture_image=rotate_image;
1800 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
1801 picture_image->columns,method,exception);
1802 picture_image=DestroyImage(picture_image);
1803 if (bend_image == (Image *) NULL)
1804 return((Image *) NULL);
1805 picture_image=bend_image;
1806 rotate_image=RotateImage(picture_image,-90.0,exception);
1807 picture_image=DestroyImage(picture_image);
1808 if (rotate_image == (Image *) NULL)
1809 return((Image *) NULL);
1810 picture_image=rotate_image;
1811 picture_image->background_color=image->background_color;
1812 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
1813 exception);
1814 if (polaroid_image == (Image *) NULL)
1815 {
1816 picture_image=DestroyImage(picture_image);
1817 return(picture_image);
1818 }
1819 flop_image=FlopImage(polaroid_image,exception);
1820 polaroid_image=DestroyImage(polaroid_image);
1821 if (flop_image == (Image *) NULL)
1822 {
1823 picture_image=DestroyImage(picture_image);
1824 return(picture_image);
1825 }
1826 polaroid_image=flop_image;
1827 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
1828 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
1829 picture_image=DestroyImage(picture_image);
1830 (void) QueryColorCompliance("none",AllCompliance,
1831 &polaroid_image->background_color,exception);
1832 rotate_image=RotateImage(polaroid_image,angle,exception);
1833 polaroid_image=DestroyImage(polaroid_image);
1834 if (rotate_image == (Image *) NULL)
1835 return((Image *) NULL);
1836 polaroid_image=rotate_image;
1837 trim_image=TrimImage(polaroid_image,exception);
1838 polaroid_image=DestroyImage(polaroid_image);
1839 if (trim_image == (Image *) NULL)
1840 return((Image *) NULL);
1841 polaroid_image=trim_image;
1842 return(polaroid_image);
1843}
1844
1845/*
1846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1847% %
1848% %
1849% %
1850% S e p i a T o n e I m a g e %
1851% %
1852% %
1853% %
1854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855%
1856% MagickSepiaToneImage() applies a special effect to the image, similar to the
1857% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
1858% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
1859% threshold of 80% is a good starting point for a reasonable tone.
1860%
1861% The format of the SepiaToneImage method is:
1862%
1863% Image *SepiaToneImage(const Image *image,const double threshold,
1864% ExceptionInfo *exception)
1865%
1866% A description of each parameter follows:
1867%
1868% o image: the image.
1869%
1870% o threshold: the tone threshold.
1871%
1872% o exception: return any errors or warnings in this structure.
1873%
1874*/
1875MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
1876 ExceptionInfo *exception)
1877{
1878#define SepiaToneImageTag "SepiaTone/Image"
1879
1880 CacheView
1881 *image_view,
1882 *sepia_view;
1883
1884 Image
1885 *sepia_image;
1886
1887 MagickBooleanType
1888 status;
1889
1890 MagickOffsetType
1891 progress;
1892
1893 ssize_t
1894 y;
1895
1896 /*
1897 Initialize sepia-toned image attributes.
1898 */
1899 assert(image != (const Image *) NULL);
1900 assert(image->signature == MagickCoreSignature);
1901 assert(exception != (ExceptionInfo *) NULL);
1902 assert(exception->signature == MagickCoreSignature);
1903 if (IsEventLogging() != MagickFalse)
1904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1905 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
1906 if (sepia_image == (Image *) NULL)
1907 return((Image *) NULL);
1908 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
1909 {
1910 sepia_image=DestroyImage(sepia_image);
1911 return((Image *) NULL);
1912 }
1913 /*
1914 Tone each row of the image.
1915 */
1916 status=MagickTrue;
1917 progress=0;
1918 image_view=AcquireVirtualCacheView(image,exception);
1919 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
1920#if defined(MAGICKCORE_OPENMP_SUPPORT)
1921 #pragma omp parallel for schedule(static) shared(progress,status) \
1922 magick_number_threads(image,sepia_image,image->rows,1)
1923#endif
1924 for (y=0; y < (ssize_t) image->rows; y++)
1925 {
1926 const Quantum
1927 *magick_restrict p;
1928
1929 ssize_t
1930 x;
1931
1932 Quantum
1933 *magick_restrict q;
1934
1935 if (status == MagickFalse)
1936 continue;
1937 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1938 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
1939 exception);
1940 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1941 {
1942 status=MagickFalse;
1943 continue;
1944 }
1945 for (x=0; x < (ssize_t) image->columns; x++)
1946 {
1947 double
1948 intensity,
1949 tone;
1950
1951 intensity=GetPixelIntensity(image,p);
1952 tone=intensity > threshold ? (double) QuantumRange : intensity+
1953 (double) QuantumRange-threshold;
1954 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
1955 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
1956 intensity+(double) QuantumRange-7.0*threshold/6.0;
1957 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
1958 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
1959 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
1960 tone=threshold/7.0;
1961 if ((double) GetPixelGreen(image,q) < tone)
1962 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
1963 if ((double) GetPixelBlue(image,q) < tone)
1964 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
1965 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
1966 p+=(ptrdiff_t) GetPixelChannels(image);
1967 q+=(ptrdiff_t) GetPixelChannels(sepia_image);
1968 }
1969 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
1970 status=MagickFalse;
1971 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1972 {
1973 MagickBooleanType
1974 proceed;
1975
1976#if defined(MAGICKCORE_OPENMP_SUPPORT)
1977 #pragma omp atomic
1978#endif
1979 progress++;
1980 proceed=SetImageProgress(image,SepiaToneImageTag,progress,image->rows);
1981 if (proceed == MagickFalse)
1982 status=MagickFalse;
1983 }
1984 }
1985 sepia_view=DestroyCacheView(sepia_view);
1986 image_view=DestroyCacheView(image_view);
1987 (void) NormalizeImage(sepia_image,exception);
1988 (void) ContrastImage(sepia_image,MagickTrue,exception);
1989 if (status == MagickFalse)
1990 sepia_image=DestroyImage(sepia_image);
1991 return(sepia_image);
1992}
1993
1994/*
1995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1996% %
1997% %
1998% %
1999% S h a d o w I m a g e %
2000% %
2001% %
2002% %
2003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004%
2005% ShadowImage() simulates a shadow from the specified image and returns it.
2006%
2007% The format of the ShadowImage method is:
2008%
2009% Image *ShadowImage(const Image *image,const double alpha,
2010% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
2011% ExceptionInfo *exception)
2012%
2013% A description of each parameter follows:
2014%
2015% o image: the image.
2016%
2017% o alpha: percentage transparency.
2018%
2019% o sigma: the standard deviation of the Gaussian, in pixels.
2020%
2021% o x_offset: the shadow x-offset.
2022%
2023% o y_offset: the shadow y-offset.
2024%
2025% o exception: return any errors or warnings in this structure.
2026%
2027*/
2028MagickExport Image *ShadowImage(const Image *image,const double alpha,
2029 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
2030 ExceptionInfo *exception)
2031{
2032#define ShadowImageTag "Shadow/Image"
2033
2034 CacheView
2035 *image_view;
2036
2037 ChannelType
2038 channel_mask;
2039
2040 Image
2041 *border_image,
2042 *clone_image,
2043 *shadow_image;
2044
2045 MagickBooleanType
2046 status;
2047
2048 PixelInfo
2049 background_color;
2050
2052 border_info;
2053
2054 ssize_t
2055 y;
2056
2057 assert(image != (Image *) NULL);
2058 assert(image->signature == MagickCoreSignature);
2059 assert(exception != (ExceptionInfo *) NULL);
2060 assert(exception->signature == MagickCoreSignature);
2061 if (IsEventLogging() != MagickFalse)
2062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2063 clone_image=CloneImage(image,0,0,MagickTrue,exception);
2064 if (clone_image == (Image *) NULL)
2065 return((Image *) NULL);
2066 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2067 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
2068 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
2069 exception);
2070 border_info.width=CastDoubleToUnsigned(2.0*sigma+0.5);
2071 border_info.height=CastDoubleToUnsigned(2.0*sigma+0.5);
2072 border_info.x=0;
2073 border_info.y=0;
2074 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
2075 exception);
2076 clone_image->alpha_trait=BlendPixelTrait;
2077 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
2078 clone_image=DestroyImage(clone_image);
2079 if (border_image == (Image *) NULL)
2080 return((Image *) NULL);
2081 if (border_image->alpha_trait == UndefinedPixelTrait)
2082 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
2083 /*
2084 Shadow image.
2085 */
2086 status=MagickTrue;
2087 background_color=border_image->background_color;
2088 background_color.alpha_trait=BlendPixelTrait;
2089 image_view=AcquireAuthenticCacheView(border_image,exception);
2090 for (y=0; y < (ssize_t) border_image->rows; y++)
2091 {
2092 Quantum
2093 *magick_restrict q;
2094
2095 ssize_t
2096 x;
2097
2098 if (status == MagickFalse)
2099 continue;
2100 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
2101 exception);
2102 if (q == (Quantum *) NULL)
2103 {
2104 status=MagickFalse;
2105 continue;
2106 }
2107 for (x=0; x < (ssize_t) border_image->columns; x++)
2108 {
2109 if (border_image->alpha_trait != UndefinedPixelTrait)
2110 background_color.alpha=(double) GetPixelAlpha(border_image,q)*alpha/
2111 100.0;
2112 SetPixelViaPixelInfo(border_image,&background_color,q);
2113 q+=(ptrdiff_t) GetPixelChannels(border_image);
2114 }
2115 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2116 status=MagickFalse;
2117 }
2118 image_view=DestroyCacheView(image_view);
2119 if (status == MagickFalse)
2120 {
2121 border_image=DestroyImage(border_image);
2122 return((Image *) NULL);
2123 }
2124 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
2125 shadow_image=BlurImage(border_image,0.0,sigma,exception);
2126 border_image=DestroyImage(border_image);
2127 if (shadow_image == (Image *) NULL)
2128 return((Image *) NULL);
2129 (void) SetPixelChannelMask(shadow_image,channel_mask);
2130 if (shadow_image->page.width == 0)
2131 shadow_image->page.width=shadow_image->columns;
2132 if (shadow_image->page.height == 0)
2133 shadow_image->page.height=shadow_image->rows;
2134 shadow_image->page.width=(size_t) ((ssize_t) shadow_image->page.width+
2135 x_offset-(ssize_t) border_info.width);
2136 shadow_image->page.height=(size_t) ((ssize_t) shadow_image->page.height+
2137 y_offset-(ssize_t) border_info.height);
2138 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
2139 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
2140 return(shadow_image);
2141}
2142
2143/*
2144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2145% %
2146% %
2147% %
2148% S k e t c h I m a g e %
2149% %
2150% %
2151% %
2152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2153%
2154% SketchImage() simulates a pencil sketch. We convolve the image with a
2155% Gaussian operator of the given radius and standard deviation (sigma). For
2156% reasonable results, radius should be larger than sigma. Use a radius of 0
2157% and SketchImage() selects a suitable radius for you. Angle gives the angle
2158% of the sketch.
2159%
2160% The format of the SketchImage method is:
2161%
2162% Image *SketchImage(const Image *image,const double radius,
2163% const double sigma,const double angle,ExceptionInfo *exception)
2164%
2165% A description of each parameter follows:
2166%
2167% o image: the image.
2168%
2169% o radius: the radius of the Gaussian, in pixels, not counting the
2170% center pixel.
2171%
2172% o sigma: the standard deviation of the Gaussian, in pixels.
2173%
2174% o angle: apply the effect along this angle.
2175%
2176% o exception: return any errors or warnings in this structure.
2177%
2178*/
2179MagickExport Image *SketchImage(const Image *image,const double radius,
2180 const double sigma,const double angle,ExceptionInfo *exception)
2181{
2182 CacheView
2183 *random_view;
2184
2185 Image
2186 *blend_image,
2187 *blur_image,
2188 *dodge_image,
2189 *random_image,
2190 *sketch_image;
2191
2192 MagickBooleanType
2193 status;
2194
2196 **magick_restrict random_info;
2197
2198 ssize_t
2199 y;
2200
2201#if defined(MAGICKCORE_OPENMP_SUPPORT)
2202 unsigned long
2203 key;
2204#endif
2205
2206 /*
2207 Sketch image.
2208 */
2209 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
2210 MagickTrue,exception);
2211 if (random_image == (Image *) NULL)
2212 return((Image *) NULL);
2213 status=MagickTrue;
2214 random_info=AcquireRandomInfoTLS();
2215 random_view=AcquireAuthenticCacheView(random_image,exception);
2216#if defined(MAGICKCORE_OPENMP_SUPPORT)
2217 key=GetRandomSecretKey(random_info[0]);
2218 #pragma omp parallel for schedule(static) shared(status) \
2219 magick_number_threads(random_image,random_image,random_image->rows,key == ~0UL ? 0 : 2)
2220#endif
2221 for (y=0; y < (ssize_t) random_image->rows; y++)
2222 {
2223 const int
2224 id = GetOpenMPThreadId();
2225
2226 Quantum
2227 *magick_restrict q;
2228
2229 ssize_t
2230 x;
2231
2232 if (status == MagickFalse)
2233 continue;
2234 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
2235 exception);
2236 if (q == (Quantum *) NULL)
2237 {
2238 status=MagickFalse;
2239 continue;
2240 }
2241 for (x=0; x < (ssize_t) random_image->columns; x++)
2242 {
2243 double
2244 value;
2245
2246 ssize_t
2247 i;
2248
2249 value=GetPseudoRandomValue(random_info[id]);
2250 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
2251 {
2252 PixelChannel channel = GetPixelChannelChannel(image,i);
2253 PixelTrait traits = GetPixelChannelTraits(image,channel);
2254 if (traits == UndefinedPixelTrait)
2255 continue;
2256 q[i]=ClampToQuantum((double) QuantumRange*value);
2257 }
2258 q+=(ptrdiff_t) GetPixelChannels(random_image);
2259 }
2260 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
2261 status=MagickFalse;
2262 }
2263 random_view=DestroyCacheView(random_view);
2264 random_info=DestroyRandomInfoTLS(random_info);
2265 if (status == MagickFalse)
2266 {
2267 random_image=DestroyImage(random_image);
2268 return(random_image);
2269 }
2270 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
2271 random_image=DestroyImage(random_image);
2272 if (blur_image == (Image *) NULL)
2273 return((Image *) NULL);
2274 dodge_image=EdgeImage(blur_image,radius,exception);
2275 blur_image=DestroyImage(blur_image);
2276 if (dodge_image == (Image *) NULL)
2277 return((Image *) NULL);
2278 status=ClampImage(dodge_image,exception);
2279 if (status != MagickFalse)
2280 status=NormalizeImage(dodge_image,exception);
2281 if (status != MagickFalse)
2282 status=NegateImage(dodge_image,MagickFalse,exception);
2283 if (status != MagickFalse)
2284 status=TransformImage(&dodge_image,(char *) NULL,"50%",exception);
2285 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
2286 if (sketch_image == (Image *) NULL)
2287 {
2288 dodge_image=DestroyImage(dodge_image);
2289 return((Image *) NULL);
2290 }
2291 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
2292 MagickTrue,0,0,exception);
2293 dodge_image=DestroyImage(dodge_image);
2294 blend_image=CloneImage(image,0,0,MagickTrue,exception);
2295 if (blend_image == (Image *) NULL)
2296 {
2297 sketch_image=DestroyImage(sketch_image);
2298 return((Image *) NULL);
2299 }
2300 if (blend_image->alpha_trait != BlendPixelTrait)
2301 (void) SetImageAlpha(blend_image,TransparentAlpha,exception);
2302 (void) SetImageArtifact(blend_image,"compose:args","20x80");
2303 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
2304 0,0,exception);
2305 blend_image=DestroyImage(blend_image);
2306 return(sketch_image);
2307}
2308
2309/*
2310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2311% %
2312% %
2313% %
2314% S o l a r i z e I m a g e %
2315% %
2316% %
2317% %
2318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2319%
2320% SolarizeImage() applies a special effect to the image, similar to the effect
2321% achieved in a photo darkroom by selectively exposing areas of photo
2322% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
2323% measure of the extent of the solarization.
2324%
2325% The format of the SolarizeImage method is:
2326%
2327% MagickBooleanType SolarizeImage(Image *image,const double threshold,
2328% ExceptionInfo *exception)
2329%
2330% A description of each parameter follows:
2331%
2332% o image: the image.
2333%
2334% o threshold: Define the extent of the solarization.
2335%
2336% o exception: return any errors or warnings in this structure.
2337%
2338*/
2339MagickExport MagickBooleanType SolarizeImage(Image *image,
2340 const double threshold,ExceptionInfo *exception)
2341{
2342#define SolarizeImageTag "Solarize/Image"
2343
2344 CacheView
2345 *image_view;
2346
2347 MagickBooleanType
2348 status;
2349
2350 MagickOffsetType
2351 progress;
2352
2353 ssize_t
2354 y;
2355
2356 assert(image != (Image *) NULL);
2357 assert(image->signature == MagickCoreSignature);
2358 if (IsEventLogging() != MagickFalse)
2359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2360 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2361 (void) SetImageColorspace(image,sRGBColorspace,exception);
2362 if (image->storage_class == PseudoClass)
2363 {
2364 ssize_t
2365 i;
2366
2367 /*
2368 Solarize colormap.
2369 */
2370 for (i=0; i < (ssize_t) image->colors; i++)
2371 {
2372 if ((double) image->colormap[i].red > threshold)
2373 image->colormap[i].red=(double) QuantumRange-image->colormap[i].red;
2374 if ((double) image->colormap[i].green > threshold)
2375 image->colormap[i].green=(double) QuantumRange-
2376 image->colormap[i].green;
2377 if ((double) image->colormap[i].blue > threshold)
2378 image->colormap[i].blue=(double) QuantumRange-image->colormap[i].blue;
2379 }
2380 return(SyncImage(image,exception));
2381 }
2382 /*
2383 Solarize image.
2384 */
2385 status=MagickTrue;
2386 progress=0;
2387 image_view=AcquireAuthenticCacheView(image,exception);
2388#if defined(MAGICKCORE_OPENMP_SUPPORT)
2389 #pragma omp parallel for schedule(static) shared(progress,status) \
2390 magick_number_threads(image,image,image->rows,2)
2391#endif
2392 for (y=0; y < (ssize_t) image->rows; y++)
2393 {
2394 ssize_t
2395 x;
2396
2397 Quantum
2398 *magick_restrict q;
2399
2400 if (status == MagickFalse)
2401 continue;
2402 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2403 if (q == (Quantum *) NULL)
2404 {
2405 status=MagickFalse;
2406 continue;
2407 }
2408 for (x=0; x < (ssize_t) image->columns; x++)
2409 {
2410 ssize_t
2411 i;
2412
2413 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2414 {
2415 PixelChannel channel = GetPixelChannelChannel(image,i);
2416 PixelTrait traits = GetPixelChannelTraits(image,channel);
2417 if ((traits & UpdatePixelTrait) == 0)
2418 continue;
2419 if ((double) q[i] > threshold)
2420 q[i]=QuantumRange-q[i];
2421 }
2422 q+=(ptrdiff_t) GetPixelChannels(image);
2423 }
2424 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2425 status=MagickFalse;
2426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2427 {
2428 MagickBooleanType
2429 proceed;
2430
2431#if defined(MAGICKCORE_OPENMP_SUPPORT)
2432 #pragma omp atomic
2433#endif
2434 progress++;
2435 proceed=SetImageProgress(image,SolarizeImageTag,progress,image->rows);
2436 if (proceed == MagickFalse)
2437 status=MagickFalse;
2438 }
2439 }
2440 image_view=DestroyCacheView(image_view);
2441 return(status);
2442}
2443
2444/*
2445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2446% %
2447% %
2448% %
2449% S t e g a n o I m a g e %
2450% %
2451% %
2452% %
2453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2454%
2455% SteganoImage() hides a digital watermark within the image. Recover
2456% the hidden watermark later to prove that the authenticity of an image.
2457% Offset defines the start position within the image to hide the watermark.
2458%
2459% The format of the SteganoImage method is:
2460%
2461% Image *SteganoImage(const Image *image,Image *watermark,
2462% ExceptionInfo *exception)
2463%
2464% A description of each parameter follows:
2465%
2466% o image: the image.
2467%
2468% o watermark: the watermark image.
2469%
2470% o exception: return any errors or warnings in this structure.
2471%
2472*/
2473MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
2474 ExceptionInfo *exception)
2475{
2476#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
2477#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
2478 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
2479#define SteganoImageTag "Stegano/Image"
2480
2481 CacheView
2482 *stegano_view,
2483 *watermark_view;
2484
2485 Image
2486 *stegano_image;
2487
2488 int
2489 c;
2490
2491 MagickBooleanType
2492 status;
2493
2494 PixelInfo
2495 pixel;
2496
2497 Quantum
2498 *q;
2499
2500 ssize_t
2501 x;
2502
2503 size_t
2504 depth,
2505 one;
2506
2507 ssize_t
2508 i,
2509 j,
2510 k,
2511 y;
2512
2513 /*
2514 Initialize steganographic image attributes.
2515 */
2516 assert(image != (const Image *) NULL);
2517 assert(image->signature == MagickCoreSignature);
2518 assert(watermark != (const Image *) NULL);
2519 assert(watermark->signature == MagickCoreSignature);
2520 assert(exception != (ExceptionInfo *) NULL);
2521 assert(exception->signature == MagickCoreSignature);
2522 if (IsEventLogging() != MagickFalse)
2523 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2524 one=1UL;
2525 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
2526 if (stegano_image == (Image *) NULL)
2527 return((Image *) NULL);
2528 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
2529 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
2530 {
2531 stegano_image=DestroyImage(stegano_image);
2532 return((Image *) NULL);
2533 }
2534 /*
2535 Hide watermark in low-order bits of image.
2536 */
2537 c=0;
2538 i=0;
2539 j=0;
2540 depth=stegano_image->depth;
2541 k=stegano_image->offset;
2542 status=MagickTrue;
2543 watermark_view=AcquireVirtualCacheView(watermark,exception);
2544 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
2545 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
2546 {
2547 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
2548 {
2549 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
2550 {
2551 ssize_t
2552 offset;
2553
2554 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
2555 exception);
2556 offset=k/(ssize_t) stegano_image->columns;
2557 if (offset >= (ssize_t) stegano_image->rows)
2558 break;
2559 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
2560 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
2561 exception);
2562 if (q == (Quantum *) NULL)
2563 break;
2564 switch (c)
2565 {
2566 case 0:
2567 {
2568 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
2569 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2570 break;
2571 }
2572 case 1:
2573 {
2574 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
2575 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2576 break;
2577 }
2578 case 2:
2579 {
2580 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
2581 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
2582 break;
2583 }
2584 }
2585 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
2586 break;
2587 c++;
2588 if (c == 3)
2589 c=0;
2590 k++;
2591 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
2592 k=0;
2593 if (k == stegano_image->offset)
2594 j++;
2595 }
2596 }
2597 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2598 {
2599 MagickBooleanType
2600 proceed;
2601
2602 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
2603 depth-i,depth);
2604 if (proceed == MagickFalse)
2605 status=MagickFalse;
2606 }
2607 }
2608 stegano_view=DestroyCacheView(stegano_view);
2609 watermark_view=DestroyCacheView(watermark_view);
2610 if (status == MagickFalse)
2611 stegano_image=DestroyImage(stegano_image);
2612 return(stegano_image);
2613}
2614
2615/*
2616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2617% %
2618% %
2619% %
2620% S t e r e o A n a g l y p h I m a g e %
2621% %
2622% %
2623% %
2624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2625%
2626% StereoAnaglyphImage() combines two images and produces a single image that
2627% is the composite of a left and right image of a stereo pair. Special
2628% red-green stereo glasses are required to view this effect.
2629%
2630% The format of the StereoAnaglyphImage method is:
2631%
2632% Image *StereoImage(const Image *left_image,const Image *right_image,
2633% ExceptionInfo *exception)
2634% Image *StereoAnaglyphImage(const Image *left_image,
2635% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
2636% ExceptionInfo *exception)
2637%
2638% A description of each parameter follows:
2639%
2640% o left_image: the left image.
2641%
2642% o right_image: the right image.
2643%
2644% o exception: return any errors or warnings in this structure.
2645%
2646% o x_offset: amount, in pixels, by which the left image is offset to the
2647% right of the right image.
2648%
2649% o y_offset: amount, in pixels, by which the left image is offset to the
2650% bottom of the right image.
2651%
2652%
2653*/
2654MagickExport Image *StereoImage(const Image *left_image,
2655 const Image *right_image,ExceptionInfo *exception)
2656{
2657 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
2658}
2659
2660MagickExport Image *StereoAnaglyphImage(const Image *left_image,
2661 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
2662 ExceptionInfo *exception)
2663{
2664#define StereoImageTag "Stereo/Image"
2665
2666 const Image
2667 *image;
2668
2669 Image
2670 *stereo_image;
2671
2672 MagickBooleanType
2673 status;
2674
2675 ssize_t
2676 y;
2677
2678 assert(left_image != (const Image *) NULL);
2679 assert(left_image->signature == MagickCoreSignature);
2680 assert(right_image != (const Image *) NULL);
2681 assert(right_image->signature == MagickCoreSignature);
2682 assert(exception != (ExceptionInfo *) NULL);
2683 assert(exception->signature == MagickCoreSignature);
2684 if (IsEventLogging() != MagickFalse)
2685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2686 left_image->filename);
2687 image=left_image;
2688 if ((left_image->columns != right_image->columns) ||
2689 (left_image->rows != right_image->rows))
2690 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
2691 /*
2692 Initialize stereo image attributes.
2693 */
2694 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
2695 MagickTrue,exception);
2696 if (stereo_image == (Image *) NULL)
2697 return((Image *) NULL);
2698 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
2699 {
2700 stereo_image=DestroyImage(stereo_image);
2701 return((Image *) NULL);
2702 }
2703 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
2704 /*
2705 Copy left image to red channel and right image to blue channel.
2706 */
2707 status=MagickTrue;
2708 for (y=0; y < (ssize_t) stereo_image->rows; y++)
2709 {
2710 const Quantum
2711 *magick_restrict p,
2712 *magick_restrict q;
2713
2714 ssize_t
2715 x;
2716
2717 Quantum
2718 *magick_restrict r;
2719
2720 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
2721 exception);
2722 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
2723 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
2724 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
2725 (r == (Quantum *) NULL))
2726 break;
2727 for (x=0; x < (ssize_t) stereo_image->columns; x++)
2728 {
2729 SetPixelRed(stereo_image,GetPixelRed(left_image,p),r);
2730 SetPixelGreen(stereo_image,GetPixelGreen(right_image,q),r);
2731 SetPixelBlue(stereo_image,GetPixelBlue(right_image,q),r);
2732 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
2733 SetPixelAlpha(stereo_image,(GetPixelAlpha(left_image,p)+
2734 GetPixelAlpha(right_image,q))/2,r);
2735 p+=(ptrdiff_t) GetPixelChannels(left_image);
2736 q+=(ptrdiff_t) GetPixelChannels(right_image);
2737 r+=(ptrdiff_t) GetPixelChannels(stereo_image);
2738 }
2739 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
2740 break;
2741 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2742 {
2743 MagickBooleanType
2744 proceed;
2745
2746 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
2747 stereo_image->rows);
2748 if (proceed == MagickFalse)
2749 status=MagickFalse;
2750 }
2751 }
2752 if (status == MagickFalse)
2753 stereo_image=DestroyImage(stereo_image);
2754 return(stereo_image);
2755}
2756
2757/*
2758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2759% %
2760% %
2761% %
2762% S w i r l I m a g e %
2763% %
2764% %
2765% %
2766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2767%
2768% SwirlImage() swirls the pixels about the center of the image, where
2769% degrees indicates the sweep of the arc through which each pixel is moved.
2770% You get a more dramatic effect as the degrees move from 1 to 360.
2771%
2772% The format of the SwirlImage method is:
2773%
2774% Image *SwirlImage(const Image *image,double degrees,
2775% const PixelInterpolateMethod method,ExceptionInfo *exception)
2776%
2777% A description of each parameter follows:
2778%
2779% o image: the image.
2780%
2781% o degrees: Define the tightness of the swirling effect.
2782%
2783% o method: the pixel interpolation method.
2784%
2785% o exception: return any errors or warnings in this structure.
2786%
2787*/
2788MagickExport Image *SwirlImage(const Image *image,double degrees,
2789 const PixelInterpolateMethod method,ExceptionInfo *exception)
2790{
2791#define SwirlImageTag "Swirl/Image"
2792
2793 CacheView
2794 *canvas_view,
2795 *interpolate_view,
2796 *swirl_view;
2797
2798 double
2799 radius;
2800
2801 Image
2802 *canvas_image,
2803 *swirl_image;
2804
2805 MagickBooleanType
2806 status;
2807
2808 MagickOffsetType
2809 progress;
2810
2811 PointInfo
2812 center,
2813 scale;
2814
2815 ssize_t
2816 y;
2817
2818 /*
2819 Initialize swirl image attributes.
2820 */
2821 assert(image != (const Image *) NULL);
2822 assert(image->signature == MagickCoreSignature);
2823 assert(exception != (ExceptionInfo *) NULL);
2824 assert(exception->signature == MagickCoreSignature);
2825 if (IsEventLogging() != MagickFalse)
2826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2827 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
2828 if (canvas_image == (Image *) NULL)
2829 return((Image *) NULL);
2830 swirl_image=CloneImage(canvas_image,0,0,MagickTrue,exception);
2831 if (swirl_image == (Image *) NULL)
2832 {
2833 canvas_image=DestroyImage(canvas_image);
2834 return((Image *) NULL);
2835 }
2836 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
2837 {
2838 canvas_image=DestroyImage(canvas_image);
2839 swirl_image=DestroyImage(swirl_image);
2840 return((Image *) NULL);
2841 }
2842 if (swirl_image->background_color.alpha_trait != UndefinedPixelTrait)
2843 (void) SetImageAlphaChannel(swirl_image,OnAlphaChannel,exception);
2844 /*
2845 Compute scaling factor.
2846 */
2847 center.x=(double) canvas_image->columns/2.0;
2848 center.y=(double) canvas_image->rows/2.0;
2849 radius=MagickMax(center.x,center.y);
2850 scale.x=1.0;
2851 scale.y=1.0;
2852 if (canvas_image->columns > canvas_image->rows)
2853 scale.y=(double) canvas_image->columns/(double) canvas_image->rows;
2854 else
2855 if (canvas_image->columns < canvas_image->rows)
2856 scale.x=(double) canvas_image->rows/(double) canvas_image->columns;
2857 degrees=(double) DegreesToRadians(degrees);
2858 /*
2859 Swirl image.
2860 */
2861 status=MagickTrue;
2862 progress=0;
2863 canvas_view=AcquireVirtualCacheView(canvas_image,exception);
2864 interpolate_view=AcquireVirtualCacheView(image,exception);
2865 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
2866#if defined(MAGICKCORE_OPENMP_SUPPORT)
2867 #pragma omp parallel for schedule(static) shared(progress,status) \
2868 magick_number_threads(canvas_image,swirl_image,canvas_image->rows,1)
2869#endif
2870 for (y=0; y < (ssize_t) canvas_image->rows; y++)
2871 {
2872 double
2873 distance;
2874
2875 PointInfo
2876 delta;
2877
2878 const Quantum
2879 *magick_restrict p;
2880
2881 ssize_t
2882 x;
2883
2884 Quantum
2885 *magick_restrict q;
2886
2887 if (status == MagickFalse)
2888 continue;
2889 p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas_image->columns,1,
2890 exception);
2891 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
2892 exception);
2893 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2894 {
2895 status=MagickFalse;
2896 continue;
2897 }
2898 delta.y=scale.y*(double) (y-center.y);
2899 for (x=0; x < (ssize_t) canvas_image->columns; x++)
2900 {
2901 /*
2902 Determine if the pixel is within an ellipse.
2903 */
2904 delta.x=scale.x*(double) (x-center.x);
2905 distance=delta.x*delta.x+delta.y*delta.y;
2906 if (distance >= (radius*radius))
2907 {
2908 ssize_t
2909 i;
2910
2911 for (i=0; i < (ssize_t) GetPixelChannels(canvas_image); i++)
2912 {
2913 PixelChannel channel = GetPixelChannelChannel(canvas_image,i);
2914 PixelTrait traits = GetPixelChannelTraits(canvas_image,channel);
2915 PixelTrait swirl_traits = GetPixelChannelTraits(swirl_image,
2916 channel);
2917 if ((traits == UndefinedPixelTrait) ||
2918 (swirl_traits == UndefinedPixelTrait))
2919 continue;
2920 SetPixelChannel(swirl_image,channel,p[i],q);
2921 }
2922 }
2923 else
2924 {
2925 double
2926 cosine,
2927 factor,
2928 sine;
2929
2930 /*
2931 Swirl the pixel.
2932 */
2933 factor=1.0-sqrt((double) distance)/radius;
2934 sine=sin((double) (degrees*factor*factor));
2935 cosine=cos((double) (degrees*factor*factor));
2936 status=InterpolatePixelChannels(canvas_image,interpolate_view,
2937 swirl_image,method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),
2938 (double) ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,
2939 exception);
2940 if (status == MagickFalse)
2941 break;
2942 }
2943 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
2944 q+=(ptrdiff_t) GetPixelChannels(swirl_image);
2945 }
2946 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
2947 status=MagickFalse;
2948 if (canvas_image->progress_monitor != (MagickProgressMonitor) NULL)
2949 {
2950 MagickBooleanType
2951 proceed;
2952
2953#if defined(MAGICKCORE_OPENMP_SUPPORT)
2954 #pragma omp atomic
2955#endif
2956 progress++;
2957 proceed=SetImageProgress(canvas_image,SwirlImageTag,progress,
2958 canvas_image->rows);
2959 if (proceed == MagickFalse)
2960 status=MagickFalse;
2961 }
2962 }
2963 swirl_view=DestroyCacheView(swirl_view);
2964 interpolate_view=DestroyCacheView(interpolate_view);
2965 canvas_view=DestroyCacheView(canvas_view);
2966 canvas_image=DestroyImage(canvas_image);
2967 if (status == MagickFalse)
2968 swirl_image=DestroyImage(swirl_image);
2969 return(swirl_image);
2970}
2971
2972/*
2973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2974% %
2975% %
2976% %
2977% T i n t I m a g e %
2978% %
2979% %
2980% %
2981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2982%
2983% TintImage() applies a color vector to each pixel in the image. The length
2984% of the vector is 0 for black and white and at its maximum for the midtones.
2985% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
2986%
2987% The format of the TintImage method is:
2988%
2989% Image *TintImage(const Image *image,const char *blend,
2990% const PixelInfo *tint,ExceptionInfo *exception)
2991%
2992% A description of each parameter follows:
2993%
2994% o image: the image.
2995%
2996% o blend: A color value used for tinting.
2997%
2998% o tint: A color value used for tinting.
2999%
3000% o exception: return any errors or warnings in this structure.
3001%
3002*/
3003MagickExport Image *TintImage(const Image *image,const char *blend,
3004 const PixelInfo *tint,ExceptionInfo *exception)
3005{
3006#define TintImageTag "Tint/Image"
3007
3008 CacheView
3009 *image_view,
3010 *tint_view;
3011
3012 double
3013 intensity;
3014
3016 geometry_info;
3017
3018 Image
3019 *tint_image;
3020
3021 MagickBooleanType
3022 status;
3023
3024 MagickOffsetType
3025 progress;
3026
3027 PixelInfo
3028 color_vector;
3029
3030 MagickStatusType
3031 flags;
3032
3033 ssize_t
3034 y;
3035
3036 /*
3037 Allocate tint image.
3038 */
3039 assert(image != (const Image *) NULL);
3040 assert(image->signature == MagickCoreSignature);
3041 assert(exception != (ExceptionInfo *) NULL);
3042 assert(exception->signature == MagickCoreSignature);
3043 if (IsEventLogging() != MagickFalse)
3044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3045 tint_image=CloneImage(image,0,0,MagickTrue,exception);
3046 if (tint_image == (Image *) NULL)
3047 return((Image *) NULL);
3048 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
3049 {
3050 tint_image=DestroyImage(tint_image);
3051 return((Image *) NULL);
3052 }
3053 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
3054 (IsPixelInfoGray(tint) == MagickFalse))
3055 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
3056 if (blend == (const char *) NULL)
3057 return(tint_image);
3058 /*
3059 Determine RGB values of the color.
3060 */
3061 GetPixelInfo(image,&color_vector);
3062 flags=ParseGeometry(blend,&geometry_info);
3063 color_vector.red=geometry_info.rho;
3064 color_vector.green=geometry_info.rho;
3065 color_vector.blue=geometry_info.rho;
3066 color_vector.alpha=(MagickRealType) OpaqueAlpha;
3067 if ((flags & SigmaValue) != 0)
3068 color_vector.green=geometry_info.sigma;
3069 if ((flags & XiValue) != 0)
3070 color_vector.blue=geometry_info.xi;
3071 if ((flags & PsiValue) != 0)
3072 color_vector.alpha=geometry_info.psi;
3073 if (image->colorspace == CMYKColorspace)
3074 {
3075 color_vector.black=geometry_info.rho;
3076 if ((flags & PsiValue) != 0)
3077 color_vector.black=geometry_info.psi;
3078 if ((flags & ChiValue) != 0)
3079 color_vector.alpha=geometry_info.chi;
3080 }
3081 intensity=(double) GetPixelInfoIntensity((const Image *) NULL,tint);
3082 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
3083 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
3084 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
3085 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
3086 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
3087 /*
3088 Tint image.
3089 */
3090 status=MagickTrue;
3091 progress=0;
3092 image_view=AcquireVirtualCacheView(image,exception);
3093 tint_view=AcquireAuthenticCacheView(tint_image,exception);
3094#if defined(MAGICKCORE_OPENMP_SUPPORT)
3095 #pragma omp parallel for schedule(static) shared(progress,status) \
3096 magick_number_threads(image,tint_image,image->rows,1)
3097#endif
3098 for (y=0; y < (ssize_t) image->rows; y++)
3099 {
3100 const Quantum
3101 *magick_restrict p;
3102
3103 Quantum
3104 *magick_restrict q;
3105
3106 ssize_t
3107 x;
3108
3109 if (status == MagickFalse)
3110 continue;
3111 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3112 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
3113 exception);
3114 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3115 {
3116 status=MagickFalse;
3117 continue;
3118 }
3119 for (x=0; x < (ssize_t) image->columns; x++)
3120 {
3121 PixelInfo
3122 pixel;
3123
3124 double
3125 weight;
3126
3127 GetPixelInfo(image,&pixel);
3128 weight=QuantumScale*(double) GetPixelRed(image,p)-0.5;
3129 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
3130 (1.0-(4.0*(weight*weight)));
3131 weight=QuantumScale*(double) GetPixelGreen(image,p)-0.5;
3132 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
3133 (1.0-(4.0*(weight*weight)));
3134 weight=QuantumScale*(double) GetPixelBlue(image,p)-0.5;
3135 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
3136 (1.0-(4.0*(weight*weight)));
3137 weight=QuantumScale*(double) GetPixelBlack(image,p)-0.5;
3138 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
3139 (1.0-(4.0*(weight*weight)));
3140 pixel.alpha=(MagickRealType) GetPixelAlpha(image,p);
3141 SetPixelViaPixelInfo(tint_image,&pixel,q);
3142 p+=(ptrdiff_t) GetPixelChannels(image);
3143 q+=(ptrdiff_t) GetPixelChannels(tint_image);
3144 }
3145 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
3146 status=MagickFalse;
3147 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3148 {
3149 MagickBooleanType
3150 proceed;
3151
3152#if defined(MAGICKCORE_OPENMP_SUPPORT)
3153 #pragma omp atomic
3154#endif
3155 progress++;
3156 proceed=SetImageProgress(image,TintImageTag,progress,image->rows);
3157 if (proceed == MagickFalse)
3158 status=MagickFalse;
3159 }
3160 }
3161 tint_view=DestroyCacheView(tint_view);
3162 image_view=DestroyCacheView(image_view);
3163 if (status == MagickFalse)
3164 tint_image=DestroyImage(tint_image);
3165 return(tint_image);
3166}
3167
3168/*
3169%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3170% %
3171% %
3172% %
3173% V i g n e t t e I m a g e %
3174% %
3175% %
3176% %
3177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3178%
3179% VignetteImage() softens the edges of the image in vignette style.
3180%
3181% The format of the VignetteImage method is:
3182%
3183% Image *VignetteImage(const Image *image,const double radius,
3184% const double sigma,const ssize_t x,const ssize_t y,
3185% ExceptionInfo *exception)
3186%
3187% A description of each parameter follows:
3188%
3189% o image: the image.
3190%
3191% o radius: the radius of the pixel neighborhood.
3192%
3193% o sigma: the standard deviation of the Gaussian, in pixels.
3194%
3195% o x, y: Define the x and y ellipse offset.
3196%
3197% o exception: return any errors or warnings in this structure.
3198%
3199*/
3200MagickExport Image *VignetteImage(const Image *image,const double radius,
3201 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
3202{
3203 char
3204 ellipse[MagickPathExtent];
3205
3206 DrawInfo
3207 *draw_info;
3208
3209 Image
3210 *canvas,
3211 *blur_image,
3212 *oval_image,
3213 *vignette_image;
3214
3215 assert(image != (Image *) NULL);
3216 assert(image->signature == MagickCoreSignature);
3217 assert(exception != (ExceptionInfo *) NULL);
3218 assert(exception->signature == MagickCoreSignature);
3219 if (IsEventLogging() != MagickFalse)
3220 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3221 canvas=CloneImage(image,0,0,MagickTrue,exception);
3222 if (canvas == (Image *) NULL)
3223 return((Image *) NULL);
3224 if (SetImageStorageClass(canvas,DirectClass,exception) == MagickFalse)
3225 {
3226 canvas=DestroyImage(canvas);
3227 return((Image *) NULL);
3228 }
3229 canvas->alpha_trait=BlendPixelTrait;
3230 oval_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
3231 exception);
3232 if (oval_image == (Image *) NULL)
3233 {
3234 canvas=DestroyImage(canvas);
3235 return((Image *) NULL);
3236 }
3237 (void) QueryColorCompliance("#000000",AllCompliance,
3238 &oval_image->background_color,exception);
3239 (void) SetImageBackgroundColor(oval_image,exception);
3240 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
3241 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
3242 exception);
3243 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
3244 exception);
3245 (void) FormatLocaleString(ellipse,MagickPathExtent,"ellipse %g,%g,%g,%g,"
3246 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
3247 image->rows/2.0-y);
3248 draw_info->primitive=AcquireString(ellipse);
3249 (void) DrawImage(oval_image,draw_info,exception);
3250 draw_info=DestroyDrawInfo(draw_info);
3251 blur_image=BlurImage(oval_image,radius,sigma,exception);
3252 oval_image=DestroyImage(oval_image);
3253 if (blur_image == (Image *) NULL)
3254 {
3255 canvas=DestroyImage(canvas);
3256 return((Image *) NULL);
3257 }
3258 blur_image->alpha_trait=UndefinedPixelTrait;
3259 (void) CompositeImage(canvas,blur_image,IntensityCompositeOp,MagickTrue,
3260 0,0,exception);
3261 blur_image=DestroyImage(blur_image);
3262 vignette_image=MergeImageLayers(canvas,FlattenLayer,exception);
3263 canvas=DestroyImage(canvas);
3264 if (vignette_image != (Image *) NULL)
3265 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
3266 return(vignette_image);
3267}
3268
3269/*
3270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3271% %
3272% %
3273% %
3274% W a v e I m a g e %
3275% %
3276% %
3277% %
3278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3279%
3280% WaveImage() creates a "ripple" effect in the image by shifting the pixels
3281% vertically along a sine wave whose amplitude and wavelength is specified
3282% by the given parameters.
3283%
3284% The format of the WaveImage method is:
3285%
3286% Image *WaveImage(const Image *image,const double amplitude,
3287% const double wave_length,const PixelInterpolateMethod method,
3288% ExceptionInfo *exception)
3289%
3290% A description of each parameter follows:
3291%
3292% o image: the image.
3293%
3294% o amplitude, wave_length: Define the amplitude and wave length of the
3295% sine wave.
3296%
3297% o interpolate: the pixel interpolation method.
3298%
3299% o exception: return any errors or warnings in this structure.
3300%
3301*/
3302MagickExport Image *WaveImage(const Image *image,const double amplitude,
3303 const double wave_length,const PixelInterpolateMethod method,
3304 ExceptionInfo *exception)
3305{
3306#define WaveImageTag "Wave/Image"
3307
3308 CacheView
3309 *canvas_image_view,
3310 *wave_view;
3311
3312 float
3313 *sine_map;
3314
3315 Image
3316 *canvas_image,
3317 *wave_image;
3318
3319 MagickBooleanType
3320 status;
3321
3322 MagickOffsetType
3323 progress;
3324
3325 ssize_t
3326 i;
3327
3328 ssize_t
3329 y;
3330
3331 /*
3332 Initialize wave image attributes.
3333 */
3334 assert(image != (Image *) NULL);
3335 assert(image->signature == MagickCoreSignature);
3336 assert(exception != (ExceptionInfo *) NULL);
3337 assert(exception->signature == MagickCoreSignature);
3338 if (IsEventLogging() != MagickFalse)
3339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3340 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
3341 if (canvas_image == (Image *) NULL)
3342 return((Image *) NULL);
3343 if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
3344 (canvas_image->background_color.alpha != (double) OpaqueAlpha))
3345 (void) SetImageAlpha(canvas_image,OpaqueAlpha,exception);
3346 wave_image=CloneImage(canvas_image,canvas_image->columns,(size_t)
3347 (canvas_image->rows+2.0*fabs(amplitude)),MagickTrue,exception);
3348 if (wave_image == (Image *) NULL)
3349 {
3350 canvas_image=DestroyImage(canvas_image);
3351 return((Image *) NULL);
3352 }
3353 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
3354 {
3355 canvas_image=DestroyImage(canvas_image);
3356 wave_image=DestroyImage(wave_image);
3357 return((Image *) NULL);
3358 }
3359 /*
3360 Allocate sine map.
3361 */
3362 sine_map=(float *) AcquireQuantumMemory((size_t) wave_image->columns,
3363 sizeof(*sine_map));
3364 if (sine_map == (float *) NULL)
3365 {
3366 canvas_image=DestroyImage(canvas_image);
3367 wave_image=DestroyImage(wave_image);
3368 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3369 }
3370 for (i=0; i < (ssize_t) wave_image->columns; i++)
3371 sine_map[i]=(float) (fabs(amplitude)+amplitude*sin((double)
3372 ((2.0*MagickPI*i)*(double) PerceptibleReciprocal(wave_length))));
3373 /*
3374 Wave image.
3375 */
3376 status=MagickTrue;
3377 progress=0;
3378 canvas_image_view=AcquireVirtualCacheView(canvas_image,exception);
3379 wave_view=AcquireAuthenticCacheView(wave_image,exception);
3380 (void) SetCacheViewVirtualPixelMethod(canvas_image_view,
3381 BackgroundVirtualPixelMethod);
3382#if defined(MAGICKCORE_OPENMP_SUPPORT)
3383 #pragma omp parallel for schedule(static) shared(progress,status) \
3384 magick_number_threads(canvas_image,wave_image,wave_image->rows,1)
3385#endif
3386 for (y=0; y < (ssize_t) wave_image->rows; y++)
3387 {
3388 const Quantum
3389 *magick_restrict p;
3390
3391 Quantum
3392 *magick_restrict q;
3393
3394 ssize_t
3395 x;
3396
3397 if (status == MagickFalse)
3398 continue;
3399 p=GetCacheViewVirtualPixels(canvas_image_view,0,y,canvas_image->columns,1,
3400 exception);
3401 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
3402 exception);
3403 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3404 {
3405 status=MagickFalse;
3406 continue;
3407 }
3408 for (x=0; x < (ssize_t) wave_image->columns; x++)
3409 {
3410 status=InterpolatePixelChannels(canvas_image,canvas_image_view,
3411 wave_image,method,(double) x,(double) (y-sine_map[x]),q,exception);
3412 if (status == MagickFalse)
3413 break;
3414 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
3415 q+=(ptrdiff_t) GetPixelChannels(wave_image);
3416 }
3417 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
3418 status=MagickFalse;
3419 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3420 {
3421 MagickBooleanType
3422 proceed;
3423
3424#if defined(MAGICKCORE_OPENMP_SUPPORT)
3425 #pragma omp atomic
3426#endif
3427 progress++;
3428 proceed=SetImageProgress(canvas_image,WaveImageTag,progress,
3429 canvas_image->rows);
3430 if (proceed == MagickFalse)
3431 status=MagickFalse;
3432 }
3433 }
3434 wave_view=DestroyCacheView(wave_view);
3435 canvas_image_view=DestroyCacheView(canvas_image_view);
3436 canvas_image=DestroyImage(canvas_image);
3437 sine_map=(float *) RelinquishMagickMemory(sine_map);
3438 if (status == MagickFalse)
3439 wave_image=DestroyImage(wave_image);
3440 return(wave_image);
3441}
3442
3443/*
3444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3445% %
3446% %
3447% %
3448% W a v e l e t D e n o i s e I m a g e %
3449% %
3450% %
3451% %
3452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3453%
3454% WaveletDenoiseImage() removes noise from the image using a wavelet
3455% transform. The wavelet transform is a fast hierarchical scheme for
3456% processing an image using a set of consecutive lowpass and high_pass filters,
3457% followed by a decimation. This results in a decomposition into different
3458% scales which can be regarded as different “frequency bands”, determined by
3459% the mother wavelet. Adapted from dcraw.c by David Coffin.
3460%
3461% The format of the WaveletDenoiseImage method is:
3462%
3463% Image *WaveletDenoiseImage(const Image *image,const double threshold,
3464% const double softness,ExceptionInfo *exception)
3465%
3466% A description of each parameter follows:
3467%
3468% o image: the image.
3469%
3470% o threshold: set the threshold for smoothing.
3471%
3472% o softness: attenuate the smoothing threshold.
3473%
3474% o exception: return any errors or warnings in this structure.
3475%
3476*/
3477
3478static inline void HatTransform(const float *magick_restrict pixels,
3479 const size_t stride,const size_t extent,const size_t scale,float *kernel)
3480{
3481 const float
3482 *magick_restrict p,
3483 *magick_restrict q,
3484 *magick_restrict r;
3485
3486 ssize_t
3487 i;
3488
3489 p=pixels;
3490 q=pixels+scale*stride;
3491 r=pixels+scale*stride;
3492 for (i=0; i < (ssize_t) scale; i++)
3493 {
3494 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
3495 p+=(ptrdiff_t) stride;
3496 q-=stride;
3497 r+=(ptrdiff_t) stride;
3498 }
3499 for ( ; i < (ssize_t) (extent-scale); i++)
3500 {
3501 kernel[i]=0.25f*(2.0f*(*p)+*(p-scale*stride)+*(p+scale*stride));
3502 p+=(ptrdiff_t) stride;
3503 }
3504 q=p-scale*stride;
3505 r=pixels+stride*(extent-2);
3506 for ( ; i < (ssize_t) extent; i++)
3507 {
3508 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
3509 p+=(ptrdiff_t) stride;
3510 q+=(ptrdiff_t) stride;
3511 r-=stride;
3512 }
3513}
3514
3515MagickExport Image *WaveletDenoiseImage(const Image *image,
3516 const double threshold,const double softness,ExceptionInfo *exception)
3517{
3518 CacheView
3519 *image_view,
3520 *noise_view;
3521
3522 float
3523 *kernel,
3524 *pixels;
3525
3526 Image
3527 *noise_image;
3528
3529 MagickBooleanType
3530 status;
3531
3532 MagickSizeType
3533 number_pixels;
3534
3536 *pixels_info;
3537
3538 ssize_t
3539 channel;
3540
3541 static const float
3542 noise_levels[] = { 0.8002f, 0.2735f, 0.1202f, 0.0585f, 0.0291f, 0.0152f,
3543 0.0080f, 0.0044f };
3544
3545 /*
3546 Initialize noise image attributes.
3547 */
3548 assert(image != (const Image *) NULL);
3549 assert(image->signature == MagickCoreSignature);
3550 assert(exception != (ExceptionInfo *) NULL);
3551 assert(exception->signature == MagickCoreSignature);
3552 if (IsEventLogging() != MagickFalse)
3553 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3554#if defined(MAGICKCORE_OPENCL_SUPPORT)
3555 noise_image=AccelerateWaveletDenoiseImage(image,threshold,exception);
3556 if (noise_image != (Image *) NULL)
3557 return(noise_image);
3558#endif
3559 noise_image=CloneImage(image,0,0,MagickTrue,exception);
3560 if (noise_image == (Image *) NULL)
3561 return((Image *) NULL);
3562 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
3563 {
3564 noise_image=DestroyImage(noise_image);
3565 return((Image *) NULL);
3566 }
3567 if (AcquireMagickResource(WidthResource,4*image->columns) == MagickFalse)
3568 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3569 pixels_info=AcquireVirtualMemory(3*image->columns,image->rows*
3570 sizeof(*pixels));
3571 kernel=(float *) AcquireQuantumMemory(MagickMax(image->rows,image->columns)+1,
3572 GetOpenMPMaximumThreads()*sizeof(*kernel));
3573 if ((pixels_info == (MemoryInfo *) NULL) || (kernel == (float *) NULL))
3574 {
3575 if (kernel != (float *) NULL)
3576 kernel=(float *) RelinquishMagickMemory(kernel);
3577 if (pixels_info != (MemoryInfo *) NULL)
3578 pixels_info=RelinquishVirtualMemory(pixels_info);
3579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3580 }
3581 pixels=(float *) GetVirtualMemoryBlob(pixels_info);
3582 status=MagickTrue;
3583 number_pixels=(MagickSizeType) image->columns*image->rows;
3584 image_view=AcquireAuthenticCacheView(image,exception);
3585 noise_view=AcquireAuthenticCacheView(noise_image,exception);
3586 for (channel=0; channel < (ssize_t) GetPixelChannels(image); channel++)
3587 {
3588 ssize_t
3589 i;
3590
3591 size_t
3592 high_pass,
3593 low_pass;
3594
3595 ssize_t
3596 level,
3597 y;
3598
3599 PixelChannel
3600 pixel_channel;
3601
3602 PixelTrait
3603 traits;
3604
3605 if (status == MagickFalse)
3606 continue;
3607 traits=GetPixelChannelTraits(image,(PixelChannel) channel);
3608 if (traits == UndefinedPixelTrait)
3609 continue;
3610 pixel_channel=GetPixelChannelChannel(image,channel);
3611 if ((pixel_channel != RedPixelChannel) &&
3612 (pixel_channel != GreenPixelChannel) &&
3613 (pixel_channel != BluePixelChannel))
3614 continue;
3615 /*
3616 Copy channel from image to wavelet pixel array.
3617 */
3618 i=0;
3619 for (y=0; y < (ssize_t) image->rows; y++)
3620 {
3621 const Quantum
3622 *magick_restrict p;
3623
3624 ssize_t
3625 x;
3626
3627 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3628 if (p == (const Quantum *) NULL)
3629 {
3630 status=MagickFalse;
3631 break;
3632 }
3633 for (x=0; x < (ssize_t) image->columns; x++)
3634 {
3635 pixels[i++]=(float) p[channel];
3636 p+=(ptrdiff_t) GetPixelChannels(image);
3637 }
3638 }
3639 /*
3640 Low pass filter outputs are called approximation kernel & high pass
3641 filters are referred to as detail kernel. The detail kernel
3642 have high values in the noisy parts of the signal.
3643 */
3644 high_pass=0;
3645 for (level=0; level < 5; level++)
3646 {
3647 double
3648 magnitude;
3649
3650 ssize_t
3651 x;
3652
3653 low_pass=(size_t) (number_pixels*((level & 0x01)+1));
3654#if defined(MAGICKCORE_OPENMP_SUPPORT)
3655 #pragma omp parallel for schedule(static,1) \
3656 magick_number_threads(image,image,image->rows,1)
3657#endif
3658 for (y=0; y < (ssize_t) image->rows; y++)
3659 {
3660 const int
3661 id = GetOpenMPThreadId();
3662
3663 float
3664 *magick_restrict p,
3665 *magick_restrict q;
3666
3667 ssize_t
3668 c;
3669
3670 p=kernel+id*(ssize_t) image->columns;
3671 q=pixels+y*(ssize_t) image->columns;
3672 HatTransform(q+high_pass,1,image->columns,((size_t) 1UL << level),p);
3673 q+=(ptrdiff_t) low_pass;
3674 for (c=0; c < (ssize_t) image->columns; c++)
3675 *q++=(*p++);
3676 }
3677#if defined(MAGICKCORE_OPENMP_SUPPORT)
3678 #pragma omp parallel for schedule(static,1) \
3679 magick_number_threads(image,image,image->columns,1)
3680#endif
3681 for (x=0; x < (ssize_t) image->columns; x++)
3682 {
3683 const int
3684 id = GetOpenMPThreadId();
3685
3686 float
3687 *magick_restrict p,
3688 *magick_restrict q;
3689
3690 ssize_t
3691 r;
3692
3693 p=kernel+id*(ssize_t) image->rows;
3694 q=pixels+x+low_pass;
3695 HatTransform(q,image->columns,image->rows,((size_t) 1UL << level),p);
3696 for (r=0; r < (ssize_t) image->rows; r++)
3697 {
3698 *q=(*p++);
3699 q+=(ptrdiff_t) image->columns;
3700 }
3701 }
3702 /*
3703 To threshold, each coefficient is compared to a threshold value and
3704 attenuated / shrunk by some factor.
3705 */
3706 magnitude=threshold*(double) noise_levels[level];
3707 for (i=0; i < (ssize_t) number_pixels; ++i)
3708 {
3709 pixels[(ssize_t) high_pass+i]-=pixels[(ssize_t) low_pass+i];
3710 if ((double) pixels[(ssize_t) high_pass+i] < -magnitude)
3711 pixels[(ssize_t) high_pass+i]+=(float) (magnitude-softness*magnitude);
3712 else
3713 if ((double) pixels[(ssize_t) high_pass+i] > magnitude)
3714 pixels[(ssize_t) high_pass+i]-=(float) (magnitude-softness*
3715 magnitude);
3716 else
3717 pixels[(ssize_t) high_pass+i]*=(float) softness;
3718 if (high_pass != 0)
3719 pixels[i]+=pixels[(ssize_t) high_pass+i];
3720 }
3721 high_pass=low_pass;
3722 }
3723 /*
3724 Reconstruct image from the thresholded wavelet kernel.
3725 */
3726 i=0;
3727 for (y=0; y < (ssize_t) image->rows; y++)
3728 {
3729 MagickBooleanType
3730 sync;
3731
3732 Quantum
3733 *magick_restrict q;
3734
3735 ssize_t
3736 x;
3737
3738 ssize_t
3739 offset;
3740
3741 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
3742 exception);
3743 if (q == (Quantum *) NULL)
3744 {
3745 status=MagickFalse;
3746 break;
3747 }
3748 offset=GetPixelChannelOffset(noise_image,pixel_channel);
3749 for (x=0; x < (ssize_t) image->columns; x++)
3750 {
3751 MagickRealType
3752 pixel;
3753
3754 pixel=(MagickRealType) pixels[i]+(MagickRealType)
3755 pixels[(ssize_t) low_pass+i];
3756 q[offset]=ClampToQuantum(pixel);
3757 i++;
3758 q+=(ptrdiff_t) GetPixelChannels(noise_image);
3759 }
3760 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
3761 if (sync == MagickFalse)
3762 status=MagickFalse;
3763 }
3764 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3765 {
3766 MagickBooleanType
3767 proceed;
3768
3769 proceed=SetImageProgress(image,AddNoiseImageTag,(MagickOffsetType)
3770 channel,GetPixelChannels(image));
3771 if (proceed == MagickFalse)
3772 status=MagickFalse;
3773 }
3774 }
3775 noise_view=DestroyCacheView(noise_view);
3776 image_view=DestroyCacheView(image_view);
3777 kernel=(float *) RelinquishMagickMemory(kernel);
3778 pixels_info=RelinquishVirtualMemory(pixels_info);
3779 if (status == MagickFalse)
3780 noise_image=DestroyImage(noise_image);
3781 return(noise_image);
3782}