MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
fourier.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF OOO U U RRRR IIIII EEEEE RRRR %
7% F O O U U R R I E R R %
8% FFF O O U U RRRR I EEE RRRR %
9% F O O U U R R I E R R %
10% F OOO UUU R R IIIII EEEEE R R %
11% %
12% %
13% MagickCore Discrete Fourier Transform Methods %
14% %
15% Software Design %
16% Sean Burke %
17% Fred Weinhaus %
18% Cristy %
19% July 2009 %
20% %
21% %
22% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
23% dedicated to making software imaging solutions freely available. %
24% %
25% You may not use this file except in compliance with the License. You may %
26% obtain a copy of the License at %
27% %
28% https://imagemagick.org/license/ %
29% %
30% Unless required by applicable law or agreed to in writing, software %
31% distributed under the License is distributed on an "AS IS" BASIS, %
32% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
33% See the License for the specific language governing permissions and %
34% limitations under the License. %
35% %
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37%
38%
39%
40*/
41␌
42/*
43 Include declarations.
44*/
45#include "MagickCore/studio.h"
46#include "MagickCore/artifact.h"
47#include "MagickCore/attribute.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/cache.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/fourier.h"
54#include "MagickCore/log.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
57#include "MagickCore/monitor-private.h"
58#include "MagickCore/pixel-accessor.h"
59#include "MagickCore/property.h"
60#include "MagickCore/quantum-private.h"
61#include "MagickCore/resource_.h"
62#include "MagickCore/string-private.h"
63#include "MagickCore/thread-private.h"
64#if defined(MAGICKCORE_FFTW_DELEGATE)
65#if defined(_MSC_VER)
66#define ENABLE_FFTW_DELEGATE
67#elif !defined(__cplusplus) && !defined(c_plusplus)
68#define ENABLE_FFTW_DELEGATE
69#endif
70#endif
71#if defined(ENABLE_FFTW_DELEGATE)
72#if defined(MAGICKCORE_HAVE_COMPLEX_H)
73#include <complex.h>
74#endif
75#include <fftw3.h>
76#if !defined(MAGICKCORE_HAVE_CABS)
77#define cabs(z) (sqrt(z[0]*z[0]+z[1]*z[1]))
78#endif
79#if !defined(MAGICKCORE_HAVE_CARG)
80#define carg(z) (atan2(cimag(z),creal(z)))
81#endif
82#if !defined(MAGICKCORE_HAVE_CIMAG)
83#define cimag(z) (z[1])
84#endif
85#if !defined(MAGICKCORE_HAVE_CREAL)
86#define creal(z) (z[0])
87#endif
88#endif
89␌
90/*
91 Typedef declarations.
92*/
93typedef struct _FourierInfo
94{
95 PixelChannel
96 channel;
97
98 MagickBooleanType
99 modulus;
100
101 size_t
102 center,
103 width,
104 height;
105} FourierInfo;
106␌
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% C o m p l e x I m a g e s %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% ComplexImages() performs complex mathematics on an image sequence.
119%
120% The format of the ComplexImages method is:
121%
122% MagickBooleanType ComplexImages(Image *images,const ComplexOperator op,
123% ExceptionInfo *exception)
124%
125% A description of each parameter follows:
126%
127% o image: the image.
128%
129% o op: A complex operator.
130%
131% o exception: return any errors or warnings in this structure.
132%
133*/
134MagickExport Image *ComplexImages(const Image *images,const ComplexOperator op,
135 ExceptionInfo *exception)
136{
137#define ComplexImageTag "Complex/Image"
138
139 CacheView
140 *Ai_view,
141 *Ar_view,
142 *Bi_view,
143 *Br_view,
144 *Ci_view,
145 *Cr_view;
146
147 const char
148 *artifact;
149
150 const Image
151 *Ai_image,
152 *Ar_image,
153 *Bi_image,
154 *Br_image;
155
156 double
157 snr;
158
159 Image
160 *Ci_image,
161 *complex_images,
162 *Cr_image,
163 *image;
164
165 MagickBooleanType
166 status;
167
168 MagickOffsetType
169 progress;
170
171 size_t
172 columns,
173 number_channels,
174 rows;
175
176 ssize_t
177 y;
178
179 assert(images != (Image *) NULL);
180 assert(images->signature == MagickCoreSignature);
181 assert(exception != (ExceptionInfo *) NULL);
182 assert(exception->signature == MagickCoreSignature);
183 if (IsEventLogging() != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
185 if (images->next == (Image *) NULL)
186 {
187 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
188 "ImageSequenceRequired","`%s'",images->filename);
189 return((Image *) NULL);
190 }
191 image=CloneImage(images,0,0,MagickTrue,exception);
192 if (image == (Image *) NULL)
193 return((Image *) NULL);
194 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
195 {
196 image=DestroyImageList(image);
197 return(image);
198 }
199 image->depth=32UL;
200 complex_images=NewImageList();
201 AppendImageToList(&complex_images,image);
202 image=CloneImage(images->next,0,0,MagickTrue,exception);
203 if (image == (Image *) NULL)
204 {
205 complex_images=DestroyImageList(complex_images);
206 return(complex_images);
207 }
208 image->depth=32UL;
209 AppendImageToList(&complex_images,image);
210 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
211 {
212 complex_images=DestroyImageList(complex_images);
213 return(complex_images);
214 }
215 /*
216 Apply complex mathematics to image pixels.
217 */
218 artifact=GetImageArtifact(images,"complex:snr");
219 snr=0.0;
220 if (artifact != (const char *) NULL)
221 snr=StringToDouble(artifact,(char **) NULL);
222 Ar_image=images;
223 Ai_image=images->next;
224 Br_image=images;
225 Bi_image=images->next;
226 if ((images->next->next != (Image *) NULL) &&
227 (images->next->next->next != (Image *) NULL))
228 {
229 Br_image=images->next->next;
230 Bi_image=images->next->next->next;
231 }
232 Cr_image=complex_images;
233 Ci_image=complex_images->next;
234 number_channels=MagickMin(MagickMin(MagickMin(
235 Ar_image->number_channels,Ai_image->number_channels),MagickMin(
236 Br_image->number_channels,Bi_image->number_channels)),MagickMin(
237 Cr_image->number_channels,Ci_image->number_channels));
238 Ar_view=AcquireVirtualCacheView(Ar_image,exception);
239 Ai_view=AcquireVirtualCacheView(Ai_image,exception);
240 Br_view=AcquireVirtualCacheView(Br_image,exception);
241 Bi_view=AcquireVirtualCacheView(Bi_image,exception);
242 Cr_view=AcquireAuthenticCacheView(Cr_image,exception);
243 Ci_view=AcquireAuthenticCacheView(Ci_image,exception);
244 status=MagickTrue;
245 progress=0;
246 columns=MagickMin(Cr_image->columns,Ci_image->columns);
247 rows=MagickMin(Cr_image->rows,Ci_image->rows);
248#if defined(MAGICKCORE_OPENMP_SUPPORT)
249 #pragma omp parallel for schedule(static) shared(progress,status) \
250 magick_number_threads(Cr_image,complex_images,rows,1L)
251#endif
252 for (y=0; y < (ssize_t) rows; y++)
253 {
254 const Quantum
255 *magick_restrict Ai,
256 *magick_restrict Ar,
257 *magick_restrict Bi,
258 *magick_restrict Br;
259
260 Quantum
261 *magick_restrict Ci,
262 *magick_restrict Cr;
263
264 ssize_t
265 x;
266
267 if (status == MagickFalse)
268 continue;
269 Ar=GetCacheViewVirtualPixels(Ar_view,0,y,columns,1,exception);
270 Ai=GetCacheViewVirtualPixels(Ai_view,0,y,columns,1,exception);
271 Br=GetCacheViewVirtualPixels(Br_view,0,y,columns,1,exception);
272 Bi=GetCacheViewVirtualPixels(Bi_view,0,y,columns,1,exception);
273 Cr=QueueCacheViewAuthenticPixels(Cr_view,0,y,columns,1,exception);
274 Ci=QueueCacheViewAuthenticPixels(Ci_view,0,y,columns,1,exception);
275 if ((Ar == (const Quantum *) NULL) || (Ai == (const Quantum *) NULL) ||
276 (Br == (const Quantum *) NULL) || (Bi == (const Quantum *) NULL) ||
277 (Cr == (Quantum *) NULL) || (Ci == (Quantum *) NULL))
278 {
279 status=MagickFalse;
280 continue;
281 }
282 for (x=0; x < (ssize_t) columns; x++)
283 {
284 ssize_t
285 i;
286
287 for (i=0; i < (ssize_t) number_channels; i++)
288 {
289 double
290 ai = QuantumScale*(double) Ai[i],
291 ar = QuantumScale*(double) Ar[i],
292 bi = QuantumScale*(double) Bi[i],
293 br = QuantumScale*(double) Br[i],
294 ci,
295 cr;
296
297 switch (op)
298 {
299 case AddComplexOperator:
300 {
301 cr=ar+br;
302 ci=ai+bi;
303 break;
304 }
305 case ConjugateComplexOperator:
306 default:
307 {
308 cr=ar;
309 ci=(-ai);
310 break;
311 }
312 case DivideComplexOperator:
313 {
314 cr=MagickSafeReciprocal(br*br+bi*bi+snr)*(ar*br+ai*bi);
315 ci=MagickSafeReciprocal(br*br+bi*bi+snr)*(ai*br-ar*bi);
316 break;
317 }
318 case MagnitudePhaseComplexOperator:
319 {
320 cr=sqrt(ar*ar+ai*ai);
321 ci=atan2((double) ai,(double) ar)/(2.0*MagickPI)+0.5;
322 break;
323 }
324 case MultiplyComplexOperator:
325 {
326 cr=(ar*br-ai*bi);
327 ci=(ai*br+ar*bi);
328 break;
329 }
330 case RealImaginaryComplexOperator:
331 {
332 cr=ar*cos(2.0*MagickPI*(ai-0.5));
333 ci=ar*sin(2.0*MagickPI*(ai-0.5));
334 break;
335 }
336 case SubtractComplexOperator:
337 {
338 cr=ar-br;
339 ci=ai-bi;
340 break;
341 }
342 }
343 Cr[i]=ClampToQuantum((double) QuantumRange*cr);
344 Ci[i]=ClampToQuantum((double) QuantumRange*ci);
345 }
346 Ar+=(ptrdiff_t) GetPixelChannels(Ar_image);
347 Ai+=(ptrdiff_t) GetPixelChannels(Ai_image);
348 Br+=(ptrdiff_t) GetPixelChannels(Br_image);
349 Bi+=(ptrdiff_t) GetPixelChannels(Bi_image);
350 Cr+=(ptrdiff_t) GetPixelChannels(Cr_image);
351 Ci+=(ptrdiff_t) GetPixelChannels(Ci_image);
352 }
353 if (SyncCacheViewAuthenticPixels(Ci_view,exception) == MagickFalse)
354 status=MagickFalse;
355 if (SyncCacheViewAuthenticPixels(Cr_view,exception) == MagickFalse)
356 status=MagickFalse;
357 if (images->progress_monitor != (MagickProgressMonitor) NULL)
358 {
359 MagickBooleanType
360 proceed;
361
362#if defined(MAGICKCORE_OPENMP_SUPPORT)
363 #pragma omp atomic
364#endif
365 progress++;
366 proceed=SetImageProgress(images,ComplexImageTag,progress,images->rows);
367 if (proceed == MagickFalse)
368 status=MagickFalse;
369 }
370 }
371 Cr_view=DestroyCacheView(Cr_view);
372 Ci_view=DestroyCacheView(Ci_view);
373 Br_view=DestroyCacheView(Br_view);
374 Bi_view=DestroyCacheView(Bi_view);
375 Ar_view=DestroyCacheView(Ar_view);
376 Ai_view=DestroyCacheView(Ai_view);
377 if (status == MagickFalse)
378 complex_images=DestroyImageList(complex_images);
379 return(complex_images);
380}
381␌
382/*
383%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384% %
385% %
386% %
387% F o r w a r d F o u r i e r T r a n s f o r m I m a g e %
388% %
389% %
390% %
391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392%
393% ForwardFourierTransformImage() implements the discrete Fourier transform
394% (DFT) of the image either as a magnitude / phase or real / imaginary image
395% pair.
396%
397% The format of the ForwardFourierTransformImage method is:
398%
399% Image *ForwardFourierTransformImage(const Image *image,
400% const MagickBooleanType modulus,ExceptionInfo *exception)
401%
402% A description of each parameter follows:
403%
404% o image: the image.
405%
406% o modulus: if true, return as transform as a magnitude / phase pair
407% otherwise a real / imaginary image pair.
408%
409% o exception: return any errors or warnings in this structure.
410%
411*/
412
413#if defined(ENABLE_FFTW_DELEGATE)
414
415static MagickBooleanType ForwardQuadrantSwap(const size_t width,
416 const size_t height,const MagickBooleanType conjugate,
417 const double *source_pixels,double *forward_pixels)
418{
419 size_t
420 center;
421
422 double
423 value;
424
425 ssize_t
426 source_x,
427 source_y,
428 target_x,
429 target_y,
430 x,
431 y;
432
433 /*
434 Expand the Hermitian half-spectrum and center the zero-frequency pixel.
435 */
436 center=width/2+1;
437 for (y=0L; y < (ssize_t) height; y++)
438 for (x=0L; x < (ssize_t) width; x++)
439 {
440 if (x < (ssize_t) center)
441 {
442 source_x=x;
443 source_y=y;
444 value=source_pixels[source_y*(ssize_t) center+source_x];
445 }
446 else
447 {
448 source_x=(ssize_t) width-x;
449 source_y=((ssize_t) height-y) % (ssize_t) height;
450 value=source_pixels[source_y*(ssize_t) center+source_x];
451 if (conjugate != MagickFalse)
452 value=(-value);
453 }
454 target_x=(x+(ssize_t) (width/2)) % (ssize_t) width;
455 target_y=(y+(ssize_t) (height/2)) % (ssize_t) height;
456 forward_pixels[target_y*(ssize_t) width+target_x]=value;
457 }
458 return(MagickTrue);
459}
460
461static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
462 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
463{
464 CacheView
465 *magnitude_view,
466 *phase_view;
467
468 double
469 *magnitude_pixels,
470 *phase_pixels;
471
472 Image
473 *magnitude_image,
474 *phase_image;
475
476 MagickBooleanType
477 status;
478
479 MemoryInfo
480 *magnitude_info,
481 *phase_info;
482
483 Quantum
484 *q;
485
486 ssize_t
487 i,
488 x,
489 y;
490
491 magnitude_image=GetFirstImageInList(image);
492 phase_image=GetNextImageInList(image);
493 if (phase_image == (Image *) NULL)
494 {
495 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
496 "ImageSequenceRequired","`%s'",image->filename);
497 return(MagickFalse);
498 }
499 /*
500 Create "Fourier Transform" image from constituent arrays.
501 */
502 magnitude_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
503 sizeof(*magnitude_pixels));
504 phase_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
505 sizeof(*phase_pixels));
506 if ((magnitude_info == (MemoryInfo *) NULL) ||
507 (phase_info == (MemoryInfo *) NULL))
508 {
509 if (phase_info != (MemoryInfo *) NULL)
510 phase_info=RelinquishVirtualMemory(phase_info);
511 if (magnitude_info != (MemoryInfo *) NULL)
512 magnitude_info=RelinquishVirtualMemory(magnitude_info);
513 (void) ThrowMagickException(exception,GetMagickModule(),
514 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
515 return(MagickFalse);
516 }
517 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
518 (void) memset(magnitude_pixels,0,fourier_info->width*
519 fourier_info->height*sizeof(*magnitude_pixels));
520 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
521 (void) memset(phase_pixels,0,fourier_info->width*
522 fourier_info->height*sizeof(*phase_pixels));
523 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
524 MagickFalse,magnitude,magnitude_pixels);
525 if (status != MagickFalse)
526 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
527 MagickTrue,phase,phase_pixels);
528 if (status == MagickFalse)
529 {
530 phase_info=RelinquishVirtualMemory(phase_info);
531 magnitude_info=RelinquishVirtualMemory(magnitude_info);
532 return(MagickFalse);
533 }
534 if (fourier_info->modulus != MagickFalse)
535 {
536 i=0L;
537 for (y=0L; y < (ssize_t) fourier_info->height; y++)
538 for (x=0L; x < (ssize_t) fourier_info->width; x++)
539 {
540 phase_pixels[i]/=(2.0*MagickPI);
541 phase_pixels[i]+=0.5;
542 i++;
543 }
544 }
545 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
546 i=0L;
547 for (y=0L; y < (ssize_t) fourier_info->height; y++)
548 {
549 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->width,1UL,
550 exception);
551 if (q == (Quantum *) NULL)
552 break;
553 for (x=0L; x < (ssize_t) fourier_info->width; x++)
554 {
555 switch (fourier_info->channel)
556 {
557 case RedPixelChannel:
558 default:
559 {
560 SetPixelRed(magnitude_image,ClampToQuantum((double) QuantumRange*
561 magnitude_pixels[i]),q);
562 break;
563 }
564 case GreenPixelChannel:
565 {
566 SetPixelGreen(magnitude_image,ClampToQuantum((double) QuantumRange*
567 magnitude_pixels[i]),q);
568 break;
569 }
570 case BluePixelChannel:
571 {
572 SetPixelBlue(magnitude_image,ClampToQuantum((double) QuantumRange*
573 magnitude_pixels[i]),q);
574 break;
575 }
576 case BlackPixelChannel:
577 {
578 SetPixelBlack(magnitude_image,ClampToQuantum((double) QuantumRange*
579 magnitude_pixels[i]),q);
580 break;
581 }
582 case AlphaPixelChannel:
583 {
584 SetPixelAlpha(magnitude_image,ClampToQuantum((double) QuantumRange*
585 magnitude_pixels[i]),q);
586 break;
587 }
588 }
589 i++;
590 q+=(ptrdiff_t) GetPixelChannels(magnitude_image);
591 }
592 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
593 if (status == MagickFalse)
594 break;
595 }
596 magnitude_view=DestroyCacheView(magnitude_view);
597 i=0L;
598 phase_view=AcquireAuthenticCacheView(phase_image,exception);
599 for (y=0L; y < (ssize_t) fourier_info->height; y++)
600 {
601 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->width,1UL,
602 exception);
603 if (q == (Quantum *) NULL)
604 break;
605 for (x=0L; x < (ssize_t) fourier_info->width; x++)
606 {
607 switch (fourier_info->channel)
608 {
609 case RedPixelChannel:
610 default:
611 {
612 SetPixelRed(phase_image,ClampToQuantum((double) QuantumRange*
613 phase_pixels[i]),q);
614 break;
615 }
616 case GreenPixelChannel:
617 {
618 SetPixelGreen(phase_image,ClampToQuantum((double) QuantumRange*
619 phase_pixels[i]),q);
620 break;
621 }
622 case BluePixelChannel:
623 {
624 SetPixelBlue(phase_image,ClampToQuantum((double) QuantumRange*
625 phase_pixels[i]),q);
626 break;
627 }
628 case BlackPixelChannel:
629 {
630 SetPixelBlack(phase_image,ClampToQuantum((double) QuantumRange*
631 phase_pixels[i]),q);
632 break;
633 }
634 case AlphaPixelChannel:
635 {
636 SetPixelAlpha(phase_image,ClampToQuantum((double) QuantumRange*
637 phase_pixels[i]),q);
638 break;
639 }
640 }
641 i++;
642 q+=(ptrdiff_t) GetPixelChannels(phase_image);
643 }
644 status=SyncCacheViewAuthenticPixels(phase_view,exception);
645 if (status == MagickFalse)
646 break;
647 }
648 phase_view=DestroyCacheView(phase_view);
649 phase_info=RelinquishVirtualMemory(phase_info);
650 magnitude_info=RelinquishVirtualMemory(magnitude_info);
651 return(status);
652}
653
654static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
655 const Image *image,double *magnitude_pixels,double *phase_pixels,
656 ExceptionInfo *exception)
657{
658 CacheView
659 *image_view;
660
661 const char
662 *value;
663
664 const Quantum
665 *p;
666
667 double
668 *source_pixels;
669
670 fftw_complex
671 *forward_pixels;
672
673 fftw_plan
674 fftw_r2c_plan;
675
676 MemoryInfo
677 *forward_info,
678 *source_info;
679
680 ssize_t
681 i,
682 x,
683 y;
684
685 /*
686 Generate the forward Fourier transform.
687 */
688 if ((fourier_info->width >= INT_MAX) || (fourier_info->height >= INT_MAX))
689 {
690 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
691 "WidthOrHeightExceedsLimit","`%s'",image->filename);
692 return(MagickFalse);
693 }
694 source_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
695 sizeof(*source_pixels));
696 if (source_info == (MemoryInfo *) NULL)
697 {
698 (void) ThrowMagickException(exception,GetMagickModule(),
699 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
700 return(MagickFalse);
701 }
702 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
703 memset(source_pixels,0,fourier_info->width*fourier_info->height*
704 sizeof(*source_pixels));
705 image_view=AcquireVirtualCacheView(image,exception);
706 for (y=0L; y < (ssize_t) image->rows; y++)
707 {
708 p=GetCacheViewVirtualPixels(image_view,0L,y,image->columns,1UL,exception);
709 if (p == (const Quantum *) NULL)
710 {
711 image_view=DestroyCacheView(image_view);
712 source_info=RelinquishVirtualMemory(source_info);
713 return(MagickFalse);
714 }
715 for (x=0L; x < (ssize_t) image->columns; x++)
716 {
717 switch (fourier_info->channel)
718 {
719 case RedPixelChannel:
720 default:
721 {
722 source_pixels[y*(ssize_t) fourier_info->width+x]=QuantumScale*
723 (double) GetPixelRed(image,p);
724 break;
725 }
726 case GreenPixelChannel:
727 {
728 source_pixels[y*(ssize_t) fourier_info->width+x]=QuantumScale*
729 (double) GetPixelGreen(image,p);
730 break;
731 }
732 case BluePixelChannel:
733 {
734 source_pixels[y*(ssize_t) fourier_info->width+x]=QuantumScale*
735 (double) GetPixelBlue(image,p);
736 break;
737 }
738 case BlackPixelChannel:
739 {
740 source_pixels[y*(ssize_t) fourier_info->width+x]=QuantumScale*
741 (double) GetPixelBlack(image,p);
742 break;
743 }
744 case AlphaPixelChannel:
745 {
746 source_pixels[y*(ssize_t) fourier_info->width+x]=QuantumScale*
747 (double) GetPixelAlpha(image,p);
748 break;
749 }
750 }
751 p+=(ptrdiff_t) GetPixelChannels(image);
752 }
753 }
754 image_view=DestroyCacheView(image_view);
755 forward_info=AcquireVirtualMemory(fourier_info->height,fourier_info->center*
756 sizeof(*forward_pixels));
757 if (forward_info == (MemoryInfo *) NULL)
758 {
759 (void) ThrowMagickException(exception,GetMagickModule(),
760 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
761 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
762 return(MagickFalse);
763 }
764 forward_pixels=(fftw_complex *) GetVirtualMemoryBlob(forward_info);
765#if defined(MAGICKCORE_OPENMP_SUPPORT)
766 #pragma omp critical (MagickCore_ForwardFourierTransform)
767#endif
768 fftw_r2c_plan=fftw_plan_dft_r2c_2d((int) fourier_info->height,
769 (int) fourier_info->width,source_pixels,forward_pixels,FFTW_ESTIMATE);
770 if (fftw_r2c_plan == (fftw_plan) NULL)
771 {
772 forward_info=RelinquishVirtualMemory(forward_info);
773 source_info=RelinquishVirtualMemory(source_info);
774 (void) ThrowMagickException(exception,GetMagickModule(),
775 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
776 return(MagickFalse);
777 }
778 fftw_execute_dft_r2c(fftw_r2c_plan,source_pixels,forward_pixels);
779 fftw_destroy_plan(fftw_r2c_plan);
780 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
781 value=GetImageArtifact(image,"fourier:normalize");
782 if ((value == (const char *) NULL) || (LocaleCompare(value,"forward") == 0))
783 {
784 double
785 gamma;
786
787 /*
788 Normalize forward transform.
789 */
790 i=0L;
791 gamma=MagickSafeReciprocal((double) fourier_info->width*
792 fourier_info->height);
793 for (y=0L; y < (ssize_t) fourier_info->height; y++)
794 for (x=0L; x < (ssize_t) fourier_info->center; x++)
795 {
796#if defined(MAGICKCORE_HAVE_COMPLEX_H)
797 forward_pixels[i]*=gamma;
798#else
799 forward_pixels[i][0]*=gamma;
800 forward_pixels[i][1]*=gamma;
801#endif
802 i++;
803 }
804 }
805 /*
806 Generate magnitude and phase (or real and imaginary).
807 */
808 i=0L;
809 if (fourier_info->modulus != MagickFalse)
810 for (y=0L; y < (ssize_t) fourier_info->height; y++)
811 for (x=0L; x < (ssize_t) fourier_info->center; x++)
812 {
813 magnitude_pixels[i]=cabs(forward_pixels[i]);
814 phase_pixels[i]=carg(forward_pixels[i]);
815 i++;
816 }
817 else
818 for (y=0L; y < (ssize_t) fourier_info->height; y++)
819 for (x=0L; x < (ssize_t) fourier_info->center; x++)
820 {
821 magnitude_pixels[i]=creal(forward_pixels[i]);
822 phase_pixels[i]=cimag(forward_pixels[i]);
823 i++;
824 }
825 forward_info=(MemoryInfo *) RelinquishVirtualMemory(forward_info);
826 return(MagickTrue);
827}
828
829static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
830 const PixelChannel channel,const MagickBooleanType modulus,
831 Image *fourier_image,ExceptionInfo *exception)
832{
833 double
834 *magnitude_pixels,
835 *phase_pixels;
836
837 FourierInfo
838 fourier_info;
839
840 MagickBooleanType
841 status;
842
843 MemoryInfo
844 *magnitude_info,
845 *phase_info;
846
847 fourier_info.width=image->columns+(image->columns & 0x01);
848 fourier_info.height=image->rows+(image->rows & 0x01);
849 fourier_info.center=fourier_info.width/2+1;
850 fourier_info.channel=channel;
851 fourier_info.modulus=modulus;
852 magnitude_info=AcquireVirtualMemory(fourier_info.height,
853 fourier_info.center*sizeof(*magnitude_pixels));
854 phase_info=AcquireVirtualMemory(fourier_info.height,fourier_info.center*
855 sizeof(*phase_pixels));
856 if ((magnitude_info == (MemoryInfo *) NULL) ||
857 (phase_info == (MemoryInfo *) NULL))
858 {
859 if (phase_info != (MemoryInfo *) NULL)
860 phase_info=RelinquishVirtualMemory(phase_info);
861 if (magnitude_info != (MemoryInfo *) NULL)
862 magnitude_info=RelinquishVirtualMemory(magnitude_info);
863 (void) ThrowMagickException(exception,GetMagickModule(),
864 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
865 return(MagickFalse);
866 }
867 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
868 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
869 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
870 phase_pixels,exception);
871 if (status != MagickFalse)
872 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
873 phase_pixels,exception);
874 phase_info=RelinquishVirtualMemory(phase_info);
875 magnitude_info=RelinquishVirtualMemory(magnitude_info);
876 return(status);
877}
878#endif
879
880MagickExport Image *ForwardFourierTransformImage(const Image *image,
881 const MagickBooleanType modulus,ExceptionInfo *exception)
882{
883 Image
884 *fourier_image;
885
886 fourier_image=NewImageList();
887#if !defined(ENABLE_FFTW_DELEGATE)
888 (void) modulus;
889 (void) ThrowMagickException(exception,GetMagickModule(),
890 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)",
891 image->filename);
892#else
893 {
894 Image
895 *magnitude_image;
896
897 size_t
898 height,
899 width;
900
901 width=image->columns+(image->columns & 0x01);
902 height=image->rows+(image->rows & 0x01);
903 magnitude_image=CloneImage(image,width,height,MagickTrue,exception);
904 if (magnitude_image != (Image *) NULL)
905 {
906 Image
907 *phase_image;
908
909 magnitude_image->storage_class=DirectClass;
910 magnitude_image->depth=32UL;
911 phase_image=CloneImage(image,width,height,MagickTrue,exception);
912 if (phase_image == (Image *) NULL)
913 magnitude_image=DestroyImage(magnitude_image);
914 else
915 {
916 MagickBooleanType
917 is_gray,
918 status;
919
920 phase_image->storage_class=DirectClass;
921 phase_image->depth=32UL;
922 AppendImageToList(&fourier_image,magnitude_image);
923 AppendImageToList(&fourier_image,phase_image);
924 status=MagickTrue;
925 is_gray=IsImageGray(image);
926#if defined(MAGICKCORE_OPENMP_SUPPORT)
927 #pragma omp parallel sections
928#endif
929 {
930#if defined(MAGICKCORE_OPENMP_SUPPORT)
931 #pragma omp section
932#endif
933 {
934 MagickBooleanType
935 thread_status;
936
937 if (is_gray != MagickFalse)
938 thread_status=ForwardFourierTransformChannel(image,
939 GrayPixelChannel,modulus,fourier_image,exception);
940 else
941 thread_status=ForwardFourierTransformChannel(image,
942 RedPixelChannel,modulus,fourier_image,exception);
943 if (thread_status == MagickFalse)
944 status=thread_status;
945 }
946#if defined(MAGICKCORE_OPENMP_SUPPORT)
947 #pragma omp section
948#endif
949 {
950 MagickBooleanType
951 thread_status;
952
953 thread_status=MagickTrue;
954 if (is_gray == MagickFalse)
955 thread_status=ForwardFourierTransformChannel(image,
956 GreenPixelChannel,modulus,fourier_image,exception);
957 if (thread_status == MagickFalse)
958 status=thread_status;
959 }
960#if defined(MAGICKCORE_OPENMP_SUPPORT)
961 #pragma omp section
962#endif
963 {
964 MagickBooleanType
965 thread_status;
966
967 thread_status=MagickTrue;
968 if (is_gray == MagickFalse)
969 thread_status=ForwardFourierTransformChannel(image,
970 BluePixelChannel,modulus,fourier_image,exception);
971 if (thread_status == MagickFalse)
972 status=thread_status;
973 }
974#if defined(MAGICKCORE_OPENMP_SUPPORT)
975 #pragma omp section
976#endif
977 {
978 MagickBooleanType
979 thread_status;
980
981 thread_status=MagickTrue;
982 if (image->colorspace == CMYKColorspace)
983 thread_status=ForwardFourierTransformChannel(image,
984 BlackPixelChannel,modulus,fourier_image,exception);
985 if (thread_status == MagickFalse)
986 status=thread_status;
987 }
988#if defined(MAGICKCORE_OPENMP_SUPPORT)
989 #pragma omp section
990#endif
991 {
992 MagickBooleanType
993 thread_status;
994
995 thread_status=MagickTrue;
996 if (image->alpha_trait != UndefinedPixelTrait)
997 thread_status=ForwardFourierTransformChannel(image,
998 AlphaPixelChannel,modulus,fourier_image,exception);
999 if (thread_status == MagickFalse)
1000 status=thread_status;
1001 }
1002 }
1003 if (status == MagickFalse)
1004 fourier_image=DestroyImageList(fourier_image);
1005 fftw_cleanup();
1006 }
1007 }
1008 }
1009#endif
1010 return(fourier_image);
1011}
1012␌
1013/*
1014%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015% %
1016% %
1017% %
1018% I n v e r s e F o u r i e r T r a n s f o r m I m a g e %
1019% %
1020% %
1021% %
1022%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023%
1024% InverseFourierTransformImage() implements the inverse discrete Fourier
1025% transform (DFT) of the image either as a magnitude / phase or real /
1026% imaginary image pair.
1027%
1028% The format of the InverseFourierTransformImage method is:
1029%
1030% Image *InverseFourierTransformImage(const Image *magnitude_image,
1031% const Image *phase_image,const MagickBooleanType modulus,
1032% ExceptionInfo *exception)
1033%
1034% A description of each parameter follows:
1035%
1036% o magnitude_image: the magnitude or real image.
1037%
1038% o phase_image: the phase or imaginary image.
1039%
1040% o modulus: if true, return transform as a magnitude / phase pair
1041% otherwise a real / imaginary image pair.
1042%
1043% o exception: return any errors or warnings in this structure.
1044%
1045*/
1046
1047#if defined(ENABLE_FFTW_DELEGATE)
1048static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
1049 const Image *magnitude_image,const Image *phase_image,
1050 fftw_complex *fourier_pixels,ExceptionInfo *exception)
1051{
1052 CacheView
1053 *magnitude_view,
1054 *phase_view;
1055
1056 const Quantum
1057 *p;
1058
1059 double
1060 *inverse_pixels,
1061 *magnitude_pixels,
1062 *phase_pixels;
1063
1064 MagickBooleanType
1065 status;
1066
1067 MemoryInfo
1068 *inverse_info,
1069 *magnitude_info,
1070 *phase_info;
1071
1072 ssize_t
1073 i,
1074 source_x,
1075 source_y,
1076 x,
1077 y;
1078
1079 /*
1080 Inverse fourier - read image and break down into a double array.
1081 */
1082 status=MagickTrue;
1083 magnitude_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
1084 sizeof(*magnitude_pixels));
1085 phase_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
1086 sizeof(*phase_pixels));
1087 inverse_info=AcquireVirtualMemory(fourier_info->height,
1088 fourier_info->center*sizeof(*inverse_pixels));
1089 if ((magnitude_info == (MemoryInfo *) NULL) ||
1090 (phase_info == (MemoryInfo *) NULL) ||
1091 (inverse_info == (MemoryInfo *) NULL))
1092 {
1093 if (magnitude_info != (MemoryInfo *) NULL)
1094 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1095 if (phase_info != (MemoryInfo *) NULL)
1096 phase_info=RelinquishVirtualMemory(phase_info);
1097 if (inverse_info != (MemoryInfo *) NULL)
1098 inverse_info=RelinquishVirtualMemory(inverse_info);
1099 (void) ThrowMagickException(exception,GetMagickModule(),
1100 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1101 magnitude_image->filename);
1102 return(MagickFalse);
1103 }
1104 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
1105 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
1106 inverse_pixels=(double *) GetVirtualMemoryBlob(inverse_info);
1107 i=0L;
1108 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
1109 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1110 {
1111 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
1112 exception);
1113 if (p == (const Quantum *) NULL)
1114 {
1115 status=MagickFalse;
1116 break;
1117 }
1118 for (x=0L; x < (ssize_t) fourier_info->width; x++)
1119 {
1120 switch (fourier_info->channel)
1121 {
1122 case RedPixelChannel:
1123 default:
1124 {
1125 magnitude_pixels[i]=QuantumScale*(double)
1126 GetPixelRed(magnitude_image,p);
1127 break;
1128 }
1129 case GreenPixelChannel:
1130 {
1131 magnitude_pixels[i]=QuantumScale*(double)
1132 GetPixelGreen(magnitude_image,p);
1133 break;
1134 }
1135 case BluePixelChannel:
1136 {
1137 magnitude_pixels[i]=QuantumScale*(double)
1138 GetPixelBlue(magnitude_image,p);
1139 break;
1140 }
1141 case BlackPixelChannel:
1142 {
1143 magnitude_pixels[i]=QuantumScale*(double)
1144 GetPixelBlack(magnitude_image,p);
1145 break;
1146 }
1147 case AlphaPixelChannel:
1148 {
1149 magnitude_pixels[i]=QuantumScale*(double)
1150 GetPixelAlpha(magnitude_image,p);
1151 break;
1152 }
1153 }
1154 i++;
1155 p+=(ptrdiff_t) GetPixelChannels(magnitude_image);
1156 }
1157 }
1158 magnitude_view=DestroyCacheView(magnitude_view);
1159 if (status == MagickFalse)
1160 {
1161 inverse_info=RelinquishVirtualMemory(inverse_info);
1162 phase_info=RelinquishVirtualMemory(phase_info);
1163 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1164 return(MagickFalse);
1165 }
1166 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1167 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1168 {
1169 source_x=(x+(ssize_t) (fourier_info->width/2)) %
1170 (ssize_t) fourier_info->width;
1171 source_y=(y+(ssize_t) (fourier_info->height/2)) %
1172 (ssize_t) fourier_info->height;
1173 inverse_pixels[y*(ssize_t) fourier_info->center+x]=
1174 magnitude_pixels[source_y*(ssize_t) fourier_info->width+source_x];
1175 }
1176 (void) memcpy(magnitude_pixels,inverse_pixels,fourier_info->height*
1177 fourier_info->center*sizeof(*magnitude_pixels));
1178 i=0L;
1179 phase_view=AcquireVirtualCacheView(phase_image,exception);
1180 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1181 {
1182 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
1183 exception);
1184 if (p == (const Quantum *) NULL)
1185 {
1186 status=MagickFalse;
1187 break;
1188 }
1189 for (x=0L; x < (ssize_t) fourier_info->width; x++)
1190 {
1191 switch (fourier_info->channel)
1192 {
1193 case RedPixelChannel:
1194 default:
1195 {
1196 phase_pixels[i]=QuantumScale*(double) GetPixelRed(phase_image,p);
1197 break;
1198 }
1199 case GreenPixelChannel:
1200 {
1201 phase_pixels[i]=QuantumScale*(double) GetPixelGreen(phase_image,p);
1202 break;
1203 }
1204 case BluePixelChannel:
1205 {
1206 phase_pixels[i]=QuantumScale*(double) GetPixelBlue(phase_image,p);
1207 break;
1208 }
1209 case BlackPixelChannel:
1210 {
1211 phase_pixels[i]=QuantumScale*(double) GetPixelBlack(phase_image,p);
1212 break;
1213 }
1214 case AlphaPixelChannel:
1215 {
1216 phase_pixels[i]=QuantumScale*(double) GetPixelAlpha(phase_image,p);
1217 break;
1218 }
1219 }
1220 i++;
1221 p+=(ptrdiff_t) GetPixelChannels(phase_image);
1222 }
1223 }
1224 phase_view=DestroyCacheView(phase_view);
1225 if (status == MagickFalse)
1226 {
1227 inverse_info=RelinquishVirtualMemory(inverse_info);
1228 phase_info=RelinquishVirtualMemory(phase_info);
1229 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1230 return(MagickFalse);
1231 }
1232 if (fourier_info->modulus != MagickFalse)
1233 {
1234 i=0L;
1235 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1236 for (x=0L; x < (ssize_t) fourier_info->width; x++)
1237 {
1238 phase_pixels[i]-=0.5;
1239 phase_pixels[i]*=(2.0*MagickPI);
1240 i++;
1241 }
1242 }
1243 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1244 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1245 {
1246 source_x=(x+(ssize_t) (fourier_info->width/2)) %
1247 (ssize_t) fourier_info->width;
1248 source_y=(y+(ssize_t) (fourier_info->height/2)) %
1249 (ssize_t) fourier_info->height;
1250 inverse_pixels[y*(ssize_t) fourier_info->center+x]=
1251 phase_pixels[source_y*(ssize_t) fourier_info->width+source_x];
1252 }
1253 (void) memcpy(phase_pixels,inverse_pixels,fourier_info->height*
1254 fourier_info->center*sizeof(*phase_pixels));
1255 inverse_info=RelinquishVirtualMemory(inverse_info);
1256 /*
1257 Merge two sets.
1258 */
1259 i=0L;
1260 if (fourier_info->modulus != MagickFalse)
1261 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1262 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1263 {
1264#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1265 fourier_pixels[i]=magnitude_pixels[i]*cos(phase_pixels[i])+I*
1266 magnitude_pixels[i]*sin(phase_pixels[i]);
1267#else
1268 fourier_pixels[i][0]=magnitude_pixels[i]*cos(phase_pixels[i]);
1269 fourier_pixels[i][1]=magnitude_pixels[i]*sin(phase_pixels[i]);
1270#endif
1271 i++;
1272 }
1273 else
1274 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1275 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1276 {
1277#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1278 fourier_pixels[i]=magnitude_pixels[i]+I*phase_pixels[i];
1279#else
1280 fourier_pixels[i][0]=magnitude_pixels[i];
1281 fourier_pixels[i][1]=phase_pixels[i];
1282#endif
1283 i++;
1284 }
1285 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1286 phase_info=RelinquishVirtualMemory(phase_info);
1287 return(status);
1288}
1289
1290static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
1291 fftw_complex *fourier_pixels,Image *image,ExceptionInfo *exception)
1292{
1293 CacheView
1294 *image_view;
1295
1296 const char
1297 *value;
1298
1299 double
1300 *source_pixels;
1301
1302 fftw_plan
1303 fftw_c2r_plan;
1304
1305 MemoryInfo
1306 *source_info;
1307
1308 Quantum
1309 *q;
1310
1311 ssize_t
1312 i,
1313 x,
1314 y;
1315
1316 /*
1317 Generate the inverse Fourier transform.
1318 */
1319 if ((fourier_info->width >= INT_MAX) || (fourier_info->height >= INT_MAX))
1320 {
1321 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1322 "WidthOrHeightExceedsLimit","`%s'",image->filename);
1323 return(MagickFalse);
1324 }
1325 source_info=AcquireVirtualMemory(fourier_info->width,fourier_info->height*
1326 sizeof(*source_pixels));
1327 if (source_info == (MemoryInfo *) NULL)
1328 {
1329 (void) ThrowMagickException(exception,GetMagickModule(),
1330 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1331 return(MagickFalse);
1332 }
1333 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
1334 value=GetImageArtifact(image,"fourier:normalize");
1335 if ((value != (const char *) NULL) && (LocaleCompare(value,"inverse") == 0))
1336 {
1337 double
1338 gamma;
1339
1340 /*
1341 Normalize inverse transform.
1342 */
1343 i=0L;
1344 gamma=MagickSafeReciprocal((double) fourier_info->width*
1345 fourier_info->height);
1346 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1347 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1348 {
1349#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1350 fourier_pixels[i]*=gamma;
1351#else
1352 fourier_pixels[i][0]*=gamma;
1353 fourier_pixels[i][1]*=gamma;
1354#endif
1355 i++;
1356 }
1357 }
1358#if defined(MAGICKCORE_OPENMP_SUPPORT)
1359 #pragma omp critical (MagickCore_InverseFourierTransform)
1360#endif
1361 fftw_c2r_plan=fftw_plan_dft_c2r_2d((int) fourier_info->height,
1362 (int) fourier_info->width,fourier_pixels,source_pixels,FFTW_ESTIMATE);
1363 if (fftw_c2r_plan == (fftw_plan) NULL)
1364 {
1365 source_info=RelinquishVirtualMemory(source_info);
1366 (void) ThrowMagickException(exception,GetMagickModule(),
1367 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1368 return(MagickFalse);
1369 }
1370 fftw_execute_dft_c2r(fftw_c2r_plan,fourier_pixels,source_pixels);
1371 fftw_destroy_plan(fftw_c2r_plan);
1372 i=0L;
1373 image_view=AcquireAuthenticCacheView(image,exception);
1374 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1375 {
1376 if (y >= (ssize_t) image->rows)
1377 break;
1378 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1379 image->columns ? image->columns : fourier_info->width,1UL,exception);
1380 if (q == (Quantum *) NULL)
1381 break;
1382 for (x=0L; x < (ssize_t) fourier_info->width; x++)
1383 {
1384 if (x < (ssize_t) image->columns)
1385 switch (fourier_info->channel)
1386 {
1387 case RedPixelChannel:
1388 default:
1389 {
1390 SetPixelRed(image,ClampToQuantum((double) QuantumRange*
1391 source_pixels[i]),q);
1392 break;
1393 }
1394 case GreenPixelChannel:
1395 {
1396 SetPixelGreen(image,ClampToQuantum((double) QuantumRange*
1397 source_pixels[i]),q);
1398 break;
1399 }
1400 case BluePixelChannel:
1401 {
1402 SetPixelBlue(image,ClampToQuantum((double) QuantumRange*
1403 source_pixels[i]),q);
1404 break;
1405 }
1406 case BlackPixelChannel:
1407 {
1408 SetPixelBlack(image,ClampToQuantum((double) QuantumRange*
1409 source_pixels[i]),q);
1410 break;
1411 }
1412 case AlphaPixelChannel:
1413 {
1414 SetPixelAlpha(image,ClampToQuantum((double) QuantumRange*
1415 source_pixels[i]),q);
1416 break;
1417 }
1418 }
1419 i++;
1420 q+=(ptrdiff_t) GetPixelChannels(image);
1421 }
1422 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1423 break;
1424 }
1425 image_view=DestroyCacheView(image_view);
1426 source_info=RelinquishVirtualMemory(source_info);
1427 return(MagickTrue);
1428}
1429
1430static MagickBooleanType InverseFourierTransformChannel(
1431 const Image *magnitude_image,const Image *phase_image,
1432 const PixelChannel channel,const MagickBooleanType modulus,
1433 Image *fourier_image,ExceptionInfo *exception)
1434{
1435 fftw_complex
1436 *inverse_pixels;
1437
1438 FourierInfo
1439 fourier_info;
1440
1441 MagickBooleanType
1442 status;
1443
1444 MemoryInfo
1445 *inverse_info;
1446
1447 fourier_info.width=magnitude_image->columns;
1448 fourier_info.height=magnitude_image->rows;
1449 fourier_info.center=fourier_info.width/2+1;
1450 fourier_info.channel=channel;
1451 fourier_info.modulus=modulus;
1452 inverse_info=AcquireVirtualMemory(fourier_info.height,
1453 fourier_info.center*sizeof(*inverse_pixels));
1454 if (inverse_info == (MemoryInfo *) NULL)
1455 {
1456 (void) ThrowMagickException(exception,GetMagickModule(),
1457 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1458 magnitude_image->filename);
1459 return(MagickFalse);
1460 }
1461 inverse_pixels=(fftw_complex *) GetVirtualMemoryBlob(inverse_info);
1462 status=InverseFourier(&fourier_info,magnitude_image,phase_image,
1463 inverse_pixels,exception);
1464 if (status != MagickFalse)
1465 status=InverseFourierTransform(&fourier_info,inverse_pixels,fourier_image,
1466 exception);
1467 inverse_info=RelinquishVirtualMemory(inverse_info);
1468 return(status);
1469}
1470#endif
1471
1472MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1473 const Image *phase_image,const MagickBooleanType modulus,
1474 ExceptionInfo *exception)
1475{
1476 Image
1477 *fourier_image;
1478
1479 assert(magnitude_image != (Image *) NULL);
1480 assert(magnitude_image->signature == MagickCoreSignature);
1481 if (IsEventLogging() != MagickFalse)
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1483 magnitude_image->filename);
1484 if (phase_image == (Image *) NULL)
1485 {
1486 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1487 "ImageSequenceRequired","`%s'",magnitude_image->filename);
1488 return((Image *) NULL);
1489 }
1490 if ((magnitude_image->columns != phase_image->columns) ||
1491 (magnitude_image->rows != phase_image->rows))
1492 {
1493 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1494 "ImageSizeDiffers","`%s'",magnitude_image->filename);
1495 return((Image *) NULL);
1496 }
1497#if !defined(ENABLE_FFTW_DELEGATE)
1498 fourier_image=(Image *) NULL;
1499 (void) modulus;
1500 (void) ThrowMagickException(exception,GetMagickModule(),
1501 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)",
1502 magnitude_image->filename);
1503#else
1504 {
1505 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
1506 magnitude_image->rows,MagickTrue,exception);
1507 if (fourier_image != (Image *) NULL)
1508 {
1509 MagickBooleanType
1510 is_gray,
1511 status;
1512
1513 status=MagickTrue;
1514 is_gray=IsImageGray(magnitude_image);
1515 if (is_gray != MagickFalse)
1516 is_gray=IsImageGray(phase_image);
1517#if defined(MAGICKCORE_OPENMP_SUPPORT)
1518 #pragma omp parallel sections
1519#endif
1520 {
1521#if defined(MAGICKCORE_OPENMP_SUPPORT)
1522 #pragma omp section
1523#endif
1524 {
1525 MagickBooleanType
1526 thread_status;
1527
1528 if (is_gray != MagickFalse)
1529 thread_status=InverseFourierTransformChannel(magnitude_image,
1530 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
1531 else
1532 thread_status=InverseFourierTransformChannel(magnitude_image,
1533 phase_image,RedPixelChannel,modulus,fourier_image,exception);
1534 if (thread_status == MagickFalse)
1535 status=thread_status;
1536 }
1537#if defined(MAGICKCORE_OPENMP_SUPPORT)
1538 #pragma omp section
1539#endif
1540 {
1541 MagickBooleanType
1542 thread_status;
1543
1544 thread_status=MagickTrue;
1545 if (is_gray == MagickFalse)
1546 thread_status=InverseFourierTransformChannel(magnitude_image,
1547 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
1548 if (thread_status == MagickFalse)
1549 status=thread_status;
1550 }
1551#if defined(MAGICKCORE_OPENMP_SUPPORT)
1552 #pragma omp section
1553#endif
1554 {
1555 MagickBooleanType
1556 thread_status;
1557
1558 thread_status=MagickTrue;
1559 if (is_gray == MagickFalse)
1560 thread_status=InverseFourierTransformChannel(magnitude_image,
1561 phase_image,BluePixelChannel,modulus,fourier_image,exception);
1562 if (thread_status == MagickFalse)
1563 status=thread_status;
1564 }
1565#if defined(MAGICKCORE_OPENMP_SUPPORT)
1566 #pragma omp section
1567#endif
1568 {
1569 MagickBooleanType
1570 thread_status;
1571
1572 thread_status=MagickTrue;
1573 if (magnitude_image->colorspace == CMYKColorspace)
1574 thread_status=InverseFourierTransformChannel(magnitude_image,
1575 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
1576 if (thread_status == MagickFalse)
1577 status=thread_status;
1578 }
1579#if defined(MAGICKCORE_OPENMP_SUPPORT)
1580 #pragma omp section
1581#endif
1582 {
1583 MagickBooleanType
1584 thread_status;
1585
1586 thread_status=MagickTrue;
1587 if (magnitude_image->alpha_trait != UndefinedPixelTrait)
1588 thread_status=InverseFourierTransformChannel(magnitude_image,
1589 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
1590 if (thread_status == MagickFalse)
1591 status=thread_status;
1592 }
1593 }
1594 if (status == MagickFalse)
1595 fourier_image=DestroyImage(fourier_image);
1596 }
1597 fftw_cleanup();
1598 }
1599#endif
1600 return(fourier_image);
1601}