00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CXWindowsClipboardBMPConverter.h"
00020
00021
00022 struct CBMPHeader {
00023 public:
00024 UInt16 type;
00025 UInt32 size;
00026 UInt16 reserved1;
00027 UInt16 reserved2;
00028 UInt32 offset;
00029 };
00030
00031
00032 static inline
00033 UInt32
00034 fromLEU32(const UInt8* data)
00035 {
00036 return static_cast<UInt32>(data[0]) |
00037 (static_cast<UInt32>(data[1]) << 8) |
00038 (static_cast<UInt32>(data[2]) << 16) |
00039 (static_cast<UInt32>(data[3]) << 24);
00040 }
00041
00042 static
00043 void
00044 toLE(UInt8*& dst, char src)
00045 {
00046 dst[0] = static_cast<UInt8>(src);
00047 dst += 1;
00048 }
00049
00050 static
00051 void
00052 toLE(UInt8*& dst, UInt16 src)
00053 {
00054 dst[0] = static_cast<UInt8>(src & 0xffu);
00055 dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
00056 dst += 2;
00057 }
00058
00059 static
00060 void
00061 toLE(UInt8*& dst, UInt32 src)
00062 {
00063 dst[0] = static_cast<UInt8>(src & 0xffu);
00064 dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
00065 dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
00066 dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
00067 dst += 4;
00068 }
00069
00070
00071
00072
00073
00074 CXWindowsClipboardBMPConverter::CXWindowsClipboardBMPConverter(
00075 Display* display) :
00076 m_atom(XInternAtom(display, "image/bmp", False))
00077 {
00078
00079 }
00080
00081 CXWindowsClipboardBMPConverter::~CXWindowsClipboardBMPConverter()
00082 {
00083
00084 }
00085
00086 IClipboard::EFormat
00087 CXWindowsClipboardBMPConverter::getFormat() const
00088 {
00089 return IClipboard::kBitmap;
00090 }
00091
00092 Atom
00093 CXWindowsClipboardBMPConverter::getAtom() const
00094 {
00095 return m_atom;
00096 }
00097
00098 int
00099 CXWindowsClipboardBMPConverter::getDataSize() const
00100 {
00101 return 8;
00102 }
00103
00104 CString
00105 CXWindowsClipboardBMPConverter::fromIClipboard(const CString& bmp) const
00106 {
00107
00108 UInt8 header[14];
00109 UInt8* dst = header;
00110 toLE(dst, 'B');
00111 toLE(dst, 'M');
00112 toLE(dst, static_cast<UInt32>(14 + bmp.size()));
00113 toLE(dst, static_cast<UInt16>(0));
00114 toLE(dst, static_cast<UInt16>(0));
00115 toLE(dst, static_cast<UInt32>(14 + 40));
00116 return CString(reinterpret_cast<const char*>(header), 14) + bmp;
00117 }
00118
00119 CString
00120 CXWindowsClipboardBMPConverter::toIClipboard(const CString& bmp) const
00121 {
00122
00123 if (bmp.size() <= 14 + 40) {
00124 return CString();
00125 }
00126
00127
00128 const UInt8* rawBMPHeader = reinterpret_cast<const UInt8*>(bmp.data());
00129 if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
00130 return CString();
00131 }
00132
00133
00134 UInt32 offset = fromLEU32(rawBMPHeader + 10);
00135
00136
00137 if (offset == 14 + 40) {
00138 return bmp.substr(14);
00139 }
00140 else {
00141 return bmp.substr(14, 40) + bmp.substr(offset, bmp.size() - offset);
00142 }
00143 }