source: trunk/synergy/lib/platform/CXWindowsClipboardBMPConverter.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 3.0 KB
Line 
1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2004 Chris Schoeneman
4 *
5 * This package is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * found in the file COPYING that should have accompanied this file.
8 *
9 * This package is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include "CXWindowsClipboardBMPConverter.h"
16
17// BMP file header structure
18struct CBMPHeader {
19public:
20 UInt16 type;
21 UInt32 size;
22 UInt16 reserved1;
23 UInt16 reserved2;
24 UInt32 offset;
25};
26
27// BMP is little-endian
28static inline
29UInt32
30fromLEU32(const UInt8* data)
31{
32 return static_cast<UInt32>(data[0]) |
33 (static_cast<UInt32>(data[1]) << 8) |
34 (static_cast<UInt32>(data[2]) << 16) |
35 (static_cast<UInt32>(data[3]) << 24);
36}
37
38static
39void
40toLE(UInt8*& dst, char src)
41{
42 dst[0] = static_cast<UInt8>(src);
43 dst += 1;
44}
45
46static
47void
48toLE(UInt8*& dst, UInt16 src)
49{
50 dst[0] = static_cast<UInt8>(src & 0xffu);
51 dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
52 dst += 2;
53}
54
55static
56void
57toLE(UInt8*& dst, UInt32 src)
58{
59 dst[0] = static_cast<UInt8>(src & 0xffu);
60 dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
61 dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
62 dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
63 dst += 4;
64}
65
66//
67// CXWindowsClipboardBMPConverter
68//
69
70CXWindowsClipboardBMPConverter::CXWindowsClipboardBMPConverter(
71 Display* display) :
72 m_atom(XInternAtom(display, "image/bmp", False))
73{
74 // do nothing
75}
76
77CXWindowsClipboardBMPConverter::~CXWindowsClipboardBMPConverter()
78{
79 // do nothing
80}
81
82IClipboard::EFormat
83CXWindowsClipboardBMPConverter::getFormat() const
84{
85 return IClipboard::kBitmap;
86}
87
88Atom
89CXWindowsClipboardBMPConverter::getAtom() const
90{
91 return m_atom;
92}
93
94int
95CXWindowsClipboardBMPConverter::getDataSize() const
96{
97 return 8;
98}
99
100CString
101CXWindowsClipboardBMPConverter::fromIClipboard(const CString& bmp) const
102{
103 // create BMP image
104 UInt8 header[14];
105 UInt8* dst = header;
106 toLE(dst, 'B');
107 toLE(dst, 'M');
108 toLE(dst, static_cast<UInt32>(14 + bmp.size()));
109 toLE(dst, static_cast<UInt16>(0));
110 toLE(dst, static_cast<UInt16>(0));
111 toLE(dst, static_cast<UInt32>(14 + 40));
112 return CString(reinterpret_cast<const char*>(header), 14) + bmp;
113}
114
115CString
116CXWindowsClipboardBMPConverter::toIClipboard(const CString& bmp) const
117{
118 // make sure data is big enough for a BMP file
119 if (bmp.size() <= 14 + 40) {
120 return CString();
121 }
122
123 // check BMP file header
124 const UInt8* rawBMPHeader = reinterpret_cast<const UInt8*>(bmp.data());
125 if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
126 return CString();
127 }
128
129 // get offset to image data
130 UInt32 offset = fromLEU32(rawBMPHeader + 10);
131
132 // construct BMP
133 if (offset == 14 + 40) {
134 return bmp.substr(14);
135 }
136 else {
137 return bmp.substr(14, 40) + bmp.substr(offset, bmp.size() - offset);
138 }
139}
Note: See TracBrowser for help on using the repository browser.