Skip to content

Commit 54887f7

Browse files
zivyhjmjohnson
authored andcommitted
ENH: add missing metadata to DCMTKImageIO
The DCMTKImageIO was not updating the metadata dictionary resulting in empty metadata when reading DICOM files. This commit adds the missing code to update the metadata dictionary with the DICOM tags read from the file. Note that tags that are part of a DICOM sequence are skipped due to the flat nature of the metadata dictionary. Encoding DICOM sequences requires supporting a heirarchical structure in the metadata dictionary which is not straightforward. This omission is consistent with the GDCMImageIO behavior. Possibly address in the future.
1 parent 11c2f38 commit 54887f7

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎Modules/IO/DCMTK/include/itkDCMTKFileReader.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ class ITKIODCMTK_EXPORT DCMTKFileReader
498498
static bool
499499
IsImageFile(const std::string & filename);
500500

501+
DcmDataset *
502+
GetDataset() const
503+
{
504+
return m_Dataset;
505+
}
506+
501507
private:
502508
std::string m_FileName;
503509
DcmFileFormat * m_DFile{ nullptr };

‎Modules/IO/DCMTK/src/itkDCMTKImageIO.cxx‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
#include "itkByteSwapper.h"
2222
#include "itksys/SystemTools.hxx"
2323
#include "itkDCMTKFileReader.h"
24+
#include "itkMetaDataObject.h"
2425
#include <iostream>
2526
#include "vnl/vnl_cross.h"
2627
#include "itkMath.h"
28+
#include "itksys/Base64.h"
2729

2830
#include "dcmtk/dcmimgle/dcmimage.h"
2931
#include "dcmtk/dcmjpeg/djdecode.h"
3032
#include "dcmtk/dcmjpls/djdecode.h"
3133
#include "dcmtk/dcmdata/dcmetinf.h"
3234
#include "dcmtk/dcmdata/dcrledrg.h"
35+
#include "dcmtk/dcmdata/dcelem.h"
3336
#include "dcmtk/oflog/oflog.h"
3437

3538
namespace
@@ -478,6 +481,62 @@ DCMTKImageIO::ReadImageInformation()
478481
this->m_Spacing.push_back(1.0);
479482
}
480483

484+
// Populate the metadata dictionary with all DICOM tag values
485+
MetaDataDictionary & dict = this->GetMetaDataDictionary();
486+
dict.Clear();
487+
DcmDataset * dataset = reader.GetDataset();
488+
if (dataset != nullptr)
489+
{
490+
const unsigned long numElements = dataset->card();
491+
for (unsigned long i = 0; i < numElements; ++i)
492+
{
493+
DcmElement * element = dataset->getElement(i);
494+
if (element == nullptr)
495+
{
496+
continue;
497+
}
498+
const DcmTag & tag = element->getTag();
499+
// Skip pixel data (7FE0,0010)
500+
if (tag.getGroup() == 0x7fe0 && tag.getElement() == 0x0010)
501+
{
502+
continue;
503+
}
504+
// Format key as "GGGG|EEEE" (lowercase hex, matching GDCMImageIO)
505+
char key[10];
506+
std::snprintf(key, sizeof(key), "%04x|%04x", tag.getGroup(), tag.getElement());
507+
508+
const DcmEVR vr = element->getVR();
509+
if (vr == EVR_SQ || vr == EVR_OB || vr == EVR_OW || vr == EVR_OF || vr == EVR_UN || vr == EVR_ox || vr == EVR_px)
510+
{
511+
// Binary VR — base64-encode the raw bytes
512+
Uint8 * byteValue = nullptr;
513+
if (element->getUint8Array(byteValue) == EC_Normal && byteValue != nullptr)
514+
{
515+
const Uint32 length = element->getLength();
516+
if (length > 0)
517+
{
518+
int encodedLengthEstimate = 2 * static_cast<int>(length);
519+
encodedLengthEstimate = ((encodedLengthEstimate / 4) + 1) * 4;
520+
const auto bin = std::make_unique<char[]>(encodedLengthEstimate);
521+
const auto encodedLengthActual =
522+
static_cast<unsigned int>(itksysBase64_Encode(reinterpret_cast<const unsigned char *>(byteValue),
523+
static_cast<SizeValueType>(length),
524+
reinterpret_cast<unsigned char *>(bin.get()),
525+
0));
526+
EncapsulateMetaData<std::string>(dict, key, std::string(bin.get(), encodedLengthActual));
527+
}
528+
}
529+
}
530+
else
531+
{
532+
OFString value;
533+
if (element->getOFStringArray(value) == EC_Normal)
534+
{
535+
EncapsulateMetaData<std::string>(dict, key, std::string(value.c_str()));
536+
}
537+
}
538+
}
539+
}
481540

