/* * Copyright (c) 2021, Kyle Pereira * * SPDX-License-Identifier: BSD-2-Clause */ #include "ClipboardServerConnection.h" #include #include #include // Copied from LibGUI/Clipboard.cpp RefPtr ClipboardServerConnection::get_bitmap() { auto clipping = get_clipboard_data(); if (clipping.mime_type() != "image/x-serenityos") return nullptr; HashMap const& metadata = clipping.metadata().entries(); auto width = metadata.get("width").value_or("0").to_uint(); if (!width.has_value() || width.value() == 0) return nullptr; auto height = metadata.get("height").value_or("0").to_uint(); if (!height.has_value() || height.value() == 0) return nullptr; auto scale = metadata.get("scale").value_or("0").to_uint(); if (!scale.has_value() || scale.value() == 0) return nullptr; auto pitch = metadata.get("pitch").value_or("0").to_uint(); if (!pitch.has_value() || pitch.value() == 0) return nullptr; auto format = metadata.get("format").value_or("0").to_uint(); if (!format.has_value() || format.value() == 0) return nullptr; auto data = clipping.data().data(); auto clipping_bitmap = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), const_cast(data)); auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value()); for (int y = 0; y < clipping_bitmap->physical_height(); ++y) { for (int x = 0; x < clipping_bitmap->physical_width(); ++x) { auto pixel = clipping_bitmap->get_pixel(x, y); bitmap->set_pixel(x, y, pixel); } } return bitmap; } // Copied from LibGUI/Clipboard.cpp void ClipboardServerConnection::set_bitmap(Gfx::Bitmap const& bitmap) { HashMap metadata; metadata.set("width", String::number(bitmap.width())); metadata.set("height", String::number(bitmap.height())); metadata.set("scale", String::number(bitmap.scale())); metadata.set("format", String::number((int)bitmap.format())); metadata.set("pitch", String::number(bitmap.pitch())); ReadonlyBytes data { bitmap.scanline(0), bitmap.size_in_bytes() }; auto buffer = Core::AnonymousBuffer::create_with_size(bitmap.size_in_bytes()); memcpy(buffer.data(), data.data(), data.size()); this->async_set_clipboard_data(buffer, "image/x-serenityos", metadata); }