1use crate::decoder::header::{parse_animation_webp, ParsedAnimationFrame};
4use crate::decoder::lossless::decode_lossless_vp8l_to_rgba;
5use crate::decoder::lossy::{decode_lossy_vp8_frame_to_rgba, DecodedImage};
6use crate::decoder::DecoderError;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct DecodedAnimationFrame {
11 pub duration: usize,
13 pub rgba: Vec<u8>,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct DecodedAnimation {
20 pub width: usize,
22 pub height: usize,
24 pub background_color: u32,
26 pub loop_count: u16,
28 pub frames: Vec<DecodedAnimationFrame>,
30}
31
32fn argb_to_rgba(argb: u32) -> [u8; 4] {
33 [
34 ((argb >> 16) & 0xff) as u8,
35 ((argb >> 8) & 0xff) as u8,
36 (argb & 0xff) as u8,
37 (argb >> 24) as u8,
38 ]
39}
40
41fn fill_rect(
42 canvas: &mut [u8],
43 canvas_width: usize,
44 x_offset: usize,
45 y_offset: usize,
46 width: usize,
47 height: usize,
48 rgba: [u8; 4],
49) {
50 for y in 0..height {
51 let row = ((y_offset + y) * canvas_width + x_offset) * 4;
52 for x in 0..width {
53 let dst = row + x * 4;
54 canvas[dst..dst + 4].copy_from_slice(&rgba);
55 }
56 }
57}
58
59fn blend_channel(src: u8, src_alpha: u32, dst: u8, dst_factor_alpha: u32, scale: u32) -> u8 {
60 let blended = (src as u32 * src_alpha + dst as u32 * dst_factor_alpha) * scale;
61 (blended >> 24) as u8
62}
63
64fn blend_pixel_non_premult(src: [u8; 4], dst: [u8; 4]) -> [u8; 4] {
65 let src_alpha = src[3] as u32;
66 if src_alpha == 0 {
67 return dst;
68 }
69 if src_alpha == 255 {
70 return src;
71 }
72
73 let dst_alpha = dst[3] as u32;
74 let dst_factor_alpha = (dst_alpha * (256 - src_alpha)) >> 8;
75 let blend_alpha = src_alpha + dst_factor_alpha;
76 let scale = (1u32 << 24) / blend_alpha;
77
78 [
79 blend_channel(src[0], src_alpha, dst[0], dst_factor_alpha, scale),
80 blend_channel(src[1], src_alpha, dst[1], dst_factor_alpha, scale),
81 blend_channel(src[2], src_alpha, dst[2], dst_factor_alpha, scale),
82 blend_alpha as u8,
83 ]
84}
85
86fn composite_frame(
87 canvas: &mut [u8],
88 canvas_width: usize,
89 frame_rgba: &[u8],
90 frame: &ParsedAnimationFrame<'_>,
91) {
92 for y in 0..frame.height {
93 let src_row = y * frame.width * 4;
94 let dst_row = ((frame.y_offset + y) * canvas_width + frame.x_offset) * 4;
95 for x in 0..frame.width {
96 let src = src_row + x * 4;
97 let dst = dst_row + x * 4;
98 if frame.blend {
99 let src_pixel = [
100 frame_rgba[src],
101 frame_rgba[src + 1],
102 frame_rgba[src + 2],
103 frame_rgba[src + 3],
104 ];
105 let dst_pixel = [
106 canvas[dst],
107 canvas[dst + 1],
108 canvas[dst + 2],
109 canvas[dst + 3],
110 ];
111 let out = blend_pixel_non_premult(src_pixel, dst_pixel);
112 canvas[dst..dst + 4].copy_from_slice(&out);
113 } else {
114 canvas[dst..dst + 4].copy_from_slice(&frame_rgba[src..src + 4]);
115 }
116 }
117 }
118}
119
120fn decode_frame_image(frame: &ParsedAnimationFrame<'_>) -> Result<DecodedImage, DecoderError> {
121 let image = match &frame.image_chunk.fourcc {
122 b"VP8L" => {
123 if frame.alpha_chunk.is_some() {
124 return Err(DecoderError::Bitstream(
125 "VP8L animation frame must not carry ALPH chunk",
126 ));
127 }
128 decode_lossless_vp8l_to_rgba(frame.image_data)?
129 }
130 b"VP8 " => decode_lossy_vp8_frame_to_rgba(frame.image_data, frame.alpha_data)?,
131 _ => return Err(DecoderError::Bitstream("unsupported animation frame chunk")),
132 };
133
134 if image.width != frame.width || image.height != frame.height {
135 return Err(DecoderError::Bitstream(
136 "animation frame dimensions do not match bitstream",
137 ));
138 }
139 Ok(image)
140}
141
142pub fn decode_animation_webp(data: &[u8]) -> Result<DecodedAnimation, DecoderError> {
144 let parsed = parse_animation_webp(data)?;
145 let background = argb_to_rgba(parsed.animation.background_color);
146 let mut canvas = vec![0u8; parsed.features.width * parsed.features.height * 4];
147 fill_rect(
148 &mut canvas,
149 parsed.features.width,
150 0,
151 0,
152 parsed.features.width,
153 parsed.features.height,
154 background,
155 );
156
157 let mut previous_rect = None;
158 let mut frames = Vec::with_capacity(parsed.frames.len());
159 for frame in &parsed.frames {
160 if let Some((x_offset, y_offset, width, height)) = previous_rect.take() {
161 fill_rect(
162 &mut canvas,
163 parsed.features.width,
164 x_offset,
165 y_offset,
166 width,
167 height,
168 background,
169 );
170 }
171
172 let decoded = decode_frame_image(frame)?;
173 composite_frame(&mut canvas, parsed.features.width, &decoded.rgba, frame);
174 frames.push(DecodedAnimationFrame {
175 duration: frame.duration,
176 rgba: canvas.clone(),
177 });
178
179 if frame.dispose_to_background {
180 previous_rect = Some((frame.x_offset, frame.y_offset, frame.width, frame.height));
181 }
182 }
183
184 Ok(DecodedAnimation {
185 width: parsed.features.width,
186 height: parsed.features.height,
187 background_color: parsed.animation.background_color,
188 loop_count: parsed.animation.loop_count,
189 frames,
190 })
191}