ladybird/Userland/Libraries/LibGfx/ImageFormats/WebPSharedLossless.cpp
Nico Weber ed2658d72c LibGfx/WebP: Move some to-be-shared code to WebPSharedLossless.h
No behavior change. No measurable performance different either.

(I tried `hyperfine 'Build/lagom/bin/image --no-output foo.webp'`
for a few input images before and after this change, and I didn't
see a difference. I also tried if moving both
Gfx::CanonicalCode::read_symbol() and
Compress::CanonicalCode::read_symbol() inline, and that didn't
help either.)
2024-05-20 13:17:34 -04:00

35 lines
952 B
C++

/*
* Copyright (c) 2024, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ImageFormats/WebPSharedLossless.h>
namespace Gfx {
ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
{
auto non_zero_symbol_count = 0;
auto last_non_zero_symbol = -1;
for (size_t i = 0; i < bytes.size(); i++) {
if (bytes[i] != 0) {
non_zero_symbol_count++;
last_non_zero_symbol = i;
}
}
if (non_zero_symbol_count == 1)
return CanonicalCode(last_non_zero_symbol);
return CanonicalCode(TRY(Compress::CanonicalCode::from_bytes(bytes)));
}
ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& bit_stream) const
{
return TRY(m_code.visit(
[](u32 single_code) -> ErrorOr<u32> { return single_code; },
[&bit_stream](Compress::CanonicalCode const& code) { return code.read_symbol(bit_stream); }));
}
}