Skip to content

Commit

Permalink
Don't try to convert lengths with pixel/referenceframe units to physi…
Browse files Browse the repository at this point in the history
…cal units
  • Loading branch information
melissalinkert committed Sep 11, 2024
1 parent cd22cb9 commit ab8234f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 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 @@ -2076,6 +2076,18 @@ 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 || size.unit() == UNITS.PIXEL || size.unit() == UNITS.REFERENCEFRAME) {
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 ab8234f

Please sign in to comment.