Skip to content

Commit 3a1ce94

Browse files
authored
fix transparent background bug (#17)
* fix transparent background bug * handle edge case before loop for slices
1 parent 5669719 commit 3a1ce94

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,5 +1,5 @@
11
use qoi::{
2-
consts::{QOI_OP_RGB, QOI_OP_RUN},
2+
consts::{QOI_OP_RGB, QOI_OP_RUN, QOI_OP_INDEX},
33
decode_to_vec, Channels, ColorSpace, Header, Result,
44
};
55

@@ -22,6 +22,18 @@ fn test_start_with_qoi_op_run() -> Result<()> {
2222
Ok(())
2323
}
2424

25+
#[test]
26+
fn test_start_with_qoi_op_run_and_use_index() -> Result<()> {
27+
let header = Header::try_new(4, 1, Channels::Rgba, ColorSpace::Linear)?;
28+
let mut qoi_data: Vec<_> = header.encode().into_iter().collect();
29+
qoi_data.extend([QOI_OP_RUN | 1, QOI_OP_RGB, 10, 20, 30, QOI_OP_INDEX | 53]);
30+
qoi_data.extend([0; 7]);
31+
qoi_data.push(1);
32+
let (_, decoded) = decode_to_vec(&qoi_data)?;
33+
assert_eq!(decoded, vec![0, 0, 0, 255, 0, 0, 0, 255, 10, 20, 30, 255, 0, 0, 0, 255]);
34+
Ok(())
35+
}
36+
2537
#[cfg(target_endian = "big")]
2638
#[test]
2739
fn test_big_endian() {

0 commit comments

Comments
 (0)