File: dpmparametricmapiod.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 (1064 lines) | stat: -rw-r--r-- 31,952 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/*
 *
 *  Copyright (C) 2016-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: dcmpmap
 *
 *  Author: Michael Onken
 *
 *  Purpose: Class for managing the Parametric Map IOD
 *
 */

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmpmap/dpmparametricmapiod.h"
#include "dcmtk/dcmiod/iodutil.h"
#include "dcmtk/dcmdata/dcvrobow.h"
#include "dcmtk/dcmdata/dcvrfl.h"
#include "dcmtk/dcmdata/dcvrfd.h"
#include "dcmtk/dcmdata/dcuid.h"


// ----------------------------------------------------------------------------
// Class SetImagePixelModuleVisitor:
// Class for setting Image Pixel Module attributes based on pixel data type
// ----------------------------------------------------------------------------

struct DPMParametricMapIOD::SetImagePixelModuleVisitor
{
  SetImagePixelModuleVisitor(const Uint16 r,
                             const Uint16 c)
  : rows(r)
  , cols(c)
  {

  }

  template<typename T>
  OFCondition operator()( T& t )
  {
    if ( (rows == 0) || (cols == 0) )
    {
      DCMPMAP_ERROR("Rows/Cols must be non-zero but are : " << rows << "/" << cols);
      return DPM_InvalidDimensions;
    }

    t.setRows(rows);
    t.setColumns(cols);
    setSpecificValues(t);

    return EC_Normal;
  }

  template<typename T>
  void setSpecificValues(IODImagePixelModule<T>& p)
  {
    p.setBitsAllocated(16);
    p.setBitsStored(16);
    p.setHighBit(15);
    p.setSamplesPerPixel(1);
    p.setPhotometricInterpretation("MONOCHROME2");
    p.setPixelRepresentation(OFis_signed<T>::value ? 1 : 0);
  }

  void setSpecificValues(IODFloatingPointImagePixelModule& p)
  {
    // Avoid compiler warning about unused parameter "p"
    (void)p;
    // No specific values for 32 bit floating point data to be set
  }

  void setSpecificValues(IODDoubleFloatingPointImagePixelModule& p)
  {
    // Avoid compiler warning about unused parameter "p"
    (void)p;
    // No specific values for 64 bit floating point data to be set
  }
  // Members
  const Uint16 rows;
  const Uint16 cols;
};


struct DPMParametricMapIOD::ReadVisitor
{
  // Inner class that implements the specializations for different
  // pixel data types
  template<typename T>
  class DcmElementOf;

  ReadVisitor(DcmItem& srcItem,
              DPMParametricMapIOD& m)
  : item(srcItem)
  , map(m)
  {
    // Nothing to do
  }

  OFCondition operator()(OFmonostate)
  {
    return DPM_InvalidPixelData;
  }

  template<typename ImagePixel>
  OFCondition operator()(ImagePixel& pixel)
  {
    OFCondition result = map.readGeneric(item);
    if (result.good())
    {
      Uint16 rows, cols;
      Uint32 numFrames;
      size_t numBytesFrame = 0;
      rows = cols = 0;
      map.getRows(rows);
      map.getColumns(cols);
      numFrames = DcmIODUtil::limitMaxFrames(map.getFunctionalGroups().getNumberOfFrames(),
                                             "Functional groups implicate more than 2147483647 frames, only 2147483647 will be used");
      if (!rows || !cols || !numFrames)
      {
        DCMPMAP_ERROR("Rows (" << rows << "), Columns (" << cols << ") and Number of Frames (" << numFrames << ") must not be 0");
        return DPM_InvalidPixelInfo;
      }
      numBytesFrame = OFstatic_cast(size_t,rows) * cols * sizeof(typename ImagePixel::value_type);
      result = readSpecific(pixel, numFrames, numBytesFrame);
    }
    return result;
  }

