Skip to content

Commit

Permalink
wip heif support
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Oct 30, 2023
1 parent 6194c63 commit 0756b4e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ img-parts = "0.3.0"
dark-light = "1.0.0"
trash = "3.1"
lutgen = {version ="0.9.0", features = ["lutgen-palettes"]}
libheif-rs = { version = "0.22.0", default-features = false }

[features]
avif_native = ["avif-decode"]
Expand Down
54 changes: 54 additions & 0 deletions src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,60 @@ pub fn open_image(img_location: &Path) -> Result<Receiver<Frame>> {

// col.add_still(i.to_rgba8());
}
"heif" | "heic" => {
use libheif_rs::{
Channel, ColorSpace, HeifContext, ItemId, LibHeif, Result, RgbChroma,
};

let lib_heif = LibHeif::new();
let ctx = HeifContext::read_from_file(&img_location.to_string_lossy())?;
let handle = ctx.primary_image_handle()?;

// Get Exif
// let mut meta_ids: Vec<ItemId> = vec![0; 1];
// let count = handle.metadata_block_ids(&mut meta_ids, b"Exif");
// assert_eq!(count, 1);
// let exif: Vec<u8> = handle.metadata(meta_ids[0])?;

// Decode the image
let image = lib_heif.decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)?;
debug!("decoded HEIF/HEIC");

// Get "pixels"
let planes = image.planes();
let interleaved_plane = planes.interleaved.unwrap();


debug!("bpp {}", interleaved_plane.bits_per_pixel);
debug!("stride {}", interleaved_plane.stride);
debug!("sbpp {}", interleaved_plane.storage_bits_per_pixel);
debug!("s {}x{}", interleaved_plane.width, interleaved_plane.height);
let mut img_buffer = vec![];

for p in interleaved_plane.data.as_rgb() {
img_buffer.push(p.r);
img_buffer.push(p.g);
img_buffer.push(p.b);
img_buffer.push(255);
}

// for interleaved_plane in buf {
// img_buffer.push(b.r);
// img_buffer.push(b.g);
// img_buffer.push(b.b);
// img_buffer.push(255);
// }
let buf = image::ImageBuffer::from_vec(interleaved_plane.width as u32, interleaved_plane.height as u32, img_buffer)
.context("Can't create HEIC ImageBuffer with given res")?;
// debug!("{:?}",buf.);
_ = sender.send(Frame::new_still(buf));
return Ok(receiver);

// let mut file = File::open(img_location)?;
// let mut buf = vec![];

// col.add_still(i.to_rgba8());
}
#[cfg(feature = "avif_native")]
#[cfg(not(feature = "dav1d"))]
"avif" => {
Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::Command;
use log::debug;
use log::error;
use log::info;
use log::trace;
use log::warn;
use nalgebra::Vector2;
use notan::app::Event;
Expand Down Expand Up @@ -545,7 +546,7 @@ fn event(app: &mut App, state: &mut OculanteState, evt: Event) {
(delta_y / divisor).max(-5.0).min(5.0),
state.image_geometry.scale,
);
info!("Delta {delta}, raw {delta_y}");
trace!("Delta {delta}, raw {delta_y}");
let new_scale = state.image_geometry.scale + delta;
// limit scale
if new_scale > 0.01 && new_scale < 40. {
Expand Down Expand Up @@ -615,7 +616,7 @@ fn update(app: &mut App, state: &mut OculanteState) {
app.window().size(),
);
state.persistent_settings.save_blocking();
debug!("Save {t}");
trace!("Save {t}");
}

let mouse_pos = app.mouse.position();
Expand Down Expand Up @@ -704,7 +705,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
// check if a new texture has been sent
if let Ok(frame) = state.texture_channel.1.try_recv() {
let img = frame.buffer;
// debug!("Received image buffer: {:?}", img.dimensions());
debug!("Received image buffer: {:?}", img.dimensions());
state.image_dimension = img.dimensions();
// state.current_texture = img.to_texture(gfx);

Expand All @@ -726,6 +727,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O

match frame.source {
FrameSource::Still => {
debug!("Received still");
state.edit_state.result_image_op = Default::default();
state.edit_state.result_pixel_op = Default::default();

Expand Down Expand Up @@ -762,7 +764,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
}
}
} else if let Some(parent) = p.parent() {
info!("Looking for {}", parent.join(".oculante").display());
debug!("Looking for {}", parent.join(".oculante").display());
if parent.join(".oculante").is_file() {
info!("is file {}", parent.join(".oculante").display());

Expand Down Expand Up @@ -802,6 +804,8 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
state.current_texture = img.to_texture(gfx);
}
} else {
debug!("Setting texture");
_ = img.save("debug.png");
state.current_texture = img.to_texture(gfx);
}

Expand Down Expand Up @@ -831,7 +835,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
}

if state.redraw {
debug!("Force redraw");
trace!("Force redraw");
app.window().request_frame();
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::shortcuts::{lookup, InputEvent, Shortcuts};
pub const SUPPORTED_EXTENSIONS: &[&str] = &[
"bmp", "dds", "exr", "ff", "gif", "hdr", "ico", "jpeg", "jpg", "png", "pnm", "psd", "svg",
"tga", "tif", "tiff", "webp", "nef", "cr2", "dng", "mos", "erf", "raf", "arw", "3fr", "ari",
"srf", "sr2", "braw", "r3d", "nrw", "raw", "avif", "jxl", "ppm",
"srf", "sr2", "braw", "r3d", "nrw", "raw", "avif", "jxl", "ppm", "heif", "heic",
];

fn is_pixel_fully_transparent(p: &Rgba<u8>) -> bool {
Expand Down

0 comments on commit 0756b4e

Please sign in to comment.