Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkDICOMSegIOMimeTypes.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 
14 #include "mitkIOMimeTypes.h"
15 
16 #include <array>
17 
18 #include <mitkLogMacros.h>
19 
20 #include <itkGDCMImageIO.h>
21 #include <itksys/SystemTools.hxx>
22 
23 #include <dcmtk/dcmdata/dcfilefo.h>
24 #include <dcmtk/dcmdata/dcdeftag.h>
25 
26 namespace mitk
27 {
28  std::vector<CustomMimeType *> MitkDICOMSEGIOMimeTypes::Get()
29  {
30  std::vector<CustomMimeType *> mimeTypes;
31 
32  // order matters here (descending rank for mime types)
33 
34  mimeTypes.push_back(DICOMSEG_MIMETYPE().Clone());
35  return mimeTypes;
36  }
37 
38 
40  {
41  this->AddExtension("dcm");
43  this->SetComment("DICOM SEG");
44  }
45 
46 
48  {
49  bool canRead(CustomMimeType::AppliesTo(path));
50 
51  // fix for bug 18572
52  // Currently this function is called for writing as well as reading, in that case
53  // the image information can of course not be read
54  // This is a bug, this function should only be called for reading.
55  if (!itksys::SystemTools::FileExists(path.c_str()))
56  {
57  return canRead;
58  }
59  // end fix for bug 18572
60 
61  const std::size_t offset = 128;
62  const std::size_t bufferSize = 4;
63 
64  std::ifstream myfile(path, std::ifstream::binary);
65 
66  if (!myfile.is_open())
67  return false;
68 
69  myfile.seekg(0, std::ifstream::end);
70  const auto fileSize = static_cast<std::size_t>(myfile.tellg());
71 
72  if (fileSize < offset + bufferSize)
73  return false;
74 
75  myfile.seekg(offset);
76  std::array<char, bufferSize> buffer;
77  myfile.read(buffer.data(), bufferSize);
78 
79  if (0 != std::string(buffer.data(), bufferSize).compare("DICM"))
80  return false;
81 
82  DcmFileFormat dcmFileFormat;
83  OFCondition status = dcmFileFormat.loadFile(path.c_str());
84 
85  if (status.bad())
86  {
87  canRead = false;
88  }
89 
90  if (!canRead)
91  {
92  return canRead;
93  }
94 
95  OFString modality;
96  OFString sopClassUID;
97  if (dcmFileFormat.getDataset()->findAndGetOFString(DCM_Modality, modality).good() && dcmFileFormat.getDataset()->findAndGetOFString(DCM_SOPClassUID, sopClassUID).good())
98  {
99  if (modality.compare("SEG") == 0)
100  {//atm we could read SegmentationStorage files. Other storage classes with "SEG" modality, e.g. SurfaceSegmentationStorage (1.2.840.10008.5.1.4.1.1.66.5), are not supported yet.
101  if (sopClassUID.compare("1.2.840.10008.5.1.4.1.1.66.4") == 0)
102  {
103  canRead = true;
104  }
105  else
106  {
107  canRead = false;
108  }
109  }
110  else
111  {
112  canRead = false;
113  }
114  }
115 
116  return canRead;
117  }
118 
120  {
121  return new MitkDICOMSEGMimeType(*this);
122  }
123 
125  {
126  return MitkDICOMSEGMimeType();
127  }
128 
130  {
131  // create a unique and sensible name for this mime type
132  return IOMimeTypes::DEFAULT_BASE_NAME() + ".image.dicom.seg";
133  }
134 }
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool AppliesTo(const std::string &path) const
Checks if the MimeType can handle file at the given location.
void SetComment(const std::string &comment)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
static Vector3D offset
static std::string CATEGORY_IMAGES()
static std::string DEFAULT_BASE_NAME()
static MitkDICOMSEGMimeType DICOMSEG_MIMETYPE()
bool compare(std::pair< double, int > i, std::pair< double, int > j)
void AddExtension(const std::string &extension)
bool AppliesTo(const std::string &path) const override
Checks if the MimeType can handle file at the given location.
void SetCategory(const std::string &category)
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
static std::vector< CustomMimeType * > Get()