pub fn decode_animation_webp(
data: &[u8],
) -> Result<DecodedAnimation, DecoderError>Expand description
Decodes an animated WebP container to a sequence of composited RGBA frames.
Examples found in repository?
examples/webp2bmp.rs (line 144)
108fn main() -> Result<(), Error> {
109 let mut args = std::env::args_os().skip(1);
110 let input = args
111 .next()
112 .map(PathBuf::from)
113 .unwrap_or_else(|| PathBuf::from("_testdata/sample.webp"));
114 let output = args.next().map(PathBuf::from).unwrap_or_else(|| {
115 if input == PathBuf::from("_testdata/sample.webp") {
116 PathBuf::from("target/sample.bmp")
117 } else if input == PathBuf::from("_testdata/sample_animation.webp") {
118 PathBuf::from("target/sample_animation")
119 } else {
120 default_output_path(&input)
121 }
122 });
123
124 let data = fs::read(&input)?;
125 let features = get_features(&data)?;
126
127 if features.has_animation {
128 let mut prefix = if output.is_dir() {
129 output.join(
130 input
131 .file_stem()
132 .and_then(|stem| stem.to_str())
133 .unwrap_or("frame"),
134 )
135 } else {
136 output
137 };
138 if prefix.extension().is_some() {
139 prefix.set_extension("");
140 } else if prefix.as_os_str().is_empty() {
141 prefix = default_animation_output_prefix(&input);
142 }
143
144 let animation = decode_animation_webp(&data)?;
145 let mut written_paths = Vec::with_capacity(animation.frames.len());
146 for (index, frame) in animation.frames.into_iter().enumerate() {
147 let path = animation_frame_path(&prefix, index);
148 if let Some(parent) = path.parent() {
149 if !parent.as_os_str().is_empty() {
150 fs::create_dir_all(parent)?;
151 }
152 }
153 let bmp = encode_bmp24_from_rgba(animation.width, animation.height, &frame.rgba)?;
154 fs::write(&path, bmp)?;
155 written_paths.push(path);
156 }
157 for path in written_paths {
158 println!("{}", path.display());
159 }
160 return Ok(());
161 }
162
163 let image = decode(&data)?;
164 let bmp = encode_bmp24_from_rgba(image.width, image.height, &image.rgba)?;
165
166 if let Some(parent) = output.parent() {
167 if !parent.as_os_str().is_empty() {
168 fs::create_dir_all(parent)?;
169 }
170 }
171 fs::write(&output, bmp)?;
172
173 println!("{}", output.display());
174 Ok(())
175}