deskflow/src/lib/platform/XWindowsClipboardAnyBitmapConverter.cpp

167 lines
5.3 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2004 Chris Schoeneman
*
2012-06-10 16:50:54 +00:00
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
2012-06-10 16:50:54 +00:00
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "platform/XWindowsClipboardAnyBitmapConverter.h"
2012-06-10 16:50:54 +00:00
// BMP info header structure
struct CBMPInfoHeader
{
2012-06-10 16:50:54 +00:00
public:
UInt32 biSize;
SInt32 biWidth;
SInt32 biHeight;
2025-01-11 00:58:26 +00:00
uint16_t biPlanes;
uint16_t biBitCount;
UInt32 biCompression;
UInt32 biSizeImage;
SInt32 biXPelsPerMeter;
SInt32 biYPelsPerMeter;
UInt32 biClrUsed;
UInt32 biClrImportant;
2012-06-10 16:50:54 +00:00
};
// BMP is little-endian
2025-01-11 00:58:26 +00:00
static void toLE(uint8_t *&dst, uint16_t src)
{
2025-01-11 00:09:43 +00:00
dst[0] = static_cast<uint8_t>(src & 0xffu);
dst[1] = static_cast<uint8_t>((src >> 8) & 0xffu);
dst += 2;
2012-06-10 16:50:54 +00:00
}
2025-01-11 00:09:43 +00:00
static void toLE(uint8_t *&dst, SInt32 src)
{
2025-01-11 00:09:43 +00:00
dst[0] = static_cast<uint8_t>(src & 0xffu);
dst[1] = static_cast<uint8_t>((src >> 8) & 0xffu);
dst[2] = static_cast<uint8_t>((src >> 16) & 0xffu);
dst[3] = static_cast<uint8_t>((src >> 24) & 0xffu);
dst += 4;
2012-06-10 16:50:54 +00:00
}
2025-01-11 00:09:43 +00:00
static void toLE(uint8_t *&dst, UInt32 src)
{
2025-01-11 00:09:43 +00:00
dst[0] = static_cast<uint8_t>(src & 0xffu);
dst[1] = static_cast<uint8_t>((src >> 8) & 0xffu);
dst[2] = static_cast<uint8_t>((src >> 16) & 0xffu);
dst[3] = static_cast<uint8_t>((src >> 24) & 0xffu);
dst += 4;
2012-06-10 16:50:54 +00:00
}
2025-01-11 00:58:26 +00:00
static inline uint16_t fromLEU16(const uint8_t *data)
{
2025-01-11 00:58:26 +00:00
return static_cast<uint16_t>(data[0]) | (static_cast<uint16_t>(data[1]) << 8);
2012-06-10 16:50:54 +00:00
}
2025-01-11 00:09:43 +00:00
static inline SInt32 fromLES32(const uint8_t *data)
{
return static_cast<SInt32>(
static_cast<UInt32>(data[0]) | (static_cast<UInt32>(data[1]) << 8) | (static_cast<UInt32>(data[2]) << 16) |
(static_cast<UInt32>(data[3]) << 24)
);
2012-06-10 16:50:54 +00:00
}
2025-01-11 00:09:43 +00:00
static inline UInt32 fromLEU32(const uint8_t *data)
{
return static_cast<UInt32>(data[0]) | (static_cast<UInt32>(data[1]) << 8) | (static_cast<UInt32>(data[2]) << 16) |
(static_cast<UInt32>(data[3]) << 24);
2012-06-10 16:50:54 +00:00
}
//
2014-11-12 11:28:41 +00:00
// XWindowsClipboardAnyBitmapConverter
2012-06-10 16:50:54 +00:00
//
XWindowsClipboardAnyBitmapConverter::XWindowsClipboardAnyBitmapConverter()
{
// do nothing
2012-06-10 16:50:54 +00:00
}
XWindowsClipboardAnyBitmapConverter::~XWindowsClipboardAnyBitmapConverter()
{
// do nothing
2012-06-10 16:50:54 +00:00
}
IClipboard::EFormat XWindowsClipboardAnyBitmapConverter::getFormat() const
{
return IClipboard::kBitmap;
2012-06-10 16:50:54 +00:00
}
int XWindowsClipboardAnyBitmapConverter::getDataSize() const
{
return 8;
}
2012-06-10 16:50:54 +00:00
std::string XWindowsClipboardAnyBitmapConverter::fromIClipboard(const std::string &bmp) const
{
// fill BMP info header with native-endian data
CBMPInfoHeader infoHeader;
2025-01-11 00:09:43 +00:00
const uint8_t *rawBMPInfoHeader = reinterpret_cast<const uint8_t *>(bmp.data());
infoHeader.biSize = fromLEU32(rawBMPInfoHeader + 0);
infoHeader.biWidth = fromLES32(rawBMPInfoHeader + 4);
infoHeader.biHeight = fromLES32(rawBMPInfoHeader + 8);
infoHeader.biPlanes = fromLEU16(rawBMPInfoHeader + 12);
infoHeader.biBitCount = fromLEU16(rawBMPInfoHeader + 14);
infoHeader.biCompression = fromLEU32(rawBMPInfoHeader + 16);
infoHeader.biSizeImage = fromLEU32(rawBMPInfoHeader + 20);
infoHeader.biXPelsPerMeter = fromLES32(rawBMPInfoHeader + 24);
infoHeader.biYPelsPerMeter = fromLES32(rawBMPInfoHeader + 28);
infoHeader.biClrUsed = fromLEU32(rawBMPInfoHeader + 32);
infoHeader.biClrImportant = fromLEU32(rawBMPInfoHeader + 36);
// check that format is acceptable
if (infoHeader.biSize != 40 || infoHeader.biWidth == 0 || infoHeader.biHeight == 0 || infoHeader.biPlanes != 0 ||
infoHeader.biCompression != 0 || (infoHeader.biBitCount != 24 && infoHeader.biBitCount != 32)) {
return std::string();
}
// convert to image format
2025-01-11 00:09:43 +00:00
const uint8_t *rawBMPPixels = rawBMPInfoHeader + 40;
if (infoHeader.biBitCount == 24) {
return doBGRFromIClipboard(rawBMPPixels, infoHeader.biWidth, infoHeader.biHeight);
} else {
return doBGRAFromIClipboard(rawBMPPixels, infoHeader.biWidth, infoHeader.biHeight);
}
2012-06-10 16:50:54 +00:00
}
std::string XWindowsClipboardAnyBitmapConverter::toIClipboard(const std::string &image) const
{
// convert to raw BMP data
UInt32 w, h, depth;
std::string rawBMP = doToIClipboard(image, w, h, depth);
if (rawBMP.empty() || w == 0 || h == 0 || (depth != 24 && depth != 32)) {
return std::string();
}
// fill BMP info header with little-endian data
2025-01-11 00:09:43 +00:00
uint8_t infoHeader[40];
uint8_t *dst = infoHeader;
toLE(dst, static_cast<UInt32>(40));
toLE(dst, static_cast<SInt32>(w));
toLE(dst, static_cast<SInt32>(h));
2025-01-11 00:58:26 +00:00
toLE(dst, static_cast<uint16_t>(1));
toLE(dst, static_cast<uint16_t>(depth));
toLE(dst, static_cast<UInt32>(0)); // BI_RGB
toLE(dst, static_cast<UInt32>(image.size()));
toLE(dst, static_cast<SInt32>(2834)); // 72 dpi
toLE(dst, static_cast<SInt32>(2834)); // 72 dpi
toLE(dst, static_cast<UInt32>(0));
toLE(dst, static_cast<UInt32>(0));
// construct image
return std::string(reinterpret_cast<const char *>(infoHeader), sizeof(infoHeader)) + rawBMP;
2012-06-10 16:50:54 +00:00
}