From b237d652e7f8ad403ad705f12eecab0f367c2430 Mon Sep 17 00:00:00 2001 From: Inflation <2375962+inflation@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:45:40 +0800 Subject: [PATCH] refactor: Update error messages for more information on how to debug issues --- jpegxl-rs/src/decode/event.rs | 38 +++++++++++++++++++++++++++++++++++ jpegxl-rs/src/errors.rs | 15 +++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 jpegxl-rs/src/decode/event.rs diff --git a/jpegxl-rs/src/decode/event.rs b/jpegxl-rs/src/decode/event.rs new file mode 100644 index 0000000..a010cd8 --- /dev/null +++ b/jpegxl-rs/src/decode/event.rs @@ -0,0 +1,38 @@ +use std::ffi::c_int; + +/// Target of the color profile. +pub use jpegxl_sys::decode::JxlColorProfileTarget as ColorProfileTarget; + +use super::{ColorEncodingConfig, Config}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Event { + BasicInfo, + ColorEncoding(ColorEncodingConfig), +} + +impl From for c_int { + fn from(value: Event) -> Self { + match value { + Event::BasicInfo => 0x40, + Event::ColorEncoding { .. } => 0x100, + } + } +} + +pub(crate) fn parse_events(iter: I) -> (c_int, Config) +where + I: IntoIterator, +{ + iter.into_iter() + .fold((0, Config::default()), |(flag, config), x| { + let flag = flag | c_int::from(x); + let config = match x { + Event::ColorEncoding(val) => Config { + color_profile: Some(val), + }, + _ => config, + }; + (flag, config) + }) +} diff --git a/jpegxl-rs/src/errors.rs b/jpegxl-rs/src/errors.rs index 94306f9..dc35a1d 100644 --- a/jpegxl-rs/src/errors.rs +++ b/jpegxl-rs/src/errors.rs @@ -28,7 +28,10 @@ pub enum DecodeError { #[error("Cannot create a decoder")] CannotCreateDecoder, /// Unknown Error - #[error("Generic Error")] + #[error( + "Generic Error. Please build `libjxl` from source (using `vendored` feature) + in debug mode to get more information. Check `stderr` for any internal error messages." + )] GenericError, /// Invalid input #[error("The input does not contain a valid codestream or container")] @@ -36,6 +39,8 @@ pub enum DecodeError { /// Unsupported Pixel bit width #[error("Unsupported Pixel bit width: {0}")] UnsupportedBitWidth(u32), + #[error("Invalid usage of the library, please file an issus: {0}")] + InvalidUsage(&'static str), /// Unknown status #[error("Unknown status: `{0:?}`")] UnknownStatus(JxlDecoderStatus), @@ -49,7 +54,10 @@ pub enum EncodeError { #[error("Cannot create an encoder")] CannotCreateEncoder, /// Generic Error - #[error("Generic Error")] + #[error( + "Generic Error. Please build `libjxl` from source (using `vendored` feature) + in debug mode to get more information. Check `stderr` for any internal error messages." + )] GenericError, /// Not Supported #[error("Encoder does not support it (yet)")] @@ -66,7 +74,8 @@ pub enum EncodeError { /// Input is invalid (e.g. corrupt JPEG file or ICC profile) #[error("Input is invalid")] BadInput, - /// The encoder API is used in an incorrect way. In this case, a debug build of libjxl should output a specific error message + /// The encoder API is used in an incorrect way. In this case, + /// a debug build of libjxl should output a specific error message #[error("The encoder API is used in an incorrect way")] ApiUsage, /// Unknown status