/*! * Read ECAT63 image matrix header and data. * If only header is to be read, set last_block=first_block. * Note: data is not calibrated with factor in main header. * * @param fp ECAT file pointer * @param first_block Subheader record number * @param last_block Last data block number * @param h Ptr to subheader data which is filled * @param fdata Ptr to the address of the matrix data * @return 0 if ok, 1 invalid input, 5 failed to read sub header, * 6 invalid (x,y,z) dimesions, 8 failed to allocate memory for meta-data, * 9 failed to read matrix data, 11 failed to allocate memory for image data */ int ecat63ReadImageMatrix(FILE *fp, int first_block, int last_block, ECAT63_imageheader *h, float **fdata) { int i, ret, blockNr, pxlNr; char *mdata, *mptr; float *_fdata, *fptr; short int *sptr; int *iptr; if(ECAT63_TEST) printf("ecat63ReadImageMatrix(fp, %d, %d, hdr, fdata)\n", first_block, last_block); if(fp==NULL || first_block<=MatFirstDirBlk || h==NULL) { sprintf(ecat63errmsg, "invalid function parameter.\n"); return(1); } *fdata=(float*)NULL; /* Read subheader */ ret=ecat63ReadImageheader(fp, first_block, h); if(ret) { sprintf(ecat63errmsg, "cannot read subheader (%d).\n", ret); return(5); } if(ECAT63_TEST>4) ecat63PrintImageheader(h, stdout); pxlNr=h->dimension_1*h->dimension_2; if(pxlNr<=0) { sprintf(ecat63errmsg, "invalid matrix dimension.\n"); return(6); } /* Read matrix data */ blockNr=last_block-first_block; if(blockNr<1) return(0); mdata=(char*)malloc(blockNr*MatBLKSIZE); if(mdata==NULL) { sprintf(ecat63errmsg, "cannot allocate memory.\n"); return(8); } mptr=mdata; ret=ecat63ReadMatdata(fp, first_block+1, blockNr, mptr, h->data_type); if(ret || mdata==NULL) { sprintf(ecat63errmsg, "cannot read matrix data (%d).\n", ret); free(mdata); return(9); } /* Allocate memory for float data */ _fdata=(float*)malloc(pxlNr*sizeof(float)); if(_fdata==NULL) { sprintf(ecat63errmsg, "cannot allocate memory.\n"); free(mdata); return(11); } /* Convert matrix data to floats */ if(h->ecat_calibration_fctr>0.0) h->quant_scale*=h->ecat_calibration_fctr; fptr=_fdata; mptr=mdata; if(h->data_type==BYTE_TYPE) { for(i=0; i<pxlNr; i++, mptr++, fptr++) *fptr=h->quant_scale*(float)(*mptr); } else if(h->data_type==VAX_I2 || h->data_type==SUN_I2) { for(i=0; i<pxlNr; i++, mptr+=2, fptr++) { sptr=(short int*)mptr; *fptr=h->quant_scale*(float)(*sptr); } } else if(h->data_type==VAX_I4 || h->data_type==SUN_I4) { for(i=0; i<pxlNr; i++, mptr+=4, fptr++) { iptr=(int*)mptr; *fptr=h->quant_scale*(float)(*iptr); } } else if(h->data_type==VAX_R4 || h->data_type==IEEE_R4) { memcpy(fptr, mptr, pxlNr*4); for(i=0; i<pxlNr; i++, fptr++) *fptr *= h->quant_scale; } free(mdata); *fdata=_fdata; return(0); }
/** Print ECAT63 subheader contents into specified file pointer. \return Returns 0 when successful. */ int ecat6PrintSubheader( /** ECAT 6.3 mainheader (not printed but needed here) */ ECAT63_mainheader mh, /** File pointer to ECAT 6.3 file */ FILE *fp, /** ECAT 6.3 plane */ int plane, /** ECAT 6.3 frame */ int frame, /** Output is written to this file pointer; it can be stdout */ FILE *ofp ) { int mi, ret, nr=0; static MATRIXLIST mlist; ECAT63_imageheader image_header; ECAT63_scanheader scan_header; ECAT63_attnheader attn_header; ECAT63_normheader norm_header; Matval matval; /* Read matrix list and nr */ ecat63InitMatlist(&mlist); ret=ecat63ReadMatlist(fp, &mlist); if(ret) { fprintf(stderr, "Error (%d): cannot read matrix list.\n", ret); return 2; } if(mlist.matrixNr<=0) { fprintf(stderr, "Error: matrix list is empty.\n"); return 2; } if(ECAT63_TEST>1) ecat63PrintMatlist(&mlist); /* * Read and print subheaders one at a time */ for(mi=nr=0; mi<mlist.matrixNr; mi++) { /* Get plane and frame nr */ mat_numdoc(mlist.matdir[mi].matnum, &matval); /* Check if this is supposed to be listed or not */ if(frame>=0 && frame!=matval.frame) continue; if(plane>=0 && plane!=matval.plane) continue; /* Read subheader */ if(mh.file_type==IMAGE_DATA) ret=ecat63ReadImageheader(fp, mlist.matdir[mi].strtblk, &image_header); else if(mh.file_type==RAW_DATA) ret=ecat63ReadScanheader(fp, mlist.matdir[mi].strtblk, &scan_header); else if(mh.file_type==ATTN_DATA) ret=ecat63ReadAttnheader(fp, mlist.matdir[mi].strtblk, &attn_header); else if(mh.file_type==NORM_DATA) ret=ecat63ReadNormheader(fp, mlist.matdir[mi].strtblk, &norm_header); if(ret) { fprintf(stderr, "Error: cannot read matrix %u subheader.\n", mlist.matdir[mi].matnum); ecat63EmptyMatlist(&mlist); return 4; } /* Print subheader */ fprintf(fp, "Matrix: plane %d frame %d gate %d bed %d\n", matval.plane, matval.frame, matval.gate, matval.bed); if(mh.file_type==IMAGE_DATA) ecat63PrintImageheader(&image_header, ofp); else if(mh.file_type==RAW_DATA) ecat63PrintScanheader(&scan_header, ofp); else if(mh.file_type==ATTN_DATA) ecat63PrintAttnheader(&attn_header, ofp); else if(mh.file_type==NORM_DATA) ecat63PrintNormheader(&norm_header, ofp); nr++; // counter } /* next matrix */ ecat63EmptyMatlist(&mlist); if(nr==0 && (plane>=0 || frame>=0)) { fprintf(stderr, "Error: specified matrices not found.\n"); return(11); } return(0); }