www.pudn.com > fft_c.rar > fft.c


#include "stdio.h" 
#include "stdlib.h" 
#include "tiffio.c" 
#include  
// 
// FFT example for 159731  
// Martin 21/5/01 
// 
# define M_PIl          3.1415926535897932384626433832795029L  /* pi */ 
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr 
 
typedef struct { // a complex pixel 
  double real; 
  double imag; 
} cpixel; 
 
typedef struct { // a complex image 
  int x,y; 
  cpixel **data; 
} cimage; 
 
typedef cimage * cimg_ptr; // a pointer to a complex image 
 
int topixel(double d) { // convert d to a pixel 
  if (d>254) return 255; 
  if (d<1) return 0; 
  return (int) d; 
} 
 
cimg_ptr create_cimage(int x,int y) { // create complex image 
  cimg_ptr im; 
  int a; 
 
  im=new(cimage,1); 
  im->x=x; 
  im->y=y; 
  im->data=new(cpixel *,im->y); 
  im->data[0]=new(cpixel,im->x*im->y); 
  for( a=1;a < im->y;a++) 
    im->data[a]=im->data[a-1]+im->x; 
  return im; 
} 
// 1D fft of array size nn 
// data stored as  
// data[0] is real[0], data[1] is imag[0] 
// data[w] is real[1], data[w+1] is imag[1] 
// data[2w] is real[2], data[2w+1] is imag[2] 
// isign is 1 for forward transform and -1 for inverse 
void fft(double *data,int nn,int isign,int w) {  
  int n,mmax,m,j,istep,i; 
  double wtemp,wr,wpr,wpi,wi,theta; 
  double tempr,tempi; 
  data-=w; 
  n=nn<<1; 
  j=1; 
  for (i=1;ii)  
    { 
      SWAP(data[j*w],data[i*w]); 
      SWAP(data[(j*w)+1],data[(i*w)+1]); 
    } 
    m=n>>1; 
    while (m>=2 && j>m )  
    { 
      j-=m; 
      m >>= 1; 
    } 
    j+=m; 
  } 
  mmax=2; 
  while (n>mmax) { 
    istep=2*mmax; 
    theta=(2*M_PIl)/(isign * mmax); 
    wtemp=sin(.5*theta); 
    wpr= -2.0 * wtemp*wtemp; 
    wpi=sin(theta); 
    wr=1.0; 
    wi=0.0; 
    for(m=1;my;j++) 
   fft((double *)(i->data[j]),512,dir,1); 
 for(j=0;jx;j++) 
   fft((double *)(i->data[0])+j*2,512,dir,512); 
} 
 
int main() { 
  img_ptr im,im1; 
  int width,height,i,j; 
  cimg_ptr fft; 
 
//  FILE *fin=fopen("clown.tif","rb");    
//  FILE *fout=fopen("output1.tif","wb"); 
  FILE *fin=fopen("output1.tif","rb");    
  FILE *fout=fopen("output2.tif","wb"); 
  if(!fin) {puts("Can't open input file");exit(1);} 
  im=load_image(fin); 
  width=im->x; 
  height=im->y; 
  fft=create_cimage(width,height); 
  for(j=0;jdata[j][i].real=im->data[j][i]; 
      fft->data[j][i].imag=0; 
    } 
  } 
//  fft2d(fft,1); 
  fft2d(fft,-1); 
  for(j=0;jdata[j][i]=topixel(10*(fft->data[j][i].real)+128); 
    } 
  } 
 
/*  for(j=0;jdata[j][i]=topixel(fft->data[j][i].real); 
    } 
  }*/ 
  save_image(fout,im); 
}