Coverage Report

Created: 2025-08-12 07:37

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