]> granicus.if.org Git - imagemagick/blob - coders/clipboard.c
(no commit message)
[imagemagick] / coders / clipboard.c
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-2011 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 %    http://www.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 \f
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 <wingdi.h>
49 #  endif
50 #endif
51 #include "MagickCore/blob.h"
52 #include "MagickCore/blob-private.h"
53 #include "MagickCore/cache.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/nt-feature.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
67 \f
68 /*
69   Forward declarations.
70 */
71 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
72 static MagickBooleanType
73   WriteCLIPBOARDImage(const ImageInfo *,Image *,ExceptionInfo *);
74 #endif
75 \f
76 /*
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %                                                                             %
79 %                                                                             %
80 %                                                                             %
81 %   R e a d C L I P B O A R D I m a g e                                       %
82 %                                                                             %
83 %                                                                             %
84 %                                                                             %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %
87 %  ReadCLIPBOARDImage() reads an image from the system clipboard and returns
88 %  it.  It allocates the memory necessary for the new Image structure and
89 %  returns a pointer to the new image.
90 %
91 %  The format of the ReadCLIPBOARDImage method is:
92 %
93 %      Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
94 %        ExceptionInfo exception)
95 %
96 %  A description of each parameter follows:
97 %
98 %    o image_info: the image info.
99 %
100 %    o exception: return any errors or warnings in this structure.
101 %
102 */
103 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
104 static Image *ReadCLIPBOARDImage(const ImageInfo *image_info,
105   ExceptionInfo *exception)
106 {
107   Image
108     *image;
109
110   register ssize_t
111     x;
112
113   register Quantum
114     *q;
115
116   ssize_t
117     y;
118
119   assert(image_info != (const ImageInfo *) NULL);
120   assert(image_info->signature == MagickSignature);
121   if (image_info->debug != MagickFalse)
122     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
123       image_info->filename);
124   assert(exception != (ExceptionInfo *) NULL);
125   assert(exception->signature == MagickSignature);
126   image=AcquireImage(image_info,exception);
127   {
128     HBITMAP
129       bitmapH;
130
131     HPALETTE
132       hPal;
133
134     OpenClipboard(NULL);
135     bitmapH=(HBITMAP) GetClipboardData(CF_BITMAP);
136     hPal=(HPALETTE) GetClipboardData(CF_PALETTE);
137     CloseClipboard();
138     if ( bitmapH == NULL )
139       ThrowReaderException(CoderError,"NoBitmapOnClipboard");
140     {
141       BITMAPINFO
142         DIBinfo;
143
144       BITMAP
145         bitmap;
146
147       HBITMAP
148         hBitmap,
149         hOldBitmap;
150
151       HDC
152         hDC,
153         hMemDC;
154
155       RGBQUAD
156         *pBits,
157         *ppBits;
158
159       /* create an offscreen DC for the source */
160       hMemDC=CreateCompatibleDC(NULL);
161       hOldBitmap=(HBITMAP) SelectObject(hMemDC,bitmapH);
162       GetObject(bitmapH,sizeof(BITMAP),(LPSTR) &bitmap);
163       if ((image->columns == 0) || (image->rows == 0))
164         {
165           image->rows=bitmap.bmHeight;
166           image->columns=bitmap.bmWidth;
167         }
168       /*
169         Initialize the bitmap header info.
170       */
171       (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
172       DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
173       DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
174       DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
175       DIBinfo.bmiHeader.biPlanes=1;
176       DIBinfo.bmiHeader.biBitCount=32;
177       DIBinfo.bmiHeader.biCompression=BI_RGB;
178       hDC=GetDC(NULL);
179       if (hDC == 0)
180         ThrowReaderException(CoderError,"UnableToCreateADC");
181       hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,
182         NULL,0);
183       ReleaseDC(NULL,hDC);
184       if (hBitmap == 0)
185         ThrowReaderException(CoderError,"UnableToCreateBitmap");
186       /* create an offscreen DC */
187       hDC=CreateCompatibleDC(NULL);
188       if (hDC == 0)
189         {
190           DeleteObject(hBitmap);
191           ThrowReaderException(CoderError,"UnableToCreateADC");
192         }
193       hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
194       if (hOldBitmap == 0)
195         {
196           DeleteDC(hDC);
197           DeleteObject(hBitmap);
198           ThrowReaderException(CoderError,"UnableToCreateBitmap");
199         }
200       if (hPal != NULL)
201       {
202         /* Kenichi Masuko says this needed */
203         SelectPalette(hDC, hPal, FALSE);
204         RealizePalette(hDC);
205       }
206       /* bitblt from the memory to the DIB-based one */
207       BitBlt(hDC,0,0,(int) image->columns,(int) image->rows,hMemDC,0,0,SRCCOPY);
208       /* finally copy the pixels! */
209       pBits=ppBits;
210       for (y=0; y < (ssize_t) image->rows; y++)
211       {
212         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
213         if (q == (Quantum *) NULL)
214           break;
215         for (x=0; x < (ssize_t) image->columns; x++)
216         {
217           SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
218           SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
219           SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
220           SetPixelAlpha(image,OpaqueAlpha,q);
221           pBits++;
222           q+=GetPixelChannels(image);
223         }
224         if (SyncAuthenticPixels(image,exception) == MagickFalse)
225           break;
226       }
227       DeleteDC(hDC);
228       DeleteObject(hBitmap);
229     }
230   }
231   (void) CloseBlob(image);
232   return(GetFirstImageInList(image));
233 }
234 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
235 \f
236 /*
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 %                                                                             %
239 %                                                                             %
240 %                                                                             %
241 %   R e g i s t e r C L I P B O A R D I m a g e                               %
242 %                                                                             %
243 %                                                                             %
244 %                                                                             %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 %
247 %  RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to
248 %  the list of supported formats.  The attributes include the image format
249 %  tag, a method to read and/or write the format, whether the format
250 %  supports the saving of more than one frame to the same file or blob,
251 %  whether the format supports native in-memory I/O, and a brief
252 %  description of the format.
253 %
254 %  The format of the RegisterCLIPBOARDImage method is:
255 %
256 %      size_t RegisterCLIPBOARDImage(void)
257 %
258 */
259 ModuleExport size_t RegisterCLIPBOARDImage(void)
260 {
261   MagickInfo
262     *entry;
263
264   entry=SetMagickInfo("CLIPBOARD");
265 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
266   entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage;
267   entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage;
268 #endif
269   entry->adjoin=MagickFalse;
270   entry->format_type=ImplicitFormatType;
271   entry->description=ConstantString("The system clipboard");
272   entry->module=ConstantString("CLIPBOARD");
273   (void) RegisterMagickInfo(entry);
274   return(MagickImageCoderSignature);
275 }
276 \f
277 /*
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 %                                                                             %
280 %                                                                             %
281 %                                                                             %
282 %   U n r e g i s t e r C L I P B O A R D I m a g e                           %
283 %                                                                             %
284 %                                                                             %
285 %                                                                             %
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 %
288 %  UnregisterCLIPBOARDImage() removes format registrations made by the
289 %  RGB module from the list of supported formats.
290 %
291 %  The format of the UnregisterCLIPBOARDImage method is:
292 %
293 %      UnregisterCLIPBOARDImage(void)
294 %
295 */
296 ModuleExport void UnregisterCLIPBOARDImage(void)
297 {
298   (void) UnregisterMagickInfo("CLIPBOARD");
299 }
300 \f
301 /*
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 %                                                                             %
304 %                                                                             %
305 %                                                                             %
306 %   W r i t e C L I P B O A R D I m a g e                                     %
307 %                                                                             %
308 %                                                                             %
309 %                                                                             %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 %
312 %  WriteCLIPBOARDImage() writes an image to the system clipboard.
313 %
314 %  The format of the WriteCLIPBOARDImage method is:
315 %
316 %      MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
317 %        Image *image,ExceptionInfo *exception)
318 %
319 %  A description of each parameter follows.
320 %
321 %    o image_info: the image info.
322 %
323 %    o image:  The image.
324 %
325 %    o exception: return any errors or warnings in this structure.
326 %
327 */
328 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
329 static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info,
330   Image *image,ExceptionInfo *exception)
331 {
332   /*
333     Allocate memory for pixels.
334   */
335   assert(image_info != (const ImageInfo *) NULL);
336   assert(image_info->signature == MagickSignature);
337   assert(image != (Image *) NULL);
338   assert(image->signature == MagickSignature);
339   if (image->debug != MagickFalse)
340     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
341   {
342     HBITMAP
343       bitmapH;
344
345     OpenClipboard(NULL);
346     EmptyClipboard();
347     bitmapH=(HBITMAP) ImageToHBITMAP(image);
348     SetClipboardData(CF_BITMAP,bitmapH);
349     CloseClipboard();
350   }
351   return(MagickTrue);
352 }
353 #endif /* MAGICKCORE_WINGDI32_DELEGATE */