forked from ImagingDataCommons/libdicom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdicom-parse.c
More file actions
1116 lines (938 loc) · 31.9 KB
/
dicom-parse.c
File metadata and controls
1116 lines (938 loc) · 31.9 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Implementation of Part 10 of the DICOM standard: Media Storage and File
* Format for Media Interchange.
*/
#include "config.h"
#ifdef _WIN32
// the Windows CRT considers strdup and strcpy unsafe
#define _CRT_SECURE_NO_WARNINGS
// and deprecates strdup
#define strdup(v) _strdup(v)
#endif
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "utarray.h"
#include <dicom/dicom.h>
#include "pdicom.h"
/* The size of the buffer we use for reading smaller element values. This is
* large enough for most VRs.
*/
#define INPUT_BUFFER_SIZE (256)
typedef struct _DcmParseState {
DcmError **error;
DcmIO *io;
bool implicit;
bool big_endian;
const DcmParse *parse;
void *client;
DcmDataSet *meta;
int64_t offset;
int64_t pixel_data_offset;
} DcmParseState;
static int64_t dcm_read(DcmParseState *state,
char *buffer, int64_t length, int64_t *position)
{
int64_t bytes_read = dcm_io_read(state->error, state->io, buffer, length);
if (bytes_read < 0) {
return bytes_read;
}
*position += bytes_read;
return bytes_read;
}
static bool dcm_require(DcmParseState *state,
char *buffer, int64_t length, int64_t *position)
{
while (length > 0) {
int64_t bytes_read = dcm_read(state, buffer, length, position);
if (bytes_read < 0) {
return false;
} else if (bytes_read == 0) {
dcm_error_set(state->error, DCM_ERROR_CODE_IO,
"end of filehandle",
"needed %zd bytes beyond end of filehandle", length);
return false;
}
buffer += bytes_read;
length -= bytes_read;
}
return true;
}
static bool dcm_seekcur(DcmParseState *state, int64_t offset, int64_t *position)
{
int64_t new_offset = dcm_io_seek(state->error, state->io, offset, SEEK_CUR);
if (new_offset < 0) {
return false;
}
*position += offset;
return true;
}
static bool dcm_is_eof(DcmParseState *state)
{
bool eof = true;
char buffer[1];
int64_t bytes_read = dcm_io_read(NULL, state->io, buffer, 1);
if (bytes_read > 0) {
eof = false;
int64_t position = 0;
(void) dcm_seekcur(state, -1, &position);
}
return eof;
}
/* TRUE for big-endian machines, like PPC. We need to byteswap DICOM
* numeric types in this case. Run time tests for this are much
* simpler to manage when cross-compiling.
*/
static bool is_big_endian(void)
{
union {
uint32_t i;
char c[4];
} bint = {0x01020304};
return bint.c[0] == 1;
}
#define SWAP16(V) \
((uint16_t) ( \
(uint16_t) ((uint16_t) (V) >> 8) | \
(uint16_t) ((uint16_t) (V) << 8) \
))
#define SWAP32(V) \
((uint32_t) ( \
(((uint32_t) (V) & UINT32_C(0x000000ff)) << 24) | \
(((uint32_t) (V) & UINT32_C(0x0000ff00)) << 8) | \
(((uint32_t) (V) & UINT32_C(0x00ff0000)) >> 8) | \
(((uint32_t) (V) & UINT32_C(0xff000000)) >> 24) \
))
#define SWAP64(V) \
((uint64_t) ( \
(((uint64_t) (V) & UINT64_C(0x00000000000000ff)) << 56) | \
(((uint64_t) (V) & UINT64_C(0x000000000000ff00)) << 40) | \
(((uint64_t) (V) & UINT64_C(0x0000000000ff0000)) << 24) | \
(((uint64_t) (V) & UINT64_C(0x00000000ff000000)) << 8) | \
(((uint64_t) (V) & UINT64_C(0x000000ff00000000)) >> 8) | \
(((uint64_t) (V) & UINT64_C(0x0000ff0000000000)) >> 24) | \
(((uint64_t) (V) & UINT64_C(0x00ff000000000000)) >> 40) | \
(((uint64_t) (V) & UINT64_C(0xff00000000000000)) >> 56) \
))
static void byteswap(char *data, size_t length, size_t size)
{
// only swap if the data is "swappable"
if (size > 0 &&
length >= size &&
length % size == 0) {
size_t n_elements = length / size;
switch (size) {
case 2:
for (size_t i = 0; i < n_elements; i++) {
uint16_t *v = &((uint16_t *) data)[i];
*v = SWAP16(*v);
}
break;
case 4:
for (size_t i = 0; i < n_elements; i++) {
uint32_t *v = &((uint32_t *) data)[i];
*v = SWAP32(*v);
}
break;
case 8:
for (size_t i = 0; i < n_elements; i++) {
uint64_t *v = &((uint64_t *) data)[i];
*v = SWAP64(*v);
}
break;
default:
break;
}
}
}
static bool read_uint16(DcmParseState *state,
uint16_t *value, int64_t *position)
{
union {
uint16_t i;
char c[2];
} buffer;
if (!dcm_require(state, buffer.c, 2, position)) {
return false;
}
if (state->big_endian) {
byteswap(buffer.c, 2, 2);
}
*value = buffer.i;
return true;
}
static bool read_uint32(DcmParseState *state,
uint32_t *value, int64_t *position)
{
union {
uint32_t i;
char c[4];
} buffer;
if (!dcm_require(state, buffer.c, 4, position)) {
return false;
}
if (state->big_endian) {
byteswap(buffer.c, 4, 4);
}
*value = buffer.i;
return true;
}
static bool read_tag(DcmParseState *state, uint32_t *tag, int64_t *position)
{
uint16_t group, elem;
if (!read_uint16(state, &group, position) ||
!read_uint16(state, &elem, position)) {
return false;
}
*tag = ((uint32_t)group << 16) | elem;
return true;
}
/* This is used recursively.
*/
static bool parse_element(DcmParseState *state,
int64_t *position);
static bool parse_element_header(DcmParseState *state,
uint32_t *tag,
DcmVR *vr,
uint32_t *length,
int64_t *position)
{
if (!read_tag(state, tag, position)) {
return false;
}
if (state->implicit) {
// this can be an ambiguous VR, eg. pixeldata is allowed in implicit
// mode and has to be disambiguated later from other tags
*vr = dcm_vr_from_tag(*tag);
if (*vr == DCM_VR_ERROR) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element header failed",
"tag %08x not allowed in implicit mode", *tag);
return false;
}
if (!read_uint32(state, length, position)) {
return false;
}
} else {
// Value Representation
char vr_str[3];
if (!dcm_require(state, vr_str, 2, position)) {
return false;
}
vr_str[2] = '\0';
*vr = dcm_dict_vr_from_str(vr_str);
if (!dcm_is_valid_vr_for_tag(*vr, *tag)) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element header failed",
"tag %08x cannot have VR '%s'", *tag, vr_str);
return false;
}
if (dcm_dict_vr_header_length(*vr) == 2) {
// These VRs have a short length of only two bytes
uint16_t short_length;
if (!read_uint16(state, &short_length, position)) {
return false;
}
*length = (uint32_t) short_length;
} else {
// Other VRs have two reserved bytes before length of four bytes
uint16_t reserved;
if (!read_uint16(state, &reserved, position) ||
!read_uint32(state, length, position)) {
return false;
}
if (reserved != 0x0000) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element header failed",
"unexpected value for reserved bytes "
"of data element %08x with VR '%s'",
tag, vr);
return false;
}
}
}
return true;
}
static bool parse_element_sequence(DcmParseState *state,
uint32_t seq_tag,
DcmVR seq_vr,
uint32_t seq_length,
int64_t *position)
{
if (state->parse->sequence_begin &&
!state->parse->sequence_begin(state->error,
state->client,
seq_tag,
seq_vr,
seq_length)) {
return false;
}
int index = 0;
while (*position < seq_length) {
dcm_log_debug("read Item #%d", index);
uint32_t item_tag;
uint32_t item_length;
if (!read_tag(state, &item_tag, position) ||
!read_uint32(state, &item_length, position)) {
return false;
}
if (item_tag == TAG_SQ_DELIM) {
dcm_log_debug("stop reading data element -- "
"encountered SequenceDelimination tag");
break;
}
if (item_tag != TAG_ITEM) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element failed",
"expected tag '%08x' instead of '%08x' "
"for item #%d",
TAG_ITEM,
item_tag,
index);
return false;
}
if (item_length == 0xFFFFFFFF) {
dcm_log_debug("item #%d has undefined length", index);
} else {
dcm_log_debug("item #%d has defined length %d",
index, item_length);
}
if (state->parse->dataset_begin &&
!state->parse->dataset_begin(state->error, state->client)) {
return false;
}
int64_t item_position = 0;
while (item_position < item_length) {
// peek the next tag
if (!read_tag(state, &item_tag, &item_position)) {
return false;
}
if (item_tag == TAG_ITEM_DELIM) {
dcm_log_debug("stop reading Item #%d -- "
"encountered Item Delimination Tag",
index);
// step over the tag length
if (!dcm_seekcur(state, 4, &item_position)) {
return false;
}
break;
}
// back to start of element
if (!dcm_seekcur(state, -4, &item_position)) {
return false;
}
if (!parse_element(state, &item_position)) {
return false;
}
}
*position += item_position;
if (state->parse->dataset_end &&
!state->parse->dataset_end(state->error, state->client)) {
return false;
}
index += 1;
}
if (state->parse->sequence_end &&
!state->parse->sequence_end(state->error,
state->client,
seq_tag,
seq_vr,
seq_length)) {
return false;
}
return true;
}
static bool parse_pixeldata_item(DcmParseState *state,
uint32_t tag,
DcmVR vr,
uint32_t length,
uint32_t item_length,
int64_t *position)
{
// a read buffer on the stack for small objects
char input_buffer[INPUT_BUFFER_SIZE];
char *value;
char *value_free = NULL;
USED(tag);
// read to our stack buffer, if possible
if (item_length > INPUT_BUFFER_SIZE) {
value = value_free = DCM_MALLOC(state->error, item_length);
if (value_free == NULL) {
return false;
}
} else {
value = input_buffer;
}
if (!dcm_require(state, value, item_length, position)) {
if (value_free != NULL) {
free(value_free);
}
return false;
}
// native (not encapsulated) pixeldata is always little-endian and needs
// byteswapping on big-endian machines
if (length != 0xffffffff &&
state->big_endian) {
byteswap(value, item_length, dcm_dict_vr_size(vr));
}
if (state->parse->pixeldata_create &&
!state->parse->pixeldata_create(state->error,
state->client,
tag,
vr,
value,
item_length)) {
if (value_free != NULL) {
free(value_free);
}
return false;
}
if (value_free != NULL) {
free(value_free);
}
return true;
}
static bool parse_pixeldata(DcmParseState *state,
uint32_t tag,
DcmVR vr,
uint32_t length,
int64_t *position)
{
if (state->parse->pixeldata_begin &&
!state->parse->pixeldata_begin(state->error,
state->client,
tag,
vr,
length)) {
return false;
}
if (length == 0xffffffff) {
// a sequence of encapsulated pixeldata items
for (int index = 0; true; index++) {
uint32_t item_tag;
uint32_t item_length;
dcm_log_debug("read Item #%d", index);
if (!read_tag(state, &item_tag, position) ||
!read_uint32(state, &item_length, position)) {
return false;
}
if (item_tag == TAG_SQ_DELIM) {
dcm_log_debug("stop reading data element -- "
"encountered SequenceDelimination Tag");
break;
}
if (item_tag != TAG_ITEM) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element failed",
"expected tag '%08x' instead of '%08x' "
"for Item #%d",
TAG_ITEM,
item_tag,
index);
return false;
}
if (!parse_pixeldata_item(state,
tag,
vr,
length,
item_length,
position)) {
return false;
}
}
} else {
// a single native pixeldata item
if (!parse_pixeldata_item(state, tag, vr, length, length, position)) {
return false;
}
}
if (state->parse->pixeldata_end &&
!state->parse->pixeldata_end(state->error,
state->client)) {
return false;
}
return true;
}
static bool parse_element_body(DcmParseState *state,
uint32_t tag,
DcmVR vr,
uint32_t length,
int64_t *position)
{
DcmVRClass vr_class = dcm_dict_vr_class(vr);
size_t size = dcm_dict_vr_size(vr);
char *value;
char *value_free = NULL;
char input_buffer[INPUT_BUFFER_SIZE];
/* We treat pixeldata as a special case so we can handle encapsulated
* image sequences.
*/
if (tag == TAG_PIXEL_DATA ||
tag == TAG_FLOAT_PIXEL_DATA ||
tag == TAG_DOUBLE_PIXEL_DATA) {
return parse_pixeldata(state, tag, vr, length, position);
}
dcm_log_debug("Read Data Element body '%08x'", tag);
switch (vr_class) {
case DCM_VR_CLASS_STRING_SINGLE:
case DCM_VR_CLASS_STRING_MULTI:
case DCM_VR_CLASS_NUMERIC_DECIMAL:
case DCM_VR_CLASS_NUMERIC_INTEGER:
case DCM_VR_CLASS_BINARY:
if (vr_class == DCM_VR_CLASS_NUMERIC_DECIMAL ||
vr_class == DCM_VR_CLASS_NUMERIC_INTEGER) {
// all numeric classes have a size
if (size > 0 &&
length % size != 0) {
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element failed",
"bad length for tag '%08x'",
tag);
return false;
}
}
// read to a static char buffer, if possible
if ((int64_t) length + 1 >= INPUT_BUFFER_SIZE) {
value = value_free = DCM_MALLOC(state->error,
(size_t) length + 1);
if (value == NULL) {
return false;
}
} else {
value = input_buffer;
}
if (!dcm_require(state, value, length, position)) {
if (value_free != NULL) {
free(value_free);
}
return false;
}
value[length] = '\0';
if (length > 0 &&
(vr_class == DCM_VR_CLASS_STRING_SINGLE ||
vr_class == DCM_VR_CLASS_STRING_MULTI) &&
vr != DCM_VR_UI &&
isspace(value[length - 1])) {
value[length - 1] = '\0';
}
if (size > 0 && state->big_endian) {
byteswap(value, length, size);
}
if (state->parse->element_create &&
!state->parse->element_create(state->error,
state->client,
tag,
vr,
value,
length)) {
if (value_free != NULL) {
free(value_free);
}
return false;
}
if (value_free != NULL) {
free(value_free);
}
break;
case DCM_VR_CLASS_SEQUENCE:
if (length == 0xFFFFFFFF) {
dcm_log_debug("Sequence of Data Element '%08x' "
"has undefined length",
tag);
} else {
dcm_log_debug("Sequence of Data Element '%08x' "
"has defined length %d",
tag, length);
}
int64_t seq_position = 0;
if (!parse_element_sequence(state,
tag,
vr,
length,
&seq_position)) {
return false;
}
*position += seq_position;
break;
default:
dcm_error_set(state->error, DCM_ERROR_CODE_PARSE,
"reading of data element failed",
"data element '%08x' has unexpected VR", tag);
return false;
}
return true;
}
static bool parse_element(DcmParseState *state,
int64_t *position)
{
uint32_t tag;
DcmVR vr;
uint32_t length;
if (!parse_element_header(state, &tag, &vr, &length, position) ||
!parse_element_body(state, tag, vr, length, position)) {
return false;
}
return true;
}
/* Top-level datasets don't have an enclosing length, and can broken by a
* stop function.
*/
static bool parse_toplevel_dataset(DcmParseState *state,
int64_t *position)
{
if (state->parse->dataset_begin &&
!state->parse->dataset_begin(state->error, state->client)) {
return false;
}
for (;;) {
if (dcm_is_eof(state)) {
dcm_log_info("stop reading Data Set -- reached end of filehandle");
break;
}
uint32_t tag;
DcmVR vr;
uint32_t length;
int64_t element_start = 0;
if (!parse_element_header(state, &tag, &vr, &length, &element_start)) {
return false;
}
if (tag == TAG_TRAILING_PADDING) {
dcm_log_info("Stop reading Data Set",
"Encountered Data Set Trailing Tag");
break;
}
if (state->parse->stop &&
state->parse->stop(state->client, tag, vr, length)) {
// seek back to the start of this element
if (!dcm_seekcur(state, -element_start, &element_start)) {
return false;
}
break;
}
*position += element_start;
if (!parse_element_body(state, tag, vr, length, position)) {
return false;
}
}
if (state->parse->dataset_end &&
!state->parse->dataset_end(state->error, state->client)) {
return false;
}
return true;
}
/* Parse a dataset from a filehandle.
*/
bool dcm_parse_dataset(DcmError **error,
DcmIO *io,
bool implicit,
const DcmParse *parse,
void *client)
{
DcmParseState state = {
.error = error,
.io = io,
.implicit = implicit,
.big_endian = is_big_endian(),
.parse = parse,
.client = client
};
int64_t position = 0;
if (!parse_toplevel_dataset(&state, &position)) {
return false;
}
return true;
}
/* Parse a group. A length element, followed by a list of elements.
*/
bool dcm_parse_group(DcmError **error,
DcmIO *io,
bool implicit,
const DcmParse *parse,
void *client)
{
DcmParseState state = {
.error = error,
.io = io,
.implicit = implicit,
.big_endian = is_big_endian(),
.parse = parse,
.client = client
};
int64_t position = 0;
/* Groups start with (xxxx0000, UL, 4), meaning a 32-bit length value.
*/
uint32_t tag;
DcmVR vr;
uint32_t length;
if (!parse_element_header(&state, &tag, &vr, &length, &position)) {
return false;
}
uint16_t element_number = tag & 0xffff;
uint16_t group_number = tag >> 16;
if (element_number != 0x0000 || vr != DCM_VR_UL || length != 4) {
dcm_error_set(state.error, DCM_ERROR_CODE_PARSE,
"reading of group failed",
"bad group length element");
return false;
}
uint32_t group_length;
if (!read_uint32(&state, &group_length, &position)) {
return false;
}
// parse the elements in the group to a dataset
if (state.parse->dataset_begin &&
!state.parse->dataset_begin(state.error, state.client)) {
return false;
}
while (position < group_length) {
int64_t element_start = 0;
if (!parse_element_header(&state, &tag, &vr, &length, &element_start)) {
return false;
}
// stop if we read the first tag of the group beyond,
// or if the stop function triggers
if ((tag >> 16) != group_number ||
(state.parse->stop &&
state.parse->stop(state.client, tag, vr, length))) {
// seek back to the start of this element
if (!dcm_seekcur(&state, -element_start, &element_start)) {
return false;
}
break;
}
position += element_start;
if (!parse_element_body(&state, tag, vr, length, &position)) {
return false;
}
}
if (state.parse->dataset_end &&
!state.parse->dataset_end(state.error, state.client)) {
return false;
}
return true;
}
/* Walk pixeldata and set up offsets. We use the BOT, if present, otherwise we
* have to scan the whole thing.
*
* Each offset is the seek from the start of pixeldata to the ITEM for that
* frame.
*/
bool dcm_parse_pixeldata_offsets(DcmError **error,
DcmIO *io,
bool implicit,
int64_t *first_frame_offset,
int64_t *offsets,
int num_frames)
{
DcmParseState state = {
.error = error,
.io = io,
.implicit = implicit,
.big_endian = is_big_endian()
};
int64_t position = 0;
dcm_log_debug("parsing PixelData");
uint32_t tag;
DcmVR vr;
uint32_t length;
uint32_t value;
if (!parse_element_header(&state, &tag, &vr, &length, &position)) {
return false;
}
if (tag != TAG_PIXEL_DATA &&
tag != TAG_FLOAT_PIXEL_DATA &&
tag != TAG_DOUBLE_PIXEL_DATA) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"parsing PixelData failed",
"file pointer not positioned at PixelData element");
return false;
}
// The header of the 0th item (the BOT)
if (!read_tag(&state, &tag, &position) ||
!read_uint32(&state, &length, &position)) {
return false;
}
if (tag != TAG_ITEM) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"reading BasicOffsetTable failed",
"unexpected tag found for BasicOffsetTable item");
return false;
}
if (length > 0) {
// There is a non-zero length BOT, use that
dcm_log_info("reading Basic Offset Table");
// Read offset values from BOT Item value
// FIXME .. could do this with a single require to a uint32_t array,
// see numeric array read above
for (int i = 0; i < num_frames; i++) {
if (!read_uint32(&state, &value, &position)) {
return false;
}
if (value == TAG_ITEM) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"reading BasicOffsetTable failed",
"encountered unexpected item tag "
"in BasicOffsetTable");
return false;
}
offsets[i] = value;
}
// and that's the offset to the item header on the first frame
*first_frame_offset = position;
// the next thing should be the tag for frame 1
if (!read_tag(&state, &tag, &position)) {
return false;
}
if (tag != TAG_ITEM) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"reading BasicOffsetTable failed",
"BasicOffsetTable too large");
return false;
}
} else {
// the BOT is missing, we must scan pixeldata to find the position of
// each frame
// we could use our generic parser above ^^ but we have a special loop
// here as an optimisation (we can skip over the pixel data itself)
dcm_log_info("building Offset Table from Pixel Data");
// 0 in the BOT is the offset to the start of frame 1, ie. here
*first_frame_offset = position;
for (int i = 0; i < num_frames; i++) {
if (!read_tag(&state, &tag, &position) ||
!read_uint32(&state, &length, &position)) {
return false;
}
if (tag == TAG_SQ_DELIM) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"reading BasicOffsetTable failed",
"too few frames in PixelData");
return false;
}
if (tag != TAG_ITEM) {
dcm_error_set(error, DCM_ERROR_CODE_PARSE,
"building BasicOffsetTable failed",
"frame Item #%d has wrong tag '%08x'",
i + 1,
tag);
return false;
}
// step back to the start of the item for this frame
offsets[i] = position - *first_frame_offset - 8;
// and seek forward over the value
if (!dcm_seekcur(&state, length, &position)) {
return false;
}
}
// in case multiple frames 1:1 frame to fragment mapping is assumed,
// therefore the next thing should be the end of sequence tag
if (!read_tag(&state, &tag, &position)) {
return false;
}
if (num_frames != 1 && tag != TAG_SQ_DELIM) {