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) && defined(__CYGWIN__)
42 # define MAGICKCORE_EMF_DELEGATE
44 #if defined(MAGICKCORE_EMF_DELEGATE)
45 # if defined(__CYGWIN__)
48 # include "MagickCore/nt-base-private.h"
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/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/pixel.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsEMF() returns MagickTrue if the image format type, identified by the
82 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
84 % The format of the ReadEMFImage method is:
86 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
88 % A description of each parameter follows:
90 % o magick: compare image format pattern against these bytes.
92 % o length: Specifies the length of the magick string.
95 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
99 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % IsWMF() returns MagickTrue if the image format type, identified by the
116 % magick string, is a Windows MetaFile (WMF) file.
118 % The format of the ReadEMFImage method is:
120 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
122 % A description of each parameter follows:
124 % o magick: compare image format pattern against these bytes.
126 % o length: Specifies the length of the magick string.
129 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
133 if (memcmp(magick,"\327\315\306\232",4) == 0)
135 if (memcmp(magick,"\001\000\011\000",4) == 0)
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % R e a d E M F I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or
152 % Windows MetaFile (WMF) file using the Windows API and returns it. It
153 % allocates the memory necessary for the new Image structure and returns a
154 % pointer to the new image.
156 % The format of the ReadEMFImage method is:
158 % Image *ReadEMFImage(const ImageInfo *image_info,
159 % ExceptionInfo *exception)
161 % A description of each parameter follows:
163 % o image_info: the image info..
165 % o exception: return any errors or warnings in this structure.
170 #if defined(MAGICKCORE_HAVE__WFOPEN)
171 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
173 register const unsigned char
176 if (utf16 != (wchar_t *) NULL)
185 Convert UTF-8 to UTF-16.
188 for (p=utf8; *p != '\0'; p++)
190 if ((*p & 0x80) == 0)
193 if ((*p & 0xE0) == 0xC0)
198 if ((*p & 0xC0) != 0x80)
203 if ((*p & 0xF0) == 0xE0)
208 if ((*p & 0xC0) != 0x80)
213 if ((*p & 0xC0) != 0x80)
225 Compute UTF-16 string length.
227 for (p=utf8; *p != '\0'; p++)
229 if ((*p & 0x80) == 0)
232 if ((*p & 0xE0) == 0xC0)
235 if ((*p & 0xC0) != 0x80)
239 if ((*p & 0xF0) == 0xE0)
242 if ((*p & 0xC0) != 0x80)
245 if ((*p & 0xC0) != 0x80)
254 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
262 length=UTF8ToUTF16(source,(wchar_t *) NULL);
269 Not UTF-8, just copy.
271 length=strlen((char *) source);
272 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
273 if (utf16 == (wchar_t *) NULL)
274 return((wchar_t *) NULL);
275 for (i=0; i <= (ssize_t) length; i++)
279 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
280 if (utf16 == (wchar_t *) NULL)
281 return((wchar_t *) NULL);
282 length=UTF8ToUTF16(source,utf16);
288 This method reads either an enhanced metafile, a regular 16bit Windows
289 metafile, or an Aldus Placeable metafile and converts it into an enhanced
290 metafile. Width and height are returned in .01mm units.
292 #if defined(MAGICKCORE_EMF_DELEGATE)
293 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
296 #pragma pack( push, 2 )
305 } APMHEADER, *PAPMHEADER;
334 hTemp=GetEnhMetaFile(path);
335 #if defined(MAGICKCORE_HAVE__WFOPEN)
336 if (hTemp == (HENHMETAFILE) NULL)
341 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
342 if (unicode_path != (wchar_t *) NULL)
344 hTemp=GetEnhMetaFileW(unicode_path);
345 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
349 if (hTemp != (HENHMETAFILE) NULL)
354 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
355 *width=emfh.rclFrame.right-emfh.rclFrame.left;
356 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
359 hOld=GetMetaFile(path);
360 if (hOld != (HMETAFILE) NULL)
363 16bit windows metafile.
365 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
368 DeleteMetaFile(hOld);
369 return((HENHMETAFILE) NULL);
371 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
372 if (pBits == (LPBYTE) NULL)
374 DeleteMetaFile(hOld);
375 return((HENHMETAFILE) NULL);
377 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
379 pBits=(BYTE *) DestroyString((char *) pBits);
380 DeleteMetaFile(hOld);
381 return((HENHMETAFILE) NULL);
384 Make an enhanced metafile from the windows metafile.
386 mp.mm=MM_ANISOTROPIC;
391 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
393 DeleteMetaFile(hOld);
394 pBits=(BYTE *) DestroyString((char *) pBits);
395 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
396 *width=emfh.rclFrame.right-emfh.rclFrame.left;
397 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
401 Aldus Placeable metafile.
403 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
405 if (hFile == INVALID_HANDLE_VALUE)
407 dwSize=GetFileSize(hFile,NULL);
408 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
409 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
411 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
413 pBits=(BYTE *) DestroyString((char *) pBits);
414 return((HENHMETAFILE) NULL);
417 Make an enhanced metafile from the placable metafile.
419 mp.mm=MM_ANISOTROPIC;
420 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
422 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
423 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
425 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
428 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
430 pBits=(BYTE *) DestroyString((char *) pBits);
434 #define CENTIMETERS_INCH 2.54
436 static Image *ReadEMFImage(const ImageInfo *image_info,
437 ExceptionInfo *exception)
473 image=AcquireImage(image_info,exception);
474 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
475 if (hemf == (HENHMETAFILE) NULL)
476 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
477 if ((image->columns == 0) || (image->rows == 0))
483 y_resolution=DefaultResolution;
484 x_resolution=DefaultResolution;
485 if (image->resolution.y > 0)
487 y_resolution=image->resolution.y;
488 if (image->units == PixelsPerCentimeterResolution)
489 y_resolution*=CENTIMETERS_INCH;
491 if (image->resolution.x > 0)
493 x_resolution=image->resolution.x;
494 if (image->units == PixelsPerCentimeterResolution)
495 x_resolution*=CENTIMETERS_INCH;
497 image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5);
498 image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)*
501 if (image_info->size != (char *) NULL)
506 image->columns=width;
510 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
512 if (image_info->page != (char *) NULL)
526 geometry=GetPageGeometry(image_info->page);
527 p=strchr(geometry,'>');
528 if (p == (char *) NULL)
530 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
532 if (image->resolution.x != 0.0)
533 image->columns=(size_t) floor((image->columns*image->resolution.x)+
535 if (image->resolution.y != 0.0)
536 image->rows=(size_t) floor((image->rows*image->resolution.y)+0.5);
541 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
543 if (image->resolution.x != 0.0)
544 image->columns=(size_t) floor(((image->columns*image->resolution.x)/
545 DefaultResolution)+0.5);
546 if (image->resolution.y != 0.0)
547 image->rows=(size_t) floor(((image->rows*image->resolution.y)/
548 DefaultResolution)+0.5);
551 geometry=DestroyString(geometry);
554 if (hDC == (HDC) NULL)
556 DeleteEnhMetaFile(hemf);
557 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
560 Initialize the bitmap header info.
562 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
563 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
564 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
565 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
566 DIBinfo.bmiHeader.biPlanes=1;
567 DIBinfo.bmiHeader.biBitCount=32;
568 DIBinfo.bmiHeader.biCompression=BI_RGB;
569 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
572 if (hBitmap == (HBITMAP) NULL)
574 DeleteEnhMetaFile(hemf);
575 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
577 hDC=CreateCompatibleDC(NULL);
578 if (hDC == (HDC) NULL)
580 DeleteEnhMetaFile(hemf);
581 DeleteObject(hBitmap);
582 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
584 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
585 if (hOldBitmap == (HBITMAP) NULL)
587 DeleteEnhMetaFile(hemf);
589 DeleteObject(hBitmap);
590 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
593 Initialize the bitmap to the image background color.
596 for (y=0; y < (ssize_t) image->rows; y++)
598 for (x=0; x < (ssize_t) image->columns; x++)
600 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
601 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
602 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
608 rect.right=(LONG) image->columns;
609 rect.bottom=(LONG) image->rows;
611 Convert metafile pixels.
613 PlayEnhMetaFile(hDC,hemf,&rect);
615 for (y=0; y < (ssize_t) image->rows; y++)
617 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
618 if (q == (Quantum *) NULL)
620 for (x=0; x < (ssize_t) image->columns; x++)
622 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
623 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
624 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
625 SetPixelAlpha(image,OpaqueAlpha,q);
627 q+=GetPixelChannels(image);
629 if (SyncAuthenticPixels(image,exception) == MagickFalse)
632 DeleteEnhMetaFile(hemf);
633 SelectObject(hDC,hOldBitmap);
635 DeleteObject(hBitmap);
636 return(GetFirstImageInList(image));
638 #endif /* MAGICKCORE_EMF_DELEGATE */
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % R e g i s t e r E M F I m a g e %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % RegisterEMFImage() adds attributes for the EMF image format to
652 % the list of supported formats. The attributes include the image format
653 % tag, a method to read and/or write the format, whether the format
654 % supports the saving of more than one frame to the same file or blob,
655 % whether the format supports native in-memory I/O, and a brief
656 % description of the format.
658 % The format of the RegisterEMFImage method is:
660 % size_t RegisterEMFImage(void)
663 ModuleExport size_t RegisterEMFImage(void)
668 entry=SetMagickInfo("EMF");
669 #if defined(MAGICKCORE_EMF_DELEGATE)
670 entry->decoder=ReadEMFImage;
672 entry->description=ConstantString(
673 "Windows WIN32 API rendered Enhanced Meta File");
674 entry->magick=(IsImageFormatHandler *) IsEMF;
675 entry->blob_support=MagickFalse;
676 entry->module=ConstantString("WMF");
677 (void) RegisterMagickInfo(entry);
678 entry=SetMagickInfo("WMFWIN32");
679 #if defined(MAGICKCORE_EMF_DELEGATE)
680 entry->decoder=ReadEMFImage;
682 entry->description=ConstantString("Windows WIN32 API rendered Meta File");
683 entry->magick=(IsImageFormatHandler *) IsWMF;
684 entry->blob_support=MagickFalse;
685 entry->module=ConstantString("WMFWIN32");
686 (void) RegisterMagickInfo(entry);
687 return(MagickImageCoderSignature);
691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695 % U n r e g i s t e r E M F I m a g e %
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701 % UnregisterEMFImage() removes format registrations made by the
702 % EMF module from the list of supported formats.
704 % The format of the UnregisterEMFImage method is:
706 % UnregisterEMFImage(void)
709 ModuleExport void UnregisterEMFImage(void)
711 (void) UnregisterMagickInfo("EMF");
712 (void) UnregisterMagickInfo("WMFWIN32");