45 if (retval->loadFile(filename))
51 DicomImageReader::DicomImageReader() :
56 bool DicomImageReader::loadFile(QString filename)
59 OFCondition status = mFileFormat.loadFile(filename.toLatin1().data());
65 mDataset = mFileFormat.getDataset();
71 return this->wrapInCTK(mDataset);
74 double DicomImageReader::getDouble(
const DcmTagKey& tag,
const unsigned long pos,
const OFBool searchIntoSub)
const
77 OFCondition condition;
78 condition = mDataset->findAndGetFloat64(tag, retval, pos, searchIntoSub);
79 if (!condition.good())
81 QString tagName = this->
item()->TagDescription(tag);
82 this->error(QString(
"Failed to get tag %1/%2").arg(tagName).arg(pos));
90 retval.
center = this->getDouble(DCM_WindowCenter, 0, OFTrue);
91 retval.
width = this->getDouble(DCM_WindowWidth, 0, OFTrue);
101 OFCondition condition;
103 condition = mDataset->findAndGetOFString(DCM_ImageType, value, 2, OFTrue);
104 if (condition.good())
106 QString imageSpesialization(value.c_str());
107 if (imageSpesialization.compare(
"LOCALIZER", Qt::CaseSensitive) == 0)
115 int numberOfFrames = this->
item()->GetElementAsInteger(DCM_NumberOfFrames);
116 if (numberOfFrames==0)
118 unsigned short rows = 0;
119 unsigned short columns = 0;
120 mDataset->findAndGetUint16(DCM_Rows, rows, 0, OFTrue);
121 mDataset->findAndGetUint16(DCM_Columns, columns, 0, OFTrue);
122 if (rows*columns > 0)
125 return numberOfFrames;
134 for (
int i=0; i<3; ++i)
136 OFCondition condition;
137 e_x[i] = this->getDouble(DCM_ImageOrientationPatient, i, OFTrue);
138 e_y[i] = this->getDouble(DCM_ImageOrientationPatient, i+3, OFTrue);
139 pos[i] = this->getDouble(DCM_ImagePositionPatient, i, OFTrue);
151 retval->InitializeFromItem(item);
155 void DicomImageReader::error(QString message)
const
157 reportError(QString(
"Dicom convert: [%1] in %2").arg(message).arg(mFilename));
162 DicomImage dicomImage(mFilename.toLatin1().data());
163 const DiPixel *pixels = dicomImage.getInterData();
166 this->error(
"Found no pixel data");
172 data->SetSpacing(this->getSpacing().data());
174 Eigen::Array3i dim = this->getDim(dicomImage);
175 data->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
177 int samplesPerPixel = pixels->getPlanes();
178 int scalarSize = dim.prod() * samplesPerPixel;
179 int pixelDepth = dicomImage.getDepth();
181 switch (pixels->getRepresentation())
185 data->AllocateScalars(VTK_UNSIGNED_CHAR, samplesPerPixel);
189 data->AllocateScalars(VTK_UNSIGNED_SHORT, samplesPerPixel);
192 this->error(
"DICOM EPR_Uint32 not supported");
197 data->AllocateScalars(VTK_CHAR, samplesPerPixel);
201 data->AllocateScalars(VTK_SHORT, samplesPerPixel);
204 this->error(
"DICOM EPR_Sint32 not supported");
209 int bytesPerPixel = data->GetScalarSize() * samplesPerPixel;
211 memcpy(data->GetScalarPointer(), pixels->getData(), pixels->getCount()*bytesPerPixel);
212 if (pixels->getCount()!=scalarSize)
213 this->error(
"Mismatch in pixel counts");
218 Eigen::Array3d DicomImageReader::getSpacing()
const
220 Eigen::Array3d spacing;
221 spacing[0] = this->getDouble(DCM_PixelSpacing, 0, OFTrue);
222 spacing[1] = this->getDouble(DCM_PixelSpacing, 1, OFTrue);
223 spacing[2] = this->getDouble(DCM_SliceThickness, 0, OFTrue);
228 Eigen::Array3i DicomImageReader::getDim(
const DicomImage& dicomImage)
const
231 dim[0] = dicomImage.getWidth();
232 dim[1] = dicomImage.getHeight();
233 dim[2] = dicomImage.getFrameCount();
239 QString rawName = this->
item()->GetElementAsString(DCM_PatientName);
240 return this->formatPatientName(rawName);
243 QString DicomImageReader::formatPatientName(QString rawName)
const
247 OFString dicomName = rawName.toStdString().c_str();
248 OFString formattedName;
249 OFString lastName, firstName, middleName, namePrefix, nameSuffix;
250 OFCondition l_error = DcmPersonName::getNameComponentsFromString(dicomName,
251 lastName, firstName, middleName, namePrefix, nameSuffix);
254 formattedName.clear();
258 if (!lastName.empty())
260 formattedName += lastName;
261 if ( !(firstName.empty() && middleName.empty()) )
263 formattedName +=
",";
266 if (!firstName.empty())
268 formattedName +=
" ";
269 formattedName += firstName;
271 if (!middleName.empty())
273 formattedName +=
" ";
274 formattedName += middleName;
276 if (!nameSuffix.empty())
278 formattedName +=
", ";
279 formattedName += nameSuffix;
281 if (!namePrefix.empty())
283 formattedName +=
" (";
284 formattedName += namePrefix;
285 formattedName +=
")";
288 return QString(formattedName.c_str());
static DicomImageReaderPtr createFromFile(QString filename)
bool isLocalizerImage() const
void reportError(QString msg)
int getNumberOfFrames() const
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
WindowLevel getWindowLevel() const
ctkDICOMItemPtr item() const
Transform3D getImageTransformPatient() const
vtkImageDataPtr createVtkImageData()
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D ¢er)
QString getPatientName() const
boost::shared_ptr< class ctkDICOMItem > ctkDICOMItemPtr
boost::shared_ptr< class DicomImageReader > DicomImageReaderPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
void setDeepModified(vtkImageDataPtr image)