|
| 1 | +use std::ffi::CString; |
| 2 | + |
| 3 | +#[cfg(feature = "img")] |
| 4 | +use image::DynamicImage; |
| 5 | +use libwebp_sys::*; |
| 6 | + |
| 7 | +#[cfg(feature = "img")] |
| 8 | +use image::*; |
| 9 | + |
| 10 | +use crate::{shared::*, Encoder}; |
| 11 | + |
| 12 | +pub struct AnimFrame<'a> { |
| 13 | + image: &'a [u8], |
| 14 | + layout: PixelLayout, |
| 15 | + width: u32, |
| 16 | + height: u32, |
| 17 | + timestamp: i32, |
| 18 | + config: Option<&'a WebPConfig>, |
| 19 | +} |
| 20 | +impl<'a> AnimFrame<'a> { |
| 21 | + pub fn new( |
| 22 | + image: &'a [u8], |
| 23 | + layout: PixelLayout, |
| 24 | + width: u32, |
| 25 | + height: u32, |
| 26 | + timestamp: i32, |
| 27 | + config: Option<&'a WebPConfig>, |
| 28 | + ) -> Self { |
| 29 | + Self { |
| 30 | + image, |
| 31 | + layout, |
| 32 | + width, |
| 33 | + height, |
| 34 | + timestamp, |
| 35 | + config, |
| 36 | + } |
| 37 | + } |
| 38 | + #[cfg(feature = "img")] |
| 39 | + pub fn from_image(image: &'a DynamicImage, timestamp: i32) -> Result<Self, &str> { |
| 40 | + match image { |
| 41 | + DynamicImage::ImageLuma8(_) => Err("Unimplemented"), |
| 42 | + DynamicImage::ImageLumaA8(_) => Err("Unimplemented"), |
| 43 | + DynamicImage::ImageRgb8(image) => Ok(Self::from_rgb( |
| 44 | + image.as_ref(), |
| 45 | + image.width(), |
| 46 | + image.height(), |
| 47 | + timestamp, |
| 48 | + )), |
| 49 | + DynamicImage::ImageRgba8(image) => Ok(Self::from_rgba( |
| 50 | + image.as_ref(), |
| 51 | + image.width(), |
| 52 | + image.height(), |
| 53 | + timestamp, |
| 54 | + )), |
| 55 | + _ => Err("Unimplemented"), |
| 56 | + } |
| 57 | + } |
| 58 | + /// Creates a new encoder from the given image data in the RGB pixel layout. |
| 59 | + pub fn from_rgb(image: &'a [u8], width: u32, height: u32, timestamp: i32) -> Self { |
| 60 | + Self::new(image, PixelLayout::Rgb, width, height, timestamp, None) |
| 61 | + } |
| 62 | + /// Creates a new encoder from the given image data in the RGBA pixel layout. |
| 63 | + pub fn from_rgba(image: &'a [u8], width: u32, height: u32, timestamp: i32) -> Self { |
| 64 | + Self::new(image, PixelLayout::Rgba, width, height, timestamp, None) |
| 65 | + } |
| 66 | + pub fn get_image(&self) -> &[u8] { |
| 67 | + &self.image |
| 68 | + } |
| 69 | + pub fn get_layout(&self) -> PixelLayout { |
| 70 | + self.layout |
| 71 | + } |
| 72 | + pub fn get_time_ms(&self) -> i32 { |
| 73 | + self.timestamp |
| 74 | + } |
| 75 | + pub fn width(&self) -> u32 { |
| 76 | + self.width |
| 77 | + } |
| 78 | + pub fn height(&self) -> u32 { |
| 79 | + self.height |
| 80 | + } |
| 81 | +} |
| 82 | +impl<'a> From<&'a AnimFrame<'a>> for Encoder<'a> { |
| 83 | + fn from(f: &'a AnimFrame) -> Self { |
| 84 | + Encoder::new(f.get_image(), f.layout, f.width, f.height) |
| 85 | + } |
| 86 | +} |
| 87 | +#[cfg(feature = "img")] |
| 88 | +impl Into<DynamicImage> for &AnimFrame<'_> { |
| 89 | + fn into(self) -> DynamicImage { |
| 90 | + if self.layout.is_alpha() { |
| 91 | + let image = |
| 92 | + ImageBuffer::from_raw(self.width(), self.height(), self.get_image().to_owned()) |
| 93 | + .expect("ImageBuffer couldn't be created"); |
| 94 | + DynamicImage::ImageRgba8(image) |
| 95 | + } else { |
| 96 | + let image = |
| 97 | + ImageBuffer::from_raw(self.width(), self.height(), self.get_image().to_owned()) |
| 98 | + .expect("ImageBuffer couldn't be created"); |
| 99 | + DynamicImage::ImageRgb8(image) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +pub struct AnimEncoder<'a> { |
| 104 | + frames: Vec<AnimFrame<'a>>, |
| 105 | + width: u32, |
| 106 | + height: u32, |
| 107 | + config: &'a WebPConfig, |
| 108 | + muxparams: WebPMuxAnimParams, |
| 109 | +} |
| 110 | +impl<'a> AnimEncoder<'a> { |
| 111 | + pub fn new(width: u32, height: u32, config: &'a WebPConfig) -> Self { |
| 112 | + Self { |
| 113 | + frames: vec![], |
| 114 | + width, |
| 115 | + height, |
| 116 | + config, |
| 117 | + muxparams: WebPMuxAnimParams { |
| 118 | + bgcolor: 0, |
| 119 | + loop_count: 0, |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | + pub fn set_bgcolor(&mut self, rgba: [u8; 4]) { |
| 124 | + let bgcolor = (u32::from(rgba[3]) << 24) |
| 125 | + + (u32::from(rgba[2]) << 16) |
| 126 | + + (u32::from(rgba[1]) << 8) |
| 127 | + + (u32::from(rgba[0])); |
| 128 | + self.muxparams.bgcolor = bgcolor; |
| 129 | + } |
| 130 | + pub fn set_loop_count(&mut self, loop_count: i32) { |
| 131 | + self.muxparams.loop_count = loop_count; |
| 132 | + } |
| 133 | + pub fn add_frame(&mut self, frame: AnimFrame<'a>) { |
| 134 | + self.frames.push(frame); |
| 135 | + } |
| 136 | + pub fn encode(&self) -> WebPMemory { |
| 137 | + self.try_encode().unwrap() |
| 138 | + } |
| 139 | + pub fn try_encode(&self) -> Result<WebPMemory, AnimEncodeError> { |
| 140 | + unsafe { anim_encode(&self) } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +#[derive(Debug)] |
| 145 | +pub enum AnimEncodeError { |
| 146 | + WebPEncodingError(WebPEncodingError), |
| 147 | + WebPMuxError(WebPMuxError), |
| 148 | + WebPAnimEncoderGetError(String), |
| 149 | +} |
| 150 | +unsafe fn anim_encode(all_frame: &AnimEncoder) -> Result<WebPMemory, AnimEncodeError> { |
| 151 | + let width = all_frame.width; |
| 152 | + let height = all_frame.height; |
| 153 | + let mut uninit = std::mem::MaybeUninit::<WebPAnimEncoderOptions>::uninit(); |
| 154 | + |
| 155 | + let mux_abi_version = WebPGetMuxABIVersion(); |
| 156 | + WebPAnimEncoderOptionsInitInternal(uninit.as_mut_ptr(), mux_abi_version); |
| 157 | + let encoder = WebPAnimEncoderNewInternal( |
| 158 | + width as i32, |
| 159 | + height as i32, |
| 160 | + uninit.as_ptr(), |
| 161 | + mux_abi_version, |
| 162 | + ); |
| 163 | + let mut frame_pictures = vec![]; |
| 164 | + for frame in all_frame.frames.iter() { |
| 165 | + let mut pic = crate::new_picture(frame.image, frame.layout, width, height); |
| 166 | + let config = frame.config.unwrap_or(all_frame.config); |
| 167 | + let ok = WebPAnimEncoderAdd( |
| 168 | + encoder, |
| 169 | + &mut *pic as *mut _, |
| 170 | + frame.timestamp as std::os::raw::c_int, |
| 171 | + config, |
| 172 | + ); |
| 173 | + if ok == 0 { |
| 174 | + //ok == false |
| 175 | + WebPAnimEncoderDelete(encoder); |
| 176 | + return Err(AnimEncodeError::WebPEncodingError(pic.error_code)); |
| 177 | + } |
| 178 | + frame_pictures.push(pic); |
| 179 | + } |
| 180 | + WebPAnimEncoderAdd(encoder, std::ptr::null_mut(), 0, std::ptr::null()); |
| 181 | + |
| 182 | + let mut webp_data = std::mem::MaybeUninit::<WebPData>::uninit(); |
| 183 | + let ok = WebPAnimEncoderAssemble(encoder, webp_data.as_mut_ptr()); |
| 184 | + if ok == 0 { |
| 185 | + //ok == false |
| 186 | + let cstring = WebPAnimEncoderGetError(encoder); |
| 187 | + let cstring = CString::from_raw(cstring as *mut _); |
| 188 | + let string = cstring.to_string_lossy().to_string(); |
| 189 | + WebPAnimEncoderDelete(encoder); |
| 190 | + return Err(AnimEncodeError::WebPAnimEncoderGetError(string)); |
| 191 | + } |
| 192 | + WebPAnimEncoderDelete(encoder); |
| 193 | + let mux = WebPMuxCreateInternal(webp_data.as_ptr(), 1, mux_abi_version); |
| 194 | + let mux_error = WebPMuxSetAnimationParams(mux, &all_frame.muxparams); |
| 195 | + if mux_error != WebPMuxError::WEBP_MUX_OK { |
| 196 | + return Err(AnimEncodeError::WebPMuxError(mux_error)); |
| 197 | + } |
| 198 | + let mut raw_data: WebPData = webp_data.assume_init(); |
| 199 | + WebPDataClear(&mut raw_data); |
| 200 | + let mut webp_data = std::mem::MaybeUninit::<WebPData>::uninit(); |
| 201 | + WebPMuxAssemble(mux, webp_data.as_mut_ptr()); |
| 202 | + WebPMuxDelete(mux); |
| 203 | + let raw_data: WebPData = webp_data.assume_init(); |
| 204 | + Ok(WebPMemory(raw_data.bytes as *mut u8, raw_data.size)) |
| 205 | +} |
0 commit comments