2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Windows Enahanced Metafile Format %
21 % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 * Include declarations.
42 #include "MagickCore/studio.h"
43 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
44 # if !defined(_MSC_VER)
45 # if defined(__CYGWIN__)
52 # pragma comment(lib, "gdiplus.lib")
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"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/module.h"
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % IsEMF() returns MagickTrue if the image format type, identified by the
85 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
87 % The format of the ReadEMFImage method is:
89 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
91 % A description of each parameter follows:
93 % o magick: compare image format pattern against these bytes.
95 % o length: Specifies the length of the magick string.
98 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
102 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % IsWMF() returns MagickTrue if the image format type, identified by the
119 % magick string, is a Windows MetaFile (WMF) file.
121 % The format of the ReadEMFImage method is:
123 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
125 % A description of each parameter follows:
127 % o magick: compare image format pattern against these bytes.
129 % o length: Specifies the length of the magick string.
132 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
136 if (memcmp(magick,"\327\315\306\232",4) == 0)
138 if (memcmp(magick,"\001\000\011\000",4) == 0)
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % R e a d E M F I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
159 % The format of the ReadEMFImage method is:
161 % Image *ReadEMFImage(const ImageInfo *image_info,
162 % ExceptionInfo *exception)
164 % A description of each parameter follows:
166 % o image_info: the image info..
168 % o exception: return any errors or warnings in this structure.
172 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
173 # if !defined(_MSC_VER)
174 # if defined(MAGICKCORE_HAVE__WFOPEN)
175 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
177 register const unsigned char
180 if (utf16 != (wchar_t *) NULL)
189 Convert UTF-8 to UTF-16.
192 for (p=utf8; *p != '\0'; p++)
194 if ((*p & 0x80) == 0)
197 if ((*p & 0xE0) == 0xC0)
202 if ((*p & 0xC0) != 0x80)
207 if ((*p & 0xF0) == 0xE0)
212 if ((*p & 0xC0) != 0x80)
217 if ((*p & 0xC0) != 0x80)
229 Compute UTF-16 string length.
231 for (p=utf8; *p != '\0'; p++)
233 if ((*p & 0x80) == 0)
236 if ((*p & 0xE0) == 0xC0)
239 if ((*p & 0xC0) != 0x80)
243 if ((*p & 0xF0) == 0xE0)
246 if ((*p & 0xC0) != 0x80)
249 if ((*p & 0xC0) != 0x80)
258 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
266 length=UTF8ToUTF16(source,(wchar_t *) NULL);
273 Not UTF-8, just copy.
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++)
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);
289 # endif /* MAGICKCORE_HAVE__WFOPEN */
291 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
294 #pragma pack( push, 2 )
303 } APMHEADER, *PAPMHEADER;
332 hTemp=GetEnhMetaFile(path);
333 #if defined(MAGICKCORE_HAVE__WFOPEN)
334 if (hTemp == (HENHMETAFILE) NULL)
339 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
340 if (unicode_path != (wchar_t *) NULL)
342 hTemp=GetEnhMetaFileW(unicode_path);
343 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
347 if (hTemp != (HENHMETAFILE) NULL)
352 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
353 *width=emfh.rclFrame.right-emfh.rclFrame.left;
354 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
357 hOld=GetMetaFile(path);
358 if (hOld != (HMETAFILE) NULL)
361 16bit windows metafile.
363 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
366 DeleteMetaFile(hOld);
367 return((HENHMETAFILE) NULL);
369 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
370 if (pBits == (LPBYTE) NULL)
372 DeleteMetaFile(hOld);
373 return((HENHMETAFILE) NULL);
375 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
377 pBits=(BYTE *) DestroyString((char *) pBits);
378 DeleteMetaFile(hOld);
379 return((HENHMETAFILE) NULL);
382 Make an enhanced metafile from the windows metafile.
384 mp.mm=MM_ANISOTROPIC;
389 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
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;
399 Aldus Placeable metafile.
401 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
403 if (hFile == INVALID_HANDLE_VALUE)
405 dwSize=GetFileSize(hFile,NULL);
406 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
407 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
409 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
411 pBits=(BYTE *) DestroyString((char *) pBits);
412 return((HENHMETAFILE) NULL);
415 Make an enhanced metafile from the placable metafile.
417 mp.mm=MM_ANISOTROPIC;
418 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
420 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
421 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
423 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
426 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
428 pBits=(BYTE *) DestroyString((char *) pBits);
432 #define CENTIMETERS_INCH 2.54
434 static Image *ReadEMFImage(const ImageInfo *image_info,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 status=SetImageExtent(image,image->columns,image->rows,exception);
513 if (status == MagickFalse)
514 return(DestroyImageList(image));
515 if (image_info->page != (char *) NULL)
529 geometry=GetPageGeometry(image_info->page);
530 p=strchr(geometry,'>');
531 if (p == (char *) NULL)
533 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
535 if (image->resolution.x != 0.0)
536 image->columns=(size_t) floor((image->columns*image->resolution.x)+
538 if (image->resolution.y != 0.0)
539 image->rows=(size_t) floor((image->rows*image->resolution.y)+0.5);
544 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
546 if (image->resolution.x != 0.0)
547 image->columns=(size_t) floor(((image->columns*image->resolution.x)/
548 DefaultResolution)+0.5);
549 if (image->resolution.y != 0.0)
550 image->rows=(size_t) floor(((image->rows*image->resolution.y)/
551 DefaultResolution)+0.5);
554 geometry=DestroyString(geometry);
557 if (hDC == (HDC) NULL)
559 DeleteEnhMetaFile(hemf);
560 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
563 Initialize the bitmap header info.
565 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
566 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
567 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
568 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
569 DIBinfo.bmiHeader.biPlanes=1;
570 DIBinfo.bmiHeader.biBitCount=32;
571 DIBinfo.bmiHeader.biCompression=BI_RGB;
572 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
575 if (hBitmap == (HBITMAP) NULL)
577 DeleteEnhMetaFile(hemf);
578 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
580 hDC=CreateCompatibleDC(NULL);
581 if (hDC == (HDC) NULL)
583 DeleteEnhMetaFile(hemf);
584 DeleteObject(hBitmap);
585 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
587 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
588 if (hOldBitmap == (HBITMAP) NULL)
590 DeleteEnhMetaFile(hemf);
592 DeleteObject(hBitmap);
593 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
596 Initialize the bitmap to the image background color.
599 for (y=0; y < (ssize_t) image->rows; y++)
601 for (x=0; x < (ssize_t) image->columns; x++)
603 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
604 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
605 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
611 rect.right=(LONG) image->columns;
612 rect.bottom=(LONG) image->rows;
614 Convert metafile pixels.
616 PlayEnhMetaFile(hDC,hemf,&rect);
618 for (y=0; y < (ssize_t) image->rows; y++)
620 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
621 if (q == (Quantum *) NULL)
623 for (x=0; x < (ssize_t) image->columns; x++)
625 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
626 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
627 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
628 SetPixelAlpha(image,OpaqueAlpha,q);
630 q+=GetPixelChannels(image);
632 if (SyncAuthenticPixels(image,exception) == MagickFalse)
635 DeleteEnhMetaFile(hemf);
636 SelectObject(hDC,hOldBitmap);
638 DeleteObject(hBitmap);
639 return(GetFirstImageInList(image));
642 static Image *ReadEMFImage(const ImageInfo *image_info,
643 ExceptionInfo *exception)
651 Gdiplus::GdiplusStartupInput
688 fileName[MagickPathExtent];
690 assert(image_info != (const ImageInfo *) NULL);
691 assert(image_info->signature == MagickCoreSignature);
692 if (image_info->debug != MagickFalse)
693 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
694 image_info->filename);
695 assert(exception != (ExceptionInfo *) NULL);
697 image=AcquireImage(image_info,exception);
698 if (Gdiplus::GdiplusStartup(&token,&startup_input,NULL) !=
700 ThrowReaderException(CoderError, "GdiplusStartupFailed");
701 MultiByteToWideChar(CP_UTF8,0,image->filename,-1,fileName,MagickPathExtent);
702 source=Gdiplus::Image::FromFile(fileName);
703 if (source == (Gdiplus::Image *) NULL)
705 Gdiplus::GdiplusShutdown(token);
706 ThrowReaderException(FileOpenError,"UnableToOpenFile");
709 image->resolution.x=source->GetHorizontalResolution();
710 image->resolution.y=source->GetVerticalResolution();
711 image->columns=(size_t) source->GetWidth();
712 image->rows=(size_t) source->GetHeight();
713 if (image_info->density != (char *) NULL)
715 flags=ParseGeometry(image_info->density,&geometry_info);
716 image->resolution.x=geometry_info.rho;
717 image->resolution.y=geometry_info.sigma;
718 if ((flags & SigmaValue) == 0)
719 image->resolution.y=image->resolution.x;
720 if ((image->resolution.x > 0.0) && (image->resolution.y > 0.0))
722 image->columns=(size_t) floor((Gdiplus::REAL) source->GetWidth() /
723 source->GetHorizontalResolution() * image->resolution.x + 0.5);
724 image->rows=(size_t)floor((Gdiplus::REAL) source->GetHeight() /
725 source->GetVerticalResolution() * image->resolution.y + 0.5);
729 bitmap=new Gdiplus::Bitmap((INT) image->columns,(INT) image->rows,
730 PixelFormat32bppARGB);
731 graphics=Gdiplus::Graphics::FromImage(bitmap);
732 graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
733 graphics->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
734 graphics->SetTextRenderingHint(Gdiplus::TextRenderingHintClearTypeGridFit);
735 graphics->Clear(Gdiplus::Color((BYTE) ScaleQuantumToChar(
736 image->background_color.alpha),(BYTE) ScaleQuantumToChar(
737 image->background_color.red),(BYTE) ScaleQuantumToChar(
738 image->background_color.green),(BYTE) ScaleQuantumToChar(
739 image->background_color.blue)));
740 graphics->DrawImage(source,0,0,(INT) image->columns,(INT) image->rows);
744 rect=Gdiplus::Rect(0,0,(INT) image->columns,(INT) image->rows);
745 if (bitmap->LockBits(&rect,Gdiplus::ImageLockModeRead,PixelFormat32bppARGB,
746 &bitmap_data) != Gdiplus::Ok)
749 Gdiplus::GdiplusShutdown(token);
750 ThrowReaderException(FileOpenError,"UnableToReadImageData");
753 image->alpha_trait=BlendPixelTrait;
754 for (y=0; y < (ssize_t) image->rows; y++)
756 p=(unsigned char *) bitmap_data.Scan0+(y*abs(bitmap_data.Stride));
757 if (bitmap_data.Stride < 0)
758 q=GetAuthenticPixels(image,0,image->rows-y-1,image->columns,1,exception);
760 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
761 if (q == (Quantum *) NULL)
764 for (x=0; x < (ssize_t) image->columns; x++)
766 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
767 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
768 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
769 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
770 q+=GetPixelChannels(image);
773 if (SyncAuthenticPixels(image,exception) == MagickFalse)
777 bitmap->UnlockBits(&bitmap_data);
779 Gdiplus::GdiplusShutdown(token);
782 # endif /* _MSC_VER */
783 #endif /* MAGICKCORE_EMF_DELEGATE */
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % R e g i s t e r E M F I m a g e %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % RegisterEMFImage() adds attributes for the EMF image format to
797 % the list of supported formats. The attributes include the image format
798 % tag, a method to read and/or write the format, whether the format
799 % supports the saving of more than one frame to the same file or blob,
800 % whether the format supports native in-memory I/O, and a brief
801 % description of the format.
803 % The format of the RegisterEMFImage method is:
805 % size_t RegisterEMFImage(void)
808 ModuleExport size_t RegisterEMFImage(void)
813 entry=AcquireMagickInfo("EMF","EMF","Windows Enhanced Meta File");
814 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
815 entry->decoder=ReadEMFImage;
817 entry->magick=(IsImageFormatHandler *) IsEMF;
818 entry->flags^=CoderBlobSupportFlag;
819 (void) RegisterMagickInfo(entry);
820 entry=AcquireMagickInfo("EMF","WMF","Windows Meta File");
821 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
822 entry->decoder=ReadEMFImage;
824 entry->magick=(IsImageFormatHandler *) IsWMF;
825 entry->flags^=CoderBlobSupportFlag;
826 (void) RegisterMagickInfo(entry);
827 return(MagickImageCoderSignature);
831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 % U n r e g i s t e r E M F I m a g e %
839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 % UnregisterEMFImage() removes format registrations made by the
842 % EMF module from the list of supported formats.
844 % The format of the UnregisterEMFImage method is:
846 % UnregisterEMFImage(void)
849 ModuleExport void UnregisterEMFImage(void)
851 (void) UnregisterMagickInfo("EMF");
852 (void) UnregisterMagickInfo("WMF");