1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 typedef enum { ECAT6, ECAT7, Interfile } FileFormat;
5 
6 typedef enum {
7   MAT_OK,
8   MAT_READ_ERROR,
9   MAT_WRITE_ERROR,
10   MAT_INVALID_DIRBLK,
11   MAT_ACS_FILE_NOT_FOUND,
12   MAT_INTERFILE_OPEN_ERR,
13   MAT_FILE_TYPE_NOT_MATCH,
14   MAT_READ_FROM_NILFPTR,
15   MAT_NOMHD_FILE_OBJECT,
16   MAT_NIL_SHPTR,
17   MAT_NIL_DATA_PTR,
18   MAT_MATRIX_NOT_FOUND,
19   MAT_UNKNOWN_FILE_TYPE,
20   MAT_ACS_CREATE_ERR,
21   MAT_BAD_ATTRIBUTE,
22   MAT_BAD_FILE_ACCESS_MODE,
23   MAT_INVALID_DIMENSION,
24   MAT_NO_SLICES_FOUND,
25   MAT_INVALID_DATA_TYPE,
26   MAT_INVALID_MBED_POSITION
27 } MatrixErrorCode;
28 
29 typedef enum {
30 	NoData, Sinogram, PetImage, AttenCor, Normalization,
31 	PolarMap, ByteVolume, PetVolume, ByteProjection,
32 	PetProjection, ByteImage, Short3dSinogram, Byte3dSinogram, Norm3d,
33 	Float3dSinogram,InterfileImage, NumDataSetTypes
34 } DataSetType;
35 
36 typedef enum {
37   UnknownMatDataType, ByteData, VAX_Ix2, VAX_Ix4,
38   VAX_Rx4, IeeeFloat, SunShort, SunLong, NumMatrixDataTypes, ColorData,
39   BitData
40 } MatrixDataType;
41 
42 MatrixErrorCode matrix_errno;
43 
44 char matrix_errtxt[132];
45 
46 typedef struct XMAIN_HEAD {
47   char magic_number[14];
48   char original_file_name[32];
49   short sw_version;
50   short system_type;
51   short file_type;
52   char serial_number[10];
53   short align_0;						/* 4 byte alignment purpose */
54   unsigned int scan_start_time;
55   char isotope_code[8];
56   float isotope_halflife;
57   char radiopharmaceutical[32];
58   float gantry_tilt;
59   float gantry_rotation;
60   float bed_elevation;
61   float intrinsic_tilt;
62   short wobble_speed;
63   short transm_source_type;
64   float distance_scanned;
65   float transaxial_fov;
66   short angular_compression;
67   short coin_samp_mode;
68   short axial_samp_mode;
69   short align_1;
70   float calibration_factor;
71   short calibration_units;
72   short calibration_units_label;
73   short compression_code;
74   char study_name[12];
75   char patient_id[16];
76   char patient_name[32];
77   char patient_sex[1];
78   char patient_dexterity[1];
79   float patient_age;
80   float patient_height;
81   float patient_weight;
82   int patient_birth_date;
83   char physician_name[32];
84   char operator_name[32];
85   char study_description[32];
86   short acquisition_type;
87   short patient_orientation;
88   char facility_name[20];
89   short num_planes;
90   short num_frames;
91   short num_gates;
92   short num_bed_pos;
93   float init_bed_position;
94   float bed_offset[15];
95   float plane_separation;
96   short lwr_sctr_thres;
97   short lwr_true_thres;
98   short upr_true_thres;
99   char user_process_code[10];
100   short acquisition_mode;
101   short align_2;
102   float bin_size;
103   float branching_fraction;
104   unsigned int dose_start_time;
105   float dosage;
106   float well_counter_factor;
107   char data_units[32];
108   short septa_state;
109   short align_3;
110 } Main_header;
111 
112 typedef struct XIMAGE_SUB {
113   short data_type;
114   short num_dimensions;
115   short x_dimension;
116   short y_dimension;
117   short z_dimension;
118   short align_0;
119   float z_offset;
120   float x_offset;
121   float y_offset;
122   float recon_zoom;
123   float scale_factor;
124   short image_min;
125   short image_max;
126   float x_pixel_size;
127   float y_pixel_size;
128   float z_pixel_size;
129   unsigned int frame_duration;
130   unsigned int frame_start_time;
131   short filter_code;
132   short align_1;
133   float x_resolution;
134   float y_resolution;
135   float z_resolution;
136   float num_r_elements;
137   float num_angles;
138   float z_rotation_angle;
139   float decay_corr_fctr;
140   int processing_code;
141   unsigned int gate_duration;
142   int r_wave_offset;
143   int num_accepted_beats;
144   float filter_cutoff_frequency;
145   float filter_resolution;
146   float filter_ramp_slope;
147   short filter_order;
148   short align_2;
149   float filter_scatter_fraction;
150   float filter_scatter_slope;
151   char annotation[40];
152   float mt_1_1;
153   float mt_1_2;
154   float mt_1_3;
155   float mt_2_1;
156   float mt_2_2;
157   float mt_2_3;
158   float mt_3_1;
159   float mt_3_2;
160   float mt_3_3;
161   float rfilter_cutoff;
162   float rfilter_resolution;
163   short rfilter_code;
164   short rfilter_order;
165   float zfilter_cutoff;
166   float zfilter_resolution;
167   short zfilter_code;
168   short zfilter_order;
169   float mt_1_4;
170   float mt_2_4;
171   float mt_3_4;
172   short scatter_type;
173   short recon_type;
174   short recon_views;
175   short align_3;
176 } Image_subheader;
177 
178 typedef struct matdirnode {
179   int		matnum ;
180   int		strtblk ;
181   int		endblk ;
182   int		matstat ;
183   struct matdirnode *next ;
184 } MatDirNode ;
185 
186 typedef struct matdirlist {
187   int	nmats ;
188   MatDirNode *first ;
189   MatDirNode *last ;
190 } MatDirList ;
191 
192 typedef struct matrix_file {
193   char		*fname ;
194   Main_header	*mhptr ;
195   MatDirList	*dirlist ;
196   FILE		*fptr ;
197   int		acs ;
198   FileFormat file_format;
199   char **interfile_header;
200 } MatrixFile;
201 
202 typedef struct matrixdata {
203   int		matnum ;	/* matrix number */
204   MatrixFile	*matfile ;	/* pointer to parent */
205   DataSetType	mat_type ;	/* type of matrix? */
206   MatrixDataType	data_type ;	/* type of data */
207   void *	shptr ;		/* pointer to sub-header */
208   void *	data_ptr ;	/* pointer to data */
209   int		data_size ;	/* size of data in bytes */
210   int		xdim;		/* dimensions of data */
211   int		ydim;		/* y dimension */
212   int		zdim;		/* for volumes */
213   float		scale_factor ;	/* valid if data is int? */
214   float		pixel_size;	/* xdim data spacing (cm) */
215   float		y_size;		/* ydim data spacing (cm) */
216   float		z_size;		/* zdim data spacing (cm) */
217   float		data_min;	/* min value of data */
218   float		data_max;	/* max value of data */
219   float       x_origin;       /* x origin of data */
220   float       y_origin;       /* y origin of data */
221   float       z_origin;       /* z origin of data */
222 } MatrixData ;
223 
224 MatrixFile *matrix_create(const char *fname, Main_header * proto_mhptr);
225 int matrix_write(MatrixFile *mptr, int matnum, MatrixData *data);
226 int matrix_close(MatrixFile *mptr);
227 int mat_numcod(int frame, int plane, int gate, int data, int bed);
228