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