summaryrefslogtreecommitdiffstats
path: root/funtools/funtest/mkdim.c
blob: d0f468d3b101998a2a25ca6b86d17cdda8d32789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Solaris:
   gcc -g -o mkdim mkdim.c -I /soft/saord/include -L/soft/saord/lib -lfuntools
   -ldl -lsocket -lnsl -lm
   OS X:
   gcc -g -o mkdim mkdim.c -I /soft/saord/include -L/soft/saord/lib -lfuntools -lm

*/

#include <funtools.h>
#include <stdlib.h>
#include <string.h>
#include <file.h>

extern char *optarg;
extern int optind;

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
main(argc, argv)
     int argc;
     char **argv;
#endif
{
  int dim1, dim2, bitpix;
  int tdim;
  char *buf=NULL;
  char *oname=NULL;
  char lbuf[SZ_LINE];
  unsigned char *cbuf;
  short *sbuf;
  int *ibuf;
  float *fbuf;
  double *dbuf;
  double x, y, val;
  Fun ofun=NULL;

  /* exit on gio errors */
  setgerror(2);

  if( argc != 5 ){
    gerror(stderr, "usage: cat tfile | %s oname xdim ydim bitpix\n", argv[0]);
    return 1;
  }
  oname = argv[1];
  dim1 = atoi(argv[2]);
  dim2 = atoi(argv[3]);
  bitpix = atoi(argv[4]);
  
  /* open the output FITS file */
  if( oname ){
    if( !(ofun = FunOpen(oname, "w", NULL)) )
      gerror(stderr, "could not FunOpen output  file: %s\n", argv[optind-1]);
  }

  /* set appropriate data type buffer to point to image buffer */
  /* allocate the buffer */
  tdim = dim1*dim2;
  buf = malloc(tdim*ft_sizeof(bitpix));
  while( fgets(lbuf, SZ_LINE, stdin) ){
    if( sscanf(lbuf, "%lf %lf %lf", &x, &y, &val) != 3 ){
      gerror(stderr, "invalid input: requires x, y, val; got %s\n", lbuf);
    }
    switch(bitpix){
    case 8:
      cbuf = (unsigned char *)buf;
      cbuf[(int)y*dim2+(int)x] = (unsigned char)val;
      break;
    case 16:
      sbuf = (short *)buf;
      sbuf[(int)y*dim2+(int)x] = (short)val;
      break;
    case 32:
      ibuf = (int *)buf;
      ibuf[(int)y*dim2+(int)x] = (int)val;
      break;
    case -32:
      fbuf = (float *)buf;
      fbuf[(int)y*dim2+(int)x] = (float)val;
      break;
    case -64:
      dbuf = (double *)buf;
      dbuf[(int)y*dim2+(int)x] = val;
      break;
    }
  }
  /* write the output image, updating the FITS header from the orig file */
  if( ofun && buf ){
    if( !FunImagePut(ofun, buf, dim1, dim2, bitpix, NULL) )
      gerror(stderr, "could not FunImagePut: %s\n", argv[2]);
  }
  if( buf ) free(buf);
  if( ofun ) FunClose(ofun);
  return(0);
}