mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibGUI: Add a Clipboard API for retrieving a copied Gfx::Bitmap
The returned bitmap is always going to be a 32-bit RGBA bitmap for now. In the future we may want to support copy-pasting other formats.
This commit is contained in:
parent
2e6d59b7b2
commit
158f3b9362
Notes:
sideshowbarker
2024-07-19 02:54:36 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/158f3b93629
2 changed files with 44 additions and 1 deletions
|
@ -120,9 +120,51 @@ void ClipboardServerConnection::handle(const Messages::ClipboardClient::Clipboar
|
|||
clipboard.on_change(message.mime_type());
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> Clipboard::bitmap() const
|
||||
{
|
||||
auto clipping = data_and_type();
|
||||
|
||||
if (clipping.mime_type != "image/x-serenityos")
|
||||
return nullptr;
|
||||
|
||||
auto width = clipping.metadata.get("width").value_or("0").to_uint();
|
||||
if (!width.has_value() || width.value() == 0)
|
||||
return nullptr;
|
||||
|
||||
auto height = clipping.metadata.get("height").value_or("0").to_uint();
|
||||
if (!height.has_value() || height.value() == 0)
|
||||
return nullptr;
|
||||
|
||||
auto pitch = clipping.metadata.get("pitch").value_or("0").to_uint();
|
||||
if (!pitch.has_value() || pitch.value() == 0)
|
||||
return nullptr;
|
||||
|
||||
auto format = clipping.metadata.get("format").value_or("0").to_uint();
|
||||
if (!format.has_value() || format.value() == 0)
|
||||
return nullptr;
|
||||
|
||||
auto clipping_bitmap = Gfx::Bitmap::create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, pitch.value(), (Gfx::RGBA32*)clipping.data.data());
|
||||
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { (int)width.value(), (int)height.value() });
|
||||
|
||||
for (int y = 0; y < clipping_bitmap->height(); ++y) {
|
||||
for (int x = 0; x < clipping_bitmap->width(); ++x) {
|
||||
auto pixel = clipping_bitmap->get_pixel(x, y);
|
||||
bitmap->set_pixel(x, y, pixel);
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
void Clipboard::set_bitmap(const Gfx::Bitmap& bitmap)
|
||||
{
|
||||
(void) bitmap;
|
||||
HashMap<String, String> metadata;
|
||||
metadata.set("width", String::number(bitmap.width()));
|
||||
metadata.set("height", String::number(bitmap.height()));
|
||||
metadata.set("format", String::number((int)bitmap.format()));
|
||||
metadata.set("pitch", String::number(bitmap.pitch()));
|
||||
metadata.set("bpp", String::number(bitmap.bpp()));
|
||||
set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", metadata);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ public:
|
|||
}
|
||||
|
||||
void set_bitmap(const Gfx::Bitmap&);
|
||||
RefPtr<Gfx::Bitmap> bitmap() const;
|
||||
|
||||
struct DataAndType {
|
||||
ByteBuffer data;
|
||||
|
|
Loading…
Reference in a new issue