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