Skip to content

Commit

Permalink
Merge pull request #4215 from melissalinkert/fix-dicom-export-imagej
Browse files Browse the repository at this point in the history
Fix DICOM export in ImageJ
  • Loading branch information
sbesson authored Sep 13, 2024
2 parents 1f03e09 + c7e9161 commit e431517
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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

0 comments on commit e431517

Please sign in to comment.