use ::image::ImageBuffer;
pub use ::image::{DynamicImage, ImageFormat};
use anyhow::{Context, Result, bail, ensure};
pub use ros2_interfaces_jazzy_rkyv::{builtin_interfaces::msg::Time, std_msgs::msg::Header};
pub trait ImageMessageExt: Sized {
fn header_mut(&mut self) -> &mut Header;
fn with_header(mut self, header: Header) -> Self {
*self.header_mut() = header;
self
}
fn with_frame_id(mut self, frame_id: impl Into<String>) -> Self {
self.header_mut().frame_id = frame_id.into();
self
}
fn with_stamp(mut self, stamp: Time) -> Self {
self.header_mut().stamp = stamp;
self
}
}
impl ImageMessageExt for crate::Image {
fn header_mut(&mut self) -> &mut Header {
&mut self.header
}
}
impl ImageMessageExt for crate::CompressedImage {
fn header_mut(&mut self) -> &mut Header {
&mut self.header
}
}
pub trait ToImage {
fn to_image(&self) -> Result<DynamicImage>;
}
impl ToImage for crate::Image {
fn to_image(&self) -> Result<DynamicImage> {
raw_image(
self.width,
self.height,
&self.encoding,
self.step,
self.is_bigendian,
&self.data,
)
}
}
pub fn archived_image_to_image(message: &rkyv::Archived<crate::Image>) -> Result<DynamicImage> {
raw_image(
message.width.to_native(),
message.height.to_native(),
message.encoding.as_str(),
message.step.to_native(),
message.is_bigendian,
message.data.as_slice(),
)
}
impl ToImage for crate::CompressedImage {
fn to_image(&self) -> Result<DynamicImage> {
compressed_image(&self.format, &self.data)
}
}
pub fn archived_compressed_image_to_image(
message: &rkyv::Archived<crate::CompressedImage>,
) -> Result<DynamicImage> {
compressed_image(message.format.as_str(), message.data.as_slice())
}
pub fn image_from_dynamic(pixels: &DynamicImage) -> Result<crate::Image> {
let (encoding, pixel_bytes, sixteen_bit) = match pixels {
DynamicImage::ImageLuma8(_) => ("mono8", 1, false),
DynamicImage::ImageRgb8(_) => ("rgb8", 3, false),
DynamicImage::ImageRgba8(_) => ("rgba8", 4, false),
DynamicImage::ImageLuma16(_) => ("mono16", 2, true),
DynamicImage::ImageRgb16(_) => ("rgb16", 6, true),
DynamicImage::ImageRgba16(_) => ("rgba16", 8, true),
_ => bail!(
"Unsupported DynamicImage color type for raw ROS encoding: {:?}",
pixels.color()
),
};
ensure!(
pixels.width() > 0 && pixels.height() > 0,
"Image dimensions must be nonzero"
);
let step = pixels
.width()
.checked_mul(pixel_bytes)
.context("ROS image step overflows")?;
Ok(crate::Image {
header: Default::default(),
width: pixels.width(),
height: pixels.height(),
encoding: encoding.into(),
is_bigendian: u8::from(sixteen_bit && cfg!(target_endian = "big")),
step,
data: pixels.as_bytes().to_vec(),
})
}
pub fn compressed_image_from_dynamic(
pixels: &DynamicImage,
format: ::image::ImageFormat,
) -> Result<crate::CompressedImage> {
let codec = match format {
::image::ImageFormat::Jpeg => "jpeg",
::image::ImageFormat::Png => "png",
_ => bail!("Compressed ROS image encoding supports JPEG and PNG"),
};
ensure!(
pixels.width() > 0 && pixels.height() > 0,
"Image dimensions must be nonzero"
);
let mut output = std::io::Cursor::new(Vec::new());
::image::write_buffer_with_format(
&mut output,
pixels.as_bytes(),
pixels.width(),
pixels.height(),
pixels.color(),
format,
)
.context("Cannot encode compressed image")?;
Ok(crate::CompressedImage {
header: Default::default(),
format: codec.into(),
data: output.into_inner(),
})
}
fn compressed_image(format: &str, data: &[u8]) -> Result<DynamicImage> {
ensure!(
!format.to_ascii_lowercase().contains("compresseddepth"),
"compressedDepth images require depth transport decoding"
);
::image::ImageReader::new(std::io::Cursor::new(data))
.with_guessed_format()
.context("Cannot identify compressed image format")?
.decode()
.context("Cannot decode compressed image")
}
fn raw_image(
width: u32,
height: u32,
encoding: &str,
step: u32,
bigendian: u8,
data: &[u8],
) -> Result<DynamicImage> {
let (channels, sample_bytes, bgr) = match encoding {
"mono8" | "8UC1" => (1, 1, false),
"rgb8" => (3, 1, false),
"bgr8" => (3, 1, true),
"rgba8" => (4, 1, false),
"bgra8" => (4, 1, true),
"mono16" | "16UC1" => (1, 2, false),
"rgb16" => (3, 2, false),
"bgr16" => (3, 2, true),
"rgba16" => (4, 2, false),
"bgra16" => (4, 2, true),
_ => bail!("Unsupported ROS image encoding: {encoding}"),
};
ensure!(width > 0 && height > 0, "Image dimensions must be nonzero");
ensure!(bigendian <= 1, "Image is_bigendian must be 0 or 1");
let row_bytes = usize::try_from(width)?
.checked_mul(channels * sample_bytes)
.context("Image row size overflows")?;
let stride = usize::try_from(step)?;
ensure!(
stride >= row_bytes,
"Image step {step} is smaller than row size {row_bytes}"
);
let expected = stride
.checked_mul(usize::try_from(height)?)
.context("Image data size overflows")?;
ensure!(
data.len() == expected,
"Image data has {} bytes, expected {expected}",
data.len()
);
let packed_len = row_bytes
.checked_mul(usize::try_from(height)?)
.context("Packed image size overflows")?;
let mut packed = Vec::new();
packed
.try_reserve_exact(packed_len)
.context("Cannot allocate image pixels")?;
for row in data.chunks_exact(stride) {
packed.extend_from_slice(&row[..row_bytes]);
}
macro_rules! build {
($pixels:expr, $mono:ident, $rgb:ident, $rgba:ident) => {{
match channels {
1 => DynamicImage::$mono(
ImageBuffer::from_raw(width, height, $pixels)
.context("Invalid grayscale image dimensions")?,
),
3 => DynamicImage::$rgb(
ImageBuffer::from_raw(width, height, $pixels)
.context("Invalid RGB image dimensions")?,
),
4 => DynamicImage::$rgba(
ImageBuffer::from_raw(width, height, $pixels)
.context("Invalid RGBA image dimensions")?,
),
_ => unreachable!(),
}
}};
}
if sample_bytes == 1 {
if bgr {
for pixel in packed.chunks_exact_mut(channels) {
pixel.swap(0, 2);
}
}
Ok(build!(packed, ImageLuma8, ImageRgb8, ImageRgba8))
} else {
let mut samples: Vec<u16> = packed
.as_chunks::<2>()
.0
.iter()
.map(|&bytes| {
if bigendian == 1 {
u16::from_be_bytes(bytes)
} else {
u16::from_le_bytes(bytes)
}
})
.collect();
if bgr {
for pixel in samples.chunks_exact_mut(channels) {
pixel.swap(0, 2);
}
}
Ok(build!(samples, ImageLuma16, ImageRgb16, ImageRgba16))
}
}
#[cfg(test)]
mod tests;