fw4spl
DicomDataReader.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_DICOMDATAREADER_HXX__
8 #define __FWGDCMIO_HELPER_DICOMDATAREADER_HXX__
9 
10 #include "fwGdcmIO/config.hpp"
11 #include "fwGdcmIO/helper/Encoding.hpp"
12 
13 #include <fwLog/Logger.hpp>
14 
15 #include <gdcmAttribute.h>
16 #include <gdcmDataSet.h>
17 
18 namespace fwGdcmIO
19 {
20 
21 namespace helper
22 {
23 
27 class FWGDCMIO_CLASS_API DicomDataReader
28 {
29 public:
30 
44  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
45  static std::string getTagValue(const ::gdcm::DataSet& dataset,
46  const std::string& charset = "",
47  const ::fwLog::Logger::sptr& logger = nullptr)
48  {
49  std::string result = "";
50 
51  const ::gdcm::Tag tag(GROUP, ELEMENT);
52 
53  if (dataset.FindDataElement(tag))
54  {
55  const ::gdcm::DataElement& dataElement = dataset.GetDataElement(tag);
56 
57  if (!dataElement.IsEmpty()) // Can be type 2
58  {
59  // Retrieve buffer
60  const ::gdcm::ByteValue* bv = dataElement.GetByteValue();
61 
62  if(bv)
63  {
64  std::string buffer(bv->GetPointer(), bv->GetLength());
65 
66  // Trim buffer
67  const std::string trimmedBuffer = ::gdcm::LOComp::Trim(buffer.c_str());
68 
69  try
70  {
71  result = ::fwGdcmIO::helper::Encoding::convertString(trimmedBuffer, charset, logger);
72  }
73  catch(const std::runtime_error& e)
74  {
75  if(logger)
76  {
77  std::stringstream ss;
78  ss << "Could not read tag " << tag << " : " << e.what();
79  logger->warning(ss.str());
80  }
81  result = trimmedBuffer;
82  }
83  }
84  }
85  }
86 
87  return result;
88  }
89 
101  template< std::uint16_t GROUP, std::uint16_t ELEMENT >
102  static std::string getTagValue(const std::string& buffer,
103  const std::string& charset = "",
104  const ::fwLog::Logger::sptr& logger = 0)
105  {
106  std::string result = "";
107 
108  const ::gdcm::Tag tag = ::gdcm::Attribute< GROUP, ELEMENT >::GetTag();
109 
110  // Trim buffer
111  const std::string trimmedBuffer = ::gdcm::LOComp::Trim(buffer.c_str());
112 
113  try
114  {
115  result = ::fwGdcmIO::helper::Encoding::convertString(trimmedBuffer, charset, logger);
116  }
117  catch(const std::runtime_error& e)
118  {
119  if(logger)
120  {
121  std::stringstream ss;
122  ss << "Could not read tag " << tag << " : " << e.what();
123  logger->warning(ss.str());
124  }
125  result = trimmedBuffer;
126  }
127 
128  return result;
129  }
130 
139  template< std::uint16_t GROUP, std::uint16_t ELEMENT, typename T >
140  static const T getTagValue(const ::gdcm::DataSet& dataset)
141  {
142  ::gdcm::Attribute< GROUP, ELEMENT > attribute;
143  attribute.SetFromDataSet(dataset);
144  return attribute.GetValue();
145  }
146 
147 };
148 
149 } // namespace helper
150 } // namespace fwGdcmIO
151 
152 #endif // __FWGDCMIO_HELPER_DICOMDATAREADER_HXX__
153 
This class contains helpers to handle GDCM data reading.
static std::string getTagValue(const ::gdcm::DataSet &dataset, const std::string &charset="", const ::fwLog::Logger::sptr &logger=nullptr)
Return a string from a tag found in dataset. An empty string returned means the tag is not found or e...
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
static std::string getTagValue(const std::string &buffer, const std::string &charset="", const ::fwLog::Logger::sptr &logger=0)
Return an utf-8 tag value from the tag value&#39;s buffer.
static const T getTagValue(const ::gdcm::DataSet &dataset)
Return a value from a tag found in dataset.
static FWGDCMIO_API std::string convertString(const std::string &source, const std::string &definedCharsetTerm, const std::shared_ptr< ::fwLog::Logger > &logger=nullptr)
Convert a DICOM string from the specified charset to utf-8.
Definition: Encoding.cpp:170