Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkImageSerializer.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkImageSerializer.h"
14 #include "mitkIOUtil.h"
15 #include "mitkImage.h"
16 #include <Poco/Path.h>
17 
18 MITK_REGISTER_SERIALIZER(ImageSerializer)
19 
21 {
22 }
23 
25 {
26 }
27 
29 {
30  const auto *image = dynamic_cast<const Image *>(m_Data.GetPointer());
31  if (!image)
32  {
33  MITK_ERROR << " Object at " << (const void *)this->m_Data << " is not an mitk::Image. Cannot serialize as image.";
34  return "";
35  }
36 
37  std::string filename(this->GetUniqueFilenameInWorkingDirectory());
38  std::cout << "creating file " << filename << " in " << m_WorkingDirectory << std::endl;
39  filename += "_";
40  filename += m_FilenameHint;
41 
42  std::string fullname(m_WorkingDirectory);
43  fullname += Poco::Path::separator();
44  fullname += filename + ".nrrd";
45 
46  try
47  {
48  IOUtil::Save(image, fullname);
49  }
50  catch (std::exception &e)
51  {
52  MITK_ERROR << " Error serializing object at " << (const void *)this->m_Data << " to " << fullname << ": "
53  << e.what();
54  return "";
55  }
56  return Poco::Path(fullname).getFileName(); // + ".pic";
57 }
#define MITK_REGISTER_SERIALIZER(classname)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
std::string GetUniqueFilenameInWorkingDirectory()
DataCollection - Class to facilitate loading/accessing structured data.
Serializes mitk::Image for mitk::SceneIO.
BaseData::ConstPointer m_Data
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
std::string Serialize() override
Serializes given BaseData object.