/
wmfcodec.c
391 lines (361 loc) · 11.5 KB
/
wmfcodec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright (C) 2007 Novell, Inc (http://www.novell.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Sebastien Pouliot <sebastien@ximian.com>
*/
#include "wmfcodec.h"
/* Codecinfo related data*/
GUID gdip_wmf_image_format_guid = {0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e}};
static ImageCodecInfo wmf_codec;
static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W', 'M', 'F', 0}; /* Built-in WMF */
static const WCHAR wmf_extension[] = {'*','.','W', 'M', 'F', 0}; /* *.WMF */
static const WCHAR wmf_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'x', '-', 'w', 'm', 'f', 0}; /* image/x-wmf */
static const WCHAR wmf_format[] = {'W', 'M', 'F', 0}; /* WMF */
static const BYTE wmf_sig_pattern[] = { 0xD7, 0xCD, 0xC6, 0x9A };
static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
//#define DEBUG_WMF_ALL
#ifdef DEBUG_WMF_ALL
#define DEBUG_WMF
#define DEBUG_WMF_2
#define DEBUG_WMF_3
#define DEBUG_WMF_NOTIMPLEMENTED
#endif
ImageCodecInfo*
gdip_getcodecinfo_wmf ()
{
wmf_codec.Clsid = (CLSID) { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } };
wmf_codec.FormatID = gdip_wmf_image_format_guid;
wmf_codec.CodecName = (const WCHAR*) wmf_codecname;
wmf_codec.DllName = NULL;
wmf_codec.FormatDescription = (const WCHAR*) wmf_format;
wmf_codec.FilenameExtension = (const WCHAR*) wmf_extension;
wmf_codec.MimeType = (const WCHAR*) wmf_mimetype;
wmf_codec.Flags = ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin;
wmf_codec.Version = 1;
wmf_codec.SigCount = 1;
wmf_codec.SigSize = 4;
wmf_codec.SigPattern = wmf_sig_pattern;
wmf_codec.SigMask = wmf_sig_mask;
return &wmf_codec;
}
static WORD
GetWORD (int position, BYTE *data)
{
WORD *value = (WORD*)(data + position);
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
return GUINT16_FROM_LE (*value);
#else
return *value;
#endif
}
static DWORD
GetDWORD (int position, BYTE* data)
{
DWORD *value = (DWORD*)(data + position);
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
return GUINT32_FROM_LE (*value);
#else
return *value;
#endif
}
static DWORD
GetColor (WORD w1, WORD w2)
{
DWORD color = (w2 << 16) | w1;
BYTE *p = (BYTE*)&color;
BYTE temp = p[0];
p[0] = p[2];
p[2] = temp;
return color;
}
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GETW(x) (*(WORD*)(data + (x)))
#else
#define GETW(x) (GUINT16_FROM_LE(*(WORD*)(data + (x))))
#endif
#define GetParam(x,y) GetWORD((6 + ((x) << 1)), (y))
/* http://wvware.sourceforge.net/caolan/Polygon.html */
static GpStatus
Polygon (MetafilePlayContext *context, BYTE *data, int len)
{
GpPointF *points, *pt;
GpStatus status;
int p;
/* variable number of parameters */
SHORT num = GETW(WP1);
/* len (in WORDs) = num (WORD) + num * (x WORD + y WORD) */
if (num > len + 1)
return InvalidParameter;
#ifdef DEBUG_WMF
printf ("Polygon %d points", num);
#endif
points = (GpPointF*) GdipAlloc (num * sizeof (GpPointF));
if (!points)
return OutOfMemory;
int n = 2;
for (p = 0, pt = points; p < num; p++, pt++) {
pt->X = GETW(WP(n));
n++;
pt->Y = GETW(WP(n));
n++;
#ifdef DEBUG_WMF
printf ("\n\tpoly to %g,%g", pt->X, pt->Y);
#endif
}
status = gdip_metafile_Polygon (context, points, num);
GdipFree (points);
return status;
}
/* http://wvware.sourceforge.net/caolan/Polyline.html */
static GpStatus
Polyline (MetafilePlayContext *context, BYTE *data)
{
GpStatus status;
int p;
/* variable number of parameters */
SHORT num = GETW(WP1);
#ifdef DEBUG_WMF
printf ("Polyline %d points", num);
#endif
SHORT x1 = GETW(WP2);
SHORT y1 = GETW(WP3);
int n = 4;
for (p = 1; p < num; p++) {
SHORT x2 = GETW(WP(n));
n++;
SHORT y2 = GETW(WP(n));
n++;
#ifdef DEBUG_WMF_2
printf ("\n\tdraw from %d,%d to %d,%d", x1, y1, x2, y2);
#endif
GpPen *pen = gdip_metafile_GetSelectedPen (context);
status = GdipDrawLine (context->graphics, pen, x1, y1, x2, y2);
if (status != Ok)
return status;
x1 = x2;
y1 = y2;
}
return Ok;
}
/* http://wvware.sourceforge.net/caolan/PolyPolygon.html */
/* storage isn't very efficient, # of polygons, size of each polygon, data for each polygon */
static GpStatus
PolyPolygon (MetafilePlayContext *context, BYTE *data)
{
GpStatus status = Ok;
/* variable number of parameters */
int poly_num = GETW(WP1);
int i;
PointFList *list = GdipAlloc (poly_num * sizeof (PointFList));
PointFList *current = list;
#ifdef DEBUG_WMF
printf ("PolyPolygon has %d polygons", poly_num);
#endif
int n = 2;
/* read size of each polygon and allocate the required memory */
for (i = 0; i < poly_num; i++) {
current->num = GETW(WP(n));
n++;
current->points = (GpPointF*) GdipAlloc (current->num * sizeof (GpPointF));
#ifdef DEBUG_WMF_2
printf ("\n\tSub Polygon #%d has %d points", i, current->num);
#endif
current++;
}
/* read the points for each polygons */
current = list;
for (i = 0; i < poly_num; i++) {
GpPointF *pt = current->points;
int p;
for (p = 0; p < current->num; p++) {
pt->X = GETW(WP(n));
n++;
pt->Y = GETW(WP(n));
n++;
#ifdef DEBUG_WMF_3
printf ("\n\t\tpoly to %g,%g", pt->X, pt->Y);
#endif
pt++;
}
GpStatus s = gdip_metafile_Polygon (context, current->points, current->num);
if (s != Ok)
status = s;
/* free points */
GdipFree (current->points);
current++;
}
/* all points were freed, after being drawn, so we just have to free the polygon list*/
GdipFree (list);
return status;
}
GpStatus
gdip_metafile_play_wmf (MetafilePlayContext *context)
{
GpStatus status = Ok;
GpMetafile *metafile = context->metafile;
GpGraphics *graphics = context->graphics;
BYTE *data = metafile->data;
BYTE *end = data + metafile->length;
#ifdef DEBUG_WMF
int i = 1, j;
#endif
/* reality check - each record is, at minimum, 6 bytes long (4 size + 2 function) */
while (data < end - WMF_MIN_RECORD_SIZE) {
DWORD size = GETDW(RECORDSIZE);
WORD func = GETW(FUNCTION);
int params = size - (WMF_MIN_RECORD_SIZE / sizeof (WORD));
#ifdef DEBUG_WMF
printf ("\n[#%d] size %d ", i++, size);
#endif
/* reality check - enough data available to read all parameters ? (params is in WORD) */
if ((params << 1) > (end - data)) {
status = InvalidParameter;
goto cleanup;
}
/* Notes:
* - The previous check doesn't mean we have all required parameters (only the one encoded)
* - sometimes there are extra (undocumented?, buggy?) parameters for some functions
*/
switch (func) {
case METAFILE_RECORD_SAVEDC:
WMF_CHECK_PARAMS(0);
status = gdip_metafile_SaveDC (context);
break;
case METAFILE_RECORD_SETBKMODE:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetBkMode (context, GETW(WP1));
break;
case METAFILE_RECORD_SETMAPMODE:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetMapMode (context, GETW(WP1));
break;
case METAFILE_RECORD_SETROP2:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetROP2 (context, GETW(WP1));
break;
case METAFILE_RECORD_SETRELABS:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetRelabs (context, GETW(WP1));
break;
case METAFILE_RECORD_SETPOLYFILLMODE:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetPolyFillMode (context, GETW(WP1));
break;
case METAFILE_RECORD_SETSTRETCHBLTMODE:
WMF_CHECK_PARAMS(1); /* 2 but second is unused (32bits?) */
status = gdip_metafile_SetStretchBltMode (context, GETW(WP1));
break;
case METAFILE_RECORD_RESTOREDC:
WMF_CHECK_PARAMS(0);
status = gdip_metafile_RestoreDC (context);
break;
case METAFILE_RECORD_SELECTOBJECT:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SelectObject (context, GETW(WP1));
break;
case METAFILE_RECORD_SETTEXTALIGN:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_SetTextAlign (context, GETW(WP1));
break;
case METAFILE_RECORD_DELETEOBJECT:
WMF_CHECK_PARAMS(1);
status = gdip_metafile_DeleteObject (context, GETW(WP1));
break;
case METAFILE_RECORD_SETBKCOLOR:
WMF_CHECK_PARAMS(2);
status = gdip_metafile_SetBkColor (context, GetColor (GETW(WP1), GETW(WP2)));
break;
case METAFILE_RECORD_SETWINDOWORG:
WMF_CHECK_PARAMS(2);
status = gdip_metafile_SetWindowOrg (context, GETW(WP1), GETW(WP2));
break;
case METAFILE_RECORD_SETWINDOWEXT:
WMF_CHECK_PARAMS(2);
status = gdip_metafile_SetWindowExt (context, GETW(WP1), GETW(WP2));
break;
case METAFILE_RECORD_LINETO:
WMF_CHECK_PARAMS(2);
status = gdip_metafile_LineTo (context, GETW(WP1), GETW(WP2));
break;
case METAFILE_RECORD_MOVETO:
WMF_CHECK_PARAMS(2);
status = gdip_metafile_MoveTo (context, GETW(WP1), GETW(WP2));
break;
case METAFILE_RECORD_CREATEPENINDIRECT:
/* note: documented with only 4 parameters, LOGPEN use a POINT to specify width, so y (3) is unused) */
WMF_CHECK_PARAMS(5);
status = gdip_metafile_CreatePenIndirect (context, GETW(WP1), GETW(WP2), GetColor (GETW(WP4), GETW(WP5)));
break;
case METAFILE_RECORD_CREATEBRUSHINDIRECT:
WMF_CHECK_PARAMS(4);
status = gdip_metafile_CreateBrushIndirect (context, GETW(WP1), GetColor (GETW(WP2), GETW(WP3)), GETW(WP4));
break;
case METAFILE_RECORD_POLYGON:
status = Polygon (context, data, params);
break;
case METAFILE_RECORD_POLYLINE:
status = Polyline (context, data);
break;
case METAFILE_RECORD_POLYPOLYGON:
status = PolyPolygon (context, data);
break;
case METAFILE_RECORD_ARC:
WMF_CHECK_PARAMS(8);
status = gdip_metafile_Arc (context, GETW(WP1), GETW(WP2), GETW(WP3), GETW(WP4), GETW(WP5), GETW(WP6),
GETW(WP7), GETW(WP8));
break;
case METAFILE_RECORD_STRETCHDIBITS: {
WMF_CHECK_PARAMS(14);
BITMAPINFO *bmi = (BITMAPINFO*) (data + 14 * sizeof (WORD));
void* bits = (void*) (bmi + GETDW(WP12));
status = gdip_metafile_StretchDIBits (context, GETW(WP11), GETW(WP10), GETW(WP9), GETW(WP8), GETW(WP7),
GETW(WP6), GETW(WP5), GETW(WP4), bits, bmi, GETW(WP3), GETDW(WP1));
break;
}
default:
/* unprocessed records, ignore the data */
/* 3 for size (DWORD) == 2 * SHORT + function == 1 SHORT */
#ifdef DEBUG_WMF
printf ("Unimplemented_%X (", func);
for (j = 0; j < params; j++) {
printf (" %d", GetParam (j, data));
}
printf (" )");
#endif
break;
}
if (status != Ok) {
g_warning ("Parsing interupted, status %d returned from function %d.", status, func);
goto cleanup;
}
data += size * 2;
}
cleanup:
return status;
}
GpStatus
gdip_load_wmf_image_from_file (FILE *fp, GpImage **image)
{
return gdip_get_metafile_from ((void*)fp, (GpMetafile**)image, File);
}
GpStatus
gdip_load_wmf_image_from_stream_delegate (dstream_t *loader, GpImage **image)
{
return gdip_get_metafile_from ((void *)loader, (GpMetafile**)image, DStream);
}