482541
this->OpenDicomImage();
483542
const DiPixel * interData = this->m_DImage->getInterData();

‎Modules/IO/DCMTK/test/CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ itk_module_test()
22
set(
33
ITKIODCMTKTests
44
itkDCMTKGetDicomTagsTest.cxx
5+
itkDCMTKImageIOMetadataTest.cxx
56
itkDCMTKImageIOMultiFrameImageTest.cxx
67
itkDCMTKImageIONoPreambleTest.cxx
78
itkDCMTKImageIOOrthoDirTest.cxx
@@ -82,6 +83,14 @@ itk_add_test(
8283
${ITK_TEST_OUTPUT_DIR}/DICOMTags.txt
8384
)
8485

86+
itk_add_test(
87+
NAME itkDCMTKImageIOMetadataTest
88+
COMMAND
89+
ITKIODCMTKTestDriver
90+
itkDCMTKImageIOMetadataTest
91+
DATA{${ITK_DATA_ROOT}/Input/DicomSeries/Image0075.dcm}
92+
)
93+
8594
itk_add_test(
8695
NAME itkDCMTKImageIOTest1
8796
COMMAND
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
19+
#include "itkDCMTKImageIO.h"
20+
#include "itkMetaDataObject.h"
21+
#include "itkTestingMacros.h"
22+
23+
#include <iostream>
24+
25+
// Tests that DCMTKImageIO::ReadImageInformation() populates the metadata
26+
// dictionary with DICOM tag values using "GGGG|EEEE" keys (lowercase
27+
// matching GDCMImageIO behavior). Expected values are verified against
28+
// Input/DicomSeries/Image0075.dcm using the same fixture as
29+
// itkDCMTKGetDicomTagsTest.
30+
int
31+
itkDCMTKImageIOMetadataTest(int argc, char * argv[])
32+
{
33+
if (argc != 2)
34+
{
35+
std::cerr << "Missing parameters." << std::endl;
36+
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " <DicomImage>" << std::endl;
37+
return EXIT_FAILURE;
38+
}
39+
40+
auto dcmtkIO = itk::DCMTKImageIO::New();
41+
42+
ITK_TEST_EXPECT_TRUE(dcmtkIO->CanReadFile(argv[1]));
43+
dcmtkIO->SetFileName(argv[1]);
44+
ITK_TRY_EXPECT_NO_EXCEPTION(dcmtkIO->ReadImageInformation());
45+
46+
const itk::MetaDataDictionary & dict = dcmtkIO->GetMetaDataDictionary();
47+
48+
// Dictionary must be populated
49+
ITK_TEST_EXPECT_TRUE(!dict.GetKeys().empty());
50+
51+
// Pixel data (7FE0,0010) must NOT appear in the dictionary
52+
ITK_TEST_EXPECT_TRUE(!dict.HasKey("7fe0|0010"));
53+
54+
// Verify string-valued DICOM tags using the "GGGG|EEEE" key format.
55+
// Values are specific to the test file Input/DicomSeries/Image0075.dcm
56+
std::string value;
57+
58+
// (0008,0021) DA StudyDate
59+
ITK_TEST_EXPECT_TRUE(itk::ExposeMetaData<std::string>(dict, "0008|0021", value));
60+
ITK_TEST_EXPECT_EQUAL(value, std::string("20030625"));
61+
62+
// (0010,0010) PN PatientName
63+
ITK_TEST_EXPECT_TRUE(itk::ExposeMetaData<std::string>(dict, "0010|0010", value));
64+
ITK_TEST_EXPECT_EQUAL(value, std::string("Wes Turner"));
65+
66+
// (0010,0040) CS PatientSex
67+
ITK_TEST_EXPECT_TRUE(itk::ExposeMetaData<std::string>(dict, "0010|0040", value));
68+
ITK_TEST_EXPECT_EQUAL(value, std::string("O"));
69+
70+
// (0028,0004) CS PhotometricInterpretation
71+
ITK_TEST_EXPECT_TRUE(itk::ExposeMetaData<std::string>(dict, "0028|0004", value));
72+
ITK_TEST_EXPECT_EQUAL(value, std::string("MONOCHROME2"));
73+
74+
std::cout << "Test finished." << std::endl;
75+
return EXIT_SUCCESS;
76+
}

0 commit comments

Comments
 (0)