mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
encode images with alpha channel if present for preview
This commit is contained in:
parent
276f69128d
commit
7517b7fd24
2 changed files with 40 additions and 20 deletions
|
|
@ -2,7 +2,7 @@ use std::{fs::File, io::BufReader, path::{Path, PathBuf}};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use exif::{In, Tag};
|
use exif::{In, Tag};
|
||||||
use image::{codecs::jpeg::JpegEncoder, imageops::{self, FilterType}, DynamicImage, Limits};
|
use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, imageops::{self, FilterType}, DynamicImage, ImageEncoder, Limits};
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
use yazi_config::{PREVIEW, TASKS};
|
use yazi_config::{PREVIEW, TASKS};
|
||||||
|
|
||||||
|
|
@ -30,18 +30,25 @@ impl Image {
|
||||||
img = img.resize(w, h, Self::filter());
|
img = img.resize(w, h, Self::filter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buf = Vec::new();
|
||||||
img = Self::rotate(img, orientation);
|
img = Self::rotate(img, orientation);
|
||||||
if !matches!(img, DynamicImage::ImageRgb8(_)) {
|
|
||||||
img = DynamicImage::ImageRgb8(img.into_rgb8());
|
match img {
|
||||||
|
DynamicImage::ImageRgb8(_) => {
|
||||||
|
JpegEncoder::new_with_quality(&mut buf, PREVIEW.image_quality).encode_image(&img)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let (width, height) = (img.width(), img.height());
|
||||||
|
let img_rgba8 = img.into_rgba8(); // Consume `img` here
|
||||||
|
PngEncoder::new(&mut buf).write_image(
|
||||||
|
&img_rgba8,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
image::ExtendedColorType::Rgba8,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
JpegEncoder::new_with_quality(&mut buf, PREVIEW.image_quality).encode(
|
|
||||||
img.as_bytes(),
|
|
||||||
img.width(),
|
|
||||||
img.height(),
|
|
||||||
img.color().into(),
|
|
||||||
)?;
|
|
||||||
Ok::<_, anyhow::Error>(buf)
|
Ok::<_, anyhow::Error>(buf)
|
||||||
})
|
})
|
||||||
.await??;
|
.await??;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use std::{io::Write, path::Path};
|
use std::{io::Write, path::Path};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::{engine::{general_purpose::STANDARD, Config}, Engine};
|
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||||
use crossterm::{cursor::MoveTo, queue};
|
use crossterm::{cursor::MoveTo, queue};
|
||||||
use image::{codecs::jpeg::JpegEncoder, DynamicImage};
|
use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, DynamicImage, ImageEncoder};
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
|
use yazi_config::PREVIEW;
|
||||||
|
|
||||||
use super::image::Image;
|
use super::image::Image;
|
||||||
use crate::{adapter::Adapter, Emulator, CLOSE, START};
|
use crate::{adapter::Adapter, Emulator, CLOSE, START};
|
||||||
|
|
@ -38,22 +39,34 @@ impl Iterm2 {
|
||||||
|
|
||||||
async fn encode(img: DynamicImage) -> Result<Vec<u8>> {
|
async fn encode(img: DynamicImage) -> Result<Vec<u8>> {
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
let mut jpg = vec![];
|
let width = img.width();
|
||||||
JpegEncoder::new_with_quality(&mut jpg, 75).encode_image(&img)?;
|
let height = img.height();
|
||||||
|
|
||||||
let len = base64::encoded_len(jpg.len(), STANDARD.config().encode_padding());
|
let mut img_buf = vec![];
|
||||||
let mut buf = Vec::with_capacity(200 + len.unwrap_or(1 << 16));
|
|
||||||
|
|
||||||
|
if img.color().has_alpha() {
|
||||||
|
PngEncoder::new(&mut img_buf).write_image(
|
||||||
|
&img.into_rgba8(),
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
image::ExtendedColorType::Rgba8,
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
JpegEncoder::new_with_quality(&mut img_buf, PREVIEW.image_quality).encode_image(&img)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = vec![];
|
||||||
write!(
|
write!(
|
||||||
buf,
|
buf,
|
||||||
"{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:{}\x07{}",
|
"{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:{}\x07{}",
|
||||||
START,
|
START,
|
||||||
jpg.len(),
|
img_buf.len(),
|
||||||
img.width(),
|
width,
|
||||||
img.height(),
|
height,
|
||||||
STANDARD.encode(&jpg),
|
STANDARD.encode(&img_buf),
|
||||||
CLOSE
|
CLOSE
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue