blob: ff50ea5fc5d042ebf2d85267d314997af07745e2 [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
427 *fourier;
428
429 fftw_plan
430 fftw_r2c_plan;
431
cristy7d4aa382013-06-30 01:59:39 +0000432 MemoryInfo
433 *source_info;
434
cristy4c08aed2011-07-01 19:47:50 +0000435 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000436 *p;
437
cristybb503372010-05-27 20:51:26 +0000438 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000439 i,
440 x;
441
cristyc4ea4a42011-01-24 01:43:30 +0000442 ssize_t
443 y;
444
cristy3ed852e2009-09-05 21:47:34 +0000445 /*
446 Generate the forward Fourier transform.
447 */
cristy7d4aa382013-06-30 01:59:39 +0000448 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000449 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000450 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000451 {
452 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000453 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000454 return(MagickFalse);
455 }
cristybb3c02e2013-07-02 00:43:10 +0000456 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
457 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
458 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000459 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000460 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000461 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000462 {
463 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
464 exception);
cristy4c08aed2011-07-01 19:47:50 +0000465 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000466 break;
cristybb503372010-05-27 20:51:26 +0000467 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000468 {
469 switch (fourier_info->channel)
470 {
cristyd3090f92011-10-18 00:05:15 +0000471 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000472 default:
473 {
cristybb3c02e2013-07-02 00:43:10 +0000474 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000475 break;
476 }
cristyd3090f92011-10-18 00:05:15 +0000477 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000478 {
cristybb3c02e2013-07-02 00:43:10 +0000479 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000480 break;
481 }
cristyd3090f92011-10-18 00:05:15 +0000482 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000483 {
cristybb3c02e2013-07-02 00:43:10 +0000484 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000485 break;
486 }
cristyd3090f92011-10-18 00:05:15 +0000487 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000488 {
cristybb3c02e2013-07-02 00:43:10 +0000489 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000490 break;
491 }
cristyd3090f92011-10-18 00:05:15 +0000492 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000493 {
cristybb3c02e2013-07-02 00:43:10 +0000494 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000495 break;
496 }
cristy3ed852e2009-09-05 21:47:34 +0000497 }
498 i++;
cristyed231572011-07-14 02:18:59 +0000499 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000500 }
501 }
502 image_view=DestroyCacheView(image_view);
cristyb41ee102010-10-04 16:46:15 +0000503 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info->height,
cristy3ed852e2009-09-05 21:47:34 +0000504 fourier_info->center*sizeof(*fourier));
505 if (fourier == (fftw_complex *) NULL)
506 {
507 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000508 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000509 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000510 return(MagickFalse);
511 }
cristyb5d5f722009-11-04 03:03:49 +0000512#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000513 #pragma omp critical (MagickCore_ForwardFourierTransform)
514#endif
cristy70841a12012-10-27 20:52:57 +0000515 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000516 source_pixels,fourier,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000517 fftw_execute(fftw_r2c_plan);
518 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000519 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000520 /*
521 Normalize Fourier transform.
522 */
523 n=(double) fourier_info->width*(double) fourier_info->width;
524 i=0L;
cristybb503372010-05-27 20:51:26 +0000525 for (y=0L; y < (ssize_t) fourier_info->height; y++)
526 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy56ed31c2010-03-22 00:46:21 +0000527 {
528#if defined(MAGICKCORE_HAVE_COMPLEX_H)
529 fourier[i]/=n;
530#else
531 fourier[i][0]/=n;
532 fourier[i][1]/=n;
533#endif
534 i++;
535 }
cristy3ed852e2009-09-05 21:47:34 +0000536 /*
537 Generate magnitude and phase (or real and imaginary).
538 */
539 i=0L;
540 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000541 for (y=0L; y < (ssize_t) fourier_info->height; y++)
542 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000543 {
544 magnitude[i]=cabs(fourier[i]);
545 phase[i]=carg(fourier[i]);
546 i++;
547 }
548 else
cristybb503372010-05-27 20:51:26 +0000549 for (y=0L; y < (ssize_t) fourier_info->height; y++)
550 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000551 {
552 magnitude[i]=creal(fourier[i]);
553 phase[i]=cimag(fourier[i]);
554 i++;
555 }
cristyb41ee102010-10-04 16:46:15 +0000556 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
cristy3ed852e2009-09-05 21:47:34 +0000557 return(MagickTrue);
558}
559
560static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000561 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000562 Image *fourier_image,ExceptionInfo *exception)
563{
564 double
565 *magnitude,
566 *phase;
567
cristybb3c02e2013-07-02 00:43:10 +0000568 fftw_complex
569 *fourier;
570
cristy56ed31c2010-03-22 00:46:21 +0000571 FourierInfo
572 fourier_info;
573
cristyc4ea4a42011-01-24 01:43:30 +0000574 MagickBooleanType
575 status;
576
cristy3ed852e2009-09-05 21:47:34 +0000577 size_t
578 extent;
579
580 fourier_info.width=image->columns;
581 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
582 ((image->rows % 2) != 0))
583 {
584 extent=image->columns < image->rows ? image->rows : image->columns;
585 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
586 }
587 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000588 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000589 fourier_info.channel=channel;
590 fourier_info.modulus=modulus;
cristybb3c02e2013-07-02 00:43:10 +0000591 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
cristy3ed852e2009-09-05 21:47:34 +0000592 fourier_info.center*sizeof(*magnitude));
cristybb3c02e2013-07-02 00:43:10 +0000593 if (magnitude == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
595 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000596 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000597 return(MagickFalse);
598 }
cristybb3c02e2013-07-02 00:43:10 +0000599 phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
600 fourier_info.center*sizeof(*phase));
601 if (phase == (double *) NULL)
602 {
603 (void) ThrowMagickException(exception,GetMagickModule(),
604 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
605 magnitude=(double *) RelinquishMagickMemory(magnitude);
606 return(MagickFalse);
607 }
608 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height,
609 fourier_info.center*sizeof(*fourier));
610 if (fourier == (fftw_complex *) NULL)
611 {
612 (void) ThrowMagickException(exception,GetMagickModule(),
613 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
614 phase=(double *) RelinquishMagickMemory(phase);
615 magnitude=(double *) RelinquishMagickMemory(magnitude);
616 return(MagickFalse);
617 }
cristy3ed852e2009-09-05 21:47:34 +0000618 status=ForwardFourierTransform(&fourier_info,image,magnitude,phase,exception);
619 if (status != MagickFalse)
620 status=ForwardFourier(&fourier_info,fourier_image,magnitude,phase,
621 exception);
cristybb3c02e2013-07-02 00:43:10 +0000622 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
623 phase=(double *) RelinquishMagickMemory(phase);
624 magnitude=(double *) RelinquishMagickMemory(magnitude);
cristy3ed852e2009-09-05 21:47:34 +0000625 return(status);
626}
627#endif
628
629MagickExport Image *ForwardFourierTransformImage(const Image *image,
630 const MagickBooleanType modulus,ExceptionInfo *exception)
631{
632 Image
633 *fourier_image;
634
635 fourier_image=NewImageList();
636#if !defined(MAGICKCORE_FFTW_DELEGATE)
637 (void) modulus;
638 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000639 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000640 image->filename);
641#else
642 {
643 Image
644 *magnitude_image;
645
cristybb503372010-05-27 20:51:26 +0000646 size_t
cristy3ed852e2009-09-05 21:47:34 +0000647 extent,
648 width;
649
650 width=image->columns;
651 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
652 ((image->rows % 2) != 0))
653 {
654 extent=image->columns < image->rows ? image->rows : image->columns;
655 width=(extent & 0x01) == 1 ? extent+1UL : extent;
656 }
657 magnitude_image=CloneImage(image,width,width,MagickFalse,exception);
658 if (magnitude_image != (Image *) NULL)
659 {
660 Image
661 *phase_image;
662
663 magnitude_image->storage_class=DirectClass;
664 magnitude_image->depth=32UL;
665 phase_image=CloneImage(image,width,width,MagickFalse,exception);
666 if (phase_image == (Image *) NULL)
667 magnitude_image=DestroyImage(magnitude_image);
668 else
669 {
670 MagickBooleanType
671 is_gray,
672 status;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 phase_image->storage_class=DirectClass;
675 phase_image->depth=32UL;
676 AppendImageToList(&fourier_image,magnitude_image);
677 AppendImageToList(&fourier_image,phase_image);
678 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000679 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000680#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000681 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000682#endif
cristy3ed852e2009-09-05 21:47:34 +0000683 {
cristyb34ef052010-10-07 00:12:05 +0000684#if defined(MAGICKCORE_OPENMP_SUPPORT)
685 #pragma omp section
686#endif
cristy3ed852e2009-09-05 21:47:34 +0000687 {
cristyb34ef052010-10-07 00:12:05 +0000688 MagickBooleanType
689 thread_status;
690
691 if (is_gray != MagickFalse)
692 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000693 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000694 else
695 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000696 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000697 if (thread_status == MagickFalse)
698 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000699 }
cristyb34ef052010-10-07 00:12:05 +0000700#if defined(MAGICKCORE_OPENMP_SUPPORT)
701 #pragma omp section
702#endif
703 {
704 MagickBooleanType
705 thread_status;
706
707 thread_status=MagickTrue;
708 if (is_gray == MagickFalse)
709 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000710 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000711 if (thread_status == MagickFalse)
712 status=thread_status;
713 }
714#if defined(MAGICKCORE_OPENMP_SUPPORT)
715 #pragma omp section
716#endif
717 {
718 MagickBooleanType
719 thread_status;
720
721 thread_status=MagickTrue;
722 if (is_gray == MagickFalse)
723 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000724 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000725 if (thread_status == MagickFalse)
726 status=thread_status;
727 }
728#if defined(MAGICKCORE_OPENMP_SUPPORT)
729 #pragma omp section
730#endif
731 {
732 MagickBooleanType
733 thread_status;
734
735 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000736 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000737 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000738 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000739 if (thread_status == MagickFalse)
740 status=thread_status;
741 }
742#if defined(MAGICKCORE_OPENMP_SUPPORT)
743 #pragma omp section
744#endif
745 {
746 MagickBooleanType
747 thread_status;
748
749 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +0000750 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +0000751 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000752 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000753 if (thread_status == MagickFalse)
754 status=thread_status;
755 }
cristy3ed852e2009-09-05 21:47:34 +0000756 }
757 if (status == MagickFalse)
758 fourier_image=DestroyImageList(fourier_image);
759 fftw_cleanup();
760 }
761 }
762 }
763#endif
764 return(fourier_image);
765}
766
767/*
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769% %
770% %
771% %
772% 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 %
773% %
774% %
775% %
776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777%
778% InverseFourierTransformImage() implements the inverse discrete Fourier
779% transform (DFT) of the image either as a magnitude / phase or real /
780% imaginary image pair.
781%
782% The format of the InverseFourierTransformImage method is:
783%
cristyc9550792009-11-13 20:05:42 +0000784% Image *InverseFourierTransformImage(const Image *magnitude_image,
785% const Image *phase_image,const MagickBooleanType modulus,
786% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000787%
788% A description of each parameter follows:
789%
cristyc9550792009-11-13 20:05:42 +0000790% o magnitude_image: the magnitude or real image.
791%
792% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +0000793%
794% o modulus: if true, return transform as a magnitude / phase pair
795% otherwise a real / imaginary image pair.
796%
797% o exception: return any errors or warnings in this structure.
798%
799*/
800
801#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +0000802static MagickBooleanType InverseQuadrantSwap(const size_t width,
803 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +0000804{
cristyc4ea4a42011-01-24 01:43:30 +0000805 register ssize_t
806 x;
807
cristybb503372010-05-27 20:51:26 +0000808 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000809 center,
810 y;
811
cristy3ed852e2009-09-05 21:47:34 +0000812 /*
813 Swap quadrants.
814 */
cristy233fe582012-07-07 14:00:18 +0000815 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +0000816 for (y=1L; y < (ssize_t) height; y++)
817 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000818 destination[center*(height-y)-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +0000819 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000820 destination[center*y]=source[y*width+width/2L];
821 for (x=0L; x < center; x++)
822 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +0000823 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +0000824}
825
826static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristyc9550792009-11-13 20:05:42 +0000827 const Image *magnitude_image,const Image *phase_image,fftw_complex *fourier,
828 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000829{
830 CacheView
831 *magnitude_view,
832 *phase_view;
833
834 double
cristybb3c02e2013-07-02 00:43:10 +0000835 *magnitude,
836 *phase,
cristy7d4aa382013-06-30 01:59:39 +0000837 *magnitude_pixels,
838 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000839
cristy3ed852e2009-09-05 21:47:34 +0000840 MagickBooleanType
841 status;
842
cristy4c08aed2011-07-01 19:47:50 +0000843 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000844 *p;
845
cristybb503372010-05-27 20:51:26 +0000846 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000847 i,
848 x;
849
cristyc4ea4a42011-01-24 01:43:30 +0000850 ssize_t
851 y;
852
cristy3ed852e2009-09-05 21:47:34 +0000853 /*
854 Inverse fourier - read image and break down into a double array.
855 */
cristybb3c02e2013-07-02 00:43:10 +0000856 magnitude_pixels=(double *) AcquireQuantumMemory((size_t)
857 fourier_info->height,fourier_info->width*sizeof(*magnitude_pixels));
858 if (magnitude_pixels == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000859 {
860 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000861 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +0000862 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000863 return(MagickFalse);
864 }
cristybb3c02e2013-07-02 00:43:10 +0000865 phase_pixels=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
866 fourier_info->width*sizeof(*phase_pixels));
867 if (phase_pixels == (double *) NULL)
868 {
869 (void) ThrowMagickException(exception,GetMagickModule(),
870 ResourceLimitError,"MemoryAllocationFailed","`%s'",
871 magnitude_image->filename);
872 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
873 return(MagickFalse);
874 }
cristy3ed852e2009-09-05 21:47:34 +0000875 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000876 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +0000877 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000878 {
879 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
880 exception);
cristy4c08aed2011-07-01 19:47:50 +0000881 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000882 break;
cristybb503372010-05-27 20:51:26 +0000883 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000884 {
885 switch (fourier_info->channel)
886 {
cristyd3090f92011-10-18 00:05:15 +0000887 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000888 default:
889 {
cristy7d4aa382013-06-30 01:59:39 +0000890 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000891 break;
892 }
cristyd3090f92011-10-18 00:05:15 +0000893 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000894 {
cristy7d4aa382013-06-30 01:59:39 +0000895 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000896 break;
897 }
cristyd3090f92011-10-18 00:05:15 +0000898 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000899 {
cristy7d4aa382013-06-30 01:59:39 +0000900 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000901 break;
902 }
cristyd3090f92011-10-18 00:05:15 +0000903 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000904 {
cristy7d4aa382013-06-30 01:59:39 +0000905 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000906 break;
907 }
cristyd3090f92011-10-18 00:05:15 +0000908 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000909 {
cristy7d4aa382013-06-30 01:59:39 +0000910 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000911 break;
912 }
cristy3ed852e2009-09-05 21:47:34 +0000913 }
914 i++;
cristyed231572011-07-14 02:18:59 +0000915 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000916 }
917 }
918 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000919 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000920 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000921 {
922 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
923 exception);
cristy4c08aed2011-07-01 19:47:50 +0000924 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000925 break;
cristybb503372010-05-27 20:51:26 +0000926 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 switch (fourier_info->channel)
929 {
cristyd3090f92011-10-18 00:05:15 +0000930 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000931 default:
932 {
cristy7d4aa382013-06-30 01:59:39 +0000933 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000934 break;
935 }
cristyd3090f92011-10-18 00:05:15 +0000936 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000937 {
cristy7d4aa382013-06-30 01:59:39 +0000938 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000939 break;
940 }
cristyd3090f92011-10-18 00:05:15 +0000941 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristy7d4aa382013-06-30 01:59:39 +0000943 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000944 break;
945 }
cristyd3090f92011-10-18 00:05:15 +0000946 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000947 {
cristy7d4aa382013-06-30 01:59:39 +0000948 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000949 break;
950 }
cristyd3090f92011-10-18 00:05:15 +0000951 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000952 {
cristy7d4aa382013-06-30 01:59:39 +0000953 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000954 break;
955 }
cristy3ed852e2009-09-05 21:47:34 +0000956 }
957 i++;
cristyed231572011-07-14 02:18:59 +0000958 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000959 }
960 }
961 if (fourier_info->modulus != MagickFalse)
962 {
963 i=0L;
cristybb503372010-05-27 20:51:26 +0000964 for (y=0L; y < (ssize_t) fourier_info->height; y++)
965 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000966 {
cristy7d4aa382013-06-30 01:59:39 +0000967 phase_pixels[i]-=0.5;
968 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +0000969 i++;
970 }
971 }
972 magnitude_view=DestroyCacheView(magnitude_view);
973 phase_view=DestroyCacheView(phase_view);
cristybb3c02e2013-07-02 00:43:10 +0000974 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
975 fourier_info->center*sizeof(*magnitude));
976 if (magnitude == (double *) NULL)
977 {
978 (void) ThrowMagickException(exception,GetMagickModule(),
979 ResourceLimitError,"MemoryAllocationFailed","`%s'",
980 magnitude_image->filename);
981 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
982 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
983 return(MagickFalse);
984 }
cristy3ed852e2009-09-05 21:47:34 +0000985 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000986 magnitude_pixels,magnitude);
987 magnitude_pixels=(double *) RelinquishMagickMemory(magnitude_pixels);
988 phase=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
989 fourier_info->width*sizeof(*phase));
990 if (phase == (double *) NULL)
991 {
992 (void) ThrowMagickException(exception,GetMagickModule(),
993 ResourceLimitError,"MemoryAllocationFailed","`%s'",
994 magnitude_image->filename);
995 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
996 return(MagickFalse);
997 }
cristy7d4aa382013-06-30 01:59:39 +0000998 CorrectPhaseLHS(fourier_info->width,fourier_info->width,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000999 if (status != MagickFalse)
1000 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +00001001 phase_pixels,phase);
1002 phase_pixels=(double *) RelinquishMagickMemory(phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001003 /*
1004 Merge two sets.
1005 */
1006 i=0L;
1007 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001008 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1009 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001010 {
cristy56ed31c2010-03-22 00:46:21 +00001011#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristybb3c02e2013-07-02 00:43:10 +00001012 fourier[i]=magnitude[i]*cos(phase[i])+I*magnitude[i]*sin(phase[i]);
cristy56ed31c2010-03-22 00:46:21 +00001013#else
cristybb3c02e2013-07-02 00:43:10 +00001014 fourier[i][0]=magnitude[i]*cos(phase[i]);
1015 fourier[i][1]=magnitude[i]*sin(phase[i]);
cristy56ed31c2010-03-22 00:46:21 +00001016#endif
cristy3ed852e2009-09-05 21:47:34 +00001017 i++;
1018 }
1019 else
cristybb503372010-05-27 20:51:26 +00001020 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1021 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001022 {
cristy56ed31c2010-03-22 00:46:21 +00001023#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristybb3c02e2013-07-02 00:43:10 +00001024 fourier[i]=magnitude[i]+I*phase[i];
cristy56ed31c2010-03-22 00:46:21 +00001025#else
cristybb3c02e2013-07-02 00:43:10 +00001026 fourier[i][0]=magnitude[i];
1027 fourier[i][1]=phase[i];
cristy56ed31c2010-03-22 00:46:21 +00001028#endif
cristy3ed852e2009-09-05 21:47:34 +00001029 i++;
1030 }
cristybb3c02e2013-07-02 00:43:10 +00001031 phase=(double *) RelinquishMagickMemory(phase);
1032 magnitude=(double *) RelinquishMagickMemory(magnitude);
cristy3ed852e2009-09-05 21:47:34 +00001033 return(status);
1034}
1035
1036static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
1037 fftw_complex *fourier,Image *image,ExceptionInfo *exception)
1038{
1039 CacheView
1040 *image_view;
1041
1042 double
1043 *source;
1044
1045 fftw_plan
1046 fftw_c2r_plan;
1047
cristy4c08aed2011-07-01 19:47:50 +00001048 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001049 *q;
1050
cristybb503372010-05-27 20:51:26 +00001051 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001052 i,
1053 x;
1054
cristyc4ea4a42011-01-24 01:43:30 +00001055 ssize_t
1056 y;
cristy3ed852e2009-09-05 21:47:34 +00001057
cristybb3c02e2013-07-02 00:43:10 +00001058 source=(double *) AcquireQuantumMemory((size_t) fourier_info->height,
cristyc4ea4a42011-01-24 01:43:30 +00001059 fourier_info->width*sizeof(*source));
cristybb3c02e2013-07-02 00:43:10 +00001060 if (source == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001061 {
1062 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001063 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001064 return(MagickFalse);
1065 }
cristyb5d5f722009-11-04 03:03:49 +00001066#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001067 #pragma omp critical (MagickCore_InverseFourierTransform)
1068#endif
cristydf079ac2010-09-10 01:45:44 +00001069 {
1070 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
1071 fourier,source,FFTW_ESTIMATE);
1072 fftw_execute(fftw_c2r_plan);
1073 fftw_destroy_plan(fftw_c2r_plan);
1074 }
cristy3ed852e2009-09-05 21:47:34 +00001075 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001076 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001077 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001078 {
cristy85812052010-09-14 17:56:15 +00001079 if (y >= (ssize_t) image->rows)
1080 break;
1081 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1082 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001083 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001084 break;
cristybb503372010-05-27 20:51:26 +00001085 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001086 {
cristy233fe582012-07-07 14:00:18 +00001087 if (x < (ssize_t) image->columns)
1088 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
cristy233fe582012-07-07 14:00:18 +00001090 case RedPixelChannel:
1091 default:
1092 {
1093 SetPixelRed(image,ClampToQuantum(QuantumRange*source[i]),q);
1094 break;
1095 }
1096 case GreenPixelChannel:
1097 {
1098 SetPixelGreen(image,ClampToQuantum(QuantumRange*source[i]),q);
1099 break;
1100 }
1101 case BluePixelChannel:
1102 {
1103 SetPixelBlue(image,ClampToQuantum(QuantumRange*source[i]),q);
1104 break;
1105 }
1106 case BlackPixelChannel:
1107 {
1108 SetPixelBlack(image,ClampToQuantum(QuantumRange*source[i]),q);
1109 break;
1110 }
1111 case AlphaPixelChannel:
1112 {
1113 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source[i]),q);
1114 break;
1115 }
cristy3ed852e2009-09-05 21:47:34 +00001116 }
cristy3ed852e2009-09-05 21:47:34 +00001117 i++;
cristyed231572011-07-14 02:18:59 +00001118 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001119 }
1120 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1121 break;
1122 }
1123 image_view=DestroyCacheView(image_view);
cristybb3c02e2013-07-02 00:43:10 +00001124 source=(double *) RelinquishMagickMemory(source);
cristy3ed852e2009-09-05 21:47:34 +00001125 return(MagickTrue);
1126}
1127
cristyc9550792009-11-13 20:05:42 +00001128static MagickBooleanType InverseFourierTransformChannel(
1129 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001130 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001131 Image *fourier_image,ExceptionInfo *exception)
1132{
1133 double
1134 *magnitude,
1135 *phase;
1136
1137 fftw_complex
cristybb3c02e2013-07-02 00:43:10 +00001138 *fourier;
cristy3ed852e2009-09-05 21:47:34 +00001139
1140 FourierInfo
1141 fourier_info;
1142
1143 MagickBooleanType
1144 status;
1145
1146 size_t
1147 extent;
1148
cristyc9550792009-11-13 20:05:42 +00001149 fourier_info.width=magnitude_image->columns;
1150 if ((magnitude_image->columns != magnitude_image->rows) ||
1151 ((magnitude_image->columns % 2) != 0) ||
1152 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001153 {
cristyc9550792009-11-13 20:05:42 +00001154 extent=magnitude_image->columns < magnitude_image->rows ?
1155 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001156 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1157 }
1158 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001159 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001160 fourier_info.channel=channel;
1161 fourier_info.modulus=modulus;
cristybb3c02e2013-07-02 00:43:10 +00001162 magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
cristyc4ea4a42011-01-24 01:43:30 +00001163 fourier_info.center*sizeof(*magnitude));
cristybb3c02e2013-07-02 00:43:10 +00001164 if (magnitude == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001165 {
1166 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001167 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001168 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001169 return(MagickFalse);
1170 }
cristybb3c02e2013-07-02 00:43:10 +00001171 phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height,
1172 fourier_info.center*sizeof(*phase));
1173 if (phase == (double *) NULL)
1174 {
1175 (void) ThrowMagickException(exception,GetMagickModule(),
1176 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1177 magnitude_image->filename);
1178 magnitude=(double *) RelinquishMagickMemory(magnitude);
1179 return(MagickFalse);
1180 }
1181 fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height,
1182 fourier_info.center*sizeof(*fourier));
1183 if (fourier == (fftw_complex *) NULL)
1184 {
1185 (void) ThrowMagickException(exception,GetMagickModule(),
1186 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1187 magnitude_image->filename);
1188 phase=(double *) RelinquishMagickMemory(phase);
1189 magnitude=(double *) RelinquishMagickMemory(magnitude);
1190 return(MagickFalse);
1191 }
1192 status=InverseFourier(&fourier_info,magnitude_image,phase_image,fourier,
cristyc9550792009-11-13 20:05:42 +00001193 exception);
cristy3ed852e2009-09-05 21:47:34 +00001194 if (status != MagickFalse)
cristybb3c02e2013-07-02 00:43:10 +00001195 status=InverseFourierTransform(&fourier_info,fourier,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001196 exception);
cristybb3c02e2013-07-02 00:43:10 +00001197 fourier=(fftw_complex *) RelinquishMagickMemory(fourier);
1198 phase=(double *) RelinquishMagickMemory(phase);
1199 magnitude=(double *) RelinquishMagickMemory(magnitude);
cristy3ed852e2009-09-05 21:47:34 +00001200 return(status);
1201}
1202#endif
1203
cristyc9550792009-11-13 20:05:42 +00001204MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1205 const Image *phase_image,const MagickBooleanType modulus,
1206 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001207{
1208 Image
1209 *fourier_image;
1210
cristyc9550792009-11-13 20:05:42 +00001211 assert(magnitude_image != (Image *) NULL);
1212 assert(magnitude_image->signature == MagickSignature);
1213 if (magnitude_image->debug != MagickFalse)
1214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1215 magnitude_image->filename);
1216 if (phase_image == (Image *) NULL)
1217 {
1218 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001219 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001220 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001221 }
cristy3ed852e2009-09-05 21:47:34 +00001222#if !defined(MAGICKCORE_FFTW_DELEGATE)
1223 fourier_image=(Image *) NULL;
1224 (void) modulus;
1225 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001226 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001227 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001228#else
1229 {
cristyc9550792009-11-13 20:05:42 +00001230 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
1231 magnitude_image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001232 if (fourier_image != (Image *) NULL)
1233 {
1234 MagickBooleanType
1235 is_gray,
1236 status;
1237
cristy3ed852e2009-09-05 21:47:34 +00001238 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001239 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001240 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001241 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001242#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001243 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001244#endif
cristy3ed852e2009-09-05 21:47:34 +00001245 {
cristyb34ef052010-10-07 00:12:05 +00001246#if defined(MAGICKCORE_OPENMP_SUPPORT)
1247 #pragma omp section
1248#endif
cristy3ed852e2009-09-05 21:47:34 +00001249 {
cristyb34ef052010-10-07 00:12:05 +00001250 MagickBooleanType
1251 thread_status;
1252
1253 if (is_gray != MagickFalse)
1254 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001255 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001256 else
cristyc9550792009-11-13 20:05:42 +00001257 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001258 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001259 if (thread_status == MagickFalse)
1260 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001261 }
cristyb34ef052010-10-07 00:12:05 +00001262#if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp section
1264#endif
1265 {
1266 MagickBooleanType
1267 thread_status;
1268
1269 thread_status=MagickTrue;
1270 if (is_gray == MagickFalse)
1271 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001272 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001273 if (thread_status == MagickFalse)
1274 status=thread_status;
1275 }
1276#if defined(MAGICKCORE_OPENMP_SUPPORT)
1277 #pragma omp section
1278#endif
1279 {
1280 MagickBooleanType
1281 thread_status;
1282
1283 thread_status=MagickTrue;
1284 if (is_gray == MagickFalse)
1285 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001286 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001287 if (thread_status == MagickFalse)
1288 status=thread_status;
1289 }
1290#if defined(MAGICKCORE_OPENMP_SUPPORT)
1291 #pragma omp section
1292#endif
1293 {
1294 MagickBooleanType
1295 thread_status;
1296
1297 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001298 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001299 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001300 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001301 if (thread_status == MagickFalse)
1302 status=thread_status;
1303 }
1304#if defined(MAGICKCORE_OPENMP_SUPPORT)
1305 #pragma omp section
1306#endif
1307 {
1308 MagickBooleanType
1309 thread_status;
1310
1311 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001312 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001313 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001314 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001315 if (thread_status == MagickFalse)
1316 status=thread_status;
1317 }
cristy3ed852e2009-09-05 21:47:34 +00001318 }
1319 if (status == MagickFalse)
1320 fourier_image=DestroyImage(fourier_image);
1321 }
cristyb34ef052010-10-07 00:12:05 +00001322 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001323 }
1324#endif
1325 return(fourier_image);
1326}