r40: made c++ compatible
[ctsim.git] / include / sdf.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: sdf.h,v 1.3 2000/05/07 12:46:19 kevin Exp $
6 **  $Log: sdf.h,v $
7 **  Revision 1.3  2000/05/07 12:46:19  kevin
8 **  made c++ compatible
9 **
10 **  Revision 1.2  2000/04/28 14:14:16  kevin
11 **  *** empty log message ***
12 **
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27 /* FILE IDENTIFICATION
28  *
29  *      Name:         sdf.h         Header file for Standard Data FIle system
30  *      Programmer:   Kevin Rosenberg
31  *      Date Started:  9-18-84
32  *      Last Change:  12-24-84
33  */
34
35 #ifndef __H_SDF
36 #define __H_SDF
37
38 #include "kmath.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43
44 /*----------------*/
45 /* SYSTEM ALIASES */
46 /*----------------*/
47
48 #define SDF_VERSION  2
49 #define SDF_ID_STR   "SDF"
50
51 /*------------------*/
52 /* DEFINITION BLOCK */
53 /*------------------*/
54
55 /* fixed system constants */
56
57 #define BLK_SIZE   256          /* Size of a header block */
58 #define NUM_DIRBLK   1          /* number of directory blocks */
59
60 /* block types */
61
62 #define BT_EMPTY        0       /* Not being used */
63 #define BT_DIRECTORY    1       /* First blocks in file */
64 #define BT_LABEL        2       /* Label block */
65
66 /* label types */
67
68 #define LT_EMPTY          0     /* label record is available for use */
69 #define LT_TITLE          1     /* descriptive label given by user */
70 #define LT_HISTORY        2     /* history label generated by application */
71                                 /* program */
72 #define LT_NOTE           3     /* A note created with a label editor */
73
74 /* codes for open_sdf and create_sdf */
75
76 #define SDF_RDONLY  0
77 #define SDF_WRONLY  1
78 #define SDF_RDWR    2
79
80 /* function return codes */
81
82 #define SDF_ERROR  ERROR
83 #define SDF_2D_ERROR SDF_ERROR
84 #define SDF_OK     OK
85
86 /* types of sdf file, this area is EXPANDABLE */
87
88 #define SDF_FTYPE_2D    10      /* Two-dimensional data file */
89
90
91 /* BLOCK IDENTIFICATION
92  *
93  *   Name: Directory Header Block
94  *   Size: Multiple of BLK_SIZE
95  *
96  * DESCRIPTION
97  *    This is the first block of a sdf file.  It contains information on the 
98  *    size of the users' data plus the number and type of header blocks.
99  */
100
101 struct dir_blk_st {
102     int block_type;             /* required at the beginning of every block */
103                                 /* to identify type of block */
104
105     char id_str[4];             /* identification string == SDF_ID_STR */
106     int sdf_version;            /* which version of sdf system == SDF_VERSION */
107
108     int blk_size;               /* size of a header block in bytes */
109
110     int df_type;                /* type of data file, used as identification */
111                                 /* by application programs */
112
113     unsigned int num_dirblk;    /* number of directory header blocks */
114                                 /* currently, only 1 block long */
115      
116 /* label directory */
117
118      unsigned int start_label;  /* starting position of label header blocks */
119      unsigned int num_label;    /* number of label header blocks */
120
121 /* data header info */
122
123     unsigned int start_dhead;   /* block number of the start of the users' */
124                                 /* data header blocks */
125     unsigned int num_dhead;     /* number of data header blocks */
126     unsigned int dhead_len;     /* length of data header in bytes */
127
128 /* data record info */
129
130     unsigned int start_drec;    /* block number of start of data records */
131     unsigned int num_drec;      /* number of of user data records */
132     unsigned int drec_size;     /* size of users' record */
133     int num_dval;               /* number of values in each data record */
134     int dval_size;              /* size of data value in bytes */
135                                 /* NOTE:  drec_size = dval_size * num_dval */
136 };
137
138
139 /* BLOCK IDENTIFICATION
140  *
141  *    Name:  Label block
142  *    Size:  BLK_SIZE
143  *
144  * DESCRIPTION                              
145  *        This block contains both user and history labels.  Labels are alpha-
146  *     numeric.  History labels include time & date stamp plus calculation time.  
147  */
148
149 #define LABEL_LEN        159    /* size of alphanumeric label */
150 #define LABELS_PER_BLOCK  1
151                                 /* number of labels in each block */
152
153 struct label_blk_st {
154     int block_type;             /* required at the beginning of every block */
155     int label_type;             /* type of label */
156     char label_str[LABEL_LEN+1];/* alpha numeric label */
157
158 /* history label time stamping */
159
160     TIMEDATE timedate;          /* time and date of operation */
161     float calc_time;            /* calculation time in seconds */
162 };
163
164
165 /* BLOCK IDENTIFICATION
166  *
167  *      Name:  Data Header Block
168  *      Size:  Variable, but always integer multiple of BLK_SIZE
169  *
170  * DESCRIPTION
171  *        This block hold information entirely specific to the data in the sdf 
172  *      file, such as the user coordinates of the extent of an image, or the
173  *      the rotation increment of raysum data.  When an application program
174  *      reads or writes the data header, it passes the size in bytes of the
175  *      data header to be read or written.  If the length of a data header
176  *      being read doesn't match the length of the data header in the file,
177  *      the header is still read, but a WARNING message is given.
178  */
179
180
181
182 /* STRUCTURE IDENTIFICATION
183  *
184  *     Name:  Data File Structure       Contains all information on open files
185  *
186  * DESCRIPTION
187  *        This structure is generated by open_file() and create_file(), it is
188  *     passed to all subroutines of the sdf system.
189  */
190
191 struct sdfile_st {
192     bool open;                  /* if file is open or not */
193     int  mode;                  /* read-write mode */
194     int  fd;                    /* file descriptor gotten from C library */
195     bool error;                 /* TRUE if error occurred, all further */
196                                 /* procesing is stopped */
197     bool error_report;          /* Error reporting flag */
198
199     unsigned int blk_size;      /* size of a header block */
200     unsigned int num_dirblk;    /* number of directory header blocks */
201     char fname[MAXFULLNAME+1];  /* operating system filename */
202
203     int df_type;                /* identification type of data file */
204
205 /* variables that are used when writing to a file, they define what parts
206  * of the file have been written
207  */
208
209     struct wrt {
210         unsigned dir : 1;       /* 1 if directory header has been written */
211         unsigned label : 1;     /* 1 if labels have been written */
212         unsigned dhead : 1;     /* 1 if data header has been written */
213         unsigned drec : 1;      /* 1 when data records have been written */
214     } written;
215
216 /* Directory information to have about the file */
217
218     unsigned int start_label;   /* starting position of labels */
219     unsigned int num_label;     /* number of labels in file */
220
221     unsigned int dhead_len;     /* length of data header in bytes */
222
223     unsigned int num_drec;      /* number of data records */
224     unsigned int drec_size;     /* size of data records in bytes */
225     int num_dval;               /* number of values in each data record */
226     int dval_size;              /* size of data value in bytes */
227                                 /* NOTE:  drec_size = dval_size * num_dval */
228
229 /* ffset pointers into sdf file */
230
231     long int pos_dhead;         /* starting position of data header */
232     long int pos_drec;          /* starting position of users' data */
233 };
234
235
236 union sdf_blk_un {
237     char buf[BLK_SIZE];
238     struct dir_blk_st   dir;
239     struct label_blk_st lab;
240 };
241
242 typedef struct dir_blk_st       DIR_BLK;
243 typedef struct label_blk_st     LABEL_BLK;
244 typedef struct sdfile_st        SDFILE;
245 typedef union  sdf_blk_un       SDF_BLK;
246
247
248 /*----------------------------------------------------------------------*/
249 /*              2-Dimensional Standard Data File Header                 */
250 /*----------------------------------------------------------------------*/
251
252
253 /* STRUCTURE IDENTIFICATION
254  *
255  *      Name:  sdf_2d_dhead_st          Data header for 2d data files
256  *
257  * DESCRIPTION
258  *
259  *          This structure holds the data that is stored in an image sdf file's
260  *      data header.  It contains all data needed to use 2d data records
261  */
262
263 struct sdf_2d_dhead_st {
264         int file_type;          /* type of data making up this file */
265                                 /* currenty, DFT_2D_IMAGE or DFT_2D_RAYSUM */
266         int nx;                 /* number of rows */
267         int ny;                 /* number of columns in row */
268         int val_type;           /* type of pixel value, eg, DT_FLOAT */
269         int val_size;           /* size of pixel value in bytes */
270
271         bool axis_ext_kwn;      /* TRUE if picture extent is known */
272                                 /* stored in xmin,xmax,ymin,ymax */
273         double xmin, ymin;      /* position of lower left corner of picture */
274         double xmax, ymax;      /* position of upper right corner of picture */
275
276         bool axis_incr_kwn;     /* TRUE if increments are known */
277         double xinc, yinc;
278
279         bool val_ext_kwn;       /* TRUE if pixel extent is known */
280         double dvalmin, dvalmax;/* min & max data value for FLOAT & DOUBLE */
281         long lvalmin, lvalmax;  /* data extent for INT & LONG */
282 };
283
284
285 /* STRUCTURE IDENTIFICATION
286  *
287  *      Name:  sdf_2d_st                        Information about an sdf image
288  *
289  * DESCRIPTION
290  *
291  *          This structure holds all the information need to access an sdf
292  *      image.
293  */
294
295 struct sdf_2d_st {
296   struct sdf_2d_dhead_st dhead; /* image information that is stored in data header */
297   SDFILE *dfp;                  /* pointer to image's standard data file */
298   MTX *mtx;                     /* data values */
299   int memory_only; /* TRUE if sdf_2d file is stored in memory only */
300 };
301
302 typedef struct sdf_2d_dhead_st  SDF_2D_DHEAD;
303 typedef struct sdf_2d_st        SDF_2D;
304
305 /* types of 2d data files */
306
307 #define SDF_2D_IMAGE    100             /* data file type is an image */
308 #define SDF_2D_RAYSUM   101             /* raysum data file type */
309
310
311 /* sdf.c */
312 SDFILE *sdf_open(const char *filename, const int mode);
313 SDFILE *sdf_create(const char *filename, const int mode, const unsigned int num_drec, const unsigned int num_dval, const int dval_size, const int df_type);
314 int sdf_read_label(LABEL_BLK *lrec, const int nlab, SDFILE *dfp);
315 int sdf_write_label(LABEL_BLK *lrec, const int nlab, SDFILE *dfp);
316 int sdf_add_label(const int ltype, const char *lstr, const double calc_time, SDFILE *dfp);
317 int sdf_add_empty_label(SDFILE *dfp);
318 int sdf_copy_labels(SDFILE *dfp_to, SDFILE *dfp_from);
319 int sdf_read_dhead(void *dh_buf, const unsigned int dhead_len, SDFILE *dfp);
320 int sdf_write_dhead(const void *dh_buf, const unsigned int dhead_len, SDFILE *dfp);
321 int sdf_read_drec(void *drec, const int start_rec, const int num_rec, SDFILE *dfp);
322 int sdf_write_drec(const void *drec, const int start_rec, const int num_rec, SDFILE *dfp);
323 int sdf_read_dval(void *dval, const int rec, const int start_val, const int num_val, SDFILE *dfp);
324 int sdf_write_dval(const void *dval, const int rec, const int start_val, const int num_val, SDFILE *dfp);
325 int sdf_close(SDFILE *dfp);
326 void sdf_error_report(SDFILE *dfp, const int flag);
327 void sdf_error(const SDFILE *dfp, const char *str, ...);
328 bool sdf_check_dfp(const SDFILE *dfp, const int mode, const char *name);
329 void *sdf_alloc_blk(const int n, SDFILE *dfp, const char *name);
330 int sdf_free_blk(void *hb, SDFILE *dfp, const char *name);
331 /* sdf_2d.c */
332 SDF_2D *sdf_2d_open(const char *fname, const int mode);
333 SDF_2D *sdf_2d_create(const char *fname, const int mode, const int nx, const int ny, const int val_type, const int df_type);
334 int sdf_2d_save(SDF_2D *imp);
335 int sdf_2d_restore(SDF_2D *imp);
336 int sdf_2d_alloc_mtx(SDF_2D *imp);
337 int sdf_2d_free_mtx(SDF_2D *sdf_2d);
338 int sdf_2d_read_row(char *row_buf, const unsigned int row, SDF_2D *imp);
339 int sdf_2d_write_row(const char *row_buf, const unsigned int row, SDF_2D *imp);
340 int sdf_2d_read_col(char *col_buf, const unsigned int col, SDF_2D *imp);
341 int sdf_2d_write_col(const char *col_buf, const unsigned int col, SDF_2D *imp);
342 int sdf_2d_close(SDF_2D *imp);
343 char *sdf_2d_alloc_row(const int nrow, SDF_2D *imp);
344 char *sdf_2d_alloc_col(const int ncol, SDF_2D *imp);
345 void sdf_2d_error(const SDF_2D *imp, const char *str, ...);
346
347 #ifdef __cplusplus
348 }
349 #endif /* __cplusplus */
350
351 #endif