[GDIPLUS] Sync with Wine Staging 1.7.47. CORE-9924
[reactos.git] / reactos / dll / win32 / gdiplus / region.c
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
3 * Copyright (C) 2013 Dmitry Timoshkov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "gdiplus_private.h"
21
22 /**********************************************************
23 *
24 * Data returned by GdipGetRegionData looks something like this:
25 *
26 * struct region_data_header
27 * {
28 * DWORD size; size in bytes of the data - 8.
29 * DWORD magic1; probably a checksum.
30 * DWORD magic2; always seems to be 0xdbc01001 - version?
31 * DWORD num_ops; number of combining ops * 2
32 * };
33 *
34 * Then follows a sequence of combining ops and region elements.
35 *
36 * A region element is either a RECTF or some path data.
37 *
38 * Combining ops are just stored as their CombineMode value.
39 *
40 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
41 * stored as 0x10000002 (with no following RECTF) and an infinite rect
42 * is stored as 0x10000003 (again with no following RECTF).
43 *
44 * Path data is preceded by the DWORD 0x10000001. Then follows a
45 * DWORD size and then size bytes of data.
46 *
47 * The combining ops are stored in the reverse order to the region
48 * elements and in the reverse order to which the region was
49 * constructed.
50 *
51 * When two or more complex regions (ie those with more than one
52 * element) are combined, the combining op for the two regions comes
53 * first, then the combining ops for the region elements in region 1,
54 * followed by the region elements for region 1, then follows the
55 * combining ops for region 2 and finally region 2's region elements.
56 * Presumably you're supposed to use the 0x1000000x header to find the
57 * end of the op list (the count of the elements in each region is not
58 * stored).
59 *
60 * When a simple region (1 element) is combined, it's treated as if a
61 * single rect/path is being combined.
62 *
63 */
64
65 #define FLAGS_NOFLAGS 0x0
66 #define FLAGS_INTPATH 0x4000
67
68 struct memory_buffer
69 {
70 const BYTE *buffer;
71 INT size, pos;
72 };
73
74 struct region_header
75 {
76 DWORD size;
77 DWORD checksum;
78 DWORD magic;
79 DWORD num_children;
80 };
81
82 struct path_header
83 {
84 DWORD size;
85 DWORD magic;
86 DWORD count;
87 DWORD flags;
88 };
89
90 /* Header size as far as header->size is concerned. This doesn't include
91 * header->size or header->checksum
92 */
93 static const INT sizeheader_size = sizeof(DWORD) * 2;
94
95 typedef struct packed_point
96 {
97 short X;
98 short Y;
99 } packed_point;
100
101 /* Test to see if the path could be stored as an array of shorts */
102 static BOOL is_integer_path(const GpPath *path)
103 {
104 int i;
105
106 if (!path->pathdata.Count) return FALSE;
107
108 for (i = 0; i < path->pathdata.Count; i++)
109 {
110 short x, y;
111 x = gdip_round(path->pathdata.Points[i].X);
112 y = gdip_round(path->pathdata.Points[i].Y);
113 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
114 return FALSE;
115 }
116 return TRUE;
117 }
118
119 /* Everything is measured in DWORDS; round up if there's a remainder */
120 static inline INT get_pathtypes_size(const GpPath* path)
121 {
122 INT needed = path->pathdata.Count / sizeof(DWORD);
123
124 if (path->pathdata.Count % sizeof(DWORD) > 0)
125 needed++;
126
127 return needed * sizeof(DWORD);
128 }
129
130 static inline INT get_element_size(const region_element* element)
131 {
132 INT needed = sizeof(DWORD); /* DWORD for the type */
133 switch(element->type)
134 {
135 case RegionDataRect:
136 return needed + sizeof(GpRect);
137 case RegionDataPath:
138 {
139 const GpPath *path = element->elementdata.path;
140 DWORD flags = is_integer_path(path) ? FLAGS_INTPATH : FLAGS_NOFLAGS;
141 /* 3 for headers, once again size doesn't count itself */
142 needed += sizeof(DWORD) * 3;
143 if (flags & FLAGS_INTPATH)
144 needed += 2 * sizeof(SHORT) * path->pathdata.Count;
145 else
146 needed += 2 * sizeof(FLOAT) * path->pathdata.Count;
147
148 needed += get_pathtypes_size(path);
149 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
150 return needed;
151 }
152 case RegionDataEmptyRect:
153 case RegionDataInfiniteRect:
154 return needed;
155 default:
156 needed += get_element_size(element->elementdata.combine.left);
157 needed += get_element_size(element->elementdata.combine.right);
158 return needed;
159 }
160
161 return 0;
162 }
163
164 /* Does not check parameters, caller must do that */
165 static inline GpStatus init_region(GpRegion* region, const RegionType type)
166 {
167 region->node.type = type;
168 region->num_children = 0;
169
170 return Ok;
171 }
172
173 static inline GpStatus clone_element(const region_element* element,
174 region_element** element2)
175 {
176 GpStatus stat;
177
178 /* root node is allocated with GpRegion */
179 if(!*element2){
180 *element2 = GdipAlloc(sizeof(region_element));
181 if (!*element2)
182 return OutOfMemory;
183 }
184
185 (*element2)->type = element->type;
186
187 switch (element->type)
188 {
189 case RegionDataRect:
190 (*element2)->elementdata.rect = element->elementdata.rect;
191 return Ok;
192 case RegionDataEmptyRect:
193 case RegionDataInfiniteRect:
194 return Ok;
195 case RegionDataPath:
196 stat = GdipClonePath(element->elementdata.path, &(*element2)->elementdata.path);
197 if (stat == Ok) return Ok;
198 break;
199 default:
200 (*element2)->elementdata.combine.left = NULL;
201 (*element2)->elementdata.combine.right = NULL;
202
203 stat = clone_element(element->elementdata.combine.left,
204 &(*element2)->elementdata.combine.left);
205 if (stat == Ok)
206 {
207 stat = clone_element(element->elementdata.combine.right,
208 &(*element2)->elementdata.combine.right);
209 if (stat == Ok) return Ok;
210 }
211 break;
212 }
213
214 delete_element(*element2);
215 *element2 = NULL;
216 return stat;
217 }
218
219 /* Common code for CombineRegion*
220 * All the caller has to do is get its format into an element
221 */
222 static inline void fuse_region(GpRegion* region, region_element* left,
223 region_element* right, const CombineMode mode)
224 {
225 region->node.type = mode;
226 region->node.elementdata.combine.left = left;
227 region->node.elementdata.combine.right = right;
228 region->num_children += 2;
229 }
230
231 /*****************************************************************************
232 * GdipCloneRegion [GDIPLUS.@]
233 *
234 * Creates a deep copy of the region
235 *
236 * PARAMS
237 * region [I] source region
238 * clone [O] resulting clone
239 *
240 * RETURNS
241 * SUCCESS: Ok
242 * FAILURE: InvalidParameter or OutOfMemory
243 */
244 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
245 {
246 region_element *element;
247
248 TRACE("%p %p\n", region, clone);
249
250 if (!(region && clone))
251 return InvalidParameter;
252
253 *clone = GdipAlloc(sizeof(GpRegion));
254 if (!*clone)
255 return OutOfMemory;
256 element = &(*clone)->node;
257
258 (*clone)->num_children = region->num_children;
259 return clone_element(&region->node, &element);
260 }
261
262 /*****************************************************************************
263 * GdipCombineRegionPath [GDIPLUS.@]
264 */
265 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
266 {
267 GpRegion *path_region;
268 region_element *left, *right = NULL;
269 GpStatus stat;
270
271 TRACE("%p %p %d\n", region, path, mode);
272
273 if (!(region && path))
274 return InvalidParameter;
275
276 stat = GdipCreateRegionPath(path, &path_region);
277 if (stat != Ok)
278 return stat;
279
280 /* simply replace region data */
281 if(mode == CombineModeReplace){
282 delete_element(&region->node);
283 memcpy(region, path_region, sizeof(GpRegion));
284 GdipFree(path_region);
285 return Ok;
286 }
287
288 left = GdipAlloc(sizeof(region_element));
289 if (left)
290 {
291 *left = region->node;
292 stat = clone_element(&path_region->node, &right);
293 if (stat == Ok)
294 {
295 fuse_region(region, left, right, mode);
296 GdipDeleteRegion(path_region);
297 return Ok;
298 }
299 }
300 else
301 stat = OutOfMemory;
302
303 GdipFree(left);
304 GdipDeleteRegion(path_region);
305 return stat;
306 }
307
308 /*****************************************************************************
309 * GdipCombineRegionRect [GDIPLUS.@]
310 */
311 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
312 GDIPCONST GpRectF *rect, CombineMode mode)
313 {
314 GpRegion *rect_region;
315 region_element *left, *right = NULL;
316 GpStatus stat;
317
318 TRACE("%p %s %d\n", region, debugstr_rectf(rect), mode);
319
320 if (!(region && rect))
321 return InvalidParameter;
322
323 stat = GdipCreateRegionRect(rect, &rect_region);
324 if (stat != Ok)
325 return stat;
326
327 /* simply replace region data */
328 if(mode == CombineModeReplace){
329 delete_element(&region->node);
330 memcpy(region, rect_region, sizeof(GpRegion));
331 GdipFree(rect_region);
332 return Ok;
333 }
334
335 left = GdipAlloc(sizeof(region_element));
336 if (left)
337 {
338 memcpy(left, &region->node, sizeof(region_element));
339 stat = clone_element(&rect_region->node, &right);
340 if (stat == Ok)
341 {
342 fuse_region(region, left, right, mode);
343 GdipDeleteRegion(rect_region);
344 return Ok;
345 }
346 }
347 else
348 stat = OutOfMemory;
349
350 GdipFree(left);
351 GdipDeleteRegion(rect_region);
352 return stat;
353 }
354
355 /*****************************************************************************
356 * GdipCombineRegionRectI [GDIPLUS.@]
357 */
358 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
359 GDIPCONST GpRect *rect, CombineMode mode)
360 {
361 GpRectF rectf;
362
363 TRACE("%p %p %d\n", region, rect, mode);
364
365 if (!rect)
366 return InvalidParameter;
367
368 rectf.X = (REAL)rect->X;
369 rectf.Y = (REAL)rect->Y;
370 rectf.Height = (REAL)rect->Height;
371 rectf.Width = (REAL)rect->Width;
372
373 return GdipCombineRegionRect(region, &rectf, mode);
374 }
375
376 /*****************************************************************************
377 * GdipCombineRegionRegion [GDIPLUS.@]
378 */
379 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
380 GpRegion *region2, CombineMode mode)
381 {
382 region_element *left, *right = NULL;
383 GpStatus stat;
384 GpRegion *reg2copy;
385
386 TRACE("%p %p %d\n", region1, region2, mode);
387
388 if(!(region1 && region2))
389 return InvalidParameter;
390
391 /* simply replace region data */
392 if(mode == CombineModeReplace){
393 stat = GdipCloneRegion(region2, &reg2copy);
394 if(stat != Ok) return stat;
395
396 delete_element(&region1->node);
397 memcpy(region1, reg2copy, sizeof(GpRegion));
398 GdipFree(reg2copy);
399 return Ok;
400 }
401
402 left = GdipAlloc(sizeof(region_element));
403 if (!left)
404 return OutOfMemory;
405
406 *left = region1->node;
407 stat = clone_element(&region2->node, &right);
408 if (stat != Ok)
409 {
410 GdipFree(left);
411 return OutOfMemory;
412 }
413
414 fuse_region(region1, left, right, mode);
415 region1->num_children += region2->num_children;
416
417 return Ok;
418 }
419
420 /*****************************************************************************
421 * GdipCreateRegion [GDIPLUS.@]
422 */
423 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
424 {
425 TRACE("%p\n", region);
426
427 if(!region)
428 return InvalidParameter;
429
430 *region = GdipAlloc(sizeof(GpRegion));
431 if(!*region)
432 return OutOfMemory;
433
434 TRACE("=> %p\n", *region);
435
436 return init_region(*region, RegionDataInfiniteRect);
437 }
438
439 /*****************************************************************************
440 * GdipCreateRegionPath [GDIPLUS.@]
441 *
442 * Creates a GpRegion from a GpPath
443 *
444 * PARAMS
445 * path [I] path to base the region on
446 * region [O] pointer to the newly allocated region
447 *
448 * RETURNS
449 * SUCCESS: Ok
450 * FAILURE: InvalidParameter
451 *
452 * NOTES
453 * If a path has no floating point points, its points will be stored as shorts
454 * (INTPATH)
455 *
456 * If a path is empty, it is considered to be an INTPATH
457 */
458 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
459 {
460 region_element* element;
461 GpStatus stat;
462
463 TRACE("%p, %p\n", path, region);
464
465 if (!(path && region))
466 return InvalidParameter;
467
468 *region = GdipAlloc(sizeof(GpRegion));
469 if(!*region)
470 return OutOfMemory;
471 stat = init_region(*region, RegionDataPath);
472 if (stat != Ok)
473 {
474 GdipDeleteRegion(*region);
475 return stat;
476 }
477 element = &(*region)->node;
478
479 stat = GdipClonePath(path, &element->elementdata.path);
480 if (stat != Ok)
481 {
482 GdipDeleteRegion(*region);
483 return stat;
484 }
485
486 return Ok;
487 }
488
489 /*****************************************************************************
490 * GdipCreateRegionRect [GDIPLUS.@]
491 */
492 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
493 GpRegion **region)
494 {
495 GpStatus stat;
496
497 TRACE("%p, %p\n", rect, region);
498
499 if (!(rect && region))
500 return InvalidParameter;
501
502 *region = GdipAlloc(sizeof(GpRegion));
503 stat = init_region(*region, RegionDataRect);
504 if(stat != Ok)
505 {
506 GdipDeleteRegion(*region);
507 return stat;
508 }
509
510 (*region)->node.elementdata.rect.X = rect->X;
511 (*region)->node.elementdata.rect.Y = rect->Y;
512 (*region)->node.elementdata.rect.Width = rect->Width;
513 (*region)->node.elementdata.rect.Height = rect->Height;
514
515 return Ok;
516 }
517
518 /*****************************************************************************
519 * GdipCreateRegionRectI [GDIPLUS.@]
520 */
521 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
522 GpRegion **region)
523 {
524 GpRectF rectf;
525
526 TRACE("%p, %p\n", rect, region);
527
528 rectf.X = (REAL)rect->X;
529 rectf.Y = (REAL)rect->Y;
530 rectf.Width = (REAL)rect->Width;
531 rectf.Height = (REAL)rect->Height;
532
533 return GdipCreateRegionRect(&rectf, region);
534 }
535
536 /******************************************************************************
537 * GdipCreateRegionHrgn [GDIPLUS.@]
538 */
539 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
540 {
541 DWORD size;
542 LPRGNDATA buf;
543 LPRECT rect;
544 GpStatus stat;
545 GpPath* path;
546 GpRegion* local;
547 DWORD i;
548
549 TRACE("(%p, %p)\n", hrgn, region);
550
551 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
552 return InvalidParameter;
553
554 buf = GdipAlloc(size);
555 if(!buf)
556 return OutOfMemory;
557
558 if(!GetRegionData(hrgn, size, buf)){
559 GdipFree(buf);
560 return GenericError;
561 }
562
563 if(buf->rdh.nCount == 0){
564 if((stat = GdipCreateRegion(&local)) != Ok){
565 GdipFree(buf);
566 return stat;
567 }
568 if((stat = GdipSetEmpty(local)) != Ok){
569 GdipFree(buf);
570 GdipDeleteRegion(local);
571 return stat;
572 }
573 *region = local;
574 GdipFree(buf);
575 return Ok;
576 }
577
578 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
579 GdipFree(buf);
580 return stat;
581 }
582
583 rect = (LPRECT)buf->Buffer;
584 for(i = 0; i < buf->rdh.nCount; i++){
585 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
586 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
587 GdipFree(buf);
588 GdipDeletePath(path);
589 return stat;
590 }
591 rect++;
592 }
593
594 stat = GdipCreateRegionPath(path, region);
595
596 GdipFree(buf);
597 GdipDeletePath(path);
598 return stat;
599 }
600
601 /*****************************************************************************
602 * GdipDeleteRegion [GDIPLUS.@]
603 */
604 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
605 {
606 TRACE("%p\n", region);
607
608 if (!region)
609 return InvalidParameter;
610
611 delete_element(&region->node);
612 GdipFree(region);
613
614 return Ok;
615 }
616
617 /*****************************************************************************
618 * GdipGetRegionBounds [GDIPLUS.@]
619 */
620 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
621 {
622 HRGN hrgn;
623 RECT r;
624 GpStatus status;
625
626 TRACE("(%p, %p, %p)\n", region, graphics, rect);
627
628 if(!region || !graphics || !rect)
629 return InvalidParameter;
630
631 /* Contrary to MSDN, native ignores the graphics transform. */
632 status = GdipGetRegionHRgn(region, NULL, &hrgn);
633 if(status != Ok)
634 return status;
635
636 /* infinite */
637 if(!hrgn){
638 rect->X = rect->Y = -(REAL)(1 << 22);
639 rect->Width = rect->Height = (REAL)(1 << 23);
640 TRACE("%p => infinite\n", region);
641 return Ok;
642 }
643
644 if(GetRgnBox(hrgn, &r)){
645 rect->X = r.left;
646 rect->Y = r.top;
647 rect->Width = r.right - r.left;
648 rect->Height = r.bottom - r.top;
649 TRACE("%p => %s\n", region, debugstr_rectf(rect));
650 }
651 else
652 status = GenericError;
653
654 DeleteObject(hrgn);
655
656 return status;
657 }
658
659 /*****************************************************************************
660 * GdipGetRegionBoundsI [GDIPLUS.@]
661 */
662 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
663 {
664 GpRectF rectf;
665 GpStatus status;
666
667 TRACE("(%p, %p, %p)\n", region, graphics, rect);
668
669 if(!rect)
670 return InvalidParameter;
671
672 status = GdipGetRegionBounds(region, graphics, &rectf);
673 if(status == Ok){
674 rect->X = gdip_round(rectf.X);
675 rect->Y = gdip_round(rectf.Y);
676 rect->Width = gdip_round(rectf.Width);
677 rect->Height = gdip_round(rectf.Height);
678 }
679
680 return status;
681 }
682
683 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
684 {
685 location[*offset] = write;
686 (*offset)++;
687 }
688
689 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
690 {
691 ((FLOAT*)location)[*offset] = write;
692 (*offset)++;
693 }
694
695 static inline void write_packed_point(DWORD* location, INT* offset,
696 const GpPointF* write)
697 {
698 packed_point *point = (packed_point *)(location + *offset);
699 point->X = gdip_round(write->X);
700 point->Y = gdip_round(write->Y);
701 (*offset)++;
702 }
703
704 static inline void write_path_types(DWORD* location, INT* offset,
705 const GpPath* path)
706 {
707 INT rounded_size = get_pathtypes_size(path);
708
709 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
710
711 /* The unwritten parts of the DWORD (if any) must be cleared */
712 if (rounded_size - path->pathdata.Count)
713 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
714 path->pathdata.Count, rounded_size - path->pathdata.Count);
715 *offset += rounded_size / sizeof(DWORD);
716 }
717
718 static void write_element(const region_element* element, DWORD *buffer,
719 INT* filled)
720 {
721 write_dword(buffer, filled, element->type);
722 switch (element->type)
723 {
724 case CombineModeReplace:
725 case CombineModeIntersect:
726 case CombineModeUnion:
727 case CombineModeXor:
728 case CombineModeExclude:
729 case CombineModeComplement:
730 write_element(element->elementdata.combine.left, buffer, filled);
731 write_element(element->elementdata.combine.right, buffer, filled);
732 break;
733 case RegionDataRect:
734 write_float(buffer, filled, element->elementdata.rect.X);
735 write_float(buffer, filled, element->elementdata.rect.Y);
736 write_float(buffer, filled, element->elementdata.rect.Width);
737 write_float(buffer, filled, element->elementdata.rect.Height);
738 break;
739 case RegionDataPath:
740 {
741 INT i;
742 const GpPath* path = element->elementdata.path;
743 struct path_header *pathheader;
744
745 pathheader = (struct path_header *)(buffer + *filled);
746
747 pathheader->flags = is_integer_path(path) ? FLAGS_INTPATH : FLAGS_NOFLAGS;
748 /* 3 for headers, once again size doesn't count itself */
749 pathheader->size = sizeof(DWORD) * 3;
750 if (pathheader->flags & FLAGS_INTPATH)
751 pathheader->size += 2 * sizeof(SHORT) * path->pathdata.Count;
752 else
753 pathheader->size += 2 * sizeof(FLOAT) * path->pathdata.Count;
754 pathheader->size += get_pathtypes_size(path);
755 pathheader->magic = VERSION_MAGIC;
756 pathheader->count = path->pathdata.Count;
757
758 *filled += 4;
759
760 switch (pathheader->flags & FLAGS_INTPATH)
761 {
762 case FLAGS_NOFLAGS:
763 for (i = 0; i < path->pathdata.Count; i++)
764 {
765 write_float(buffer, filled, path->pathdata.Points[i].X);
766 write_float(buffer, filled, path->pathdata.Points[i].Y);
767 }
768 break;
769 case FLAGS_INTPATH:
770 for (i = 0; i < path->pathdata.Count; i++)
771 {
772 write_packed_point(buffer, filled,
773 &path->pathdata.Points[i]);
774 }
775 break;
776 }
777 write_path_types(buffer, filled, path);
778 break;
779 }
780 case RegionDataEmptyRect:
781 case RegionDataInfiniteRect:
782 break;
783 }
784 }
785
786 /*****************************************************************************
787 * GdipGetRegionData [GDIPLUS.@]
788 *
789 * Returns the header, followed by combining ops and region elements.
790 *
791 * PARAMS
792 * region [I] region to retrieve from
793 * buffer [O] buffer to hold the resulting data
794 * size [I] size of the buffer
795 * needed [O] (optional) how much data was written
796 *
797 * RETURNS
798 * SUCCESS: Ok
799 * FAILURE: InvalidParameter
800 *
801 * NOTES
802 * The header contains the size, a checksum, a version string, and the number
803 * of children. The size does not count itself or the checksum.
804 * Version is always something like 0xdbc01001 or 0xdbc01002
805 *
806 * An element is a RECT, or PATH; Combining ops are stored as their
807 * CombineMode value. Special regions (infinite, empty) emit just their
808 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
809 * their code followed by a second header for the path followed by the actual
810 * path data. Followed by the flags for each point. The pathheader contains
811 * the size of the data to follow, a version number again, followed by a count
812 * of how many points, and any special flags which may apply. 0x4000 means its
813 * a path of shorts instead of FLOAT.
814 *
815 * Combining Ops are stored in reverse order from when they were constructed;
816 * the output is a tree where the left side combining area is always taken
817 * first.
818 */
819 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
820 UINT *needed)
821 {
822 struct region_header *region_header;
823 INT filled = 0;
824 UINT required;
825 GpStatus status;
826
827 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
828
829 if (!region || !buffer || !size)
830 return InvalidParameter;
831
832 status = GdipGetRegionDataSize(region, &required);
833 if (status != Ok) return status;
834 if (size < required)
835 {
836 if (needed) *needed = size;
837 return InsufficientBuffer;
838 }
839
840 region_header = (struct region_header *)buffer;
841 region_header->size = sizeheader_size + get_element_size(&region->node);
842 region_header->checksum = 0;
843 region_header->magic = VERSION_MAGIC;
844 region_header->num_children = region->num_children;
845 filled += 4;
846 /* With few exceptions, everything written is DWORD aligned,
847 * so use that as our base */
848 write_element(&region->node, (DWORD*)buffer, &filled);
849
850 if (needed)
851 *needed = filled * sizeof(DWORD);
852
853 return Ok;
854 }
855
856 static inline void init_memory_buffer(struct memory_buffer *mbuf, const BYTE *buffer, INT size)
857 {
858 mbuf->buffer = buffer;
859 mbuf->size = size;
860 mbuf->pos = 0;
861 }
862
863 static inline const void *buffer_read(struct memory_buffer *mbuf, INT size)
864 {
865 if (mbuf->size - mbuf->pos >= size)
866 {
867 const void *data = mbuf->buffer + mbuf->pos;
868 mbuf->pos += size;
869 return data;
870 }
871 return NULL;
872 }
873
874 static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count)
875 {
876 GpStatus status;
877 const DWORD *type;
878
879 type = buffer_read(mbuf, sizeof(*type));
880 if (!type) return Ok;
881
882 TRACE("type %#x\n", *type);
883
884 node->type = *type;
885
886 switch (node->type)
887 {
888 case CombineModeReplace:
889 case CombineModeIntersect:
890 case CombineModeUnion:
891 case CombineModeXor:
892 case CombineModeExclude:
893 case CombineModeComplement:
894 {
895 region_element *left, *right;
896
897 left = GdipAlloc(sizeof(region_element));
898 if (!left) return OutOfMemory;
899 right = GdipAlloc(sizeof(region_element));
900 if (!right)
901 {
902 GdipFree(left);
903 return OutOfMemory;
904 }
905
906 status = read_element(mbuf, region, left, count);
907 if (status == Ok)
908 {
909 status = read_element(mbuf, region, right, count);
910 if (status == Ok)
911 {
912 node->elementdata.combine.left = left;
913 node->elementdata.combine.right = right;
914 region->num_children += 2;
915 return Ok;
916 }
917 }
918
919 GdipFree(left);
920 GdipFree(right);
921 return status;
922 }
923
924 case RegionDataRect:
925 {
926 const GpRectF *rc;
927
928 rc = buffer_read(mbuf, sizeof(*rc));
929 if (!rc)
930 {
931 ERR("failed to read rect data\n");
932 return InvalidParameter;
933 }
934
935 node->elementdata.rect = *rc;
936 *count += 1;
937 return Ok;
938 }
939
940 case RegionDataPath:
941 {
942 GpPath *path;
943 const struct path_header *path_header;
944 const BYTE *types;
945
946 path_header = buffer_read(mbuf, sizeof(*path_header));
947 if (!path_header)
948 {
949 ERR("failed to read path header\n");
950 return InvalidParameter;
951 }
952 if (path_header->magic != VERSION_MAGIC)
953 {
954 ERR("invalid path header magic %#x\n", path_header->magic);
955 return InvalidParameter;
956 }
957
958 /* Windows always fails to create an empty path in a region */
959 if (!path_header->count)
960 {
961 TRACE("refusing to create an empty path in a region\n");
962 return GenericError;
963 }
964
965 status = GdipCreatePath(FillModeAlternate, &path);
966 if (status) return status;
967
968 node->elementdata.path = path;
969
970 if (!lengthen_path(path, path_header->count))
971 return OutOfMemory;
972
973 path->pathdata.Count = path_header->count;
974
975 if (path_header->flags & ~FLAGS_INTPATH)
976 FIXME("unhandled path flags %#x\n", path_header->flags);
977
978 if (path_header->flags & FLAGS_INTPATH)
979 {
980 const packed_point *pt;
981 DWORD i;
982
983 pt = buffer_read(mbuf, sizeof(*pt) * path_header->count);
984 if (!pt)
985 {
986 ERR("failed to read packed %u path points\n", path_header->count);
987 return InvalidParameter;
988 }
989
990 for (i = 0; i < path_header->count; i++)
991 {
992 path->pathdata.Points[i].X = (REAL)pt[i].X;
993 path->pathdata.Points[i].Y = (REAL)pt[i].Y;
994 }
995 }
996 else
997 {
998 const GpPointF *ptf;
999
1000 ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count);
1001 if (!ptf)
1002 {
1003 ERR("failed to read %u path points\n", path_header->count);
1004 return InvalidParameter;
1005 }
1006 memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count);
1007 }
1008
1009 types = buffer_read(mbuf, path_header->count);
1010 if (!types)
1011 {
1012 ERR("failed to read %u path types\n", path_header->count);
1013 return InvalidParameter;
1014 }
1015 memcpy(path->pathdata.Types, types, path_header->count);
1016 if (path_header->count & 3)
1017 {
1018 if (!buffer_read(mbuf, 4 - (path_header->count & 3)))
1019 {
1020 ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3));
1021 return InvalidParameter;
1022 }
1023 }
1024
1025 *count += 1;
1026 return Ok;
1027 }
1028
1029 case RegionDataEmptyRect:
1030 case RegionDataInfiniteRect:
1031 *count += 1;
1032 return Ok;
1033
1034 default:
1035 FIXME("element type %#x is not supported\n", *type);
1036 break;
1037 }
1038
1039 return InvalidParameter;
1040 }
1041
1042 /*****************************************************************************
1043 * GdipCreateRegionRgnData [GDIPLUS.@]
1044 */
1045 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
1046 {
1047 const struct region_header *region_header;
1048 struct memory_buffer mbuf;
1049 GpStatus status;
1050 INT count;
1051
1052 TRACE("(%p, %d, %p)\n", data, size, region);
1053
1054 if (!data || !size)
1055 return InvalidParameter;
1056
1057 init_memory_buffer(&mbuf, data, size);
1058
1059 region_header = buffer_read(&mbuf, sizeof(*region_header));
1060 if (!region_header || (region_header->magic != VERSION_MAGIC &&
1061 region_header->magic != VERSION_MAGIC2))
1062 return InvalidParameter;
1063
1064 status = GdipCreateRegion(region);
1065 if (status != Ok)
1066 return status;
1067
1068 count = 0;
1069 status = read_element(&mbuf, *region, &(*region)->node, &count);
1070 if (status == Ok && !count)
1071 status = InvalidParameter;
1072
1073 if (status != Ok)
1074 {
1075 GdipDeleteRegion(*region);
1076 *region = NULL;
1077 }
1078
1079 return status;
1080 }
1081
1082 /*****************************************************************************
1083 * GdipGetRegionDataSize [GDIPLUS.@]
1084 */
1085 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
1086 {
1087 TRACE("%p, %p\n", region, needed);
1088
1089 if (!(region && needed))
1090 return InvalidParameter;
1091
1092 /* header.size doesn't count header.size and header.checksum */
1093 *needed = sizeof(DWORD) * 2 + sizeheader_size + get_element_size(&region->node);
1094
1095 return Ok;
1096 }
1097
1098 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
1099 {
1100 HDC new_hdc=NULL;
1101 GpGraphics *new_graphics=NULL;
1102 GpStatus stat;
1103 INT save_state;
1104
1105 if (!graphics)
1106 {
1107 new_hdc = CreateCompatibleDC(0);
1108 if (!new_hdc)
1109 return OutOfMemory;
1110
1111 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
1112 graphics = new_graphics;
1113 if (stat != Ok)
1114 {
1115 DeleteDC(new_hdc);
1116 return stat;
1117 }
1118 }
1119 else if (!graphics->hdc)
1120 {
1121 graphics->hdc = new_hdc = CreateCompatibleDC(0);
1122 if (!new_hdc)
1123 return OutOfMemory;
1124 }
1125
1126 save_state = SaveDC(graphics->hdc);
1127 EndPath(graphics->hdc);
1128
1129 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1130 : WINDING));
1131
1132 stat = trace_path(graphics, path);
1133 if (stat == Ok)
1134 {
1135 *hrgn = PathToRegion(graphics->hdc);
1136 stat = *hrgn ? Ok : OutOfMemory;
1137 }
1138
1139 RestoreDC(graphics->hdc, save_state);
1140 if (new_hdc)
1141 {
1142 DeleteDC(new_hdc);
1143 if (new_graphics)
1144 GdipDeleteGraphics(new_graphics);
1145 else
1146 graphics->hdc = NULL;
1147 }
1148
1149 return stat;
1150 }
1151
1152 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
1153 {
1154 switch (element->type)
1155 {
1156 case RegionDataInfiniteRect:
1157 *hrgn = NULL;
1158 return Ok;
1159 case RegionDataEmptyRect:
1160 *hrgn = CreateRectRgn(0, 0, 0, 0);
1161 return *hrgn ? Ok : OutOfMemory;
1162 case RegionDataPath:
1163 return get_path_hrgn(element->elementdata.path, graphics, hrgn);
1164 case RegionDataRect:
1165 {
1166 GpPath* path;
1167 GpStatus stat;
1168 GpRectF* rc = &element->elementdata.rect;
1169
1170 stat = GdipCreatePath(FillModeAlternate, &path);
1171 if (stat != Ok)
1172 return stat;
1173 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
1174
1175 if (stat == Ok)
1176 stat = get_path_hrgn(path, graphics, hrgn);
1177
1178 GdipDeletePath(path);
1179
1180 return stat;
1181 }
1182 case CombineModeIntersect:
1183 case CombineModeUnion:
1184 case CombineModeXor:
1185 case CombineModeExclude:
1186 case CombineModeComplement:
1187 {
1188 HRGN left, right;
1189 GpStatus stat;
1190 int ret;
1191
1192 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
1193 if (stat != Ok)
1194 {
1195 *hrgn = NULL;
1196 return stat;
1197 }
1198
1199 if (left == NULL)
1200 {
1201 /* existing region is infinite */
1202 switch (element->type)
1203 {
1204 case CombineModeIntersect:
1205 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
1206 case CombineModeXor: case CombineModeExclude:
1207 left = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1208 break;
1209 case CombineModeUnion: case CombineModeComplement:
1210 *hrgn = NULL;
1211 return Ok;
1212 }
1213 }
1214
1215 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1216 if (stat != Ok)
1217 {
1218 DeleteObject(left);
1219 *hrgn = NULL;
1220 return stat;
1221 }
1222
1223 if (right == NULL)
1224 {
1225 /* new region is infinite */
1226 switch (element->type)
1227 {
1228 case CombineModeIntersect:
1229 *hrgn = left;
1230 return Ok;
1231 case CombineModeXor: case CombineModeComplement:
1232 right = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1233 break;
1234 case CombineModeUnion: case CombineModeExclude:
1235 DeleteObject(left);
1236 *hrgn = NULL;
1237 return Ok;
1238 }
1239 }
1240
1241 switch (element->type)
1242 {
1243 case CombineModeIntersect:
1244 ret = CombineRgn(left, left, right, RGN_AND);
1245 break;
1246 case CombineModeUnion:
1247 ret = CombineRgn(left, left, right, RGN_OR);
1248 break;
1249 case CombineModeXor:
1250 ret = CombineRgn(left, left, right, RGN_XOR);
1251 break;
1252 case CombineModeExclude:
1253 ret = CombineRgn(left, left, right, RGN_DIFF);
1254 break;
1255 case CombineModeComplement:
1256 ret = CombineRgn(left, right, left, RGN_DIFF);
1257 break;
1258 default:
1259 ret = ERROR;
1260 }
1261
1262 DeleteObject(right);
1263
1264 if (ret == ERROR)
1265 {
1266 DeleteObject(left);
1267 *hrgn = NULL;
1268 return GenericError;
1269 }
1270
1271 *hrgn = left;
1272 return Ok;
1273 }
1274 default:
1275 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1276 *hrgn = NULL;
1277 return NotImplemented;
1278 }
1279 }
1280
1281 /*****************************************************************************
1282 * GdipGetRegionHRgn [GDIPLUS.@]
1283 */
1284 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1285 {
1286 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1287
1288 if (!region || !hrgn)
1289 return InvalidParameter;
1290
1291 return get_region_hrgn(&region->node, graphics, hrgn);
1292 }
1293
1294 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1295 {
1296 GpStatus status;
1297 GpRectF rect;
1298
1299 TRACE("(%p, %p, %p)\n", region, graphics, res);
1300
1301 if(!region || !graphics || !res)
1302 return InvalidParameter;
1303
1304 status = GdipGetRegionBounds(region, graphics, &rect);
1305 if (status != Ok) return status;
1306
1307 *res = rect.Width == 0.0 && rect.Height == 0.0;
1308 TRACE("=> %d\n", *res);
1309
1310 return Ok;
1311 }
1312
1313 /*****************************************************************************
1314 * GdipIsEqualRegion [GDIPLUS.@]
1315 */
1316 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1317 BOOL *res)
1318 {
1319 HRGN hrgn1, hrgn2;
1320 GpStatus stat;
1321
1322 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1323
1324 if(!region || !region2 || !graphics || !res)
1325 return InvalidParameter;
1326
1327 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1328 if(stat != Ok)
1329 return stat;
1330 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1331 if(stat != Ok){
1332 DeleteObject(hrgn1);
1333 return stat;
1334 }
1335
1336 *res = EqualRgn(hrgn1, hrgn2);
1337
1338 /* one of GpRegions is infinite */
1339 if(*res == ERROR)
1340 *res = (!hrgn1 && !hrgn2);
1341
1342 DeleteObject(hrgn1);
1343 DeleteObject(hrgn2);
1344
1345 return Ok;
1346 }
1347
1348 /*****************************************************************************
1349 * GdipIsInfiniteRegion [GDIPLUS.@]
1350 */
1351 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1352 {
1353 /* I think graphics is ignored here */
1354 TRACE("(%p, %p, %p)\n", region, graphics, res);
1355
1356 if(!region || !graphics || !res)
1357 return InvalidParameter;
1358
1359 *res = (region->node.type == RegionDataInfiniteRect);
1360
1361 return Ok;
1362 }
1363
1364 /*****************************************************************************
1365 * GdipIsVisibleRegionRect [GDIPLUS.@]
1366 */
1367 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1368 {
1369 HRGN hrgn;
1370 GpStatus stat;
1371 RECT rect;
1372
1373 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1374
1375 if(!region || !res)
1376 return InvalidParameter;
1377
1378 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1379 return stat;
1380
1381 /* infinite */
1382 if(!hrgn){
1383 *res = TRUE;
1384 return Ok;
1385 }
1386
1387 rect.left = ceilr(x);
1388 rect.top = ceilr(y);
1389 rect.right = ceilr(x + w);
1390 rect.bottom = ceilr(y + h);
1391
1392 *res = RectInRegion(hrgn, &rect);
1393
1394 DeleteObject(hrgn);
1395
1396 return Ok;
1397 }
1398
1399 /*****************************************************************************
1400 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1401 */
1402 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1403 {
1404 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1405 if(!region || !res)
1406 return InvalidParameter;
1407
1408 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1409 }
1410
1411 /*****************************************************************************
1412 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1413 */
1414 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1415 {
1416 HRGN hrgn;
1417 GpStatus stat;
1418
1419 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1420
1421 if(!region || !res)
1422 return InvalidParameter;
1423
1424 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1425 return stat;
1426
1427 /* infinite */
1428 if(!hrgn){
1429 *res = TRUE;
1430 return Ok;
1431 }
1432
1433 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1434
1435 DeleteObject(hrgn);
1436
1437 return Ok;
1438 }
1439
1440 /*****************************************************************************
1441 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1442 */
1443 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1444 {
1445 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1446
1447 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1448 }
1449
1450 /*****************************************************************************
1451 * GdipSetEmpty [GDIPLUS.@]
1452 */
1453 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1454 {
1455 GpStatus stat;
1456
1457 TRACE("%p\n", region);
1458
1459 if (!region)
1460 return InvalidParameter;
1461
1462 delete_element(&region->node);
1463 stat = init_region(region, RegionDataEmptyRect);
1464
1465 return stat;
1466 }
1467
1468 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1469 {
1470 GpStatus stat;
1471
1472 TRACE("%p\n", region);
1473
1474 if (!region)
1475 return InvalidParameter;
1476
1477 delete_element(&region->node);
1478 stat = init_region(region, RegionDataInfiniteRect);
1479
1480 return stat;
1481 }
1482
1483 /* Transforms GpRegion elements with given matrix */
1484 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1485 {
1486 GpStatus stat;
1487
1488 switch(element->type)
1489 {
1490 case RegionDataEmptyRect:
1491 case RegionDataInfiniteRect:
1492 return Ok;
1493 case RegionDataRect:
1494 {
1495 /* We can't transform a rectangle, so convert it to a path. */
1496 GpRegion *new_region;
1497 GpPath *path;
1498
1499 stat = GdipCreatePath(FillModeAlternate, &path);
1500 if (stat == Ok)
1501 {
1502 stat = GdipAddPathRectangle(path,
1503 element->elementdata.rect.X, element->elementdata.rect.Y,
1504 element->elementdata.rect.Width, element->elementdata.rect.Height);
1505
1506 if (stat == Ok)
1507 stat = GdipCreateRegionPath(path, &new_region);
1508
1509 GdipDeletePath(path);
1510 }
1511
1512 if (stat == Ok)
1513 {
1514 /* Steal the element from the created region. */
1515 memcpy(element, &new_region->node, sizeof(region_element));
1516 GdipFree(new_region);
1517 }
1518 else
1519 return stat;
1520 }
1521 /* Fall-through to do the actual conversion. */
1522 case RegionDataPath:
1523 if (!element->elementdata.path->pathdata.Count)
1524 return Ok;
1525
1526 stat = GdipTransformMatrixPoints(matrix,
1527 element->elementdata.path->pathdata.Points,
1528 element->elementdata.path->pathdata.Count);
1529 return stat;
1530 default:
1531 stat = transform_region_element(element->elementdata.combine.left, matrix);
1532 if (stat == Ok)
1533 stat = transform_region_element(element->elementdata.combine.right, matrix);
1534 return stat;
1535 }
1536 }
1537
1538 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1539 {
1540 TRACE("(%p, %p)\n", region, matrix);
1541
1542 if (!region || !matrix)
1543 return InvalidParameter;
1544
1545 return transform_region_element(&region->node, matrix);
1546 }
1547
1548 /* Translates GpRegion elements with specified offsets */
1549 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1550 {
1551 INT i;
1552
1553 switch(element->type)
1554 {
1555 case RegionDataEmptyRect:
1556 case RegionDataInfiniteRect:
1557 return;
1558 case RegionDataRect:
1559 element->elementdata.rect.X += dx;
1560 element->elementdata.rect.Y += dy;
1561 return;
1562 case RegionDataPath:
1563 for(i = 0; i < element->elementdata.path->pathdata.Count; i++){
1564 element->elementdata.path->pathdata.Points[i].X += dx;
1565 element->elementdata.path->pathdata.Points[i].Y += dy;
1566 }
1567 return;
1568 default:
1569 translate_region_element(element->elementdata.combine.left, dx, dy);
1570 translate_region_element(element->elementdata.combine.right, dx, dy);
1571 return;
1572 }
1573 }
1574
1575 /*****************************************************************************
1576 * GdipTranslateRegion [GDIPLUS.@]
1577 */
1578 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1579 {
1580 TRACE("(%p, %f, %f)\n", region, dx, dy);
1581
1582 if(!region)
1583 return InvalidParameter;
1584
1585 translate_region_element(&region->node, dx, dy);
1586
1587 return Ok;
1588 }
1589
1590 /*****************************************************************************
1591 * GdipTranslateRegionI [GDIPLUS.@]
1592 */
1593 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1594 {
1595 TRACE("(%p, %d, %d)\n", region, dx, dy);
1596
1597 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1598 }
1599
1600 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1601 {
1602 GpRegion *region_copy;
1603 GpStatus stat;
1604 HRGN hrgn;
1605 DWORD data_size;
1606
1607 stat = GdipCloneRegion(region, &region_copy);
1608
1609 if (stat == Ok)
1610 {
1611 stat = GdipTransformRegion(region_copy, matrix);
1612
1613 if (stat == Ok)
1614 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1615
1616 if (stat == Ok)
1617 {
1618 if (hrgn)
1619 {
1620 data_size = GetRegionData(hrgn, 0, NULL);
1621
1622 *data = GdipAlloc(data_size);
1623
1624 if (*data)
1625 GetRegionData(hrgn, data_size, *data);
1626 else
1627 stat = OutOfMemory;
1628
1629 DeleteObject(hrgn);
1630 }
1631 else
1632 {
1633 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1634
1635 *data = GdipAlloc(data_size);
1636
1637 if (*data)
1638 {
1639 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1640 (*data)->rdh.iType = RDH_RECTANGLES;
1641 (*data)->rdh.nCount = 1;
1642 (*data)->rdh.nRgnSize = sizeof(RECT);
1643 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1644 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1645
1646 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1647 }
1648 else
1649 stat = OutOfMemory;
1650 }
1651 }
1652
1653 GdipDeleteRegion(region_copy);
1654 }
1655
1656 return stat;
1657 }
1658
1659 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1660 {
1661 GpStatus stat;
1662 LPRGNDATA data;
1663
1664 TRACE("(%p, %p, %p)\n", region, count, matrix);
1665
1666 if (!region || !count || !matrix)
1667 return InvalidParameter;
1668
1669 stat = get_region_scans_data(region, matrix, &data);
1670
1671 if (stat == Ok)
1672 {
1673 *count = data->rdh.nCount;
1674 GdipFree(data);
1675 }
1676
1677 return stat;
1678 }
1679
1680 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1681 {
1682 GpStatus stat;
1683 DWORD i;
1684 LPRGNDATA data;
1685 RECT *rects;
1686
1687 if (!region || !count || !matrix)
1688 return InvalidParameter;
1689
1690 stat = get_region_scans_data(region, matrix, &data);
1691
1692 if (stat == Ok)
1693 {
1694 *count = data->rdh.nCount;
1695 rects = (RECT*)data->Buffer;
1696
1697 if (scans)
1698 {
1699 for (i=0; i<data->rdh.nCount; i++)
1700 {
1701 scans[i].X = rects[i].left;
1702 scans[i].Y = rects[i].top;
1703 scans[i].Width = rects[i].right - rects[i].left;
1704 scans[i].Height = rects[i].bottom - rects[i].top;
1705 }
1706 }
1707
1708 GdipFree(data);
1709 }
1710
1711 return Ok;
1712 }
1713
1714 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1715 {
1716 GpStatus stat;
1717 DWORD i;
1718 LPRGNDATA data;
1719 RECT *rects;
1720
1721 if (!region || !count || !matrix)
1722 return InvalidParameter;
1723
1724 stat = get_region_scans_data(region, matrix, &data);
1725
1726 if (stat == Ok)
1727 {
1728 *count = data->rdh.nCount;
1729 rects = (RECT*)data->Buffer;
1730
1731 if (scans)
1732 {
1733 for (i=0; i<data->rdh.nCount; i++)
1734 {
1735 scans[i].X = rects[i].left;
1736 scans[i].Y = rects[i].top;
1737 scans[i].Width = rects[i].right - rects[i].left;
1738 scans[i].Height = rects[i].bottom - rects[i].top;
1739 }
1740 }
1741
1742 GdipFree(data);
1743 }
1744
1745 return Ok;
1746 }