| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "MagickCore/studio.h" |
| 43 |
#if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 44 |
# if defined(__CYGWIN__) |
| 45 |
# include <windows.h> |
| 46 |
# else |
| 47 |
|
| 48 |
# include "MagickCore/nt-base-private.h" |
| 49 |
# include <wingdi.h> |
| 50 |
# endif |
| 51 |
#endif |
| 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" |
| 62 |
#include "MagickCore/nt-feature.h" |
| 63 |
#include "MagickCore/pixel-accessor.h" |
| 64 |
#include "MagickCore/quantum-private.h" |
| 65 |
#include "MagickCore/static.h" |
| 66 |
#include "MagickCore/string_.h" |
| 67 |
#include "MagickCore/module.h" |
| 68 |
|
| 69 |
#define BMP_HEADER_SIZE 14 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 75 |
static MagickBooleanType |
| 76 |
WriteCLIPBOARDImage(const ImageInfo *,Image *,ExceptionInfo *); |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 107 |
static Image *ReadCLIPBOARDImage(const ImageInfo *image_info, |
| 108 |
ExceptionInfo *exception) |
| 109 |
{ |
| 110 |
unsigned char |
| 111 |
*p; |
| 112 |
|
| 113 |
HANDLE |
| 114 |
clip_handle; |
| 115 |
|
| 116 |
Image |
| 117 |
*image; |
| 118 |
|
| 119 |
ImageInfo |
| 120 |
*read_info; |
| 121 |
|
| 122 |
LPVOID |
| 123 |
clip_mem; |
| 124 |
|
| 125 |
size_t |
| 126 |
clip_size, |
| 127 |
total_size; |
| 128 |
|
| 129 |
unsigned char |
| 130 |
offset; |
| 131 |
|
| 132 |
void |
| 133 |
*clip_data; |
| 134 |
|
| 135 |
assert(image_info != (const ImageInfo *) NULL); |
| 136 |
assert(image_info->signature == MagickCoreSignature); |
| 137 |
assert(exception != (ExceptionInfo *) NULL); |
| 138 |
assert(exception->signature == MagickCoreSignature); |
| 139 |
if (IsEventLogging() != MagickFalse) |
| 140 |
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 141 |
image_info->filename); |
| 142 |
image=AcquireImage(image_info,exception); |
| 143 |
if (!IsClipboardFormatAvailable(CF_DIB) && |
| 144 |
!IsClipboardFormatAvailable(CF_DIBV5)) |
| 145 |
ThrowReaderException(CoderError,"NoBitmapOnClipboard"); |
| 146 |
if (!OpenClipboard(NULL)) |
| 147 |
ThrowReaderException(CoderError,"UnableToReadImageData"); |
| 148 |
clip_handle=GetClipboardData(CF_DIBV5); |
| 149 |
if (!clip_handle) |
| 150 |
clip_handle=GetClipboardData(CF_DIB); |
| 151 |
if ((clip_handle == NULL) || (clip_handle == INVALID_HANDLE_VALUE)) |
| 152 |
{ |
| 153 |
CloseClipboard(); |
| 154 |
ThrowReaderException(CoderError,"UnableToReadImageData"); |
| 155 |
} |
| 156 |
clip_size=(size_t) GlobalSize(clip_handle); |
| 157 |
total_size=clip_size+BMP_HEADER_SIZE; |
| 158 |
clip_data=AcquireMagickMemory(total_size); |
| 159 |
if (clip_data == (void *) NULL) |
| 160 |
{ |
| 161 |
CloseClipboard(); |
| 162 |
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 163 |
} |
| 164 |
clip_mem=GlobalLock(clip_handle); |
| 165 |
if (clip_mem == (LPVOID) NULL) |
| 166 |
{ |
| 167 |
CloseClipboard(); |
| 168 |
clip_data=RelinquishMagickMemory(clip_data); |
| 169 |
ThrowReaderException(CoderError,"UnableToReadImageData"); |
| 170 |
} |
| 171 |
p=(unsigned char *) clip_data; |
| 172 |
p+=(ptrdiff_t) BMP_HEADER_SIZE; |
| 173 |
(void) memcpy(p,clip_mem,clip_size); |
| 174 |
(void) GlobalUnlock(clip_mem); |
| 175 |
(void) CloseClipboard(); |
| 176 |
memset(clip_data,0,BMP_HEADER_SIZE); |
| 177 |
offset=p[0]; |
| 178 |
if ((p[0] == 40) && (p[16] == BI_BITFIELDS)) |
| 179 |
offset+=12; |
| 180 |
else |
| 181 |
{ |
| 182 |
unsigned int |
| 183 |
image_size; |
| 184 |
|
| 185 |
image_size=(unsigned int) p[20]; |
| 186 |
image_size|=(unsigned int) p[21] << 8; |
| 187 |
image_size|=(unsigned int) p[22] << 16; |
| 188 |
image_size|=(unsigned int) p[23] << 24; |
| 189 |
|
| 190 |
if (clip_size - offset - image_size == 12) |
| 191 |
offset+=12; |
| 192 |
} |
| 193 |
offset+=BMP_HEADER_SIZE; |
| 194 |
p-=(ptrdiff_t)BMP_HEADER_SIZE; |
| 195 |
p[0]='B'; |
| 196 |
p[1]='M'; |
| 197 |
p[2]=(unsigned char) total_size; |
| 198 |
p[3]=(unsigned char) (total_size >> 8); |
| 199 |
p[4]=(unsigned char) (total_size >> 16); |
| 200 |
p[5]=(unsigned char) (total_size >> 24); |
| 201 |
p[10]=offset; |
| 202 |
read_info=CloneImageInfo(image_info); |
| 203 |
(void) CopyMagickString(read_info->magick,"BMP",MagickPathExtent); |
| 204 |
image=BlobToImage(read_info,clip_data,total_size,exception); |
| 205 |
read_info=DestroyImageInfo(read_info); |
| 206 |
clip_data=RelinquishMagickMemory(clip_data); |
| 207 |
return(image); |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ModuleExport size_t RegisterCLIPBOARDImage(void) |
| 235 |
{ |
| 236 |
MagickInfo |
| 237 |
*entry; |
| 238 |
|
| 239 |
entry=AcquireMagickInfo("CLIPBOARD","CLIPBOARD","The system clipboard"); |
| 240 |
#if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 241 |
entry->decoder=(DecodeImageHandler *) ReadCLIPBOARDImage; |
| 242 |
entry->encoder=(EncodeImageHandler *) WriteCLIPBOARDImage; |
| 243 |
#endif |
| 244 |
entry->flags^=CoderAdjoinFlag; |
| 245 |
entry->format_type=ImplicitFormatType; |
| 246 |
(void) RegisterMagickInfo(entry); |
| 247 |
return(MagickImageCoderSignature); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
ModuleExport void UnregisterCLIPBOARDImage(void) |
| 270 |
{ |
| 271 |
(void) UnregisterMagickInfo("CLIPBOARD"); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 302 |
static MagickBooleanType WriteCLIPBOARDImage(const ImageInfo *image_info, |
| 303 |
Image *image,ExceptionInfo *exception) |
| 304 |
{ |
| 305 |
HANDLE |
| 306 |
clip_handle; |
| 307 |
|
| 308 |
ImageInfo |
| 309 |
*write_info; |
| 310 |
|
| 311 |
LPVOID |
| 312 |
clip_mem; |
| 313 |
|
| 314 |
size_t |
| 315 |
length; |
| 316 |
|
| 317 |
unsigned char |
| 318 |
*p; |
| 319 |
|
| 320 |
void |
| 321 |
*clip_data; |
| 322 |
|
| 323 |
assert(image_info != (const ImageInfo *) NULL); |
| 324 |
assert(image_info->signature == MagickCoreSignature); |
| 325 |
assert(image != (Image *) NULL); |
| 326 |
assert(image->signature == MagickCoreSignature); |
| 327 |
if (IsEventLogging() != MagickFalse) |
| 328 |
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 329 |
if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 330 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 331 |
write_info=CloneImageInfo(image_info); |
| 332 |
if ((image->alpha_trait & BlendPixelTrait) == 0) |
| 333 |
(void) CopyMagickString(write_info->magick,"BMP3",MagickPathExtent); |
| 334 |
else |
| 335 |
(void) CopyMagickString(write_info->magick,"BMP",MagickPathExtent); |
| 336 |
clip_data=ImageToBlob(write_info,image,&length,exception); |
| 337 |
write_info=DestroyImageInfo(write_info); |
| 338 |
if (clip_data == (void *) NULL) |
| 339 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 340 |
clip_handle=(HANDLE) GlobalAlloc(GMEM_MOVEABLE,length-BMP_HEADER_SIZE); |
| 341 |
if (clip_handle == (HANDLE) NULL) |
| 342 |
{ |
| 343 |
clip_data=RelinquishMagickMemory(clip_data); |
| 344 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 345 |
} |
| 346 |
clip_mem=GlobalLock(clip_handle); |
| 347 |
if (clip_mem == (LPVOID) NULL) |
| 348 |
{ |
| 349 |
(void) GlobalFree((HGLOBAL) clip_handle); |
| 350 |
clip_data=RelinquishMagickMemory(clip_data); |
| 351 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 352 |
} |
| 353 |
p=(unsigned char *) clip_data; |
| 354 |
p+=(ptrdiff_t) BMP_HEADER_SIZE; |
| 355 |
(void) memcpy(clip_mem,p,length-BMP_HEADER_SIZE); |
| 356 |
(void) GlobalUnlock(clip_mem); |
| 357 |
clip_data=RelinquishMagickMemory(clip_data); |
| 358 |
if (!OpenClipboard(NULL)) |
| 359 |
{ |
| 360 |
(void) GlobalFree((HGLOBAL) clip_handle); |
| 361 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 362 |
} |
| 363 |
(void) EmptyClipboard(); |
| 364 |
if ((image->alpha_trait & BlendPixelTrait) == 0) |
| 365 |
SetClipboardData(CF_DIB,clip_handle); |
| 366 |
else |
| 367 |
SetClipboardData(CF_DIBV5,clip_handle); |
| 368 |
(void) CloseClipboard(); |
| 369 |
return(MagickTrue); |
| 370 |
} |
| 371 |
#endif |