Skip to content

Commit 52bda55

Browse files
committed
fix: Handle non utf-8 characters in OME-XML
By using String::from_utf8_lossy. Changed to another sample OME-TIFF file, and increased prefetch bytes to more than 32kb to avoid hanging when parsing the StripOffsets tag.
1 parent d41d111 commit 52bda55

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/metadata/reader.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,7 @@ async fn read_tag_value<F: MetadataFetch>(
614614
if let Some(first) = out.iter().position(|&b| b == 0) {
615615
out.truncate(first);
616616
}
617-
Ok(Value::Ascii(
618-
String::from_utf8(out).map_err(|err| AsyncTiffError::General(err.to_string()))?,
619-
))
617+
Ok(Value::Ascii(String::from_utf8_lossy(&out).into_owned()))
620618
}
621619
}
622620
}

tests/ome_tiff.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use async_tiff::tiff::tags::PhotometricInterpretation;
88
use async_tiff::TIFF;
99
use reqwest::Url;
1010

11-
async fn open_remote_tiff(url: &str) -> TIFF {
11+
async fn open_remote_tiff(url: &str, prefetch_bytes: u64) -> TIFF {
1212
let parsed_url = Url::parse(url).expect("failed parsing url");
1313
let (store, path) = object_store::parse_url(&parsed_url).unwrap();
1414

1515
let reader = Arc::new(ObjectReader::new(Arc::new(store), path)) as Arc<dyn AsyncFileReader>;
16-
let prefetch_reader = PrefetchBuffer::new(reader.clone(), 32 * 1024)
16+
let prefetch_reader = PrefetchBuffer::new(reader.clone(), prefetch_bytes)
1717
.await
1818
.unwrap();
1919
let mut metadata_reader = TiffMetadataReader::try_open(&prefetch_reader)
@@ -28,17 +28,44 @@ async fn open_remote_tiff(url: &str) -> TIFF {
2828

2929
#[tokio::test]
3030
async fn test_ome_tiff_single_channel() {
31-
let tiff =
32-
open_remote_tiff("https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/single-channel.ome.tif").await;
31+
let tiff = open_remote_tiff(
32+
"https://cildata.crbs.ucsd.edu/media/images/40613/40613.tif",
33+
32 * 128 * 1024,
34+
)
35+
.await;
3336

34-
assert_eq!(tiff.ifds().len(), 1);
37+
assert_eq!(tiff.ifds().len(), 3);
3538
let ifd = &tiff.ifds()[0];
3639

3740
assert_eq!(
3841
ifd.photometric_interpretation(),
3942
PhotometricInterpretation::BlackIsZero
4043
);
41-
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>"));
44+
assert_eq!(
45+
ifd.image_description(),
46+
Some(
47+
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. -->
48+
<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">
49+
<Experimenter ID="urn:lsid:export.openmicroscopy.org:Experimenter:46be26c5-9fcf-497e-a913-48513759f00b_3"/>
50+
<Group ID="urn:lsid:export.openmicroscopy.org:ExperimenterGroup:46be26c5-9fcf-497e-a913-48513759f00b_54:3259518"/>
51+
<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">
52+
<CreationDate>2012-03-25 21:26:29.0</CreationDate>
53+
<ExperimenterRef ID="urn:lsid:export.openmicroscopy.org:Experimenter:46be26c5-9fcf-497e-a913-48513759f00b_3"/>
54+
<GroupRef ID="urn:lsid:export.openmicroscopy.org:ExperimenterGroup:46be26c5-9fcf-497e-a913-48513759f00b_54:3259518"/>
55+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81798:24177119" Name="Red" SamplesPerPixel="1"/>
56+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81799:24177119" Name="Green" SamplesPerPixel="1"/>
57+
<LogicalChannel ID="urn:lsid:export.openmicroscopy.org:LogicalChannel:46be26c5-9fcf-497e-a913-48513759f00b_81800:24177119" Name="Blue" SamplesPerPixel="1"/>
58+
<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">
59+
<TiffData>
60+
<UUID FileName="__omero_export__2069104425008571311.ome.tiff">3af39f55-0ac0-431a-bc60-8f9c3e782b85</UUID>
61+
</TiffData>
62+
</Pixels>
63+
</Image>
64+
</OME>
65+
"##
66+
)
67+
);
4268

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

0 commit comments

Comments
 (0)