fw4spl
DicomDataWriter.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWGDCMIO_HELPER_DICOMDATAWRITER_HXX__
8 #define __FWGDCMIO_HELPER_DICOMDATAWRITER_HXX__
9 
10 #include "fwGdcmIO/config.hpp"
11 #include "fwGdcmIO/container/DicomCodedAttribute.hpp"
12 
13 #include <gdcmAttribute.h>
14 #include <gdcmDataSet.h>
15 #include <gdcmElement.h>
16 #include <gdcmSequenceOfItems.h>
17 
18 namespace fwGdcmIO
19 {
20 
21 namespace helper
22 {
23 
27 class FWGDCMIO_CLASS_API DicomDataWriter
28 {
29 public:
30 
37  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
38  static void setEmptyTagValue(::gdcm::DataSet& dataset)
39  {
40  ::gdcm::Attribute< GROUP, ELEMENT > attribute;
41  dataset.Insert(attribute.GetAsDataElement());
42  }
43 
52  template< typename T, std::uint16_t GROUP, std::uint16_t ELEMENT >
53  static void setTagValue(const T& value, ::gdcm::DataSet& dataset)
54  {
55  ::gdcm::Attribute< GROUP, ELEMENT > attribute;
56  attribute.SetValue(value);
57  dataset.Insert(attribute.GetAsDataElement());
58  }
59 
67  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
68  static void setTagValue(const std::string& value, ::gdcm::DataSet& dataset)
69  {
70  setTagValue<std::string, GROUP, ELEMENT>(value, dataset);
71  }
72 
82  template< typename T, std::uint16_t GROUP, std::uint16_t ELEMENT >
83  static void setTagValues(const T* array, const size_t size, ::gdcm::DataSet& dataset)
84  {
85  ::gdcm::Attribute< GROUP, ELEMENT > attribute;
86  if(array)
87  {
88  attribute.SetValues(array, static_cast<unsigned int>(size));
89  }
90  dataset.Insert(attribute.GetAsDataElement());
91  }
92 
100  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
101  static void setSequenceTagValue(::gdcm::SmartPointer< ::gdcm::SequenceOfItems > sequence,
102  ::gdcm::DataSet& dataset)
103  {
104  // Create the sequence of items
105  ::gdcm::DataElement dataElement(::gdcm::Attribute< GROUP, ELEMENT >::GetTag());
106  dataElement.SetVR(::gdcm::VR::SQ);
107  dataElement.SetValue(*sequence);
108  dataElement.SetVL(sequence->GetLength());
109 
110  // Insert the sequence of items
111  dataset.Insert(dataElement);
112  }
113 
120  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
121  static ::gdcm::SmartPointer< ::gdcm::SequenceOfItems > createAndSetSequenceTagValue(::gdcm::DataSet& dataset)
122  {
123  ::gdcm::SmartPointer< ::gdcm::SequenceOfItems > sequence = new ::gdcm::SequenceOfItems();
124  sequence->SetLengthToUndefined();
125  setSequenceTagValue< GROUP, ELEMENT >(sequence, dataset);
126  return sequence;
127  }
128 
138  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
139  static void setAndMergeSequenceTagValue(::gdcm::SmartPointer< ::gdcm::SequenceOfItems > sequence,
140  ::gdcm::DataSet& dataset)
141  {
142  // Set or add the SQ
143  if (!dataset.FindDataElement(::gdcm::Attribute< GROUP, ELEMENT >::GetTag()))
144  {
145  setSequenceTagValue< GROUP, ELEMENT >(sequence, dataset);
146  }
147  else
148  {
149  // Get old SQ
150  const ::gdcm::DataElement& dataElement =
151  dataset.GetDataElement(::gdcm::Attribute< GROUP, ELEMENT >::GetTag());
152  ::gdcm::SmartPointer< ::gdcm::SequenceOfItems > oldSequence = dataElement.GetValueAsSQ();
153 
154  // Add items of the new SQ to the old SQ
155  auto nbItem = sequence->GetNumberOfItems();
156  for (decltype(nbItem)i = 1; i <= nbItem; ++i) // WARN : item start at 1
157  {
158  oldSequence->AddItem(sequence->GetItem(i));
159  }
160  }
161  }
162 
171  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
173  ::gdcm::DataSet& dataset)
174  {
175  auto sequence = createAndSetSequenceTagValue< GROUP, ELEMENT >(dataset);
176 
177  ::gdcm::Item item;
178  item.SetVLToUndefined();
179  ::gdcm::DataSet& itemDataset = item.GetNestedDataSet();
180 
181  // Code Value - Type 1C
182  setTagValue< 0x0008, 0x0100 >(attribute.getCodeValue(), itemDataset);
183 
184  // Coding Scheme Designator - Type 1C
185  setTagValue< 0x0008, 0x0102 >(attribute.getCodingSchemeDesignator(), itemDataset);
186 
187  // Coding Scheme Version - Type 1C
188  if(!attribute.getCodingSchemeVersion().empty())
189  {
190  setTagValue< 0x0008, 0x0103 >(attribute.getCodingSchemeVersion(), itemDataset);
191  }
192 
193  // Code Meaning - Type 1
194  setTagValue< 0x0008, 0x0104>(attribute.getCodeMeaning(), itemDataset);
195 
196  sequence->AddItem(item);
197  }
198 
199 };
200 
201 } // namespace helper
202 } // namespace fwGdcmIO
203 
204 #endif // __FWGDCMIO_HELPER_DICOMDATAWRITER_HXX__
205 
static void setEmptyTagValue(::gdcm::DataSet &dataset)
Insert an empty tag in a data set. Useful for empty type 2 tags.
static void setCodeSequenceTagValue(::fwGdcmIO::container::DicomCodedAttribute attribute,::gdcm::DataSet &dataset)
Set tag value for the code sequence.
static void setAndMergeSequenceTagValue(::gdcm::SmartPointer< ::gdcm::SequenceOfItems > sequence,::gdcm::DataSet &dataset)
Insert a sequence of items with a tag in the specified data set. If the tag already exists...
static void setSequenceTagValue(::gdcm::SmartPointer< ::gdcm::SequenceOfItems > sequence,::gdcm::DataSet &dataset)
Set and insert a sequence of items with a tag in the specified data set.
const std::string getCodingSchemeVersion() const
Get coding scheme version.
const std::string getCodeMeaning() const
Get code meaning.
static::gdcm::SmartPointer< ::gdcm::SequenceOfItems > createAndSetSequenceTagValue(::gdcm::DataSet &dataset)
Create and set a sequence of items with a tag in the specified data set.
static void setTagValues(const T *array, const size_t size,::gdcm::DataSet &dataset)
Insert multiple values of a tag in a data set.
const std::string getCodingSchemeDesignator() const
Get coding scheme designator.
const std::string getCodeValue() const
Get code value.
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
static void setTagValue(const T &value,::gdcm::DataSet &dataset)
Insert the value of a tag in a data set.
This class represents a DICOM basic coded entry. It stores the four mains attributes : Code Value...
This class contains helpers to write information into GDCM datasets.
static void setTagValue(const std::string &value,::gdcm::DataSet &dataset)
Insert the string value of a tag in a data set.