blob: 3032e000a59a91dd1b5048bb3d84bb818f9d6951 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% EEEEE M M FFFFF %
7% E MM MM F %
8% EEE M M M FFF %
9% E M M F %
10% EEEEE M M F %
11% %
12% %
13% Read Windows Enahanced Metafile Format %
14% %
15% Software Design %
16% Bill Radcliffe %
17% 2001 %
18% %
19% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% http://www.imagemagick.org/script/license.php %
26% %
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. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34*/
35
36/*
37 * Include declarations.
38 */
39
40#include "magick/studio.h"
41#if defined(MAGICKCORE_WINGDI32_DELEGATE)
42# if defined(__CYGWIN__)
43# include <windows.h>
44# else
45 /* All MinGW needs ... */
46# include <wingdi.h>
47# endif
48#endif
49
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"
65
66/*
67%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68% %
69% %
70% %
71% I s E F M %
72% %
73% %
74% %
75%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76%
77% IsEMF() returns MagickTrue if the image format type, identified by the
78% magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
79%
80% The format of the ReadEMFImage method is:
81%
82% MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
83%
84% A description of each parameter follows:
85%
86% o magick: compare image format pattern against these bytes.
87%
88% o length: Specifies the length of the magick string.
89%
90*/
91static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
92{
93 if (length < 48)
94 return(MagickFalse);
95 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
96 return(MagickTrue);
97 return(MagickFalse);
98}
99
100/*
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102% %
103% %
104% %
105% I s W M F %
106% %
107% %
108% %
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110%
111% IsWMF() returns MagickTrue if the image format type, identified by the
112% magick string, is a Windows MetaFile (WMF) file.
113%
114% The format of the ReadEMFImage method is:
115%
116% MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
117%
118% A description of each parameter follows:
119%
120% o magick: compare image format pattern against these bytes.
121%
122% o length: Specifies the length of the magick string.
123%
124*/
125static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
126{
127 if (length < 4)
128 return(MagickFalse);
129 if (memcmp(magick,"\327\315\306\232",4) == 0)
130 return(MagickTrue);
131 if (memcmp(magick,"\001\000\011\000",4) == 0)
132 return(MagickTrue);
133 return(MagickFalse);
134}
135
136/*
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138% %
139% %
140% %
141% R e a d E M F I m a g e %
142% %
143% %
144% %
145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146%
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.
151%
152% The format of the ReadEMFImage method is:
153%
154% Image *ReadEMFImage(const ImageInfo *image_info,
155% ExceptionInfo *exception)
156%
157% A description of each parameter follows:
158%
159% o image_info: the image info..
160%
161% o exception: return any errors or warnings in this structure.
162%
163*/
164
165#if defined(MAGICKCORE_HAVE__WFOPEN)
166static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
167{
168 register const unsigned char
169 *p;
170
171 if (utf16 != (wchar_t *) NULL)
172 {
173 register wchar_t
174 *q;
175
176 wchar_t
177 c;
178
179 /*
180 Convert UTF-8 to UTF-16.
181 */
182 q=utf16;
183 for (p=utf8; *p != '\0'; p++)
184 {
185 if ((*p & 0x80) == 0)
186 *q=(*p);
187 else
188 if ((*p & 0xE0) == 0xC0)
189 {
190 c=(*p);
191 *q=(c & 0x1F) << 6;
192 p++;
193 if ((*p & 0xC0) != 0x80)
194 return(0);
195 *q|=(*p & 0x3F);
196 }
197 else
198 if ((*p & 0xF0) == 0xE0)
199 {
200 c=(*p);
201 *q=c << 12;
202 p++;
203 if ((*p & 0xC0) != 0x80)
204 return(0);
205 c=(*p);
206 *q|=(c & 0x3F) << 6;
207 p++;
208 if ((*p & 0xC0) != 0x80)
209 return(0);
210 *q|=(*p & 0x3F);
211 }
212 else
213 return(0);
214 q++;
215 }
216 *q++='\0';
217 return(q-utf16);
218 }
219 /*
220 Compute UTF-16 string length.
221 */
222 for (p=utf8; *p != '\0'; p++)
223 {
224 if ((*p & 0x80) == 0)
225 ;
226 else
227 if ((*p & 0xE0) == 0xC0)
228 {
229 p++;
230 if ((*p & 0xC0) != 0x80)
231 return(0);
232 }
233 else
234 if ((*p & 0xF0) == 0xE0)
235 {
236 p++;
237 if ((*p & 0xC0) != 0x80)
238 return(0);
239 p++;
240 if ((*p & 0xC0) != 0x80)
241 return(0);
242 }
243 else
244 return(0);
245 }
246 return(p-utf8);
247}
248
249static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
250{
251 size_t
252 length;
253
254 wchar_t
255 *utf16;
256
257 length=UTF8ToUTF16(source,(wchar_t *) NULL);
258 if (length == 0)
259 {
260 register long
261 i;
262
263 /*
264 Not UTF-8, just copy.
265 */
266 length=strlen(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 <= (long) length; i++)
271 utf16[i]=source[i];
272 return(utf16);
273 }
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);
278 return(utf16);
279}
280#endif
281
282/*
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.
286*/
287#if defined(MAGICKCORE_WINGDI32_DELEGATE)
288static HENHMETAFILE ReadEnhMetaFile(const char *path,long *width,
289 long *height)
290{
291#pragma pack( push )
292#pragma pack( 2 )
293 typedef struct
294 {
295 DWORD dwKey;
296 WORD hmf;
297 SMALL_RECT bbox;
298 WORD wInch;
299 DWORD dwReserved;
300 WORD wCheckSum;
301 } APMHEADER, *PAPMHEADER;
302#pragma pack( pop )
303
304 DWORD
305 dwSize;
306
307 ENHMETAHEADER
308 emfh;
309
310 HANDLE
311 hFile;
312
313 HDC
314 hDC;
315
316 HENHMETAFILE
317 hTemp;
318
319 LPBYTE
320 pBits;
321
322 METAFILEPICT
323 mp;
324
325 HMETAFILE
326 hOld;
327
328 *width=512;
329 *height=512;
330 hTemp=GetEnhMetaFile(path);
331#if defined(MAGICKCORE_HAVE__WFOPEN)
332 if (hTemp == (HENHMETAFILE) NULL)
333 {
334 wchar_t
335 *unicode_path;
336
337 unicode_path=ConvertUTF8ToUTF16(path);
338 if (unicode_path != (wchar_t *) NULL)
339 {
340 hTemp=GetEnhMetaFileW(unicode_path);
341 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
342 }
343 }
344#endif
345 if (hTemp != (HENHMETAFILE) NULL)
346 {
347 /*
348 Enhanced metafile.
349 */
350 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
351 *width=emfh.rclFrame.right-emfh.rclFrame.left;
352 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
353 return(hTemp);
354 }
355 hOld=GetMetaFile(path);
356 if (hOld != (HMETAFILE) NULL)
357 {
358 /*
359 16bit windows metafile.
360 */
361 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
362 if (dwSize == 0)
363 {
364 DeleteMetaFile(hOld);
365 return((HENHMETAFILE) NULL);
366 }
367 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
368 if (pBits == (LPBYTE) NULL)
369 {
370 DeleteMetaFile(hOld);
371 return((HENHMETAFILE) NULL);
372 }
373 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
374 {
375 pBits=(BYTE *) DestroyString((char *) pBits);
376 DeleteMetaFile(hOld);
377 return((HENHMETAFILE) NULL);
378 }
379 /*
380 Make an enhanced metafile from the windows metafile.
381 */
382 mp.mm=MM_ANISOTROPIC;
383 mp.xExt=1000;
384 mp.yExt=1000;
385 mp.hMF=NULL;
386 hDC=GetDC(NULL);
387 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
388 ReleaseDC(NULL,hDC);
389 DeleteMetaFile(hOld);
390 pBits=(BYTE *) DestroyString((char *) pBits);
391 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
392 *width=emfh.rclFrame.right-emfh.rclFrame.left;
393 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
394 return(hTemp);
395 }
396 /*
397 Aldus Placeable metafile.
398 */
399 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
400 NULL);
401 if (hFile == INVALID_HANDLE_VALUE)
402 return(NULL);
403 dwSize=GetFileSize(hFile,NULL);
404 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
405 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
406 CloseHandle(hFile);
407 if (((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l)
408 {
409 pBits=(BYTE *) DestroyString((char *) pBits);
410 return((HENHMETAFILE) NULL);
411 }
412 /*
413 Make an enhanced metafile from the placable metafile.
414 */
415 mp.mm=MM_ANISOTROPIC;
416 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
417 *width=mp.xExt;
418 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
419 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
420 *height=mp.yExt;
421 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
422 mp.hMF=NULL;
423 hDC=GetDC(NULL);
424 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
425 ReleaseDC(NULL,hDC);
426 pBits=(BYTE *) DestroyString((char *) pBits);
427 return(hTemp);
428}
429
430#define CENTIMETERS_INCH 2.54
431
432static Image *ReadEMFImage(const ImageInfo *image_info,
433 ExceptionInfo *exception)
434{
435 BITMAPINFO
436 DIBinfo;
437
438 HBITMAP
439 hBitmap,
440 hOldBitmap;
441
442 HDC
443 hDC;
444
445 HENHMETAFILE
446 hemf;
447
448 Image
449 *image;
450
451 long
452 height,
453 width,
454 y;
455
456 RECT
457 rect;
458
459 register long
460 x;
461
462 register PixelPacket
463 *q;
464
465 RGBQUAD
466 *pBits,
467 *ppBits;
468
469 image=AcquireImage(image_info);
470 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
471 if (hemf == (HENHMETAFILE) NULL)
472 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
473 if ((image->columns == 0) || (image->rows == 0))
474 {
475 double
476 y_resolution,
477 x_resolution;
478
479 y_resolution=DefaultResolution;
480 x_resolution=DefaultResolution;
481 if (image->y_resolution > 0)
482 {
483 y_resolution=image->y_resolution;
484 if (image->units == PixelsPerCentimeterResolution)
485 y_resolution*=CENTIMETERS_INCH;
486 }
487 if (image->x_resolution > 0)
488 {
489 x_resolution=image->x_resolution;
490 if (image->units == PixelsPerCentimeterResolution)
491 x_resolution*=CENTIMETERS_INCH;
492 }
493 image->rows=(unsigned long) ((height/1000.0/CENTIMETERS_INCH)*
494 y_resolution+0.5);
495 image->columns=(unsigned long) ((width/1000.0/CENTIMETERS_INCH)*
496 x_resolution+0.5);
497 }
498 if (image_info->size != (char *) NULL)
499 {
500 long
501 x;
502
503 image->columns=width;
504 image->rows=height;
505 x=0;
506 y=0;
507 (void) GetGeometry(image_info->size,&x,&y,&image->columns,&image->rows);
508 }
509 if (image_info->page != (char *) NULL)
510 {
511 char
512 *geometry;
513
514 long
515 sans;
516
517 register char
518 *p;
519
520 MagickStatusType
521 flags;
522
523 geometry=GetPageGeometry(image_info->page);
524 p=strchr(geometry,'>');
525 if (p == (char *) NULL)
526 {
527 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
528 &image->rows);
529 if (image->x_resolution != 0.0)
530 image->columns=(unsigned long) ((image->columns*
531 image->x_resolution)+0.5);
532 if (image->y_resolution != 0.0)
533 image->rows=(unsigned long) ((image->rows*image->y_resolution)+0.5);
534 }
535 else
536 {
537 *p='\0';
538 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
539 &image->rows);
540 if (image->x_resolution != 0.0)
541 image->columns=(unsigned long) (((image->columns*
542 image->x_resolution)/DefaultResolution)+0.5);
543 if (image->y_resolution != 0.0)
544 image->rows=(unsigned long) (((image->rows*image->y_resolution)/
545 DefaultResolution)+0.5);
546 }
547 geometry=DestroyString(geometry);
548 }
549 hDC=GetDC(NULL);
550 if (hDC == (HDC) NULL)
551 {
552 DeleteEnhMetaFile(hemf);
553 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
554 }
555 /*
556 Initialize the bitmap header info.
557 */
558 (void) ResetMagickMemory(&DIBinfo,0,sizeof(BITMAPINFO));
559 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
560 DIBinfo.bmiHeader.biWidth=image->columns;
561 DIBinfo.bmiHeader.biHeight=(-1)*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,
566 NULL,0);
567 ReleaseDC(NULL,hDC);
568 if (hBitmap == (HBITMAP) NULL)
569 {
570 DeleteEnhMetaFile(hemf);
571 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
572 }
573 hDC=CreateCompatibleDC(NULL);
574 if (hDC == (HDC) NULL)
575 {
576 DeleteEnhMetaFile(hemf);
577 DeleteObject(hBitmap);
578 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
579 }
580 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
581 if (hOldBitmap == (HBITMAP) NULL)
582 {
583 DeleteEnhMetaFile(hemf);
584 DeleteDC(hDC);
585 DeleteObject(hBitmap);
586 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
587 }
588 /*
589 Initialize the bitmap to the image background color.
590 */
591 pBits=ppBits;
592 for (y=0; y < (long) image->rows; y++)
593 {
594 for (x=0; x < (long) image->columns; x++)
595 {
596 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
597 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
598 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
599 pBits++;
600 }
601 }
602 rect.top=0;
603 rect.left=0;
604 rect.right=image->columns;
605 rect.bottom=image->rows;
606 /*
607 Convert metafile pixels.
608 */
609 PlayEnhMetaFile(hDC,hemf,&rect);
610 pBits=ppBits;
611 for (y=0; y < (long) image->rows; y++)
612 {
613 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
614 if (q == (PixelPacket *) NULL)
615 break;
616 for (x=0; x < (long) image->columns; x++)
617 {
618 q->red=ScaleCharToQuantum(pBits->rgbRed);
619 q->green=ScaleCharToQuantum(pBits->rgbGreen);
620 q->blue=ScaleCharToQuantum(pBits->rgbBlue);
621 q->opacity=OpaqueOpacity;
622 pBits++;
623 q++;
624 }
625 if (SyncAuthenticPixels(image,exception) == MagickFalse)
626 break;
627 }
628 DeleteEnhMetaFile(hemf);
629 SelectObject(hDC,hOldBitmap);
630 DeleteDC(hDC);
631 DeleteObject(hBitmap);
632 return(GetFirstImageInList(image));
633}
634#endif /* MAGICKCORE_WINGDI32_DELEGATE */
635
636/*
637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638% %
639% %
640% %
641% R e g i s t e r E M F I m a g e %
642% %
643% %
644% %
645%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646%
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.
653%
654% The format of the RegisterEMFImage method is:
655%
656% unsigned long RegisterEMFImage(void)
657%
658*/
659ModuleExport unsigned long RegisterEMFImage(void)
660{
661 MagickInfo
662 *entry;
663
664 entry=SetMagickInfo("EMF");
665#if defined(MAGICKCORE_WINGDI32_DELEGATE)
666 entry->decoder=ReadEMFImage;
667#endif
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;
677#endif
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);
684}
685
686/*
687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688% %
689% %
690% %
691% U n r e g i s t e r E M F I m a g e %
692% %
693% %
694% %
695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696%
697% UnregisterEMFImage() removes format registrations made by the
698% EMF module from the list of supported formats.
699%
700% The format of the UnregisterEMFImage method is:
701%
702% UnregisterEMFImage(void)
703%
704*/
705ModuleExport void UnregisterEMFImage(void)
706{
707 (void) UnregisterMagickInfo("EMF");
708 (void) UnregisterMagickInfo("WMFWIN32");
709}