1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef _itkImageFileCastWriter_hxx
#define _itkImageFileCastWriter_hxx
#include "itkImageFileCastWriter.h"
#include "itkDataObject.h"
#include "itkDeref.h"
#include "itkObjectFactoryBase.h"
#include "itkImageIOFactory.h"
#include "itkCommand.h"
#include <vnl/vnl_vector.h>
#include "itkVectorImage.h"
#include "itkDefaultConvertPixelTraits.h"
#include <iomanip>
namespace itk
{
//---------------------------------------------------------
template <typename TInputImage>
std::string
ImageFileCastWriter<TInputImage>::GetDefaultOutputComponentType()
{
using PixelType = typename TInputImage::InternalPixelType;
return ImageIOBase::GetComponentTypeAsString(ImageIOBase::MapPixelType<PixelType>::CType);
}
//---------------------------------------------------------
template <typename TInputImage>
void
ImageFileCastWriter<TInputImage>::GenerateData()
{
const TInputImage & input = Deref(this->GetInput());
itkDebugMacro("Writing file: " << this->GetFileName());
ImageIOBase & imageIO = Deref(this->GetModifiableImageIO());
// Make sure that the image is the right type and no more than
// four components.
using ScalarType = typename TInputImage::PixelType;
if (strcmp(input.GetNameOfClass(), "VectorImage") == 0)
{
using VectorImageScalarType = typename TInputImage::InternalPixelType;
imageIO.SetPixelTypeInfo(static_cast<const VectorImageScalarType *>(nullptr));
using AccessorFunctorType = typename TInputImage::AccessorFunctorType;
imageIO.SetNumberOfComponents(AccessorFunctorType::GetVectorLength(&input));
}
else
{
// Set the pixel and component type; the number of components.
imageIO.SetPixelTypeInfo(static_cast<const ScalarType *>(nullptr));
}
/** Setup the image IO for writing. */
imageIO.SetFileName(this->GetFileName());
/** Get the number of Components */
unsigned int numberOfComponents = imageIO.GetNumberOfComponents();
/** Extract the data as a raw buffer pointer and possibly convert.
* Converting is only possible if the number of components equals 1 */
if (m_OutputComponentType != ImageIOBase::GetComponentTypeAsString(imageIO.GetComponentType()) &&
m_OutputComponentType != elx::PixelTypeToString<ScalarType>() && numberOfComponents == 1)
{
const void * const convertedDataBuffer = [this, &input] {
/** convert the scalar image to a scalar image with another componenttype
* The imageIO's PixelType is also changed */
if (m_OutputComponentType == "char")
{
return this->ConvertScalarImage<char>(input);
}
if (m_OutputComponentType == "unsigned_char")
{
return this->ConvertScalarImage<unsigned char>(input);
}
if (m_OutputComponentType == "short")
{
return this->ConvertScalarImage<short>(input);
}
if (m_OutputComponentType == "unsigned_short")
{
return this->ConvertScalarImage<unsigned short>(input);
}
if (m_OutputComponentType == "int")
{
return this->ConvertScalarImage<int>(input);
}
if (m_OutputComponentType == "unsigned_int")
{
return this->ConvertScalarImage<unsigned int>(input);
}
if (m_OutputComponentType == "long")
{
return this->ConvertScalarImage<long>(input);
}
if (m_OutputComponentType == "unsigned_long")
{
return this->ConvertScalarImage<unsigned long>(input);
}
if (m_OutputComponentType == "float")
{
return this->ConvertScalarImage<float>(input);
}
if (m_OutputComponentType == "double")
{
return this->ConvertScalarImage<double>(input);
}
return ConvertScalarImageAsSpecifiedByFixedWidthPixelType<std::int8_t,
std::uint8_t,
std::int16_t,
std::uint16_t,
std::int32_t,
std::uint32_t,
std::int64_t,
std::uint64_t,
float,
double>(input);
}();
if (convertedDataBuffer == nullptr)
{
itkExceptionMacro("Unable to convert the input image. An unknown or unsupported component type was specified: "
<< std::quoted(m_OutputComponentType) << ".");
}
/** Do the writing */
imageIO.Write(convertedDataBuffer);
/** Release the caster's memory */
m_Caster = nullptr;
}
else
{
/** No casting needed or possible, just write */
const void * dataPtr = input.GetBufferPointer();
imageIO.Write(dataPtr);
}
}
} // end namespace itk
#endif
|