File: iodutil.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (825 lines) | stat: -rw-r--r-- 30,121 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  Copyright (C) 2015-2024, Open Connections GmbH
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation are maintained by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module:  dcmiod
 *
 *  Author:  Michael Onken
 *
 *  Purpose: Static helper functionality for dcmiod module
 *
 */

#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmiod/iodutil.h"
#include "dcmtk/dcmdata/dctypes.h" // logger
#include "dcmtk/dcmiod/iodrules.h"

#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcdicent.h"
#include "dcmtk/dcmdata/dcdict.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcsequen.h"
#include "dcmtk/dcmdata/dcuid.h"
#include "dcmtk/dcmdata/dcvrda.h"
#include "dcmtk/dcmdata/dcvrtm.h"
#include "dcmtk/dcmiod/iodtypes.h"
#include "dcmtk/ofstd/ofstring.h"

// --- static helpers ---

OFCondition DcmIODUtil::getAndCheckElementFromDataset(
    DcmItem& dataset, DcmElement& delem, const OFString& vm, const OFString& type, const char* moduleName)
{
    DcmStack stack;
    const DcmTagKey tagKey = delem.getTag();
    OFCondition result     = dataset.search(tagKey, stack, ESM_fromHere, OFFalse /*searchIntoSub*/);
    if (result.good() && stack.top()->isElement())
    {
        /* copy object from search stack */
        result = delem.copyFrom(*stack.top());
        /* we need a reference to the original element in order to determine the SpecificCharacterSet */
        checkElementValue(OFstatic_cast(DcmElement*, stack.top()),
                          tagKey,
                          vm,
                          type,
                          result,
                          moduleName,
                          dcmtk::log4cplus::WARN_LOG_LEVEL);
    }
    /* the element could not be found in the dataset */
    else
    {
        checkElementValue(delem, vm, type, result, moduleName, dcmtk::log4cplus::WARN_LOG_LEVEL);
    }
    return result;
}

OFCondition DcmIODUtil::getAndCheckElementFromDataset(DcmItem& dataset,
                                                      const DcmTagKey& tagKey,
                                                      DcmElement*& delem,
                                                      const OFString& vm,
                                                      const OFString& type,
                                                      const char* moduleName)
{
    if (delem)
        return EC_IllegalParameter;

    DcmStack stack;
    OFCondition result = dataset.search(tagKey, stack, ESM_fromHere, OFFalse /*searchIntoSub*/);
    if (result.good() && stack.top()->isElement())
    {
        /* copy object from search stack */
        delem = OFstatic_cast(DcmElement*, stack.top()->clone());
        /* we need a reference to the original element in order to determine the SpecificCharacterSet */
        checkElementValue(OFstatic_cast(DcmElement*, stack.top()),
                          tagKey,
                          vm,
                          type,
                          result,
                          moduleName,
                          dcmtk::log4cplus::WARN_LOG_LEVEL);
    }
    /* the element could not be found in the dataset */
    else
        checkElementValue(delem, tagKey, vm, type, result, moduleName, dcmtk::log4cplus::WARN_LOG_LEVEL);

    return result;
}

OFCondition DcmIODUtil::getAndCheckElementFromDataset(DcmItem& dataset, DcmElement& delem, const IODRule* rule)
{
    if (rule == NULL)
        return EC_CannotCheck;

    return getAndCheckElementFromDataset(dataset, delem, rule->getVM(), rule->getType(), rule->getModule().c_str());
}

OFCondition DcmIODUtil::getAndCheckElementFromDataset(DcmItem& dataset, DcmElement*& delem, const IODRule* rule)
{
    if (rule == NULL)
        return EC_CannotCheck;

    return getAndCheckElementFromDataset(
        dataset, rule->getTagKey(), delem, rule->getVM(), rule->getType(), rule->getModule().c_str());
}

OFCondition
DcmIODUtil::copyElementToDataset(OFCondition& result, DcmItem& dataset, const DcmElement& delem, const IODRule* rule)
{
    if (rule == NULL)
        return EC_CannotCheck;

    if (result.bad())
        return result;

    // addElementToDataset() consumes element, so create a copy
    DcmElement* copy = OFstatic_cast(DcmElement*, delem.clone());
    if (!copy)
        return EC_MemoryExhausted;

    return addElementToDataset(result, dataset, copy, rule);
}

