TPCCLIB
Loading...
Searching...
No Matches
ecatio.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <time.h>
11#include <string.h>
12/*****************************************************************************/
13#include "tpcextensions.h"
14/*****************************************************************************/
15#include "tpcecat.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
26 const char *filename,
30 FILE *fp,
33 const unsigned int blocknumber,
35 unsigned char *data
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}
70/*****************************************************************************/
71
72/*****************************************************************************/
87 const char *filename,
92 FILE *fp
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}
119/*****************************************************************************/
120
121/*****************************************************************************/
int ecatReadBlock(const char *filename, FILE *fp, const unsigned int blocknumber, unsigned char *data)
Definition ecatio.c:23
int ecatVerifyMagic(const char *filename, FILE *fp)
Definition ecatio.c:84
Header file for libtpcecat.
#define ECATBLKSIZE
Definition tpcecat.h:37
Header file for library libtpcextensions.
@ 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.