blob: 7ff6d62baadb6572104e4a71f54c12e1dccf3b6e [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;
202 if ((images->next->next == (Image *) NULL) ||
203 (images->next->next->next == (Image *) NULL))
204 {
205 Br_image=images;
206 Bi_image=images->next;
207 }
208 else
209 {
210 Br_image=images->next->next;
211 Bi_image=images->next->next->next;
212 }
213 Cr_image=complex_images;
214 Ci_image=complex_images->next;
215 Ar_view=AcquireVirtualCacheView(Ar_image,exception);
216 Ai_view=AcquireVirtualCacheView(Ai_image,exception);
217 Br_view=AcquireVirtualCacheView(Br_image,exception);
218 Bi_view=AcquireVirtualCacheView(Bi_image,exception);
219 Cr_view=AcquireAuthenticCacheView(Cr_image,exception);
220 Ci_view=AcquireAuthenticCacheView(Ci_image,exception);
221 status=MagickTrue;
222 progress=0;
223#if defined(MAGICKCORE_OPENMP_SUPPORT)
224 #pragma omp parallel for schedule(static,4) shared(progress,status) \
225 magick_threads(images,complex_images,images->rows,1L)
226#endif
227 for (y=0; y < (ssize_t) images->rows; y++)
228 {
229 register const Quantum
230 *restrict Ai,
231 *restrict Ar,
232 *restrict Bi,
233 *restrict Br;
234
235 register Quantum
236 *restrict Ci,
237 *restrict Cr;
238
239 register ssize_t
240 x;
241
cristy34919ed2013-10-06 15:42:40 +0000242 if (status == MagickFalse)
243 continue;
cristy1042ed22013-10-05 17:38:54 +0000244 Ar=GetCacheViewVirtualPixels(Ar_view,0,y,images->columns,1,exception);
245 Ai=GetCacheViewVirtualPixels(Ai_view,0,y,images->columns,1,exception);
246 Br=GetCacheViewVirtualPixels(Br_view,0,y,images->columns,1,exception);
247 Bi=GetCacheViewVirtualPixels(Bi_view,0,y,images->columns,1,exception);
248 Cr=QueueCacheViewAuthenticPixels(Cr_view,0,y,images->columns,1,exception);
249 Ci=QueueCacheViewAuthenticPixels(Ci_view,0,y,images->columns,1,exception);
250 if ((Ar == (const Quantum *) NULL) || (Ai == (const Quantum *) NULL) ||
251 (Br == (const Quantum *) NULL) || (Bi == (const Quantum *) NULL) ||
252 (Cr == (Quantum *) NULL) || (Ci == (Quantum *) NULL))
253 {
254 status=MagickFalse;
255 continue;
256 }
257 for (x=0; x < (ssize_t) images->columns; x++)
258 {
cristy9f654472013-10-05 19:44:06 +0000259 register ssize_t
260 i;
261
262 for (i=0; i < (ssize_t) GetPixelChannels(images); i++)
263 {
264 switch (operator)
265 {
cristy19f78862013-10-05 22:21:46 +0000266 case AddComplexOperator:
267 {
268 Cr[i]=Ar[i]+Br[i];
269 Ci[i]=Ai[i]+Bi[i];
270 break;
271 }
cristy9f654472013-10-05 19:44:06 +0000272 case ConjugateComplexOperator:
273 default:
274 {
275 Cr[i]=Ar[i];
276 Ci[i]=(-Bi[i]);
277 break;
278 }
279 case DivideComplexOperator:
280 {
281 double
282 gamma;
283
284 gamma=PerceptibleReciprocal(Br[i]*Br[i]+Bi[i]*Bi[i]);
285 Cr[i]=gamma*(Ar[i]*Br[i]+Ai[i]*Bi[i]);
286 Ci[i]=gamma*(Ai[i]*Br[i]-Ai[i]*Bi[i]);
287 break;
288 }
cristyf46941c2013-10-06 22:25:41 +0000289 case MagnitudePhaseComplexOperator:
290 {
cristy8526a972013-10-08 00:52:54 +0000291 Cr[i]=sqrt(Ar[i]*Ar[i]+Ai[i]*Ai[i]);
292 Ci[i]=atan2(Ai[i],Ar[i]);
cristyf46941c2013-10-06 22:25:41 +0000293 break;
294 }
cristy9f654472013-10-05 19:44:06 +0000295 case MultiplyComplexOperator:
296 {
cristyf46941c2013-10-06 22:25:41 +0000297 Cr[i]=(Ar[i]*Br[i]-Ai[i]*Bi[i]);
298 Ci[i]=(Ai[i]*Br[i]+Ar[i]*Bi[i]);
299 break;
300 }
301 case RealImaginaryComplexOperator:
302 {
cristy8526a972013-10-08 00:52:54 +0000303 Cr[i]=Ar[i]*exp(Ai[i]);
304 Ci[i]=Ar[i]*(cos(Ai[i])+sin(Ai[i]));
cristy9f654472013-10-05 19:44:06 +0000305 break;
306 }
cristy19f78862013-10-05 22:21:46 +0000307 case SubtractComplexOperator:
308 {
309 Cr[i]=Ar[i]-Br[i];
310 Ci[i]=Ai[i]-Bi[i];
311 break;
312 }
cristy9f654472013-10-05 19:44:06 +0000313 }
cristy19f78862013-10-05 22:21:46 +0000314 Ar+=GetPixelChannels(Ar_image);
315 Ai+=GetPixelChannels(Ai_image);
316 Br+=GetPixelChannels(Br_image);
317 Bi+=GetPixelChannels(Bi_image);
318 Cr+=GetPixelChannels(Cr_image);
319 Ci+=GetPixelChannels(Ci_image);
cristy9f654472013-10-05 19:44:06 +0000320 }
cristy1042ed22013-10-05 17:38:54 +0000321 }
322 if (SyncCacheViewAuthenticPixels(Ci_view,exception) == MagickFalse)
323 status=MagickFalse;
324 if (SyncCacheViewAuthenticPixels(Cr_view,exception) == MagickFalse)
325 status=MagickFalse;
326 if (images->progress_monitor != (MagickProgressMonitor) NULL)
327 {
328 MagickBooleanType
329 proceed;
330
331#if defined(MAGICKCORE_OPENMP_SUPPORT)
332 #pragma omp critical (MagickCore_ComplexImages)
333#endif
334 proceed=SetImageProgress(images,ComplexImageTag,progress++,
335 images->rows);
336 if (proceed == MagickFalse)
337 status=MagickFalse;
338 }
339 }
340 Cr_view=DestroyCacheView(Cr_view);
341 Ci_view=DestroyCacheView(Ci_view);
342 Br_view=DestroyCacheView(Br_view);
343 Bi_view=DestroyCacheView(Bi_view);
344 Ar_view=DestroyCacheView(Ar_view);
345 Ai_view=DestroyCacheView(Ai_view);
346 if (status == MagickFalse)
347 complex_images=DestroyImageList(complex_images);
348 return(complex_images);
cristy790190d2013-10-04 00:51:51 +0000349}
350
351/*
352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353% %
354% %
355% %
cristy3ed852e2009-09-05 21:47:34 +0000356% 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 %
357% %
358% %
359% %
360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361%
362% ForwardFourierTransformImage() implements the discrete Fourier transform
363% (DFT) of the image either as a magnitude / phase or real / imaginary image
364% pair.
365%
366% The format of the ForwadFourierTransformImage method is:
367%
368% Image *ForwardFourierTransformImage(const Image *image,
369% const MagickBooleanType modulus,ExceptionInfo *exception)
370%
371% A description of each parameter follows:
372%
373% o image: the image.
374%
375% o modulus: if true, return as transform as a magnitude / phase pair
376% otherwise a real / imaginary image pair.
377%
378% o exception: return any errors or warnings in this structure.
379%
380*/
381
382#if defined(MAGICKCORE_FFTW_DELEGATE)
383
cristyc4ea4a42011-01-24 01:43:30 +0000384static MagickBooleanType RollFourier(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000385 const ssize_t x_offset,const ssize_t y_offset,double *roll_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000386{
387 double
cristy699ae5b2013-07-03 13:41:29 +0000388 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000389
cristy7d4aa382013-06-30 01:59:39 +0000390 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000391 *source_info;
cristy7d4aa382013-06-30 01:59:39 +0000392
cristyc4ea4a42011-01-24 01:43:30 +0000393 register ssize_t
394 i,
395 x;
396
cristybb503372010-05-27 20:51:26 +0000397 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000398 u,
399 v,
400 y;
401
cristy3ed852e2009-09-05 21:47:34 +0000402 /*
cristy2da05352010-09-15 19:22:17 +0000403 Move zero frequency (DC, average color) from (0,0) to (width/2,height/2).
cristy3ed852e2009-09-05 21:47:34 +0000404 */
cristy699ae5b2013-07-03 13:41:29 +0000405 source_info=AcquireVirtualMemory(height,width*sizeof(*source_pixels));
406 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000407 return(MagickFalse);
cristy699ae5b2013-07-03 13:41:29 +0000408 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000409 i=0L;
cristybb503372010-05-27 20:51:26 +0000410 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000411 {
412 if (y_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000413 v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset;
cristy3ed852e2009-09-05 21:47:34 +0000414 else
cristybb503372010-05-27 20:51:26 +0000415 v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height :
cristy3ed852e2009-09-05 21:47:34 +0000416 y+y_offset;
cristybb503372010-05-27 20:51:26 +0000417 for (x=0L; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000418 {
419 if (x_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000420 u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset;
cristy3ed852e2009-09-05 21:47:34 +0000421 else
cristybb503372010-05-27 20:51:26 +0000422 u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width :
cristy3ed852e2009-09-05 21:47:34 +0000423 x+x_offset;
cristy699ae5b2013-07-03 13:41:29 +0000424 source_pixels[v*width+u]=roll_pixels[i++];
cristyc4ea4a42011-01-24 01:43:30 +0000425 }
cristy3ed852e2009-09-05 21:47:34 +0000426 }
cristy699ae5b2013-07-03 13:41:29 +0000427 (void) CopyMagickMemory(roll_pixels,source_pixels,height*width*
428 sizeof(*source_pixels));
429 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000430 return(MagickTrue);
431}
432
cristybb503372010-05-27 20:51:26 +0000433static MagickBooleanType ForwardQuadrantSwap(const size_t width,
cristy699ae5b2013-07-03 13:41:29 +0000434 const size_t height,double *source_pixels,double *forward_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000435{
cristy3ed852e2009-09-05 21:47:34 +0000436 MagickBooleanType
437 status;
438
cristybb503372010-05-27 20:51:26 +0000439 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000440 x;
441
cristyc4ea4a42011-01-24 01:43:30 +0000442 ssize_t
443 center,
444 y;
445
cristy3ed852e2009-09-05 21:47:34 +0000446 /*
447 Swap quadrants.
448 */
cristybb503372010-05-27 20:51:26 +0000449 center=(ssize_t) floor((double) width/2L)+1L;
cristy699ae5b2013-07-03 13:41:29 +0000450 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,
451 source_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000452 if (status == MagickFalse)
453 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000454 for (y=0L; y < (ssize_t) height; y++)
455 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy3d04ed72013-08-18 17:51:01 +0000456 forward_pixels[y*width+x+width/2L]=source_pixels[y*center+x];
cristybb503372010-05-27 20:51:26 +0000457 for (y=1; y < (ssize_t) height; y++)
458 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +0000459 forward_pixels[(height-y)*width+width/2L-x-1L]=
cristy3d04ed72013-08-18 17:51:01 +0000460 source_pixels[y*center+x+1L];
cristybb503372010-05-27 20:51:26 +0000461 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000462 forward_pixels[-x+width/2L-1L]=forward_pixels[x+width/2L+1L];
cristy3ed852e2009-09-05 21:47:34 +0000463 return(MagickTrue);
464}
465
cristyc4ea4a42011-01-24 01:43:30 +0000466static void CorrectPhaseLHS(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000467 double *fourier_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000468{
cristybb503372010-05-27 20:51:26 +0000469 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000470 x;
471
cristy9d314ff2011-03-09 01:30:28 +0000472 ssize_t
473 y;
474
cristybb503372010-05-27 20:51:26 +0000475 for (y=0L; y < (ssize_t) height; y++)
476 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000477 fourier_pixels[y*width+x]*=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +0000478}
479
480static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
481 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
482{
483 CacheView
484 *magnitude_view,
485 *phase_view;
486
487 double
cristy7d4aa382013-06-30 01:59:39 +0000488 *magnitude_pixels,
489 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000490
491 Image
492 *magnitude_image,
493 *phase_image;
494
cristy3ed852e2009-09-05 21:47:34 +0000495 MagickBooleanType
496 status;
497
cristy7d4aa382013-06-30 01:59:39 +0000498 MemoryInfo
499 *magnitude_info,
500 *phase_info;
501
cristy4c08aed2011-07-01 19:47:50 +0000502 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000503 *q;
504
cristyf5742792012-11-27 18:31:26 +0000505 register ssize_t
506 x;
507
cristyc4ea4a42011-01-24 01:43:30 +0000508 ssize_t
509 i,
510 y;
511
cristy3ed852e2009-09-05 21:47:34 +0000512 magnitude_image=GetFirstImageInList(image);
513 phase_image=GetNextImageInList(image);
514 if (phase_image == (Image *) NULL)
515 {
516 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +0000517 "TwoOrMoreImagesRequired","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000518 return(MagickFalse);
519 }
520 /*
521 Create "Fourier Transform" image from constituent arrays.
522 */
cristy7d4aa382013-06-30 01:59:39 +0000523 magnitude_info=AcquireVirtualMemory((size_t) fourier_info->height,
524 fourier_info->width*sizeof(*magnitude_pixels));
525 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
526 fourier_info->width*sizeof(*phase_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000527 if ((magnitude_info == (MemoryInfo *) NULL) ||
528 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000529 {
cristy7d4aa382013-06-30 01:59:39 +0000530 if (phase_info != (MemoryInfo *) NULL)
531 phase_info=RelinquishVirtualMemory(phase_info);
532 if (magnitude_info != (MemoryInfo *) NULL)
533 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000534 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000535 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000536 return(MagickFalse);
537 }
cristy7d4aa382013-06-30 01:59:39 +0000538 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
539 (void) ResetMagickMemory(magnitude_pixels,0,fourier_info->height*
540 fourier_info->width*sizeof(*magnitude_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000541 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
cristy7d4aa382013-06-30 01:59:39 +0000542 (void) ResetMagickMemory(phase_pixels,0,fourier_info->height*
543 fourier_info->width*sizeof(*phase_pixels));
cristy13c99c42013-08-18 13:53:30 +0000544 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
cristy7d4aa382013-06-30 01:59:39 +0000545 magnitude,magnitude_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000546 if (status != MagickFalse)
cristy13c99c42013-08-18 13:53:30 +0000547 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,phase,
cristy7d4aa382013-06-30 01:59:39 +0000548 phase_pixels);
cristy13c99c42013-08-18 13:53:30 +0000549 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000550 if (fourier_info->modulus != MagickFalse)
551 {
552 i=0L;
cristybb503372010-05-27 20:51:26 +0000553 for (y=0L; y < (ssize_t) fourier_info->height; y++)
554 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000555 {
cristy7d4aa382013-06-30 01:59:39 +0000556 phase_pixels[i]/=(2.0*MagickPI);
557 phase_pixels[i]+=0.5;
cristy3ed852e2009-09-05 21:47:34 +0000558 i++;
559 }
560 }
cristy46ff2672012-12-14 15:32:26 +0000561 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000562 i=0L;
cristybb503372010-05-27 20:51:26 +0000563 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000564 {
565 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
566 exception);
cristyacd2ed22011-08-30 01:44:23 +0000567 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000568 break;
cristybb503372010-05-27 20:51:26 +0000569 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000570 {
571 switch (fourier_info->channel)
572 {
cristyd3090f92011-10-18 00:05:15 +0000573 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000574 default:
575 {
cristy4c08aed2011-07-01 19:47:50 +0000576 SetPixelRed(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000577 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000578 break;
579 }
cristyd3090f92011-10-18 00:05:15 +0000580 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000581 {
cristy4c08aed2011-07-01 19:47:50 +0000582 SetPixelGreen(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000583 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000584 break;
585 }
cristyd3090f92011-10-18 00:05:15 +0000586 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristy4c08aed2011-07-01 19:47:50 +0000588 SetPixelBlue(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000589 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000590 break;
591 }
cristyd3090f92011-10-18 00:05:15 +0000592 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000593 {
cristy4c08aed2011-07-01 19:47:50 +0000594 SetPixelBlack(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000595 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000596 break;
597 }
cristyd3090f92011-10-18 00:05:15 +0000598 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000599 {
cristy4c08aed2011-07-01 19:47:50 +0000600 SetPixelAlpha(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000601 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000602 break;
603 }
cristy3ed852e2009-09-05 21:47:34 +0000604 }
605 i++;
cristyed231572011-07-14 02:18:59 +0000606 q+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000607 }
608 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
609 if (status == MagickFalse)
610 break;
611 }
cristydb070952012-04-20 14:33:00 +0000612 magnitude_view=DestroyCacheView(magnitude_view);
cristy3ed852e2009-09-05 21:47:34 +0000613 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000614 phase_view=AcquireAuthenticCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000615 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000616 {
617 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
618 exception);
cristyacd2ed22011-08-30 01:44:23 +0000619 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000620 break;
cristybb503372010-05-27 20:51:26 +0000621 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000622 {
623 switch (fourier_info->channel)
624 {
cristyd3090f92011-10-18 00:05:15 +0000625 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000626 default:
627 {
cristy4c08aed2011-07-01 19:47:50 +0000628 SetPixelRed(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000629 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000630 break;
631 }
cristyd3090f92011-10-18 00:05:15 +0000632 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000633 {
cristy4c08aed2011-07-01 19:47:50 +0000634 SetPixelGreen(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000635 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000636 break;
637 }
cristyd3090f92011-10-18 00:05:15 +0000638 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000639 {
cristy4c08aed2011-07-01 19:47:50 +0000640 SetPixelBlue(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000641 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000642 break;
643 }
cristyd3090f92011-10-18 00:05:15 +0000644 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristy4c08aed2011-07-01 19:47:50 +0000646 SetPixelBlack(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000647 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000648 break;
649 }
cristyd3090f92011-10-18 00:05:15 +0000650 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristy4c08aed2011-07-01 19:47:50 +0000652 SetPixelAlpha(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000653 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000654 break;
655 }
cristy3ed852e2009-09-05 21:47:34 +0000656 }
657 i++;
cristyed231572011-07-14 02:18:59 +0000658 q+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000659 }
660 status=SyncCacheViewAuthenticPixels(phase_view,exception);
661 if (status == MagickFalse)
662 break;
663 }
664 phase_view=DestroyCacheView(phase_view);
cristy7d4aa382013-06-30 01:59:39 +0000665 phase_info=RelinquishVirtualMemory(phase_info);
666 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000667 return(status);
668}
669
670static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +0000671 const Image *image,double *magnitude_pixels,double *phase_pixels,
672 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000673{
674 CacheView
675 *image_view;
676
cristy99dc0362013-09-15 18:32:54 +0000677 const char
678 *value;
679
cristy3ed852e2009-09-05 21:47:34 +0000680 double
cristybb3c02e2013-07-02 00:43:10 +0000681 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000682
683 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +0000684 *forward_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000685
686 fftw_plan
687 fftw_r2c_plan;
688
cristy7d4aa382013-06-30 01:59:39 +0000689 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000690 *forward_info,
cristy7d4aa382013-06-30 01:59:39 +0000691 *source_info;
692
cristy4c08aed2011-07-01 19:47:50 +0000693 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000694 *p;
695
cristybb503372010-05-27 20:51:26 +0000696 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000697 i,
698 x;
699
cristyc4ea4a42011-01-24 01:43:30 +0000700 ssize_t
701 y;
702
cristy3ed852e2009-09-05 21:47:34 +0000703 /*
704 Generate the forward Fourier transform.
705 */
cristy7d4aa382013-06-30 01:59:39 +0000706 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000707 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000708 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000709 {
710 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000711 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000712 return(MagickFalse);
713 }
cristybb3c02e2013-07-02 00:43:10 +0000714 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
715 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
716 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000717 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000718 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000719 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000720 {
721 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
722 exception);
cristy4c08aed2011-07-01 19:47:50 +0000723 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000724 break;
cristybb503372010-05-27 20:51:26 +0000725 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000726 {
727 switch (fourier_info->channel)
728 {
cristyd3090f92011-10-18 00:05:15 +0000729 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000730 default:
731 {
cristybb3c02e2013-07-02 00:43:10 +0000732 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000733 break;
734 }
cristyd3090f92011-10-18 00:05:15 +0000735 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000736 {
cristybb3c02e2013-07-02 00:43:10 +0000737 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000738 break;
739 }
cristyd3090f92011-10-18 00:05:15 +0000740 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000741 {
cristybb3c02e2013-07-02 00:43:10 +0000742 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000743 break;
744 }
cristyd3090f92011-10-18 00:05:15 +0000745 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000746 {
cristybb3c02e2013-07-02 00:43:10 +0000747 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000748 break;
749 }
cristyd3090f92011-10-18 00:05:15 +0000750 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000751 {
cristybb3c02e2013-07-02 00:43:10 +0000752 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000753 break;
754 }
cristy3ed852e2009-09-05 21:47:34 +0000755 }
756 i++;
cristyed231572011-07-14 02:18:59 +0000757 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000758 }
759 }
760 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +0000761 forward_info=AcquireVirtualMemory((size_t) fourier_info->height,
762 fourier_info->center*sizeof(*forward_pixels));
763 if (forward_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000764 {
765 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000766 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000767 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000768 return(MagickFalse);
769 }
cristy699ae5b2013-07-03 13:41:29 +0000770 forward_pixels=(fftw_complex *) GetVirtualMemoryBlob(forward_info);
cristyb5d5f722009-11-04 03:03:49 +0000771#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000772 #pragma omp critical (MagickCore_ForwardFourierTransform)
773#endif
cristy70841a12012-10-27 20:52:57 +0000774 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +0000775 source_pixels,forward_pixels,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000776 fftw_execute(fftw_r2c_plan);
777 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000778 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy99dc0362013-09-15 18:32:54 +0000779 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +0000780 if ((value == (const char *) NULL) || (LocaleCompare(value,"forward") == 0))
cristy56ed31c2010-03-22 00:46:21 +0000781 {
cristy99dc0362013-09-15 18:32:54 +0000782 double
783 gamma;
784
785 /*
786 Normalize Fourier transform.
787 */
788 i=0L;
789 gamma=PerceptibleReciprocal((double) fourier_info->width*
790 fourier_info->height);
791 for (y=0L; y < (ssize_t) fourier_info->height; y++)
792 for (x=0L; x < (ssize_t) fourier_info->center; x++)
793 {
cristy56ed31c2010-03-22 00:46:21 +0000794#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy99dc0362013-09-15 18:32:54 +0000795 forward_pixels[i]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000796#else
cristy99dc0362013-09-15 18:32:54 +0000797 forward_pixels[i][0]*=gamma;
798 forward_pixels[i][1]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000799#endif
cristy99dc0362013-09-15 18:32:54 +0000800 i++;
801 }
cristy56ed31c2010-03-22 00:46:21 +0000802 }
cristy3ed852e2009-09-05 21:47:34 +0000803 /*
804 Generate magnitude and phase (or real and imaginary).
805 */
806 i=0L;
807 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000808 for (y=0L; y < (ssize_t) fourier_info->height; y++)
809 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000810 {
cristy699ae5b2013-07-03 13:41:29 +0000811 magnitude_pixels[i]=cabs(forward_pixels[i]);
812 phase_pixels[i]=carg(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000813 i++;
814 }
815 else
cristybb503372010-05-27 20:51:26 +0000816 for (y=0L; y < (ssize_t) fourier_info->height; y++)
817 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000818 {
cristy699ae5b2013-07-03 13:41:29 +0000819 magnitude_pixels[i]=creal(forward_pixels[i]);
820 phase_pixels[i]=cimag(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000821 i++;
822 }
cristy699ae5b2013-07-03 13:41:29 +0000823 forward_info=(MemoryInfo *) RelinquishVirtualMemory(forward_info);
cristy3ed852e2009-09-05 21:47:34 +0000824 return(MagickTrue);
825}
826
827static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000828 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000829 Image *fourier_image,ExceptionInfo *exception)
830{
831 double
cristyce9fe782013-07-03 00:59:41 +0000832 *magnitude_pixels,
833 *phase_pixels;
cristybb3c02e2013-07-02 00:43:10 +0000834
cristy56ed31c2010-03-22 00:46:21 +0000835 FourierInfo
836 fourier_info;
837
cristyc4ea4a42011-01-24 01:43:30 +0000838 MagickBooleanType
839 status;
840
cristyce9fe782013-07-03 00:59:41 +0000841 MemoryInfo
842 *magnitude_info,
843 *phase_info;
844
cristy3ed852e2009-09-05 21:47:34 +0000845 size_t
846 extent;
847
848 fourier_info.width=image->columns;
849 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
850 ((image->rows % 2) != 0))
851 {
852 extent=image->columns < image->rows ? image->rows : image->columns;
853 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
854 }
855 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000856 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000857 fourier_info.channel=channel;
858 fourier_info.modulus=modulus;
cristyce9fe782013-07-03 00:59:41 +0000859 magnitude_info=AcquireVirtualMemory((size_t) fourier_info.height,
860 fourier_info.center*sizeof(*magnitude_pixels));
861 phase_info=AcquireVirtualMemory((size_t) fourier_info.height,
862 fourier_info.center*sizeof(*phase_pixels));
863 if ((magnitude_info == (MemoryInfo *) NULL) ||
864 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000865 {
cristyce9fe782013-07-03 00:59:41 +0000866 if (phase_info != (MemoryInfo *) NULL)
867 phase_info=RelinquishVirtualMemory(phase_info);
868 if (magnitude_info == (MemoryInfo *) NULL)
869 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000870 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000871 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000872 return(MagickFalse);
873 }
cristyce9fe782013-07-03 00:59:41 +0000874 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
875 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
876 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
877 phase_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000878 if (status != MagickFalse)
cristyce9fe782013-07-03 00:59:41 +0000879 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
880 phase_pixels,exception);
881 phase_info=RelinquishVirtualMemory(phase_info);
882 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000883 return(status);
884}
885#endif
886
887MagickExport Image *ForwardFourierTransformImage(const Image *image,
888 const MagickBooleanType modulus,ExceptionInfo *exception)
889{
890 Image
891 *fourier_image;
892
893 fourier_image=NewImageList();
894#if !defined(MAGICKCORE_FFTW_DELEGATE)
895 (void) modulus;
896 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000897 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000898 image->filename);
899#else
900 {
901 Image
902 *magnitude_image;
903
cristybb503372010-05-27 20:51:26 +0000904 size_t
cristy3ed852e2009-09-05 21:47:34 +0000905 extent,
906 width;
907
908 width=image->columns;
909 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
910 ((image->rows % 2) != 0))
911 {
912 extent=image->columns < image->rows ? image->rows : image->columns;
913 width=(extent & 0x01) == 1 ? extent+1UL : extent;
914 }
cristy4c9c4d02013-10-08 00:05:57 +0000915 magnitude_image=CloneImage(image,width,width,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;
cristy4c9c4d02013-10-08 00:05:57 +0000923 phase_image=CloneImage(image,width,width,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}