OFCondition DcmIODUtil::copyElementToDataset(OFCondition& result,
                                             DcmItem& dataset,
                                             const DcmElement& delem,
                                             const OFString& vm,
                                             const OFString& type,
                                             const char* moduleName)
{
    // Create temporary rule (in order to avoid code-copying)
    IODRule rule(delem.getTag(), vm, type, moduleName, DcmIODTypes::IE_UNDEFINED);
    return copyElementToDataset(result, dataset, delem, &rule);
}

OFCondition
DcmIODUtil::addElementToDataset(OFCondition& result, DcmItem& dataset, DcmElement* delem, const IODRule* rule, const OFBool checkValue)
{
    OFBool insertionOK = OFFalse;
    if (result.good())
    {
        if (rule != NULL)
        {
            // Create empty type 2 element if required
            OFString type = rule->getType();
            if (delem == NULL)
            {
                if (type == "2")
                {
                    delem = DcmItem::newDicomElement(rule->getTagKey());
                    if (delem == NULL)
                    {
                        result = EC_MemoryExhausted;
                        return result;
                    };
                }
                else if (type != "1")
                {
                    // Not type 1 or type 2 means the is type 1C, 2C or 3. For those it is
                    // fine to not insert anything
                    return EC_Normal;
                }
                else
                {
                    // type 1 is missing
                    DCMIOD_ERROR(DcmTag(rule->getTagKey()).getTagName()
                                 << " " << rule->getTagKey() << " absent in " << rule->getModule() << " (type "
                                 << rule->getType() << ")");
                    result = IOD_EC_MissingAttribute;
                    return result;
                }
            }
            // At this point, we certainly have an element. Check its value (empty ok for type 2).
            dcmtk::log4cplus::LogLevel logLevel;
            if (checkValue) logLevel = dcmtk::log4cplus::ERROR_LOG_LEVEL; else logLevel = dcmtk::log4cplus::WARN_LOG_LEVEL;
            if ((type == "2") || !delem->isEmpty())
            {
                // Insert non-empty element or empty "type 2" element. First, perform the insertion, and then
                // check the value. This is (at least) required for checking the character set of string values which
                // relies on the element context, i.e. the surrounding item or dataset.
                result = dataset.insert(delem, OFTrue /*replaceOld*/);
                if (result.good())
                {
                    result = checkElementValue(*delem,
                                               rule->getVM(),
                                               type,
                                               result,
                                               rule->getModule().c_str(),
                                               logLevel);
                    resetConditionIfCheckDisabled(result, checkValue, *delem);
                }
                if (result.good())
                {
                    insertionOK = OFTrue;
                }
                // remove element if value is invalid
                else
                {
                    dataset.remove(delem);
                }
            }
            else if (type == "1")
            {
                // Empty element value not allowed for "type 1"
                result = EC_InvalidValue;
                checkElementValue(
                    *delem, rule->getVM(), type, result, rule->getModule().c_str(), logLevel);
            }
        }
        else
        {
            // No rule, no checks
            result = EC_CannotCheck;
        }
    }
    if (!insertionOK)
    {
        // Delete element since dataset did not take over ownership
        delete delem;
    }

    return result;
}

const OFString& DcmIODUtil::currentDate(OFString& dateString)
{
    if (DcmDate::getCurrentDate(dateString).bad())
    {
        dateString = "";
    }
    return dateString;
}

const OFString& DcmIODUtil::currentTime(OFString& timeString)
{
    if (DcmTime::getCurrentTime(timeString, OFTrue /*seconds*/, OFFalse /*fraction*/).bad())
    {
        timeString = "";
    }
    return timeString;
}

