1#![allow(dead_code)]
15#![allow(clippy::all)]
16#![allow(clippy::unwrap_used)]
21#![allow(clippy::expect_used)]
22
23pub mod io;
24
25mod alpha_blending;
26mod decoder;
27mod extended;
28mod huffman;
29mod loop_filter;
30mod lossless;
31mod lossless_transform;
32mod transform;
33pub mod vp8;
34mod vp8_arithmetic_decoder;
35pub mod yuv;
36
37pub use decoder::{DecodingError, WebPDecoder};
38
39use alloc::vec;
40use alloc::vec::Vec;
41
42#[inline]
44pub(crate) fn decode_kill_checkpoint(step: usize) {
45 if step & 31 == 0 {
46 crate::kernel::window_mgr::kill_checkpoint();
47 }
48}
49
50pub fn decode_webp(data: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
53 crate::kernel::window_mgr::kill_checkpoint();
54 let cursor = io::Cursor::new(data);
55 let mut decoder = WebPDecoder::new(cursor).ok()?;
56 let (w, h) = decoder.dimensions();
57 if w == 0 || h == 0 || w > 8192 || h > 8192 {
58 return None;
59 }
60 let has_alpha = decoder.has_alpha();
61 let buf_size = decoder.output_buffer_size()?;
62 let mut buf = vec![0u8; buf_size];
63 decoder.read_image(&mut buf).ok()?;
64
65 let total = (w * h) as usize;
67 if has_alpha {
68 if buf.len() == total * 4 {
70 return Some((w, h, buf));
71 }
72 } else {
73 if buf.len() == total * 3 {
75 let mut rgba = vec![0u8; total * 4];
76 let stride = (w as usize).max(1);
77 for i in 0..total {
78 if i % stride == 0 {
79 decode_kill_checkpoint(i / stride);
80 }
81 rgba[i * 4] = buf[i * 3];
82 rgba[i * 4 + 1] = buf[i * 3 + 1];
83 rgba[i * 4 + 2] = buf[i * 3 + 2];
84 rgba[i * 4 + 3] = 255;
85 }
86 return Some((w, h, rgba));
87 }
88 }
89 None
90}