Skip to main content

jpegxr/
lib.rs

1#![allow(dead_code)]
2#![allow(non_snake_case)]
3#![allow(non_camel_case_types)]
4#![allow(non_upper_case_globals)]
5#![allow(unused_imports)]
6#![allow(unused_variables)]
7#![allow(clippy::missing_safety_doc)]
8
9//! Safe JPEG-XR decode, encode, and transcode API backed by a faithful Rust
10//! translation of JXRLib.
11
12/// Crate-wide error type: the idiomatic replacement for BOTH C error families — the
13/// glue/stream `WMP_err*` codes (`ERR`/`int`, `windowsmediaphoto.h`) and the codec
14/// `ERR_CODE` status (`ICERR_OK`/`ICERR_ERROR`). Every function whose original C returned
15/// one of those integer codes returns `Result<T, WmpError>`: success (`WMP_errSuccess` /
16/// `ICERR_OK`, both `0`) maps to `Ok(...)`, and each failure code maps to the matching
17/// variant. The generic codec failure `ICERR_ERROR` (== `WMP_errFail`, both `-1`) is
18/// `WmpError::Fail`; where the cause is known, a more specific variant is used.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum WmpError {
21    Fail,
22    NotYetImplemented,
23    AbstractMethod,
24    OutOfMemory,
25    FileIO,
26    BufferOverflow,
27    InvalidParameter,
28    InvalidArgument,
29    UnsupportedFormat,
30    IncorrectCodecVersion,
31    IndexNotFound,
32    OutOfSequence,
33    NotInitialized,
34    MustBeMultipleOf16LinesUntilLastCall,
35    PlanarAlphaBandedEncRequiresTempFile,
36    AlphaModeCannotBeTranscoded,
37    IncorrectCodecSubVersion,
38}
39
40impl std::fmt::Display for WmpError {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        let name = match self {
43            WmpError::Fail => "operation failed",
44            WmpError::NotYetImplemented => "not yet implemented",
45            WmpError::AbstractMethod => "abstract method",
46            WmpError::OutOfMemory => "out of memory",
47            WmpError::FileIO => "file I/O error",
48            WmpError::BufferOverflow => "buffer overflow",
49            WmpError::InvalidParameter => "invalid parameter",
50            WmpError::InvalidArgument => "invalid argument",
51            WmpError::UnsupportedFormat => "unsupported format",
52            WmpError::IncorrectCodecVersion => "incorrect codec version",
53            WmpError::IndexNotFound => "index not found",
54            WmpError::OutOfSequence => "out of sequence",
55            WmpError::NotInitialized => "not initialized",
56            WmpError::MustBeMultipleOf16LinesUntilLastCall => {
57                "must be a multiple of 16 lines until the last call"
58            }
59            WmpError::PlanarAlphaBandedEncRequiresTempFile => {
60                "planar alpha banded encoding requires a temp file"
61            }
62            WmpError::AlphaModeCannotBeTranscoded => "alpha mode cannot be transcoded",
63            WmpError::IncorrectCodecSubVersion => "incorrect codec sub-version",
64        };
65        f.write_str(name)
66    }
67}
68
69impl std::error::Error for WmpError {}
70
71mod api;
72mod common;
73mod image;
74mod jxrgluelib;
75
76pub use api::{
77    decode_bytes, decode_file, decode_reader, encode_to_path, encode_to_vec, encode_to_writer,
78    transcode_bytes_to_vec, transcode_file_to_path, transcode_reader_to_writer, BitDepth,
79    ChannelOrder, ColorFormat, DecodedImage, Decoder, Encoder, EncoderColorFormat, EncoderOptions,
80    Error, PixelFormat, PixelInterpretation, Result, SampleFormat,
81};