-
Notifications
You must be signed in to change notification settings - Fork 3
/
vtkKWClipboardHelper.cxx
289 lines (236 loc) · 7.19 KB
/
vtkKWClipboardHelper.cxx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*=========================================================================
Module: vtkKWClipboardHelper.cxx,v
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
#include <Carbon/Carbon.h>
#define Cursor X11Cursor
#endif
#endif // __APPLE__
#include "vtkWindows.h"
#include "vtkKWClipboardHelper.h"
#include "vtkDebugLeaks.h"
#include "vtkImageCast.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
#undef Cursor
#endif
#endif // __APPLE__
vtkStandardNewMacro( vtkKWClipboardHelper );
vtkCxxRevisionMacro(vtkKWClipboardHelper, "1.0");
//----------------------------------------------------------------------------
vtkKWClipboardHelper::vtkKWClipboardHelper()
{
}
//----------------------------------------------------------------------------
vtkKWClipboardHelper::~vtkKWClipboardHelper()
{
}
//----------------------------------------------------------------------------
int vtkKWClipboardHelper::CopyTextToClipboard(const char* text)
{
if(!text || !(*text))
{
return 0;
}
#ifdef _WIN32
if (::OpenClipboard(NULL))
{
EmptyClipboard();
HANDLE hDIB =
::GlobalAlloc(GMEM_DDESHARE, strlen(text) + 1);
char *ptr = (char*)::GlobalLock(hDIB);
strcpy(ptr, text);
SetClipboardData(CF_TEXT, hDIB);
::GlobalUnlock(hDIB);
CloseClipboard();
return 1;
}
#endif
// For MacOsX - 10.4+
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
PasteboardRef pb = NULL;
if(noErr == PasteboardCreate(kPasteboardClipboard,&pb))
{
CFDataRef filedata = CFDataCreate(kCFAllocatorDefault,
(UInt8*)text,strlen(text));
PasteboardClear(pb);
PasteboardSyncFlags syncFlags = PasteboardSynchronize(pb);
PasteboardPutItemFlavor(pb,(PasteboardItemID)1,
CFSTR("public.utf8-plain-text"),filedata,0);
CFRelease(filedata);
CFRelease(pb);
return 1;
}
#endif
#endif
return 0;
}
//----------------------------------------------------------------------------
int vtkKWClipboardHelper::CopyImageToClipboard(vtkImageData* iData)
{
if(!iData)
{
return 0;
}
int *extent = iData->GetExtent();
if (extent[0] > extent[1] && extent[2] > extent[3] && extent[4] > extent[5])
{
return 0;
}
int scalarType = iData->GetScalarType();
unsigned char *ptr = NULL;
if(scalarType == VTK_UNSIGNED_CHAR)
{
ptr = (unsigned char *)(iData->GetScalarPointer());
}
else
{
vtkImageCast* cast = vtkImageCast::New();
cast->SetInput(iData);
cast->SetOutputScalarTypeToUnsignedChar();
cast->Update();
ptr = (unsigned char *)(cast->GetOutput()->GetScalarPointer());
cast->Delete();
}
if(ptr == NULL)
{
return 0;
}
extent = iData->GetWholeExtent();
int size[2];
size[0] = extent[1] - extent[0] + 1;
size[1] = extent[3] - extent[2] + 1;
int numComponents = iData->GetNumberOfScalarComponents();
// Save to clipboard
#ifdef _WIN32
//if (::OpenClipboard(
// (HWND)first_rwwidget->GetRenderWindow()->GetGenericWindowId()))
if (::OpenClipboard(NULL))
{
int data_width = ((size[0] * 3 + 3) / 4) * 4;
EmptyClipboard();
DWORD dwLen = sizeof(BITMAPINFOHEADER) + data_width * size[1];
HANDLE hDIB = ::GlobalAlloc(GHND, dwLen);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) ::GlobalLock(hDIB);
lpbi->biSize = sizeof(BITMAPINFOHEADER);
lpbi->biWidth = size[0];
lpbi->biHeight = size[1];
lpbi->biPlanes = 1;
lpbi->biBitCount = 24;
lpbi->biCompression = BI_RGB;
lpbi->biClrUsed = 0;
lpbi->biClrImportant = 0;
lpbi->biSizeImage = data_width * size[1];
// Copy the data to the clipboard
unsigned char *dest = (unsigned char *)lpbi + lpbi->biSize;
int diffWidth = data_width - size[0]*3;
int i,j;
for (i = 0; i < size[1]; i++)
{
for (j = 0; j < size[0]; j++)
{
*dest++ = ptr[2];
*dest++ = ptr[1];
*dest++ = *ptr;
ptr += numComponents;
}
dest = dest + diffWidth;
}
SetClipboardData (CF_DIB, hDIB);
::GlobalUnlock(hDIB);
CloseClipboard();
return 1;
}
#endif
// For MacOsX - 10.4+
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
void* bitmapData;
int bytePRow = size[0]*4;
int byteCount = bytePRow*size[1];
bitmapData = malloc(byteCount);
unsigned char* newDest = (unsigned char*)bitmapData;
int i, j;
unsigned char* ptrOrig = ptr;
int origBPR = numComponents*size[0];
for(i=size[1]-1;i>=0;i--)
{
ptr = ptrOrig + i*origBPR;
for(int j=0;j<size[0];j++)
{
*newDest++ = *ptr;
*newDest++ = ptr[1];
*newDest++ = ptr[2];
*newDest++ = (numComponents==4) ? ptr[3] : 1;
ptr += numComponents;
}
}
CGColorSpaceRef colorSpace= CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef pBitmapRef = CGBitmapContextCreate (
bitmapData, size[0], size[1], 8, bytePRow,
colorSpace, kCGImageAlphaNoneSkipLast);
CFRelease(colorSpace);
CGImageRef image = CGBitmapContextCreateImage(pBitmapRef);
//Copy our image into data
CFMutableDataRef data1 = CFDataCreateMutable(kCFAllocatorDefault,0);
CGImageDestinationRef dest1 = CGImageDestinationCreateWithData(
(CFMutableDataRef)data1,CFSTR("com.apple.pict"),1,NULL);
CGImageDestinationAddImage(dest1,image,NULL);
CGImageDestinationFinalize(dest1);
CGImageRelease(image);
CFRelease(dest1);
CFRange range;
range.location = 0;
range.length = 512;//PICT header length
// The clipboard REQIRES only the image data, not the header.
CFDataDeleteBytes(data1, range);
CGImageRef image2 = CGBitmapContextCreateImage(pBitmapRef);
CFMutableDataRef data2 = CFDataCreateMutable(kCFAllocatorDefault,0);
CGImageDestinationRef dest2 =
CGImageDestinationCreateWithData(data2,CFSTR("public.png"),1,NULL);
CGImageDestinationAddImage(dest2,image2,NULL);
CGImageDestinationFinalize(dest2);
CFRelease(dest2);
CGImageRelease(image2);
CFRelease(pBitmapRef);
OSStatus err = noErr;
PasteboardRef pb = NULL;
err = PasteboardCreate(kPasteboardClipboard,&pb);
if(err == noErr)
{
PasteboardClear(pb);
PasteboardSyncFlags syncFlags = PasteboardSynchronize(pb);
PasteboardPutItemFlavor(pb,(PasteboardItemID)1,CFSTR("com.apple.pict"),data1,0);
PasteboardPutItemFlavor(pb,(PasteboardItemID)2, CFSTR("public.png"),data2, 0);
}
CFRelease(data1);
CFRelease(data2);
if(pb)
{
CFRelease(pb);
}
free(bitmapData);
if(err == noErr)
{
return 1;
}
#endif
#endif
return 0;
}
//----------------------------------------------------------------------------
void vtkKWClipboardHelper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}