Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "itkTIFFImageIOFactory.h"
#include "itkBMPImageIOFactory.h"
#include "itkVTKImageIOFactory.h"
#include "itkVTIImageIOFactory.h"
#include "itkNrrdImageIOFactory.h"
#include "itkGiplImageIOFactory.h"
#include "itkNiftiImageIOFactory.h"
Expand Down Expand Up @@ -72,6 +73,7 @@ RegisterRequiredIOFactories()
itk::ObjectFactoryBase::RegisterFactory(itk::GDCMImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::JPEGImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::VTKImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::VTIImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::PNGImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::TIFFImageIOFactory::New());
itk::ObjectFactoryBase::RegisterFactory(itk::BMPImageIOFactory::New());
Expand Down
178 changes: 178 additions & 0 deletions Modules/IO/VTK/include/itkVTIImageIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkVTIImageIO_h
#define itkVTIImageIO_h
#include "ITKIOVTKExport.h"

#include <fstream>
#include "itkImageIOBase.h"

namespace itk
{
/**
* \class VTIImageIO
*
* \brief ImageIO class for reading and writing VTK XML ImageData (.vti) files.
*
* Supports the VTK XML ImageData format (version 0.1 and 2.2), including
* ASCII, binary (base64-encoded), and raw-appended data formats.
* Scalar, vector (3-component), RGB, RGBA, and symmetric second rank tensor
* pixel types are supported.
*
* The XML structure is parsed using the expat XML library (provided by
* ITKExpat / ITKIOXML), so the parser is robust to attribute ordering,
* whitespace, comments, and CDATA sections. The raw appended data section
* (which is XML-illegal binary content following an `_` marker) is read
* directly from the file at the byte offset recorded by the parser when
* it encountered the `<AppendedData>` element.
*
* \ingroup IOFilters
* \ingroup ITKIOVTK
*/
class ITKIOVTK_EXPORT VTIImageIO : public ImageIOBase
{
public:
ITK_DISALLOW_COPY_AND_MOVE(VTIImageIO);

/** Standard class type aliases. */
using Self = VTIImageIO;
using Superclass = ImageIOBase;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;

/** Method for creation through the object factory. */
itkNewMacro(Self);

/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(VTIImageIO);

/*-------- This part of the interface deals with reading data. ------ */

/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
bool
CanReadFile(const char *) override;

/** Set the spacing and dimension information for the current filename. */
void
ReadImageInformation() override;

/** Reads the data from disk into the memory buffer provided. */
void
Read(void * buffer) override;

/*-------- This part of the interfaces deals with writing data. ----- */

/** Determine the file type. Returns true if this ImageIO can write the
* file specified. */
bool
CanWriteFile(const char *) override;

/** Writes the spacing and dimensions of the image.
* Assumes SetFileName has been called with a valid file name. */
void
WriteImageInformation() override
{}

/** Writes the data to disk from the memory buffer provided. Make sure
* that the IORegion has been set properly. */
void
Write(const void * buffer) override;

protected:
VTIImageIO();
~VTIImageIO() override;

void
PrintSelf(std::ostream & os, Indent indent) const override;

private:
/** Parse the XML header to fill image information. */
void
InternalReadImageInformation();

/** Map VTK type string to ITK IOComponentEnum. */
static IOComponentEnum
VTKTypeStringToITKComponent(const std::string & vtkType);

/** Map ITK IOComponentEnum to VTK type string. */
static std::string
ITKComponentToVTKTypeString(IOComponentEnum t);

/** Decode a base64-encoded string into raw bytes. Returns the number
* of decoded bytes. */
static SizeType
DecodeBase64(const std::string & encoded, std::vector<unsigned char> & decoded);

/** Encode raw bytes as a base64 string. */
static std::string
EncodeBase64(const unsigned char * data, SizeType numBytes);

/** Trim leading/trailing whitespace from a string. */
static std::string
TrimString(const std::string & s);

// Encoding format of the data array found in the file.
// (Plain comment, not doxygen, to satisfy KWStyle's `\class` rule for
// class-like declarations.)
enum class DataEncoding : std::uint8_t
{
ASCII,
Base64, // binary data encoded in base64 (format="binary")
RawAppended, // raw binary appended data (format="appended" with raw encoding)
ZLibBase64, // zlib-compressed data encoded in base64
ZLibAppended // zlib-compressed raw appended data
};

DataEncoding m_DataEncoding{ DataEncoding::Base64 };

/** Cached ASCII data text content captured by the expat parser when the
* active DataArray is in ASCII format. Empty otherwise. */
std::string m_AsciiDataContent{};

/** Cached base64 data text content captured by the expat parser when the
* active DataArray is in base64 ("binary") format. Empty otherwise. */
std::string m_Base64DataContent{};

/** Byte offset into the file where the appended data section begins
* (only relevant when m_DataEncoding == RawAppended). */
std::streampos m_AppendedDataOffset{ 0 };

/** Offset within the appended data block for this DataArray (in bytes,
* not counting the leading block-size UInt32/UInt64). */
SizeType m_DataArrayOffset{ 0 };

/** Whether the header uses 64-bit block-size integers (header_type="UInt64"). */
bool m_HeaderTypeUInt64{ false };

/** Whether the file uses zlib compression (compressor="vtkZLibDataCompressor"). */
bool m_IsZLibCompressed{ false };

/** Decompress a VTK zlib-compressed block sequence into raw bytes.
* The input buffer begins with the VTK multi-block compression header
* (nblocks / uncompressed_blocksize / last_partial_size / compressed_sizes[]).
* Header integers are UInt32 or UInt64 per \a headerUInt64. */
static void
DecompressZLib(const unsigned char * compressedData,
std::size_t compressedDataSize,
bool headerUInt64,
std::vector<unsigned char> & uncompressed);
};
} // end namespace itk

