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