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 | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 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 ... */ |
| 48 | # include <wingdi.h> |
| 49 | # endif |
| 50 | #endif |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 2c5fc27 | 2012-02-22 01:27:46 +0000 | [diff] [blame] | 61 | #include "MagickCore/nt-base-private.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 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 111 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | x; |
| 113 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 114 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | *q; |
| 116 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 117 | ssize_t |
| 118 | y; |
| 119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 120 | assert(image_info != (const ImageInfo *) NULL); |
| 121 | assert(image_info->signature == MagickSignature); |
| 122 | if (image_info->debug != MagickFalse) |
| 123 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 124 | image_info->filename); |
| 125 | assert(exception != (ExceptionInfo *) NULL); |
| 126 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 127 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 128 | { |
| 129 | HBITMAP |
| 130 | bitmapH; |
| 131 | |
| 132 | HPALETTE |
| 133 | hPal; |
| 134 | |
| 135 | OpenClipboard(NULL); |
| 136 | bitmapH=(HBITMAP) GetClipboardData(CF_BITMAP); |
| 137 | hPal=(HPALETTE) GetClipboardData(CF_PALETTE); |
| 138 | CloseClipboard(); |
| 139 | if ( bitmapH == NULL ) |
| 140 | ThrowReaderException(CoderError,"NoBitmapOnClipboard"); |
| 141 | { |
| 142 | BITMAPINFO |
| 143 | DIBinfo; |
| 144 | |
| 145 | BITMAP |
| 146 | bitmap; |
| 147 | |
| 148 | HBITMAP |
| 149 | hBitmap, |
| 150 | hOldBitmap; |
| 151 | |
| 152 | HDC |
| 153 | hDC, |
| 154 | hMemDC; |
| 155 | |
| 156 | RGBQUAD |
| 157 | *pBits, |
| 158 | *ppBits; |
| 159 | |
| 160 | /* create an offscreen DC for the source */ |
| 161 | hMemDC=CreateCompatibleDC(NULL); |
| 162 | hOldBitmap=(HBITMAP) SelectObject(hMemDC,bitmapH); |
| 163 | GetObject(bitmapH,sizeof(BITMAP),(LPSTR) &bitmap); |
| 164 | if ((image->columns == 0) || (image->rows == 0)) |
| 165 | { |
| 166 | image->rows=bitmap.bmHeight; |
| 167 | image->columns=bitmap.bmWidth; |
| 168 | } |
| 169 | /* |
| 170 | Initialize the bitmap header info. |
| 171 | */ |
| 172 | (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO)); |
| 173 | DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 174 | DIBinfo.bmiHeader.biWidth=(LONG) image->columns; |
| 175 | DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | DIBinfo.bmiHeader.biPlanes=1; |
| 177 | DIBinfo.bmiHeader.biBitCount=32; |
| 178 | DIBinfo.bmiHeader.biCompression=BI_RGB; |
| 179 | hDC=GetDC(NULL); |
| 180 | if (hDC == 0) |
| 181 | ThrowReaderException(CoderError,"UnableToCreateADC"); |
| 182 | hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits, |
| 183 | NULL,0); |
| 184 | ReleaseDC(NULL,hDC); |
| 185 | if (hBitmap == 0) |
| 186 | ThrowReaderException(CoderError,"UnableToCreateBitmap"); |
| 187 | /* create an offscreen DC */ |
| 188 | hDC=CreateCompatibleDC(NULL); |
| 189 | if (hDC == 0) |
| 190 | { |
| 191 | DeleteObject(hBitmap); |
| 192 | ThrowReaderException(CoderError,"UnableToCreateADC"); |
| 193 | } |
| 194 | hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap); |
| 195 | if (hOldBitmap == 0) |
| 196 | { |
| 197 | DeleteDC(hDC); |
| 198 | DeleteObject(hBitmap); |
| 199 | ThrowReaderException(CoderError,"UnableToCreateBitmap"); |
| 200 | } |
| 201 | if (hPal != NULL) |
| 202 | { |
| 203 | /* Kenichi Masuko says this needed */ |
| 204 | SelectPalette(hDC, hPal, FALSE); |
| 205 | RealizePalette(hDC); |
| 206 | } |
| 207 | /* bitblt from the memory to the DIB-based one */ |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 208 | 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] | 209 | /* finally copy the pixels! */ |
| 210 | pBits=ppBits; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 211 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | { |
| 213 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 214 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 216 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 218 | SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q); |
| 219 | SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q); |
| 220 | SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q); |
| 221 | SetPixelAlpha(image,OpaqueAlpha,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | pBits++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 223 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 224 | } |
| 225 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 226 | break; |
| 227 | } |
| 228 | DeleteDC(hDC); |
| 229 | DeleteObject(hBitmap); |
| 230 | } |
| 231 | } |
| 232 | (void) CloseBlob(image); |
| 233 | return(GetFirstImageInList(image)); |
| 234 | } |
| 235 | #endif /* MAGICKCORE_WINGDI32_DELEGATE */ |
| 236 | |
| 237 | /* |
| 238 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 239 | % % |
| 240 | % % |
| 241 | % % |
| 242 | % R e g i s t e r C L I P B O A R D I m a g e % |
| 243 | % % |
| 244 | % % |
| 245 | % % |
| 246 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 247 | % |
| 248 | % RegisterCLIPBOARDImage() adds attributes for the clipboard "image format" to |
| 249 | % the list of supported formats. The attributes include the image format |
| 250 | % tag, a method to read and/or write the format, whether the format |
| 251 | % supports the saving of more than one frame to the same file or blob, |
| 252 | % whether the format supports native in-memory I/O, and a brief |
| 253 | % description of the format. |
| 254 | % |
| 255 | % The format of the RegisterCLIPBOARDImage method is: |
| 256 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 257 | % size_t RegisterCLIPBOARDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | % |
| 259 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 260 | ModuleExport size_t RegisterCLIPBOARDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 261 | { |
| 262 | MagickInfo |
| 263 | *entry; |
| 264 | |
| 265 | entry=SetMagickInfo("CLIPBOARD"); |
| 266 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 267 | entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage; |
| 268 | entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage; |
| 269 | #endif |
| 270 | entry->adjoin=MagickFalse; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 271 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | entry->description=ConstantString("The system clipboard"); |
| 273 | entry->module=ConstantString("CLIPBOARD"); |
| 274 | (void) RegisterMagickInfo(entry); |
| 275 | return(MagickImageCoderSignature); |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 280 | % % |
| 281 | % % |
| 282 | % % |
| 283 | % U n r e g i s t e r C L I P B O A R D I m a g e % |
| 284 | % % |
| 285 | % % |
| 286 | % % |
| 287 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 288 | % |
| 289 | % UnregisterCLIPBOARDImage() removes format registrations made by the |
| 290 | % RGB module from the list of supported formats. |
| 291 | % |
| 292 | % The format of the UnregisterCLIPBOARDImage method is: |
| 293 | % |
| 294 | % UnregisterCLIPBOARDImage(void) |
| 295 | % |
| 296 | */ |
| 297 | ModuleExport void UnregisterCLIPBOARDImage(void) |
| 298 | { |
| 299 | (void) UnregisterMagickInfo("CLIPBOARD"); |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 304 | % % |
| 305 | % % |
| 306 | % % |
| 307 | % W r i t e C L I P B O A R D I m a g e % |
| 308 | % % |
| 309 | % % |
| 310 | % % |
| 311 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 312 | % |
| 313 | % WriteCLIPBOARDImage() writes an image to the system clipboard. |
| 314 | % |
| 315 | % The format of the WriteCLIPBOARDImage method is: |
| 316 | % |
| 317 | % MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 318 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 319 | % |
| 320 | % A description of each parameter follows. |
| 321 | % |
| 322 | % o image_info: the image info. |
| 323 | % |
| 324 | % o image: The image. |
| 325 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 326 | % o exception: return any errors or warnings in this structure. |
| 327 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | */ |
| 329 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 330 | static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 331 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | { |
| 333 | /* |
| 334 | Allocate memory for pixels. |
| 335 | */ |
| 336 | assert(image_info != (const ImageInfo *) NULL); |
| 337 | assert(image_info->signature == MagickSignature); |
| 338 | assert(image != (Image *) NULL); |
| 339 | assert(image->signature == MagickSignature); |
| 340 | if (image->debug != MagickFalse) |
| 341 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 342 | { |
| 343 | HBITMAP |
| 344 | bitmapH; |
| 345 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 346 | OpenClipboard(NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | EmptyClipboard(); |
cristy | a7540de | 2012-02-20 15:05:32 +0000 | [diff] [blame] | 348 | bitmapH=(HBITMAP) ImageToHBITMAP(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | SetClipboardData(CF_BITMAP,bitmapH); |
| 350 | CloseClipboard(); |
| 351 | } |
| 352 | return(MagickTrue); |
| 353 | } |
| 354 | #endif /* MAGICKCORE_WINGDI32_DELEGATE */ |