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

Procedures for reading Siemens Inveon micropET images. More...

#include "tpcclibConfig.h"
#include "tpcmicropet.h"

Go to the source code of this file.

Functions

int micropetHeaderRead (const char *hdrfile, IFT *header, TPCSTATUS *status)
 
int micropetExists (const char *filename, char *hdrfile, char *imgfile, IFT *header, TPCSTATUS *status)
 Verify if specified file name refers to an existing microPET file.
 

Detailed Description

Procedures for reading Siemens Inveon micropET images.

Definition in file micropetio.c.

Function Documentation

◆ micropetExists()

int micropetExists ( const char * filename,
char * hdrfile,
char * imgfile,
IFT * header,
TPCSTATUS * status )

Verify if specified file name refers to an existing microPET file.

Other image formats may have similar names (*.img and *img.hdr), thus image header contents are verified here. Pixel data (*.img) is only verified to exist.

See also
niftiExists, anaExists, micropetHeaderRead
Returns
Returns 0 if it is not microPET, otherwise the major version of microPET format.
Todo
Finalize and test.
Parameters
filenameFile name, either header file (*.img.hdr), image file (*.img), or base name without extensions .img, .img.hdr, .pet.img, .pet.img.hdr, .ct.img, or .ct.img.hdr
hdrfileIf file name refers to a microPET file, then header file name will be written in this char pointer (space needs to allocated by caller); enter NULL if not needed.
imgfileIf file name refers to a microPET file, then image file name will be written in this char pointer (space needs to allocated by caller); enter NULL if not needed.
headerPointer to microPET header contents, which is filled in this function; any previous contents are removed; enter NULL, if not needed.
statusPointer to status data; enter NULL if not needed.

Definition at line 84 of file micropetio.c.

99 {
100 int verbose=0; if(status!=NULL) verbose=status->verbose;
101 if(verbose>0) {printf("%s('%s', ...)\n", __func__, filename); fflush(stdout);}
102 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
103
104 /* Initiate output */
105 if(hdrfile!=NULL) hdrfile[0]=(char)0;
106 if(imgfile!=NULL) imgfile[0]=(char)0;
107 if(header!=NULL) iftFree(header);
108
109 /* Empty file name means not a microPET file */
110 if(filename==NULL || strnlen(filename, 2)<1) {
111 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
112 return(0);
113 }
114
115 char local_imgfile[FILENAME_MAX], local_hdrfile[FILENAME_MAX+4];
116
117 /* If there are extensions, what is the last extension? */
118 /* Based on that, try to make image and header file names */
119 char *last_extension=filenameGetExtension(filename);
120 if(verbose>1) {printf(" last extension := '%s'\n", last_extension); fflush(stdout);}
121 if(last_extension==NULL) { // Base name without any extension was given
122 strcpy(local_imgfile, filename); strlcat(local_imgfile, ".img", FILENAME_MAX);
123 strcpy(local_hdrfile, local_imgfile); strlcat(local_hdrfile, ".hdr", FILENAME_MAX);
124 } else if(strcasecmp(last_extension, ".IMG")==0) { // img file name was given
125 strcpy(local_imgfile, filename);
126 strcpy(local_hdrfile, local_imgfile); strlcat(local_hdrfile, ".hdr", FILENAME_MAX);
127 } else if(strcasecmp(last_extension, ".HDR")==0) { // header file name was given
128 strcpy(local_imgfile, filename); filenameRmExtension(local_imgfile);
129 strcpy(local_hdrfile, filename);
130 } else { // Base name, including extra extensions, was given
131 strcpy(local_imgfile, filename); strlcat(local_imgfile, ".img", FILENAME_MAX);
132 strcpy(local_hdrfile, local_imgfile); strlcat(local_hdrfile, ".hdr", FILENAME_MAX);
133 }
134 if(verbose>1) {
135 printf(" tentative image file name := '%s'\n", local_imgfile);
136 printf(" tentative header file name := '%s'\n", local_hdrfile);
137 fflush(stdout);
138 }
139
140 /* Check that files do exist */
141 {
142 int bothExist=1;
143 if(!fileExist(local_hdrfile)) {
144 bothExist=0;
145 if(verbose>1) printf(" '%s' does not exist.\n", local_hdrfile);
146 }
147 if(!fileExist(local_imgfile)) {
148 bothExist=0;
149 if(verbose>1) printf(" '%s' does not exist.\n", local_imgfile);
150 }
151 if(!bothExist) {
152 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_FILE);
153 return(0);
154 }
155 }
156
157 /* Read header to check that this indeed is a microPET file */
158 IFT ift, *local_hdr;
159 iftInit(&ift);
160 if(header==NULL) local_hdr=&ift; else local_hdr=header;
161 if(micropetHeaderRead(local_hdrfile, local_hdr, status)!=TPCERROR_OK) {
162 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
163 return(0);
164 }
165
166 /* Read file version from the header */
167 double version=0.0;
168 if(iftGetDoubleValue(local_hdr, "version", 0, &version)<0) {
169 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
170 iftFree(&ift); return(0);
171 }
172 iftFree(&ift);
173 if(verbose>1) {printf(" version := %g\n", version); fflush(stdout);}
174
175 /* Set output */
176 if(hdrfile!=NULL) strlcpy(hdrfile, local_hdrfile, FILENAME_MAX);
177 if(imgfile!=NULL) strlcpy(imgfile, local_imgfile, FILENAME_MAX);
178
179 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
180 return((int)version);
181}
char * filenameGetExtension(const char *s)
Get the last extension of a file name.
Definition filename.c:178
int filenameRmExtension(char *s)
Definition filename.c:71
int fileExist(const char *filename)
Definition filexist.c:17
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftGetDoubleValue(IFT *ift, const char *key, int index, double *v)
Definition iftfind.c:191
int micropetHeaderRead(const char *hdrfile, IFT *header, TPCSTATUS *status)
Definition micropetio.c:14
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592
Definition tpcift.h:43
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_NO_FILE
File does not exist.
@ TPCERROR_INVALID_HEADER
Invalid header contents.
@ TPCERROR_OK
No error.
@ TPCERROR_INVALID_FILENAME
Invalid file name.

