Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Optimize encoding by reducing branches for Qoi::RUN
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Joruk committed Nov 28, 2021
1 parent b6ffeaa commit 3f3ee0a
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ where

impl IsBetween for i16 {}

#[inline]
fn write_run(dest: &mut [u8], dest_pos: &mut usize, run: &mut u16) {
if *run < 33 {
*run -= 1;
dest[0] = Qoi::RUN_8 | (*run as u8);
*dest_pos += 1;
} else {
*run -= 33;
dest[0] = Qoi::RUN_16 | ((*run >> 8u16) as u8);
*dest_pos += 1;

dest[1] = *run as u8;
*dest_pos += 1;
}

*run = 0;
}

#[inline]
fn can_diff_8(dr: i16, dg: i16, db: i16, da: i16) -> bool {
da == 0 && dr.is_between(-2, 1) && dg.is_between(-2, 1) && db.is_between(-2, 1)
Expand Down Expand Up @@ -103,26 +121,15 @@ where

if pixel == previous_pixel {
run += 1;
}

if run > 0 && (pixel != previous_pixel || run == 0x2020 || index == last_chunk_index) {
if run < 33 {
run -= 1;
dest[dest_pos] = Qoi::RUN_8 | (run as u8);
dest_pos += 1;
} else {
run -= 33;
dest[dest_pos] = Qoi::RUN_16 | ((run >> 8u16) as u8);
dest_pos += 1;

dest[dest_pos] = run as u8;
dest_pos += 1;
if run == 0x2020 || index == last_chunk_index {
write_run(&mut dest[dest_pos..], &mut dest_pos, &mut run);
}
} else {
if run > 0 {
write_run(&mut dest[dest_pos..], &mut dest_pos, &mut run);
}

run = 0;
}

if pixel != previous_pixel {
let cache_index = pixel.cache_index();

if pixel == cache[cache_index] {
Expand Down

0 comments on commit 3f3ee0a

Please sign in to comment.