1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

LibGfx: Use ErrorOr<T> for try_create_from_serialized_byte_buffer()

This commit is contained in:
Andreas Kling 2021-11-06 19:53:32 +01:00
parent 0de33b3d6c
commit 6e255b262f
3 changed files with 10 additions and 15 deletions

View file

@ -33,7 +33,7 @@ DragOperation::Outcome DragOperation::exec()
Gfx::ShareableBitmap drag_bitmap; Gfx::ShareableBitmap drag_bitmap;
if (m_mime_data->has_format("image/x-raw-bitmap")) { if (m_mime_data->has_format("image/x-raw-bitmap")) {
auto data = m_mime_data->data("image/x-raw-bitmap"); auto data = m_mime_data->data("image/x-raw-bitmap");
auto bitmap = Gfx::Bitmap::try_create_from_serialized_byte_buffer(move(data)); auto bitmap = Gfx::Bitmap::try_create_from_serialized_byte_buffer(move(data)).release_value_but_fixme_should_propagate_errors();
drag_bitmap = bitmap->to_shareable_bitmap(); drag_bitmap = bitmap->to_shareable_bitmap();
} }

View file

@ -211,7 +211,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_with_anonymous_buffer(BitmapFo
/// - palette count /// - palette count
/// - palette data (= palette count * BGRA8888) /// - palette data (= palette count * BGRA8888)
/// - image data (= actual size * u8) /// - image data (= actual size * u8)
RefPtr<Bitmap> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer)
{ {
InputMemoryStream stream { buffer }; InputMemoryStream stream { buffer };
size_t actual_size; size_t actual_size;
@ -229,35 +229,31 @@ RefPtr<Bitmap> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffe
}; };
if (!read(actual_size) || !read(width) || !read(height) || !read(scale_factor) || !read(format) || !read(palette_size)) if (!read(actual_size) || !read(width) || !read(height) || !read(scale_factor) || !read(format) || !read(palette_size))
return nullptr; return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv);
if (format > BitmapFormat::BGRA8888 || format < BitmapFormat::Indexed1) if (format > BitmapFormat::BGRA8888 || format < BitmapFormat::Indexed1)
return nullptr; return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv);
if (!check_size({ width, height }, scale_factor, format, actual_size)) if (!check_size({ width, height }, scale_factor, format, actual_size))
return {}; return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv);
palette.ensure_capacity(palette_size); palette.ensure_capacity(palette_size);
for (size_t i = 0; i < palette_size; ++i) { for (size_t i = 0; i < palette_size; ++i) {
if (!read(palette[i])) if (!read(palette[i]))
return {}; return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv);
} }
if (stream.remaining() < actual_size) if (stream.remaining() < actual_size)
return {}; return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv);
auto data = stream.bytes().slice(stream.offset(), actual_size); auto data = stream.bytes().slice(stream.offset(), actual_size);
auto bitmap_or_error = Bitmap::try_create(format, { width, height }, scale_factor); auto bitmap = TRY(Bitmap::try_create(format, { width, height }, scale_factor));
if (bitmap_or_error.is_error())
return {};
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
bitmap->m_palette = new RGBA32[palette_size]; bitmap->m_palette = new RGBA32[palette_size];
memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32)); memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32));
data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() }); data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() });
return bitmap; return bitmap;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,7 +8,6 @@
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <LibCore/AnonymousBuffer.h> #include <LibCore/AnonymousBuffer.h>
#include <LibGfx/Color.h> #include <LibGfx/Color.h>
#include <LibGfx/Forward.h> #include <LibGfx/Forward.h>
@ -96,7 +95,7 @@ public:
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_file(String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_file(String const& path, int scale_factor = 1);
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1);
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector<RGBA32> const& palette); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector<RGBA32> const& palette);
[[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer); static ErrorOr<NonnullRefPtr<Bitmap>> try_create_from_serialized_byte_buffer(ByteBuffer&&);
static bool is_path_a_supported_image_format(StringView const& path) static bool is_path_a_supported_image_format(StringView const& path)
{ {