Compare commits

...

1 Commits

Author SHA1 Message Date
itsjunetime 997631aac9 inline pnm decoder instead of using generalized load fn 2026-03-08 15:08:32 -05:00
2 changed files with 24 additions and 17 deletions
+23 -16
View File
@@ -1,11 +1,12 @@
use std::{ use std::{
io::Cursor,
num::NonZeroUsize, num::NonZeroUsize,
time::{SystemTime, UNIX_EPOCH} time::{SystemTime, UNIX_EPOCH}
}; };
use flume::{Receiver, SendError, Sender, TryRecvError}; use flume::{Receiver, SendError, Sender, TryRecvError};
use futures_util::stream::StreamExt as _; use futures_util::stream::StreamExt as _;
use image::DynamicImage; use image::{DynamicImage, codecs::pnm::PnmDecoder};
use kittage::{NumberOrId, action::NONZERO_ONE}; use kittage::{NumberOrId, action::NONZERO_ONE};
use ratatui::layout::Rect; use ratatui::layout::Rect;
use ratatui_image::{ use ratatui_image::{
@@ -111,22 +112,26 @@ pub async fn run_conversion_loop(
return Ok(None); return Ok(None);
}; };
let mut dyn_img = image::load_from_memory_with_format( let decoder = PnmDecoder::new(Cursor::new(&page_info.img_data.pixels)).map_err(|e| {
&page_info.img_data.pixels, RenderError::Converting(format!(
image::ImageFormat::Pnm "The image data provided from mupdf was not in pnm format ({e}); don't know how to convert"
) ))
.map_err(|e| RenderError::Converting(format!("Can't load image: {e}")))?; })?;
match dyn_img { // The image we get should always already be `ImageRgb8`, so this `into` shouldn't do any
DynamicImage::ImageRgb8(ref mut img) => // conversions or anything, but just in case some underlying detail of mupdf or image
for quad in &*page_info.result_rects { // changes, we do the `into` instead of just `match + unreachable!()` to avoid panicking
img.par_enumerate_pixels_mut() let mut dyn_img = DynamicImage::from_decoder(decoder)
.filter(|(x, y, _)| { .map_err(|e| RenderError::Converting(format!("Can't load image: {e}")))?
*x > quad.ul_x && *x < quad.lr_x && *y > quad.ul_y && *y < quad.lr_y .into_rgb8();
})
.for_each(|(_, _, px)| px.0[2] = px.0[2].saturating_sub(u8::MAX / 2)); for quad in &*page_info.result_rects {
}, dyn_img
_ => unreachable!() .par_enumerate_pixels_mut()
.filter(|(x, y, _)| {
*x > quad.ul_x && *x < quad.lr_x && *y > quad.ul_y && *y < quad.lr_y
})
.for_each(|(_, _, px)| px.0[2] = px.0[2].saturating_sub(u8::MAX / 2));
} }
let img_area = Rect { let img_area = Rect {
@@ -136,6 +141,8 @@ pub async fn run_conversion_loop(
y: 0 y: 0
}; };
let dyn_img = DynamicImage::ImageRgb8(dyn_img);
let txt_img = match picker.protocol_type() { let txt_img = match picker.protocol_type() {
ProtocolType::Kitty => { ProtocolType::Kitty => {
let rn = SystemTime::now() let rn = SystemTime::now()