winex11.drv: Correct sizes in COMPOSITIONSTRING structure when updating fields.
[wine.git] / dlls / gdiplus / region.c
bloba0c0823ef6840cf33d102eb6c00f42d06d7aa1e6
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
3 * Copyright (C) 2013 Dmitry Timoshkov
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.
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.
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
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
26 #include "objbase.h"
28 #include "gdiplus.h"
29 #include "gdiplus_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34 /**********************************************************
36 * Data returned by GdipGetRegionData looks something like this:
38 * struct region_data_header
39 * {
40 * DWORD size; size in bytes of the data - 8.
41 * DWORD magic1; probably a checksum.
42 * DWORD magic2; always seems to be 0xdbc01001 - version?
43 * DWORD num_ops; number of combining ops * 2
44 * };
46 * Then follows a sequence of combining ops and region elements.
48 * A region element is either a RECTF or some path data.
50 * Combining ops are just stored as their CombineMode value.
52 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
53 * stored as 0x10000002 (with no following RECTF) and an infinite rect
54 * is stored as 0x10000003 (again with no following RECTF).
56 * Path data is preceded by the DWORD 0x10000001. Then follows a
57 * DWORD size and then size bytes of data.
59 * The combining ops are stored in the reverse order to the region
60 * elements and in the reverse order to which the region was
61 * constructed.
63 * When two or more complex regions (ie those with more than one
64 * element) are combined, the combining op for the two regions comes
65 * first, then the combining ops for the region elements in region 1,
66 * followed by the region elements for region 1, then follows the
67 * combining ops for region 2 and finally region 2's region elements.
68 * Presumably you're supposed to use the 0x1000000x header to find the
69 * end of the op list (the count of the elements in each region is not
70 * stored).
72 * When a simple region (1 element) is combined, it's treated as if a
73 * single rect/path is being combined.
77 #define FLAGS_NOFLAGS 0x0
78 #define FLAGS_INTPATH 0x4000
80 struct memory_buffer
82 const BYTE *buffer;
83 INT size, pos;
86 struct region_header
88 DWORD size;
89 DWORD checksum;
90 DWORD magic;
91 DWORD num_children;
94 struct path_header
96 DWORD size;
97 DWORD magic;
98 DWORD count;
99 DWORD flags;
102 /* Header size as far as header->size is concerned. This doesn't include
103 * header->size or header->checksum
105 static const INT sizeheader_size = sizeof(DWORD) * 2;
107 typedef struct packed_point
109 short X;
110 short Y;
111 } packed_point;
113 /* Test to see if the path could be stored as an array of shorts */
114 static BOOL is_integer_path(const GpPath *path)
116 int i;
118 if (!path->pathdata.Count) return FALSE;
120 for (i = 0; i < path->pathdata.Count; i++)
122 short x, y;
123 x = gdip_round(path->pathdata.Points[i].X);
124 y = gdip_round(path->pathdata.Points[i].Y);
125 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
126 return FALSE;
128 return TRUE;
131 /* Everything is measured in DWORDS; round up if there's a remainder */
132 static inline INT get_pathtypes_size(const GpPath* path)
134 INT needed = path->pathdata.Count / sizeof(DWORD);
136 if (path->pathdata.Count % sizeof(DWORD) > 0)
137 needed++;
139 return needed * sizeof(DWORD);
142 static inline INT get_element_size(const region_element* element)
144 INT needed = sizeof(DWORD); /* DWORD for the type */
145 switch(element->type)
147 case RegionDataRect:
148 return needed + sizeof(GpRect);
149 case RegionDataPath:
151 const GpPath *path = element->elementdata.path;
152 DWORD flags = is_integer_path(path) ? FLAGS_INTPATH : FLAGS_NOFLAGS;
153 /* 3 for headers, once again size doesn't count itself */
154 needed += sizeof(DWORD) * 3;
155 if (flags & FLAGS_INTPATH)
156 needed += 2 * sizeof(SHORT) * path->pathdata.Count;
157 else
158 needed += 2 * sizeof(FLOAT) * path->pathdata.Count;
160 needed += get_pathtypes_size(path);
161 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
162 return needed;
164 case RegionDataEmptyRect:
165 case RegionDataInfiniteRect:
166 return needed;
167 default:
168 needed += get_element_size(element->elementdata.combine.left);
169 needed += get_element_size(element->elementdata.combine.right);
170 return needed;
173 return 0;
176 /* Does not check parameters, caller must do that */
177 static inline GpStatus init_region(GpRegion* region, const RegionType type)
179 region->node.type = type;
180 region->num_children = 0;
182 return Ok;
185 static inline GpStatus clone_element(const region_element* element,
186 region_element** element2)
188 GpStatus stat;
190 /* root node is allocated with GpRegion */
191 if(!*element2){
192 *element2 = heap_alloc_zero(sizeof(region_element));
193 if (!*element2)
194 return OutOfMemory;
197 (*element2)->type = element->type;
199 switch (element->type)
201 case RegionDataRect:
202 (*element2)->elementdata.rect = element->elementdata.rect;
203 return Ok;
204 case RegionDataEmptyRect:
205 case RegionDataInfiniteRect:
206 return Ok;
207 case RegionDataPath:
208 stat = GdipClonePath(element->elementdata.path, &(*element2)->elementdata.path);
209 if (stat == Ok) return Ok;
210 break;
211 default:
212 (*element2)->elementdata.combine.left = NULL;
213 (*element2)->elementdata.combine.right = NULL;
215 stat = clone_element(element->elementdata.combine.left,
216 &(*element2)->elementdata.combine.left);
217 if (stat == Ok)
219 stat = clone_element(element->elementdata.combine.right,
220 &(*element2)->elementdata.combine.right);
221 if (stat == Ok) return Ok;
223 break;
226 delete_element(*element2);
227 *element2 = NULL;
228 return stat;
231 /* Common code for CombineRegion*
232 * All the caller has to do is get its format into an element
234 static inline void fuse_region(GpRegion* region, region_element* left,
235 region_element* right, const CombineMode mode)
237 region->node.type = mode;
238 region->node.elementdata.combine.left = left;
239 region->node.elementdata.combine.right = right;
240 region->num_children += 2;
243 /*****************************************************************************
244 * GdipCloneRegion [GDIPLUS.@]
246 * Creates a deep copy of the region
248 * PARAMS
249 * region [I] source region
250 * clone [O] resulting clone
252 * RETURNS
253 * SUCCESS: Ok
254 * FAILURE: InvalidParameter or OutOfMemory
256 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
258 region_element *element;
260 TRACE("%p %p\n", region, clone);
262 if (!(region && clone))
263 return InvalidParameter;
265 *clone = heap_alloc_zero(sizeof(GpRegion));
266 if (!*clone)
267 return OutOfMemory;
268 element = &(*clone)->node;
270 (*clone)->num_children = region->num_children;
271 return clone_element(&region->node, &element);
274 /*****************************************************************************
275 * GdipCombineRegionPath [GDIPLUS.@]
277 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
279 GpRegion *path_region;
280 region_element *left, *right = NULL;
281 GpStatus stat;
283 TRACE("%p %p %d\n", region, path, mode);
285 if (!(region && path))
286 return InvalidParameter;
288 stat = GdipCreateRegionPath(path, &path_region);
289 if (stat != Ok)
290 return stat;
292 /* simply replace region data */
293 if(mode == CombineModeReplace){
294 delete_element(&region->node);
295 memcpy(region, path_region, sizeof(GpRegion));
296 heap_free(path_region);
297 return Ok;
300 left = heap_alloc_zero(sizeof(region_element));
301 if (left)
303 *left = region->node;
304 stat = clone_element(&path_region->node, &right);
305 if (stat == Ok)
307 fuse_region(region, left, right, mode);
308 GdipDeleteRegion(path_region);
309 return Ok;
312 else
313 stat = OutOfMemory;
315 heap_free(left);
316 GdipDeleteRegion(path_region);
317 return stat;
320 /*****************************************************************************
321 * GdipCombineRegionRect [GDIPLUS.@]
323 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
324 GDIPCONST GpRectF *rect, CombineMode mode)
326 GpRegion *rect_region;
327 region_element *left, *right = NULL;
328 GpStatus stat;
330 TRACE("%p %s %d\n", region, debugstr_rectf(rect), mode);
332 if (!(region && rect))
333 return InvalidParameter;
335 stat = GdipCreateRegionRect(rect, &rect_region);
336 if (stat != Ok)
337 return stat;
339 /* simply replace region data */
340 if(mode == CombineModeReplace){
341 delete_element(&region->node);
342 memcpy(region, rect_region, sizeof(GpRegion));
343 heap_free(rect_region);
344 return Ok;
347 left = heap_alloc_zero(sizeof(region_element));
348 if (left)
350 memcpy(left, &region->node, sizeof(region_element));
351 stat = clone_element(&rect_region->node, &right);
352 if (stat == Ok)
354 fuse_region(region, left, right, mode);
355 GdipDeleteRegion(rect_region);
356 return Ok;
359 else
360 stat = OutOfMemory;
362 heap_free(left);
363 GdipDeleteRegion(rect_region);
364 return stat;
367 /*****************************************************************************
368 * GdipCombineRegionRectI [GDIPLUS.@]
370 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
371 GDIPCONST GpRect *rect, CombineMode mode)
373 GpRectF rectf;
375 TRACE("%p %p %d\n", region, rect, mode);
377 if (!rect)
378 return InvalidParameter;
380 rectf.X = (REAL)rect->X;
381 rectf.Y = (REAL)rect->Y;
382 rectf.Height = (REAL)rect->Height;
383 rectf.Width = (REAL)rect->Width;
385 return GdipCombineRegionRect(region, &rectf, mode);
388 /*****************************************************************************
389 * GdipCombineRegionRegion [GDIPLUS.@]
391 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
392 GpRegion *region2, CombineMode mode)
394 region_element *left, *right = NULL;
395 GpStatus stat;
396 GpRegion *reg2copy;
398 TRACE("%p %p %d\n", region1, region2, mode);
400 if(!(region1 && region2))
401 return InvalidParameter;
403 /* simply replace region data */
404 if(mode == CombineModeReplace){
405 stat = GdipCloneRegion(region2, &reg2copy);
406 if(stat != Ok) return stat;
408 delete_element(&region1->node);
409 memcpy(region1, reg2copy, sizeof(GpRegion));
410 heap_free(reg2copy);
411 return Ok;
414 left = heap_alloc_zero(sizeof(region_element));
415 if (!left)
416 return OutOfMemory;
418 *left = region1->node;
419 stat = clone_element(&region2->node, &right);
420 if (stat != Ok)
422 heap_free(left);
423 return OutOfMemory;
426 fuse_region(region1, left, right, mode);
427 region1->num_children += region2->num_children;
429 return Ok;
432 /*****************************************************************************
433 * GdipCreateRegion [GDIPLUS.@]
435 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
437 TRACE("%p\n", region);
439 if(!region)
440 return InvalidParameter;
442 *region = heap_alloc_zero(sizeof(GpRegion));
443 if(!*region)
444 return OutOfMemory;
446 TRACE("=> %p\n", *region);
448 return init_region(*region, RegionDataInfiniteRect);
451 /*****************************************************************************
452 * GdipCreateRegionPath [GDIPLUS.@]
454 * Creates a GpRegion from a GpPath
456 * PARAMS
457 * path [I] path to base the region on
458 * region [O] pointer to the newly allocated region
460 * RETURNS
461 * SUCCESS: Ok
462 * FAILURE: InvalidParameter
464 * NOTES
465 * If a path has no floating point points, its points will be stored as shorts
466 * (INTPATH)
468 * If a path is empty, it is considered to be an INTPATH
470 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
472 region_element* element;
473 GpStatus stat;
475 TRACE("%p, %p\n", path, region);
477 if (!(path && region))
478 return InvalidParameter;
480 *region = heap_alloc_zero(sizeof(GpRegion));
481 if(!*region)
482 return OutOfMemory;
483 stat = init_region(*region, RegionDataPath);
484 if (stat != Ok)
486 GdipDeleteRegion(*region);
487 return stat;
489 element = &(*region)->node;
491 stat = GdipClonePath(path, &element->elementdata.path);
492 if (stat != Ok)
494 GdipDeleteRegion(*region);
495 return stat;
498 return Ok;
501 /*****************************************************************************
502 * GdipCreateRegionRect [GDIPLUS.@]
504 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
505 GpRegion **region)
507 GpStatus stat;
509 TRACE("%p, %p\n", rect, region);
511 if (!(rect && region))
512 return InvalidParameter;
514 *region = heap_alloc_zero(sizeof(GpRegion));
515 stat = init_region(*region, RegionDataRect);
516 if(stat != Ok)
518 GdipDeleteRegion(*region);
519 return stat;
522 (*region)->node.elementdata.rect.X = rect->X;
523 (*region)->node.elementdata.rect.Y = rect->Y;
524 (*region)->node.elementdata.rect.Width = rect->Width;
525 (*region)->node.elementdata.rect.Height = rect->Height;
527 return Ok;
530 /*****************************************************************************
531 * GdipCreateRegionRectI [GDIPLUS.@]
533 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
534 GpRegion **region)
536 GpRectF rectf;
538 TRACE("%p, %p\n", rect, region);
540 rectf.X = (REAL)rect->X;
541 rectf.Y = (REAL)rect->Y;
542 rectf.Width = (REAL)rect->Width;
543 rectf.Height = (REAL)rect->Height;
545 return GdipCreateRegionRect(&rectf, region);
548 /******************************************************************************
549 * GdipCreateRegionHrgn [GDIPLUS.@]
551 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
553 DWORD size;
554 LPRGNDATA buf;
555 LPRECT rect;
556 GpStatus stat;
557 GpPath* path;
558 GpRegion* local;
559 DWORD i;
561 TRACE("(%p, %p)\n", hrgn, region);
563 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
564 return InvalidParameter;
566 buf = heap_alloc_zero(size);
567 if(!buf)
568 return OutOfMemory;
570 if(!GetRegionData(hrgn, size, buf)){
571 heap_free(buf);
572 return GenericError;
575 if(buf->rdh.nCount == 0){
576 if((stat = GdipCreateRegion(&local)) != Ok){
577 heap_free(buf);
578 return stat;
580 if((stat = GdipSetEmpty(local)) != Ok){
581 heap_free(buf);
582 GdipDeleteRegion(local);
583 return stat;
585 *region = local;
586 heap_free(buf);
587 return Ok;
590 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
591 heap_free(buf);
592 return stat;
595 rect = (LPRECT)buf->Buffer;
596 for(i = 0; i < buf->rdh.nCount; i++){
597 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
598 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
599 heap_free(buf);
600 GdipDeletePath(path);
601 return stat;
603 rect++;
606 stat = GdipCreateRegionPath(path, region);
608 heap_free(buf);
609 GdipDeletePath(path);
610 return stat;
613 /*****************************************************************************
614 * GdipDeleteRegion [GDIPLUS.@]
616 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
618 TRACE("%p\n", region);
620 if (!region)
621 return InvalidParameter;
623 delete_element(&region->node);
624 heap_free(region);
626 return Ok;
629 /*****************************************************************************
630 * GdipGetRegionBounds [GDIPLUS.@]
632 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
634 HRGN hrgn;
635 RECT r;
636 GpStatus status;
638 TRACE("(%p, %p, %p)\n", region, graphics, rect);
640 if(!region || !graphics || !rect)
641 return InvalidParameter;
643 /* Contrary to MSDN, native ignores the graphics transform. */
644 status = GdipGetRegionHRgn(region, NULL, &hrgn);
645 if(status != Ok)
646 return status;
648 /* infinite */
649 if(!hrgn){
650 rect->X = rect->Y = -(REAL)(1 << 22);
651 rect->Width = rect->Height = (REAL)(1 << 23);
652 TRACE("%p => infinite\n", region);
653 return Ok;
656 if(GetRgnBox(hrgn, &r)){
657 rect->X = r.left;
658 rect->Y = r.top;
659 rect->Width = r.right - r.left;
660 rect->Height = r.bottom - r.top;
661 TRACE("%p => %s\n", region, debugstr_rectf(rect));
663 else
664 status = GenericError;
666 DeleteObject(hrgn);
668 return status;
671 /*****************************************************************************
672 * GdipGetRegionBoundsI [GDIPLUS.@]
674 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
676 GpRectF rectf;
677 GpStatus status;
679 TRACE("(%p, %p, %p)\n", region, graphics, rect);
681 if(!rect)
682 return InvalidParameter;
684 status = GdipGetRegionBounds(region, graphics, &rectf);
685 if(status == Ok){
686 rect->X = gdip_round(rectf.X);
687 rect->Y = gdip_round(rectf.Y);
688 rect->Width = gdip_round(rectf.Width);
689 rect->Height = gdip_round(rectf.Height);
692 return status;
695 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
697 location[*offset] = write;
698 (*offset)++;
701 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
703 ((FLOAT*)location)[*offset] = write;
704 (*offset)++;
707 static inline void write_packed_point(DWORD* location, INT* offset,
708 const GpPointF* write)
710 packed_point *point = (packed_point *)(location + *offset);
711 point->X = gdip_round(write->X);
712 point->Y = gdip_round(write->Y);
713 (*offset)++;
716 static inline void write_path_types(DWORD* location, INT* offset,
717 const GpPath* path)
719 INT rounded_size = get_pathtypes_size(path);
721 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
723 /* The unwritten parts of the DWORD (if any) must be cleared */
724 if (rounded_size - path->pathdata.Count)
725 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
726 path->pathdata.Count, rounded_size - path->pathdata.Count);
727 *offset += rounded_size / sizeof(DWORD);
730 static void write_element(const region_element* element, DWORD *buffer,
731 INT* filled)
733 write_dword(buffer, filled, element->type);
734 switch (element->type)
736 case CombineModeReplace:
737 case CombineModeIntersect:
738 case CombineModeUnion:
739 case CombineModeXor:
740 case CombineModeExclude:
741 case CombineModeComplement:
742 write_element(element->elementdata.combine.left, buffer, filled);
743 write_element(element->elementdata.combine.right, buffer, filled);
744 break;
745 case RegionDataRect:
746 write_float(buffer, filled, element->elementdata.rect.X);
747 write_float(buffer, filled, element->elementdata.rect.Y);
748 write_float(buffer, filled, element->elementdata.rect.Width);
749 write_float(buffer, filled, element->elementdata.rect.Height);
750 break;
751 case RegionDataPath:
753 INT i;
754 const GpPath* path = element->elementdata.path;
755 struct path_header *pathheader;
757 pathheader = (struct path_header *)(buffer + *filled);
759 pathheader->flags = is_integer_path(path) ? FLAGS_INTPATH : FLAGS_NOFLAGS;
760 /* 3 for headers, once again size doesn't count itself */
761 pathheader->size = sizeof(DWORD) * 3;
762 if (pathheader->flags & FLAGS_INTPATH)
763 pathheader->size += 2 * sizeof(SHORT) * path->pathdata.Count;
764 else
765 pathheader->size += 2 * sizeof(FLOAT) * path->pathdata.Count;
766 pathheader->size += get_pathtypes_size(path);
767 pathheader->magic = VERSION_MAGIC;
768 pathheader->count = path->pathdata.Count;
770 *filled += 4;
772 switch (pathheader->flags & FLAGS_INTPATH)
774 case FLAGS_NOFLAGS:
775 for (i = 0; i < path->pathdata.Count; i++)
777 write_float(buffer, filled, path->pathdata.Points[i].X);
778 write_float(buffer, filled, path->pathdata.Points[i].Y);
780 break;
781 case FLAGS_INTPATH:
782 for (i = 0; i < path->pathdata.Count; i++)
784 write_packed_point(buffer, filled,
785 &path->pathdata.Points[i]);
787 break;
789 write_path_types(buffer, filled, path);
790 break;
792 case RegionDataEmptyRect:
793 case RegionDataInfiniteRect:
794 break;
798 /*****************************************************************************
799 * GdipGetRegionData [GDIPLUS.@]
801 * Returns the header, followed by combining ops and region elements.
803 * PARAMS
804 * region [I] region to retrieve from
805 * buffer [O] buffer to hold the resulting data
806 * size [I] size of the buffer
807 * needed [O] (optional) how much data was written
809 * RETURNS
810 * SUCCESS: Ok
811 * FAILURE: InvalidParameter
813 * NOTES
814 * The header contains the size, a checksum, a version string, and the number
815 * of children. The size does not count itself or the checksum.
816 * Version is always something like 0xdbc01001 or 0xdbc01002
818 * An element is a RECT, or PATH; Combining ops are stored as their
819 * CombineMode value. Special regions (infinite, empty) emit just their
820 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
821 * their code followed by a second header for the path followed by the actual
822 * path data. Followed by the flags for each point. The pathheader contains
823 * the size of the data to follow, a version number again, followed by a count
824 * of how many points, and any special flags which may apply. 0x4000 means its
825 * a path of shorts instead of FLOAT.
827 * Combining Ops are stored in reverse order from when they were constructed;
828 * the output is a tree where the left side combining area is always taken
829 * first.
831 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
832 UINT *needed)
834 struct region_header *region_header;
835 INT filled = 0;
836 UINT required;
837 GpStatus status;
839 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
841 if (!region || !buffer || !size)
842 return InvalidParameter;
844 status = GdipGetRegionDataSize(region, &required);
845 if (status != Ok) return status;
846 if (size < required)
848 if (needed) *needed = size;
849 return InsufficientBuffer;
852 region_header = (struct region_header *)buffer;
853 region_header->size = sizeheader_size + get_element_size(&region->node);
854 region_header->checksum = 0;
855 region_header->magic = VERSION_MAGIC;
856 region_header->num_children = region->num_children;
857 filled += 4;
858 /* With few exceptions, everything written is DWORD aligned,
859 * so use that as our base */
860 write_element(&region->node, (DWORD*)buffer, &filled);
862 if (needed)
863 *needed = filled * sizeof(DWORD);
865 return Ok;
868 static inline void init_memory_buffer(struct memory_buffer *mbuf, const BYTE *buffer, INT size)
870 mbuf->buffer = buffer;
871 mbuf->size = size;
872 mbuf->pos = 0;
875 static inline const void *buffer_read(struct memory_buffer *mbuf, INT size)
877 if (mbuf->size - mbuf->pos >= size)
879 const void *data = mbuf->buffer + mbuf->pos;
880 mbuf->pos += size;
881 return data;
883 return NULL;
886 static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count)
888 GpStatus status;
889 const DWORD *type;
891 type = buffer_read(mbuf, sizeof(*type));
892 if (!type) return Ok;
894 TRACE("type %#x\n", *type);
896 node->type = *type;
898 switch (node->type)
900 case CombineModeReplace:
901 case CombineModeIntersect:
902 case CombineModeUnion:
903 case CombineModeXor:
904 case CombineModeExclude:
905 case CombineModeComplement:
907 region_element *left, *right;
909 left = heap_alloc_zero(sizeof(region_element));
910 if (!left) return OutOfMemory;
911 right = heap_alloc_zero(sizeof(region_element));
912 if (!right)
914 heap_free(left);
915 return OutOfMemory;
918 status = read_element(mbuf, region, left, count);
919 if (status == Ok)
921 status = read_element(mbuf, region, right, count);
922 if (status == Ok)
924 node->elementdata.combine.left = left;
925 node->elementdata.combine.right = right;
926 region->num_children += 2;
927 return Ok;
931 heap_free(left);
932 heap_free(right);
933 return status;
936 case RegionDataRect:
938 const GpRectF *rc;
940 rc = buffer_read(mbuf, sizeof(*rc));
941 if (!rc)
943 ERR("failed to read rect data\n");
944 return InvalidParameter;
947 node->elementdata.rect = *rc;
948 *count += 1;
949 return Ok;
952 case RegionDataPath:
954 GpPath *path;
955 const struct path_header *path_header;
956 const BYTE *types;
958 path_header = buffer_read(mbuf, sizeof(*path_header));
959 if (!path_header)
961 ERR("failed to read path header\n");
962 return InvalidParameter;
964 if (path_header->magic != VERSION_MAGIC)
966 ERR("invalid path header magic %#x\n", path_header->magic);
967 return InvalidParameter;
970 /* Windows always fails to create an empty path in a region */
971 if (!path_header->count)
973 TRACE("refusing to create an empty path in a region\n");
974 return GenericError;
977 status = GdipCreatePath(FillModeAlternate, &path);
978 if (status) return status;
980 node->elementdata.path = path;
982 if (!lengthen_path(path, path_header->count))
983 return OutOfMemory;
985 path->pathdata.Count = path_header->count;
987 if (path_header->flags & ~FLAGS_INTPATH)
988 FIXME("unhandled path flags %#x\n", path_header->flags);
990 if (path_header->flags & FLAGS_INTPATH)
992 const packed_point *pt;
993 DWORD i;
995 pt = buffer_read(mbuf, sizeof(*pt) * path_header->count);
996 if (!pt)
998 ERR("failed to read packed %u path points\n", path_header->count);
999 return InvalidParameter;
1002 for (i = 0; i < path_header->count; i++)
1004 path->pathdata.Points[i].X = (REAL)pt[i].X;
1005 path->pathdata.Points[i].Y = (REAL)pt[i].Y;
1008 else
1010 const GpPointF *ptf;
1012 ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count);
1013 if (!ptf)
1015 ERR("failed to read %u path points\n", path_header->count);
1016 return InvalidParameter;
1018 memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count);
1021 types = buffer_read(mbuf, path_header->count);
1022 if (!types)
1024 ERR("failed to read %u path types\n", path_header->count);
1025 return InvalidParameter;
1027 memcpy(path->pathdata.Types, types, path_header->count);
1028 if (path_header->count & 3)
1030 if (!buffer_read(mbuf, 4 - (path_header->count & 3)))
1032 ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3));
1033 return InvalidParameter;
1037 *count += 1;
1038 return Ok;
1041 case RegionDataEmptyRect:
1042 case RegionDataInfiniteRect:
1043 *count += 1;
1044 return Ok;
1046 default:
1047 FIXME("element type %#x is not supported\n", *type);
1048 break;
1051 return InvalidParameter;
1054 /*****************************************************************************
1055 * GdipCreateRegionRgnData [GDIPLUS.@]
1057 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
1059 const struct region_header *region_header;
1060 struct memory_buffer mbuf;
1061 GpStatus status;
1062 INT count;
1064 TRACE("(%p, %d, %p)\n", data, size, region);
1066 if (!data || !size)
1067 return InvalidParameter;
1069 init_memory_buffer(&mbuf, data, size);
1071 region_header = buffer_read(&mbuf, sizeof(*region_header));
1072 if (!region_header || (region_header->magic != VERSION_MAGIC &&
1073 region_header->magic != VERSION_MAGIC2))
1074 return InvalidParameter;
1076 status = GdipCreateRegion(region);
1077 if (status != Ok)
1078 return status;
1080 count = 0;
1081 status = read_element(&mbuf, *region, &(*region)->node, &count);
1082 if (status == Ok && !count)
1083 status = InvalidParameter;
1085 if (status != Ok)
1087 GdipDeleteRegion(*region);
1088 *region = NULL;
1091 return status;
1094 /*****************************************************************************
1095 * GdipGetRegionDataSize [GDIPLUS.@]
1097 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
1099 TRACE("%p, %p\n", region, needed);
1101 if (!(region && needed))
1102 return InvalidParameter;
1104 /* header.size doesn't count header.size and header.checksum */
1105 *needed = sizeof(DWORD) * 2 + sizeheader_size + get_element_size(&region->node);
1107 return Ok;
1110 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
1112 HDC new_hdc=NULL;
1113 GpGraphics *new_graphics=NULL;
1114 GpStatus stat;
1115 INT save_state;
1117 if (!graphics)
1119 new_hdc = CreateCompatibleDC(0);
1120 if (!new_hdc)
1121 return OutOfMemory;
1123 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
1124 graphics = new_graphics;
1125 if (stat != Ok)
1127 DeleteDC(new_hdc);
1128 return stat;
1131 else if (!graphics->hdc)
1133 graphics->hdc = new_hdc = CreateCompatibleDC(0);
1134 if (!new_hdc)
1135 return OutOfMemory;
1138 save_state = SaveDC(graphics->hdc);
1139 EndPath(graphics->hdc);
1141 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1142 : WINDING));
1144 stat = trace_path(graphics, path);
1145 if (stat == Ok)
1147 *hrgn = PathToRegion(graphics->hdc);
1148 stat = *hrgn ? Ok : OutOfMemory;
1151 RestoreDC(graphics->hdc, save_state);
1152 if (new_hdc)
1154 DeleteDC(new_hdc);
1155 if (new_graphics)
1156 GdipDeleteGraphics(new_graphics);
1157 else
1158 graphics->hdc = NULL;
1161 return stat;
1164 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
1166 switch (element->type)
1168 case RegionDataInfiniteRect:
1169 *hrgn = NULL;
1170 return Ok;
1171 case RegionDataEmptyRect:
1172 *hrgn = CreateRectRgn(0, 0, 0, 0);
1173 return *hrgn ? Ok : OutOfMemory;
1174 case RegionDataPath:
1175 return get_path_hrgn(element->elementdata.path, graphics, hrgn);
1176 case RegionDataRect:
1178 GpPath* path;
1179 GpStatus stat;
1180 GpRectF* rc = &element->elementdata.rect;
1182 stat = GdipCreatePath(FillModeAlternate, &path);
1183 if (stat != Ok)
1184 return stat;
1185 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
1187 if (stat == Ok)
1188 stat = get_path_hrgn(path, graphics, hrgn);
1190 GdipDeletePath(path);
1192 return stat;
1194 case CombineModeIntersect:
1195 case CombineModeUnion:
1196 case CombineModeXor:
1197 case CombineModeExclude:
1198 case CombineModeComplement:
1200 HRGN left, right;
1201 GpStatus stat;
1202 int ret;
1204 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
1205 if (stat != Ok)
1207 *hrgn = NULL;
1208 return stat;
1211 if (left == NULL)
1213 /* existing region is infinite */
1214 switch (element->type)
1216 case CombineModeIntersect:
1217 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
1218 case CombineModeXor: case CombineModeExclude:
1219 left = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1220 break;
1221 case CombineModeUnion: case CombineModeComplement:
1222 *hrgn = NULL;
1223 return Ok;
1227 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1228 if (stat != Ok)
1230 DeleteObject(left);
1231 *hrgn = NULL;
1232 return stat;
1235 if (right == NULL)
1237 /* new region is infinite */
1238 switch (element->type)
1240 case CombineModeIntersect:
1241 *hrgn = left;
1242 return Ok;
1243 case CombineModeXor: case CombineModeComplement:
1244 right = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1245 break;
1246 case CombineModeUnion: case CombineModeExclude:
1247 DeleteObject(left);
1248 *hrgn = NULL;
1249 return Ok;
1253 switch (element->type)
1255 case CombineModeIntersect:
1256 ret = CombineRgn(left, left, right, RGN_AND);
1257 break;
1258 case CombineModeUnion:
1259 ret = CombineRgn(left, left, right, RGN_OR);
1260 break;
1261 case CombineModeXor:
1262 ret = CombineRgn(left, left, right, RGN_XOR);
1263 break;
1264 case CombineModeExclude:
1265 ret = CombineRgn(left, left, right, RGN_DIFF);
1266 break;
1267 case CombineModeComplement:
1268 ret = CombineRgn(left, right, left, RGN_DIFF);
1269 break;
1270 default:
1271 ret = ERROR;
1274 DeleteObject(right);
1276 if (ret == ERROR)
1278 DeleteObject(left);
1279 *hrgn = NULL;
1280 return GenericError;
1283 *hrgn = left;
1284 return Ok;
1286 default:
1287 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1288 *hrgn = NULL;
1289 return NotImplemented;
1293 /*****************************************************************************
1294 * GdipGetRegionHRgn [GDIPLUS.@]
1296 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1298 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1300 if (!region || !hrgn)
1301 return InvalidParameter;
1303 return get_region_hrgn(&region->node, graphics, hrgn);
1306 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1308 GpStatus status;
1309 GpRectF rect;
1311 TRACE("(%p, %p, %p)\n", region, graphics, res);
1313 if(!region || !graphics || !res)
1314 return InvalidParameter;
1316 status = GdipGetRegionBounds(region, graphics, &rect);
1317 if (status != Ok) return status;
1319 *res = rect.Width == 0.0 && rect.Height == 0.0;
1320 TRACE("=> %d\n", *res);
1322 return Ok;
1325 /*****************************************************************************
1326 * GdipIsEqualRegion [GDIPLUS.@]
1328 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1329 BOOL *res)
1331 HRGN hrgn1, hrgn2;
1332 GpStatus stat;
1334 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1336 if(!region || !region2 || !graphics || !res)
1337 return InvalidParameter;
1339 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1340 if(stat != Ok)
1341 return stat;
1342 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1343 if(stat != Ok){
1344 DeleteObject(hrgn1);
1345 return stat;
1348 *res = EqualRgn(hrgn1, hrgn2);
1350 /* one of GpRegions is infinite */
1351 if(*res == ERROR)
1352 *res = (!hrgn1 && !hrgn2);
1354 DeleteObject(hrgn1);
1355 DeleteObject(hrgn2);
1357 return Ok;
1360 /*****************************************************************************
1361 * GdipIsInfiniteRegion [GDIPLUS.@]
1363 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1365 /* I think graphics is ignored here */
1366 TRACE("(%p, %p, %p)\n", region, graphics, res);
1368 if(!region || !graphics || !res)
1369 return InvalidParameter;
1371 *res = (region->node.type == RegionDataInfiniteRect);
1373 return Ok;
1376 /*****************************************************************************
1377 * GdipIsVisibleRegionRect [GDIPLUS.@]
1379 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1381 HRGN hrgn;
1382 GpStatus stat;
1383 RECT rect;
1385 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1387 if(!region || !res)
1388 return InvalidParameter;
1390 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1391 return stat;
1393 /* infinite */
1394 if(!hrgn){
1395 *res = TRUE;
1396 return Ok;
1399 rect.left = ceilr(x);
1400 rect.top = ceilr(y);
1401 rect.right = ceilr(x + w);
1402 rect.bottom = ceilr(y + h);
1404 *res = RectInRegion(hrgn, &rect);
1406 DeleteObject(hrgn);
1408 return Ok;
1411 /*****************************************************************************
1412 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1414 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1416 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1417 if(!region || !res)
1418 return InvalidParameter;
1420 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1423 /*****************************************************************************
1424 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1426 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1428 HRGN hrgn;
1429 GpStatus stat;
1431 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1433 if(!region || !res)
1434 return InvalidParameter;
1436 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1437 return stat;
1439 /* infinite */
1440 if(!hrgn){
1441 *res = TRUE;
1442 return Ok;
1445 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1447 DeleteObject(hrgn);
1449 return Ok;
1452 /*****************************************************************************
1453 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1455 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1457 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1459 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1462 /*****************************************************************************
1463 * GdipSetEmpty [GDIPLUS.@]
1465 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1467 GpStatus stat;
1469 TRACE("%p\n", region);
1471 if (!region)
1472 return InvalidParameter;
1474 delete_element(&region->node);
1475 stat = init_region(region, RegionDataEmptyRect);
1477 return stat;
1480 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1482 GpStatus stat;
1484 TRACE("%p\n", region);
1486 if (!region)
1487 return InvalidParameter;
1489 delete_element(&region->node);
1490 stat = init_region(region, RegionDataInfiniteRect);
1492 return stat;
1495 /* Transforms GpRegion elements with given matrix */
1496 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1498 GpStatus stat;
1500 switch(element->type)
1502 case RegionDataEmptyRect:
1503 case RegionDataInfiniteRect:
1504 return Ok;
1505 case RegionDataRect:
1507 /* We can't transform a rectangle, so convert it to a path. */
1508 GpRegion *new_region;
1509 GpPath *path;
1511 stat = GdipCreatePath(FillModeAlternate, &path);
1512 if (stat == Ok)
1514 stat = GdipAddPathRectangle(path,
1515 element->elementdata.rect.X, element->elementdata.rect.Y,
1516 element->elementdata.rect.Width, element->elementdata.rect.Height);
1518 if (stat == Ok)
1519 stat = GdipCreateRegionPath(path, &new_region);
1521 GdipDeletePath(path);
1524 if (stat == Ok)
1526 /* Steal the element from the created region. */
1527 memcpy(element, &new_region->node, sizeof(region_element));
1528 heap_free(new_region);
1530 else
1531 return stat;
1533 /* Fall-through to do the actual conversion. */
1534 case RegionDataPath:
1535 if (!element->elementdata.path->pathdata.Count)
1536 return Ok;
1538 stat = GdipTransformMatrixPoints(matrix,
1539 element->elementdata.path->pathdata.Points,
1540 element->elementdata.path->pathdata.Count);
1541 return stat;
1542 default:
1543 stat = transform_region_element(element->elementdata.combine.left, matrix);
1544 if (stat == Ok)
1545 stat = transform_region_element(element->elementdata.combine.right, matrix);
1546 return stat;
1550 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1552 TRACE("(%p, %p)\n", region, matrix);
1554 if (!region || !matrix)
1555 return InvalidParameter;
1557 return transform_region_element(&region->node, matrix);
1560 /* Translates GpRegion elements with specified offsets */
1561 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1563 INT i;
1565 switch(element->type)
1567 case RegionDataEmptyRect:
1568 case RegionDataInfiniteRect:
1569 return;
1570 case RegionDataRect:
1571 element->elementdata.rect.X += dx;
1572 element->elementdata.rect.Y += dy;
1573 return;
1574 case RegionDataPath:
1575 for(i = 0; i < element->elementdata.path->pathdata.Count; i++){
1576 element->elementdata.path->pathdata.Points[i].X += dx;
1577 element->elementdata.path->pathdata.Points[i].Y += dy;
1579 return;
1580 default:
1581 translate_region_element(element->elementdata.combine.left, dx, dy);
1582 translate_region_element(element->elementdata.combine.right, dx, dy);
1583 return;
1587 /*****************************************************************************
1588 * GdipTranslateRegion [GDIPLUS.@]
1590 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1592 TRACE("(%p, %f, %f)\n", region, dx, dy);
1594 if(!region)
1595 return InvalidParameter;
1597 translate_region_element(&region->node, dx, dy);
1599 return Ok;
1602 /*****************************************************************************
1603 * GdipTranslateRegionI [GDIPLUS.@]
1605 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1607 TRACE("(%p, %d, %d)\n", region, dx, dy);
1609 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1612 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1614 GpRegion *region_copy;
1615 GpStatus stat;
1616 HRGN hrgn;
1617 DWORD data_size;
1619 stat = GdipCloneRegion(region, &region_copy);
1621 if (stat == Ok)
1623 stat = GdipTransformRegion(region_copy, matrix);
1625 if (stat == Ok)
1626 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1628 if (stat == Ok)
1630 if (hrgn)
1632 data_size = GetRegionData(hrgn, 0, NULL);
1634 *data = heap_alloc_zero(data_size);
1636 if (*data)
1637 GetRegionData(hrgn, data_size, *data);
1638 else
1639 stat = OutOfMemory;
1641 DeleteObject(hrgn);
1643 else
1645 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1647 *data = heap_alloc_zero(data_size);
1649 if (*data)
1651 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1652 (*data)->rdh.iType = RDH_RECTANGLES;
1653 (*data)->rdh.nCount = 1;
1654 (*data)->rdh.nRgnSize = sizeof(RECT);
1655 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1656 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1658 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1660 else
1661 stat = OutOfMemory;
1665 GdipDeleteRegion(region_copy);
1668 return stat;
1671 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1673 GpStatus stat;
1674 LPRGNDATA data;
1676 TRACE("(%p, %p, %p)\n", region, count, matrix);
1678 if (!region || !count || !matrix)
1679 return InvalidParameter;
1681 stat = get_region_scans_data(region, matrix, &data);
1683 if (stat == Ok)
1685 *count = data->rdh.nCount;
1686 heap_free(data);
1689 return stat;
1692 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1694 GpStatus stat;
1695 DWORD i;
1696 LPRGNDATA data;
1697 RECT *rects;
1699 if (!region || !count || !matrix)
1700 return InvalidParameter;
1702 stat = get_region_scans_data(region, matrix, &data);
1704 if (stat == Ok)
1706 *count = data->rdh.nCount;
1707 rects = (RECT*)data->Buffer;
1709 if (scans)
1711 for (i=0; i<data->rdh.nCount; i++)
1713 scans[i].X = rects[i].left;
1714 scans[i].Y = rects[i].top;
1715 scans[i].Width = rects[i].right - rects[i].left;
1716 scans[i].Height = rects[i].bottom - rects[i].top;
1720 heap_free(data);
1723 return Ok;
1726 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1728 GpStatus stat;
1729 DWORD i;
1730 LPRGNDATA data;
1731 RECT *rects;
1733 if (!region || !count || !matrix)
1734 return InvalidParameter;
1736 stat = get_region_scans_data(region, matrix, &data);
1738 if (stat == Ok)
1740 *count = data->rdh.nCount;
1741 rects = (RECT*)data->Buffer;
1743 if (scans)
1745 for (i=0; i<data->rdh.nCount; i++)
1747 scans[i].X = rects[i].left;
1748 scans[i].Y = rects[i].top;
1749 scans[i].Width = rects[i].right - rects[i].left;
1750 scans[i].Height = rects[i].bottom - rects[i].top;
1754 heap_free(data);
1757 return Ok;