summaryrefslogtreecommitdiff
path: root/src/e2e.c
blob: cb5dc62b66793f2c2bab0dd546ffbe44dbd98f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "e2e.h"

static uint16_t u16le(const char *src)
{
	uint8_t bytes[2];

	memcpy(bytes, src, 2);

	return (uint16_t)bytes[0]
	       + (((uint16_t)bytes[1]) << 8);
}

static uint32_t u32le(const char *src)
{
	uint8_t bytes[4];

	memcpy(bytes, src, 4);

	return (uint32_t)bytes[0]
	       + (((uint32_t)bytes[1]) << 8)
	       + (((uint32_t)bytes[2]) << 16)
	       + (((uint32_t)bytes[3]) << 24);
}

static int make_pointer(const struct e2e_data *data, const char *pos,
                        const char **out)
{
	uint32_t offs;

	offs = u32le(pos);

	if (!offs)
		*out = NULL;
	else {
		if (offs > data->end - data->start)
			return 1;

		*out = data->start + offs;
	}

	return 0;
}

#define HEADER_END 36

#define DIRECTORY_NUM_ENTRIES   36
#define DIRECTORY_CURRENT       40
#define DIRECTORY_NEXT          44
#define DIRECTORY_END          52

#define ENTRY_POS          0
#define ENTRY_START        4 
#define ENTRY_SIZE         8
#define ENTRY_PATIENT_ID   16
#define ENTRY_STUDY_ID     20
#define ENTRY_SERIES_ID    24
#define ENTRY_SLICE_ID     28
#define ENTRY_TYPE         36
#define ENTRY_END          44

#define CHUNK_POS          20
#define CHUNK_SIZE         24
#define CHUNK_PATIENT_ID   32
#define CHUNK_STUDY_ID     36
#define CHUNK_SERIES_ID    40
#define CHUNK_SLICE_ID     44
#define CHUNK_IND          48
#define CHUNK_TYPE         52
#define CHUNK_HEADER_END   60

#define IMAGE_SIZE      0
#define IMAGE_TYPE      4
#define IMAGE_WIDTH     12
#define IMAGE_HEIGHT    16
#define IMAGE_IMAGE     20

#define UF16_MANTISSA_MASK  ((1 << 11) - 1)  // 10 most significant bits
#define UF16_EXP_OFFSET -38 // FIXME: pure guess

static int read_fundus(struct e2e_data *data, const char *cursor,
                       struct e2e_image *image)
{
	// This cast is _technically_ illegal, but I can't think of a way in
	// which this would cause problems in practice.
	image->fundus = (const uint8_t*)cursor;

	return 0;
}

static int read_tomogram(struct e2e_data *data, const char *cursor,
                         struct e2e_image *image)
{
	image->tomogram = calloc(image->width * image->height, sizeof(float));
	if (!image->tomogram)
		return ENOMEM;

	for (size_t y = 0; y < image->height; y++)
	for (size_t x = 0; x < image->width; x++) {
		size_t i = y * image->width + x;
		uint16_t raw;
		int exp, man;

		raw = u16le(cursor + i * 2);
		man = raw & UF16_MANTISSA_MASK;
		exp = (raw & ~UF16_MANTISSA_MASK) >> 10;
		image->tomogram[i] = ldexpf(man, exp2f(exp + UF16_EXP_OFFSET));
	}

	return 0;
}

static int read_image(struct e2e_data *data, const char *cursor,
                      struct e2e_image *image)
{
	if (cursor + IMAGE_IMAGE > data->end) {
		fprintf(stderr, "Image data at %td ends past the end of file.\n",
		        cursor - data->start);
		return 1;
	}

	image->size = u32le(cursor + IMAGE_SIZE);
	image->type = u32le(cursor + IMAGE_TYPE);
	image->width = u32le(cursor + IMAGE_WIDTH);
	image->height = u32le(cursor + IMAGE_HEIGHT);
	cursor += IMAGE_IMAGE;

	switch (image->type) {
	case IMAGE_FUNDUS:
		return read_fundus(data, cursor, image);

	case IMAGE_TOMOGRAM:
		return read_tomogram(data, cursor, image);
	}

	return 0;
}

static int read_entry(struct e2e_data *data, const char *cursor,
                      struct e2e_entry *entry)
{
	const char *pos;
	struct e2e_chunk *chunk;

	if (cursor + ENTRY_END > data->end) {
		fprintf(stderr, "Entry at %td data->ends past the data->end of file.\n",
		        cursor - data->start);
		return 1;
	}

	if (make_pointer(data, cursor + ENTRY_POS, &pos)) {
		fprintf(stderr, "Entry at %td has a bad 'pos' entry.\n",
		        cursor - data->start);
		return 1;
	}

	if (pos != cursor) {
		fprintf(stderr, "Entry at %td has a 'pos' entry that differs from the entry's actual position",
		        cursor - data->start);
		return 1;
	}

	if (make_pointer(data, cursor + ENTRY_START, &entry->start)) {
		fprintf(stderr, "Entry at %td has a bad 'data->start' entry.\n",
		        cursor - data->start);
		return 1;
	}