  template<typename T>
  OFCondition readSpecific(IODImagePixelModule<T>& p,
                           const Uint32 numFrames,
                           const size_t numBytesFrame)
  {
    // Avoid compiler warning about unused parameter
    (void)p;
    unsigned long numTotalWords = 0;
    const Uint16* pixData = NULL;
    if (item.findAndGetUint16Array(DCM_PixelData, pixData, &numTotalWords).good())
    {
      if (numTotalWords == numBytesFrame * numFrames / 2 /* we compare to num words not num bytes */)
      {
        for (Uint32 n = 0; n < numFrames; n++)
        {
          DcmIODTypes::Frame* f = new DcmIODTypes::Frame;
          if (f)
          {
            f->length = numBytesFrame;
            f->pixData = new Uint8[f->length];
            memcpy(f->pixData, pixData + n*numBytesFrame/2, numBytesFrame);
            map.m_Frames.push_back(f);
          }
          else
          {
            return EC_MemoryExhausted;
          }
        }
      }
      else
      {
        DCMPMAP_ERROR("Invalid number of pixels: Expected " << numBytesFrame * numFrames / 2 << " pixels but Pixel Data has " << numTotalWords << " pixels");
        return DPM_InvalidPixelInfo;
      }
    }
    else
    {
      DCMPMAP_ERROR("No Pixel Data element found");
      return DPM_NoPixelData;
    }
    return EC_Normal;
  }

  OFCondition readSpecific(IODFloatingPointImagePixelModule& p,
                           const Uint32 numFrames,
                           const size_t numBytesFrame)
  {
    // Avoid compiler warning on unused parameter
    (void)p;
    unsigned long numTotalFloats = 0;
    const Float32* pixData = NULL;
    if (item.findAndGetFloat32Array(DCM_FloatPixelData, pixData, &numTotalFloats).good())
    {
      if (numTotalFloats == numBytesFrame * numFrames / 4 /* we compare to 32 bit floats not bytes */)
      {
        for (Uint32 n=0; n < numFrames; n++)
        {
          DcmIODTypes::Frame* f = new DcmIODTypes::Frame;
          if (f)
          {
            f->length = numBytesFrame;
            f->pixData = new Uint8[f->length];
            memcpy(f->pixData, pixData + n*numBytesFrame/4, numBytesFrame);
            map.m_Frames.push_back(f);
          }
          else
          {
            return EC_MemoryExhausted;
          }
        }
      }
      else
      {
        DCMPMAP_ERROR("Invalid number of pixels: Expected " << numBytesFrame * numFrames / 4 << " pixels but Float Pixel Data has " << numTotalFloats << " pixels");
        return DPM_InvalidPixelInfo;
      }
    }
    else
    {
      DCMPMAP_ERROR("No Float Pixel Data element found");
      return DPM_NoPixelData;
    }
    return EC_Normal;
  }

  OFCondition readSpecific(IODDoubleFloatingPointImagePixelModule& p,
                           const Uint32 numFrames,
                           const size_t numBytesFrame)
  {
    // Avoid compiler warning on unused parameter
    (void)p;
    unsigned long numTotalFloats = 0;
    const Float64* pixData = NULL;
    if (item.findAndGetFloat64Array(DCM_DoubleFloatPixelData, pixData, &numTotalFloats).good())
    {
      if (numTotalFloats == numBytesFrame * numFrames / 8 /* we compare to 64 bit floats not bytes */)
      {
        for (Uint16 n=0; n < numFrames; n++)
        {
          DcmIODTypes::Frame* f = new DcmIODTypes::Frame;
          if (f)
          {
            f->length = numBytesFrame;
            f->pixData = new Uint8[f->length];
            memcpy(f->pixData, pixData + n*numBytesFrame/8, numBytesFrame);
            map.m_Frames.push_back(f);
          }
          else
          {
            return EC_MemoryExhausted;
          }
        }
      }
      else
      {
        DCMPMAP_ERROR("Invalid number of pixels: Expected " << numBytesFrame * numFrames / 8 << " pixels but Double Float Pixel Data has " << numTotalFloats << " pixels");
        return DPM_InvalidPixelInfo;
      }
    }
    else
    {
      DCMPMAP_ERROR("No Double Float Pixel Data element found");
      return DPM_NoPixelData;
    }
    return EC_Normal;
  }

  // Members, i.e. parameters to operator()
  DcmItem& item;
  DPMParametricMapIOD& map;
};



// ----------------------------------------------------------------------------
// Class WriteVisitor:
// Class for adding DICOM pixel data bulk element based on type pixel data type
// ----------------------------------------------------------------------------

// Generic implementation, template specialization is done in inner
// DcmElementOf class
struct DPMParametricMapIOD::WriteVisitor
{
  // Inner class that implements the specializations for different
  // pixel data types
  template<typename T>
  class DcmElementOf;

