From d5cfc60013c69f2f50fcc91b41eaa227dec065c8 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Tue, 4 Jan 2022 16:53:28 +0300 Subject: [PATCH] Fix an edge case bug with non-reference encoding --- src/encode.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/encode.rs b/src/encode.rs index ec66b74..1d60bb8 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -15,7 +15,7 @@ use crate::types::{Channels, ColorSpace}; use crate::utils::GenericWriter; use crate::utils::{unlikely, BytesMut, Writer}; -#[allow(clippy::cast_possible_truncation, unused_assignments)] +#[allow(clippy::cast_possible_truncation, unused_assignments, unused_variables)] fn encode_impl(mut buf: W, data: &[u8]) -> Result where Pixel: SupportedChannels, @@ -28,6 +28,7 @@ where let mut hash_prev = Pixel::::new().with_a(0xff).hash_index(); let mut run = 0_u8; let mut px = Pixel::::new().with_a(0xff); + let mut index_allowed = N == 3; let n_pixels = data.len() / N; @@ -44,7 +45,7 @@ where #[cfg(not(feature = "reference"))] { // credits for the original idea: @zakarumych - buf = buf.write_one(if run == 1 && i != 1 { + buf = buf.write_one(if run == 1 && index_allowed { QOI_OP_INDEX | (hash_prev as u8) } else { QOI_OP_RUN | (run - 1) @@ -56,6 +57,7 @@ where } run = 0; } + index_allowed = true; let px_rgba = px.as_rgba(0xff); hash_prev = px_rgba.hash_index(); let index_px = &mut index[hash_prev as usize];