Skip to content

Commit 45722f2

Browse files
committed
feat(server): add BitmapUpdate::sub()
Signed-off-by: Marc-André Lureau <[email protected]>
1 parent 171d3fe commit 45722f2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

crates/ironrdp-server/src/display.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,62 @@ pub struct BitmapUpdate {
9696
pub stride: usize,
9797
}
9898

99+
impl BitmapUpdate {
100+
/// Extracts a sub-region of the bitmap update.
101+
///
102+
/// # Parameters
103+
///
104+
/// - `x`: The x-coordinate of the top-left corner of the sub-region.
105+
/// - `y`: The y-coordinate of the top-left corner of the sub-region.
106+
/// - `width`: The width of the sub-region.
107+
/// - `height`: The height of the sub-region.
108+
///
109+
/// # Returns
110+
///
111+
/// An `Option` containing a new `BitmapUpdate` representing the sub-region if the specified
112+
/// dimensions are within the bounds of the original bitmap update, otherwise `None`.
113+
///
114+
/// # Example
115+
///
116+
/// ```
117+
/// # use core::num::NonZeroU16;
118+
/// # use bytes::Bytes;
119+
/// # use ironrdp_graphics::image_processing::PixelFormat;
120+
/// # use ironrdp_server::BitmapUpdate;
121+
/// let original = BitmapUpdate {
122+
/// x: 0,
123+
/// y: 0,
124+
/// width: NonZeroU16::new(100).unwrap(),
125+
/// height: NonZeroU16::new(100).unwrap(),
126+
/// format: PixelFormat::ARgb32,
127+
/// data: Bytes::from(vec![0; 40000]),
128+
/// stride: 400,
129+
/// };
130+
///
131+
/// let sub_region = original.sub(10, 10, NonZeroU16::new(50).unwrap(), NonZeroU16::new(50).unwrap());
132+
/// assert!(sub_region.is_some());
133+
/// ```
134+
#[must_use]
135+
pub fn sub(&self, x: u16, y: u16, width: NonZeroU16, height: NonZeroU16) -> Option<Self> {
136+
if x + width.get() > self.width.get() || y + height.get() > self.height.get() {
137+
None
138+
} else {
139+
let bpp = usize::from(self.format.bytes_per_pixel());
140+
let start = usize::from(y) * self.stride + usize::from(x) * bpp;
141+
let end = start + usize::from(height.get() - 1) * self.stride + usize::from(width.get()) * bpp;
142+
Some(Self {
143+
x: self.x + x,
144+
y: self.y + y,
145+
width,
146+
height,
147+
format: self.format,
148+
data: self.data.slice(start..end),
149+
stride: self.stride,
150+
})
151+
}
152+
}
153+
}
154+
99155
impl core::fmt::Debug for BitmapUpdate {
100156
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101157
f.debug_struct("BitmapUpdate")

0 commit comments

Comments
 (0)