| 1 |
/*========================================================================= |
| 2 |
* |
| 3 |
* Copyright NumFOCUS |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*=========================================================================*/ |
| 18 |
#ifndef itkDCMTKImageIO_h |
| 19 |
#define itkDCMTKImageIO_h |
| 20 |
#include "ITKIODCMTKExport.h" |
| 21 |
|
| 22 |
|
| 23 |
#include <fstream> |
| 24 |
#include <cstdio> |
| 25 |
#include "itkImageIOBase.h" |
| 26 |
|
| 27 |
class DicomImage; |
| 28 |
|
| 29 |
namespace itk |
| 30 |
{ |
| 31 |
/** \class DCMTKImageIOEnums |
| 32 |
* \brief Enums used by the DCMTKImageIO class |
| 33 |
* \ingroup IOFilters |
| 34 |
* \ingroup ITKIODCMTK |
| 35 |
*/ |
| 36 |
class DCMTKImageIOEnums |
| 37 |
{ |
| 38 |
public: |
| 39 |
/** |
| 40 |
* \class LogLevel |
| 41 |
* \ingroup IOFilters |
| 42 |
* \ingroup ITKIODCMTK |
| 43 |
* enum for DCMTK log level. These are defined here without |
| 44 |
* reference to DCMTK library enumerations, to avoid including |
| 45 |
* dcmtk headers in this header. |
| 46 |
*/ |
| 47 |
enum class LogLevel : uint8_t |
| 48 |
{ |
| 49 |
TRACE_LOG_LEVEL = 0, |
| 50 |
DEBUG_LOG_LEVEL, |
| 51 |
INFO_LOG_LEVEL, |
| 52 |
WARN_LOG_LEVEL, |
| 53 |
ERROR_LOG_LEVEL, |
| 54 |
FATAL_LOG_LEVEL, |
| 55 |
OFF_LOG_LEVEL, |
| 56 |
}; |
| 57 |
}; |
| 58 |
// Define how to print enumeration |
| 59 |
extern ITKIODCMTK_EXPORT std::ostream & |
| 60 |
operator<<(std::ostream & out, const DCMTKImageIOEnums::LogLevel value); |
| 61 |
|
| 62 |
/** |
| 63 |
* \class DCMTKImageIO |
| 64 |
* |
| 65 |
* \brief Read DICOM image file format. |
| 66 |
* |
| 67 |
* \ingroup IOFilters |
| 68 |
* |
| 69 |
* \ingroup ITKIODCMTK |
| 70 |
*/ |
| 71 |
class ITKIODCMTK_EXPORT DCMTKImageIO : public ImageIOBase |
| 72 |
{ |
| 73 |
public: |
| 74 |
ITK_DISALLOW_COPY_AND_MOVE(DCMTKImageIO); |
| 75 |
|
| 76 |
/** Standard class type aliases. */ |
| 77 |
using Self = DCMTKImageIO; |
| 78 |
using Superclass = ImageIOBase; |
| 79 |
using Pointer = SmartPointer<Self>; |
| 80 |
|
| 81 |
/** Method for creation through the object factory. */ |
| 82 |
itkNewMacro(Self); |
| 83 |
|
| 84 |
/** \see LightObject::GetNameOfClass() */ |
| 85 |
itkOverrideGetNameOfClassMacro(DCMTKImageIO); |
| 86 |
|
| 87 |
using LogLevelEnum = DCMTKImageIOEnums::LogLevel; |
| 88 |
#if !defined(ITK_LEGACY_REMOVE) |
| 89 |
/**Exposes enums values for backwards compatibility*/ |
| 90 |
static constexpr LogLevelEnum TRACE_LOG_LEVEL = LogLevelEnum::TRACE_LOG_LEVEL; |
| 91 |
static constexpr LogLevelEnum DEBUG_LOG_LEVEL = LogLevelEnum::DEBUG_LOG_LEVEL; |
| 92 |
static constexpr LogLevelEnum INFO_LOG_LEVEL = LogLevelEnum::INFO_LOG_LEVEL; |
| 93 |
static constexpr LogLevelEnum WARN_LOG_LEVEL = LogLevelEnum::WARN_LOG_LEVEL; |
| 94 |
static constexpr LogLevelEnum ERROR_LOG_LEVEL = LogLevelEnum::ERROR_LOG_LEVEL; |
| 95 |
static constexpr LogLevelEnum FATAL_LOG_LEVEL = LogLevelEnum::FATAL_LOG_LEVEL; |
| 96 |
static constexpr LogLevelEnum OFF_LOG_LEVEL = LogLevelEnum::OFF_LOG_LEVEL; |
| 97 |
#endif |
| 98 |
|
| 99 |
/** */ |
| 100 |
void |
| 101 |
SetDicomImagePointer(DicomImage * UserProvided) |
| 102 |
{ |
| 103 |
m_DImage = UserProvided; |
| 104 |
m_DicomImageSetByUser = true; |
| 105 |
} |
| 106 |
|
| 107 |
/*-------- This part of the interfaces deals with reading data. ----- */ |
| 108 |
|
| 109 |
/** Determine the file type. Returns true if this ImageIO can read the |
| 110 |
* file specified. */ |
| 111 |
bool |
| 112 |
CanReadFile(const char *) override; |
| 113 |
|
| 114 |
/** Set the spacing and dimension information for the set filename. */ |
| 115 |
void |
| 116 |
ReadImageInformation() override; |
| 117 |
|
| 118 |
/** Reads the data from disk into the memory buffer provided. */ |
| 119 |
void |
| 120 |
Read(void * buffer) override; |
| 121 |
|
| 122 |
/*-------- This part of the interfaces deals with writing data. ----- */ |
| 123 |
|
| 124 |
/** Determine the file type. Returns true if this ImageIO can write the |
| 125 |
* file specified. */ |
| 126 |
bool |
| 127 |
CanWriteFile(const char *) override; |
| 128 |
|
| 129 |
/** Set the spacing and dimension information for the set filename. */ |
| 130 |
void |
| 131 |
WriteImageInformation() override; |
| 132 |
|
| 133 |
/** Writes the data to disk from the memory buffer provided. Make sure |
| 134 |
* that the IORegions has been set properly. */ |
| 135 |
void |
| 136 |
Write(const void * buffer) override; |
| 137 |
|
| 138 |
/** Set the DCMTK Message Logging Level */ |
| 139 |
void |
| 140 |
SetLogLevel(LogLevelEnum level); |
| 141 |
/** Get the DCMTK Message Logging Level */ |
| 142 |
LogLevelEnum |
| 143 |
GetLogLevel() const; |
| 144 |
|
| 145 |
DCMTKImageIO(); |
| 146 |
~DCMTKImageIO() override; |
| 147 |
void |
| 148 |
PrintSelf(std::ostream & os, Indent indent) const override; |
| 149 |
|
| 150 |
private: |
| 151 |
void |
| 152 |
OpenDicomImage(); |
| 153 |
|
| 154 |
/** Finds the correct type to call the templated function ReorderRGBValues(...) */ |
| 155 |
void |
| 156 |
ReorderRGBValues(void * buffer, const void * data, size_t count, unsigned int voxel_size); |
| 157 |
/** Reorders RGB values in an image from color-by-plane (R1R2R3...G1G2G3...B1B2B3...) |
| 158 |
* to color-by-pixel (R1G1B1...R2G2B2...R3G3B3...). `voxel_size` specifies the pixel size: It should |
| 159 |
* be 3 for RGB images, and 4 for RGBA images. |
| 160 |
* The code in this function is based on code available in DCMTK dcmimage/include/dcmtk/dcmimage/dicoopxt.h |
| 161 |
* in the Convert(...) function.*/ |
| 162 |
template <typename T> |
| 163 |
void |
| 164 |
ReorderRGBValues(void * buffer, const void * data, size_t count, unsigned int voxel_size) |
| 165 |
{ |
| 166 |
auto * output_buffer = static_cast<T *>(buffer); |
| 167 |
const auto ** input_buffer = static_cast<const T **>(const_cast<void *>(data)); |
| 168 |
for (size_t pos = 0; pos < count; ++pos) |
| 169 |
{ |
| 170 |
for (unsigned int color = 0; color < voxel_size; ++color) |
| 171 |
{ |
| 172 |
*(output_buffer++) = input_buffer[color][pos]; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/*----- internal helpers --------------------------------------------*/ |
| 178 |
bool m_UseJPEGCodec{}; |
| 179 |
bool m_UseJPLSCodec{}; |
| 180 |
bool m_UseRLECodec{}; |
| 181 |
|
| 182 |
DicomImage * m_DImage{}; |
| 183 |
|
| 184 |
bool m_DicomImageSetByUser{}; |
| 185 |
|
| 186 |
double m_RescaleSlope{}; |
| 187 |
double m_RescaleIntercept{}; |
| 188 |
std::string m_LastFileName{}; |
| 189 |
}; |
| 190 |
|
| 191 |
// Define how to print enumeration |
| 192 |
extern ITKIODCMTK_EXPORT std::ostream & |
| 193 |
operator<<(std::ostream & out, DCMTKImageIO::LogLevelEnum value); |
| 194 |
|
| 195 |
} // end namespace itk |
| 196 |
|
| 197 |
#endif // itkDCMTKImageIO_h |