	entry->size = u32le(cursor + ENTRY_SIZE);
	entry->patient_id = u32le(cursor + ENTRY_PATIENT_ID);
	entry->study_id = u32le(cursor + ENTRY_STUDY_ID);
	entry->series_id = u32le(cursor + ENTRY_SERIES_ID);
	entry->slice_id = u32le(cursor + ENTRY_SLICE_ID);
	entry->type = u32le(cursor + ENTRY_TYPE);

	if (!entry->start || entry->start < data->start + HEADER_END)
		return 0;

	cursor = entry->start;
	chunk = &entry->chunk;
	entry->has_chunk = 1;

	chunk->entry = entry;
	chunk->origin = cursor;

	if (cursor + CHUNK_HEADER_END > data->end) {
		fprintf(stderr, "Chunk at %td ends past the end of file.\n",
		        cursor - data->start);
		return 1;
	}

	if (make_pointer(data, cursor + CHUNK_POS, &pos)) {
		fprintf(stderr, "Chunk at %td has a bad 'pos' entry.\n",
		        cursor - data->start);
		return 1;
	}

	if (pos != cursor) {
		fprintf(stderr, "Chunk at %td has a 'pos' entry that differs from the entry's actual position.\n",
		        cursor - data->start);
		return 1;
	}

	chunk->size = u32le(cursor + CHUNK_SIZE);
	chunk->patient_id = u32le(cursor + CHUNK_PATIENT_ID);
	chunk->study_id = u32le(cursor + CHUNK_STUDY_ID);
	chunk->series_id = u32le(cursor + CHUNK_SERIES_ID);
	chunk->slice_id = u32le(cursor + CHUNK_SLICE_ID);
	chunk->type = u32le(cursor + CHUNK_TYPE);
	cursor += CHUNK_HEADER_END;

	switch (chunk->type) {
	case CHUNK_IMAGE:
		return read_image(data, cursor, &chunk->image);
	}

	return 0;
}

static int read_directory(struct e2e_data *data, const char *cursor,
                          struct e2e_directory *dir)
{
	if (cursor + DIRECTORY_END > data->end) {
		fprintf(stderr, "Directory at %td data->ends past the data->end of file.\n",
		        cursor - data->start);
		return 1;
	}

	dir->num_entries = u32le(cursor + DIRECTORY_NUM_ENTRIES);

	if (make_pointer(data, cursor + DIRECTORY_CURRENT, &dir->current))  {
		fprintf(stderr, "Directory at %td contains a bad 'current' field.\n",
		        cursor - data->start);
		return 1;
	}

	if (make_pointer(data, cursor + DIRECTORY_NEXT, &dir->next))  {
		fprintf(stderr, "Directory at %td contains a bad 'current' field.\n",
		        cursor - data->start);
		return 1;
	}

	return 0;
}

// Because entries are a part of a larger array in e2e_data, they're freed
// all at once by e2e_destroy and not here.
void e2e_entry_destroy(struct e2e_entry *entry)
{
	if (entry->has_chunk
	    && entry->chunk.type == CHUNK_IMAGE
	    && entry->chunk.image.type == IMAGE_TOMOGRAM)
		free(entry->chunk.image.tomogram);
}

void e2e_destroy(struct e2e_data *data)
{
	struct e2e_directory *dir, *next;

	for (dir = data->dirs; dir; dir = next) {
		next = dir->data_list.next;

		for (size_t i = 0; i < dir->num_entries; i++)
			e2e_entry_destroy(dir->entries + i);
		free(dir->entries);
		free(dir);
	}
}

int e2e_read(struct e2e_data *data, const char *start, const char *end)
{
	int rv = 0;
	const char *cursor;

	data->start = start;
	data->end = end;
	data->dirs = NULL;

	if (start + HEADER_END > end) {
		fprintf(stderr, "File too short to be an E2E file.\n");
		return 1;
	}

	// TODO: verify the header

	cursor = start + HEADER_END;
	cursor += DIRECTORY_END; // skip the first directory (wtf?)

	while (cursor) {
		struct e2e_directory *dir;
		size_t i;

		dir = malloc(sizeof(struct e2e_directory));
		if (!dir) {
			rv = ENOMEM;
			goto error;
		}

		rv = read_directory(data, cursor, dir);
		if (rv)
			goto error_read_directory;

		dir->entries = calloc(dir->num_entries, sizeof(struct e2e_entry));
		if (!dir->entries) {
			rv = ENOMEM;
			goto error_alloc_entries;
		}

		for (i = 0; i < dir->num_entries; i++) {
			const char *entry_cursor;
			struct e2e_entry *entry = dir->entries + i;

			entry_cursor = cursor + DIRECTORY_END + i * ENTRY_END;
			rv = read_entry(data, entry_cursor, entry);
			if (rv)
				goto error_read_entry;
		}

		eli_append(&data->dirs, dir, data_list);
		cursor = dir->next;
		continue;


	error_read_entry:
		for (size_t j = 0; j < i; j++)
			e2e_entry_destroy(dir->entries + j);
		free(dir->entries);
	error_alloc_entries:
	error_read_directory:
		free(dir);
		goto error;
	}

	return 0;

error:
	e2e_destroy(data);
	return rv;
}