1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-29 06:55:07 +00:00

LibGfx+Userland: Make PNGWriter::encode() return ErrorOr<ByteBuffer>

This is a first step towards handling PNG encoding failures instead of
just falling over and crashing the program.

This initial step will cause encode() to return an error if the final
ByteBuffer copy fails to allocate. There are more potential failures
that will be surfaced by subsequent commits.

Two FIXMEs were killed in the making of this patch. :^)
This commit is contained in:
Andreas Kling 2022-12-05 20:34:27 +01:00 committed by Sam Atkins
parent 95b4ffad39
commit d88c7fee32
9 changed files with 18 additions and 13 deletions

View file

@ -176,8 +176,12 @@ DeprecatedString HTMLCanvasElement::to_data_url(DeprecatedString const& type, [[
return {};
if (type != "image/png")
return {};
auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap);
return AK::URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_deprecated_string();
auto encoded_bitmap_or_error = Gfx::PNGWriter::encode(*m_bitmap);
if (encoded_bitmap_or_error.is_error()) {
dbgln("Gfx::PNGWriter failed to encode the HTMLCanvasElement: {}", encoded_bitmap_or_error.error());
return {};
}
return AK::URL::create_with_data(type, encode_base64(encoded_bitmap_or_error.value()), true).to_deprecated_string();
}
void HTMLCanvasElement::present()