CustusX  22.04-rc5
An IGT application
cxNIfTIReader.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxNIfTIReader.h"
13 
14 #include <QFileInfo>
15 #include "vtkNIFTIImageReader.h"
16 #include "vtkMatrix4x4.h"
17 #include "vtkImageData.h"
19 #include "cxTypeConversions.h"
20 #include "cxErrorObserver.h"
21 #include "cxImage.h"
22 #include "cxPatientModelService.h"
23 
24 namespace cx
25 {
26 
28  FileReaderWriterImplService("NIfTIReader", Image::getTypeName(), "", "nii", patientModelService)
29 {
30  sform_matrix = vtkMatrix4x4Ptr::New();
31 }
32 
34 {
35 }
36 
37 
38 bool NIfTIReader::readInto(ImagePtr image, QString path)
39 {
40  if (!image)
41  return false;
42 
43  vtkImageDataPtr raw = this->loadVtkImageData(path);
44  if(!raw)
45  return false;
46 
47  Transform3D rMd(sform_matrix);
48  image->setVtkImageData(raw);
49  image->get_rMd_History()->setRegistration(rMd);
50 
51  return true;
52 }
53 
54 
55 bool NIfTIReader::readInto(DataPtr data, QString filename)
56 {
57  return this->readInto(boost::dynamic_pointer_cast<Image>(data), filename);
58 }
59 
61 {
62  vtkNIFTIImageReaderPtr reader = vtkNIFTIImageReaderPtr::New();
63  reader->SetFileName(cstring_cast(filename));
64  reader->ReleaseDataFlagOn();
65  reader->Update();
66 
78  sform_matrix = reader->GetSFormMatrix();
79 
80  if (!ErrorObserver::checkedRead(reader, filename))
81  return vtkImageDataPtr();
82 
83  return reader->GetOutput();
84 }
85 
87 {
88  return false;
89 }
90 
92 {
93  return Image::getTypeName();
94 }
95 
96 bool NIfTIReader::canRead(const QString &type, const QString &filename)
97 {
98  return this->canReadInternal(type, filename);
99 }
100 
101 DataPtr NIfTIReader::read(const QString &uid, const QString &filename)
102 {
103  ImagePtr image(new Image(uid, vtkImageDataPtr()));
104  this->readInto(image, filename);
105  return image;
106 }
107 
108 std::vector<DataPtr> NIfTIReader::read(const QString &filename)
109 {
110  std::vector<DataPtr> retval;
111  ImagePtr image = boost::dynamic_pointer_cast<Image>(this->createData(Image::getTypeName(), filename));
112 
113  vtkImageDataPtr raw = this->loadVtkImageData(filename);
114  if(!raw)
115  return retval;
116 
117  Transform3D rMd(sform_matrix);
118  image->setVtkImageData(raw);
119  image->get_rMd_History()->setRegistration(rMd);
120 
121 
122  retval.push_back(image);
123  return retval;
124 }
125 
127 {
128  return "";
129 }
130 
131 bool NIfTIReader::canWrite(const QString &type, const QString &filename) const
132 {
133  return this->canWriteInternal(type, filename);
134 }
135 
136 void NIfTIReader::write(cx::DataPtr data, const QString &filename)
137 {
138 }
139 
140 } //namespace cx
141 
DataPtr createData(QString type, QString filename, QString name="") const
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
bool canWrite(const QString &type, const QString &filename) const
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
bool canReadInternal(const QString &type, const QString &filename) const
cstring_cast_Placeholder cstring_cast(const T &val)
virtual ~NIfTIReader()
boost::shared_ptr< class Data > DataPtr
QString canWriteDataType() const
void write(DataPtr data, const QString &filename)
DataPtr read(const QString &uid, const QString &filename)
A volumetric data set.
Definition: cxImage.h:45
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
static QString getTypeName()
Definition: cxImage.h:126
bool readInto(ImagePtr image, QString filename)
vtkSmartPointer< class vtkNIFTIImageReader > vtkNIFTIImageReaderPtr
virtual vtkImageDataPtr loadVtkImageData(QString filename)
static bool checkedRead(vtkSmartPointer< vtkAlgorithm > reader, QString filename)
QString canReadDataType() const
NIfTIReader(PatientModelServicePtr patientModelService)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
bool canRead(const QString &type, const QString &filename)
bool canWriteInternal(const QString &type, const QString &filename) const
Namespace for all CustusX production code.