Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#4926 from zivy/dicomTagW…
Browse files Browse the repository at this point in the history
…arning

ENH: Change from Debug to Warning macro.
  • Loading branch information
zivy authored Nov 4, 2024
2 parents 6cb6bf9 + e7b504b commit dae9bef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
17 changes: 14 additions & 3 deletions Modules/IO/GDCM/src/itkGDCMImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,19 @@ GDCMImageIO::Write(const void * buffer)
gdcm::StringFilter sf;
sf.SetFile(writer.GetFile());

std::vector<std::string> problematicKeys;

while (itr != end)
{
std::string value;
const std::string & key = itr->first; // Needed for bcc32
ExposeMetaData<std::string>(dict, key, value);

// Convert DICOM name to DICOM (group,element)
bool b = tag.ReadFromPipeSeparatedString(key.c_str());
// Convert DICOM name to DICOM (group,element). Corner case
// currently ignored, same tag appears twice in the dictionary
// once with comma separator and once with pipe. The last one
// encountered is the one used to set the tag value.
bool b = tag.ReadFromPipeSeparatedString(key.c_str()) || tag.ReadFromCommaSeparatedString(key.c_str());

// Anything that has been changed in the MetaData Dict will be pushed
// into the DICOM header:
Expand Down Expand Up @@ -1006,13 +1011,19 @@ GDCMImageIO::Write(const void * buffer)
}
else
{
itkDebugMacro("GDCMImageIO: non-DICOM and non-ITK standard key = " << key);
problematicKeys.push_back(key);
}
}

++itr;
}

if (!problematicKeys.empty())
{
itkWarningMacro("ignoring non-DICOM and non-ITK standard keys = "
<< problematicKeys[0] +
std::accumulate(problematicKeys.begin() + 1, problematicKeys.end(), std::string(", ")));
}
gdcm::SmartPointer<gdcm::Image> simage = new gdcm::Image;
gdcm::Image & image = *simage;
image.SetNumberOfDimensions(2); // good default
Expand Down
52 changes: 47 additions & 5 deletions Modules/IO/GDCM/test/itkGDCMImageIOTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ itkGDCMImageIOTest2(int argc, char * argv[])
// reader->GetOutput()->Print(std::cout);

itk::MetaDataDictionary & dict = dicomIO->GetMetaDataDictionary();
std::string tagkey, value;
std::string tagkey, value, commatagkey, commavalue;
tagkey = "0002|0002";
value = "1.2.840.10008.5.1.4.1.1.4"; // Media Storage SOP Class UID
itk::EncapsulateMetaData<std::string>(dict, tagkey, value);
Expand All @@ -78,9 +78,9 @@ itkGDCMImageIOTest2(int argc, char * argv[])
tagkey = "0020|1040"; // Position Reference Indicator
value = "";
itk::EncapsulateMetaData<std::string>(dict, tagkey, value);
tagkey = "0018|0020"; // Scanning Sequence
value = "GR";
itk::EncapsulateMetaData<std::string>(dict, tagkey, value);
commatagkey = "0018,0020"; // Scanning Sequence
commavalue = "GR";
itk::EncapsulateMetaData<std::string>(dict, commatagkey, commavalue);
tagkey = "0018|0021"; // Sequence Variant
value = "SS\\SP";
itk::EncapsulateMetaData<std::string>(dict, tagkey, value);
Expand Down Expand Up @@ -155,6 +155,48 @@ itkGDCMImageIOTest2(int argc, char * argv[])

ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


// Try to read tags from the written file
auto outputReader = ReaderType::New();
outputReader->SetFileName(writer->GetFileName());
try
{
outputReader->Update();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: exception in file reader " << std::endl;
std::cerr << error << std::endl;
return EXIT_FAILURE;
}
auto & inputDict = outputReader->GetOutput()->GetMetaDataDictionary();
// DICOM tags can be specified using pipe or comma separators for
// writing in the metadata dictionary, but are always read back with
// the pipe separator.
commatagkey[4] = '|';
auto tagIt = inputDict.Find(commatagkey);
if (tagIt == inputDict.End())
{
std::cerr << "Error: Tag " << commatagkey << " expected to be in file but missing" << std::endl;
return EXIT_FAILURE;
}
else
{
itk::MetaDataObject<std::string>::ConstPointer tagvalue =
dynamic_cast<const itk::MetaDataObject<std::string> *>(tagIt->second.GetPointer());
if (tagvalue)
{
if (tagvalue->GetMetaDataObjectValue() != commavalue)
{
std::cerr << "Error: Written tag value was (" << commavalue << "), read value was ("
<< tagvalue->GetMetaDataObjectValue() << ")" << std::endl;
return EXIT_FAILURE;
}
}
else
{
std::cerr << "Error: Tag value for tag (" << commatagkey << ") is missing" << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

0 comments on commit dae9bef

Please sign in to comment.