File: readarr.c

package info (click to toggle)
saoimage 1.35.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,188 kB
  • ctags: 4,403
  • sloc: ansic: 52,848; makefile: 243; sh: 31
file content (138 lines) | stat: -rw-r--r-- 4,493 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	readarr.c (Read Array)
 * Purpose:	Read in raster line array images
 * Subroutine:	read_array()			returns: void
 * Copyright:	1999 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version	     31 October 1988
 * 		{1} Doug Mink  add skip over images          18 October 1995
 *              {2} Peter Teuben  fixed bug "-skip 0 -plane N"   23 Feb 1999
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* define stderr */
#include <X11/Xlib.h>		/* needed for control.h */
#include "hfiles/constant.h"	/* define data type codes */
#include "hfiles/control.h"	/* define IOP codes */
#include "hfiles/image.h"

/*
 * Subroutine:	read_array
 * Purpose:	Read array data from a file
 * Note:	Assumes file was tested benignly, exits here if trouble
 */
void read_array ( fd, img, imgbuf, filebuf, width, height, X, Y, block,
		  verbose )
     int fd;			/* if >=0 handle to open & ready image file */
     struct imageRec *img;	/* record describing image file and its use */
     short *imgbuf;		/* buffer to receive i*2 data */
     char *filebuf;		/* buffer to receive raw data */
     int width, height;		/* width and height of buffer */
     int X, Y;			/* starting point in image file (no support) */
     int block;			/* blocking factor (not yet supported */
     int verbose;		/* whether to print explanatory messages */
{
  int vals;
  static int read_data();
  int image_start;
  int open_disk(), lseek_disk();
  void close_disk();
  void say_goodbye(), scale_data_u1(), scale_data_i2(), scale_data_u2();
  void scale_data_i4(), scale_data_r4(), scale_data_r8();

  if( (X!=0) || (Y!=0) || (block!=1) ) {
    (void)fprintf(stderr, "Error: no subsection support yet\n");
    return;
  }
  /* if not passed an open file, open it and move past the header */
  if( fd == -1 ) {
    /* open the image file */
    if( (fd = open_disk(img->filename, IOP_Read, 0)) < 0 )
      return;
    /* skip header if necessary */
    if( img->headersize > 0 || img->nimage > 1) {
       image_start = img->headersize +
              ((img->nimage-1) * img->filerows * img->filecols * img->bytepix);
      if( lseek_disk(fd, image_start, img->filename) < 0 ) {
	close_disk(fd, img->filename);
	return;
      }
    }
  }
  /* FOR NOW, READ ARRAY CANNOT HANDLE OVERSIZED ARRAYS */
  if( (width != img->filecols) ||
      (height != img->filerows) ) {
    (void)fprintf(stderr, "Error: cannot handle %d x %d array\n",
		  img->filecols, img->filerows);
    return;
  }
  /* read in the data */
  vals = read_data(fd, img, filebuf);
  /* read the image into the picture buffer */
  switch( img->storage_type ) {
  case ARR_U1:
    scale_data_u1(img, imgbuf, (unsigned char *)filebuf, vals);
    break;
  case ARR_I2:
    scale_data_i2(img, imgbuf, (short *)filebuf, vals);
    break;
  case ARR_U2:
    scale_data_u2(img, imgbuf, (unsigned short *)filebuf, vals);
    break;
  case ARR_I4:
    scale_data_i4(img, imgbuf, (long *)filebuf, vals, verbose);
    break;
  case ARR_R4:
    scale_data_r4(img, imgbuf, (float *)filebuf, vals, verbose);
    break;
  case ARR_R8:
    scale_data_r8(img, imgbuf, (double *)filebuf, vals, verbose);
    break;
  default:
    (void)fprintf(stderr, "illegal array type: %d\n", img->storage_type);
    exit(1);
  }
  /* adjust buffer scale and bias to include that of original file */
  if( img->fscaled ) {
    if( img->fiscaled ) {
      img->fiscale *= img->fscale;
      img->fibias = (img->fscale * img->fibias) + img->fbias;
    } else {
      img->fiscaled = img->fscaled;
      img->fiscale = img->fscale;
      img->fibias = img->fbias;
    }
  }
  /* close the file */
  close_disk(fd, img->filename);
}

/*
 * Subroutine:	read_data
 * Purpose:	Read in the array data
 * UNIX calls:	read
 */
static int read_data ( fd, img, databuf )
     int fd;
     struct imageRec *img;
     char *databuf;
{
  int vals, nbytes;
  int read_disk();

  /* find size of image */
  vals = img->filecols * img->filerows;
  nbytes = vals * img->bytepix;
  /* read the file */
  if( read_disk(fd, databuf, nbytes, 1, img->filename, "data") != nbytes )
    return( 0 );
  else
    return( vals );
}