2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read Windows Enahanced Metafile Format %
19 % Copyright 1999-2011 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 This method reads either an enhanced metafile, a regular 16bit Windows
168 metafile, or an Aldus Placeable metafile and converts it into an enhanced
169 metafile. Width and height are returned in .01mm units.
171 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
172 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
175 #pragma pack( push, 2 )
184 } APMHEADER, *PAPMHEADER;
213 hTemp=GetEnhMetaFile(path);
214 #if defined(MAGICKCORE_HAVE__WFOPEN)
215 if (hTemp == (HENHMETAFILE) NULL)
223 count=MultiByteToWideChar(CP_ACP,0,path,-1,NULL,0);
224 unicode_path=(wchar_t *) AcquireQuantumMemory(count,
225 sizeof(*unicode_path));
226 if (unicode_path != (wchar_t *) NULL)
228 hTemp=GetEnhMetaFileW(unicode_path);
229 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
233 if (hTemp != (HENHMETAFILE) NULL)
238 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
239 *width=emfh.rclFrame.right-emfh.rclFrame.left;
240 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
243 hOld=GetMetaFile(path);
244 if (hOld != (HMETAFILE) NULL)
247 16bit windows metafile.
249 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
252 DeleteMetaFile(hOld);
253 return((HENHMETAFILE) NULL);
255 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
256 if (pBits == (LPBYTE) NULL)
258 DeleteMetaFile(hOld);
259 return((HENHMETAFILE) NULL);
261 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
263 pBits=(BYTE *) DestroyString((char *) pBits);
264 DeleteMetaFile(hOld);
265 return((HENHMETAFILE) NULL);
268 Make an enhanced metafile from the windows metafile.
270 mp.mm=MM_ANISOTROPIC;
275 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
277 DeleteMetaFile(hOld);
278 pBits=(BYTE *) DestroyString((char *) pBits);
279 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
280 *width=emfh.rclFrame.right-emfh.rclFrame.left;
281 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
285 Aldus Placeable metafile.
287 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
289 if (hFile == INVALID_HANDLE_VALUE)
291 dwSize=GetFileSize(hFile,NULL);
292 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
293 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
295 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
297 pBits=(BYTE *) DestroyString((char *) pBits);
298 return((HENHMETAFILE) NULL);
301 Make an enhanced metafile from the placable metafile.
303 mp.mm=MM_ANISOTROPIC;
304 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
306 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
307 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
309 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
312 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
314 pBits=(BYTE *) DestroyString((char *) pBits);
318 #define CENTIMETERS_INCH 2.54
320 static Image *ReadEMFImage(const ImageInfo *image_info,
321 ExceptionInfo *exception)
357 image=AcquireImage(image_info);
358 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
359 if (hemf == (HENHMETAFILE) NULL)
360 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
361 if ((image->columns == 0) || (image->rows == 0))
367 y_resolution=DefaultResolution;
368 x_resolution=DefaultResolution;
369 if (image->y_resolution > 0)
371 y_resolution=image->y_resolution;
372 if (image->units == PixelsPerCentimeterResolution)
373 y_resolution*=CENTIMETERS_INCH;
375 if (image->x_resolution > 0)
377 x_resolution=image->x_resolution;
378 if (image->units == PixelsPerCentimeterResolution)
379 x_resolution*=CENTIMETERS_INCH;
381 image->rows=(size_t) ((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5);
382 image->columns=(size_t) ((width/1000.0/CENTIMETERS_INCH)*
385 if (image_info->size != (char *) NULL)
390 image->columns=width;
394 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
396 if (image_info->page != (char *) NULL)
410 geometry=GetPageGeometry(image_info->page);
411 p=strchr(geometry,'>');
412 if (p == (char *) NULL)
414 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
416 if (image->x_resolution != 0.0)
417 image->columns=(size_t) floor((image->columns*image->x_resolution)+
419 if (image->y_resolution != 0.0)
420 image->rows=(size_t) floor((image->rows*image->y_resolution)+0.5);
425 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
427 if (image->x_resolution != 0.0)
428 image->columns=(size_t) floor(((image->columns*image->x_resolution)/
429 DefaultResolution)+0.5);
430 if (image->y_resolution != 0.0)
431 image->rows=(size_t) floor(((image->rows*image->y_resolution)/
432 DefaultResolution)+0.5);
434 geometry=DestroyString(geometry);
437 if (hDC == (HDC) NULL)
439 DeleteEnhMetaFile(hemf);
440 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
443 Initialize the bitmap header info.
445 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
446 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
447 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
448 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
449 DIBinfo.bmiHeader.biPlanes=1;
450 DIBinfo.bmiHeader.biBitCount=32;
451 DIBinfo.bmiHeader.biCompression=BI_RGB;
452 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
455 if (hBitmap == (HBITMAP) NULL)
457 DeleteEnhMetaFile(hemf);
458 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
460 hDC=CreateCompatibleDC(NULL);
461 if (hDC == (HDC) NULL)
463 DeleteEnhMetaFile(hemf);
464 DeleteObject(hBitmap);
465 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
467 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
468 if (hOldBitmap == (HBITMAP) NULL)
470 DeleteEnhMetaFile(hemf);
472 DeleteObject(hBitmap);
473 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
476 Initialize the bitmap to the image background color.
479 for (y=0; y < (ssize_t) image->rows; y++)
481 for (x=0; x < (ssize_t) image->columns; x++)
483 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
484 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
485 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
491 rect.right=(LONG) image->columns;
492 rect.bottom=(LONG) image->rows;
494 Convert metafile pixels.
496 PlayEnhMetaFile(hDC,hemf,&rect);
498 for (y=0; y < (ssize_t) image->rows; y++)
500 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
501 if (q == (const Quantum *) NULL)
503 for (x=0; x < (ssize_t) image->columns; x++)
505 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
506 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
507 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
508 SetPixelAlpha(image,OpaqueAlpha,q);
510 q+=GetPixelChannels(image);
512 if (SyncAuthenticPixels(image,exception) == MagickFalse)
515 DeleteEnhMetaFile(hemf);
516 SelectObject(hDC,hOldBitmap);
518 DeleteObject(hBitmap);
519 return(GetFirstImageInList(image));
521 #endif /* MAGICKCORE_WINGDI32_DELEGATE */
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % R e g i s t e r E M F I m a g e %
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % RegisterEMFImage() adds attributes for the EMF image format to
535 % the list of supported formats. The attributes include the image format
536 % tag, a method to read and/or write the format, whether the format
537 % supports the saving of more than one frame to the same file or blob,
538 % whether the format supports native in-memory I/O, and a brief
539 % description of the format.
541 % The format of the RegisterEMFImage method is:
543 % size_t RegisterEMFImage(void)
546 ModuleExport size_t RegisterEMFImage(void)
551 entry=SetMagickInfo("EMF");
552 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
553 entry->decoder=ReadEMFImage;
555 entry->description=ConstantString(
556 "Windows WIN32 API rendered Enhanced Meta File");
557 entry->magick=(IsImageFormatHandler *) IsEMF;
558 entry->blob_support=MagickFalse;
559 entry->module=ConstantString("WMF");
560 (void) RegisterMagickInfo(entry);
561 entry=SetMagickInfo("WMFWIN32");
562 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
563 entry->decoder=ReadEMFImage;
565 entry->description=ConstantString("Windows WIN32 API rendered Meta File");
566 entry->magick=(IsImageFormatHandler *) IsWMF;
567 entry->blob_support=MagickFalse;
568 entry->module=ConstantString("WMFWIN32");
569 (void) RegisterMagickInfo(entry);
570 return(MagickImageCoderSignature);
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 % U n r e g i s t e r E M F I m a g e %
582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 % UnregisterEMFImage() removes format registrations made by the
585 % EMF module from the list of supported formats.
587 % The format of the UnregisterEMFImage method is:
589 % UnregisterEMFImage(void)
592 ModuleExport void UnregisterEMFImage(void)
594 (void) UnregisterMagickInfo("EMF");
595 (void) UnregisterMagickInfo("WMFWIN32");