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__)
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/geometry.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/pixel.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"
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % IsEMF() returns MagickTrue if the image format type, identified by the
81 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
83 % The format of the ReadEMFImage method is:
85 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
87 % A description of each parameter follows:
89 % o magick: compare image format pattern against these bytes.
91 % o length: Specifies the length of the magick string.
94 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
98 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % IsWMF() returns MagickTrue if the image format type, identified by the
115 % magick string, is a Windows MetaFile (WMF) file.
117 % The format of the ReadEMFImage method is:
119 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
121 % A description of each parameter follows:
123 % o magick: compare image format pattern against these bytes.
125 % o length: Specifies the length of the magick string.
128 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
132 if (memcmp(magick,"\327\315\306\232",4) == 0)
134 if (memcmp(magick,"\001\000\011\000",4) == 0)
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % R e a d E M F I m a g e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or
151 % Windows MetaFile (WMF) file using the Windows API and returns it. It
152 % allocates the memory necessary for the new Image structure and returns a
153 % pointer to the new image.
155 % The format of the ReadEMFImage method is:
157 % Image *ReadEMFImage(const ImageInfo *image_info,
158 % ExceptionInfo *exception)
160 % A description of each parameter follows:
162 % o image_info: the image info..
164 % o exception: return any errors or warnings in this structure.
169 #if defined(MAGICKCORE_HAVE__WFOPEN)
170 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
172 register const unsigned char
175 if (utf16 != (wchar_t *) NULL)
184 Convert UTF-8 to UTF-16.
187 for (p=utf8; *p != '\0'; p++)
189 if ((*p & 0x80) == 0)
192 if ((*p & 0xE0) == 0xC0)
197 if ((*p & 0xC0) != 0x80)
202 if ((*p & 0xF0) == 0xE0)
207 if ((*p & 0xC0) != 0x80)
212 if ((*p & 0xC0) != 0x80)
224 Compute UTF-16 string length.
226 for (p=utf8; *p != '\0'; p++)
228 if ((*p & 0x80) == 0)
231 if ((*p & 0xE0) == 0xC0)
234 if ((*p & 0xC0) != 0x80)
238 if ((*p & 0xF0) == 0xE0)
241 if ((*p & 0xC0) != 0x80)
244 if ((*p & 0xC0) != 0x80)
253 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
261 length=UTF8ToUTF16(source,(wchar_t *) NULL);
268 Not UTF-8, just copy.
270 length=strlen((char *) source);
271 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
272 if (utf16 == (wchar_t *) NULL)
273 return((wchar_t *) NULL);
274 for (i=0; i <= (ssize_t) length; i++)
278 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
279 if (utf16 == (wchar_t *) NULL)
280 return((wchar_t *) NULL);
281 length=UTF8ToUTF16(source,utf16);
287 This method reads either an enhanced metafile, a regular 16bit Windows
288 metafile, or an Aldus Placeable metafile and converts it into an enhanced
289 metafile. Width and height are returned in .01mm units.
291 #if defined(MAGICKCORE_EMF_DELEGATE)
292 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
295 #pragma pack( push, 2 )
304 } APMHEADER, *PAPMHEADER;
333 hTemp=GetEnhMetaFile(path);
334 #if defined(MAGICKCORE_HAVE__WFOPEN)
335 if (hTemp == (HENHMETAFILE) NULL)
340 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
341 if (unicode_path != (wchar_t *) NULL)
343 hTemp=GetEnhMetaFileW(unicode_path);
344 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
348 if (hTemp != (HENHMETAFILE) NULL)
353 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
354 *width=emfh.rclFrame.right-emfh.rclFrame.left;
355 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
358 hOld=GetMetaFile(path);
359 if (hOld != (HMETAFILE) NULL)
362 16bit windows metafile.
364 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
367 DeleteMetaFile(hOld);
368 return((HENHMETAFILE) NULL);
370 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
371 if (pBits == (LPBYTE) NULL)
373 DeleteMetaFile(hOld);
374 return((HENHMETAFILE) NULL);
376 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
378 pBits=(BYTE *) DestroyString((char *) pBits);
379 DeleteMetaFile(hOld);
380 return((HENHMETAFILE) NULL);
383 Make an enhanced metafile from the windows metafile.
385 mp.mm=MM_ANISOTROPIC;
390 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
392 DeleteMetaFile(hOld);
393 pBits=(BYTE *) DestroyString((char *) pBits);
394 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
395 *width=emfh.rclFrame.right-emfh.rclFrame.left;
396 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
400 Aldus Placeable metafile.
402 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
404 if (hFile == INVALID_HANDLE_VALUE)
406 dwSize=GetFileSize(hFile,NULL);
407 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
408 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
410 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
412 pBits=(BYTE *) DestroyString((char *) pBits);
413 return((HENHMETAFILE) NULL);
416 Make an enhanced metafile from the placable metafile.
418 mp.mm=MM_ANISOTROPIC;
419 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
421 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
422 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
424 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
427 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
429 pBits=(BYTE *) DestroyString((char *) pBits);
433 #define CENTIMETERS_INCH 2.54
435 static Image *ReadEMFImage(const ImageInfo *image_info,
436 ExceptionInfo *exception)
472 image=AcquireImage(image_info,exception);
473 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
474 if (hemf == (HENHMETAFILE) NULL)
475 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
476 if ((image->columns == 0) || (image->rows == 0))
482 y_resolution=DefaultResolution;
483 x_resolution=DefaultResolution;
484 if (image->resolution.y > 0)
486 y_resolution=image->resolution.y;
487 if (image->units == PixelsPerCentimeterResolution)
488 y_resolution*=CENTIMETERS_INCH;
490 if (image->resolution.x > 0)
492 x_resolution=image->resolution.x;
493 if (image->units == PixelsPerCentimeterResolution)
494 x_resolution*=CENTIMETERS_INCH;
496 image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5);
497 image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)*
500 if (image_info->size != (char *) NULL)
505 image->columns=width;
509 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
511 if (image_info->page != (char *) NULL)
525 geometry=GetPageGeometry(image_info->page);
526 p=strchr(geometry,'>');
527 if (p == (char *) NULL)
529 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
531 if (image->resolution.x != 0.0)
532 image->columns=(size_t) floor((image->columns*image->resolution.x)+
534 if (image->resolution.y != 0.0)
535 image->rows=(size_t) floor((image->rows*image->resolution.y)+0.5);
540 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
542 if (image->resolution.x != 0.0)
543 image->columns=(size_t) floor(((image->columns*image->resolution.x)/
544 DefaultResolution)+0.5);
545 if (image->resolution.y != 0.0)
546 image->rows=(size_t) floor(((image->rows*image->resolution.y)/
547 DefaultResolution)+0.5);
550 geometry=DestroyString(geometry);
553 if (hDC == (HDC) NULL)
555 DeleteEnhMetaFile(hemf);
556 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
559 Initialize the bitmap header info.
561 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
562 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
563 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
564 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
565 DIBinfo.bmiHeader.biPlanes=1;
566 DIBinfo.bmiHeader.biBitCount=32;
567 DIBinfo.bmiHeader.biCompression=BI_RGB;
568 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
571 if (hBitmap == (HBITMAP) NULL)
573 DeleteEnhMetaFile(hemf);
574 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
576 hDC=CreateCompatibleDC(NULL);
577 if (hDC == (HDC) NULL)
579 DeleteEnhMetaFile(hemf);
580 DeleteObject(hBitmap);
581 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
583 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
584 if (hOldBitmap == (HBITMAP) NULL)
586 DeleteEnhMetaFile(hemf);
588 DeleteObject(hBitmap);
589 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
592 Initialize the bitmap to the image background color.
595 for (y=0; y < (ssize_t) image->rows; y++)
597 for (x=0; x < (ssize_t) image->columns; x++)
599 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
600 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
601 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
607 rect.right=(LONG) image->columns;
608 rect.bottom=(LONG) image->rows;
610 Convert metafile pixels.
612 PlayEnhMetaFile(hDC,hemf,&rect);
614 for (y=0; y < (ssize_t) image->rows; y++)
616 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
617 if (q == (Quantum *) NULL)
619 for (x=0; x < (ssize_t) image->columns; x++)
621 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
622 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
623 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
624 SetPixelAlpha(image,OpaqueAlpha,q);
626 q+=GetPixelChannels(image);
628 if (SyncAuthenticPixels(image,exception) == MagickFalse)
631 DeleteEnhMetaFile(hemf);
632 SelectObject(hDC,hOldBitmap);
634 DeleteObject(hBitmap);
635 return(GetFirstImageInList(image));
637 #endif /* MAGICKCORE_EMF_DELEGATE */
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % R e g i s t e r E M F I m a g e %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650 % RegisterEMFImage() adds attributes for the EMF image format to
651 % the list of supported formats. The attributes include the image format
652 % tag, a method to read and/or write the format, whether the format
653 % supports the saving of more than one frame to the same file or blob,
654 % whether the format supports native in-memory I/O, and a brief
655 % description of the format.
657 % The format of the RegisterEMFImage method is:
659 % size_t RegisterEMFImage(void)
662 ModuleExport size_t RegisterEMFImage(void)
667 entry=SetMagickInfo("EMF");
668 #if defined(MAGICKCORE_EMF_DELEGATE)
669 entry->decoder=ReadEMFImage;
671 entry->description=ConstantString(
672 "Windows WIN32 API rendered Enhanced Meta File");
673 entry->magick=(IsImageFormatHandler *) IsEMF;
674 entry->blob_support=MagickFalse;
675 entry->module=ConstantString("WMF");
676 (void) RegisterMagickInfo(entry);
677 entry=SetMagickInfo("WMFWIN32");
678 #if defined(MAGICKCORE_EMF_DELEGATE)
679 entry->decoder=ReadEMFImage;
681 entry->description=ConstantString("Windows WIN32 API rendered Meta File");
682 entry->magick=(IsImageFormatHandler *) IsWMF;
683 entry->blob_support=MagickFalse;
684 entry->module=ConstantString("WMFWIN32");
685 (void) RegisterMagickInfo(entry);
686 return(MagickImageCoderSignature);
690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694 % U n r e g i s t e r E M F I m a g e %
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % UnregisterEMFImage() removes format registrations made by the
701 % EMF module from the list of supported formats.
703 % The format of the UnregisterEMFImage method is:
705 % UnregisterEMFImage(void)
708 ModuleExport void UnregisterEMFImage(void)
710 (void) UnregisterMagickInfo("EMF");
711 (void) UnregisterMagickInfo("WMFWIN32");