-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvtkDICOMMetaData.cxx
1187 lines (1088 loc) · 31.1 KB
/
vtkDICOMMetaData.cxx
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
/*=========================================================================
Program: DICOM for VTK
Copyright (c) 2012-2024 David Gobbi
All rights reserved.
See Copyright.txt or http://dgobbi.github.io/bsd3.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkDICOMMetaData.h"
#include "vtkDICOMDictionary.h"
#include "vtkDICOMItem.h"
#include "vtkDICOMTagPath.h"
#include "vtkObjectFactory.h"
#include "vtkMatrix4x4.h"
#include "vtkAbstractArray.h"
#include "vtkIntArray.h"
#include <assert.h>
#include <vector>
#include <utility>
vtkStandardNewMacro(vtkDICOMMetaData);
// The hash table size, must be a power of two
#define METADATA_HASH_SIZE 512
//----------------------------------------------------------------------------
// Constructor
vtkDICOMMetaData::vtkDICOMMetaData()
{
this->NumberOfInstances = 1;
this->NumberOfDataElements = 0;
this->Table = nullptr;
this->Head.Prev = nullptr;
this->Head.Next = &this->Tail;
this->Tail.Prev = &this->Head;
this->Tail.Next = nullptr;
this->FileIndexArray = nullptr;
this->FrameIndexArray = nullptr;
}
// Destructor
vtkDICOMMetaData::~vtkDICOMMetaData()
{
this->Clear();
if (this->FileIndexArray)
{
this->FileIndexArray->Delete();
}
if (this->FrameIndexArray)
{
this->FrameIndexArray->Delete();
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::Clear()
{
vtkDICOMDataElement **htable = this->Table;
if (htable)
{
for (unsigned int i = 0; i < METADATA_HASH_SIZE; i++)
{
delete [] htable[i];
}
delete [] htable;
}
this->NumberOfDataElements = 0;
this->Table = nullptr;
this->Head.Next = &this->Tail;
this->Tail.Prev = &this->Head;
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::Initialize()
{
this->Clear();
this->NumberOfInstances = 1;
if (this->FileIndexArray)
{
this->FileIndexArray->Delete();
this->FileIndexArray = nullptr;
}
if (this->FrameIndexArray)
{
this->FrameIndexArray->Delete();
this->FrameIndexArray = nullptr;
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::SetNumberOfInstances(int n)
{
if (this->Table != nullptr)
{
vtkErrorMacro("SetNumberOfInstances: Cannot set NumberOfInstances after "
"attributes have been added");
}
else
{
this->NumberOfInstances = n;
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::SetFileIndexArray(vtkIntArray *a)
{
if (this->FileIndexArray != a)
{
if (this->FileIndexArray)
{
this->FileIndexArray->Delete();
}
this->FileIndexArray = a;
if (this->FileIndexArray)
{
this->FileIndexArray->Register(this);
}
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::SetFrameIndexArray(vtkIntArray *a)
{
if (this->FrameIndexArray != a)
{
if (this->FrameIndexArray)
{
this->FrameIndexArray->Delete();
}
this->FrameIndexArray = a;
if (this->FrameIndexArray)
{
this->FrameIndexArray->Register(this);
}
this->Modified();
}
}
//----------------------------------------------------------------------------
// Erase an element from the hash table
void vtkDICOMMetaData::Erase(vtkDICOMTag tag)
{
unsigned int m = METADATA_HASH_SIZE - 1;
unsigned int i = (tag.ComputeHash() & m);
vtkDICOMDataElement **htable = this->Table;
vtkDICOMDataElement *hptr;
if (htable && (hptr = htable[i]) != nullptr)
{
while (hptr->Next != nullptr)
{
if (hptr->Tag == tag)
{
// remove from the linked list
hptr->Next->Prev = hptr->Prev;
hptr->Prev->Next = hptr->Next;
// remove from the hash table
hptr[0] = hptr[1];
while (hptr->Next != nullptr)
{
// adjust links as necessary
hptr->Prev->Next = hptr;
hptr->Next->Prev = hptr;
hptr++;
hptr[0] = hptr[1];
}
this->NumberOfDataElements--;
break;
}
hptr++;
}
}
}
//----------------------------------------------------------------------------
// Get an element from the hash table.
vtkDICOMDataElement *vtkDICOMMetaData::FindDataElement(
vtkDICOMTag tag)
{
unsigned int m = METADATA_HASH_SIZE - 1;
unsigned int i = (tag.ComputeHash() & m);
vtkDICOMDataElement **htable = this->Table;
vtkDICOMDataElement *hptr;
if (htable && (hptr = htable[i]) != nullptr)
{
while (hptr->Next != nullptr)
{
if (hptr->Tag == tag)
{
return hptr;
}
hptr++;
}
}
return nullptr;
}
//----------------------------------------------------------------------------
bool vtkDICOMMetaData::Has(vtkDICOMTag tag)
{
return (this->FindDataElement(tag) != nullptr);
}
//----------------------------------------------------------------------------
const vtkDICOMValue *vtkDICOMMetaData::FindAttributeValue(
int idx, vtkDICOMTag tag)
{
vtkDICOMDataElement *a = this->FindDataElement(tag);
const vtkDICOMValue *vptr = nullptr;
if (a != nullptr)
{
vptr = &a->Value;
// is this a sequence of values?
const vtkDICOMValue *sptr = vptr->GetMultiplexData();
if (sptr)
{
size_t n = vptr->GetNumberOfValues();
vptr = nullptr;
if (idx >= 0 && static_cast<size_t>(idx) < n)
{
if (sptr[idx].IsValid())
{
vptr = &sptr[idx];
}
}
}
}
return vptr;
}
//----------------------------------------------------------------------------
const vtkDICOMValue *vtkDICOMMetaData::FindAttributeValue(
int idx, const vtkDICOMTagPath& tagpath)
{
const vtkDICOMValue *vptr = this->FindAttributeValue(idx, tagpath.GetHead());
if (vptr != nullptr && tagpath.HasTail())
{
size_t i = tagpath.GetIndex();
size_t n = vptr->GetNumberOfValues();
const vtkDICOMItem *items = vptr->GetSequenceData();
vptr = nullptr;
if (items != nullptr && i < n)
{
vptr = &items[i].Get(tagpath.GetTail());
if (!vptr->IsValid())
{
vptr = nullptr;
}
}
}
return vptr;
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(vtkDICOMTag tag)
{
const vtkDICOMValue *vptr = this->FindAttributeValue(0, tag);
return (vptr ? *vptr : this->Tail.Value);
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(int idx, vtkDICOMTag tag)
{
const vtkDICOMValue *vptr = this->FindAttributeValue(idx, tag);
return (vptr ? *vptr : this->Tail.Value);
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(const vtkDICOMTagPath &tagpath)
{
const vtkDICOMValue *vptr = this->FindAttributeValue(0, tagpath);
return (vptr ? *vptr : this->Tail.Value);
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(
int idx, const vtkDICOMTagPath &tagpath)
{
const vtkDICOMValue *vptr = this->FindAttributeValue(idx, tagpath);
return (vptr ? *vptr : this->Tail.Value);
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(
int idx, int frame, const vtkDICOMTagPath &tagpath)
{
// if either of these is present in an enhanced DICOM file, then they
// will be searched before the root is searched
const DC::EnumType fgs[2] = {
DC::PerFrameFunctionalGroupsSequence,
DC::SharedFunctionalGroupsSequence
};
// for temporarily saving location to private value (see below)
const vtkDICOMValue *privateValue = nullptr;
// search PerFrame and then Shared functional sequences, if present
// (if frame is "-1", then only search SharedFunctionalGroups)
int istart = (frame < 0 ? 1 : 0);
for (int i = istart; i < 2; i++)
{
// we only need the frame number for the PerFrame sequence
size_t f = (i == 0 ? frame : 0);
const vtkDICOMValue *seq = this->FindAttributeValue(idx, fgs[i]);
if (seq && f < seq->GetNumberOfValues())
{
// search for the item that matches the frame
const vtkDICOMItem *items = seq->GetSequenceData();
const vtkDICOMValue &v = items[f].Get(tagpath);
if (v.IsValid())
{
return v;
}
// search within all the sequences in the item
vtkDICOMDataElementIterator iter = items[f].Begin();
vtkDICOMDataElementIterator iterEnd = items[f].End();
while (iter != iterEnd)
{
const vtkDICOMValue &u = iter->GetValue();
if (u.GetNumberOfValues() == 1)
{
const vtkDICOMItem *item = u.GetSequenceData();
if (item)
{
const vtkDICOMValue &w = item->Get(tagpath);
if (w.IsValid())
{
if ((iter->GetTag().GetGroup() & 1) == 0)
{
return w;
}
else if (privateValue == nullptr)
{
// if we found the attribute in a private sequence,
// then save but and keep searching to see if it will
// eventually be found somewhere public
privateValue = &w;
}
}
}
}
++iter;
}
}
}
// search root last of all
const vtkDICOMValue *vptr = this->FindAttributeValue(idx, tagpath);
// try private value (see above) if attribute wasn't found
vptr = (vptr ? vptr : privateValue);
return (vptr ? *vptr : this->Tail.Value);
}
//----------------------------------------------------------------------------
const vtkDICOMValue &vtkDICOMMetaData::Get(int idx, int frame, vtkDICOMTag tag)
{
return this->Get(idx, frame, vtkDICOMTagPath(tag));
}
//----------------------------------------------------------------------------
int vtkDICOMMetaData::GetFileIndex(int sliceIdx)
{
if (this->FileIndexArray == nullptr || sliceIdx < 0 ||
sliceIdx >= this->FileIndexArray->GetNumberOfTuples())
{
return -1;
}
int n = this->FileIndexArray->GetNumberOfComponents();
return this->FileIndexArray->GetValue(sliceIdx*n);
}
//----------------------------------------------------------------------------
int vtkDICOMMetaData::GetFileIndex(int sliceIdx, int compIdx, int numComp)
{
if (this->FileIndexArray == nullptr || sliceIdx < 0 ||
sliceIdx >= this->FileIndexArray->GetNumberOfTuples() ||
compIdx < 0 || compIdx >= numComp)
{
return -1;
}
int n = this->FileIndexArray->GetNumberOfComponents();
return this->FileIndexArray->GetValue(sliceIdx*n + compIdx*n/numComp);
}
//----------------------------------------------------------------------------
int vtkDICOMMetaData::GetFrameIndex(int sliceIdx)
{
if (this->FrameIndexArray == nullptr || sliceIdx < 0 ||
sliceIdx >= this->FrameIndexArray->GetNumberOfTuples())
{
return -1;
}
int n = this->FrameIndexArray->GetNumberOfComponents();
return this->FrameIndexArray->GetValue(sliceIdx*n);
}
//----------------------------------------------------------------------------
int vtkDICOMMetaData::GetFrameIndex(int sliceIdx, int compIdx, int numComp)
{
if (this->FrameIndexArray == nullptr || sliceIdx < 0 ||
sliceIdx >= this->FrameIndexArray->GetNumberOfTuples() ||
compIdx < 0 || compIdx >= numComp)
{
return -1;
}
int n = this->FrameIndexArray->GetNumberOfComponents();
return this->FrameIndexArray->GetValue(sliceIdx*n + compIdx*n/numComp);
}
//----------------------------------------------------------------------------
// Return a reference to the element within the hash table, which can
// be used to insert a new value.
vtkDICOMDataElement *vtkDICOMMetaData::FindDataElementOrInsert(
vtkDICOMTag tag)
{
unsigned int m = METADATA_HASH_SIZE - 1;
unsigned int i = (tag.ComputeHash() & m);
vtkDICOMDataElement **htable = this->Table;
vtkDICOMDataElement *hptr;
if (htable == nullptr)
{
// allocate the hash table
m = METADATA_HASH_SIZE;
htable = new vtkDICOMDataElement *[METADATA_HASH_SIZE];
this->Table = htable;
do { *htable++ = nullptr; } while (--m);
htable = this->Table;
}
hptr = htable[i];
if (hptr == nullptr)
{
hptr = new vtkDICOMDataElement[4];
htable[i] = hptr;
}
else if (hptr->Next != nullptr)
{
// see if item is already there
unsigned int n = 0;
do
{
if (hptr->Tag == tag)
{
return hptr;
}
n++;
hptr++;
}
while (hptr->Next != nullptr);
// if n+1 is a power of two, double allocated space
if (n > 2 && (n & (n+1)) == 0)
{
vtkDICOMDataElement *oldptr = htable[i];
hptr = new vtkDICOMDataElement[2*(n+1)];
htable[i] = hptr;
// copy the old list
for (unsigned int j = 0; j < n; j++)
{
*hptr = oldptr[j];
// link the new element into the list
hptr->Next->Prev = hptr;
hptr->Prev->Next = hptr;
hptr++;
}
delete [] oldptr;
}
}
// insert into the linked list
vtkDICOMDataElement *tptr = &this->Tail;
do
{
tptr = tptr->Prev;
}
while (tag < tptr->GetTag());
hptr->Prev = tptr;
hptr->Next = tptr->Next;
hptr->Prev->Next = hptr;
hptr->Next->Prev = hptr;
this->NumberOfDataElements++;
return hptr;
}
//----------------------------------------------------------------------------
int vtkDICOMMetaData::FindItemsOrInsert(
int idx, bool useidx, const vtkDICOMTagPath& tagpath,
vtkDICOMItem *itemarray[])
{
vtkDICOMTag tag = tagpath.GetHead();
vtkDICOMDataElement *loc = this->FindDataElementOrInsert(tag);
if (loc == nullptr)
{
vtkErrorMacro("SetAttributeValue: tag group number must not be zero.");
return 0;
}
loc->Tag = tag;
vtkDICOMValue *vptr = &loc->Value;
if (!vptr->IsValid())
{
vtkDICOMVR vr = this->FindDictVR(idx, tag);
if (vr != vtkDICOMVR::SQ && vr != vtkDICOMVR::UN)
{
// we just inserted a non-SQ value, remove it
this->Erase(tag);
return 0;
}
}
else if (vptr->GetVR() != vtkDICOMVR::SQ)
{
return 0;
}
// is this a series of values?
int count = 1;
vtkDICOMValue *sptr = vtkDICOMValueFriendMetaData::GetMultiplex(vptr);
if (sptr != nullptr)
{
if (useidx)
{
assert(idx >= 0 && idx < this->NumberOfInstances);
sptr = &sptr[idx];
}
else
{
count = this->NumberOfInstances;
}
}
else if (useidx && this->NumberOfInstances > 1)
{
// create a multiplex
assert(idx >= 0 && idx < this->NumberOfInstances);
int n = this->NumberOfInstances;
vtkDICOMValue l;
sptr = l.AllocateMultiplexData(vtkDICOMVR::SQ, n);
for (int i = 0; i < n; i++)
{
if (i != idx)
{
sptr[i] = *vptr;
}
}
*vptr = l;
sptr = &sptr[idx];
}
else
{
sptr = vptr;
}
size_t i = tagpath.GetIndex();
for (int k = 0; k < count; k++)
{
size_t n = i+1;
size_t m = 0;
const vtkDICOMItem *oldItems = sptr[k].GetSequenceData();
if (oldItems != nullptr)
{
m = sptr[k].GetNumberOfValues();
n = (n > m ? n : m);
}
vtkDICOMValue seq;
vtkDICOMItem *items = seq.AllocateSequenceData(vtkDICOMVR::SQ, n);
// copy the old sequence into the new one (shallow copy)
for (size_t j = 0; j < m; j++)
{
items[j] = oldItems[j];
}
// Get the character set and default VR for XS
vtkDICOMCharacterSet cs = vtkDICOMCharacterSet::ISO_IR_6;
vtkDICOMVR vrForXS = vtkDICOMVR::US;
if (n > m)
{
const vtkDICOMValue& vcs = this->Get(DC::SpecificCharacterSet);
if (vcs.IsValid())
{
cs = vtkDICOMCharacterSet(vcs.GetCharData(), vcs.GetVL());
}
const vtkDICOMValue &v = this->Get(DC::PixelRepresentation);
if (v.IsValid())
{
vrForXS = (v.AsUnsignedShort() == 0 ?
vtkDICOMVR::US : vtkDICOMVR::SS);
}
}
for (size_t j = m; j < n; j++)
{
items[j] = vtkDICOMItem(cs, vrForXS);
}
sptr[k] = seq;
itemarray[k] = &items[i];
}
return count;
}
vtkDICOMItem *vtkDICOMMetaData::FindItemOrInsert(
int idx, const vtkDICOMTagPath& tagpath)
{
vtkDICOMItem *itemptr = nullptr;
this->FindItemsOrInsert(idx, true, tagpath, &itemptr);
return itemptr;
}
//----------------------------------------------------------------------------
// Insert an attribute into the hash table
void vtkDICOMMetaData::Set(vtkDICOMTag tag, const vtkDICOMValue& v)
{
if (v.IsValid())
{
vtkDICOMDataElement *loc = this->FindDataElementOrInsert(tag);
if (loc == nullptr)
{
vtkErrorMacro("SetAttributeValue: tag group number must not be zero.");
return;
}
loc->Tag = tag;
loc->Value = v;
}
else
{
this->Erase(tag);
}
}
void vtkDICOMMetaData::Set(vtkDICOMTag tag, double v)
{
vtkDICOMVR vr = this->FindDictVR(0, tag);
if (vr != vtkDICOMVR::UN)
{
this->Set(tag, vtkDICOMValue(vr, v));
}
else
{
vtkErrorMacro("SetAttributeValue: could not find tag (" <<
tag << ") in the dictionary");
}
}
void vtkDICOMMetaData::Set(vtkDICOMTag tag, const std::string& v)
{
vtkDICOMVR vr = this->FindDictVR(0, tag);
if (vr.HasSpecificCharacterSet() && tag > DC::SpecificCharacterSet)
{
this->Set(tag, this->MakeValueWithSpecificCharacterSet(vr, v));
}
else if (vr != vtkDICOMVR::UN)
{
this->Set(tag, vtkDICOMValue(vr, v));
}
else
{
vtkErrorMacro("SetAttributeValue: could not find tag (" <<
tag << ") in the dictionary");
}
}
//----------------------------------------------------------------------------
// Insert an attribute for a particular image
void vtkDICOMMetaData::Set(int idx, vtkDICOMTag tag, const vtkDICOMValue& v)
{
vtkDICOMDataElement *loc = this->FindDataElementOrInsert(tag);
if (loc == nullptr)
{
vtkErrorMacro("SetAttributeValue: tag group number must not be zero.");
return;
}
loc->Tag = tag;
vtkDICOMValue *vptr = &loc->Value;
assert(idx >= 0 && idx < this->NumberOfInstances);
// is this a sequence of values?
vtkDICOMValue *sptr = vtkDICOMValueFriendMetaData::GetMultiplex(vptr);
if (sptr)
{
sptr[idx] = v;
if (!v.IsValid())
{
// if invalid value was added, make sure valid values remain
bool valid = false;
for (int i = 0; i < this->NumberOfInstances; i++)
{
valid |= sptr[i].IsValid();
}
if (!valid)
{
this->Erase(tag);
}
}
else
{
// if all values are the same, replace with a single value
bool same = true;
for (int i = 0; i < this->NumberOfInstances && same; i++)
{
same = (i == idx || sptr[i] == v);
}
if (same)
{
loc->Value = v;
}
}
}
else if (v != *vptr)
{
// differs from other instances, must turn value into a list,
// so create a value that is actually a list of values
int n = this->NumberOfInstances;
vtkDICOMValue l;
vtkDICOMVR vr = (vptr->IsValid() ? vptr->GetVR() : v.GetVR());
sptr = l.AllocateMultiplexData(vr, n);
for (int i = 0; i < n; i++)
{
if (i == idx)
{
sptr[i] = v;
}
else
{
sptr[i] = *vptr;
}
}
*vptr = l;
}
else if (!vptr->IsValid())
{
this->Erase(tag);
}
}
void vtkDICOMMetaData::Set(int idx, vtkDICOMTag tag, double v)
{
vtkDICOMVR vr = this->FindDictVR(idx, tag);
if (vr != vtkDICOMVR::UN)
{
this->Set(idx, tag, vtkDICOMValue(vr, v));
}
else
{
vtkErrorMacro("SetAttributeValue: could not find tag (" <<
tag << ") in the dictionary");
}
}
void vtkDICOMMetaData::Set(int idx, vtkDICOMTag tag, const std::string& v)
{
vtkDICOMVR vr = this->FindDictVR(idx, tag);
if (vr.HasSpecificCharacterSet() && tag > DC::SpecificCharacterSet)
{
this->Set(idx, tag, this->MakeValueWithSpecificCharacterSet(vr, v));
}
else if (vr != vtkDICOMVR::UN)
{
this->Set(idx, tag, vtkDICOMValue(vr, v));
}
else
{
vtkErrorMacro("SetAttributeValue: could not find tag (" <<
tag << ") in the dictionary");
}
}
//----------------------------------------------------------------------------
// Insert an attribute at a particular path
void vtkDICOMMetaData::Set(
const vtkDICOMTagPath& tagpath, const vtkDICOMValue& v)
{
if (tagpath.HasTail())
{
vtkDICOMItem **items = new vtkDICOMItem *[this->NumberOfInstances];
int n = this->FindItemsOrInsert(0, false, tagpath, items);
for (int i = 0; i < n; i++)
{
items[i]->Set(tagpath.GetTail(), v);
}
delete [] items;
}
else
{
this->Set(tagpath.GetHead(), v);
}
}
void vtkDICOMMetaData::Set(const vtkDICOMTagPath& tagpath, double v)
{
if (tagpath.HasTail())
{
vtkDICOMItem **items = new vtkDICOMItem *[this->NumberOfInstances];
int n = this->FindItemsOrInsert(0, false, tagpath, items);
for (int i = 0; i < n; i++)
{
items[i]->Set(tagpath.GetTail(), v);
}
delete [] items;
}
else
{
this->Set(tagpath.GetHead(), v);
}
}
void vtkDICOMMetaData::Set(
const vtkDICOMTagPath& tagpath, const std::string& v)
{
if (tagpath.HasTail())
{
vtkDICOMItem **items = new vtkDICOMItem *[this->NumberOfInstances];
int n = this->FindItemsOrInsert(0, false, tagpath, items);
for (int i = 0; i < n; i++)
{
items[i]->Set(tagpath.GetTail(), v);
}
delete [] items;
}
else
{
this->Set(tagpath.GetHead(), v);
}
}
//----------------------------------------------------------------------------
// Insert an attribute for a particular image
void vtkDICOMMetaData::Set(
int idx, const vtkDICOMTagPath& tagpath, const vtkDICOMValue& v)
{
if (tagpath.HasTail())
{
vtkDICOMItem *item = this->FindItemOrInsert(idx, tagpath);
if (item)
{
item->Set(tagpath.GetTail(), v);
}
}
else
{
this->Set(idx, tagpath.GetHead(), v);
}
}
void vtkDICOMMetaData::Set(int idx, const vtkDICOMTagPath& tagpath, double v)
{
if (tagpath.HasTail())
{
vtkDICOMItem *item = this->FindItemOrInsert(idx, tagpath);
if (item)
{
item->Set(tagpath.GetTail(), v);
}
}
else
{
this->Set(idx, tagpath.GetHead(), v);
}
}
void vtkDICOMMetaData::Set(
int idx, const vtkDICOMTagPath& tagpath, const std::string& v)
{
if (tagpath.HasTail())
{
vtkDICOMItem *item = this->FindItemOrInsert(idx, tagpath);
if (item)
{
item->Set(tagpath.GetTail(), v);
}
}
else
{
this->Set(idx, tagpath.GetHead(), v);
}
}
//----------------------------------------------------------------------------
vtkDICOMValue vtkDICOMMetaData::MakeValueWithSpecificCharacterSet(
vtkDICOMVR vr, const std::string& v)
{
// note that there is similar code in vtkDICOMItem
vtkDICOMCharacterSet cs; // defaults to ASCII
const vtkDICOMValue& vcs = this->Get(DC::SpecificCharacterSet);
if (vcs.IsValid())
{
cs = vtkDICOMCharacterSet(vcs.GetCharData(), vcs.GetVL());
}
return vtkDICOMValue(vr, cs, v);
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::CopyAttributes(vtkDICOMMetaData *o)
{
// note: this method does not check for collisions between
// private tag blocks, so at most one of the two data sets
// can safely have private tags when this method is called.
if (o != nullptr && o != this)
{
vtkDICOMDataElement **otable = o->Table;
if (otable != nullptr)
{
const vtkDICOMDataElement *iter = o->Head.Next;
const vtkDICOMDataElement *iterEnd = &o->Tail;
while (iter != iterEnd)
{
// if this is a per-instance element, then make a copy of it
const vtkDICOMValue *vptr = iter->Value.GetMultiplexData();
if (vptr == nullptr)
{
vtkDICOMDataElement *e = this->FindDataElementOrInsert(iter->Tag);
e->Tag = iter->Tag;
e->Value = iter->Value;
}
else if (this->NumberOfInstances == o->NumberOfInstances)
{
vtkDICOMDataElement *e = this->FindDataElementOrInsert(iter->Tag);
e->Tag = iter->Tag;
vtkDICOMValue *nvptr = e->Value.AllocateMultiplexData(
iter->Value.GetVR(), this->NumberOfInstances);
for (int i = 0; i < this->NumberOfInstances; i++)
{
nvptr[i] = vptr[i];
}
}
iter = iter->Next;
}
}
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::ShallowCopy(vtkDataObject *source)
{
vtkDICOMMetaData *o = vtkDICOMMetaData::SafeDownCast(source);
if (o != this)
{
this->Initialize();
if (o != nullptr)
{
this->NumberOfInstances = o->NumberOfInstances;
this->CopyAttributes(o);
this->SetFileIndexArray(o->FileIndexArray);
this->SetFrameIndexArray(o->FrameIndexArray);
}
this->vtkDataObject::ShallowCopy(source);
}
}
//----------------------------------------------------------------------------
void vtkDICOMMetaData::DeepCopy(vtkDataObject *source)
{
vtkDICOMMetaData *o = vtkDICOMMetaData::SafeDownCast(source);
if (o != this)
{
this->Initialize();
if (o != nullptr)
{
this->NumberOfInstances = o->NumberOfInstances;
this->CopyAttributes(o);
if (o->FileIndexArray)
{
this->FileIndexArray = vtkIntArray::New();
this->FileIndexArray->DeepCopy(o->FileIndexArray);
}
if (o->FrameIndexArray)
{
this->FrameIndexArray = vtkIntArray::New();
this->FrameIndexArray->DeepCopy(o->FrameIndexArray);
}
}
this->vtkDataObject::DeepCopy(source);
}
}
//----------------------------------------------------------------------------