gdiplus: Implement GdipPlayMetafileRecord for EMF records.
[wine/multimedia.git] / dlls / gdiplus / tests / metafile.c
blob9530b0c540b0a830f45ef39e0f66a0fbf9d36ac7
1 /*
2 * Unit test suite for metafiles
4 * Copyright (C) 2011 Vincent Povirk for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "windows.h"
22 #include <stdio.h>
23 #include "gdiplus.h"
24 #include "wine/test.h"
26 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
28 typedef struct emfplus_record
30 ULONG todo;
31 ULONG record_type;
32 ULONG playback_todo;
33 } emfplus_record;
35 typedef struct emfplus_check_state
37 const char *desc;
38 int count;
39 const struct emfplus_record *expected;
40 GpMetafile *metafile;
41 } emfplus_check_state;
43 static void check_record(int count, const char *desc, const struct emfplus_record *expected, const struct emfplus_record *actual)
45 if (expected->todo)
46 todo_wine ok(expected->record_type == actual->record_type,
47 "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
48 expected->record_type, actual->record_type);
49 else
50 ok(expected->record_type == actual->record_type,
51 "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
52 expected->record_type, actual->record_type);
55 typedef struct EmfPlusRecordHeader
57 WORD Type;
58 WORD Flags;
59 DWORD Size;
60 DWORD DataSize;
61 } EmfPlusRecordHeader;
63 static int CALLBACK enum_emf_proc(HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpEMFR,
64 int nObj, LPARAM lpData)
66 emfplus_check_state *state = (emfplus_check_state*)lpData;
67 emfplus_record actual;
69 if (lpEMFR->iType == EMR_GDICOMMENT)
71 const EMRGDICOMMENT *comment = (const EMRGDICOMMENT*)lpEMFR;
73 if (comment->cbData >= 4 && memcmp(comment->Data, "EMF+", 4) == 0)
75 int offset = 4;
77 while (offset + sizeof(EmfPlusRecordHeader) <= comment->cbData)
79 const EmfPlusRecordHeader *record = (const EmfPlusRecordHeader*)&comment->Data[offset];
81 ok(record->Size == record->DataSize + sizeof(EmfPlusRecordHeader),
82 "%s: EMF+ record datasize %u and size %u mismatch\n", state->desc, record->DataSize, record->Size);
84 ok(offset + record->DataSize <= comment->cbData,
85 "%s: EMF+ record truncated\n", state->desc);
87 if (offset + record->DataSize > comment->cbData)
88 return 0;
90 if (state->expected[state->count].record_type)
92 actual.todo = 0;
93 actual.record_type = record->Type;
95 check_record(state->count, state->desc, &state->expected[state->count], &actual);
97 state->count++;
99 else
101 ok(0, "%s: Unexpected EMF+ 0x%x record\n", state->desc, record->Type);
104 offset += record->Size;
107 ok(offset == comment->cbData, "%s: truncated EMF+ record data?\n", state->desc);
109 return 1;
113 if (state->expected[state->count].record_type)
115 actual.todo = 0;
116 actual.record_type = lpEMFR->iType;
118 check_record(state->count, state->desc, &state->expected[state->count], &actual);
120 state->count++;
122 else
124 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, lpEMFR->iType);
127 return 1;
130 static void check_emfplus(HENHMETAFILE hemf, const emfplus_record *expected, const char *desc)
132 emfplus_check_state state;
134 state.desc = desc;
135 state.count = 0;
136 state.expected = expected;
138 EnumEnhMetaFile(0, hemf, enum_emf_proc, &state, NULL);
140 if (expected[state.count].todo)
141 todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
142 else
143 ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
146 static BOOL CALLBACK enum_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
147 unsigned int dataSize, const unsigned char *pStr, void *userdata)
149 emfplus_check_state *state = (emfplus_check_state*)userdata;
150 emfplus_record actual;
152 actual.todo = 0;
153 actual.record_type = record_type;
155 if (dataSize == 0)
156 ok(pStr == NULL, "non-NULL pStr\n");
158 if (state->expected[state->count].record_type)
160 check_record(state->count, state->desc, &state->expected[state->count], &actual);
162 state->count++;
164 else
166 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, record_type);
169 return TRUE;
172 static void check_metafile(GpMetafile *metafile, const emfplus_record *expected, const char *desc,
173 const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
175 GpStatus stat;
176 HDC hdc;
177 GpGraphics *graphics;
178 emfplus_check_state state;
180 state.desc = desc;
181 state.count = 0;
182 state.expected = expected;
183 state.metafile = metafile;
185 hdc = CreateCompatibleDC(0);
187 stat = GdipCreateFromHDC(hdc, &graphics);
188 expect(Ok, stat);
190 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
191 3, src_rect, src_unit, enum_metafile_proc, &state, NULL);
192 expect(Ok, stat);
194 if (expected[state.count].todo)
195 todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
196 else
197 ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
199 GdipDeleteGraphics(graphics);
201 DeleteDC(hdc);
204 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
205 unsigned int dataSize, const unsigned char *pStr, void *userdata)
207 emfplus_check_state *state = (emfplus_check_state*)userdata;
208 GpStatus stat;
210 stat = GdipPlayMetafileRecord(state->metafile, record_type, flags, dataSize, pStr);
212 if (state->expected[state->count].record_type)
214 if (state->expected[state->count].playback_todo)
215 todo_wine ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
216 else
217 ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
218 state->count++;
220 else
222 if (state->expected[state->count].playback_todo)
223 todo_wine ok(0, "%s: too many records\n", state->desc);
224 else
225 ok(0, "%s: too many records\n", state->desc);
227 return FALSE;
230 return TRUE;
233 static void play_metafile(GpMetafile *metafile, GpGraphics *graphics, const emfplus_record *expected,
234 const char *desc, const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
236 GpStatus stat;
237 emfplus_check_state state;
239 state.desc = desc;
240 state.count = 0;
241 state.expected = expected;
242 state.metafile = metafile;
244 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
245 3, src_rect, src_unit, play_metafile_proc, &state, NULL);
246 expect(Ok, stat);
249 static const emfplus_record empty_records[] = {
250 {0, EMR_HEADER},
251 {0, EmfPlusRecordTypeHeader},
252 {0, EmfPlusRecordTypeEndOfFile},
253 {0, EMR_EOF},
257 static void test_empty(void)
259 GpStatus stat;
260 GpMetafile *metafile;
261 GpGraphics *graphics;
262 HDC hdc;
263 HENHMETAFILE hemf, dummy;
264 BOOL ret;
265 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
266 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
267 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
269 hdc = CreateCompatibleDC(0);
271 stat = GdipRecordMetafile(NULL, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
272 expect(InvalidParameter, stat);
274 stat = GdipRecordMetafile(hdc, MetafileTypeInvalid, &frame, MetafileFrameUnitPixel, description, &metafile);
275 expect(InvalidParameter, stat);
277 stat = GdipRecordMetafile(hdc, MetafileTypeWmf, &frame, MetafileFrameUnitPixel, description, &metafile);
278 expect(InvalidParameter, stat);
280 stat = GdipRecordMetafile(hdc, MetafileTypeWmfPlaceable, &frame, MetafileFrameUnitPixel, description, &metafile);
281 expect(InvalidParameter, stat);
283 stat = GdipRecordMetafile(hdc, MetafileTypeEmfPlusDual+1, &frame, MetafileFrameUnitPixel, description, &metafile);
284 expect(InvalidParameter, stat);
286 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, NULL);
287 expect(InvalidParameter, stat);
289 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
290 expect(Ok, stat);
292 DeleteDC(hdc);
294 if (stat != Ok)
295 return;
297 stat = GdipGetHemfFromMetafile(metafile, &hemf);
298 expect(InvalidParameter, stat);
300 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
301 expect(Ok, stat);
303 stat = GdipGetHemfFromMetafile(metafile, &hemf);
304 expect(InvalidParameter, stat);
306 stat = GdipDeleteGraphics(graphics);
307 expect(Ok, stat);
309 check_metafile(metafile, empty_records, "empty metafile", dst_points, &frame, UnitPixel);
311 stat = GdipGetHemfFromMetafile(metafile, &hemf);
312 expect(Ok, stat);
314 stat = GdipGetHemfFromMetafile(metafile, &dummy);
315 expect(InvalidParameter, stat);
317 stat = GdipDisposeImage((GpImage*)metafile);
318 expect(Ok, stat);
320 check_emfplus(hemf, empty_records, "empty emf");
322 ret = DeleteEnhMetaFile(hemf);
323 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
326 static const emfplus_record getdc_records[] = {
327 {0, EMR_HEADER},
328 {0, EmfPlusRecordTypeHeader},
329 {0, EmfPlusRecordTypeGetDC},
330 {0, EMR_CREATEBRUSHINDIRECT},
331 {0, EMR_SELECTOBJECT},
332 {0, EMR_RECTANGLE},
333 {0, EMR_SELECTOBJECT},
334 {0, EMR_DELETEOBJECT},
335 {0, EmfPlusRecordTypeEndOfFile},
336 {0, EMR_EOF},
340 static void test_getdc(void)
342 GpStatus stat;
343 GpMetafile *metafile;
344 GpGraphics *graphics;
345 HDC hdc, metafile_dc;
346 HENHMETAFILE hemf;
347 BOOL ret;
348 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
349 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
350 static const GpPointF dst_points_half[3] = {{0.0,0.0},{50.0,0.0},{0.0,50.0}};
351 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
352 HBRUSH hbrush, holdbrush;
353 GpBitmap *bitmap;
354 ARGB color;
356 hdc = CreateCompatibleDC(0);
358 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
359 expect(Ok, stat);
361 DeleteDC(hdc);
363 if (stat != Ok)
364 return;
366 stat = GdipGetHemfFromMetafile(metafile, &hemf);
367 expect(InvalidParameter, stat);
369 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
370 expect(Ok, stat);
372 stat = GdipGetDC(graphics, &metafile_dc);
373 expect(Ok, stat);
375 if (stat != Ok)
377 GdipDeleteGraphics(graphics);
378 GdipDisposeImage((GpImage*)metafile);
379 return;
382 hbrush = CreateSolidBrush(0xff0000);
384 holdbrush = SelectObject(metafile_dc, hbrush);
386 Rectangle(metafile_dc, 25, 25, 75, 75);
388 SelectObject(metafile_dc, holdbrush);
390 DeleteObject(hbrush);
392 stat = GdipReleaseDC(graphics, metafile_dc);
393 expect(Ok, stat);
395 stat = GdipDeleteGraphics(graphics);
396 expect(Ok, stat);
398 check_metafile(metafile, getdc_records, "getdc metafile", dst_points, &frame, UnitPixel);
400 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
401 expect(Ok, stat);
403 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
404 expect(Ok, stat);
406 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points, &frame, UnitPixel);
408 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
409 expect(Ok, stat);
410 expect(0, color);
412 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
413 expect(Ok, stat);
414 expect(0xff0000ff, color);
416 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
417 expect(Ok, stat);
419 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points_half, &frame, UnitPixel);
421 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
422 expect(Ok, stat);
423 expect(0xff0000ff, color);
425 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
426 expect(Ok, stat);
427 expect(0, color);
429 stat = GdipDeleteGraphics(graphics);
430 expect(Ok, stat);
432 stat = GdipDisposeImage((GpImage*)bitmap);
433 expect(Ok, stat);
435 stat = GdipGetHemfFromMetafile(metafile, &hemf);
436 expect(Ok, stat);
438 stat = GdipDisposeImage((GpImage*)metafile);
439 expect(Ok, stat);
441 check_emfplus(hemf, getdc_records, "getdc emf");
443 ret = DeleteEnhMetaFile(hemf);
444 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
447 static const emfplus_record emfonly_records[] = {
448 {0, EMR_HEADER},
449 {0, EMR_CREATEBRUSHINDIRECT},
450 {0, EMR_SELECTOBJECT},
451 {0, EMR_RECTANGLE},
452 {0, EMR_SELECTOBJECT},
453 {0, EMR_DELETEOBJECT},
454 {0, EMR_EOF},
458 static void test_emfonly(void)
460 GpStatus stat;
461 GpMetafile *metafile;
462 GpGraphics *graphics;
463 HDC hdc, metafile_dc;
464 HENHMETAFILE hemf;
465 BOOL ret;
466 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
467 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
468 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
469 HBRUSH hbrush, holdbrush;
470 GpBitmap *bitmap;
471 ARGB color;
473 hdc = CreateCompatibleDC(0);
475 stat = GdipRecordMetafile(hdc, EmfTypeEmfOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
476 expect(Ok, stat);
478 DeleteDC(hdc);
480 if (stat != Ok)
481 return;
483 stat = GdipGetHemfFromMetafile(metafile, &hemf);
484 expect(InvalidParameter, stat);
486 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
487 expect(Ok, stat);
489 stat = GdipGetDC(graphics, &metafile_dc);
490 expect(Ok, stat);
492 if (stat != Ok)
494 GdipDeleteGraphics(graphics);
495 GdipDisposeImage((GpImage*)metafile);
496 return;
499 hbrush = CreateSolidBrush(0xff0000);
501 holdbrush = SelectObject(metafile_dc, hbrush);
503 Rectangle(metafile_dc, 25, 25, 75, 75);
505 SelectObject(metafile_dc, holdbrush);
507 DeleteObject(hbrush);
509 stat = GdipReleaseDC(graphics, metafile_dc);
510 expect(Ok, stat);
512 stat = GdipDeleteGraphics(graphics);
513 expect(Ok, stat);
515 check_metafile(metafile, emfonly_records, "emfonly metafile", dst_points, &frame, UnitPixel);
517 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
518 expect(Ok, stat);
520 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
521 expect(Ok, stat);
523 play_metafile(metafile, graphics, emfonly_records, "emfonly playback", dst_points, &frame, UnitPixel);
525 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
526 expect(Ok, stat);
527 expect(0, color);
529 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
530 expect(Ok, stat);
531 expect(0xff0000ff, color);
533 stat = GdipDeleteGraphics(graphics);
534 expect(Ok, stat);
536 stat = GdipDisposeImage((GpImage*)bitmap);
537 expect(Ok, stat);
539 stat = GdipGetHemfFromMetafile(metafile, &hemf);
540 expect(Ok, stat);
542 stat = GdipDisposeImage((GpImage*)metafile);
543 expect(Ok, stat);
545 check_emfplus(hemf, emfonly_records, "emfonly emf");
547 ret = DeleteEnhMetaFile(hemf);
548 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
551 START_TEST(metafile)
553 struct GdiplusStartupInput gdiplusStartupInput;
554 ULONG_PTR gdiplusToken;
556 gdiplusStartupInput.GdiplusVersion = 1;
557 gdiplusStartupInput.DebugEventCallback = NULL;
558 gdiplusStartupInput.SuppressBackgroundThread = 0;
559 gdiplusStartupInput.SuppressExternalCodecs = 0;
561 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
563 test_empty();
564 test_getdc();
565 test_emfonly();
567 GdiplusShutdown(gdiplusToken);