OFCondition DcmIODUtil::checkElementValue(const DcmElement* delem,
                                          const DcmTagKey& tagKey,
                                          const OFString& vm,
                                          const OFString& type,
                                          const OFCondition& searchCond,
                                          const char* moduleName,
                                          const dcmtk::log4cplus::LogLevel logLevel)
{
    OFCondition result     = EC_Normal;
    const OFString tagName = DcmTag(tagKey).getTagName();
    const OFString module  = (moduleName == NULL) ? "IOD" : moduleName;
    OFOStringStream error;
    /* NB: type 1C and 2C cannot be checked, assuming to be optional */
    if (((type == "1") || (type == "2")) && searchCond.bad())
    {
        error << tagName << " " << tagKey << " absent in " << module << " (type " << type << ")";
        result = IOD_EC_MissingAttribute;
    }
    else if ((delem == NULL)
             || OFconst_cast(DcmElement*, delem)
                    ->isEmpty(OFTrue /*normalize*/)) // cast away constness of delem; value modification can happen (eg.
                                                     // to remove padding)
    {
        /* however, type 1C should never be present with empty value */
        if (((type == "1") || (type == "1C")) && searchCond.good())
        {
            error << tagName << " " << tagKey << " empty in " << module << " (type " << type << ")";
            result = EC_MissingValue;
        }
    }
    else
    {
        result = OFconst_cast(DcmElement*, delem)
                     ->checkValue(vm, OFTrue /*oldFormat*/); // cast away constness of delem; value modification can
                                                             // happen (eg. to remove padding)
        if (result == EC_InvalidCharacter)
        {
            error << tagName << " " << tagKey << " contains invalid character(s) in " << module;
        }
        else if (result == EC_ValueRepresentationViolated)
        {
            error << tagName << " " << tagKey << " violates VR definition in " << module;
        }
        else if (result == EC_ValueMultiplicityViolated)
        {
            const OFString vmText = (delem->getVR() == EVR_SQ) ? " #items" : " VM";
            error << tagName << " " << tagKey << vmText << " != " << vm << " in " << module;
        }
        else if (result == EC_MaximumLengthViolated)
        {
            error << tagName << " " << tagKey << " violates maximum VR length in " << module;
        }
        else if (result.bad())
        {
            error << "INTERNAL ERROR while checking value of " << tagName << " " << tagKey << " in " << module;
            result = EC_Normal;
        }
    }
    OFSTRINGSTREAM_GETSTR(error, tmpString)
    if (strlen(tmpString) > 0)
    {
        switch (logLevel)
        {
            case dcmtk::log4cplus::TRACE_LOG_LEVEL:
                DCMIOD_TRACE(tmpString);
                break;
            case dcmtk::log4cplus::DEBUG_LOG_LEVEL:
                DCMIOD_DEBUG(tmpString);
                break;
            case dcmtk::log4cplus::WARN_LOG_LEVEL:
                DCMIOD_WARN(tmpString);
                break;
            case dcmtk::log4cplus::INFO_LOG_LEVEL:
                DCMIOD_INFO(tmpString);
                break;
            case dcmtk::log4cplus::ERROR_LOG_LEVEL:
                DCMIOD_ERROR(tmpString);
                break;
            case dcmtk::log4cplus::FATAL_LOG_LEVEL:
                DCMIOD_FATAL(tmpString);
                break;
            default:
                DCMIOD_WARN(tmpString);
                ;
        }
    }
    OFSTRINGSTREAM_FREESTR(tmpString)
    return result;
}

OFCondition DcmIODUtil::checkElementValue(const DcmElement& delem,
                                          const OFString& vm,
                                          const OFString& type,
                                          const OFCondition& searchCond,
                                          const char* moduleName,
                                          const dcmtk::log4cplus::LogLevel logLevel)
{
    /* call the real function */
    return checkElementValue(&delem, delem.getTag(), vm, type, searchCond, moduleName, logLevel);
}

OFCondition DcmIODUtil::getStringValueFromElement(const DcmElement& delem, OFString& stringValue, const signed long pos)
{
    OFCondition result = EC_Normal;
    // cast away constness of delem; value modification can happen (eg. to remove padding)
    if (pos < 0)
        result = OFconst_cast(DcmElement&, delem).getOFStringArray(stringValue);
    else
        result = OFconst_cast(DcmElement&, delem).getOFString(stringValue, OFstatic_cast(unsigned long, pos));
    if (result.bad())
        stringValue.clear();
    return result;
}

