Skip to content

Commit 09dadb0

Browse files
authored
Merge pull request #5 from elmarco/issue-16
decode: fix transparent background bug
2 parents 75c7f18 + f8805ca commit 09dadb0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/decode.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ where
3535
let mut px = Pixel::<N>::new().with_a(0xff);
3636
let mut px_rgba: Pixel<4>;
3737

38+
if matches!(data, [QOI_OP_RUN..=QOI_OP_RUN_END, ..]) {
39+
px_rgba = px.as_rgba(0xff);
40+
index[px_rgba.hash_index() as usize] = px_rgba;
41+
}
42+
3843
while let [px_out, ptail @ ..] = pixels {
3944
pixels = ptail;
4045
match data {
@@ -178,6 +183,7 @@ where
178183
let (phead, ptail) = pixels.split_at_mut(run); // can't panic
179184
phead.fill(px.into());
180185
pixels = ptail;
186+
index[px.hash_index() as usize] = px;
181187
continue;
182188
}
183189
QOI_OP_DIFF..=QOI_OP_DIFF_END => {

tests/test_misc.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(not(feature = "std"), allow(unused_imports, dead_code))]
22

33
use qoi::{
4-
consts::{QOI_OP_RGB, QOI_OP_RUN},
4+
consts::{QOI_OP_INDEX, QOI_OP_RGB, QOI_OP_RUN},
55
decode_to_vec, Channels, ColorSpace, Decoder, Header, Result,
66
};
77

@@ -62,6 +62,18 @@ fn test_start_with_qoi_op_run() -> Result<()> {
6262
Ok(())
6363
}
6464

65+
#[test]
66+
fn test_start_with_qoi_op_run_and_use_index() -> Result<()> {
67+
let header = Header::try_new(4, 1, Channels::Rgba, ColorSpace::Linear)?;
68+
let mut qoi_data: Vec<_> = header.encode().into_iter().collect();
69+
qoi_data.extend([QOI_OP_RUN | 1, QOI_OP_RGB, 10, 20, 30, QOI_OP_INDEX | 53]);
70+
qoi_data.extend([0; 7]);
71+
qoi_data.push(1);
72+
let (_, decoded) = decode_to_vec(&qoi_data)?;
73+
assert_eq!(decoded, vec![0, 0, 0, 255, 0, 0, 0, 255, 10, 20, 30, 255, 0, 0, 0, 255]);
74+
Ok(())
75+
}
76+
6577
#[cfg(target_endian = "big")]
6678
#[test]
6779
fn test_big_endian() {

0 commit comments

Comments
 (0)