TPCCLIB
Loading...
Searching...
No Matches
imgfile.c File Reference

I/O routines for IMG data. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

int imgRead (const char *fname, IMG *img)
 
int imgWrite (const char *fname, IMG *img)
 
int imgReadHeader (const char *fname, IMG *img, int format)
 
int imgReadFrame (const char *fname, int frame_to_read, IMG *img, int frame_index)
 
int imgWriteFrame (const char *fname, int frame_to_write, IMG *img, int frame_index)
 
void imgFormatFromFName (IMG *img, const char *fname)
 
int imgFormatDetermine (const char *fname, char *basename, char *hdrfile, char *imgfile, char *siffile, int *file_format, int *scanner, int *type, int *modality, int verbose)
 

Detailed Description

I/O routines for IMG data.

Author
Vesa Oikonen, Harri Merisaari, Calle Laakkonen

Currently supported file formats:

  • ECAT 6.3 images and sinograms
  • ECAT 7.x 2D and 3D images (volumes) and sinograms
  • Analyze 7.5 images (subset)
  • NIfTI-1 images (subset)
  • microPET images (only reading)

Definition in file imgfile.c.

Function Documentation

◆ imgFormatDetermine()

int imgFormatDetermine ( const char * fname,
char * basename,
char * hdrfile,
char * imgfile,
char * siffile,
int * file_format,
int * scanner,
int * type,
int * modality,
int verbose )

Determine the file format and type of an existing PET image data.

Note that this function only identifies those formats and types that were needed in TPC at the time of writing this function. Note also that this function does not care whether the format is fully or not at all supported by other library functions.

See also
imgRead, imgWrite, imgReadHeader
Returns
Returns 0 when no errors occurred, which does not mean that format was identified.
Parameters
fnameFull name of image file. In case of Analyze or microPET image, hdr or img file name, or basename without extension is accepted. Pathname is not accepted.
basenamePointer to allocated string where image file name without extensions is written (only for Analyze and microPET); enter NULL, if not needed.
hdrfilePointer to allocated string where header file name without extensions is written (only for Analyze and microPET); enter NULL, if not needed.
imgfilePointer to allocated string where image data file name without extensions is written (only for Analyze and microPET); enter NULL, if not needed.
siffilePointer to allocated string where SIF file name without extensions is written (only for Analyze), if available; enter NULL, if not needed.
file_formatPointer to int where image format ID is written; 0=unknown, for other formats see definitions in img.h
scannerPointer to int where scanner ID is written; 0=unknown, for other formats see definitions in img.h
typePointer to int where image type is written; 0=unknown, 1=image, 2=sinogram, 3=polarmap, please check definitions in img.h
modalityPointer to int where modality is written; 0=unknown, 1=PET, 2=MRI, 3=CT, ..., please check definitions in img.h
verboseVerbose level; if zero, then nothing is printed into stdout or stderr

Definition at line 536 of file imgfile.c.

