00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <math.h>
00015 #include <ctype.h>
00016 #include <string.h>
00017 #include <strings.h>
00018 #include <unistd.h>
00019 #include <time.h>
00020
00021 #include <swap.h>
00022 #include "include/ecat7.h"
00023 #include "include/substitutions.h"
00024
00025
00026
00030 int ecat7EditMHeader(
00032 ECAT7_mainheader *h,
00034 char *field,
00036 char *value
00037 ) {
00038 int ii, yy, mm, dd, ho, mi, se;
00039 struct tm *st;
00040 time_t timet;
00041 short int si;
00042 float f;
00043
00044 si=atoi(value); ii=atoi(value); f=atof(value);
00045 if(strcmp(field, "magic_number")==0) {
00046 strncpy(h->magic_number, value, 14);
00047 } else if(strcmp(field, "original_file_name")==0) {
00048 strncpy(h->original_file_name, value, 32);
00049 } else if(strcmp(field, "sw_version")==0) {
00050 if(si<=0) return(2); else h->sw_version=si;
00051 } else if(strcmp(field, "system_type")==0) {
00052 if(si<0) return(2); else h->system_type=si;
00053 } else if(strcmp(field, "file_type")==0) {
00054 if(si<0) return(2); else h->file_type=si;
00055 } else if(strcmp(field, "serial_number")==0) {
00056 strncpy(h->serial_number, value, 10);
00057 } else if(strcmp(field, "scan_start_time")==0) {
00058 timet=time(NULL); st=localtime(&timet);
00059 if(sscanf(value, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &ho, &mi, &se)!=6)
00060 return(2);
00061 st->tm_mday=dd; st->tm_mon=mm-1; st->tm_year=yy-1900;
00062 st->tm_hour=ho; st->tm_min=mi; st->tm_sec=se; st->tm_isdst=-1;
00063 h->scan_start_time=mktime(st);
00064 } else if(strcmp(field, "isotope_name")==0) {
00065 strncpy(h->isotope_name, value, 8);
00066 } else if(strcmp(field, "isotope_halflife")==0) {
00067 if(f<=1.0E-3) return(2); else h->isotope_halflife=f;
00068 } else if(strcmp(field, "radiopharmaceutical")==0) {
00069 strncpy(h->radiopharmaceutical, value, 32);
00070 } else if(strcmp(field, "gantry_tilt")==0) {
00071 h->gantry_tilt=f;
00072 } else if(strcmp(field, "gantry_rotation")==0) {
00073 h->gantry_rotation=f;
00074 } else if(strcmp(field, "bed_elevation")==0) {
00075 h->bed_elevation=f;
00076 } else if(strcmp(field, "intrinsic_tilt")==0) {
00077 h->intrinsic_tilt=f;
00078 } else if(strcmp(field, "wobble_speed")==0) {
00079 h->wobble_speed=si;
00080 } else if(strcmp(field, "transm_source_type")==0) {
00081 h->transm_source_type=si;
00082 } else if(strcmp(field, "distance_scanned")==0) {
00083 h->distance_scanned=f;
00084 } else if(strcmp(field, "transaxial_fov")==0) {
00085 h->transaxial_fov=f;
00086 } else if(strcmp(field, "angular_compression")==0) {
00087 h->angular_compression=si;
00088 } else if(strcmp(field, "coin_samp_mode")==0) {
00089 h->coin_samp_mode=si;
00090 } else if(strcmp(field, "axial_samp_mode")==0) {
00091 h->axial_samp_mode=si;
00092 } else if(strcmp(field, "ecat_calibration_factor")==0) {
00093 h->ecat_calibration_factor=f;
00094 } else if(strcmp(field, "calibration_units")==0) {
00095 h->calibration_units=si;
00096 } else if(strcmp(field, "calibration_units_label")==0) {
00097 h->calibration_units_label=si;
00098 } else if(strcmp(field, "compression_code")==0) {
00099 h->compression_code=si;
00100 } else if(strcmp(field, "study_type")==0) {
00101 strncpy(h->study_type, value, 12);
00102 } else if(strcmp(field, "patient_id")==0) {
00103 strncpy(h->patient_id, value, 16);
00104 } else if(strcmp(field, "patient_name")==0) {
00105 strncpy(h->patient_name, value, 32);
00106 } else if(strcmp(field, "patient_sex")==0) {
00107 h->patient_sex=value[0];
00108 } else if(strcmp(field, "patient_dexterity")==0) {
00109 h->patient_dexterity=value[0];
00110 } else if(strcmp(field, "patient_age")==0) {
00111 h->patient_age=f;
00112 } else if(strcmp(field, "patient_height")==0) {
00113 h->patient_height=f;
00114 } else if(strcmp(field, "patient_weight")==0) {
00115 h->patient_weight=f;
00116 } else if(strcmp(field, "patient_birth_date")==0) {
00117 timet=time(NULL); st=localtime(&timet);
00118 if(sscanf(value, "%d-%d-%d", &yy, &mm, &dd)!=3) return(2);
00119 st->tm_mday=dd; st->tm_mon=mm-1; st->tm_year=yy-1900;
00120 st->tm_hour=0; st->tm_min=0; st->tm_sec=0; st->tm_isdst=-1;
00121 h->patient_birth_date=mktime(st);
00122 } else if(strcmp(field, "physician_name")==0) {
00123 strncpy(h->physician_name, value, 32);
00124 } else if(strcmp(field, "operator_name")==0) {
00125 strncpy(h->operator_name, value, 32);
00126 } else if(strcmp(field, "study_description")==0) {
00127 strncpy(h->study_description, value, 32);
00128 } else if(strcmp(field, "acquisition_type")==0) {
00129 h->acquisition_type=si;
00130 } else if(strcmp(field, "patient_orientation")==0) {
00131 h->patient_orientation=si;
00132 } else if(strcmp(field, "facility_name")==0) {
00133 strncpy(h->facility_name, value, 20);
00134 } else if(strcmp(field, "num_planes")==0) {
00135 h->num_planes=si;
00136 } else if(strcmp(field, "num_frames")==0) {
00137 h->num_frames=si;
00138 } else if(strcmp(field, "num_gates")==0) {
00139 h->num_gates=si;
00140 } else if(strcmp(field, "num_bed_pos")==0) {
00141 h->num_bed_pos=si;
00142 } else if(strcmp(field, "init_bed_position")==0) {
00143 h->init_bed_position=f;
00144 } else if(strcmp(field, "bed_position")==0) {
00145 sscanf(value, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
00146 h->bed_position+0, h->bed_position+1, h->bed_position+2,
00147 h->bed_position+3, h->bed_position+4, h->bed_position+5,
00148 h->bed_position+6, h->bed_position+7, h->bed_position+8,
00149 h->bed_position+9, h->bed_position+10, h->bed_position+11,
00150 h->bed_position+12, h->bed_position+13, h->bed_position+14
00151 );
00152 } else if(strcmp(field, "plane_separation")==0) {
00153 h->plane_separation=f;
00154 } else if(strcmp(field, "lwr_sctr_thres")==0) {
00155 h->lwr_sctr_thres=si;
00156 } else if(strcmp(field, "lwr_true_thres")==0) {
00157 h->lwr_true_thres=si;
00158 } else if(strcmp(field, "upr_true_thres")==0) {
00159 h->upr_true_thres=si;
00160 } else if(strcmp(field, "user_process_code")==0) {
00161 strncpy(h->user_process_code, value, 10);
00162 } else if(strcmp(field, "acquisition_mode")==0) {
00163 h->acquisition_mode=si;
00164 } else if(strcmp(field, "bin_size")==0) {
00165 h->bin_size=f;
00166 } else if(strcmp(field, "branching_fraction")==0) {
00167 h->branching_fraction=f;
00168 } else if(strcmp(field, "dose_start_time")==0) {
00169 timet=time(NULL); st=localtime(&timet);
00170 if(sscanf(value, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &ho, &mi, &se)!=6)
00171 return(2);
00172 st->tm_mday=dd; st->tm_mon=mm-1; st->tm_year=yy-1900;
00173 st->tm_hour=ho; st->tm_min=mi; st->tm_sec=se; st->tm_isdst=-1;
00174 h->dose_start_time=mktime(st);
00175 } else if(strcmp(field, "dosage")==0) {
00176 h->dosage=f;
00177 } else if(strcmp(field, "well_counter_corr_factor")==0) {
00178 h->well_counter_corr_factor=f;
00179 } else if(strcmp(field, "data_units")==0) {
00180 strncpy(h->data_units, value, 32);
00181 } else if(strcmp(field, "septa_state")==0) {
00182 h->septa_state=si;
00183 } else
00184 return(1);
00185
00186 return(0);
00187 }
00188
00189
00190
00195 int ecat7EditSHeader(
00197 ECAT7_scanheader *h,
00199 char *field,
00201 char *value
00202 ) {
00203 int i, ii;
00204 short int si;
00205 float f;
00206 char *cptr;
00207
00208 si=atoi(value); ii=atoi(value); f=atof(value);
00209
00210 if(strcasecmp(field, "DATA_TYPE")==0) {
00211 h->data_type=si;
00212 } else if(strcasecmp(field, "NUM_DIMENSIONS")==0) {
00213 h->num_dimensions=si;
00214 } else if(strcasecmp(field, "NUM_R_ELEMENTS")==0) {
00215 h->num_r_elements=si;
00216 } else if(strcasecmp(field, "NUM_ANGLES")==0) {
00217 h->num_angles=si;
00218 } else if(strcasecmp(field, "CORRECTIONS_APPLIED")==0) {
00219 h->corrections_applied=si;
00220 } else if(strncasecmp(field, "NUM_Z_ELEMENTS", 14)==0) {
00221 cptr=strtok(value, " \t,;\n\r");
00222 for(i=0; i<64; i++) {
00223 if(cptr==NULL) break;
00224 h->num_z_elements[i]=(short int)atoi(cptr);
00225 cptr=strtok(NULL, " \t,;\n\r");
00226 }
00227 } else if(strcasecmp(field, "RING_DIFFERENCE")==0) {
00228 h->ring_difference=si;
00229 } else if(strcasecmp(field, "STORAGE_ORDER")==0) {
00230 h->storage_order=si;
00231 } else if(strcasecmp(field, "AXIAL_COMPRESSION")==0) {
00232 h->axial_compression=si;
00233 } else if(strcasecmp(field, "X_RESOLUTION")==0) {
00234 h->x_resolution=f;
00235 } else if(strcasecmp(field, "V_RESOLUTION")==0) {
00236 h->v_resolution=f;
00237 } else if(strcasecmp(field, "Z_RESOLUTION")==0) {
00238 h->z_resolution=f;
00239 } else if(strcasecmp(field, "W_RESOLUTION")==0) {
00240 h->w_resolution=f;
00241 } else if(strncasecmp(field, "FILL_GATE", 9)==0) {
00242 cptr=strtok(value, " \t,;\n\r");
00243 for(i=0; i<6; i++) {
00244 if(cptr==NULL) break;
00245 h->fill_gate[i]=(short int)atoi(cptr);
00246 cptr=strtok(NULL, " \t,;\n\r");
00247 }
00248 } else if(strcasecmp(field, "GATE_DURATION")==0) {
00249 h->gate_duration=ii;
00250 } else if(strcasecmp(field, "R_WAVE_OFFSET")==0) {
00251 h->r_wave_offset=ii;
00252 } else if(strcasecmp(field, "NUM_ACCEPTED_BEATS")==0) {
00253 h->num_accepted_beats=ii;
00254 } else if(strcasecmp(field, "SCALE_FACTOR")==0) {
00255 h->scale_factor=f;
00256 } else if(strcasecmp(field, "SCAN_MIN")==0) {
00257 h->scan_min=si;
00258 } else if(strcasecmp(field, "SCAN_MAX")==0) {
00259 h->scan_max=si;
00260 } else if(strcasecmp(field, "PROMPTS")==0) {
00261 h->prompts=ii;
00262 } else if(strcasecmp(field, "DELAYED")==0) {
00263 h->delayed=ii;
00264 } else if(strcasecmp(field, "MULTIPLES")==0) {
00265 h->multiples=ii;
00266 } else if(strcasecmp(field, "NET_TRUES")==0) {
00267 h->net_trues=ii;
00268 } else if(strcasecmp(field, "TOT_AVG_COR")==0) {
00269 h->tot_avg_cor=f;
00270 } else if(strcasecmp(field, "TOT_AVG_UNCOR")==0) {
00271 h->tot_avg_uncor=f;
00272 } else if(strcasecmp(field, "TOTAL_COIN_RATE")==0) {
00273 h->total_coin_rate=ii;
00274 } else if(strcasecmp(field, "FRAME_START_TIME")==0) {
00275 h->frame_start_time=ii;
00276 } else if(strcasecmp(field, "FRAME_DURATION")==0) {
00277 h->frame_duration=ii;
00278 } else if(strcasecmp(field, "DEADTIME_CORRECTION_FACTOR")==0) {
00279 h->deadtime_correction_factor=f;
00280 } else if(strncasecmp(field, "FILL_CTI", 8)==0) {
00281 cptr=strtok(value, " \t,;\n\r");
00282 for(i=0; i<90; i++) {
00283 if(cptr==NULL) break;
00284 h->fill_cti[i]=(short int)atoi(cptr);
00285 cptr=strtok(NULL, " \t,;\n\r");
00286 }
00287 } else if(strncasecmp(field, "FILL_USER", 9)==0) {
00288 cptr=strtok(value, " \t,;\n\r");
00289 for(i=0; i<50; i++) {
00290 if(cptr==NULL) break;
00291 h->fill_user[i]=(short int)atoi(cptr);
00292 cptr=strtok(NULL, " \t,;\n\r");
00293 }
00294 } else if(strncasecmp(field, "UNCOR_SINGLES", 13)==0) {
00295 cptr=strtok(value, " \t,;\n\r");
00296 for(i=0; i<128; i++) {
00297 if(cptr==NULL) break;
00298 h->fill_user[i]=(float)atof(cptr);
00299 cptr=strtok(NULL, " \t,;\n\r");
00300 }
00301 } else
00302 return(1);
00303 return(0);
00304 }
00305
00306
00307
00312 int ecat7EditVHeader(
00314 ECAT7_imageheader *h,
00316 char *field,
00318 char *value
00319 ) {
00320 int ii;
00321 short int si;
00322 float f;
00323
00324 si=atoi(value); ii=atoi(value); f=atof(value);
00325
00326 if(strcasecmp(field, "DATA_TYPE")==0) {
00327 h->data_type=si;
00328 } else if(strcasecmp(field, "NUM_DIMENSIONS")==0) {
00329 h->num_dimensions=si;
00330 } else if(strcasecmp(field, "X_DIMENSION")==0) {
00331 h->x_dimension=si;
00332 } else if(strcasecmp(field, "Y_DIMENSION")==0) {
00333 h->y_dimension=si;
00334 } else if(strcasecmp(field, "Z_DIMENSION")==0) {
00335 h->z_dimension=si;
00336 } else if(strcasecmp(field, "X_OFFSET")==0) {
00337 h->x_offset=f;
00338 } else if(strcasecmp(field, "Y_OFFSET")==0) {
00339 h->y_offset=f;
00340 } else if(strcasecmp(field, "Z_OFFSET")==0) {
00341 h->z_offset=f;
00342 } else if(strcasecmp(field, "RECON_ZOOM")==0) {
00343 h->recon_zoom=f;
00344 } else if(strcasecmp(field, "SCALE_FACTOR")==0) {
00345 h->scale_factor=f;
00346 } else if(strcasecmp(field, "IMAGE_MIN")==0) {
00347 h->image_min=si;
00348 } else if(strcasecmp(field, "IMAGE_MAX")==0) {
00349 h->image_max=si;
00350 } else if(strcasecmp(field, "X_PIXEL_SIZE")==0) {
00351 h->x_pixel_size=f;
00352 } else if(strcasecmp(field, "Y_PIXEL_SIZE")==0) {
00353 h->y_pixel_size=f;
00354 } else if(strcasecmp(field, "Z_PIXEL_SIZE")==0) {
00355 h->z_pixel_size=f;
00356 } else if(strcasecmp(field, "FRAME_DURATION")==0) {
00357 h->frame_duration=ii;
00358 } else if(strcasecmp(field, "FRAME_START_TIME")==0) {
00359 h->frame_start_time=ii;
00360 } else if(strcasecmp(field, "FILTER_CODE")==0) {
00361 h->filter_code=si;
00362 } else if(strcasecmp(field, "X_RESOLUTION")==0) {
00363 h->x_resolution=f;
00364 } else if(strcasecmp(field, "Y_RESOLUTION")==0) {
00365 h->y_resolution=f;
00366 } else if(strcasecmp(field, "Z_RESOLUTION")==0) {
00367 h->z_resolution=f;
00368 } else if(strcasecmp(field, "NUM_R_ELEMENTS")==0) {
00369 h->num_r_elements=f;
00370 } else if(strcasecmp(field, "NUM_ANGLES")==0) {
00371 h->num_angles=f;
00372 } else if(strcasecmp(field, "Z_ROTATION_ANGLES")==0) {
00373 h->z_rotation_angle=f;
00374 } else if(strcasecmp(field, "DECAY_CORR_FCTR")==0) {
00375 h->decay_corr_fctr=f;
00376 } else if(strcasecmp(field, "PROCESSING_CODE")==0) {
00377 h->processing_code=ii;
00378 } else if(strcasecmp(field, "GATE_DURATIONR")==0) {
00379 h->gate_duration=ii;
00380 } else if(strcasecmp(field, "R_WAVE_OFFSET")==0) {
00381 h->r_wave_offset=ii;
00382 } else if(strcasecmp(field, "NUM_ACCEPTED_BEATS")==0) {
00383 h->num_accepted_beats=ii;
00384 } else if(strcasecmp(field, "FILTER_CUTOFF_FREQUENCY")==0) {
00385 h->filter_cutoff_frequency=f;
00386 } else if(strcasecmp(field, "FILTER_RESOLUTION")==0) {
00387 h->filter_resolution=f;
00388 } else if(strcasecmp(field, "FILTER_RAMP_SLOPE")==0) {
00389 h->filter_ramp_slope=f;
00390 } else if(strcasecmp(field, "FILTER_ORDER")==0) {
00391 h->filter_order=f;
00392 } else if(strcasecmp(field, "FILTER_SCATTER_FRACTION")==0) {
00393 h->filter_scatter_fraction=f;
00394 } else if(strcasecmp(field, "FILTER_SCATTER_SLOPE")==0) {
00395 h->filter_scatter_slope=si;
00396 } else if(strcasecmp(field, "ANNOTATION")==0) {
00397 strncpy(h->annotation, value, 40);
00398 } else if(strcasecmp(field, "MT_1_1")==0) {
00399 h->mt_1_1=f;
00400 } else if(strcasecmp(field, "MT_1_2")==0) {
00401 h->mt_1_2=f;
00402 } else if(strcasecmp(field, "MT_1_3")==0) {
00403 h->mt_1_3=f;
00404 } else if(strcasecmp(field, "MT_2_1")==0) {
00405 h->mt_2_1=f;
00406 } else if(strcasecmp(field, "MT_2_2")==0) {
00407 h->mt_2_2=f;
00408 } else if(strcasecmp(field, "MT_2_3")==0) {
00409 h->mt_2_3=f;
00410 } else if(strcasecmp(field, "MT_3_1")==0) {
00411 h->mt_3_1=f;
00412 } else if(strcasecmp(field, "MT_3_2")==0) {
00413 h->mt_3_2=f;
00414 } else if(strcasecmp(field, "MT_3_3")==0) {
00415 h->mt_3_3=f;
00416 } else if(strcasecmp(field, "RFILTER_CUTOFF")==0) {
00417 h->rfilter_cutoff=f;
00418 } else if(strcasecmp(field, "RFILTER_RESOLUTION")==0) {
00419 h->rfilter_resolution=f;
00420 } else if(strcasecmp(field, "RFILTER_CODE")==0) {
00421 h->rfilter_code=si;
00422 } else if(strcasecmp(field, "RFILTER_ORDER")==0) {
00423 h->rfilter_order=si;
00424 } else if(strcasecmp(field, "ZFILTER_CUTOFF")==0) {
00425 h->zfilter_cutoff=f;
00426 } else if(strcasecmp(field, "ZFILTER_RESOLUTION")==0) {
00427 h->zfilter_resolution=f;
00428 } else if(strcasecmp(field, "ZFILTER_CODE")==0) {
00429 h->zfilter_code=si;
00430 } else if(strcasecmp(field, "ZFILTER_ORDER")==0) {
00431 h->zfilter_order=si;
00432 } else if(strcasecmp(field, "MT_1_4")==0) {
00433 h->mt_1_4=f;
00434 } else if(strcasecmp(field, "MT_2_4")==0) {
00435 h->mt_2_4=f;
00436 } else if(strcasecmp(field, "MT_3_4")==0) {
00437 h->mt_3_4=f;
00438 } else if(strcasecmp(field, "SCATTER_TYPE")==0) {
00439 h->scatter_type=si;
00440 } else if(strcasecmp(field, "RECON_TYPE")==0) {
00441 h->recon_type=si;
00442 } else if(strcasecmp(field, "RECON_VIEWS")==0) {
00443 h->recon_views=si;
00444 } else
00445 return(1);
00446
00447 return(0);
00448 }
00449
00450
00451
00452