ft should work now - maybe api still sux!
authorhackbard <hackbard>
Thu, 7 Oct 2004 14:59:51 +0000 (14:59 +0000)
committerhackbard <hackbard>
Thu, 7 Oct 2004 14:59:51 +0000 (14:59 +0000)
dft.c

diff --git a/dft.c b/dft.c
index 385c22d..20550b4 100644 (file)
--- a/dft.c
+++ b/dft.c
@@ -12,6 +12,11 @@ int main(int argc,char **argv) {
   t_fourier fourier;
   t_bmp src;
   t_bmp dst;
+  int x,y;
+  int offy,offt;
+  int scale;
+  double max;
+  double *mag;
 
   bmp_init(&src,1);
   bmp_init(&dst,1);
@@ -20,22 +25,73 @@ int main(int argc,char **argv) {
   strcpy(src.file,argv[1]);
   strcpy(dst.file,argv[2]);
 
+  fourier_init(&fourier,1);
+  fourier.type=DFT|FWD;
+  fourier.dim=2;
+
   bmp_read_file(&src);
 
   dst.width=src.info.width;
   dst.height=src.info.height;
   bmp_alloc_map(&dst);
 
-  /* simple copy for testing by now ! */
-  memcpy(dst.map,src.map,src.info.imagesize);
+  fourier.data_len[0]=src.info.width;
+  fourier.data_len[1]=src.info.height;
+  fourier_alloc_data(&fourier);
+
+  mag=(double *)malloc(fourier.data_len[0]*fourier.data_len[1]*sizeof(double));
+  if(mag==NULL) {
+    printf("unable to alloc mag memory\n");
+    return -1;
+  }
+
+  // copy the data
+  offy=0;
+  for(y=0;y<fourier.data_len[1];y++) {
+    for(x=0;x<fourier.data_len[0];x++) {
+      offt=offy+x;
+      fourier.data[offt].r=(src.map[offt].r+src.map[offt].g+src.map[offt].b)/3;
+    }
+    offy+=fourier.data_len[0];
+  }
+
+  // do the fourier trafo
+  fourier_dft_2d(&fourier);
+
+  printf("fourier done!\n");
+
+  // copy back the data, intensity = sqrt(r^2+i^2)
+  max=0;
+  offy=0;
+  for(y=0;y<fourier.data_len[1];y++) {
+    for(x=0;x<fourier.data_len[0];x++) {
+      offt=offy+x;
+      mag[offt]=sqrt(fourier.ftdata[offt].r*fourier.ftdata[offt].r+fourier.ftdata[offt].i*fourier.ftdata[offt].i);
+      if(mag[offt]>max) max=mag[offt];
+    }
+    offy+=fourier.data_len[0];
+  }
+
+  if(max!=0) scale=(int)255/max;
+  else scale=0;
+
+  if(scale!=0) {
+    printf("scaling image intensity: %d\n",scale);
+    for(x=0;x<dst.width*dst.height;x++) {
+      dst.map[x].r=scale*mag[x];
+      dst.map[x].g=dst.map[x].r;
+      dst.map[x].b=dst.map[x].r;
+    }
+  }
 
   bmp_write_file(&dst);
 
+  fourier_shutdown(&fourier);
+
   bmp_shutdown(&src);
   bmp_shutdown(&dst);
 
   printf("done ...\n");
 
   return 1;
-
 }