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-2020 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 MagickBooleanType
126 status;
127
128 register ssize_t
129 x;
130
131 register Quantum
132 *q;
133
134 size_t
135 clip_size,
136 total_size;
137
138 ssize_t
139 y;
140
141 unsigned char
142 offset;
143
144 void
145 *clip_data;
146
147 assert(image_info != (const ImageInfo *) NULL);
148 assert(image_info->signature == MagickCoreSignature);
149 if (image_info->debug != MagickFalse)
150 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
151 image_info->filename);
152 assert(exception != (ExceptionInfo *) NULL);
153 assert(exception->signature == MagickCoreSignature);
154 image=AcquireImage(image_info,exception);
155 if (!IsClipboardFormatAvailable(CF_DIB) &&
156 !IsClipboardFormatAvailable(CF_DIBV5))
157 ThrowReaderException(CoderError,"NoBitmapOnClipboard");
158 if (!OpenClipboard(NULL))
159 ThrowReaderException(CoderError,"UnableToReadImageData");
160 clip_handle=GetClipboardData(CF_DIBV5);
161 if (!clip_handle)
162 clip_handle=GetClipboardData(CF_DIB);
163 if ((clip_handle == NULL) || (clip_handle == INVALID_HANDLE_VALUE))
164 {
165 CloseClipboard();
166 ThrowReaderException(CoderError,"UnableToReadImageData");
167 }
168 clip_size=(size_t) GlobalSize(clip_handle);
169 total_size=clip_size+BMP_HEADER_SIZE;
170 clip_data=AcquireMagickMemory(total_size);
171 if (clip_data == (void *) NULL)
172 {
173 CloseClipboard();
174 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
175 }
176 clip_mem=GlobalLock(clip_handle);
177 if (clip_mem == (LPVOID) NULL)
178 {
179 CloseClipboard();
180 clip_data=RelinquishMagickMemory(clip_data);
181 ThrowReaderException(CoderError,"UnableToReadImageData");
182 }
183 p=(unsigned char *) clip_data;
184 p+=BMP_HEADER_SIZE;
185 (void) memcpy(p,clip_mem,clip_size);
186 (void) GlobalUnlock(clip_mem);
187 (void) CloseClipboard();
188 memset(clip_data,0,BMP_HEADER_SIZE);
189 offset=p[0]+BMP_HEADER_SIZE;
190 if ((p[0] == 40) && (p[16] == BI_BITFIELDS))
191 offset+=12;
192 p-=BMP_HEADER_SIZE;
193 p[0]='B';
194 p[1]='M';
195 p[2]=(unsigned char) total_size;
196 p[3]=(unsigned char) (total_size >> 8);
197 p[4]=(unsigned char) (total_size >> 16);
198 p[5]=(unsigned char) (total_size >> 24);
199 p[10]=offset;
200 read_info=CloneImageInfo(image_info);
201 (void) CopyMagickString(read_info->magick,"BMP",MaxTextExtent);
202 image=BlobToImage(read_info,clip_data,total_size,exception);
203 read_info=DestroyImageInfo(read_info);
204 clip_data=RelinquishMagickMemory(clip_data);
205 return(image);
206 }
207 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
208
209 /*
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 % %
212 % %
213 % %
214 % R e g i s t e r C L I P B O A R D I m a g e %
215 % %
216 % %
217 % %
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 %
220 % RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to
221 % the list of supported formats. The attributes include the image format
222 % tag, a method to read and/or write the format, whether the format
223 % supports the saving of more than one frame to the same file or blob,
224 % whether the format supports native in-memory I/O, and a brief
225 % description of the format.
226 %
227 % The format of the RegisterCLIPBOARDImage method is:
228 %
229 % size_t RegisterCLIPBOARDImage(void)
230 %
231 */
RegisterCLIPBOARDImage(void)232 ModuleExport size_t RegisterCLIPBOARDImage(void)
233 {
234 MagickInfo
235 *entry;
236
237 entry=AcquireMagickInfo("CLIPBOARD","CLIPBOARD","The system clipboard");
238 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
239 entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage;
240 entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage;
241 #endif
242 entry->flags^=CoderAdjoinFlag;
243 entry->format_type=ImplicitFormatType;
244 (void) RegisterMagickInfo(entry);
245 return(MagickImageCoderSignature);
246 }
247
248 /*
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % %
251 % %
252 % %
253 % U n r e g i s t e r C L I P B O A R D I m a g e %
254 % %
255 % %
256 % %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 %
259 % UnregisterCLIPBOARDImage() removes format registrations made by the
260 % RGB module from the list of supported formats.
261 %
262 % The format of the UnregisterCLIPBOARDImage method is:
263 %
264 % UnregisterCLIPBOARDImage(void)
265 %
266 */
UnregisterCLIPBOARDImage(void)267 ModuleExport void UnregisterCLIPBOARDImage(void)
268 {
269 (void) UnregisterMagickInfo("CLIPBOARD");
270 }
271
272 /*
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % %
275 % %
276 % %
277 % W r i t e C L I P B O A R D I m a g e %
278 % %
279 % %
280 % %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 %
283 % WriteCLIPBOARDImage() writes an image to the system clipboard.
284 %
285 % The format of the WriteCLIPBOARDImage method is:
286 %
287 % MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
288 % Image *image,ExceptionInfo *exception)
289 %
290 % A description of each parameter follows.
291 %
292 % o image_info: the image info.
293 %
294 % o image: The image.
295 %
296 % o exception: return any errors or warnings in this structure.
297 %
298 */
299 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
WriteCLIPBOARDImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)300 static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
301 Image *image,ExceptionInfo *exception)
302 {
303 HANDLE
304 clip_handle;
305
306 ImageInfo
307 *write_info;
308
309 LPVOID
310 clip_mem;
311
312 size_t
313 length;
314
315 unsigned char
316 *p;
317
318 void
319 *clip_data;
320
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickCoreSignature);
323 assert(image != (Image *) NULL);
324 assert(image->signature == MagickCoreSignature);
325 if (image->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
327 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
328 ThrowWriterException(CoderError,"UnableToWriteImageData");
329 write_info=CloneImageInfo(image_info);
330 if (image->alpha_trait == UndefinedPixelTrait)
331 (void) CopyMagickString(write_info->magick,"BMP3",MaxTextExtent);
332 else
333 (void) CopyMagickString(write_info->magick,"BMP",MaxTextExtent);
334 clip_data=ImageToBlob(write_info,image,&length,exception);
335 write_info=DestroyImageInfo(write_info);
336 if (clip_data == (void *) NULL)
337 ThrowWriterException(CoderError,"UnableToWriteImageData");
338 clip_handle=(HANDLE) GlobalAlloc(GMEM_MOVEABLE,length-BMP_HEADER_SIZE);
339 if (clip_handle == (HANDLE) NULL)
340 {
341 clip_data=RelinquishMagickMemory(clip_data);
342 ThrowWriterException(CoderError,"UnableToWriteImageData");
343 }
344 clip_mem=GlobalLock(clip_handle);
345 if (clip_mem == (LPVOID) NULL)
346 {
347 (void) GlobalFree((HGLOBAL) clip_handle);
348 clip_data=RelinquishMagickMemory(clip_data);
349 ThrowWriterException(CoderError,"UnableToWriteImageData");
350 }
351 p=(unsigned char *) clip_data;
352 p+=BMP_HEADER_SIZE;
353 (void) memcpy(clip_mem,p,length-BMP_HEADER_SIZE);
354 (void) GlobalUnlock(clip_mem);
355 clip_data=RelinquishMagickMemory(clip_data);
356 if (!OpenClipboard(NULL))
357 {
358 (void) GlobalFree((HGLOBAL) clip_handle);
359 ThrowWriterException(CoderError,"UnableToWriteImageData");
360 }
361 (void) EmptyClipboard();
362 if (image->alpha_trait == UndefinedPixelTrait)
363 SetClipboardData(CF_DIB,clip_handle);
364 else
365 SetClipboardData(CF_DIBV5,clip_handle);
366 (void) CloseClipboard();
367 return(MagickTrue);
368 }
369 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
370