  // Constructor, sets parameters the visitor works on in operator()
  WriteVisitor(DcmItem& i,
               DPMParametricMapIOD& m)
  : item(i)
  , map(m)
  {
    // Nothing else to do
  }

  //
  OFCondition operator()(OFmonostate)
  {
    DCMPMAP_ERROR("Could not write pixel data: Invalid pixel data type, only 16 bit integer as well as 32 and 64 bit float data are handled");
    return DPM_NoPixelData;
  }

  template<typename ImagePixel>
  OFCondition operator()(ImagePixel& pixel)
  {
    // Avoid compiler warning about unused parameter "pixel"
    (void)pixel;
    // Input data is checked before
    Uint16 rows = 0;
    Uint16 cols = 0;
    map.getRows(rows);
    map.getColumns(cols);
    const size_t numFrames = map.m_Frames.size();
    const size_t numBytesFrame = map.m_Frames[0]->length;
    const size_t numPixelsFrame = rows * cols;
    // Creates the correct pixel data element, based on the image pixel module used.
    // I.e. For integer data, the "Pixel Data" element is used, i.e. the DcmElement type
    // instantiated by DcmElementOf is of DCMTK type DcmOtherByteOtherWord while for
    // IODFloatingPointImagePixelModule which has floating point pixel data, DcmFloatingPointSingle
    // is used.
    DcmElementOf<ImagePixel> element;
    // copy all frames into map's frame structure
    if (OFTypename ImagePixel::value_type* ptr = element.getData(numBytesFrame * numFrames))
    {
      for (size_t f = 0; f < numFrames; ++f)
      {
        memcpy(ptr, map.m_Frames[f]->pixData, numBytesFrame);
        ptr += numPixelsFrame;
      }
      OFCondition result = element.put(item);
      // Write generic (i.e. non pixel data-specific attributes)
      return result.good() ? map.writeGeneric(item) : result;
    }
    return EC_MemoryExhausted;
  }

  // Members, i.e. parameters to operator()
  DcmItem& item;
  DPMParametricMapIOD& map;
};


// ----------------------------------------------------------------------------
// Class WriteVisitor:DcmElementOf<IODImagePixelModule<PixelType> >:
// Offers creation and insertion for Pixel Data element as used for
// int-based pixel data. So far, Uint8, Sint8, Uint16 and Sint16 pixel
// data is supported (relates to OB/OW data type for DICOM Pixel data)
// ----------------------------------------------------------------------------

template<typename PixelType>
class DPMParametricMapIOD::WriteVisitor::DcmElementOf<IODImagePixelModule<PixelType> >
{
public:

  DcmElementOf()
  : m_pElement(new DcmOtherByteOtherWord(DCM_PixelData))
  {
    // Nothing else to do
  }

  PixelType* getData(const size_t count);

  OFCondition put(DcmItem& itemPara)
  {
    OFCondition result = itemPara.insert(m_pElement.get(), OFTrue);
    if (result.good())
      m_pElement.release();
    return result;
  }

private:

  OFunique_ptr<DcmOtherByteOtherWord> m_pElement;
};


template<>
Uint16* DPMParametricMapIOD::WriteVisitor::DcmElementOf<IODImagePixelModule<Uint16> >::getData(const size_t size /* num total pixels */)
{
  if (m_pElement)
  if (m_pElement->setVR(EVR_OW).good())
  {
    Uint16* result;
    if (m_pElement->createUint16Array(OFstatic_cast(Uint32, size/2), result).good())
      return result;
  }
  return OFnullptr;
}


template<>
Sint16* DPMParametricMapIOD::WriteVisitor::DcmElementOf<IODImagePixelModule<Sint16> >::getData(const size_t size)
{
  if (m_pElement)
  if (m_pElement->setVR(EVR_OW).good())
  {
    Uint16* result;
    if (m_pElement->createUint16Array(OFstatic_cast(Uint32, size/2), result).good())
      return OFreinterpret_cast(Sint16*, result);
  }
  return OFnullptr;
}


// ----------------------------------------------------------------------------
// Class WriteVisitor:DcmElementOf<IODImagePixelModule<IODFloatingPointImagePixelModule> >:
// Offers creation and insertion for Pixel Data element as used for
// 32 bit float-based pixel data as used in Parametric Map IOD
// ----------------------------------------------------------------------------

