• Main Page
  • Classes
  • Files
  • File List

CXWindowsClipboardBMPConverter.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CXWindowsClipboardBMPConverter.h"
00020 
00021 // BMP file header structure
00022 struct CBMPHeader {
00023 public:
00024     UInt16              type;
00025     UInt32              size;
00026     UInt16              reserved1;
00027     UInt16              reserved2;
00028     UInt32              offset;
00029 };
00030 
00031 // BMP is little-endian
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 // CXWindowsClipboardBMPConverter
00072 //
00073 
00074 CXWindowsClipboardBMPConverter::CXWindowsClipboardBMPConverter(
00075                 Display* display) :
00076     m_atom(XInternAtom(display, "image/bmp", False))
00077 {
00078     // do nothing
00079 }
00080 
00081 CXWindowsClipboardBMPConverter::~CXWindowsClipboardBMPConverter()
00082 {
00083     // do nothing
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     // create BMP image
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     // make sure data is big enough for a BMP file
00123     if (bmp.size() <= 14 + 40) {
00124         return CString();
00125     }
00126 
00127     // check BMP file header
00128     const UInt8* rawBMPHeader = reinterpret_cast<const UInt8*>(bmp.data());
00129     if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
00130         return CString();
00131     }
00132 
00133     // get offset to image data
00134     UInt32 offset = fromLEU32(rawBMPHeader + 10);
00135 
00136     // construct BMP
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 }

Generated on Sun May 19 2013 00:00:05 for Synergy by  doxygen 1.7.1