aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/XWindowsClipboardAnyBitmapConverter.cpp
blob: 493b1e88c868a0a366650e5a72b8205e89f08dd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2004 Chris Schoeneman
 * 
 * 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.
 * 
 * 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"

// BMP info header structure
struct CBMPInfoHeader {
public:
    UInt32                biSize;
    SInt32                biWidth;
    SInt32                biHeight;
    UInt16                biPlanes;
    UInt16                biBitCount;
    UInt32                biCompression;
    UInt32                biSizeImage;
    SInt32                biXPelsPerMeter;
    SInt32                biYPelsPerMeter;
    UInt32                biClrUsed;
    UInt32                biClrImportant;
};

// BMP is little-endian

static
void
toLE(UInt8*& dst, UInt16 src)
{
    dst[0] = static_cast<UInt8>(src & 0xffu);
    dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
    dst += 2;
}

static
void
toLE(UInt8*& dst, SInt32 src)
{
    dst[0] = static_cast<UInt8>(src & 0xffu);
    dst[1] = static_cast<UInt8>((src >>  8) & 0xffu);
    dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
    dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
    dst += 4;
}

static
void
toLE(UInt8*& dst, UInt32 src)
{
    dst[0] = static_cast<UInt8>(src & 0xffu);
    dst[1] = static_cast<UInt8>((src >>  8) & 0xffu);
    dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
    dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
    dst += 4;
}

static inline
UInt16
fromLEU16(const UInt8* data)
{
    return static_cast<UInt16>(data[0]) |
            (static_cast<UInt16>(data[1]) << 8);
}

static inline
SInt32
fromLES32(const UInt8* 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));
}

static inline
UInt32
fromLEU32(const UInt8* 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);
}


//
// XWindowsClipboardAnyBitmapConverter
//

XWindowsClipboardAnyBitmapConverter::XWindowsClipboardAnyBitmapConverter()
{
    // do nothing
}

XWindowsClipboardAnyBitmapConverter::~XWindowsClipboardAnyBitmapConverter()
{
    // do nothing
}

IClipboard::EFormat
XWindowsClipboardAnyBitmapConverter::getFormat() const
{
    return IClipboard::kBitmap;
}

int
XWindowsClipboardAnyBitmapConverter::getDataSize() const
{
    return 8;
}

String
XWindowsClipboardAnyBitmapConverter::fromIClipboard(const String& bmp) const
{
    // fill BMP info header with native-endian data
    CBMPInfoHeader infoHeader;
    const UInt8* rawBMPInfoHeader = reinterpret_cast<const UInt8*>(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 String();
    }

    // convert to image format
    const UInt8* rawBMPPixels = rawBMPInfoHeader + 40;
    if (infoHeader.biBitCount == 24) {
        return doBGRFromIClipboard(rawBMPPixels,
                            infoHeader.biWidth, infoHeader.biHeight);
    }
    else {
        return doBGRAFromIClipboard(rawBMPPixels,
                            infoHeader.biWidth, infoHeader.biHeight);
    }
}

String
XWindowsClipboardAnyBitmapConverter::toIClipboard(const String& image) const
{
    // convert to raw BMP data
    UInt32 w, h, depth;
    String rawBMP = doToIClipboard(image, w, h, depth);
    if (rawBMP.empty() || w == 0 || h == 0 || (depth != 24 && depth != 32)) {
        return String();
    }

    // fill BMP info header with little-endian data
    UInt8 infoHeader[40];
    UInt8* dst = infoHeader;
    toLE(dst, static_cast<UInt32>(40));
    toLE(dst, static_cast<SInt32>(w));
    toLE(dst, static_cast<SInt32>(h));
    toLE(dst, static_cast<UInt16>(1));
    toLE(dst, static_cast<UInt16>(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 String(reinterpret_cast<const char*>(infoHeader),
                            sizeof(infoHeader)) + rawBMP;
}