From ab8234f870221f6e56ddec8688a16de4d00f85e0 Mon Sep 17 00:00:00 2001 From: Melissa Linkert Date: Wed, 11 Sep 2024 11:59:00 -0500 Subject: [PATCH] Don't try to convert lengths with pixel/referenceframe units to physical units --- .../src/loci/formats/out/DicomWriter.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/components/formats-bsd/src/loci/formats/out/DicomWriter.java b/components/formats-bsd/src/loci/formats/out/DicomWriter.java index 4b26e2e8dc7..05d27e26de6 100644 --- a/components/formats-bsd/src/loci/formats/out/DicomWriter.java +++ b/components/formats-bsd/src/loci/formats/out/DicomWriter.java @@ -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); @@ -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))); } @@ -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); @@ -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);