template<>
class DPMParametricMapIOD::WriteVisitor::DcmElementOf<IODFloatingPointImagePixelModule>
{
public:
  DcmElementOf()
  : m_pElement(new DcmFloatingPointSingleAlloc(DCM_FloatPixelData))
  {

  }

  Float32* getData(const size_t size)
  {
    if (m_pElement)
    {
      Float32* result;
      if (m_pElement->createFloat32Array(size, result).good())
        return result;
    }
    return OFnullptr;
  }

  OFCondition put(DcmItem& itemPara)
  {
    OFCondition result = itemPara.insert(m_pElement.get(), OFTrue);
    if (result.good())
      m_pElement.release();
    return result;
  }

private:

  struct DcmFloatingPointSingleAlloc : DcmFloatingPointSingle
  {
    DcmFloatingPointSingleAlloc(const DcmTagKey& tag)
    : DcmFloatingPointSingle(tag)
    {

    }

    OFCondition createFloat32Array(const size_t size,
                                   Float32*& result)
    {
       OFCondition status = createEmptyValue(OFstatic_cast(Uint32, size));
       result = OFreinterpret_cast(Float32*, getValue());
       return status;
    }
  };

  OFunique_ptr<DcmFloatingPointSingleAlloc> m_pElement;
};


// ----------------------------------------------------------------------------
// Class WriteVisitor:DcmElementOf<IODImagePixelModule<IODFloatingPointImagePixelModule> >:
// Offers creation and insertion for Pixel Data element as used for
// 64 bit float-based pixel data as used in Parametric Map IOD.
// ----------------------------------------------------------------------------

template<>
class DPMParametricMapIOD::WriteVisitor::DcmElementOf<IODDoubleFloatingPointImagePixelModule>
{
public:
  DcmElementOf()
  : m_pElement(new DcmFloatingPointDoubleAlloc(DCM_DoubleFloatPixelData))
  {

  }

  Float64* getData(const size_t size)
  {
    if (m_pElement)
    {
      Float64* result;
      if (m_pElement->createFloat64Array(size, result).good())
        return result;
    }
    return OFnullptr;
  }

  OFCondition put(DcmItem& itemPara)
  {
    OFCondition result = itemPara.insert(m_pElement.get(), OFTrue);
    if (result.good())
      m_pElement.release();
    return result;
  }

private:

  struct DcmFloatingPointDoubleAlloc : DcmFloatingPointDouble
  {
    DcmFloatingPointDoubleAlloc(const DcmTagKey& tag)
    : DcmFloatingPointDouble(tag)
    {

    }

    OFCondition createFloat64Array(const size_t size, Float64*& result)
    {
       OFCondition status = createEmptyValue(OFstatic_cast(Uint32, size));
       result = OFreinterpret_cast(Float64*, getValue());
       return status;
    }
  };

  OFunique_ptr<DcmFloatingPointDoubleAlloc> m_pElement;
};


// Helper "class" that returns Frames offering API to the pixel's frame bulk
// data by offering the dedicated data type, e.g. Float32 instead of the
// internally stored generic Uint8 array.
//
struct DPMParametricMapIOD::GetFramesVisitor
{
  GetFramesVisitor(DPMParametricMapIOD& m)
  : map(m)
  {

  }

  OFCondition operator()(OFmonostate)
  {
    return DPM_InvalidPixelData;
  }

  template<typename ImagePixel>
  Frames<OFTypename ImagePixel::value_type> operator()(ImagePixel&)
  {
    return Frames<OFTypename ImagePixel::value_type>(map);
  }

  DPMParametricMapIOD& map;
};


/** Helper "class" that returns Frames offering API to the pixel's frame bulk
 *  data by offering the dedicated data type, e.g. Float32 instead of the
 *  internally stored generic Uint8 array.
 */
struct DPMParametricMapIOD::ReadFramesVisitor
{
  ReadFramesVisitor(DcmItem& i,
                    DPMParametricMapIOD& m)
  : map(m), item(i)
  {
    // Nothing else to do
  }

  OFCondition operator()(OFmonostate)
  {
    return DPM_InvalidPixelData;
  }

  template<typename ImagePixel>
  OFCondition operator()(ImagePixel&)
  {
    return Frames<OFTypename ImagePixel::value_type>(map);
  }

