2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Windows Enahanced Metafile Format %
19 % Copyright 1999-2010 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 "magick/studio.h"
41 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
42 # if defined(__CYGWIN__)
45 /* All MinGW needs ... */
50 #include "magick/blob.h"
51 #include "magick/blob-private.h"
52 #include "magick/cache.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/image.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/magick.h"
60 #include "magick/memory_.h"
61 #include "magick/quantum-private.h"
62 #include "magick/static.h"
63 #include "magick/string_.h"
64 #include "magick/module.h"
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % IsEMF() returns MagickTrue if the image format type, identified by the
78 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
80 % The format of the ReadEMFImage method is:
82 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
84 % A description of each parameter follows:
86 % o magick: compare image format pattern against these bytes.
88 % o length: Specifies the length of the magick string.
91 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
95 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % IsWMF() returns MagickTrue if the image format type, identified by the
112 % magick string, is a Windows MetaFile (WMF) file.
114 % The format of the ReadEMFImage method is:
116 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
118 % A description of each parameter follows:
120 % o magick: compare image format pattern against these bytes.
122 % o length: Specifies the length of the magick string.
125 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
129 if (memcmp(magick,"\327\315\306\232",4) == 0)
131 if (memcmp(magick,"\001\000\011\000",4) == 0)
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % R e a d E M F I m a g e %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or
148 % Windows MetaFile (WMF) file using the Windows API and returns it. It
149 % allocates the memory necessary for the new Image structure and returns a
150 % pointer to the new image.
152 % The format of the ReadEMFImage method is:
154 % Image *ReadEMFImage(const ImageInfo *image_info,
155 % ExceptionInfo *exception)
157 % A description of each parameter follows:
159 % o image_info: the image info..
161 % o exception: return any errors or warnings in this structure.
165 #if defined(MAGICKCORE_HAVE__WFOPEN)
166 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
168 register const unsigned char
171 if (utf16 != (wchar_t *) NULL)
180 Convert UTF-8 to UTF-16.
183 for (p=utf8; *p != '\0'; p++)
185 if ((*p & 0x80) == 0)
188 if ((*p & 0xE0) == 0xC0)
193 if ((*p & 0xC0) != 0x80)
198 if ((*p & 0xF0) == 0xE0)
203 if ((*p & 0xC0) != 0x80)
208 if ((*p & 0xC0) != 0x80)
220 Compute UTF-16 string length.
222 for (p=utf8; *p != '\0'; p++)
224 if ((*p & 0x80) == 0)
227 if ((*p & 0xE0) == 0xC0)
230 if ((*p & 0xC0) != 0x80)
234 if ((*p & 0xF0) == 0xE0)
237 if ((*p & 0xC0) != 0x80)
240 if ((*p & 0xC0) != 0x80)
249 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
257 length=UTF8ToUTF16(source,(wchar_t *) NULL);
264 Not UTF-8, just copy.
266 length=strlen((char *) source);
267 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
268 if (utf16 == (wchar_t *) NULL)
269 return((wchar_t *) NULL);
270 for (i=0; i <= (ssize_t) length; i++)
274 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
275 if (utf16 == (wchar_t *) NULL)
276 return((wchar_t *) NULL);
277 length=UTF8ToUTF16(source,utf16);
283 This method reads either an enhanced metafile, a regular 16bit Windows
284 metafile, or an Aldus Placeable metafile and converts it into an enhanced
285 metafile. Width and height are returned in .01mm units.
287 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
288 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
291 #pragma pack( push, 2 )
300 } APMHEADER, *PAPMHEADER;
329 hTemp=GetEnhMetaFile(path);
330 #if defined(MAGICKCORE_HAVE__WFOPEN)
331 if (hTemp == (HENHMETAFILE) NULL)
336 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
337 if (unicode_path != (wchar_t *) NULL)
339 hTemp=GetEnhMetaFileW(unicode_path);
340 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
344 if (hTemp != (HENHMETAFILE) NULL)
349 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
350 *width=emfh.rclFrame.right-emfh.rclFrame.left;
351 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
354 hOld=GetMetaFile(path);
355 if (hOld != (HMETAFILE) NULL)
358 16bit windows metafile.
360 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
363 DeleteMetaFile(hOld);
364 return((HENHMETAFILE) NULL);
366 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
367 if (pBits == (LPBYTE) NULL)
369 DeleteMetaFile(hOld);
370 return((HENHMETAFILE) NULL);
372 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
374 pBits=(BYTE *) DestroyString((char *) pBits);
375 DeleteMetaFile(hOld);
376 return((HENHMETAFILE) NULL);
379 Make an enhanced metafile from the windows metafile.
381 mp.mm=MM_ANISOTROPIC;
386 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
388 DeleteMetaFile(hOld);
389 pBits=(BYTE *) DestroyString((char *) pBits);
390 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
391 *width=emfh.rclFrame.right-emfh.rclFrame.left;
392 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
396 Aldus Placeable metafile.
398 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
400 if (hFile == INVALID_HANDLE_VALUE)
402 dwSize=GetFileSize(hFile,NULL);
403 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
404 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
406 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
408 pBits=(BYTE *) DestroyString((char *) pBits);
409 return((HENHMETAFILE) NULL);
412 Make an enhanced metafile from the placable metafile.
414 mp.mm=MM_ANISOTROPIC;
415 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
417 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
418 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
420 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
423 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
425 pBits=(BYTE *) DestroyString((char *) pBits);
429 #define CENTIMETERS_INCH 2.54
431 static Image *ReadEMFImage(const ImageInfo *image_info,
432 ExceptionInfo *exception)
468 image=AcquireImage(image_info);
469 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
470 if (hemf == (HENHMETAFILE) NULL)
471 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
472 if ((image->columns == 0) || (image->rows == 0))
478 y_resolution=DefaultResolution;
479 x_resolution=DefaultResolution;
480 if (image->y_resolution > 0)
482 y_resolution=image->y_resolution;
483 if (image->units == PixelsPerCentimeterResolution)
484 y_resolution*=CENTIMETERS_INCH;
486 if (image->x_resolution > 0)
488 x_resolution=image->x_resolution;
489 if (image->units == PixelsPerCentimeterResolution)
490 x_resolution*=CENTIMETERS_INCH;
492 image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*
494 image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)*
497 if (image_info->size != (char *) NULL)
502 image->columns=width;
506 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
508 if (image_info->page != (char *) NULL)
522 geometry=GetPageGeometry(image_info->page);
523 p=strchr(geometry,'>');
524 if (p == (char *) NULL)
526 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
528 if (image->x_resolution != 0.0)
529 image->columns=(size_t) floor((image->columns*
530 image->x_resolution)+0.5);
531 if (image->y_resolution != 0.0)
532 image->rows=(size_t) floor((image->rows*image->y_resolution)+
538 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
540 if (image->x_resolution != 0.0)
541 image->columns=(size_t) floor(((image->columns*
542 image->x_resolution)/DefaultResolution)+0.5);
543 if (image->y_resolution != 0.0)
544 image->rows=(size_t) floor(((image->rows*
545 image->y_resolution)/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,
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 == (PixelPacket *) NULL)
616 for (x=0; x < (ssize_t) image->columns; x++)
618 q->red=ScaleCharToQuantum(pBits->rgbRed);
619 q->green=ScaleCharToQuantum(pBits->rgbGreen);
620 q->blue=ScaleCharToQuantum(pBits->rgbBlue);
621 SetOpacityPixelComponent(q,OpaqueOpacity);
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");