From fdf4b2604deac2685d76ab8d7131bbe521f121a6 Mon Sep 17 00:00:00 2001 From: crs Date: Mon, 3 May 2004 21:14:01 +0000 Subject: [PATCH] Fixed X11 BMP and other bitmap conversion. Had data alignment problems. --- .../CXWindowsClipboardAnyBitmapConverter.cpp | 84 ++++++++----------- .../CXWindowsClipboardBMPConverter.cpp | 55 +++++++----- 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/lib/platform/CXWindowsClipboardAnyBitmapConverter.cpp b/lib/platform/CXWindowsClipboardAnyBitmapConverter.cpp index e0d6e40a..ab6afae2 100644 --- a/lib/platform/CXWindowsClipboardAnyBitmapConverter.cpp +++ b/lib/platform/CXWindowsClipboardAnyBitmapConverter.cpp @@ -31,47 +31,36 @@ public: }; // BMP is little-endian -static inline -UInt16 -toLE(UInt16 data) + +static +void +toLE(UInt8*& dst, UInt16 src) { - union x16 { - UInt8 n8[2]; - UInt16 n16; - } c; - c.n8[0] = static_cast(data & 0xffu); - c.n8[1] = static_cast((data >> 8) & 0xffu); - return c.n16; + dst[0] = static_cast(src & 0xffu); + dst[1] = static_cast((src >> 8) & 0xffu); + dst += 2; } -static inline -SInt32 -toLE(SInt32 data) +static +void +toLE(UInt8*& dst, SInt32 src) { - union x32 { - UInt8 n8[4]; - SInt32 n32; - } c; - c.n8[0] = static_cast(data & 0xffu); - c.n8[1] = static_cast((data >> 8) & 0xffu); - c.n8[2] = static_cast((data >> 16) & 0xffu); - c.n8[3] = static_cast((data >> 24) & 0xffu); - return c.n32; + dst[0] = static_cast(src & 0xffu); + dst[1] = static_cast((src >> 8) & 0xffu); + dst[2] = static_cast((src >> 16) & 0xffu); + dst[3] = static_cast((src >> 24) & 0xffu); + dst += 4; } -static inline -UInt32 -toLE(UInt32 data) +static +void +toLE(UInt8*& dst, UInt32 src) { - union x32 { - UInt8 n8[4]; - UInt32 n32; - } c; - c.n8[0] = static_cast(data & 0xffu); - c.n8[1] = static_cast((data >> 8) & 0xffu); - c.n8[2] = static_cast((data >> 16) & 0xffu); - c.n8[3] = static_cast((data >> 24) & 0xffu); - return c.n32; + dst[0] = static_cast(src & 0xffu); + dst[1] = static_cast((src >> 8) & 0xffu); + dst[2] = static_cast((src >> 16) & 0xffu); + dst[3] = static_cast((src >> 24) & 0xffu); + dst += 4; } static inline @@ -178,20 +167,21 @@ CXWindowsClipboardAnyBitmapConverter::toIClipboard(const CString& image) const } // fill BMP info header with little-endian data - CBMPInfoHeader infoHeader; - infoHeader.biSize = toLE(40); - infoHeader.biWidth = toLE(w); - infoHeader.biHeight = toLE(h); - infoHeader.biPlanes = toLE(1); - infoHeader.biBitCount = toLE(depth); - infoHeader.biCompression = toLE(0); // BI_RGB - infoHeader.biSizeImage = image.size(); - infoHeader.biXPelsPerMeter = toLE(2834); // 72 dpi - infoHeader.biYPelsPerMeter = toLE(2834); // 72 dpi - infoHeader.biClrUsed = toLE(0); - infoHeader.biClrImportant = toLE(0); + UInt8 infoHeader[40]; + UInt8* dst = infoHeader; + toLE(dst, static_cast(40)); + toLE(dst, static_cast(w)); + toLE(dst, static_cast(h)); + toLE(dst, static_cast(1)); + toLE(dst, static_cast(depth)); + toLE(dst, static_cast(0)); // BI_RGB + toLE(dst, static_cast(image.size())); + toLE(dst, static_cast(2834)); // 72 dpi + toLE(dst, static_cast(2834)); // 72 dpi + toLE(dst, static_cast(0)); + toLE(dst, static_cast(0)); // construct image - return CString(reinterpret_cast(&infoHeader), + return CString(reinterpret_cast(infoHeader), sizeof(infoHeader)) + rawBMP; } diff --git a/lib/platform/CXWindowsClipboardBMPConverter.cpp b/lib/platform/CXWindowsClipboardBMPConverter.cpp index af5cc59a..bd3438a2 100644 --- a/lib/platform/CXWindowsClipboardBMPConverter.cpp +++ b/lib/platform/CXWindowsClipboardBMPConverter.cpp @@ -35,19 +35,32 @@ fromLEU32(const UInt8* data) (static_cast(data[3]) << 24); } -static inline -UInt32 -toLE(UInt32 data) +static +void +toLE(UInt8*& dst, char src) { - union x32 { - UInt8 n8[4]; - UInt32 n32; - } c; - c.n8[0] = static_cast(data & 0xffu); - c.n8[1] = static_cast((data >> 8) & 0xffu); - c.n8[2] = static_cast((data >> 16) & 0xffu); - c.n8[3] = static_cast((data >> 24) & 0xffu); - return c.n32; + dst[0] = static_cast(src); + dst += 1; +} + +static +void +toLE(UInt8*& dst, UInt16 src) +{ + dst[0] = static_cast(src & 0xffu); + dst[1] = static_cast((src >> 8) & 0xffu); + dst += 2; +} + +static +void +toLE(UInt8*& dst, UInt32 src) +{ + dst[0] = static_cast(src & 0xffu); + dst[1] = static_cast((src >> 8) & 0xffu); + dst[2] = static_cast((src >> 16) & 0xffu); + dst[3] = static_cast((src >> 24) & 0xffu); + dst += 4; } // @@ -88,15 +101,15 @@ CString CXWindowsClipboardBMPConverter::fromIClipboard(const CString& bmp) const { // create BMP image - CBMPHeader header; - char* type = reinterpret_cast(&header.type); - type[0] = 'B'; - type[1] = 'M'; - header.size = toLE(14 + bmp.size()); - header.reserved1 = 0; - header.reserved2 = 0; - header.offset = toLE(14 + 40); - return CString(reinterpret_cast(&header), 14) + bmp; + UInt8 header[14]; + UInt8* dst = header; + toLE(dst, 'B'); + toLE(dst, 'M'); + toLE(dst, 14 + bmp.size()); + toLE(dst, static_cast(0)); + toLE(dst, static_cast(0)); + toLE(dst, static_cast(14 + 40)); + return CString(reinterpret_cast(header), 14) + bmp; } CString