blob: c44a4b1b89e19af040add0ad041439cac8b11123 [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"
cristy99dc0362013-09-15 18:32:54 +000046#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000047#include "MagickCore/attribute.h"
cristy7d4aa382013-06-30 01:59:39 +000048#include "MagickCore/blob.h"
cristy4c08aed2011-07-01 19:47:50 +000049#include "MagickCore/cache.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/fourier.h"
54#include "MagickCore/log.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
cristy1042ed22013-10-05 17:38:54 +000057#include "MagickCore/monitor-private.h"
cristy4c08aed2011-07-01 19:47:50 +000058#include "MagickCore/pixel-accessor.h"
cristy99dc0362013-09-15 18:32:54 +000059#include "MagickCore/pixel-private.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/property.h"
61#include "MagickCore/quantum-private.h"
cristyac245f82012-05-05 17:13:57 +000062#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000063#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000064#if defined(MAGICKCORE_FFTW_DELEGATE)
cristy56ed31c2010-03-22 00:46:21 +000065#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy3ed852e2009-09-05 21:47:34 +000066#include <complex.h>
cristy56ed31c2010-03-22 00:46:21 +000067#endif
cristy3ed852e2009-09-05 21:47:34 +000068#include <fftw3.h>
cristy47b022b2011-01-18 22:29:21 +000069#if !defined(MAGICKCORE_HAVE_CABS)
70#define cabs(z) (sqrt(z[0]*z[0]+z[1]*z[1]))
71#endif
72#if !defined(MAGICKCORE_HAVE_CARG)
cristy4da3ba32011-02-07 16:58:11 +000073#define carg(z) (atan2(cimag(z),creal(z)))
cristy47b022b2011-01-18 22:29:21 +000074#endif
75#if !defined(MAGICKCORE_HAVE_CIMAG)
76#define cimag(z) (z[1])
77#endif
78#if !defined(MAGICKCORE_HAVE_CREAL)
79#define creal(z) (z[0])
80#endif
cristy3ed852e2009-09-05 21:47:34 +000081#endif
82
83/*
84 Typedef declarations.
85*/
86typedef struct _FourierInfo
87{
cristyd3090f92011-10-18 00:05:15 +000088 PixelChannel
cristy3ed852e2009-09-05 21:47:34 +000089 channel;
90
91 MagickBooleanType
92 modulus;
93
cristybb503372010-05-27 20:51:26 +000094 size_t
cristy3ed852e2009-09-05 21:47:34 +000095 width,
96 height;
97
cristybb503372010-05-27 20:51:26 +000098 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000099 center;
100} FourierInfo;
101
102/*
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104% %
105% %
106% %
cristy790190d2013-10-04 00:51:51 +0000107% C o m p l e x I m a g e s %
108% %
109% %
110% %
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112%
113% ComplexImages() performs complex mathematics on an image sequence.
114%
115% The format of the ComplexImages method is:
116%
117% MagickBooleanType ComplexImages(Image *images,
118% const ComplexOperator operator,ExceptionInfo *exception)
119%
120% A description of each parameter follows:
121%
122% o image: the image.
123%
124% o operator: A complex operator.
125%
126% o exception: return any errors or warnings in this structure.
127%
128*/
129MagickExport Image *ComplexImages(const Image *images,
130 const ComplexOperator operator,ExceptionInfo *exception)
131{
132#define ComplexImageTag "Complex/Image"
133
134 CacheView
cristy1042ed22013-10-05 17:38:54 +0000135 *Ai_view,
136 *Ar_view,
137 *Bi_view,
138 *Br_view,
139 *Ci_view,
140 *Cr_view;
141
142 const Image
143 *Ai_image,
144 *Ar_image,
145 *Bi_image,
146 *Br_image;
cristy790190d2013-10-04 00:51:51 +0000147
148 Image
cristy1042ed22013-10-05 17:38:54 +0000149 *Ci_image,
150 *complex_images,
cristy34919ed2013-10-06 15:42:40 +0000151 *Cr_image,
152 *image;
cristy790190d2013-10-04 00:51:51 +0000153
154 MagickBooleanType
155 status;
156
157 MagickOffsetType
158 progress;
159
cristy790190d2013-10-04 00:51:51 +0000160 ssize_t
161 y;
162
163 assert(images != (Image *) NULL);
164 assert(images->signature == MagickSignature);
165 if (images->debug != MagickFalse)
166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
167 assert(exception != (ExceptionInfo *) NULL);
168 assert(exception->signature == MagickSignature);
cristy1042ed22013-10-05 17:38:54 +0000169 if (images->next == (Image *) NULL)
170 {
171 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
172 "ImageSequenceRequired","`%s'",images->filename);
173 return((Image *) NULL);
174 }
cristy34919ed2013-10-06 15:42:40 +0000175 image=CloneImage(images,images->columns,images->rows,MagickTrue,exception);
176 if (image == (Image *) NULL)
cristy1042ed22013-10-05 17:38:54 +0000177 return((Image *) NULL);
cristy34919ed2013-10-06 15:42:40 +0000178 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
179 {
180 image=DestroyImageList(image);
181 return(image);
182 }
183 complex_images=NewImageList();
184 AppendImageToList(&complex_images,image);
185 image=CloneImage(images,images->columns,images->rows,MagickTrue,exception);
186 if (image == (Image *) NULL)
cristy1042ed22013-10-05 17:38:54 +0000187 {
188 complex_images=DestroyImageList(complex_images);
189 return(complex_images);
190 }
cristy34919ed2013-10-06 15:42:40 +0000191 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
192 {
193 complex_images=DestroyImageList(complex_images);
194 return(complex_images);
195 }
196 AppendImageToList(&complex_images,image);
cristy1042ed22013-10-05 17:38:54 +0000197 /*
198 Apply complex mathematics to image pixels.
199 */
200 Ar_image=images;
201 Ai_image=images->next;
cristy8dc6a072013-10-08 12:00:48 +0000202 Br_image=images;
203 Bi_image=images->next;
204 if ((images->next->next != (Image *) NULL) &&
205 (images->next->next->next != (Image *) NULL))
cristy1042ed22013-10-05 17:38:54 +0000206 {
207 Br_image=images->next->next;
208 Bi_image=images->next->next->next;
209 }
210 Cr_image=complex_images;
211 Ci_image=complex_images->next;
212 Ar_view=AcquireVirtualCacheView(Ar_image,exception);
213 Ai_view=AcquireVirtualCacheView(Ai_image,exception);
214 Br_view=AcquireVirtualCacheView(Br_image,exception);
215 Bi_view=AcquireVirtualCacheView(Bi_image,exception);
216 Cr_view=AcquireAuthenticCacheView(Cr_image,exception);
217 Ci_view=AcquireAuthenticCacheView(Ci_image,exception);
218 status=MagickTrue;
219 progress=0;
220#if defined(MAGICKCORE_OPENMP_SUPPORT)
221 #pragma omp parallel for schedule(static,4) shared(progress,status) \
222 magick_threads(images,complex_images,images->rows,1L)
223#endif
224 for (y=0; y < (ssize_t) images->rows; y++)
225 {
226 register const Quantum
227 *restrict Ai,
228 *restrict Ar,
229 *restrict Bi,
230 *restrict Br;
231
232 register Quantum
233 *restrict Ci,
234 *restrict Cr;
235
236 register ssize_t
237 x;
238
cristy34919ed2013-10-06 15:42:40 +0000239 if (status == MagickFalse)
240 continue;
cristy1042ed22013-10-05 17:38:54 +0000241 Ar=GetCacheViewVirtualPixels(Ar_view,0,y,images->columns,1,exception);
242 Ai=GetCacheViewVirtualPixels(Ai_view,0,y,images->columns,1,exception);
243 Br=GetCacheViewVirtualPixels(Br_view,0,y,images->columns,1,exception);
244 Bi=GetCacheViewVirtualPixels(Bi_view,0,y,images->columns,1,exception);
245 Cr=QueueCacheViewAuthenticPixels(Cr_view,0,y,images->columns,1,exception);
246 Ci=QueueCacheViewAuthenticPixels(Ci_view,0,y,images->columns,1,exception);
247 if ((Ar == (const Quantum *) NULL) || (Ai == (const Quantum *) NULL) ||
248 (Br == (const Quantum *) NULL) || (Bi == (const Quantum *) NULL) ||
249 (Cr == (Quantum *) NULL) || (Ci == (Quantum *) NULL))
250 {
251 status=MagickFalse;
252 continue;
253 }
254 for (x=0; x < (ssize_t) images->columns; x++)
255 {
cristy9f654472013-10-05 19:44:06 +0000256 register ssize_t
257 i;
258
259 for (i=0; i < (ssize_t) GetPixelChannels(images); i++)
260 {
261 switch (operator)
262 {
cristy19f78862013-10-05 22:21:46 +0000263 case AddComplexOperator:
264 {
265 Cr[i]=Ar[i]+Br[i];
266 Ci[i]=Ai[i]+Bi[i];
267 break;
268 }
cristy9f654472013-10-05 19:44:06 +0000269 case ConjugateComplexOperator:
270 default:
271 {
272 Cr[i]=Ar[i];
273 Ci[i]=(-Bi[i]);
274 break;
275 }
276 case DivideComplexOperator:
277 {
278 double
279 gamma;
280
281 gamma=PerceptibleReciprocal(Br[i]*Br[i]+Bi[i]*Bi[i]);
282 Cr[i]=gamma*(Ar[i]*Br[i]+Ai[i]*Bi[i]);
283 Ci[i]=gamma*(Ai[i]*Br[i]-Ai[i]*Bi[i]);
284 break;
285 }
cristyf46941c2013-10-06 22:25:41 +0000286 case MagnitudePhaseComplexOperator:
287 {
cristy8526a972013-10-08 00:52:54 +0000288 Cr[i]=sqrt(Ar[i]*Ar[i]+Ai[i]*Ai[i]);
289 Ci[i]=atan2(Ai[i],Ar[i]);
cristyf46941c2013-10-06 22:25:41 +0000290 break;
291 }
cristy9f654472013-10-05 19:44:06 +0000292 case MultiplyComplexOperator:
293 {
cristy15b07b32013-10-08 12:10:31 +0000294 Cr[i]=QuantumScale*(Ar[i]*Br[i]-Ai[i]*Bi[i]);
295 Ci[i]=QuantumScale*(Ai[i]*Br[i]+Ar[i]*Bi[i]);
cristyf46941c2013-10-06 22:25:41 +0000296 break;
297 }
298 case RealImaginaryComplexOperator:
299 {
cristyda36ce22013-10-08 17:37:50 +0000300 Cr[i]=Ar[i]*cos(Ai[i]);
301 Ci[i]=Ar[i]*sin(Ai[i]);
cristy9f654472013-10-05 19:44:06 +0000302 break;
303 }
cristy19f78862013-10-05 22:21:46 +0000304 case SubtractComplexOperator:
305 {
306 Cr[i]=Ar[i]-Br[i];
307 Ci[i]=Ai[i]-Bi[i];
308 break;
309 }
cristy9f654472013-10-05 19:44:06 +0000310 }
cristy19f78862013-10-05 22:21:46 +0000311 Ar+=GetPixelChannels(Ar_image);
312 Ai+=GetPixelChannels(Ai_image);
313 Br+=GetPixelChannels(Br_image);
314 Bi+=GetPixelChannels(Bi_image);
315 Cr+=GetPixelChannels(Cr_image);
316 Ci+=GetPixelChannels(Ci_image);
cristy9f654472013-10-05 19:44:06 +0000317 }
cristy1042ed22013-10-05 17:38:54 +0000318 }
319 if (SyncCacheViewAuthenticPixels(Ci_view,exception) == MagickFalse)
320 status=MagickFalse;
321 if (SyncCacheViewAuthenticPixels(Cr_view,exception) == MagickFalse)
322 status=MagickFalse;
323 if (images->progress_monitor != (MagickProgressMonitor) NULL)
324 {
325 MagickBooleanType
326 proceed;
327
328#if defined(MAGICKCORE_OPENMP_SUPPORT)
329 #pragma omp critical (MagickCore_ComplexImages)
330#endif
331 proceed=SetImageProgress(images,ComplexImageTag,progress++,
332 images->rows);
333 if (proceed == MagickFalse)
334 status=MagickFalse;
335 }
336 }
337 Cr_view=DestroyCacheView(Cr_view);
338 Ci_view=DestroyCacheView(Ci_view);
339 Br_view=DestroyCacheView(Br_view);
340 Bi_view=DestroyCacheView(Bi_view);
341 Ar_view=DestroyCacheView(Ar_view);
342 Ai_view=DestroyCacheView(Ai_view);
343 if (status == MagickFalse)
344 complex_images=DestroyImageList(complex_images);
345 return(complex_images);
cristy790190d2013-10-04 00:51:51 +0000346}
347
348/*
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350% %
351% %
352% %
cristy3ed852e2009-09-05 21:47:34 +0000353% 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 %
354% %
355% %
356% %
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358%
359% ForwardFourierTransformImage() implements the discrete Fourier transform
360% (DFT) of the image either as a magnitude / phase or real / imaginary image
361% pair.
362%
363% The format of the ForwadFourierTransformImage method is:
364%
365% Image *ForwardFourierTransformImage(const Image *image,
366% const MagickBooleanType modulus,ExceptionInfo *exception)
367%
368% A description of each parameter follows:
369%
370% o image: the image.
371%
372% o modulus: if true, return as transform as a magnitude / phase pair
373% otherwise a real / imaginary image pair.
374%
375% o exception: return any errors or warnings in this structure.
376%
377*/
378
379#if defined(MAGICKCORE_FFTW_DELEGATE)
380
cristyc4ea4a42011-01-24 01:43:30 +0000381static MagickBooleanType RollFourier(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000382 const ssize_t x_offset,const ssize_t y_offset,double *roll_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384 double
cristy699ae5b2013-07-03 13:41:29 +0000385 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000386
cristy7d4aa382013-06-30 01:59:39 +0000387 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000388 *source_info;
cristy7d4aa382013-06-30 01:59:39 +0000389
cristyc4ea4a42011-01-24 01:43:30 +0000390 register ssize_t
391 i,
392 x;
393
cristybb503372010-05-27 20:51:26 +0000394 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000395 u,
396 v,
397 y;
398
cristy3ed852e2009-09-05 21:47:34 +0000399 /*
cristy2da05352010-09-15 19:22:17 +0000400 Move zero frequency (DC, average color) from (0,0) to (width/2,height/2).
cristy3ed852e2009-09-05 21:47:34 +0000401 */
cristy699ae5b2013-07-03 13:41:29 +0000402 source_info=AcquireVirtualMemory(height,width*sizeof(*source_pixels));
403 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000404 return(MagickFalse);
cristy699ae5b2013-07-03 13:41:29 +0000405 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000406 i=0L;
cristybb503372010-05-27 20:51:26 +0000407 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000408 {
409 if (y_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000410 v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset;
cristy3ed852e2009-09-05 21:47:34 +0000411 else
cristybb503372010-05-27 20:51:26 +0000412 v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height :
cristy3ed852e2009-09-05 21:47:34 +0000413 y+y_offset;
cristybb503372010-05-27 20:51:26 +0000414 for (x=0L; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000415 {
416 if (x_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000417 u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset;
cristy3ed852e2009-09-05 21:47:34 +0000418 else
cristybb503372010-05-27 20:51:26 +0000419 u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width :
cristy3ed852e2009-09-05 21:47:34 +0000420 x+x_offset;
cristy699ae5b2013-07-03 13:41:29 +0000421 source_pixels[v*width+u]=roll_pixels[i++];
cristyc4ea4a42011-01-24 01:43:30 +0000422 }
cristy3ed852e2009-09-05 21:47:34 +0000423 }
cristy699ae5b2013-07-03 13:41:29 +0000424 (void) CopyMagickMemory(roll_pixels,source_pixels,height*width*
425 sizeof(*source_pixels));
426 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000427 return(MagickTrue);
428}
429
cristybb503372010-05-27 20:51:26 +0000430static MagickBooleanType ForwardQuadrantSwap(const size_t width,
cristy699ae5b2013-07-03 13:41:29 +0000431 const size_t height,double *source_pixels,double *forward_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000432{
cristy3ed852e2009-09-05 21:47:34 +0000433 MagickBooleanType
434 status;
435
cristybb503372010-05-27 20:51:26 +0000436 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000437 x;
438
cristyc4ea4a42011-01-24 01:43:30 +0000439 ssize_t
440 center,
441 y;
442
cristy3ed852e2009-09-05 21:47:34 +0000443 /*
444 Swap quadrants.
445 */
cristybb503372010-05-27 20:51:26 +0000446 center=(ssize_t) floor((double) width/2L)+1L;
cristy699ae5b2013-07-03 13:41:29 +0000447 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,
448 source_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000449 if (status == MagickFalse)
450 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000451 for (y=0L; y < (ssize_t) height; y++)
452 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy3d04ed72013-08-18 17:51:01 +0000453 forward_pixels[y*width+x+width/2L]=source_pixels[y*center+x];
cristybb503372010-05-27 20:51:26 +0000454 for (y=1; y < (ssize_t) height; y++)
455 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +0000456 forward_pixels[(height-y)*width+width/2L-x-1L]=
cristy3d04ed72013-08-18 17:51:01 +0000457 source_pixels[y*center+x+1L];
cristybb503372010-05-27 20:51:26 +0000458 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000459 forward_pixels[-x+width/2L-1L]=forward_pixels[x+width/2L+1L];
cristy3ed852e2009-09-05 21:47:34 +0000460 return(MagickTrue);
461}
462
cristyc4ea4a42011-01-24 01:43:30 +0000463static void CorrectPhaseLHS(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000464 double *fourier_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000465{
cristybb503372010-05-27 20:51:26 +0000466 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000467 x;
468
cristy9d314ff2011-03-09 01:30:28 +0000469 ssize_t
470 y;
471
cristybb503372010-05-27 20:51:26 +0000472 for (y=0L; y < (ssize_t) height; y++)
473 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000474 fourier_pixels[y*width+x]*=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +0000475}
476
477static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
478 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
479{
480 CacheView
481 *magnitude_view,
482 *phase_view;
483
484 double
cristy7d4aa382013-06-30 01:59:39 +0000485 *magnitude_pixels,
486 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000487
488 Image
489 *magnitude_image,
490 *phase_image;
491
cristy3ed852e2009-09-05 21:47:34 +0000492 MagickBooleanType
493 status;
494
cristy7d4aa382013-06-30 01:59:39 +0000495 MemoryInfo
496 *magnitude_info,
497 *phase_info;
498
cristy4c08aed2011-07-01 19:47:50 +0000499 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000500 *q;
501
cristyf5742792012-11-27 18:31:26 +0000502 register ssize_t
503 x;
504
cristyc4ea4a42011-01-24 01:43:30 +0000505 ssize_t
506 i,
507 y;
508
cristy3ed852e2009-09-05 21:47:34 +0000509 magnitude_image=GetFirstImageInList(image);
510 phase_image=GetNextImageInList(image);
511 if (phase_image == (Image *) NULL)
512 {
513 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +0000514 "TwoOrMoreImagesRequired","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000515 return(MagickFalse);
516 }
517 /*
518 Create "Fourier Transform" image from constituent arrays.
519 */
cristy7d4aa382013-06-30 01:59:39 +0000520 magnitude_info=AcquireVirtualMemory((size_t) fourier_info->height,
521 fourier_info->width*sizeof(*magnitude_pixels));
522 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
523 fourier_info->width*sizeof(*phase_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000524 if ((magnitude_info == (MemoryInfo *) NULL) ||
525 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000526 {
cristy7d4aa382013-06-30 01:59:39 +0000527 if (phase_info != (MemoryInfo *) NULL)
528 phase_info=RelinquishVirtualMemory(phase_info);
529 if (magnitude_info != (MemoryInfo *) NULL)
530 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000531 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000532 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000533 return(MagickFalse);
534 }
cristy7d4aa382013-06-30 01:59:39 +0000535 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
536 (void) ResetMagickMemory(magnitude_pixels,0,fourier_info->height*
537 fourier_info->width*sizeof(*magnitude_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000538 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
cristy7d4aa382013-06-30 01:59:39 +0000539 (void) ResetMagickMemory(phase_pixels,0,fourier_info->height*
540 fourier_info->width*sizeof(*phase_pixels));
cristy13c99c42013-08-18 13:53:30 +0000541 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
cristy7d4aa382013-06-30 01:59:39 +0000542 magnitude,magnitude_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000543 if (status != MagickFalse)
cristy13c99c42013-08-18 13:53:30 +0000544 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,phase,
cristy7d4aa382013-06-30 01:59:39 +0000545 phase_pixels);
cristy13c99c42013-08-18 13:53:30 +0000546 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000547 if (fourier_info->modulus != MagickFalse)
548 {
549 i=0L;
cristybb503372010-05-27 20:51:26 +0000550 for (y=0L; y < (ssize_t) fourier_info->height; y++)
551 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000552 {
cristy7d4aa382013-06-30 01:59:39 +0000553 phase_pixels[i]/=(2.0*MagickPI);
554 phase_pixels[i]+=0.5;
cristy3ed852e2009-09-05 21:47:34 +0000555 i++;
556 }
557 }
cristy46ff2672012-12-14 15:32:26 +0000558 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000559 i=0L;
cristybb503372010-05-27 20:51:26 +0000560 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000561 {
562 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
563 exception);
cristyacd2ed22011-08-30 01:44:23 +0000564 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000565 break;
cristybb503372010-05-27 20:51:26 +0000566 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000567 {
568 switch (fourier_info->channel)
569 {
cristyd3090f92011-10-18 00:05:15 +0000570 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000571 default:
572 {
cristy4c08aed2011-07-01 19:47:50 +0000573 SetPixelRed(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000574 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000575 break;
576 }
cristyd3090f92011-10-18 00:05:15 +0000577 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000578 {
cristy4c08aed2011-07-01 19:47:50 +0000579 SetPixelGreen(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000580 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000581 break;
582 }
cristyd3090f92011-10-18 00:05:15 +0000583 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000584 {
cristy4c08aed2011-07-01 19:47:50 +0000585 SetPixelBlue(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000586 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000587 break;
588 }
cristyd3090f92011-10-18 00:05:15 +0000589 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4c08aed2011-07-01 19:47:50 +0000591 SetPixelBlack(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000592 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000593 break;
594 }
cristyd3090f92011-10-18 00:05:15 +0000595 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000596 {
cristy4c08aed2011-07-01 19:47:50 +0000597 SetPixelAlpha(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000598 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000599 break;
600 }
cristy3ed852e2009-09-05 21:47:34 +0000601 }
602 i++;
cristyed231572011-07-14 02:18:59 +0000603 q+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000604 }
605 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
606 if (status == MagickFalse)
607 break;
608 }
cristydb070952012-04-20 14:33:00 +0000609 magnitude_view=DestroyCacheView(magnitude_view);
cristy3ed852e2009-09-05 21:47:34 +0000610 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000611 phase_view=AcquireAuthenticCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000612 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000613 {
614 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
615 exception);
cristyacd2ed22011-08-30 01:44:23 +0000616 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000617 break;
cristybb503372010-05-27 20:51:26 +0000618 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000619 {
620 switch (fourier_info->channel)
621 {
cristyd3090f92011-10-18 00:05:15 +0000622 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000623 default:
624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 SetPixelRed(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000626 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
cristyd3090f92011-10-18 00:05:15 +0000629 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000630 {
cristy4c08aed2011-07-01 19:47:50 +0000631 SetPixelGreen(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000632 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000633 break;
634 }
cristyd3090f92011-10-18 00:05:15 +0000635 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000636 {
cristy4c08aed2011-07-01 19:47:50 +0000637 SetPixelBlue(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000638 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000639 break;
640 }
cristyd3090f92011-10-18 00:05:15 +0000641 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000642 {
cristy4c08aed2011-07-01 19:47:50 +0000643 SetPixelBlack(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000644 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000645 break;
646 }
cristyd3090f92011-10-18 00:05:15 +0000647 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristy4c08aed2011-07-01 19:47:50 +0000649 SetPixelAlpha(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000650 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000651 break;
652 }
cristy3ed852e2009-09-05 21:47:34 +0000653 }
654 i++;
cristyed231572011-07-14 02:18:59 +0000655 q+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000656 }
657 status=SyncCacheViewAuthenticPixels(phase_view,exception);
658 if (status == MagickFalse)
659 break;
660 }
661 phase_view=DestroyCacheView(phase_view);
cristy7d4aa382013-06-30 01:59:39 +0000662 phase_info=RelinquishVirtualMemory(phase_info);
663 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000664 return(status);
665}
666
667static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +0000668 const Image *image,double *magnitude_pixels,double *phase_pixels,
669 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000670{
671 CacheView
672 *image_view;
673
cristy99dc0362013-09-15 18:32:54 +0000674 const char
675 *value;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 double
cristybb3c02e2013-07-02 00:43:10 +0000678 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000679
680 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +0000681 *forward_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000682
683 fftw_plan
684 fftw_r2c_plan;
685
cristy7d4aa382013-06-30 01:59:39 +0000686 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000687 *forward_info,
cristy7d4aa382013-06-30 01:59:39 +0000688 *source_info;
689
cristy4c08aed2011-07-01 19:47:50 +0000690 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000691 *p;
692
cristybb503372010-05-27 20:51:26 +0000693 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000694 i,
695 x;
696
cristyc4ea4a42011-01-24 01:43:30 +0000697 ssize_t
698 y;
699
cristy3ed852e2009-09-05 21:47:34 +0000700 /*
701 Generate the forward Fourier transform.
702 */
cristy7d4aa382013-06-30 01:59:39 +0000703 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000704 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000705 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000706 {
707 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000708 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000709 return(MagickFalse);
710 }
cristybb3c02e2013-07-02 00:43:10 +0000711 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
712 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
713 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000714 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000715 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000716 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000717 {
718 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
719 exception);
cristy4c08aed2011-07-01 19:47:50 +0000720 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000721 break;
cristybb503372010-05-27 20:51:26 +0000722 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000723 {
724 switch (fourier_info->channel)
725 {
cristyd3090f92011-10-18 00:05:15 +0000726 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000727 default:
728 {
cristybb3c02e2013-07-02 00:43:10 +0000729 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000730 break;
731 }
cristyd3090f92011-10-18 00:05:15 +0000732 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000733 {
cristybb3c02e2013-07-02 00:43:10 +0000734 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000735 break;
736 }
cristyd3090f92011-10-18 00:05:15 +0000737 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000738 {
cristybb3c02e2013-07-02 00:43:10 +0000739 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000740 break;
741 }
cristyd3090f92011-10-18 00:05:15 +0000742 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000743 {
cristybb3c02e2013-07-02 00:43:10 +0000744 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000745 break;
746 }
cristyd3090f92011-10-18 00:05:15 +0000747 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000748 {
cristybb3c02e2013-07-02 00:43:10 +0000749 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000750 break;
751 }
cristy3ed852e2009-09-05 21:47:34 +0000752 }
753 i++;
cristyed231572011-07-14 02:18:59 +0000754 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000755 }
756 }
757 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +0000758 forward_info=AcquireVirtualMemory((size_t) fourier_info->height,
759 fourier_info->center*sizeof(*forward_pixels));
760 if (forward_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000761 {
762 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000763 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000764 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000765 return(MagickFalse);
766 }
cristy699ae5b2013-07-03 13:41:29 +0000767 forward_pixels=(fftw_complex *) GetVirtualMemoryBlob(forward_info);
cristyb5d5f722009-11-04 03:03:49 +0000768#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000769 #pragma omp critical (MagickCore_ForwardFourierTransform)
770#endif
cristy70841a12012-10-27 20:52:57 +0000771 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +0000772 source_pixels,forward_pixels,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000773 fftw_execute(fftw_r2c_plan);
774 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000775 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy99dc0362013-09-15 18:32:54 +0000776 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +0000777 if ((value == (const char *) NULL) || (LocaleCompare(value,"forward") == 0))
cristy56ed31c2010-03-22 00:46:21 +0000778 {
cristy99dc0362013-09-15 18:32:54 +0000779 double
780 gamma;
781
782 /*
783 Normalize Fourier transform.
784 */
785 i=0L;
786 gamma=PerceptibleReciprocal((double) fourier_info->width*
787 fourier_info->height);
788 for (y=0L; y < (ssize_t) fourier_info->height; y++)
789 for (x=0L; x < (ssize_t) fourier_info->center; x++)
790 {
cristy56ed31c2010-03-22 00:46:21 +0000791#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy99dc0362013-09-15 18:32:54 +0000792 forward_pixels[i]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000793#else
cristy99dc0362013-09-15 18:32:54 +0000794 forward_pixels[i][0]*=gamma;
795 forward_pixels[i][1]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000796#endif
cristy99dc0362013-09-15 18:32:54 +0000797 i++;
798 }
cristy56ed31c2010-03-22 00:46:21 +0000799 }
cristy3ed852e2009-09-05 21:47:34 +0000800 /*
801 Generate magnitude and phase (or real and imaginary).
802 */
803 i=0L;
804 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000805 for (y=0L; y < (ssize_t) fourier_info->height; y++)
806 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000807 {
cristy699ae5b2013-07-03 13:41:29 +0000808 magnitude_pixels[i]=cabs(forward_pixels[i]);
809 phase_pixels[i]=carg(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000810 i++;
811 }
812 else
cristybb503372010-05-27 20:51:26 +0000813 for (y=0L; y < (ssize_t) fourier_info->height; y++)
814 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000815 {
cristy699ae5b2013-07-03 13:41:29 +0000816 magnitude_pixels[i]=creal(forward_pixels[i]);
817 phase_pixels[i]=cimag(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000818 i++;
819 }
cristy699ae5b2013-07-03 13:41:29 +0000820 forward_info=(MemoryInfo *) RelinquishVirtualMemory(forward_info);
cristy3ed852e2009-09-05 21:47:34 +0000821 return(MagickTrue);
822}
823
824static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000825 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000826 Image *fourier_image,ExceptionInfo *exception)
827{
828 double
cristyce9fe782013-07-03 00:59:41 +0000829 *magnitude_pixels,
830 *phase_pixels;
cristybb3c02e2013-07-02 00:43:10 +0000831
cristy56ed31c2010-03-22 00:46:21 +0000832 FourierInfo
833 fourier_info;
834
cristyc4ea4a42011-01-24 01:43:30 +0000835 MagickBooleanType
836 status;
837
cristyce9fe782013-07-03 00:59:41 +0000838 MemoryInfo
839 *magnitude_info,
840 *phase_info;
841
cristy3ed852e2009-09-05 21:47:34 +0000842 size_t
843 extent;
844
845 fourier_info.width=image->columns;
846 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
847 ((image->rows % 2) != 0))
848 {
849 extent=image->columns < image->rows ? image->rows : image->columns;
850 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
851 }
852 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000853 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000854 fourier_info.channel=channel;
855 fourier_info.modulus=modulus;
cristyce9fe782013-07-03 00:59:41 +0000856 magnitude_info=AcquireVirtualMemory((size_t) fourier_info.height,
857 fourier_info.center*sizeof(*magnitude_pixels));
858 phase_info=AcquireVirtualMemory((size_t) fourier_info.height,
859 fourier_info.center*sizeof(*phase_pixels));
860 if ((magnitude_info == (MemoryInfo *) NULL) ||
861 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000862 {
cristyce9fe782013-07-03 00:59:41 +0000863 if (phase_info != (MemoryInfo *) NULL)
864 phase_info=RelinquishVirtualMemory(phase_info);
865 if (magnitude_info == (MemoryInfo *) NULL)
866 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000868 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000869 return(MagickFalse);
870 }
cristyce9fe782013-07-03 00:59:41 +0000871 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
872 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
873 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
874 phase_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000875 if (status != MagickFalse)
cristyce9fe782013-07-03 00:59:41 +0000876 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
877 phase_pixels,exception);
878 phase_info=RelinquishVirtualMemory(phase_info);
879 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000880 return(status);
881}
882#endif
883
884MagickExport Image *ForwardFourierTransformImage(const Image *image,
885 const MagickBooleanType modulus,ExceptionInfo *exception)
886{
887 Image
888 *fourier_image;
889
890 fourier_image=NewImageList();
891#if !defined(MAGICKCORE_FFTW_DELEGATE)
892 (void) modulus;
893 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000894 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000895 image->filename);
896#else
897 {
898 Image
899 *magnitude_image;
900
cristybb503372010-05-27 20:51:26 +0000901 size_t
cristy3ed852e2009-09-05 21:47:34 +0000902 extent,
cristyc9721ff2013-10-08 19:40:01 +0000903 height,
cristy3ed852e2009-09-05 21:47:34 +0000904 width;
905
906 width=image->columns;
cristyc9721ff2013-10-08 19:40:01 +0000907 height=image->rows;
cristy3ed852e2009-09-05 21:47:34 +0000908 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
909 ((image->rows % 2) != 0))
910 {
911 extent=image->columns < image->rows ? image->rows : image->columns;
912 width=(extent & 0x01) == 1 ? extent+1UL : extent;
913 }
cristyc9721ff2013-10-08 19:40:01 +0000914 height=width;
915 magnitude_image=CloneImage(image,width,height,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000916 if (magnitude_image != (Image *) NULL)
917 {
918 Image
919 *phase_image;
920
921 magnitude_image->storage_class=DirectClass;
922 magnitude_image->depth=32UL;
cristyc9721ff2013-10-08 19:40:01 +0000923 phase_image=CloneImage(image,width,height,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000924 if (phase_image == (Image *) NULL)
925 magnitude_image=DestroyImage(magnitude_image);
926 else
927 {
928 MagickBooleanType
929 is_gray,
930 status;
931
cristy3ed852e2009-09-05 21:47:34 +0000932 phase_image->storage_class=DirectClass;
933 phase_image->depth=32UL;
934 AppendImageToList(&fourier_image,magnitude_image);
935 AppendImageToList(&fourier_image,phase_image);
936 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000937 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000938#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000939 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000940#endif
cristy3ed852e2009-09-05 21:47:34 +0000941 {
cristyb34ef052010-10-07 00:12:05 +0000942#if defined(MAGICKCORE_OPENMP_SUPPORT)
943 #pragma omp section
944#endif
cristy3ed852e2009-09-05 21:47:34 +0000945 {
cristyb34ef052010-10-07 00:12:05 +0000946 MagickBooleanType
947 thread_status;
948
949 if (is_gray != MagickFalse)
950 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000951 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000952 else
953 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000954 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000955 if (thread_status == MagickFalse)
956 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000957 }
cristyb34ef052010-10-07 00:12:05 +0000958#if defined(MAGICKCORE_OPENMP_SUPPORT)
959 #pragma omp section
960#endif
961 {
962 MagickBooleanType
963 thread_status;
964
965 thread_status=MagickTrue;
966 if (is_gray == MagickFalse)
967 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000968 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000969 if (thread_status == MagickFalse)
970 status=thread_status;
971 }
972#if defined(MAGICKCORE_OPENMP_SUPPORT)
973 #pragma omp section
974#endif
975 {
976 MagickBooleanType
977 thread_status;
978
979 thread_status=MagickTrue;
980 if (is_gray == MagickFalse)
981 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000982 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000983 if (thread_status == MagickFalse)
984 status=thread_status;
985 }
986#if defined(MAGICKCORE_OPENMP_SUPPORT)
987 #pragma omp section
988#endif
989 {
990 MagickBooleanType
991 thread_status;
992
993 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000994 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000995 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000996 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000997 if (thread_status == MagickFalse)
998 status=thread_status;
999 }
1000#if defined(MAGICKCORE_OPENMP_SUPPORT)
1001 #pragma omp section
1002#endif
1003 {
1004 MagickBooleanType
1005 thread_status;
1006
1007 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001008 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001009 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +00001010 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001011 if (thread_status == MagickFalse)
1012 status=thread_status;
1013 }
cristy3ed852e2009-09-05 21:47:34 +00001014 }
1015 if (status == MagickFalse)
1016 fourier_image=DestroyImageList(fourier_image);
1017 fftw_cleanup();
1018 }
1019 }
1020 }
1021#endif
1022 return(fourier_image);
1023}
1024
1025/*
1026%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027% %
1028% %
1029% %
1030% 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 %
1031% %
1032% %
1033% %
1034%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035%
1036% InverseFourierTransformImage() implements the inverse discrete Fourier
1037% transform (DFT) of the image either as a magnitude / phase or real /
1038% imaginary image pair.
1039%
1040% The format of the InverseFourierTransformImage method is:
1041%
cristyc9550792009-11-13 20:05:42 +00001042% Image *InverseFourierTransformImage(const Image *magnitude_image,
1043% const Image *phase_image,const MagickBooleanType modulus,
1044% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001045%
1046% A description of each parameter follows:
1047%
cristyc9550792009-11-13 20:05:42 +00001048% o magnitude_image: the magnitude or real image.
1049%
1050% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +00001051%
1052% o modulus: if true, return transform as a magnitude / phase pair
1053% otherwise a real / imaginary image pair.
1054%
1055% o exception: return any errors or warnings in this structure.
1056%
1057*/
1058
1059#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +00001060static MagickBooleanType InverseQuadrantSwap(const size_t width,
1061 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +00001062{
cristyc4ea4a42011-01-24 01:43:30 +00001063 register ssize_t
1064 x;
1065
cristybb503372010-05-27 20:51:26 +00001066 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001067 center,
1068 y;
1069
cristy3ed852e2009-09-05 21:47:34 +00001070 /*
1071 Swap quadrants.
1072 */
cristy233fe582012-07-07 14:00:18 +00001073 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +00001074 for (y=1L; y < (ssize_t) height; y++)
1075 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +00001076 destination[(height-y)*center-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +00001077 for (y=0L; y < (ssize_t) height; y++)
cristy3d04ed72013-08-18 17:51:01 +00001078 destination[y*center]=source[y*width+width/2L];
cristy3ed852e2009-09-05 21:47:34 +00001079 for (x=0L; x < center; x++)
1080 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +00001081 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +00001082}
1083
1084static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001085 const Image *magnitude_image,const Image *phase_image,
1086 fftw_complex *fourier_pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001087{
1088 CacheView
1089 *magnitude_view,
1090 *phase_view;
1091
1092 double
cristy699ae5b2013-07-03 13:41:29 +00001093 *inverse_pixels,
cristy7d4aa382013-06-30 01:59:39 +00001094 *magnitude_pixels,
1095 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001096
cristy3ed852e2009-09-05 21:47:34 +00001097 MagickBooleanType
1098 status;
1099
cristy699ae5b2013-07-03 13:41:29 +00001100 MemoryInfo
1101 *inverse_info,
1102 *magnitude_info,
1103 *phase_info;
1104
cristy4c08aed2011-07-01 19:47:50 +00001105 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001106 *p;
1107
cristybb503372010-05-27 20:51:26 +00001108 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001109 i,
1110 x;
1111
cristyc4ea4a42011-01-24 01:43:30 +00001112 ssize_t
1113 y;
1114
cristy3ed852e2009-09-05 21:47:34 +00001115 /*
1116 Inverse fourier - read image and break down into a double array.
1117 */
cristy699ae5b2013-07-03 13:41:29 +00001118 magnitude_info=AcquireVirtualMemory((size_t)fourier_info->height,
1119 fourier_info->width*sizeof(*magnitude_pixels));
1120 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +00001121 fourier_info->width*sizeof(*phase_pixels));
cristy699ae5b2013-07-03 13:41:29 +00001122 inverse_info=AcquireVirtualMemory((size_t) fourier_info->height,
1123 fourier_info->center*sizeof(*inverse_pixels));
1124 if ((magnitude_info == (MemoryInfo *) NULL) ||
1125 (phase_info == (MemoryInfo *) NULL) ||
1126 (inverse_info == (MemoryInfo *) NULL))
cristybb3c02e2013-07-02 00:43:10 +00001127 {
cristy699ae5b2013-07-03 13:41:29 +00001128 if (magnitude_info != (MemoryInfo *) NULL)
1129 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1130 if (phase_info != (MemoryInfo *) NULL)
1131 phase_info=RelinquishVirtualMemory(phase_info);
1132 if (inverse_info != (MemoryInfo *) NULL)
1133 inverse_info=RelinquishVirtualMemory(inverse_info);
cristybb3c02e2013-07-02 00:43:10 +00001134 (void) ThrowMagickException(exception,GetMagickModule(),
1135 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1136 magnitude_image->filename);
cristybb3c02e2013-07-02 00:43:10 +00001137 return(MagickFalse);
1138 }
cristy699ae5b2013-07-03 13:41:29 +00001139 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
1140 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
1141 inverse_pixels=(double *) GetVirtualMemoryBlob(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001142 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001143 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +00001144 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001145 {
1146 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
1147 exception);
cristy4c08aed2011-07-01 19:47:50 +00001148 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001149 break;
cristybb503372010-05-27 20:51:26 +00001150 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001151 {
1152 switch (fourier_info->channel)
1153 {
cristyd3090f92011-10-18 00:05:15 +00001154 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001155 default:
1156 {
cristy7d4aa382013-06-30 01:59:39 +00001157 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001158 break;
1159 }
cristyd3090f92011-10-18 00:05:15 +00001160 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001161 {
cristy7d4aa382013-06-30 01:59:39 +00001162 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001163 break;
1164 }
cristyd3090f92011-10-18 00:05:15 +00001165 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001166 {
cristy7d4aa382013-06-30 01:59:39 +00001167 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001168 break;
1169 }
cristyd3090f92011-10-18 00:05:15 +00001170 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001171 {
cristy7d4aa382013-06-30 01:59:39 +00001172 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001173 break;
1174 }
cristyd3090f92011-10-18 00:05:15 +00001175 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001176 {
cristy7d4aa382013-06-30 01:59:39 +00001177 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001178 break;
1179 }
cristy3ed852e2009-09-05 21:47:34 +00001180 }
1181 i++;
cristyed231572011-07-14 02:18:59 +00001182 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +00001183 }
1184 }
cristy699ae5b2013-07-03 13:41:29 +00001185 magnitude_view=DestroyCacheView(magnitude_view);
1186 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
1187 magnitude_pixels,inverse_pixels);
1188 (void) CopyMagickMemory(magnitude_pixels,inverse_pixels,fourier_info->height*
1189 fourier_info->center*sizeof(*magnitude_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001190 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001191 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +00001192 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001193 {
1194 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
1195 exception);
cristy4c08aed2011-07-01 19:47:50 +00001196 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001197 break;
cristybb503372010-05-27 20:51:26 +00001198 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001199 {
1200 switch (fourier_info->channel)
1201 {
cristyd3090f92011-10-18 00:05:15 +00001202 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001203 default:
1204 {
cristy7d4aa382013-06-30 01:59:39 +00001205 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001206 break;
1207 }
cristyd3090f92011-10-18 00:05:15 +00001208 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001209 {
cristy7d4aa382013-06-30 01:59:39 +00001210 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001211 break;
1212 }
cristyd3090f92011-10-18 00:05:15 +00001213 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001214 {
cristy7d4aa382013-06-30 01:59:39 +00001215 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001216 break;
1217 }
cristyd3090f92011-10-18 00:05:15 +00001218 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001219 {
cristy7d4aa382013-06-30 01:59:39 +00001220 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001221 break;
1222 }
cristyd3090f92011-10-18 00:05:15 +00001223 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001224 {
cristy7d4aa382013-06-30 01:59:39 +00001225 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001226 break;
1227 }
cristy3ed852e2009-09-05 21:47:34 +00001228 }
1229 i++;
cristyed231572011-07-14 02:18:59 +00001230 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +00001231 }
1232 }
1233 if (fourier_info->modulus != MagickFalse)
1234 {
1235 i=0L;
cristybb503372010-05-27 20:51:26 +00001236 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1237 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001238 {
cristy7d4aa382013-06-30 01:59:39 +00001239 phase_pixels[i]-=0.5;
1240 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00001241 i++;
1242 }
1243 }
cristy3ed852e2009-09-05 21:47:34 +00001244 phase_view=DestroyCacheView(phase_view);
cristy13c99c42013-08-18 13:53:30 +00001245 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001246 if (status != MagickFalse)
1247 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001248 phase_pixels,inverse_pixels);
1249 (void) CopyMagickMemory(phase_pixels,inverse_pixels,fourier_info->height*
1250 fourier_info->center*sizeof(*phase_pixels));
1251 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001252 /*
1253 Merge two sets.
1254 */
1255 i=0L;
1256 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001257 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1258 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001259 {
cristy56ed31c2010-03-22 00:46:21 +00001260#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001261 fourier_pixels[i]=magnitude_pixels[i]*cos(phase_pixels[i])+I*
1262 magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001263#else
cristy699ae5b2013-07-03 13:41:29 +00001264 fourier_pixels[i][0]=magnitude_pixels[i]*cos(phase_pixels[i]);
1265 fourier_pixels[i][1]=magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001266#endif
cristy3ed852e2009-09-05 21:47:34 +00001267 i++;
1268 }
1269 else
cristybb503372010-05-27 20:51:26 +00001270 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1271 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001272 {
cristy56ed31c2010-03-22 00:46:21 +00001273#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001274 fourier_pixels[i]=magnitude_pixels[i]+I*phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001275#else
cristy699ae5b2013-07-03 13:41:29 +00001276 fourier_pixels[i][0]=magnitude_pixels[i];
1277 fourier_pixels[i][1]=phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001278#endif
cristy3ed852e2009-09-05 21:47:34 +00001279 i++;
1280 }
cristy699ae5b2013-07-03 13:41:29 +00001281 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1282 phase_info=RelinquishVirtualMemory(phase_info);
cristy3ed852e2009-09-05 21:47:34 +00001283 return(status);
1284}
1285
1286static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001287 fftw_complex *fourier_pixels,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001288{
1289 CacheView
1290 *image_view;
1291
cristy99dc0362013-09-15 18:32:54 +00001292 const char
1293 *value;
1294
cristy3ed852e2009-09-05 21:47:34 +00001295 double
cristy699ae5b2013-07-03 13:41:29 +00001296 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001297
1298 fftw_plan
1299 fftw_c2r_plan;
1300
cristy699ae5b2013-07-03 13:41:29 +00001301 MemoryInfo
1302 *source_info;
1303
cristy4c08aed2011-07-01 19:47:50 +00001304 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001305 *q;
1306
cristybb503372010-05-27 20:51:26 +00001307 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001308 i,
1309 x;
1310
cristyc4ea4a42011-01-24 01:43:30 +00001311 ssize_t
1312 y;
cristy3ed852e2009-09-05 21:47:34 +00001313
cristy699ae5b2013-07-03 13:41:29 +00001314 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
1315 fourier_info->width*sizeof(*source_pixels));
1316 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001317 {
1318 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001319 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001320 return(MagickFalse);
1321 }
cristy699ae5b2013-07-03 13:41:29 +00001322 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy99dc0362013-09-15 18:32:54 +00001323 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +00001324 if (LocaleCompare(value,"inverse") == 0)
cristy99dc0362013-09-15 18:32:54 +00001325 {
1326 double
1327 gamma;
1328
1329 /*
1330 Normalize inverse transform.
1331 */
1332 i=0L;
1333 gamma=PerceptibleReciprocal((double) fourier_info->width*
1334 fourier_info->height);
1335 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1336 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1337 {
1338#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1339 fourier_pixels[i]*=gamma;
1340#else
1341 fourier_pixels[i][0]*=gamma;
1342 fourier_pixels[i][1]*=gamma;
1343#endif
1344 i++;
1345 }
1346 }
cristyb5d5f722009-11-04 03:03:49 +00001347#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001348 #pragma omp critical (MagickCore_InverseFourierTransform)
1349#endif
cristydf079ac2010-09-10 01:45:44 +00001350 {
1351 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001352 fourier_pixels,source_pixels,FFTW_ESTIMATE);
cristydf079ac2010-09-10 01:45:44 +00001353 fftw_execute(fftw_c2r_plan);
1354 fftw_destroy_plan(fftw_c2r_plan);
1355 }
cristy3ed852e2009-09-05 21:47:34 +00001356 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001357 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001358 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001359 {
cristy85812052010-09-14 17:56:15 +00001360 if (y >= (ssize_t) image->rows)
1361 break;
1362 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1363 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001364 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001365 break;
cristybb503372010-05-27 20:51:26 +00001366 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001367 {
cristy233fe582012-07-07 14:00:18 +00001368 if (x < (ssize_t) image->columns)
1369 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001370 {
cristy233fe582012-07-07 14:00:18 +00001371 case RedPixelChannel:
1372 default:
1373 {
cristy699ae5b2013-07-03 13:41:29 +00001374 SetPixelRed(image,ClampToQuantum(QuantumRange*source_pixels[i]),q);
cristy233fe582012-07-07 14:00:18 +00001375 break;
1376 }
1377 case GreenPixelChannel:
1378 {
cristy699ae5b2013-07-03 13:41:29 +00001379 SetPixelGreen(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1380 q);
cristy233fe582012-07-07 14:00:18 +00001381 break;
1382 }
1383 case BluePixelChannel:
1384 {
cristy699ae5b2013-07-03 13:41:29 +00001385 SetPixelBlue(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1386 q);
cristy233fe582012-07-07 14:00:18 +00001387 break;
1388 }
1389 case BlackPixelChannel:
1390 {
cristy699ae5b2013-07-03 13:41:29 +00001391 SetPixelBlack(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1392 q);
cristy233fe582012-07-07 14:00:18 +00001393 break;
1394 }
1395 case AlphaPixelChannel:
1396 {
cristy699ae5b2013-07-03 13:41:29 +00001397 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1398 q);
cristy233fe582012-07-07 14:00:18 +00001399 break;
1400 }
cristy3ed852e2009-09-05 21:47:34 +00001401 }
cristy3ed852e2009-09-05 21:47:34 +00001402 i++;
cristyed231572011-07-14 02:18:59 +00001403 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001404 }
1405 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1406 break;
1407 }
1408 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +00001409 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +00001410 return(MagickTrue);
1411}
1412
cristyc9550792009-11-13 20:05:42 +00001413static MagickBooleanType InverseFourierTransformChannel(
1414 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001415 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001416 Image *fourier_image,ExceptionInfo *exception)
1417{
cristy3ed852e2009-09-05 21:47:34 +00001418 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +00001419 *inverse_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001420
1421 FourierInfo
1422 fourier_info;
1423
1424 MagickBooleanType
1425 status;
1426
cristy699ae5b2013-07-03 13:41:29 +00001427 MemoryInfo
1428 *inverse_info;
1429
cristy3ed852e2009-09-05 21:47:34 +00001430 size_t
1431 extent;
1432
cristyc9550792009-11-13 20:05:42 +00001433 fourier_info.width=magnitude_image->columns;
1434 if ((magnitude_image->columns != magnitude_image->rows) ||
1435 ((magnitude_image->columns % 2) != 0) ||
1436 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001437 {
cristyc9550792009-11-13 20:05:42 +00001438 extent=magnitude_image->columns < magnitude_image->rows ?
1439 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001440 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1441 }
1442 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001443 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001444 fourier_info.channel=channel;
1445 fourier_info.modulus=modulus;
cristy699ae5b2013-07-03 13:41:29 +00001446 inverse_info=AcquireVirtualMemory((size_t) fourier_info.height,
1447 fourier_info.center*sizeof(*inverse_pixels));
1448 if (inverse_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001449 {
1450 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001451 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001452 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001453 return(MagickFalse);
1454 }
cristy699ae5b2013-07-03 13:41:29 +00001455 inverse_pixels=(fftw_complex *) GetVirtualMemoryBlob(inverse_info);
1456 status=InverseFourier(&fourier_info,magnitude_image,phase_image,
1457 inverse_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001458 if (status != MagickFalse)
cristy699ae5b2013-07-03 13:41:29 +00001459 status=InverseFourierTransform(&fourier_info,inverse_pixels,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001460 exception);
cristy699ae5b2013-07-03 13:41:29 +00001461 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001462 return(status);
1463}
1464#endif
1465
cristyc9550792009-11-13 20:05:42 +00001466MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1467 const Image *phase_image,const MagickBooleanType modulus,
1468 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001469{
1470 Image
1471 *fourier_image;
1472
cristyc9550792009-11-13 20:05:42 +00001473 assert(magnitude_image != (Image *) NULL);
1474 assert(magnitude_image->signature == MagickSignature);
1475 if (magnitude_image->debug != MagickFalse)
1476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1477 magnitude_image->filename);
1478 if (phase_image == (Image *) NULL)
1479 {
1480 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001481 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001482 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001483 }
cristy3ed852e2009-09-05 21:47:34 +00001484#if !defined(MAGICKCORE_FFTW_DELEGATE)
1485 fourier_image=(Image *) NULL;
1486 (void) modulus;
1487 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001488 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001489 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001490#else
1491 {
cristyc9550792009-11-13 20:05:42 +00001492 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
cristy4c9c4d02013-10-08 00:05:57 +00001493 magnitude_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001494 if (fourier_image != (Image *) NULL)
1495 {
1496 MagickBooleanType
1497 is_gray,
1498 status;
1499
cristy3ed852e2009-09-05 21:47:34 +00001500 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001501 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001502 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001503 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001504#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001505 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001506#endif
cristy3ed852e2009-09-05 21:47:34 +00001507 {
cristyb34ef052010-10-07 00:12:05 +00001508#if defined(MAGICKCORE_OPENMP_SUPPORT)
1509 #pragma omp section
1510#endif
cristy3ed852e2009-09-05 21:47:34 +00001511 {
cristyb34ef052010-10-07 00:12:05 +00001512 MagickBooleanType
1513 thread_status;
1514
1515 if (is_gray != MagickFalse)
1516 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001517 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001518 else
cristyc9550792009-11-13 20:05:42 +00001519 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001520 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001521 if (thread_status == MagickFalse)
1522 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001523 }
cristyb34ef052010-10-07 00:12:05 +00001524#if defined(MAGICKCORE_OPENMP_SUPPORT)
1525 #pragma omp section
1526#endif
1527 {
1528 MagickBooleanType
1529 thread_status;
1530
1531 thread_status=MagickTrue;
1532 if (is_gray == MagickFalse)
1533 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001534 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001535 if (thread_status == MagickFalse)
1536 status=thread_status;
1537 }
1538#if defined(MAGICKCORE_OPENMP_SUPPORT)
1539 #pragma omp section
1540#endif
1541 {
1542 MagickBooleanType
1543 thread_status;
1544
1545 thread_status=MagickTrue;
1546 if (is_gray == MagickFalse)
1547 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001548 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001549 if (thread_status == MagickFalse)
1550 status=thread_status;
1551 }
1552#if defined(MAGICKCORE_OPENMP_SUPPORT)
1553 #pragma omp section
1554#endif
1555 {
1556 MagickBooleanType
1557 thread_status;
1558
1559 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001560 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001561 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001562 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001563 if (thread_status == MagickFalse)
1564 status=thread_status;
1565 }
1566#if defined(MAGICKCORE_OPENMP_SUPPORT)
1567 #pragma omp section
1568#endif
1569 {
1570 MagickBooleanType
1571 thread_status;
1572
1573 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001574 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001575 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001576 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001577 if (thread_status == MagickFalse)
1578 status=thread_status;
1579 }
cristy3ed852e2009-09-05 21:47:34 +00001580 }
1581 if (status == MagickFalse)
1582 fourier_image=DestroyImage(fourier_image);
1583 }
cristyb34ef052010-10-07 00:12:05 +00001584 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001585 }
1586#endif
1587 return(fourier_image);
1588}