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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkDCMTKFileReader_h
#define itkDCMTKFileReader_h
#include "ITKIODCMTKExport.h"
#include <stack>
#include <vector>
#include "itkByteSwapper.h"
#include "itkIntTypes.h"
#include "vnl/vnl_vector.h"
#include "dcmtk/dcmdata/dcdict.h" // For DcmDataDictionary
#include "dcmtk/dcmdata/dcdicent.h"
#include "dcmtk/dcmdata/dcxfer.h"
#include "dcmtk/dcmdata/dcvrds.h"
#include "dcmtk/dcmdata/dcstack.h"
#include "dcmtk/dcmdata/dcdatset.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcvrobow.h"
#include "dcmtk/dcmdata/dcsequen.h"
#include "itkMacro.h"
#include "itkImageIOBase.h"
class DcmSequenceOfItems;
class DcmFileFormat;
class DcmDictEntry;
// Don't print error messages if you're not throwing
// an exception
// std::cerr body;
#define DCMTKExceptionOrErrorReturn(body) \
{ \
if(throwException) \
{ \
itkGenericExceptionMacro(body); \
} \
else \
{ \
return EXIT_FAILURE; \
} \
}
namespace itk
{
// Forward reference because of circular dependencies
class ITK_FORWARD_EXPORT DCMTKSequence;
class ITKIODCMTK_EXPORT DCMTKItem
{
public:
DCMTKItem() : m_DcmItem(ITK_NULLPTR)
{
}
void SetDcmItem(DcmItem *item);
int GetElementSQ(const unsigned short group,
const unsigned short entry,
DCMTKSequence &sequence,
const bool throwException = true) const;
private:
DcmItem *m_DcmItem;
};
class ITKIODCMTK_EXPORT DCMTKSequence
{
public:
DCMTKSequence() : m_DcmSequenceOfItems(ITK_NULLPTR) {}
void SetDcmSequenceOfItems(DcmSequenceOfItems *seq);
int card() const;
int GetSequence(unsigned long index,
DCMTKSequence &target,const bool throwException = true) const;
int GetStack(const unsigned short group,
const unsigned short element,
DcmStack &resultStack, const bool throwException = true) const;
int GetElementCS(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementOB(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementCSorOB(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
template <typename TType>
int GetElementDSorOB(const unsigned short group,
const unsigned short element,
TType &target,
const bool throwException = true) const
{
if(this->GetElementDS<TType>(group,element,1,&target,false) == EXIT_SUCCESS)
{
return EXIT_SUCCESS;
}
std::string val;
if(this->GetElementOB(group,element,val,throwException) != EXIT_SUCCESS)
{
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
const char *data = val.c_str();
const TType *fptr = reinterpret_cast<const TType *>(data);
target = *fptr;
return EXIT_SUCCESS;
}
template <typename TType>
int GetElementDSorOB(const unsigned short group,
const unsigned short element,
int count,
TType *target,
const bool throwException = true) const
{
if(this->GetElementDS<TType>(group,element,count,target,false) == EXIT_SUCCESS)
{
return EXIT_SUCCESS;
}
std::string val;
if(this->GetElementOB(group,element,val,throwException) != EXIT_SUCCESS)
{
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
const char *data = val.c_str();
const TType *fptr = reinterpret_cast<const TType *>(data);
for(int i = 0; i < count; ++i)
{
target[i] = fptr[i];
}
return EXIT_SUCCESS;
}
int GetElementFD(const unsigned short group,
const unsigned short element,
int count,
double *target,
const bool throwException = true) const;
int GetElementFD(const unsigned short group,
const unsigned short element,
double &target,
const bool throwException = true) const;
int GetElementDS(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementTM(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
/** Get an array of data values, as contained in a DICOM
* DecimalString Item
*/
template <typename TType>
int GetElementDS(const unsigned short group,
const unsigned short element,
unsigned short count,
TType *target,
const bool throwException = true) const
{
DcmStack resultStack;
if(this->GetStack(group,element,resultStack,throwException) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
DcmDecimalString *dsItem =
dynamic_cast<DcmDecimalString *>(resultStack.top());
if(dsItem == ITK_NULLPTR)
{
DCMTKExceptionOrErrorReturn(<< "Can't get DecimalString Element at tag "
<< std::hex << group << " "
<< element << std::dec);
}
OFVector<Float64> doubleVals;
if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
{
DCMTKExceptionOrErrorReturn(<< "Cant extract Array from DecimalString " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
if(doubleVals.size() != count)
{
DCMTKExceptionOrErrorReturn(<< "DecimalString " << std::hex
<< group << " " << std::hex
<< element << " expected "
<< count << "items, but found "
<< doubleVals.size() << std::dec);
}
for(unsigned i = 0; i < count; i++)
{
target[i] = static_cast<TType>(doubleVals[i]);
}
return EXIT_SUCCESS;
}
int GetElementSQ(const unsigned short group,
const unsigned short element,
DCMTKSequence &target,
const bool throwException = true) const;
int GetElementItem(unsigned short itemIndex,
DCMTKItem &target,
const bool throwException = true) const;
void print(std::ostream &out) const
{
this->m_DcmSequenceOfItems->print(out);
}
private:
DcmSequenceOfItems *m_DcmSequenceOfItems;
};
class ITKIODCMTK_EXPORT DCMTKFileReader
{
public:
typedef DCMTKFileReader Self;
DCMTKFileReader() : m_DFile(ITK_NULLPTR),
m_Dataset(ITK_NULLPTR),
m_Xfer(EXS_Unknown),
m_FrameCount(0),
m_FileNumber(-1L)
{}
~DCMTKFileReader();
void SetFileName(const std::string &fileName);
const std::string &GetFileName() const;
void LoadFile();
int GetElementLO(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementLO(const unsigned short group,
const unsigned short element,
std::vector<std::string> &target,
const bool throwException = true) const;
/** Get an array of data values, as contained in a DICOM
* DecimalString Item
*/
template <typename TType>
int GetElementDS(const unsigned short group,
const unsigned short element,
unsigned short count,
TType *target,
const bool throwException = true) const
{
DcmTagKey tagkey(group,element);
DcmElement *el;
if(this->m_Dataset->findAndGetElement(tagkey,el) != EC_Normal)
{
DCMTKExceptionOrErrorReturn(<< "Cant find tag " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
DcmDecimalString *dsItem = dynamic_cast<DcmDecimalString *>(el);
if(dsItem == ITK_NULLPTR)
{
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
OFVector<Float64> doubleVals;
if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
{
DCMTKExceptionOrErrorReturn(<< "Cant extract Array from DecimalString " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
if(doubleVals.size() != count)
{
DCMTKExceptionOrErrorReturn(<< "DecimalString " << std::hex
<< group << " " << std::hex
<< element << " expected "
<< count << "items, but found "
<< doubleVals.size() << std::dec);
}
for(unsigned i = 0; i < count; i++)
{
target[i] = static_cast<TType>(doubleVals[i]);
}
return EXIT_SUCCESS;
}
template <typename TType>
int GetElementDSorOB(const unsigned short group,
const unsigned short element,
TType &target,
const bool throwException = true) const
{
if(this->GetElementDS<TType>(group,element,1,&target,false) == EXIT_SUCCESS)
{
return EXIT_SUCCESS;
}
std::string val;
if(this->GetElementOB(group,element,val) != EXIT_SUCCESS)
{
DCMTKExceptionOrErrorReturn(<< "Cant find DecimalString element " << std::hex
<< group << " " << std::hex
<< element << std::dec);
}
const char *data = val.c_str();
const TType *fptr = reinterpret_cast<const TType *>(data);
target = *fptr;
switch(this->GetTransferSyntax())
{
case EXS_LittleEndianImplicit:
case EXS_LittleEndianExplicit:
itk::ByteSwapper<TType>::SwapFromSystemToLittleEndian(&target);
break;
case EXS_BigEndianImplicit:
case EXS_BigEndianExplicit:
itk::ByteSwapper<TType>::SwapFromSystemToBigEndian(&target);
break;
default:
break;
}
return EXIT_SUCCESS;
}
/** Get a DecimalString Item as a single string
*/
int GetElementDS(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementFD(const unsigned short group,
const unsigned short element,
double &target,
const bool throwException = true) const;
int GetElementFD(const unsigned short group,
const unsigned short element,
int count,
double * target,
const bool throwException = true) const;
int GetElementFL(const unsigned short group,
const unsigned short element,
float &target,
const bool throwException = true) const;
int GetElementFLorOB(const unsigned short group,
const unsigned short element,
float &target,
const bool throwException = true) const;
int GetElementUS(const unsigned short group,
const unsigned short element,
unsigned short &target,
const bool throwException = true) const;
int GetElementUS(const unsigned short group,
const unsigned short element,
unsigned short *&target,
const bool throwException = true) const;
/** Get a DecimalString Item as a single string
*/
int GetElementCS(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
/** Get a PersonName Item as a single string
*/
int GetElementPN(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
/** get an IS (Integer String Item
*/
int GetElementIS(const unsigned short group,
const unsigned short element,
::itk::int32_t &target,
const bool throwException = true) const;
int GetElementSL(const unsigned short group,
const unsigned short element,
::itk::int32_t &target,
const bool throwException = true) const;
int GetElementISorOB(const unsigned short group,
const unsigned short element,
::itk::int32_t &target,
const bool throwException = true) const;
int GetElementCSorOB(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
/** get an OB OtherByte Item
*/
int GetElementOB(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementSQ(const unsigned short group,
unsigned short entry,
DCMTKSequence &sequence,
const bool throwException = true) const;
int GetElementUI(const unsigned short group,
unsigned short entry,
std::string &target,
const bool throwException = true) const;
int GetElementDA(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetElementTM(const unsigned short group,
const unsigned short element,
std::string &target,
const bool throwException = true) const;
int GetDirCosines(vnl_vector<double> &dir1,
vnl_vector<double> &dir2,
vnl_vector<double> &dir3) const;
int GetDirCosArray(double * const dircos) const;
int GetFrameCount() const;
int GetSlopeIntercept(double &slope, double &intercept) const;
int GetDimensions(unsigned short &rows, unsigned short &columns) const;
ImageIOBase::IOComponentType GetImageDataType() const;
ImageIOBase::IOPixelType GetImagePixelType() const;
int GetSpacing(double * const spacing) const;
int GetOrigin(double * const origin) const;
bool HasPixelData() const;
E_TransferSyntax GetTransferSyntax() const;
long GetFileNumber() const;
static void
AddDictEntry(DcmDictEntry *entry);
static bool CanReadFile(const std::string &filename);
static bool IsImageFile(const std::string &filename);
private:
std::string m_FileName;
DcmFileFormat* m_DFile;
DcmDataset * m_Dataset;
E_TransferSyntax m_Xfer;
Sint32 m_FrameCount;
long m_FileNumber;
};
extern bool CompareDCMTKFileReaders(DCMTKFileReader *a, DCMTKFileReader *b);
}
#endif // itkDCMTKFileReader_h
|