]> granicus.if.org Git - imagemagick/blob - coders/clipboard.c
(no commit message)
[imagemagick] / coders / clipboard.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                                                                             %
7 %                                                                             %
8 %                        Read/Write Windows Clipboard.                        %
9 %                                                                             %
10 %                              Software Design                                %
11 %                             Leonard Rosenthol                               %
12 %                                 May 2002                                    %
13 %                                                                             %
14 %                                                                             %
15 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
16 %  dedicated to making software imaging solutions freely available.           %
17 %                                                                             %
18 %  You may not use this file except in compliance with the License.  You may  %
19 %  obtain a copy of the License at                                            %
20 %                                                                             %
21 %    http://www.imagemagick.org/script/license.php                            %
22 %                                                                             %
23 %  Unless required by applicable law or agreed to in writing, software        %
24 %  distributed under the License is distributed on an "AS IS" BASIS,          %
25 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
26 %  See the License for the specific language governing permissions and        %
27 %  limitations under the License.                                             %
28 %                                                                             %
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 %
31 %
32 */
33 \f
34 /*
35   Include declarations.
36 */
37 #include "magick/studio.h"
38 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
39 #  if defined(__CYGWIN__)
40 #    include <windows.h>
41 #  else
42      /* All MinGW needs ... */
43 #    include <wingdi.h>
44 #  endif
45 #endif
46 #include "magick/blob.h"
47 #include "magick/blob-private.h"
48 #include "magick/cache.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/nt-feature.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 \f
62 /*
63   Forward declarations.
64 */
65 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
66 static MagickBooleanType
67   WriteCLIPBOARDImage(const ImageInfo *,Image *);
68 #endif
69 \f
70 /*
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %   R e a d C L I P B O A R D I m a g e                                       %
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %
81 %  ReadCLIPBOARDImage() reads an image from the system clipboard and returns
82 %  it.  It allocates the memory necessary for the new Image structure and
83 %  returns a pointer to the new image.
84 %
85 %  The format of the ReadCLIPBOARDImage method is:
86 %
87 %      Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
88 %        ExceptionInfo exception)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o image_info: the image info.
93 %
94 %    o exception: return any errors or warnings in this structure.
95 %
96 */
97 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
98 static Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
99   ExceptionInfo *exception)
100 {
101   Image
102     *image;
103
104   long
105     y;
106
107   register long
108     x;
109
110   register PixelPacket
111     *q;
112
113   assert(image_info != (const ImageInfo *) NULL);
114   assert(image_info->signature == MagickSignature);
115   if (image_info->debug != MagickFalse)
116     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
117       image_info->filename);
118   assert(exception != (ExceptionInfo *) NULL);
119   assert(exception->signature == MagickSignature);
120   image=AcquireImage(image_info);
121   {
122     HBITMAP
123       bitmapH;
124
125     HPALETTE
126       hPal;
127
128     OpenClipboard(NULL);
129     bitmapH=(HBITMAP) GetClipboardData(CF_BITMAP);
130     hPal=(HPALETTE) GetClipboardData(CF_PALETTE);
131     CloseClipboard();
132     if ( bitmapH == NULL )
133       ThrowReaderException(CoderError,"NoBitmapOnClipboard");
134     {
135       BITMAPINFO
136         DIBinfo;
137
138       BITMAP
139         bitmap;
140
141       HBITMAP
142         hBitmap,
143         hOldBitmap;
144
145       HDC
146         hDC,
147         hMemDC;
148
149       RGBQUAD
150         *pBits,
151         *ppBits;
152
153       /* create an offscreen DC for the source */
154       hMemDC=CreateCompatibleDC(NULL);
155       hOldBitmap=(HBITMAP) SelectObject(hMemDC,bitmapH);
156       GetObject(bitmapH,sizeof(BITMAP),(LPSTR) &bitmap);
157       if ((image->columns == 0) || (image->rows == 0))
158         {
159           image->rows=bitmap.bmHeight;
160           image->columns=bitmap.bmWidth;
161         }
162       /*
163         Initialize the bitmap header info.
164       */
165       (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
166       DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
167       DIBinfo.bmiHeader.biWidth=image->columns;
168       DIBinfo.bmiHeader.biHeight=(-1)*image->rows;
169       DIBinfo.bmiHeader.biPlanes=1;
170       DIBinfo.bmiHeader.biBitCount=32;
171       DIBinfo.bmiHeader.biCompression=BI_RGB;
172       hDC=GetDC(NULL);
173       if (hDC == 0)
174         ThrowReaderException(CoderError,"UnableToCreateADC");
175       hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,
176         NULL,0);
177       ReleaseDC(NULL,hDC);
178       if (hBitmap == 0)
179         ThrowReaderException(CoderError,"UnableToCreateBitmap");
180       /* create an offscreen DC */
181       hDC=CreateCompatibleDC(NULL);
182       if (hDC == 0)
183         {
184           DeleteObject(hBitmap);
185           ThrowReaderException(CoderError,"UnableToCreateADC");
186         }
187       hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
188       if (hOldBitmap == 0)
189         {
190           DeleteDC(hDC);
191           DeleteObject(hBitmap);
192           ThrowReaderException(CoderError,"UnableToCreateBitmap");
193         }
194       if (hPal != NULL)
195       {
196         /* Kenichi Masuko says this needed */
197         SelectPalette(hDC, hPal, FALSE);
198         RealizePalette(hDC);
199       }
200       /* bitblt from the memory to the DIB-based one */
201       BitBlt(hDC,0,0,image->columns,image->rows,hMemDC,0,0,SRCCOPY);
202       /* finally copy the pixels! */
203       pBits=ppBits;
204       for (y=0; y < (long) image->rows; y++)
205       {
206         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
207         if (q == (PixelPacket *) NULL)
208           break;
209         for (x=0; x < (long) image->columns; x++)
210         {
211           q->red=ScaleCharToQuantum(pBits->rgbRed);
212           q->green=ScaleCharToQuantum(pBits->rgbGreen);
213           q->blue=ScaleCharToQuantum(pBits->rgbBlue);
214           SetOpacitySample(q,OpaqueOpacity);
215           pBits++;
216           q++;
217         }
218         if (SyncAuthenticPixels(image,exception) == MagickFalse)
219           break;
220       }
221       DeleteDC(hDC);
222       DeleteObject(hBitmap);
223     }
224   }
225   (void) CloseBlob(image);
226   return(GetFirstImageInList(image));
227 }
228 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
229 \f
230 /*
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 %                                                                             %
233 %                                                                             %
234 %                                                                             %
235 %   R e g i s t e r C L I P B O A R D I m a g e                               %
236 %                                                                             %
237 %                                                                             %
238 %                                                                             %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 %
241 %  RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to
242 %  the list of supported formats.  The attributes include the image format
243 %  tag, a method to read and/or write the format, whether the format
244 %  supports the saving of more than one frame to the same file or blob,
245 %  whether the format supports native in-memory I/O, and a brief
246 %  description of the format.
247 %
248 %  The format of the RegisterCLIPBOARDImage method is:
249 %
250 %      unsigned long RegisterCLIPBOARDImage(void)
251 %
252 */
253 ModuleExport unsigned long RegisterCLIPBOARDImage(void)
254 {
255   MagickInfo
256     *entry;
257
258   entry=SetMagickInfo("CLIPBOARD");
259 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
260   entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage;
261   entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage;
262 #endif
263   entry->adjoin=MagickFalse;
264   entry->format_type=ExplicitFormatType;
265   entry->description=ConstantString("The system clipboard");
266   entry->module=ConstantString("CLIPBOARD");
267   (void) RegisterMagickInfo(entry);
268   return(MagickImageCoderSignature);
269 }
270 \f
271 /*
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 %                                                                             %
274 %                                                                             %
275 %                                                                             %
276 %   U n r e g i s t e r C L I P B O A R D I m a g e                           %
277 %                                                                             %
278 %                                                                             %
279 %                                                                             %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 %
282 %  UnregisterCLIPBOARDImage() removes format registrations made by the
283 %  RGB module from the list of supported formats.
284 %
285 %  The format of the UnregisterCLIPBOARDImage method is:
286 %
287 %      UnregisterCLIPBOARDImage(void)
288 %
289 */
290 ModuleExport void UnregisterCLIPBOARDImage(void)
291 {
292   (void) UnregisterMagickInfo("CLIPBOARD");
293 }
294 \f
295 /*
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 %                                                                             %
298 %                                                                             %
299 %                                                                             %
300 %   W r i t e C L I P B O A R D I m a g e                                     %
301 %                                                                             %
302 %                                                                             %
303 %                                                                             %
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 %
306 %  WriteCLIPBOARDImage() writes an image to the system clipboard.
307 %
308 %  The format of the WriteCLIPBOARDImage method is:
309 %
310 %      MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
311 %        Image *image)
312 %
313 %  A description of each parameter follows.
314 %
315 %    o image_info: the image info.
316 %
317 %    o image:  The image.
318 %
319 */
320 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
321 static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
322   Image *image)
323 {
324   /*
325     Allocate memory for pixels.
326   */
327   assert(image_info != (const ImageInfo *) NULL);
328   assert(image_info->signature == MagickSignature);
329   assert(image != (Image *) NULL);
330   assert(image->signature == MagickSignature);
331   if (image->debug != MagickFalse)
332     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
333   {
334     HBITMAP
335       bitmapH;
336
337     OpenClipboard( NULL );
338     EmptyClipboard();
339     bitmapH=(HBITMAP) ImageToHBITMAP(image);
340     SetClipboardData(CF_BITMAP,bitmapH);
341     CloseClipboard();
342   }
343   return(MagickTrue);
344 }
345 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
346