Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DICOM export in ImageJ #4215

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ else if (FormatTools.isSigned(originalType)) {
String lsid = MetadataTools.createLSID("Channel", 0, c);
store.setChannelID(lsid, 0, c);
}
store.setChannelSamplesPerPixel(new PositiveInteger(channels), 0, 0);
store.setChannelSamplesPerPixel(new PositiveInteger(channels), 0, c);

if (imp instanceof CompositeImage) {
luts[c] = ((CompositeImage) imp).getChannelLut(c + 1);
Expand Down
30 changes: 26 additions & 4 deletions components/formats-bsd/src/loci/formats/out/DicomWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ public void setId(String id) throws FormatException, IOException {
opticalSequence.children.add(illuminationTypeCodes);

DicomTag wavelength = new DicomTag(ILLUMINATION_WAVELENGTH, FL);
Length wave = r.getChannelEmissionWavelength(pyramid, c);
Length wave = fixUnits(r.getChannelEmissionWavelength(pyramid, c));
wavelength.value = new float[] {wave == null ? 1f : wave.value(UNITS.NM).floatValue()};
opticalSequence.children.add(wavelength);

Expand Down Expand Up @@ -1019,7 +1019,7 @@ public void setId(String id) throws FormatException, IOException {

DicomTag sliceThickness = new DicomTag(SLICE_THICKNESS, DS);
DicomTag sliceSpace = new DicomTag(SLICE_SPACING, DS);
Length physicalZ = r.getPixelsPhysicalSizeZ(pyramid);
Length physicalZ = fixUnits(r.getPixelsPhysicalSizeZ(pyramid));
if (physicalZ != null) {
sliceThickness.value = padString(String.valueOf(physicalZ.value(UNITS.MM)));
}
Expand All @@ -1032,8 +1032,8 @@ public void setId(String id) throws FormatException, IOException {
pixelMeasuresSequence.children.add(sliceSpace);

DicomTag pixelSpacing = new DicomTag(PIXEL_SPACING, DS);
Length physicalX = r.getPixelsPhysicalSizeX(pyramid);
Length physicalY = r.getPixelsPhysicalSizeY(pyramid);
Length physicalX = fixUnits(r.getPixelsPhysicalSizeX(pyramid));
Length physicalY = fixUnits(r.getPixelsPhysicalSizeY(pyramid));
String px = physicalX == null ? "1" : String.valueOf(physicalX.value(UNITS.MM));
String py = physicalY == null ? "1" : String.valueOf(physicalY.value(UNITS.MM));
pixelSpacing.value = padString(px + "\\" + py);
Expand Down Expand Up @@ -1383,6 +1383,11 @@ public void close() throws IOException {
for (int res=0; res<resolutionCount; res++) {
resolution = res;
openFile(pyramid, resolution);

if (out == null) {
// already closed
continue;
}
int resolutionIndex = getIndex(pyramid, resolution);

out.seek(out.length());
Expand Down Expand Up @@ -2071,6 +2076,23 @@ private short[] makeShortArray(int v) {
return s;
}

/**
* Check if the unit for the given Length is "pixel"
* or "referenceframe". These two units cannot be assigned to
* proper physical units (e.g. mm), so need to be handled specially.
*/
private Length fixUnits(Length size) {
if (size == null) {
return null;
}
if (size.unit() == UNITS.PIXEL || size.unit() == UNITS.REFERENCEFRAME) {
LOGGER.warn("Found physical length '{}' in relative units '{}'; this value will be lost",
size.value(), size.unit());
return null;
}
return size;
}

private TiffRational getPhysicalSize(Length size) {
if (size == null || size.value(UNITS.MICROMETER) == null) {
return new TiffRational(0, 1000);
Expand Down