Skip to content

BUG: Make unit DICOM foreground values opt-in - #7175

Open
ayaanrustagi wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
ayaanrustagi:scanfuzz/binary-seg-labels
Open

ayaanrustagi wants to merge 2 commits into
InsightSoftwareConsortium:mainfrom
ayaanrustagi:scanfuzz/binary-seg-labels

Conversation

@ayaanrustagi

@ayaanrustagi ayaanrustagi commented Nov 15, 2026 •

Copy link
Copy Markdown

Add an opt-in foreground value for single-bit DICOM reads. Preserve the existing default of 255 and generate the SEG regression input in the test.

Behavior and tests

Set GDCMImageIO::SetSingleBitForegroundValue(1) to read the set bits as 1. This changes their magnitude only. It does not recover individual segment IDs in multisegment SEG objects.

The regression test generates single-frame and multiframe SEG inputs with partial final bytes. It checks every output pixel in the default and opt-in modes. The existing one-bit compliance image remains the compatibility test for the default. The generated fixture removes the need for a new ExternalData object. All eight GDCMImageIO GoogleTests and the existing single-bit compliance test passed locally.

Addresses the foreground-value portion of #6641. Labelmap opening and orientation failures are separate.

AI assistance

Codex helped implement the changes and run the automated checks. The PR remains a draft while human verification and maintainer review are pending.

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:IO Issues affecting the IO module labels Nov 15, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing a pull request! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜
More support and guidance on the contribution process can be found in our contributing guide. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the pull request and comment
on it.

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down. Two CI failures and a design question block this for now.

  1. The existing itkGDCM_ComplianceTest_singlebit fails (ARMBUILD-Ubuntu-24.04-arm). Its input Input/single-bit.dcm is already a Segmentation Storage object (SOP class 1.2.840.10008.5.1.4.1.1.66.4, BINARY, 128x128x16), and Baseline/single-bit.mha stores 255 for its foreground. With this change 4089 voxels read as 1, a difference of 254 each. The "existing one-bit cases pass" note in the PR body covers only the two GTests, which write non-SEG (OT) files.
  2. The new fixture is not reachable from any ExternalData mirror, so the ExternalData build step fails, ITKIOGDCMGTestDriver is not built, and the remaining *_NOT_BUILT failures on the other jobs follow from that. The test file can build the SEG itself, see the inline comment, which removes the hosting question entirely.
  3. Silent behavior change. Code that reads a BINARY SEG through GDCMImageIO (ITK, SimpleITK ReadImage, itk.imread) and thresholds or compares against 255 keeps compiling and silently gets an empty mask. Could this be opt-in on GDCMImageIO, default 255, so existing results and the compliance baseline stay valid? If the default should change, that needs a stated downstream impact, an ITKv6 migration-guide entry, a commit body explaining it, and a new single-bit.mha baseline.

Not addressed here, for the record: in a multi-segment BINARY SEG every segment still reads as 1, so per-segment identity is not recovered either way.

Comment thread Modules/IO/GDCM/src/itkGDCMImageIO.cxx Outdated
gdcm::MediaStorage mediaStorage;
mediaStorage.SetFromFile(reader.GetFile());
// Binary segmentation foreground represents label 1 (DICOM PS3.3 C.8.20.2).
const unsigned char foreground = mediaStorage == gdcm::MediaStorage::SegmentationStorage ? 1 : 255;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should-fix: this flips every existing BINARY SEG read from 255 to 1, including the one itkGDCM_ComplianceTest_singlebit checks against Baseline/single-bit.mha (fails in CI). Suggest an opt-in instead, e.g. a SetSingleBitForegroundValue(unsigned char) or a boolean for SEG, default 255, so existing results do not change. Also, ReadImageInformation already resolves gdcm::MediaStorage (around line 657); deciding the foreground there next to m_SingleBit avoids a second SetFromFile per Read() (nit).

