Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkCastToFloatImageFilter.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 "mitkImageReadAccessor.h"
15 #include "mitkImageWriteAccessor.h"
16 
18 {
19  MITK_INFO << "Instantiating CastToFloatImageFilter...";
20  SetNumberOfIndexedInputs(1);
21  SetNumberOfIndexedOutputs(1);
22  MITK_INFO << "Instantiating CastToFloatImageFilter...[Done]";
23 }
24 
26 {
27  MITK_INFO << "Destructed CastToFloatImageFilter.";
28 }
29 
30 template<class TPixelType>
31 float* CastData(const void* inputArray, unsigned long length)
32 {
33  float* outputArray = new float[length];
34  for (unsigned long i = 0; i < length; ++i)
35  {
36  outputArray[i] = (float)((TPixelType*)inputArray)[i];
37  }
38  return outputArray;
39 }
40 
42 {
43  mitk::Image::Pointer inputImage = GetInput();
44  mitk::Image::Pointer outputImage = GetOutput();
45 
46  std::string type = inputImage->GetPixelType().GetTypeAsString();
47 
48  if (type == "scalar (float)" || type == " (float)")
49  {
50  outputImage->Initialize(mitk::MakeScalarPixelType<float>(), inputImage->GetDimension(), inputImage->GetDimensions());
51  outputImage->SetSpacing(inputImage->GetGeometry()->GetSpacing());
52  outputImage->SetImportVolume(mitk::ImageWriteAccessor(inputImage).GetData(), 0, 0, mitk::Image::ImportMemoryManagementType::CopyMemory);
53  MITK_INFO << "Input is already float type. Nothing to do here.";
54  return;
55  }
56  if (outputImage.IsNull())
57  {
58  outputImage = mitk::Image::New();
59  }
60 
61  unsigned long length = 1;
62  for (unsigned int i = 0, limit = inputImage->GetDimension(); i < limit; ++i)
63  length *= inputImage->GetDimensions()[i];
64 
65  float* outputData;
66 
67  mitk::ImageReadAccessor readAccess(inputImage);
68 
69  if (type == "scalar (short)" || type == " (short)")
70  outputData = CastData<short>(readAccess.GetData(), length);
71  else if (type == "scalar (unsigned short)" || type == " (unsigned short)" || type == " (unsigned_short)" || type == "scalar (unsigned_short)")
72  outputData = CastData<unsigned short>(readAccess.GetData(), length);
73  else if (type == "scalar (int)" || type == " (int)")
74  outputData = CastData<int>(readAccess.GetData(), length);
75  else if (type == "scalar (unsigned int)" || type == " (unsigned int)" || type == " (unsigned_int)" || type == "scalar (unsigned_int)")
76  outputData = CastData<unsigned int>(readAccess.GetData(), length);
77  else if (type == "scalar (double)" || type == " (double)")
78  outputData = CastData<double>(readAccess.GetData(), length);
79  else if (type == "scalar (long)" || type == " (long)")
80  outputData = CastData<long>(readAccess.GetData(), length);
81  else
82  mitkThrow() << "The given image has a datatype that is not yet supported. Given datatype: " << type;
83 
84  outputImage->Initialize(mitk::MakeScalarPixelType<float>(), inputImage->GetDimension(), inputImage->GetDimensions());
85  outputImage->SetSpacing(inputImage->GetGeometry()->GetSpacing());
86  outputImage->SetImportVolume(outputData, 0, 0, mitk::Image::ImportMemoryManagementType::CopyMemory);
87  delete[] outputData;
88 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
float * CastData(const void *inputArray, unsigned long length)
#define mitkThrow()
static Pointer New()
InputImageType * GetInput(void)
OutputType * GetOutput()
Get the output data of this image source object.
ImageWriteAccessor class to get locked write-access for a particular image part.
ImageReadAccessor class to get locked read access for a particular image part.
const void * GetData() const
Gives const access to the data.