blob: cff3c1b0df0747e457fe16ac552abdb64f4bef5b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
19% July 2009 %
20% %
21% %
cristy45ef08f2012-12-07 13:13:34 +000022% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000023% 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% http://www.imagemagick.org/script/license.php %
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*/
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/studio.h"
46#include "MagickCore/attribute.h"
cristy7d4aa382013-06-30 01:59:39 +000047#include "MagickCore/blob.h"
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/cache.h"
49#include "MagickCore/image.h"
50#include "MagickCore/image-private.h"
51#include "MagickCore/list.h"
52#include "MagickCore/fourier.h"
53#include "MagickCore/log.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/monitor.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/property.h"
58#include "MagickCore/quantum-private.h"
cristyac245f82012-05-05 17:13:57 +000059#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000061#if defined(MAGICKCORE_FFTW_DELEGATE)
cristy56ed31c2010-03-22 00:46:21 +000062#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy3ed852e2009-09-05 21:47:34 +000063#include <complex.h>
cristy56ed31c2010-03-22 00:46:21 +000064#endif
cristy3ed852e2009-09-05 21:47:34 +000065#include <fftw3.h>
cristy47b022b2011-01-18 22:29:21 +000066#if !defined(MAGICKCORE_HAVE_CABS)
67#define cabs(z) (sqrt(z[0]*z[0]+z[1]*z[1]))
68#endif
69#if !defined(MAGICKCORE_HAVE_CARG)
cristy4da3ba32011-02-07 16:58:11 +000070#define carg(z) (atan2(cimag(z),creal(z)))
cristy47b022b2011-01-18 22:29:21 +000071#endif
72#if !defined(MAGICKCORE_HAVE_CIMAG)
73#define cimag(z) (z[1])
74#endif
75#if !defined(MAGICKCORE_HAVE_CREAL)
76#define creal(z) (z[0])
77#endif
cristy3ed852e2009-09-05 21:47:34 +000078#endif
79
80/*
81 Typedef declarations.
82*/
83typedef struct _FourierInfo
84{
cristyd3090f92011-10-18 00:05:15 +000085 PixelChannel
cristy3ed852e2009-09-05 21:47:34 +000086 channel;
87
88 MagickBooleanType
89 modulus;
90
cristybb503372010-05-27 20:51:26 +000091 size_t
cristy3ed852e2009-09-05 21:47:34 +000092 width,
93 height;
94
cristybb503372010-05-27 20:51:26 +000095 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000096 center;
97} FourierInfo;
98
99/*
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101% %
102% %
103% %
104% 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 %
105% %
106% %
107% %
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109%
110% ForwardFourierTransformImage() implements the discrete Fourier transform
111% (DFT) of the image either as a magnitude / phase or real / imaginary image
112% pair.
113%
114% The format of the ForwadFourierTransformImage method is:
115%
116% Image *ForwardFourierTransformImage(const Image *image,
117% const MagickBooleanType modulus,ExceptionInfo *exception)
118%
119% A description of each parameter follows:
120%
121% o image: the image.
122%
123% o modulus: if true, return as transform as a magnitude / phase pair
124% otherwise a real / imaginary image pair.
125%
126% o exception: return any errors or warnings in this structure.
127%
128*/
129
130#if defined(MAGICKCORE_FFTW_DELEGATE)
131
cristyc4ea4a42011-01-24 01:43:30 +0000132static MagickBooleanType RollFourier(const size_t width,const size_t height,
cristybb3c02e2013-07-02 00:43:10 +0000133 const ssize_t x_offset,const ssize_t y_offset,double *fourier_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000134{
135 double
cristybb3c02e2013-07-02 00:43:10 +0000136 *roll_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000137
cristy7d4aa382013-06-30 01:59:39 +0000138 MemoryInfo
139 *roll_info;
140
cristyc4ea4a42011-01-24 01:43:30 +0000141 register ssize_t
142 i,
143 x;
144
cristybb503372010-05-27 20:51:26 +0000145 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000146 u,
147 v,
148 y;
149
cristy3ed852e2009-09-05 21:47:34 +0000150 /*
cristy2da05352010-09-15 19:22:17 +0000151 Move zero frequency (DC, average color) from (0,0) to (width/2,height/2).
cristy3ed852e2009-09-05 21:47:34 +0000152 */
cristybb3c02e2013-07-02 00:43:10 +0000153 roll_info=AcquireVirtualMemory(height,width*sizeof(*roll_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000154 if (roll_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000155 return(MagickFalse);
cristybb3c02e2013-07-02 00:43:10 +0000156 roll_pixels=(double *) GetVirtualMemoryBlob(roll_info);
cristy3ed852e2009-09-05 21:47:34 +0000157 i=0L;
cristybb503372010-05-27 20:51:26 +0000158 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000159 {
160 if (y_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000161 v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset;
cristy3ed852e2009-09-05 21:47:34 +0000162 else
cristybb503372010-05-27 20:51:26 +0000163 v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height :
cristy3ed852e2009-09-05 21:47:34 +0000164 y+y_offset;
cristybb503372010-05-27 20:51:26 +0000165 for (x=0L; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000166 {
167 if (x_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000168 u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset;
cristy3ed852e2009-09-05 21:47:34 +0000169 else
cristybb503372010-05-27 20:51:26 +0000170 u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width :
cristy3ed852e2009-09-05 21:47:34 +0000171 x+x_offset;
cristybb3c02e2013-07-02 00:43:10 +0000172 roll_pixels[v*width+u]=fourier_pixels[i++];
cristyc4ea4a42011-01-24 01:43:30 +0000173 }
cristy3ed852e2009-09-05 21:47:34 +0000174 }
cristybb3c02e2013-07-02 00:43:10 +0000175 (void) CopyMagickMemory(fourier_pixels,roll_pixels,height*width*
176 sizeof(*roll_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000177 roll_info=RelinquishVirtualMemory(roll_info);
cristy3ed852e2009-09-05 21:47:34 +0000178 return(MagickTrue);
179}
180
cristybb503372010-05-27 20:51:26 +0000181static MagickBooleanType ForwardQuadrantSwap(const size_t width,
182 const size_t height,double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +0000183{
cristy3ed852e2009-09-05 21:47:34 +0000184 MagickBooleanType
185 status;
186
cristybb503372010-05-27 20:51:26 +0000187 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000188 x;
189
cristyc4ea4a42011-01-24 01:43:30 +0000190 ssize_t
191 center,
192 y;
193
cristy3ed852e2009-09-05 21:47:34 +0000194 /*
195 Swap quadrants.
196 */
cristybb503372010-05-27 20:51:26 +0000197 center=(ssize_t) floor((double) width/2L)+1L;
198 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,source);
cristy3ed852e2009-09-05 21:47:34 +0000199 if (status == MagickFalse)
200 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000201 for (y=0L; y < (ssize_t) height; y++)
202 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000203 destination[width*y+x+width/2L]=source[center*y+x];
cristybb503372010-05-27 20:51:26 +0000204 for (y=1; y < (ssize_t) height; y++)
205 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000206 destination[width*(height-y)+width/2L-x-1L]=source[center*y+x+1L];
cristybb503372010-05-27 20:51:26 +0000207 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000208 destination[-x+width/2L-1L]=destination[x+width/2L+1L];
209 return(MagickTrue);
210}
211
cristyc4ea4a42011-01-24 01:43:30 +0000212static void CorrectPhaseLHS(const size_t width,const size_t height,
213 double *fourier)
cristy3ed852e2009-09-05 21:47:34 +0000214{
cristybb503372010-05-27 20:51:26 +0000215 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000216 x;
217
cristy9d314ff2011-03-09 01:30:28 +0000218 ssize_t
219 y;
220
cristybb503372010-05-27 20:51:26 +0000221 for (y=0L; y < (ssize_t) height; y++)
222 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000223 fourier[y*width+x]*=(-1.0);
224}
225
226static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
227 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
228{
229 CacheView
230 *magnitude_view,
231 *phase_view;
232
233 double
cristy7d4aa382013-06-30 01:59:39 +0000234 *magnitude_pixels,
235 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000236
237 Image
238 *magnitude_image,
239 *phase_image;
240
cristy3ed852e2009-09-05 21:47:34 +0000241 MagickBooleanType
242 status;
243
cristy7d4aa382013-06-30 01:59:39 +0000244 MemoryInfo
245 *magnitude_info,
246 *phase_info;
247
cristy4c08aed2011-07-01 19:47:50 +0000248 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000249 *q;
250
cristyf5742792012-11-27 18:31:26 +0000251 register ssize_t
252 x;
253
cristyc4ea4a42011-01-24 01:43:30 +0000254 ssize_t
255 i,
256 y;
257
cristy3ed852e2009-09-05 21:47:34 +0000258 magnitude_image=GetFirstImageInList(image);
259 phase_image=GetNextImageInList(image);
260 if (phase_image == (Image *) NULL)
261 {
262 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +0000263 "TwoOrMoreImagesRequired","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000264 return(MagickFalse);
265 }
266 /*
267 Create "Fourier Transform" image from constituent arrays.
268 */
cristy7d4aa382013-06-30 01:59:39 +0000269 magnitude_info=AcquireVirtualMemory((size_t) fourier_info->height,
270 fourier_info->width*sizeof(*magnitude_pixels));
271 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
272 fourier_info->width*sizeof(*phase_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000273 if ((magnitude_info == (MemoryInfo *) NULL) ||
274 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000275 {
cristy7d4aa382013-06-30 01:59:39 +0000276 if (phase_info != (MemoryInfo *) NULL)
277 phase_info=RelinquishVirtualMemory(phase_info);
278 if (magnitude_info != (MemoryInfo *) NULL)
279 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000280 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000281 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000282 return(MagickFalse);
283 }
cristy7d4aa382013-06-30 01:59:39 +0000284 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
285 (void) ResetMagickMemory(magnitude_pixels,0,fourier_info->height*
286 fourier_info->width*sizeof(*magnitude_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000287 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
cristy7d4aa382013-06-30 01:59:39 +0000288 (void) ResetMagickMemory(phase_pixels,0,fourier_info->height*
289 fourier_info->width*sizeof(*phase_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000290 status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height,
cristy7d4aa382013-06-30 01:59:39 +0000291 magnitude,magnitude_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000292 if (status != MagickFalse)
293 status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height,phase,
cristy7d4aa382013-06-30 01:59:39 +0000294 phase_pixels);
295 CorrectPhaseLHS(fourier_info->height,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000296 if (fourier_info->modulus != MagickFalse)
297 {
298 i=0L;
cristybb503372010-05-27 20:51:26 +0000299 for (y=0L; y < (ssize_t) fourier_info->height; y++)
300 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000301 {
cristy7d4aa382013-06-30 01:59:39 +0000302 phase_pixels[i]/=(2.0*MagickPI);
303 phase_pixels[i]+=0.5;
cristy3ed852e2009-09-05 21:47:34 +0000304 i++;
305 }
306 }
cristy46ff2672012-12-14 15:32:26 +0000307 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000308 i=0L;
cristybb503372010-05-27 20:51:26 +0000309 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000310 {
311 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
312 exception);
cristyacd2ed22011-08-30 01:44:23 +0000313 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000314 break;
cristybb503372010-05-27 20:51:26 +0000315 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000316 {
317 switch (fourier_info->channel)
318 {
cristyd3090f92011-10-18 00:05:15 +0000319 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000320 default:
321 {
cristy4c08aed2011-07-01 19:47:50 +0000322 SetPixelRed(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000323 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000324 break;
325 }
cristyd3090f92011-10-18 00:05:15 +0000326 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000327 {
cristy4c08aed2011-07-01 19:47:50 +0000328 SetPixelGreen(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000329 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000330 break;
331 }
cristyd3090f92011-10-18 00:05:15 +0000332 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000333 {
cristy4c08aed2011-07-01 19:47:50 +0000334 SetPixelBlue(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000335 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000336 break;
337 }
cristyd3090f92011-10-18 00:05:15 +0000338 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000339 {
cristy4c08aed2011-07-01 19:47:50 +0000340 SetPixelBlack(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000341 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000342 break;
343 }
cristyd3090f92011-10-18 00:05:15 +0000344 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000345 {
cristy4c08aed2011-07-01 19:47:50 +0000346 SetPixelAlpha(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000347 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000348 break;
349 }
cristy3ed852e2009-09-05 21:47:34 +0000350 }
351 i++;
cristyed231572011-07-14 02:18:59 +0000352 q+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000353 }
354 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
355 if (status == MagickFalse)
356 break;
357 }
cristydb070952012-04-20 14:33:00 +0000358 magnitude_view=DestroyCacheView(magnitude_view);
cristy3ed852e2009-09-05 21:47:34 +0000359 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000360 phase_view=AcquireAuthenticCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000361 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000362 {
363 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
364 exception);
cristyacd2ed22011-08-30 01:44:23 +0000365 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000366 break;
cristybb503372010-05-27 20:51:26 +0000367 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000368 {
369 switch (fourier_info->channel)
370 {
cristyd3090f92011-10-18 00:05:15 +0000371 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000372 default:
373 {
cristy4c08aed2011-07-01 19:47:50 +0000374 SetPixelRed(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000375 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000376 break;
377 }
cristyd3090f92011-10-18 00:05:15 +0000378 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000379 {
cristy4c08aed2011-07-01 19:47:50 +0000380 SetPixelGreen(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000381 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000382 break;
383 }
cristyd3090f92011-10-18 00:05:15 +0000384 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000385 {
cristy4c08aed2011-07-01 19:47:50 +0000386 SetPixelBlue(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000387 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000388 break;
389 }
cristyd3090f92011-10-18 00:05:15 +0000390 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000391 {
cristy4c08aed2011-07-01 19:47:50 +0000392 SetPixelBlack(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000393 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000394 break;
395 }
cristyd3090f92011-10-18 00:05:15 +0000396 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000397 {
cristy4c08aed2011-07-01 19:47:50 +0000398 SetPixelAlpha(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000399 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000400 break;
401 }
cristy3ed852e2009-09-05 21:47:34 +0000402 }
403 i++;
cristyed231572011-07-14 02:18:59 +0000404 q+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000405 }
406 status=SyncCacheViewAuthenticPixels(phase_view,exception);
407 if (status == MagickFalse)
408 break;
409 }
410 phase_view=DestroyCacheView(phase_view);
cristy7d4aa382013-06-30 01:59:39 +0000411 phase_info=RelinquishVirtualMemory(phase_info);
412 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000413 return(status);
414}
415
416static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
417 const Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
418{
419 CacheView
420 *image_view;
421
422 double
423 n,
cristybb3c02e2013-07-02 00:43:10 +0000424 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000425
426 fftw_complex
cristy5f13d952013-07-02 14:36:09 +0000427 *destination_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000428
429 fftw_plan
430 fftw_r2c_plan;
431
cristy7d4aa382013-06-30 01:59:39 +0000432 MemoryInfo
cristy5f13d952013-07-02 14:36:09 +0000433 *destination_info,
cristy7d4aa382013-06-30 01:59:39 +0000434 *source_info;
435
cristy4c08aed2011-07-01 19:47:50 +0000436 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000437 *p;
438
cristybb503372010-05-27 20:51:26 +0000439 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000440 i,
441 x;
442
cristyc4ea4a42011-01-24 01:43:30 +0000443 ssize_t
444 y;
445
cristy3ed852e2009-09-05 21:47:34 +0000446 /*
447 Generate the forward Fourier transform.
448 */
cristy7d4aa382013-06-30 01:59:39 +0000449 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000450 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000451 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000452 {
453 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000454 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000455 return(MagickFalse);
456 }
cristybb3c02e2013-07-02 00:43:10 +0000457 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
458 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
459 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000460 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000461 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000462 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000463 {
464 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
465 exception);
cristy4c08aed2011-07-01 19:47:50 +0000466 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000467 break;
cristybb503372010-05-27 20:51:26 +0000468 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
470 switch (fourier_info->channel)
471 {
cristyd3090f92011-10-18 00:05:15 +0000472 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000473 default:
474 {
cristybb3c02e2013-07-02 00:43:10 +0000475 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000476 break;
477 }
cristyd3090f92011-10-18 00:05:15 +0000478 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000479 {
cristybb3c02e2013-07-02 00:43:10 +0000480 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000481 break;
482 }
cristyd3090f92011-10-18 00:05:15 +0000483 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000484 {
cristybb3c02e2013-07-02 00:43:10 +0000485 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000486 break;
487 }
cristyd3090f92011-10-18 00:05:15 +0000488 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000489 {
cristybb3c02e2013-07-02 00:43:10 +0000490 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000491 break;
492 }
cristyd3090f92011-10-18 00:05:15 +0000493 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000494 {
cristybb3c02e2013-07-02 00:43:10 +0000495 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000496 break;
497 }
cristy3ed852e2009-09-05 21:47:34 +0000498 }
499 i++;
cristyed231572011-07-14 02:18:59 +0000500 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000501 }
502 }
503 image_view=DestroyCacheView(image_view);
cristy5f13d952013-07-02 14:36:09 +0000504 destination_info=AcquireVirtualMemory((size_t) fourier_info->height,
505 fourier_info->center*sizeof(*destination_pixels));
506 if (destination_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000507 {
508 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000509 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000510 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000511 return(MagickFalse);
512 }
cristy5f13d952013-07-02 14:36:09 +0000513 destination_pixels=(fftw_complex *) GetVirtualMemoryBlob(destination_info);
cristyb5d5f722009-11-04 03:03:49 +0000514#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000515 #pragma omp critical (MagickCore_ForwardFourierTransform)
516#endif
cristy70841a12012-10-27 20:52:57 +0000517 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristy5f13d952013-07-02 14:36:09 +0000518 source_pixels,destination_pixels,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000519 fftw_execute(fftw_r2c_plan);
520 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000521 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000522 /*
523 Normalize Fourier transform.
524 */
525 n=(double) fourier_info->width*(double) fourier_info->width;
526 i=0L;
cristybb503372010-05-27 20:51:26 +0000527 for (y=0L; y < (ssize_t) fourier_info->height; y++)
528 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy56ed31c2010-03-22 00:46:21 +0000529 {
530#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy5f13d952013-07-02 14:36:09 +0000531 destination_pixels[i]/=n;
cristy56ed31c2010-03-22 00:46:21 +0000532#else
cristy5f13d952013-07-02 14:36:09 +0000533 destination_pixels[i][0]/=n;
534 destination_pixels[i][1]/=n;
cristy56ed31c2010-03-22 00:46:21 +0000535#endif
536 i++;
537 }
cristy3ed852e2009-09-05 21:47:34 +0000538 /*
539 Generate magnitude and phase (or real and imaginary).
540 */
541 i=0L;
542 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000543 for (y=0L; y < (ssize_t) fourier_info->height; y++)
544 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristy5f13d952013-07-02 14:36:09 +0000546 magnitude[i]=cabs(destination_pixels[i]);
547 phase[i]=carg(destination_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000548 i++;
549 }
550 else
cristybb503372010-05-27 20:51:26 +0000551 for (y=0L; y < (ssize_t) fourier_info->height; y++)
552 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000553 {
cristy5f13d952013-07-02 14:36:09 +0000554 magnitude[i]=creal(destination_pixels[i]);
555 phase[i]=cimag(destination_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000556 i++;
557 }
cristy5f13d952013-07-02 14:36:09 +0000558 destination_info=(MemoryInfo *) RelinquishVirtualMemory(destination_info);
cristy3ed852e2009-09-05 21:47:34 +0000559 return(MagickTrue);
560}
561
562static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000563 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000564 Image *fourier_image,ExceptionInfo *exception)
565{
566 double
cristyce9fe782013-07-03 00:59:41 +0000567 *magnitude_pixels,
568 *phase_pixels;
cristybb3c02e2013-07-02 00:43:10 +0000569
cristy56ed31c2010-03-22 00:46:21 +0000570 FourierInfo
571 fourier_info;
572
cristyc4ea4a42011-01-24 01:43:30 +0000573 MagickBooleanType
574 status;
575
cristyce9fe782013-07-03 00:59:41 +0000576 MemoryInfo
577 *magnitude_info,
578 *phase_info;
579
cristy3ed852e2009-09-05 21:47:34 +0000580 size_t
581 extent;
582
583 fourier_info.width=image->columns;
584 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
585 ((image->rows % 2) != 0))
586 {
587 extent=image->columns < image->rows ? image->rows : image->columns;
588 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
589 }
590 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000591 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000592 fourier_info.channel=channel;
593 fourier_info.modulus=modulus;
cristyce9fe782013-07-03 00:59:41 +0000594 magnitude_info=AcquireVirtualMemory((size_t) fourier_info.height,
595 fourier_info.center*sizeof(*magnitude_pixels));
596 phase_info=AcquireVirtualMemory((size_t) fourier_info.height,
597 fourier_info.center*sizeof(*phase_pixels));
598 if ((magnitude_info == (MemoryInfo *) NULL) ||
599 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000600 {
cristyce9fe782013-07-03 00:59:41 +0000601 if (phase_info != (MemoryInfo *) NULL)
602 phase_info=RelinquishVirtualMemory(phase_info);
603 if (magnitude_info == (MemoryInfo *) NULL)
604 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000605 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000606 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000607 return(MagickFalse);
608 }
cristyce9fe782013-07-03 00:59:41 +0000609 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
610 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
611 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
612 phase_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000613 if (status != MagickFalse)
cristyce9fe782013-07-03 00:59:41 +0000614 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
615 phase_pixels,exception);
616 phase_info=RelinquishVirtualMemory(phase_info);
617 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000618 return(status);
619}
620#endif
621
622MagickExport Image *ForwardFourierTransformImage(const Image *image,
623 const MagickBooleanType modulus,ExceptionInfo *exception)
624{
625 Image
626 *fourier_image;
627
628 fourier_image=NewImageList();
629#if !defined(MAGICKCORE_FFTW_DELEGATE)
630 (void) modulus;
631 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000632 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000633 image->filename);
634#else
635 {
636 Image
637 *magnitude_image;
638
cristybb503372010-05-27 20:51:26 +0000639 size_t
cristy3ed852e2009-09-05 21:47:34 +0000640 extent,
641 width;
642
643 width=image->columns;
644 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
645 ((image->rows % 2) != 0))
646 {
647 extent=image->columns < image->rows ? image->rows : image->columns;
648 width=(extent & 0x01) == 1 ? extent+1UL : extent;
649 }
650 magnitude_image=CloneImage(image,width,width,MagickFalse,exception);
651 if (magnitude_image != (Image *) NULL)
652 {
653 Image
654 *phase_image;
655
656 magnitude_image->storage_class=DirectClass;
657 magnitude_image->depth=32UL;
658 phase_image=CloneImage(image,width,width,MagickFalse,exception);
659 if (phase_image == (Image *) NULL)
660 magnitude_image=DestroyImage(magnitude_image);
661 else
662 {
663 MagickBooleanType
664 is_gray,
665 status;
666
cristy3ed852e2009-09-05 21:47:34 +0000667 phase_image->storage_class=DirectClass;
668 phase_image->depth=32UL;
669 AppendImageToList(&fourier_image,magnitude_image);
670 AppendImageToList(&fourier_image,phase_image);
671 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000672 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000673#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000674 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000675#endif
cristy3ed852e2009-09-05 21:47:34 +0000676 {
cristyb34ef052010-10-07 00:12:05 +0000677#if defined(MAGICKCORE_OPENMP_SUPPORT)
678 #pragma omp section
679#endif
cristy3ed852e2009-09-05 21:47:34 +0000680 {
cristyb34ef052010-10-07 00:12:05 +0000681 MagickBooleanType
682 thread_status;
683
684 if (is_gray != MagickFalse)
685 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000686 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000687 else
688 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000689 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000690 if (thread_status == MagickFalse)
691 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000692 }
cristyb34ef052010-10-07 00:12:05 +0000693#if defined(MAGICKCORE_OPENMP_SUPPORT)
694 #pragma omp section
695#endif
696 {
697 MagickBooleanType
698 thread_status;
699
700 thread_status=MagickTrue;
701 if (is_gray == MagickFalse)
702 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000703 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000704 if (thread_status == MagickFalse)
705 status=thread_status;
706 }
707#if defined(MAGICKCORE_OPENMP_SUPPORT)
708 #pragma omp section
709#endif
710 {
711 MagickBooleanType
712 thread_status;
713
714 thread_status=MagickTrue;
715 if (is_gray == MagickFalse)
716 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000717 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000718 if (thread_status == MagickFalse)
719 status=thread_status;
720 }
721#if defined(MAGICKCORE_OPENMP_SUPPORT)
722 #pragma omp section
723#endif
724 {
725 MagickBooleanType
726 thread_status;
727
728 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000729 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000730 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000731 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000732 if (thread_status == MagickFalse)
733 status=thread_status;
734 }
735#if defined(MAGICKCORE_OPENMP_SUPPORT)
736 #pragma omp section
737#endif
738 {
739 MagickBooleanType
740 thread_status;
741
742 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +0000743 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +0000744 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000745 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000746 if (thread_status == MagickFalse)
747 status=thread_status;
748 }
cristy3ed852e2009-09-05 21:47:34 +0000749 }
750 if (status == MagickFalse)
751 fourier_image=DestroyImageList(fourier_image);
752 fftw_cleanup();
753 }
754 }
755 }
756#endif
757 return(fourier_image);
758}
759
760/*
761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762% %
763% %
764% %
765% 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 %
766% %
767% %
768% %
769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770%
771% InverseFourierTransformImage() implements the inverse discrete Fourier
772% transform (DFT) of the image either as a magnitude / phase or real /
773% imaginary image pair.
774%
775% The format of the InverseFourierTransformImage method is:
776%
cristyc9550792009-11-13 20:05:42 +0000777% Image *InverseFourierTransformImage(const Image *magnitude_image,
778% const Image *phase_image,const MagickBooleanType modulus,
779% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000780%
781% A description of each parameter follows:
782%
cristyc9550792009-11-13 20:05:42 +0000783% o magnitude_image: the magnitude or real image.
784%
785% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +0000786%
787% o modulus: if true, return transform as a magnitude / phase pair
788% otherwise a real / imaginary image pair.
789%
790% o exception: return any errors or warnings in this structure.
791%
792*/
793
794#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +0000795static MagickBooleanType InverseQuadrantSwap(const size_t width,
796 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +0000797{
cristyc4ea4a42011-01-24 01:43:30 +0000798 register ssize_t
799 x;
800
cristybb503372010-05-27 20:51:26 +0000801 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000802 center,
803 y;
804
cristy3ed852e2009-09-05 21:47:34 +0000805 /*
806 Swap quadrants.
807 */
cristy233fe582012-07-07 14:00:18 +0000808 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +0000809 for (y=1L; y < (ssize_t) height; y++)
810 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000811 destination[center*(height-y)-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +0000812 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000813 destination[center*y]=source[y*width+width/2L];
814 for (x=0L; x < center; x++)
815 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +0000816 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +0000817}
818
819static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristyc9550792009-11-13 20:05:42 +0000820 const Image *magnitude_image,const Image *phase_image,fftw_complex *fourier,
821 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000822{
823 CacheView
824 *magnitude_view,
825 *phase_view;
826
827 double
cristybb3c02e2013-07-02 00:43:10 +0000828 *magnitude,
829 *phase,
cristy7d4aa382013-06-30 01:59:39 +0000830 *magnitude_pixels,
831 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000832
cristy3ed852e2009-09-05 21:47:34 +0000833 MagickBooleanType
834 status;
835
cristy4c08aed2011-07-01 19:47:50 +0000836 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000837 *p;
838
cristybb503372010-05-27 20:51:26 +0000839 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000840 i,
841 x;
842
cristyc4ea4a42011-01-24 01:43:30 +0000843 ssize_t
844 y;
845
cristy3ed852e2009-09-05 21:47:34 +0000846 /*
847 Inverse fourier - read image and break down into a double array.
848 */
cristybb3c02e2013-07-02 00:43:10 +0000849 magnitude_pixels=(double *) AcquireQuantumMemory((size_t)
850 fourier_info->height,fourier_info->width*sizeof(*magnitude_pixels));
851 if (magnitude_pixels == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000852 {
853 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000854 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +0000855 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000856 return(MagickFalse);
857 }
cristybb3c02e2013-07-02 00:43:10 +0000858 phase_pixels=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
859 fourier_info->width*sizeof(*phase_pixels));
860 if (phase_pixels == (double *) NULL)
861 {
862 (void) ThrowMagickException(exception,GetMagickModule(),
863 ResourceLimitError,"MemoryAllocationFailed","`%s'",
864 magnitude_image->filename);
865 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
866 return(MagickFalse);
867 }
cristy3ed852e2009-09-05 21:47:34 +0000868 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000869 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +0000870 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000871 {
872 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
873 exception);
cristy4c08aed2011-07-01 19:47:50 +0000874 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000875 break;
cristybb503372010-05-27 20:51:26 +0000876 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000877 {
878 switch (fourier_info->channel)
879 {
cristyd3090f92011-10-18 00:05:15 +0000880 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000881 default:
882 {
cristy7d4aa382013-06-30 01:59:39 +0000883 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000884 break;
885 }
cristyd3090f92011-10-18 00:05:15 +0000886 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000887 {
cristy7d4aa382013-06-30 01:59:39 +0000888 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000889 break;
890 }
cristyd3090f92011-10-18 00:05:15 +0000891 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000892 {
cristy7d4aa382013-06-30 01:59:39 +0000893 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000894 break;
895 }
cristyd3090f92011-10-18 00:05:15 +0000896 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000897 {
cristy7d4aa382013-06-30 01:59:39 +0000898 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000899 break;
900 }
cristyd3090f92011-10-18 00:05:15 +0000901 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000902 {
cristy7d4aa382013-06-30 01:59:39 +0000903 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000904 break;
905 }
cristy3ed852e2009-09-05 21:47:34 +0000906 }
907 i++;
cristyed231572011-07-14 02:18:59 +0000908 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000909 }
910 }
911 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000912 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000913 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000914 {
915 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
916 exception);
cristy4c08aed2011-07-01 19:47:50 +0000917 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000918 break;
cristybb503372010-05-27 20:51:26 +0000919 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000920 {
921 switch (fourier_info->channel)
922 {
cristyd3090f92011-10-18 00:05:15 +0000923 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000924 default:
925 {
cristy7d4aa382013-06-30 01:59:39 +0000926 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000927 break;
928 }
cristyd3090f92011-10-18 00:05:15 +0000929 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000930 {
cristy7d4aa382013-06-30 01:59:39 +0000931 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000932 break;
933 }
cristyd3090f92011-10-18 00:05:15 +0000934 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000935 {
cristy7d4aa382013-06-30 01:59:39 +0000936 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000937 break;
938 }
cristyd3090f92011-10-18 00:05:15 +0000939 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000940 {
cristy7d4aa382013-06-30 01:59:39 +0000941 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000942 break;
943 }
cristyd3090f92011-10-18 00:05:15 +0000944 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000945 {
cristy7d4aa382013-06-30 01:59:39 +0000946 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000947 break;
948 }
cristy3ed852e2009-09-05 21:47:34 +0000949 }
950 i++;
cristyed231572011-07-14 02:18:59 +0000951 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000952 }
953 }
954 if (fourier_info->modulus != MagickFalse)
955 {
956 i=0L;
cristybb503372010-05-27 20:51:26 +0000957 for (y=0L; y < (ssize_t) fourier_info->height; y++)
958 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000959 {
cristy7d4aa382013-06-30 01:59:39 +0000960 phase_pixels[i]-=0.5;
961 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +0000962 i++;
963 }
964 }
965 magnitude_view=DestroyCacheView(magnitude_view);
966 phase_view=DestroyCacheView(phase_view);
cristybb3c02e2013-07-02 00:43:10 +0000967 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
968 fourier_info->center*sizeof(*magnitude));
969 if (magnitude == (double *) NULL)
970 {
971 (void) ThrowMagickException(exception,GetMagickModule(),
972 ResourceLimitError,"MemoryAllocationFailed","`%s'",
973 magnitude_image->filename);
974 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
975 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
976 return(MagickFalse);
977 }
cristy3ed852e2009-09-05 21:47:34 +0000978 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000979 magnitude_pixels,magnitude);
980 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
981 phase=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
982 fourier_info->width*sizeof(*phase));
983 if (phase == (double *) NULL)
984 {
985 (void) ThrowMagickException(exception,GetMagickModule(),
986 ResourceLimitError,"MemoryAllocationFailed","`%s'",
987 magnitude_image->filename);
988 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
989 return(MagickFalse);
990 }
cristy7d4aa382013-06-30 01:59:39 +0000991 CorrectPhaseLHS(fourier_info->width,fourier_info->width,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000992 if (status != MagickFalse)
993 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000994 phase_pixels,phase);
995 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000996 /*
997 Merge two sets.
998 */
999 i=0L;
1000 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001001 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1002 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001003 {
cristy56ed31c2010-03-22 00:46:21 +00001004#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristybb3c02e2013-07-02 00:43:10 +00001005 fourier[i]=magnitude[i]*cos(phase[i])+I*magnitude[i]*sin(phase[i]);
cristy56ed31c2010-03-22 00:46:21 +00001006#else
cristybb3c02e2013-07-02 00:43:10 +00001007 fourier[i][0]=magnitude[i]*cos(phase[i]);
1008 fourier[i][1]=magnitude[i]*sin(phase[i]);
cristy56ed31c2010-03-22 00:46:21 +00001009#endif
cristy3ed852e2009-09-05 21:47:34 +00001010 i++;
1011 }
1012 else
cristybb503372010-05-27 20:51:26 +00001013 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1014 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001015 {
cristy56ed31c2010-03-22 00:46:21 +00001016#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristybb3c02e2013-07-02 00:43:10 +00001017 fourier[i]=magnitude[i]+I*phase[i];
cristy56ed31c2010-03-22 00:46:21 +00001018#else
cristybb3c02e2013-07-02 00:43:10 +00001019 fourier[i][0]=magnitude[i];
1020 fourier[i][1]=phase[i];
cristy56ed31c2010-03-22 00:46:21 +00001021#endif
cristy3ed852e2009-09-05 21:47:34 +00001022 i++;
1023 }
cristybb3c02e2013-07-02 00:43:10 +00001024 phase=(double *) RelinquishMagickMemory(phase);
1025 magnitude=(double *) RelinquishMagickMemory(magnitude);
cristy3ed852e2009-09-05 21:47:34 +00001026 return(status);
1027}
1028
1029static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
1030 fftw_complex *fourier,Image *image,ExceptionInfo *exception)
1031{
1032 CacheView
1033 *image_view;
1034
1035 double
1036 *source;
1037
1038 fftw_plan
1039 fftw_c2r_plan;
1040
cristy4c08aed2011-07-01 19:47:50 +00001041 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001042 *q;
1043
cristybb503372010-05-27 20:51:26 +00001044 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001045 i,
1046 x;
1047
cristyc4ea4a42011-01-24 01:43:30 +00001048 ssize_t
1049 y;
cristy3ed852e2009-09-05 21:47:34 +00001050
cristybb3c02e2013-07-02 00:43:10 +00001051 source=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
cristyc4ea4a42011-01-24 01:43:30 +00001052 fourier_info->width*sizeof(*source));
cristybb3c02e2013-07-02 00:43:10 +00001053 if (source == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001054 {
1055 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001056 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001057 return(MagickFalse);
1058 }
cristyb5d5f722009-11-04 03:03:49 +00001059#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001060 #pragma omp critical (MagickCore_InverseFourierTransform)
1061#endif
cristydf079ac2010-09-10 01:45:44 +00001062 {
1063 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
1064 fourier,source,FFTW_ESTIMATE);
1065 fftw_execute(fftw_c2r_plan);
1066 fftw_destroy_plan(fftw_c2r_plan);
1067 }
cristy3ed852e2009-09-05 21:47:34 +00001068 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001069 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001070 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy85812052010-09-14 17:56:15 +00001072 if (y >= (ssize_t) image->rows)
1073 break;
1074 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1075 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001076 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001077 break;
cristybb503372010-05-27 20:51:26 +00001078 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001079 {
cristy233fe582012-07-07 14:00:18 +00001080 if (x < (ssize_t) image->columns)
1081 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001082 {
cristy233fe582012-07-07 14:00:18 +00001083 case RedPixelChannel:
1084 default:
1085 {
1086 SetPixelRed(image,ClampToQuantum(QuantumRange*source[i]),q);
1087 break;
1088 }
1089 case GreenPixelChannel:
1090 {
1091 SetPixelGreen(image,ClampToQuantum(QuantumRange*source[i]),q);
1092 break;
1093 }
1094 case BluePixelChannel:
1095 {
1096 SetPixelBlue(image,ClampToQuantum(QuantumRange*source[i]),q);
1097 break;
1098 }
1099 case BlackPixelChannel:
1100 {
1101 SetPixelBlack(image,ClampToQuantum(QuantumRange*source[i]),q);
1102 break;
1103 }
1104 case AlphaPixelChannel:
1105 {
1106 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source[i]),q);
1107 break;
1108 }
cristy3ed852e2009-09-05 21:47:34 +00001109 }
cristy3ed852e2009-09-05 21:47:34 +00001110 i++;
cristyed231572011-07-14 02:18:59 +00001111 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001112 }
1113 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1114 break;
1115 }
1116 image_view=DestroyCacheView(image_view);
cristybb3c02e2013-07-02 00:43:10 +00001117 source=(double *) RelinquishMagickMemory(source);
cristy3ed852e2009-09-05 21:47:34 +00001118 return(MagickTrue);
1119}
1120
cristyc9550792009-11-13 20:05:42 +00001121static MagickBooleanType InverseFourierTransformChannel(
1122 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001123 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001124 Image *fourier_image,ExceptionInfo *exception)
1125{
1126 double
1127 *magnitude,
1128 *phase;
1129
1130 fftw_complex
cristybb3c02e2013-07-02 00:43:10 +00001131 *fourier;
cristy3ed852e2009-09-05 21:47:34 +00001132
1133 FourierInfo
1134 fourier_info;
1135
1136 MagickBooleanType
1137 status;
1138
1139 size_t
1140 extent;
1141
cristyc9550792009-11-13 20:05:42 +00001142 fourier_info.width=magnitude_image->columns;
1143 if ((magnitude_image->columns != magnitude_image->rows) ||
1144 ((magnitude_image->columns % 2) != 0) ||
1145 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001146 {
cristyc9550792009-11-13 20:05:42 +00001147 extent=magnitude_image->columns < magnitude_image->rows ?
1148 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001149 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1150 }
1151 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001152 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001153 fourier_info.channel=channel;
1154 fourier_info.modulus=modulus;
cristybb3c02e2013-07-02 00:43:10 +00001155 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
cristyc4ea4a42011-01-24 01:43:30 +00001156 fourier_info.center*sizeof(*magnitude));
cristybb3c02e2013-07-02 00:43:10 +00001157 if (magnitude == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001158 {
1159 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001160 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001161 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001162 return(MagickFalse);
1163 }
cristybb3c02e2013-07-02 00:43:10 +00001164 phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
1165 fourier_info.center*sizeof(*phase));
1166 if (phase == (double *) NULL)
1167 {
1168 (void) ThrowMagickException(exception,GetMagickModule(),
1169 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1170 magnitude_image->filename);
1171 magnitude=(double *) RelinquishMagickMemory(magnitude);
1172 return(MagickFalse);
1173 }
1174 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height,
1175 fourier_info.center*sizeof(*fourier));
1176 if (fourier == (fftw_complex *) NULL)
1177 {
1178 (void) ThrowMagickException(exception,GetMagickModule(),
1179 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1180 magnitude_image->filename);
1181 phase=(double *) RelinquishMagickMemory(phase);
1182 magnitude=(double *) RelinquishMagickMemory(magnitude);
1183 return(MagickFalse);
1184 }
1185 status=InverseFourier(&fourier_info,magnitude_image,phase_image,fourier,
cristyc9550792009-11-13 20:05:42 +00001186 exception);
cristy3ed852e2009-09-05 21:47:34 +00001187 if (status != MagickFalse)
cristybb3c02e2013-07-02 00:43:10 +00001188 status=InverseFourierTransform(&fourier_info,fourier,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001189 exception);
cristybb3c02e2013-07-02 00:43:10 +00001190 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
1191 phase=(double *) RelinquishMagickMemory(phase);
1192 magnitude=(double *) RelinquishMagickMemory(magnitude);
cristy3ed852e2009-09-05 21:47:34 +00001193 return(status);
1194}
1195#endif
1196
cristyc9550792009-11-13 20:05:42 +00001197MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1198 const Image *phase_image,const MagickBooleanType modulus,
1199 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001200{
1201 Image
1202 *fourier_image;
1203
cristyc9550792009-11-13 20:05:42 +00001204 assert(magnitude_image != (Image *) NULL);
1205 assert(magnitude_image->signature == MagickSignature);
1206 if (magnitude_image->debug != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1208 magnitude_image->filename);
1209 if (phase_image == (Image *) NULL)
1210 {
1211 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001212 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001213 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001214 }
cristy3ed852e2009-09-05 21:47:34 +00001215#if !defined(MAGICKCORE_FFTW_DELEGATE)
1216 fourier_image=(Image *) NULL;
1217 (void) modulus;
1218 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001219 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001220 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001221#else
1222 {
cristyc9550792009-11-13 20:05:42 +00001223 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
1224 magnitude_image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001225 if (fourier_image != (Image *) NULL)
1226 {
1227 MagickBooleanType
1228 is_gray,
1229 status;
1230
cristy3ed852e2009-09-05 21:47:34 +00001231 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001232 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001233 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001234 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001235#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001236 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001237#endif
cristy3ed852e2009-09-05 21:47:34 +00001238 {
cristyb34ef052010-10-07 00:12:05 +00001239#if defined(MAGICKCORE_OPENMP_SUPPORT)
1240 #pragma omp section
1241#endif
cristy3ed852e2009-09-05 21:47:34 +00001242 {
cristyb34ef052010-10-07 00:12:05 +00001243 MagickBooleanType
1244 thread_status;
1245
1246 if (is_gray != MagickFalse)
1247 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001248 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001249 else
cristyc9550792009-11-13 20:05:42 +00001250 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001251 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001252 if (thread_status == MagickFalse)
1253 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001254 }
cristyb34ef052010-10-07 00:12:05 +00001255#if defined(MAGICKCORE_OPENMP_SUPPORT)
1256 #pragma omp section
1257#endif
1258 {
1259 MagickBooleanType
1260 thread_status;
1261
1262 thread_status=MagickTrue;
1263 if (is_gray == MagickFalse)
1264 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001265 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001266 if (thread_status == MagickFalse)
1267 status=thread_status;
1268 }
1269#if defined(MAGICKCORE_OPENMP_SUPPORT)
1270 #pragma omp section
1271#endif
1272 {
1273 MagickBooleanType
1274 thread_status;
1275
1276 thread_status=MagickTrue;
1277 if (is_gray == MagickFalse)
1278 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001279 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001280 if (thread_status == MagickFalse)
1281 status=thread_status;
1282 }
1283#if defined(MAGICKCORE_OPENMP_SUPPORT)
1284 #pragma omp section
1285#endif
1286 {
1287 MagickBooleanType
1288 thread_status;
1289
1290 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001291 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001292 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001293 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001294 if (thread_status == MagickFalse)
1295 status=thread_status;
1296 }
1297#if defined(MAGICKCORE_OPENMP_SUPPORT)
1298 #pragma omp section
1299#endif
1300 {
1301 MagickBooleanType
1302 thread_status;
1303
1304 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001305 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001306 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001307 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001308 if (thread_status == MagickFalse)
1309 status=thread_status;
1310 }
cristy3ed852e2009-09-05 21:47:34 +00001311 }
1312 if (status == MagickFalse)
1313 fourier_image=DestroyImage(fourier_image);
1314 }
cristyb34ef052010-10-07 00:12:05 +00001315 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001316 }
1317#endif
1318 return(fourier_image);
1319}