OFCondition DcmIODUtil::getFloat64ValueFromElement(const DcmElement& delem, Float64& result, const unsigned long pos)
{
    return OFconst_cast(DcmElement&, delem).getFloat64(result, OFstatic_cast(unsigned long, pos));
}

OFCondition DcmIODUtil::getFloat64ValuesFromElement(const DcmElement& delem, OFVector<Float64>& result)
{
    Float64* floats  = NULL;
    OFCondition cond = OFconst_cast(DcmElement&, delem).getFloat64Array(floats);
    if (cond.good())
    {
        size_t vm = OFconst_cast(DcmElement&, delem).getNumberOfValues();
        for (size_t n = 0; n < vm; n++)
        {
            result.push_back(floats[n]);
        }
    }
    return cond;
}

OFCondition
DcmIODUtil::getStringValueFromItem(const DcmTagKey& key, DcmItem& item, OFString& result, const signed long& pos)
{
    DcmElement* elem = NULL;
    item.findAndGetElement(key, elem);
    if (elem != NULL)
        return DcmIODUtil::getStringValueFromElement(*elem, result, pos);
    else
        return EC_TagNotFound;
}

OFCondition
DcmIODUtil::getFloat64ValueFromItem(const DcmTagKey& key, DcmItem& item, Float64& result, const unsigned long& pos)
{
    DcmElement* elem = NULL;
    OFCondition cond = item.findAndGetElement(key, elem);
    if (elem != NULL)
    {
        cond = DcmIODUtil::getFloat64ValueFromElement(*elem, result, pos);
    }
    return cond;
}

OFCondition DcmIODUtil::getFloat64ValuesFromItem(const DcmTagKey& key, DcmItem& item, OFVector<Float64>& result)
{
    DcmElement* elem = NULL;
    OFCondition cond = item.findAndGetElement(key, elem);
    if (elem != NULL)
    {
        cond = DcmIODUtil::getFloat64ValuesFromElement(*elem, result);
    }
    return cond;
}

OFCondition DcmIODUtil::setFloat64ValuesOnElement(DcmElement& delem,
                                                  const OFVector<Float64>& values,
                                                  const OFString& vm,
                                                  const OFBool check)
{
    OFCondition result;
    if (values.size() > OFnumeric_limits<unsigned long>::max())
    {
        DCMIOD_ERROR("Too many values provided (" << values.size() << " for element: " << delem.getTag());
        return IOD_EC_InvalidElementValue;
    }
    const unsigned long vmCount          = OFstatic_cast(unsigned long, values.size());
    OFVector<Float64>::const_iterator it = values.begin();
    for (unsigned long count = 0; count < vmCount; count++)
    {
        result = delem.putFloat64((*it), count);
        if (result.bad())
        {
            DCMIOD_WARN(delem.getTag() << ": Setting value "
                                       << " #" << count << " to \" " << *it << "\" not possible");
        }
        else if (check)
        {
            result = DcmElement::checkVM(vmCount, vm);
        }
        it++;
    }
    return result;
}

OFCondition DcmIODUtil::getFloat32ValueFromElement(const DcmElement& delem, Float32& result, const unsigned long pos)
{
    return OFconst_cast(DcmElement&, delem).getFloat32(result, OFstatic_cast(unsigned long, pos));
}

OFCondition DcmIODUtil::setFloat32ValuesOnElement(DcmElement& delem,
                                                  const OFVector<Float32>& values,
                                                  const OFString& vm,
                                                  const OFBool check)
{
    OFCondition result;
    if (values.size() > OFnumeric_limits<unsigned long>::max())
    {
        DCMIOD_ERROR("Too many values provided (" << values.size() << " for element: " << delem.getTag());
        return IOD_EC_InvalidElementValue;
    }
    const unsigned long vmCount          = OFstatic_cast(unsigned long, values.size());
    OFVector<Float32>::const_iterator it = values.begin();
    for (unsigned long count = 0; count < vmCount; count++)
    {
        result = delem.putFloat32((*it), count);
        if (result.bad())
        {
            DCMIOD_WARN(delem.getTag() << ": Setting value "
                                       << " #" << count << " to \" " << *it << "\" not possible");
        }
        else if (check)
        {
            result = DcmElement::checkVM(vmCount, vm);
        }
        it++;
    }
    return result;
}

