blob: ab17cf6a387594f2b852b2bac2cc82aaf8256cb6 [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]);
cristy857c8a12013-10-08 23:19:33 +0000283 Ci[i]=gamma*(Ai[i]*Br[i]-Ar[i]*Bi[i]);
cristy9f654472013-10-05 19:44:06 +0000284 break;
285 }
cristyf46941c2013-10-06 22:25:41 +0000286 case MagnitudePhaseComplexOperator:
287 {
cristy8526a972013-10-08 00:52:54 +0000288 Cr[i]=sqrt(Ar[i]*Ar[i]+Ai[i]*Ai[i]);
289 Ci[i]=atan2(Ai[i],Ar[i]);
cristyf46941c2013-10-06 22:25:41 +0000290 break;
291 }
cristy9f654472013-10-05 19:44:06 +0000292 case MultiplyComplexOperator:
293 {
cristy15b07b32013-10-08 12:10:31 +0000294 Cr[i]=QuantumScale*(Ar[i]*Br[i]-Ai[i]*Bi[i]);
295 Ci[i]=QuantumScale*(Ai[i]*Br[i]+Ar[i]*Bi[i]);
cristyf46941c2013-10-06 22:25:41 +0000296 break;
297 }
298 case RealImaginaryComplexOperator:
299 {
cristyda36ce22013-10-08 17:37:50 +0000300 Cr[i]=Ar[i]*cos(Ai[i]);
301 Ci[i]=Ar[i]*sin(Ai[i]);
cristy9f654472013-10-05 19:44:06 +0000302 break;
303 }
cristy19f78862013-10-05 22:21:46 +0000304 case SubtractComplexOperator:
305 {
306 Cr[i]=Ar[i]-Br[i];
307 Ci[i]=Ai[i]-Bi[i];
308 break;
309 }
cristy9f654472013-10-05 19:44:06 +0000310 }
cristy19f78862013-10-05 22:21:46 +0000311 Ar+=GetPixelChannels(Ar_image);
312 Ai+=GetPixelChannels(Ai_image);
313 Br+=GetPixelChannels(Br_image);
314 Bi+=GetPixelChannels(Bi_image);
315 Cr+=GetPixelChannels(Cr_image);
316 Ci+=GetPixelChannels(Ci_image);
cristy9f654472013-10-05 19:44:06 +0000317 }
cristy1042ed22013-10-05 17:38:54 +0000318 }
319 if (SyncCacheViewAuthenticPixels(Ci_view,exception) == MagickFalse)
320 status=MagickFalse;
321 if (SyncCacheViewAuthenticPixels(Cr_view,exception) == MagickFalse)
322 status=MagickFalse;
323 if (images->progress_monitor != (MagickProgressMonitor) NULL)
324 {
325 MagickBooleanType
326 proceed;
327
328#if defined(MAGICKCORE_OPENMP_SUPPORT)
329 #pragma omp critical (MagickCore_ComplexImages)
330#endif
331 proceed=SetImageProgress(images,ComplexImageTag,progress++,
332 images->rows);
333 if (proceed == MagickFalse)
334 status=MagickFalse;
335 }
336 }
337 Cr_view=DestroyCacheView(Cr_view);
338 Ci_view=DestroyCacheView(Ci_view);
339 Br_view=DestroyCacheView(Br_view);
340 Bi_view=DestroyCacheView(Bi_view);
341 Ar_view=DestroyCacheView(Ar_view);
342 Ai_view=DestroyCacheView(Ai_view);
343 if (status == MagickFalse)
344 complex_images=DestroyImageList(complex_images);
345 return(complex_images);
cristy790190d2013-10-04 00:51:51 +0000346}
347
348/*
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350% %
351% %
352% %
cristy3ed852e2009-09-05 21:47:34 +0000353% F o r w a r d F o u r i e r T r a n s f o r m I m a g e %
354% %
355% %
356% %
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358%
359% ForwardFourierTransformImage() implements the discrete Fourier transform
360% (DFT) of the image either as a magnitude / phase or real / imaginary image
361% pair.
362%
363% The format of the ForwadFourierTransformImage method is:
364%
365% Image *ForwardFourierTransformImage(const Image *image,
366% const MagickBooleanType modulus,ExceptionInfo *exception)
367%
368% A description of each parameter follows:
369%
370% o image: the image.
371%
372% o modulus: if true, return as transform as a magnitude / phase pair
373% otherwise a real / imaginary image pair.
374%
375% o exception: return any errors or warnings in this structure.
376%
377*/
378
379#if defined(MAGICKCORE_FFTW_DELEGATE)
380
cristyc4ea4a42011-01-24 01:43:30 +0000381static MagickBooleanType RollFourier(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000382 const ssize_t x_offset,const ssize_t y_offset,double *roll_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384 double
cristy699ae5b2013-07-03 13:41:29 +0000385 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000386
cristy7d4aa382013-06-30 01:59:39 +0000387 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000388 *source_info;
cristy7d4aa382013-06-30 01:59:39 +0000389
cristyc4ea4a42011-01-24 01:43:30 +0000390 register ssize_t
391 i,
392 x;
393
cristybb503372010-05-27 20:51:26 +0000394 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000395 u,
396 v,
397 y;
398
cristy3ed852e2009-09-05 21:47:34 +0000399 /*
cristy2da05352010-09-15 19:22:17 +0000400 Move zero frequency (DC, average color) from (0,0) to (width/2,height/2).
cristy3ed852e2009-09-05 21:47:34 +0000401 */
cristy699ae5b2013-07-03 13:41:29 +0000402 source_info=AcquireVirtualMemory(height,width*sizeof(*source_pixels));
403 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000404 return(MagickFalse);
cristy699ae5b2013-07-03 13:41:29 +0000405 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000406 i=0L;
cristybb503372010-05-27 20:51:26 +0000407 for (y=0L; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000408 {
409 if (y_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000410 v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset;
cristy3ed852e2009-09-05 21:47:34 +0000411 else
cristybb503372010-05-27 20:51:26 +0000412 v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height :
cristy3ed852e2009-09-05 21:47:34 +0000413 y+y_offset;
cristybb503372010-05-27 20:51:26 +0000414 for (x=0L; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000415 {
416 if (x_offset < 0L)
cristybb503372010-05-27 20:51:26 +0000417 u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset;
cristy3ed852e2009-09-05 21:47:34 +0000418 else
cristybb503372010-05-27 20:51:26 +0000419 u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width :
cristy3ed852e2009-09-05 21:47:34 +0000420 x+x_offset;
cristy699ae5b2013-07-03 13:41:29 +0000421 source_pixels[v*width+u]=roll_pixels[i++];
cristyc4ea4a42011-01-24 01:43:30 +0000422 }
cristy3ed852e2009-09-05 21:47:34 +0000423 }
cristy699ae5b2013-07-03 13:41:29 +0000424 (void) CopyMagickMemory(roll_pixels,source_pixels,height*width*
425 sizeof(*source_pixels));
426 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000427 return(MagickTrue);
428}
429
cristybb503372010-05-27 20:51:26 +0000430static MagickBooleanType ForwardQuadrantSwap(const size_t width,
cristy699ae5b2013-07-03 13:41:29 +0000431 const size_t height,double *source_pixels,double *forward_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000432{
cristy3ed852e2009-09-05 21:47:34 +0000433 MagickBooleanType
434 status;
435
cristybb503372010-05-27 20:51:26 +0000436 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000437 x;
438
cristyc4ea4a42011-01-24 01:43:30 +0000439 ssize_t
440 center,
441 y;
442
cristy3ed852e2009-09-05 21:47:34 +0000443 /*
444 Swap quadrants.
445 */
cristybb503372010-05-27 20:51:26 +0000446 center=(ssize_t) floor((double) width/2L)+1L;
cristy699ae5b2013-07-03 13:41:29 +0000447 status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,
448 source_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000449 if (status == MagickFalse)
450 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000451 for (y=0L; y < (ssize_t) height; y++)
452 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy3d04ed72013-08-18 17:51:01 +0000453 forward_pixels[y*width+x+width/2L]=source_pixels[y*center+x];
cristybb503372010-05-27 20:51:26 +0000454 for (y=1; y < (ssize_t) height; y++)
455 for (x=0L; x < (ssize_t) (width/2L-1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +0000456 forward_pixels[(height-y)*width+width/2L-x-1L]=
cristy3d04ed72013-08-18 17:51:01 +0000457 source_pixels[y*center+x+1L];
cristybb503372010-05-27 20:51:26 +0000458 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000459 forward_pixels[-x+width/2L-1L]=forward_pixels[x+width/2L+1L];
cristy3ed852e2009-09-05 21:47:34 +0000460 return(MagickTrue);
461}
462
cristyc4ea4a42011-01-24 01:43:30 +0000463static void CorrectPhaseLHS(const size_t width,const size_t height,
cristy699ae5b2013-07-03 13:41:29 +0000464 double *fourier_pixels)
cristy3ed852e2009-09-05 21:47:34 +0000465{
cristybb503372010-05-27 20:51:26 +0000466 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000467 x;
468
cristy9d314ff2011-03-09 01:30:28 +0000469 ssize_t
470 y;
471
cristybb503372010-05-27 20:51:26 +0000472 for (y=0L; y < (ssize_t) height; y++)
473 for (x=0L; x < (ssize_t) (width/2L); x++)
cristy699ae5b2013-07-03 13:41:29 +0000474 fourier_pixels[y*width+x]*=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +0000475}
476
477static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info,
478 Image *image,double *magnitude,double *phase,ExceptionInfo *exception)
479{
480 CacheView
481 *magnitude_view,
482 *phase_view;
483
484 double
cristy7d4aa382013-06-30 01:59:39 +0000485 *magnitude_pixels,
486 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000487
488 Image
489 *magnitude_image,
490 *phase_image;
491
cristy3ed852e2009-09-05 21:47:34 +0000492 MagickBooleanType
493 status;
494
cristy7d4aa382013-06-30 01:59:39 +0000495 MemoryInfo
496 *magnitude_info,
497 *phase_info;
498
cristy4c08aed2011-07-01 19:47:50 +0000499 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000500 *q;
501
cristyf5742792012-11-27 18:31:26 +0000502 register ssize_t
503 x;
504
cristyc4ea4a42011-01-24 01:43:30 +0000505 ssize_t
506 i,
507 y;
508
cristy3ed852e2009-09-05 21:47:34 +0000509 magnitude_image=GetFirstImageInList(image);
510 phase_image=GetNextImageInList(image);
511 if (phase_image == (Image *) NULL)
512 {
513 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +0000514 "TwoOrMoreImagesRequired","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000515 return(MagickFalse);
516 }
517 /*
518 Create "Fourier Transform" image from constituent arrays.
519 */
cristy7d4aa382013-06-30 01:59:39 +0000520 magnitude_info=AcquireVirtualMemory((size_t) fourier_info->height,
521 fourier_info->width*sizeof(*magnitude_pixels));
522 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
523 fourier_info->width*sizeof(*phase_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000524 if ((magnitude_info == (MemoryInfo *) NULL) ||
525 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000526 {
cristy7d4aa382013-06-30 01:59:39 +0000527 if (phase_info != (MemoryInfo *) NULL)
528 phase_info=RelinquishVirtualMemory(phase_info);
529 if (magnitude_info != (MemoryInfo *) NULL)
530 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000531 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000532 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000533 return(MagickFalse);
534 }
cristy7d4aa382013-06-30 01:59:39 +0000535 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
536 (void) ResetMagickMemory(magnitude_pixels,0,fourier_info->height*
537 fourier_info->width*sizeof(*magnitude_pixels));
cristybb3c02e2013-07-02 00:43:10 +0000538 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
cristy7d4aa382013-06-30 01:59:39 +0000539 (void) ResetMagickMemory(phase_pixels,0,fourier_info->height*
540 fourier_info->width*sizeof(*phase_pixels));
cristy13c99c42013-08-18 13:53:30 +0000541 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,
cristy7d4aa382013-06-30 01:59:39 +0000542 magnitude,magnitude_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000543 if (status != MagickFalse)
cristy13c99c42013-08-18 13:53:30 +0000544 status=ForwardQuadrantSwap(fourier_info->width,fourier_info->height,phase,
cristy7d4aa382013-06-30 01:59:39 +0000545 phase_pixels);
cristy13c99c42013-08-18 13:53:30 +0000546 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000547 if (fourier_info->modulus != MagickFalse)
548 {
549 i=0L;
cristybb503372010-05-27 20:51:26 +0000550 for (y=0L; y < (ssize_t) fourier_info->height; y++)
551 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000552 {
cristy7d4aa382013-06-30 01:59:39 +0000553 phase_pixels[i]/=(2.0*MagickPI);
554 phase_pixels[i]+=0.5;
cristy3ed852e2009-09-05 21:47:34 +0000555 i++;
556 }
557 }
cristy46ff2672012-12-14 15:32:26 +0000558 magnitude_view=AcquireAuthenticCacheView(magnitude_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000559 i=0L;
cristybb503372010-05-27 20:51:26 +0000560 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000561 {
562 q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL,
563 exception);
cristyacd2ed22011-08-30 01:44:23 +0000564 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000565 break;
cristybb503372010-05-27 20:51:26 +0000566 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000567 {
568 switch (fourier_info->channel)
569 {
cristyd3090f92011-10-18 00:05:15 +0000570 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000571 default:
572 {
cristy4c08aed2011-07-01 19:47:50 +0000573 SetPixelRed(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000574 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000575 break;
576 }
cristyd3090f92011-10-18 00:05:15 +0000577 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000578 {
cristy4c08aed2011-07-01 19:47:50 +0000579 SetPixelGreen(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000580 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000581 break;
582 }
cristyd3090f92011-10-18 00:05:15 +0000583 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000584 {
cristy4c08aed2011-07-01 19:47:50 +0000585 SetPixelBlue(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000586 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000587 break;
588 }
cristyd3090f92011-10-18 00:05:15 +0000589 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000590 {
cristy4c08aed2011-07-01 19:47:50 +0000591 SetPixelBlack(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000592 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000593 break;
594 }
cristyd3090f92011-10-18 00:05:15 +0000595 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000596 {
cristy4c08aed2011-07-01 19:47:50 +0000597 SetPixelAlpha(magnitude_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000598 magnitude_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000599 break;
600 }
cristy3ed852e2009-09-05 21:47:34 +0000601 }
602 i++;
cristyed231572011-07-14 02:18:59 +0000603 q+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +0000604 }
605 status=SyncCacheViewAuthenticPixels(magnitude_view,exception);
606 if (status == MagickFalse)
607 break;
608 }
cristydb070952012-04-20 14:33:00 +0000609 magnitude_view=DestroyCacheView(magnitude_view);
cristy3ed852e2009-09-05 21:47:34 +0000610 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000611 phase_view=AcquireAuthenticCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +0000612 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000613 {
614 q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL,
615 exception);
cristyacd2ed22011-08-30 01:44:23 +0000616 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000617 break;
cristybb503372010-05-27 20:51:26 +0000618 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000619 {
620 switch (fourier_info->channel)
621 {
cristyd3090f92011-10-18 00:05:15 +0000622 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000623 default:
624 {
cristy4c08aed2011-07-01 19:47:50 +0000625 SetPixelRed(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000626 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
cristyd3090f92011-10-18 00:05:15 +0000629 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000630 {
cristy4c08aed2011-07-01 19:47:50 +0000631 SetPixelGreen(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000632 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000633 break;
634 }
cristyd3090f92011-10-18 00:05:15 +0000635 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000636 {
cristy4c08aed2011-07-01 19:47:50 +0000637 SetPixelBlue(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000638 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000639 break;
640 }
cristyd3090f92011-10-18 00:05:15 +0000641 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000642 {
cristy4c08aed2011-07-01 19:47:50 +0000643 SetPixelBlack(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000644 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000645 break;
646 }
cristyd3090f92011-10-18 00:05:15 +0000647 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristy4c08aed2011-07-01 19:47:50 +0000649 SetPixelAlpha(phase_image,ClampToQuantum(QuantumRange*
cristy7d4aa382013-06-30 01:59:39 +0000650 phase_pixels[i]),q);
cristy3ed852e2009-09-05 21:47:34 +0000651 break;
652 }
cristy3ed852e2009-09-05 21:47:34 +0000653 }
654 i++;
cristyed231572011-07-14 02:18:59 +0000655 q+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +0000656 }
657 status=SyncCacheViewAuthenticPixels(phase_view,exception);
658 if (status == MagickFalse)
659 break;
660 }
661 phase_view=DestroyCacheView(phase_view);
cristy7d4aa382013-06-30 01:59:39 +0000662 phase_info=RelinquishVirtualMemory(phase_info);
663 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000664 return(status);
665}
666
667static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +0000668 const Image *image,double *magnitude_pixels,double *phase_pixels,
669 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000670{
671 CacheView
672 *image_view;
673
cristy99dc0362013-09-15 18:32:54 +0000674 const char
675 *value;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 double
cristybb3c02e2013-07-02 00:43:10 +0000678 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000679
680 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +0000681 *forward_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000682
683 fftw_plan
684 fftw_r2c_plan;
685
cristy7d4aa382013-06-30 01:59:39 +0000686 MemoryInfo
cristy699ae5b2013-07-03 13:41:29 +0000687 *forward_info,
cristy7d4aa382013-06-30 01:59:39 +0000688 *source_info;
689
cristy4c08aed2011-07-01 19:47:50 +0000690 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000691 *p;
692
cristybb503372010-05-27 20:51:26 +0000693 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000694 i,
695 x;
696
cristyc4ea4a42011-01-24 01:43:30 +0000697 ssize_t
698 y;
699
cristy3ed852e2009-09-05 21:47:34 +0000700 /*
701 Generate the forward Fourier transform.
702 */
cristy7d4aa382013-06-30 01:59:39 +0000703 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +0000704 fourier_info->width*sizeof(*source_pixels));
cristy7d4aa382013-06-30 01:59:39 +0000705 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000706 {
707 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000708 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000709 return(MagickFalse);
710 }
cristybb3c02e2013-07-02 00:43:10 +0000711 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
712 ResetMagickMemory(source_pixels,0,fourier_info->height*fourier_info->width*
713 sizeof(*source_pixels));
cristy3ed852e2009-09-05 21:47:34 +0000714 i=0L;
cristy46ff2672012-12-14 15:32:26 +0000715 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000716 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000717 {
718 p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL,
719 exception);
cristy4c08aed2011-07-01 19:47:50 +0000720 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000721 break;
cristybb503372010-05-27 20:51:26 +0000722 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000723 {
724 switch (fourier_info->channel)
725 {
cristyd3090f92011-10-18 00:05:15 +0000726 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000727 default:
728 {
cristybb3c02e2013-07-02 00:43:10 +0000729 source_pixels[i]=QuantumScale*GetPixelRed(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000730 break;
731 }
cristyd3090f92011-10-18 00:05:15 +0000732 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000733 {
cristybb3c02e2013-07-02 00:43:10 +0000734 source_pixels[i]=QuantumScale*GetPixelGreen(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000735 break;
736 }
cristyd3090f92011-10-18 00:05:15 +0000737 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000738 {
cristybb3c02e2013-07-02 00:43:10 +0000739 source_pixels[i]=QuantumScale*GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000740 break;
741 }
cristyd3090f92011-10-18 00:05:15 +0000742 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000743 {
cristybb3c02e2013-07-02 00:43:10 +0000744 source_pixels[i]=QuantumScale*GetPixelBlack(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000745 break;
746 }
cristyd3090f92011-10-18 00:05:15 +0000747 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +0000748 {
cristybb3c02e2013-07-02 00:43:10 +0000749 source_pixels[i]=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000750 break;
751 }
cristy3ed852e2009-09-05 21:47:34 +0000752 }
753 i++;
cristyed231572011-07-14 02:18:59 +0000754 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000755 }
756 }
757 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +0000758 forward_info=AcquireVirtualMemory((size_t) fourier_info->height,
759 fourier_info->center*sizeof(*forward_pixels));
760 if (forward_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000761 {
762 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000763 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristybb3c02e2013-07-02 00:43:10 +0000764 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +0000765 return(MagickFalse);
766 }
cristy699ae5b2013-07-03 13:41:29 +0000767 forward_pixels=(fftw_complex *) GetVirtualMemoryBlob(forward_info);
cristyb5d5f722009-11-04 03:03:49 +0000768#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000769 #pragma omp critical (MagickCore_ForwardFourierTransform)
770#endif
cristy70841a12012-10-27 20:52:57 +0000771 fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +0000772 source_pixels,forward_pixels,FFTW_ESTIMATE);
cristy3ed852e2009-09-05 21:47:34 +0000773 fftw_execute(fftw_r2c_plan);
774 fftw_destroy_plan(fftw_r2c_plan);
cristybb3c02e2013-07-02 00:43:10 +0000775 source_info=(MemoryInfo *) RelinquishVirtualMemory(source_info);
cristy99dc0362013-09-15 18:32:54 +0000776 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +0000777 if ((value == (const char *) NULL) || (LocaleCompare(value,"forward") == 0))
cristy56ed31c2010-03-22 00:46:21 +0000778 {
cristy99dc0362013-09-15 18:32:54 +0000779 double
780 gamma;
781
782 /*
783 Normalize Fourier transform.
784 */
785 i=0L;
786 gamma=PerceptibleReciprocal((double) fourier_info->width*
787 fourier_info->height);
788 for (y=0L; y < (ssize_t) fourier_info->height; y++)
789 for (x=0L; x < (ssize_t) fourier_info->center; x++)
790 {
cristy56ed31c2010-03-22 00:46:21 +0000791#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy99dc0362013-09-15 18:32:54 +0000792 forward_pixels[i]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000793#else
cristy99dc0362013-09-15 18:32:54 +0000794 forward_pixels[i][0]*=gamma;
795 forward_pixels[i][1]*=gamma;
cristy56ed31c2010-03-22 00:46:21 +0000796#endif
cristy99dc0362013-09-15 18:32:54 +0000797 i++;
798 }
cristy56ed31c2010-03-22 00:46:21 +0000799 }
cristy3ed852e2009-09-05 21:47:34 +0000800 /*
801 Generate magnitude and phase (or real and imaginary).
802 */
803 i=0L;
804 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000805 for (y=0L; y < (ssize_t) fourier_info->height; y++)
806 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000807 {
cristy699ae5b2013-07-03 13:41:29 +0000808 magnitude_pixels[i]=cabs(forward_pixels[i]);
809 phase_pixels[i]=carg(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000810 i++;
811 }
812 else
cristybb503372010-05-27 20:51:26 +0000813 for (y=0L; y < (ssize_t) fourier_info->height; y++)
814 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +0000815 {
cristy699ae5b2013-07-03 13:41:29 +0000816 magnitude_pixels[i]=creal(forward_pixels[i]);
817 phase_pixels[i]=cimag(forward_pixels[i]);
cristy3ed852e2009-09-05 21:47:34 +0000818 i++;
819 }
cristy699ae5b2013-07-03 13:41:29 +0000820 forward_info=(MemoryInfo *) RelinquishVirtualMemory(forward_info);
cristy3ed852e2009-09-05 21:47:34 +0000821 return(MagickTrue);
822}
823
824static MagickBooleanType ForwardFourierTransformChannel(const Image *image,
cristyd3090f92011-10-18 00:05:15 +0000825 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +0000826 Image *fourier_image,ExceptionInfo *exception)
827{
828 double
cristyce9fe782013-07-03 00:59:41 +0000829 *magnitude_pixels,
830 *phase_pixels;
cristybb3c02e2013-07-02 00:43:10 +0000831
cristy56ed31c2010-03-22 00:46:21 +0000832 FourierInfo
833 fourier_info;
834
cristyc4ea4a42011-01-24 01:43:30 +0000835 MagickBooleanType
836 status;
837
cristyce9fe782013-07-03 00:59:41 +0000838 MemoryInfo
839 *magnitude_info,
840 *phase_info;
841
cristy3ed852e2009-09-05 21:47:34 +0000842 size_t
843 extent;
844
845 fourier_info.width=image->columns;
cristy740b4f12013-10-08 19:51:07 +0000846 fourier_info.height=image->rows;
cristy3ed852e2009-09-05 21:47:34 +0000847 if ((image->columns != image->rows) || ((image->columns % 2) != 0) ||
848 ((image->rows % 2) != 0))
849 {
850 extent=image->columns < image->rows ? image->rows : image->columns;
851 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
852 }
853 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +0000854 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +0000855 fourier_info.channel=channel;
856 fourier_info.modulus=modulus;
cristyce9fe782013-07-03 00:59:41 +0000857 magnitude_info=AcquireVirtualMemory((size_t) fourier_info.height,
858 fourier_info.center*sizeof(*magnitude_pixels));
859 phase_info=AcquireVirtualMemory((size_t) fourier_info.height,
860 fourier_info.center*sizeof(*phase_pixels));
861 if ((magnitude_info == (MemoryInfo *) NULL) ||
862 (phase_info == (MemoryInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000863 {
cristyce9fe782013-07-03 00:59:41 +0000864 if (phase_info != (MemoryInfo *) NULL)
865 phase_info=RelinquishVirtualMemory(phase_info);
866 if (magnitude_info == (MemoryInfo *) NULL)
867 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000868 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000869 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000870 return(MagickFalse);
871 }
cristyce9fe782013-07-03 00:59:41 +0000872 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
873 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
874 status=ForwardFourierTransform(&fourier_info,image,magnitude_pixels,
875 phase_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000876 if (status != MagickFalse)
cristyce9fe782013-07-03 00:59:41 +0000877 status=ForwardFourier(&fourier_info,fourier_image,magnitude_pixels,
878 phase_pixels,exception);
879 phase_info=RelinquishVirtualMemory(phase_info);
880 magnitude_info=RelinquishVirtualMemory(magnitude_info);
cristy3ed852e2009-09-05 21:47:34 +0000881 return(status);
882}
883#endif
884
885MagickExport Image *ForwardFourierTransformImage(const Image *image,
886 const MagickBooleanType modulus,ExceptionInfo *exception)
887{
888 Image
889 *fourier_image;
890
891 fourier_image=NewImageList();
892#if !defined(MAGICKCORE_FFTW_DELEGATE)
893 (void) modulus;
894 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000895 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristy3ed852e2009-09-05 21:47:34 +0000896 image->filename);
897#else
898 {
899 Image
900 *magnitude_image;
901
cristybb503372010-05-27 20:51:26 +0000902 size_t
cristy3ed852e2009-09-05 21:47:34 +0000903 extent,
cristyc9721ff2013-10-08 19:40:01 +0000904 height,
cristy3ed852e2009-09-05 21:47:34 +0000905 width;
906
907 width=image->columns;
cristyc9721ff2013-10-08 19:40:01 +0000908 height=image->rows;
cristy3ed852e2009-09-05 21:47:34 +0000909 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 }
cristyc9721ff2013-10-08 19:40:01 +0000915 height=width;
916 magnitude_image=CloneImage(image,width,height,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000917 if (magnitude_image != (Image *) NULL)
918 {
919 Image
920 *phase_image;
921
922 magnitude_image->storage_class=DirectClass;
923 magnitude_image->depth=32UL;
cristyc9721ff2013-10-08 19:40:01 +0000924 phase_image=CloneImage(image,width,height,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000925 if (phase_image == (Image *) NULL)
926 magnitude_image=DestroyImage(magnitude_image);
927 else
928 {
929 MagickBooleanType
930 is_gray,
931 status;
932
cristy3ed852e2009-09-05 21:47:34 +0000933 phase_image->storage_class=DirectClass;
934 phase_image->depth=32UL;
935 AppendImageToList(&fourier_image,magnitude_image);
936 AppendImageToList(&fourier_image,phase_image);
937 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000938 is_gray=IsImageGray(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000939#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy564a5692012-01-20 23:56:26 +0000940 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +0000941#endif
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristyb34ef052010-10-07 00:12:05 +0000943#if defined(MAGICKCORE_OPENMP_SUPPORT)
944 #pragma omp section
945#endif
cristy3ed852e2009-09-05 21:47:34 +0000946 {
cristyb34ef052010-10-07 00:12:05 +0000947 MagickBooleanType
948 thread_status;
949
950 if (is_gray != MagickFalse)
951 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000952 GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000953 else
954 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000955 RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000956 if (thread_status == MagickFalse)
957 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +0000958 }
cristyb34ef052010-10-07 00:12:05 +0000959#if defined(MAGICKCORE_OPENMP_SUPPORT)
960 #pragma omp section
961#endif
962 {
963 MagickBooleanType
964 thread_status;
965
966 thread_status=MagickTrue;
967 if (is_gray == MagickFalse)
968 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000969 GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000970 if (thread_status == MagickFalse)
971 status=thread_status;
972 }
973#if defined(MAGICKCORE_OPENMP_SUPPORT)
974 #pragma omp section
975#endif
976 {
977 MagickBooleanType
978 thread_status;
979
980 thread_status=MagickTrue;
981 if (is_gray == MagickFalse)
982 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000983 BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000984 if (thread_status == MagickFalse)
985 status=thread_status;
986 }
987#if defined(MAGICKCORE_OPENMP_SUPPORT)
988 #pragma omp section
989#endif
990 {
991 MagickBooleanType
992 thread_status;
993
994 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000995 if (image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +0000996 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +0000997 BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +0000998 if (thread_status == MagickFalse)
999 status=thread_status;
1000 }
1001#if defined(MAGICKCORE_OPENMP_SUPPORT)
1002 #pragma omp section
1003#endif
1004 {
1005 MagickBooleanType
1006 thread_status;
1007
1008 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001009 if (image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001010 thread_status=ForwardFourierTransformChannel(image,
cristyd3090f92011-10-18 00:05:15 +00001011 AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001012 if (thread_status == MagickFalse)
1013 status=thread_status;
1014 }
cristy3ed852e2009-09-05 21:47:34 +00001015 }
1016 if (status == MagickFalse)
1017 fourier_image=DestroyImageList(fourier_image);
1018 fftw_cleanup();
1019 }
1020 }
1021 }
1022#endif
1023 return(fourier_image);
1024}
1025
1026/*
1027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028% %
1029% %
1030% %
1031% 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 %
1032% %
1033% %
1034% %
1035%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036%
1037% InverseFourierTransformImage() implements the inverse discrete Fourier
1038% transform (DFT) of the image either as a magnitude / phase or real /
1039% imaginary image pair.
1040%
1041% The format of the InverseFourierTransformImage method is:
1042%
cristyc9550792009-11-13 20:05:42 +00001043% Image *InverseFourierTransformImage(const Image *magnitude_image,
1044% const Image *phase_image,const MagickBooleanType modulus,
1045% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001046%
1047% A description of each parameter follows:
1048%
cristyc9550792009-11-13 20:05:42 +00001049% o magnitude_image: the magnitude or real image.
1050%
1051% o phase_image: the phase or imaginary image.
cristy3ed852e2009-09-05 21:47:34 +00001052%
1053% o modulus: if true, return transform as a magnitude / phase pair
1054% otherwise a real / imaginary image pair.
1055%
1056% o exception: return any errors or warnings in this structure.
1057%
1058*/
1059
1060#if defined(MAGICKCORE_FFTW_DELEGATE)
cristybb503372010-05-27 20:51:26 +00001061static MagickBooleanType InverseQuadrantSwap(const size_t width,
1062 const size_t height,const double *source,double *destination)
cristy3ed852e2009-09-05 21:47:34 +00001063{
cristyc4ea4a42011-01-24 01:43:30 +00001064 register ssize_t
1065 x;
1066
cristybb503372010-05-27 20:51:26 +00001067 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001068 center,
1069 y;
1070
cristy3ed852e2009-09-05 21:47:34 +00001071 /*
1072 Swap quadrants.
1073 */
cristy233fe582012-07-07 14:00:18 +00001074 center=(ssize_t) floor((double) width/2L)+1L;
cristybb503372010-05-27 20:51:26 +00001075 for (y=1L; y < (ssize_t) height; y++)
1076 for (x=0L; x < (ssize_t) (width/2L+1L); x++)
cristy9b1d6c72013-08-18 17:53:56 +00001077 destination[(height-y)*center-x+width/2L]=source[y*width+x];
cristybb503372010-05-27 20:51:26 +00001078 for (y=0L; y < (ssize_t) height; y++)
cristy3d04ed72013-08-18 17:51:01 +00001079 destination[y*center]=source[y*width+width/2L];
cristy3ed852e2009-09-05 21:47:34 +00001080 for (x=0L; x < center; x++)
1081 destination[x]=source[center-x-1L];
cristybb503372010-05-27 20:51:26 +00001082 return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination));
cristy3ed852e2009-09-05 21:47:34 +00001083}
1084
1085static MagickBooleanType InverseFourier(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001086 const Image *magnitude_image,const Image *phase_image,
1087 fftw_complex *fourier_pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001088{
1089 CacheView
1090 *magnitude_view,
1091 *phase_view;
1092
1093 double
cristy699ae5b2013-07-03 13:41:29 +00001094 *inverse_pixels,
cristy7d4aa382013-06-30 01:59:39 +00001095 *magnitude_pixels,
1096 *phase_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001097
cristy3ed852e2009-09-05 21:47:34 +00001098 MagickBooleanType
1099 status;
1100
cristy699ae5b2013-07-03 13:41:29 +00001101 MemoryInfo
1102 *inverse_info,
1103 *magnitude_info,
1104 *phase_info;
1105
cristy4c08aed2011-07-01 19:47:50 +00001106 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001107 *p;
1108
cristybb503372010-05-27 20:51:26 +00001109 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001110 i,
1111 x;
1112
cristyc4ea4a42011-01-24 01:43:30 +00001113 ssize_t
1114 y;
1115
cristy3ed852e2009-09-05 21:47:34 +00001116 /*
1117 Inverse fourier - read image and break down into a double array.
1118 */
cristy699ae5b2013-07-03 13:41:29 +00001119 magnitude_info=AcquireVirtualMemory((size_t)fourier_info->height,
1120 fourier_info->width*sizeof(*magnitude_pixels));
1121 phase_info=AcquireVirtualMemory((size_t) fourier_info->height,
cristybb3c02e2013-07-02 00:43:10 +00001122 fourier_info->width*sizeof(*phase_pixels));
cristy699ae5b2013-07-03 13:41:29 +00001123 inverse_info=AcquireVirtualMemory((size_t) fourier_info->height,
1124 fourier_info->center*sizeof(*inverse_pixels));
1125 if ((magnitude_info == (MemoryInfo *) NULL) ||
1126 (phase_info == (MemoryInfo *) NULL) ||
1127 (inverse_info == (MemoryInfo *) NULL))
cristybb3c02e2013-07-02 00:43:10 +00001128 {
cristy699ae5b2013-07-03 13:41:29 +00001129 if (magnitude_info != (MemoryInfo *) NULL)
1130 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1131 if (phase_info != (MemoryInfo *) NULL)
1132 phase_info=RelinquishVirtualMemory(phase_info);
1133 if (inverse_info != (MemoryInfo *) NULL)
1134 inverse_info=RelinquishVirtualMemory(inverse_info);
cristybb3c02e2013-07-02 00:43:10 +00001135 (void) ThrowMagickException(exception,GetMagickModule(),
1136 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1137 magnitude_image->filename);
cristybb3c02e2013-07-02 00:43:10 +00001138 return(MagickFalse);
1139 }
cristy699ae5b2013-07-03 13:41:29 +00001140 magnitude_pixels=(double *) GetVirtualMemoryBlob(magnitude_info);
1141 phase_pixels=(double *) GetVirtualMemoryBlob(phase_info);
1142 inverse_pixels=(double *) GetVirtualMemoryBlob(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001143 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001144 magnitude_view=AcquireVirtualCacheView(magnitude_image,exception);
cristybb503372010-05-27 20:51:26 +00001145 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001146 {
1147 p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL,
1148 exception);
cristy4c08aed2011-07-01 19:47:50 +00001149 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001150 break;
cristybb503372010-05-27 20:51:26 +00001151 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 switch (fourier_info->channel)
1154 {
cristyd3090f92011-10-18 00:05:15 +00001155 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001156 default:
1157 {
cristy7d4aa382013-06-30 01:59:39 +00001158 magnitude_pixels[i]=QuantumScale*GetPixelRed(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001159 break;
1160 }
cristyd3090f92011-10-18 00:05:15 +00001161 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001162 {
cristy7d4aa382013-06-30 01:59:39 +00001163 magnitude_pixels[i]=QuantumScale*GetPixelGreen(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001164 break;
1165 }
cristyd3090f92011-10-18 00:05:15 +00001166 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001167 {
cristy7d4aa382013-06-30 01:59:39 +00001168 magnitude_pixels[i]=QuantumScale*GetPixelBlue(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001169 break;
1170 }
cristyd3090f92011-10-18 00:05:15 +00001171 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001172 {
cristy7d4aa382013-06-30 01:59:39 +00001173 magnitude_pixels[i]=QuantumScale*GetPixelBlack(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001174 break;
1175 }
cristyd3090f92011-10-18 00:05:15 +00001176 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001177 {
cristy7d4aa382013-06-30 01:59:39 +00001178 magnitude_pixels[i]=QuantumScale*GetPixelAlpha(magnitude_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001179 break;
1180 }
cristy3ed852e2009-09-05 21:47:34 +00001181 }
1182 i++;
cristyed231572011-07-14 02:18:59 +00001183 p+=GetPixelChannels(magnitude_image);
cristy3ed852e2009-09-05 21:47:34 +00001184 }
1185 }
cristy699ae5b2013-07-03 13:41:29 +00001186 magnitude_view=DestroyCacheView(magnitude_view);
1187 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
1188 magnitude_pixels,inverse_pixels);
1189 (void) CopyMagickMemory(magnitude_pixels,inverse_pixels,fourier_info->height*
1190 fourier_info->center*sizeof(*magnitude_pixels));
cristy3ed852e2009-09-05 21:47:34 +00001191 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001192 phase_view=AcquireVirtualCacheView(phase_image,exception);
cristybb503372010-05-27 20:51:26 +00001193 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001194 {
1195 p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1,
1196 exception);
cristy4c08aed2011-07-01 19:47:50 +00001197 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001198 break;
cristybb503372010-05-27 20:51:26 +00001199 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001200 {
1201 switch (fourier_info->channel)
1202 {
cristyd3090f92011-10-18 00:05:15 +00001203 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001204 default:
1205 {
cristy7d4aa382013-06-30 01:59:39 +00001206 phase_pixels[i]=QuantumScale*GetPixelRed(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001207 break;
1208 }
cristyd3090f92011-10-18 00:05:15 +00001209 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001210 {
cristy7d4aa382013-06-30 01:59:39 +00001211 phase_pixels[i]=QuantumScale*GetPixelGreen(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001212 break;
1213 }
cristyd3090f92011-10-18 00:05:15 +00001214 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001215 {
cristy7d4aa382013-06-30 01:59:39 +00001216 phase_pixels[i]=QuantumScale*GetPixelBlue(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001217 break;
1218 }
cristyd3090f92011-10-18 00:05:15 +00001219 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001220 {
cristy7d4aa382013-06-30 01:59:39 +00001221 phase_pixels[i]=QuantumScale*GetPixelBlack(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001222 break;
1223 }
cristyd3090f92011-10-18 00:05:15 +00001224 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001225 {
cristy7d4aa382013-06-30 01:59:39 +00001226 phase_pixels[i]=QuantumScale*GetPixelAlpha(phase_image,p);
cristy3ed852e2009-09-05 21:47:34 +00001227 break;
1228 }
cristy3ed852e2009-09-05 21:47:34 +00001229 }
1230 i++;
cristyed231572011-07-14 02:18:59 +00001231 p+=GetPixelChannels(phase_image);
cristy3ed852e2009-09-05 21:47:34 +00001232 }
1233 }
1234 if (fourier_info->modulus != MagickFalse)
1235 {
1236 i=0L;
cristybb503372010-05-27 20:51:26 +00001237 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1238 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001239 {
cristy7d4aa382013-06-30 01:59:39 +00001240 phase_pixels[i]-=0.5;
1241 phase_pixels[i]*=(2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00001242 i++;
1243 }
1244 }
cristy3ed852e2009-09-05 21:47:34 +00001245 phase_view=DestroyCacheView(phase_view);
cristy13c99c42013-08-18 13:53:30 +00001246 CorrectPhaseLHS(fourier_info->width,fourier_info->height,phase_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001247 if (status != MagickFalse)
1248 status=InverseQuadrantSwap(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001249 phase_pixels,inverse_pixels);
1250 (void) CopyMagickMemory(phase_pixels,inverse_pixels,fourier_info->height*
1251 fourier_info->center*sizeof(*phase_pixels));
1252 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001253 /*
1254 Merge two sets.
1255 */
1256 i=0L;
1257 if (fourier_info->modulus != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001258 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1259 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001260 {
cristy56ed31c2010-03-22 00:46:21 +00001261#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001262 fourier_pixels[i]=magnitude_pixels[i]*cos(phase_pixels[i])+I*
1263 magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001264#else
cristy699ae5b2013-07-03 13:41:29 +00001265 fourier_pixels[i][0]=magnitude_pixels[i]*cos(phase_pixels[i]);
1266 fourier_pixels[i][1]=magnitude_pixels[i]*sin(phase_pixels[i]);
cristy56ed31c2010-03-22 00:46:21 +00001267#endif
cristy3ed852e2009-09-05 21:47:34 +00001268 i++;
1269 }
1270 else
cristybb503372010-05-27 20:51:26 +00001271 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1272 for (x=0L; x < (ssize_t) fourier_info->center; x++)
cristy3ed852e2009-09-05 21:47:34 +00001273 {
cristy56ed31c2010-03-22 00:46:21 +00001274#if defined(MAGICKCORE_HAVE_COMPLEX_H)
cristy699ae5b2013-07-03 13:41:29 +00001275 fourier_pixels[i]=magnitude_pixels[i]+I*phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001276#else
cristy699ae5b2013-07-03 13:41:29 +00001277 fourier_pixels[i][0]=magnitude_pixels[i];
1278 fourier_pixels[i][1]=phase_pixels[i];
cristy56ed31c2010-03-22 00:46:21 +00001279#endif
cristy3ed852e2009-09-05 21:47:34 +00001280 i++;
1281 }
cristy699ae5b2013-07-03 13:41:29 +00001282 magnitude_info=RelinquishVirtualMemory(magnitude_info);
1283 phase_info=RelinquishVirtualMemory(phase_info);
cristy3ed852e2009-09-05 21:47:34 +00001284 return(status);
1285}
1286
1287static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info,
cristy699ae5b2013-07-03 13:41:29 +00001288 fftw_complex *fourier_pixels,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001289{
1290 CacheView
1291 *image_view;
1292
cristy99dc0362013-09-15 18:32:54 +00001293 const char
1294 *value;
1295
cristy3ed852e2009-09-05 21:47:34 +00001296 double
cristy699ae5b2013-07-03 13:41:29 +00001297 *source_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001298
1299 fftw_plan
1300 fftw_c2r_plan;
1301
cristy699ae5b2013-07-03 13:41:29 +00001302 MemoryInfo
1303 *source_info;
1304
cristy4c08aed2011-07-01 19:47:50 +00001305 register Quantum
cristyc4ea4a42011-01-24 01:43:30 +00001306 *q;
1307
cristybb503372010-05-27 20:51:26 +00001308 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001309 i,
1310 x;
1311
cristyc4ea4a42011-01-24 01:43:30 +00001312 ssize_t
1313 y;
cristy3ed852e2009-09-05 21:47:34 +00001314
cristy699ae5b2013-07-03 13:41:29 +00001315 source_info=AcquireVirtualMemory((size_t) fourier_info->height,
1316 fourier_info->width*sizeof(*source_pixels));
1317 if (source_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001318 {
1319 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001320 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001321 return(MagickFalse);
1322 }
cristy699ae5b2013-07-03 13:41:29 +00001323 source_pixels=(double *) GetVirtualMemoryBlob(source_info);
cristy99dc0362013-09-15 18:32:54 +00001324 value=GetImageArtifact(image,"fourier:normalize");
cristyf8ecbfd2013-09-15 19:11:14 +00001325 if (LocaleCompare(value,"inverse") == 0)
cristy99dc0362013-09-15 18:32:54 +00001326 {
1327 double
1328 gamma;
1329
1330 /*
1331 Normalize inverse transform.
1332 */
1333 i=0L;
1334 gamma=PerceptibleReciprocal((double) fourier_info->width*
1335 fourier_info->height);
1336 for (y=0L; y < (ssize_t) fourier_info->height; y++)
1337 for (x=0L; x < (ssize_t) fourier_info->center; x++)
1338 {
1339#if defined(MAGICKCORE_HAVE_COMPLEX_H)
1340 fourier_pixels[i]*=gamma;
1341#else
1342 fourier_pixels[i][0]*=gamma;
1343 fourier_pixels[i][1]*=gamma;
1344#endif
1345 i++;
1346 }
1347 }
cristyb5d5f722009-11-04 03:03:49 +00001348#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001349 #pragma omp critical (MagickCore_InverseFourierTransform)
1350#endif
cristydf079ac2010-09-10 01:45:44 +00001351 {
1352 fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height,
cristy699ae5b2013-07-03 13:41:29 +00001353 fourier_pixels,source_pixels,FFTW_ESTIMATE);
cristydf079ac2010-09-10 01:45:44 +00001354 fftw_execute(fftw_c2r_plan);
1355 fftw_destroy_plan(fftw_c2r_plan);
1356 }
cristy3ed852e2009-09-05 21:47:34 +00001357 i=0L;
cristy46ff2672012-12-14 15:32:26 +00001358 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001359 for (y=0L; y < (ssize_t) fourier_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001360 {
cristy85812052010-09-14 17:56:15 +00001361 if (y >= (ssize_t) image->rows)
1362 break;
1363 q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width >
1364 image->columns ? image->columns : fourier_info->width,1UL,exception);
cristyacd2ed22011-08-30 01:44:23 +00001365 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001366 break;
cristybb503372010-05-27 20:51:26 +00001367 for (x=0L; x < (ssize_t) fourier_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001368 {
cristy233fe582012-07-07 14:00:18 +00001369 if (x < (ssize_t) image->columns)
1370 switch (fourier_info->channel)
cristy3ed852e2009-09-05 21:47:34 +00001371 {
cristy233fe582012-07-07 14:00:18 +00001372 case RedPixelChannel:
1373 default:
1374 {
cristy699ae5b2013-07-03 13:41:29 +00001375 SetPixelRed(image,ClampToQuantum(QuantumRange*source_pixels[i]),q);
cristy233fe582012-07-07 14:00:18 +00001376 break;
1377 }
1378 case GreenPixelChannel:
1379 {
cristy699ae5b2013-07-03 13:41:29 +00001380 SetPixelGreen(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1381 q);
cristy233fe582012-07-07 14:00:18 +00001382 break;
1383 }
1384 case BluePixelChannel:
1385 {
cristy699ae5b2013-07-03 13:41:29 +00001386 SetPixelBlue(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1387 q);
cristy233fe582012-07-07 14:00:18 +00001388 break;
1389 }
1390 case BlackPixelChannel:
1391 {
cristy699ae5b2013-07-03 13:41:29 +00001392 SetPixelBlack(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1393 q);
cristy233fe582012-07-07 14:00:18 +00001394 break;
1395 }
1396 case AlphaPixelChannel:
1397 {
cristy699ae5b2013-07-03 13:41:29 +00001398 SetPixelAlpha(image,ClampToQuantum(QuantumRange*source_pixels[i]),
1399 q);
cristy233fe582012-07-07 14:00:18 +00001400 break;
1401 }
cristy3ed852e2009-09-05 21:47:34 +00001402 }
cristy3ed852e2009-09-05 21:47:34 +00001403 i++;
cristyed231572011-07-14 02:18:59 +00001404 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001405 }
1406 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1407 break;
1408 }
1409 image_view=DestroyCacheView(image_view);
cristy699ae5b2013-07-03 13:41:29 +00001410 source_info=RelinquishVirtualMemory(source_info);
cristy3ed852e2009-09-05 21:47:34 +00001411 return(MagickTrue);
1412}
1413
cristyc9550792009-11-13 20:05:42 +00001414static MagickBooleanType InverseFourierTransformChannel(
1415 const Image *magnitude_image,const Image *phase_image,
cristyd3090f92011-10-18 00:05:15 +00001416 const PixelChannel channel,const MagickBooleanType modulus,
cristy3ed852e2009-09-05 21:47:34 +00001417 Image *fourier_image,ExceptionInfo *exception)
1418{
cristy3ed852e2009-09-05 21:47:34 +00001419 fftw_complex
cristy699ae5b2013-07-03 13:41:29 +00001420 *inverse_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001421
1422 FourierInfo
1423 fourier_info;
1424
1425 MagickBooleanType
1426 status;
1427
cristy699ae5b2013-07-03 13:41:29 +00001428 MemoryInfo
1429 *inverse_info;
1430
cristy3ed852e2009-09-05 21:47:34 +00001431 size_t
1432 extent;
1433
cristyc9550792009-11-13 20:05:42 +00001434 fourier_info.width=magnitude_image->columns;
cristy0a69e642013-10-08 19:51:44 +00001435 fourier_info.height=magnitude_image->rows;
cristyc9550792009-11-13 20:05:42 +00001436 if ((magnitude_image->columns != magnitude_image->rows) ||
1437 ((magnitude_image->columns % 2) != 0) ||
1438 ((magnitude_image->rows % 2) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001439 {
cristyc9550792009-11-13 20:05:42 +00001440 extent=magnitude_image->columns < magnitude_image->rows ?
1441 magnitude_image->rows : magnitude_image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001442 fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent;
1443 }
1444 fourier_info.height=fourier_info.width;
cristy233fe582012-07-07 14:00:18 +00001445 fourier_info.center=(ssize_t) floor((double) fourier_info.width/2L)+1L;
cristy3ed852e2009-09-05 21:47:34 +00001446 fourier_info.channel=channel;
1447 fourier_info.modulus=modulus;
cristy699ae5b2013-07-03 13:41:29 +00001448 inverse_info=AcquireVirtualMemory((size_t) fourier_info.height,
1449 fourier_info.center*sizeof(*inverse_pixels));
1450 if (inverse_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001451 {
1452 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001453 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristyc9550792009-11-13 20:05:42 +00001454 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001455 return(MagickFalse);
1456 }
cristy699ae5b2013-07-03 13:41:29 +00001457 inverse_pixels=(fftw_complex *) GetVirtualMemoryBlob(inverse_info);
1458 status=InverseFourier(&fourier_info,magnitude_image,phase_image,
1459 inverse_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001460 if (status != MagickFalse)
cristy699ae5b2013-07-03 13:41:29 +00001461 status=InverseFourierTransform(&fourier_info,inverse_pixels,fourier_image,
cristy3ed852e2009-09-05 21:47:34 +00001462 exception);
cristy699ae5b2013-07-03 13:41:29 +00001463 inverse_info=RelinquishVirtualMemory(inverse_info);
cristy3ed852e2009-09-05 21:47:34 +00001464 return(status);
1465}
1466#endif
1467
cristyc9550792009-11-13 20:05:42 +00001468MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image,
1469 const Image *phase_image,const MagickBooleanType modulus,
1470 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001471{
1472 Image
1473 *fourier_image;
1474
cristyc9550792009-11-13 20:05:42 +00001475 assert(magnitude_image != (Image *) NULL);
1476 assert(magnitude_image->signature == MagickSignature);
1477 if (magnitude_image->debug != MagickFalse)
1478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1479 magnitude_image->filename);
1480 if (phase_image == (Image *) NULL)
1481 {
1482 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
cristyefe601c2013-01-05 17:51:12 +00001483 "TwoOrMoreImagesRequired","`%s'",magnitude_image->filename);
cristy9372a152009-11-18 01:42:16 +00001484 return((Image *) NULL);
cristyc9550792009-11-13 20:05:42 +00001485 }
cristy3ed852e2009-09-05 21:47:34 +00001486#if !defined(MAGICKCORE_FFTW_DELEGATE)
1487 fourier_image=(Image *) NULL;
1488 (void) modulus;
1489 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001490 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (FFTW)",
cristyc9550792009-11-13 20:05:42 +00001491 magnitude_image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001492#else
1493 {
cristyc9550792009-11-13 20:05:42 +00001494 fourier_image=CloneImage(magnitude_image,magnitude_image->columns,
cristy4c9c4d02013-10-08 00:05:57 +00001495 magnitude_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001496 if (fourier_image != (Image *) NULL)
1497 {
1498 MagickBooleanType
1499 is_gray,
1500 status;
1501
cristy3ed852e2009-09-05 21:47:34 +00001502 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001503 is_gray=IsImageGray(magnitude_image,exception);
cristyc9550792009-11-13 20:05:42 +00001504 if (is_gray != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001505 is_gray=IsImageGray(phase_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001506#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb34ef052010-10-07 00:12:05 +00001507 #pragma omp parallel sections
cristy3ed852e2009-09-05 21:47:34 +00001508#endif
cristy3ed852e2009-09-05 21:47:34 +00001509 {
cristyb34ef052010-10-07 00:12:05 +00001510#if defined(MAGICKCORE_OPENMP_SUPPORT)
1511 #pragma omp section
1512#endif
cristy3ed852e2009-09-05 21:47:34 +00001513 {
cristyb34ef052010-10-07 00:12:05 +00001514 MagickBooleanType
1515 thread_status;
1516
1517 if (is_gray != MagickFalse)
1518 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001519 phase_image,GrayPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001520 else
cristyc9550792009-11-13 20:05:42 +00001521 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001522 phase_image,RedPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001523 if (thread_status == MagickFalse)
1524 status=thread_status;
cristy3ed852e2009-09-05 21:47:34 +00001525 }
cristyb34ef052010-10-07 00:12:05 +00001526#if defined(MAGICKCORE_OPENMP_SUPPORT)
1527 #pragma omp section
1528#endif
1529 {
1530 MagickBooleanType
1531 thread_status;
1532
1533 thread_status=MagickTrue;
1534 if (is_gray == MagickFalse)
1535 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001536 phase_image,GreenPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001537 if (thread_status == MagickFalse)
1538 status=thread_status;
1539 }
1540#if defined(MAGICKCORE_OPENMP_SUPPORT)
1541 #pragma omp section
1542#endif
1543 {
1544 MagickBooleanType
1545 thread_status;
1546
1547 thread_status=MagickTrue;
1548 if (is_gray == MagickFalse)
1549 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001550 phase_image,BluePixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001551 if (thread_status == MagickFalse)
1552 status=thread_status;
1553 }
1554#if defined(MAGICKCORE_OPENMP_SUPPORT)
1555 #pragma omp section
1556#endif
1557 {
1558 MagickBooleanType
1559 thread_status;
1560
1561 thread_status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00001562 if (magnitude_image->colorspace == CMYKColorspace)
cristyb34ef052010-10-07 00:12:05 +00001563 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001564 phase_image,BlackPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001565 if (thread_status == MagickFalse)
1566 status=thread_status;
1567 }
1568#if defined(MAGICKCORE_OPENMP_SUPPORT)
1569 #pragma omp section
1570#endif
1571 {
1572 MagickBooleanType
1573 thread_status;
1574
1575 thread_status=MagickTrue;
cristy8a46d822012-08-28 23:32:39 +00001576 if (magnitude_image->alpha_trait == BlendPixelTrait)
cristyb34ef052010-10-07 00:12:05 +00001577 thread_status=InverseFourierTransformChannel(magnitude_image,
cristyd3090f92011-10-18 00:05:15 +00001578 phase_image,AlphaPixelChannel,modulus,fourier_image,exception);
cristyb34ef052010-10-07 00:12:05 +00001579 if (thread_status == MagickFalse)
1580 status=thread_status;
1581 }
cristy3ed852e2009-09-05 21:47:34 +00001582 }
1583 if (status == MagickFalse)
1584 fourier_image=DestroyImage(fourier_image);
1585 }
cristyb34ef052010-10-07 00:12:05 +00001586 fftw_cleanup();
cristy3ed852e2009-09-05 21:47:34 +00001587 }
1588#endif
1589 return(fourier_image);
1590}