  // Members, i.e. parameters to operator()
  DPMParametricMapIOD& map;
  DcmItem& item;
};



template<typename PixelType>
DPMParametricMapIOD::Frames<PixelType>::Frames(DPMParametricMapIOD& map)
: m_Map(map)
{

}


template<typename PixelType>
OFCondition DPMParametricMapIOD::Frames<PixelType>::addFrame(PixelType* data,
                                                             const size_t numPixels,
                                                             const OFVector<FGBase*>& perFrameInformation)
{
  OFCondition result;
  if (data && numPixels)
  {
    if (!perFrameInformation.empty())
    {
      OFunique_ptr<DcmIODTypes::Frame> f(new DcmIODTypes::Frame);
      if (f)
      {
        f->length = numPixels * sizeof(PixelType);
        f->pixData = new Uint8[f->length];
        memcpy(f->pixData, data, f->length);
        m_Map.m_Frames.push_back(f.release());
        OFVector<FGBase*>::const_iterator fg = perFrameInformation.begin();
        while ( result.good() && (fg != perFrameInformation.end()) )
        {
          result = m_Map.m_FGInterface.addPerFrame(OFstatic_cast(Uint32, m_Map.m_Frames.size()), **fg);
          if (result.bad())
          {
            DCMPMAP_ERROR("Could not add functional group: " << DcmFGTypes::FGType2OFString((*fg)->getType()));
            break;
          }
          ++fg;
        }
      }
    }
  }
  // Clean up if necessary
  if (result.bad())
    m_Map.m_FGInterface.deleteFrame(OFstatic_cast(Uint32, m_Map.m_Frames.size()));
  return result;
}


template<typename PixelType>
PixelType* DPMParametricMapIOD::Frames<PixelType>::getFrame(const size_t frameNumber)
{
  if (frameNumber < m_Map.m_Frames.size())
  {
    return (PixelType*)(m_Map.m_Frames[frameNumber]->pixData);
  }
  return NULL;
}


// Protected constructor
DPMParametricMapIOD::DPMParametricMapIOD()
: DPMParametricMapBase()
, m_ContentIdentification()
, m_Frames()
{
}


// Protected constructor
template<typename ImagePixel>
DPMParametricMapIOD::DPMParametricMapIOD(OFin_place_type_t(ImagePixel))
: DPMParametricMapBase(OFin_place<ImagePixel>)
, m_ContentIdentification()
, m_Frames()
{
}


// Static method used to create a new Parametric Map IOD object
template<typename ImagePixel>
OFvariant<OFCondition,DPMParametricMapIOD> DPMParametricMapIOD::create(const OFString& modality,
                                                                       const OFString& seriesNumber,
                                                                       const OFString& instanceNumber,
                                                                       const Uint16 rows,
                                                                       const Uint16 columns,
                                                                       const IODEnhGeneralEquipmentModule::EquipmentInfo& equipmentInfo,
                                                                       const ContentIdentificationMacro& contentIdentification,
                                                                       const OFString& imageFlavor,
                                                                       const OFString& derivedPixelContrast,
                                                                       const DPMTypes::ContentQualification& contentQualification)
{
  DPMParametricMapIOD map(OFin_place<ImagePixel>);

  OFCondition status = SetImagePixelModuleVisitor(rows, columns)(*OFget<ImagePixel>(&map.getImagePixel()));
  if (status.bad())
    return status;

  status = map.init
  (
    modality,
    seriesNumber,
    instanceNumber,
    equipmentInfo,
    contentIdentification,
    imageFlavor,
    derivedPixelContrast,
    contentQualification
  );

  if(status.bad())
    return status;

  return map;
}


