2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % Read/Write Windows Clipboard. %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
44 # if defined(__CYGWIN__)
47 /* All MinGW needs ... */
48 # include "MagickCore/nt-base-private.h"
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"
69 #define BMP_HEADER_SIZE 14
74 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
75 static MagickBooleanType
76 WriteCLIPBOARDImage(const ImageInfo *,Image *,ExceptionInfo *);
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % R e a d C L I P B O A R D I m a g e %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
94 % The format of the ReadCLIPBOARDImage method is:
96 % Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
97 % ExceptionInfo exception)
99 % A description of each parameter follows:
101 % o image_info: the image info.
103 % o exception: return any errors or warnings in this structure.
106 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
107 static Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
108 ExceptionInfo *exception)
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_BITMAP) &&
156 !IsClipboardFormatAvailable(CF_DIB) &&
157 !IsClipboardFormatAvailable(CF_DIBV5))
158 ThrowReaderException(CoderError,"NoBitmapOnClipboard");
159 if (!OpenClipboard(NULL))
160 ThrowReaderException(CoderError,"UnableToReadImageData");
161 clip_handle=GetClipboardData(CF_DIBV5);
163 clip_handle=GetClipboardData(CF_DIB);
164 if ((clip_handle == NULL) || (clip_handle == INVALID_HANDLE_VALUE))
167 ThrowReaderException(CoderError,"UnableToReadImageData");
169 clip_size=(size_t) GlobalSize(clip_handle);
170 total_size=clip_size+BMP_HEADER_SIZE;
171 clip_data=AcquireMagickMemory(total_size);
172 if (clip_data == (void *) NULL)
175 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
177 clip_mem=GlobalLock(clip_handle);
178 if (clip_mem == (LPVOID) NULL)
181 ThrowReaderException(CoderError,"UnableToReadImageData");
183 p=(unsigned char *) clip_data;
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=((unsigned int) p[0])+BMP_HEADER_SIZE;
193 p[2]=(unsigned char) total_size;
194 p[3]=(unsigned char) (total_size >> 8);
195 p[4]=(unsigned char) (total_size >> 16);
196 p[5]=(unsigned char) (total_size >> 24);
198 read_info=CloneImageInfo(image_info);
199 (void) CopyMagickString(read_info->magick,"BMP",MaxTextExtent);
200 image=BlobToImage(read_info,clip_data,total_size,exception);
201 read_info=DestroyImageInfo(read_info);
202 clip_data=RelinquishMagickMemory(clip_data);
205 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % R e g i s t e r C L I P B O A R D I m a g e %
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to
219 % the list of supported formats. The attributes include the image format
220 % tag, a method to read and/or write the format, whether the format
221 % supports the saving of more than one frame to the same file or blob,
222 % whether the format supports native in-memory I/O, and a brief
223 % description of the format.
225 % The format of the RegisterCLIPBOARDImage method is:
227 % size_t RegisterCLIPBOARDImage(void)
230 ModuleExport size_t RegisterCLIPBOARDImage(void)
235 entry=AcquireMagickInfo("CLIPBOARD","CLIPBOARD","The system clipboard");
236 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
237 entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage;
238 entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage;
240 entry->flags^=CoderAdjoinFlag;
241 entry->format_type=ImplicitFormatType;
242 (void) RegisterMagickInfo(entry);
243 return(MagickImageCoderSignature);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % U n r e g i s t e r C L I P B O A R D I m a g e %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % UnregisterCLIPBOARDImage() removes format registrations made by the
258 % RGB module from the list of supported formats.
260 % The format of the UnregisterCLIPBOARDImage method is:
262 % UnregisterCLIPBOARDImage(void)
265 ModuleExport void UnregisterCLIPBOARDImage(void)
267 (void) UnregisterMagickInfo("CLIPBOARD");
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % W r i t e C L I P B O A R D I m a g e %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % WriteCLIPBOARDImage() writes an image to the system clipboard.
283 % The format of the WriteCLIPBOARDImage method is:
285 % MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
286 % Image *image,ExceptionInfo *exception)
288 % A description of each parameter follows.
290 % o image_info: the image info.
292 % o image: The image.
294 % o exception: return any errors or warnings in this structure.
297 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
298 static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
299 Image *image,ExceptionInfo *exception)
319 assert(image_info != (const ImageInfo *) NULL);
320 assert(image_info->signature == MagickCoreSignature);
321 assert(image != (Image *) NULL);
322 assert(image->signature == MagickCoreSignature);
323 if (image->debug != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
325 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
326 ThrowWriterException(CoderError,"UnableToWriteImageData");
327 write_info=CloneImageInfo(image_info);
328 if (image->alpha_trait == UndefinedPixelTrait)
329 (void) CopyMagickString(write_info->magick,"BMP3",MaxTextExtent);
331 (void) CopyMagickString(write_info->magick,"BMP",MaxTextExtent);
332 clip_data=ImageToBlob(write_info,image,&length,exception);
333 write_info=DestroyImageInfo(write_info);
334 if (clip_data == (void *) NULL)
335 ThrowWriterException(CoderError,"UnableToWriteImageData");
336 clip_handle=(HANDLE) GlobalAlloc(GMEM_MOVEABLE,length-BMP_HEADER_SIZE);
337 if (clip_handle == (HANDLE) NULL)
339 clip_data=RelinquishMagickMemory(clip_data);
340 ThrowWriterException(CoderError,"UnableToWriteImageData");
342 clip_mem=GlobalLock(clip_handle);
343 if (clip_handle == (LPVOID) NULL)
345 (void) GlobalFree((HGLOBAL) clip_handle);
346 clip_data=RelinquishMagickMemory(clip_data);
347 ThrowWriterException(CoderError,"UnableToWriteImageData");
349 p=(unsigned char *) clip_data;
351 (void) memcpy(clip_mem,p,length-BMP_HEADER_SIZE);
352 (void) GlobalUnlock(clip_mem);
353 clip_data=RelinquishMagickMemory(clip_data);
354 if (!OpenClipboard(NULL))
356 (void) GlobalFree((HGLOBAL) clip_handle);
357 ThrowWriterException(CoderError,"UnableToWriteImageData");
359 (void) EmptyClipboard();
360 if (image->alpha_trait == UndefinedPixelTrait)
361 SetClipboardData(CF_DIB,clip_handle);
363 SetClipboardData(CF_DIBV5,clip_handle);
364 (void) CloseClipboard();
367 #endif /* MAGICKCORE_WINGDI32_DELEGATE */