mirror of
https://github.com/fossasia/badgemagic-rs
synced 2025-07-15 17:13:59 +00:00
feat: Add animated GIF support
This commit is contained in:
parent
b794eec9a5
commit
b344ac00b1
1 changed files with 35 additions and 2 deletions
37
src/main.rs
37
src/main.rs
|
@ -17,9 +17,11 @@ use embedded_graphics::{
|
|||
text::Text,
|
||||
Drawable, Pixel,
|
||||
};
|
||||
use image::{imageops::FilterType, ImageReader};
|
||||
use image::{
|
||||
codecs::gif::GifDecoder, imageops::FilterType, AnimationDecoder, ImageReader, Pixel as iPixel,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::{fs, path::PathBuf};
|
||||
use std::{fs, fs::File, io::BufReader, path::PathBuf};
|
||||
#[cfg(feature = "u8g2-fonts")]
|
||||
use u8g2_fonts::{fonts::u8g2_font_lucasfont_alternate_tf, U8g2TextStyle};
|
||||
|
||||
|
@ -99,6 +101,7 @@ enum Content {
|
|||
BitmapBase64 { width: u32, bitmap_base64: String },
|
||||
BitmapFile { width: u32, bitmap_file: PathBuf },
|
||||
ImageFile { img_file: PathBuf },
|
||||
GifFile { gif_file: PathBuf },
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
|
@ -252,6 +255,36 @@ fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
|
|||
}
|
||||
}
|
||||
}
|
||||
Content::GifFile { gif_file } => {
|
||||
let file_in = BufReader::new(File::open(gif_file)?);
|
||||
let frames = GifDecoder::new(file_in)?
|
||||
.into_frames()
|
||||
.collect_frames()
|
||||
.expect("error decoding gif");
|
||||
|
||||
let frame_count = frames.len();
|
||||
let (width, height) = frames.first().unwrap().buffer().dimensions();
|
||||
if height != 11 || width != 44 {
|
||||
anyhow::bail!("Expected 44x11 pixel gif file");
|
||||
}
|
||||
|
||||
let mut buffer = payload.add_message(style, (48 * frame_count + 7) / 8);
|
||||
|
||||
for (i, frame) in frames.iter().enumerate() {
|
||||
let buf = frame.buffer();
|
||||
for y in 0..11 {
|
||||
for x in 0..44 {
|
||||
if buf.get_pixel(x, y).to_luma().0 > [31] {
|
||||
Pixel(
|
||||
Point::new((x as usize + i * 48).try_into()?, y.try_into()?),
|
||||
BinaryColor::On,
|
||||
)
|
||||
.draw(&mut buffer)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue