[GDIPLUS_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[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 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <math.h>
26
27 #include <wine/test.h>
28 #include <wingdi.h>
29 #include <objbase.h>
30 #include <gdiplus.h>
31
32 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
33 #define expectf_(expected, got, precision) ok(fabs((expected) - (got)) <= (precision), "Expected %f, got %f\n", (expected), (got))
34 #define expectf(expected, got) expectf_((expected), (got), 0.001)
35
36 static BOOL save_metafiles;
37
38 typedef struct emfplus_record
39 {
40 BOOL todo;
41 ULONG record_type;
42 BOOL playback_todo;
43 } emfplus_record;
44
45 typedef struct emfplus_check_state
46 {
47 const char *desc;
48 int count;
49 const struct emfplus_record *expected;
50 GpMetafile *metafile;
51 } emfplus_check_state;
52
53 static void check_record(int count, const char *desc, const struct emfplus_record *expected, const struct emfplus_record *actual)
54 {
55 if (expected->todo)
56 todo_wine ok(expected->record_type == actual->record_type,
57 "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
58 expected->record_type, actual->record_type);
59 else
60 ok(expected->record_type == actual->record_type,
61 "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
62 expected->record_type, actual->record_type);
63 }
64
65 typedef struct EmfPlusRecordHeader
66 {
67 WORD Type;
68 WORD Flags;
69 DWORD Size;
70 DWORD DataSize;
71 } EmfPlusRecordHeader;
72
73 static int CALLBACK enum_emf_proc(HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpEMFR,
74 int nObj, LPARAM lpData)
75 {
76 emfplus_check_state *state = (emfplus_check_state*)lpData;
77 emfplus_record actual;
78
79 if (lpEMFR->iType == EMR_GDICOMMENT)
80 {
81 const EMRGDICOMMENT *comment = (const EMRGDICOMMENT*)lpEMFR;
82
83 if (comment->cbData >= 4 && memcmp(comment->Data, "EMF+", 4) == 0)
84 {
85 int offset = 4;
86
87 while (offset + sizeof(EmfPlusRecordHeader) <= comment->cbData)
88 {
89 const EmfPlusRecordHeader *record = (const EmfPlusRecordHeader*)&comment->Data[offset];
90
91 ok(record->Size == record->DataSize + sizeof(EmfPlusRecordHeader),
92 "%s: EMF+ record datasize %u and size %u mismatch\n", state->desc, record->DataSize, record->Size);
93
94 ok(offset + record->DataSize <= comment->cbData,
95 "%s: EMF+ record truncated\n", state->desc);
96
97 if (offset + record->DataSize > comment->cbData)
98 return 0;
99
100 if (state->expected[state->count].record_type)
101 {
102 actual.todo = FALSE;
103 actual.record_type = record->Type;
104
105 check_record(state->count, state->desc, &state->expected[state->count], &actual);
106
107 state->count++;
108 }
109 else
110 {
111 ok(0, "%s: Unexpected EMF+ 0x%x record\n", state->desc, record->Type);
112 }
113
114 offset += record->Size;
115 }
116
117 ok(offset == comment->cbData, "%s: truncated EMF+ record data?\n", state->desc);
118
119 return 1;
120 }
121 }
122
123 if (state->expected[state->count].record_type)
124 {
125 actual.todo = FALSE;
126 actual.record_type = lpEMFR->iType;
127
128 check_record(state->count, state->desc, &state->expected[state->count], &actual);
129
130 state->count++;
131 }
132 else
133 {
134 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, lpEMFR->iType);
135 }
136
137 return 1;
138 }
139
140 static void check_emfplus(HENHMETAFILE hemf, const emfplus_record *expected, const char *desc)
141 {
142 emfplus_check_state state;
143
144 state.desc = desc;
145 state.count = 0;
146 state.expected = expected;
147
148 EnumEnhMetaFile(0, hemf, enum_emf_proc, &state, NULL);
149
150 if (expected[state.count].todo)
151 todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
152 else
153 ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
154 }
155
156 static BOOL CALLBACK enum_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
157 unsigned int dataSize, const unsigned char *pStr, void *userdata)
158 {
159 emfplus_check_state *state = (emfplus_check_state*)userdata;
160 emfplus_record actual;
161
162 actual.todo = FALSE;
163 actual.record_type = record_type;
164
165 if (dataSize == 0)
166 ok(pStr == NULL, "non-NULL pStr\n");
167
168 if (state->expected[state->count].record_type)
169 {
170 check_record(state->count, state->desc, &state->expected[state->count], &actual);
171
172 state->count++;
173 }
174 else
175 {
176 ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, record_type);
177 }
178
179 return TRUE;
180 }
181
182 static void check_metafile(GpMetafile *metafile, const emfplus_record *expected, const char *desc,
183 const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
184 {
185 GpStatus stat;
186 HDC hdc;
187 GpGraphics *graphics;
188 emfplus_check_state state;
189
190 state.desc = desc;
191 state.count = 0;
192 state.expected = expected;
193 state.metafile = metafile;
194
195 hdc = CreateCompatibleDC(0);
196
197 stat = GdipCreateFromHDC(hdc, &graphics);
198 expect(Ok, stat);
199
200 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
201 3, src_rect, src_unit, enum_metafile_proc, &state, NULL);
202 expect(Ok, stat);
203
204 if (expected[state.count].todo)
205 todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
206 else
207 ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
208
209 GdipDeleteGraphics(graphics);
210
211 DeleteDC(hdc);
212 }
213
214 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
215 unsigned int dataSize, const unsigned char *pStr, void *userdata)
216 {
217 emfplus_check_state *state = (emfplus_check_state*)userdata;
218 GpStatus stat;
219
220 stat = GdipPlayMetafileRecord(state->metafile, record_type, flags, dataSize, pStr);
221
222 if (state->expected[state->count].record_type)
223 {
224 if (state->expected[state->count].playback_todo)
225 todo_wine ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
226 else
227 ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
228 state->count++;
229 }
230 else
231 {
232 if (state->expected[state->count].playback_todo)
233 todo_wine ok(0, "%s: too many records\n", state->desc);
234 else
235 ok(0, "%s: too many records\n", state->desc);
236
237 return FALSE;
238 }
239
240 return TRUE;
241 }
242
243 static void play_metafile(GpMetafile *metafile, GpGraphics *graphics, const emfplus_record *expected,
244 const char *desc, const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
245 {
246 GpStatus stat;
247 emfplus_check_state state;
248
249 state.desc = desc;
250 state.count = 0;
251 state.expected = expected;
252 state.metafile = metafile;
253
254 stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
255 3, src_rect, src_unit, play_metafile_proc, &state, NULL);
256 expect(Ok, stat);
257 }
258
259 static void save_metafile(GpMetafile *metafile, const char *filename)
260 {
261 if (save_metafiles)
262 {
263 GpMetafile *clone;
264 HENHMETAFILE hemf;
265 GpStatus stat;
266
267 stat = GdipCloneImage((GpImage*)metafile, (GpImage**)&clone);
268 expect(Ok, stat);
269
270 stat = GdipGetHemfFromMetafile(clone, &hemf);
271 expect(Ok, stat);
272
273 DeleteEnhMetaFile(CopyEnhMetaFileA(hemf, filename));
274
275 DeleteEnhMetaFile(hemf);
276
277 stat = GdipDisposeImage((GpImage*)clone);
278 expect(Ok, stat);
279 }
280 }
281
282 static const emfplus_record empty_records[] = {
283 {0, EMR_HEADER},
284 {0, EmfPlusRecordTypeHeader},
285 {0, EmfPlusRecordTypeEndOfFile},
286 {0, EMR_EOF},
287 {0}
288 };
289
290 static void test_empty(void)
291 {
292 GpStatus stat;
293 GpMetafile *metafile;
294 GpGraphics *graphics;
295 HDC hdc;
296 GpRectF bounds;
297 GpUnit unit;
298 REAL xres, yres;
299 HENHMETAFILE hemf, dummy;
300 MetafileHeader header;
301 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
302 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
303 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
304
305 hdc = CreateCompatibleDC(0);
306
307 stat = GdipRecordMetafile(NULL, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
308 expect(InvalidParameter, stat);
309
310 stat = GdipRecordMetafile(hdc, MetafileTypeInvalid, &frame, MetafileFrameUnitPixel, description, &metafile);
311 expect(InvalidParameter, stat);
312
313 stat = GdipRecordMetafile(hdc, MetafileTypeWmf, &frame, MetafileFrameUnitPixel, description, &metafile);
314 expect(InvalidParameter, stat);
315
316 stat = GdipRecordMetafile(hdc, MetafileTypeWmfPlaceable, &frame, MetafileFrameUnitPixel, description, &metafile);
317 expect(InvalidParameter, stat);
318
319 stat = GdipRecordMetafile(hdc, MetafileTypeEmfPlusDual+1, &frame, MetafileFrameUnitPixel, description, &metafile);
320 expect(InvalidParameter, stat);
321
322 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, NULL);
323 expect(InvalidParameter, stat);
324
325 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
326 expect(Ok, stat);
327
328 DeleteDC(hdc);
329
330 if (stat != Ok)
331 return;
332
333 stat = GdipGetHemfFromMetafile(metafile, &hemf);
334 expect(InvalidParameter, stat);
335
336 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
337 expect(Ok, stat);
338
339 stat = GdipGetHemfFromMetafile(metafile, &hemf);
340 expect(InvalidParameter, stat);
341
342 stat = GdipDeleteGraphics(graphics);
343 expect(Ok, stat);
344
345 check_metafile(metafile, empty_records, "empty metafile", dst_points, &frame, UnitPixel);
346
347 save_metafile(metafile, "empty.emf");
348
349 stat = GdipGetImageBounds((GpImage*)metafile, &bounds, &unit);
350 expect(Ok, stat);
351 expectf(0.0, bounds.X);
352 expectf(0.0, bounds.Y);
353 expectf_(100.0, bounds.Width, 0.05);
354 expectf_(100.0, bounds.Height, 0.05);
355 expect(UnitPixel, unit);
356
357 stat = GdipGetImageHorizontalResolution((GpImage*)metafile, &xres);
358 expect(Ok, stat);
359
360 stat = GdipGetImageVerticalResolution((GpImage*)metafile, &yres);
361 expect(Ok, stat);
362
363 stat = GdipGetHemfFromMetafile(metafile, &hemf);
364 expect(Ok, stat);
365
366 stat = GdipGetHemfFromMetafile(metafile, &dummy);
367 expect(InvalidParameter, stat);
368
369 stat = GdipDisposeImage((GpImage*)metafile);
370 expect(Ok, stat);
371
372 check_emfplus(hemf, empty_records, "empty emf");
373
374 memset(&header, 0xaa, sizeof(header));
375 stat = GdipGetMetafileHeaderFromEmf(hemf, &header);
376 expect(Ok, stat);
377 expect(MetafileTypeEmfPlusOnly, header.Type);
378 expect(U(header).EmfHeader.nBytes, header.Size);
379 ok(header.Version == 0xdbc01001 || header.Version == 0xdbc01002, "Unexpected version %x\n", header.Version);
380 expect(1, header.EmfPlusFlags); /* reference device was display, not printer */
381 expectf(xres, header.DpiX);
382 expectf(xres, U(header).EmfHeader.szlDevice.cx / (REAL)U(header).EmfHeader.szlMillimeters.cx * 25.4);
383 expectf(yres, header.DpiY);
384 expectf(yres, U(header).EmfHeader.szlDevice.cy / (REAL)U(header).EmfHeader.szlMillimeters.cy * 25.4);
385 expect(0, header.X);
386 expect(0, header.Y);
387 expect(100, header.Width);
388 expect(100, header.Height);
389 expect(28, header.EmfPlusHeaderSize);
390 expect(96, header.LogicalDpiX);
391 expect(96, header.LogicalDpiX);
392 expect(EMR_HEADER, U(header).EmfHeader.iType);
393 expect(0, U(header).EmfHeader.rclBounds.left);
394 expect(0, U(header).EmfHeader.rclBounds.top);
395 expect(-1, U(header).EmfHeader.rclBounds.right);
396 expect(-1, U(header).EmfHeader.rclBounds.bottom);
397 expect(0, U(header).EmfHeader.rclFrame.left);
398 expect(0, U(header).EmfHeader.rclFrame.top);
399 expectf_(100.0, U(header).EmfHeader.rclFrame.right * xres / 2540.0, 2.0);
400 expectf_(100.0, U(header).EmfHeader.rclFrame.bottom * yres / 2540.0, 2.0);
401
402 stat = GdipCreateMetafileFromEmf(hemf, TRUE, &metafile);
403 expect(Ok, stat);
404
405 stat = GdipGetImageBounds((GpImage*)metafile, &bounds, &unit);
406 expect(Ok, stat);
407 expectf(0.0, bounds.X);
408 expectf(0.0, bounds.Y);
409 expectf_(100.0, bounds.Width, 0.05);
410 expectf_(100.0, bounds.Height, 0.05);
411 expect(UnitPixel, unit);
412
413 stat = GdipGetImageHorizontalResolution((GpImage*)metafile, &xres);
414 expect(Ok, stat);
415 expectf(header.DpiX, xres);
416
417 stat = GdipGetImageVerticalResolution((GpImage*)metafile, &yres);
418 expect(Ok, stat);
419 expectf(header.DpiY, yres);
420
421 stat = GdipDisposeImage((GpImage*)metafile);
422 expect(Ok, stat);
423 }
424
425 static const emfplus_record getdc_records[] = {
426 {0, EMR_HEADER},
427 {0, EmfPlusRecordTypeHeader},
428 {0, EmfPlusRecordTypeGetDC},
429 {0, EMR_CREATEBRUSHINDIRECT},
430 {0, EMR_SELECTOBJECT},
431 {0, EMR_RECTANGLE},
432 {0, EMR_SELECTOBJECT},
433 {0, EMR_DELETEOBJECT},
434 {0, EmfPlusRecordTypeEndOfFile},
435 {0, EMR_EOF},
436 {0}
437 };
438
439 static void test_getdc(void)
440 {
441 GpStatus stat;
442 GpMetafile *metafile;
443 GpGraphics *graphics;
444 HDC hdc, metafile_dc;
445 HENHMETAFILE hemf;
446 BOOL ret;
447 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
448 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
449 static const GpPointF dst_points_half[3] = {{0.0,0.0},{50.0,0.0},{0.0,50.0}};
450 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
451 HBRUSH hbrush, holdbrush;
452 GpBitmap *bitmap;
453 ARGB color;
454
455 hdc = CreateCompatibleDC(0);
456
457 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
458 expect(Ok, stat);
459
460 DeleteDC(hdc);
461
462 if (stat != Ok)
463 return;
464
465 stat = GdipGetHemfFromMetafile(metafile, &hemf);
466 expect(InvalidParameter, stat);
467
468 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
469 expect(Ok, stat);
470
471 stat = GdipGetDC(graphics, &metafile_dc);
472 expect(Ok, stat);
473
474 if (stat != Ok)
475 {
476 GdipDeleteGraphics(graphics);
477 GdipDisposeImage((GpImage*)metafile);
478 return;
479 }
480
481 hbrush = CreateSolidBrush(0xff0000);
482
483 holdbrush = SelectObject(metafile_dc, hbrush);
484
485 Rectangle(metafile_dc, 25, 25, 75, 75);
486
487 SelectObject(metafile_dc, holdbrush);
488
489 DeleteObject(hbrush);
490
491 stat = GdipReleaseDC(graphics, metafile_dc);
492 expect(Ok, stat);
493
494 stat = GdipDeleteGraphics(graphics);
495 expect(Ok, stat);
496
497 check_metafile(metafile, getdc_records, "getdc metafile", dst_points, &frame, UnitPixel);
498
499 save_metafile(metafile, "getdc.emf");
500
501 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
502 expect(Ok, stat);
503
504 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
505 expect(Ok, stat);
506
507 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points, &frame, UnitPixel);
508
509 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
510 expect(Ok, stat);
511 expect(0, color);
512
513 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
514 expect(Ok, stat);
515 expect(0xff0000ff, color);
516
517 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
518 expect(Ok, stat);
519
520 play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points_half, &frame, UnitPixel);
521
522 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
523 expect(Ok, stat);
524 expect(0xff0000ff, color);
525
526 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
527 expect(Ok, stat);
528 expect(0, color);
529
530 stat = GdipBitmapSetPixel(bitmap, 15, 15, 0);
531 expect(Ok, stat);
532
533 stat = GdipDrawImagePointsRect(graphics, (GpImage*)metafile, dst_points, 3,
534 0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
535 expect(Ok, stat);
536
537 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
538 expect(Ok, stat);
539 expect(0, color);
540
541 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
542 expect(Ok, stat);
543 expect(0xff0000ff, color);
544
545 stat = GdipDeleteGraphics(graphics);
546 expect(Ok, stat);
547
548 stat = GdipDisposeImage((GpImage*)bitmap);
549 expect(Ok, stat);
550
551 stat = GdipGetHemfFromMetafile(metafile, &hemf);
552 expect(Ok, stat);
553
554 stat = GdipDisposeImage((GpImage*)metafile);
555 expect(Ok, stat);
556
557 check_emfplus(hemf, getdc_records, "getdc emf");
558
559 ret = DeleteEnhMetaFile(hemf);
560 ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
561 }
562
563 static const emfplus_record emfonly_records[] = {
564 {0, EMR_HEADER},
565 {0, EMR_CREATEBRUSHINDIRECT},
566 {0, EMR_SELECTOBJECT},
567 {0, EMR_RECTANGLE},
568 {0, EMR_SELECTOBJECT},
569 {0, EMR_DELETEOBJECT},
570 {0, EMR_EOF},
571 {0}
572 };
573
574 static void test_emfonly(void)
575 {
576 GpStatus stat;
577 GpMetafile *metafile;
578 GpImage *clone;
579 GpGraphics *graphics;
580 HDC hdc, metafile_dc;
581 GpRectF bounds;
582 GpUnit unit;
583 REAL xres, yres;
584 HENHMETAFILE hemf;
585 MetafileHeader header;
586 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
587 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
588 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
589 HBRUSH hbrush, holdbrush;
590 GpBitmap *bitmap;
591 ARGB color;
592
593 hdc = CreateCompatibleDC(0);
594
595 stat = GdipRecordMetafile(hdc, EmfTypeEmfOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
596 expect(Ok, stat);
597
598 DeleteDC(hdc);
599
600 if (stat != Ok)
601 return;
602
603 stat = GdipGetHemfFromMetafile(metafile, &hemf);
604 expect(InvalidParameter, stat);
605
606 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
607 expect(Ok, stat);
608
609 stat = GdipGetDC(graphics, &metafile_dc);
610 expect(Ok, stat);
611
612 if (stat != Ok)
613 {
614 GdipDeleteGraphics(graphics);
615 GdipDisposeImage((GpImage*)metafile);
616 return;
617 }
618
619 hbrush = CreateSolidBrush(0xff0000);
620
621 holdbrush = SelectObject(metafile_dc, hbrush);
622
623 Rectangle(metafile_dc, 25, 25, 75, 75);
624
625 SelectObject(metafile_dc, holdbrush);
626
627 DeleteObject(hbrush);
628
629 stat = GdipReleaseDC(graphics, metafile_dc);
630 expect(Ok, stat);
631
632 stat = GdipDeleteGraphics(graphics);
633 expect(Ok, stat);
634
635 check_metafile(metafile, emfonly_records, "emfonly metafile", dst_points, &frame, UnitPixel);
636
637 save_metafile(metafile, "emfonly.emf");
638
639 stat = GdipGetImageBounds((GpImage*)metafile, &bounds, &unit);
640 expect(Ok, stat);
641 expectf(0.0, bounds.X);
642 expectf(0.0, bounds.Y);
643 expectf_(100.0, bounds.Width, 0.05);
644 expectf_(100.0, bounds.Height, 0.05);
645 expect(UnitPixel, unit);
646
647 stat = GdipGetImageHorizontalResolution((GpImage*)metafile, &xres);
648 expect(Ok, stat);
649
650 stat = GdipGetImageVerticalResolution((GpImage*)metafile, &yres);
651 expect(Ok, stat);
652
653 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
654 expect(Ok, stat);
655
656 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
657 expect(Ok, stat);
658
659 play_metafile(metafile, graphics, emfonly_records, "emfonly playback", dst_points, &frame, UnitPixel);
660
661 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
662 expect(Ok, stat);
663 expect(0, color);
664
665 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
666 expect(Ok, stat);
667 expect(0xff0000ff, color);
668
669 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
670 expect(Ok, stat);
671
672 stat = GdipDrawImagePointsRect(graphics, (GpImage*)metafile, dst_points, 3,
673 0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
674 expect(Ok, stat);
675
676 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
677 expect(Ok, stat);
678 expect(0, color);
679
680 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
681 expect(Ok, stat);
682 expect(0xff0000ff, color);
683
684 stat = GdipCloneImage((GpImage*)metafile, &clone);
685 expect(Ok, stat);
686
687 if (stat == Ok)
688 {
689 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
690 expect(Ok, stat);
691
692 stat = GdipDrawImagePointsRect(graphics, clone, dst_points, 3,
693 0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
694 expect(Ok, stat);
695
696 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
697 expect(Ok, stat);
698 expect(0, color);
699
700 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
701 expect(Ok, stat);
702 expect(0xff0000ff, color);
703
704 GdipDisposeImage(clone);
705 }
706
707 stat = GdipDeleteGraphics(graphics);
708 expect(Ok, stat);
709
710 stat = GdipDisposeImage((GpImage*)bitmap);
711 expect(Ok, stat);
712
713 stat = GdipGetHemfFromMetafile(metafile, &hemf);
714 expect(Ok, stat);
715
716 stat = GdipDisposeImage((GpImage*)metafile);
717 expect(Ok, stat);
718
719 check_emfplus(hemf, emfonly_records, "emfonly emf");
720
721 memset(&header, 0xaa, sizeof(header));
722 stat = GdipGetMetafileHeaderFromEmf(hemf, &header);
723 expect(Ok, stat);
724 expect(MetafileTypeEmf, header.Type);
725 expect(U(header).EmfHeader.nBytes, header.Size);
726 expect(0x10000, header.Version);
727 expect(0, header.EmfPlusFlags);
728 expectf(xres, header.DpiX);
729 expectf(xres, U(header).EmfHeader.szlDevice.cx / (REAL)U(header).EmfHeader.szlMillimeters.cx * 25.4);
730 expectf(yres, header.DpiY);
731 expectf(yres, U(header).EmfHeader.szlDevice.cy / (REAL)U(header).EmfHeader.szlMillimeters.cy * 25.4);
732 expect(0, header.X);
733 expect(0, header.Y);
734 expect(100, header.Width);
735 expect(100, header.Height);
736 expect(0, header.EmfPlusHeaderSize);
737 expect(0, header.LogicalDpiX);
738 expect(0, header.LogicalDpiX);
739 expect(EMR_HEADER, U(header).EmfHeader.iType);
740 expect(25, U(header).EmfHeader.rclBounds.left);
741 expect(25, U(header).EmfHeader.rclBounds.top);
742 expect(74, U(header).EmfHeader.rclBounds.right);
743 expect(74, U(header).EmfHeader.rclBounds.bottom);
744 expect(0, U(header).EmfHeader.rclFrame.left);
745 expect(0, U(header).EmfHeader.rclFrame.top);
746 expectf_(100.0, U(header).EmfHeader.rclFrame.right * xres / 2540.0, 2.0);
747 expectf_(100.0, U(header).EmfHeader.rclFrame.bottom * yres / 2540.0, 2.0);
748
749 stat = GdipCreateMetafileFromEmf(hemf, TRUE, &metafile);
750 expect(Ok, stat);
751
752 stat = GdipGetImageBounds((GpImage*)metafile, &bounds, &unit);
753 expect(Ok, stat);
754 expectf(0.0, bounds.X);
755 expectf(0.0, bounds.Y);
756 expectf_(100.0, bounds.Width, 0.05);
757 expectf_(100.0, bounds.Height, 0.05);
758 expect(UnitPixel, unit);
759
760 stat = GdipGetImageHorizontalResolution((GpImage*)metafile, &xres);
761 expect(Ok, stat);
762 expectf(header.DpiX, xres);
763
764 stat = GdipGetImageVerticalResolution((GpImage*)metafile, &yres);
765 expect(Ok, stat);
766 expectf(header.DpiY, yres);
767
768 stat = GdipDisposeImage((GpImage*)metafile);
769 expect(Ok, stat);
770 }
771
772 static const emfplus_record fillrect_records[] = {
773 {0, EMR_HEADER},
774 {0, EmfPlusRecordTypeHeader},
775 {0, EmfPlusRecordTypeFillRects},
776 {0, EmfPlusRecordTypeEndOfFile},
777 {0, EMR_EOF},
778 {0}
779 };
780
781 static void test_fillrect(void)
782 {
783 GpStatus stat;
784 GpMetafile *metafile;
785 GpGraphics *graphics;
786 HDC hdc;
787 HENHMETAFILE hemf;
788 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
789 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
790 static const GpPointF dst_points_half[3] = {{0.0,0.0},{50.0,0.0},{0.0,50.0}};
791 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
792 GpBitmap *bitmap;
793 ARGB color;
794 GpBrush *brush;
795
796 hdc = CreateCompatibleDC(0);
797
798 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
799 expect(Ok, stat);
800
801 DeleteDC(hdc);
802
803 if (stat != Ok)
804 return;
805
806 stat = GdipGetHemfFromMetafile(metafile, &hemf);
807 expect(InvalidParameter, stat);
808
809 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
810 expect(Ok, stat);
811
812 stat = GdipCreateSolidFill((ARGB)0xff0000ff, (GpSolidFill**)&brush);
813 expect(Ok, stat);
814
815 stat = GdipFillRectangleI(graphics, brush, 25, 25, 75, 75);
816 expect(Ok, stat);
817
818 stat = GdipDeleteBrush(brush);
819 expect(Ok, stat);
820
821 stat = GdipDeleteGraphics(graphics);
822 expect(Ok, stat);
823
824 check_metafile(metafile, fillrect_records, "fillrect metafile", dst_points, &frame, UnitPixel);
825
826 save_metafile(metafile, "fillrect.emf");
827
828 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
829 expect(Ok, stat);
830
831 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
832 expect(Ok, stat);
833
834 play_metafile(metafile, graphics, fillrect_records, "fillrect playback", dst_points, &frame, UnitPixel);
835
836 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
837 expect(Ok, stat);
838 expect(0, color);
839
840 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
841 expect(Ok, stat);
842 expect(0xff0000ff, color);
843
844 stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
845 expect(Ok, stat);
846
847 play_metafile(metafile, graphics, fillrect_records, "fillrect playback", dst_points_half, &frame, UnitPixel);
848
849 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
850 expect(Ok, stat);
851 expect(0xff0000ff, color);
852
853 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
854 expect(Ok, stat);
855 expect(0, color);
856
857 stat = GdipBitmapSetPixel(bitmap, 15, 15, 0);
858 expect(Ok, stat);
859
860 stat = GdipDrawImagePointsRect(graphics, (GpImage*)metafile, dst_points, 3,
861 0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
862 expect(Ok, stat);
863
864 stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
865 expect(Ok, stat);
866 expect(0, color);
867
868 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
869 expect(Ok, stat);
870 expect(0xff0000ff, color);
871
872 stat = GdipDeleteGraphics(graphics);
873 expect(Ok, stat);
874
875 stat = GdipDisposeImage((GpImage*)bitmap);
876 expect(Ok, stat);
877
878 stat = GdipDisposeImage((GpImage*)metafile);
879 expect(Ok, stat);
880 }
881
882 static const emfplus_record pagetransform_records[] = {
883 {0, EMR_HEADER},
884 {0, EmfPlusRecordTypeHeader},
885 {0, EmfPlusRecordTypeFillRects},
886 {0, EmfPlusRecordTypeSetPageTransform},
887 {0, EmfPlusRecordTypeFillRects},
888 {0, EmfPlusRecordTypeSetPageTransform},
889 {0, EmfPlusRecordTypeFillRects},
890 {0, EmfPlusRecordTypeSetPageTransform},
891 {0, EmfPlusRecordTypeFillRects},
892 {0, EmfPlusRecordTypeSetPageTransform},
893 {0, EmfPlusRecordTypeFillRects},
894 {0, EmfPlusRecordTypeEndOfFile},
895 {0, EMR_EOF},
896 {0}
897 };
898
899 static void test_pagetransform(void)
900 {
901 GpStatus stat;
902 GpMetafile *metafile;
903 GpGraphics *graphics;
904 HDC hdc;
905 static const GpRectF frame = {0.0, 0.0, 5.0, 5.0};
906 static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
907 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
908 GpBitmap *bitmap;
909 ARGB color;
910 GpBrush *brush;
911 GpUnit unit;
912 REAL scale, dpix, dpiy;
913 UINT width, height;
914
915 hdc = CreateCompatibleDC(0);
916
917 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitInch, description, &metafile);
918 expect(Ok, stat);
919
920 DeleteDC(hdc);
921
922 if (stat != Ok)
923 return;
924
925 stat = GdipGetImageHorizontalResolution((GpImage*)metafile, &dpix);
926 todo_wine expect(InvalidParameter, stat);
927
928 stat = GdipGetImageVerticalResolution((GpImage*)metafile, &dpiy);
929 todo_wine expect(InvalidParameter, stat);
930
931 stat = GdipGetImageWidth((GpImage*)metafile, &width);
932 todo_wine expect(InvalidParameter, stat);
933
934 stat = GdipGetImageHeight((GpImage*)metafile, &height);
935 todo_wine expect(InvalidParameter, stat);
936
937 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
938 expect(Ok, stat);
939
940 /* initial scale */
941 stat = GdipGetPageUnit(graphics, &unit);
942 expect(Ok, stat);
943 expect(UnitDisplay, unit);
944
945 stat = GdipGetPageScale(graphics, &scale);
946 expect(Ok, stat);
947 expectf(1.0, scale);
948
949 stat = GdipGetDpiX(graphics, &dpix);
950 expect(Ok, stat);
951 expectf(96.0, dpix);
952
953 stat = GdipGetDpiY(graphics, &dpiy);
954 expect(Ok, stat);
955 expectf(96.0, dpiy);
956
957 stat = GdipCreateSolidFill((ARGB)0xff0000ff, (GpSolidFill**)&brush);
958 expect(Ok, stat);
959
960 stat = GdipFillRectangleI(graphics, brush, 1, 2, 1, 1);
961 expect(Ok, stat);
962
963 stat = GdipDeleteBrush(brush);
964 expect(Ok, stat);
965
966 /* page unit = pixels */
967 stat = GdipSetPageUnit(graphics, UnitPixel);
968 expect(Ok, stat);
969
970 stat = GdipGetPageUnit(graphics, &unit);
971 expect(Ok, stat);
972 expect(UnitPixel, unit);
973
974 stat = GdipCreateSolidFill((ARGB)0xff00ff00, (GpSolidFill**)&brush);
975 expect(Ok, stat);
976
977 stat = GdipFillRectangleI(graphics, brush, 0, 1, 1, 1);
978 expect(Ok, stat);
979
980 stat = GdipDeleteBrush(brush);
981 expect(Ok, stat);
982
983 /* page scale = 3, unit = pixels */
984 stat = GdipSetPageScale(graphics, 3.0);
985 expect(Ok, stat);
986
987 stat = GdipGetPageScale(graphics, &scale);
988 expect(Ok, stat);
989 expectf(3.0, scale);
990
991 stat = GdipCreateSolidFill((ARGB)0xff00ffff, (GpSolidFill**)&brush);
992 expect(Ok, stat);
993
994 stat = GdipFillRectangleI(graphics, brush, 0, 1, 2, 2);
995 expect(Ok, stat);
996
997 stat = GdipDeleteBrush(brush);
998 expect(Ok, stat);
999
1000 /* page scale = 3, unit = inches */
1001 stat = GdipSetPageUnit(graphics, UnitInch);
1002 expect(Ok, stat);
1003
1004 stat = GdipGetPageUnit(graphics, &unit);
1005 expect(Ok, stat);
1006 expect(UnitInch, unit);
1007
1008 stat = GdipCreateSolidFill((ARGB)0xffff0000, (GpSolidFill**)&brush);
1009 expect(Ok, stat);
1010
1011 stat = GdipFillRectangle(graphics, brush, 1.0/96.0, 0, 1, 1);
1012 expect(Ok, stat);
1013
1014 stat = GdipDeleteBrush(brush);
1015 expect(Ok, stat);
1016
1017 /* page scale = 3, unit = display */
1018 stat = GdipSetPageUnit(graphics, UnitDisplay);
1019 expect(Ok, stat);
1020
1021 stat = GdipGetPageUnit(graphics, &unit);
1022 expect(Ok, stat);
1023 expect(UnitDisplay, unit);
1024
1025 stat = GdipCreateSolidFill((ARGB)0xffff00ff, (GpSolidFill**)&brush);
1026 expect(Ok, stat);
1027
1028 stat = GdipFillRectangle(graphics, brush, 3, 3, 2, 2);
1029 expect(Ok, stat);
1030
1031 stat = GdipDeleteBrush(brush);
1032 expect(Ok, stat);
1033
1034 stat = GdipDeleteGraphics(graphics);
1035 expect(Ok, stat);
1036
1037 check_metafile(metafile, pagetransform_records, "pagetransform metafile", dst_points, &frame, UnitPixel);
1038
1039 save_metafile(metafile, "pagetransform.emf");
1040
1041 stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
1042 expect(Ok, stat);
1043
1044 stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
1045 expect(Ok, stat);
1046
1047 play_metafile(metafile, graphics, pagetransform_records, "pagetransform playback", dst_points, &frame, UnitPixel);
1048
1049 stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
1050 expect(Ok, stat);
1051 expect(0, color);
1052
1053 stat = GdipBitmapGetPixel(bitmap, 30, 50, &color);
1054 expect(Ok, stat);
1055 expect(0xff0000ff, color);
1056
1057 stat = GdipBitmapGetPixel(bitmap, 10, 30, &color);
1058 expect(Ok, stat);
1059 expect(0xff00ff00, color);
1060
1061 stat = GdipBitmapGetPixel(bitmap, 20, 80, &color);
1062 expect(Ok, stat);
1063 expect(0xff00ffff, color);
1064
1065 stat = GdipBitmapGetPixel(bitmap, 80, 20, &color);
1066 expect(Ok, stat);
1067 expect(0xffff0000, color);
1068
1069 stat = GdipBitmapGetPixel(bitmap, 80, 80, &color);
1070 expect(Ok, stat);
1071 expect(0xffff00ff, color);
1072
1073 stat = GdipDeleteGraphics(graphics);
1074 expect(Ok, stat);
1075
1076 stat = GdipDisposeImage((GpImage*)bitmap);
1077 expect(Ok, stat);
1078
1079 stat = GdipDisposeImage((GpImage*)metafile);
1080 expect(Ok, stat);
1081 }
1082
1083 static void test_converttoemfplus(void)
1084 {
1085 GpStatus (WINAPI *pGdipConvertToEmfPlus)( const GpGraphics *graphics, GpMetafile *metafile, BOOL *succ,
1086 EmfType emfType, const WCHAR *description, GpMetafile **outmetafile);
1087 static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
1088 static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
1089 GpStatus stat;
1090 GpMetafile *metafile, *metafile2 = NULL, *emhmeta;
1091 GpGraphics *graphics;
1092 HDC hdc;
1093 BOOL succ;
1094 HMODULE mod = GetModuleHandleA("gdiplus.dll");
1095
1096 pGdipConvertToEmfPlus = (void*)GetProcAddress( mod, "GdipConvertToEmfPlus");
1097 if(!pGdipConvertToEmfPlus)
1098 {
1099 /* GdipConvertToEmfPlus was introduced in Windows Vista. */
1100 win_skip("GDIPlus version 1.1 not available\n");
1101 return;
1102 }
1103
1104 hdc = CreateCompatibleDC(0);
1105
1106 stat = GdipRecordMetafile(hdc, MetafileTypeEmf, &frame, MetafileFrameUnitPixel, description, &metafile);
1107 expect(Ok, stat);
1108
1109 stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &emhmeta);
1110 expect(Ok, stat);
1111
1112 DeleteDC(hdc);
1113
1114 if (stat != Ok)
1115 return;
1116
1117 stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
1118 expect(Ok, stat);
1119
1120 /* Invalid Parameters */
1121 stat = pGdipConvertToEmfPlus(NULL, metafile, &succ, EmfTypeEmfPlusOnly, description, &metafile2);
1122 expect(InvalidParameter, stat);
1123
1124 stat = pGdipConvertToEmfPlus(graphics, NULL, &succ, EmfTypeEmfPlusOnly, description, &metafile2);
1125 expect(InvalidParameter, stat);
1126
1127 stat = pGdipConvertToEmfPlus(graphics, metafile, &succ, EmfTypeEmfPlusOnly, description, NULL);
1128 expect(InvalidParameter, stat);
1129
1130 stat = pGdipConvertToEmfPlus(graphics, metafile, NULL, MetafileTypeInvalid, NULL, &metafile2);
1131 expect(InvalidParameter, stat);
1132
1133 stat = pGdipConvertToEmfPlus(graphics, metafile, NULL, MetafileTypeEmfPlusDual+1, NULL, &metafile2);
1134 expect(InvalidParameter, stat);
1135
1136 /* If we are already an Enhanced Metafile then the conversion fails. */
1137 stat = pGdipConvertToEmfPlus(graphics, emhmeta, NULL, EmfTypeEmfPlusOnly, NULL, &metafile2);
1138 todo_wine expect(InvalidParameter, stat);
1139
1140 stat = pGdipConvertToEmfPlus(graphics, metafile, NULL, EmfTypeEmfPlusOnly, NULL, &metafile2);
1141 todo_wine expect(Ok, stat);
1142 if(metafile2)
1143 GdipDisposeImage((GpImage*)metafile2);
1144
1145 succ = FALSE;
1146 stat = pGdipConvertToEmfPlus(graphics, metafile, &succ, EmfTypeEmfPlusOnly, NULL, &metafile2);
1147 todo_wine expect(Ok, stat);
1148 if(metafile2)
1149 GdipDisposeImage((GpImage*)metafile2);
1150
1151 stat = GdipDeleteGraphics(graphics);
1152 expect(Ok, stat);
1153
1154 stat = GdipDisposeImage((GpImage*)metafile);
1155 expect(Ok, stat);
1156
1157 stat = GdipDisposeImage((GpImage*)emhmeta);
1158 expect(Ok, stat);
1159 }
1160
1161 START_TEST(metafile)
1162 {
1163 struct GdiplusStartupInput gdiplusStartupInput;
1164 ULONG_PTR gdiplusToken;
1165 int myARGC;
1166 char **myARGV;
1167
1168 gdiplusStartupInput.GdiplusVersion = 1;
1169 gdiplusStartupInput.DebugEventCallback = NULL;
1170 gdiplusStartupInput.SuppressBackgroundThread = 0;
1171 gdiplusStartupInput.SuppressExternalCodecs = 0;
1172
1173 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
1174
1175 myARGC = winetest_get_mainargs( &myARGV );
1176
1177 if (myARGC >= 3 && !strcmp(myARGV[2], "save"))
1178 save_metafiles = TRUE;
1179
1180 test_empty();
1181 test_getdc();
1182 test_emfonly();
1183 test_fillrect();
1184 test_pagetransform();
1185 test_converttoemfplus();
1186
1187 GdiplusShutdown(gdiplusToken);
1188 }