Skip to content

Commit

Permalink
MetaIO 2024-04-03 (ea08147a)
Browse files Browse the repository at this point in the history
Code extracted from:

    https://github.com/Kitware/MetaIO.git

at commit ea08147a91e7676c47a9741baae0edd8eecf3c2f (master).
  • Loading branch information
MetaIO Maintainers authored and dzenanz committed Apr 3, 2024
1 parent 7c8f3bb commit 4951dbc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/metaImage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,24 @@ namespace METAIO_NAMESPACE
// 1 Gigabyte is the maximum chunk to read/write in on function call
static const std::streamoff MaxIOChunk = 1024 * 1024 * 1024;

std::set<std::string> MetaImage::m_ImageReservedKeywords = {
"Modality",
"DimSize",
"SequenceID",
"ElementSizeValid",
"ElementSize",
"ElementType",
"ElementDataFileName",
};

//
// MetaImage Constructors
//
MetaImage::MetaImage()
{
META_DEBUG_PRINT( "MetaImage()" );

AddReservedKeywords(m_ImageReservedKeywords);
m_CompressionTable = new MET_CompressionTableType;
m_CompressionTable->compressedStream = nullptr;
m_CompressionTable->buffer = nullptr;
Expand All @@ -102,6 +113,7 @@ MetaImage::MetaImage(const char * _headerName)
{
META_DEBUG_PRINT( "MetaImage()" );

AddReservedKeywords(m_ImageReservedKeywords);
m_CompressionTable = new MET_CompressionTableType;
m_CompressionTable->compressedStream = nullptr;
m_CompressionTable->buffer = nullptr;
Expand All @@ -115,6 +127,7 @@ MetaImage::MetaImage(MetaImage * _im)
{
META_DEBUG_PRINT( "MetaImage()" );

AddReservedKeywords(m_ImageReservedKeywords);
m_CompressionTable = new MET_CompressionTableType;
m_CompressionTable->compressedStream = nullptr;
m_CompressionTable->buffer = nullptr;
Expand Down Expand Up @@ -164,6 +177,7 @@ MetaImage::MetaImage(int _nDims,
int _elementNumberOfChannels,
void * _elementData)
{
AddReservedKeywords(m_ImageReservedKeywords);
// Only consider at most 10 element of spacing:
// See MetaObject::InitializeEssential(_nDims)
double tmpElementSpacing[10];
Expand All @@ -183,6 +197,7 @@ MetaImage::MetaImage(int _nDims,
int _elementNumberOfChannels,
void * _elementData)
{
AddReservedKeywords(m_ImageReservedKeywords);
InitHelper(_nDims, _dimSize, _elementSpacing, _elementType, _elementNumberOfChannels, _elementData);
}

Expand All @@ -197,6 +212,7 @@ MetaImage::MetaImage(int _x,
{
META_DEBUG_PRINT( "MetaImage()" );

AddReservedKeywords(m_ImageReservedKeywords);
m_CompressionTable = new MET_CompressionTableType;
m_CompressionTable->compressedStream = nullptr;
m_CompressionTable->buffer = nullptr;
Expand Down Expand Up @@ -233,6 +249,7 @@ MetaImage::MetaImage(int _x,
{
META_DEBUG_PRINT( "MetaImage()" );

AddReservedKeywords(m_ImageReservedKeywords);
m_CompressionTable = new MET_CompressionTableType;
m_CompressionTable->compressedStream = nullptr;
m_CompressionTable->buffer = nullptr;
Expand Down Expand Up @@ -2835,7 +2852,6 @@ MetaImage::ReadROIStream(int * _indexMin,
delete[] wrds;
delete readStreamTemp;
return false;
return false;
}

// read only one slice
Expand Down
2 changes: 2 additions & 0 deletions src/metaImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ class METAIO_EXPORT MetaImage : public MetaObject

// PROTECTED
protected:
static std::set<std::string> m_ImageReservedKeywords;

MET_ImageModalityEnumType m_Modality;


Expand Down
14 changes: 14 additions & 0 deletions src/metaObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ MetaObject::M_Destroy();
}


std::set<std::string>
MetaObject::GetReservedKeywords() const
{
return m_ReservedKeywords;
}


void
MetaObject::AddReservedKeywords(std::set<std::string> additionalKeywords)
{
m_ReservedKeywords.insert(additionalKeywords.begin(), additionalKeywords.end());
}


// Clear Fields only, if the pointer is in the UserField list it is not deleted.
void
MetaObject::ClearFields()
Expand Down
33 changes: 33 additions & 0 deletions src/metaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# include "metaEvent.h"

# include <string>
#include <set>

# ifdef _MSC_VER
# pragma warning(disable : 4251)
Expand All @@ -33,6 +34,26 @@ class METAIO_EXPORT MetaObject
{
// PROTECTED
protected:
std::set<std::string> m_ReservedKeywords = {
"ObjectType",
"ObjectSubType",
"NDims",
"Offset",
"Position",
"Origin",
"TransformMatrix",
"CenterOfRotation",
"AnatomicalOrientation",
"DistanceUnits",
"ElementSpacing",
"Color",
"AcquisitionDate",
"BinaryData",
"BinaryDataByteOrderMSB",
"CompressedData",
"CompressionLevel"
};

typedef std::vector<MET_FieldRecordType *> FieldsContainerType;

std::ifstream * m_ReadStream;
Expand Down Expand Up @@ -85,6 +106,9 @@ class METAIO_EXPORT MetaObject

static void M_Destroy();

void
AddReservedKeywords(std::set<std::string> additionalKeywords);

virtual void
M_SetupReadFields();

Expand Down Expand Up @@ -115,6 +139,9 @@ class METAIO_EXPORT MetaObject

virtual ~MetaObject();

std::set<std::string>
GetReservedKeywords() const;

void
FileName(const char * _fileName);
const char *
Expand Down Expand Up @@ -382,6 +409,12 @@ class METAIO_EXPORT MetaObject
bool _required = true,
int _dependsOn = -1)
{
// if given field name is one of the reserved keywords,
// don't add it
if(m_ReservedKeywords.find(std::string(_fieldName)) != m_ReservedKeywords.end())
{
return false;
}
// don't add the same field twice. In the unlikely event
// a field of the same name gets added more than once,
// over-write the existing FieldRecord
Expand Down
7 changes: 7 additions & 0 deletions src/tests/testMeta3Image.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ main(int, char *[])
}
}

// user field cannot use reserved keywords
char m[] = { 'C', 'T', '\0' };
if(tIm.AddUserField("Modality", MET_STRING, 3, m))
{
return EXIT_FAILURE;
}

tIm.Write("test.mha");
tIm.PrintInfo();

Expand Down

0 comments on commit 4951dbc

Please sign in to comment.