[GDIPLUS_WINETEST]
[reactos.git] / rostests / winetests / gdiplus / metafile.c
1 /*
2 * Unit test suite for metafiles
3 *
4 * Copyright (C) 2011 Vincent Povirk for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "windows.h"
22 #include <stdio.h>
23 #include "gdiplus.h"
24 #include "wine/test.h"
25
26 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
27
28 typedef struct emfplus_record
29 {
30 ULONG todo;
31 ULONG record_type;
32 ULONG playback_todo;
33 } emfplus_record;
34
35 typedef struct emfplus_check_state
36 {
37 const char *desc;
38 int count;
39 const struct emfplus_record *expected;
40 GpMetafile *metafile;
41 } emfplus_check_state;
42
43 static void check_record(int count, const char *desc, const struct emfplus_record *expected, const struct emfplus_record *actual)
44 {
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);
53 }
54
55 typedef struct EmfPlusRecordHeader
56 {
57 WORD Type;
58 WORD Flags;
59 DWORD Size;
60 DWORD DataSize;
61 } EmfPlusRecordHeader;
62
63 static int CALLBACK enum_emf_proc(HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpEMFR,
64 int nObj, LPARAM lpData)
65 {
66 emfplus_check_state *state = (emfplus_check_state*)lpData;
67 emfplus_record actual;
68
69 if (lpEMFR->iType == EMR_GDICOMMENT)
70 {
71 const EMRGDICOMMENT *comment = (const EMRGDICOMMENT*)lpEMFR;
72
73 if (comment->cbData >= 4 && memcmp(comment->Data, "EMF+", 4) == 0)
74 {
75 int offset = 4;
76
77 while (offset + sizeof(EmfPlusRecordHeader) <= comment->cbData)
78 {
79 const EmfPlusRecordHeader *record = (const EmfPlusRecordHeader*)&comment->Data[offset];
80
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);
83
84 ok(offset + record->DataSize <= comment->cbData,
85 "%s: EMF+ record truncated\n", state->desc);
86
87 if (offset + record->DataSize > comment->cbData)
88 return 0;
89
90 if (state->expected[state->count].record_type)
91 {
92 actual.todo = 0;
93 actual.record_type = record->Type;
94
95 check_record(state->count, state->desc, &state->expected[state->count], &actual);
96
97 state->count++;
98 }
99 else
100 {
101 ok(0, "%s: Unexpected EMF+ 0x%x record\n", state->desc, record->Type);
102 }
103
104 offset += record->Size;
105 }
106
107 ok(offset == comment->cbData, "%s: truncated EMF+ record data?\n", state->desc);
108
109 return 1;
110 }
111 }
112
113 if (state->expected[state->count].record_type)
114 {
115 actual.todo = 0;
116 actual.record_type = lpEMFR->iType;
117
118 check_record(state->count, state->desc, &state->expected[state->count], &actual);
119
120 state->count++;
121 }
122 else
123 {
124 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, lpEMFR->iType);
125 }
126
127 return 1;
128 }
129
130 static void check_emfplus(HENHMETAFILE hemf, const emfplus_record *expected, const char *desc)
131 {
132 emfplus_check_state state;
133
134 state.desc = desc;
135 state.count = 0;
136 state.expected = expected;
137
138 EnumEnhMetaFile(0, hemf, enum_emf_proc, &state, NULL);
139
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);
144 }
145
146 static BOOL CALLBACK enum_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
147 unsigned int dataSize, const unsigned char *pStr, void *userdata)
148 {
149 emfplus_check_state *state = (emfplus_check_state*)userdata;
150 emfplus_record actual;
151
152 actual.todo = 0;
153 actual.record_type = record_type;
154
155 if (dataSize == 0)
156 ok(pStr == NULL, "non-NULL pStr\n");
157
158 if (state->expected[state->count].record_type)
159 {
160 check_record(state->count, state->desc, &state->expected[state->count], &actual);
161
162 state->count++;
163 }
164 else
165 {
166 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, record_type);
167 }
168
169 return TRUE;
170 }
171
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)
174 {
175 GpStatus stat;
176 HDC hdc;
177 GpGraphics *graphics;
178 emfplus_check_state state;
179
180 state.desc = desc;
181 state.count = 0;
182 state.expected = expected;
183 state.metafile = metafile;
184
185 hdc = CreateCompatibleDC(0);
186
187 stat = GdipCreateFromHDC(hdc, &graphics);
188 expect(Ok, stat);
189
190 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
191 3, src_rect, src_unit, enum_metafile_proc, &state, NULL);
192 expect(Ok, stat);
193
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);
198
199 GdipDeleteGraphics(graphics);
200
201 DeleteDC(hdc);
202 }
203
204 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
205 unsigned int dataSize, const unsigned char *pStr, void *userdata)
206 {
207 emfplus_check_state *state = (emfplus_check_state*)userdata;
208 GpStatus stat;
209
210 stat = GdipPlayMetafileRecord(state->metafile, record_type, flags, dataSize, pStr);
211
212 if (state->expected[state->count].record_type)
213 {
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++;
219 }
220 else
221 {
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);
226
227 return FALSE;
228 }
229
230 return TRUE;
231 }
232
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)
235 {
236 GpStatus stat;
237 emfplus_check_state state;
238
239 state.desc = desc;
240 state.count = 0;
241 state.expected = expected;
242 state.metafile = metafile;
243
244 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
245 3, src_rect, src_unit, play_metafile_proc, &state, NULL);
246 expect(Ok, stat);
247 }
248
249 static const emfplus_record empty_records[] = {
250 {0, EMR_HEADER},
251 {0, EmfPlusRecordTypeHeader},
252 {0, EmfPlusRecordTypeEndOfFile},
253 {0, EMR_EOF},
254 {0}
255 };
256
257 static void test_empty(void)
258 {
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};
268
269 hdc = CreateCompatibleDC(0);
270
271 stat = GdipRecordMetafile(NULL, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
272 expect(InvalidParameter, stat);
273
274 stat = GdipRecordMetafile(hdc, MetafileTypeInvalid, &frame, MetafileFrameUnitPixel, description, &metafile);
275 expect(InvalidParameter, stat);
276
277 stat = GdipRecordMetafile(hdc, MetafileTypeWmf, &frame, MetafileFrameUnitPixel, description, &metafile);
278 expect(InvalidParameter, stat);
279
280 stat = GdipRecordMetafile(hdc, MetafileTypeWmfPlaceable, &frame, MetafileFrameUnitPixel, description, &metafile);
281 expect(InvalidParameter, stat);
282
283 stat = GdipRecordMetafile(hdc, MetafileTypeEmfPlusDual+1, &frame, MetafileFrameUnitPixel, description, &metafile);
284 expect(InvalidParameter, stat);
285
286 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, NULL);
287 expect(InvalidParameter, stat);
288
289 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
290 expect(Ok, stat);
291
292 DeleteDC(hdc);
293
294 if (stat != Ok)
295 return;
296
297 stat = GdipGetHemfFromMetafile(metafile, &hemf);
298 expect(InvalidParameter, stat);
299
300 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
301 expect(Ok, stat);
302
303 stat = GdipGetHemfFromMetafile(metafile, &hemf);
304 expect(InvalidParameter, stat);
305
306 stat = GdipDeleteGraphics(graphics);
307 expect(Ok, stat);
308
309 check_metafile(metafile, empty_records, "empty metafile", dst_points, &frame, UnitPixel);
310
311 stat = GdipGetHemfFromMetafile(metafile, &hemf);
312 expect(Ok, stat);
313
314 stat = GdipGetHemfFromMetafile(metafile, &dummy);
315 expect(InvalidParameter, stat);
316
317 stat = GdipDisposeImage((GpImage*)metafile);
318 expect(Ok, stat);
319
320 check_emfplus(hemf, empty_records, "empty emf");
321
322 ret = DeleteEnhMetaFile(hemf);
323 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
324 }
325
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},
337 {0}
338 };
339
340 static void test_getdc(void)
341 {
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;
355
356 hdc = CreateCompatibleDC(0);
357
358 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
359 expect(Ok, stat);
360
361 DeleteDC(hdc);
362
363 if (stat != Ok)
364 return;
365
366 stat = GdipGetHemfFromMetafile(metafile, &hemf);
367 expect(InvalidParameter, stat);
368
369 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
370 expect(Ok, stat);
371
372 stat = GdipGetDC(graphics, &metafile_dc);
373 expect(Ok, stat);
374
375 if (stat != Ok)
376 {
377 GdipDeleteGraphics(graphics);
378 GdipDisposeImage((GpImage*)metafile);
379 return;
380 }
381
382 hbrush = CreateSolidBrush(0xff0000);
383
384 holdbrush = SelectObject(metafile_dc, hbrush);
385
386 Rectangle(metafile_dc, 25, 25, 75, 75);
387
388 SelectObject(metafile_dc, holdbrush);
389
390 DeleteObject(hbrush);
391
392 stat = GdipReleaseDC(graphics, metafile_dc);
393 expect(Ok, stat);
394
395 stat = GdipDeleteGraphics(graphics);
396 expect(Ok, stat);
397
398 check_metafile(metafile, getdc_records, "getdc metafile", dst_points, &frame, UnitPixel);
399
400 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
401 expect(Ok, stat);
402
403 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
404 expect(Ok, stat);
405
406 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points, &frame, UnitPixel);
407
408 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
409 expect(Ok, stat);
410 expect(0, color);
411
412 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
413 expect(Ok, stat);
414 expect(0xff0000ff, color);
415
416 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
417 expect(Ok, stat);
418
419 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points_half, &frame, UnitPixel);
420
421 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
422 expect(Ok, stat);
423 expect(0xff0000ff, color);
424
425 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
426 expect(Ok, stat);
427 expect(0, color);
428
429 stat = GdipDeleteGraphics(graphics);
430 expect(Ok, stat);
431
432 stat = GdipDisposeImage((GpImage*)bitmap);
433 expect(Ok, stat);
434
435 stat = GdipGetHemfFromMetafile(metafile, &hemf);
436 expect(Ok, stat);
437
438 stat = GdipDisposeImage((GpImage*)metafile);
439 expect(Ok, stat);
440
441 check_emfplus(hemf, getdc_records, "getdc emf");
442
443 ret = DeleteEnhMetaFile(hemf);
444 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
445 }
446
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},
455 {0}
456 };
457
458 static void test_emfonly(void)
459 {
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;
472
473 hdc = CreateCompatibleDC(0);
474
475 stat = GdipRecordMetafile(hdc, EmfTypeEmfOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
476 expect(Ok, stat);
477
478 DeleteDC(hdc);
479
480 if (stat != Ok)
481 return;
482
483 stat = GdipGetHemfFromMetafile(metafile, &hemf);
484 expect(InvalidParameter, stat);
485
486 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
487 expect(Ok, stat);
488
489 stat = GdipGetDC(graphics, &metafile_dc);
490 expect(Ok, stat);
491
492 if (stat != Ok)
493 {
494 GdipDeleteGraphics(graphics);
495 GdipDisposeImage((GpImage*)metafile);
496 return;
497 }
498
499 hbrush = CreateSolidBrush(0xff0000);
500
501 holdbrush = SelectObject(metafile_dc, hbrush);
502
503 Rectangle(metafile_dc, 25, 25, 75, 75);
504
505 SelectObject(metafile_dc, holdbrush);
506
507 DeleteObject(hbrush);
508
509 stat = GdipReleaseDC(graphics, metafile_dc);
510 expect(Ok, stat);
511
512 stat = GdipDeleteGraphics(graphics);
513 expect(Ok, stat);
514
515 check_metafile(metafile, emfonly_records, "emfonly metafile", dst_points, &frame, UnitPixel);
516
517 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
518 expect(Ok, stat);
519
520 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
521 expect(Ok, stat);
522
523 play_metafile(metafile, graphics, emfonly_records, "emfonly playback", dst_points, &frame, UnitPixel);
524
525 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
526 expect(Ok, stat);
527 expect(0, color);
528
529 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
530 expect(Ok, stat);
531 expect(0xff0000ff, color);
532
533 stat = GdipDeleteGraphics(graphics);
534 expect(Ok, stat);
535
536 stat = GdipDisposeImage((GpImage*)bitmap);
537 expect(Ok, stat);
538
539 stat = GdipGetHemfFromMetafile(metafile, &hemf);
540 expect(Ok, stat);
541
542 stat = GdipDisposeImage((GpImage*)metafile);
543 expect(Ok, stat);
544
545 check_emfplus(hemf, emfonly_records, "emfonly emf");
546
547 ret = DeleteEnhMetaFile(hemf);
548 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
549 }
550
551 START_TEST(metafile)
552 {
553 struct GdiplusStartupInput gdiplusStartupInput;
554 ULONG_PTR gdiplusToken;
555
556 gdiplusStartupInput.GdiplusVersion = 1;
557 gdiplusStartupInput.DebugEventCallback = NULL;
558 gdiplusStartupInput.SuppressBackgroundThread = 0;
559 gdiplusStartupInput.SuppressExternalCodecs = 0;
560
561 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
562
563 test_empty();
564 test_getdc();
565 test_emfonly();
566
567 GdiplusShutdown(gdiplusToken);
568 }