Fraxinus  18.10
An IGT application
cxDataReaderWriter.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 
13 #include "cxDataReaderWriter.h"
14 
15 #include <sstream>
16 #include <vtkImageData.h>
17 #include <vtkMetaImageReader.h>
18 #include <vtkSmartPointer.h>
19 #include <vtkMetaImageWriter.h>
20 
21 #include <vtkPolyData.h>
22 #include <vtkPolyDataReader.h>
23 #include <vtkXMLPolyDataReader.h>
24 #include <vtkPolyDataWriter.h>
25 #include <vtkSTLReader.h>
26 #include <vtkImageChangeInformation.h>
27 #include "vtkMINCImageReader.h"
28 #include "vtkTransform.h"
29 #include "vtkCommand.h"
30 #include <vtkPNGReader.h>
31 #include <vtkNIFTIImageReader.h>
32 #include <vtkNIFTIImageHeader.h>
33 
34 #include <QtCore>
35 #include <QDomDocument>
36 #include <QFileInfo>
37 #include <QFile>
38 #include <QTextStream>
39 #include <QDir>
40 
41 #include "cxTransform3D.h"
43 #include "cxLogger.h"
44 #include "cxTypeConversions.h"
45 #include "cxUtilHelpers.h"
46 #include "cxVideoSource.h"
47 #include "cxCustomMetaImage.h"
48 #include "cxConfig.h"
49 
50 #include "cxImageLUT2D.h"
51 #include "cxImageTF3D.h"
52 #include "cxErrorObserver.h"
53 
54 
55 typedef vtkSmartPointer<class vtkPNGReader> vtkPNGReaderPtr;
56 
57 namespace cx
58 {
59 
60 //-----
62 {
63  vtkMetaImageReaderPtr reader = vtkMetaImageReaderPtr::New();
64  reader->SetFileName(cstring_cast(filename));
65  reader->ReleaseDataFlagOn();
66 
67  if (!ErrorObserver::checkedRead(reader, filename))
68  return vtkImageDataPtr();
69 
70  vtkImageChangeInformationPtr zeroer = vtkImageChangeInformationPtr::New();
71  zeroer->SetInputConnection(reader->GetOutputPort());
72  zeroer->SetOutputOrigin(0, 0, 0);
73  zeroer->Update();
74  return zeroer->GetOutput();
75 }
76 
77 bool MetaImageReader::readInto(DataPtr data, QString filename)
78 {
79  return this->readInto(boost::dynamic_pointer_cast<Image>(data), filename);
80 }
81 
82 bool MetaImageReader::readInto(ImagePtr image, QString filename)
83 {
84  if (!image)
85  return false;
86 
87  CustomMetaImagePtr customReader = CustomMetaImage::create(filename);
88  Transform3D rMd = customReader->readTransform();
89 
90  vtkImageDataPtr raw = this->loadVtkImageData(filename);
91  if(!raw)
92  return false;
93 
94  image->setVtkImageData(raw);
95  image->get_rMd_History()->setRegistration(rMd);
96  image->setModality(customReader->readModality());
97  image->setImageType(customReader->readImageType());
98 
99  bool ok1 = true;
100  bool ok2 = true;
101  double level = customReader->readKey("WindowLevel").toDouble(&ok1);
102  double window = customReader->readKey("WindowWidth").toDouble(&ok2);
103 
104  if (ok1 && ok2)
105  {
106  image->setInitialWindowLevel(window, level);
107  image->resetTransferFunctions();
108  }
109 
110  return true;
111 }
112 
113 //-----
114 DataPtr MetaImageReader::load(const QString& uid, const QString& filename)
115 {
116  ImagePtr image(new Image(uid, vtkImageDataPtr()));
117  this->readInto(image, filename);
118  return image;
119 }
120 
121 void MetaImageReader::saveImage(ImagePtr image, const QString& filename)
122 {
123  vtkMetaImageWriterPtr writer = vtkMetaImageWriterPtr::New();
124  writer->SetInputData(image->getBaseVtkImageData());
125  writer->SetFileDimensionality(3);
126  writer->SetFileName(cstring_cast(filename));
127  QDir().mkpath(QFileInfo(filename).path());
128 
129 // std::cout << "SAVING MHD COMPRESSED " << filename << std::endl;
130 // writer->SetCompression(true);
131  writer->SetCompression(false);
132 // writer->Update(); // caused writing of (null).0 files - not necessary
133  writer->Write();
134 
135  writer = 0;
136 
137  CustomMetaImagePtr customReader = CustomMetaImage::create(filename);
138  customReader->setTransform(image->get_rMd());
139  customReader->setModality(image->getModality());
140  customReader->setImageType(image->getImageType());
141  customReader->setKey("WindowLevel", qstring_cast(image->getInitialWindowLevel()));
142  customReader->setKey("WindowWidth", qstring_cast(image->getInitialWindowWidth()));
143  customReader->setKey("Creator", QString("CustusX_%1").arg(CustusX_VERSION_STRING));
144 }
145 
149 
151  DataReader()
152 {
153  sform_matrix = vtkMatrix4x4Ptr::New();
154 }
155 
156 bool NIfTIReader::readInto(ImagePtr image, QString path)
157 {
158  if (!image)
159  return false;
160 
161  vtkImageDataPtr raw = this->loadVtkImageData(path);
162  if(!raw)
163  return false;
164 
165  Transform3D rMd(sform_matrix);
166  image->setVtkImageData(raw);
167  image->get_rMd_History()->setRegistration(rMd);
168 
169  return true;
170 }
171 
172 bool NIfTIReader::readInto(DataPtr data, QString filename)
173 {
174  return this->readInto(boost::dynamic_pointer_cast<Image>(data), filename);
175 }
176 
177 DataPtr NIfTIReader::load(const QString &uid, const QString &filename)
178 {
179  ImagePtr image(new Image(uid, vtkImageDataPtr()));
180  this->readInto(image, filename);
181  return image;
182 }
183 
185 {
186  vtkNIFTIImageReaderPtr reader = vtkNIFTIImageReaderPtr::New();
187  reader->SetFileName(cstring_cast(filename));
188  reader->ReleaseDataFlagOn();
189  reader->Update();
190 
202  sform_matrix = reader->GetSFormMatrix();
203 
204  if (!ErrorObserver::checkedRead(reader, filename))
205  return vtkImageDataPtr();
206 
207  return reader->GetOutput();
208 }
209 
210 
214 
215 bool PNGImageReader::readInto(DataPtr data, QString filename)
216 {
217  return this->readInto(boost::dynamic_pointer_cast<Image>(data), filename);
218 }
219 
220 bool PNGImageReader::readInto(ImagePtr image, QString filename)
221 {
222  if (!image)
223  return false;
224  vtkImageDataPtr raw = this->loadVtkImageData(filename);
225  if(!raw)
226  return false;
227  image->setVtkImageData(raw);
228  return true;
229 }
230 
231 DataPtr PNGImageReader::load(const QString& uid, const QString& filename)
232 {
233  ImagePtr image(new Image(uid, vtkImageDataPtr()));
234  this->readInto(image, filename);
235  return image;
236 }
237 
239 {
240  vtkPNGReaderPtr pngReader = vtkPNGReaderPtr::New();
241  pngReader->SetFileName(filename.toStdString().c_str());
242  pngReader->Update();
243  return pngReader->GetOutput();
244 }
245 
249 
250 bool PolyDataMeshReader::readInto(DataPtr data, QString filename)
251 {
252  return this->readInto(boost::dynamic_pointer_cast<Mesh>(data), filename);
253 }
254 
255 bool PolyDataMeshReader::readInto(MeshPtr mesh, QString filename)
256 {
257  if (!mesh)
258  return false;
259  vtkPolyDataPtr raw = this->loadVtkPolyData(filename);
260  if(!raw)
261  return false;
262  mesh->setVtkPolyData(raw);
263  return true;
264 }
265 //-----
267 {
268  vtkPolyDataReaderPtr reader = vtkPolyDataReaderPtr::New();
269  reader->SetFileName(cstring_cast(fileName));
270 
271  if (!ErrorObserver::checkedRead(reader, fileName))
272  return vtkPolyDataPtr();
273 
274  vtkPolyDataPtr polyData = reader->GetOutput();
275  return polyData;
276 }
277 
278 DataPtr PolyDataMeshReader::load(const QString& uid, const QString& filename)
279 {
280  MeshPtr mesh(new Mesh(uid));
281  this->readInto(mesh, filename);
282  return mesh;
283 }
284 
288 
289 
290 bool XMLPolyDataMeshReader::readInto(DataPtr data, QString filename)
291 {
292  return this->readInto(boost::dynamic_pointer_cast<Mesh>(data), filename);
293 }
294 
295 bool XMLPolyDataMeshReader::readInto(MeshPtr mesh, QString filename)
296 {
297  if (!mesh)
298  return false;
299  vtkPolyDataPtr raw = this->loadVtkPolyData(filename);
300  if(!raw)
301  return false;
302  mesh->setVtkPolyData(raw);
303  return true;
304 }
305 //-----
307 {
308  vtkXMLPolyDataReaderPtr reader = vtkXMLPolyDataReaderPtr::New();
309  reader->SetFileName(cstring_cast(fileName));
310 
311  if (!ErrorObserver::checkedRead(reader, fileName))
312  return vtkPolyDataPtr();
313 
314  vtkPolyDataPtr polyData = reader->GetOutput();
315  return polyData;
316 }
317 
318 DataPtr XMLPolyDataMeshReader::load(const QString& uid, const QString& filename)
319 {
320  MeshPtr mesh(new Mesh(uid));
321  this->readInto(mesh, filename);
322  return mesh;
323 }
324 
328 
329 
330 bool StlMeshReader::readInto(DataPtr data, QString filename)
331 {
332  return this->readInto(boost::dynamic_pointer_cast<Mesh>(data), filename);
333 }
334 
335 bool StlMeshReader::readInto(MeshPtr mesh, QString filename)
336 {
337  if (!mesh)
338  return false;
339  vtkPolyDataPtr raw = this->loadVtkPolyData(filename);
340  if(!raw)
341  return false;
342  mesh->setVtkPolyData(raw);
343  return true;
344 }
345 
346 
348 {
349  vtkSTLReaderPtr reader = vtkSTLReaderPtr::New();
350  reader->SetFileName(cstring_cast(fileName));
351 
352  if (!ErrorObserver::checkedRead(reader, fileName))
353  return vtkPolyDataPtr();
354 
355  vtkPolyDataPtr polyData = reader->GetOutput();
356  return polyData;
357 }
358 
359 DataPtr StlMeshReader::load(const QString& uid, const QString& filename)
360 {
361  MeshPtr mesh(new Mesh(uid));
362  this->readInto(mesh, filename);
363  return mesh;
364 }
365 
369 
370 
372 {
373  mDataReaders.insert(DataReaderPtr(new MetaImageReader()));
374  mDataReaders.insert(DataReaderPtr(new NIfTIReader()));
375  mDataReaders.insert(DataReaderPtr(new PolyDataMeshReader()));
376  mDataReaders.insert(DataReaderPtr(new XMLPolyDataMeshReader()));
377  mDataReaders.insert(DataReaderPtr(new StlMeshReader()));
378  mDataReaders.insert(DataReaderPtr(new PNGImageReader()));
379 }
380 
381 DataReaderPtr DataReaderWriter::findReader(const QString& path, const QString& type)
382 {
383  for (DataReadersType::iterator iter = mDataReaders.begin(); iter != mDataReaders.end(); ++iter)
384  {
385  if ((*iter)->canLoad(type, path))
386  return *iter;
387  }
388  return DataReaderPtr();
389 }
390 
392 {
393  DataReaderPtr reader = this->findReader(filename);
394  if (reader)
395  return reader->loadVtkImageData(filename);
396  return vtkImageDataPtr();
397 }
398 
400 {
401  DataReaderPtr reader = this->findReader(filename);
402  if (reader)
403  return reader->loadVtkPolyData(filename);
404  return vtkPolyDataPtr();
405 }
406 
407 QString DataReaderWriter::findDataTypeFromFile(QString filename)
408 {
409  DataReaderPtr reader = this->findReader(filename);
410  if (reader)
411  return reader->canLoadDataType();
412  return "";
413 }
414 
415 void DataReaderWriter::readInto(DataPtr data, QString path)
416 {
417  DataReaderPtr reader = this->findReader(path, data->getType());
418  if (reader)
419  reader->readInto(data, path);
420 
421  if(data)
422  {
423  QFileInfo fileInfo(qstring_cast(path));
424  data->setName(changeExtension(fileInfo.fileName(), ""));
425  data->setFilename(path); // need path even when not set explicitly: nice for testing
426  }
427 
428 }
429 
430 
434 
435 
436 } // namespace cx
437 
438 
QString qstring_cast(const T &val)
A mesh data set.
Definition: cxMesh.h:45
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
QString findDataTypeFromFile(QString filename)
virtual DataPtr load(const QString &uid, const QString &filename)
virtual bool readInto(DataPtr data, QString path)
virtual bool readInto(DataPtr data, QString path)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
vtkSmartPointer< vtkImageChangeInformation > vtkImageChangeInformationPtr
Definition: cxImage.cpp:46
vtkSmartPointer< class vtkXMLPolyDataReader > vtkXMLPolyDataReaderPtr
cstring_cast_Placeholder cstring_cast(const T &val)
virtual vtkImageDataPtr loadVtkImageData(QString filename)
virtual bool readInto(DataPtr data, QString filename)
virtual DataPtr load(const QString &uid, const QString &filename)
boost::shared_ptr< DataReader > DataReaderPtr
vtkPolyDataPtr loadVtkPolyData(QString filename)
boost::shared_ptr< class Data > DataPtr
virtual DataPtr load(const QString &uid, const QString &filename)
vtkSmartPointer< class vtkMetaImageReader > vtkMetaImageReaderPtr
boost::shared_ptr< class CustomMetaImage > CustomMetaImagePtr
virtual DataPtr load(const QString &uid, const QString &filename)
virtual bool readInto(DataPtr data, QString path)
vtkSmartPointer< class vtkSTLReader > vtkSTLReaderPtr
A volumetric data set.
Definition: cxImage.h:45
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
vtkSmartPointer< class vtkPNGReader > vtkPNGReaderPtr
vtkSmartPointer< class vtkMetaImageWriter > vtkMetaImageWriterPtr
vtkSmartPointer< class vtkNIFTIImageReader > vtkNIFTIImageReaderPtr
void saveImage(ImagePtr image, const QString &filename)
virtual bool readInto(DataPtr data, QString path)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
virtual DataPtr load(const QString &uid, const QString &filename)
virtual vtkImageDataPtr loadVtkImageData(QString filename)
QString changeExtension(QString name, QString ext)
void readInto(DataPtr data, QString path)
virtual vtkImageDataPtr loadVtkImageData(QString filename)
static bool checkedRead(vtkSmartPointer< vtkAlgorithm > reader, QString filename)
Reader for portable network graphics .png files.
Reader for .vtp files.
vtkSmartPointer< class vtkPolyDataReader > vtkPolyDataReaderPtr
Reader for .vtk files.
virtual bool readInto(DataPtr data, QString path)
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Reader for metaheader .mhd files.
Reader for STL files.
vtkImageDataPtr loadVtkImageData(QString filename)
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
Interface for Data file readers.
static CustomMetaImagePtr create(QString filename)
Namespace for all CustusX production code.
virtual DataPtr load(const QString &uid, const QString &filename)