cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % EEEEE M M FFFFF % |
| 7 | % E MM MM F % |
| 8 | % EEE M M M FFF % |
| 9 | % E M M F % |
| 10 | % EEEEE M M F % |
| 11 | % % |
| 12 | % % |
| 13 | % Read Windows Enahanced Metafile Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Bill Radcliffe % |
| 17 | % 2001 % |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 18 | % Dirk Lemstra % |
| 19 | % January 2014 % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 20 | % % |
cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 21 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 22 | % dedicated to making software imaging solutions freely available. % |
| 23 | % % |
| 24 | % You may not use this file except in compliance with the License. You may % |
| 25 | % obtain a copy of the License at % |
| 26 | % % |
| 27 | % http://www.imagemagick.org/script/license.php % |
| 28 | % % |
| 29 | % Unless required by applicable law or agreed to in writing, software % |
| 30 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 31 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 32 | % See the License for the specific language governing permissions and % |
| 33 | % limitations under the License. % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * Include declarations. |
| 40 | */ |
| 41 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 43 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
dirk | bdad5f9 | 2014-01-23 21:37:16 +0000 | [diff] [blame] | 44 | # if !defined(_MSC_VER) |
| 45 | # if defined(__CYGWIN__) |
| 46 | # include <windows.h> |
| 47 | # else |
| 48 | # include <wingdi.h> |
| 49 | # endif |
| 50 | # else |
| 51 | # include <gdiplus.h> |
| 52 | # pragma comment(lib, "gdiplus.lib") |
| 53 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 54 | #endif |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 55 | #include "MagickCore/blob.h" |
| 56 | #include "MagickCore/blob-private.h" |
| 57 | #include "MagickCore/cache.h" |
| 58 | #include "MagickCore/exception.h" |
| 59 | #include "MagickCore/exception-private.h" |
| 60 | #include "MagickCore/geometry.h" |
| 61 | #include "MagickCore/image.h" |
| 62 | #include "MagickCore/image-private.h" |
| 63 | #include "MagickCore/list.h" |
| 64 | #include "MagickCore/magick.h" |
| 65 | #include "MagickCore/memory_.h" |
| 66 | #include "MagickCore/pixel.h" |
cristy | 6c9c9e8 | 2011-08-05 14:27:57 +0000 | [diff] [blame] | 67 | #include "MagickCore/pixel-accessor.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 68 | #include "MagickCore/quantum-private.h" |
| 69 | #include "MagickCore/static.h" |
| 70 | #include "MagickCore/string_.h" |
| 71 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | % % |
| 76 | % % |
| 77 | % % |
| 78 | % I s E F M % |
| 79 | % % |
| 80 | % % |
| 81 | % % |
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | % |
| 84 | % IsEMF() returns MagickTrue if the image format type, identified by the |
| 85 | % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file. |
| 86 | % |
| 87 | % The format of the ReadEMFImage method is: |
| 88 | % |
| 89 | % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length) |
| 90 | % |
| 91 | % A description of each parameter follows: |
| 92 | % |
| 93 | % o magick: compare image format pattern against these bytes. |
| 94 | % |
| 95 | % o length: Specifies the length of the magick string. |
| 96 | % |
| 97 | */ |
| 98 | static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length) |
| 99 | { |
| 100 | if (length < 48) |
| 101 | return(MagickFalse); |
| 102 | if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0) |
| 103 | return(MagickTrue); |
| 104 | return(MagickFalse); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 109 | % % |
| 110 | % % |
| 111 | % % |
| 112 | % I s W M F % |
| 113 | % % |
| 114 | % % |
| 115 | % % |
| 116 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 117 | % |
| 118 | % IsWMF() returns MagickTrue if the image format type, identified by the |
| 119 | % magick string, is a Windows MetaFile (WMF) file. |
| 120 | % |
| 121 | % The format of the ReadEMFImage method is: |
| 122 | % |
| 123 | % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length) |
| 124 | % |
| 125 | % A description of each parameter follows: |
| 126 | % |
| 127 | % o magick: compare image format pattern against these bytes. |
| 128 | % |
| 129 | % o length: Specifies the length of the magick string. |
| 130 | % |
| 131 | */ |
| 132 | static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length) |
| 133 | { |
| 134 | if (length < 4) |
| 135 | return(MagickFalse); |
| 136 | if (memcmp(magick,"\327\315\306\232",4) == 0) |
| 137 | return(MagickTrue); |
| 138 | if (memcmp(magick,"\001\000\011\000",4) == 0) |
| 139 | return(MagickTrue); |
| 140 | return(MagickFalse); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 145 | % % |
| 146 | % % |
| 147 | % % |
| 148 | % R e a d E M F I m a g e % |
| 149 | % % |
| 150 | % % |
| 151 | % % |
| 152 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 153 | % |
| 154 | % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or |
| 155 | % Windows MetaFile (WMF) file using the Windows API and returns it. It |
| 156 | % allocates the memory necessary for the new Image structure and returns a |
| 157 | % pointer to the new image. |
| 158 | % |
| 159 | % The format of the ReadEMFImage method is: |
| 160 | % |
| 161 | % Image *ReadEMFImage(const ImageInfo *image_info, |
| 162 | % ExceptionInfo *exception) |
| 163 | % |
| 164 | % A description of each parameter follows: |
| 165 | % |
| 166 | % o image_info: the image info.. |
| 167 | % |
| 168 | % o exception: return any errors or warnings in this structure. |
| 169 | % |
| 170 | */ |
| 171 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 172 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
dirk | bdad5f9 | 2014-01-23 21:37:16 +0000 | [diff] [blame] | 173 | # if !defined(_MSC_VER) |
| 174 | # if defined(MAGICKCORE_HAVE__WFOPEN) |
| 175 | static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16) |
| 176 | { |
| 177 | register const unsigned char |
| 178 | *p; |
| 179 | |
| 180 | if (utf16 != (wchar_t *) NULL) |
| 181 | { |
| 182 | register wchar_t |
| 183 | *q; |
| 184 | |
| 185 | wchar_t |
| 186 | c; |
| 187 | |
| 188 | /* |
| 189 | Convert UTF-8 to UTF-16. |
| 190 | */ |
| 191 | q=utf16; |
| 192 | for (p=utf8; *p != '\0'; p++) |
| 193 | { |
| 194 | if ((*p & 0x80) == 0) |
| 195 | *q=(*p); |
| 196 | else |
| 197 | if ((*p & 0xE0) == 0xC0) |
| 198 | { |
| 199 | c=(*p); |
| 200 | *q=(c & 0x1F) << 6; |
| 201 | p++; |
| 202 | if ((*p & 0xC0) != 0x80) |
| 203 | return(0); |
| 204 | *q|=(*p & 0x3F); |
| 205 | } |
| 206 | else |
| 207 | if ((*p & 0xF0) == 0xE0) |
| 208 | { |
| 209 | c=(*p); |
| 210 | *q=c << 12; |
| 211 | p++; |
| 212 | if ((*p & 0xC0) != 0x80) |
| 213 | return(0); |
| 214 | c=(*p); |
| 215 | *q|=(c & 0x3F) << 6; |
| 216 | p++; |
| 217 | if ((*p & 0xC0) != 0x80) |
| 218 | return(0); |
| 219 | *q|=(*p & 0x3F); |
| 220 | } |
| 221 | else |
| 222 | return(0); |
| 223 | q++; |
| 224 | } |
| 225 | *q++='\0'; |
| 226 | return(q-utf16); |
| 227 | } |
| 228 | /* |
| 229 | Compute UTF-16 string length. |
| 230 | */ |
| 231 | for (p=utf8; *p != '\0'; p++) |
| 232 | { |
| 233 | if ((*p & 0x80) == 0) |
| 234 | ; |
| 235 | else |
| 236 | if ((*p & 0xE0) == 0xC0) |
| 237 | { |
| 238 | p++; |
| 239 | if ((*p & 0xC0) != 0x80) |
| 240 | return(0); |
| 241 | } |
| 242 | else |
| 243 | if ((*p & 0xF0) == 0xE0) |
| 244 | { |
| 245 | p++; |
| 246 | if ((*p & 0xC0) != 0x80) |
| 247 | return(0); |
| 248 | p++; |
| 249 | if ((*p & 0xC0) != 0x80) |
| 250 | return(0); |
| 251 | } |
| 252 | else |
| 253 | return(0); |
| 254 | } |
| 255 | return(p-utf8); |
| 256 | } |
| 257 | |
| 258 | static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source) |
| 259 | { |
| 260 | size_t |
| 261 | length; |
| 262 | |
| 263 | wchar_t |
| 264 | *utf16; |
| 265 | |
| 266 | length=UTF8ToUTF16(source,(wchar_t *) NULL); |
| 267 | if (length == 0) |
| 268 | { |
| 269 | register ssize_t |
| 270 | i; |
| 271 | |
| 272 | /* |
| 273 | Not UTF-8, just copy. |
| 274 | */ |
| 275 | length=strlen((char *) source); |
| 276 | utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16)); |
| 277 | if (utf16 == (wchar_t *) NULL) |
| 278 | return((wchar_t *) NULL); |
| 279 | for (i=0; i <= (ssize_t) length; i++) |
| 280 | utf16[i]=source[i]; |
| 281 | return(utf16); |
| 282 | } |
| 283 | utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16)); |
| 284 | if (utf16 == (wchar_t *) NULL) |
| 285 | return((wchar_t *) NULL); |
| 286 | length=UTF8ToUTF16(source,utf16); |
| 287 | return(utf16); |
| 288 | } |
| 289 | # endif /* MAGICKCORE_HAVE__WFOPEN */ |
| 290 | |
| 291 | static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width, |
| 292 | ssize_t *height) |
| 293 | { |
| 294 | #pragma pack( push, 2 ) |
| 295 | typedef struct |
| 296 | { |
| 297 | DWORD dwKey; |
| 298 | WORD hmf; |
| 299 | SMALL_RECT bbox; |
| 300 | WORD wInch; |
| 301 | DWORD dwReserved; |
| 302 | WORD wCheckSum; |
| 303 | } APMHEADER, *PAPMHEADER; |
| 304 | #pragma pack( pop ) |
| 305 | |
| 306 | DWORD |
| 307 | dwSize; |
| 308 | |
| 309 | ENHMETAHEADER |
| 310 | emfh; |
| 311 | |
| 312 | HANDLE |
| 313 | hFile; |
| 314 | |
| 315 | HDC |
| 316 | hDC; |
| 317 | |
| 318 | HENHMETAFILE |
| 319 | hTemp; |
| 320 | |
| 321 | LPBYTE |
| 322 | pBits; |
| 323 | |
| 324 | METAFILEPICT |
| 325 | mp; |
| 326 | |
| 327 | HMETAFILE |
| 328 | hOld; |
| 329 | |
| 330 | *width=512; |
| 331 | *height=512; |
| 332 | hTemp=GetEnhMetaFile(path); |
| 333 | #if defined(MAGICKCORE_HAVE__WFOPEN) |
| 334 | if (hTemp == (HENHMETAFILE) NULL) |
| 335 | { |
| 336 | wchar_t |
| 337 | *unicode_path; |
| 338 | |
| 339 | unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path); |
| 340 | if (unicode_path != (wchar_t *) NULL) |
| 341 | { |
| 342 | hTemp=GetEnhMetaFileW(unicode_path); |
| 343 | unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path); |
| 344 | } |
| 345 | } |
| 346 | #endif |
| 347 | if (hTemp != (HENHMETAFILE) NULL) |
| 348 | { |
| 349 | /* |
| 350 | Enhanced metafile. |
| 351 | */ |
| 352 | GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh); |
| 353 | *width=emfh.rclFrame.right-emfh.rclFrame.left; |
| 354 | *height=emfh.rclFrame.bottom-emfh.rclFrame.top; |
| 355 | return(hTemp); |
| 356 | } |
| 357 | hOld=GetMetaFile(path); |
| 358 | if (hOld != (HMETAFILE) NULL) |
| 359 | { |
| 360 | /* |
| 361 | 16bit windows metafile. |
| 362 | */ |
| 363 | dwSize=GetMetaFileBitsEx(hOld,0,NULL); |
| 364 | if (dwSize == 0) |
| 365 | { |
| 366 | DeleteMetaFile(hOld); |
| 367 | return((HENHMETAFILE) NULL); |
| 368 | } |
| 369 | pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits)); |
| 370 | if (pBits == (LPBYTE) NULL) |
| 371 | { |
| 372 | DeleteMetaFile(hOld); |
| 373 | return((HENHMETAFILE) NULL); |
| 374 | } |
| 375 | if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0) |
| 376 | { |
| 377 | pBits=(BYTE *) DestroyString((char *) pBits); |
| 378 | DeleteMetaFile(hOld); |
| 379 | return((HENHMETAFILE) NULL); |
| 380 | } |
| 381 | /* |
| 382 | Make an enhanced metafile from the windows metafile. |
| 383 | */ |
| 384 | mp.mm=MM_ANISOTROPIC; |
| 385 | mp.xExt=1000; |
| 386 | mp.yExt=1000; |
| 387 | mp.hMF=NULL; |
| 388 | hDC=GetDC(NULL); |
| 389 | hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp); |
| 390 | ReleaseDC(NULL,hDC); |
| 391 | DeleteMetaFile(hOld); |
| 392 | pBits=(BYTE *) DestroyString((char *) pBits); |
| 393 | GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh); |
| 394 | *width=emfh.rclFrame.right-emfh.rclFrame.left; |
| 395 | *height=emfh.rclFrame.bottom-emfh.rclFrame.top; |
| 396 | return(hTemp); |
| 397 | } |
| 398 | /* |
| 399 | Aldus Placeable metafile. |
| 400 | */ |
| 401 | hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, |
| 402 | NULL); |
| 403 | if (hFile == INVALID_HANDLE_VALUE) |
| 404 | return(NULL); |
| 405 | dwSize=GetFileSize(hFile,NULL); |
| 406 | pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits)); |
| 407 | ReadFile(hFile,pBits,dwSize,&dwSize,NULL); |
| 408 | CloseHandle(hFile); |
| 409 | if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l) |
| 410 | { |
| 411 | pBits=(BYTE *) DestroyString((char *) pBits); |
| 412 | return((HENHMETAFILE) NULL); |
| 413 | } |
| 414 | /* |
| 415 | Make an enhanced metafile from the placable metafile. |
| 416 | */ |
| 417 | mp.mm=MM_ANISOTROPIC; |
| 418 | mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left; |
| 419 | *width=mp.xExt; |
| 420 | mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch); |
| 421 | mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top; |
| 422 | *height=mp.yExt; |
| 423 | mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch); |
| 424 | mp.hMF=NULL; |
| 425 | hDC=GetDC(NULL); |
| 426 | hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp); |
| 427 | ReleaseDC(NULL,hDC); |
| 428 | pBits=(BYTE *) DestroyString((char *) pBits); |
| 429 | return(hTemp); |
| 430 | } |
| 431 | |
| 432 | #define CENTIMETERS_INCH 2.54 |
| 433 | |
| 434 | static Image *ReadEMFImage(const ImageInfo *image_info, |
| 435 | ExceptionInfo *exception) |
| 436 | { |
| 437 | BITMAPINFO |
| 438 | DIBinfo; |
| 439 | |
| 440 | HBITMAP |
| 441 | hBitmap, |
| 442 | hOldBitmap; |
| 443 | |
| 444 | HDC |
| 445 | hDC; |
| 446 | |
| 447 | HENHMETAFILE |
| 448 | hemf; |
| 449 | |
| 450 | Image |
| 451 | *image; |
| 452 | |
| 453 | RECT |
| 454 | rect; |
| 455 | |
| 456 | register ssize_t |
| 457 | x; |
| 458 | |
| 459 | register Quantum |
| 460 | *q; |
| 461 | |
| 462 | RGBQUAD |
| 463 | *pBits, |
| 464 | *ppBits; |
| 465 | |
| 466 | ssize_t |
| 467 | height, |
| 468 | width, |
| 469 | y; |
| 470 | |
| 471 | image=AcquireImage(image_info,exception); |
| 472 | hemf=ReadEnhMetaFile(image_info->filename,&width,&height); |
| 473 | if (hemf == (HENHMETAFILE) NULL) |
| 474 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 475 | if ((image->columns == 0) || (image->rows == 0)) |
| 476 | { |
| 477 | double |
| 478 | y_resolution, |
| 479 | x_resolution; |
| 480 | |
| 481 | y_resolution=DefaultResolution; |
| 482 | x_resolution=DefaultResolution; |
| 483 | if (image->resolution.y > 0) |
| 484 | { |
| 485 | y_resolution=image->resolution.y; |
| 486 | if (image->units == PixelsPerCentimeterResolution) |
| 487 | y_resolution*=CENTIMETERS_INCH; |
| 488 | } |
| 489 | if (image->resolution.x > 0) |
| 490 | { |
| 491 | x_resolution=image->resolution.x; |
| 492 | if (image->units == PixelsPerCentimeterResolution) |
| 493 | x_resolution*=CENTIMETERS_INCH; |
| 494 | } |
| 495 | image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5); |
| 496 | image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)* |
| 497 | x_resolution+0.5); |
| 498 | } |
| 499 | if (image_info->size != (char *) NULL) |
| 500 | { |
| 501 | ssize_t |
| 502 | x; |
| 503 | |
| 504 | image->columns=width; |
| 505 | image->rows=height; |
| 506 | x=0; |
| 507 | y=0; |
| 508 | (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows); |
| 509 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame^] | 510 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 511 | if (status == MagickFalse) |
| 512 | return(DestroyImageList(image)); |
dirk | bdad5f9 | 2014-01-23 21:37:16 +0000 | [diff] [blame] | 513 | if (image_info->page != (char *) NULL) |
| 514 | { |
| 515 | char |
| 516 | *geometry; |
| 517 | |
| 518 | register char |
| 519 | *p; |
| 520 | |
| 521 | MagickStatusType |
| 522 | flags; |
| 523 | |
| 524 | ssize_t |
| 525 | sans; |
| 526 | |
| 527 | geometry=GetPageGeometry(image_info->page); |
| 528 | p=strchr(geometry,'>'); |
| 529 | if (p == (char *) NULL) |
| 530 | { |
| 531 | flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns, |
| 532 | &image->rows); |
| 533 | if (image->resolution.x != 0.0) |
| 534 | image->columns=(size_t) floor((image->columns*image->resolution.x)+ |
| 535 | 0.5); |
| 536 | if (image->resolution.y != 0.0) |
| 537 | image->rows=(size_t) floor((image->rows*image->resolution.y)+0.5); |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | *p='\0'; |
| 542 | flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns, |
| 543 | &image->rows); |
| 544 | if (image->resolution.x != 0.0) |
| 545 | image->columns=(size_t) floor(((image->columns*image->resolution.x)/ |
| 546 | DefaultResolution)+0.5); |
| 547 | if (image->resolution.y != 0.0) |
| 548 | image->rows=(size_t) floor(((image->rows*image->resolution.y)/ |
| 549 | DefaultResolution)+0.5); |
| 550 | } |
| 551 | (void) flags; |
| 552 | geometry=DestroyString(geometry); |
| 553 | } |
| 554 | hDC=GetDC(NULL); |
| 555 | if (hDC == (HDC) NULL) |
| 556 | { |
| 557 | DeleteEnhMetaFile(hemf); |
| 558 | ThrowReaderException(ResourceLimitError,"UnableToCreateADC"); |
| 559 | } |
| 560 | /* |
| 561 | Initialize the bitmap header info. |
| 562 | */ |
| 563 | (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO)); |
| 564 | DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); |
| 565 | DIBinfo.bmiHeader.biWidth=(LONG) image->columns; |
| 566 | DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows; |
| 567 | DIBinfo.bmiHeader.biPlanes=1; |
| 568 | DIBinfo.bmiHeader.biBitCount=32; |
| 569 | DIBinfo.bmiHeader.biCompression=BI_RGB; |
| 570 | hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL, |
| 571 | 0); |
| 572 | ReleaseDC(NULL,hDC); |
| 573 | if (hBitmap == (HBITMAP) NULL) |
| 574 | { |
| 575 | DeleteEnhMetaFile(hemf); |
| 576 | ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap"); |
| 577 | } |
| 578 | hDC=CreateCompatibleDC(NULL); |
| 579 | if (hDC == (HDC) NULL) |
| 580 | { |
| 581 | DeleteEnhMetaFile(hemf); |
| 582 | DeleteObject(hBitmap); |
| 583 | ThrowReaderException(ResourceLimitError,"UnableToCreateADC"); |
| 584 | } |
| 585 | hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap); |
| 586 | if (hOldBitmap == (HBITMAP) NULL) |
| 587 | { |
| 588 | DeleteEnhMetaFile(hemf); |
| 589 | DeleteDC(hDC); |
| 590 | DeleteObject(hBitmap); |
| 591 | ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap"); |
| 592 | } |
| 593 | /* |
| 594 | Initialize the bitmap to the image background color. |
| 595 | */ |
| 596 | pBits=ppBits; |
| 597 | for (y=0; y < (ssize_t) image->rows; y++) |
| 598 | { |
| 599 | for (x=0; x < (ssize_t) image->columns; x++) |
| 600 | { |
| 601 | pBits->rgbRed=ScaleQuantumToChar(image->background_color.red); |
| 602 | pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green); |
| 603 | pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue); |
| 604 | pBits++; |
| 605 | } |
| 606 | } |
| 607 | rect.top=0; |
| 608 | rect.left=0; |
| 609 | rect.right=(LONG) image->columns; |
| 610 | rect.bottom=(LONG) image->rows; |
| 611 | /* |
| 612 | Convert metafile pixels. |
| 613 | */ |
| 614 | PlayEnhMetaFile(hDC,hemf,&rect); |
| 615 | pBits=ppBits; |
| 616 | for (y=0; y < (ssize_t) image->rows; y++) |
| 617 | { |
| 618 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 619 | if (q == (Quantum *) NULL) |
| 620 | break; |
| 621 | for (x=0; x < (ssize_t) image->columns; x++) |
| 622 | { |
| 623 | SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q); |
| 624 | SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q); |
| 625 | SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q); |
| 626 | SetPixelAlpha(image,OpaqueAlpha,q); |
| 627 | pBits++; |
| 628 | q+=GetPixelChannels(image); |
| 629 | } |
| 630 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 631 | break; |
| 632 | } |
| 633 | DeleteEnhMetaFile(hemf); |
| 634 | SelectObject(hDC,hOldBitmap); |
| 635 | DeleteDC(hDC); |
| 636 | DeleteObject(hBitmap); |
| 637 | return(GetFirstImageInList(image)); |
| 638 | } |
| 639 | # else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 640 | static Image *ReadEMFImage(const ImageInfo *image_info, |
| 641 | ExceptionInfo *exception) |
| 642 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 643 | Gdiplus::Bitmap |
| 644 | *bitmap; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 645 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 646 | Gdiplus::BitmapData |
| 647 | bitmap_data; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 649 | Gdiplus::GdiplusStartupInput |
| 650 | startup_input; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 651 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 652 | Gdiplus::Graphics |
| 653 | *graphics; |
| 654 | |
| 655 | Gdiplus::Image |
| 656 | *source; |
| 657 | |
| 658 | Gdiplus::Rect |
| 659 | rect; |
| 660 | |
| 661 | GeometryInfo |
| 662 | geometry_info; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 663 | |
| 664 | Image |
| 665 | *image; |
| 666 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 667 | MagickStatusType |
| 668 | flags; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 669 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 670 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 671 | *q; |
| 672 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 673 | register ssize_t |
| 674 | x; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 675 | |
cristy | 202de44 | 2011-04-24 18:19:07 +0000 | [diff] [blame] | 676 | ssize_t |
cristy | 202de44 | 2011-04-24 18:19:07 +0000 | [diff] [blame] | 677 | y; |
| 678 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 679 | ULONG_PTR |
| 680 | token; |
| 681 | |
| 682 | unsigned char |
| 683 | *p; |
| 684 | |
| 685 | wchar_t |
| 686 | fileName[MaxTextExtent]; |
| 687 | |
| 688 | assert(image_info != (const ImageInfo *) NULL); |
| 689 | assert(image_info->signature == MagickSignature); |
| 690 | if (image_info->debug != MagickFalse) |
| 691 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 692 | image_info->filename); |
| 693 | assert(exception != (ExceptionInfo *) NULL); |
| 694 | |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 695 | image=AcquireImage(image_info,exception); |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 696 | if (Gdiplus::GdiplusStartup(&token,&startup_input,NULL) != |
| 697 | Gdiplus::Status::Ok) |
| 698 | ThrowReaderException(CoderError, "GdiplusStartupFailed"); |
| 699 | MultiByteToWideChar(CP_UTF8,0,image->filename,-1,fileName,MaxTextExtent); |
| 700 | source=Gdiplus::Image::FromFile(fileName); |
dirk | a71de00 | 2014-01-07 19:37:54 +0000 | [diff] [blame] | 701 | if (source == (Gdiplus::Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 702 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 703 | Gdiplus::GdiplusShutdown(token); |
| 704 | ThrowReaderException(FileOpenError,"UnableToOpenFile"); |
| 705 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 706 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 707 | image->resolution.x=source->GetHorizontalResolution(); |
| 708 | image->resolution.y=source->GetVerticalResolution(); |
| 709 | image->columns=(size_t) source->GetWidth(); |
| 710 | image->rows=(size_t) source->GetHeight(); |
| 711 | if (image_info->density != (char *) NULL) |
| 712 | { |
| 713 | flags=ParseGeometry(image_info->density,&geometry_info); |
| 714 | image->resolution.x=geometry_info.rho; |
| 715 | image->resolution.y=geometry_info.sigma; |
| 716 | if ((flags & SigmaValue) == 0) |
| 717 | image->resolution.y=image->resolution.x; |
| 718 | if ((image->resolution.x > 0.0) && (image->resolution.y > 0.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 719 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 720 | image->columns=(size_t) floor((Gdiplus::REAL) source->GetWidth() / |
| 721 | source->GetHorizontalResolution() * image->resolution.x + 0.5); |
| 722 | image->rows=(size_t)floor((Gdiplus::REAL) source->GetHeight() / |
| 723 | source->GetVerticalResolution() * image->resolution.y + 0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 724 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 725 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 726 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 727 | bitmap=new Gdiplus::Bitmap((INT) image->columns,(INT) image->rows, |
| 728 | PixelFormat32bppARGB); |
| 729 | graphics=Gdiplus::Graphics::FromImage(bitmap); |
| 730 | graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic); |
| 731 | graphics->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality); |
| 732 | graphics->SetTextRenderingHint(Gdiplus::TextRenderingHintClearTypeGridFit); |
| 733 | graphics->Clear(Gdiplus::Color((BYTE) ScaleQuantumToChar( |
| 734 | image->background_color.alpha),(BYTE) ScaleQuantumToChar( |
| 735 | image->background_color.red),(BYTE) ScaleQuantumToChar( |
| 736 | image->background_color.green),(BYTE) ScaleQuantumToChar( |
| 737 | image->background_color.blue))); |
| 738 | graphics->DrawImage(source,0,0,(INT) image->columns,(INT) image->rows); |
| 739 | delete graphics; |
| 740 | delete source; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 742 | rect=Gdiplus::Rect(0,0,(INT) image->columns,(INT) image->rows); |
| 743 | if (bitmap->LockBits(&rect,Gdiplus::ImageLockModeRead,PixelFormat32bppARGB, |
| 744 | &bitmap_data) != Gdiplus::Ok) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 745 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 746 | delete bitmap; |
| 747 | Gdiplus::GdiplusShutdown(token); |
| 748 | ThrowReaderException(FileOpenError,"UnableToReadImageData"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | } |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 750 | |
| 751 | image->alpha_trait=BlendPixelTrait; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 752 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 753 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 754 | p=(unsigned char *) bitmap_data.Scan0+(y*abs(bitmap_data.Stride)); |
| 755 | if (bitmap_data.Stride < 0) |
| 756 | q=GetAuthenticPixels(image,0,image->rows-y-1,image->columns,1,exception); |
| 757 | else |
| 758 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 759 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 760 | break; |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 761 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 762 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | { |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 764 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 765 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 766 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| 767 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 768 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | } |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 770 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 771 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 772 | break; |
| 773 | } |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 774 | |
| 775 | bitmap->UnlockBits(&bitmap_data); |
| 776 | delete bitmap; |
| 777 | Gdiplus::GdiplusShutdown(token); |
| 778 | return(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 779 | } |
dirk | bdad5f9 | 2014-01-23 21:37:16 +0000 | [diff] [blame] | 780 | # endif /* _MSC_VER */ |
cristy | 2dfb1df | 2012-02-25 14:16:09 +0000 | [diff] [blame] | 781 | #endif /* MAGICKCORE_EMF_DELEGATE */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 782 | |
| 783 | /* |
| 784 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 785 | % % |
| 786 | % % |
| 787 | % % |
| 788 | % R e g i s t e r E M F I m a g e % |
| 789 | % % |
| 790 | % % |
| 791 | % % |
| 792 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 793 | % |
| 794 | % RegisterEMFImage() adds attributes for the EMF image format to |
| 795 | % the list of supported formats. The attributes include the image format |
| 796 | % tag, a method to read and/or write the format, whether the format |
| 797 | % supports the saving of more than one frame to the same file or blob, |
| 798 | % whether the format supports native in-memory I/O, and a brief |
| 799 | % description of the format. |
| 800 | % |
| 801 | % The format of the RegisterEMFImage method is: |
| 802 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 803 | % size_t RegisterEMFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 804 | % |
| 805 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 806 | ModuleExport size_t RegisterEMFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 807 | { |
| 808 | MagickInfo |
| 809 | *entry; |
| 810 | |
| 811 | entry=SetMagickInfo("EMF"); |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 812 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | entry->decoder=ReadEMFImage; |
| 814 | #endif |
| 815 | entry->description=ConstantString( |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 816 | "Windows Enhanced Meta File"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 817 | entry->magick=(IsImageFormatHandler *) IsEMF; |
| 818 | entry->blob_support=MagickFalse; |
| 819 | entry->module=ConstantString("WMF"); |
| 820 | (void) RegisterMagickInfo(entry); |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 821 | entry=SetMagickInfo("WMF"); |
| 822 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 823 | entry->decoder=ReadEMFImage; |
| 824 | #endif |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 825 | entry->description=ConstantString("Windows Meta File"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 826 | entry->magick=(IsImageFormatHandler *) IsWMF; |
| 827 | entry->blob_support=MagickFalse; |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 828 | entry->module=ConstantString("WMF"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 829 | (void) RegisterMagickInfo(entry); |
| 830 | return(MagickImageCoderSignature); |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 835 | % % |
| 836 | % % |
| 837 | % % |
| 838 | % U n r e g i s t e r E M F I m a g e % |
| 839 | % % |
| 840 | % % |
| 841 | % % |
| 842 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 843 | % |
| 844 | % UnregisterEMFImage() removes format registrations made by the |
| 845 | % EMF module from the list of supported formats. |
| 846 | % |
| 847 | % The format of the UnregisterEMFImage method is: |
| 848 | % |
| 849 | % UnregisterEMFImage(void) |
| 850 | % |
| 851 | */ |
| 852 | ModuleExport void UnregisterEMFImage(void) |
| 853 | { |
| 854 | (void) UnregisterMagickInfo("EMF"); |
dirk | 073ff37 | 2014-01-05 14:05:25 +0000 | [diff] [blame] | 855 | (void) UnregisterMagickInfo("WMF"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | } |