1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % CCCC L IIIII PPPP BBBB OOO AAA RRRR DDDD %
7 % C L I P P B B O O A A R R D D %
8 % C L I PPP BBBB O O AAAAA RRRR D D %
9 % C L I P B B O O A A R R D D %
10 % CCCC LLLLL IIIII P BBBB OOO A A R R DDDD %
11 % %
12 % %
13 % Read/Write Windows Clipboard. %
14 % %
15 % Software Design %
16 % Leonard Rosenthol %
17 % May 2002 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38
39 /*
40 Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
44 # if defined(__CYGWIN__)
45 # include <windows.h>
46 # else
47 /* All MinGW needs ... */
48 # include "MagickCore/nt-base-private.h"
49 # include <wingdi.h>
50 # endif
51 #endif
52 #include "MagickCore/blob.h"
53 #include "MagickCore/blob-private.h"
54 #include "MagickCore/cache.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/nt-feature.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/module.h"
68
69 #define BMP_HEADER_SIZE 14
70
71 /*
72 Forward declarations.
73 */
74 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
75 static MagickBooleanType
76 WriteCLIPBOARDImage(const ImageInfo *,Image *,ExceptionInfo *);
77 #endif
78
79 /*
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % %
82 % %
83 % %
84 % R e a d C L I P B O A R D I m a g e %
85 % %
86 % %
87 % %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %
90 % ReadCLIPBOARDImage() reads an image from the system clipboard and returns
91 % it. It allocates the memory necessary for the new Image structure and
92 % returns a pointer to the new image.
93 %
94 % The format of the ReadCLIPBOARDImage method is:
95 %
96 % Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
97 % ExceptionInfo exception)
98 %
99 % A description of each parameter follows:
100 %
101 % o image_info: the image info.
102 %
103 % o exception: return any errors or warnings in this structure.
104 %
105 */
106 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
ReadCLIPBOARDImage(const ImageInfo * image_info,ExceptionInfo * exception)107 static Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
108 ExceptionInfo *exception)
109 {
110 unsigned char
111 *p;
112
113 HANDLE
114 clip_handle;
115
116 Image
117 *image;
118
119 ImageInfo
120 *read_info;
121
122 LPVOID
123 clip_mem;
124
125 size_t
126 clip_size,
127 total_size;
128
129 unsigned char
130 offset;
131
132 void
133 *clip_data;
134
135 assert(image_info != (const ImageInfo *) NULL);
136 assert(image_info->signature == MagickCoreSignature);
137 if (image_info->debug != MagickFalse)
138 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
139 image_info->filename);
140 assert(exception != (ExceptionInfo *) NULL);
141 assert(exception->signature == MagickCoreSignature);
142 image=AcquireImage(image_info,exception);
143 if (!IsClipboardFormatAvailable(CF_DIB) &&
144 !IsClipboardFormatAvailable(CF_DIBV5))
145 ThrowReaderException(CoderError,"NoBitmapOnClipboard");
146 if (!OpenClipboard(NULL))
147 ThrowReaderException(CoderError,"UnableToReadImageData");
148 clip_handle=GetClipboardData(CF_DIBV5);
149 if (!clip_handle)
150 clip_handle=GetClipboardData(CF_DIB);
151 if ((clip_handle == NULL) || (clip_handle == INVALID_HANDLE_VALUE))
152 {
153 CloseClipboard();
154 ThrowReaderException(CoderError,"UnableToReadImageData");
155 }
156 clip_size=(size_t) GlobalSize(clip_handle);
157 total_size=clip_size+BMP_HEADER_SIZE;
158 clip_data=AcquireMagickMemory(total_size);
159 if (clip_data == (void *) NULL)
160 {
161 CloseClipboard();
162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
163 }
164 clip_mem=GlobalLock(clip_handle);
165 if (clip_mem == (LPVOID) NULL)
166 {
167 CloseClipboard();
168 clip_data=RelinquishMagickMemory(clip_data);
169 ThrowReaderException(CoderError,"UnableToReadImageData");
170 }
171 p=(unsigned char *) clip_data;
172 p+=BMP_HEADER_SIZE;
173 (void) memcpy(p,clip_mem,clip_size);
174 (void) GlobalUnlock(clip_mem);
175 (void) CloseClipboard();
176 memset(clip_data,0,BMP_HEADER_SIZE);
177 offset=p[0];
178 if ((p[0] == 40) && (p[16] == BI_BITFIELDS))
179 offset+=12;
180 else
181 {
182 unsigned int
183 image_size;
184
185 image_size=(unsigned int) p[20];
186 image_size|=(unsigned int) p[21] << 8;
187 image_size|=(unsigned int) p[22] << 16;
188 image_size|=(unsigned int) p[23] << 24;
189 /* Hack for chrome where the offset seems to be incorrect */
190 if (clip_size - offset - image_size == 12)
191 offset+=12;
192 }
193 offset+=BMP_HEADER_SIZE;
194 p-=BMP_HEADER_SIZE;
195 p[0]='B';
196 p[1]='M';
197 p[2]=(unsigned char) total_size;
198 p[3]=(unsigned char) (total_size >> 8);
199 p[4]=(unsigned char) (total_size >> 16);
200 p[5]=(unsigned char) (total_size >> 24);
201 p[10]=offset;
202 read_info=CloneImageInfo(image_info);
203 (void) CopyMagickString(read_info->magick,"BMP",MagickPathExtent);
204 image=BlobToImage(read_info,clip_data,total_size,exception);
205 read_info=DestroyImageInfo(read_info);
206 clip_data=RelinquishMagickMemory(clip_data);
207 return(image);
208 }
209 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
210
211 /*
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 % %
214 % %
215 % %
216 % R e g i s t e r C L I P B O A R D I m a g e %
217 % %
218 % %
219 % %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %
222 % RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to
223 % the list of supported formats. The attributes include the image format
224 % tag, a method to read and/or write the format, whether the format
225 % supports the saving of more than one frame to the same file or blob,
226 % whether the format supports native in-memory I/O, and a brief
227 % description of the format.
228 %
229 % The format of the RegisterCLIPBOARDImage method is:
230 %
231 % size_t RegisterCLIPBOARDImage(void)
232 %
233 */
RegisterCLIPBOARDImage(void)234 ModuleExport size_t RegisterCLIPBOARDImage(void)
235 {
236 MagickInfo
237 *entry;
238
239 entry=AcquireMagickInfo("CLIPBOARD","CLIPBOARD","The system clipboard");
240 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
241 entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage;
242 entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage;
243 #endif
244 entry->flags^=CoderAdjoinFlag;
245 entry->format_type=ImplicitFormatType;
246 (void) RegisterMagickInfo(entry);
247 return(MagickImageCoderSignature);
248 }
249
250 /*
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % %
253 % %
254 % %
255 % U n r e g i s t e r C L I P B O A R D I m a g e %
256 % %
257 % %
258 % %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 %
261 % UnregisterCLIPBOARDImage() removes format registrations made by the
262 % RGB module from the list of supported formats.
263 %
264 % The format of the UnregisterCLIPBOARDImage method is:
265 %
266 % UnregisterCLIPBOARDImage(void)
267 %
268 */
UnregisterCLIPBOARDImage(void)269 ModuleExport void UnregisterCLIPBOARDImage(void)
270 {
271 (void) UnregisterMagickInfo("CLIPBOARD");
272 }
273
274 /*
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % %
277 % %
278 % %
279 % W r i t e C L I P B O A R D I m a g e %
280 % %
281 % %
282 % %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 %
285 % WriteCLIPBOARDImage() writes an image to the system clipboard.
286 %
287 % The format of the WriteCLIPBOARDImage method is:
288 %
289 % MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
290 % Image *image,ExceptionInfo *exception)
291 %
292 % A description of each parameter follows.
293 %
294 % o image_info: the image info.
295 %
296 % o image: The image.
297 %
298 % o exception: return any errors or warnings in this structure.
299 %
300 */
301 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
WriteCLIPBOARDImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)302 static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
303 Image *image,ExceptionInfo *exception)
304 {
305 HANDLE
306 clip_handle;
307
308 ImageInfo
309 *write_info;
310
311 LPVOID
312 clip_mem;
313
314 size_t
315 length;
316
317 unsigned char
318 *p;
319
320 void
321 *clip_data;
322
323 assert(image_info != (const ImageInfo *) NULL);
324 assert(image_info->signature == MagickCoreSignature);
325 assert(image != (Image *) NULL);
326 assert(image->signature == MagickCoreSignature);
327 if (image->debug != MagickFalse)
328 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
329 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
330 ThrowWriterException(CoderError,"UnableToWriteImageData");
331 write_info=CloneImageInfo(image_info);
332 if (image->alpha_trait == UndefinedPixelTrait)
333 (void) CopyMagickString(write_info->magick,"BMP3",MagickPathExtent);
334 else
335 (void) CopyMagickString(write_info->magick,"BMP",MagickPathExtent);
336 clip_data=ImageToBlob(write_info,image,&length,exception);
337 write_info=DestroyImageInfo(write_info);
338 if (clip_data == (void *) NULL)
339 ThrowWriterException(CoderError,"UnableToWriteImageData");
340 clip_handle=(HANDLE) GlobalAlloc(GMEM_MOVEABLE,length-BMP_HEADER_SIZE);
341 if (clip_handle == (HANDLE) NULL)
342 {
343 clip_data=RelinquishMagickMemory(clip_data);
344 ThrowWriterException(CoderError,"UnableToWriteImageData");
345 }
346 clip_mem=GlobalLock(clip_handle);
347 if (clip_mem == (LPVOID) NULL)
348 {
349 (void) GlobalFree((HGLOBAL) clip_handle);
350 clip_data=RelinquishMagickMemory(clip_data);
351 ThrowWriterException(CoderError,"UnableToWriteImageData");
352 }
353 p=(unsigned char *) clip_data;
354 p+=BMP_HEADER_SIZE;
355 (void) memcpy(clip_mem,p,length-BMP_HEADER_SIZE);
356 (void) GlobalUnlock(clip_mem);
357 clip_data=RelinquishMagickMemory(clip_data);
358 if (!OpenClipboard(NULL))
359 {
360 (void) GlobalFree((HGLOBAL) clip_handle);
361 ThrowWriterException(CoderError,"UnableToWriteImageData");
362 }
363 (void) EmptyClipboard();
364 if (image->alpha_trait == UndefinedPixelTrait)
365 SetClipboardData(CF_DIB,clip_handle);
366 else
367 SetClipboardData(CF_DIBV5,clip_handle);
368 (void) CloseClipboard();
369 return(MagickTrue);
370 }
371 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
372