Windows clipboard for images improved

This commit is contained in:
Yousef Khadadeh 2026-01-12 15:02:55 +03:00
parent afaf43823c
commit db8418159e
2 changed files with 67 additions and 38 deletions

View file

@ -32,7 +32,11 @@ cpack -G DragNDrop
## Windows ## Windows
The windows version of this program is built on Visual Studio 2022's compiler. The windows version of this program is built on Visual Studio 2022's compiler.
After cloning the repository, `cd` into the repo, then run: After cloning the repository, `cd` into the repo, then update the git submodules to get the `clip` library:
```
git submodule update --init --recursive
```
Then run:
``` ```
.\conan\export_libs.bat .\conan\export_libs.bat
conan install . --build=missing -pr=conan/profiles/win-x86_64 conan install . --build=missing -pr=conan/profiles/win-x86_64

View file

@ -147,6 +147,26 @@ std::string InputManager::get_clipboard_str_SDL() {
return toRet; return toRet;
} }
#ifdef _WIN32
// From clip library example, useful for debugging
void print_clip_image_spec(const clip::image_spec& spec) {
std::cout << "Image in clipboard "
<< spec.width << "x" << spec.height
<< " (" << spec.bits_per_pixel << "bpp)\n"
<< "Format:" << "\n"
<< std::hex
<< " Red mask: " << spec.red_mask << "\n"
<< " Green mask: " << spec.green_mask << "\n"
<< " Blue mask: " << spec.blue_mask << "\n"
<< " Alpha mask: " << spec.alpha_mask << "\n"
<< std::dec
<< " Red shift: " << spec.red_shift << "\n"
<< " Green shift: " << spec.green_shift << "\n"
<< " Blue shift: " << spec.blue_shift << "\n"
<< " Alpha shift: " << spec.alpha_shift << "\n";
}
#endif
void InputManager::get_clipboard_image_data_SDL(const std::function<void(std::string_view data)>& callback) { void InputManager::get_clipboard_image_data_SDL(const std::function<void(std::string_view data)>& callback) {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
static std::unordered_set<std::string> validMimetypes; static std::unordered_set<std::string> validMimetypes;
@ -167,46 +187,51 @@ void InputManager::get_clipboard_image_data_SDL(const std::function<void(std::st
if(validMimetypes.contains(mimeTypes[i])) { if(validMimetypes.contains(mimeTypes[i])) {
if(std::string(mimeTypes[i]) == "image/bmp") { if(std::string(mimeTypes[i]) == "image/bmp") {
#ifdef _WIN32_ #ifdef _WIN32_
if(!clip::has(clip::image_format())) { std::vector<uint8_t> result;
Logger::get().log("INFO", "[InputManager::get_clipboard_image_data_SDL] Clipboard doesn't contain an image"); try {
return; if(!clip::has(clip::image_format()))
} throw std::runtime_error("Clipboard doesn't contain an image");
clip::image img; clip::image img;
if(!clip::get_image(img)) { if(!clip::get_image(img))
Logger::get().log("INFO", "[InputManager::get_clipboard_image_data_SDL] Error getting image from clipboard"); throw std::runtime_error("Error getting image from clipboard");
return;
}
clip::image_spec spec = img.spec(); clip::image_spec spec = img.spec();
if(spec.bits_per_pixel != 8) { SDL_PixelFormat pFormat = SDL_GetPixelFormatForMasks(spec.bits_per_pixel, spec.red_mask, spec.green_mask, spec.blue_mask, spec.alpha_mask);
Logger::get().log("INFO", "[InputManager::get_clipboard_image_data_SDL] Clipboard doesn't use 8 bits per pixel"); if(pFormat != SDL_PIXELFORMAT_UNKNOWN) {
return; SDL_Surface* surfOriginal = SDL_CreateSurfaceFrom(spec.width, spec.height, pFormat, img.data(), spec.bytes_per_row);
} if(surfOriginal) {
SDL_LockSurface(surfOriginal);
char* rawImageData = img.data(); SDL_Surface* cSurf = SDL_ConvertSurface(surfOriginal, SDL_PIXELFORMAT_RGBA32);
std::vector<uint8_t> rgbaImageData(spec.width * spec.height * 4); SDL_UnlockSurface(surfOriginal);
for(size_t y = 0; y < spec.height; y++) { SDL_DestroySurface(surfOriginal);
for(size_t x = 0; x < spec.width; x++) { if(cSurf) {
size_t rawDataIndex = x + y * spec.bytes_per_row; SDL_LockSurface(cSurf);
size_t rgbaDataIndex = x + y * spec.height; SkDynamicMemoryWStream out;
unsigned long rawDataAtIndex = *reinterpret_cast<unsigned long*>(rawImageData + rawDataIndex); SkPngEncoder::Encode(&out, SkPixmap(SkImageInfo::Make(cSurf->w, cSurf->h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType), cSurf->pixels, cSurf->pitch), {});
rgbaImageData[rgbaDataIndex + 0] = (rawDataAtIndex & spec.red_mask) >> spec.red_shift; out.flush();
rgbaImageData[rgbaDataIndex + 1] = (rawDataAtIndex & spec.green_mask) >> spec.green_shift; SDL_UnlockSurface(cSurf);
rgbaImageData[rgbaDataIndex + 2] = (rawDataAtIndex & spec.blue_mask) >> spec.blue_mask; SDL_DestroySurface(cSurf);
if(spec.alpha_mask == 0) result = out.detachAsVector();
rgbaImageData[rgbaDataIndex + 3] = 255; }
else
throw std::runtime_error("Bitmap could not be converted to new format");
}
else else
rgbaImageData[rgbaDataIndex + 3] = (rawDataAtIndex & spec.alpha_mask) >> spec.alpha_mask; throw std::runtime_error("Bitmap could not be read into surface");
} }
else
throw std::runtime_error("Bitmap format is unsupported");
} }
catch(const std::runtime_error& e) {
SkDynamicMemoryWStream out; Logger::get().log("INFO", std::string("[InputManager::get_clipboard_image_data_SDL] Error thrown: ") + e.what());
SkPngEncoder::Encode(&out, SkPixmap(SkImageInfo::Make(spec.width, spec.height, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType), rgbaImageData.data(), rgbaImageData.size()), {}); }
out.flush(); catch(...) {
std::vector<uint8_t> result = out.detachAsVector(); Logger::get().log("INFO", "[InputManager::get_clipboard_image_data_SDL] Unknown error thrown");
callback(std::string_view(reinterpret_cast<char*>(result.data()), result.size())); }
if(!result.empty())
callback(std::string_view((char*)(result.data()), result.size())); // This part of the code should throw errors
#endif #endif
} }
else { else {