blob: e908cca2e89d6f4ec8b754eeb8b5dbcd5a0fcde9 [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,
cristy699ae5b2013-07-03 13:41:29 +0000133 const ssize_t x_offset,const ssize_t y_offset,double *roll_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000134{
135 double
cristy699ae5b2013-07-03 13:41:29 +0000136 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000137
cristy7d4aa382013-06-30 01:59:39 +0000138 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000139 *source_info;
cristy7d4aa382013-06-30 01:59:39 +0000140
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 */
cristy699ae5b2013-07-03 13:41:29 +0000153 source_info=AcquireVirtualMemory(height,width*sizeof(*source_pixels));
154 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000155 return(MagickFalse);
cristy699ae5b2013-07-03 13:41:29 +0000156 source_pixels=(double *) GetVirtualMemoryBlob(source_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;
cristy699ae5b2013-07-03 13:41:29 +0000172 source_pixels[v*width+u]=roll_pixels[i++];
cristyc4ea4a42011-01-24 01:43:30 +0000173 }
cristy3ed852e2009-09-05 21:47:34 +0000174 }
cristy699ae5b2013-07-03 13:41:29 +0000175 (void) CopyMagickMemory(roll_pixels,source_pixels,height*width*
176 sizeof(*source_pixels));
177 source_info=RelinquishVirtualMemory(source_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,
cristy699ae5b2013-07-03 13:41:29 +0000182 const size_t height,double *source_pixels,double *forward_pixels)
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;
cristy699ae5b2013-07-03 13:41:29 +0000198 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,
199 source_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000200 if (status == MagickFalse)
201 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000202 for (y=0L; y < (ssize_t) height; y++)
203 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000204 forward_pixels[width*y+x+width/2L]=source_pixels[center*y+x];
cristybb503372010-05-27 20:51:26 +0000205 for (y=1; y < (ssize_t) height; y++)
206 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000207 forward_pixels[width*(height-y)+width/2L-x-1L]=
208 source_pixels[center*y+x+1L];
cristybb503372010-05-27 20:51:26 +0000209 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000210 forward_pixels[-x+width/2L-1L]=forward_pixels[x+width/2L+1L];
cristy3ed852e2009-09-05 21:47:34 +0000211 return(MagickTrue);
212}
213
cristyc4ea4a42011-01-24 01:43:30 +0000214static void CorrectPhaseLHS(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000215 double *fourier_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000216{
cristybb503372010-05-27 20:51:26 +0000217 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000218 x;
219
cristy9d314ff2011-03-09 01:30:28 +0000220 ssize_t
221 y;
222
cristybb503372010-05-27 20:51:26 +0000223 for (y=0L; y < (ssize_t) height; y++)
224 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000225 fourier_pixels[y*width+x]*=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +0000226}
227
228static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
229 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
230{
231 CacheView
232 *magnitude_view,
233 *phase_view;
234
235 double
cristy7d4aa382013-06-30 01:59:39 +0000236 *magnitude_pixels,
237 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000238
239 Image
240 *magnitude_image,
241 *phase_image;
242
cristy3ed852e2009-09-05 21:47:34 +0000243 MagickBooleanType
244 status;
245
cristy7d4aa382013-06-30 01:59:39 +0000246 MemoryInfo
247 *magnitude_info,
248 *phase_info;
249
cristy4c08aed2011-07-01 19:47:50 +0000250 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000251 *q;
252
cristyf5742792012-11-27 18:31:26 +0000253 register ssize_t
254 x;
255
cristyc4ea4a42011-01-24 01:43:30 +0000256 ssize_t
257 i,
258 y;
259
cristy3ed852e2009-09-05 21:47:34 +0000260 magnitude_image=GetFirstImageInList(image);
261 phase_image=GetNextImageInList(image);
262 if (phase_image == (Image *) NULL)
263 {
264 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +0000265 "TwoOrMoreImagesRequired","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000266 return(MagickFalse);
267 }
268 /*
269 Create "Fourier Transform" image from constituent arrays.
270 */
cristy7d4aa382013-06-30 01:59:39 +0000271 magnitude_info=AcquireVirtualMemory((size_t) fourier_info->height,
272 fourier_info->width*sizeof(*magnitude_pixels));
273 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
274 fourier_info->width*sizeof(*phase_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000275 if ((magnitude_info == (MemoryInfo *) NULL) ||
276 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000277 {
cristy7d4aa382013-06-30 01:59:39 +0000278 if (phase_info != (MemoryInfo *) NULL)
279 phase_info=RelinquishVirtualMemory(phase_info);
280 if (magnitude_info != (MemoryInfo *) NULL)
281 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000282 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000283 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000284 return(MagickFalse);
285 }
cristy7d4aa382013-06-30 01:59:39 +0000286 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
287 (void) ResetMagickMemory(magnitude_pixels,0,fourier_info->height*
288 fourier_info->width*sizeof(*magnitude_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000289 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
cristy7d4aa382013-06-30 01:59:39 +0000290 (void) ResetMagickMemory(phase_pixels,0,fourier_info->height*
291 fourier_info->width*sizeof(*phase_pixels));
cristy13c99c42013-08-18 13:53:30 +0000292 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
cristy7d4aa382013-06-30 01:59:39 +0000293 magnitude,magnitude_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000294 if (status != MagickFalse)
cristy13c99c42013-08-18 13:53:30 +0000295 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,phase,
cristy7d4aa382013-06-30 01:59:39 +0000296 phase_pixels);
cristy13c99c42013-08-18 13:53:30 +0000297 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000298 if (fourier_info->modulus != MagickFalse)
299 {
300 i=0L;
cristybb503372010-05-27 20:51:26 +0000301 for (y=0L; y < (ssize_t) fourier_info->height; y++)
302 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000303 {
cristy7d4aa382013-06-30 01:59:39 +0000304 phase_pixels[i]/=(2.0*MagickPI);
305 phase_pixels[i]+=0.5;
cristy3ed852e2009-09-05 21:47:34 +0000306 i++;
307 }
308 }
cristy46ff2672012-12-14 15:32:26 +0000309 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000310 i=0L;
cristybb503372010-05-27 20:51:26 +0000311 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000312 {
313 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
314 exception);
cristyacd2ed22011-08-30 01:44:23 +0000315 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000316 break;
cristybb503372010-05-27 20:51:26 +0000317 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000318 {
319 switch (fourier_info->channel)
320 {
cristyd3090f92011-10-18 00:05:15 +0000321 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000322 default:
323 {
cristy4c08aed2011-07-01 19:47:50 +0000324 SetPixelRed(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000325 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000326 break;
327 }
cristyd3090f92011-10-18 00:05:15 +0000328 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000329 {
cristy4c08aed2011-07-01 19:47:50 +0000330 SetPixelGreen(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000331 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000332 break;
333 }
cristyd3090f92011-10-18 00:05:15 +0000334 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000335 {
cristy4c08aed2011-07-01 19:47:50 +0000336 SetPixelBlue(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000337 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000338 break;
339 }
cristyd3090f92011-10-18 00:05:15 +0000340 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000341 {
cristy4c08aed2011-07-01 19:47:50 +0000342 SetPixelBlack(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000343 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000344 break;
345 }
cristyd3090f92011-10-18 00:05:15 +0000346 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000347 {
cristy4c08aed2011-07-01 19:47:50 +0000348 SetPixelAlpha(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000349 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000350 break;
351 }
cristy3ed852e2009-09-05 21:47:34 +0000352 }
353 i++;
cristyed231572011-07-14 02:18:59 +0000354 q+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000355 }
356 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
357 if (status == MagickFalse)
358 break;
359 }
cristydb070952012-04-20 14:33:00 +0000360 magnitude_view=DestroyCacheView(magnitude_view);
cristy3ed852e2009-09-05 21:47:34 +0000361 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000362 phase_view=AcquireAuthenticCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000363 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000364 {
365 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
366 exception);
cristyacd2ed22011-08-30 01:44:23 +0000367 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000368 break;
cristybb503372010-05-27 20:51:26 +0000369 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000370 {
371 switch (fourier_info->channel)
372 {
cristyd3090f92011-10-18 00:05:15 +0000373 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000374 default:
375 {
cristy4c08aed2011-07-01 19:47:50 +0000376 SetPixelRed(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000377 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000378 break;
379 }
cristyd3090f92011-10-18 00:05:15 +0000380 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000381 {
cristy4c08aed2011-07-01 19:47:50 +0000382 SetPixelGreen(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000383 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000384 break;
385 }
cristyd3090f92011-10-18 00:05:15 +0000386 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000387 {
cristy4c08aed2011-07-01 19:47:50 +0000388 SetPixelBlue(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000389 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000390 break;
391 }
cristyd3090f92011-10-18 00:05:15 +0000392 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000393 {
cristy4c08aed2011-07-01 19:47:50 +0000394 SetPixelBlack(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000395 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000396 break;
397 }
cristyd3090f92011-10-18 00:05:15 +0000398 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000399 {
cristy4c08aed2011-07-01 19:47:50 +0000400 SetPixelAlpha(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000401 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000402 break;
403 }
cristy3ed852e2009-09-05 21:47:34 +0000404 }
405 i++;
cristyed231572011-07-14 02:18:59 +0000406 q+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000407 }
408 status=SyncCacheViewAuthenticPixels(phase_view,exception);
409 if (status == MagickFalse)
410 break;
411 }
412 phase_view=DestroyCacheView(phase_view);
cristy7d4aa382013-06-30 01:59:39 +0000413 phase_info=RelinquishVirtualMemory(phase_info);
414 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000415 return(status);
416}
417
418static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +0000419 const Image *image,double *magnitude_pixels,double *phase_pixels,
420 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000421{
422 CacheView
423 *image_view;
424
425 double
426 n,
cristybb3c02e2013-07-02 00:43:10 +0000427 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000428
429 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +0000430 *forward_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000431
432 fftw_plan
433 fftw_r2c_plan;
434
cristy7d4aa382013-06-30 01:59:39 +0000435 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000436 *forward_info,
cristy7d4aa382013-06-30 01:59:39 +0000437 *source_info;
438
cristy4c08aed2011-07-01 19:47:50 +0000439 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000440 *p;
441
cristybb503372010-05-27 20:51:26 +0000442 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000443 i,
444 x;
445
cristyc4ea4a42011-01-24 01:43:30 +0000446 ssize_t
447 y;
448
cristy3ed852e2009-09-05 21:47:34 +0000449 /*
450 Generate the forward Fourier transform.
451 */
cristy7d4aa382013-06-30 01:59:39 +0000452 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000453 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000454 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000455 {
456 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000457 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000458 return(MagickFalse);
459 }
cristybb3c02e2013-07-02 00:43:10 +0000460 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
461 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
462 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000463 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000464 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000465 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000466 {
467 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
468 exception);
cristy4c08aed2011-07-01 19:47:50 +0000469 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000470 break;
cristybb503372010-05-27 20:51:26 +0000471 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000472 {
473 switch (fourier_info->channel)
474 {
cristyd3090f92011-10-18 00:05:15 +0000475 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000476 default:
477 {
cristybb3c02e2013-07-02 00:43:10 +0000478 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000479 break;
480 }
cristyd3090f92011-10-18 00:05:15 +0000481 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000482 {
cristybb3c02e2013-07-02 00:43:10 +0000483 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000484 break;
485 }
cristyd3090f92011-10-18 00:05:15 +0000486 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000487 {
cristybb3c02e2013-07-02 00:43:10 +0000488 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000489 break;
490 }
cristyd3090f92011-10-18 00:05:15 +0000491 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000492 {
cristybb3c02e2013-07-02 00:43:10 +0000493 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000494 break;
495 }
cristyd3090f92011-10-18 00:05:15 +0000496 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000497 {
cristybb3c02e2013-07-02 00:43:10 +0000498 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000499 break;
500 }
cristy3ed852e2009-09-05 21:47:34 +0000501 }
502 i++;
cristyed231572011-07-14 02:18:59 +0000503 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000504 }
505 }
506 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +0000507 forward_info=AcquireVirtualMemory((size_t) fourier_info->height,
508 fourier_info->center*sizeof(*forward_pixels));
509 if (forward_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000510 {
511 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000512 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000513 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000514 return(MagickFalse);
515 }
cristy699ae5b2013-07-03 13:41:29 +0000516 forward_pixels=(fftw_complex *) GetVirtualMemoryBlob(forward_info);
cristyb5d5f722009-11-04 03:03:49 +0000517#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000518 #pragma omp critical (MagickCore_ForwardFourierTransform)
519#endif
cristy70841a12012-10-27 20:52:57 +0000520 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +0000521 source_pixels,forward_pixels,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000522 fftw_execute(fftw_r2c_plan);
523 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000524 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000525 /*
526 Normalize Fourier transform.
527 */
cristy13c99c42013-08-18 13:53:30 +0000528 n=(double) fourier_info->width*(double) fourier_info->height;
cristy3ed852e2009-09-05 21:47:34 +0000529 i=0L;
cristybb503372010-05-27 20:51:26 +0000530 for (y=0L; y < (ssize_t) fourier_info->height; y++)
531 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy56ed31c2010-03-22 00:46:21 +0000532 {
533#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +0000534 forward_pixels[i]/=n;
cristy56ed31c2010-03-22 00:46:21 +0000535#else
cristy699ae5b2013-07-03 13:41:29 +0000536 forward_pixels[i][0]/=n;
537 forward_pixels[i][1]/=n;
cristy56ed31c2010-03-22 00:46:21 +0000538#endif
539 i++;
540 }
cristy3ed852e2009-09-05 21:47:34 +0000541 /*
542 Generate magnitude and phase (or real and imaginary).
543 */
544 i=0L;
545 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000546 for (y=0L; y < (ssize_t) fourier_info->height; y++)
547 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000548 {
cristy699ae5b2013-07-03 13:41:29 +0000549 magnitude_pixels[i]=cabs(forward_pixels[i]);
550 phase_pixels[i]=carg(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000551 i++;
552 }
553 else
cristybb503372010-05-27 20:51:26 +0000554 for (y=0L; y < (ssize_t) fourier_info->height; y++)
555 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000556 {
cristy699ae5b2013-07-03 13:41:29 +0000557 magnitude_pixels[i]=creal(forward_pixels[i]);
558 phase_pixels[i]=cimag(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000559 i++;
560 }
cristy699ae5b2013-07-03 13:41:29 +0000561 forward_info=(MemoryInfo *) RelinquishVirtualMemory(forward_info);
cristy3ed852e2009-09-05 21:47:34 +0000562 return(MagickTrue);
563}
564
565static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000566 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000567 Image *fourier_image,ExceptionInfo *exception)
568{
569 double
cristyce9fe782013-07-03 00:59:41 +0000570 *magnitude_pixels,
571 *phase_pixels;
cristybb3c02e2013-07-02 00:43:10 +0000572
cristy56ed31c2010-03-22 00:46:21 +0000573 FourierInfo
574 fourier_info;
575
cristyc4ea4a42011-01-24 01:43:30 +0000576 MagickBooleanType
577 status;
578
cristyce9fe782013-07-03 00:59:41 +0000579 MemoryInfo
580 *magnitude_info,
581 *phase_info;
582
cristy3ed852e2009-09-05 21:47:34 +0000583 size_t
584 extent;
585
586 fourier_info.width=image->columns;
587 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
588 ((image->rows % 2) != 0))
589 {
590 extent=image->columns < image->rows ? image->rows : image->columns;
591 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
592 }
593 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000594 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000595 fourier_info.channel=channel;
596 fourier_info.modulus=modulus;
cristyce9fe782013-07-03 00:59:41 +0000597 magnitude_info=AcquireVirtualMemory((size_t) fourier_info.height,
598 fourier_info.center*sizeof(*magnitude_pixels));
599 phase_info=AcquireVirtualMemory((size_t) fourier_info.height,
600 fourier_info.center*sizeof(*phase_pixels));
601 if ((magnitude_info == (MemoryInfo *) NULL) ||
602 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000603 {
cristyce9fe782013-07-03 00:59:41 +0000604 if (phase_info != (MemoryInfo *) NULL)
605 phase_info=RelinquishVirtualMemory(phase_info);
606 if (magnitude_info == (MemoryInfo *) NULL)
607 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000608 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000609 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000610 return(MagickFalse);
611 }
cristyce9fe782013-07-03 00:59:41 +0000612 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
613 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
614 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
615 phase_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000616 if (status != MagickFalse)
cristyce9fe782013-07-03 00:59:41 +0000617 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
618 phase_pixels,exception);
619 phase_info=RelinquishVirtualMemory(phase_info);
620 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000621 return(status);
622}
623#endif
624
625MagickExport Image *ForwardFourierTransformImage(const Image *image,
626 const MagickBooleanType modulus,ExceptionInfo *exception)
627{
628 Image
629 *fourier_image;
630
631 fourier_image=NewImageList();
632#if !defined(MAGICKCORE_FFTW_DELEGATE)
633 (void) modulus;
634 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000635 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000636 image->filename);
637#else
638 {
639 Image
640 *magnitude_image;
641
cristybb503372010-05-27 20:51:26 +0000642 size_t
cristy3ed852e2009-09-05 21:47:34 +0000643 extent,
644 width;
645
646 width=image->columns;
647 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
648 ((image->rows % 2) != 0))
649 {
650 extent=image->columns < image->rows ? image->rows : image->columns;
651 width=(extent & 0x01) == 1 ? extent+1UL : extent;
652 }
653 magnitude_image=CloneImage(image,width,width,MagickFalse,exception);
654 if (magnitude_image != (Image *) NULL)
655 {
656 Image
657 *phase_image;
658
659 magnitude_image->storage_class=DirectClass;
660 magnitude_image->depth=32UL;
661 phase_image=CloneImage(image,width,width,MagickFalse,exception);
662 if (phase_image == (Image *) NULL)
663 magnitude_image=DestroyImage(magnitude_image);
664 else
665 {
666 MagickBooleanType
667 is_gray,
668 status;
669
cristy3ed852e2009-09-05 21:47:34 +0000670 phase_image->storage_class=DirectClass;
671 phase_image->depth=32UL;
672 AppendImageToList(&fourier_image,magnitude_image);
673 AppendImageToList(&fourier_image,phase_image);
674 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000675 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000676#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000677 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000678#endif
cristy3ed852e2009-09-05 21:47:34 +0000679 {
cristyb34ef052010-10-07 00:12:05 +0000680#if defined(MAGICKCORE_OPENMP_SUPPORT)
681 #pragma omp section
682#endif
cristy3ed852e2009-09-05 21:47:34 +0000683 {
cristyb34ef052010-10-07 00:12:05 +0000684 MagickBooleanType
685 thread_status;
686
687 if (is_gray != MagickFalse)
688 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000689 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000690 else
691 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000692 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000693 if (thread_status == MagickFalse)
694 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000695 }
cristyb34ef052010-10-07 00:12:05 +0000696#if defined(MAGICKCORE_OPENMP_SUPPORT)
697 #pragma omp section
698#endif
699 {
700 MagickBooleanType
701 thread_status;
702
703 thread_status=MagickTrue;
704 if (is_gray == MagickFalse)
705 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000706 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000707 if (thread_status == MagickFalse)
708 status=thread_status;
709 }
710#if defined(MAGICKCORE_OPENMP_SUPPORT)
711 #pragma omp section
712#endif
713 {
714 MagickBooleanType
715 thread_status;
716
717 thread_status=MagickTrue;
718 if (is_gray == MagickFalse)
719 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000720 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000721 if (thread_status == MagickFalse)
722 status=thread_status;
723 }
724#if defined(MAGICKCORE_OPENMP_SUPPORT)
725 #pragma omp section
726#endif
727 {
728 MagickBooleanType
729 thread_status;
730
731 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000732 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000733 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000734 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000735 if (thread_status == MagickFalse)
736 status=thread_status;
737 }
738#if defined(MAGICKCORE_OPENMP_SUPPORT)
739 #pragma omp section
740#endif
741 {
742 MagickBooleanType
743 thread_status;
744
745 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +0000746 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +0000747 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000748 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000749 if (thread_status == MagickFalse)
750 status=thread_status;
751 }
cristy3ed852e2009-09-05 21:47:34 +0000752 }
753 if (status == MagickFalse)
754 fourier_image=DestroyImageList(fourier_image);
755 fftw_cleanup();
756 }
757 }
758 }
759#endif
760 return(fourier_image);
761}
762
763/*
764%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765% %
766% %
767% %
768% I n v e r s e F o u r i e r T r a n s f o r m I m a g e %
769% %
770% %
771% %
772%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773%
774% InverseFourierTransformImage() implements the inverse discrete Fourier
775% transform (DFT) of the image either as a magnitude / phase or real /
776% imaginary image pair.
777%
778% The format of the InverseFourierTransformImage method is:
779%
cristyc9550792009-11-13 20:05:42 +0000780% Image *InverseFourierTransformImage(const Image *magnitude_image,
781% const Image *phase_image,const MagickBooleanType modulus,
782% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000783%
784% A description of each parameter follows:
785%
cristyc9550792009-11-13 20:05:42 +0000786% o magnitude_image: the magnitude or real image.
787%
788% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +0000789%
790% o modulus: if true, return transform as a magnitude / phase pair
791% otherwise a real / imaginary image pair.
792%
793% o exception: return any errors or warnings in this structure.
794%
795*/
796
797#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +0000798static MagickBooleanType InverseQuadrantSwap(const size_t width,
799 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +0000800{
cristyc4ea4a42011-01-24 01:43:30 +0000801 register ssize_t
802 x;
803
cristybb503372010-05-27 20:51:26 +0000804 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000805 center,
806 y;
807
cristy3ed852e2009-09-05 21:47:34 +0000808 /*
809 Swap quadrants.
810 */
cristy233fe582012-07-07 14:00:18 +0000811 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +0000812 for (y=1L; y < (ssize_t) height; y++)
813 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy3ed852e2009-09-05 21:47:34 +0000814 destination[center*(height-y)-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +0000815 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000816 destination[center*y]=source[y*width+width/2L];
817 for (x=0L; x < center; x++)
818 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +0000819 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +0000820}
821
822static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +0000823 const Image *magnitude_image,const Image *phase_image,
824 fftw_complex *fourier_pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000825{
826 CacheView
827 *magnitude_view,
828 *phase_view;
829
830 double
cristy699ae5b2013-07-03 13:41:29 +0000831 *inverse_pixels,
cristy7d4aa382013-06-30 01:59:39 +0000832 *magnitude_pixels,
833 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000834
cristy3ed852e2009-09-05 21:47:34 +0000835 MagickBooleanType
836 status;
837
cristy699ae5b2013-07-03 13:41:29 +0000838 MemoryInfo
839 *inverse_info,
840 *magnitude_info,
841 *phase_info;
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 */
cristy699ae5b2013-07-03 13:41:29 +0000856 magnitude_info=AcquireVirtualMemory((size_t)fourier_info->height,
857 fourier_info->width*sizeof(*magnitude_pixels));
858 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000859 fourier_info->width*sizeof(*phase_pixels));
cristy699ae5b2013-07-03 13:41:29 +0000860 inverse_info=AcquireVirtualMemory((size_t) fourier_info->height,
861 fourier_info->center*sizeof(*inverse_pixels));
862 if ((magnitude_info == (MemoryInfo *) NULL) ||
863 (phase_info == (MemoryInfo *) NULL) ||
864 (inverse_info == (MemoryInfo *) NULL))
cristybb3c02e2013-07-02 00:43:10 +0000865 {
cristy699ae5b2013-07-03 13:41:29 +0000866 if (magnitude_info != (MemoryInfo *) NULL)
867 magnitude_info=RelinquishVirtualMemory(magnitude_info);
868 if (phase_info != (MemoryInfo *) NULL)
869 phase_info=RelinquishVirtualMemory(phase_info);
870 if (inverse_info != (MemoryInfo *) NULL)
871 inverse_info=RelinquishVirtualMemory(inverse_info);
cristybb3c02e2013-07-02 00:43:10 +0000872 (void) ThrowMagickException(exception,GetMagickModule(),
873 ResourceLimitError,"MemoryAllocationFailed","`%s'",
874 magnitude_image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000875 return(MagickFalse);
876 }
cristy699ae5b2013-07-03 13:41:29 +0000877 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
878 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
879 inverse_pixels=(double *) GetVirtualMemoryBlob(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +0000880 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000881 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +0000882 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000883 {
884 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
885 exception);
cristy4c08aed2011-07-01 19:47:50 +0000886 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000887 break;
cristybb503372010-05-27 20:51:26 +0000888 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000889 {
890 switch (fourier_info->channel)
891 {
cristyd3090f92011-10-18 00:05:15 +0000892 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000893 default:
894 {
cristy7d4aa382013-06-30 01:59:39 +0000895 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000896 break;
897 }
cristyd3090f92011-10-18 00:05:15 +0000898 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000899 {
cristy7d4aa382013-06-30 01:59:39 +0000900 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000901 break;
902 }
cristyd3090f92011-10-18 00:05:15 +0000903 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000904 {
cristy7d4aa382013-06-30 01:59:39 +0000905 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000906 break;
907 }
cristyd3090f92011-10-18 00:05:15 +0000908 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000909 {
cristy7d4aa382013-06-30 01:59:39 +0000910 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000911 break;
912 }
cristyd3090f92011-10-18 00:05:15 +0000913 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000914 {
cristy7d4aa382013-06-30 01:59:39 +0000915 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000916 break;
917 }
cristy3ed852e2009-09-05 21:47:34 +0000918 }
919 i++;
cristyed231572011-07-14 02:18:59 +0000920 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000921 }
922 }
cristy699ae5b2013-07-03 13:41:29 +0000923 magnitude_view=DestroyCacheView(magnitude_view);
924 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
925 magnitude_pixels,inverse_pixels);
926 (void) CopyMagickMemory(magnitude_pixels,inverse_pixels,fourier_info->height*
927 fourier_info->center*sizeof(*magnitude_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000928 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000929 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000930 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000931 {
932 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
933 exception);
cristy4c08aed2011-07-01 19:47:50 +0000934 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000935 break;
cristybb503372010-05-27 20:51:26 +0000936 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000937 {
938 switch (fourier_info->channel)
939 {
cristyd3090f92011-10-18 00:05:15 +0000940 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000941 default:
942 {
cristy7d4aa382013-06-30 01:59:39 +0000943 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000944 break;
945 }
cristyd3090f92011-10-18 00:05:15 +0000946 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000947 {
cristy7d4aa382013-06-30 01:59:39 +0000948 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000949 break;
950 }
cristyd3090f92011-10-18 00:05:15 +0000951 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000952 {
cristy7d4aa382013-06-30 01:59:39 +0000953 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000954 break;
955 }
cristyd3090f92011-10-18 00:05:15 +0000956 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000957 {
cristy7d4aa382013-06-30 01:59:39 +0000958 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000959 break;
960 }
cristyd3090f92011-10-18 00:05:15 +0000961 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000962 {
cristy7d4aa382013-06-30 01:59:39 +0000963 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000964 break;
965 }
cristy3ed852e2009-09-05 21:47:34 +0000966 }
967 i++;
cristyed231572011-07-14 02:18:59 +0000968 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000969 }
970 }
971 if (fourier_info->modulus != MagickFalse)
972 {
973 i=0L;
cristybb503372010-05-27 20:51:26 +0000974 for (y=0L; y < (ssize_t) fourier_info->height; y++)
975 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000976 {
cristy7d4aa382013-06-30 01:59:39 +0000977 phase_pixels[i]-=0.5;
978 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +0000979 i++;
980 }
981 }
cristy3ed852e2009-09-05 21:47:34 +0000982 phase_view=DestroyCacheView(phase_view);
cristy13c99c42013-08-18 13:53:30 +0000983 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000984 if (status != MagickFalse)
985 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +0000986 phase_pixels,inverse_pixels);
987 (void) CopyMagickMemory(phase_pixels,inverse_pixels,fourier_info->height*
988 fourier_info->center*sizeof(*phase_pixels));
989 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +0000990 /*
991 Merge two sets.
992 */
993 i=0L;
994 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000995 for (y=0L; y < (ssize_t) fourier_info->height; y++)
996 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000997 {
cristy56ed31c2010-03-22 00:46:21 +0000998#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +0000999 fourier_pixels[i]=magnitude_pixels[i]*cos(phase_pixels[i])+I*
1000 magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001001#else
cristy699ae5b2013-07-03 13:41:29 +00001002 fourier_pixels[i][0]=magnitude_pixels[i]*cos(phase_pixels[i]);
1003 fourier_pixels[i][1]=magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001004#endif
cristy3ed852e2009-09-05 21:47:34 +00001005 i++;
1006 }
1007 else
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)
cristy699ae5b2013-07-03 13:41:29 +00001012 fourier_pixels[i]=magnitude_pixels[i]+I*phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001013#else
cristy699ae5b2013-07-03 13:41:29 +00001014 fourier_pixels[i][0]=magnitude_pixels[i];
1015 fourier_pixels[i][1]=phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001016#endif
cristy3ed852e2009-09-05 21:47:34 +00001017 i++;
1018 }
cristy699ae5b2013-07-03 13:41:29 +00001019 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1020 phase_info=RelinquishVirtualMemory(phase_info);
cristy3ed852e2009-09-05 21:47:34 +00001021 return(status);
1022}
1023
1024static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001025 fftw_complex *fourier_pixels,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001026{
1027 CacheView
1028 *image_view;
1029
1030 double
cristy699ae5b2013-07-03 13:41:29 +00001031 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001032
1033 fftw_plan
1034 fftw_c2r_plan;
1035
cristy699ae5b2013-07-03 13:41:29 +00001036 MemoryInfo
1037 *source_info;
1038
cristy4c08aed2011-07-01 19:47:50 +00001039 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001040 *q;
1041
cristybb503372010-05-27 20:51:26 +00001042 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001043 i,
1044 x;
1045
cristyc4ea4a42011-01-24 01:43:30 +00001046 ssize_t
1047 y;
cristy3ed852e2009-09-05 21:47:34 +00001048
cristy699ae5b2013-07-03 13:41:29 +00001049 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
1050 fourier_info->width*sizeof(*source_pixels));
1051 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001052 {
1053 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001054 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001055 return(MagickFalse);
1056 }
cristy699ae5b2013-07-03 13:41:29 +00001057 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristyb5d5f722009-11-04 03:03:49 +00001058#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001059 #pragma omp critical (MagickCore_InverseFourierTransform)
1060#endif
cristydf079ac2010-09-10 01:45:44 +00001061 {
1062 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001063 fourier_pixels,source_pixels,FFTW_ESTIMATE);
cristydf079ac2010-09-10 01:45:44 +00001064 fftw_execute(fftw_c2r_plan);
1065 fftw_destroy_plan(fftw_c2r_plan);
1066 }
cristy3ed852e2009-09-05 21:47:34 +00001067 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001068 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001069 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001070 {
cristy85812052010-09-14 17:56:15 +00001071 if (y >= (ssize_t) image->rows)
1072 break;
1073 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1074 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001075 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001076 break;
cristybb503372010-05-27 20:51:26 +00001077 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001078 {
cristy233fe582012-07-07 14:00:18 +00001079 if (x < (ssize_t) image->columns)
1080 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001081 {
cristy233fe582012-07-07 14:00:18 +00001082 case RedPixelChannel:
1083 default:
1084 {
cristy699ae5b2013-07-03 13:41:29 +00001085 SetPixelRed(image,ClampToQuantum(QuantumRange*source_pixels[i]),q);
cristy233fe582012-07-07 14:00:18 +00001086 break;
1087 }
1088 case GreenPixelChannel:
1089 {
cristy699ae5b2013-07-03 13:41:29 +00001090 SetPixelGreen(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1091 q);
cristy233fe582012-07-07 14:00:18 +00001092 break;
1093 }
1094 case BluePixelChannel:
1095 {
cristy699ae5b2013-07-03 13:41:29 +00001096 SetPixelBlue(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1097 q);
cristy233fe582012-07-07 14:00:18 +00001098 break;
1099 }
1100 case BlackPixelChannel:
1101 {
cristy699ae5b2013-07-03 13:41:29 +00001102 SetPixelBlack(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1103 q);
cristy233fe582012-07-07 14:00:18 +00001104 break;
1105 }
1106 case AlphaPixelChannel:
1107 {
cristy699ae5b2013-07-03 13:41:29 +00001108 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1109 q);
cristy233fe582012-07-07 14:00:18 +00001110 break;
1111 }
cristy3ed852e2009-09-05 21:47:34 +00001112 }
cristy3ed852e2009-09-05 21:47:34 +00001113 i++;
cristyed231572011-07-14 02:18:59 +00001114 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001115 }
1116 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1117 break;
1118 }
1119 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +00001120 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +00001121 return(MagickTrue);
1122}
1123
cristyc9550792009-11-13 20:05:42 +00001124static MagickBooleanType InverseFourierTransformChannel(
1125 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001126 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001127 Image *fourier_image,ExceptionInfo *exception)
1128{
cristy3ed852e2009-09-05 21:47:34 +00001129 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +00001130 *inverse_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001131
1132 FourierInfo
1133 fourier_info;
1134
1135 MagickBooleanType
1136 status;
1137
cristy699ae5b2013-07-03 13:41:29 +00001138 MemoryInfo
1139 *inverse_info;
1140
cristy3ed852e2009-09-05 21:47:34 +00001141 size_t
1142 extent;
1143
cristyc9550792009-11-13 20:05:42 +00001144 fourier_info.width=magnitude_image->columns;
1145 if ((magnitude_image->columns != magnitude_image->rows) ||
1146 ((magnitude_image->columns % 2) != 0) ||
1147 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001148 {
cristyc9550792009-11-13 20:05:42 +00001149 extent=magnitude_image->columns < magnitude_image->rows ?
1150 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001151 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1152 }
1153 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001154 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001155 fourier_info.channel=channel;
1156 fourier_info.modulus=modulus;
cristy699ae5b2013-07-03 13:41:29 +00001157 inverse_info=AcquireVirtualMemory((size_t) fourier_info.height,
1158 fourier_info.center*sizeof(*inverse_pixels));
1159 if (inverse_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001160 {
1161 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001162 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001163 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001164 return(MagickFalse);
1165 }
cristy699ae5b2013-07-03 13:41:29 +00001166 inverse_pixels=(fftw_complex *) GetVirtualMemoryBlob(inverse_info);
1167 status=InverseFourier(&fourier_info,magnitude_image,phase_image,
1168 inverse_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001169 if (status != MagickFalse)
cristy699ae5b2013-07-03 13:41:29 +00001170 status=InverseFourierTransform(&fourier_info,inverse_pixels,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001171 exception);
cristy699ae5b2013-07-03 13:41:29 +00001172 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001173 return(status);
1174}
1175#endif
1176
cristyc9550792009-11-13 20:05:42 +00001177MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1178 const Image *phase_image,const MagickBooleanType modulus,
1179 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001180{
1181 Image
1182 *fourier_image;
1183
cristyc9550792009-11-13 20:05:42 +00001184 assert(magnitude_image != (Image *) NULL);
1185 assert(magnitude_image->signature == MagickSignature);
1186 if (magnitude_image->debug != MagickFalse)
1187 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1188 magnitude_image->filename);
1189 if (phase_image == (Image *) NULL)
1190 {
1191 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001192 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001193 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001194 }
cristy3ed852e2009-09-05 21:47:34 +00001195#if !defined(MAGICKCORE_FFTW_DELEGATE)
1196 fourier_image=(Image *) NULL;
1197 (void) modulus;
1198 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001199 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001200 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001201#else
1202 {
cristyc9550792009-11-13 20:05:42 +00001203 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
1204 magnitude_image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001205 if (fourier_image != (Image *) NULL)
1206 {
1207 MagickBooleanType
1208 is_gray,
1209 status;
1210
cristy3ed852e2009-09-05 21:47:34 +00001211 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001212 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001213 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001214 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001215#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001216 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001217#endif
cristy3ed852e2009-09-05 21:47:34 +00001218 {
cristyb34ef052010-10-07 00:12:05 +00001219#if defined(MAGICKCORE_OPENMP_SUPPORT)
1220 #pragma omp section
1221#endif
cristy3ed852e2009-09-05 21:47:34 +00001222 {
cristyb34ef052010-10-07 00:12:05 +00001223 MagickBooleanType
1224 thread_status;
1225
1226 if (is_gray != MagickFalse)
1227 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001228 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001229 else
cristyc9550792009-11-13 20:05:42 +00001230 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001231 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001232 if (thread_status == MagickFalse)
1233 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001234 }
cristyb34ef052010-10-07 00:12:05 +00001235#if defined(MAGICKCORE_OPENMP_SUPPORT)
1236 #pragma omp section
1237#endif
1238 {
1239 MagickBooleanType
1240 thread_status;
1241
1242 thread_status=MagickTrue;
1243 if (is_gray == MagickFalse)
1244 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001245 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001246 if (thread_status == MagickFalse)
1247 status=thread_status;
1248 }
1249#if defined(MAGICKCORE_OPENMP_SUPPORT)
1250 #pragma omp section
1251#endif
1252 {
1253 MagickBooleanType
1254 thread_status;
1255
1256 thread_status=MagickTrue;
1257 if (is_gray == MagickFalse)
1258 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001259 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001260 if (thread_status == MagickFalse)
1261 status=thread_status;
1262 }
1263#if defined(MAGICKCORE_OPENMP_SUPPORT)
1264 #pragma omp section
1265#endif
1266 {
1267 MagickBooleanType
1268 thread_status;
1269
1270 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001271 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001272 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001273 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001274 if (thread_status == MagickFalse)
1275 status=thread_status;
1276 }
1277#if defined(MAGICKCORE_OPENMP_SUPPORT)
1278 #pragma omp section
1279#endif
1280 {
1281 MagickBooleanType
1282 thread_status;
1283
1284 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001285 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001286 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001287 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001288 if (thread_status == MagickFalse)
1289 status=thread_status;
1290 }
cristy3ed852e2009-09-05 21:47:34 +00001291 }
1292 if (status == MagickFalse)
1293 fourier_image=DestroyImage(fourier_image);
1294 }
cristyb34ef052010-10-07 00:12:05 +00001295 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001296 }
1297#endif
1298 return(fourier_image);
1299}