Cykooz / libheif-rs

Safe wrapper to libheif-sys for parsing heif/heic files
MIT License
34 stars 11 forks source link

Read from image bytes #19

Closed ollyde closed 7 months ago

ollyde commented 7 months ago

We have let image_file_bytes: Vec<u8> in our control flow which is HEIC

How can we load it into the context without file?

let lib_heif = LibHeif::new();

Cykooz commented 7 months ago

HeifContext::read_from_bytes()

ollyde commented 7 months ago

@Cykooz is there any convenience to convert this to PNG?

Cykooz commented 7 months ago

@ollyde

ollyde commented 7 months ago

@Cykooz thanks, I see the planes stuff on the home page; but it's confusing. Was hoping there was just a convert_to_format or something simple.

https://github.com/Cykooz/libheif-rs

Cykooz commented 7 months ago

You must use planes. It is raw buffer with pixels data.

let handle = context.primary_image_handle().unwrap();
let chroma = if handle.has_alpha_channel() {
    RgbChroma::Rgba
} else {
    RgbChroma::Rgb
};
let image = lib_hef.decode(&handle, ColorSpace::Rgb(chroma), None).unwrap();
let planes = image.planes();
if let Some(buffer) = planes.interleaved {
  // `buffer` is raw buffer with RGB(A) pixels
}
ollyde commented 7 months ago

@Cykooz thanks for you help, but it's too complex, decided to use image-magic, has a one liner for bytes :-)