2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Discrete Fourier Transform Methods %
22 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
23 % dedicated to making software imaging solutions freely available. %
25 % You may not use this file except in compliance with the License. You may %
26 % obtain a copy of the License at %
28 % http://www.imagemagick.org/script/license.php %
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. %
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 #include "magick/studio.h"
46 #include "magick/cache.h"
47 #include "magick/image.h"
48 #include "magick/image-private.h"
49 #include "magick/list.h"
50 #include "magick/fourier.h"
51 #include "magick/log.h"
52 #include "magick/memory_.h"
53 #include "magick/monitor.h"
54 #include "magick/property.h"
55 #include "magick/quantum-private.h"
56 #include "magick/thread-private.h"
57 #if defined(MAGICKCORE_FFTW_DELEGATE)
58 #if defined(MAGICKCORE_HAVE_COMPLEX_H)
62 #if !defined(MAGICKCORE_HAVE_CABS)
63 #define cabs(z) (sqrt(z[0]*z[0]+z[1]*z[1]))
65 #if !defined(MAGICKCORE_HAVE_CARG)
66 #define carg(z) (atan2(cimag(z[1]),creal(z[0])))
68 #if !defined(MAGICKCORE_HAVE_CIMAG)
69 #define cimag(z) (z[1])
71 #if !defined(MAGICKCORE_HAVE_CREAL)
72 #define creal(z) (z[0])
79 typedef struct _FourierInfo
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % 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 %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % ForwardFourierTransformImage() implements the discrete Fourier transform
107 % (DFT) of the image either as a magnitude / phase or real / imaginary image
110 % The format of the ForwadFourierTransformImage method is:
112 % Image *ForwardFourierTransformImage(const Image *image,
113 % const MagickBooleanType modulus,ExceptionInfo *exception)
115 % A description of each parameter follows:
117 % o image: the image.
119 % o modulus: if true, return as transform as a magnitude / phase pair
120 % otherwise a real / imaginary image pair.
122 % o exception: return any errors or warnings in this structure.
126 #if defined(MAGICKCORE_FFTW_DELEGATE)
128 static MagickBooleanType RollFourier(const size_t width,
129 const size_t height,const ssize_t x_offset,const ssize_t y_offset,
145 Move zero frequency (DC, average color) from (0,0) to (width/2,height/2).
147 roll=(double *) AcquireQuantumMemory((size_t) width,height*sizeof(*roll));
148 if (roll == (double *) NULL)
151 for (y=0L; y < (ssize_t) height; y++)
154 v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset;
156 v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height :
158 for (x=0L; x < (ssize_t) width; x++)
161 u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset;
163 u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width :
165 roll[v*width+u]=fourier[i++];
168 (void) CopyMagickMemory(fourier,roll,width*height*sizeof(*roll));
169 roll=(double *) RelinquishMagickMemory(roll);
173 static MagickBooleanType ForwardQuadrantSwap(const size_t width,
174 const size_t height,double *source,double *destination)
189 center=(ssize_t) floor((double) width/2L)+1L;
190 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,source);
191 if (status == MagickFalse)
193 for (y=0L; y < (ssize_t) height; y++)
194 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
195 destination[width*y+x+width/2L]=source[center*y+x];
196 for (y=1; y < (ssize_t) height; y++)
197 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
198 destination[width*(height-y)+width/2L-x-1L]=source[center*y+x+1L];
199 for (x=0L; x < (ssize_t) (width/2L); x++)
200 destination[-x+width/2L-1L]=destination[x+width/2L+1L];
204 static void CorrectPhaseLHS(const size_t width,
205 const size_t height,double *fourier)
213 for (y=0L; y < (ssize_t) height; y++)
214 for (x=0L; x < (ssize_t) (width/2L); x++)
215 fourier[y*width+x]*=(-1.0);
218 static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
219 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
249 magnitude_image=GetFirstImageInList(image);
250 phase_image=GetNextImageInList(image);
251 if (phase_image == (Image *) NULL)
253 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
254 "ImageSequenceRequired","`%s'",image->filename);
258 Create "Fourier Transform" image from constituent arrays.
260 magnitude_source=(double *) AcquireQuantumMemory((size_t)
261 fourier_info->height,fourier_info->width*sizeof(*magnitude_source));
262 if (magnitude_source == (double *) NULL)
264 (void) ResetMagickMemory(magnitude_source,0,fourier_info->width*
265 fourier_info->height*sizeof(*magnitude_source));
266 phase_source=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
267 fourier_info->width*sizeof(*phase_source));
268 if (magnitude_source == (double *) NULL)
270 (void) ThrowMagickException(exception,GetMagickModule(),
271 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
272 magnitude_source=(double *) RelinquishMagickMemory(magnitude_source);
275 status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height,
276 magnitude,magnitude_source);
277 if (status != MagickFalse)
278 status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height,phase,
280 CorrectPhaseLHS(fourier_info->height,fourier_info->height,phase_source);
281 if (fourier_info->modulus != MagickFalse)
284 for (y=0L; y < (ssize_t) fourier_info->height; y++)
285 for (x=0L; x < (ssize_t) fourier_info->width; x++)
287 phase_source[i]/=(2.0*MagickPI);
288 phase_source[i]+=0.5;
292 magnitude_view=AcquireCacheView(magnitude_image);
293 phase_view=AcquireCacheView(phase_image);
295 for (y=0L; y < (ssize_t) fourier_info->height; y++)
297 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
299 if (q == (PixelPacket *) NULL)
301 indexes=GetCacheViewAuthenticIndexQueue(magnitude_view);
302 for (x=0L; x < (ssize_t) fourier_info->width; x++)
304 switch (fourier_info->channel)
309 q->red=ClampToQuantum(QuantumRange*magnitude_source[i]);
314 q->green=ClampToQuantum(QuantumRange*magnitude_source[i]);
319 q->blue=ClampToQuantum(QuantumRange*magnitude_source[i]);
324 q->opacity=ClampToQuantum(QuantumRange*magnitude_source[i]);
329 indexes[x]=ClampToQuantum(QuantumRange*magnitude_source[i]);
334 q->red=ClampToQuantum(QuantumRange*magnitude_source[i]);
343 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
344 if (status == MagickFalse)
348 for (y=0L; y < (ssize_t) fourier_info->height; y++)
350 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
352 if (q == (PixelPacket *) NULL)
354 indexes=GetCacheViewAuthenticIndexQueue(phase_view);
355 for (x=0L; x < (ssize_t) fourier_info->width; x++)
357 switch (fourier_info->channel)
362 q->red=ClampToQuantum(QuantumRange*phase_source[i]);
367 q->green=ClampToQuantum(QuantumRange*phase_source[i]);
372 q->blue=ClampToQuantum(QuantumRange*phase_source[i]);
377 q->opacity=ClampToQuantum(QuantumRange*phase_source[i]);
382 indexes[x]=ClampToQuantum(QuantumRange*phase_source[i]);
387 q->red=ClampToQuantum(QuantumRange*phase_source[i]);
396 status=SyncCacheViewAuthenticPixels(phase_view,exception);
397 if (status == MagickFalse)
400 phase_view=DestroyCacheView(phase_view);
401 magnitude_view=DestroyCacheView(magnitude_view);
402 phase_source=(double *) RelinquishMagickMemory(phase_source);
403 magnitude_source=(double *) RelinquishMagickMemory(magnitude_source);
407 static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
408 const Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
426 register const IndexPacket
429 register const PixelPacket
437 Generate the forward Fourier transform.
439 source=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
440 fourier_info->width*sizeof(*source));
441 if (source == (double *) NULL)
443 (void) ThrowMagickException(exception,GetMagickModule(),
444 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
447 ResetMagickMemory(source,0,fourier_info->width*fourier_info->height*
450 image_view=AcquireCacheView(image);
451 for (y=0L; y < (ssize_t) fourier_info->height; y++)
453 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
455 if (p == (const PixelPacket *) NULL)
457 indexes=GetCacheViewVirtualIndexQueue(image_view);
458 for (x=0L; x < (ssize_t) fourier_info->width; x++)
460 switch (fourier_info->channel)
465 source[i]=QuantumScale*GetRedPixelComponent(p);
470 source[i]=QuantumScale*GetGreenPixelComponent(p);
475 source[i]=QuantumScale*GetBluePixelComponent(p);
480 source[i]=QuantumScale*GetOpacityPixelComponent(p);
485 source[i]=QuantumScale*indexes[x];
490 source[i]=QuantumScale*GetRedPixelComponent(p);
498 image_view=DestroyCacheView(image_view);
499 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info->height,
500 fourier_info->center*sizeof(*fourier));
501 if (fourier == (fftw_complex *) NULL)
503 (void) ThrowMagickException(exception,GetMagickModule(),
504 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
505 source=(double *) RelinquishMagickMemory(source);
508 #if defined(MAGICKCORE_OPENMP_SUPPORT)
509 #pragma omp critical (MagickCore_ForwardFourierTransform)
511 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->width,
512 source,fourier,FFTW_ESTIMATE);
513 fftw_execute(fftw_r2c_plan);
514 fftw_destroy_plan(fftw_r2c_plan);
515 source=(double *) RelinquishMagickMemory(source);
517 Normalize Fourier transform.
519 n=(double) fourier_info->width*(double) fourier_info->width;
521 for (y=0L; y < (ssize_t) fourier_info->height; y++)
522 for (x=0L; x < (ssize_t) fourier_info->center; x++)
524 #if defined(MAGICKCORE_HAVE_COMPLEX_H)
533 Generate magnitude and phase (or real and imaginary).
536 if (fourier_info->modulus != MagickFalse)
537 for (y=0L; y < (ssize_t) fourier_info->height; y++)
538 for (x=0L; x < (ssize_t) fourier_info->center; x++)
540 magnitude[i]=cabs(fourier[i]);
541 phase[i]=carg(fourier[i]);
545 for (y=0L; y < (ssize_t) fourier_info->height; y++)
546 for (x=0L; x < (ssize_t) fourier_info->center; x++)
548 magnitude[i]=creal(fourier[i]);
549 phase[i]=cimag(fourier[i]);
552 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
556 static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
557 const ChannelType channel,const MagickBooleanType modulus,
558 Image *fourier_image,ExceptionInfo *exception)
576 fourier_info.width=image->columns;
577 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
578 ((image->rows % 2) != 0))
580 extent=image->columns < image->rows ? image->rows : image->columns;
581 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
583 fourier_info.height=fourier_info.width;
584 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2.0)+1L;
585 fourier_info.channel=channel;
586 fourier_info.modulus=modulus;
587 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
588 fourier_info.center*sizeof(*magnitude));
589 if (magnitude == (double *) NULL)
591 (void) ThrowMagickException(exception,GetMagickModule(),
592 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
595 phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
596 fourier_info.center*sizeof(*phase));
597 if (phase == (double *) NULL)
599 (void) ThrowMagickException(exception,GetMagickModule(),
600 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
601 magnitude=(double *) RelinquishMagickMemory(magnitude);
604 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height,
605 fourier_info.center*sizeof(*fourier));
606 if (fourier == (fftw_complex *) NULL)
608 (void) ThrowMagickException(exception,GetMagickModule(),
609 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
610 phase=(double *) RelinquishMagickMemory(phase);
611 magnitude=(double *) RelinquishMagickMemory(magnitude);
614 status=ForwardFourierTransform(&fourier_info,image,magnitude,phase,exception);
615 if (status != MagickFalse)
616 status=ForwardFourier(&fourier_info,fourier_image,magnitude,phase,
618 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
619 phase=(double *) RelinquishMagickMemory(phase);
620 magnitude=(double *) RelinquishMagickMemory(magnitude);
625 MagickExport Image *ForwardFourierTransformImage(const Image *image,
626 const MagickBooleanType modulus,ExceptionInfo *exception)
631 fourier_image=NewImageList();
632 #if !defined(MAGICKCORE_FFTW_DELEGATE)
634 (void) ThrowMagickException(exception,GetMagickModule(),
635 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)",
646 width=image->columns;
647 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
648 ((image->rows % 2) != 0))
650 extent=image->columns < image->rows ? image->rows : image->columns;
651 width=(extent & 0x01) == 1 ? extent+1UL : extent;
653 magnitude_image=CloneImage(image,width,width,MagickFalse,exception);
654 if (magnitude_image != (Image *) NULL)
659 magnitude_image->storage_class=DirectClass;
660 magnitude_image->depth=32UL;
661 phase_image=CloneImage(image,width,width,MagickFalse,exception);
662 if (phase_image == (Image *) NULL)
663 magnitude_image=DestroyImage(magnitude_image);
670 phase_image->storage_class=DirectClass;
671 phase_image->depth=32UL;
672 AppendImageToList(&fourier_image,magnitude_image);
673 AppendImageToList(&fourier_image,phase_image);
675 is_gray=IsGrayImage(image,exception);
676 #if defined(MAGICKCORE_OPENMP_SUPPORT)
677 #pragma omp parallel sections
680 #if defined(MAGICKCORE_OPENMP_SUPPORT)
687 if (is_gray != MagickFalse)
688 thread_status=ForwardFourierTransformChannel(image,
689 GrayChannels,modulus,fourier_image,exception);
691 thread_status=ForwardFourierTransformChannel(image,
692 RedChannel,modulus,fourier_image,exception);
693 if (thread_status == MagickFalse)
694 status=thread_status;
696 #if defined(MAGICKCORE_OPENMP_SUPPORT)
703 thread_status=MagickTrue;
704 if (is_gray == MagickFalse)
705 thread_status=ForwardFourierTransformChannel(image,
706 GreenChannel,modulus,fourier_image,exception);
707 if (thread_status == MagickFalse)
708 status=thread_status;
710 #if defined(MAGICKCORE_OPENMP_SUPPORT)
717 thread_status=MagickTrue;
718 if (is_gray == MagickFalse)
719 thread_status=ForwardFourierTransformChannel(image,
720 BlueChannel,modulus,fourier_image,exception);
721 if (thread_status == MagickFalse)
722 status=thread_status;
724 #if defined(MAGICKCORE_OPENMP_SUPPORT)
731 thread_status=MagickTrue;
732 if (image->matte != MagickFalse)
733 thread_status=ForwardFourierTransformChannel(image,
734 OpacityChannel,modulus,fourier_image,exception);
735 if (thread_status == MagickFalse)
736 status=thread_status;
738 #if defined(MAGICKCORE_OPENMP_SUPPORT)
745 thread_status=MagickTrue;
746 if (image->colorspace == CMYKColorspace)
747 thread_status=ForwardFourierTransformChannel(image,
748 IndexChannel,modulus,fourier_image,exception);
749 if (thread_status == MagickFalse)
750 status=thread_status;
753 if (status == MagickFalse)
754 fourier_image=DestroyImageList(fourier_image);
760 return(fourier_image);
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % 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 %
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % InverseFourierTransformImage() implements the inverse discrete Fourier
775 % transform (DFT) of the image either as a magnitude / phase or real /
776 % imaginary image pair.
778 % The format of the InverseFourierTransformImage method is:
780 % Image *InverseFourierTransformImage(const Image *magnitude_image,
781 % const Image *phase_image,const MagickBooleanType modulus,
782 % ExceptionInfo *exception)
784 % A description of each parameter follows:
786 % o magnitude_image: the magnitude or real image.
788 % o phase_image: the phase or imaginary image.
790 % o modulus: if true, return transform as a magnitude / phase pair
791 % otherwise a real / imaginary image pair.
793 % o exception: return any errors or warnings in this structure.
797 #if defined(MAGICKCORE_FFTW_DELEGATE)
798 static MagickBooleanType InverseQuadrantSwap(const size_t width,
799 const size_t height,const double *source,double *destination)
811 center=(ssize_t) floor((double) width/2.0)+1L;
812 for (y=1L; y < (ssize_t) height; y++)
813 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
814 destination[center*(height-y)-x+width/2L]=source[y*width+x];
815 for (y=0L; y < (ssize_t) height; y++)
816 destination[center*y]=source[y*width+width/2L];
817 for (x=0L; x < center; x++)
818 destination[x]=source[center-x-1L];
819 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
822 static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
823 const Image *magnitude_image,const Image *phase_image,fftw_complex *fourier,
824 ExceptionInfo *exception)
842 register const IndexPacket
845 register const PixelPacket
853 Inverse fourier - read image and break down into a double array.
855 magnitude_source=(double *) AcquireQuantumMemory((size_t)
856 fourier_info->height,fourier_info->width*sizeof(*magnitude_source));
857 if (magnitude_source == (double *) NULL)
859 (void) ThrowMagickException(exception,GetMagickModule(),
860 ResourceLimitError,"MemoryAllocationFailed","`%s'",
861 magnitude_image->filename);
864 phase_source=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
865 fourier_info->height*sizeof(*phase_source));
866 if (phase_source == (double *) NULL)
868 (void) ThrowMagickException(exception,GetMagickModule(),
869 ResourceLimitError,"MemoryAllocationFailed","`%s'",
870 magnitude_image->filename);
871 magnitude_source=(double *) RelinquishMagickMemory(magnitude_source);
875 magnitude_view=AcquireCacheView(magnitude_image);
876 for (y=0L; y < (ssize_t) fourier_info->height; y++)
878 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
880 if (p == (const PixelPacket *) NULL)
882 indexes=GetCacheViewAuthenticIndexQueue(magnitude_view);
883 for (x=0L; x < (ssize_t) fourier_info->width; x++)
885 switch (fourier_info->channel)
890 magnitude_source[i]=QuantumScale*GetRedPixelComponent(p);
895 magnitude_source[i]=QuantumScale*GetGreenPixelComponent(p);
900 magnitude_source[i]=QuantumScale*GetBluePixelComponent(p);
905 magnitude_source[i]=QuantumScale*GetOpacityPixelComponent(p);
910 magnitude_source[i]=QuantumScale*indexes[x];
915 magnitude_source[i]=QuantumScale*GetRedPixelComponent(p);
924 phase_view=AcquireCacheView(phase_image);
925 for (y=0L; y < (ssize_t) fourier_info->height; y++)
927 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
929 if (p == (const PixelPacket *) NULL)
931 indexes=GetCacheViewAuthenticIndexQueue(phase_view);
932 for (x=0L; x < (ssize_t) fourier_info->width; x++)
934 switch (fourier_info->channel)
939 phase_source[i]=QuantumScale*GetRedPixelComponent(p);
944 phase_source[i]=QuantumScale*GetGreenPixelComponent(p);
949 phase_source[i]=QuantumScale*GetBluePixelComponent(p);
954 phase_source[i]=QuantumScale*GetOpacityPixelComponent(p);
959 phase_source[i]=QuantumScale*indexes[x];
964 phase_source[i]=QuantumScale*GetRedPixelComponent(p);
972 if (fourier_info->modulus != MagickFalse)
975 for (y=0L; y < (ssize_t) fourier_info->height; y++)
976 for (x=0L; x < (ssize_t) fourier_info->width; x++)
978 phase_source[i]-=0.5;
979 phase_source[i]*=(2.0*MagickPI);
983 magnitude_view=DestroyCacheView(magnitude_view);
984 phase_view=DestroyCacheView(phase_view);
985 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
986 fourier_info->center*sizeof(*magnitude));
987 if (magnitude == (double *) NULL)
989 (void) ThrowMagickException(exception,GetMagickModule(),
990 ResourceLimitError,"MemoryAllocationFailed","`%s'",
991 magnitude_image->filename);
992 magnitude_source=(double *) RelinquishMagickMemory(magnitude_source);
993 phase_source=(double *) RelinquishMagickMemory(phase_source);
996 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
997 magnitude_source,magnitude);
998 magnitude_source=(double *) RelinquishMagickMemory(magnitude_source);
999 phase=(double *) AcquireQuantumMemory((size_t) fourier_info->width,
1000 fourier_info->height*sizeof(*phase));
1001 if (phase == (double *) NULL)
1003 (void) ThrowMagickException(exception,GetMagickModule(),
1004 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1005 magnitude_image->filename);
1006 phase_source=(double *) RelinquishMagickMemory(phase_source);
1007 return(MagickFalse);
1009 CorrectPhaseLHS(fourier_info->width,fourier_info->width,phase_source);
1010 if (status != MagickFalse)
1011 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
1012 phase_source,phase);
1013 phase_source=(double *) RelinquishMagickMemory(phase_source);
1018 if (fourier_info->modulus != MagickFalse)
1019 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1020 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1022 #if defined(MAGICKCORE_HAVE_COMPLEX_H)
1023 fourier[i]=magnitude[i]*cos(phase[i])+I*magnitude[i]*sin(phase[i]);
1025 fourier[i][0]=magnitude[i]*cos(phase[i]);
1026 fourier[i][1]=magnitude[i]*sin(phase[i]);
1031 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1032 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1034 #if defined(MAGICKCORE_HAVE_COMPLEX_H)
1035 fourier[i]=magnitude[i]+I*phase[i];
1037 fourier[i][0]=magnitude[i];
1038 fourier[i][1]=phase[i];
1042 phase=(double *) RelinquishMagickMemory(phase);
1043 magnitude=(double *) RelinquishMagickMemory(magnitude);
1047 static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
1048 fftw_complex *fourier,Image *image,ExceptionInfo *exception)
1062 register IndexPacket
1069 register PixelPacket
1072 source=(double *) AcquireQuantumMemory((size_t) fourier_info->width,
1073 fourier_info->height*sizeof(double));
1074 if (source == (double *) NULL)
1076 (void) ThrowMagickException(exception,GetMagickModule(),
1077 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1078 return(MagickFalse);
1080 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1081 #pragma omp critical (MagickCore_InverseFourierTransform)
1084 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
1085 fourier,source,FFTW_ESTIMATE);
1086 fftw_execute(fftw_c2r_plan);
1087 fftw_destroy_plan(fftw_c2r_plan);
1090 image_view=AcquireCacheView(image);
1091 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1093 if (y >= (ssize_t) image->rows)
1095 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1096 image->columns ? image->columns : fourier_info->width,1UL,exception);
1097 if (q == (PixelPacket *) NULL)
1099 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1100 for (x=0L; x < (ssize_t) fourier_info->width; x++)
1102 switch (fourier_info->channel)
1107 q->red=ClampToQuantum(QuantumRange*source[i]);
1112 q->green=ClampToQuantum(QuantumRange*source[i]);
1117 q->blue=ClampToQuantum(QuantumRange*source[i]);
1120 case OpacityChannel:
1122 q->opacity=ClampToQuantum(QuantumRange*source[i]);
1127 indexes[x]=ClampToQuantum(QuantumRange*source[i]);
1132 q->red=ClampToQuantum(QuantumRange*source[i]);
1141 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1144 image_view=DestroyCacheView(image_view);
1145 source=(double *) RelinquishMagickMemory(source);
1149 static MagickBooleanType InverseFourierTransformChannel(
1150 const Image *magnitude_image,const Image *phase_image,
1151 const ChannelType channel,const MagickBooleanType modulus,
1152 Image *fourier_image,ExceptionInfo *exception)
1170 fourier_info.width=magnitude_image->columns;
1171 if ((magnitude_image->columns != magnitude_image->rows) ||
1172 ((magnitude_image->columns % 2) != 0) ||
1173 ((magnitude_image->rows % 2) != 0))
1175 extent=magnitude_image->columns < magnitude_image->rows ?
1176 magnitude_image->rows : magnitude_image->columns;
1177 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1179 fourier_info.height=fourier_info.width;
1180 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2.0)+1L;
1181 fourier_info.channel=channel;
1182 fourier_info.modulus=modulus;
1183 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
1184 fourier_info.center*sizeof(double));
1185 if (magnitude == (double *) NULL)
1187 (void) ThrowMagickException(exception,GetMagickModule(),
1188 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1189 magnitude_image->filename);
1190 return(MagickFalse);
1192 phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
1193 fourier_info.center*sizeof(double));
1194 if (phase == (double *) NULL)
1196 (void) ThrowMagickException(exception,GetMagickModule(),
1197 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1198 magnitude_image->filename);
1199 magnitude=(double *) RelinquishMagickMemory(magnitude);
1200 return(MagickFalse);
1202 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height,
1203 fourier_info.center*sizeof(*fourier));
1204 if (fourier == (fftw_complex *) NULL)
1206 (void) ThrowMagickException(exception,GetMagickModule(),
1207 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1208 magnitude_image->filename);
1209 phase=(double *) RelinquishMagickMemory(phase);
1210 magnitude=(double *) RelinquishMagickMemory(magnitude);
1211 return(MagickFalse);
1213 status=InverseFourier(&fourier_info,magnitude_image,phase_image,fourier,
1215 if (status != MagickFalse)
1216 status=InverseFourierTransform(&fourier_info,fourier,fourier_image,
1218 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
1219 phase=(double *) RelinquishMagickMemory(phase);
1220 magnitude=(double *) RelinquishMagickMemory(magnitude);
1225 MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1226 const Image *phase_image,const MagickBooleanType modulus,
1227 ExceptionInfo *exception)
1232 assert(magnitude_image != (Image *) NULL);
1233 assert(magnitude_image->signature == MagickSignature);
1234 if (magnitude_image->debug != MagickFalse)
1235 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1236 magnitude_image->filename);
1237 if (phase_image == (Image *) NULL)
1239 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1240 "ImageSequenceRequired","`%s'",magnitude_image->filename);
1241 return((Image *) NULL);
1243 #if !defined(MAGICKCORE_FFTW_DELEGATE)
1244 fourier_image=(Image *) NULL;
1246 (void) ThrowMagickException(exception,GetMagickModule(),
1247 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)",
1248 magnitude_image->filename);
1251 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
1252 magnitude_image->rows,MagickFalse,exception);
1253 if (fourier_image != (Image *) NULL)
1260 is_gray=IsGrayImage(magnitude_image,exception);
1261 if (is_gray != MagickFalse)
1262 is_gray=IsGrayImage(phase_image,exception);
1263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1264 #pragma omp parallel sections
1267 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1274 if (is_gray != MagickFalse)
1275 thread_status=InverseFourierTransformChannel(magnitude_image,
1276 phase_image,GrayChannels,modulus,fourier_image,exception);
1278 thread_status=InverseFourierTransformChannel(magnitude_image,
1279 phase_image,RedChannel,modulus,fourier_image,exception);
1280 if (thread_status == MagickFalse)
1281 status=thread_status;
1283 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1290 thread_status=MagickTrue;
1291 if (is_gray == MagickFalse)
1292 thread_status=InverseFourierTransformChannel(magnitude_image,
1293 phase_image,GreenChannel,modulus,fourier_image,exception);
1294 if (thread_status == MagickFalse)
1295 status=thread_status;
1297 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1304 thread_status=MagickTrue;
1305 if (is_gray == MagickFalse)
1306 thread_status=InverseFourierTransformChannel(magnitude_image,
1307 phase_image,BlueChannel,modulus,fourier_image,exception);
1308 if (thread_status == MagickFalse)
1309 status=thread_status;
1311 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1318 thread_status=MagickTrue;
1319 if (magnitude_image->matte != MagickFalse)
1320 thread_status=InverseFourierTransformChannel(magnitude_image,
1321 phase_image,OpacityChannel,modulus,fourier_image,exception);
1322 if (thread_status == MagickFalse)
1323 status=thread_status;
1325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1332 thread_status=MagickTrue;
1333 if (magnitude_image->colorspace == CMYKColorspace)
1334 thread_status=InverseFourierTransformChannel(magnitude_image,
1335 phase_image,IndexChannel,modulus,fourier_image,exception);
1336 if (thread_status == MagickFalse)
1337 status=thread_status;
1340 if (status == MagickFalse)
1341 fourier_image=DestroyImage(fourier_image);
1346 return(fourier_image);