I am pretty new to Rust, and I am trying to use the jpegxl-rs crate to encode files to .jxl and then also be able to decode back into the original file type. I have everything set up, but when I go to use encoder.encode() I run into an error where it says "Encoder failed to encode file /home/[user]/Pictures/[filename].png: The encoder API is used in an incorrect way" .
From the research I've done, I should have everything correct, but I still don't understand why it is throwing an error. Has anyone on here messed with jpegxl-rs and had this issue? I am using Tauri, but I don't think that is part of the issue as I am using Rust specific libraries. Here i...
I am pretty new to Rust, and I am trying to use the jpegxl-rs crate to encode files to .jxl and then also be able to decode back into the original file type. I have everything set up, but when I go to use encoder.encode() I run into an error where it says "Encoder failed to encode file /home/[user]/Pictures/[filename].png: The encoder API is used in an incorrect way" .
From the research I've done, I should have everything correct, but I still don't understand why it is throwing an error. Has anyone on here messed with jpegxl-rs and had this issue? I am using Tauri, but I don't think that is part of the issue as I am using Rust specific libraries. Here is the code:
use image::ImageReader; use jpegxl_rs::encode::{EncoderResult, EncoderSpeed}; use jpegxl_rs::encoder_builder; #[tauri::command] pub fn process_file_array(file_paths: Vec<String>) -> Result<String, String> { for file in &file_paths { println!("Processing file: {}", file); let open_file = ImageReader::open(file) .map_err(|e| format!("Failed to open file {}: {}", file, e))? .decode() .map_err(|e| format!("Failed to decode file {}: {}", file, e))?; let img_alpha = open_file.has_alpha(); let img_bytes = convert_file_to_bytes(img_alpha, &open_file); println!("Opened file {}: ", file); let mut encoder = encoder_builder() . has_alpha (img_alpha) . lossless (true) . speed (EncoderSpeed::Tortoise) . uses_original_profile (true) .build() .map_err(|e| format!("Encoder failed to build on file {}: {}", file, e))?; println!("Encoder built successfully for file {}", file); let buffer: EncoderResult<f32> = encoder . encode (&img_bytes, open_file.width(), open_file.height()) .map_err(|e| format!("Encoder failed to encode file {}: {}", file, e))?; println!("Encoded file {}: {} bytes", file, buffer.data.len()); //TODO: Write the buffer to file } Ok(format!( "Processed {} files successfully!", file_paths.len() )) } fn convert_file_to_bytes(has_alpha: bool, img: &image::DynamicImage) -> Vec<u16> { if has_alpha { img.to_rgba16().into_raw() } else { img.to_rgb16().into_raw() } }