OFCondition DcmIODUtil::setUint16ValuesOnElement(DcmElement& delem,
                                                 const OFVector<Uint16>& values,
                                                 const OFString& vm,
                                                 const OFBool check)
{
    OFCondition result;
    if (values.size() > OFnumeric_limits<unsigned long>::max())
    {
        DCMIOD_ERROR("Too many values provided (" << values.size() << " for element: " << delem.getTag());
        return IOD_EC_InvalidElementValue;
    }
    const unsigned long vmCount         = OFstatic_cast(unsigned long, values.size());
    OFVector<Uint16>::const_iterator it = values.begin();
    for (unsigned long count = 0; count < vmCount; count++)
    {
        result = delem.putUint16((*it), count);
        if (result.bad())
        {
            DCMIOD_WARN(delem.getTag() << ": Setting value "
                                       << " #" << count << " to \" " << *it << "\" not possible");
        }
        else if (check)
        {
            result = DcmElement::checkVM(vmCount, vm);
        }
        it++;
    }
    return result;
}

OFCondition DcmIODUtil::getUint16ValuesFromElement(DcmElement& delem, OFVector<Uint16>& values)
{
    OFCondition result;
    const size_t count = delem.getNumberOfValues();
    values.reserve(count);
    for (size_t i = 0; i < count; i++)
    {
        Uint16 val;
        result = delem.getUint16(val, OFstatic_cast(unsigned long, i));
        if (result.bad())
        {
            DCMIOD_WARN(delem.getTag() << ": Getting value "
                                       << " #" << i << " not possible");
            break;
        }
        values.push_back(val);
    }
    return result;
}

void DcmIODUtil::checkSubSequence(OFCondition& result,
                                  DcmItem& surroundingItem,
                                  const DcmTagKey& seqKey,
                                  const OFString& cardinality,
                                  const OFString& type,
                                  const OFString& module,
                                  const dcmtk::log4cplus::LogLevel logLevel)
{
    OFCondition exists = EC_Normal;
    /* check result */
    if (result.good())
    {
        DcmSequenceOfItems* seq = NULL;
        exists                  = surroundingItem.findAndGetSequence(seqKey, seq);
        result = DcmIODUtil::checkElementValue(seq, seqKey, cardinality, type, exists, module.c_str(), logLevel);
    }
}

OFCondition DcmIODUtil::getAndCheckSingleItem(DcmSequenceOfItems& seq, DcmItem*& item, const DcmTagKey& checkKey)
{
    item                   = NULL;
    const OFString tagName = OFconst_cast(DcmTag*, &seq.getTag())->getTagName(); // getTagName is not const...
    if (checkKey != DCM_UndefinedTagKey)
    {
        if (seq.getTag() != checkKey)
        {
            DCMIOD_ERROR("Expected sequence " << checkKey << " but got " << &seq.getTag() << "(" << tagName << ")");
            return EC_ItemNotFound;
        }
    }

    // get actual tag name and cardinality
    const unsigned long card = seq.card();
    if (card != 1)
    {
        if (card > 1)
        {
            DCMIOD_WARN("Only single item allowed in " << tagName << ", ignoring " << card - 1 << " other items");
        }
        else // card = 0
        {
            DCMIOD_WARN("Cannot read from sequence " << tagName << ": No item found");
            return EC_ItemNotFound;
        }
    }
    // get item and return it
    item = seq.getItem(0);
    if (item == NULL)
    {
        DCMIOD_ERROR("Cannot read item from " << tagName << ": No item found (internal error)");
        return EC_CorruptedData;
    }
    return EC_Normal;
}

