MeVisLab Toolbox Reference
mlMultiFileVolumeListDcmtkTools.h
Go to the documentation of this file.
1// Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2// **InsertLicense** code
3//----------------------------------------------------------------------------------
5
10//----------------------------------------------------------------------------------
11#pragma once
12
13// ML-includes
14// Local includes
16#include "mlTypeDefs.h"
17
18#include <dcmtk/dcmdata/dcfilefo.h>
19#include <dcmtk/dcmdata/dcdeftag.h>
20
22typedef boost::shared_ptr<DcmFileFormat> DcmFileFormatPtr;
23
25
26//--------------------------------------------------------------------------------------------------
28//--------------------------------------------------------------------------------------------------
29inline bool loadDcmtkFile(const std::string& fName, DcmFileFormat& dcmfile, std::string* errVal)
30{
31 OFCondition cond = dcmfile.loadFile(fName.c_str());
32 if (cond.bad()) {
33 if (errVal) { *errVal = std::string("loadDcmFile:") + std::string(cond.text()); }
34 return false;
35 }
36 cond = dcmfile.loadAllDataIntoMemory();
37 if (cond.bad()) {
38 if (errVal) { *errVal = std::string("loadDcmFile:") + std::string(cond.text()); }
39 return false;
40 }
41 return true;
42}
43
44//--------------------------------------------------------------------------------------------------
46//--------------------------------------------------------------------------------------------------
48{
49 DcmDataset *dataset = dcmfile.getDataset();
50 OFString modality;
51 OFCondition cond = dataset->findAndGetOFString(DCM_Modality, modality);
52 if (cond.bad()) {
53 if (errVal){ *errVal = std::string("getModality:") + std::string(cond.text()); }
54 }
55 return modality;
56}
57
58//--------------------------------------------------------------------------------------------------
61//--------------------------------------------------------------------------------------------------
63{
65 template <typename DCMObjType>
67 {
68 return dcmObj->read(item);
69 }
70};
71
72//--------------------------------------------------------------------------------------------------
95//--------------------------------------------------------------------------------------------------
96template <typename DCMObjType, typename DCMObjBoostPtrType, class DCMReaderFunctor=DefaultDcmtkReaderFunctor>
98 std::string *errVal,
99 const std::string &modalityStr,
100 const std::string &typeStr,
102{
104 if (modality.compare(modalityStr.c_str()) == 0) {
105 DcmDataset *dataset = dcmFile.getDataset();
106 DcmItem &item = dynamic_cast<DcmItem&>(*dataset);
110 if (cond.bad()) {
111 if (errVal) { *errVal = std::string("get") + typeStr + ": " + std::string(cond.text()); }
112 } else {
113 return dcmObj;
114 }
115 } else {
116 if (errVal) { *errVal = std::string("get") + typeStr + ": Data set contains no " + typeStr + " object"; }
117 }
118 return DCMObjBoostPtrType();
119}
120
121//--------------------------------------------------------------------------------------------------
146//--------------------------------------------------------------------------------------------------
147template <typename DCMObjType, typename DCMObjBoostPtrType, class DCMReaderFunctor=DefaultDcmtkReaderFunctor>
149 std::string *errVal,
151 const std::string &modalityStr,
152 const std::string &typeStr,
154{
155 // Allow all three combinations:
156 // dcmFilePtr == NULL : A temporary DcmFileFormat is used for loading and a boost pointer
157 // to the loaded object is returned.
158 // dcmFilePtr != NULL and *dcmFilePtr == NULL: Create a new DcmFileFormat object and set *dcmFilePtr to it and a
159 // boost pointer to the loaded object is returned.
160 // The file pointer is set even if loading the fName fails.
161 // dcmFilePtr != NULL and *dcmFilePtr != NULL: Load file from filename with already existing *dcmFilePtr
162 // so no new DcmFileFormat is created. A boost pointer to the
163 // loaded object is returned.
164 bool success = true;
165 if (dcmFilePtr && !(*dcmFilePtr)) {
166 // No DcmFileFormat is available. Create a new one and return it in *dcmFilePtr.
168 success = (*dcmFilePtr != NULL);
169 }
170
171 // Temporary loader used in case of NULL dcmFilePtr.
173 if (success) {
174 // Either dcmFilePtr && *dcmFilePtr should be valid or dcmFilePtr is NULL.
176 }
177 if (success) {
178 // DcmFile could be loaded either with **dcmFilePtr or with dcmfile loader.
179 // Either dcmFilePtr && *dcmFilePtr should be valid or dcmFilePtr is NULL.
181 errVal,
183 typeStr,
185 }
186
187 // If we reached here then loading or DcmFileFormat object creation failed, return a NULL object.
188 return DCMObjBoostPtrType();
189}
190
Project global and OS specific declarations.
boost::shared_ptr< DcmFileFormat > DcmFileFormatPtr
Boost shared pointer managing a DcmFileFormat object.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
DCMObjBoostPtrType getDCMTKObjFromFile(DcmFileFormat &dcmFile, std::string *errVal, const std::string &modalityStr, const std::string &typeStr, DCMReaderFunctor *readerFunctor=NULL)
A template for loading a dcmtk object by a file object.
OFString getDcmtkModality(DcmFileFormat &dcmfile, std::string *errVal)
Return the modality of a given DcmFileFormat.
bool loadDcmtkFile(const std::string &fName, DcmFileFormat &dcmfile, std::string *errVal)
Load a DICOM file into the dcmtk DcmFileFormat object.
DCMObjBoostPtrType getDCMTKObjFromString(const std::string &fName, std::string *errVal, DcmFileFormatPtr *dcmFilePtr, const std::string &modalityStr, const std::string &typeStr, DCMReaderFunctor *readerFunctor=NULL)
A template for loading a dcmtk object by a file name.
Default class providing the read function for getDCMTKObjFromFile which needs the option to extent it...
OFCondition read(DCMObjType &dcmObj, DcmItem &item)
Returns the result after reading item from dcmObj.