Skip to content

Commit 1c67ef7

Browse files
committed
WIP: Add support for reading/writing VTK XML ImageData (.vti) format
Prompt for Claude Sonnet 4.6: Add support for .vti image file format. Look at how similar .vtk image file format is implemented in itk::VTKImageIO class. Summary Full implementation supporting: • Reading: ASCII, base64-binary, and raw-appended data formats; little-endian and big-endian files; scalar, vector, RGB, RGBA, and symmetric tensor pixel types • Writing: base64-binary (default) and ASCII formats; all standard scalar/vector/tensor pixel types • Byte-swapping to/from system native byte order • Proper block-size header handling (UInt32/UInt64) for base64 data
1 parent a86e08f commit 1c67ef7

File tree

6 files changed

+1145
-1
lines changed

6 files changed

+1145
-1
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#ifndef itkVTIImageIO_h
19+
#define itkVTIImageIO_h
20+
#include "ITKIOVTKExport.h"
21+
22+
#include <fstream>
23+
#include "itkImageIOBase.h"
24+
25+
namespace itk
26+
{
27+
/**
28+
* \class VTIImageIO
29+
*
30+
* \brief ImageIO class for reading and writing VTK XML ImageData (.vti) files
31+
*
32+
* Supports the VTK XML ImageData format (version 0.1 and 2.2), including
33+
* ASCII, binary (base64-encoded), and raw-appended data formats.
34+
* Scalar, vector (3-component), RGB, RGBA, and symmetric second rank tensor
35+
* pixel types are supported.
36+
*
37+
* \ingroup IOFilters
38+
* \ingroup ITKIOVTK
39+
*/
40+
class ITKIOVTK_EXPORT VTIImageIO : public ImageIOBase
41+
{
42+
public:
43+
ITK_DISALLOW_COPY_AND_MOVE(VTIImageIO);
44+
45+
/** Standard class type aliases. */
46+
using Self = VTIImageIO;
47+
using Superclass = ImageIOBase;
48+
using Pointer = SmartPointer<Self>;
49+
using ConstPointer = SmartPointer<const Self>;
50+
51+
/** Method for creation through the object factory. */
52+
itkNewMacro(Self);
53+
54+
/** \see LightObject::GetNameOfClass() */
55+
itkOverrideGetNameOfClassMacro(VTIImageIO);
56+
57+
/*-------- This part of the interface deals with reading data. ------ */
58+
59+
/** Determine the file type. Returns true if this ImageIO can read the
60+
* file specified. */
61+
bool
62+
CanReadFile(const char *) override;
63+
64+
/** Set the spacing and dimension information for the current filename. */
65+
void
66+
ReadImageInformation() override;
67+
68+
/** Reads the data from disk into the memory buffer provided. */
69+
void
70+
Read(void * buffer) override;
71+
72+
/*-------- This part of the interfaces deals with writing data. ----- */
73+
74+
/** Determine the file type. Returns true if this ImageIO can write the
75+
* file specified. */
76+
bool
77+
CanWriteFile(const char *) override;
78+
79+
/** Writes the spacing and dimensions of the image.
80+
* Assumes SetFileName has been called with a valid file name. */
81+
void
82+
WriteImageInformation() override
83+
{}
84+
85+
/** Writes the data to disk from the memory buffer provided. Make sure
86+
* that the IORegion has been set properly. */
87+
void
88+
Write(const void * buffer) override;
89+
90+
protected:
91+
VTIImageIO();
92+
~VTIImageIO() override;
93+
94+
void
95+
PrintSelf(std::ostream & os, Indent indent) const override;
96+
97+
private:
98+
/** Parse the XML header to fill image information. */
99+
void
100+
InternalReadImageInformation();
101+
102+
/** Map VTK type string to ITK IOComponentEnum. */
103+
static IOComponentEnum
104+
VTKTypeStringToITKComponent(const std::string & vtkType);
105+
106+
/** Map ITK IOComponentEnum to VTK type string. */
107+
static std::string
108+
ITKComponentToVTKTypeString(IOComponentEnum t);
109+
110+
/** Decode a base64-encoded string into raw bytes. Returns number of decoded bytes. */
111+
static SizeType
112+
DecodeBase64(const std::string & encoded, std::vector<unsigned char> & decoded);
113+
114+
/** Encode raw bytes as a base64 string. */
115+
static std::string
116+
EncodeBase64(const unsigned char * data, SizeType numBytes);
117+
118+
/** Trim leading/trailing whitespace from a string. */
119+
static std::string
120+
TrimString(const std::string & s);
121+
122+
/** The encoding format of the data array found in the file. */
123+
enum class DataEncoding
124+
{
125+
ASCII,
126+
Base64, // binary data encoded in base64 (format="binary")
127+
RawAppended // raw binary appended data (format="appended" with raw encoding)
128+
};
129+
130+
DataEncoding m_DataEncoding{ DataEncoding::Base64 };
131+
132+
/** Byte offset into the file where the appended data section begins
133+
* (only relevant when m_DataEncoding == RawAppended). */
134+
std::streampos m_AppendedDataOffset{ 0 };
135+
136+
/** Offset within the appended data block for this DataArray (in bytes,
137+
* not counting the leading block-size UInt32/UInt64). */
138+
SizeType m_DataArrayOffset{ 0 };
139+
140+
/** Whether the header uses 64-bit block-size integers (header_type="UInt64"). */
141+
bool m_HeaderTypeUInt64{ false };
142+
};
143+
} // end namespace itk
144+
145+
#endif // itkVTIImageIO_h
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#ifndef itkVTIImageIOFactory_h
19+
#define itkVTIImageIOFactory_h
20+
#include "ITKIOVTKExport.h"
21+
22+
#include "itkObjectFactoryBase.h"
23+
#include "itkImageIOBase.h"
24+
25+
namespace itk
26+
{
27+
/**
28+
* \class VTIImageIOFactory
29+
* \brief Create instances of VTIImageIO objects using an object factory.
30+
* \ingroup ITKIOVTK
31+
*/
32+
class ITKIOVTK_EXPORT VTIImageIOFactory : public ObjectFactoryBase
33+
{
34+
public:
35+
ITK_DISALLOW_COPY_AND_MOVE(VTIImageIOFactory);
36+
37+
/** Standard class type aliases. */
38+
using Self = VTIImageIOFactory;
39+
using Superclass = ObjectFactoryBase;
40+
using Pointer = SmartPointer<Self>;
41+
using ConstPointer = SmartPointer<const Self>;
42+
43+
/** Class Methods used to interface with the registered factories. */
44+
const char *
45+
GetITKSourceVersion() const override;
46+
47+
const char *
48+
GetDescription() const override;
49+
50+
/** Method for class instantiation. */
51+
itkFactorylessNewMacro(Self);
52+
53+
/** \see LightObject::GetNameOfClass() */
54+
itkOverrideGetNameOfClassMacro(VTIImageIOFactory);
55+
56+
/** Register one factory of this type */
57+
static void
58+
RegisterOneFactory()
59+
{
60+
auto vtiFactory = VTIImageIOFactory::New();
61+
62+
ObjectFactoryBase::RegisterFactoryInternal(vtiFactory);
63+
}
64+
65+
protected:
66+
VTIImageIOFactory();
67+
~VTIImageIOFactory() override;
68+
};
69+
} // end namespace itk
70+
71+
#endif

Modules/IO/VTK/itk-module.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
set(
22
DOCUMENTATION
33
"This module contains classes for reading and writing image
4-
files in the \"legacy\" (non-XML) VTK file format."
4+
files in the \"legacy\" (non-XML) VTK file format and the VTK XML ImageData
5+
(.vti) file format."
56
)
67

78
itk_module(
@@ -14,5 +15,6 @@ itk_module(
1415
ITKImageSources
1516
FACTORY_NAMES
1617
ImageIO::VTK
18+
ImageIO::VTI
1719
DESCRIPTION "${DOCUMENTATION}"
1820
)

Modules/IO/VTK/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ set(
22
ITKIOVTK_SRCS
33
itkVTKImageIOFactory.cxx
44
itkVTKImageIO.cxx
5+
itkVTIImageIOFactory.cxx
6+
itkVTIImageIO.cxx
57
)
68

79
itk_module_add_library(ITKIOVTK ${ITKIOVTK_SRCS})

0 commit comments

Comments
 (0)