566 {
567 char *cptr, temp[FILENAME_MAX];
568 int ret, fformat=IMG_UNKNOWN;
569 NIFTI_DSR nifti_dsr;
570 IMG img;
571
572 if(verbose>0) {printf("imgFormatDetermine(\"%s\", ...)\n", fname); fflush(stdout);}
573 /* Initiate results */
574 if(basename!=NULL) strcpy(basename, "");
575 if(hdrfile!=NULL) strcpy(hdrfile, "");
576 if(imgfile!=NULL) strcpy(imgfile, "");
577 if(siffile!=NULL) strcpy(siffile, "");
578 if(file_format!=NULL) *file_format=IMG_UNKNOWN;
579 if(scanner!=NULL) *scanner=SCANNER_UNKNOWN;
580 if(type!=NULL) *type=IMG_TYPE_UNKNOWN;
581 if(modality!=NULL) *modality=IMG_MODALITY_UNKNOWN;
582 if(fname==NULL) return STATUS_FAULT;
583 if(strlen(fname)<1) return STATUS_NOFILE;
584
585 /* Check the image data exists and is accessible */
586 strcpy(temp, fname);
587 if(access(temp, 0) == -1) {
588 if(verbose>1) printf(" file is not directly accessible.\n");
589 /* Try to add .nii to the name */
590 sprintf(temp, "%s.nii", fname);
591 if(access(temp, 0) == -1) {
592 if(verbose>1) printf(" file is not accessible with .nii extension.\n");
593 /* Try to add .img to the name */
594 sprintf(temp, "%s.img", fname);
595 if(access(temp, 0) == -1) {
596 if(verbose>1) printf(" file is not accessible with .img extension.\n");
597 /* Try to add .hdr to the name */
598 sprintf(temp, "%s.hdr", fname);
599 if(access(temp, 0) == -1) sprintf(temp, "%s.i.hdr", fname);
600 if(access(temp, 0) == -1) sprintf(temp, "%s.img.hdr", fname);
601 if(access(temp, 0) == -1) {
602 if(verbose>1) printf(" file is not accessible with .hdr extension.\n");
603 /* Try to add .dcm to the name */
604 sprintf(temp, "%s.dcm", fname);
605 if(access(temp, 0) == -1) {
606 if(verbose>1) printf(" file is not accessible with .dcm extension.\n");
607 return STATUS_NOFILE;
608 }
609 }
610 }
611 }
612 }
613 if(verbose>1) {printf("'%s' is accessible.\n", temp); fflush(stdout);}
614
615 /* DICOM is identified from the file name extension, and not processed any further */
616 cptr=strrchr(temp, '.');
617 if(cptr!=NULL && strcasecmp(cptr, ".DCM")==0) {
618 fformat=IMG_DICOM; if(file_format!=NULL) *file_format=fformat;
619 if(verbose>1) printf("file was identified to be in DICOM format.\n");
620 return STATUS_OK;
621 }
622
623 /* Try to read it as ECAT file first, because images which consist of
624 more than one file may reside in the same folder with
625 other formats */
626 imgInit(&img);
627 /* Is this an ECAT7 file */
628 ret=imgReadEcat7Header(fname, &img);
629 if(ret==STATUS_OK) {
630 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
631 if(verbose>1) printf("file was identified to be in ECAT7 format.\n");
632 } else if(ret==STATUS_VARMATSIZE) {
633 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
634 if(verbose>1) printf("file is ECAT7 but matrix sizes are different\n.");
635 } else if(ret==STATUS_UNKNOWNFORMAT || ret==STATUS_NOFILE) {
636 /* If main header was read but format was not identified as Ecat7,
637 it might be in Ecat6 format */
638 ret=imgReadEcat63Header(fname, &img);
639 /* if necessary, try also with .img added */
640 if(ret!=STATUS_OK && ret!=STATUS_VARMATSIZE && ret!=STATUS_MISSINGMATRIX) {
641 sprintf(temp, "%s.img", fname); ret=imgReadEcat63Header(temp, &img);}
642 if(ret==STATUS_OK) {
643 /* Is this an ECAT6 file; however this is rather uncertain step, because
644 ECAT6 files don't contain any magic number */
645 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
646 if(verbose>1) printf("file was identified to be in ECAT6 format.\n");
647 } else if(ret==STATUS_VARMATSIZE || ret==STATUS_MISSINGMATRIX) {
648 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
649 if(verbose>1) printf("file is ECAT63 but matrix sizes are different\n.");
650 }
651 }
652
653 /* If format was not yet identified, then try to read it as NIfTI;
654 this must be done before trying Analyze, because dual format NIfTI
655 is compatible with Analyze format */
656 if(fformat==IMG_UNKNOWN &&
657 niftiExists(fname, hdrfile, imgfile, siffile, &nifti_dsr, verbose-2, NULL)>0)
658 {
659 if(verbose>1) printf("file was identified to be in NIfTI format.\n");
660 /* Read NIfTI header information into IMG struct */
661 ret=imgGetNiftiHeader(&img, &nifti_dsr, verbose-2);
662 if(ret==STATUS_OK) {
663 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
664 }
665 }
666
667 /* If format was not yet identified, then try to read it as microPET */
668 if(fformat==IMG_UNKNOWN && upetExists(fname, hdrfile, imgfile, verbose-1)==2) {
669 fformat=IMG_MICROPET; if(file_format!=NULL) *file_format=fformat;
670 if(verbose>1) printf("file was identified to be in microPET format\n.");
671 /* Read header information from file */
672 ret=imgReadMicropetHeader(fname, &img);
673 if(ret==STATUS_OK) {
674 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
675 }
676 }
677
678 /* If format was not yet identified, then try to read it as Analyze */
679 if(fformat==IMG_UNKNOWN && anaDatabaseExists(fname, hdrfile, imgfile, siffile)>0) {
680 if(verbose>1) printf("file was identified to be in Analyze format.\n");
681 fformat=IMG_ANA; if(file_format!=NULL) *file_format=fformat;
682 /* Read Analyze header information */
683 ret=imgReadAnalyzeHeader(hdrfile, &img);
684 if(ret==STATUS_OK) {
685 fformat=img._fileFormat; if(file_format!=NULL) *file_format=fformat;
686 }
687 }
688
689 /* If format was not yet identified, check if it is DICOM (without .dcm extension
690 that was checked previously) */
691 if(dcmVerifyMagic(fname, NULL)) {
692 fformat=IMG_DICOM; if(file_format!=NULL) *file_format=fformat;
693 if(verbose>1) printf("file was identified to be in DICOM format.\n");
694 return STATUS_OK;
695 }
696
697 /* If format was not yet identified, check if it is Interfile */
698 if(fformat==IMG_UNKNOWN && interfileExists(fname, hdrfile, imgfile, verbose-1)!=0) {
699 fformat=IMG_INTERFILE; if(file_format!=NULL) *file_format=fformat;
700 if(verbose>1) printf("file was identified to be in Interfile format\n.");
701 }
702
703 /* Fill other information */
704 if(scanner!=NULL) *scanner=img.scanner;
705 if(type!=NULL) *type=img.type;
706 if(modality!=NULL) *modality=img.modality;
707
708 /* Quit */
709 imgEmpty(&img);
710 if(verbose>1) printf("fformat := %d\n", fformat);
711 return STATUS_OK;
712}
int anaDatabaseExists(const char *dbname, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:704
int dcmVerifyMagic(const char *filename, FILE *fp)
Definition dcm.c:157
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgReadAnalyzeHeader(const char *dbname, IMG *img)
Definition img_ana.c:360
int imgReadEcat63Header(const char *fname, IMG *img)
Definition img_e63.c:1318
int imgReadEcat7Header(const char *fname, IMG *img)
Definition img_e7.c:860
int imgGetNiftiHeader(IMG *img, NIFTI_DSR *dsr, int verbose)
Definition img_nii.c:218
int imgReadMicropetHeader(const char *dbname, IMG *img)
Definition img_upet.c:767
int interfileExists(const char *fname, char *hdrfile, char *imgfile, int verbose)
Definition interfile.c:193
#define IMG_INTERFILE
#define IMG_TYPE_UNKNOWN
#define IMG_UNKNOWN
#define IMG_DICOM
int niftiExists(const char *dbname, char *hdrfile, char *imgile, char *siffile, NIFTI_DSR *header, int verbose, char *status)
Definition nifti.c:160
#define IMG_ANA
#define IMG_MICROPET
int upetExists(const char *upetname, char *hdrfile, char *imgfile, int verbose)
Definition micropet.c:86
#define SCANNER_UNKNOWN
#define IMG_MODALITY_UNKNOWN
char type
int _fileFormat
int scanner
int modality

Referenced by imgReadHeader().

◆ imgFormatFromFName()

void imgFormatFromFName ( IMG * img,
const char * fname )

Determine IMG _fileFormat from file name extension, if not already defined. Default is ECAT 7 image volume, if nothing else can be guessed.

Note that NIfTI dual file format, Analyze, and microPET files can not be separated by file naming, thus all of these formats will be identified as Analyze by this function (default may be changed to NIfTI in future).

See also
imgRead, imgWrite
Parameters
imgTarget image structure where file format is saved; should have IMG_UNKNOWN as file type.
fnameName of file that is used to determine format.

Definition at line 483 of file imgfile.c.

488 {
489 char *cptr=NULL, *cptr2=NULL, temp[FILENAME_MAX];
490
491 if(IMG_TEST>2) printf("imgFormatFromFName(img, %s)\n", fname);
492 if(img->_fileFormat!=IMG_UNKNOWN && img->_fileFormat>0) {
493 if(IMG_TEST>3) printf(" _fileFormat := %d, not changed\n", img->_fileFormat);
494 return;
495 }
496 img->_fileFormat=IMG_E7; /* default */
497 /* get extensions */
498 strcpy(temp, fname); cptr=strrchr(temp, '.');
499 if(cptr!=NULL) {
500 *cptr=(char)0; cptr++;
501 cptr2=strrchr(temp, '.'); if(cptr2!=NULL) {*cptr2=(char)0; cptr2++;}
502 }
503 if(cptr2!=NULL) {
504 if(strcasecmp(cptr2, "i.hdr")==0) { img->_fileFormat=IMG_INTERFILE; return;}
505 if(strcasecmp(cptr2, "i.img")==0) { img->_fileFormat=IMG_INTERFILE; return;}
506 }
507 if(cptr!=NULL) {
508 if(strcasecmp(cptr, "hdr")==0) { img->_fileFormat=IMG_ANA; return;}
509 if(strcasecmp(cptr, "polmap")==0) { img->_fileFormat=IMG_POLARMAP; return;}
510 if(strcasecmp(cptr, "img")==0 ||
511 strcasecmp(cptr, "scn")==0 ||
512 strcasecmp(cptr, "nrm")==0 ||
513 strcasecmp(cptr, "atn")==0) {
514 img->_fileFormat=IMG_E63; return;
515 }
516 if(strcasecmp(cptr, "dcm")==0) { img->_fileFormat=IMG_DICOM; return;}
517 if(strcasecmp(cptr, "i")==0) { img->_fileFormat=IMG_INTERFILE; return;}
518 if(strcasecmp(cptr, "nii")==0) { img->_fileFormat=IMG_NIFTI_1S; return;}
519 } else { /* no extension at all */
520 img->_fileFormat=IMG_ANA;
521 }
522}
int IMG_TEST
Definition img.c:6
#define IMG_E7
#define IMG_NIFTI_1S
#define IMG_E63
#define IMG_POLARMAP

Referenced by imgWrite(), and imgWriteFrame().

◆ imgRead()

int imgRead ( const char * fname,
IMG * img )

Read an image or sinogram file in ECAT 6.3 or ECAT 7.x format, or image in NIfTI-1, Analyze 7.5, or microPET format.

See also
imgReadFrame, imgWrite
Returns
0 if ok, 1 invalid input, 2 image status is not 'initialized', 4 unrecognised format, 5 unsupported Ecat7 type, sets IMG->statmsg in case of error.
Parameters
fnameInput filename.
imgPointer to initialized IMG structure.
See also
imgInit, imgEmpty

Definition at line 26 of file imgfile.c.

33 {
34 FILE *fp;
35 int ret;
36 ECAT7_mainheader ecat7_main_header;
37 ECAT63_mainheader ecat63_main_header;
38 char temp[FILENAME_MAX];
39
40 if(IMG_TEST) {printf("imgRead(%s, *img)\n", fname); fflush(stdout);}
41 /* Check the arguments */
42 if(fname==NULL) {img->statmsg=imgStatus(STATUS_FAULT); return(1);}
43 if(img==NULL || img->status!=IMG_STATUS_INITIALIZED) {
44 img->statmsg=imgStatus(STATUS_FAULT); return(2);}
45
46 /* Check if we have NIfTI file, which may be in single file format,
47 or dual file format which has similar names as microPET and Analyze */
48 if(niftiExists(fname, NULL, NULL, NULL, NULL, IMG_TEST-3, NULL)>0) {
49 /* Read NIfTI image */
50 ret=imgReadNifti(fname, img, IMG_TEST);
51 if(IMG_TEST) {printf("imgReadNifti() := %d\n", ret); fflush(stdout);}
52 if(ret==STATUS_OK) {
53 if(IMG_TEST) printf("%s identified as supported NIfTI.\n", fname);
54 img->statmsg=imgStatus(STATUS_OK);
55 return(STATUS_OK);
56 }
57 img->statmsg=imgStatus(ret); return(4);
58 }
59
60 /* Check if we have microPET or Analyze file, which consist of separate header
61 and data files, and have similar names */
62 if(upetExists(fname, NULL, NULL, IMG_TEST-3)==2) {
63 /* Read microPET image */
64 ret=imgReadMicropet(fname, img); if(ret!=STATUS_OK) return(3);
65 if(IMG_TEST) printf("%s identified as microPET format.\n", fname);
66 return(0);
67 }
68 if(anaExistsNew(fname, temp, NULL, NULL)!=0) {
70 /* Read Analyze image */
71 ret=imgReadAnalyze(temp, img);
72 if(IMG_TEST) {printf("imgReadAnalyze() := %d\n", ret); fflush(stdout);}
73 if(ret==STATUS_OK) {
74 if(IMG_TEST) printf("%s identified as supported Analyze 7.5 format.\n", fname);
75 img->statmsg=imgStatus(STATUS_OK);
76 return(0);
77 }
78 if(ret==STATUS_NOSIFDATA || ret==STATUS_WRONGSIFDATA) {
79 img->statmsg=imgStatus(ret); return(0);}
80 img->statmsg=imgStatus(ret); return(4);
81 }
82
83 /* Check if we have an ECAT file */
84 /* Open file for read */
85 if((fp=fopen(fname, "rb")) == NULL) {
86 img->statmsg=imgStatus(STATUS_NOFILE); return(4);
87 }
88 /* Try to read ECAT 7.x main header */
89 ret=ecat7ReadMainheader(fp, &ecat7_main_header);
90 if(ret) {fclose(fp); img->statmsg=imgStatus(STATUS_UNKNOWNFORMAT); return(4);}
91 /* If header could be read, check for magic number */
92 if(strncmp(ecat7_main_header.magic_number, ECAT7V_MAGICNR, 7)==0) {
93 /* This is ECAT 7.x file */
94 /* Check if file type is supported */
95 if(imgEcat7Supported(&ecat7_main_header)==0) {
96 fclose(fp); img->statmsg=imgStatus(STATUS_UNSUPPORTED); return(5);
97 }
98 fclose(fp);
99 /* Read file */
100 if(IMG_TEST) printf("%s identified as supported ECAT 7.x %s format\n",
101 fname, ecat7filetype(ecat7_main_header.file_type));
102 ret=imgReadEcat7(fname, img);
103 if(ret) {if(IMG_TEST) printf("imgReadEcat7()=%d\n", ret); return(6);}
104 } else {
105 /* Check if file is in ECAT 6.3 format */
106 ret=ecat63ReadMainheader(fp, &ecat63_main_header);
107 fclose(fp);
108 if(ret==0) {
109 /* It seems to be ECAT 6.3, so read it */
110 if(IMG_TEST) printf("%s identified as supported ECAT 6.3 %s format\n",
111 fname, ecat7filetype(ecat63_main_header.file_type));
112 ret=ecat63ReadAllToImg(fname, img);
113 if(ret) {
114 if(IMG_TEST) fprintf(stderr, "ecat63ReaddAllToImg: %s\n", ecat63errmsg);
115 if(ret==6) img->statmsg=imgStatus(STATUS_MISSINGMATRIX);
116 else img->statmsg=imgStatus(STATUS_UNSUPPORTED);
117 return(6);
118 }
119 } else {img->statmsg=imgStatus(STATUS_UNKNOWNFORMAT); return(4);}
120 }
121 img->statmsg=imgStatus(STATUS_OK);
122 return(0);
123}
int anaExistsNew(const char *filename, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:45
void anaRemoveFNameExtension(char *fname)
Definition analyze.c:687
char ecat63errmsg[128]
Definition ecat63h.c:7
int ecat63ReadMainheader(FILE *fp, ECAT63_mainheader *h)
Definition ecat63r.c:25
char * ecat7filetype(short int file_type)
Definition ecat7p.c:447
int ecat7ReadMainheader(FILE *fp, ECAT7_mainheader *h)
Definition ecat7r.c:15
char * imgStatus(int status_index)
Definition img.c:330
int imgReadAnalyze(const char *dbname, IMG *img)
Definition img_ana.c:24
int ecat63ReadAllToImg(const char *fname, IMG *img)
Definition img_e63.c:22
int imgEcat7Supported(ECAT7_mainheader *h)
Definition img_e7.c:1036
int imgReadEcat7(const char *fname, IMG *img)
Definition img_e7.c:19
int imgReadNifti(const char *filename, IMG *img, int verbose)
Definition img_nii.c:23
int imgReadMicropet(const char *fname, IMG *img)
Definition img_upet.c:971
#define IMG_STATUS_INITIALIZED
short int file_type
char magic_number[14]
char status
const char * statmsg

Referenced by imgReadModelingData().

◆ imgReadFrame()

int imgReadFrame ( const char * fname,
int frame_to_read,
IMG * img,
int frame_index )

Read one time frame from a supported PET image or sinogram file into IMG data structure.

This functions can be called repeatedly to read all the frames one at a time to conserve memory.

See also
imgRead, imgReadHeader, imgWriteFrame
Returns
errstatus, which is STATUS_OK (0) when call was successful, and >0 in case of an error. Specifically, return value STATUS_NOMATRIX signals that frame does not exist, i.e. all frames have been read. IMG.statmsg can be set using ERROR_STATUS.
Parameters
fnameName of file from which IMG contents will be read. Currently supported file formats are ECAT 6.3 images and sinograms, ECAT 7.x 2D and 3D images and sinograms, and NIfTI-1, Analyze 7.5, and microPET 3D and 4D images.
frame_to_readFrame which will be read [1..frameNr].
imgPointer to initiated or occupied IMG data. If occupied, then new frame is tested to match the previous file type, dimensions, and other fundamental information contained in the IMG. If not occupied, then memory is allocated here.
frame_indexIMG frame index (0..dimt-1) where data will be placed. If index is >0, then the memory for that frame must be allocated before calling this function.

Definition at line 269 of file imgfile.c.

285 {
286 IMG test_img;
287 int ret=0;
288
289 if(IMG_TEST) {
290 printf("\nimgReadFrame(%s, %d, *img, %d)\n", fname, frame_to_read, frame_index);
291 fflush(stdout);
292 }
293 /*
294 * Check the input
295 */
296 if(fname==NULL) return STATUS_FAULT;
297 if(img==NULL) return STATUS_FAULT;
298 if(img->status!=IMG_STATUS_INITIALIZED && img->status!=IMG_STATUS_OCCUPIED) return STATUS_FAULT;
299 if(frame_to_read<1) return STATUS_FAULT;
300 if(frame_index<0) return STATUS_FAULT;
301 /* if frame_index>0, then there must be sufficient memory allocated for it */
302 if(frame_index>0) {
303 if(img->status!=IMG_STATUS_OCCUPIED) return STATUS_FAULT;
304 if(frame_index>img->dimt-1) return STATUS_FAULT;
305 }
306
307 /*
308 * If IMG is preallocated, check that fundamental header information
309 * is compatible with old and new contents.
310 * If not allocated, then read the header contents, and allocate it
311 */
312 imgInit(&test_img);
313 if(img->status==IMG_STATUS_OCCUPIED) {
314 ret=imgReadHeader(fname, &test_img, img->_fileFormat);
315 imgSetStatus(&test_img, ret);
316 if(IMG_TEST>1) printf("imgReadHeader() return message := %s\n", test_img.statmsg);
317 if(ret) return(ret);
318 if(IMG_TEST>3) imgInfo(&test_img);
319 /* Test that file format and type are the same */
320 ret=0;
321 if(img->type!=test_img.type) ret++;
322 if(img->_fileFormat!=test_img._fileFormat) ret++;
323 /* Test that x, y, and z dimensions are the same */
324 if(img->dimx!=test_img.dimx) ret++;
325 if(img->dimy!=test_img.dimy) ret++;
326 if(img->dimz!=test_img.dimz) ret++;
327 imgEmpty(&test_img); if(ret>0) return STATUS_INVALIDHEADER;
328 } else {
329 ret=imgReadHeader(fname, img, IMG_UNKNOWN);
330 imgSetStatus(img, ret);
331 if(IMG_TEST>1) printf("imgReadHeader() return message := %s\n", img->statmsg);
332 if(ret) return(ret);
333 if(IMG_TEST>3) imgInfo(img);
334 /* Allocate memory for one frame */
335 img->dimt=1;
336 ret=imgAllocate(img, img->dimz, img->dimy, img->dimx, img->dimt);
337 if(ret) return STATUS_NOMEMORY;
338 }
339
340 /*
341 * Read the frame data and corresponding information like frame time
342 * if available
343 */
344 switch(img->_fileFormat) {
345 case IMG_E7:
346 case IMG_E7_2D:
347 case IMG_POLARMAP:
348 ret=imgReadEcat7Frame(fname, frame_to_read, img, frame_index);
349 if(IMG_TEST>1) printf("imgReadEcat7Frame() return value := %d\n", ret);
350 break;
351 case IMG_E63:
352 ret=imgReadEcat63Frame(fname, frame_to_read, img, frame_index);
353 if(IMG_TEST>1) printf("imgReadEcat63Frame() return value := %d\n", ret);
354 break;
355 case IMG_ANA:
356 case IMG_ANA_L:
357 ret=imgReadAnalyzeFrame(fname, frame_to_read, img, frame_index);
358 if(IMG_TEST>1) printf("imgReadAnalyzeFrame() return value := %d\n", ret);
359 break;
360 case IMG_NIFTI_1S:
361 case IMG_NIFTI_1D:
362 ret=imgReadNiftiFrame(fname, frame_to_read, img, frame_index, IMG_TEST-2);
363 if(IMG_TEST>1) printf("imgReadNiftiFrame() return value := %d\n", ret);
364 break;
365 case IMG_MICROPET:
366 ret=imgReadMicropetFrame(fname, frame_to_read, img, frame_index);
367 if(IMG_TEST>1) printf("imgReadAnalyzeFrame() return value := %d\n", ret);
368 break;
369 default:
370 ret=STATUS_UNSUPPORTED;
371 }
372 imgSetStatus(img, ret);
373 return ret;
374}
void imgInfo(IMG *image)
Definition img.c:359
int imgAllocate(IMG *image, int planes, int rows, int columns, int frames)
Definition img.c:194
void imgSetStatus(IMG *img, int status_index)
Definition img.c:345
int imgReadAnalyzeFrame(const char *fname, int frame_to_read, IMG *img, int frame_index)
Definition img_ana.c:639
int imgReadEcat63Frame(const char *fname, int frame_to_read, IMG *img, int frame_index)
Definition img_e63.c:1503
int imgReadEcat7Frame(const char *fname, int frame_to_read, IMG *img, int frame_index)
Definition img_e7.c:1103
int imgReadNiftiFrame(const char *filename, int frame_to_read, IMG *img, int frame_index, int verbose)
Definition img_nii.c:311
int imgReadMicropetFrame(const char *fname, int frame_to_read, IMG *img, int frame_index)
Definition img_upet.c:819
int imgReadHeader(const char *fname, IMG *img, int format)
Definition imgfile.c:199
#define IMG_STATUS_OCCUPIED
#define IMG_ANA_L
#define IMG_E7_2D
#define IMG_NIFTI_1D
unsigned short int dimx
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy

Referenced by imgReadMinMax().

◆ imgReadHeader()

int imgReadHeader ( const char * fname,
IMG * img,
int format )

Fill IMG struct header information from an image or sinogram file in ECAT 6.3, ECAT 7.x or Analyze 7.5 format.

Information concerning separate frames or planes is not filled.

See also
imgInit, imgEmpty, imgReadFrame
Returns
errstatus, which is STATUS_OK (0) when call was successful, and >0 in case of an error.
Parameters
fnameImage or sinogram file name
imgPointer to initialized but not allocated IMG structure.
formatImage file format, if known (IMG_E63, IMG_E7, ...); if not known, set to IMG_UNKNOWN or 0.

Definition at line 199 of file imgfile.c.

207 {
208 int ret;
209
210 if(IMG_TEST) {
211 printf("\nimgReadHeader(%s, *img, %d)\n", fname, format);
212 fflush(stdout);
213 }
214
215 /* Check the arguments */
216 if(fname==NULL) return STATUS_FAULT;
217 if(img==NULL) return STATUS_FAULT;
218 if(img->status!=IMG_STATUS_INITIALIZED) return STATUS_FAULT;
219
220 /* If user did not know the file format, then try to decide it */
221 if(format==IMG_UNKNOWN) {
222 int scanner, imgtype, modality;
223 ret=imgFormatDetermine(fname, NULL, NULL, NULL, NULL,
224 &format, &scanner, &imgtype, &modality, IMG_TEST-3);
225 if(ret!=0) {
226 imgSetStatus(img, ret);
227 return(ret);
228 }
229 if(format==IMG_UNKNOWN) {
230 imgSetStatus(img, STATUS_UNSUPPORTED);
231 return(STATUS_UNSUPPORTED);
232 }
233 }
234
235 /* Read the header for the file format */
236 ret=STATUS_UNSUPPORTED;
237 if(format==IMG_ANA || format==IMG_ANA_L) {
238 /* Read Analyze header information */
239 ret=imgReadAnalyzeHeader(fname, img);
240 } else if(format==IMG_NIFTI_1S || format==IMG_NIFTI_1D) {
241 /* Read NIfTI image header information */
242 ret=imgReadNiftiHeader(fname, img, IMG_TEST-2);
243 } else if(format==IMG_MICROPET) {
244 /* Read microPET header information */
245 ret=imgReadMicropetHeader(fname, img);
246 } else if(format==IMG_E7 || format==IMG_E7_2D) {
247 ret=imgReadEcat7Header(fname, img);
248 } else if(format==IMG_E63 || format==IMG_POLARMAP) {
249 ret=imgReadEcat63Header(fname, img);
250 }
251 imgSetStatus(img, ret);
252 return(ret);
253}
int imgReadNiftiHeader(const char *filename, IMG *img, int verbose)
Definition img_nii.c:134
int imgFormatDetermine(const char *fname, char *basename, char *hdrfile, char *imgfile, char *siffile, int *file_format, int *scanner, int *type, int *modality, int verbose)
Definition imgfile.c:536

Referenced by imgReadFrame().

◆ imgWrite()

int imgWrite ( const char * fname,
IMG * img )

Write an image or sinogram file.

Format depends on _fileFormat or file name extension.

See also
imgInit, imgEmpty, imgRead
Returns
0 if ok, 1 invalid input, 2 invalid image type or status, 5 failed to write file, sets IMG->statmsg in case of error
Parameters
fnameFile name for output image.
imgPointer to IMG data.

Definition at line 136 of file imgfile.c.

141 {
142 int ret;
143
144 if(IMG_TEST) printf("imgWrite(%s, *img)\n", fname);
145 /* Check the arguments */
146 if(fname==NULL) return(1);
147 if(img==NULL || img->status!=IMG_STATUS_OCCUPIED) {
148 imgSetStatus(img, STATUS_FAULT); return(2);}
149 if(img->type!=IMG_TYPE_RAW &&
150 img->type!=IMG_TYPE_IMAGE &&
151 img->type!=IMG_TYPE_POLARMAP) {
152 imgSetStatus(img, STATUS_FAULT); return(2);}
153
154 /* If _fileFormat is not defined, then determine it from the file name */
155 if(img->_fileFormat==IMG_UNKNOWN) {
156 if(IMG_TEST>1) printf(" file format determined based on file name\n");
157 imgFormatFromFName(img, fname);
158 if(IMG_TEST>1) printf(" _fileFormat := %d\n", img->_fileFormat);
159 }
160
161 /* Write */
162 if(img->_fileFormat==IMG_E63) {
163 ret=ecat63WriteAllImg(fname, img);
164 switch(ret) {
165 case 0: break;
166 case 4: imgSetStatus(img, STATUS_NOMEMORY); break;
167 case 3: imgSetStatus(img, STATUS_NOWRITEPERM); break;
168 case 9: imgSetStatus(img, STATUS_DISKFULL); break;
169 default: imgSetStatus(img, STATUS_FAULT);
170 }
171 if(ret) return(7);
172 } else if(img->_fileFormat==IMG_ANA || img->_fileFormat==IMG_ANA_L) {
173 ret=imgWriteAnalyze(fname, img); if(ret) return(5);
174 } else if(img->_fileFormat==IMG_NIFTI_1S || img->_fileFormat==IMG_NIFTI_1D) {
175 ret=imgWriteNifti(fname, img, 1, IMG_TEST-1); if(ret) return(5);
176 } else if(img->_fileFormat==IMG_E7_2D) {
177 ret=imgWrite2DEcat7(fname, img); if(ret) return(5);
178 } else if(img->_fileFormat==IMG_POLARMAP) {
179 ret=imgWritePolarmap(fname, img); if(ret) return(5);
180 } else {
181 ret=imgWriteEcat7(fname, img); if(ret) return(5);
182 }
183 imgSetStatus(img, STATUS_OK);
184 return(0);
185}
int imgWriteAnalyze(const char *dbname, IMG *img)
Definition img_ana.c:162
int ecat63WriteAllImg(const char *fname, IMG *img)
Definition img_e63.c:335
int imgWrite2DEcat7(const char *fname, IMG *img)
Definition img_e7.c:526
int imgWriteEcat7(const char *fname, IMG *img)
Definition img_e7.c:382
int imgWritePolarmap(const char *fname, IMG *img)
Definition img_e7.c:661
int imgWriteNifti(const char *dbname, IMG *img, int save_sif, int verbose)
Definition img_nii.c:811
void imgFormatFromFName(IMG *img, const char *fname)
Definition imgfile.c:483
#define IMG_TYPE_RAW
#define IMG_TYPE_POLARMAP
#define IMG_TYPE_IMAGE

Referenced by imgMicropetCTToEcat7().

◆ imgWriteFrame()

int imgWriteFrame ( const char * fname,
int frame_to_write,
IMG * img,
int frame_index )

Write one PET frame from IMG data struct into a supported PET image or sinogram file.

This function can be called repeatedly to write all frames one at a time to conserve memory.

Currently supported file formats are ECAT 6.3 images and sinograms, and ECAT 7.x 2D and 3D images and sinograms, and NIfTI-1 images. Analyze 7.5 images are NOT supported (because global min and max would be needed). SIF for NIfTI-1 files is not written by this function.

See also
imgReadFrame, imgReadHeader, imgWrite
Returns
Returns errstatus, which is STATUS_OK (0) when call was successful, and >0 in case of an error.
Parameters
fnameName of file where IMG contents will be written. If file exists, data is either overwritten or catenated as a new frame, depending on the following arguments. If file does not exist, it is created.
frame_to_writePET frame number (1..frameNr) which will be written: If set to 0, frame data will be written to an existing or new PET file as a new frame, never overwriting existing data. If >0, then frame data is written as specified frame number, overwriting any data existing with the same frame number
imgPointer to the IMG data structure.
frame_indexIMG frame index (0..dimt-1) which will be written.

Definition at line 392 of file imgfile.c.

408 {
409 int ret=0;
410
411 if(IMG_TEST>0) {
412 printf("\nimgWriteFrame(%s, %d, *img, %d)\n", fname, frame_to_write, frame_index);
413 fflush(stdout);
414 }
415 if(IMG_TEST>3) {
416 char buf[32];
417 if(!ctime_r_int(&img->scanStart, buf)) strcpy(buf, "1900-01-01 00:00:00");
418 fprintf(stdout, " scan_start_time := %s\n", buf);
419 }
420
421 /*
422 * Check the input
423 */
424 if(fname==NULL) return STATUS_FAULT;
425 if(img==NULL) return STATUS_FAULT;
426 if(img->status!=IMG_STATUS_OCCUPIED) return STATUS_FAULT;
427 if(frame_to_write<0) return STATUS_FAULT;
428 if(frame_index<0 || frame_index>=img->dimt) return STATUS_FAULT;
429
430
431 /*
432 * Call separate function for each supported file format
433 */
434 imgFormatFromFName(img, fname);
435 switch(img->_fileFormat) {
436 case IMG_E7:
437 case IMG_E7_2D:
438 case IMG_POLARMAP:
439 ret=imgWriteEcat7Frame(fname, frame_to_write, img, frame_index);
440 break;
441 case IMG_E63:
442 ret=imgWriteEcat63Frame(fname, frame_to_write, img, frame_index);
443 break;
444 case IMG_ANA:
445 case IMG_ANA_L:
446 ret=STATUS_UNSUPPORTED;
447 /* Not supported because would require global min&max values
448 * if saved in short ints which is now the only possibility
449 * ret=imgWriteAnaFrame(fname, frame_to_write, img, frame_index);
450 */
451 break;
452 case IMG_NIFTI_1D:
453 case IMG_NIFTI_1S:
454#if(0)
455 /* Not supported because would require global min&max values
456 * if saved in short ints which is now the only possibility
457 */
458 ret=STATUS_UNSUPPORTED;
459#endif
460 /* Nifti is currently always written as floats, therefore
461 global min and max pixel values are not yet needed */
462 ret=imgWriteNiftiFrame(fname, frame_to_write, img, frame_index, 0, 0, IMG_TEST-2);
463 break;
464 default:
465 ret=STATUS_UNSUPPORTED;
466 }
467 imgSetStatus(img, ret);
468 return ret;
469}
char * ctime_r_int(const time_t *t, char *buf)
Convert calendard time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
Definition datetime.c:110
int imgWriteEcat63Frame(const char *fname, int frame_to_write, IMG *img, int frame_index)
Definition img_e63.c:1711
int imgWriteEcat7Frame(const char *fname, int frame_to_write, IMG *img, int frame_index)
Definition img_e7.c:1295
int imgWriteNiftiFrame(const char *dbname, int frame_to_write, IMG *img, int frame_index, float fmin, float fmax, int verbose)
Definition img_nii.c:594
time_t scanStart

Referenced by imgAnalyzeToEcat(), imgMicropetPETToEcat7(), and imgNiftiToEcat().