Simplify layout structs
This commit is contained in:
1 parent
dce1c40bd5
commit
6439880e3d
2 files changed
+12
-12
No files matched your search
@@ -30,10 +30,8 @@ pub(crate) struct TileAttributes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct PlaneLayout {
|
pub(crate) struct PlaneLayout {
|
||||||
/// Buffer offset from one row of the image to its next, in bytes.
|
/// The underlying readout layout.
|
||||||
pub output_row_stride: usize,
|
pub readout: ReadoutLayout,
|
||||||
/// Number of bytes in a plane.
|
|
||||||
pub plane_stride: usize,
|
|
||||||
/// Buffer offset from one plane of output to the next.
|
/// Buffer offset from one plane of output to the next.
|
||||||
pub plane_offsets: Vec<usize>,
|
pub plane_offsets: Vec<usize>,
|
||||||
/// Total number of bytes for all planes in given order.
|
/// Total number of bytes for all planes in given order.
|
||||||
@@ -99,6 +97,7 @@ pub(crate) struct Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Describes how to read a tile-aligned portion of the image.
|
/// Describes how to read a tile-aligned portion of the image.
|
||||||
|
#[derive(Clone)]
|
||||||
pub(crate) struct ReadoutLayout {
|
pub(crate) struct ReadoutLayout {
|
||||||
/// The planar configuration, which applies to both the underlying image and the output buffer.
|
/// The planar configuration, which applies to both the underlying image and the output buffer.
|
||||||
/// This may be relaxed if we find a clean enough way to provide it.
|
/// This may be relaxed if we find a clean enough way to provide it.
|
||||||
@@ -1040,10 +1039,9 @@ impl ReadoutLayout {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ok(PlaneLayout {
|
Ok(PlaneLayout {
|
||||||
output_row_stride: self.row_stride,
|
|
||||||
plane_stride: self.plane_stride,
|
|
||||||
plane_offsets,
|
plane_offsets,
|
||||||
total_bytes,
|
total_bytes,
|
||||||
|
readout: self.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+8
-6
@@ -226,10 +226,10 @@ pub struct BufferLayoutPreference {
|
|||||||
impl BufferLayoutPreference {
|
impl BufferLayoutPreference {
|
||||||
fn from_planes(layout: &image::PlaneLayout) -> Self {
|
fn from_planes(layout: &image::PlaneLayout) -> Self {
|
||||||
BufferLayoutPreference {
|
BufferLayoutPreference {
|
||||||
len: layout.plane_stride,
|
len: layout.readout.plane_stride,
|
||||||
row_stride: core::num::NonZeroUsize::new(layout.output_row_stride),
|
row_stride: core::num::NonZeroUsize::new(layout.readout.row_stride),
|
||||||
planes: layout.plane_offsets.len(),
|
planes: layout.plane_offsets.len(),
|
||||||
plane_stride: core::num::NonZeroUsize::new(layout.plane_stride),
|
plane_stride: core::num::NonZeroUsize::new(layout.readout.plane_stride),
|
||||||
complete_len: layout.total_bytes,
|
complete_len: layout.total_bytes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1299,22 +1299,24 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
let readout = self.image().readout_for_image()?;
|
let readout = self.image().readout_for_image()?;
|
||||||
|
|
||||||
let ref layout @ image::PlaneLayout {
|
let ref layout @ image::PlaneLayout {
|
||||||
output_row_stride: _,
|
|
||||||
plane_stride,
|
|
||||||
ref plane_offsets,
|
ref plane_offsets,
|
||||||
// We assume that is correct, so really it can be ignored.
|
// We assume that is correct, so really it can be ignored.
|
||||||
total_bytes: _,
|
total_bytes: _,
|
||||||
|
ref readout,
|
||||||
} = readout.to_plane_layout()?;
|
} = readout.to_plane_layout()?;
|
||||||
|
|
||||||
let preference = BufferLayoutPreference::from_planes(layout);
|
let preference = BufferLayoutPreference::from_planes(layout);
|
||||||
preference.assert_min_layout(buffer)?;
|
preference.assert_min_layout(buffer)?;
|
||||||
|
|
||||||
|
// Note: with differently sized planes this is dependent on the plane.
|
||||||
|
let last_plane_start = buffer.len().checked_sub(readout.plane_stride);
|
||||||
|
|
||||||
// Find how many planes fit into the output buffer.
|
// Find how many planes fit into the output buffer.
|
||||||
let used_plane_offsets = plane_offsets
|
let used_plane_offsets = plane_offsets
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
// Find the first plane that would not fit completely at its offset.
|
// Find the first plane that would not fit completely at its offset.
|
||||||
.skip_while(|(_, &offset)| buffer.len().checked_sub(plane_stride) >= Some(offset))
|
.skip_while(|(_, &offset)| last_plane_start >= Some(offset))
|
||||||
.nth(0)
|
.nth(0)
|
||||||
// If all planes fit, use all of them.
|
// If all planes fit, use all of them.
|
||||||
.map_or(plane_offsets.len(), |(idx, _)| idx);
|
.map_or(plane_offsets.len(), |(idx, _)| idx);
|
||||||
|
|||||||
Reference in new issue
Block a user