#endif // itkVTIImageIO_h
71 changes: 71 additions & 0 deletions Modules/IO/VTK/include/itkVTIImageIOFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkVTIImageIOFactory_h
#define itkVTIImageIOFactory_h
#include "ITKIOVTKExport.h"

#include "itkObjectFactoryBase.h"
#include "itkImageIOBase.h"

namespace itk
{
/**
* \class VTIImageIOFactory
* \brief Create instances of VTIImageIO objects using an object factory.
* \ingroup ITKIOVTK
*/
class ITKIOVTK_EXPORT VTIImageIOFactory : public ObjectFactoryBase
{
public:
ITK_DISALLOW_COPY_AND_MOVE(VTIImageIOFactory);

/** Standard class type aliases. */
using Self = VTIImageIOFactory;
using Superclass = ObjectFactoryBase;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;

/** Class Methods used to interface with the registered factories. */
const char *
GetITKSourceVersion() const override;

const char *
GetDescription() const override;

/** Method for class instantiation. */
itkFactorylessNewMacro(Self);

/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(VTIImageIOFactory);

/** Register one factory of this type */
static void
RegisterOneFactory()
{
auto vtiFactory = VTIImageIOFactory::New();

ObjectFactoryBase::RegisterFactoryInternal(vtiFactory);
}

protected:
VTIImageIOFactory();
~VTIImageIOFactory() override;
};
} // end namespace itk

#endif
8 changes: 7 additions & 1 deletion Modules/IO/VTK/itk-module.cmake
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
set(
DOCUMENTATION
"This module contains classes for reading and writing image
files in the \"legacy\" (non-XML) VTK file format."
files in the \"legacy\" (non-XML) VTK file format and the VTK XML
ImageData (.vti) file format."
)

itk_module(
ITKIOVTK
ENABLE_SHARED
DEPENDS
ITKIOImageBase
PRIVATE_DEPENDS
ITKExpat
ITKZLIB
TEST_DEPENDS
ITKTestKernel
ITKImageSources
ITKZLIB
FACTORY_NAMES
ImageIO::VTK
ImageIO::VTI
DESCRIPTION "${DOCUMENTATION}"
)
2 changes: 2 additions & 0 deletions Modules/IO/VTK/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ set(
ITKIOVTK_SRCS
itkVTKImageIO.cxx
itkVTKImageIOFactory.cxx
itkVTIImageIOFactory.cxx
itkVTIImageIO.cxx
)

itk_module_add_library(ITKIOVTK ${ITKIOVTK_SRCS})
Loading
Loading