TPCCLIB
Loading...
Searching...
No Matches
ecat7r.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcimgio.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
17 FILE *fp,
20) {
21 unsigned char buf[MatBLKSIZE];
22 int little; /* 1 if current platform is little endian (i386), else 0 */
23 struct tm st;
24
25 if(ECAT7_TEST) printf("ecat7ReadMainheader()\n");
26 if(fp==NULL || h==NULL) return(1);
27 little=little_endian(); if(ECAT7_TEST) printf("little=%d\n", little);
28
29 /* Seek the first block */
30 fseek(fp, 0, SEEK_SET); if(ftell(fp)!=0) return(2);
31 /* Read the header block */
32 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
33
34 /* Copy the header fields and swap if necessary */
35 memcpy(&h->magic_number, buf+0, 14);
36 memcpy(&h->original_file_name, buf+14, 32);
37 if(little) swabip(buf+46, 2);
38 memcpy(&h->sw_version, buf+46, 2);
39 if(little) swabip(buf+48, 2);
40 memcpy(&h->system_type, buf+48, 2);
41 if(little) swabip(buf+50, 2);
42 memcpy(&h->file_type, buf+50, 2);
43 memcpy(&h->serial_number, buf+52, 10);
44 if(little) swawbip(buf+62, 4);
45 memcpy(&h->scan_start_time, buf+62, 4);
46 //printf("ecat7ReadMainheader(): scan_start_time := %d\n", h->scan_start_time);
47 memcpy(&h->isotope_name, buf+66, 8);
48 if(little) swawbip(buf+74, 4);
49 memcpy(&h->isotope_halflife, buf+74, 4);
50 memcpy(&h->radiopharmaceutical, buf+78, 32);
51 if(little) swawbip(buf+110, 4);
52 memcpy(&h->gantry_tilt, buf+110, 4);
53 if(little) swawbip(buf+114, 4);
54 memcpy(&h->gantry_rotation, buf+114, 4);
55 if(little) swawbip(buf+118, 4);
56 memcpy(&h->bed_elevation, buf+118, 4);
57 if(little) swawbip(buf+122, 4);
58 memcpy(&h->intrinsic_tilt, buf+122, 4);
59 if(little) swabip(buf+126, 2);
60 memcpy(&h->wobble_speed, buf+126, 2);
61 if(little) swabip(buf+128, 2);
62 memcpy(&h->transm_source_type, buf+128, 2);
63 if(little) swawbip(buf+130, 4);
64 memcpy(&h->distance_scanned, buf+130, 4);
65 if(little) swawbip(buf+134, 4);
66 memcpy(&h->transaxial_fov, buf+134, 4);
67 if(little) swabip(buf+138, 2);
68 memcpy(&h->angular_compression, buf+138, 2);
69 if(little) swabip(buf+140, 2);
70 memcpy(&h->coin_samp_mode, buf+140, 2);
71 if(little) swabip(buf+142, 2);
72 memcpy(&h->axial_samp_mode, buf+142, 2);
73 if(little) swawbip(buf+144, 4);
74 memcpy(&h->ecat_calibration_factor, buf+144, 4);
75 if(little) swabip(buf+148, 2);
76 memcpy(&h->calibration_units, buf+148, 2);
77 if(little) swabip(buf+150, 2);
78 memcpy(&h->calibration_units_label, buf+150, 2);
79 if(little) swabip(buf+152, 2);
80 memcpy(&h->compression_code, buf+152, 2);
81 memcpy(&h->study_type, buf+154, 12);
82 memcpy(&h->patient_id, buf+166, 16);
83 memcpy(&h->patient_name, buf+182, 32);
84 memcpy(&h->patient_sex, buf+214, 1);
85 memcpy(&h->patient_dexterity, buf+215, 1);
86 if(little) swawbip(buf+216, 4);
87 memcpy(&h->patient_age, buf+216, 4);
88 if(little) swawbip(buf+220, 4);
89 memcpy(&h->patient_height, buf+220, 4);
90 if(little) swawbip(buf+224, 4);
91 memcpy(&h->patient_weight, buf+224, 4);
92 if(little) swawbip(buf+228, 4);
93 memcpy(&h->patient_birth_date, buf+228, 4);
94 memcpy(&h->physician_name, buf+232, 32);
95 memcpy(&h->operator_name, buf+264, 32);
96 memcpy(&h->study_description, buf+296, 32);
97 if(little) swabip(buf+328, 2);
98 memcpy(&h->acquisition_type, buf+328, 2);
99 if(little) swabip(buf+330, 2);
100 memcpy(&h->patient_orientation, buf+330, 2);
101 memcpy(&h->facility_name, buf+332, 20);
102 if(little) swabip(buf+352, 2);
103 memcpy(&h->num_planes, buf+352, 2);
104 if(little) swabip(buf+354, 2);
105 memcpy(&h->num_frames, buf+354, 2);
106 if(little) swabip(buf+356, 2);
107 memcpy(&h->num_gates, buf+356, 2);
108 if(little) swabip(buf+358, 2);
109 memcpy(&h->num_bed_pos, buf+358, 2);
110 if(little) swawbip(buf+360, 4);
111 memcpy(&h->init_bed_position, buf+360, 4);
112 if(little) swawbip(buf+364, 15*4);
113 memcpy(h->bed_position, buf+364, 15*4);
114 if(little) swawbip(buf+424, 4);
115 memcpy(&h->plane_separation, buf+424, 4);
116 if(little) swabip(buf+428, 2);
117 memcpy(&h->lwr_sctr_thres, buf+428, 2);
118 if(little) swabip(buf+430, 2);
119 memcpy(&h->lwr_true_thres, buf+430, 2);
120 memcpy(&h->upr_true_thres, buf+432, 2); if(little) swabip(&h->upr_true_thres,2);
121 memcpy(&h->user_process_code, buf+434, 10);
122 if(little) swabip(buf+444, 2);
123 memcpy(&h->acquisition_mode, buf+444, 2);
124 if(little) swawbip(buf+446, 4);
125 memcpy(&h->bin_size, buf+446, 4);
126 if(little) swawbip(buf+450, 4);
127 memcpy(&h->branching_fraction, buf+450, 4);
128 if(little) swawbip(buf+454, 4);
129 memcpy(&h->dose_start_time, buf+454, 4);
130 if(little) swawbip(buf+458, 4);
131 memcpy(&h->dosage, buf+458, 4);
132 if(little) swawbip(buf+462, 4);
133 memcpy(&h->well_counter_corr_factor, buf+462,4);
134 memcpy(&h->data_units, buf+466, 32);
135 if(little) swabip(buf+498, 2);
136 memcpy(&h->septa_state, buf+498, 2);
137 memcpy(&h->fill_cti, buf+500, 12);
138
139 /* Patient birth date can have been saved in two different int formats,
140 either YYYYMMDD or as seconds from start of year 1970. In latter case
141 the number can be negative. */
142 /* Seconds from start of year 1970 are converted to YYYYMMDD format */
143 if(isdate4(h->patient_birth_date, NULL, NULL, NULL)!=0) {
144 time_t t=h->patient_birth_date; gmtime_r(&t, &st);
145 h->patient_birth_date=10000*(st.tm_year+1900)+100*(st.tm_mon+1)+st.tm_mday;
146 }
147
148 return(0);
149}
150/*****************************************************************************/
151
152/*****************************************************************************/
162int ecat7ReadImageheader(FILE *fp, int blk, ECAT7_imageheader *h) {
163 unsigned char buf[MatBLKSIZE];
164 int little; /* 1 if current platform is little endian (i386), else 0 */
165
166 if(ECAT7_TEST) printf("ecat7ReadImageheader()\n");
167 if(fp==NULL || h==NULL) return(1);
168 little=little_endian(); if(ECAT7_TEST) printf("little=%d\n", little);
169
170 /* Seek the subheader block */
171 long long pos=(blk-1)*(long long)MatBLKSIZE;
172 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
173 /* Read the header block */
174 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
175
176 /* Copy the header fields and swap if necessary */
177 if(little) swabip(buf+0, 2);
178 memcpy(&h->data_type, buf+0, 2);
179 if(little) swabip(buf+2, 2);
180 memcpy(&h->num_dimensions, buf+2, 2);
181 if(little) swabip(buf+4, 2);
182 memcpy(&h->x_dimension, buf+4, 2);
183 if(little) swabip(buf+6, 2);
184 memcpy(&h->y_dimension, buf+6, 2);
185 if(little) swabip(buf+8, 2);
186 memcpy(&h->z_dimension, buf+8, 2);
187 if(little) swawbip(buf+10, 4);
188 memcpy(&h->x_offset, buf+10, 4);
189 if(little) swawbip(buf+14, 4);
190 memcpy(&h->y_offset, buf+14, 4);
191 if(little) swawbip(buf+18, 4);
192 memcpy(&h->z_offset, buf+18, 4);
193 if(little) swawbip(buf+22, 4);
194 memcpy(&h->recon_zoom, buf+22, 4);
195 if(little) swawbip(buf+26, 4);
196 memcpy(&h->scale_factor, buf+26, 4);
197 if(little) swabip(buf+30, 2);
198 memcpy(&h->image_min, buf+30, 2);
199 if(little) swabip(buf+32, 2);
200 memcpy(&h->image_max, buf+32, 2);
201 if(little) swawbip(buf+34, 4);
202 memcpy(&h->x_pixel_size, buf+34, 4);
203 if(little) swawbip(buf+38, 4);
204 memcpy(&h->y_pixel_size, buf+38, 4);
205 if(little) swawbip(buf+42, 4);
206 memcpy(&h->z_pixel_size, buf+42, 4);
207 if(little) swawbip(buf+46, 4);
208 memcpy(&h->frame_duration, buf+46, 4);
209 if(little) swawbip(buf+50, 4);
210 memcpy(&h->frame_start_time, buf+50, 4);
211 if(little) swabip(buf+54, 2);
212 memcpy(&h->filter_code, buf+54, 2);
213 if(little) swawbip(buf+56, 4);
214 memcpy(&h->x_resolution, buf+56, 4);
215 if(little) swawbip(buf+60, 4);
216 memcpy(&h->y_resolution, buf+60, 4);
217 if(little) swawbip(buf+64, 4);
218 memcpy(&h->z_resolution, buf+64, 4);
219 if(little) swawbip(buf+68, 4);
220 memcpy(&h->num_r_elements, buf+68, 4);
221 if(little) swawbip(buf+72, 4);
222 memcpy(&h->num_angles, buf+72, 4);
223 if(little) swawbip(buf+76, 4);
224 memcpy(&h->z_rotation_angle, buf+76, 4);
225 if(little) swawbip(buf+80, 4);
226 memcpy(&h->decay_corr_fctr, buf+80, 4);
227 if(little) swawbip(buf+84, 4);
228 memcpy(&h->processing_code, buf+84, 4);
229 if(little) swawbip(buf+88, 4);
230 memcpy(&h->gate_duration, buf+88, 4);
231 if(little) swawbip(buf+92, 4);
232 memcpy(&h->r_wave_offset, buf+92, 4);
233 if(little) swawbip(buf+96, 4);
234 memcpy(&h->num_accepted_beats, buf+96, 4);
235 if(little) swawbip(buf+100, 4);
236 memcpy(&h->filter_cutoff_frequency, buf+100, 4);
237 if(little) swawbip(buf+104, 4);
238 memcpy(&h->filter_resolution, buf+104, 4);
239 if(little) swawbip(buf+108, 4);
240 memcpy(&h->filter_ramp_slope, buf+108, 4);
241 if(little) swabip(buf+112, 2);
242 memcpy(&h->filter_order, buf+112, 2);
243 if(little) swawbip(buf+114, 4);
244 memcpy(&h->filter_scatter_fraction, buf+114, 4);
245 if(little) swawbip(buf+118, 4);
246 memcpy(&h->filter_scatter_slope, buf+118, 4);
247 memcpy(&h->annotation, buf+122, 40);
248 if(little) swawbip(buf+162, 4);
249 memcpy(&h->mt_1_1, buf+162, 4);
250 if(little) swawbip(buf+166, 4);
251 memcpy(&h->mt_1_2, buf+166, 4);
252 if(little) swawbip(buf+170, 4);
253 memcpy(&h->mt_1_3, buf+170, 4);
254 if(little) swawbip(buf+174, 4);
255 memcpy(&h->mt_2_1, buf+174, 4);
256 if(little) swawbip(buf+178, 4);
257 memcpy(&h->mt_2_2, buf+178, 4);
258 if(little) swawbip(buf+182, 4);
259 memcpy(&h->mt_2_3, buf+182, 4);
260 if(little) swawbip(buf+186, 4);
261 memcpy(&h->mt_3_1, buf+186, 4);
262 if(little) swawbip(buf+190, 4);
263 memcpy(&h->mt_3_2, buf+190, 4);
264 if(little) swawbip(buf+194, 4);
265 memcpy(&h->mt_3_3, buf+194, 4);
266 if(little) swawbip(buf+198, 4);
267 memcpy(&h->rfilter_cutoff, buf+198, 4);
268 if(little) swawbip(buf+202, 4);
269 memcpy(&h->rfilter_resolution, buf+202, 4);
270 if(little) swabip(buf+206, 2);
271 memcpy(&h->rfilter_code, buf+206, 2);
272 if(little) swabip(buf+208, 2);
273 memcpy(&h->rfilter_order, buf+208, 2);
274 if(little) swawbip(buf+210, 4);
275 memcpy(&h->zfilter_cutoff, buf+210, 4);
276 if(little) swawbip(buf+214, 4);
277 memcpy(&h->zfilter_resolution, buf+214, 4);
278 if(little) swabip(buf+218, 2);
279 memcpy(&h->zfilter_code, buf+218, 2);
280 if(little) swabip(buf+220, 2);
281 memcpy(&h->zfilter_order, buf+220, 2);
282 if(little) swawbip(buf+222, 4);
283 memcpy(&h->mt_1_4, buf+222, 4);
284 if(little) swawbip(buf+226, 4);
285 memcpy(&h->mt_2_4, buf+226, 4);
286 if(little) swawbip(buf+230, 4);
287 memcpy(&h->mt_3_4, buf+230, 4);
288 if(little) swabip(buf+234, 2);
289 memcpy(&h->scatter_type, buf+234, 2);
290 if(little) swabip(buf+236, 2);
291 memcpy(&h->recon_type, buf+236, 2);
292 if(little) swabip(buf+238, 2);
293 memcpy(&h->recon_views, buf+238, 2);
294 memcpy(&h->fill_cti, buf+240, 174);
295 memcpy(&h->fill_user, buf+414, 96);
296
297 return(0);
298}
299/*****************************************************************************/
300
301/*****************************************************************************/
311int ecat7ReadAttenheader(FILE *fp, int blk, ECAT7_attenheader *h) {
312 unsigned char buf[MatBLKSIZE];
313 int little; /* 1 if current platform is little endian (i386), else 0 */
314
315 if(ECAT7_TEST) printf("ecat7ReadAttenheader()\n");
316 if(fp==NULL || h==NULL) return(1);
317 little=little_endian();
318
319 /* Seek the subheader block */
320 long long pos=(blk-1)*(long long)MatBLKSIZE;
321 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
322 /* Read the header block */
323 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
324 /* Copy the header fields and swap if necessary */
325 if(little) swabip(buf+0, 2);
326 memcpy(&h->data_type, buf+0, 2);
327 if(little) swabip(buf+2, 2);
328 memcpy(&h->num_dimensions, buf+2, 2);
329 if(little) swabip(buf+4, 2);
330 memcpy(&h->attenuation_type, buf+4, 2);
331 if(little) swabip(buf+6, 2);
332 memcpy(&h->num_r_elements, buf+6, 2);
333 if(little) swabip(buf+8, 2);
334 memcpy(&h->num_angles, buf+8, 2);
335 if(little) swabip(buf+10, 2);
336 memcpy(&h->num_z_elements, buf+10, 2);
337 if(little) swabip(buf+12, 2);
338 memcpy(&h->ring_difference, buf+12, 2);
339 if(little) swawbip(buf+14, 4);
340 memcpy(&h->x_resolution, buf+14, 4);
341 if(little) swawbip(buf+18, 4);
342 memcpy(&h->y_resolution, buf+18, 4);
343 if(little) swawbip(buf+22, 4);
344 memcpy(&h->z_resolution, buf+22, 4);
345 if(little) swawbip(buf+26, 4);
346 memcpy(&h->w_resolution, buf+26, 4);
347 if(little) swawbip(buf+30, 4);
348 memcpy(&h->scale_factor, buf+30, 4);
349 if(little) swawbip(buf+34, 4);
350 memcpy(&h->x_offset, buf+34, 4);
351 if(little) swawbip(buf+38, 4);
352 memcpy(&h->y_offset, buf+38, 4);
353 if(little) swawbip(buf+42, 4);
354 memcpy(&h->x_radius, buf+42, 4);
355 if(little) swawbip(buf+46, 4);
356 memcpy(&h->y_radius, buf+46, 4);
357 if(little) swawbip(buf+50, 4);
358 memcpy(&h->tilt_angle, buf+50, 4);
359 if(little) swawbip(buf+54, 4);
360 memcpy(&h->attenuation_coeff, buf+54, 4);
361 if(little) swawbip(buf+58, 4);
362 memcpy(&h->attenuation_min, buf+58, 4);
363 if(little) swawbip(buf+62, 4);
364 memcpy(&h->attenuation_max, buf+62, 4);
365 if(little) swawbip(buf+66, 4);
366 memcpy(&h->skull_thickness, buf+66, 4);
367 if(little) swabip(buf+70, 2);
368 memcpy(&h->num_additional_atten_coeff, buf+70, 2);
369 if(little) swawbip(buf+72, 8*4);
370 memcpy(h->additional_atten_coeff, buf+72, 8*4);
371 if(little) swawbip(buf+104, 4);
372 memcpy(&h->edge_finding_threshold, buf+104, 4);
373 if(little) swabip(buf+108, 2);
374 memcpy(&h->storage_order, buf+108, 2);
375 if(little) swabip(buf+110, 2);
376 memcpy(&h->span, buf+110, 2);
377 if(little) swabip(buf+112, 64*2);
378 memcpy(h->z_elements, buf+112, 64*2);
379 if(little) swabip(buf+240, 86*2);
380 memcpy(h->fill_cti, buf+240, 86*2);
381 if(little) swabip(buf+412, 50*2);
382 memcpy(h->fill_user, buf+412, 50*2);
383 return(0);
384}
385/*****************************************************************************/
386
387/*****************************************************************************/
397int ecat7ReadPolmapheader(FILE *fp, int blk, ECAT7_polmapheader *h) {
398 unsigned char buf[MatBLKSIZE];
399 int little; /* 1 if current platform is little endian (i386), else 0 */
400
401 if(ECAT7_TEST) printf("ecat7ReadPolarmapheader()\n");
402 if(fp==NULL || h==NULL) return(1);
403 little=little_endian();
404
405 /* Seek the subheader block */
406 long long pos=(blk-1)*(long long)MatBLKSIZE;
407 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
408 /* Read the header block */
409 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
410 /* Copy the header fields and swap if necessary */
411 if(little) swabip(buf+0, 2);
412 memcpy(&h->data_type, buf+0, 2);
413 if(little) swabip(buf+2, 2);
414 memcpy(&h->polar_map_type, buf+2, 2);
415 if(little) swabip(buf+4, 2);
416 memcpy(&h->num_rings, buf+4, 2);
417 if(little) swabip(buf+6, 32*2);
418 memcpy(h->sectors_per_ring, buf+6, 32*2);
419 if(little) swawbip(buf+70, 32*4);
420 memcpy(h->ring_position, buf+70, 32*4);
421 if(little) swabip(buf+198, 32*2);
422 memcpy(h->ring_angle, buf+198, 32*2);
423 if(little) swabip(buf+262, 2);
424 memcpy(&h->start_angle, buf+262, 2);
425 if(little) swabip(buf+264, 3*2);
426 memcpy(h->long_axis_left, buf+264, 3*2);
427 if(little) swabip(buf+270, 3*2);
428 memcpy(h->long_axis_right, buf+270, 3*2);
429 if(little) swabip(buf+276, 2);
430 memcpy(&h->position_data, buf+276, 2);
431 if(little) swabip(buf+278, 2);
432 memcpy(&h->image_min, buf+278, 2);
433 if(little) swabip(buf+280, 2);
434 memcpy(&h->image_max, buf+280, 2);
435 if(little) swawbip(buf+282, 4);
436 memcpy(&h->scale_factor, buf+282, 4);
437 if(little) swawbip(buf+286, 4);
438 memcpy(&h->pixel_size, buf+286, 4);
439 if(little) swawbip(buf+290, 4);
440 memcpy(&h->frame_duration, buf+290, 4);
441 if(little) swawbip(buf+294, 4);
442 memcpy(&h->frame_start_time, buf+294, 4);
443 if(little) swabip(buf+298, 2);
444 memcpy(&h->processing_code, buf+298, 2);
445 if(little) swabip(buf+300, 2);
446 memcpy(&h->quant_units, buf+300, 2);
447 memcpy(h->annotation, buf+302, 40);
448 if(little) swawbip(buf+342, 4);
449 memcpy(&h->gate_duration, buf+342, 4);
450 if(little) swawbip(buf+346, 4);
451 memcpy(&h->r_wave_offset, buf+346, 4);
452 if(little) swawbip(buf+350, 4);
453 memcpy(&h->num_accepted_beats, buf+350, 4);
454 memcpy(h->polar_map_protocol, buf+354, 20);
455 memcpy(h->database_name, buf+374, 30);
456 if(little) swabip(buf+404, 27*2);
457 memcpy(h->fill_cti, buf+404, 27*2);
458 return(0);
459}
460/*****************************************************************************/
461
462/*****************************************************************************/
472int ecat7ReadNormheader(FILE *fp, int blk, ECAT7_normheader *h) {
473 unsigned char buf[MatBLKSIZE];
474 int little; /* 1 if current platform is little endian (i386), else 0 */
475
476 if(ECAT7_TEST) printf("ecat7ReadNormheader()\n");
477 if(fp==NULL || h==NULL) return(1);
478 little=little_endian();
479
480 /* Seek the subheader block */
481 long long pos=(blk-1)*(long long)MatBLKSIZE;
482 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
483 /* Read the header block */
484 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
485 /* Copy the header fields and swap if necessary */
486 if(little) swabip(buf+0, 2);
487 memcpy(&h->data_type, buf+0, 2);
488 if(little) swabip(buf+2, 2);
489 memcpy(&h->num_r_elements, buf+2, 2);
490 if(little) swabip(buf+4, 2);
491 memcpy(&h->num_transaxial_crystals, buf+4, 2);
492 if(little) swabip(buf+6, 2);
493 memcpy(&h->num_crystal_rings, buf+6, 2);
494 if(little) swabip(buf+8, 2);
495 memcpy(&h->crystals_per_ring, buf+8, 2);
496 if(little) swabip(buf+10, 2);
497 memcpy(&h->num_geo_corr_planes, buf+10, 2);
498 if(little) swabip(buf+12, 2);
499 memcpy(&h->uld, buf+12, 2);
500 if(little) swabip(buf+14, 2);
501 memcpy(&h->lld, buf+14, 2);
502 if(little) swabip(buf+16, 2);
503 memcpy(&h->scatter_energy, buf+16, 2);
504 if(little) swawbip(buf+18, 4);
505 memcpy(&h->norm_quality_factor, buf+18, 4);
506 if(little) swabip(buf+22, 2);
507 memcpy(&h->norm_quality_factor_code, buf+22, 2);
508 if(little) swawbip(buf+24, 32*4);
509 memcpy(h->ring_dtcor1, buf+24, 32*4);
510 if(little) swawbip(buf+152, 32*4);
511 memcpy(h->ring_dtcor2, buf+152, 32*4);
512 if(little) swawbip(buf+280, 8*4);
513 memcpy(h->crystal_dtcor, buf+280, 8*4);
514 if(little) swabip(buf+312, 2);
515 memcpy(&h->span, buf+312, 2);
516 if(little) swabip(buf+314, 2);
517 memcpy(&h->max_ring_diff, buf+314, 2);
518 if(little) swabip(buf+316, 48*2);
519 memcpy(h->fill_cti, buf+316, 48*2);
520 if(little) swabip(buf+412, 50*2);
521 memcpy(h->fill_user, buf+412, 50*2);
522 return(0);
523}
524/*****************************************************************************/
525
526/*****************************************************************************/
536int ecat7ReadScanheader(FILE *fp, int blk, ECAT7_scanheader *h) {
537 unsigned char buf[2*MatBLKSIZE];
538 int little; /* 1 if current platform is little endian (i386), else 0 */
539
540 if(ECAT7_TEST) printf("ecat7ReadScanheader()\n");
541 if(fp==NULL || h==NULL) return(1);
542 little=little_endian(); if(ECAT7_TEST) printf("little=%d\n", little);
543
544 /* Seek the subheader block */
545 long long pos=(blk-1)*(long long)MatBLKSIZE;
546 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
547 /* Read the header block */
548 if(fread(buf, MatBLKSIZE, 2, fp)<1) return(3);
549
550 /* Copy the header fields and swap if necessary */
551 if(little) swabip(buf+0, 2);
552 memcpy(&h->data_type, buf+0, 2);
553 if(little) swabip(buf+2, 2);
554 memcpy(&h->num_dimensions, buf+2, 2);
555 if(little) swabip(buf+4, 2);
556 memcpy(&h->num_r_elements, buf+4, 2);
557 if(little) swabip(buf+6, 2);
558 memcpy(&h->num_angles, buf+6, 2);
559 if(little) swabip(buf+8, 2);
560 memcpy(&h->corrections_applied, buf+8, 2);
561 if(little) swabip(buf+10, 64*2);
562 memcpy(h->num_z_elements, buf+10, 64*2);
563 if(little) swabip(buf+138, 2);
564 memcpy(&h->ring_difference, buf+138, 2);
565 if(little) swabip(buf+140, 2);
566 memcpy(&h->storage_order, buf+140, 2);
567 if(little) swabip(buf+142, 2);
568 memcpy(&h->axial_compression, buf+142, 2);
569 if(little) swawbip(buf+144, 4);
570 memcpy(&h->x_resolution, buf+144, 4);
571 if(little) swawbip(buf+148, 4);
572 memcpy(&h->v_resolution, buf+148, 4);
573 if(little) swawbip(buf+152, 4);
574 memcpy(&h->z_resolution, buf+152, 4);
575 if(little) swawbip(buf+156, 4);
576 memcpy(&h->w_resolution, buf+156, 4);
577 if(little) swabip(buf+160, 6*2);
578 memcpy(h->fill_gate, buf+160, 6*2);
579 if(little) swawbip(buf+172, 4);
580 memcpy(&h->gate_duration, buf+172, 4);
581 if(little) swawbip(buf+176, 4);
582 memcpy(&h->r_wave_offset, buf+176, 4);
583 if(little) swawbip(buf+180, 4);
584 memcpy(&h->num_accepted_beats, buf+180, 4);
585 if(little) swawbip(buf+184, 4);
586 memcpy(&h->scale_factor, buf+184, 4);
587 if(little) swabip(buf+188, 2);
588 memcpy(&h->scan_min, buf+188, 2);
589 if(little) swabip(buf+190, 2);
590 memcpy(&h->scan_max, buf+190, 2);
591 if(little) swawbip(buf+192, 4);
592 memcpy(&h->prompts, buf+192, 4);
593 if(little) swawbip(buf+196, 4);
594 memcpy(&h->delayed, buf+196, 4);
595 if(little) swawbip(buf+200, 4);
596 memcpy(&h->multiples, buf+200, 4);
597 if(little) swawbip(buf+204, 4);
598 memcpy(&h->net_trues, buf+204, 4);
599 if(little) swawbip(buf+208, 4);
600 memcpy(&h->tot_avg_cor, buf+208, 4);
601 if(little) swawbip(buf+212, 4);
602 memcpy(&h->tot_avg_uncor, buf+212, 4);
603 if(little) swawbip(buf+216, 4);
604 memcpy(&h->total_coin_rate, buf+216, 4);
605 if(little) swawbip(buf+220, 4);
606 memcpy(&h->frame_start_time, buf+220, 4);
607 if(little) swawbip(buf+224, 4);
608 memcpy(&h->frame_duration, buf+224, 4);
609 if(little) swawbip(buf+228, 4);
610 memcpy(&h->deadtime_correction_factor, buf+228,4);
611 if(little) swabip(buf+232, 90*2);
612 memcpy(h->fill_cti, buf+232, 90*2);
613 if(little) swabip(buf+412, 50*2);
614 memcpy(h->fill_user, buf+412, 50*2);
615 if(little) swawbip(buf+512, 128*4);
616 memcpy(h->uncor_singles, buf+512, 128*4);
617 return(0);
618}
619/*****************************************************************************/
620
621/*****************************************************************************/
631int ecat7Read2DScanheader(FILE *fp, int blk, ECAT7_2Dscanheader *h) {
632 unsigned char buf[MatBLKSIZE];
633 int little; /* 1 if current platform is little endian (i386), else 0 */
634
635 if(ECAT7_TEST) printf("ecat7Read2DScanheader()\n");
636 if(fp==NULL || h==NULL) return(1);
637 little=little_endian();
638
639 /* Seek the subheader block */
640 long long pos=(blk-1)*(long long)MatBLKSIZE;
641 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
642 /* Read the header block */
643 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
644 /* Copy the header fields and swap if necessary */
645 if(little) swabip(buf+0, 2);
646 memcpy(&h->data_type, buf+0, 2);
647 if(little) swabip(buf+2, 2);
648 memcpy(&h->num_dimensions, buf+2, 2);
649 if(little) swabip(buf+4, 2);
650 memcpy(&h->num_r_elements, buf+4, 2);
651 if(little) swabip(buf+6, 2);
652 memcpy(&h->num_angles, buf+6, 2);
653 if(little) swabip(buf+8, 2);
654 memcpy(&h->corrections_applied, buf+8, 2);
655 if(little) swabip(buf+10, 2);
656 memcpy(&h->num_z_elements, buf+10, 2);
657 if(little) swabip(buf+12, 2);
658 memcpy(&h->ring_difference, buf+12, 2);
659 if(little) swawbip(buf+14, 4);
660 memcpy(&h->x_resolution, buf+14, 4);
661 if(little) swawbip(buf+18, 4);
662 memcpy(&h->y_resolution, buf+18, 4);
663 if(little) swawbip(buf+22, 4);
664 memcpy(&h->z_resolution, buf+22, 4);
665 if(little) swawbip(buf+26, 4);
666 memcpy(&h->w_resolution, buf+26, 4);
667 if(little) swabip(buf+30, 6*2);
668 memcpy(h->fill_gate, buf+30, 6*2);
669 if(little) swawbip(buf+42, 4);
670 memcpy(&h->gate_duration, buf+42, 4);
671 if(little) swawbip(buf+46, 4);
672 memcpy(&h->r_wave_offset, buf+46, 4);
673 if(little) swawbip(buf+50, 4);
674 memcpy(&h->num_accepted_beats, buf+50, 4);
675 if(little) swawbip(buf+54, 4);
676 memcpy(&h->scale_factor, buf+54, 4);
677 if(little) swabip(buf+58, 2);
678 memcpy(&h->scan_min, buf+58, 2);
679 if(little) swabip(buf+60, 2);
680 memcpy(&h->scan_max, buf+60, 2);
681 if(little) swawbip(buf+62, 4);
682 memcpy(&h->prompts, buf+62, 4);
683 if(little) swawbip(buf+66, 4);
684 memcpy(&h->delayed, buf+66, 4);
685 if(little) swawbip(buf+70, 4);
686 memcpy(&h->multiples, buf+70, 4);
687 if(little) swawbip(buf+74, 4);
688 memcpy(&h->net_trues, buf+74, 4);
689 if(little) swawbip(buf+78, 16*4);
690 memcpy(h->cor_singles, buf+78, 16*4);
691 if(little) swawbip(buf+142, 16*4);
692 memcpy(h->uncor_singles, buf+142, 16*4);
693 if(little) swawbip(buf+206, 4);
694 memcpy(&h->tot_avg_cor, buf+206, 4);
695 if(little) swawbip(buf+210, 4);
696 memcpy(&h->tot_avg_uncor, buf+210, 4);
697 if(little) swawbip(buf+214, 4);
698 memcpy(&h->total_coin_rate, buf+214, 4);
699 if(little) swawbip(buf+218, 4);
700 memcpy(&h->frame_start_time, buf+218, 4);
701 if(little) swawbip(buf+222, 4);
702 memcpy(&h->frame_duration, buf+222, 4);
703 if(little) swawbip(buf+226, 4);
704 memcpy(&h->deadtime_correction_factor, buf+226,4);
705 if(little) swabip(buf+230, 8*2);
706 memcpy(h->physical_planes, buf+230, 8*2);
707 if(little) swabip(buf+246, 83*2);
708 memcpy(h->fill_cti, buf+246, 83*2);
709 if(little) swabip(buf+412, 50*2);
710 memcpy(h->fill_user, buf+412, 50*2);
711 return(0);
712}
713/*****************************************************************************/
714
715/*****************************************************************************/
725int ecat7Read2DNormheader(FILE *fp, int blk, ECAT7_2Dnormheader *h) {
726 unsigned char buf[MatBLKSIZE];
727 int little; /* 1 if current platform is little endian (i386), else 0 */
728
729 if(ECAT7_TEST) printf("ecat7Read2Dnormheader()\n");
730 if(fp==NULL || h==NULL) return(1);
731 little=little_endian();
732
733 /* Seek the subheader block */
734 long long pos=(blk-1)*(long long)MatBLKSIZE;
735 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(2);
736 /* Read the header block */
737 if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3);
738 /* Copy the header fields and swap if necessary */
739 if(little) swabip(buf+0, 2);
740 memcpy(&h->data_type, buf+0, 2);
741 if(little) swabip(buf+2, 2);
742 memcpy(&h->num_dimensions, buf+2, 2);
743 if(little) swabip(buf+4, 2);
744 memcpy(&h->num_r_elements, buf+4, 2);
745 if(little) swabip(buf+6, 2);
746 memcpy(&h->num_angles, buf+6, 2);
747 if(little) swabip(buf+8, 2);
748 memcpy(&h->num_z_elements, buf+8, 2);
749 if(little) swabip(buf+10, 2);
750 memcpy(&h->ring_difference, buf+10, 2);
751 if(little) swawbip(buf+12, 4);
752 memcpy(&h->scale_factor, buf+12, 4);
753 if(little) swawbip(buf+16, 4);
754 memcpy(&h->norm_min, buf+16, 4);
755 if(little) swawbip(buf+20, 4);
756 memcpy(&h->norm_max, buf+20, 4);
757 if(little) swawbip(buf+24, 4);
758 memcpy(&h->fov_source_width, buf+24, 4);
759 if(little) swawbip(buf+28, 4);
760 memcpy(&h->norm_quality_factor, buf+28, 4);
761 if(little) swabip(buf+32, 2);
762 memcpy(&h->norm_quality_factor_code, buf+32, 2);
763 if(little) swabip(buf+34, 2);
764 memcpy(&h->storage_order, buf+34, 2);
765 if(little) swabip(buf+36, 2);
766 memcpy(&h->span, buf+36, 2);
767 if(little) swabip(buf+38, 64*2);
768 memcpy(h->fill_cti, buf+38, 64*2);
769 if(little) swabip(buf+166, 123*2);
770 memcpy(h->fill_cti, buf+166, 123*2);
771 if(little) swabip(buf+412, 50*2);
772 memcpy(h->fill_user, buf+412, 50*2);
773 return(0);
774}
775/*****************************************************************************/
776
777/*****************************************************************************/
792 FILE *fp, int start_block, int block_nr, char *data, int dtype
793) {
794 int i, n, little, err=0;
795 char *cptr;
796 float f;
797
798 if(ECAT7_TEST) printf("ecat7ReadMatrixdata(fp, %d, %d, data, %d)\n",
799 start_block, block_nr, dtype);
800 /* Check the arguments */
801 if(block_nr<=0 || start_block<1 || data==NULL) return(1);
802 /* Seek the first data block */
803 long long pos=(start_block-1)*(long long)MatBLKSIZE;
804 fseeko(fp, pos, SEEK_SET); if(ftello(fp)!=pos) return(9);
805 /* Read the data blocks */
806 if(fread(data, MatBLKSIZE, block_nr, fp) < (unsigned int)block_nr) return(2);
807 /* Translate data if necessary */
808 little=little_endian();
809 switch(dtype) {
810 case ECAT7_BYTE: /* byte format...no translation necessary */
811 break;
812 case ECAT7_VAXI2: /* byte conversion necessary on big endian platform */
813 if(!little) {cptr=data; swabip(cptr, block_nr*MatBLKSIZE);}
814 break;
815 case ECAT7_VAXI4:
816 for(i=0, cptr=data; i<block_nr*MatBLKSIZE; i+=4, cptr+=4) {
817 n=ecat7rInt(cptr, 1, little); memcpy(cptr, &n, 4);
818 }
819 break;
820 case ECAT7_VAXR4:
821 for(i=0, cptr=data; i<block_nr*MatBLKSIZE; i+=4, cptr+=4) {
822 f=ecat7rFloat(cptr, 1, little); memcpy(cptr, &f, 4);
823 }
824 break;
825 case ECAT7_IEEER4: /* IEEE float ; byte conversion necessary on
826 little endian platforms */
827 case ECAT7_SUNI4: /* SUN int ; byte conversion necessary on
828 little endian platforms */
829 if(little) swawbip(data, block_nr*MatBLKSIZE);
830 break;
831 case ECAT7_SUNI2: /* SUN short ; byte conversion necessary on
832 little endian platforms */
833 if(little) swabip(data, block_nr*MatBLKSIZE);
834 break;
835 default: /* if something else, for now think it as an error */
836 err=2;
837 break;
838 }
839 return(err);
840}
841/*****************************************************************************/
842
843/*****************************************************************************/
859 FILE *fp, int first_block, int last_block, ECAT7_imageheader *h, float **fdata
860) {
861 int ret;
862 long long i, blockNr, pxlNr;
863 char *mdata, *mptr;
864 float *_fdata, *fptr;
865 short int *sptr;
866 int *iptr;
867
868
869 if(ECAT7_TEST) printf("ecat7ReadImageMatrix(fp, %d, %d, hdr, fdata)\n",
870 first_block, last_block);
871 if(fp==NULL || first_block<=MatFirstDirBlk || h==NULL) {
872 sprintf(ecat7errmsg, "invalid function parameter.\n");
873 return(1);
874 }
875 *fdata=(float*)NULL;
876
877 /* Read subheader */
878 ret=ecat7ReadImageheader(fp, first_block, h);
879 if(ret) {
880 sprintf(ecat7errmsg, "cannot read subheader (%d).\n", ret);
881 return(5);
882 }
883 if(ECAT7_TEST>4) ecat7PrintImageheader(h, stdout);
884 pxlNr=h->x_dimension*h->y_dimension;
885 if(h->num_dimensions>2) pxlNr*=h->z_dimension;
886 if(pxlNr<=0) {
887 sprintf(ecat7errmsg, "invalid matrix dimension.\n");
888 return(6);
889 }
890
891 /* Read matrix data */
892 blockNr=last_block-first_block; if(blockNr<1) return(0);
893 mdata=(char*)malloc((size_t)blockNr*MatBLKSIZE);
894 if(mdata==NULL) {
895 sprintf(ecat7errmsg, "cannot allocate memory.\n");
896 return(8);
897 }
898 mptr=mdata;
899 ret=ecat7ReadMatrixdata(fp, first_block+1, blockNr, mptr, h->data_type);
900 if(ret || mdata==NULL) {
901 sprintf(ecat7errmsg, "cannot read matrix data (%d).\n", ret);
902 free(mdata); return(9);
903 }
904
905 /* Allocate memory for float data */
906 _fdata=(float*)malloc(pxlNr*sizeof(float));
907 if(_fdata==NULL) {
908 sprintf(ecat7errmsg, "cannot allocate memory.\n");
909 free(mdata); return(11);
910 }
911
912 /* Convert matrix data to floats */
913 fptr=_fdata; mptr=mdata;
914 if(h->data_type==ECAT7_BYTE) {
915 for(i=0; i<pxlNr; i++, mptr++, fptr++)
916 *fptr=h->scale_factor*(float)(*mptr);
917 } else if(h->data_type==ECAT7_VAXI2 || h->data_type==ECAT7_SUNI2) {
918 for(i=0; i<pxlNr; i++, mptr+=2, fptr++) {
919 sptr=(short int*)mptr;
920 *fptr=h->scale_factor*(float)(*sptr);
921 if(!(*fptr>-1.0E+22 && *fptr<1.0E+22)) *fptr=0.0;
922 }
923 } else if(h->data_type==ECAT7_VAXI4 || h->data_type==ECAT7_SUNI4) {
924 for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
925 iptr=(int*)mptr;
926 *fptr=h->scale_factor*(float)(*iptr);
927 if(!(*fptr>-1.0E+22 && *fptr<1.0E+22)) *fptr=0.0;
928 }
929 } else if(h->data_type==ECAT7_VAXR4 || h->data_type==ECAT7_IEEER4) {
930 memcpy(fptr, mptr, pxlNr*4);
931 for(i=0; i<pxlNr; i++, fptr++) {
932 *fptr *= h->scale_factor;
933 if(!(*fptr>-1.0E+22 && *fptr<1.0E+22)) *fptr=0.0;
934 }
935 }
936 free(mdata);
937 *fdata=_fdata;
938
939 return(0);
940}
941/*****************************************************************************/
942
943/*****************************************************************************/
959int ecat7Read2DScanMatrix(FILE *fp, int first_block, int last_block,
960 ECAT7_2Dscanheader *h, float **fdata) {
961 int ret;
962 long long i, blockNr, pxlNr;
963 char *mdata, *mptr;
964 float *_fdata, *fptr;
965 short int *sptr;
966 int *iptr;
967
968
969 if(ECAT7_TEST) printf("ecat7Read2DScanMatrix(fp, %d, %d, hdr, fdata)\n",
970 first_block, last_block);
971 if(fp==NULL || first_block<=MatFirstDirBlk || h==NULL) {
972 sprintf(ecat7errmsg, "invalid function parameter.\n");
973 return(1);
974 }
975 *fdata=(float*)NULL;
976
977 /* Read subheader */
978 ret=ecat7Read2DScanheader(fp, first_block, h);
979 if(ret) {
980 sprintf(ecat7errmsg, "cannot read subheader (%d).\n", ret);
981 return(5);
982 }
983 if(ECAT7_TEST>4) ecat7Print2DScanheader(h, stdout);
984 pxlNr=h->num_r_elements*h->num_angles;
985 if(h->num_dimensions>2) pxlNr*=h->num_z_elements;
986 if(pxlNr<=0) {
987 sprintf(ecat7errmsg, "invalid matrix dimension.\n");
988 return(6);
989 }
990
991 /* Read matrix data */
992 blockNr=last_block-first_block; if(blockNr<1) return(0);
993 mdata=(char*)malloc((size_t)blockNr*MatBLKSIZE);
994 if(mdata==NULL) {
995 sprintf(ecat7errmsg, "cannot allocate memory.\n");
996 return(8);
997 }
998 mptr=mdata;
999 ret=ecat7ReadMatrixdata(fp, first_block+1, blockNr, mptr, h->data_type);
1000 if(ret || mdata==NULL) {
1001 sprintf(ecat7errmsg, "cannot read matrix data (%d).\n", ret);
1002 free(mdata); return(9);
1003 }
1004
1005 /* Allocate memory for float data */
1006 _fdata=(float*)malloc(pxlNr*sizeof(float));
1007 if(_fdata==NULL) {
1008 sprintf(ecat7errmsg, "cannot allocate memory.\n");
1009 free(mdata); return(11);
1010 }
1011
1012 /* Convert matrix data to floats */
1013 fptr=_fdata; mptr=mdata;
1014 if(h->data_type==ECAT7_BYTE) {
1015 for(i=0; i<pxlNr; i++, mptr++, fptr++)
1016 *fptr=h->scale_factor*(float)(*mptr);
1017 } else if(h->data_type==ECAT7_VAXI2 || h->data_type==ECAT7_SUNI2) {
1018 for(i=0; i<pxlNr; i++, mptr+=2, fptr++) {
1019 sptr=(short int*)mptr;
1020 *fptr=h->scale_factor*(float)(*sptr);
1021 }
1022 } else if(h->data_type==ECAT7_VAXI4 || h->data_type==ECAT7_SUNI4) {
1023 for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
1024 iptr=(int*)mptr;
1025 *fptr=h->scale_factor*(float)(*iptr);
1026 }
1027 } else if(h->data_type==ECAT7_VAXR4 || h->data_type==ECAT7_IEEER4) {
1028 memcpy(fptr, mptr, pxlNr*4);
1029 for(i=0; i<pxlNr; i++, fptr++) *fptr *= h->scale_factor;
1030 }
1031 free(mdata);
1032 *fdata=_fdata;
1033
1034 return(0);
1035}
1036/*****************************************************************************/
1037
1038/*****************************************************************************/
1056 FILE *fp, int first_block, int last_block, ECAT7_scanheader *h, float **fdata
1057) {
1058 int ret;
1059 long long i, blockNr, trueblockNr, pxlNr, dimz;
1060 char *mdata, *mptr;
1061 float *_fdata, *fptr;
1062 short int *sptr;
1063 int *iptr;
1064
1065
1066 if(ECAT7_TEST) printf("ecat7ReadScanMatrix(fp, %d, %d, hdr, fdata)\n",
1067 first_block, last_block);
1068 if(fp==NULL || first_block<=MatFirstDirBlk || h==NULL) {
1069 sprintf(ecat7errmsg, "invalid function parameter.\n");
1070 return(1);
1071 }
1072 *fdata=(float*)NULL;
1073
1074 /* Read subheader */
1075 ret=ecat7ReadScanheader(fp, first_block, h);
1076 if(ret) {
1077 sprintf(ecat7errmsg, "cannot read subheader (%d).\n", ret);
1078 return(5);
1079 }
1080 if(ECAT7_TEST>4) ecat7PrintScanheader(h, stdout);
1081 pxlNr=h->num_r_elements*h->num_angles;
1082 for(i=dimz=0; i<64; i++) dimz+=h->num_z_elements[i];
1083 pxlNr*=dimz;
1084 if(pxlNr<=0) {
1085 sprintf(ecat7errmsg, "invalid matrix dimension.\n");
1086 return(6);
1087 }
1088 trueblockNr=pxlNr*ecat7pxlbytes(h->data_type);
1089 trueblockNr=(trueblockNr+MatBLKSIZE-1)/MatBLKSIZE;
1090
1091 /* Read matrix data; note that header takes 2 blocks */
1092 blockNr=last_block-first_block-1; if(blockNr<1) return(0);
1093 if(blockNr<trueblockNr) trueblockNr=blockNr;
1094 mdata=(char*)malloc((size_t)blockNr*MatBLKSIZE);
1095 if(mdata==NULL) {
1096 sprintf(ecat7errmsg, "cannot allocate memory.\n");
1097 return(8);
1098 }
1099 mptr=mdata; /* note that only true block nr is read! */
1100 ret=ecat7ReadMatrixdata(fp, first_block+2, trueblockNr, mptr, h->data_type);
1101 if(ret || mdata==NULL) {
1102 sprintf(ecat7errmsg, "cannot read matrix data (%d).\n", ret);
1103 free(mdata); return(9);
1104 }
1105
1106 /* Allocate memory for float data */
1107 _fdata=(float*)malloc(pxlNr*sizeof(float));
1108 if(_fdata==NULL) {
1109 sprintf(ecat7errmsg, "cannot allocate memory.\n");
1110 free(mdata); return(11);
1111 }
1112
1113 /* Convert matrix data to floats */
1114 fptr=_fdata; mptr=mdata;
1115 if(h->data_type==ECAT7_BYTE) {
1116 for(i=0; i<pxlNr; i++, mptr++, fptr++)
1117 *fptr=h->scale_factor*(float)(*mptr);
1118 } else if(h->data_type==ECAT7_VAXI2 || h->data_type==ECAT7_SUNI2) {
1119 for(i=0; i<pxlNr; i++, mptr+=2, fptr++) {
1120 sptr=(short int*)mptr;
1121 *fptr=h->scale_factor*(float)(*sptr);
1122 }
1123 } else if(h->data_type==ECAT7_VAXI4 || h->data_type==ECAT7_SUNI4) {
1124 for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
1125 iptr=(int*)mptr;
1126 *fptr=h->scale_factor*(float)(*iptr);
1127 }
1128 } else if(h->data_type==ECAT7_VAXR4 || h->data_type==ECAT7_IEEER4) {
1129 memcpy(fptr, mptr, pxlNr*4);
1130 for(i=0; i<pxlNr; i++, fptr++) *fptr *= h->scale_factor;
1131 }
1132 free(mdata);
1133 *fdata=_fdata;
1134
1135 return(0);
1136}
1137/*****************************************************************************/
1138
1139/*****************************************************************************/
1155 FILE *fp, int first_block, int last_block, ECAT7_polmapheader *h, float **fdata
1156) {
1157 int ret;
1158 long long i, blockNr, pxlNr;
1159 char *mdata, *mptr;
1160 float *_fdata, *fptr;
1161 short int *sptr;
1162 int *iptr;
1163
1164
1165 if(ECAT7_TEST) printf("ecat7ReadPolarmapMatrix(fp, %d, %d, hdr, fdata)\n",
1166 first_block, last_block);
1167 if(fp==NULL || first_block<=MatFirstDirBlk || h==NULL) return 1;
1168 *fdata=(float*)NULL;
1169
1170 /* Read subheader */
1171 ret=ecat7ReadPolmapheader(fp, first_block, h);
1172 if(ret) {
1173 sprintf(ecat7errmsg, "cannot read subheader (%d).\n", ret);
1174 return 2;
1175 }
1176 if(ECAT7_TEST>4) ecat7PrintPolmapheader(h, stdout);
1177 for(i=pxlNr=0; i<h->num_rings; i++) pxlNr+=h->sectors_per_ring[i];
1178 if(pxlNr<=0) return 3;
1179
1180 /* Read matrix data */
1181 blockNr=last_block-first_block; if(blockNr<1) return 0;
1182 mdata=(char*)malloc((size_t)blockNr*MatBLKSIZE);
1183 if(mdata==NULL) return 4;
1184 mptr=mdata;
1185 ret=ecat7ReadMatrixdata(fp, first_block+1, blockNr, mptr, h->data_type);
1186 if(ret || mdata==NULL) {
1187 if(mdata!=NULL) free(mdata);
1188 return 5;
1189 }
1190
1191 /* Allocate memory for float data */
1192 _fdata=(float*)malloc(pxlNr*sizeof(float));
1193 if(_fdata==NULL) {
1194 sprintf(ecat7errmsg, "cannot allocate memory.\n");
1195 free(mdata); return 4;
1196 }
1197
1198 /* Convert matrix data to floats */
1199 fptr=_fdata; mptr=mdata;
1200 if(h->data_type==ECAT7_BYTE) {
1201 for(i=0; i<pxlNr; i++, mptr++, fptr++)
1202 *fptr=h->scale_factor*(float)(*mptr);
1203 } else if(h->data_type==ECAT7_VAXI2 || h->data_type==ECAT7_SUNI2) {
1204 for(i=0; i<pxlNr; i++, mptr+=2, fptr++) {
1205 sptr=(short int*)mptr;
1206 *fptr=h->scale_factor*(float)(*sptr);
1207 }
1208 } else if(h->data_type==ECAT7_VAXI4 || h->data_type==ECAT7_SUNI4) {
1209 for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
1210 iptr=(int*)mptr;
1211 *fptr=h->scale_factor*(float)(*iptr);
1212 }
1213 } else if(h->data_type==ECAT7_VAXR4 || h->data_type==ECAT7_IEEER4) {
1214 memcpy(fptr, mptr, pxlNr*4);
1215 for(i=0; i<pxlNr; i++, fptr++) *fptr *= h->scale_factor;
1216 }
1217 free(mdata);
1218 *fdata=_fdata;
1219
1220 return 0;
1221}
1222/*****************************************************************************/
1223
1224/*****************************************************************************/
1233float ecat7rFloat(void *bufi, int isvax, int islittle) {
1234 union {unsigned int ul; float f;} t;
1235
1236 memcpy(&t.ul, bufi, 4); if(t.ul==0) {return(0.0);}
1237 if(isvax) { /* if input is in VAX format */
1238 /* Swap words on i386 and bytes on SUN */
1239 if(islittle) swawip(&t.ul, 4); else swabip(&t.ul, 4);
1240 t.ul-=(2L<<23); /* subtract 2 from exp */
1241 } else { /* input is in i386 format */
1242 if(!islittle) swawbip(&t.ul, 4); /* Switch words and bytes on SUN */
1243 }
1244 return(t.f);
1245}
1246
1256int ecat7rInt(void *bufi, int isvax, int islittle) {
1257 int i;
1258
1259 if(isvax==0) {} // prevent compiler warning
1260 /* Swap both words and bytes on SUN */
1261 memcpy(&i, bufi, 4); if(!islittle) swawbip(&i, 4);
1262 return(i);
1263}
1264/*****************************************************************************/
1265
1266/*****************************************************************************/
1274int ecat7pxlbytes(short int data_type) {
1275 int byteNr=0;
1276 switch(data_type) {
1277 case ECAT7_BYTE: byteNr=1; break;
1278 case ECAT7_VAXI2:
1279 case ECAT7_SUNI2: byteNr=2; break;
1280 case ECAT7_VAXI4:
1281 case ECAT7_VAXR4:
1282 case ECAT7_IEEER4:
1283 case ECAT7_SUNI4: byteNr=4; break;
1284 }
1285 return(byteNr);
1286}
1287/*****************************************************************************/
1288
1289/*****************************************************************************/
int isdate4(int dateint, int *year, int *month, int *day)
Definition datetime.c:230
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
Definition datetime.c:22
char ecat7errmsg[128]
Definition ecat7h.c:7
int ECAT7_TEST
Definition ecat7h.c:6
void ecat7PrintImageheader(ECAT7_imageheader *h, FILE *fp)
Definition ecat7p.c:102
void ecat7PrintPolmapheader(ECAT7_polmapheader *h, FILE *fp)
Definition ecat7p.c:276
void ecat7Print2DScanheader(ECAT7_2Dscanheader *h, FILE *fp)
Definition ecat7p.c:364
void ecat7PrintScanheader(ECAT7_scanheader *h, FILE *fp)
Definition ecat7p.c:181
int ecat7pxlbytes(short int data_type)
Definition ecat7r.c:1274
int ecat7Read2DScanMatrix(FILE *fp, int first_block, int last_block, ECAT7_2Dscanheader *h, float **fdata)
Definition ecat7r.c:959
int ecat7ReadScanheader(FILE *fp, int blk, ECAT7_scanheader *h)
Definition ecat7r.c:536
float ecat7rFloat(void *bufi, int isvax, int islittle)
Definition ecat7r.c:1233
int ecat7ReadPolmapheader(FILE *fp, int blk, ECAT7_polmapheader *h)
Definition ecat7r.c:397
int ecat7ReadMatrixdata(FILE *fp, int start_block, int block_nr, char *data, int dtype)
Definition ecat7r.c:791
int ecat7ReadNormheader(FILE *fp, int blk, ECAT7_normheader *h)
Definition ecat7r.c:472
int ecat7ReadAttenheader(FILE *fp, int blk, ECAT7_attenheader *h)
Definition ecat7r.c:311
int ecat7rInt(void *bufi, int isvax, int islittle)
Definition ecat7r.c:1256
int ecat7ReadMainheader(FILE *fp, ECAT7_mainheader *h)
Definition ecat7r.c:15
int ecat7ReadPolarmapMatrix(FILE *fp, int first_block, int last_block, ECAT7_polmapheader *h, float **fdata)
Definition ecat7r.c:1154
int ecat7Read2DScanheader(FILE *fp, int blk, ECAT7_2Dscanheader *h)
Definition ecat7r.c:631
int ecat7ReadImageMatrix(FILE *fp, int first_block, int last_block, ECAT7_imageheader *h, float **fdata)
Definition ecat7r.c:858
int ecat7ReadImageheader(FILE *fp, int blk, ECAT7_imageheader *h)
Definition ecat7r.c:162
int ecat7Read2DNormheader(FILE *fp, int blk, ECAT7_2Dnormheader *h)
Definition ecat7r.c:725
int ecat7ReadScanMatrix(FILE *fp, int first_block, int last_block, ECAT7_scanheader *h, float **fdata)
Definition ecat7r.c:1055
Header file for libtpcimgio.
#define MatFirstDirBlk
#define ECAT7_IEEER4
#define ECAT7_VAXI4
#define ECAT7_SUNI4
#define ECAT7_BYTE
#define ECAT7_VAXI2
#define MatBLKSIZE
#define ECAT7_VAXR4
#define ECAT7_SUNI2
void swawip(void *buf, long long int size)
Definition swap.c:114
void swabip(void *buf, long long int size)
Definition swap.c:72
void swawbip(void *buf, long long int size)
Definition swap.c:93
int little_endian()
Definition swap.c:14
short int num_dimensions
short int ring_difference
short int storage_order
short int num_z_elements
short int fill_user[50]
short int norm_quality_factor_code
short int num_r_elements
short int fill_cti[123]
short int corrections_applied
float deadtime_correction_factor
short int num_z_elements
short int num_r_elements
short int fill_user[50]
short int ring_difference
short int physical_planes[8]
short int fill_gate[6]
short int fill_cti[83]
float cor_singles[16]
float uncor_singles[16]
short int num_dimensions
short int num_r_elements
short int z_elements[64]
short int attenuation_type
short int fill_cti[86]
short int num_additional_atten_coeff
short int num_dimensions
float edge_finding_threshold
short int num_z_elements
short int data_type
short int num_angles
short int fill_user[50]
short int storage_order
short int ring_difference
float additional_atten_coeff[8]
short int fill_cti[87]
float filter_scatter_fraction
short int filter_code
short int image_max
short int image_min
short int zfilter_order
short int fill_user[49]
short int rfilter_code
float filter_cutoff_frequency
short int zfilter_code
short int filter_order
short int y_dimension
short int z_dimension
short int recon_type
short int scatter_type
short int recon_views
short int num_dimensions
short int rfilter_order
short int x_dimension
short int data_type
short int file_type
short int compression_code
float bed_position[15]
short int system_type
char facility_name[20]
short int num_frames
char operator_name[32]
short int coin_samp_mode
char data_units[32]
char magic_number[14]
float well_counter_corr_factor
char study_type[12]
char user_process_code[10]
short int angular_compression
short int calibration_units
char patient_name[32]
short int fill_cti[6]
short int septa_state
short int num_gates
short int num_planes
short int lwr_true_thres
char physician_name[32]
short int wobble_speed
short int acquisition_mode
short int calibration_units_label
char serial_number[10]
short int num_bed_pos
float ecat_calibration_factor
short int sw_version
short int transm_source_type
short int acquisition_type
char study_description[32]
short int axial_samp_mode
char patient_id[16]
char radiopharmaceutical[32]
short int patient_orientation
short int upr_true_thres
short int lwr_sctr_thres
char original_file_name[32]
short int num_r_elements
short int data_type
short int num_transaxial_crystals
short int num_geo_corr_planes
short int fill_user[50]
short int crystals_per_ring
short int max_ring_diff
float norm_quality_factor
float ring_dtcor1[32]
short int num_crystal_rings
short int norm_quality_factor_code
float ring_dtcor2[32]
float crystal_dtcor[8]
short int scatter_energy
short int fill_cti[48]
short int fill_cti[27]
short int ring_angle[32]
short int long_axis_left[3]
short int processing_code
short int sectors_per_ring[32]
char polar_map_protocol[20]
short int start_angle
short int polar_map_type
short int position_data
float ring_position[32]
short int quant_units
short int long_axis_right[3]
short int scan_min
short int num_r_elements
short int num_angles
short int data_type
float deadtime_correction_factor
short int fill_cti[90]
short int num_dimensions
short int axial_compression
short int fill_gate[6]
short int storage_order
float uncor_singles[128]
short int scan_max
short int num_z_elements[64]
short int fill_user[50]
short int ring_difference
short int corrections_applied