1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 06:12:16 +03:00

simplify png decoding

refs: https://github.com/image-rs/image/issues/1989
This commit is contained in:
Wez Furlong 2024-01-20 12:16:01 -07:00
parent 9acc2ae6c5
commit 7ae615b01d
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387

View File

@ -11,15 +11,14 @@ use config::{AllowSquareGlyphOverflow, TextStyle};
use euclid::num::Zero; use euclid::num::Zero;
use image::io::Limits; use image::io::Limits;
use image::{ use image::{
AnimationDecoder, ColorType, DynamicImage, Frame, Frames, ImageDecoder, ImageFormat, AnimationDecoder, DynamicImage, Frame, Frames, ImageDecoder, ImageFormat, ImageResult,
ImageResult,
}; };
use lfucache::LfuCache; use lfucache::LfuCache;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use ordered_float::NotNan; use ordered_float::NotNan;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{Read, Seek}; use std::io::Seek;
use std::rc::Rc; use std::rc::Rc;
use std::sync::mpsc::{sync_channel, Receiver, RecvTimeoutError, SyncSender, TryRecvError}; use std::sync::mpsc::{sync_channel, Receiver, RecvTimeoutError, SyncSender, TryRecvError};
use std::sync::{Arc, MutexGuard}; use std::sync::{Arc, MutexGuard};
@ -272,56 +271,7 @@ impl FrameDecoder {
if decoder.is_apng() { if decoder.is_apng() {
decoder.apng().into_frames() decoder.apng().into_frames()
} else { } else {
let size = decoder.total_bytes() as usize; let buf = DynamicImage::from_decoder(decoder)?.into_rgba8();
let mut buf = vec![0u8; size];
let (width, height) = decoder.dimensions();
let color_type = decoder.color_type();
let mut reader = decoder.into_reader().context("PngDecoder into_reader")?;
reader.read(&mut buf)?;
let buf: image::RgbaImage = match color_type {
ColorType::Rgb8 => DynamicImage::ImageRgb8(
image::RgbImage::from_raw(width, height, buf).ok_or_else(|| {
anyhow::anyhow!(
"PNG {color_type:?} {size} / {width}x{height} = {} \
bytes per pixel",
size / (width * height) as usize
)
})?,
)
.into_rgba8(),
ColorType::Rgba8 => image::RgbaImage::from_raw(width, height, buf)
.ok_or_else(|| {
anyhow::anyhow!(
"PNG {color_type:?} {size} / {width}x{height} = {} \
bytes per pixel",
size / (width * height) as usize
)
})?,
ColorType::L8 => DynamicImage::ImageLuma8(
image::ImageBuffer::<image::Luma<_>, _>::from_raw(width, height, buf)
.ok_or_else(|| {
anyhow::anyhow!(
"PNG {color_type:?} {size} / {width}x{height} = {} \
bytes per pixel",
size / (width * height) as usize
)
})?,
)
.into_rgba8(),
ColorType::La8 => DynamicImage::ImageLumaA8(
image::ImageBuffer::<image::LumaA<_>, _>::from_raw(width, height, buf)
.ok_or_else(|| {
anyhow::anyhow!(
"PNG {color_type:?} {size} / {width}x{height} = {} \
bytes per pixel",
size / (width * height) as usize
)
})?,
)
.into_rgba8(),
_ => anyhow::bail!("unimplemented PNG conversion from {color_type:?}"),
};
let delay = image::Delay::from_numer_denom_ms(u32::MAX, 1); let delay = image::Delay::from_numer_denom_ms(u32::MAX, 1);
let frame = Frame::from_parts(buf, 0, 0, delay); let frame = Frame::from_parts(buf, 0, 0, delay);
Frames::new(Box::new(std::iter::once(ImageResult::Ok(frame)))) Frames::new(Box::new(std::iter::once(ImageResult::Ok(frame))))