File: CMSWindowsClipboardBitmapConverter.cpp

package info (click to toggle)
synergy 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,664 kB
  • ctags: 5,482
  • sloc: cpp: 46,292; sh: 3,392; makefile: 938; ansic: 82
file content (146 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download | duplicates (3)
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
/*
 * synergy -- mouse and keyboard sharing utility
 * 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 COPYING 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.
 */

#include "CMSWindowsClipboardBitmapConverter.h"
#include "CLog.h"

//
// CMSWindowsClipboardBitmapConverter
//

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

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

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

UINT
CMSWindowsClipboardBitmapConverter::getWin32Format() const
{
	return CF_DIB;
}

HANDLE
CMSWindowsClipboardBitmapConverter::fromIClipboard(const CString& data) const
{
	// copy to memory handle
	HGLOBAL gData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, data.size());
	if (gData != NULL) {
		// get a pointer to the allocated memory
		char* dst = (char*)GlobalLock(gData);
		if (dst != NULL) {
			memcpy(dst, data.data(), data.size());
			GlobalUnlock(gData);
		}
		else {
			GlobalFree(gData);
			gData = NULL;
		}
	}

	return gData;
}

CString
CMSWindowsClipboardBitmapConverter::toIClipboard(HANDLE data) const
{
	// get datator
	const char* src = (const char*)GlobalLock(data);
	if (src == NULL) {
		return CString();
	}
	UInt32 srcSize = (UInt32)GlobalSize(data);

	// check image type
	const BITMAPINFO* bitmap = reinterpret_cast<const BITMAPINFO*>(src);
	LOG((CLOG_INFO "bitmap: %dx%d %d", bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight, (int)bitmap->bmiHeader.biBitCount));
	if (bitmap->bmiHeader.biPlanes == 1 &&
		(bitmap->bmiHeader.biBitCount == 24 ||
		bitmap->bmiHeader.biBitCount == 32) &&
		bitmap->bmiHeader.biCompression == BI_RGB) {
		// already in canonical form
		CString image(src, srcSize);
		GlobalUnlock(data);
		return image;
	}

	// create a destination DIB section
	LOG((CLOG_INFO "convert image from: depth=%d comp=%d", bitmap->bmiHeader.biBitCount, bitmap->bmiHeader.biCompression));
	void* raw;
	BITMAPINFOHEADER info;
	LONG w               = bitmap->bmiHeader.biWidth;
	LONG h               = bitmap->bmiHeader.biHeight;
	info.biSize          = sizeof(BITMAPINFOHEADER);
	info.biWidth         = w;
	info.biHeight        = h;
	info.biPlanes        = 1;
	info.biBitCount      = 32;
	info.biCompression   = BI_RGB;
	info.biSizeImage     = 0;
	info.biXPelsPerMeter = 1000;
	info.biYPelsPerMeter = 1000;
	info.biClrUsed       = 0;
	info.biClrImportant  = 0;
	HDC dc      = GetDC(NULL);
	HBITMAP dst = CreateDIBSection(dc, (BITMAPINFO*)&info,
							DIB_RGB_COLORS, &raw, NULL, 0);

	// find the start of the pixel data
	const char* srcBits = (const char*)bitmap + bitmap->bmiHeader.biSize;
	if (bitmap->bmiHeader.biBitCount >= 16) {
		if (bitmap->bmiHeader.biCompression == BI_BITFIELDS &&
			(bitmap->bmiHeader.biBitCount == 16 ||
			bitmap->bmiHeader.biBitCount == 32)) {
			srcBits += 3 * sizeof(DWORD);
		}
	}
	else if (bitmap->bmiHeader.biClrUsed != 0) {
		srcBits += bitmap->bmiHeader.biClrUsed * sizeof(RGBQUAD);
	}
	else {
		srcBits += (1 << bitmap->bmiHeader.biBitCount) * sizeof(RGBQUAD);
	}

	// copy source image to destination image
	HDC dstDC         = CreateCompatibleDC(dc);
	HGDIOBJ oldBitmap = SelectObject(dstDC, dst);
	SetDIBitsToDevice(dstDC, 0, 0, w, h, 0, 0, 0, h,
							srcBits, bitmap, DIB_RGB_COLORS);
	SelectObject(dstDC, oldBitmap);
	DeleteDC(dstDC);
	GdiFlush();

	// extract data
	CString image((const char*)&info, info.biSize);
	image.append((const char*)raw, 4 * w * h);

	// clean up GDI
	DeleteObject(dst);
	ReleaseDC(NULL, dc);

	// release handle
	GlobalUnlock(data);

	return image;
}