Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

CXWindowsClipboardBMPConverter.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2004 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "CXWindowsClipboardBMPConverter.h"
00016 
00017 // BMP file header structure
00018 struct CBMPHeader {
00019 public:
00020     UInt16              type;
00021     UInt32              size;
00022     UInt16              reserved1;
00023     UInt16              reserved2;
00024     UInt32              offset;
00025 };
00026 
00027 // BMP is little-endian
00028 static inline
00029 UInt32
00030 fromLEU32(const UInt8* data)
00031 {
00032     return static_cast<UInt32>(data[0]) |
00033             (static_cast<UInt32>(data[1]) <<  8) |
00034             (static_cast<UInt32>(data[2]) << 16) |
00035             (static_cast<UInt32>(data[3]) << 24);
00036 }
00037 
00038 static
00039 void
00040 toLE(UInt8*& dst, char src)
00041 {
00042     dst[0] = static_cast<UInt8>(src);
00043     dst += 1;
00044 }
00045 
00046 static
00047 void
00048 toLE(UInt8*& dst, UInt16 src)
00049 {
00050     dst[0] = static_cast<UInt8>(src & 0xffu);
00051     dst[1] = static_cast<UInt8>((src >> 8) & 0xffu);
00052     dst += 2;
00053 }
00054 
00055 static
00056 void
00057 toLE(UInt8*& dst, UInt32 src)
00058 {
00059     dst[0] = static_cast<UInt8>(src & 0xffu);
00060     dst[1] = static_cast<UInt8>((src >>  8) & 0xffu);
00061     dst[2] = static_cast<UInt8>((src >> 16) & 0xffu);
00062     dst[3] = static_cast<UInt8>((src >> 24) & 0xffu);
00063     dst += 4;
00064 }
00065 
00066 //
00067 // CXWindowsClipboardBMPConverter
00068 //
00069 
00070 CXWindowsClipboardBMPConverter::CXWindowsClipboardBMPConverter(
00071                 Display* display) :
00072     m_atom(XInternAtom(display, "image/bmp", False))
00073 {
00074     // do nothing
00075 }
00076 
00077 CXWindowsClipboardBMPConverter::~CXWindowsClipboardBMPConverter()
00078 {
00079     // do nothing
00080 }
00081 
00082 IClipboard::EFormat
00083 CXWindowsClipboardBMPConverter::getFormat() const
00084 {
00085     return IClipboard::kBitmap;
00086 }
00087 
00088 Atom
00089 CXWindowsClipboardBMPConverter::getAtom() const
00090 {
00091     return m_atom;
00092 }
00093 
00094 int
00095 CXWindowsClipboardBMPConverter::getDataSize() const
00096 {
00097     return 8;
00098 }
00099 
00100 CString
00101 CXWindowsClipboardBMPConverter::fromIClipboard(const CString& bmp) const
00102 {
00103     // create BMP image
00104     UInt8 header[14];
00105     UInt8* dst = header;
00106     toLE(dst, 'B');
00107     toLE(dst, 'M');
00108     toLE(dst, static_cast<UInt32>(14 + bmp.size()));
00109     toLE(dst, static_cast<UInt16>(0));
00110     toLE(dst, static_cast<UInt16>(0));
00111     toLE(dst, static_cast<UInt32>(14 + 40));
00112     return CString(reinterpret_cast<const char*>(header), 14) + bmp;
00113 }
00114 
00115 CString
00116 CXWindowsClipboardBMPConverter::toIClipboard(const CString& bmp) const
00117 {
00118     // make sure data is big enough for a BMP file
00119     if (bmp.size() <= 14 + 40) {
00120         return CString();
00121     }
00122 
00123     // check BMP file header
00124     const UInt8* rawBMPHeader = reinterpret_cast<const UInt8*>(bmp.data());
00125     if (rawBMPHeader[0] != 'B' || rawBMPHeader[1] != 'M') {
00126         return CString();
00127     }
00128 
00129     // get offset to image data
00130     UInt32 offset = fromLEU32(rawBMPHeader + 10);
00131 
00132     // construct BMP
00133     if (offset == 14 + 40) {
00134         return bmp.substr(14);
00135     }
00136     else {
00137         return bmp.substr(14, 40) + bmp.substr(offset, bmp.size() - offset);
00138     }
00139 }

Generated on Fri Nov 6 00:21:14 2009 for synergy-plus by  doxygen 1.3.9.1