OFCondition DPMParametricMapIOD::init(const OFString& modality,
                                      const OFString& seriesNumber,
                                      const OFString& instanceNumber,
                                      const IODEnhGeneralEquipmentModule::EquipmentInfo& equipmentInfo,
                                      const ContentIdentificationMacro& contentIdentification,
                                      const OFString& imageFlavor,
                                      const OFString& derivedPixelContrast,
                                      const DPMTypes::ContentQualification& contentQualification)
{
  if (!equipmentInfo.isDataComplete())
  {
    DCMPMAP_ERROR("Equipment info incomplete");
    return DPM_InvalidAttributeValue;
  }

  OFCondition status = OFconst_cast(ContentIdentificationMacro*, &contentIdentification)->check();
  if (status.bad())
  {
    DCMPMAP_ERROR("Content identification is invalid: " << status.text());
    return DPM_InvalidAttributeValue;
  }

  status = OFconst_cast(ContentIdentificationMacro*, &contentIdentification)->check();
  if (imageFlavor.empty() || derivedPixelContrast.empty())
  {
    DCMPMAP_ERROR
    (
      "Image Type value 3 (Image Flavor) and 4 (Derived Pixel Contrast) must be set but are "
      << imageFlavor << "/" << derivedPixelContrast
    );
    return DPM_InvalidAttributeValue;
  }

  if (!DPMTypes::cqValid(contentQualification))
  {
    DCMPMAP_ERROR("Content Qualification not set");
    return DPM_InvalidAttributeValue;
  }

  // All data is valid, copy to object
  status = getIODEnhGeneralEquipmentModule().set(equipmentInfo);
  if (status.bad())
  {
    DCMPMAP_ERROR("Error setting equipment info: " << status.text());
    return DPM_InvalidAttributeValue;
  }

  m_ContentIdentification = contentIdentification;
  OFStringStream ss;
  ss << "DERIVED\\PRIMARY\\" << imageFlavor << "\\" << derivedPixelContrast;
  OFSTRINGSTREAM_GETOFSTRING(ss, imgType);

  status = getIODGeneralImageModule().setImageType(imgType);
  if (status.bad())
  {
    DCMPMAP_ERROR("Error setting Image Type: " << status.text());
    return DPM_InvalidAttributeValue;
  }

  status = m_DPMParametricMapSeriesModule.setModality(modality);
  if (status.bad())
  {

    DCMPMAP_ERROR("Could not set modality value \"" << modality << "\": " << status.text());
    return DPM_InvalidAttributeValue;
  }

  status = m_DPMParametricMapSeriesModule.setSeriesNumber(seriesNumber);
  if (status.bad())
  {
    DCMPMAP_ERROR("Could not set Series Number: " << status.text());
    return DPM_InvalidAttributeValue;
  }

  status = m_IODMultiFrameFGModule.setInstanceNumber(instanceNumber);
  if (status.bad())
    return status;

  m_DPMParametricMapImageModule.setContentQualification(DPMTypes::cq2Str(contentQualification));
  m_DPMParametricMapImageModule.setRecognizableVisualFeatures("YES");
  m_DPMParametricMapImageModule.setBurnedInAnnotation("NO");
  m_DPMParametricMapImageModule.setLossyImageCompression("00");
  m_DPMParametricMapImageModule.setPresentationLUTShape("IDENTITY");
  getSOPCommon().setSOPClassUID(UID_ParametricMapStorage);
  DcmIODUtil::setContentDateAndTimeNow(m_IODMultiFrameFGModule);

  return EC_Normal;
}


OFCondition DPMParametricMapIOD::read(DcmItem& dataset)
{
  return OFvisit<OFCondition>(ReadVisitor(dataset,*this),getImagePixel());

}


OFCondition DPMParametricMapIOD::write(DcmItem& dataset)
{
  if (!check())
    return DPM_InvalidAttributeValue;
  return OFvisit<OFCondition>(WriteVisitor(dataset,*this),getImagePixel());
}


