Skip to content

Commit 6c962b6

Browse files
authored
fix: Handle non utf-8 characters in OME-XML (#141)
By using String::from_utf8_lossy. Changed to another sample OME-TIFF file with invalid utf-8 characters.
1 parent 5e6858c commit 6c962b6

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/metadata/reader.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,7 @@ async fn read_tag_value<F: MetadataFetch>(
620620
if let Some(first) = out.iter().position(|&b| b == 0) {
621621
out.truncate(first);
622622
}
623-
Ok(TagValue::Ascii(
624-
String::from_utf8(out).map_err(|err| AsyncTiffError::General(err.to_string()))?,
625-
))
623+
Ok(TagValue::Ascii(String::from_utf8_lossy(&out).into_owned()))
626624
}
627625
}
628626
}

tests/ome_tiff.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,40 @@ async fn open_remote_tiff(url: &str) -> TIFF {
2121

2222
#[tokio::test]
2323
async fn test_ome_tiff_single_channel() {
24-
let tiff =
25-
open_remote_tiff("https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/single-channel.ome.tif").await;
24+
let tiff = open_remote_tiff("https://cildata.crbs.ucsd.edu/media/images/40613/40613.tif").await;
2625

27-
assert_eq!(tiff.ifds().len(), 1);
26+
assert_eq!(tiff.ifds().len(), 3);
2827
let ifd = &tiff.ifds()[0];
2928

3029
assert_eq!(
3130
ifd.photometric_interpretation(),
3231
PhotometricInterpretation::BlackIsZero
3332
);
34-
assert_eq!(ifd.image_description(), Some("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- Warning: this comment is an OME-XML metadata block, which contains crucial dimensional parameters and other important metadata. Please edit cautiously (if at all), and back up the original data before doing so. For more information, see the OME-TIFF web site: http://www.openmicroscopy.org/site/support/ome-model/ome-tiff/. --><OME xmlns=\"http://www.openmicroscopy.org/Schemas/OME/2016-06\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" Creator=\"OME Bio-Formats 5.2.2\" UUID=\"urn:uuid:2bc2aa39-30d2-44ee-8399-c513492dd5de\" xsi:schemaLocation=\"http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd\"><Image ID=\"Image:0\" Name=\"single-channel.ome.tif\"><Pixels BigEndian=\"true\" DimensionOrder=\"XYZCT\" ID=\"Pixels:0\" SizeC=\"1\" SizeT=\"1\" SizeX=\"439\" SizeY=\"167\" SizeZ=\"1\" Type=\"int8\"><Channel ID=\"Channel:0:0\" SamplesPerPixel=\"1\"><LightPath/></Channel><TiffData FirstC=\"0\" FirstT=\"0\" FirstZ=\"0\" IFD=\"0\" PlaneCount=\"1\"><UUID FileName=\"single-channel.ome.tif\">urn:uuid:2bc2aa39-30d2-44ee-8399-c513492dd5de</UUID></TiffData></Pixels></Image></OME>"));
33+
assert_eq!(
34+
ifd.image_description(),
35+
Some(
36+
r##"<?xml version="1.0" encoding="UTF-8"?><!-- Warning: this comment is an OME-XML metadata block, which contains crucial dimensional parameters and other important metadata. Please edit cautiously (if at all), and back up the original data before doing so. For more information, see the OME-TIFF web site: http://loci.wisc.edu/ome/ome-tiff.html. -->
37+
<OME xmlns:AML="http://www.openmicroscopy.org/Schemas/AnalysisModule/2008-09" xmlns:Bin="http://www.openmicroscopy.org/Schemas/BinaryFile/2008-09" xmlns:MLI="http://www.openmicroscopy.org/Schemas/MLI/2008-09" xmlns:SA="http://www.openmicroscopy.org/Schemas/SA/2008-09" xmlns:OME="http://www.openmicroscopy.org/Schemas/OME/2008-09" xmlns:SPW="http://www.openmicroscopy.org/Schemas/SPW/2008-09" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:CA="http://www.openmicroscopy.org/Schemas/CA/2008-09" xmlns:STD="http://www.openmicroscopy.org/Schemas/STD/2008-09" xmlns:CLI="http://www.openmicroscopy.org/Schemas/CLI/2008-09" xmlns="http://www.openmicroscopy.org/Schemas/OME/2008-09" xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2008-09 http://www.openmicroscopy.org/Schemas/OME/2008-09/ome.xsd http://www.openmicroscopy.org/Schemas/BinaryFile/2008-09 http://www.openmicroscopy.org/Schemas/BinaryFile/2008-09/BinaryFile.xsd http://www.openmicroscopy.org/Schemas/CA/2008-09 http://www.openmicroscopy.org/Schemas/CA/2008-09/CA.xsd http://www.openmicroscopy.org/Schemas/SPW/2008-09 http://www.openmicroscopy.org/Schemas/SPW/2008-09/SPW.xsd http://www.openmicroscopy.org/Schemas/STD/2008-09 http://www.openmicroscopy.org/Schemas/STD/2008-09/STD.xsd" UUID="3af39f55-0ac0-431a-bc60-8f9c3e782b85">
38+
<Experimenter ID="urn:lsid:export.openmicroscopy.org:Experimenter:46be26c5-9fcf-497e-a913-48513759f00b_3"/>
39+
<Group ID="urn:lsid:export.openmicroscopy.org:ExperimenterGroup:46be26c5-9fcf-497e-a913-48513759f00b_54:3259518"/>
40+
<Image DefaultPixels="urn:lsid:export.openmicroscopy.org:Pixels:46be26c5-9fcf-497e-a913-48513759f00b_40613:24177120" ID="urn:lsid:export.openmicroscopy.org:Image:46be26c5-9fcf-497e-a913-48513759f00b_40613:25467732" Name="IM_20100715_Nikitina_3_Niki_6_004_001_2_����������_lg.jpg">
41+
<CreationDate>2012-03-25 21:26:29.0</CreationDate>
42+
<ExperimenterRef ID="urn:lsid:export.openmicroscopy.org:Experimenter:46be26c5-9fcf-497e-a913-48513759f00b_3"/>
43+
<GroupRef ID="urn:lsid:export.openmicroscopy.org:ExperimenterGroup:46be26c5-9fcf-497e-a913-48513759f00b_54:3259518"/>
44+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81798:24177119" Name="Red" SamplesPerPixel="1"/>
45+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81799:24177119" Name="Green" SamplesPerPixel="1"/>
46+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81800:24177119" Name="Blue" SamplesPerPixel="1"/>
47+
<Pixels BigEndian="true" DimensionOrder="XYCZT" ID="urn:lsid:export.openmicroscopy.org:Pixels:46be26c5-9fcf-497e-a913-48513759f00b_40613:24177120" PixelType="uint8" SizeC="3" SizeT="1" SizeX="1024" SizeY="943" SizeZ="1">
48+
<TiffData>
49+
<UUID FileName="__omero_export__2069104425008571311.ome.tiff">3af39f55-0ac0-431a-bc60-8f9c3e782b85</UUID>
50+
</TiffData>
51+
</Pixels>
52+
</Image>
53+
</OME>
54+
"##
55+
)
56+
);
3557

3658
assert!(ifd.bits_per_sample().iter().all(|x| *x == 8));
59+
assert_eq!(ifd.software(), Some("LOCI Bio-Formats"));
3760
}

0 commit comments

Comments
 (0)