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

Basic IO functions for ECAT files. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcecat.h"

Go to the source code of this file.

Functions

int ecatReadBlock (const char *filename, FILE *fp, const unsigned int blocknumber, unsigned char *data)
 
int ecatVerifyMagic (const char *filename, FILE *fp)
 

Detailed Description

Basic IO functions for ECAT files.

Definition in file ecatio.c.

Function Documentation

◆ ecatReadBlock()

int ecatReadBlock ( const char * filename,
FILE * fp,
const unsigned int blocknumber,
unsigned char * data )

Read specified data block from an ECAT file.

See also
ecatVerifyMagic
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
filenameName of file to open and read specified data block; file is closed after reading; enter NULL to use the file pointer (next argument) instead.
fpFile pointer of file, opened with fp=fopen(filename, "rb"), to read the specified data block from; enter NULL to open file (previous argument) locally. This function leaves the file pointer to the end of data block.
blocknumberBlock number to read; enter 0 to read the block from current file pointer position, if file pointer was given.
dataPointer to write block data in; must be allocated for at least ECATBLKSIZE bytes.

Definition at line 23 of file ecatio.c.

36 {
37 if(filename==NULL && fp==NULL) return(TPCERROR_FAIL);
38 if(data==NULL) return(TPCERROR_FAIL);
39
40 if(fp!=NULL) { // read from given file pointer
41
42 /* If block number was given, then seek the block */
43 if(blocknumber>0) {
44 if(fseeko(fp, (off_t)(blocknumber-1)*ECATBLKSIZE, SEEK_SET) != 0)
45 return(TPCERROR_NO_DATA);
46 }
47 /* Read the block */
48 if(fread(data, ECATBLKSIZE, 1, fp)<1) return(TPCERROR_CANNOT_READ);
49
50 } else { // read from given file name
51
52 /* Open file for read */
53 FILE *localfp=fopen(filename, "rb");
54 if(localfp==NULL) return(TPCERROR_CANNOT_OPEN);
55
56 /* If block number was given, then seek the block */
57 if(blocknumber>0) {
58 if(fseeko(localfp, (off_t)(blocknumber-1)*ECATBLKSIZE, SEEK_SET) != 0)
59 {fclose(localfp); return(TPCERROR_NO_DATA);}
60 }
61 /* Read the block */
62 if(fread(data, ECATBLKSIZE, 1, localfp)<1) {fclose(localfp); return(TPCERROR_CANNOT_READ);}
63 fclose(localfp);
64
65 }
66
67 /* That's it then */
68 return(TPCERROR_OK);
69}
#define ECATBLKSIZE
Definition tpcecat.h:37
@ TPCERROR_FAIL
General error.
@ TPCERROR_CANNOT_OPEN
Cannot open file.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_READ
Cannot read file.

Referenced by ecatMListRead(), and ecatVerifyMagic().

◆ ecatVerifyMagic()

int ecatVerifyMagic ( const char * filename,
FILE * fp )

Verify that given file (either file name or file pointer) appears to be ECAT file, based on the magic number.

ECAT 7 has defined magic numbers, which are checked. ECAT 6 format does not a defined magic number, therefore here the file is only checked to not indicate anything else than ECAT 6.

Returns
Returns 0 if magic number is definitely not ECAT; 7 if ECAT 7 magic number is found, and 6 if magic number does not indicate anything other than ECAT 6.
Author
Vesa Oikonen
Parameters
filenameName of file to open and to verify for the magic number; file is closed after reading; enter NULL to use the file pointer (next argument) instead.
fpFile pointer of file to check, opened with fp=fopen(filename, "rb"); enter NULL to open file (previous argument) locally. This function rewinds the file pointer to start before and after reading the data.

Definition at line 84 of file ecatio.c.

93 {
94
95 /* Read the first data block (512 bytes) */
96 unsigned char buf[ECATBLKSIZE];
97 int ret=ecatReadBlock(filename, fp, 0, buf);
98 if(fp!=NULL) rewind(fp);
99 if(ret!=TPCERROR_OK) return(0);
100
101 /* Check if the start matches the ECAT 7 magic numbers */
102 if(strncmp((char*)buf, "MATRIX72v", 9)==0 || strncmp((char*)buf, "MATRIX7011", 10)==0) return(7);
103
104 /* ECAT 6 does not have defined magic numbers, but check anyway */
105 if(strncmp((char*)buf, "MATRIX6", 7)==0 || strncmp((char*)buf, "ECAT6", 5)==0) return(6);
106
107 /* Check that we do NOT have DICOM magic number */
108 if(strncmp((char*)buf+128, "DICM", 4)==0) return(0);
109
110 /* Check that we do NOT have NIfTI magic number */
111 if(strncmp((char*)buf+344, "ni1", 3)==0 || strncmp((char*)buf, "n+1", 3)==0) return(0);
112
113 /* Check that we do NOT have Interfile magic number */
114 if(strncmp((char*)buf, "!INTERFILE", 10)==0) return(0);
115
116 /* There is no indication that this IS NOT an ECAT 6 file */
117 return(6);
118}
int ecatReadBlock(const char *filename, FILE *fp, const unsigned int blocknumber, unsigned char *data)
Definition ecatio.c:23