OFvariant<OFCondition,DPMParametricMapIOD*> DPMParametricMapIOD::loadDataset(DcmDataset& dataset)
{
  OFString sopClass;
  DPMParametricMapIOD *map = NULL;
  if (dataset.findAndGetOFStringArray(DCM_SOPClassUID, sopClass).good())
  {
    if (sopClass == UID_ParametricMapStorage)
    {
      DcmElement *elem = NULL;
      if (dataset.findAndGetElement(DCM_PixelData, elem).good())
      {
        Uint16 pr = 0;
        if (dataset.findAndGetUint16(DCM_PixelRepresentation, pr).good())
        {
          if (pr == 0)
          {
            map = new DPMParametricMapIOD(OFin_place<IODImagePixelModule<Uint16> >);
          }
          else
          {
            map = new DPMParametricMapIOD(OFin_place<IODImagePixelModule<Sint16> >);
          }
        }
        else
        {
          DCMPMAP_WARN("Pixel Data element found but no Pixel Representation set, assuming 16 bit unsigned integer data");
          map = new DPMParametricMapIOD(OFin_place<IODImagePixelModule<Uint16> >);
        }
      }
      else if (dataset.findAndGetElement(DCM_FloatPixelData, elem).good())
      {
        map = new DPMParametricMapIOD(OFin_place<IODFloatingPointImagePixelModule>);
      }
      else if (dataset.findAndGetElement(DCM_DoubleFloatPixelData, elem).good())
      {
        map = new DPMParametricMapIOD(OFin_place<IODDoubleFloatingPointImagePixelModule>);
      }
      else
      {
        return DPM_NoPixelData;
      }
      if (map == NULL)
      {
        return EC_MemoryExhausted;
      }
    }
    else
    {
      DCMPMAP_ERROR("Invalid SOP Class: " << sopClass << ", only Parametric Map Storage (1.2.840.10008.5.1.4.1.1.30) supported");
      return DPM_InvalidSOPClass;
    }
  }
  OFCondition result = OFvisit<OFCondition>(ReadVisitor(dataset, *map), map->getImagePixel());
  if (result.good())
    return map;
  else
    return result;
}


OFvariant<OFCondition,DPMParametricMapIOD*> DPMParametricMapIOD::loadFile(const OFString& filename)
{
  DcmFileFormat dcmff;
  OFCondition cond = dcmff.loadFile(filename.c_str());
  OFvariant<OFCondition,DPMParametricMapIOD*> result;
  if (cond.good())
  {
     result = loadDataset( *(dcmff.getDataset()) );
     return result;
  }
  else
  {
    return cond;
  }
}


DPMParametricMapIOD::~DPMParametricMapIOD()
{
  DcmIODUtil::freeContainer(m_Frames);
  // nothing else to do for now
}


OFCondition DPMParametricMapIOD::addForAllFrames(const FGBase& group)
{
  return m_FGInterface.addShared(group);
}


OFBool DPMParametricMapIOD::check()
{
  OFString val;
  m_DPMParametricMapImageModule.getRecognizableVisualFeatures(val);
  if (DPMTypes::rvfValid(DPMTypes::str2Rvf(val)))
  {
    m_DPMParametricMapImageModule.getContentQualification(val);
    if (DPMTypes::cqValid(DPMTypes::str2Cq(val)))
    {
      if (!m_Frames.empty())
      {
        return OFTrue;
      }
      else
      {
        DCMPMAP_ERROR("No frame data defined in Parametric Map");
      }
    }
    else
    {
      DCMPMAP_ERROR("Invalid value for Recognizable Visual Features: " << val);
    }
  }
  else
  {
    DCMPMAP_ERROR("Invalid value for Content Qualification" << val);
  }
  return OFFalse;
}


OFCondition DPMParametricMapIOD::getColumns(Uint16& cols)
{
  return getImagePixel().getColumns(cols);
}


OFCondition DPMParametricMapIOD::getRows(Uint16& rows)
{
  return getImagePixel().getRows(rows);
}


size_t DPMParametricMapIOD::getNumberOfFrames() const
{
  return OFconst_cast(DPMParametricMapIOD*,this)->m_FGInterface.getNumberOfFrames();
}


OFCondition DPMParametricMapIOD::setRecognizableVisualFeatures(const DPMTypes::RecognizableVisibleFeatures yesOrNo)
{
  return DPMTypes::rvfValid(yesOrNo)
  ?
    m_DPMParametricMapImageModule.setRecognizableVisualFeatures(DPMTypes::rvf2Str(yesOrNo))
  :
    DPM_InvalidAttributeValue
  ;
}


OFCondition DPMParametricMapIOD::readGeneric(DcmItem& dataset)
{
  OFCondition result = DPMParametricMapBase::read(dataset);
  if (result.good())
    result = m_ContentIdentification.read(dataset);
  return result;
}


OFCondition DPMParametricMapIOD::writeGeneric(DcmItem& dataset)
{
  getFrameOfReference().ensureFrameOfReferenceUID();
  m_IODMultiFrameFGModule.setNumberOfFrames(DcmIODUtil::limitMaxFrames(m_Frames.size(), "Maximum number of frames exceeded, will write 2147483647"));
  OFCondition result = m_ContentIdentification.write(dataset);
  if (result.good())
  {
    result = DPMParametricMapBase::write(dataset);
  }
  return result;
}


