Skip to content

feat: add a SecondaryCapture derivation - #627

Open
pieper wants to merge 1 commit into
0.xfrom
secondary-capture-derivation
Open

pieper wants to merge 1 commit into
0.xfrom
secondary-capture-derivation

Conversation

@pieper

@pieper pieper commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Relates to #85 and #333, both asking how to make a DICOM image from a JPEG. There is currently no way to build a Secondary Capture Image without assembling the dataset by hand — which is also what any screenshot, rendering or scanned document needs.

const sc = new SecondaryCapture([referenceDataset], { SeriesDescription: "Screen capture" });
sc.setPixels({ rows: 768, columns: 1024, pixelData: rgbBytes });
const buffer = datasetToDict(sc.dataset).write();

Why DerivedDataset and not DerivedPixels

The pixels are not derived from the reference — they are supplied by the caller, and nothing about the reference's pixel geometry, frame count or PixelData should be inherited. What the reference does give is the composite context, so the capture files alongside the images it relates to. There is a test asserting a reference with Rows: 512, NumberOfFrames: 100 and its own PixelData contributes none of that.

setPixels() writes the whole Image Pixel module and validates against it: samples per pixel is inferred from the data length when not given, and a length that does not match rows × columns × samples × bytes is rejected rather than written out as a malformed instance. Colour and grayscale, 8 and 16 bits. A view covering part of a larger buffer is copied out, so passing a subarray does not silently write the whole backing buffer.

Three choices worth review

  • BurnedInAnnotation defaults to "YES". A screen capture normally has patient-identifying text drawn into it, and a de-identifier that trusts a "NO" here will pass the pixels through untouched. Overridable for a capture that genuinely carries none. I would rather the safe default be the quiet one.
  • No FrameOfReferenceUID, ImagePositionPatient or ImageOrientationPatient, even when the reference has them. A Secondary Capture is a picture of a screen; inheriting those would invite a viewer to place it in patient space.
  • _meta.TransferSyntaxUID is pinned to Explicit VR Little Endian. This one is a trap rather than a preference: DerivedDataset copies _meta from the reference, and datasetToDict keeps whatever TransferSyntaxUID it finds there — so a reference read from an implicit VR file would silently make the capture implicit VR too. The test is built from an implicit-VR reference and fails if the pin is removed.

options.referencedSeries populates ReferencedSeriesSequence and ReferencedInstanceSequence (Common Instance Reference, PS3.3 C.12.2), so what a capture was made from is recorded in standard attributes rather than left implicit.

Tests

10 in test/secondaryCapture.test.js: the SC attribute set, burned-in annotation defaulting, context-without-pixels, absence of spatial registration, the implicit-VR case, byte-exact colour round trip, grayscale and 16-bit, the validation failures, referenced instances surviving a write and read, and a partial buffer view.

Full suite: 26 suites / 364 tests.

Disclosure

AI-assisted (Claude), per the process in #517. Written for a downstream project that needs a capture carrying a reserved private block (see #626), but the derivation itself is general and has no dependency on that.

There was no way to build a Secondary Capture Image without assembling the
dataset by hand - which is what #85 and #333 were both asking for, and
what any screenshot, rendering, scanned document or photograph needs.

It extends DerivedDataset rather than DerivedPixels, because the pixels
are NOT derived from the reference: they are supplied by the caller, and
nothing about the reference's pixel geometry, frame count or PixelData
should be inherited. What the reference does give is the composite
context, so the capture files alongside the images it relates to.

    const sc = new SecondaryCapture([referenceDataset], {
        SeriesDescription: "Screen capture"
    });
    sc.setPixels({ rows: 768, columns: 1024, pixelData: rgbBytes });
    const buffer = datasetToDict(sc.dataset).write();

setPixels() writes the whole Image Pixel module and validates against it:
samples per pixel is inferred from the data length when not given, and a
length that does not match rows x columns x samples x bytes is rejected
rather than written as a malformed instance. Colour and grayscale, 8 and
16 bits. A view covering part of a larger buffer is copied out, so passing
a subarray does not silently write the whole backing buffer.

Three choices worth noting:

- BurnedInAnnotation defaults to "YES". A screen capture normally has
  patient-identifying text drawn into it, and a de-identifier that trusts
  a "NO" here will pass the pixels through untouched. Overridable for a
  capture that genuinely carries none.
- No FrameOfReferenceUID, ImagePositionPatient or ImageOrientationPatient,
  even when the reference has them. A Secondary Capture is a picture of a
  screen; inheriting those would invite a viewer to place it in space.
- _meta.TransferSyntaxUID is pinned to Explicit VR Little Endian.
  DerivedDataset copies _meta from the reference and datasetToDict keeps
  whatever TransferSyntaxUID it finds there, so a reference read from an
  implicit VR file would otherwise make the capture implicit VR too. There
  is a test for that, built from an implicit VR reference.

options.referencedSeries populates ReferencedSeriesSequence and
ReferencedInstanceSequence (Common Instance Reference, PS3.3 C.12.2), so
what a capture was made from is recorded in standard attributes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit c49174c
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6ae2702cc4aa8732f579c2d0
😎 Deploy Preview https://deploy-preview-627--dcmjs2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

rows,
columns,
pixelData,
samplesPerPixel,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest deriving samplesPerPixel and photometric interpretation based on the pixelData provided. You should also be able to infer bitsAllocated that way.

const bytesPerSample = bitsAllocated / 8;
const samples =
samplesPerPixel ??
inferSamplesPerPixel(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see you do have the infer samples per pixel here.

@wayfarer3130 wayfarer3130 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The 1.0-beta work changes how the derivations occur, with a bunch of classes to allow transforming/creating classes based on various pre-configured values. I think that ultimately makes more sense than this one.
As well, there are some issues with how this creates the object:
Wrong VR for 16-bit pixels. DerivedDataset shares its VR map (_vrMap) with the reference dataset. A 16-bit capture made from an 8-bit reference whose pixel data is OB gets written as OB, which DICOM doesn't allow above 8 bits.
Image type contradicts the PR. It writes ImageType DERIVED but no source sequence, although the PR says the pixels aren't derived from the reference.
Wrong default for MONOCHROME1. PresentationLUTShape is always IDENTITY, and the photometric value isn't checked against the number of samples.
Leftover research defaults. It keeps DerivedDataset's ImageComments, ContentQualification, SeriesNumber and SeriesDescription defaults.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants