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
42pub fn decode_webp(data: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
45 let cursor = io::Cursor::new(data);
46 let mut decoder = WebPDecoder::new(cursor).ok()?;
47 let (w, h) = decoder.dimensions();
48 if w == 0 || h == 0 || w > 8192 || h > 8192 {
49 return None;
50 }
51 let has_alpha = decoder.has_alpha();
52 let buf_size = decoder.output_buffer_size()?;
53 let mut buf = vec![0u8; buf_size];
54 decoder.read_image(&mut buf).ok()?;
55
56 let total = (w * h) as usize;
58 if has_alpha {
59 if buf.len() == total * 4 {
61 return Some((w, h, buf));
62 }
63 } else {
64 if buf.len() == total * 3 {
66 let mut rgba = vec![0u8; total * 4];
67 for i in 0..total {
68 rgba[i * 4] = buf[i * 3];
69 rgba[i * 4 + 1] = buf[i * 3 + 1];
70 rgba[i * 4 + 2] = buf[i * 3 + 2];
71 rgba[i * 4 + 3] = 255;
72 }
73 return Some((w, h, rgba));
74 }
75 }
76 None
77}