7#include "tpcclibConfig.h"
15static const char *img_format[] = {
41 return (
char*)img_format[c];
47static const char *img_fn_ext[] = {
72 return (
char*)img_fn_ext[c];
91 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
92 if(verbose>0) {printf(
"%s(img, %s)\n", __func__, fname); fflush(stdout);}
94 if(img==NULL ||
strnlen(fname, 2)<1) {
105 if(verbose>2) printf(
"%s is a path\n", fname);
107 if(verbose>2) printf(
"%s contains %u file(s)\n", fname, fileNr);
116 if(verbose>2) printf(
"%s is DICOM folder.\n", fname);
119 if(verbose>2) printf(
"%s is not a DICOM folder.\n", fname);
134 if(verbose>2) printf(
"%s is a file\n", fname);
137 printf(
"%s is in DICOM format.\n", fname);
144 if(verbose>2) printf(
" %s is a single DICOM file\n", fname);
147 if(verbose>2) printf(
" imgReadDICOM() := %s\n",
errorMsg(status->
error));
150 if(verbose>2) printf(
" %s is one of %d DICOM files\n", fname, fl.
keyNr);
159 if(
niftiExists(fname, NULL, NULL, NULL, NULL, status)==1) {
160 if(verbose>2) printf(
"%s is in NIfTI format.\n", fname);
168 if(
anaExists(fname, NULL, NULL, NULL, NULL, status)==1) {
170 printf(
"%s is in Analyze format.\n", fname);
180 if(
niftiExists(fname, NULL, NULL, NULL, NULL, status)==1) {
182 printf(
"%s is in NIfTI format.\n", fname);
188 if(
anaExists(fname, NULL, NULL, NULL, NULL, status)==1) {
190 printf(
"%s is in Analyze format.\n", fname);
222 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
223 if(verbose>0) {printf(
"%s(img, %s)\n", __func__, fname); fflush(stdout);}
225 if(img==NULL ||
strnlen(fname, 2)<1) {
241 }
else if(format!=img->
oformat) {
284 for(
unsigned int ti=0; ti<img->
dimt; ti++) {
304 for(
unsigned int ti=0; ti<img->
dimt; ti++) {
305 for(
unsigned int zi=0; zi<img->
dimz; zi++) {
321 float *fdata=NULL, *fptr;
322 if(verbose>2) printf(
"opening %s\n", fname);
323 if((fp=fopen(fname,
"wb")) == NULL) {
329 if(verbose>2) printf(
"allocating memory for %llu pixels\n", pxlNr);
330 fdata=(
float*)malloc(pxlNr*
sizeof(
float));
337 if(verbose>2) printf(
"writing frame at a time\n");
338 for(
int ti=0; ti<img->
dimt; ti++) {
340 for(
int zi=0; zi<img->
dimz; zi++)
341 for(
int yi=0; yi<img->
dimy; yi++)
342 for(
int xi=0; xi<img->
dimx; xi++)
343 *fptr++=img->
m[zi][yi][xi][ti];
344 if(fwrite((
float*)fdata, 4, pxlNr, fp) != pxlNr) {
345 fclose(fp); remove(fname); free(fdata);
351 fclose(fp); free(fdata);
352 if(verbose>2) printf(
"binary file written\n");
354 char miffile[strlen(fname)+5]; sprintf(miffile,
"%s.mif", fname);
355 if(verbose>2) printf(
"opening matrix information file %s\n", miffile);
356 if((fp=fopen(miffile,
"w")) == NULL) {
357 if(verbose>0) fprintf(stderr,
"Error: cannot open MIF for write.\n");
361 if(verbose>2) printf(
"writing matrix information file %s\n", miffile);
362 int n=fprintf(fp,
"%d %d %d %d\n", img->
dimz, img->
dimt, img->
dimx, img->
dimy);
365 if(verbose>0) fprintf(stderr,
"Error: cannot write in file %s\n", miffile);
369 if(verbose>2) printf(
"matrix information saved in %s\n", miffile);
400 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
401 if(verbose>0) {printf(
"%s(%s)\n", __func__, fname); fflush(stdout);}
408 if(verbose>1) printf(
"%s is an existing path\n", fname);
411 if(fname[strlen(fname)-1]==
'/' || fname[strlen(fname)-1]==
'\\') {
412 if(verbose>1) printf(
"%s is a non-existing path\n", fname);
419 if(verbose>1) printf(
"file name has no extension\n");
423 if(verbose>1) printf(
"file name extension: '%s'\n", exts);
473 if(strcasecmp(img_format[i], s)==0)
return i;
int anaExists(const char *filename, char *hdrfile, char *imgfile, char *siffile, ANALYZE_DSR *header, TPCSTATUS *status)
int dcmFileList(const char *filename, IFT *ift, TPCSTATUS *status)
List DICOM files belonging to one image.
int dcmVerifyMagic(const char *filename, FILE *fp)
char * filenameGetExtensions(const char *s)
Get all extensions of a file name.
unsigned short int pathFileNr(const char *pathname)
unsigned short int pathFileList(const char *pathname, IFT *ift)
int fileExist(const char *filename)
int imgReadDICOM(IMG *img, const char *fname, TPCSTATUS *status)
imgformat imgFormatFromFName(const char *fname, TPCSTATUS *status)
int imgRead(IMG *img, const char *fname, TPCSTATUS *status)
imgformat imgFormatIdentify(const char *s)
int imgWrite(IMG *img, const char *fname, TPCSTATUS *status)
char * imgFormatDescr(imgformat c)
char * imgDefaultExtension(imgformat c)
int imgWriteNifti(IMG *img, const char *fname, TPCSTATUS *status)
int imgReadNifti(IMG *img, const char *fname, TPCSTATUS *status)
int niftiExists(const char *filename, char *hdrfile, char *imgfile, char *siffile, NIFTI_DSR *header, TPCSTATUS *status)
int pathExist(const char *pathname)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strnlen(const char *s, size_t n)
char * strcasestr(const char *haystack, const char *needle)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
@ TPCERROR_NO_FILE
File does not exist.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_HEADER
Invalid header contents.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_INVALID_FILENAME
Invalid file name.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_WRITE
Cannot write file.
Header file for libtpcimage.
@ IMG_FORMAT_POLARMAP
ECAT polar map.
@ IMG_FORMAT_E7
ECAT 7.* 3D.
@ IMG_FORMAT_ANA_L
Analyze little-endian.
@ IMG_FORMAT_E63
ECAT 6.3.
@ IMG_FORMAT_INTERFILE
Interfile.
@ IMG_FORMAT_NIFTI_1D
NIfTI-1 dual-file format.
@ IMG_FORMAT_UNKNOWN
Unknown format.
@ IMG_FORMAT_LAST
End of list.
@ IMG_FORMAT_NIFTI_1S
NIfTI-1 single-file format.
@ IMG_FORMAT_MICROPET
MicroPET.
@ IMG_FORMAT_FLAT
Flat format.