2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Windows Enahanced Metafile Format %
19 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % http://www.imagemagick.org/script/license.php %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 * Include declarations.
40 #include "MagickCore/studio.h"
41 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
42 # if defined(__CYGWIN__)
49 #include "MagickCore/blob.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/pixel.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % IsEMF() returns MagickTrue if the image format type, identified by the
79 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
81 % The format of the ReadEMFImage method is:
83 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
85 % A description of each parameter follows:
87 % o magick: compare image format pattern against these bytes.
89 % o length: Specifies the length of the magick string.
92 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
96 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % IsWMF() returns MagickTrue if the image format type, identified by the
113 % magick string, is a Windows MetaFile (WMF) file.
115 % The format of the ReadEMFImage method is:
117 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
119 % A description of each parameter follows:
121 % o magick: compare image format pattern against these bytes.
123 % o length: Specifies the length of the magick string.
126 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
130 if (memcmp(magick,"\327\315\306\232",4) == 0)
132 if (memcmp(magick,"\001\000\011\000",4) == 0)
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % R e a d E M F I m a g e %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or
149 % Windows MetaFile (WMF) file using the Windows API and returns it. It
150 % allocates the memory necessary for the new Image structure and returns a
151 % pointer to the new image.
153 % The format of the ReadEMFImage method is:
155 % Image *ReadEMFImage(const ImageInfo *image_info,
156 % ExceptionInfo *exception)
158 % A description of each parameter follows:
160 % o image_info: the image info..
162 % o exception: return any errors or warnings in this structure.
167 #if defined(MAGICKCORE_HAVE__WFOPEN)
168 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
170 register const unsigned char
173 if (utf16 != (wchar_t *) NULL)
182 Convert UTF-8 to UTF-16.
185 for (p=utf8; *p != '\0'; p++)
187 if ((*p & 0x80) == 0)
190 if ((*p & 0xE0) == 0xC0)
195 if ((*p & 0xC0) != 0x80)
200 if ((*p & 0xF0) == 0xE0)
205 if ((*p & 0xC0) != 0x80)
210 if ((*p & 0xC0) != 0x80)
222 Compute UTF-16 string length.
224 for (p=utf8; *p != '\0'; p++)
226 if ((*p & 0x80) == 0)
229 if ((*p & 0xE0) == 0xC0)
232 if ((*p & 0xC0) != 0x80)
236 if ((*p & 0xF0) == 0xE0)
239 if ((*p & 0xC0) != 0x80)
242 if ((*p & 0xC0) != 0x80)
251 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
259 length=UTF8ToUTF16(source,(wchar_t *) NULL);
266 Not UTF-8, just copy.
268 length=strlen((char *) source);
269 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
270 if (utf16 == (wchar_t *) NULL)
271 return((wchar_t *) NULL);
272 for (i=0; i <= (ssize_t) length; i++)
276 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
277 if (utf16 == (wchar_t *) NULL)
278 return((wchar_t *) NULL);
279 length=UTF8ToUTF16(source,utf16);
285 This method reads either an enhanced metafile, a regular 16bit Windows
286 metafile, or an Aldus Placeable metafile and converts it into an enhanced
287 metafile. Width and height are returned in .01mm units.
289 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
290 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
293 #pragma pack( push, 2 )
302 } APMHEADER, *PAPMHEADER;
331 hTemp=GetEnhMetaFile(path);
332 #if defined(MAGICKCORE_HAVE__WFOPEN)
333 if (hTemp == (HENHMETAFILE) NULL)
338 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
339 if (unicode_path != (wchar_t *) NULL)
341 hTemp=GetEnhMetaFileW(unicode_path);
342 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
346 if (hTemp != (HENHMETAFILE) NULL)
351 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
352 *width=emfh.rclFrame.right-emfh.rclFrame.left;
353 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
356 hOld=GetMetaFile(path);
357 if (hOld != (HMETAFILE) NULL)
360 16bit windows metafile.
362 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
365 DeleteMetaFile(hOld);
366 return((HENHMETAFILE) NULL);
368 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
369 if (pBits == (LPBYTE) NULL)
371 DeleteMetaFile(hOld);
372 return((HENHMETAFILE) NULL);
374 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
376 pBits=(BYTE *) DestroyString((char *) pBits);
377 DeleteMetaFile(hOld);
378 return((HENHMETAFILE) NULL);
381 Make an enhanced metafile from the windows metafile.
383 mp.mm=MM_ANISOTROPIC;
388 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
390 DeleteMetaFile(hOld);
391 pBits=(BYTE *) DestroyString((char *) pBits);
392 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
393 *width=emfh.rclFrame.right-emfh.rclFrame.left;
394 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
398 Aldus Placeable metafile.
400 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
402 if (hFile == INVALID_HANDLE_VALUE)
404 dwSize=GetFileSize(hFile,NULL);
405 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
406 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
408 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
410 pBits=(BYTE *) DestroyString((char *) pBits);
411 return((HENHMETAFILE) NULL);
414 Make an enhanced metafile from the placable metafile.
416 mp.mm=MM_ANISOTROPIC;
417 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
419 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
420 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
422 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
425 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
427 pBits=(BYTE *) DestroyString((char *) pBits);
431 #define CENTIMETERS_INCH 2.54
433 static Image *ReadEMFImage(const ImageInfo *image_info,
434 ExceptionInfo *exception)
470 image=AcquireImage(image_info,exception);
471 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
472 if (hemf == (HENHMETAFILE) NULL)
473 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
474 if ((image->columns == 0) || (image->rows == 0))
480 y_resolution=DefaultResolution;
481 x_resolution=DefaultResolution;
482 if (image->resolution.y > 0)
484 y_resolution=image->resolution.y;
485 if (image->units == PixelsPerCentimeterResolution)
486 y_resolution*=CENTIMETERS_INCH;
488 if (image->resolution.x > 0)
490 x_resolution=image->resolution.x;
491 if (image->units == PixelsPerCentimeterResolution)
492 x_resolution*=CENTIMETERS_INCH;
494 image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5);
495 image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)*
498 if (image_info->size != (char *) NULL)
503 image->columns=width;
507 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
509 if (image_info->page != (char *) NULL)
523 geometry=GetPageGeometry(image_info->page);
524 p=strchr(geometry,'>');
525 if (p == (char *) NULL)
527 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
529 if (image->resolution.x != 0.0)
530 image->columns=(size_t) floor((image->columns*image->resolution.x)+
532 if (image->resolution.y != 0.0)
533 image->rows=(size_t) floor((image->rows*image->resolution.y)+0.5);
538 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
540 if (image->resolution.x != 0.0)
541 image->columns=(size_t) floor(((image->columns*image->resolution.x)/
542 DefaultResolution)+0.5);
543 if (image->resolution.y != 0.0)
544 image->rows=(size_t) floor(((image->rows*image->resolution.y)/
545 DefaultResolution)+0.5);
547 geometry=DestroyString(geometry);
550 if (hDC == (HDC) NULL)
552 DeleteEnhMetaFile(hemf);
553 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
556 Initialize the bitmap header info.
558 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
559 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
560 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
561 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
562 DIBinfo.bmiHeader.biPlanes=1;
563 DIBinfo.bmiHeader.biBitCount=32;
564 DIBinfo.bmiHeader.biCompression=BI_RGB;
565 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
568 if (hBitmap == (HBITMAP) NULL)
570 DeleteEnhMetaFile(hemf);
571 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
573 hDC=CreateCompatibleDC(NULL);
574 if (hDC == (HDC) NULL)
576 DeleteEnhMetaFile(hemf);
577 DeleteObject(hBitmap);
578 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
580 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
581 if (hOldBitmap == (HBITMAP) NULL)
583 DeleteEnhMetaFile(hemf);
585 DeleteObject(hBitmap);
586 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
589 Initialize the bitmap to the image background color.
592 for (y=0; y < (ssize_t) image->rows; y++)
594 for (x=0; x < (ssize_t) image->columns; x++)
596 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
597 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
598 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
604 rect.right=(LONG) image->columns;
605 rect.bottom=(LONG) image->rows;
607 Convert metafile pixels.
609 PlayEnhMetaFile(hDC,hemf,&rect);
611 for (y=0; y < (ssize_t) image->rows; y++)
613 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
614 if (q == (Quantum *) NULL)
616 for (x=0; x < (ssize_t) image->columns; x++)
618 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
619 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
620 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
621 SetPixelAlpha(image,OpaqueAlpha,q);
623 q+=GetPixelChannels(image);
625 if (SyncAuthenticPixels(image,exception) == MagickFalse)
628 DeleteEnhMetaFile(hemf);
629 SelectObject(hDC,hOldBitmap);
631 DeleteObject(hBitmap);
632 return(GetFirstImageInList(image));
634 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % R e g i s t e r E M F I m a g e %
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
647 % RegisterEMFImage() adds attributes for the EMF image format to
648 % the list of supported formats. The attributes include the image format
649 % tag, a method to read and/or write the format, whether the format
650 % supports the saving of more than one frame to the same file or blob,
651 % whether the format supports native in-memory I/O, and a brief
652 % description of the format.
654 % The format of the RegisterEMFImage method is:
656 % size_t RegisterEMFImage(void)
659 ModuleExport size_t RegisterEMFImage(void)
664 entry=SetMagickInfo("EMF");
665 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
666 entry->decoder=ReadEMFImage;
668 entry->description=ConstantString(
669 "Windows WIN32 API rendered Enhanced Meta File");
670 entry->magick=(IsImageFormatHandler *) IsEMF;
671 entry->blob_support=MagickFalse;
672 entry->module=ConstantString("WMF");
673 (void) RegisterMagickInfo(entry);
674 entry=SetMagickInfo("WMFWIN32");
675 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
676 entry->decoder=ReadEMFImage;
678 entry->description=ConstantString("Windows WIN32 API rendered Meta File");
679 entry->magick=(IsImageFormatHandler *) IsWMF;
680 entry->blob_support=MagickFalse;
681 entry->module=ConstantString("WMFWIN32");
682 (void) RegisterMagickInfo(entry);
683 return(MagickImageCoderSignature);
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691 % U n r e g i s t e r E M F I m a g e %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 % UnregisterEMFImage() removes format registrations made by the
698 % EMF module from the list of supported formats.
700 % The format of the UnregisterEMFImage method is:
702 % UnregisterEMFImage(void)
705 ModuleExport void UnregisterEMFImage(void)
707 (void) UnregisterMagickInfo("EMF");
708 (void) UnregisterMagickInfo("WMFWIN32");