fw4spl
tags.cpp
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 #include "fwGdcmIO/helper/tags.hpp"
8 
9 #include "fwGdcmIO/exception/InvalidTag.hpp"
10 #include "fwGdcmIO/helper/CsvIO.hpp"
11 
12 #include <fwCore/exceptionmacros.hpp>
13 
14 #include <boost/filesystem/operations.hpp>
15 #include <boost/numeric/conversion/cast.hpp>
16 
17 #include <fstream>
18 
19 namespace fwGdcmIO
20 {
21 
22 namespace helper
23 {
24 
25 //------------------------------------------------------------------------------
26 
27 ::gdcm::Tag getGdcmTag(const std::string& group, const std::string& element)
28 {
29  SLM_ASSERT("Group and element can not be empty", !group.empty() && !element.empty());
30 
31  typedef std::uint16_t DestType;
32  DestType groupDest;
33  DestType elementDest;
34 
35  try
36  {
37  const unsigned long groupL = std::stoul(group, nullptr, 16);
38  const unsigned long elementL = std::stoul(element, nullptr, 16);
39 
40  groupDest = ::boost::numeric_cast< DestType >(groupL);
41  elementDest = ::boost::numeric_cast< DestType >(elementL);
42  }
43  catch(std::out_of_range& e)
44  {
45  FW_RAISE_EXCEPTION(::fwGdcmIO::exception::InvalidTag(
46  "Unable to read DICOM tag from '" + group + "," + element + "' : " + e.what()));
47  }
48  catch(std::invalid_argument& e)
49  {
50  FW_RAISE_EXCEPTION(::fwGdcmIO::exception::InvalidTag(
51  "Unable to read DICOM tag from '" + group + "," + element + "' : " + e.what()));
52  }
53  catch(::boost::bad_numeric_cast& e)
54  {
55  FW_RAISE_EXCEPTION(::fwGdcmIO::exception::InvalidTag(
56  "Unable to read DICOM tag from '" + group + "," + element + "' : " + e.what()));
57  }
58 
59  return ::gdcm::Tag(groupDest, elementDest);
60 }
61 
62 //------------------------------------------------------------------------------
63 
64 PrivateTagVecType loadPrivateTags(const ::boost::filesystem::path& tagsPath)
65 {
66  SLM_ASSERT("File '" + tagsPath.string() + "' must exists",
67  ::boost::filesystem::exists(tagsPath) && ::boost::filesystem::is_regular_file(tagsPath));
68 
69  PrivateTagVecType privateTags;
70  auto csvStream = std::ifstream(tagsPath.string());
71  ::fwGdcmIO::helper::CsvIO reader(csvStream);
72  ::fwGdcmIO::helper::CsvIO::TokenContainerType tag = reader.getLine();
73 
74  while(!tag.empty())
75  {
76  OSLM_WARN_IF("Unxpected token count : " << tag.size() << " (3 expected : group, element, manufacturer)",
77  tag.size() != 3);
78  FW_RAISE_IF("Unable to read private tag file", tag.size() < 2);
79 
80  privateTags.push_back(::fwGdcmIO::helper::getGdcmTag(tag[0], tag[1]));
81  tag = reader.getLine();
82  }
83 
84  return privateTags;
85 }
86 
87 //------------------------------------------------------------------------------
88 
89 } // namespace helper
90 } // namespace fwGdcmIO
91 
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
InvalidTag exception.
Definition: InvalidTag.hpp:22
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
std::vector< std::string > TokenContainerType
Containers to store parsed tokens.
Definition: CsvIO.hpp:42
Read CSV file and returns parsed tokens. The input file is supposed to use comma separator, but another separator can be used when reading file.
Definition: CsvIO.hpp:26
#define OSLM_WARN_IF(message, cond)
Definition: spyLog.hpp:267