LibGfx: Remove WebP animation writer and utility

This went unused.
This commit is contained in:
Jelle Raaijmakers 2025-08-05 00:23:15 +02:00 committed by Andreas Kling
commit 62cf33b98e
Notes: github-actions[bot] 2025-08-05 09:31:30 +00:00
8 changed files with 2 additions and 482 deletions

View file

@ -11,9 +11,9 @@
#include <AK/Endian.h>
#include <AK/MemoryStream.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageFormats/AnimationWriter.h>
#include <LibGfx/ImageFormats/WebPShared.h>
#include <LibGfx/ImageFormats/WebPWriter.h>
#include <LibGfx/ImageFormats/WebPWriterLossless.h>
namespace Gfx {
@ -211,184 +211,4 @@ ErrorOr<void> WebPWriter::encode(Stream& stream, Bitmap const& bitmap, Options c
return {};
}
class WebPAnimationWriter : public AnimationWriter {
public:
WebPAnimationWriter(SeekableStream& stream, IntSize dimensions, u8 original_vp8x_flags, VP8LEncoderOptions vp8l_options)
: m_stream(stream)
, m_dimensions(dimensions)
, m_vp8x_flags(original_vp8x_flags)
, m_vp8l_options(vp8l_options)
{
}
virtual ErrorOr<void> add_frame(Bitmap&, int, IntPoint) override;
ErrorOr<void> update_size_in_header();
ErrorOr<void> set_alpha_bit_in_header();
private:
SeekableStream& m_stream;
IntSize m_dimensions;
u8 m_vp8x_flags { 0 };
VP8LEncoderOptions m_vp8l_options;
};
static ErrorOr<void> align_to_two(SeekableStream& stream)
{
return align_to_two(stream, TRY(stream.tell()));
}
static ErrorOr<void> write_ANMF_chunk_header(Stream& stream, ANMFChunkHeader const& chunk, size_t payload_size)
{
if (chunk.frame_width > (1 << 24) || chunk.frame_height > (1 << 24))
return Error::from_string_literal("WebP dimensions too large for ANMF chunk");
if (chunk.frame_width == 0 || chunk.frame_height == 0)
return Error::from_string_literal("WebP lossless animation frames must be at least one pixel wide and tall");
if (chunk.frame_x % 2 != 0 || chunk.frame_y % 2 != 0)
return Error::from_string_literal("WebP lossless animation frames must be at at even coordinates");
dbgln_if(WEBP_DEBUG, "writing ANMF frame_x {} frame_y {} frame_width {} frame_height {} frame_duration {} blending_method {} disposal_method {}",
chunk.frame_x, chunk.frame_y, chunk.frame_width, chunk.frame_height, chunk.frame_duration_in_milliseconds, (int)chunk.blending_method, (int)chunk.disposal_method);
TRY(write_chunk_header(stream, "ANMF"sv, 16 + payload_size));
LittleEndianOutputBitStream bit_stream { MaybeOwned<Stream>(stream) };
// "Frame X: 24 bits (uint24)
// The X coordinate of the upper left corner of the frame is Frame X * 2."
TRY(bit_stream.write_bits(chunk.frame_x / 2, 24u));
// "Frame Y: 24 bits (uint24)
// The Y coordinate of the upper left corner of the frame is Frame Y * 2."
TRY(bit_stream.write_bits(chunk.frame_y / 2, 24u));
// "Frame Width: 24 bits (uint24)
// The 1-based width of the frame. The frame width is 1 + Frame Width Minus One."
TRY(bit_stream.write_bits(chunk.frame_width - 1, 24u));
// "Frame Height: 24 bits (uint24)
// The 1-based height of the frame. The frame height is 1 + Frame Height Minus One."
TRY(bit_stream.write_bits(chunk.frame_height - 1, 24u));
// "Frame Duration: 24 bits (uint24)"
TRY(bit_stream.write_bits(chunk.frame_duration_in_milliseconds, 24u));
// Don't use bit_stream.write_bits() to write individual flags here:
// The spec describes bit flags in MSB to LSB order, but write_bits() writes LSB to MSB.
u8 flags = 0;
// "Reserved: 6 bits
// MUST be 0. Readers MUST ignore this field."
// "Blending method (B): 1 bit"
if (chunk.blending_method == ANMFChunkHeader::BlendingMethod::DoNotBlend)
flags |= 0x2;
// "Disposal method (D): 1 bit"
if (chunk.disposal_method == ANMFChunkHeader::DisposalMethod::DisposeToBackgroundColor)
flags |= 0x1;
TRY(bit_stream.write_bits(flags, 8u));
// FIXME: Make ~LittleEndianOutputBitStream do this, or make it VERIFY() that it has happened at least.
TRY(bit_stream.flush_buffer_to_stream());
return {};
}
ErrorOr<void> WebPAnimationWriter::add_frame(Bitmap& bitmap, int duration_ms, IntPoint at)
{
if (at.x() < 0 || at.y() < 0 || at.x() + bitmap.width() > m_dimensions.width() || at.y() + bitmap.height() > m_dimensions.height())
return Error::from_string_literal("Frame does not fit in animation dimensions");
// Since we have a SeekableStream, we could write both the VP8L chunk header and the ANMF chunk header with a placeholder size,
// compress the frame data directly to the stream, and then go back and update the two sizes.
// That's pretty messy though, and the compressed image data is smaller than the uncompressed bitmap passed in. So we'll buffer it.
bool is_fully_opaque;
auto vp8l_data_bytes = TRY(compress_VP8L_image_data(bitmap, m_vp8l_options, is_fully_opaque));
ANMFChunkHeader chunk;
chunk.frame_x = static_cast<u32>(at.x());
chunk.frame_y = static_cast<u32>(at.y());
chunk.frame_width = static_cast<u32>(bitmap.width());
chunk.frame_height = static_cast<u32>(bitmap.height());
chunk.frame_duration_in_milliseconds = static_cast<u32>(duration_ms);
chunk.blending_method = ANMFChunkHeader::BlendingMethod::DoNotBlend;
chunk.disposal_method = ANMFChunkHeader::DisposalMethod::DoNotDispose;
TRY(write_ANMF_chunk_header(m_stream, chunk, compute_VP8L_chunk_size(vp8l_data_bytes)));
bool alpha_is_used_hint = !is_fully_opaque;
TRY(write_VP8L_chunk(m_stream, bitmap.width(), bitmap.height(), alpha_is_used_hint, vp8l_data_bytes));
TRY(update_size_in_header());
if (!(m_vp8x_flags & 0x10) && !is_fully_opaque)
TRY(set_alpha_bit_in_header());
return {};
}
ErrorOr<void> WebPAnimationWriter::update_size_in_header()
{
auto current_offset = TRY(m_stream.tell());
TRY(m_stream.seek(4, SeekMode::SetPosition));
VERIFY(current_offset > 8);
TRY(m_stream.write_value<LittleEndian<u32>>(current_offset - 8));
TRY(m_stream.seek(current_offset, SeekMode::SetPosition));
return {};
}
ErrorOr<void> WebPAnimationWriter::set_alpha_bit_in_header()
{
m_vp8x_flags |= 0x10;
auto current_offset = TRY(m_stream.tell());
// 4 bytes for "RIFF",
// 4 bytes RIFF chunk size (i.e. file size - 8),
// 4 bytes for "WEBP",
// 4 bytes for "VP8X",
// 4 bytes for VP8X chunk size,
// followed by VP8X flags in the first byte of the VP8X chunk data.
TRY(m_stream.seek(20, SeekMode::SetPosition));
TRY(m_stream.write_value<u8>(m_vp8x_flags));
TRY(m_stream.seek(current_offset, SeekMode::SetPosition));
return {};
}
static ErrorOr<void> write_ANIM_chunk(Stream& stream, ANIMChunk const& chunk)
{
TRY(write_chunk_header(stream, "ANIM"sv, 6)); // Size of the ANIM chunk.
TRY(stream.write_value<LittleEndian<u32>>(chunk.background_color));
TRY(stream.write_value<LittleEndian<u16>>(chunk.loop_count));
return {};
}
ErrorOr<NonnullOwnPtr<AnimationWriter>> WebPWriter::start_encoding_animation(SeekableStream& stream, IntSize dimensions, int loop_count, Color background_color, Options const& options)
{
// We'll update the stream with the actual size later.
TRY(write_webp_header(stream, 0));
VP8XHeader vp8x_header;
vp8x_header.has_icc = options.icc_data.has_value();
vp8x_header.width = dimensions.width();
vp8x_header.height = dimensions.height();
vp8x_header.has_animation = true;
TRY(write_VP8X_chunk(stream, vp8x_header));
VERIFY(TRY(stream.tell()) % 2 == 0);
ByteBuffer iccp_chunk_bytes;
if (options.icc_data.has_value()) {
TRY(write_chunk_header(stream, "ICCP"sv, options.icc_data.value().size()));
TRY(stream.write_until_depleted(options.icc_data.value()));
TRY(align_to_two(stream));
}
TRY(write_ANIM_chunk(stream, { .background_color = background_color.value(), .loop_count = static_cast<u16>(loop_count) }));
auto writer = make<WebPAnimationWriter>(stream, dimensions, vp8x_flags_from_header(vp8x_header), options.vp8l_options);
TRY(writer->update_size_in_header());
return writer;
}
}