25 if(
ECAT7_TEST) printf(
"ecat7WriteMainheader()\n");
27 if(fp==NULL || h==NULL)
return(1);
89 memcpy(buf+458, &h->
dosage, 4);
if(little)
swawbip(buf+458, 4);
117 if(
ECAT7_TEST) printf(
"ecat7WriteImageheader(%d)\n", blk);
118 if(fp==NULL || blk<2 || h==NULL)
return(1);
163 memcpy(buf+162, &h->
mt_1_1, 4);
if(little)
swawbip(buf+162, 4);
164 memcpy(buf+166, &h->
mt_1_2, 4);
if(little)
swawbip(buf+166, 4);
165 memcpy(buf+170, &h->
mt_1_3, 4);
if(little)
swawbip(buf+170, 4);
166 memcpy(buf+174, &h->
mt_2_1, 4);
if(little)
swawbip(buf+174, 4);
167 memcpy(buf+178, &h->
mt_2_2, 4);
if(little)
swawbip(buf+178, 4);
168 memcpy(buf+182, &h->
mt_2_3, 4);
if(little)
swawbip(buf+182, 4);
169 memcpy(buf+186, &h->
mt_3_1, 4);
if(little)
swawbip(buf+186, 4);
170 memcpy(buf+190, &h->
mt_3_2, 4);
if(little)
swawbip(buf+190, 4);
171 memcpy(buf+194, &h->
mt_3_3, 4);
if(little)
swawbip(buf+194, 4);
180 memcpy(buf+222, &h->
mt_1_4, 4);
if(little)
swawbip(buf+222, 4);
181 memcpy(buf+226, &h->
mt_2_4, 4);
if(little)
swawbip(buf+226, 4);
182 memcpy(buf+230, &h->
mt_3_4, 4);
if(little)
swawbip(buf+230, 4);
191 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
212 if(
ECAT7_TEST) printf(
"ecat7WriteAttenheader()\n");
213 if(fp==NULL || blk<2 || h==NULL)
return(1);
247 memcpy(buf+110, &h->
span, 2);
if(little)
swabip(buf+110, 2);
249 memcpy(buf+240, h->
fill_cti, 86*2);
if(little)
swabip(buf+240, 86*2);
254 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
275 if(
ECAT7_TEST) printf(
"ecat7WritePolmapheader()\n");
276 if(fp==NULL || blk<2 || h==NULL)
return(1);
309 memcpy(buf+404, h->
fill_cti, 27*2);
if(little)
swabip(buf+404, 27*2);
313 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
334 if(
ECAT7_TEST) printf(
"ecat7WriteNormheader()\n");
335 if(fp==NULL || blk<2 || h==NULL)
return(1);
350 memcpy(buf+12, &h->
uld, 2);
if(little)
swabip(buf+12, 2);
351 memcpy(buf+14, &h->
lld, 2);
if(little)
swabip(buf+14, 2);
358 memcpy(buf+312, &h->
span, 2);
if(little)
swabip(buf+312, 2);
360 memcpy(buf+316, h->
fill_cti, 48*2);
if(little)
swabip(buf+316, 48*2);
365 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
387 if(
ECAT7_TEST) printf(
"ecat7WriteScanheader()\n");
388 if(fp==NULL || blk<2 || h==NULL)
return(1);
427 memcpy(buf+232, h->
fill_cti, 90*2);
if(little)
swabip(buf+232, 90*2);
433 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
454 if(
ECAT7_TEST) printf(
"ecat7Write2DScanheader()\n");
455 if(fp==NULL || blk<2 || h==NULL)
return(1);
495 memcpy(buf+246, h->
fill_cti, 83*2);
if(little)
swabip(buf+246, 83*2);
500 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
521 if(
ECAT7_TEST) printf(
"ecat7Write2DNormheader()\n");
522 if(fp==NULL || blk<2 || h==NULL)
return(1);
544 memcpy(buf+36, &h->
span, 2);
if(little)
swabip(buf+36, 2);
545 memcpy(buf+38, h->
fill_cti, 64*2);
if(little)
swabip(buf+38, 64*2);
546 memcpy(buf+166, h->
fill_cti, 123*2);
if(little)
swabip(buf+166, 123*2);
551 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(4);
569 char tmp[FILENAME_MAX];
572 if(
ECAT7_TEST) printf(
"ecat7Create(%s, h)\n", fname);
574 if(fname==NULL || h==NULL)
return(NULL);
576 if(access(fname, 0) != -1) {
578 if(access(tmp, 0) != -1) remove(tmp);
579 if(
ECAT7_TEST) printf(
"Renaming %s -> %s\n", fname, tmp);
583 fp=fopen(fname,
"wb+");
if(fp==NULL)
return(fp);
614 if(nr<1 || data==NULL)
return(0);
616 if(amax<0.9999)
return(1);
618 if(modf(amax, &d)>0.0001)
return(1);
620 for(
long long i=0; i<nr; i++)
if(modf(*data++, &d)>0.0001)
return(1);
637 long long pxlNr, blkNr, nxtblk, data_size;
638 float *fptr, fmin, fmax, g, f;
643 if(
ECAT7_TEST) printf(
"ecat7WriteImageMatrix(fp, %d, h, data)\n", matrix_id);
644 if(fp==NULL || matrix_id<1 || h==NULL || fdata==NULL) {
645 sprintf(
ecat7errmsg,
"invalid function parameter.\n");
656 sprintf(
ecat7errmsg,
"invalid matrix dimension.\n");
668 mdata=(
char*)calloc(blkNr,
MatBLKSIZE);
if(mdata==NULL) {
674 if(fabs(fmin)>fabs(fmax)) g=fabs(fmin);
else g=fabs(fmax);
675 if(g>0) f=32766./g;
else f=1.0;
681 sptr=(
short int*)mdata; fptr=fdata;
682 for(
long long i=0; i<pxlNr; i++, sptr++, fptr++) *sptr=(
short int)
temp_roundf(f*(*fptr));
688 sprintf(
ecat7errmsg,
"cannot determine matrix block (%lld).\n", -nxtblk);
689 free(mdata);
return(8);
691 if(
ECAT7_TEST>2) printf(
" block=%lld fmin=%g fmax=%g\n", nxtblk, fmin, fmax);
694 sprintf(
ecat7errmsg,
"cannot write subheader (%d).\n", ret);
695 free(mdata);
return(10);
702 sprintf(
ecat7errmsg,
"cannot write matrix data (%d).\n", ret);
721 long long i, data_size, nxtblk, blkNr, pxlNr;
722 float *fptr, fmin, fmax, g, f;
727 if(
ECAT7_TEST) printf(
"ecat7Write2DScanMatrix(fp, %d, h, data)\n", matrix_id);
728 if(fp==NULL || matrix_id<1 || h==NULL || fdata==NULL) {
729 sprintf(
ecat7errmsg,
"invalid function parameter.\n");
740 sprintf(
ecat7errmsg,
"invalid matrix dimension.\n");
751 mdata=(
char*)calloc(blkNr,
MatBLKSIZE);
if(mdata==NULL) {
757 if(fabs(fmin)>fabs(fmax)) g=fabs(fmin);
else g=fabs(fmax);
758 if(g>0) f=32766./g;
else f=1.0;
764 sptr=(
short int*)mdata; fptr=fdata;
765 for(i=0; i<pxlNr; i++, sptr++, fptr++) *sptr=(
short int)
temp_roundf(f*(*fptr));
771 sprintf(
ecat7errmsg,
"cannot determine matrix block (%lld).\n", -nxtblk);
772 free(mdata);
return(8);
774 if(
ECAT7_TEST>2) printf(
" block=%lld fmin=%g fmax=%g\n", nxtblk, fmin, fmax);
777 sprintf(
ecat7errmsg,
"cannot write subheader (%d).\n", ret);
778 free(mdata);
return(10);
785 sprintf(
ecat7errmsg,
"cannot write matrix data (%d).\n", ret);
804 long long i, nxtblk, blkNr, data_size, pxlNr, dimz;
805 float *fptr, fmin, fmax, g, f;
810 if(
ECAT7_TEST) printf(
"ecat7WriteScanMatrix(fp, %d, h, data)\n", matrix_id);
811 if(fp==NULL || matrix_id<1 || h==NULL || fdata==NULL) {
812 sprintf(
ecat7errmsg,
"invalid function parameter.\n");
824 sprintf(
ecat7errmsg,
"invalid matrix dimension.\n");
835 mdata=(
char*)calloc(blkNr,
MatBLKSIZE);
if(mdata==NULL) {
841 if(fabs(fmin)>fabs(fmax)) g=fabs(fmin);
else g=fabs(fmax);
842 if(g>0) f=32766./g;
else f=1.0;
848 sptr=(
short int*)mdata; fptr=fdata;
849 for(i=0; i<pxlNr; i++, sptr++, fptr++) *sptr=(
short int)
temp_roundf(f*(*fptr));
856 sprintf(
ecat7errmsg,
"cannot determine matrix block (%lld).\n", -nxtblk);
857 free(mdata);
return(8);
859 if(
ECAT7_TEST>2) printf(
" block=%lld fmin=%g fmax=%g\n", nxtblk, fmin, fmax);
862 sprintf(
ecat7errmsg,
"cannot write subheader (%d).\n", ret);
863 free(mdata);
return(10);
871 sprintf(
ecat7errmsg,
"cannot write matrix data (%d).\n", ret);
890 long long i, nxtblk, blkNr, data_size, pxlNr;
891 float *fptr, fmin, fmax, g, f;
896 if(
ECAT7_TEST) printf(
"ecat7WritePolarmapMatrix(fp, %d, h, data)\n", matrix_id);
897 if(fp==NULL || matrix_id<1 || h==NULL || fdata==NULL) {
898 sprintf(
ecat7errmsg,
"invalid function parameter.\n");
908 sprintf(
ecat7errmsg,
"invalid matrix dimension.\n");
919 mdata=(
char*)calloc(blkNr,
MatBLKSIZE);
if(mdata==NULL) {
925 if(fabs(fmin)>fabs(fmax)) g=fabs(fmin);
else g=fabs(fmax);
926 if(g>0) f=32766./g;
else f=1.0;
932 sptr=(
short int*)mdata; fptr=fdata;
933 for(i=0; i<pxlNr; i++, sptr++, fptr++) *sptr=(
short int)
temp_roundf(f*(*fptr));
939 sprintf(
ecat7errmsg,
"cannot determine matrix block (%lld).\n", -nxtblk);
940 free(mdata);
return(8);
942 if(
ECAT7_TEST>2) printf(
" block=%lld fmin=%g fmax=%g\n", nxtblk, fmin, fmax);
945 sprintf(
ecat7errmsg,
"cannot write subheader (%d).\n", ret);
946 free(mdata);
return(10);
953 sprintf(
ecat7errmsg,
"cannot write matrix data (%d).\n", ret);
978 long long i, dataSize, byteNr, blkNr;
980 if(
ECAT7_TEST) printf(
"ecat7WriteMatrixdata(fp, %d, data, %lld, %d)\n",
981 start_block, pxl_nr, pxl_size);
982 if(fp==NULL || start_block<1 || data==NULL || pxl_nr<1 || pxl_size<1)
return(1);
984 dataSize=pxl_nr*pxl_size;
987 if(
ECAT7_TEST>2) printf(
" blkNr=%lld\n", blkNr);
989 long long pos=(start_block-1)*(
long long)
MatBLKSIZE;
990 fseeko(fp, pos, SEEK_SET);
if(ftello(fp)!=pos)
return(2);
992 for(i=0, dptr=data; i<blkNr && dataSize>0; i++) {
994 memcpy(buf, dptr, byteNr);
997 if(pxl_size==2)
swabip(buf, byteNr);
998 else if(pxl_size==4)
swawbip(buf, byteNr);
1003 dptr+=byteNr; dataSize-=byteNr;
int ecat7EnterMatrix(FILE *fp, int matrix_id, int block_nr)
int ecat7pxlbytes(short int data_type)
int ecat7_is_scaling_needed(float amax, float *data, long long nr)
int ecat7WriteImageMatrix(FILE *fp, int matrix_id, ECAT7_imageheader *h, float *fdata)
int ecat7WriteScanMatrix(FILE *fp, int matrix_id, ECAT7_scanheader *h, float *fdata)
int ecat7WriteNormheader(FILE *fp, int blk, ECAT7_normheader *h)
int ecat7WriteScanheader(FILE *fp, int blk, ECAT7_scanheader *h)
int ecat7WriteImageheader(FILE *fp, int blk, ECAT7_imageheader *h)
int ecat7WriteMatrixdata(FILE *fp, int start_block, char *data, long long pxl_nr, int pxl_size)
int ecat7WritePolarmapMatrix(FILE *fp, int matrix_id, ECAT7_polmapheader *h, float *fdata)
int ecat7WritePolmapheader(FILE *fp, int blk, ECAT7_polmapheader *h)
int ecat7Write2DScanheader(FILE *fp, int blk, ECAT7_2Dscanheader *h)
int ecat7Write2DScanMatrix(FILE *fp, int matrix_id, ECAT7_2Dscanheader *h, float *fdata)
int ecat7Write2DNormheader(FILE *fp, int blk, ECAT7_2Dnormheader *h)
FILE * ecat7Create(const char *fname, ECAT7_mainheader *h)
int ecat7WriteAttenheader(FILE *fp, int blk, ECAT7_attenheader *h)
int ecat7WriteMainheader(FILE *fp, ECAT7_mainheader *h)
void fMinMaxFin(float *data, long long int n, float *fmin, float *fmax)
Header file for libtpcimgio.
void swabip(void *buf, long long int size)
void swawbip(void *buf, long long int size)
short int compression_code
float well_counter_corr_factor
char user_process_code[10]
short int angular_compression
short int calibration_units
short int acquisition_mode
short int calibration_units_label
float ecat_calibration_factor
short int transm_source_type
short int acquisition_type
char study_description[32]
short int axial_samp_mode
char radiopharmaceutical[32]
short int patient_orientation
char original_file_name[32]