Skip to content

Commit

Permalink
fix edge case in decoder when very first op is QOI_OP_RUN
Browse files Browse the repository at this point in the history
  • Loading branch information
wx257osn2 committed Feb 24, 2024
1 parent 7a946d1 commit 6d50faa
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions include/qoixx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include<bit>
#include<numeric>
#include<array>
#include<utility>

#ifndef QOIXX_NO_SIMD
#if defined(__ARM_FEATURE_SVE)
Expand Down Expand Up @@ -1146,7 +1147,7 @@ class qoi{
static constexpr auto hash_diff_table = luma_hash_diff_table.data() + hash_table_offset;
)

const auto f = [&pixels, &p, &px_len, &size, &px, &index QOIXX_HPP_WITH_TABLES(, &hash)]{
const auto f = [&pixels, &p, &px_len, &size, &px, &index QOIXX_HPP_WITH_TABLES(, &hash)](bool first){
static constexpr std::uint32_t mask_tail_6 = 0b0011'1111u;
[[maybe_unused]] static constexpr std::uint32_t mask_tail_4 = 0b0000'1111u;
[[maybe_unused]] static constexpr std::uint32_t mask_tail_2 = 0b0000'0011u;
Expand Down Expand Up @@ -1187,6 +1188,13 @@ class qoi{
run = px_len;
px_len -= run;
QOIXX_HPP_DECODE_RUN(px, run)
if(first)[[unlikely]]{
QOIXX_HPP_WITH_TABLES(hash = (0*3+0*5+0*7+255*11) % index_size;)
if constexpr(std::is_same<rgba_t, qoi::rgba_t>::value)
index[QOIXX_HPP_WITH_TABLES(hash) QOIXX_HPP_WITHOUT_TABLES((0*3+0*5+0*7+255*11) % index_size)] = px;
else
efficient_memcpy<Channels>(index + QOIXX_HPP_WITH_TABLES(hash) QOIXX_HPP_WITHOUT_TABLES((0*3+0*5+0*7+255*11) % index_size), &px);
}
return;
}
if(b1 == chunk_tag::rgb){
Expand Down Expand Up @@ -1271,8 +1279,9 @@ class qoi{
push<Channels>(pixels, &px);
};

bool first = true;
while(px_len--)[[likely]]{
f();
f(std::exchange(first, false));
if(size < sizeof(padding))[[unlikely]]{
throw std::runtime_error("qoixx::qoi::decode: insufficient input data");
}
Expand Down

0 comments on commit 6d50faa

Please sign in to comment.