Debian Codegraph @

coders/emf.c

raw symbols SCIP
1 /*
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 Enhanced Metafile Format %
14 % %
15 % Software Design %
16 % Bill Radcliffe %
17 % 2001 %
18 % Dirk Lemstra %
19 % January 2014 %
20 % %
21 % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://imagemagick.org/license/ %
28 % %
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. %
34 % %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 */
37
38 /*
39 * Include declarations.
40 */
41
42 #include "MagickCore/studio.h"
43 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
44 # if !defined(_MSC_VER)
45 # if defined(__CYGWIN__)
46 # include <windows.h>
47 # else
48 # include <wingdi.h>
49 # endif
50 # else
51 #pragma warning(disable: 4458)
52 # include <gdiplus.h>
53 #pragma warning(default: 4458)
54 # pragma comment(lib, "gdiplus.lib")
55 # endif
56 #endif
57 #include "MagickCore/blob.h"
58 #include "MagickCore/blob-private.h"
59 #include "MagickCore/cache.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/image.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/list.h"
66 #include "MagickCore/magick.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/pixel.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/static.h"
72 #include "MagickCore/string_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/utility-private.h"
75 #include "coders/emf.h"
76
77 /*
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % %
80 % %
81 % %
82 % I s E F M %
83 % %
84 % %
85 % %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %
88 % IsEMF() returns MagickTrue if the image format type, identified by the
89 % magick string, is a Microsoft Windows Enhanced MetaFile (EMF) file.
90 %
91 % The format of the ReadEMFImage method is:
92 %
93 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
94 %
95 % A description of each parameter follows:
96 %
97 % o magick: compare image format pattern against these bytes.
98 %
99 % o length: Specifies the length of the magick string.
100 %
101 */
102 static MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
103 {
104 if (length < 48)
105 return(MagickFalse);
106 if (memcmp(magick+40,"\040\105\115\106\000\000\001\000",8) == 0)
107 return(MagickTrue);
108 return(MagickFalse);
109 }
110
111 /*
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % %
114 % %
115 % %
116 % I s W M F %
117 % %
118 % %
119 % %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 %
122 % IsWMF() returns MagickTrue if the image format type, identified by the
123 % magick string, is a Windows MetaFile (WMF) file.
124 %
125 % The format of the ReadEMFImage method is:
126 %
127 % MagickBooleanType IsEMF(const unsigned char *magick,const size_t length)
128 %
129 % A description of each parameter follows:
130 %
131 % o magick: compare image format pattern against these bytes.
132 %
133 % o length: Specifies the length of the magick string.
134 %
135 */
136 static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
137 {
138 if (length < 4)
139 return(MagickFalse);
140 if (memcmp(magick,"\327\315\306\232",4) == 0)
141 return(MagickTrue);
142 if (memcmp(magick,"\001\000\011\000",4) == 0)
143 return(MagickTrue);
144 return(MagickFalse);
145 }
146
147 /*
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % %
150 % %
151 % %
152 % R e a d E M F I m a g e %
153 % %
154 % %
155 % %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %
158 % ReadEMFImage() reads an Microsoft Windows Enhanced MetaFile (EMF) or
159 % Windows MetaFile (WMF) file using the Windows API and returns it. It
160 % allocates the memory necessary for the new Image structure and returns a
161 % pointer to the new image.
162 %
163 % The format of the ReadEMFImage method is:
164 %
165 % Image *ReadEMFImage(const ImageInfo *image_info,
166 % ExceptionInfo *exception)
167 %
168 % A description of each parameter follows:
169 %
170 % o image_info: the image info..
171 %
172 % o exception: return any errors or warnings in this structure.
173 %
174 */
175
176 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
177 # if !defined(_MSC_VER)
178 # if defined(MAGICKCORE_HAVE__WFOPEN)
179 static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
180 {
181 const unsigned char
182 *p;
183
184 if (utf16 != (wchar_t *) NULL)
185 {
186 wchar_t
187 *q;
188
189 wchar_t
190 c;
191
192 /*
193 Convert UTF-8 to UTF-16.
194 */
195 q=utf16;
196 for (p=utf8; *p != '\0'; p++)
197 {
198 if ((*p & 0x80) == 0)
199 *q=(*p);
200 else
201 if ((*p & 0xE0) == 0xC0)
202 {
203 c=(*p);
204 *q=(c & 0x1F) << 6;
205 p++;
206 if ((*p & 0xC0) != 0x80)
207 return(0);
208 *q|=(*p & 0x3F);
209 }
210 else
211 if ((*p & 0xF0) == 0xE0)
212 {
213 c=(*p);
214 *q=c << 12;
215 p++;
216 if ((*p & 0xC0) != 0x80)
217 return(0);
218 c=(*p);
219 *q|=(c & 0x3F) << 6;
220 p++;
221 if ((*p & 0xC0) != 0x80)
222 return(0);
223 *q|=(*p & 0x3F);
224 }
225 else
226 return(0);
227 q++;
228 }
229 *q++='\0';
230 return(q-utf16);
231 }
232 /*
233 Compute UTF-16 string length.
234 */
235 for (p=utf8; *p != '\0'; p++)
236 {
237 if ((*p & 0x80) == 0)
238 ;
239 else
240 if ((*p & 0xE0) == 0xC0)
241 {
242 p++;
243 if ((*p & 0xC0) != 0x80)
244 return(0);
245 }
246 else
247 if ((*p & 0xF0) == 0xE0)
248 {
249 p++;
250 if ((*p & 0xC0) != 0x80)
251 return(0);
252 p++;
253 if ((*p & 0xC0) != 0x80)
254 return(0);
255 }
256 else
257 return(0);
258 }
259 return(p-utf8);
260 }
261
262 static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
263 {
264 size_t
265 length;
266
267 wchar_t
268 *utf16;
269
270 length=UTF8ToUTF16(source,(wchar_t *) NULL);
271 if (length == 0)
272 {
273 ssize_t
274 i;
275
276 /*
277 Not UTF-8, just copy.
278 */
279 length=strlen((char *) source);
280 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
281 if (utf16 == (wchar_t *) NULL)
282 return((wchar_t *) NULL);
283 for (i=0; i <= (ssize_t) length; i++)
284 utf16[i]=source[i];
285 return(utf16);
286 }
287 utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
288 if (utf16 == (wchar_t *) NULL)
289 return((wchar_t *) NULL);
290 length=UTF8ToUTF16(source,utf16);
291 return(utf16);
292 }
293 # endif /* MAGICKCORE_HAVE__WFOPEN */
294
295 static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
296 ssize_t *height)
297 {
298 #pragma pack( push, 2 )
299 typedef struct
300 {
301 DWORD dwKey;
302 WORD hmf;
303 SMALL_RECT bbox;
304 WORD wInch;
305 DWORD dwReserved;
306 WORD wCheckSum;
307 } APMHEADER, *PAPMHEADER;
308 #pragma pack( pop )
309
310 DWORD
311 dwSize;
312
313 ENHMETAHEADER
314 emfh;
315
316 HANDLE
317 hFile;
318
319 HDC
320 hDC;
321
322 HENHMETAFILE
323 hTemp;
324
325 LPBYTE
326 pBits;
327
328 METAFILEPICT
329 mp;
330
331 HMETAFILE
332 hOld;
333
334 *width=512;
335 *height=512;
336 hTemp=GetEnhMetaFile(path);
337 #if defined(MAGICKCORE_HAVE__WFOPEN)
338 if (hTemp == (HENHMETAFILE) NULL)
339 {
340 wchar_t
341 *unicode_path;
342
343 unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
344 if (unicode_path != (wchar_t *) NULL)
345 {
346 hTemp=GetEnhMetaFileW(unicode_path);
347 unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
348 }
349 }
350 #endif
351 if (hTemp != (HENHMETAFILE) NULL)
352 {
353 /*
354 Enhanced metafile.
355 */
356 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
357 *width=emfh.rclFrame.right-emfh.rclFrame.left;
358 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
359 return(hTemp);
360 }
361 hOld=GetMetaFile(path);
362 if (hOld != (HMETAFILE) NULL)
363 {
364 /*
365 16bit windows metafile.
366 */
367 dwSize=GetMetaFileBitsEx(hOld,0,NULL);
368 if (dwSize == 0)
369 {
370 DeleteMetaFile(hOld);
371 return((HENHMETAFILE) NULL);
372 }
373 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
374 if (pBits == (LPBYTE) NULL)
375 {
376 DeleteMetaFile(hOld);
377 return((HENHMETAFILE) NULL);
378 }
379 if (GetMetaFileBitsEx(hOld,dwSize,pBits) == 0)
380 {
381 pBits=(BYTE *) DestroyString((char *) pBits);
382 DeleteMetaFile(hOld);
383 return((HENHMETAFILE) NULL);
384 }
385 /*
386 Make an enhanced metafile from the windows metafile.
387 */
388 mp.mm=MM_ANISOTROPIC;
389 mp.xExt=1000;
390 mp.yExt=1000;
391 mp.hMF=NULL;
392 hDC=GetDC(NULL);
393 hTemp=SetWinMetaFileBits(dwSize,pBits,hDC,&mp);
394 ReleaseDC(NULL,hDC);
395 DeleteMetaFile(hOld);
396 pBits=(BYTE *) DestroyString((char *) pBits);
397 GetEnhMetaFileHeader(hTemp,sizeof(ENHMETAHEADER),&emfh);
398 *width=emfh.rclFrame.right-emfh.rclFrame.left;
399 *height=emfh.rclFrame.bottom-emfh.rclFrame.top;
400 return(hTemp);
401 }
402 /*
403 Aldus Placeable metafile.
404 */
405 hFile=CreateFile(path,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
406 NULL);
407 if (hFile == INVALID_HANDLE_VALUE)
408 return(NULL);
409 dwSize=GetFileSize(hFile,NULL);
410 pBits=(LPBYTE) AcquireQuantumMemory(dwSize,sizeof(*pBits));
411 if (pBits == (LPBYTE) NULL)
412 {
413 CloseHandle(hFile);
414 return((HENHMETAFILE) NULL);
415 }
416 ReadFile(hFile,pBits,dwSize,&dwSize,NULL);
417 CloseHandle(hFile);
418 if ((((PAPMHEADER) pBits)->dwKey != 0x9ac6cdd7l) ||
419 (((PAPMHEADER) pBits)->wInch == 0))
420 {
421 pBits=(BYTE *) DestroyString((char *) pBits);
422 return((HENHMETAFILE) NULL);
423 }
424 /*
425 Make an enhanced metafile from the placable metafile.
426 */
427 mp.mm=MM_ANISOTROPIC;
428 mp.xExt=((PAPMHEADER) pBits)->bbox.Right-((PAPMHEADER) pBits)->bbox.Left;
429 *width=mp.xExt;
430 mp.xExt=(mp.xExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
431 mp.yExt=((PAPMHEADER)pBits)->bbox.Bottom-((PAPMHEADER) pBits)->bbox.Top;
432 *height=mp.yExt;
433 mp.yExt=(mp.yExt*2540l)/(DWORD) (((PAPMHEADER) pBits)->wInch);
434 mp.hMF=NULL;
435 hDC=GetDC(NULL);
436 hTemp=SetWinMetaFileBits(dwSize,&(pBits[sizeof(APMHEADER)]),hDC,&mp);
437 ReleaseDC(NULL,hDC);
438 pBits=(BYTE *) DestroyString((char *) pBits);
439 return(hTemp);
440 }
441
442 #define CENTIMETERS_INCH 2.54
443
444 static Image *ReadEMFImage(const ImageInfo *image_info,ExceptionInfo *exception)
445 {
446 BITMAPINFO
447 DIBinfo;
448
449 HBITMAP
450 hBitmap,
451 hOldBitmap;
452
453 HDC
454 hDC;
455
456 HENHMETAFILE
457 hemf;
458
459 Image
460 *image;
461
462 MagickBooleanType
463 status;
464
465 RECT
466 rect;
467
468 ssize_t
469 x;
470
471 Quantum
472 *q;
473
474 RGBQUAD
475 *pBits,
476 *ppBits;
477
478 ssize_t
479 height,
480 width,
481 y;
482
483 image=AcquireImage(image_info,exception);
484 hemf=ReadEnhMetaFile(image_info->filename,&width,&height);
485 if (hemf == (HENHMETAFILE) NULL)
486 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
487 if ((image->columns == 0) || (image->rows == 0))
488 {
489 double
490 y_resolution,
491 x_resolution;
492
493 y_resolution=DefaultResolution;
494 x_resolution=DefaultResolution;
495 if (image->resolution.y > 0)
496 {
497 y_resolution=image->resolution.y;
498 if (image->units == PixelsPerCentimeterResolution)
499 y_resolution*=CENTIMETERS_INCH;
500 }
501 if (image->resolution.x > 0)
502 {
503 x_resolution=image->resolution.x;
504 if (image->units == PixelsPerCentimeterResolution)
505 x_resolution*=CENTIMETERS_INCH;
506 }
507 image->rows=CastDoubleToSizeT((height/1000.0/CENTIMETERS_INCH)*y_resolution+0.5);
508 image->columns=CastDoubleToSizeT((width/1000.0/CENTIMETERS_INCH)*x_resolution+0.5);
509 }
510 if (image_info->size != (char *) NULL)
511 {
512 image->columns=width;
513 image->rows=height;
514 (void) GetGeometry(image_info->size,(ssize_t *) NULL,(ssize_t *) NULL,
515 &image->columns,&image->rows);
516 }
517 status=SetImageExtent(image,image->columns,image->rows,exception);
518 if (status == MagickFalse)
519 return(DestroyImageList(image));
520 if (image_info->page != (char *) NULL)
521 {
522 char
523 *geometry;
524
525 char
526 *p;
527
528 MagickStatusType
529 flags;
530
531 ssize_t
532 sans;
533
534 geometry=GetPageGeometry(image_info->page);
535 p=strchr(geometry,'>');
536 if (p == (char *) NULL)
537 {
538 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
539 &image->rows);
540 if (image->resolution.x != 0.0)
541 image->columns=CastDoubleToSizeT(floor((image->columns*image->resolution.x)+0.5));
542 if (image->resolution.y != 0.0)
543 image->rows=CastDoubleToSizeT(floor((image->rows*image->resolution.y)+0.5));
544 }
545 else
546 {
547 *p='\0';
548 flags=ParseMetaGeometry(geometry,&sans,&sans,&image->columns,
549 &image->rows);
550 if (image->resolution.x != 0.0)
551 image->columns=CastDoubleToSizeT(floor(((image->columns*image->resolution.x)/
552 DefaultResolution)+0.5));
553 if (image->resolution.y != 0.0)
554 image->rows=CastDoubleToSizeT(floor(((image->rows*image->resolution.y)/
555 DefaultResolution)+0.5));
556 }
557 (void) flags;
558 geometry=DestroyString(geometry);
559 }
560 hDC=GetDC(NULL);
561 if (hDC == (HDC) NULL)
562 {
563 DeleteEnhMetaFile(hemf);
564 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
565 }
566 /*
567 Initialize the bitmap header info.
568 */
569 (void) memset(&DIBinfo,0,sizeof(BITMAPINFO));
570 DIBinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
571 DIBinfo.bmiHeader.biWidth=(LONG) image->columns;
572 DIBinfo.bmiHeader.biHeight=(-1)*(LONG) image->rows;
573 DIBinfo.bmiHeader.biPlanes=1;
574 DIBinfo.bmiHeader.biBitCount=32;
575 DIBinfo.bmiHeader.biCompression=BI_RGB;
576 hBitmap=CreateDIBSection(hDC,&DIBinfo,DIB_RGB_COLORS,(void **) &ppBits,NULL,
577 0);
578 ReleaseDC(NULL,hDC);
579 if (hBitmap == (HBITMAP) NULL)
580 {
581 DeleteEnhMetaFile(hemf);
582 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
583 }
584 hDC=CreateCompatibleDC(NULL);
585 if (hDC == (HDC) NULL)
586 {
587 DeleteEnhMetaFile(hemf);
588 DeleteObject(hBitmap);
589 ThrowReaderException(ResourceLimitError,"UnableToCreateADC");
590 }
591 hOldBitmap=(HBITMAP) SelectObject(hDC,hBitmap);
592 if (hOldBitmap == (HBITMAP) NULL)
593 {
594 DeleteEnhMetaFile(hemf);
595 DeleteDC(hDC);
596 DeleteObject(hBitmap);
597 ThrowReaderException(ResourceLimitError,"UnableToCreateBitmap");
598 }
599 /*
600 Initialize the bitmap to the image background color.
601 */
602 pBits=ppBits;
603 for (y=0; y < (ssize_t) image->rows; y++)
604 {
605 for (x=0; x < (ssize_t) image->columns; x++)
606 {
607 pBits->rgbRed=ScaleQuantumToChar(image->background_color.red);
608 pBits->rgbGreen=ScaleQuantumToChar(image->background_color.green);
609 pBits->rgbBlue=ScaleQuantumToChar(image->background_color.blue);
610 pBits++;
611 }
612 }
613 rect.top=0;
614 rect.left=0;
615 rect.right=(LONG) image->columns;
616 rect.bottom=(LONG) image->rows;
617 /*
618 Convert metafile pixels.
619 */
620 PlayEnhMetaFile(hDC,hemf,&rect);
621 pBits=ppBits;
622 for (y=0; y < (ssize_t) image->rows; y++)
623 {
624 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
625 if (q == (Quantum *) NULL)
626 break;
627 for (x=0; x < (ssize_t) image->columns; x++)
628 {
629 SetPixelRed(image,ScaleCharToQuantum(pBits->rgbRed),q);
630 SetPixelGreen(image,ScaleCharToQuantum(pBits->rgbGreen),q);
631 SetPixelBlue(image,ScaleCharToQuantum(pBits->rgbBlue),q);
632 SetPixelAlpha(image,OpaqueAlpha,q);
633 pBits++;
634 q+=(ptrdiff_t) GetPixelChannels(image);
635 }
636 if (SyncAuthenticPixels(image,exception) == MagickFalse)
637 break;
638 }
639 DeleteEnhMetaFile(hemf);
640 SelectObject(hDC,hOldBitmap);
641 DeleteDC(hDC);
642 DeleteObject(hBitmap);
643 return(GetFirstImageInList(image));
644 }
645 # else
646
647 static inline void EMFSetDimensions(Image * image,Gdiplus::Image *source)
648 {
649 if ((image->resolution.x <= 0.0) || (image->resolution.y <= 0.0))
650 return;
651
652 image->columns=CastDoubleToSizeT(floor((Gdiplus::REAL) source->GetWidth()/
653 source->GetHorizontalResolution()*image->resolution.x+0.5));
654 image->rows=CastDoubleToSizeT(floor((Gdiplus::REAL) source->GetHeight()/
655 source->GetVerticalResolution()*image->resolution.y+0.5));
656 }
657
658 static Image *ReadEMFImage(const ImageInfo *image_info,
659 ExceptionInfo *exception)
660 {
661 Gdiplus::Bitmap
662 *bitmap;
663
664 Gdiplus::BitmapData
665 bitmap_data;
666
667 Gdiplus::GdiplusStartupInput
668 startup_input;
669
670 Gdiplus::Graphics
671 *graphics;
672
673 Gdiplus::Image
674 *source;
675
676 Gdiplus::Rect
677 rect;
678
679 GeometryInfo
680 geometry_info;
681
682 Image
683 *image;
684
685 MagickStatusType
686 flags;
687
688 Quantum
689 *q;
690
691 ssize_t
692 x;
693
694 ssize_t
695 y;
696
697 ULONG_PTR
698 token;
699
700 unsigned char
701 *p;
702
703 wchar_t
704 *path;
705
706 assert(image_info != (const ImageInfo *) NULL);
707 assert(image_info->signature == MagickCoreSignature);
708 assert(exception != (ExceptionInfo *) NULL);
709 if (IsEventLogging() != MagickFalse)
710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
711 image_info->filename);
712 image=AcquireImage(image_info,exception);
713 if (Gdiplus::GdiplusStartup(&token,&startup_input,NULL) !=
714 Gdiplus::Status::Ok)
715 ThrowReaderException(CoderError, "GdiplusStartupFailed");
716 source=(Gdiplus::Image *) NULL;
717 path=NTCreateWidePath(image->filename);
718 if (path != (wchar_t *) NULL)
719 {
720 source=Gdiplus::Image::FromFile(path);
721 path=(wchar_t *) RelinquishMagickMemory(path);
722 }
723 if (source == (Gdiplus::Image *) NULL)
724 {
725 Gdiplus::GdiplusShutdown(token);
726 ThrowReaderException(FileOpenError,"UnableToOpenFile");
727 }
728 image->resolution.x=source->GetHorizontalResolution();
729 image->resolution.y=source->GetVerticalResolution();
730 image->columns=(size_t) source->GetWidth();
731 image->rows=(size_t) source->GetHeight();
732 if (image_info->size != (char *) NULL)
733 {
734 (void) GetGeometry(image_info->size,(ssize_t *) NULL,(ssize_t *) NULL,
735 &image->columns,&image->rows);
736 image->resolution.x=source->GetHorizontalResolution()*image->columns/
737 source->GetWidth();
738 image->resolution.y=source->GetVerticalResolution()*image->rows/
739 source->GetHeight();
740 if (image->resolution.x == 0)
741 image->resolution.x=image->resolution.y;
742 else if (image->resolution.y == 0)
743 image->resolution.y=image->resolution.x;
744 else
745 image->resolution.x=image->resolution.y=MagickMin(
746 image->resolution.x,image->resolution.y);
747 EMFSetDimensions(image,source);
748 }
749 else if (image_info->density != (char *) NULL)
750 {
751 flags=ParseGeometry(image_info->density,&geometry_info);
752 if ((flags & RhoValue) != 0)
753 image->resolution.x=geometry_info.rho;
754 image->resolution.y=image->resolution.x;
755 if ((flags & SigmaValue) != 0)
756 image->resolution.y=geometry_info.sigma;
757 EMFSetDimensions(image,source);
758 }
759 if (SetImageExtent(image,image->columns,image->rows,exception) == MagickFalse)
760 {
761 delete source;
762 Gdiplus::GdiplusShutdown(token);
763 return(DestroyImageList(image));
764 }
765 image->alpha_trait=BlendPixelTrait;
766 if (image->ping != MagickFalse)
767 {
768 delete source;
769 Gdiplus::GdiplusShutdown(token);
770 return(image);
771 }
772 bitmap=new Gdiplus::Bitmap((INT) image->columns,(INT) image->rows,
773 PixelFormat32bppARGB);
774 graphics=Gdiplus::Graphics::FromImage(bitmap);
775 graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
776 graphics->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
777 graphics->SetTextRenderingHint(Gdiplus::TextRenderingHintClearTypeGridFit);
778 graphics->Clear(Gdiplus::Color((BYTE) ScaleQuantumToChar(
779 (Quantum) image->background_color.alpha),(BYTE) ScaleQuantumToChar(
780 (Quantum) image->background_color.red),(BYTE) ScaleQuantumToChar(
781 (Quantum) image->background_color.green),(BYTE) ScaleQuantumToChar(
782 (Quantum) image->background_color.blue)));
783 graphics->DrawImage(source,0,0,(INT) image->columns,(INT) image->rows);
784 delete graphics;
785 delete source;
786 rect=Gdiplus::Rect(0,0,(INT) image->columns,(INT) image->rows);
787 if (bitmap->LockBits(&rect,Gdiplus::ImageLockModeRead,PixelFormat32bppARGB,
788 &bitmap_data) != Gdiplus::Ok)
789 {
790 delete bitmap;
791 Gdiplus::GdiplusShutdown(token);
792 ThrowReaderException(FileOpenError,"UnableToReadImageData");
793 }
794 for (y=0; y < (ssize_t) image->rows; y++)
795 {
796 p=(unsigned char *) bitmap_data.Scan0+(y*abs(bitmap_data.Stride));
797 if (bitmap_data.Stride < 0)
798 q=GetAuthenticPixels(image,0,image->rows-y-1,image->columns,1,exception);
799 else
800 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
801 if (q == (Quantum *) NULL)
802 break;
803
804 for (x=0; x < (ssize_t) image->columns; x++)
805 {
806 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
807 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
808 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
809 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
810 q+=(ptrdiff_t) GetPixelChannels(image);
811 }
812
813 if (SyncAuthenticPixels(image,exception) == MagickFalse)
814 break;
815 }
816 bitmap->UnlockBits(&bitmap_data);
817 delete bitmap;
818 Gdiplus::GdiplusShutdown(token);
819 return(image);
820 }
821 # endif /* _MSC_VER */
822 #endif /* MAGICKCORE_EMF_DELEGATE */
823
824 /*
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % %
827 % %
828 % %
829 % R e g i s t e r E M F I m a g e %
830 % %
831 % %
832 % %
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 %
835 % RegisterEMFImage() adds attributes for the EMF image format to
836 % the list of supported formats. The attributes include the image format
837 % tag, a method to read and/or write the format, whether the format
838 % supports the saving of more than one frame to the same file or blob,
839 % whether the format supports native in-memory I/O, and a brief
840 % description of the format.
841 %
842 % The format of the RegisterEMFImage method is:
843 %
844 % size_t RegisterEMFImage(void)
845 %
846 */
847 ModuleExport size_t RegisterEMFImage(void)
848 {
849 MagickInfo
850 *entry;
851
852 entry=AcquireMagickInfo("EMF","EMF","Windows Enhanced Meta File");
853 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
854 entry->decoder=ReadEMFImage;
855 #endif
856 entry->magick=(IsImageFormatHandler *) IsEMF;
857 entry->flags^=CoderBlobSupportFlag;
858 (void) RegisterMagickInfo(entry);
859 entry=AcquireMagickInfo("EMF","WMF","Windows Meta File");
860 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
861 entry->decoder=ReadEMFImage;
862 #endif
863 entry->magick=(IsImageFormatHandler *) IsWMF;
864 entry->flags^=CoderBlobSupportFlag;
865 (void) RegisterMagickInfo(entry);
866 return(MagickImageCoderSignature);
867 }
868
869 /*
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 % %
872 % %
873 % %
874 % U n r e g i s t e r E M F I m a g e %
875 % %
876 % %
877 % %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 %
880 % UnregisterEMFImage() removes format registrations made by the
881 % EMF module from the list of supported formats.
882 %
883 % The format of the UnregisterEMFImage method is:
884 %
885 % UnregisterEMFImage(void)
886 %
887 */
888 ModuleExport void UnregisterEMFImage(void)
889 {
890 (void) UnregisterMagickInfo("EMF");
891 (void) UnregisterMagickInfo("WMF");
892 }