cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 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 % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11 | % % |
| 12 | % % |
| 13 | % Read/Write Windows Clipboard. % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Leonard Rosenthol % |
| 17 | % May 2002 % |
| 18 | % % |
| 19 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 43 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 44 | # if defined(__CYGWIN__) |
| 45 | # include <windows.h> |
| 46 | # else |
| 47 | /* All MinGW needs ... */ |
cristy | b5ae05b | 2012-10-02 18:17:25 +0000 | [diff] [blame] | 48 | # include "MagickCore/nt-base-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 49 | # include <wingdi.h> |
| 50 | # endif |
| 51 | #endif |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | f7836bf | 2012-02-20 16:32:47 +0000 | [diff] [blame] | 62 | #include "MagickCore/nt-feature.h" |
cristy | d3a4892 | 2011-08-05 14:20:50 +0000 | [diff] [blame] | 63 | #include "MagickCore/pixel-accessor.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 64 | #include "MagickCore/quantum-private.h" |
| 65 | #include "MagickCore/static.h" |
| 66 | #include "MagickCore/string_.h" |
| 67 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | Forward declarations. |
| 71 | */ |
| 72 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 73 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 74 | WriteCLIPBOARDImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | /* |
| 78 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 79 | % % |
| 80 | % % |
| 81 | % % |
| 82 | % R e a d C L I P B O A R D I m a g e % |
| 83 | % % |
| 84 | % % |
| 85 | % % |
| 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 87 | % |
| 88 | % ReadCLIPBOARDImage() reads an image from the system clipboard and returns |
| 89 | % it. It allocates the memory necessary for the new Image structure and |
| 90 | % returns a pointer to the new image. |
| 91 | % |
| 92 | % The format of the ReadCLIPBOARDImage method is: |
| 93 | % |
| 94 | % Image *ReadCLIPBOARDImage(const ImageInfo *image_info, |
| 95 | % ExceptionInfo exception) |
| 96 | % |
| 97 | % A description of each parameter follows: |
| 98 | % |
| 99 | % o image_info: the image info. |
| 100 | % |
| 101 | % o exception: return any errors or warnings in this structure. |
| 102 | % |
| 103 | */ |
| 104 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 105 | static Image *ReadCLIPBOARDImage(const ImageInfo *image_info, |
| 106 | ExceptionInfo *exception) |
| 107 | { |
| 108 | Image |
| 109 | *image; |
| 110 | |
dirk | 3ec328f | 2014-12-15 14:57:36 +0000 | [diff] [blame] | 111 | MagickBooleanType |
| 112 | status; |
| 113 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 114 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | x; |
| 116 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 117 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | *q; |
| 119 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 120 | ssize_t |
| 121 | y; |
| 122 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 123 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 124 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 125 | if (image_info->debug != MagickFalse) |
| 126 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 127 | image_info->filename); |
| 128 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 129 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 130 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | { |
| 132 | HBITMAP |
| 133 | bitmapH; |
| 134 | |
| 135 | HPALETTE |
| 136 | hPal; |
| 137 | |
| 138 | OpenClipboard(NULL); |
| 139 | bitmapH=(HBITMAP) GetClipboardData(CF_BITMAP); |
| 140 | hPal=(HPALETTE) GetClipboardData(CF_PALETTE); |
| 141 | CloseClipboard(); |
| 142 | if ( bitmapH == NULL ) |
| 143 | ThrowReaderException(CoderError,"NoBitmapOnClipboard"); |
| 144 | { |
| 145 | BITMAPINFO |
| 146 | DIBinfo; |
| 147 | |
| 148 | BITMAP |
| 149 | bitmap; |
| 150 | |
| 151 | HBITMAP |
| 152 | hBitmap, |
| 153 | hOldBitmap; |
| 154 | |
| 155 | HDC |
| 156 | hDC, |
| 157 | hMemDC; |
| 158 | |
| 159 | RGBQUAD |
| 160 | *pBits, |
| 161 | *ppBits; |
| 162 | |
| 163 | /* create an offscreen DC for the source */ |
| 164 | hMemDC=CreateCompatibleDC(NULL); |
| 165 | hOldBitmap=(HBITMAP) SelectObject(hMemDC,bitmapH); |
| 166 | GetObject(bitmapH,sizeof(BITMAP),(LPSTR) &bitmap); |
| 167 | if ((image->columns == 0) || (image->rows == 0)) |
| 168 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 169 | image->columns=bitmap.bmWidth; |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 170 | image->rows=bitmap.bmHeight; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 172 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 173 | if (status == MagickFalse) |
| 174 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | /* |
| 176 | Initialize the bitmap header info. |
| 177 | */ |
| 178 | (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO)); |
| 179 | DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 180 | DIBinfo.bmiHeader.biWidth=(LONG) image->columns; |
| 181 | DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | DIBinfo.bmiHeader.biPlanes=1; |
| 183 | DIBinfo.bmiHeader.biBitCount=32; |
| 184 | DIBinfo.bmiHeader.biCompression=BI_RGB; |
| 185 | hDC=GetDC(NULL); |
| 186 | if (hDC == 0) |
| 187 | ThrowReaderException(CoderError,"UnableToCreateADC"); |
| 188 | hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits, |
| 189 | NULL,0); |
| 190 | ReleaseDC(NULL,hDC); |
| 191 | if (hBitmap == 0) |
| 192 | ThrowReaderException(CoderError,"UnableToCreateBitmap"); |
| 193 | /* create an offscreen DC */ |
| 194 | hDC=CreateCompatibleDC(NULL); |
| 195 | if (hDC == 0) |
| 196 | { |
| 197 | DeleteObject(hBitmap); |
| 198 | ThrowReaderException(CoderError,"UnableToCreateADC"); |
| 199 | } |
| 200 | hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap); |
| 201 | if (hOldBitmap == 0) |
| 202 | { |
| 203 | DeleteDC(hDC); |
| 204 | DeleteObject(hBitmap); |
| 205 | ThrowReaderException(CoderError,"UnableToCreateBitmap"); |
| 206 | } |
| 207 | if (hPal != NULL) |
| 208 | { |
| 209 | /* Kenichi Masuko says this needed */ |
| 210 | SelectPalette(hDC, hPal, FALSE); |
| 211 | RealizePalette(hDC); |
| 212 | } |
| 213 | /* bitblt from the memory to the DIB-based one */ |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 214 | BitBlt(hDC,0,0,(int) image->columns,(int) image->rows,hMemDC,0,0,SRCCOPY); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | /* finally copy the pixels! */ |
| 216 | pBits=ppBits; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 217 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | { |
| 219 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 220 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 221 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 222 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 223 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 224 | SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q); |
| 225 | SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q); |
| 226 | SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q); |
| 227 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | pBits++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 229 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | } |
| 231 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 232 | break; |
| 233 | } |
| 234 | DeleteDC(hDC); |
| 235 | DeleteObject(hBitmap); |
| 236 | } |
| 237 | } |
| 238 | (void) CloseBlob(image); |
| 239 | return(GetFirstImageInList(image)); |
| 240 | } |
| 241 | #endif /* MAGICKCORE_WINGDI32_DELEGATE */ |
| 242 | |
| 243 | /* |
| 244 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 245 | % % |
| 246 | % % |
| 247 | % % |
| 248 | % R e g i s t e r C L I P B O A R D I m a g e % |
| 249 | % % |
| 250 | % % |
| 251 | % % |
| 252 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 253 | % |
| 254 | % RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to |
| 255 | % the list of supported formats. The attributes include the image format |
| 256 | % tag, a method to read and/or write the format, whether the format |
| 257 | % supports the saving of more than one frame to the same file or blob, |
| 258 | % whether the format supports native in-memory I/O, and a brief |
| 259 | % description of the format. |
| 260 | % |
| 261 | % The format of the RegisterCLIPBOARDImage method is: |
| 262 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 263 | % size_t RegisterCLIPBOARDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | % |
| 265 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 266 | ModuleExport size_t RegisterCLIPBOARDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | { |
| 268 | MagickInfo |
| 269 | *entry; |
| 270 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 271 | entry=AcquireMagickInfo("CLIPBOARD","CLIPBOARD","The system clipboard"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 273 | entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage; |
| 274 | entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage; |
| 275 | #endif |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 276 | entry->flags^=CoderAdjoinFlag; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 277 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 278 | (void) RegisterMagickInfo(entry); |
| 279 | return(MagickImageCoderSignature); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 284 | % % |
| 285 | % % |
| 286 | % % |
| 287 | % U n r e g i s t e r C L I P B O A R D I m a g e % |
| 288 | % % |
| 289 | % % |
| 290 | % % |
| 291 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 292 | % |
| 293 | % UnregisterCLIPBOARDImage() removes format registrations made by the |
| 294 | % RGB module from the list of supported formats. |
| 295 | % |
| 296 | % The format of the UnregisterCLIPBOARDImage method is: |
| 297 | % |
| 298 | % UnregisterCLIPBOARDImage(void) |
| 299 | % |
| 300 | */ |
| 301 | ModuleExport void UnregisterCLIPBOARDImage(void) |
| 302 | { |
| 303 | (void) UnregisterMagickInfo("CLIPBOARD"); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 308 | % % |
| 309 | % % |
| 310 | % % |
| 311 | % W r i t e C L I P B O A R D I m a g e % |
| 312 | % % |
| 313 | % % |
| 314 | % % |
| 315 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 316 | % |
| 317 | % WriteCLIPBOARDImage() writes an image to the system clipboard. |
| 318 | % |
| 319 | % The format of the WriteCLIPBOARDImage method is: |
| 320 | % |
| 321 | % MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 322 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 323 | % |
| 324 | % A description of each parameter follows. |
| 325 | % |
| 326 | % o image_info: the image info. |
| 327 | % |
| 328 | % o image: The image. |
| 329 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 330 | % o exception: return any errors or warnings in this structure. |
| 331 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | */ |
| 333 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 334 | static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 335 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | { |
| 337 | /* |
| 338 | Allocate memory for pixels. |
| 339 | */ |
| 340 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 341 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 342 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 343 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | if (image->debug != MagickFalse) |
| 345 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 346 | { |
| 347 | HBITMAP |
| 348 | bitmapH; |
| 349 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 350 | OpenClipboard(NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | EmptyClipboard(); |
cristy | a7540de | 2012-02-20 15:05:32 +0000 | [diff] [blame] | 352 | bitmapH=(HBITMAP) ImageToHBITMAP(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 353 | SetClipboardData(CF_BITMAP,bitmapH); |
| 354 | CloseClipboard(); |
| 355 | } |
| 356 | return(MagickTrue); |
| 357 | } |
| 358 | #endif /* MAGICKCORE_WINGDI32_DELEGATE */ |