◆ micropetHeaderRead()

int micropetHeaderRead ( const char * hdrfile,
IFT * header,
TPCSTATUS * status )

Read microPET header and verify that it is a valid Concorde/microPET file header.

See also
micropetExists
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
hdrfileConcorde/microPET header file name, with correct extension and path.
headerPointer to microPET header contents, which is filled in this function; any previous contents are removed; enter NULL, if not needed.
statusPointer to status data; enter NULL if not needed.

Definition at line 14 of file micropetio.c.

22 {
23 int verbose=0; if(status!=NULL) verbose=status->verbose;
24 if(verbose>0) {printf("%s('%s', ...)\n", __func__, hdrfile); fflush(stdout);}
25 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
26
27 /* Empty file name or shorter than 'x.hdr' means not a microPET header file */
28 if(hdrfile==NULL || strnlen(hdrfile, 6)<5) {
29 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
31 }
32
33 /* Read header */
34 IFT ift, *local_hdr;
35 iftInit(&ift);
36 if(header==NULL) local_hdr=&ift; else local_hdr=header;
37 FILE *fp;
38 if((fp=fopen(hdrfile, "r"))==NULL) {
39 if(verbose>1) printf(" cannot open header file.\n");
40 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_OPEN);
42 }
43 if(iftRead(local_hdr, fp, 2, 0, NULL)!=TPCERROR_OK) {
44 if(verbose>1) printf(" cannot read header file.\n");
45 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
46 iftFree(&ift); fclose(fp);
48 }
49 fclose(fp);
50
51 /* Check that certain header parameters do exist */
52 if(verbose>1) printf(" checking header contents...\n");
53 if(iftFindKey(local_hdr, "version", 0)<0) {
54 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
55 iftFree(&ift); return(TPCERROR_INVALID_HEADER);
56 }
57 if(iftFindKey(local_hdr, "model", 0)<0) {
58 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
59 iftFree(&ift); return(TPCERROR_INVALID_HEADER);
60 }
61 if(iftFindKey(local_hdr, "institution", 0)<0) {
62 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
63 iftFree(&ift); return(TPCERROR_INVALID_HEADER);
64 }
65 if(iftFindKey(local_hdr, "number_of_dimensions", 0)<0) {
66 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
67 iftFree(&ift); return(TPCERROR_INVALID_HEADER);
68 }
69 iftFree(&ift);
70
71 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
72 return(TPCERROR_OK);
73}
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
Definition iftio.c:130
@ TPCERROR_CANNOT_OPEN
Cannot open file.

Referenced by micropetExists().