impl PlainImage { /// Create a plain image from bytes. pubfn new<T: Into<Box<[u8]>>>(data: T) -> Result<Self> { let data: Box<[u8]> = data.into(); if data.len() < MIN_IMAGE_SIZE {
bail!( "Invalid image: Only {} bytes; must be >= {} bytes",
data.as_ref().len(),
MIN_IMAGE_SIZE
);
}
let image = PlainImage { data: data.into() };
if image.image_type() != 0 {
bail!("Invalid image: image type ({}) != 0", image.image_type());
}
Ok(image)
}
/// Read a plain image from a local file path. pubfn read(path: impl AsRef<Path>) -> Result<Self> { let data = std::fs::read(path)?; let image = PlainImage::new(data)?;
// For now, only check this in read(), since new() might be used with partial images (just // the header). if image.data().len() != image.image_length() {
bail!( "Invalid image: actual size ({}) doesn't match size in header ({})",
image.data().len(),
image.image_length()
);
}
Ok(image)
}
pubfn data(&self) -> &[u8] {
&self.data
}
/// Get the value of a vector from the vector table. pubfn get_vector(&self, n: usize) -> Result<u32> { let offset: usize = n * size_of::<u32>(); let bytes = self
.data
.get(offset..offset + 4)
.ok_or_else(|| anyhow!("Invalid vector number: {n}"))?;
Ok(u32::from_le_bytes(bytes.try_into().unwrap()))
}
// Read that image. It should fail (too small). let result = PlainImage::read(temp_file.path()); let error = result.err().unwrap(); let root = error.root_cause();
assert!(format!("{root}").starts_with("Invalid image: Only"));
// Read that image. It should fail (bad type). let result = PlainImage::read(temp_file.path()); let error = result.err().unwrap(); let root = error.root_cause();
assert!(format!("{root}").starts_with("Invalid image: image type"));
// Read that image. It should fail (bad size). let result = PlainImage::read(temp_file.path()); let error = result.err().unwrap(); let root = error.root_cause();
assert!(format!("{root}").starts_with("Invalid image: actual size"));
Ok(())
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.