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
|
/************************************************************************
*
* Copyright (C) 2017-2022 IRCAD France
* Copyright (C) 2017 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#pragma once
#include "io/dicom/config.hpp"
#include "io/dicom/helper/Encoding.hpp"
#include <core/log/Logger.hpp>
#include <gdcmAttribute.h>
#include <gdcmDataSet.h>
namespace sight::io::dicom
{
namespace helper
{
/**
* @brief This class contains helpers to handle GDCM data reading.
*/
class IO_DICOM_CLASS_API DicomDataReader
{
public:
/**
* @brief Return a string from a tag found in \p dataset.
* An empty string returned means the tag is not found or empty.
* The value is returned without binary space padding.
* @tparam GROUP Group of the tag
* @tparam ELEMENT Element of the tag
* @param[in] dataset Data set containing the tag
* @param[in] charset Specific Character Set (if empty assuming ASCII).
* @param[in] logger Logger used for encoding issue
* @note charset shall be set if SpecificCharacterSet (0008,0005) is
* defined and tag's VR is SH, LO, ST, PN, LT or UT.
* @return The tag value as string
*/
template<std::uint16_t GROUP, std::uint16_t ELEMENT>
static std::string getTagValue(
const gdcm::DataSet& dataset,
const std::string& charset = "",
const core::log::Logger::sptr& logger = nullptr
)
{
std::string result = "";
const gdcm::Tag tag(GROUP, ELEMENT);
if(dataset.FindDataElement(tag))
{
const gdcm::DataElement& dataElement = dataset.GetDataElement(tag);
if(!dataElement.IsEmpty()) // Can be type 2
{
// Retrieve buffer
const gdcm::ByteValue* bv = dataElement.GetByteValue();
if(bv)
{
std::string buffer(bv->GetPointer(), bv->GetLength());
// Trim buffer
const std::string trimmedBuffer = gdcm::LOComp::Trim(buffer.c_str());
try
{
result = io::dicom::helper::Encoding::convertString(trimmedBuffer, charset, logger);
}
catch(const std::runtime_error& e)
{
if(logger)
{
std::stringstream ss;
ss << "Could not read tag " << tag << " : " << e.what();
logger->warning(ss.str());
}
result = trimmedBuffer;
}
}
}
}
return result;
}
/**
* @brief Return an utf-8 tag value from the tag value's buffer
* @tparam GROUP Group of the tag
* @tparam ELEMENT Element of the tag
* @param[in] buffer Tag value's buffer
* @param[in] charset Specific Character Set (if empty assuming ASCII).
* @param[in] logger Logger used for encoding issue
* @note charset shall be set if SpecificCharacterSet (0008,0005) is
* defined and tag's VR is SH, LO, ST, PN, LT or UT.
* @return The tag value as string
*/
template<std::uint16_t GROUP, std::uint16_t ELEMENT>
static std::string getTagValue(
const std::string& buffer,
const std::string& charset = "",
const core::log::Logger::sptr& logger = 0
)
{
std::string result = "";
const gdcm::Tag tag = gdcm::Attribute<GROUP, ELEMENT>::GetTag();
// Trim buffer
const std::string trimmedBuffer = gdcm::LOComp::Trim(buffer.c_str());
try
{
result = io::dicom::helper::Encoding::convertString(trimmedBuffer, charset, logger);
}
catch(const std::runtime_error& e)
{
if(logger)
{
std::stringstream ss;
ss << "Could not read tag " << tag << " : " << e.what();
logger->warning(ss.str());
}
result = trimmedBuffer;
}
return result;
}
/**
* @brief Return a value from a tag found in dataset.
* @tparam GROUP Group of the tag.
* @tparam ELEMENT Element of the tag.
* @tparam T Type of value.
* @param[in] dataset Data set of tags.
* @return The tag value.
*/
template<std::uint16_t GROUP, std::uint16_t ELEMENT, typename T>
static const T getTagValue(const gdcm::DataSet& dataset)
{
gdcm::Attribute<GROUP, ELEMENT> attribute;
attribute.SetFromDataSet(dataset);
return attribute.GetValue();
}
};
} // namespace helper
} // namespace sight::io::dicom
|