blob: 08e53017c0ec73409b64909a3efeaff61a41a4e8 [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 {
cristyf46941c2013-10-06 22:25:41 +0000294 Cr[i]=(Ar[i]*Br[i]-Ai[i]*Bi[i]);
295 Ci[i]=(Ai[i]*Br[i]+Ar[i]*Bi[i]);
296 break;
297 }
298 case RealImaginaryComplexOperator:
299 {
cristy8526a972013-10-08 00:52:54 +0000300 Cr[i]=Ar[i]*exp(Ai[i]);
301 Ci[i]=Ar[i]*(cos(Ai[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,
903 width;
904
905 width=image->columns;
906 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
907 ((image->rows % 2) != 0))
908 {
909 extent=image->columns < image->rows ? image->rows : image->columns;
910 width=(extent & 0x01) == 1 ? extent+1UL : extent;
911 }
cristy4c9c4d02013-10-08 00:05:57 +0000912 magnitude_image=CloneImage(image,width,width,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000913 if (magnitude_image != (Image *) NULL)
914 {
915 Image
916 *phase_image;
917
918 magnitude_image->storage_class=DirectClass;
919 magnitude_image->depth=32UL;
cristy4c9c4d02013-10-08 00:05:57 +0000920 phase_image=CloneImage(image,width,width,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000921 if (phase_image == (Image *) NULL)
922 magnitude_image=DestroyImage(magnitude_image);
923 else
924 {
925 MagickBooleanType
926 is_gray,
927 status;
928
cristy3ed852e2009-09-05 21:47:34 +0000929 phase_image->storage_class=DirectClass;
930 phase_image->depth=32UL;
931 AppendImageToList(&fourier_image,magnitude_image);
932 AppendImageToList(&fourier_image,phase_image);
933 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000934 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000935#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000936 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000937#endif
cristy3ed852e2009-09-05 21:47:34 +0000938 {
cristyb34ef052010-10-07 00:12:05 +0000939#if defined(MAGICKCORE_OPENMP_SUPPORT)
940 #pragma omp section
941#endif
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristyb34ef052010-10-07 00:12:05 +0000943 MagickBooleanType
944 thread_status;
945
946 if (is_gray != MagickFalse)
947 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000948 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000949 else
950 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000951 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000952 if (thread_status == MagickFalse)
953 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000954 }
cristyb34ef052010-10-07 00:12:05 +0000955#if defined(MAGICKCORE_OPENMP_SUPPORT)
956 #pragma omp section
957#endif
958 {
959 MagickBooleanType
960 thread_status;
961
962 thread_status=MagickTrue;
963 if (is_gray == MagickFalse)
964 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000965 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000966 if (thread_status == MagickFalse)
967 status=thread_status;
968 }
969#if defined(MAGICKCORE_OPENMP_SUPPORT)
970 #pragma omp section
971#endif
972 {
973 MagickBooleanType
974 thread_status;
975
976 thread_status=MagickTrue;
977 if (is_gray == MagickFalse)
978 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000979 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000980 if (thread_status == MagickFalse)
981 status=thread_status;
982 }
983#if defined(MAGICKCORE_OPENMP_SUPPORT)
984 #pragma omp section
985#endif
986 {
987 MagickBooleanType
988 thread_status;
989
990 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000991 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000992 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000993 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000994 if (thread_status == MagickFalse)
995 status=thread_status;
996 }
997#if defined(MAGICKCORE_OPENMP_SUPPORT)
998 #pragma omp section
999#endif
1000 {
1001 MagickBooleanType
1002 thread_status;
1003
1004 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001005 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001006 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +00001007 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001008 if (thread_status == MagickFalse)
1009 status=thread_status;
1010 }
cristy3ed852e2009-09-05 21:47:34 +00001011 }
1012 if (status == MagickFalse)
1013 fourier_image=DestroyImageList(fourier_image);
1014 fftw_cleanup();
1015 }
1016 }
1017 }
1018#endif
1019 return(fourier_image);
1020}
1021
1022/*
1023%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1024% %
1025% %
1026% %
1027% 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 %
1028% %
1029% %
1030% %
1031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032%
1033% InverseFourierTransformImage() implements the inverse discrete Fourier
1034% transform (DFT) of the image either as a magnitude / phase or real /
1035% imaginary image pair.
1036%
1037% The format of the InverseFourierTransformImage method is:
1038%
cristyc9550792009-11-13 20:05:42 +00001039% Image *InverseFourierTransformImage(const Image *magnitude_image,
1040% const Image *phase_image,const MagickBooleanType modulus,
1041% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001042%
1043% A description of each parameter follows:
1044%
cristyc9550792009-11-13 20:05:42 +00001045% o magnitude_image: the magnitude or real image.
1046%
1047% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +00001048%
1049% o modulus: if true, return transform as a magnitude / phase pair
1050% otherwise a real / imaginary image pair.
1051%
1052% o exception: return any errors or warnings in this structure.
1053%
1054*/
1055
1056#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +00001057static MagickBooleanType InverseQuadrantSwap(const size_t width,
1058 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +00001059{
cristyc4ea4a42011-01-24 01:43:30 +00001060 register ssize_t
1061 x;
1062
cristybb503372010-05-27 20:51:26 +00001063 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001064 center,
1065 y;
1066
cristy3ed852e2009-09-05 21:47:34 +00001067 /*
1068 Swap quadrants.
1069 */
cristy233fe582012-07-07 14:00:18 +00001070 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +00001071 for (y=1L; y < (ssize_t) height; y++)
1072 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +00001073 destination[(height-y)*center-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +00001074 for (y=0L; y < (ssize_t) height; y++)
cristy3d04ed72013-08-18 17:51:01 +00001075 destination[y*center]=source[y*width+width/2L];
cristy3ed852e2009-09-05 21:47:34 +00001076 for (x=0L; x < center; x++)
1077 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +00001078 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +00001079}
1080
1081static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001082 const Image *magnitude_image,const Image *phase_image,
1083 fftw_complex *fourier_pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001084{
1085 CacheView
1086 *magnitude_view,
1087 *phase_view;
1088
1089 double
cristy699ae5b2013-07-03 13:41:29 +00001090 *inverse_pixels,
cristy7d4aa382013-06-30 01:59:39 +00001091 *magnitude_pixels,
1092 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001093
cristy3ed852e2009-09-05 21:47:34 +00001094 MagickBooleanType
1095 status;
1096
cristy699ae5b2013-07-03 13:41:29 +00001097 MemoryInfo
1098 *inverse_info,
1099 *magnitude_info,
1100 *phase_info;
1101
cristy4c08aed2011-07-01 19:47:50 +00001102 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001103 *p;
1104
cristybb503372010-05-27 20:51:26 +00001105 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001106 i,
1107 x;
1108
cristyc4ea4a42011-01-24 01:43:30 +00001109 ssize_t
1110 y;
1111
cristy3ed852e2009-09-05 21:47:34 +00001112 /*
1113 Inverse fourier - read image and break down into a double array.
1114 */
cristy699ae5b2013-07-03 13:41:29 +00001115 magnitude_info=AcquireVirtualMemory((size_t)fourier_info->height,
1116 fourier_info->width*sizeof(*magnitude_pixels));
1117 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +00001118 fourier_info->width*sizeof(*phase_pixels));
cristy699ae5b2013-07-03 13:41:29 +00001119 inverse_info=AcquireVirtualMemory((size_t) fourier_info->height,
1120 fourier_info->center*sizeof(*inverse_pixels));
1121 if ((magnitude_info == (MemoryInfo *) NULL) ||
1122 (phase_info == (MemoryInfo *) NULL) ||
1123 (inverse_info == (MemoryInfo *) NULL))
cristybb3c02e2013-07-02 00:43:10 +00001124 {
cristy699ae5b2013-07-03 13:41:29 +00001125 if (magnitude_info != (MemoryInfo *) NULL)
1126 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1127 if (phase_info != (MemoryInfo *) NULL)
1128 phase_info=RelinquishVirtualMemory(phase_info);
1129 if (inverse_info != (MemoryInfo *) NULL)
1130 inverse_info=RelinquishVirtualMemory(inverse_info);
cristybb3c02e2013-07-02 00:43:10 +00001131 (void) ThrowMagickException(exception,GetMagickModule(),
1132 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1133 magnitude_image->filename);
cristybb3c02e2013-07-02 00:43:10 +00001134 return(MagickFalse);
1135 }
cristy699ae5b2013-07-03 13:41:29 +00001136 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
1137 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
1138 inverse_pixels=(double *) GetVirtualMemoryBlob(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001139 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001140 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +00001141 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001142 {
1143 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
1144 exception);
cristy4c08aed2011-07-01 19:47:50 +00001145 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001146 break;
cristybb503372010-05-27 20:51:26 +00001147 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001148 {
1149 switch (fourier_info->channel)
1150 {
cristyd3090f92011-10-18 00:05:15 +00001151 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001152 default:
1153 {
cristy7d4aa382013-06-30 01:59:39 +00001154 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001155 break;
1156 }
cristyd3090f92011-10-18 00:05:15 +00001157 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001158 {
cristy7d4aa382013-06-30 01:59:39 +00001159 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001160 break;
1161 }
cristyd3090f92011-10-18 00:05:15 +00001162 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001163 {
cristy7d4aa382013-06-30 01:59:39 +00001164 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001165 break;
1166 }
cristyd3090f92011-10-18 00:05:15 +00001167 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001168 {
cristy7d4aa382013-06-30 01:59:39 +00001169 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001170 break;
1171 }
cristyd3090f92011-10-18 00:05:15 +00001172 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001173 {
cristy7d4aa382013-06-30 01:59:39 +00001174 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001175 break;
1176 }
cristy3ed852e2009-09-05 21:47:34 +00001177 }
1178 i++;
cristyed231572011-07-14 02:18:59 +00001179 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +00001180 }
1181 }
cristy699ae5b2013-07-03 13:41:29 +00001182 magnitude_view=DestroyCacheView(magnitude_view);
1183 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
1184 magnitude_pixels,inverse_pixels);
1185 (void) CopyMagickMemory(magnitude_pixels,inverse_pixels,fourier_info->height*
1186 fourier_info->center*sizeof(*magnitude_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001187 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001188 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +00001189 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001190 {
1191 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
1192 exception);
cristy4c08aed2011-07-01 19:47:50 +00001193 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001194 break;
cristybb503372010-05-27 20:51:26 +00001195 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001196 {
1197 switch (fourier_info->channel)
1198 {
cristyd3090f92011-10-18 00:05:15 +00001199 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001200 default:
1201 {
cristy7d4aa382013-06-30 01:59:39 +00001202 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001203 break;
1204 }
cristyd3090f92011-10-18 00:05:15 +00001205 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001206 {
cristy7d4aa382013-06-30 01:59:39 +00001207 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001208 break;
1209 }
cristyd3090f92011-10-18 00:05:15 +00001210 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001211 {
cristy7d4aa382013-06-30 01:59:39 +00001212 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001213 break;
1214 }
cristyd3090f92011-10-18 00:05:15 +00001215 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001216 {
cristy7d4aa382013-06-30 01:59:39 +00001217 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001218 break;
1219 }
cristyd3090f92011-10-18 00:05:15 +00001220 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001221 {
cristy7d4aa382013-06-30 01:59:39 +00001222 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001223 break;
1224 }
cristy3ed852e2009-09-05 21:47:34 +00001225 }
1226 i++;
cristyed231572011-07-14 02:18:59 +00001227 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +00001228 }
1229 }
1230 if (fourier_info->modulus != MagickFalse)
1231 {
1232 i=0L;
cristybb503372010-05-27 20:51:26 +00001233 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1234 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001235 {
cristy7d4aa382013-06-30 01:59:39 +00001236 phase_pixels[i]-=0.5;
1237 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00001238 i++;
1239 }
1240 }
cristy3ed852e2009-09-05 21:47:34 +00001241 phase_view=DestroyCacheView(phase_view);
cristy13c99c42013-08-18 13:53:30 +00001242 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001243 if (status != MagickFalse)
1244 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001245 phase_pixels,inverse_pixels);
1246 (void) CopyMagickMemory(phase_pixels,inverse_pixels,fourier_info->height*
1247 fourier_info->center*sizeof(*phase_pixels));
1248 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001249 /*
1250 Merge two sets.
1251 */
1252 i=0L;
1253 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001254 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1255 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001256 {
cristy56ed31c2010-03-22 00:46:21 +00001257#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001258 fourier_pixels[i]=magnitude_pixels[i]*cos(phase_pixels[i])+I*
1259 magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001260#else
cristy699ae5b2013-07-03 13:41:29 +00001261 fourier_pixels[i][0]=magnitude_pixels[i]*cos(phase_pixels[i]);
1262 fourier_pixels[i][1]=magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001263#endif
cristy3ed852e2009-09-05 21:47:34 +00001264 i++;
1265 }
1266 else
cristybb503372010-05-27 20:51:26 +00001267 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1268 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001269 {
cristy56ed31c2010-03-22 00:46:21 +00001270#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001271 fourier_pixels[i]=magnitude_pixels[i]+I*phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001272#else
cristy699ae5b2013-07-03 13:41:29 +00001273 fourier_pixels[i][0]=magnitude_pixels[i];
1274 fourier_pixels[i][1]=phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001275#endif
cristy3ed852e2009-09-05 21:47:34 +00001276 i++;
1277 }
cristy699ae5b2013-07-03 13:41:29 +00001278 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1279 phase_info=RelinquishVirtualMemory(phase_info);
cristy3ed852e2009-09-05 21:47:34 +00001280 return(status);
1281}
1282
1283static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001284 fftw_complex *fourier_pixels,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001285{
1286 CacheView
1287 *image_view;
1288
cristy99dc0362013-09-15 18:32:54 +00001289 const char
1290 *value;
1291
cristy3ed852e2009-09-05 21:47:34 +00001292 double
cristy699ae5b2013-07-03 13:41:29 +00001293 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001294
1295 fftw_plan
1296 fftw_c2r_plan;
1297
cristy699ae5b2013-07-03 13:41:29 +00001298 MemoryInfo
1299 *source_info;
1300
cristy4c08aed2011-07-01 19:47:50 +00001301 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001302 *q;
1303
cristybb503372010-05-27 20:51:26 +00001304 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001305 i,
1306 x;
1307
cristyc4ea4a42011-01-24 01:43:30 +00001308 ssize_t
1309 y;
cristy3ed852e2009-09-05 21:47:34 +00001310
cristy699ae5b2013-07-03 13:41:29 +00001311 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
1312 fourier_info->width*sizeof(*source_pixels));
1313 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001314 {
1315 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001316 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001317 return(MagickFalse);
1318 }
cristy699ae5b2013-07-03 13:41:29 +00001319 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy99dc0362013-09-15 18:32:54 +00001320 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +00001321 if (LocaleCompare(value,"inverse") == 0)
cristy99dc0362013-09-15 18:32:54 +00001322 {
1323 double
1324 gamma;
1325
1326 /*
1327 Normalize inverse transform.
1328 */
1329 i=0L;
1330 gamma=PerceptibleReciprocal((double) fourier_info->width*
1331 fourier_info->height);
1332 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1333 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1334 {
1335#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1336 fourier_pixels[i]*=gamma;
1337#else
1338 fourier_pixels[i][0]*=gamma;
1339 fourier_pixels[i][1]*=gamma;
1340#endif
1341 i++;
1342 }
1343 }
cristyb5d5f722009-11-04 03:03:49 +00001344#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001345 #pragma omp critical (MagickCore_InverseFourierTransform)
1346#endif
cristydf079ac2010-09-10 01:45:44 +00001347 {
1348 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001349 fourier_pixels,source_pixels,FFTW_ESTIMATE);
cristydf079ac2010-09-10 01:45:44 +00001350 fftw_execute(fftw_c2r_plan);
1351 fftw_destroy_plan(fftw_c2r_plan);
1352 }
cristy3ed852e2009-09-05 21:47:34 +00001353 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001354 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001355 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001356 {
cristy85812052010-09-14 17:56:15 +00001357 if (y >= (ssize_t) image->rows)
1358 break;
1359 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1360 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001361 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001362 break;
cristybb503372010-05-27 20:51:26 +00001363 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001364 {
cristy233fe582012-07-07 14:00:18 +00001365 if (x < (ssize_t) image->columns)
1366 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001367 {
cristy233fe582012-07-07 14:00:18 +00001368 case RedPixelChannel:
1369 default:
1370 {
cristy699ae5b2013-07-03 13:41:29 +00001371 SetPixelRed(image,ClampToQuantum(QuantumRange*source_pixels[i]),q);
cristy233fe582012-07-07 14:00:18 +00001372 break;
1373 }
1374 case GreenPixelChannel:
1375 {
cristy699ae5b2013-07-03 13:41:29 +00001376 SetPixelGreen(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1377 q);
cristy233fe582012-07-07 14:00:18 +00001378 break;
1379 }
1380 case BluePixelChannel:
1381 {
cristy699ae5b2013-07-03 13:41:29 +00001382 SetPixelBlue(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1383 q);
cristy233fe582012-07-07 14:00:18 +00001384 break;
1385 }
1386 case BlackPixelChannel:
1387 {
cristy699ae5b2013-07-03 13:41:29 +00001388 SetPixelBlack(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1389 q);
cristy233fe582012-07-07 14:00:18 +00001390 break;
1391 }
1392 case AlphaPixelChannel:
1393 {
cristy699ae5b2013-07-03 13:41:29 +00001394 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1395 q);
cristy233fe582012-07-07 14:00:18 +00001396 break;
1397 }
cristy3ed852e2009-09-05 21:47:34 +00001398 }
cristy3ed852e2009-09-05 21:47:34 +00001399 i++;
cristyed231572011-07-14 02:18:59 +00001400 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001401 }
1402 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1403 break;
1404 }
1405 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +00001406 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +00001407 return(MagickTrue);
1408}
1409
cristyc9550792009-11-13 20:05:42 +00001410static MagickBooleanType InverseFourierTransformChannel(
1411 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001412 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001413 Image *fourier_image,ExceptionInfo *exception)
1414{
cristy3ed852e2009-09-05 21:47:34 +00001415 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +00001416 *inverse_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001417
1418 FourierInfo
1419 fourier_info;
1420
1421 MagickBooleanType
1422 status;
1423
cristy699ae5b2013-07-03 13:41:29 +00001424 MemoryInfo
1425 *inverse_info;
1426
cristy3ed852e2009-09-05 21:47:34 +00001427 size_t
1428 extent;
1429
cristyc9550792009-11-13 20:05:42 +00001430 fourier_info.width=magnitude_image->columns;
1431 if ((magnitude_image->columns != magnitude_image->rows) ||
1432 ((magnitude_image->columns % 2) != 0) ||
1433 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001434 {
cristyc9550792009-11-13 20:05:42 +00001435 extent=magnitude_image->columns < magnitude_image->rows ?
1436 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001437 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1438 }
1439 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001440 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001441 fourier_info.channel=channel;
1442 fourier_info.modulus=modulus;
cristy699ae5b2013-07-03 13:41:29 +00001443 inverse_info=AcquireVirtualMemory((size_t) fourier_info.height,
1444 fourier_info.center*sizeof(*inverse_pixels));
1445 if (inverse_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001446 {
1447 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001448 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001449 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001450 return(MagickFalse);
1451 }
cristy699ae5b2013-07-03 13:41:29 +00001452 inverse_pixels=(fftw_complex *) GetVirtualMemoryBlob(inverse_info);
1453 status=InverseFourier(&fourier_info,magnitude_image,phase_image,
1454 inverse_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001455 if (status != MagickFalse)
cristy699ae5b2013-07-03 13:41:29 +00001456 status=InverseFourierTransform(&fourier_info,inverse_pixels,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001457 exception);
cristy699ae5b2013-07-03 13:41:29 +00001458 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001459 return(status);
1460}
1461#endif
1462
cristyc9550792009-11-13 20:05:42 +00001463MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1464 const Image *phase_image,const MagickBooleanType modulus,
1465 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001466{
1467 Image
1468 *fourier_image;
1469
cristyc9550792009-11-13 20:05:42 +00001470 assert(magnitude_image != (Image *) NULL);
1471 assert(magnitude_image->signature == MagickSignature);
1472 if (magnitude_image->debug != MagickFalse)
1473 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1474 magnitude_image->filename);
1475 if (phase_image == (Image *) NULL)
1476 {
1477 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001478 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001479 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001480 }
cristy3ed852e2009-09-05 21:47:34 +00001481#if !defined(MAGICKCORE_FFTW_DELEGATE)
1482 fourier_image=(Image *) NULL;
1483 (void) modulus;
1484 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001485 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001486 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001487#else
1488 {
cristyc9550792009-11-13 20:05:42 +00001489 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
cristy4c9c4d02013-10-08 00:05:57 +00001490 magnitude_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001491 if (fourier_image != (Image *) NULL)
1492 {
1493 MagickBooleanType
1494 is_gray,
1495 status;
1496
cristy3ed852e2009-09-05 21:47:34 +00001497 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001498 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001499 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001500 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001501#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001502 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001503#endif
cristy3ed852e2009-09-05 21:47:34 +00001504 {
cristyb34ef052010-10-07 00:12:05 +00001505#if defined(MAGICKCORE_OPENMP_SUPPORT)
1506 #pragma omp section
1507#endif
cristy3ed852e2009-09-05 21:47:34 +00001508 {
cristyb34ef052010-10-07 00:12:05 +00001509 MagickBooleanType
1510 thread_status;
1511
1512 if (is_gray != MagickFalse)
1513 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001514 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001515 else
cristyc9550792009-11-13 20:05:42 +00001516 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001517 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001518 if (thread_status == MagickFalse)
1519 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001520 }
cristyb34ef052010-10-07 00:12:05 +00001521#if defined(MAGICKCORE_OPENMP_SUPPORT)
1522 #pragma omp section
1523#endif
1524 {
1525 MagickBooleanType
1526 thread_status;
1527
1528 thread_status=MagickTrue;
1529 if (is_gray == MagickFalse)
1530 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001531 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001532 if (thread_status == MagickFalse)
1533 status=thread_status;
1534 }
1535#if defined(MAGICKCORE_OPENMP_SUPPORT)
1536 #pragma omp section
1537#endif
1538 {
1539 MagickBooleanType
1540 thread_status;
1541
1542 thread_status=MagickTrue;
1543 if (is_gray == MagickFalse)
1544 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001545 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001546 if (thread_status == MagickFalse)
1547 status=thread_status;
1548 }
1549#if defined(MAGICKCORE_OPENMP_SUPPORT)
1550 #pragma omp section
1551#endif
1552 {
1553 MagickBooleanType
1554 thread_status;
1555
1556 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001557 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001558 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001559 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001560 if (thread_status == MagickFalse)
1561 status=thread_status;
1562 }
1563#if defined(MAGICKCORE_OPENMP_SUPPORT)
1564 #pragma omp section
1565#endif
1566 {
1567 MagickBooleanType
1568 thread_status;
1569
1570 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001571 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001572 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001573 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001574 if (thread_status == MagickFalse)
1575 status=thread_status;
1576 }
cristy3ed852e2009-09-05 21:47:34 +00001577 }
1578 if (status == MagickFalse)
1579 fourier_image=DestroyImage(fourier_image);
1580 }
cristyb34ef052010-10-07 00:12:05 +00001581 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001582 }
1583#endif
1584 return(fourier_image);
1585}