root / ImageMagick / trunk / coders / clipboard.c

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