Comment thread Modules/IO/GDCM/src/itkGDCMImageIO.cxx Outdated
// used when `len` is not a multiple of 8.
gdcm::MediaStorage mediaStorage;
mediaStorage.SetFromFile(reader.GetFile());
// Binary segmentation foreground represents label 1 (DICOM PS3.3 C.8.20.2).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in a BINARY SEG a set bit means "inside the segment this frame's Referenced Segment Number names", not label 1, and a multi-segment SEG still reads every segment as 1. Something like // BINARY Segmentation Storage stores segment presence as 1 (PS3.3 C.8.20.2.1). is more accurate. These four lines also landed between the PS3.5 bit-packing comment and the loop it describes; moving them above that comment keeps the two together.


TEST_F(ITKGDCMImageIO, BinarySegmentationStoragePreservesUnitForeground)
{
constexpr size_t width = 16;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should-fix: the hosted fixture can be replaced by an in-test SEG. WriteSingleBitDicom (and WriteMultiFrameSingleBitDicom) can take an optional SOP class and set (0008,0016); gdcm::Writer copies it into (0002,0002) when writing, and MediaStorage::SetFromFile prefers the dataset value anyway. Sketch (not compiled):

// helper: extra parameter, after the pixelRepresentation Replace
gdcm::MediaStorage::MSType sopClass = gdcm::MediaStorage::MS_END
...
if (sopClass != gdcm::MediaStorage::MS_END)
{
  gdcm::Attribute<0x0008, 0x0016> sopClassUid;
  sopClassUid.SetValue(gdcm::MediaStorage::GetMSString(sopClass));
  ds.Replace(sopClassUid.GetAsDataElement());
}

Then use sizes that are not a multiple of 8 (e.g. 9x3, and 12x13x8 multi-frame): 16x16 never reaches the remainder loop that this PR also changed. With pixels synthesized as 0/1, the expected value is the input itself, which also replaces the hard-coded index list on line 707 that cannot be checked from the repo. The existing tests at lines 605 and 766 keep pinning the non-SEG 255 path.

Comment thread Modules/IO/GDCM/test/CMakeLists.txt Outdated
_DICOM_SERIES_INPUT
"DATA{${ITK_DATA_ROOT}/Input/DicomSeries/,REGEX:Image[0-9]+.dcm}"
)
ExternalData_Expand_Arguments(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should-fix: with the SEG built in-test, this ExternalData_Expand_Arguments block, the BINARY_SEG_INPUT definition, Input/SyntheticBinarySegmentation.dcm.cid, and the content-links.manifest line can all go (they have to go together; a DATA{} reference without its .cid breaks configure).

@ayaanrustagi ayaanrustagi changed the title BUG: Preserve binary DICOM segmentation label values BUG: Make unit DICOM foreground values opt-in Nov 15, 2026
@ayaanrustagi

Copy link
Copy Markdown
Author

Thanks for identifying the compatibility test. The latest commit keeps the default foreground value at 255 and adds SetSingleBitForegroundValue(1) as an opt-in. The regression now generates its single-frame and multiframe SEG inputs in the test. It checks every pixel in both modes, including the bits in the partial final byte.

All eight GDCMImageIO GoogleTests and itkGDCM_ComplianceTest_singlebit passed locally. The PR is still a draft for human verification.

@github-actions github-actions Bot removed the type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots label Nov 15, 2026
@hjmjohnson
hjmjohnson self-requested a review November 16, 2026 03:50

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a better solution than your original hard-coding. It is more backward compatible for those who depend on the old behavior.

@hjmjohnson
hjmjohnson self-requested a review November 16, 2026 03:52
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 16, 2026 03:52
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds a caller-selected foreground value for one-bit DICOM reads while keeping the default at 255.

  • One-bit DICOM reads can use 1 for set pixels when callers opt in.

No actionable issues found. Tests were not run during this review.

Reviews (1) · Last reviewed commit: "BUG: Make unit DICOM foreground values o..." · Reviewed by Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:IO Issues affecting the IO module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants