home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / vfft / realfft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-20  |  5.0 KB  |  207 lines

  1. /*
  2. %    REALFFT . C
  3. %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5.  
  6. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  7. Permission is granted to reproduce this software for non-commercial
  8. purposes provided that this notice is left intact.
  9.  
  10. It is acknowledged that the U.S. Government has rights to this software
  11. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  12. and the University of California.
  13.  
  14. This software is provided as a professional and academic contribution
  15. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  16. with no warranties of any kind whatsoever, no support, no promise of
  17. updates, or printed documentation. By using this software, you
  18. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  19. University of California shall have no liability with respect to the
  20. infringement of other copyrights by any part of this software.
  21.  
  22. For further information about this notice, contact William Johnston,
  23. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  24. (wejohnston@lbl.gov)
  25.  
  26. For further information about this software, contact:
  27.     Jin Guojun
  28.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  29.     g_jin@lbl.gov
  30.  
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. %
  33. % AUTHOR    Jin Guojun - LBL    12/30/90
  34. */
  35.  
  36. #include "complex.h"
  37. #include "header.def"
  38. #include "imagedef.h"
  39.  
  40. #define    row    uimg.height
  41. #define    cln    uimg.width
  42. #define    frm    uimg.frames
  43.  
  44. U_IMAGE    uimg;
  45. bool    dimens;
  46. char    usage[]="options\n\
  47. -d    double format output\n\
  48. -D2    2-dimension\n\
  49. [<] image [> VFFT]\n";
  50. VType    *itemp;
  51. MType    fsize;
  52.  
  53.  
  54. main (argc, argv)
  55. int    argc;
  56. char**    argv;
  57. {
  58. bool    dflag=0;
  59. int    dimen1len, dimen2size, f, i;
  60.  
  61. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "S20-1");
  62.  
  63. for (i=1; i<argc; i++)
  64.     if (*argv[i] == '-'){
  65.     int    k=1;
  66.     switch (argv[i][k++]) {
  67.     case 'D':
  68.         dimens = (argv[i][k] == '2');
  69.         break;
  70.     case 'd':
  71.         dflag++;    break;
  72. #ifdef    IBMPC
  73.     case 'i':if (avset(argc, argv, &i, &k, 1) &&
  74.             !(in_fp=freopen(argv[i]+k, "rb", stdin)))
  75.             syserr("can't open %s as image input", argv[i]);
  76.         break;
  77. #endif
  78.     case 'o':if (avset(argc, argv, &i, &k) &&
  79.             freopen(argv[i]+k, "wb", stdout))    break;
  80.         message("%s can't be opened", argv[i]);
  81.     default:
  82. info:        usage_n_options(usage, i, argv[i]);
  83.     }
  84.     }
  85.     else if ((in_fp=freopen(argv[i], "r", stdin)) == NULL)
  86.     syserr("can't open frame file - %s",argv[i]);
  87.  
  88. io_test(fileno(in_fp), goto    info);
  89.  
  90. (*uimg.header_handle)(HEADER_READ, &uimg, 0, 0);
  91.  
  92. fsize = row*cln;
  93. dimen1len = (cln>>1) + 1;
  94. dimen2size = dimen1len * row << 1;
  95.  
  96. work_space_init(MAX(MAX(cln, row), frm));
  97.  
  98. if (dflag){
  99. double    *ibuf = nzalloc(fsize, sizeof(*ibuf), "fibuf"),
  100.     *obuf = nzalloc(dimen2size*(dimens?1:frm), sizeof(*obuf), "fobuf");
  101.  
  102.     uimg.o_form = dimens ? IFMT_DVFFT2D : IFMT_DVFFT3D;
  103.     uimg.pxl_out = 16;
  104. }
  105. else    {
  106. float    *ibuf = nzalloc(fsize, sizeof(*ibuf), "fibuf"),
  107.     *obuf = nzalloc(dimen2size*(dimens?1:frm), sizeof(*obuf), "fobuf");
  108.  
  109.     uimg.o_form = dimens ? IFMT_VFFT2D : IFMT_VFFT3D;
  110.     uimg.pxl_out = 8;
  111.     (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, True);
  112.     if (uimg.pxl_in<4)
  113.         itemp = nzalloc(fsize, uimg.pxl_in, "itemp");
  114.     else    itemp = ibuf;
  115.  
  116.     for (f=0; f<frm; f++){
  117.         if (uimg.in_form != IFMT_FLOAT){
  118.         i = upread(itemp, uimg.pxl_in, fsize, in_fp);
  119.         if (i != fsize)
  120.             syserr("frame %d [%ld] read %d", f, fsize, i);
  121.         switch(uimg.pxl_in){
  122.         case 1:    btof(itemp, ibuf, fsize);
  123.             break;
  124.         case 2:    stof(itemp, ibuf, fsize);
  125.             break;
  126.         case 3:    itof(itemp, ibuf, fsize);
  127.             break;
  128.         }
  129.         }
  130.  
  131.         vfft2d(ibuf, cln, row, obuf + (dimens ? 0 : f*dimen2size));
  132.  
  133.         if (dimens){
  134.         i = fwrite(obuf, sizeof(*obuf), dimen2size, out_fp);
  135.         if (i != dimen2size)
  136.             syserr("2d[%d] write %d", dimen2size, i);
  137.         }
  138.     }
  139.     if (!dimens && frm>1){
  140.     int    r;
  141.     COMPLEX    *cp;
  142.     register COMPLEX    *cmptr;
  143.     register int    c, n=frm, dimen1size = dimen1len;
  144.  
  145.         load_w(frm);
  146.  
  147.         for (r=0; r<row; r++)
  148.             for (c=0; c<dimen1size; c++){
  149.             cp = (COMPLEX*)obuf + r*dimen1size + c;
  150.             cmptr = cmpx_in;
  151.             for (i = 0; i < n; i++, cp+=dimen2size>>1)
  152.                 cmptr[i] = *cp;
  153.  
  154.             Fourier(cmptr, n, cmpx_out);
  155.  
  156.             cp = (COMPLEX*)obuf + r*dimen1size + c;/* store back to dst */
  157.             cmptr = cmpx_out;
  158.             for (i = 0; i < n; i++, cp+=dimen2size>>1)
  159.                 *cp = cmptr[i];
  160.             }
  161.     /*    row = dimen1size;    real columns for IFMT_COMPLEX */
  162.         fsize = frm*dimen2size;
  163.         i = fwrite(obuf, sizeof(*obuf), fsize, out_fp);
  164.         if (i != fsize)
  165.             syserr("3d[%d] write %d", fsize, i);
  166.     }
  167. }
  168. }
  169.  
  170. btof(ibp, obp, n)
  171. register byte*    ibp;
  172. register float    *obp;
  173. register int    n;
  174. {
  175. register int    i;
  176. for (i=0; i<n; i++)    *obp++ = *ibp++;
  177. }
  178.  
  179. stof(ibp, obp, n)
  180. register unsigned short    *ibp;
  181. register float    *obp;
  182. register int    n;
  183. {
  184. register int    i;
  185. msg("%d stof\n", n);
  186. for (i=0; i<n; i++)    *obp++ = *ibp++;
  187. }
  188.  
  189. itof(ibp, obp, n)
  190. register int*    ibp, n;
  191. register float*    obp;
  192. {
  193. register int    i;
  194. msg("%d itof\n", n);
  195. for (i=0; i<n; i++)    *obp++ = *ibp++;
  196. }
  197.  
  198. ftoc(ibp, obp, n)
  199. register float*    ibp;
  200. register double    *obp;
  201. register int    n;
  202. {
  203. register int    i;
  204. msg("%d FtoD\n", n);
  205. for (i=0; i<n; i++)    *obp++ = *ibp++;
  206. }
  207.