emf.c 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  Dump an Enhanced Meta File
 *
 *  Copyright 2005 Mike McCormack
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

21 22
#include "config.h"
#include "wine/port.h"
Mike McCormack's avatar
Mike McCormack committed
23
#include "winedump.h"
24

25
#include <stdio.h>
26 27 28 29 30 31
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
32 33 34 35 36 37 38 39 40
#include <fcntl.h>
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"

static unsigned int read_int(unsigned char *buffer)
{
41
    return buffer[0]
42 43 44 45 46 47 48 49 50 51
     + (buffer[1]<<8)
     + (buffer[2]<<16)
     + (buffer[3]<<24);
}

#define EMRCASE(x) case x: printf("%-20s %08x\n", #x, length); break

static int dump_emfrecord(int fd)
{
    unsigned char buffer[8];
52 53
    int r;
    unsigned int type, length, i;
54 55 56 57 58

    r = read(fd, buffer, 8);
    if(r!=8)
        return -1;

59 60
    type = read_int(buffer);
    length = read_int(buffer+4);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    switch(type)
    {
    EMRCASE(EMR_HEADER);
    EMRCASE(EMR_POLYGON);
    EMRCASE(EMR_POLYLINE);
    EMRCASE(EMR_SETWINDOWEXTEX);
    EMRCASE(EMR_SETWINDOWORGEX);
    EMRCASE(EMR_SETVIEWPORTEXTEX);
    EMRCASE(EMR_EOF);
    EMRCASE(EMR_SETMAPMODE);
    EMRCASE(EMR_SETPOLYFILLMODE);
    EMRCASE(EMR_SETROP2);
    EMRCASE(EMR_SCALEWINDOWEXTEX);
    EMRCASE(EMR_SAVEDC);
    EMRCASE(EMR_SELECTOBJECT);
    EMRCASE(EMR_CREATEPEN);
    EMRCASE(EMR_CREATEBRUSHINDIRECT);
    EMRCASE(EMR_DELETEOBJECT);
    EMRCASE(EMR_RECTANGLE);
    EMRCASE(EMR_SELECTPALETTE);
    EMRCASE(EMR_GDICOMMENT);
    EMRCASE(EMR_EXTSELECTCLIPRGN);
    EMRCASE(EMR_EXTCREATEFONTINDIRECTW);
    EMRCASE(EMR_EXTTEXTOUTW);
    EMRCASE(EMR_POLYGON16);
    EMRCASE(EMR_POLYLINE16);

    default:
        printf("%08x %08x\n",type,length);
        break;
    }

    if ( (length < 8) || (length % 4) )
        return -1;

    length -= 8;

    for(i=0; i<length; i+=4)
    {
         if (i%16 == 0)
             printf("   ");
         memset(buffer,0,sizeof buffer);
         r = read(fd,buffer,4);
         if(r!=4)
             return -1;
107
         printf("%08x ", read_int(buffer));
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
         if ( (i%16 == 12) || ((i+4)==length) )
             printf("\n");
    }

    return 0;
}

static int dump_emf_records(int fd)
{
    while(!dump_emfrecord(fd))
        ;
    return 0;
}

int dump_emf(const char *emf)
{
    int fd;

    fd = open(emf,O_RDONLY);
    if (fd<0) return -1;
    dump_emf_records(fd);
    close(fd);
    return 0;
}