| 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 itkDCMTKFileReader_h |
| 19 |
#define itkDCMTKFileReader_h |
| 20 |
|
| 21 |
#include "ITKIODCMTKExport.h" |
| 22 |
#include <stack> |
| 23 |
#include <vector> |
| 24 |
#include "itkByteSwapper.h" |
| 25 |
#include "itkIntTypes.h" |
| 26 |
#include "vnl/vnl_vector.h" |
| 27 |
#include "dcmtk/dcmdata/dcdict.h" // For DcmDataDictionary |
| 28 |
#include "dcmtk/dcmdata/dcdicent.h" |
| 29 |
#include "dcmtk/dcmdata/dcxfer.h" |
| 30 |
#include "dcmtk/dcmdata/dcvrds.h" |
| 31 |
#include "dcmtk/dcmdata/dcstack.h" |
| 32 |
#include "dcmtk/dcmdata/dcdatset.h" |
| 33 |
#include "dcmtk/dcmdata/dcitem.h" |
| 34 |
#include "dcmtk/dcmdata/dcvrobow.h" |
| 35 |
#include "dcmtk/dcmdata/dcsequen.h" |
| 36 |
#include "itkMacro.h" |
| 37 |
#include "itkImageIOBase.h" |
| 38 |
|
| 39 |
class DcmSequenceOfItems; |
| 40 |
class DcmFileFormat; |
| 41 |
class DcmDictEntry; |
| 42 |
|
| 43 |
// Don't print error messages if you're not throwing |
| 44 |
// an exception |
| 45 |
// std::cerr body; |
| 46 |
#define DCMTKExceptionOrErrorReturn(body) \ |
| 47 |
{ \ |
| 48 |
if (throwException) \ |
| 49 |
{ \ |
| 50 |
itkGenericExceptionMacro(body); \ |
| 51 |
} \ |
| 52 |
else \ |
| 53 |
{ \ |
| 54 |
return EXIT_FAILURE; \ |
| 55 |
} \ |
| 56 |
} |
| 57 |
|
| 58 |
namespace itk |
| 59 |
{ |
| 60 |
// Forward reference because of circular dependencies |
| 61 |
class ITK_FORWARD_EXPORT DCMTKSequence; |
| 62 |
|
| 63 |
class ITKIODCMTK_EXPORT DCMTKItem |
| 64 |
{ |
| 65 |
public: |
| 66 |
DCMTKItem() = default; |
| 67 |
|
| 68 |
void |
| 69 |
SetDcmItem(DcmItem * item); |
| 70 |
int |
| 71 |
GetElementSQ(const unsigned short group, |
| 72 |
const unsigned short entry, |
| 73 |
DCMTKSequence & sequence, |
| 74 |
const bool throwException = true) const; |
| 75 |
|
| 76 |
private: |
| 77 |
DcmItem * m_DcmItem{ nullptr }; |
| 78 |
}; |
| 79 |
|
| 80 |
class ITKIODCMTK_EXPORT DCMTKSequence |
| 81 |
{ |
| 82 |
public: |
| 83 |
DCMTKSequence() = default; |
| 84 |
void |
| 85 |
SetDcmSequenceOfItems(DcmSequenceOfItems * seq); |
| 86 |
int |
| 87 |
card() const; |
| 88 |
int |
| 89 |
GetSequence(unsigned long index, DCMTKSequence & target, const bool throwException = true) const; |
| 90 |
int |
| 91 |
GetStack(const unsigned short group, |
| 92 |
const unsigned short element, |
| 93 |
DcmStack & resultStack, |
| 94 |
const bool throwException = true) const; |
| 95 |
int |
| 96 |
GetElementCS(const unsigned short group, |
| 97 |
const unsigned short element, |
| 98 |
std::string & target, |
| 99 |
const bool throwException = true) const; |
| 100 |
|
| 101 |
int |
| 102 |
GetElementOB(const unsigned short group, |
| 103 |
const unsigned short element, |
| 104 |
std::string & target, |
| 105 |
const bool throwException = true) const; |
| 106 |
|
| 107 |
int |
| 108 |
GetElementCSorOB(const unsigned short group, |
| 109 |
const unsigned short element, |
| 110 |
std::string & target, |
| 111 |
const bool throwException = true) const; |
| 112 |
|
| 113 |
template <typename TType> |
| 114 |
int |
| 115 |
GetElementDSorOB(const unsigned short group, |
| 116 |
const unsigned short element, |
| 117 |
TType & target, |
| 118 |
const bool throwException = true) const |
| 119 |
{ |
| 120 |
if (this->GetElementDS<TType>(group, element, 1, &target, false) == EXIT_SUCCESS) |
| 121 |
{ |
| 122 |
return EXIT_SUCCESS; |
| 123 |
} |
| 124 |
std::string val; |
| 125 |
if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS) |
| 126 |
{ |
| 127 |
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex << group << ' ' << std::hex |
| 128 |
<< element << std::dec); |
| 129 |
} |
| 130 |
const char * data = val.c_str(); |
| 131 |
const auto * fptr = reinterpret_cast<const TType *>(data); |
| 132 |
target = *fptr; |
| 133 |
return EXIT_SUCCESS; |
| 134 |
} |
| 135 |
|
| 136 |
template <typename TType> |
| 137 |
int |
| 138 |
GetElementDSorOB(const unsigned short group, |
| 139 |
const unsigned short element, |
| 140 |
int count, |
| 141 |
TType * target, |
| 142 |
const bool throwException = true) const |
| 143 |
{ |
| 144 |
if (this->GetElementDS<TType>(group, element, count, target, false) == EXIT_SUCCESS) |
| 145 |
{ |
| 146 |
return EXIT_SUCCESS; |
| 147 |
} |
| 148 |
std::string val; |
| 149 |
if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS) |
| 150 |
{ |
| 151 |
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex << group << ' ' << std::hex |
| 152 |
<< element << std::dec); |
| 153 |
} |
| 154 |
const char * data = val.c_str(); |
| 155 |
const auto * fptr = reinterpret_cast<const TType *>(data); |
| 156 |
for (int i = 0; i < count; ++i) |
| 157 |
{ |
| 158 |
target[i] = fptr[i]; |
| 159 |
} |
| 160 |
return EXIT_SUCCESS; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
int |
| 165 |
GetElementFD(const unsigned short group, |
| 166 |
const unsigned short element, |
| 167 |
int count, |
| 168 |
double * target, |
| 169 |
const bool throwException = true) const; |
| 170 |
int |
| 171 |
GetElementFD(const unsigned short group, |
| 172 |
const unsigned short element, |
| 173 |
double & target, |
| 174 |
const bool throwException = true) const; |
| 175 |
int |
| 176 |
GetElementDS(const unsigned short group, |
| 177 |
const unsigned short element, |
| 178 |
std::string & target, |
| 179 |
const bool throwException = true) const; |
| 180 |
int |
| 181 |
GetElementTM(const unsigned short group, |
| 182 |
const unsigned short element, |
| 183 |
std::string & target, |
| 184 |
const bool throwException = true) const; |
| 185 |
/** Get an array of data values, as contained in a DICOM |
| 186 |
* DecimalString Item |
| 187 |
*/ |
| 188 |
template <typename TType> |
| 189 |
int |
| 190 |
GetElementDS(const unsigned short group, |
| 191 |
const unsigned short element, |
| 192 |
unsigned short count, |
| 193 |
TType * target, |
| 194 |
const bool throwException = true) const |
| 195 |
{ |
| 196 |
DcmStack resultStack; |
| 197 |
if (this->GetStack(group, element, resultStack, throwException) != EXIT_SUCCESS) |
| 198 |
{ |
| 199 |
return EXIT_FAILURE; |
| 200 |
} |
| 201 |
auto * dsItem = dynamic_cast<DcmDecimalString *>(resultStack.top()); |
| 202 |
if (dsItem == nullptr) |
| 203 |
{ |
| 204 |
DCMTKExceptionOrErrorReturn(<< "Can't get DecimalString Element at tag " << std::hex << group << ' ' << element |
| 205 |
<< std::dec); |
| 206 |
} |
| 207 |
|
| 208 |
OFVector<Float64> doubleVals; |
| 209 |
if (dsItem->getFloat64Vector(doubleVals) != EC_Normal) |
| 210 |
{ |
| 211 |
DCMTKExceptionOrErrorReturn(<< "Cant extract Array from DecimalString " << std::hex << group << ' ' << std::hex |
| 212 |
<< element << std::dec); |
| 213 |
} |
| 214 |
if (doubleVals.size() != count) |
| 215 |
{ |
| 216 |
DCMTKExceptionOrErrorReturn(<< "DecimalString " << std::hex << group << ' ' << std::hex << element << " expected " |
| 217 |
<< count << "items, but found " << doubleVals.size() << std::dec); |
| 218 |
} |
| 219 |
for (unsigned int i = 0; i < count; ++i) |
| 220 |
{ |
| 221 |
target[i] = static_cast<TType>(doubleVals[i]); |
| 222 |
} |
| 223 |
return EXIT_SUCCESS; |
| 224 |
} |
| 225 |
int |
| 226 |
GetElementSQ(const unsigned short group, |
| 227 |
const unsigned short element, |
| 228 |
DCMTKSequence & target, |
| 229 |
const bool throwException = true) const; |
| 230 |
int |
| 231 |
GetElementItem(unsigned short itemIndex, DCMTKItem & target, const bool throwException = true) const; |
| 232 |
|
| 233 |
void |
| 234 |
print(std::ostream & out) const |
| 235 |
{ |
| 236 |
this->m_DcmSequenceOfItems->print(out); |
| 237 |
} |
| 238 |
|
| 239 |
private: |
| 240 |
DcmSequenceOfItems * m_DcmSequenceOfItems{ nullptr }; |
| 241 |
}; |
| 242 |
|
| 243 |
class ITKIODCMTK_EXPORT DCMTKFileReader |
| 244 |
{ |
| 245 |
public: |
| 246 |
using Self = DCMTKFileReader; |
| 247 |
|
| 248 |
DCMTKFileReader() = default; |
| 249 |
~DCMTKFileReader(); |
| 250 |
|
| 251 |
void |
| 252 |
SetFileName(const std::string & fileName); |
| 253 |
|
| 254 |
const std::string & |
| 255 |
GetFileName() const; |
| 256 |
|
| 257 |
void |
| 258 |
LoadFile(); |
| 259 |
|
| 260 |
int |
| 261 |
GetElementLO(const unsigned short group, |
| 262 |
const unsigned short element, |
| 263 |
std::string & target, |
| 264 |
const bool throwException = true) const; |
| 265 |
int |
| 266 |
GetElementLO(const unsigned short group, |
| 267 |
const unsigned short element, |
| 268 |
std::vector<std::string> & target, |
| 269 |
const bool throwException = true) const; |
| 270 |
|
| 271 |
/** Get an array of data values, as contained in a DICOM |
| 272 |
* DecimalString Item |
| 273 |
*/ |
| 274 |
template <typename TType> |
| 275 |
int |
| 276 |
GetElementDS(const unsigned short group, |
| 277 |
const unsigned short element, |
| 278 |
unsigned short count, |
| 279 |
TType * target, |
| 280 |
const bool throwException = true) const |
| 281 |
{ |
| 282 |
DcmTagKey tagkey(group, element); |
| 283 |
DcmElement * el; |
| 284 |
if (this->m_Dataset->findAndGetElement(tagkey, el) != EC_Normal) |
| 285 |
{ |
| 286 |
DCMTKExceptionOrErrorReturn(<< "Cant find tag " << std::hex << group << ' ' << std::hex << element << std::dec); |
| 287 |
} |
| 288 |
auto * dsItem = dynamic_cast<DcmDecimalString *>(el); |
| 289 |
if (dsItem == nullptr) |
| 290 |
{ |
| 291 |
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex << group << ' ' << std::hex |
| 292 |
<< element << std::dec); |
| 293 |
} |
| 294 |
OFVector<Float64> doubleVals; |
| 295 |
if (dsItem->getFloat64Vector(doubleVals) != EC_Normal) |
| 296 |
{ |
| 297 |
DCMTKExceptionOrErrorReturn(<< "Cant extract Array from DecimalString " << std::hex << group << ' ' << std::hex |
| 298 |
<< element << std::dec); |
| 299 |
} |
| 300 |
if (doubleVals.size() != count) |
| 301 |
{ |
| 302 |
DCMTKExceptionOrErrorReturn(<< "DecimalString " << std::hex << group << ' ' << std::hex << element << " expected " |
| 303 |
<< count << "items, but found " << doubleVals.size() << std::dec); |
| 304 |
} |
| 305 |
for (unsigned int i = 0; i < count; ++i) |
| 306 |
{ |
| 307 |
target[i] = static_cast<TType>(doubleVals[i]); |
| 308 |
} |
| 309 |
return EXIT_SUCCESS; |
| 310 |
} |
| 311 |
|
| 312 |
template <typename TType> |
| 313 |
int |
| 314 |
GetElementDSorOB(const unsigned short group, |
| 315 |
const unsigned short element, |
| 316 |
TType & target, |
| 317 |
const bool throwException = true) const |
| 318 |
{ |
| 319 |
if (this->GetElementDS<TType>(group, element, 1, &target, false) == EXIT_SUCCESS) |
| 320 |
{ |
| 321 |
return EXIT_SUCCESS; |
| 322 |
} |
| 323 |
std::string val; |
| 324 |
if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS) |
| 325 |
{ |
| 326 |
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex << group << ' ' << std::hex |
| 327 |
<< element << std::dec); |
| 328 |
} |
| 329 |
const char * data = val.c_str(); |
| 330 |
const auto * fptr = reinterpret_cast<const TType *>(data); |
| 331 |
target = *fptr; |
| 332 |
switch (this->GetTransferSyntax()) |
| 333 |
{ |
| 334 |
case EXS_LittleEndianImplicit: |
| 335 |
case EXS_LittleEndianExplicit: |
| 336 |
itk::ByteSwapper<TType>::SwapFromSystemToLittleEndian(&target); |
| 337 |
break; |
| 338 |
case EXS_BigEndianImplicit: |
| 339 |
case EXS_BigEndianExplicit: |
| 340 |
itk::ByteSwapper<TType>::SwapFromSystemToBigEndian(&target); |
| 341 |
break; |
| 342 |
default: |
| 343 |
break; |
| 344 |
} |
| 345 |
return EXIT_SUCCESS; |
| 346 |
} |
| 347 |
/** Get a DecimalString Item as a single string |
| 348 |
*/ |
| 349 |
int |
| 350 |
GetElementDS(const unsigned short group, |
| 351 |
const unsigned short element, |
| 352 |
std::string & target, |
| 353 |
const bool throwException = true) const; |
| 354 |
int |
| 355 |
GetElementFD(const unsigned short group, |
| 356 |
const unsigned short element, |
| 357 |
double & target, |
| 358 |
const bool throwException = true) const; |
| 359 |
int |
| 360 |
GetElementFD(const unsigned short group, |
| 361 |
const unsigned short element, |
| 362 |
int count, |
| 363 |
double * target, |
| 364 |
const bool throwException = true) const; |
| 365 |
int |
| 366 |
GetElementFL(const unsigned short group, |
| 367 |
const unsigned short element, |
| 368 |
float & target, |
| 369 |
const bool throwException = true) const; |
| 370 |
int |
| 371 |
GetElementFLorOB(const unsigned short group, |
| 372 |
const unsigned short element, |
| 373 |
float & target, |
| 374 |
const bool throwException = true) const; |
| 375 |
|
| 376 |
int |
| 377 |
GetElementUS(const unsigned short group, |
| 378 |
const unsigned short element, |
| 379 |
unsigned short & target, |
| 380 |
const bool throwException = true) const; |
| 381 |
int |
| 382 |
GetElementUS(const unsigned short group, |
| 383 |
const unsigned short element, |
| 384 |
unsigned short *& target, |
| 385 |
const bool throwException = true) const; |
| 386 |
/** Get a DecimalString Item as a single string |
| 387 |
*/ |
| 388 |
int |
| 389 |
GetElementCS(const unsigned short group, |
| 390 |
const unsigned short element, |
| 391 |
std::string & target, |
| 392 |
const bool throwException = true) const; |
| 393 |
|
| 394 |
/** Get a PersonName Item as a single string |
| 395 |
*/ |
| 396 |
int |
| 397 |
GetElementPN(const unsigned short group, |
| 398 |
const unsigned short element, |
| 399 |
std::string & target, |
| 400 |
const bool throwException = true) const; |
| 401 |
|
| 402 |
/** get an IS (Integer String Item |
| 403 |
*/ |
| 404 |
int |
| 405 |
GetElementIS(const unsigned short group, |
| 406 |
const unsigned short element, |
| 407 |
itk::int32_t & target, |
| 408 |
const bool throwException = true) const; |
| 409 |
|
| 410 |
int |
| 411 |
GetElementSL(const unsigned short group, |
| 412 |
const unsigned short element, |
| 413 |
itk::int32_t & target, |
| 414 |
const bool throwException = true) const; |
| 415 |
|
| 416 |
int |
| 417 |
GetElementISorOB(const unsigned short group, |
| 418 |
const unsigned short element, |
| 419 |
itk::int32_t & target, |
| 420 |
const bool throwException = true) const; |
| 421 |
|
| 422 |
int |
| 423 |
GetElementCSorOB(const unsigned short group, |
| 424 |
const unsigned short element, |
| 425 |
std::string & target, |
| 426 |
const bool throwException = true) const; |
| 427 |
|
| 428 |
/** get an OB OtherByte Item |
| 429 |
*/ |
| 430 |
int |
| 431 |
GetElementOB(const unsigned short group, |
| 432 |
const unsigned short element, |
| 433 |
std::string & target, |
| 434 |
const bool throwException = true) const; |
| 435 |
|
| 436 |
int |
| 437 |
GetElementSQ(const unsigned short group, |
| 438 |
unsigned short entry, |
| 439 |
DCMTKSequence & sequence, |
| 440 |
const bool throwException = true) const; |
| 441 |
|
| 442 |
int |
| 443 |
GetElementUI(const unsigned short group, |
| 444 |
unsigned short entry, |
| 445 |
std::string & target, |
| 446 |
const bool throwException = true) const; |
| 447 |
|
| 448 |
int |
| 449 |
GetElementDA(const unsigned short group, |
| 450 |
const unsigned short element, |
| 451 |
std::string & target, |
| 452 |
const bool throwException = true) const; |
| 453 |
|
| 454 |
int |
| 455 |
GetElementTM(const unsigned short group, |
| 456 |
const unsigned short element, |
| 457 |
std::string & target, |
| 458 |
const bool throwException = true) const; |
| 459 |
|
| 460 |
int |
| 461 |
GetDirCosines(vnl_vector<double> & dir1, vnl_vector<double> & dir2, vnl_vector<double> & dir3) const; |
| 462 |
|
| 463 |
int |
| 464 |
GetDirCosArray(double * const dircos) const; |
| 465 |
|
| 466 |
int |
| 467 |
GetFrameCount() const; |
| 468 |
|
| 469 |
int |
| 470 |
GetSlopeIntercept(double & slope, double & intercept) const; |
| 471 |
|
| 472 |
int |
| 473 |
GetDimensions(unsigned short & rows, unsigned short & columns) const; |
| 474 |
|
| 475 |
IOComponentEnum |
| 476 |
GetImageDataType() const; |
| 477 |
IOPixelEnum |
| 478 |
GetImagePixelType() const; |
| 479 |
|
| 480 |
int |
| 481 |
GetSpacing(double * const spacing) const; |
| 482 |
int |
| 483 |
GetOrigin(double * const origin) const; |
| 484 |
|
| 485 |
bool |
| 486 |
HasPixelData() const; |
| 487 |
|
| 488 |
E_TransferSyntax |
| 489 |
GetTransferSyntax() const; |
| 490 |
|
| 491 |
long |
| 492 |
GetFileNumber() const; |
| 493 |
static void |
| 494 |
AddDictEntry(DcmDictEntry * entry); |
| 495 |
|
| 496 |
static bool |
| 497 |
CanReadFile(const std::string & filename); |
| 498 |
static bool |
| 499 |
IsImageFile(const std::string & filename); |
| 500 |
|
| 501 |
private: |
| 502 |
std::string m_FileName; |
| 503 |
DcmFileFormat * m_DFile{ nullptr }; |
| 504 |
DcmDataset * m_Dataset{ nullptr }; |
| 505 |
E_TransferSyntax m_Xfer{ EXS_Unknown }; |
| 506 |
Sint32 m_FrameCount{ 0 }; |
| 507 |
long m_FileNumber{ -1L }; |
| 508 |
}; |
| 509 |
|
| 510 |
extern bool |
| 511 |
CompareDCMTKFileReaders(DCMTKFileReader * a, DCMTKFileReader * b); |
| 512 |
} // namespace itk |
| 513 |
|
| 514 |
#endif // itkDCMTKFileReader_h |