fix(macos): update BMP handling to support extended headers and improve pixel offset calculation
This commit is contained in:
parent
53903016b1
commit
62a8ee749f
2 changed files with 76 additions and 69 deletions
|
|
@ -1,51 +1,42 @@
|
||||||
/*
|
/*
|
||||||
* Deskflow -- mouse and keyboard sharing utility
|
* Deskflow -- mouse and keyboard sharing utility
|
||||||
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd
|
* SPDX-FileCopyrightText: (C) 2014 - 2016, 2023 - 2026 Symless Ltd
|
||||||
* SPDX-FileCopyrightText: (C) 2014 Ryan Chapman
|
* SPDX-FileCopyrightText: (C) 2014 Ryan Chapman
|
||||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "platform/OSXClipboardBMPConverter.h"
|
#include "platform/OSXClipboardBMPConverter.h"
|
||||||
|
|
||||||
#include "base/Log.h"
|
#include "base/Log.h"
|
||||||
|
|
||||||
// BMP file header structure
|
#include <QtEndian>
|
||||||
struct CBMPHeader
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
uint16_t type;
|
|
||||||
uint32_t size;
|
|
||||||
uint16_t reserved1;
|
|
||||||
uint16_t reserved2;
|
|
||||||
uint32_t offset;
|
|
||||||
};
|
|
||||||
|
|
||||||
// BMP is little-endian
|
quint32 OSXClipboardBMPConverter::dibPixelOffset(const quint8 *dib, qsizetype dibSize)
|
||||||
static inline uint32_t fromLEU32(const uint8_t *data)
|
|
||||||
{
|
{
|
||||||
return static_cast<uint32_t>(data[0]) | (static_cast<uint32_t>(data[1]) << 8) |
|
quint32 pixelOffset = 0;
|
||||||
(static_cast<uint32_t>(data[2]) << 16) | (static_cast<uint32_t>(data[3]) << 24);
|
if (dibSize >= 16) {
|
||||||
}
|
const auto biSize = qFromLittleEndian<quint32>(dib);
|
||||||
|
if (biSize >= 12 && biSize <= dibSize) {
|
||||||
static void toLE(uint8_t *&dst, char src)
|
pixelOffset = biSize;
|
||||||
{
|
if (biSize >= kFallbackPixelOffset) {
|
||||||
dst[0] = static_cast<uint8_t>(src);
|
const auto biBitCount = qFromLittleEndian<quint16>(dib + 14);
|
||||||
dst += 1;
|
const auto biCompression = qFromLittleEndian<quint32>(dib + 16);
|
||||||
}
|
if (biSize == kFallbackPixelOffset) {
|
||||||
|
if (biCompression == kBiBitfields) {
|
||||||
static void toLE(uint8_t *&dst, uint16_t src)
|
pixelOffset += 12;
|
||||||
{
|
} else if (biCompression == kBiAlphabitfields) {
|
||||||
dst[0] = static_cast<uint8_t>(src & 0xffu);
|
pixelOffset += 16;
|
||||||
dst[1] = static_cast<uint8_t>((src >> 8) & 0xffu);
|
}
|
||||||
dst += 2;
|
}
|
||||||
}
|
if (biBitCount > 0 && biBitCount <= 8) {
|
||||||
|
const auto biClrUsed = qFromLittleEndian<quint32>(dib + 32);
|
||||||
static void toLE(uint8_t *&dst, uint32_t src)
|
const auto numColors = biClrUsed != 0 ? biClrUsed : (1u << biBitCount);
|
||||||
{
|
pixelOffset += numColors * 4;
|
||||||
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;
|
return pixelOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
IClipboard::Format OSXClipboardBMPConverter::getFormat() const
|
IClipboard::Format OSXClipboardBMPConverter::getFormat() const
|
||||||
|
|
@ -55,45 +46,54 @@ IClipboard::Format OSXClipboardBMPConverter::getFormat() const
|
||||||
|
|
||||||
CFStringRef OSXClipboardBMPConverter::getOSXFormat() const
|
CFStringRef OSXClipboardBMPConverter::getOSXFormat() const
|
||||||
{
|
{
|
||||||
// TODO: does this only work with Windows?
|
|
||||||
return CFSTR("com.microsoft.bmp");
|
return CFSTR("com.microsoft.bmp");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string OSXClipboardBMPConverter::fromIClipboard(const std::string &bmp) const
|
std::string OSXClipboardBMPConverter::fromIClipboard(const std::string &bmp) const
|
||||||
{
|
{
|
||||||
LOG_VERBOSE("getting data from clipboard");
|
if (bmp.size() < 4) {
|
||||||
// create BMP image
|
LOG_DEBUG("rejecting clipboard dib, too small to wrap as bmp, size: %zu bytes", bmp.size());
|
||||||
uint8_t header[14];
|
return std::string();
|
||||||
uint8_t *dst = header;
|
}
|
||||||
toLE(dst, 'B');
|
|
||||||
toLE(dst, 'M');
|
auto pixelOffset = dibPixelOffset(reinterpret_cast<const quint8 *>(bmp.data()), bmp.size());
|
||||||
toLE(dst, static_cast<uint32_t>(14 + bmp.size()));
|
if (pixelOffset == 0) {
|
||||||
toLE(dst, static_cast<uint16_t>(0));
|
pixelOffset = kFallbackPixelOffset;
|
||||||
toLE(dst, static_cast<uint16_t>(0));
|
}
|
||||||
toLE(dst, static_cast<uint32_t>(14 + 40));
|
|
||||||
return std::string(reinterpret_cast<const char *>(header), 14) + bmp;
|
quint8 header[kBmpFileHeaderSize];
|
||||||
|
header[0] = 'B';
|
||||||
|
header[1] = 'M';
|
||||||
|
qToLittleEndian<quint32>(static_cast<quint32>(kBmpFileHeaderSize + bmp.size()), header + 2);
|
||||||
|
qToLittleEndian<quint16>(0, header + 6);
|
||||||
|
qToLittleEndian<quint16>(0, header + 8);
|
||||||
|
qToLittleEndian<quint32>(kBmpFileHeaderSize + pixelOffset, header + kBmpHeaderDIBPad);
|
||||||
|
return std::string(reinterpret_cast<const char *>(header), kBmpFileHeaderSize) + bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string OSXClipboardBMPConverter::toIClipboard(const std::string &bmp) const
|
std::string OSXClipboardBMPConverter::toIClipboard(const std::string &bmp) const
|
||||||
{
|
{
|
||||||
// make sure data is big enough for a BMP file
|
if (bmp.size() <= kBmpFileHeaderSize) {
|
||||||
if (bmp.size() <= 14 + 40) {
|
LOG_DEBUG("rejecting clipboard bmp, too small to parse, size: %zu bytes", bmp.size());
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
const auto *raw = reinterpret_cast<const quint8 *>(bmp.data());
|
||||||
|
if (raw[0] != 'B' || raw[1] != 'M') {
|
||||||
|
LOG_DEBUG("rejecting clipboard bmp, missing bm magic");
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
// check BMP file header
|
// macOS auto-promotes pasteboard images as 32-bit BITMAPV5HEADER + BI_BITFIELDS,
|
||||||
const uint8_t *rawBMPHeader = reinterpret_cast<const uint8_t *>(bmp.data());
|
// so the full DIB (extended header, masks, palette) must reach the receiver. The
|
||||||
if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
|
// earlier code truncated to 40 bytes and dropped the BGRA bitfield masks.
|
||||||
return std::string();
|
const auto offset = qFromLittleEndian<quint32>(raw + kBmpHeaderDIBPad);
|
||||||
}
|
const auto naturalOffset = dibPixelOffset(raw + kBmpFileHeaderSize, bmp.size() - kBmpFileHeaderSize);
|
||||||
|
std::string result;
|
||||||
// get offset to image data
|
const bool hasGap = naturalOffset != 0 && offset > kBmpFileHeaderSize + naturalOffset && offset <= bmp.size();
|
||||||
uint32_t offset = fromLEU32(rawBMPHeader + 10);
|
if (hasGap) {
|
||||||
|
result = bmp.substr(kBmpFileHeaderSize, naturalOffset) + bmp.substr(offset);
|
||||||
// construct BMP
|
|
||||||
if (offset == 14 + 40) {
|
|
||||||
return bmp.substr(14);
|
|
||||||
} else {
|
} else {
|
||||||
return bmp.substr(14, 40) + bmp.substr(offset, bmp.size() - offset);
|
result = bmp.substr(kBmpFileHeaderSize);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Deskflow -- mouse and keyboard sharing utility
|
* Deskflow -- mouse and keyboard sharing utility
|
||||||
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd
|
* SPDX-FileCopyrightText: (C) 2014 - 2016, 2026 Symless Ltd
|
||||||
* SPDX-FileCopyrightText: (C) 2014 Ryan Chapman
|
* SPDX-FileCopyrightText: (C) 2014 Ryan Chapman
|
||||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||||
*/
|
*/
|
||||||
|
|
@ -9,21 +9,28 @@
|
||||||
|
|
||||||
#include "platform/OSXClipboard.h"
|
#include "platform/OSXClipboard.h"
|
||||||
|
|
||||||
//! Convert to/from some text encoding
|
#include <QtTypes>
|
||||||
|
|
||||||
class OSXClipboardBMPConverter : public IOSXClipboardConverter
|
class OSXClipboardBMPConverter : public IOSXClipboardConverter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OSXClipboardBMPConverter() = default;
|
OSXClipboardBMPConverter() = default;
|
||||||
~OSXClipboardBMPConverter() override = default;
|
~OSXClipboardBMPConverter() override = default;
|
||||||
|
|
||||||
// IMSWindowsClipboardConverter overrides
|
|
||||||
IClipboard::Format getFormat() const override;
|
IClipboard::Format getFormat() const override;
|
||||||
CFStringRef getOSXFormat() const override;
|
CFStringRef getOSXFormat() const override;
|
||||||
|
|
||||||
// OSXClipboardAnyBMPConverter overrides
|
|
||||||
std::string fromIClipboard(const std::string &) const override;
|
std::string fromIClipboard(const std::string &) const override;
|
||||||
std::string toIClipboard(const std::string &) const override;
|
std::string toIClipboard(const std::string &) const override;
|
||||||
|
|
||||||
// generic encoding converter
|
|
||||||
static std::string convertString(const std::string &data, CFStringEncoding fromEncoding, CFStringEncoding toEncoding);
|
static std::string convertString(const std::string &data, CFStringEncoding fromEncoding, CFStringEncoding toEncoding);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr quint32 kBiBitfields = 3;
|
||||||
|
static constexpr quint32 kBiAlphabitfields = 6;
|
||||||
|
static constexpr quint32 kBmpHeaderDIBPad = 10;
|
||||||
|
static constexpr quint32 kBmpFileHeaderSize = 14;
|
||||||
|
static constexpr quint32 kFallbackPixelOffset = 40;
|
||||||
|
|
||||||
|
static quint32 dibPixelOffset(const quint8 *dib, qsizetype dibSize);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue