|
| 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 |
0 commit comments