cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 22 | % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 23 | % 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 | */ |
| 45 | #include "magick/studio.h" |
| 46 | #include "magick/cache.h" |
| 47 | #include "magick/image.h" |
| 48 | #include "magick/image-private.h" |
| 49 | #include "magick/list.h" |
| 50 | #include "magick/fourier.h" |
| 51 | #include "magick/log.h" |
| 52 | #include "magick/memory_.h" |
| 53 | #include "magick/monitor.h" |
| 54 | #include "magick/property.h" |
cristy | e303898 | 2010-05-17 02:20:52 +0000 | [diff] [blame] | 55 | #include "magick/quantum-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 56 | #include "magick/thread-private.h" |
| 57 | #if defined(MAGICKCORE_FFTW_DELEGATE) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 58 | #if defined(MAGICKCORE_HAVE_COMPLEX_H) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 59 | #include <complex.h> |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 60 | #else |
| 61 | #define cabs(z) (sqrt(z[0]*z[0]+z[1]*z[1])) |
| 62 | #define carg(z) (atan2(z[1],z[0])) |
| 63 | #define creal(z) (z[0]) |
| 64 | #define cimag(z) (z[1]) |
| 65 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | #include <fftw3.h> |
| 67 | #endif |
| 68 | |
| 69 | /* |
| 70 | Typedef declarations. |
| 71 | */ |
| 72 | typedef struct _FourierInfo |
| 73 | { |
| 74 | ChannelType |
| 75 | channel; |
| 76 | |
| 77 | MagickBooleanType |
| 78 | modulus; |
| 79 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 80 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | width, |
| 82 | height; |
| 83 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 84 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 85 | center; |
| 86 | } FourierInfo; |
| 87 | |
| 88 | /* |
| 89 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | % 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 % |
| 94 | % % |
| 95 | % % |
| 96 | % % |
| 97 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 98 | % |
| 99 | % ForwardFourierTransformImage() implements the discrete Fourier transform |
| 100 | % (DFT) of the image either as a magnitude / phase or real / imaginary image |
| 101 | % pair. |
| 102 | % |
| 103 | % The format of the ForwadFourierTransformImage method is: |
| 104 | % |
| 105 | % Image *ForwardFourierTransformImage(const Image *image, |
| 106 | % const MagickBooleanType modulus,ExceptionInfo *exception) |
| 107 | % |
| 108 | % A description of each parameter follows: |
| 109 | % |
| 110 | % o image: the image. |
| 111 | % |
| 112 | % o modulus: if true, return as transform as a magnitude / phase pair |
| 113 | % otherwise a real / imaginary image pair. |
| 114 | % |
| 115 | % o exception: return any errors or warnings in this structure. |
| 116 | % |
| 117 | */ |
| 118 | |
| 119 | #if defined(MAGICKCORE_FFTW_DELEGATE) |
| 120 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 121 | static MagickBooleanType RollFourier(const size_t width, |
| 122 | const size_t height,const ssize_t x_offset,const ssize_t y_offset, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 123 | double *fourier) |
| 124 | { |
| 125 | double |
| 126 | *roll; |
| 127 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 128 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | u, |
| 130 | v, |
| 131 | y; |
| 132 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 133 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 134 | i, |
| 135 | x; |
| 136 | |
| 137 | /* |
cristy | 2da0535 | 2010-09-15 19:22:17 +0000 | [diff] [blame] | 138 | Move zero frequency (DC, average color) from (0,0) to (width/2,height/2). |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | */ |
| 140 | roll=(double *) AcquireQuantumMemory((size_t) width,height*sizeof(*roll)); |
| 141 | if (roll == (double *) NULL) |
| 142 | return(MagickFalse); |
| 143 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 144 | for (y=0L; y < (ssize_t) height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | { |
| 146 | if (y_offset < 0L) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 147 | v=((y+y_offset) < 0L) ? y+y_offset+(ssize_t) height : y+y_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 148 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 149 | v=((y+y_offset) > ((ssize_t) height-1L)) ? y+y_offset-(ssize_t) height : |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | y+y_offset; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 151 | for (x=0L; x < (ssize_t) width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 152 | { |
| 153 | if (x_offset < 0L) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 154 | u=((x+x_offset) < 0L) ? x+x_offset+(ssize_t) width : x+x_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 156 | u=((x+x_offset) > ((ssize_t) width-1L)) ? x+x_offset-(ssize_t) width : |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 157 | x+x_offset; |
| 158 | roll[v*width+u]=fourier[i++]; |
| 159 | } |
| 160 | } |
| 161 | (void) CopyMagickMemory(fourier,roll,width*height*sizeof(*roll)); |
| 162 | roll=(double *) RelinquishMagickMemory(roll); |
| 163 | return(MagickTrue); |
| 164 | } |
| 165 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 166 | static MagickBooleanType ForwardQuadrantSwap(const size_t width, |
| 167 | const size_t height,double *source,double *destination) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 168 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 169 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 170 | center, |
| 171 | y; |
| 172 | |
| 173 | MagickBooleanType |
| 174 | status; |
| 175 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 176 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | x; |
| 178 | |
| 179 | /* |
| 180 | Swap quadrants. |
| 181 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 182 | center=(ssize_t) floor((double) width/2L)+1L; |
| 183 | status=RollFourier((size_t) center,height,0L,(ssize_t) height/2L,source); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | if (status == MagickFalse) |
| 185 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 186 | for (y=0L; y < (ssize_t) height; y++) |
| 187 | for (x=0L; x < (ssize_t) (width/2L-1L); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | destination[width*y+x+width/2L]=source[center*y+x]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 189 | for (y=1; y < (ssize_t) height; y++) |
| 190 | for (x=0L; x < (ssize_t) (width/2L-1L); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 191 | destination[width*(height-y)+width/2L-x-1L]=source[center*y+x+1L]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 192 | for (x=0L; x < (ssize_t) (width/2L); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 193 | destination[-x+width/2L-1L]=destination[x+width/2L+1L]; |
| 194 | return(MagickTrue); |
| 195 | } |
| 196 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 197 | static void CorrectPhaseLHS(const size_t width, |
| 198 | const size_t height,double *fourier) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 200 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | y; |
| 202 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 203 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 204 | x; |
| 205 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 206 | for (y=0L; y < (ssize_t) height; y++) |
| 207 | for (x=0L; x < (ssize_t) (width/2L); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | fourier[y*width+x]*=(-1.0); |
| 209 | } |
| 210 | |
| 211 | static MagickBooleanType ForwardFourier(const FourierInfo *fourier_info, |
| 212 | Image *image,double *magnitude,double *phase,ExceptionInfo *exception) |
| 213 | { |
| 214 | CacheView |
| 215 | *magnitude_view, |
| 216 | *phase_view; |
| 217 | |
| 218 | double |
| 219 | *magnitude_source, |
| 220 | *phase_source; |
| 221 | |
| 222 | Image |
| 223 | *magnitude_image, |
| 224 | *phase_image; |
| 225 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 226 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | i, |
| 228 | y; |
| 229 | |
| 230 | MagickBooleanType |
| 231 | status; |
| 232 | |
| 233 | register IndexPacket |
| 234 | *indexes; |
| 235 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 236 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | x; |
| 238 | |
| 239 | register PixelPacket |
| 240 | *q; |
| 241 | |
| 242 | magnitude_image=GetFirstImageInList(image); |
| 243 | phase_image=GetNextImageInList(image); |
| 244 | if (phase_image == (Image *) NULL) |
| 245 | { |
| 246 | (void) ThrowMagickException(exception,GetMagickModule(),ImageError, |
| 247 | "ImageSequenceRequired","`%s'",image->filename); |
| 248 | return(MagickFalse); |
| 249 | } |
| 250 | /* |
| 251 | Create "Fourier Transform" image from constituent arrays. |
| 252 | */ |
| 253 | magnitude_source=(double *) AcquireQuantumMemory((size_t) |
| 254 | fourier_info->height,fourier_info->width*sizeof(*magnitude_source)); |
| 255 | if (magnitude_source == (double *) NULL) |
| 256 | return(MagickFalse); |
| 257 | (void) ResetMagickMemory(magnitude_source,0,fourier_info->width* |
| 258 | fourier_info->height*sizeof(*magnitude_source)); |
| 259 | phase_source=(double *) AcquireQuantumMemory((size_t) fourier_info->height, |
| 260 | fourier_info->width*sizeof(*phase_source)); |
| 261 | if (magnitude_source == (double *) NULL) |
| 262 | { |
| 263 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 264 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 265 | magnitude_source=(double *) RelinquishMagickMemory(magnitude_source); |
| 266 | return(MagickFalse); |
| 267 | } |
| 268 | status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height, |
| 269 | magnitude,magnitude_source); |
| 270 | if (status != MagickFalse) |
| 271 | status=ForwardQuadrantSwap(fourier_info->height,fourier_info->height,phase, |
| 272 | phase_source); |
| 273 | CorrectPhaseLHS(fourier_info->height,fourier_info->height,phase_source); |
| 274 | if (fourier_info->modulus != MagickFalse) |
| 275 | { |
| 276 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 277 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 278 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 279 | { |
| 280 | phase_source[i]/=(2.0*MagickPI); |
| 281 | phase_source[i]+=0.5; |
| 282 | i++; |
| 283 | } |
| 284 | } |
| 285 | magnitude_view=AcquireCacheView(magnitude_image); |
| 286 | phase_view=AcquireCacheView(phase_image); |
| 287 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 288 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 289 | { |
| 290 | q=GetCacheViewAuthenticPixels(magnitude_view,0L,y,fourier_info->height,1UL, |
| 291 | exception); |
| 292 | if (q == (PixelPacket *) NULL) |
| 293 | break; |
| 294 | indexes=GetCacheViewAuthenticIndexQueue(magnitude_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 295 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 296 | { |
| 297 | switch (fourier_info->channel) |
| 298 | { |
| 299 | case RedChannel: |
| 300 | default: |
| 301 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 302 | q->red=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | case GreenChannel: |
| 306 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 307 | q->green=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | case BlueChannel: |
| 311 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 312 | q->blue=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | case OpacityChannel: |
| 316 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 317 | q->opacity=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | case IndexChannel: |
| 321 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 322 | indexes[x]=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | case GrayChannels: |
| 326 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 327 | q->red=ClampToQuantum(QuantumRange*magnitude_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | q->green=q->red; |
| 329 | q->blue=q->red; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | i++; |
| 334 | q++; |
| 335 | } |
| 336 | status=SyncCacheViewAuthenticPixels(magnitude_view,exception); |
| 337 | if (status == MagickFalse) |
| 338 | break; |
| 339 | } |
| 340 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 341 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 342 | { |
| 343 | q=GetCacheViewAuthenticPixels(phase_view,0L,y,fourier_info->height,1UL, |
| 344 | exception); |
| 345 | if (q == (PixelPacket *) NULL) |
| 346 | break; |
| 347 | indexes=GetCacheViewAuthenticIndexQueue(phase_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 348 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | { |
| 350 | switch (fourier_info->channel) |
| 351 | { |
| 352 | case RedChannel: |
| 353 | default: |
| 354 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 355 | q->red=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | case GreenChannel: |
| 359 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 360 | q->green=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | case BlueChannel: |
| 364 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 365 | q->blue=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | case OpacityChannel: |
| 369 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 370 | q->opacity=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | case IndexChannel: |
| 374 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 375 | indexes[x]=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | case GrayChannels: |
| 379 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 380 | q->red=ClampToQuantum(QuantumRange*phase_source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | q->green=q->red; |
| 382 | q->blue=q->red; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | i++; |
| 387 | q++; |
| 388 | } |
| 389 | status=SyncCacheViewAuthenticPixels(phase_view,exception); |
| 390 | if (status == MagickFalse) |
| 391 | break; |
| 392 | } |
| 393 | phase_view=DestroyCacheView(phase_view); |
| 394 | magnitude_view=DestroyCacheView(magnitude_view); |
| 395 | phase_source=(double *) RelinquishMagickMemory(phase_source); |
| 396 | magnitude_source=(double *) RelinquishMagickMemory(magnitude_source); |
| 397 | return(status); |
| 398 | } |
| 399 | |
| 400 | static MagickBooleanType ForwardFourierTransform(FourierInfo *fourier_info, |
| 401 | const Image *image,double *magnitude,double *phase,ExceptionInfo *exception) |
| 402 | { |
| 403 | CacheView |
| 404 | *image_view; |
| 405 | |
| 406 | double |
| 407 | n, |
| 408 | *source; |
| 409 | |
| 410 | fftw_complex |
| 411 | *fourier; |
| 412 | |
| 413 | fftw_plan |
| 414 | fftw_r2c_plan; |
| 415 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 416 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 417 | y; |
| 418 | |
| 419 | register const IndexPacket |
| 420 | *indexes; |
| 421 | |
| 422 | register const PixelPacket |
| 423 | *p; |
| 424 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 425 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 426 | i, |
| 427 | x; |
| 428 | |
| 429 | /* |
| 430 | Generate the forward Fourier transform. |
| 431 | */ |
| 432 | source=(double *) AcquireQuantumMemory((size_t) fourier_info->height, |
| 433 | fourier_info->width*sizeof(*source)); |
| 434 | if (source == (double *) NULL) |
| 435 | { |
| 436 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 437 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 438 | return(MagickFalse); |
| 439 | } |
| 440 | ResetMagickMemory(source,0,fourier_info->width*fourier_info->height* |
| 441 | sizeof(*source)); |
| 442 | i=0L; |
| 443 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 444 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 445 | { |
| 446 | p=GetCacheViewVirtualPixels(image_view,0L,y,fourier_info->width,1UL, |
| 447 | exception); |
| 448 | if (p == (const PixelPacket *) NULL) |
| 449 | break; |
| 450 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 451 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 452 | { |
| 453 | switch (fourier_info->channel) |
| 454 | { |
| 455 | case RedChannel: |
| 456 | default: |
| 457 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 458 | source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | case GreenChannel: |
| 462 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 463 | source[i]=QuantumScale*GetGreenPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | case BlueChannel: |
| 467 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 468 | source[i]=QuantumScale*GetBluePixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | case OpacityChannel: |
| 472 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 473 | source[i]=QuantumScale*GetOpacityPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | case IndexChannel: |
| 477 | { |
| 478 | source[i]=QuantumScale*indexes[x]; |
| 479 | break; |
| 480 | } |
| 481 | case GrayChannels: |
| 482 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 483 | source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 484 | break; |
| 485 | } |
| 486 | } |
| 487 | i++; |
| 488 | p++; |
| 489 | } |
| 490 | } |
| 491 | image_view=DestroyCacheView(image_view); |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 492 | fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info->height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 493 | fourier_info->center*sizeof(*fourier)); |
| 494 | if (fourier == (fftw_complex *) NULL) |
| 495 | { |
| 496 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 497 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 498 | source=(double *) RelinquishMagickMemory(source); |
| 499 | return(MagickFalse); |
| 500 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 501 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 502 | #pragma omp critical (MagickCore_ForwardFourierTransform) |
| 503 | #endif |
| 504 | fftw_r2c_plan=fftw_plan_dft_r2c_2d(fourier_info->width,fourier_info->width, |
| 505 | source,fourier,FFTW_ESTIMATE); |
| 506 | fftw_execute(fftw_r2c_plan); |
| 507 | fftw_destroy_plan(fftw_r2c_plan); |
| 508 | source=(double *) RelinquishMagickMemory(source); |
| 509 | /* |
| 510 | Normalize Fourier transform. |
| 511 | */ |
| 512 | n=(double) fourier_info->width*(double) fourier_info->width; |
| 513 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 514 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 515 | for (x=0L; x < (ssize_t) fourier_info->center; x++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 516 | { |
| 517 | #if defined(MAGICKCORE_HAVE_COMPLEX_H) |
| 518 | fourier[i]/=n; |
| 519 | #else |
| 520 | fourier[i][0]/=n; |
| 521 | fourier[i][1]/=n; |
| 522 | #endif |
| 523 | i++; |
| 524 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 525 | /* |
| 526 | Generate magnitude and phase (or real and imaginary). |
| 527 | */ |
| 528 | i=0L; |
| 529 | if (fourier_info->modulus != MagickFalse) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 530 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 531 | for (x=0L; x < (ssize_t) fourier_info->center; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 532 | { |
| 533 | magnitude[i]=cabs(fourier[i]); |
| 534 | phase[i]=carg(fourier[i]); |
| 535 | i++; |
| 536 | } |
| 537 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 538 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 539 | for (x=0L; x < (ssize_t) fourier_info->center; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | { |
| 541 | magnitude[i]=creal(fourier[i]); |
| 542 | phase[i]=cimag(fourier[i]); |
| 543 | i++; |
| 544 | } |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 545 | fourier=(fftw_complex *) RelinquishMagickMemory(fourier); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | return(MagickTrue); |
| 547 | } |
| 548 | |
| 549 | static MagickBooleanType ForwardFourierTransformChannel(const Image *image, |
| 550 | const ChannelType channel,const MagickBooleanType modulus, |
| 551 | Image *fourier_image,ExceptionInfo *exception) |
| 552 | { |
| 553 | double |
| 554 | *magnitude, |
| 555 | *phase; |
| 556 | |
| 557 | fftw_complex |
| 558 | *fourier; |
| 559 | |
cristy | 9f3502b | 2010-03-21 02:39:26 +0000 | [diff] [blame] | 560 | MagickBooleanType |
| 561 | status; |
| 562 | |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 563 | FourierInfo |
| 564 | fourier_info; |
| 565 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 566 | size_t |
| 567 | extent; |
| 568 | |
| 569 | fourier_info.width=image->columns; |
| 570 | if ((image->columns != image->rows) || ((image->columns % 2) != 0) || |
| 571 | ((image->rows % 2) != 0)) |
| 572 | { |
| 573 | extent=image->columns < image->rows ? image->rows : image->columns; |
| 574 | fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent; |
| 575 | } |
| 576 | fourier_info.height=fourier_info.width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 577 | fourier_info.center=(ssize_t) floor((double) fourier_info.width/2.0)+1L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | fourier_info.channel=channel; |
| 579 | fourier_info.modulus=modulus; |
| 580 | magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height, |
| 581 | fourier_info.center*sizeof(*magnitude)); |
| 582 | if (magnitude == (double *) NULL) |
| 583 | { |
| 584 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 585 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 586 | return(MagickFalse); |
| 587 | } |
| 588 | phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height, |
| 589 | fourier_info.center*sizeof(*phase)); |
| 590 | if (phase == (double *) NULL) |
| 591 | { |
| 592 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 593 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 594 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 595 | return(MagickFalse); |
| 596 | } |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 597 | fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 598 | fourier_info.center*sizeof(*fourier)); |
| 599 | if (fourier == (fftw_complex *) NULL) |
| 600 | { |
| 601 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 602 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 603 | phase=(double *) RelinquishMagickMemory(phase); |
| 604 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 605 | return(MagickFalse); |
| 606 | } |
| 607 | status=ForwardFourierTransform(&fourier_info,image,magnitude,phase,exception); |
| 608 | if (status != MagickFalse) |
| 609 | status=ForwardFourier(&fourier_info,fourier_image,magnitude,phase, |
| 610 | exception); |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 611 | fourier=(fftw_complex *) RelinquishMagickMemory(fourier); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | phase=(double *) RelinquishMagickMemory(phase); |
| 613 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 614 | return(status); |
| 615 | } |
| 616 | #endif |
| 617 | |
| 618 | MagickExport Image *ForwardFourierTransformImage(const Image *image, |
| 619 | const MagickBooleanType modulus,ExceptionInfo *exception) |
| 620 | { |
| 621 | Image |
| 622 | *fourier_image; |
| 623 | |
| 624 | fourier_image=NewImageList(); |
| 625 | #if !defined(MAGICKCORE_FFTW_DELEGATE) |
| 626 | (void) modulus; |
| 627 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 628 | MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)", |
| 629 | image->filename); |
| 630 | #else |
| 631 | { |
| 632 | Image |
| 633 | *magnitude_image; |
| 634 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 635 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | extent, |
| 637 | width; |
| 638 | |
| 639 | width=image->columns; |
| 640 | if ((image->columns != image->rows) || ((image->columns % 2) != 0) || |
| 641 | ((image->rows % 2) != 0)) |
| 642 | { |
| 643 | extent=image->columns < image->rows ? image->rows : image->columns; |
| 644 | width=(extent & 0x01) == 1 ? extent+1UL : extent; |
| 645 | } |
| 646 | magnitude_image=CloneImage(image,width,width,MagickFalse,exception); |
| 647 | if (magnitude_image != (Image *) NULL) |
| 648 | { |
| 649 | Image |
| 650 | *phase_image; |
| 651 | |
| 652 | magnitude_image->storage_class=DirectClass; |
| 653 | magnitude_image->depth=32UL; |
| 654 | phase_image=CloneImage(image,width,width,MagickFalse,exception); |
| 655 | if (phase_image == (Image *) NULL) |
| 656 | magnitude_image=DestroyImage(magnitude_image); |
| 657 | else |
| 658 | { |
| 659 | MagickBooleanType |
| 660 | is_gray, |
| 661 | status; |
| 662 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 663 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 664 | i; |
| 665 | |
| 666 | phase_image->storage_class=DirectClass; |
| 667 | phase_image->depth=32UL; |
| 668 | AppendImageToList(&fourier_image,magnitude_image); |
| 669 | AppendImageToList(&fourier_image,phase_image); |
| 670 | status=MagickTrue; |
| 671 | is_gray=IsGrayImage(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 672 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 673 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 674 | #endif |
| 675 | for (i=0L; i < 5L; i++) |
| 676 | { |
| 677 | MagickBooleanType |
| 678 | thread_status; |
| 679 | |
| 680 | thread_status=MagickTrue; |
| 681 | switch (i) |
| 682 | { |
| 683 | case 0: |
| 684 | { |
| 685 | if (is_gray != MagickFalse) |
| 686 | { |
| 687 | thread_status=ForwardFourierTransformChannel(image, |
| 688 | GrayChannels,modulus,fourier_image,exception); |
| 689 | break; |
| 690 | } |
| 691 | thread_status=ForwardFourierTransformChannel(image,RedChannel, |
| 692 | modulus,fourier_image,exception); |
| 693 | break; |
| 694 | } |
| 695 | case 1: |
| 696 | { |
| 697 | if (is_gray == MagickFalse) |
| 698 | thread_status=ForwardFourierTransformChannel(image, |
| 699 | GreenChannel,modulus,fourier_image,exception); |
| 700 | break; |
| 701 | } |
| 702 | case 2: |
| 703 | { |
| 704 | if (is_gray == MagickFalse) |
| 705 | thread_status=ForwardFourierTransformChannel(image, |
| 706 | BlueChannel,modulus,fourier_image,exception); |
| 707 | break; |
| 708 | } |
| 709 | case 4: |
| 710 | { |
| 711 | if (image->matte != MagickFalse) |
| 712 | thread_status=ForwardFourierTransformChannel(image, |
| 713 | OpacityChannel,modulus,fourier_image,exception); |
| 714 | break; |
| 715 | } |
| 716 | case 5: |
| 717 | { |
| 718 | if (image->colorspace == CMYKColorspace) |
| 719 | thread_status=ForwardFourierTransformChannel(image, |
| 720 | IndexChannel,modulus,fourier_image,exception); |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | if (thread_status == MagickFalse) |
| 725 | status=thread_status; |
| 726 | } |
| 727 | if (status == MagickFalse) |
| 728 | fourier_image=DestroyImageList(fourier_image); |
| 729 | fftw_cleanup(); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | #endif |
| 734 | return(fourier_image); |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 739 | % % |
| 740 | % % |
| 741 | % % |
| 742 | % 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 % |
| 743 | % % |
| 744 | % % |
| 745 | % % |
| 746 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 747 | % |
| 748 | % InverseFourierTransformImage() implements the inverse discrete Fourier |
| 749 | % transform (DFT) of the image either as a magnitude / phase or real / |
| 750 | % imaginary image pair. |
| 751 | % |
| 752 | % The format of the InverseFourierTransformImage method is: |
| 753 | % |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 754 | % Image *InverseFourierTransformImage(const Image *magnitude_image, |
| 755 | % const Image *phase_image,const MagickBooleanType modulus, |
| 756 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | % |
| 758 | % A description of each parameter follows: |
| 759 | % |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 760 | % o magnitude_image: the magnitude or real image. |
| 761 | % |
| 762 | % o phase_image: the phase or imaginary image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | % |
| 764 | % o modulus: if true, return transform as a magnitude / phase pair |
| 765 | % otherwise a real / imaginary image pair. |
| 766 | % |
| 767 | % o exception: return any errors or warnings in this structure. |
| 768 | % |
| 769 | */ |
| 770 | |
| 771 | #if defined(MAGICKCORE_FFTW_DELEGATE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 772 | static MagickBooleanType InverseQuadrantSwap(const size_t width, |
| 773 | const size_t height,const double *source,double *destination) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 775 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | center, |
| 777 | y; |
| 778 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 779 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 780 | x; |
| 781 | |
| 782 | /* |
| 783 | Swap quadrants. |
| 784 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 785 | center=(ssize_t) floor((double) width/2.0)+1L; |
| 786 | for (y=1L; y < (ssize_t) height; y++) |
| 787 | for (x=0L; x < (ssize_t) (width/2L+1L); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 788 | destination[center*(height-y)-x+width/2L]=source[y*width+x]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 789 | for (y=0L; y < (ssize_t) height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 790 | destination[center*y]=source[y*width+width/2L]; |
| 791 | for (x=0L; x < center; x++) |
| 792 | destination[x]=source[center-x-1L]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 793 | return(RollFourier(center,height,0L,(ssize_t) height/-2L,destination)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static MagickBooleanType InverseFourier(FourierInfo *fourier_info, |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 797 | const Image *magnitude_image,const Image *phase_image,fftw_complex *fourier, |
| 798 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | { |
| 800 | CacheView |
| 801 | *magnitude_view, |
| 802 | *phase_view; |
| 803 | |
| 804 | double |
| 805 | *magnitude, |
| 806 | *phase, |
| 807 | *magnitude_source, |
| 808 | *phase_source; |
| 809 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 810 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | y; |
| 812 | |
| 813 | MagickBooleanType |
| 814 | status; |
| 815 | |
| 816 | register const IndexPacket |
| 817 | *indexes; |
| 818 | |
| 819 | register const PixelPacket |
| 820 | *p; |
| 821 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 822 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 823 | i, |
| 824 | x; |
| 825 | |
| 826 | /* |
| 827 | Inverse fourier - read image and break down into a double array. |
| 828 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 829 | magnitude_source=(double *) AcquireQuantumMemory((size_t) |
| 830 | fourier_info->height,fourier_info->width*sizeof(*magnitude_source)); |
| 831 | if (magnitude_source == (double *) NULL) |
| 832 | { |
| 833 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 834 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 835 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | return(MagickFalse); |
| 837 | } |
| 838 | phase_source=(double *) AcquireQuantumMemory((size_t) fourier_info->height, |
| 839 | fourier_info->height*sizeof(*phase_source)); |
| 840 | if (phase_source == (double *) NULL) |
| 841 | { |
| 842 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 843 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 844 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 845 | magnitude_source=(double *) RelinquishMagickMemory(magnitude_source); |
| 846 | return(MagickFalse); |
| 847 | } |
| 848 | i=0L; |
| 849 | magnitude_view=AcquireCacheView(magnitude_image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 850 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 851 | { |
| 852 | p=GetCacheViewVirtualPixels(magnitude_view,0L,y,fourier_info->width,1UL, |
| 853 | exception); |
| 854 | if (p == (const PixelPacket *) NULL) |
| 855 | break; |
| 856 | indexes=GetCacheViewAuthenticIndexQueue(magnitude_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 857 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 858 | { |
| 859 | switch (fourier_info->channel) |
| 860 | { |
| 861 | case RedChannel: |
| 862 | default: |
| 863 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 864 | magnitude_source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | case GreenChannel: |
| 868 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 869 | magnitude_source[i]=QuantumScale*GetGreenPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 870 | break; |
| 871 | } |
| 872 | case BlueChannel: |
| 873 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 874 | magnitude_source[i]=QuantumScale*GetBluePixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | case OpacityChannel: |
| 878 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 879 | magnitude_source[i]=QuantumScale*GetOpacityPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | case IndexChannel: |
| 883 | { |
| 884 | magnitude_source[i]=QuantumScale*indexes[x]; |
| 885 | break; |
| 886 | } |
| 887 | case GrayChannels: |
| 888 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 889 | magnitude_source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
| 892 | } |
| 893 | i++; |
| 894 | p++; |
| 895 | } |
| 896 | } |
| 897 | i=0L; |
| 898 | phase_view=AcquireCacheView(phase_image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 899 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 900 | { |
| 901 | p=GetCacheViewVirtualPixels(phase_view,0,y,fourier_info->width,1, |
| 902 | exception); |
| 903 | if (p == (const PixelPacket *) NULL) |
| 904 | break; |
| 905 | indexes=GetCacheViewAuthenticIndexQueue(phase_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 906 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 907 | { |
| 908 | switch (fourier_info->channel) |
| 909 | { |
| 910 | case RedChannel: |
| 911 | default: |
| 912 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 913 | phase_source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | break; |
| 915 | } |
| 916 | case GreenChannel: |
| 917 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 918 | phase_source[i]=QuantumScale*GetGreenPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 919 | break; |
| 920 | } |
| 921 | case BlueChannel: |
| 922 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 923 | phase_source[i]=QuantumScale*GetBluePixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 924 | break; |
| 925 | } |
| 926 | case OpacityChannel: |
| 927 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 928 | phase_source[i]=QuantumScale*GetOpacityPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
| 931 | case IndexChannel: |
| 932 | { |
| 933 | phase_source[i]=QuantumScale*indexes[x]; |
| 934 | break; |
| 935 | } |
| 936 | case GrayChannels: |
| 937 | { |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 938 | phase_source[i]=QuantumScale*GetRedPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | break; |
| 940 | } |
| 941 | } |
| 942 | i++; |
| 943 | p++; |
| 944 | } |
| 945 | } |
| 946 | if (fourier_info->modulus != MagickFalse) |
| 947 | { |
| 948 | i=0L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 949 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 950 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 951 | { |
| 952 | phase_source[i]-=0.5; |
| 953 | phase_source[i]*=(2.0*MagickPI); |
| 954 | i++; |
| 955 | } |
| 956 | } |
| 957 | magnitude_view=DestroyCacheView(magnitude_view); |
| 958 | phase_view=DestroyCacheView(phase_view); |
| 959 | magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info->height, |
| 960 | fourier_info->center*sizeof(*magnitude)); |
| 961 | if (magnitude == (double *) NULL) |
| 962 | { |
| 963 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 964 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 965 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 966 | magnitude_source=(double *) RelinquishMagickMemory(magnitude_source); |
| 967 | phase_source=(double *) RelinquishMagickMemory(phase_source); |
| 968 | return(MagickFalse); |
| 969 | } |
| 970 | status=InverseQuadrantSwap(fourier_info->width,fourier_info->height, |
| 971 | magnitude_source,magnitude); |
| 972 | magnitude_source=(double *) RelinquishMagickMemory(magnitude_source); |
| 973 | phase=(double *) AcquireQuantumMemory((size_t) fourier_info->width, |
| 974 | fourier_info->height*sizeof(*phase)); |
| 975 | if (phase == (double *) NULL) |
| 976 | { |
| 977 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 978 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 979 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 980 | phase_source=(double *) RelinquishMagickMemory(phase_source); |
| 981 | return(MagickFalse); |
| 982 | } |
| 983 | CorrectPhaseLHS(fourier_info->width,fourier_info->width,phase_source); |
| 984 | if (status != MagickFalse) |
| 985 | status=InverseQuadrantSwap(fourier_info->width,fourier_info->height, |
| 986 | phase_source,phase); |
| 987 | phase_source=(double *) RelinquishMagickMemory(phase_source); |
| 988 | /* |
| 989 | Merge two sets. |
| 990 | */ |
| 991 | i=0L; |
| 992 | if (fourier_info->modulus != MagickFalse) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 993 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 994 | for (x=0L; x < (ssize_t) fourier_info->center; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 995 | { |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 996 | #if defined(MAGICKCORE_HAVE_COMPLEX_H) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | fourier[i]=magnitude[i]*cos(phase[i])+I*magnitude[i]*sin(phase[i]); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 998 | #else |
| 999 | fourier[i][0]=magnitude[i]*cos(phase[i]); |
| 1000 | fourier[i][1]=magnitude[i]*sin(phase[i]); |
| 1001 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | i++; |
| 1003 | } |
| 1004 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1005 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
| 1006 | for (x=0L; x < (ssize_t) fourier_info->center; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1007 | { |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1008 | #if defined(MAGICKCORE_HAVE_COMPLEX_H) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | fourier[i]=magnitude[i]+I*phase[i]; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1010 | #else |
| 1011 | fourier[i][0]=magnitude[i]; |
| 1012 | fourier[i][1]=phase[i]; |
| 1013 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1014 | i++; |
| 1015 | } |
| 1016 | phase=(double *) RelinquishMagickMemory(phase); |
| 1017 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 1018 | return(status); |
| 1019 | } |
| 1020 | |
| 1021 | static MagickBooleanType InverseFourierTransform(FourierInfo *fourier_info, |
| 1022 | fftw_complex *fourier,Image *image,ExceptionInfo *exception) |
| 1023 | { |
| 1024 | CacheView |
| 1025 | *image_view; |
| 1026 | |
| 1027 | double |
| 1028 | *source; |
| 1029 | |
| 1030 | fftw_plan |
| 1031 | fftw_c2r_plan; |
| 1032 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1033 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1034 | y; |
| 1035 | |
| 1036 | register IndexPacket |
| 1037 | *indexes; |
| 1038 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1039 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | i, |
| 1041 | x; |
| 1042 | |
| 1043 | register PixelPacket |
| 1044 | *q; |
| 1045 | |
| 1046 | source=(double *) AcquireQuantumMemory((size_t) fourier_info->width, |
| 1047 | fourier_info->height*sizeof(double)); |
| 1048 | if (source == (double *) NULL) |
| 1049 | { |
| 1050 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 1051 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 1052 | return(MagickFalse); |
| 1053 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1054 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1055 | #pragma omp critical (MagickCore_InverseFourierTransform) |
| 1056 | #endif |
cristy | df079ac | 2010-09-10 01:45:44 +0000 | [diff] [blame] | 1057 | { |
| 1058 | fftw_c2r_plan=fftw_plan_dft_c2r_2d(fourier_info->width,fourier_info->height, |
| 1059 | fourier,source,FFTW_ESTIMATE); |
| 1060 | fftw_execute(fftw_c2r_plan); |
| 1061 | fftw_destroy_plan(fftw_c2r_plan); |
| 1062 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1063 | i=0L; |
| 1064 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1065 | for (y=0L; y < (ssize_t) fourier_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1066 | { |
cristy | 8581205 | 2010-09-14 17:56:15 +0000 | [diff] [blame] | 1067 | if (y >= (ssize_t) image->rows) |
| 1068 | break; |
| 1069 | q=GetCacheViewAuthenticPixels(image_view,0L,y,fourier_info->width > |
| 1070 | image->columns ? image->columns : fourier_info->width,1UL,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1071 | if (q == (PixelPacket *) NULL) |
| 1072 | break; |
| 1073 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1074 | for (x=0L; x < (ssize_t) fourier_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1075 | { |
| 1076 | switch (fourier_info->channel) |
| 1077 | { |
| 1078 | case RedChannel: |
| 1079 | default: |
| 1080 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1081 | q->red=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | case GreenChannel: |
| 1085 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1086 | q->green=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | case BlueChannel: |
| 1090 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1091 | q->blue=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | case OpacityChannel: |
| 1095 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1096 | q->opacity=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | case IndexChannel: |
| 1100 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1101 | indexes[x]=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1102 | break; |
| 1103 | } |
| 1104 | case GrayChannels: |
| 1105 | { |
cristy | e85007d | 2010-06-06 22:51:36 +0000 | [diff] [blame] | 1106 | q->red=ClampToQuantum(QuantumRange*source[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1107 | q->green=q->red; |
| 1108 | q->blue=q->red; |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | i++; |
| 1113 | q++; |
| 1114 | } |
| 1115 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1116 | break; |
| 1117 | } |
| 1118 | image_view=DestroyCacheView(image_view); |
| 1119 | source=(double *) RelinquishMagickMemory(source); |
| 1120 | return(MagickTrue); |
| 1121 | } |
| 1122 | |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1123 | static MagickBooleanType InverseFourierTransformChannel( |
| 1124 | const Image *magnitude_image,const Image *phase_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1125 | const ChannelType channel,const MagickBooleanType modulus, |
| 1126 | Image *fourier_image,ExceptionInfo *exception) |
| 1127 | { |
| 1128 | double |
| 1129 | *magnitude, |
| 1130 | *phase; |
| 1131 | |
| 1132 | fftw_complex |
| 1133 | *fourier; |
| 1134 | |
| 1135 | FourierInfo |
| 1136 | fourier_info; |
| 1137 | |
| 1138 | MagickBooleanType |
| 1139 | status; |
| 1140 | |
| 1141 | size_t |
| 1142 | extent; |
| 1143 | |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1144 | fourier_info.width=magnitude_image->columns; |
| 1145 | if ((magnitude_image->columns != magnitude_image->rows) || |
| 1146 | ((magnitude_image->columns % 2) != 0) || |
| 1147 | ((magnitude_image->rows % 2) != 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1148 | { |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1149 | extent=magnitude_image->columns < magnitude_image->rows ? |
| 1150 | magnitude_image->rows : magnitude_image->columns; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1151 | fourier_info.width=(extent & 0x01) == 1 ? extent+1UL : extent; |
| 1152 | } |
| 1153 | fourier_info.height=fourier_info.width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1154 | fourier_info.center=(ssize_t) floor((double) fourier_info.width/2.0)+1L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1155 | fourier_info.channel=channel; |
| 1156 | fourier_info.modulus=modulus; |
| 1157 | magnitude=(double *) AcquireQuantumMemory((size_t) fourier_info.height, |
| 1158 | fourier_info.center*sizeof(double)); |
| 1159 | if (magnitude == (double *) NULL) |
| 1160 | { |
| 1161 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1162 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 1163 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | return(MagickFalse); |
| 1165 | } |
| 1166 | phase=(double *) AcquireQuantumMemory((size_t) fourier_info.height, |
| 1167 | fourier_info.center*sizeof(double)); |
| 1168 | if (phase == (double *) NULL) |
| 1169 | { |
| 1170 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1171 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 1172 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1173 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 1174 | return(MagickFalse); |
| 1175 | } |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 1176 | fourier=(fftw_complex *) AcquireQuantumMemory((size_t) fourier_info.height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1177 | fourier_info.center*sizeof(*fourier)); |
| 1178 | if (fourier == (fftw_complex *) NULL) |
| 1179 | { |
| 1180 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1181 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 1182 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1183 | phase=(double *) RelinquishMagickMemory(phase); |
| 1184 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 1185 | return(MagickFalse); |
| 1186 | } |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1187 | status=InverseFourier(&fourier_info,magnitude_image,phase_image,fourier, |
| 1188 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1189 | if (status != MagickFalse) |
| 1190 | status=InverseFourierTransform(&fourier_info,fourier,fourier_image, |
| 1191 | exception); |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 1192 | fourier=(fftw_complex *) RelinquishMagickMemory(fourier); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1193 | phase=(double *) RelinquishMagickMemory(phase); |
| 1194 | magnitude=(double *) RelinquishMagickMemory(magnitude); |
| 1195 | return(status); |
| 1196 | } |
| 1197 | #endif |
| 1198 | |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1199 | MagickExport Image *InverseFourierTransformImage(const Image *magnitude_image, |
| 1200 | const Image *phase_image,const MagickBooleanType modulus, |
| 1201 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1202 | { |
| 1203 | Image |
| 1204 | *fourier_image; |
| 1205 | |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1206 | assert(magnitude_image != (Image *) NULL); |
| 1207 | assert(magnitude_image->signature == MagickSignature); |
| 1208 | if (magnitude_image->debug != MagickFalse) |
| 1209 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 1210 | magnitude_image->filename); |
| 1211 | if (phase_image == (Image *) NULL) |
| 1212 | { |
| 1213 | (void) ThrowMagickException(exception,GetMagickModule(),ImageError, |
| 1214 | "ImageSequenceRequired","`%s'",magnitude_image->filename); |
cristy | 9372a15 | 2009-11-18 01:42:16 +0000 | [diff] [blame] | 1215 | return((Image *) NULL); |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1216 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1217 | #if !defined(MAGICKCORE_FFTW_DELEGATE) |
| 1218 | fourier_image=(Image *) NULL; |
| 1219 | (void) modulus; |
| 1220 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 1221 | MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (FFTW)", |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1222 | magnitude_image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | #else |
| 1224 | { |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1225 | fourier_image=CloneImage(magnitude_image,magnitude_image->columns, |
| 1226 | magnitude_image->rows,MagickFalse,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1227 | if (fourier_image != (Image *) NULL) |
| 1228 | { |
| 1229 | MagickBooleanType |
| 1230 | is_gray, |
| 1231 | status; |
| 1232 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1233 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1234 | i; |
| 1235 | |
| 1236 | status=MagickTrue; |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1237 | is_gray=IsGrayImage(magnitude_image,exception); |
| 1238 | if (is_gray != MagickFalse) |
| 1239 | is_gray=IsGrayImage(phase_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1240 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1241 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1242 | #endif |
| 1243 | for (i=0L; i < 5L; i++) |
| 1244 | { |
| 1245 | MagickBooleanType |
| 1246 | thread_status; |
| 1247 | |
| 1248 | thread_status=MagickTrue; |
| 1249 | switch (i) |
| 1250 | { |
| 1251 | case 0: |
| 1252 | { |
| 1253 | if (is_gray != MagickFalse) |
| 1254 | { |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1255 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1256 | phase_image,GrayChannels,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | } |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1259 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1260 | phase_image,RedChannel,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | case 1: |
| 1264 | { |
| 1265 | if (is_gray == MagickFalse) |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1266 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1267 | phase_image,GreenChannel,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | case 2: |
| 1271 | { |
| 1272 | if (is_gray == MagickFalse) |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1273 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1274 | phase_image,BlueChannel,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | } |
| 1277 | case 3: |
| 1278 | { |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1279 | if (magnitude_image->matte != MagickFalse) |
| 1280 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1281 | phase_image,OpacityChannel,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | } |
| 1284 | case 4: |
| 1285 | { |
cristy | c955079 | 2009-11-13 20:05:42 +0000 | [diff] [blame] | 1286 | if (magnitude_image->colorspace == CMYKColorspace) |
| 1287 | thread_status=InverseFourierTransformChannel(magnitude_image, |
| 1288 | phase_image,IndexChannel,modulus,fourier_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | if (thread_status == MagickFalse) |
| 1293 | status=thread_status; |
| 1294 | } |
| 1295 | if (status == MagickFalse) |
| 1296 | fourier_image=DestroyImage(fourier_image); |
| 1297 | } |
| 1298 | fftw_cleanup(); |
| 1299 | } |
| 1300 | #endif |
| 1301 | return(fourier_image); |
| 1302 | } |