-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzzwrite.c
543 lines (482 loc) · 14.9 KB
/
zzwrite.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include <assert.h>
#include <string.h>
#include <errno.h>
#include "zz_priv.h"
#include "byteorder.h"
#include "zzwrite.h"
#ifdef DEBUG
#define verboseprint(_zz, ...) \
{ \
if (zzisverbose()) \
{ \
char vrstr[MAX_LEN_VR]; \
fprintf(stdout, " \033[22m\033[32m(%04x,%04x)\033[0m %s ", \
_zz->current.group, _zz->current.element, zzvr2str(zz->current.vr, vrstr)); \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, "\n"); \
} \
}
#else
#define verboseprint(...) {}
#endif
static inline bool explicit(struct zzfile *zz) { return zz->ladder[zz->ladderidx].txsyn == ZZ_EXPLICIT; }
static inline void implicit(struct zzio *zi, uint16_t group, uint16_t element, uint32_t length)
{
ziwrite(zi, &group, 2);
ziwrite(zi, &element, 2);
ziwrite(zi, &length, 4);
}
static inline void explicit1(struct zzio *zi, uint16_t group, uint16_t element, const char *vr, uint16_t length)
{
ziwrite(zi, &group, 2);
ziwrite(zi, &element, 2);
ziwrite(zi, vr, 2);
ziwrite(zi, &length, 2);
}
static inline void explicit2(struct zzio *zi, uint16_t group, uint16_t element, const char *vr, uint32_t length)
{
uint16_t zero = 0;
ziwrite(zi, &group, 2);
ziwrite(zi, &element, 2);
ziwrite(zi, vr, 2);
ziwrite(zi, &zero, 2);
ziwrite(zi, &length, 4);
}
static inline void writetag(struct zzfile *zz, zzKey key, enum VR vr, uint32_t size)
{
const uint16_t group = ZZ_GROUP(key);
const uint16_t element = ZZ_ELEMENT(key);
char dest[MAX_LEN_VR];
zz->current.element = element;
zz->current.group = group;
zz->current.vr = vr;
zz->current.length = size;
if (zz->ladder[zz->ladderidx].txsyn != ZZ_EXPLICIT || group == 0xfffe)
{
implicit(zz->zi, group, element, size);
}
else
{
switch (vr)
{
case OB: case OW: case OF: case SQ: case UT: case UN:
explicit2(zz->zi, group, element, zzvr2str(vr, dest), size); break;
case AE: case AS: case AT: case CS: case DA: case DS: case DT: case FL: case FD: case IS: case LO:
case LT: case PN: case SH: case SL: case SS: case ST: case TM: case UI: case UL: case US: case HACK_VR:
explicit1(zz->zi, group, element, zzvr2str(vr, dest), size); break;
default: // unknown VR, always 32-bit length
explicit2(zz->zi, group, element, zzvr2str(vr, dest), size); break;
}
}
}
void zzwCopy(struct zzfile *zz, const struct zzfile *orig)
{
writetag(zz, ZZ_KEY(orig->current.group, orig->current.element), orig->current.vr, orig->current.length);
if (orig->current.length != UNLIMITED && orig->current.length > 0 && orig->current.vr != SQ && orig->current.group != 0xfffe)
{
zisetreadpos(orig->zi, orig->current.pos); // reposition read marker to beginning of data
zicopy(zz->zi, orig->zi, orig->current.length);
}
}
void zzwSQ(struct zzfile *zz, zzKey key, uint32_t size)
{
writetag(zz, key, SQ, size);
verboseprint(zz, "\033[22m\033[33m(Sequence)\033[0m");
}
void zzwUN(struct zzfile *zz, zzKey key, uint32_t size)
{
writetag(zz, key, UN, size);
if ((long)size == UNLIMITED) verboseprint(zz, "\033[22m\033[33m(Sequence)\033[0m")
else verboseprint(zz, "\033[22m\033[33m(Unknown)\033[0m");
}
void zzwUN_begin(struct zzfile *zz, zzKey key, long *pos)
{
zzwUN(zz, key, UNLIMITED);
if (pos) *pos = ziwritepos(zz->zi) - 4; // position of length value
zz->ladderidx++;
zz->ladder[zz->ladderidx].txsyn = ZZ_IMPLICIT;
zz->ladder[zz->ladderidx].size = UNLIMITED;
zz->ladder[zz->ladderidx].group = ZZ_GROUP(key);
zz->ladder[zz->ladderidx].type = ZZ_SEQUENCE;
}
/// Pass in NULL to use UNLIMITED size
void zzwUN_end(struct zzfile *zz, long *pos)
{
if (pos) // set exact size of data written
{
ziwriteu32at(zz->zi, ziwritepos(zz->zi) - *pos - 4, *pos);
}
else
{
implicit(zz->zi, 0xfffe, 0xe0dd, 0); // add end seq tag
}
zz->ladderidx--;
}
void zzwItem_begin(struct zzfile *zz, long *pos)
{
implicit(zz->zi, 0xfffe, 0xe000, UNLIMITED);
verboseprint(zz, " ");
if (pos) *pos = ziwritepos(zz->zi) - 4; // position of length value
}
void zzwItem_end(struct zzfile *zz, long *pos)
{
if (pos) // set exact size of data written
{
ziwriteu32at(zz->zi, ziwritepos(zz->zi) - *pos - 4, *pos);
}
else
{
implicit(zz->zi, 0xfffe, 0xe00d, 0); // add end item tag
verboseprint(zz, " ");
}
}
void zzwSQ_begin(struct zzfile *zz, zzKey key, long *pos)
{
zzwSQ(zz, key, UNLIMITED);
if (pos) *pos = ziwritepos(zz->zi) - 4; // position of length value
zz->ladderidx++;
zz->ladder[zz->ladderidx].txsyn = ZZ_EXPLICIT;
zz->ladder[zz->ladderidx].size = UNLIMITED;
zz->ladder[zz->ladderidx].group = ZZ_GROUP(key);
zz->ladder[zz->ladderidx].type = ZZ_SEQUENCE;
}
void zzwSQ_end(struct zzfile *zz, long *pos)
{
zzwUN_end(zz, pos);
}
void zzwPixelData_begin(struct zzfile *zz, long frames, int bits, long size)
{
int i;
uint32_t tmp = 0;
enum VR vr = OB;
if ((bits == 16 && zz->ladder[zz->ladderidx].txsyn == ZZ_EXPLICIT) || zz->ladder[zz->ladderidx].txsyn == ZZ_IMPLICIT)
{
vr = OW; // all other cases use OB
}
if (zz->ladder[zz->ladderidx].txsyn > ZZ_EXPLICIT)
{
writetag(zz, DCM_PixelData, vr, UNLIMITED); // encapsulated pixel data
implicit(zz->zi, 0xfffe, 0xe000, sizeof(tmp) * frames);
zz->pxOffsetTable = ziwritepos(zz->zi); // position of index table
for (i = 0; i < frames; i++)
{
ziwrite(zz->zi, &tmp, sizeof(tmp)); // write empty index table
}
}
else
{
writetag(zz, DCM_PixelData, vr, size);
zz->pxOffsetTable = -1; // no index table
}
zz->frames = frames;
verboseprint(zz, " ");
}
void zzwPixelData_frame(struct zzfile *zz, int frame, const void *data, uint32_t size)
{
long wlen = size;
if (size % 2 != 0) wlen++; // padding
if (zz->ladder[zz->ladderidx].txsyn > ZZ_EXPLICIT) // write encapsulation
{
ziwriteu32at(zz->zi, ziwritepos(zz->zi) - (zz->pxOffsetTable + zz->frames * sizeof(uint32_t)), zz->pxOffsetTable + frame * sizeof(uint32_t));
implicit(zz->zi, 0xfffe, 0xe000, wlen);
}
ziwrite(zz->zi, data, size);
if (size % 2 != 0) ziwrite(zz->zi, "", 1); // pad
}
void zzwPixelData_end(struct zzfile *zz)
{
if (zz->ladder[zz->ladderidx].txsyn > ZZ_EXPLICIT) // terminate encapsulation
{
implicit(zz->zi, 0xfffe, 0xe0dd, 0);
}
}
void zzwAT(struct zzfile *zz, zzKey key, zzKey key2)
{
const uint16_t group2 = ZZ_GROUP(key2);
const uint16_t element2 = ZZ_ELEMENT(key2);
writetag(zz, key, AT, sizeof(group2) + sizeof(element2));
ziwrite(zz->zi, &group2, sizeof(group2));
ziwrite(zz->zi, &element2, sizeof(element2));
verboseprint(zz, "[%04x,%04x]", group2, element2);
}
void zzwUL(struct zzfile *zz, zzKey key, uint32_t value)
{
writetag(zz, key, UL, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%u]", value);
}
void zzwULv(struct zzfile *zz, zzKey key, int len, const uint32_t *value)
{
const int size = sizeof(*value) * len;
writetag(zz, key, UL, size);
ziwrite(zz->zi, value, size);
verboseprint(zz, "[%u...]", value[0]);
}
void zzwSL(struct zzfile *zz, zzKey key, int32_t value)
{
writetag(zz, key, SL, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%d]", value);
}
void zzwSS(struct zzfile *zz, zzKey key, int16_t value)
{
writetag(zz, key, SS, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%d]", value);
}
void zzwUS(struct zzfile *zz, zzKey key, uint16_t value)
{
writetag(zz, key, US, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%u]", value);
}
void zzwFL(struct zzfile *zz, zzKey key, float value)
{
writetag(zz, key, FL, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%f]", value);
}
void zzwFD(struct zzfile *zz, zzKey key, double value)
{
writetag(zz, key, FD, sizeof(value));
ziwrite(zz->zi, &value, sizeof(value));
verboseprint(zz, "[%f]", value);
}
void zzwOB(struct zzfile *zz, zzKey key, int len, const char *string)
{
int wlen = len;
if (len % 2 != 0) wlen++; // padding
writetag(zz, key, OB, wlen);
ziwrite(zz->zi, string, len);
if (len % 2 != 0) ziwrite(zz->zi, "", 1); // pad
verboseprint(zz, "...");
}
void zzwOW(struct zzfile *zz, zzKey key, int len, const uint16_t *string)
{
const int size = len * 2;
writetag(zz, key, OW, size);
ziwrite(zz->zi, string, size);
verboseprint(zz, "...");
}
void zzwOF(struct zzfile *zz, zzKey key, int len, const float *string)
{
const int size = len * 4;
writetag(zz, key, OF, size);
ziwrite(zz->zi, string, size);
verboseprint(zz, "[%f...]", string[0]);
}
void zzwUI(struct zzfile *zz, zzKey key, const char *string)
{
int length = MIN(strlen(string), (size_t)64);
int wlen = length;
if (length % 2 != 0) wlen++; // padding
writetag(zz, key, UI, wlen);
ziwrite(zz->zi, string, length);
if (length % 2 != 0) ziwrite(zz->zi, "", 1); // pad with null
verboseprint(zz, "[%s]", string);
}
static inline int countdelims(const char *str, const char delim)
{
unsigned i, c = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] == delim) c++;
}
return c;
}
static void wstr(struct zzfile *zz, zzKey key, const char *string, enum VR vr, size_t maxlen)
{
const size_t multiplen = maxlen * (countdelims(string, '\\') + 1); // max length times value multiplicity
const size_t length = MIN(strlen(string), multiplen);
size_t wlen = length;
if (length % 2 != 0) wlen++; // padding
writetag(zz, key, vr, wlen);
ziwrite(zz->zi, string, length);
if (length % 2 != 0)
{
// pad with a space to make even length
ziwrite(zz->zi, " ", 1);
}
verboseprint(zz, "[%s]", string);
}
void zzwPN(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, PN, 64); }
void zzwSH(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, SH, 16); }
void zzwAE(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, AE, 16); }
void zzwAS(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, AS, 4); }
void zzwCS(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, CS, 16); }
void zzwLO(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, LO, 64); }
void zzwLT(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, LT, 10240); }
void zzwST(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, ST, 1024); }
void zzwUT(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, UT, UINT32_MAX - 1); }
void zzwDSs(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, DS, 16); }
void zzwDAs(struct zzfile *zz, zzKey key, const char *string) { wstr(zz, key, string, DA, 8); }
void zzwIS(struct zzfile *zz, zzKey key, int value)
{
char tmp[MAX_LEN_IS];
memset(tmp, 0, sizeof(tmp));
snprintf(tmp, sizeof(tmp) - 1, "%d", value);
wstr(zz, key, tmp, IS, 12);
}
void zzwDSd(struct zzfile *zz, zzKey key, double value)
{
char tmp[MAX_LEN_DS];
memset(tmp, 0, sizeof(tmp));
snprintf(tmp, sizeof(tmp) - 1, "%g", value);
wstr(zz, key, tmp, DS, 16);
}
void zzwDSdv(struct zzfile *zz, zzKey key, int len, const double *value)
{
size_t wlen = 0, lenval;
long pos = ziwritepos(zz->zi);
int i;
char tmp[MAX_LEN_DS + 1];
// Set to position of size field, depending on transfer syntax
pos += explicit(zz) ? 6 : 4;
writetag(zz, key, DS, 0);
// Write out values
for (i = 0; i < len; i++)
{
memset(tmp, 0, sizeof(tmp));
snprintf(tmp, sizeof(tmp) - 2, "%g", value[i]);
lenval = strlen(tmp);
if (i + 1 < len)
{
strcat(tmp, "\\"); // value delimiter
lenval++;
}
ziwrite(zz->zi, tmp, lenval);
wlen += lenval;
}
if (wlen % 2 != 0)
{
wlen++; // padding
ziwrite(zz->zi, " ", 1); // pad with a space to make even length
}
// Now set the size of the tag
if (explicit(zz)) ziwriteu16at(zz->zi, wlen, pos);
else ziwriteu32at(zz->zi, wlen, pos);
verboseprint(zz, "[%f...]", value[0]);
}
void zzwDA(struct zzfile *zz, zzKey key, time_t datestamp)
{
char tmp[MAX_LEN_DA];
struct tm stamp;
localtime_r(&datestamp, &stamp);
strftime(tmp, MAX_LEN_DA, "%Y%m%d", &stamp);
wstr(zz, key, tmp, DA, 8);
}
void zzwTM(struct zzfile *zz, zzKey key, struct timeval datetimestamp)
{
char tmp[MAX_LEN_TM], frac[9];
struct tm stamp;
time_t datestamp = datetimestamp.tv_sec;
localtime_r(&datestamp, &stamp);
strftime(tmp, MAX_LEN_DT, "%H%M%S", &stamp);
if (datetimestamp.tv_usec > 0)
{
memset(frac, 0, sizeof(frac));
snprintf(frac, 8, ".%06u", (unsigned)datetimestamp.tv_usec);
strcat(tmp, frac);
}
wstr(zz, key, tmp, TM, 16);
}
void zzwDT(struct zzfile *zz, zzKey key, struct timeval datetimestamp)
{
char tmp[MAX_LEN_DT], frac[9];
struct tm stamp;
time_t datestamp = datetimestamp.tv_sec;
localtime_r(&datestamp, &stamp);
strftime(tmp, MAX_LEN_DT, "%Y%m%d%H%M%S", &stamp);
if (datetimestamp.tv_usec > 0)
{
memset(frac, 0, sizeof(frac));
snprintf(frac, 8, ".%06u", (unsigned)datetimestamp.tv_usec);
strcat(tmp, frac);
}
wstr(zz, key, tmp, DT, 26);
}
struct zzfile *zzcreate(const char *filename, struct zzfile *zz, const char *sopclass, const char *sopinstanceuid, const char *transfer)
{
memset(zz, 0, sizeof(*zz));
zz->acrNema = false;
zz->ladder[0].txsyn = ZZ_EXPLICIT;
zz->zi = ziopenfile(filename, "w");
if (zz->zi) zzwHeader(zz, sopclass, sopinstanceuid, transfer);
if (strcmp(transfer, UID_LittleEndianImplicitTransferSyntax) == 0)
{
zz->ladder[0].txsyn = ZZ_IMPLICIT;
}
return zz;
}
void zzwHeader(struct zzfile *zz, const char *sopclass, const char *sopinstanceuid, const char *transfer)
{
char version[3];
char zeroes[128]; // FIXME, i hate this inefficiency
const uint32_t startpos = 128 + 4 + 8;
version[0] = 0;
version[1] = 1;
version[2] = 0;
memset(zeroes, 0, sizeof(zeroes));
ziwrite(zz->zi, zeroes, 128);
ziwrite(zz->zi, "DICM", 4);
zzwUL(zz, DCM_FileMetaInformationGroupLength, 0); // length zero, fixing it below
zzwOB(zz, DCM_FileMetaInformationVersion, 2, version);
zzwUI(zz, DCM_MediaStorageSOPClassUID, sopclass);
zzwUI(zz, DCM_MediaStorageSOPInstanceUID, sopinstanceuid);
zzwUI(zz, DCM_TransferSyntaxUID, transfer);
zzwUI(zz, DCM_ImplementationClassUID, "1.2.3.4.8.2");
zzwSH(zz, DCM_ImplementationVersionName, "zzdicom");
zzwAE(zz, DCM_SourceApplicationEntityTitle, "ZZ_NONE");
// write group size
ziwriteu32at(zz->zi, ziwritepos(zz->zi) - (startpos + 4), startpos);
}
void zzwEmpty(struct zzfile *zz, zzKey key, enum VR vr)
{
writetag(zz, key, vr, 0);
verboseprint(zz, "[]");
}
void zzwMax(struct zzfile *zz, zzKey key, enum VR vr)
{
ssize_t size = 0;
switch (zz->current.vr)
{
case OW:
case OF:
case UT:
case OX:
case NO:
case HACK_VR:
case SQ:
case OB:
case UN:
assert(false);
size = 2;
break;
case AE: size = 128; break;
case AS: size = 32; break;
case CS: size = 128; break;
case DA: size = 80; break;
case DS: size = 128; break;
case DT: size = 208; break;
case IS: size = 96; break;
case LT: size = 40960; break;
case PN: size = 256; break;
case SH: size = 64; break;
case ST: size = 4096; break;
case TM: size = 128; break;
case UI: size = 64; break;
case LO: size = 256; break;
case AT: size = 32; break;
case UL: size = 32; break;
case US: size = 16; break;
case SS: size = 16; break;
case SL: size = 32; break;
case FL: size = 32; break;
case FD: size = 32; break;
}
writetag(zz, key, vr, size);
char *str = malloc(size);
memset(str, 0, size);
ziwrite(zz->zi, str, size);
verboseprint(zz, "%s", str);
}