Skip to content

Commit

Permalink
BUG: fix writing a corrupt meta image header from unknow metadata
Browse files Browse the repository at this point in the history
If an entry in the metadata dictionary which had a type that the
MetaImagIO did not know about then an empty string in the written out
for the value. This is not readable by the metaIO library. So these
unknow or empty metadata entries, are not written.

Change-Id: I322676fdc63ba3b76e21968f986e1e841c0b8e38
  • Loading branch information
blowekamp committed Aug 15, 2012
1 parent 43d8ae9 commit 64b230b
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Modules/IO/Meta/src/itkMetaImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -625,14 +625,22 @@ ::WriteImageInformation(void)
{
strs << bval;
}
else

value = strs.str();

if (value == "" )
{
itkWarningMacro("Unsupported metaData item "
<< *keyIt << " of type "
<< metaDict[*keyIt]->GetMetaDataObjectTypeName()
<< "found, won't be written to image file");
// if the value is an empty string then the resulting entry in
// the header will not be able to be read the the metaIO
// library, which results is a unreadable/corrupt file.
itkWarningMacro("Unsupported or empty metaData item "
<< *keyIt << " of type "
<< metaDict[*keyIt]->GetMetaDataObjectTypeName()
<< "found, won't be written to image file");

// so this entry should be skipped.
continue;
}
value = strs.str();

// Rolling this back out so that the tests pass.
// The meta image AddUserField requires control of the memory space.
Expand Down
4 changes: 4 additions & 0 deletions Modules/IO/Meta/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(ITKIOMetaTests
itkMetaImageIOMetaDataTest.cxx
itkMetaImageIOGzTest.cxx
itkMetaImageIOTest.cxx
itkMetaImageIOTest2.cxx
itkLargeMetaImageWriteReadTest.cxx
testMetaArray.cxx
testMetaBlob.cxx
Expand Down Expand Up @@ -34,6 +35,9 @@ itk_add_test(NAME itkMetaImageIOTest
--compare DATA{${ITK_DATA_ROOT}/Baseline/IO/HeadMRVolume.mhd,HeadMRVolume.raw}
${ITK_TEST_OUTPUT_DIR}/HeadMRVolume.mhd
itkMetaImageIOTest DATA{${ITK_DATA_ROOT}/Input/HeadMRVolume.mhd,HeadMRVolume.raw} ${ITK_TEST_OUTPUT_DIR}/HeadMRVolume.mhd)
itk_add_test(NAME itkMetaImageIOTest2
COMMAND ITKIOMetaTestDriver itkMetaImageIOTest2
${ITK_TEST_OUTPUT_DIR}/itkMetaImageIOTest2.mha)
itk_add_test(NAME itkMetaImageIOShouldFailTest
COMMAND ITKIOMetaTestDriver itkMetaImageIOTest
DATA{${ITK_DATA_ROOT}/Input/MetaImageError.mhd} 1)
Expand Down
113 changes: 113 additions & 0 deletions Modules/IO/Meta/test/itkMetaImageIOTest2.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.
*
*=========================================================================*/

#include <fstream>
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMetaDataObject.h"
#include "itkMetaImageIO.h"
#include "itkTestingHashImageFilter.h"
#include "itkTestingMacros.h"


#define SPECIFIC_IMAGEIO_MODULE_TEST

namespace
{

int TestUnknowMetaDataBug( const std::string &fname )
{

std::cout << "Testing for unknow meta data entry bug." << std::endl;

try
{

typedef unsigned short PixelType;
typedef itk::Image<PixelType, 2> ImageType;

ImageType::RegionType region;
ImageType::SizeType size = {{32,32}};
region.SetSize( size );

ImageType::Pointer image = ImageType::New();
image->SetRegions( region );
image->Allocate();
image->FillBuffer( 0 );

itk::MetaDataDictionary &dict = image->GetMetaDataDictionary();

itk::EncapsulateMetaData<float>(dict,"ASimpleFloatInitalized",static_cast<float>(1.234560F));
itk::EncapsulateMetaData<std::complex<float> >(dict,"AnUnsuportedComplexInitalized",std::complex<float>(1.234560F));

typedef itk::Testing::HashImageFilter<ImageType> Hasher;
Hasher::Pointer hasher = Hasher::New();
hasher->SetInput( image );
hasher->InPlaceOff();
hasher->Update();

std::string originalHash = hasher->GetHash();
std::cout << "\tOriginal image hash: " << originalHash << std::endl;


// Write image out
itk::ImageFileWriter<ImageType>::Pointer writer;
writer = itk::ImageFileWriter<ImageType>::New();
writer->SetInput( image );
writer->SetFileName( fname );
writer->Update();

itk::ImageFileReader<ImageType>::Pointer reader;
reader = itk::ImageFileReader<ImageType>::New();
reader->SetFileName( fname );

hasher->SetInput( reader->GetOutput() );
hasher->Update();

std::string readHash = hasher->GetHash();
std::cout << "\tRead hash: " << readHash << std::endl;

TEST_EXPECT_EQUAL( originalHash, readHash );

}
catch ( std::exception &e )
{
std::cerr << "Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}


return EXIT_SUCCESS;
}

}

int itkMetaImageIOTest2(int argc, char* argv[])
{
if(argc < 2)
{
std::cerr << "Usage: " << argv[0] << " Output\n";
return EXIT_FAILURE;
}

bool pass = true;

pass = ( TestUnknowMetaDataBug( argv[1] ) == EXIT_SUCCESS );

return EXIT_SUCCESS;
}

0 comments on commit 64b230b

Please sign in to comment.