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

Reading and writing ECAT headers. More...

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

Go to the source code of this file.

Functions

int ecatWriteMainheader (IFT *ift, unsigned char *buf, TPCSTATUS *status)
 
int ecatReadMainheader (const unsigned char *buf, IFT *ift, TPCSTATUS *status)
 

Detailed Description

Reading and writing ECAT headers.

Definition in file ecatheader.c.

Function Documentation

◆ ecatReadMainheader()

int ecatReadMainheader ( const unsigned char * buf,
IFT * ift,
TPCSTATUS * status )

Read ECAT main header from data block into IFT struct.

Remarks
Stub function.
See also
ecatWriteMainheader, ecatVerifyMagic
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
bufPointer to data block containing main header.
iftPointer to initiated IFT struct; any previous contents are NOT deleted.
statusPointer to status data; enter NULL if not needed.

Definition at line 100 of file ecatheader.c.

107 {
108 int verbose=0; if(status!=NULL) verbose=status->verbose;
109 if(verbose>0) {printf("%s()\n", __func__); fflush(stdout);}
110
111 if(buf==NULL || ift==NULL) {
112 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
113 return(TPCERROR_FAIL);
114 }
115
116 /* Is current platform little endian (1) or not (0) ? */
117 int little=endianLittle();
118 if(verbose>1) {
119 if(little) printf("little endian platform\n");
120 else printf("big endian platform\n");
121 }
122
123 /* Check if the start matches the ECAT 7 magic numbers */
124 int format=6;
125 if(strncmp((char*)buf, "MATRIX72v", 9)==0 || strncmp((char*)buf, "MATRIX7011", 10)==0) format=7;
126 if(verbose>1) {printf("format := %d\n", format); fflush(stdout);}
127
128 /* Copy the header fields to IFT; byte swapping when necessary */
129 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
130 char tmp[512];
131
132 if(format==7) {
133
134 strlcpy(tmp, (char*)buf+0, 15);
135 if(iftPut(ift, "magic_number", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
136 strlcpy(tmp, (char*)buf+14, 33);
137 if(iftPut(ift, "original_file_name", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
138
139 } else { // ECAT 6
140
141 strlcpy(tmp, (char*)buf+0, 15);
142 if(iftPut(ift, "magic_number", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
143 strlcpy(tmp, (char*)buf+28, 21);
144 if(iftPut(ift, "original_file_name", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
145
146 }
147
148
149 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
150 return(TPCERROR_OK);
151}
int endianLittle()
Definition endian.c:53
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.

◆ ecatWriteMainheader()

int ecatWriteMainheader ( IFT * ift,
unsigned char * buf,
TPCSTATUS * status )

Write ECAT main header from IFT struct into data block.

Remarks
Stub function.
See also
ecatReadMainheader, ecatVerifyMagic
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
iftPointer to IFT struct containing main header fields; contents are not modified.
bufPointer to data block of size ECATBLKSIZE bytes.
statusPointer to status data; enter NULL if not needed.

Definition at line 25 of file ecatheader.c.

32 {
33 int verbose=0; if(status!=NULL) verbose=status->verbose;
34 if(verbose>0) {printf("%s()\n", __func__); fflush(stdout);}
35
36 if(ift==NULL || buf==NULL) {
37 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
38 return(TPCERROR_FAIL);
39 }
40 if(ift->keyNr<1) {
41 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_MISSING_DATA);
43 }
44
45 /* Is current platform little endian (1) or not (0) ? */
46 int little=endianLittle();
47 if(verbose>1) {
48 if(little) printf("little endian platform\n");
49 else printf("big endian platform\n");
50 }
51
52 /* Determine ECAT format version */
53 int format=6;
54 /* If ECAT 7 magic_number not found, then assume ECAT 6 */
55 int ii=iftFindKey(ift, "magic_number", 0);
56 if(ii>=0) {
57 if(strncmp(ift->item[ii].value, "MATRIX72v", 9)==0 ||
58 strncmp(ift->item[ii].value, "MATRIX7011", 10)==0) format=7;
59 }
60 if(verbose>1) {printf("format := %d\n", format); fflush(stdout);}
61
62 /* Copy the header fields; byte swapping when necessary */
63 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_MISSING_DATA);
64 char tmp[512];
65
66 if(format==7) {
67
68 ii=iftFindKey(ift, "magic_number", 0);
69 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 15); else strcpy(tmp, "MATRIX72v");
70 memcpy((char*)buf+0, tmp, 14);
71
72 ii=iftFindKey(ift, "original_file_name", 0);
73 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 33); else strcpy(tmp, "");
74 memcpy((char*)buf+14, tmp, 32);
75
76 } else { // ECAT 6
77
78 ii=iftFindKey(ift, "magic_number", 0);
79 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 15); else strcpy(tmp, "MATRIX6");
80 memcpy((char*)buf+0, tmp, 14);
81
82 ii=iftFindKey(ift, "original_file_name", 0);
83 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 21); else strcpy(tmp, "");
84 memcpy((char*)buf+28, tmp, 20);
85
86 }
87
88
89 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
90 return(TPCERROR_OK);
91}
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
char * value
Definition tpcift.h:37
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
@ TPCERROR_MISSING_DATA
File contains missing values.

Referenced by imgWrite().