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