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 537 of file imgfile.c.

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

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

Referenced by imgFormatFromFName(), 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
122 img->statmsg=imgStatus(STATUS_OK);
123 return(0);
124}
int anaExistsNew(const char *filename, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:43
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 imgRead(), and 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 270 of file imgfile.c.

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

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

Referenced by imgReadFrame(), and imgReadHeader().

◆ 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 137 of file imgfile.c.

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

Referenced by imgMicropetCTToEcat7(), and imgWrite().

◆ 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 393 of file imgfile.c.

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