OFCondition DcmIODUtil::checkSOPClass(DcmItem* item, const OFString& desiredSOPClass, OFString& valueFound)
{
    valueFound.clear();
    if ((item == NULL) || (item->card() == 0))
    {
        DCMIOD_TRACE("Cannot check SOP Class UID: Dataset not present or empty");
        return EC_IllegalParameter;
    }
    OFCondition result = item->findAndGetOFString(DCM_SOPClassUID, valueFound);
    if (result.bad())
    {
        DCMIOD_TRACE("No SOP Class UID in file, giving up");
        return EC_TagNotFound;
    }
    if (valueFound != desiredSOPClass)
    {
        DCMIOD_TRACE("File is not of SOP Class " << desiredSOPClass << ", but instead SOP Class is "
                                                 << dcmFindNameOfUID(valueFound.c_str(), valueFound.c_str()));
        return EC_InvalidValue;
    }
    return EC_Normal;
}

OFBool DcmIODUtil::isSequenceTag(const DcmTagKey& key, const OFString& privateCreator)
{
    const DcmDataDictionary& globalDataDict = dcmDataDict.rdlock();
    const DcmDictEntry* dictRef             = NULL;
    if (privateCreator.empty())
        dictRef = globalDataDict.findEntry(key, NULL);
    else
        dictRef = globalDataDict.findEntry(key, privateCreator.c_str());
    DcmVR vr;
    if (dictRef)
    {
        vr = dictRef->getVR();
    }
    dcmDataDict.rdunlock();
    if (vr.getEVR() == EVR_SQ)
        return OFTrue;
    return OFFalse;
}

const DcmTagKey DcmIODUtil::parseTagKey(const OFString& keyString)
{
    unsigned int g, e;
    DcmTagKey resultKey = DCM_UndefinedTagKey;
    if (sscanf(keyString.c_str(), "(%x,%x)", &g, &e) == 2)
    {
        resultKey.set(OFstatic_cast(Uint16, g), OFstatic_cast(Uint16, e));
    }
    return resultKey;
}

OFCondition DcmIODUtil::decompress(DcmDataset& dset)
{
    DcmXfer xfer = dset.getOriginalXfer();
    if (xfer.isPixelDataCompressed())
    {
        if (EC_Normal != dset.chooseRepresentation(EXS_LittleEndianExplicit, NULL))
        {

            DCMIOD_ERROR("No conversion from " << xfer.getXferName() << " to uncompressed transfer syntax possible!");
            return IOD_EC_CannotDecompress;
        }
    }

    return EC_Normal;
}

OFString DcmIODUtil::createUID(const Uint8 level)
{
    char uid[100];
    switch (level)
    {
        case 0:
            dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
            break;
        case 1:
            dcmGenerateUniqueIdentifier(uid, SITE_SERIES_UID_ROOT);
            break;
        case 2:
            dcmGenerateUniqueIdentifier(uid, SITE_STUDY_UID_ROOT);
            break;
        default:
            dcmGenerateUniqueIdentifier(uid, SITE_STUDY_UID_ROOT);
    }
    return uid;
}

Uint32 DcmIODUtil::limitMaxFrames(const size_t numFramesPresent, const OFString& warning)
{
    // limit to 2^31-1 since this is the Number of Frames attribute's VR IS' maximum value
    if (numFramesPresent > 2147483647)
    {
        DCMIOD_WARN(warning);
        return 2147483647;
    }
    return OFstatic_cast(Uint32, numFramesPresent);
}

