1#[cfg(verus_keep_ghost)]
2use super::colour_spec;
3use crate::bitmap::CombinationOperator;
4use crate::{Bitmap, DecodeError, LimitKind, Limits, MalformedError, UnsupportedFeature};
5#[cfg(verus_keep_ghost)]
6use vstd::prelude::*;
7
8#[derive(Debug)]
13#[cfg_attr(verus_keep_ghost, verus_verify)]
14pub struct RgbBitmap {
15 pub(crate) width: u32,
16 pub(crate) height: u32,
17 pub(crate) stride: usize,
18 pub(crate) bytes: Vec<u8>,
19}
20
21impl RgbBitmap {
22 #[must_use]
24 #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out == colour_spec::width(self)))]
25 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
26 pub const fn width(&self) -> u32 {
27 #[cfg(verus_keep_ghost)]
28 proof! { colour_spec::bitmap_fields(self); }
29 self.width
30 }
31
32 #[must_use]
34 #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out == colour_spec::height(self)))]
35 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
36 pub const fn height(&self) -> u32 {
37 #[cfg(verus_keep_ghost)]
38 proof! { colour_spec::bitmap_fields(self); }
39 self.height
40 }
41
42 #[must_use]
44 #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out == colour_spec::stride(self)))]
45 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
46 pub const fn stride(&self) -> usize {
47 #[cfg(verus_keep_ghost)]
48 proof! { colour_spec::bitmap_fields(self); }
49 self.stride
50 }
51
52 #[must_use]
54 #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out@ == colour_spec::bytes(self)))]
55 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
56 pub fn bytes(&self) -> &[u8] {
57 #[cfg(verus_keep_ghost)]
58 proof! { colour_spec::bitmap_fields(self); }
59 &self.bytes
60 }
61
62 #[must_use]
64 #[cfg_attr(verus_keep_ghost, verus_spec(out =>
65 requires colour_spec::storage_valid(self),
66 ensures out == colour_spec::pixel(self, x, y),
67 ))]
68 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
69 pub fn pixel(&self, x: u32, y: u32) -> Option<[u8; 3]> {
70 #[cfg(verus_keep_ghost)]
71 proof! { colour_spec::bitmap_fields(self); }
72 if x >= self.width || y >= self.height {
73 return None;
74 }
75 #[cfg(verus_keep_ghost)]
76 proof! { colour_spec::pixel_offset_bounds(self, x, y); }
77 let offset = usize::try_from(y)
78 .ok()?
79 .checked_mul(self.stride)?
80 .checked_add(usize::try_from(x).ok()?.checked_mul(3)?)?;
81 Some([
82 *self.bytes.get(offset)?,
83 *self.bytes.get(offset + 1)?,
84 *self.bytes.get(offset + 2)?,
85 ])
86 }
87
88 #[cfg_attr(verus_keep_ghost, verus_spec(out =>
89 ensures match out {
90 Ok(bitmap) => colour_spec::storage_valid(&bitmap)
91 && bitmap.width == width && bitmap.height == height
92 && bitmap.bytes@.len() <= limits.max_work_units
93 && forall|index: int| 0 <= index < bitmap.bytes@.len() ==>
94 bitmap.bytes@[index] == if black { 0u8 } else { 255u8 },
95 Err(_) => true,
96 },
97 ))]
98 #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
99 pub(super) fn filled(
100 width: u32,
101 height: u32,
102 black: bool,
103 limits: &Limits,
104 ) -> Result<Self, DecodeError> {
105 crate::segment_data::enforce_dimension_limits(width, height, limits)?;
106 let stride = usize::try_from(width)
107 .map_err(|_| MalformedError::IntegerOverflow)?
108 .checked_mul(3)
109 .ok_or(MalformedError::IntegerOverflow)?;
110 let size = stride
111 .checked_mul(usize::try_from(height).map_err(|_| MalformedError::IntegerOverflow)?)
112 .ok_or(MalformedError::IntegerOverflow)?;
113 let requested = u64::try_from(size).map_err(|_| MalformedError::IntegerOverflow)?;
114 super::document::limit(LimitKind::Work, requested, limits.max_work_units)?;
115 super::document::limit(
116 LimitKind::Memory,
117 requested,
118 limits.max_decoder_managed_memory_bytes,
119 )?;
120 super::document::limit(
121 LimitKind::RetainedState,
122 requested,
123 limits.max_retained_state_bytes,
124 )?;
125 let mut bytes = Vec::new();
126 bytes
127 .try_reserve(size)
128 .map_err(|_| DecodeError::Allocation {
129 requested_bytes: requested,
130 })?;
131 let allocated =
132 u64::try_from(bytes.capacity()).map_err(|_| MalformedError::IntegerOverflow)?;
133 super::document::limit(
134 LimitKind::Memory,
135 allocated,
136 limits.max_decoder_managed_memory_bytes,
137 )?;
138 super::document::limit(
139 LimitKind::RetainedState,
140 allocated,
141 limits.max_retained_state_bytes,
142 )?;
143 let mut index = 0_usize;
144 #[cfg_attr(verus_keep_ghost, verus_spec(
145 invariant index <= size, bytes@.len() == index,
146 forall|done: int| 0 <= done < index ==> bytes@[done] == if black {0u8} else {255u8},
147 decreases size - index,
148 ))]
149 while index < size {
150 bytes.push(if black { 0 } else { 255 });
151 index += 1;
152 }
153 let bitmap = Self {
154 width,
155 height,
156 stride,
157 bytes,
158 };
159 #[cfg(verus_keep_ghost)]
160 proof! { colour_spec::bitmap_fields(&bitmap); }
161 Ok(bitmap)
162 }
163
164 #[cfg_attr(verus_keep_ghost, verus_spec(out =>
165 requires colour_spec::storage_valid(old(self)),
166 ensures colour_spec::storage_valid(final(self)),
167 final(self).width == old(self).width, final(self).height == old(self).height,
168 final(self).stride == old(self).stride,
169 out.is_ok() ==> x < final(self).width && y < final(self).height
170 && forall|channel:int| 0 <= channel < 3 ==>
171 final(self).bytes@[y as int * final(self).stride as int + x as int * 3 + channel] == value@[channel],
172 out.is_ok() ==> forall|index:int| 0 <= index < final(self).bytes@.len()
173 && !(y as int * final(self).stride as int + x as int * 3 <= index
174 < y as int * final(self).stride as int + x as int * 3 + 3)
175 ==> final(self).bytes@[index] == old(self).bytes@[index],
176 ))]
177 pub(super) fn set(&mut self, x: u32, y: u32, value: [u8; 3]) -> Result<(), DecodeError> {
178 #[cfg(verus_keep_ghost)]
179 proof! { colour_spec::bitmap_fields(self); }
180 if x >= self.width || y >= self.height {
181 return Err(MalformedError::InvalidPageState("RGB pixel outside page bounds").into());
182 }
183 let row = usize::try_from(y)
184 .map_err(|_| MalformedError::IntegerOverflow)?
185 .checked_mul(self.stride)
186 .ok_or(MalformedError::IntegerOverflow)?;
187 let column = usize::try_from(x)
188 .map_err(|_| MalformedError::IntegerOverflow)?
189 .checked_mul(3)
190 .ok_or(MalformedError::IntegerOverflow)?;
191 let offset = row
192 .checked_add(column)
193 .ok_or(MalformedError::IntegerOverflow)?;
194 if offset
195 .checked_add(2)
196 .ok_or(MalformedError::IntegerOverflow)?
197 >= self.bytes.len()
198 {
199 return Err(MalformedError::InvalidPageState("invalid RGB storage").into());
200 }
201 self.bytes[offset] = value[0];
202 self.bytes[offset + 1] = value[1];
203 self.bytes[offset + 2] = value[2];
204 #[cfg(verus_keep_ghost)]
205 proof! {
206 colour_spec::bitmap_fields(self);
207 assert(value@ == seq![value[0], value[1], value[2]]);
208 }
209 Ok(())
210 }
211
212 #[cfg_attr(verus_keep_ghost, verus_spec(out =>
213 requires colour_spec::storage_valid(old(self)),
214 crate::spec::bitmap::storage_valid(mask),
215 ensures colour_spec::storage_valid(final(self)),
216 final(self).width == old(self).width, final(self).height == old(self).height,
217 final(self).stride == old(self).stride,
218 ))]
219 pub(super) fn compose(
220 &mut self,
221 mask: &Bitmap,
222 x: u32,
223 y: u32,
224 foreground: Option<[u8; 3]>,
225 operator: CombinationOperator,
226 ) -> Result<(), DecodeError> {
227 let replace = matches!(operator, CombinationOperator::Replace);
228 if foreground.is_some() && !replace
229 || foreground.is_none() && !replace && !matches!(operator, CombinationOperator::Or)
230 {
231 return Err(DecodeError::Unsupported(UnsupportedFeature::Colour));
232 }
233 let width = self.width.saturating_sub(x).min(mask.width());
234 let height = self.height.saturating_sub(y).min(mask.height());
235 let mut row = 0_u32;
236 #[cfg_attr(verus_keep_ghost, verus_spec(
237 invariant row <= height, colour_spec::storage_valid(self),
238 crate::spec::bitmap::storage_valid(mask),
239 self.width == old(self).width, self.height == old(self).height, self.stride == old(self).stride,
240 decreases height - row,
241 ))]
242 while row < height {
243 let mut column = 0_u32;
244 #[cfg_attr(verus_keep_ghost, verus_spec(
245 invariant column <= width, colour_spec::storage_valid(self),
246 crate::spec::bitmap::storage_valid(mask),
247 self.width == old(self).width, self.height == old(self).height, self.stride == old(self).stride,
248 decreases width - column,
249 ))]
250 while column < width {
251 let ink = mask
252 .pixel(column, row)
253 .ok_or(MalformedError::InvalidPageState(
254 "RGB mask coordinate outside source",
255 ))?;
256 self.paint(
257 x.checked_add(column)
258 .ok_or(MalformedError::IntegerOverflow)?,
259 y.checked_add(row).ok_or(MalformedError::IntegerOverflow)?,
260 ink,
261 foreground,
262 replace,
263 )?;
264 column += 1;
265 }
266 row += 1;
267 }
268 Ok(())
269 }
270
271 #[cfg_attr(verus_keep_ghost, verus_spec(out =>
272 requires colour_spec::storage_valid(old(self)),
273 ensures colour_spec::storage_valid(final(self)),
274 final(self).width == old(self).width, final(self).height == old(self).height,
275 final(self).stride == old(self).stride,
276 out.is_ok() ==> x < final(self).width && y < final(self).height
277 && forall|channel:int| 0 <= channel < 3 ==>
278 final(self).bytes@[y as int * final(self).stride as int + x as int * 3 + channel]
279 == #[trigger] colour_spec::paint_channel(
280 old(self).bytes@[y as int * old(self).stride as int + x as int * 3 + channel],
281 ink, foreground, replace, channel,
282 ),
283 out.is_ok() ==> forall|index:int| 0 <= index < final(self).bytes@.len()
284 && !(y as int * final(self).stride as int + x as int * 3 <= index
285 < y as int * final(self).stride as int + x as int * 3 + 3)
286 ==> final(self).bytes@[index] == old(self).bytes@[index],
287 ))]
288 fn paint(
289 &mut self,
290 x: u32,
291 y: u32,
292 ink: bool,
293 foreground: Option<[u8; 3]>,
294 replace: bool,
295 ) -> Result<(), DecodeError> {
296 #[cfg(verus_keep_ghost)]
297 proof! { colour_spec::bitmap_fields(self); }
298 if x >= self.width || y >= self.height {
299 return Err(MalformedError::InvalidPageState("RGB paint outside page bounds").into());
300 }
301 #[cfg(verus_keep_ghost)]
302 proof! { colour_spec::pixel_offset_bounds(self, x, y); }
303 #[cfg(verus_keep_ghost)]
304 proof_decl! { let ghost before = self.bytes@; }
305 match foreground {
306 Some(rgb) if ink => {
307 self.set(x, y, rgb)?;
308 }
309 None if ink => {
310 self.set(x, y, [0, 0, 0])?;
311 }
312 None if replace => {
313 self.set(x, y, [255, 255, 255])?;
314 }
315 Some(_) | None => {}
316 }
317 #[cfg(verus_keep_ghost)]
318 proof! {
319 colour_spec::bitmap_fields(self);
320 colour_spec::pixel_offset_bounds(self, x, y);
321 reveal(colour_spec::paint_channel);
322 assert forall|channel:int| 0 <= channel < 3 implies
323 self.bytes@[y as int * self.stride as int + x as int * 3 + channel]
324 == #[trigger] colour_spec::paint_channel(
325 before[y as int * self.stride as int + x as int * 3 + channel],
326 ink, foreground, replace, channel,
327 ) by {
328 let offset = y as int * self.stride as int + x as int * 3 + channel;
329 match foreground {
330 Some(rgb) => {
331 if ink { assert(self.bytes@[offset] == rgb@[channel]); }
332 else { assert(self.bytes@[offset] == before[offset]); }
333 },
334 None => {
335 if ink { assert(self.bytes@[offset] == 0); }
336 else if replace { assert(self.bytes@[offset] == 255); }
337 else { assert(self.bytes@[offset] == before[offset]); }
338 },
339 }
340 }
341 }
342 Ok(())
343 }
344}