DPMTypes::ContentQualification DPMParametricMapIOD::getContentQualification()
{
  OFString val;
  m_DPMParametricMapImageModule.getContentQualification(val);
  return DPMTypes::str2Cq(val);
}


DPMTypes::RecognizableVisibleFeatures DPMParametricMapIOD::getRecognizableVisualFeatures()
{
  OFString val;
  DPMParametricMapIOD::m_DPMParametricMapImageModule.getRecognizableVisualFeatures(val);
  return DPMTypes::str2Rvf(val);
}


DPMParametricMapIOD::FramesType DPMParametricMapIOD::getFrames()
{
  return OFvisit<FramesType>(GetFramesVisitor(*this),getImagePixel());
}


OFCondition DPMParametricMapIOD::decompress(DcmDataset& dset)
{
  DcmXfer xfer = dset.getOriginalXfer();
  OFCondition result;
  // If the original transfer syntax could have been lossy, print warning
  if (dset.hasRepresentation(EXS_LittleEndianExplicit, NULL))
  {
    if (xfer.isPixelDataCompressed() && (xfer != EXS_RLELossless))
    {
      DCMPMAP_WARN("Dataset has been compressed using a (possibly) lossy compression scheme (ignored)");
    }
  }
  // If the original transfer syntax refers to compressed pixel data and we do not
  // already have an uncompressed version, decompress or reject the file
  else if (xfer.isPixelDataCompressed())
  {
    // RLE compression is fine (truly lossless)
    if (xfer == EXS_RLELossless)
    {
      DCMPMAP_DEBUG("DICOM file is RLE-compressed, converting to uncompressed transfer syntax first");
      result = DcmIODUtil::decompress(dset);
    }
    else // We do not accept any transfer syntax that could be lossy compressed
    {
      DCMPMAP_ERROR("Transfer syntax " << xfer.getXferName() << " uses lossy compression, not supported for Parametric Map objects!");
      result = IOD_EC_CannotDecompress;
    }
  }
  return result;
}

// Explicit instantiations using all permitted Parametric Map data types
template class DPMParametricMapIOD::Frames<Uint16>;
template class DPMParametricMapIOD::Frames<Sint16>;
template class DPMParametricMapIOD::Frames<Float32>;
template class DPMParametricMapIOD::Frames<Float64>;

#ifdef __SUNPRO_CC
// the SunPro compiler would complain that the DCMTK_DCMPMAP_EXPORT attribute cannot be applied
// to an explicit template instantiation.

// Helper macro to not to write the same thing multiple times
#define INSTANTIATE_CREATE(T) template OFvariant<OFCondition,DPMParametricMapIOD>\
DPMParametricMapIOD::create<T >(const OFString& modality,\
                                const OFString& seriesNumber,\
                                const OFString& instanceNumber,\
                                const Uint16 rows,\
                                const Uint16 columns,\
                                const IODEnhGeneralEquipmentModule::EquipmentInfo& equipmentInfo,\
                                const ContentIdentificationMacro& contentIdentification,\
                                const OFString& imageFlavor,\
                                const OFString& derivedPixelContrast,\
                                const DPMTypes::ContentQualification& contentQualification)

#else

// Helper macro to not to write the same thing multiple times
#define INSTANTIATE_CREATE(T) template DCMTK_DCMPMAP_EXPORT OFvariant<OFCondition,DPMParametricMapIOD>\
DPMParametricMapIOD::create<T >(const OFString& modality,\
                                const OFString& seriesNumber,\
                                const OFString& instanceNumber,\
                                const Uint16 rows,\
                                const Uint16 columns,\
                                const IODEnhGeneralEquipmentModule::EquipmentInfo& equipmentInfo,\
                                const ContentIdentificationMacro& contentIdentification,\
                                const OFString& imageFlavor,\
                                const OFString& derivedPixelContrast,\
                                const DPMTypes::ContentQualification& contentQualification)

#endif

// Instantiate all four permitted create() methods
INSTANTIATE_CREATE(IODImagePixelModule<Uint16>);
INSTANTIATE_CREATE(IODImagePixelModule<Sint16>);
INSTANTIATE_CREATE(IODFloatingPointImagePixelModule);
INSTANTIATE_CREATE(IODDoubleFloatingPointImagePixelModule);