OFCondition DcmIODUtil::extractBinaryFrames(Uint8* pixData,
                                            const size_t numFrames,
                                            const size_t bitsPerFrame,
                                            OFVector<DcmIODTypes::Frame*>& results)
{
    if (pixData == NULL)
    {
        DCMIOD_ERROR("Cannot extract frames from NULL pixel data");
        return EC_IllegalParameter;
    }
    if (numFrames == 0)
    {
        DCMIOD_ERROR("Cannot extract frames from 0 frames");
        return EC_IllegalParameter;
    }
    if (bitsPerFrame == 0)
    {
        DCMIOD_ERROR("Cannot extract frames with 0 bits per frame");
        return EC_IllegalParameter;
    }
    // pixData contains all frames in a single buffer. Each frame is bitsPerFrame bits long.
    // We need to extract each frame into a separate buffer (results vector). Every frame
    // in the input takes bitsPerFrame bits, and pixData's bytes are filled from left to right,
    // with bits filled from right to left (!). This means that the first frame's first bit
    // is the bit on the right of the first byte in pixData, and the last frame's last bit is
    // the bit on the left of the last byte in pixData. However, the last frame's last bit
    // can be in the middle of a byte. The resulting frame contains an exact copy of each
    // frame, however, the last byte of each frame is padded with 0s to make it a full byte.
    // E.g. if the last frame occupies only 3 bits of the last required byte, the last byte
    // of the frame will be padded with 5 0s on the left side of the that byte (since as said, bits
    // are filled from right to left).
    // Calculate number of bytes per frame
    size_t bytesPerFrame = bitsPerFrame / 8;
    if (bitsPerFrame % 8 != 0)
    {
        bytesPerFrame++; // If bitsPerFrame is not a multiple of 8, we need one more byte
    }
    // Reserve memory for results
    results.reserve(numFrames);
    for (size_t i = 0; i < numFrames; i++)
    {
        Uint8* frameData = new Uint8[bytesPerFrame];
        if (frameData == NULL)
        {
            DCMIOD_ERROR("Memory exhausted while extracting frames");
            return EC_MemoryExhausted;
        }
        memset(frameData, 0, bytesPerFrame); // Initialize to 0
        DcmIODTypes::Frame* frame = new DcmIODTypes::Frame();
        if (frame == NULL)
        {
            DCMIOD_ERROR("Memory exhausted while extracting frames");
            delete[] frameData;
            return EC_MemoryExhausted;
        }
        frame->pixData = frameData;
        frame->length = bytesPerFrame;
        results.push_back(frame);
    }

    // Extract frames
    size_t bitsLeftInInputByte = 8;
    size_t bitsLeftInTargetByte = 8;
    size_t bitsLeftInFrame = bitsPerFrame;
    size_t inputByteIndex = 0;
    size_t targetFrameIndex = 0;
    size_t targetByteIndex = 0;
    // Iterate over bits in input data (byte per byte, from left to right, and within byte from right to left)
    for (size_t i = 0; i < numFrames * bitsPerFrame; i++)
    {
        // Get current bit
        Uint8 bit = (pixData[inputByteIndex] >> (8 - bitsLeftInInputByte)) & 0x01;

        // Set bit in current frame to to position calculated from bitsLeftInTargetByte
        results[targetFrameIndex]->pixData[targetByteIndex] |= (bit << (8 - bitsLeftInTargetByte));

        // Move to next bit
        bitsLeftInInputByte--;
        bitsLeftInFrame--;
        bitsLeftInTargetByte--;

        // If we have processed all bits in the current byte, move to next byte
        if (bitsLeftInInputByte == 0)
        {
            bitsLeftInInputByte = 8;
            inputByteIndex++;
        }
        // If we have processed all bits in the current frame, move to next frame,
        // and also start new target frame
        if (bitsLeftInFrame == 0)
        {
            bitsLeftInFrame = bitsPerFrame;
            targetFrameIndex++;
            targetByteIndex = 0;
            bitsLeftInTargetByte = 8;
        }
        // Advance to next target byte if we have filled the current one
        if (bitsLeftInTargetByte == 0)
        {
            bitsLeftInTargetByte = 8;
            targetByteIndex++;
        }
    }
    return EC_Normal;
}


void DcmIODUtil::resetConditionIfCheckDisabled(OFCondition& result, const OFBool checkValue, DcmElement& elem)
{
    if (!checkValue)
    {
        if ( (result == EC_ValueRepresentationViolated) ||
             (result == EC_MaximumLengthViolated) ||
             (result == EC_InvalidCharacter) ||
             (result == EC_ValueMultiplicityViolated) )
        {
            // print element to string
            OFOStringStream oss;
            oss << elem.getTag() << " " << DcmVR(elem.getVR()).getVRName() << " ";
            oss << DcmTag(elem.getTag()).getTagName() << " ";
            if (elem.getLength() > 1024)
            {
                oss << "(value too long for printing)";
            }
            {
                OFString val;
                elem.getOFString(val, 0, OFTrue);
                oss << "[" << val << "]";
            }

            DCMIOD_DEBUG("Ignoring error (" << result.text() <<") when checking element: " << oss.str().c_str());
            result = EC_Normal;
        }
    }
}