views:

47

answers:

1

I maintain a small Perl library that extracts width/height from images, for people who want that functionality without using a larger, more generalized library. I've been asked if I can support the Windows EMF format. However, I haven't had much luck with my Google-fu in trying to find a good specification of the format, or better yet example code (in any language). I'm looking for either a decent spec on the format, or examples of reading/parsing the files. As usually, and and all help is greatly appreciated.

+5  A: 

The official specification can be downloaded directly from MSDN at http://msdn.microsoft.com/en-us/library/cc230514(PROT.10).aspx.

It will take some time to read and understand, but it should definitely be doable if you have worked with binary file formats before.

But please notice that EMF is a (pseudo-) vector image format, and so images can be scaled to any size. But there might be a default width and height. In particular, there really should be a well-defined aspect ratio.

Update

I think that the width (in pixels) of the metafile is the 5th cardinal of the file, and the height (in pixels) the 6th cardinal. In a typical case. At least this might be a decent starting point for you.

I just created a sample EMF file, which starts

01 00 00 00 88 00 00 00 00 00 00 00 00 00 00 00
ae 01 00 00 75 01 00 00 00 00 00 00 00 00 00 00

The fifth cardinal is AE010000 which, due to the byte little-endianness, is 000001AE in hex, i.e. 430 in decimal. The sixth cardinal is 75010000, i.e. 00000175 in hex or 373 in decimal. Hence I get the dimension to be 430×373 sq. pixels. Paint reports 432×374 sq. pixels.

If I get more time left, I will study the file format more extensively. But at least I hope this might be a starting point for you.

Update 2

The third and forth 32-bit integers are apparently the left and top coordinates of the image, in logical units, respectively, while the fifth and sixth 32-bit integers are the right and bottom coordinates. In most cases (top, left) = (0, 0), and then my text above is correct (i.e. then width = right, height = top coordinate).

If (top, left) <> (0, 0), then, naturally, width = right - left and height = bottom - top.

Now this is probably not the whole story; if you compare the obtained numbers with the ones reported by Paint you will get small deviations. So To be continued....

Andreas Rejbrand