1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#[allow(clippy::wildcard_imports)]
use jpegxl_sys::*;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DecodeError {
#[error(transparent)]
InputError(#[from] std::io::Error),
#[error("Cannot create a decoder")]
CannotCreateDecoder,
#[error("Decoder error: {0}")]
GenericError(&'static str),
#[error("The file does not contain a valid codestream or container")]
InvalidFileFormat,
#[error("Cannot reconstruct JPEG codestream from the file")]
CannotReconstruct,
#[error("Unknown status: `{0:?}`")]
UnknownStatus(JxlDecoderStatus),
}
#[derive(Error, Debug)]
pub enum EncodeError {
#[error("Cannot create an encoder")]
CannotCreateEncoder,
#[error("Encoder error: {0}")]
GenericError(&'static str),
#[error("Encoder does not support it (yet)")]
NotSupported,
#[error("Unknown status: `{0:?}`")]
UnknownStatus(JxlEncoderStatus),
}
pub(crate) fn check_dec_status(
status: JxlDecoderStatus,
msg: &'static str,
) -> Result<(), DecodeError> {
match status {
JxlDecoderStatus::Success => Ok(()),
JxlDecoderStatus::Error => Err(DecodeError::GenericError(msg)),
_ => Err(DecodeError::UnknownStatus(status)),
}
}
pub(crate) fn check_enc_status(
status: JxlEncoderStatus,
msg: &'static str,
) -> Result<(), EncodeError> {
match status {
JxlEncoderStatus::Success => Ok(()),
JxlEncoderStatus::Error => Err(EncodeError::GenericError(msg)),
JxlEncoderStatus::NotSupported => Err(EncodeError::NotSupported),
JxlEncoderStatus::NeedMoreOutput => Err(EncodeError::UnknownStatus(status)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_invalid_data() {
let sample = Vec::new();
let decoder = crate::decoder_builder()
.build()
.expect("Failed to create decoder");
assert!(matches!(
decoder.decode_to::<u8>(&sample),
Err(DecodeError::UnknownStatus(JxlDecoderStatus::NeedMoreInput))
));
}
#[test]
fn encode_invalid_data() {
let mut encoder = crate::encoder_builder()
.has_alpha(true)
.build()
.expect("Failed to create encoder");
assert!(matches!(
encoder.encode::<u8, u8>(&[], 0, 0),
Err(EncodeError::GenericError("Set basic info"))
));
assert!(matches!(
encoder.encode::<f32, f32>(&[1.0, 1.0, 1.0, 0.5], 1, 1),
Err(EncodeError::NotSupported)
));
}
#[test]
fn mapping() {
assert!(matches!(
check_dec_status(JxlDecoderStatus::Error, "Testing"),
Err(DecodeError::GenericError("Testing"))
));
assert!(matches!(
check_dec_status(JxlDecoderStatus::NeedMoreInput, "Testing"),
Err(DecodeError::UnknownStatus(JxlDecoderStatus::NeedMoreInput))
));
assert!(matches!(
check_enc_status(JxlEncoderStatus::NeedMoreOutput, "Testing"),
Err(EncodeError::UnknownStatus(JxlEncoderStatus::NeedMoreOutput))
));
}
}