2 * Copyright (C) 2008 Google (Lei Zhang)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33 /**********************************************************
35 * Data returned by GdipGetRegionData looks something like this:
37 * struct region_data_header
39 * DWORD size; size in bytes of the data - 8.
40 * DWORD magic1; probably a checksum.
41 * DWORD magic2; always seems to be 0xdbc01001 - version?
42 * DWORD num_ops; number of combining ops * 2
45 * Then follows a sequence of combining ops and region elements.
47 * A region element is either a RECTF or some path data.
49 * Combining ops are just stored as their CombineMode value.
51 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
52 * stored as 0x10000002 (with no following RECTF) and an infinite rect
53 * is stored as 0x10000003 (again with no following RECTF).
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
58 * The combining ops are stored in the reverse order to the region
59 * elements and in the reverse order to which the region was
62 * When two or more complex regions (ie those with more than one
63 * element) are combined, the combining op for the two regions comes
64 * first, then the combining ops for the region elements in region 1,
65 * followed by the region elements for region 1, then follows the
66 * combining ops for region 2 and finally region 2's region elements.
67 * Presumably you're supposed to use the 0x1000000x header to find the
68 * end of the op list (the count of the elements in each region is not
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
84 typedef struct packed_point
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
93 INT needed = path->pathdata.Count / sizeof(DWORD);
95 if (path->pathdata.Count % sizeof(DWORD) > 0)
98 return needed * sizeof(DWORD);
101 static inline INT get_element_size(const region_element* element)
103 INT needed = sizeof(DWORD); /* DWORD for the type */
104 switch(element->type)
107 return needed + sizeof(GpRect);
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
127 region->node.type = type;
128 region->header.checksum = 0xdeadbeef;
129 region->header.magic = VERSION_MAGIC;
130 region->header.num_children = 0;
131 region->header.size = sizeheader_size + get_element_size(®ion->node);
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
141 /* root node is allocated with GpRegion */
143 *element2 = GdipAlloc(sizeof(region_element));
148 (*element2)->type = element->type;
150 switch (element->type)
153 (*element2)->elementdata.rect = element->elementdata.rect;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
159 (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
160 stat = GdipClonePath(element->elementdata.pathdata.path,
161 &(*element2)->elementdata.pathdata.path);
162 if (stat != Ok) goto clone_out;
165 (*element2)->elementdata.combine.left = NULL;
166 (*element2)->elementdata.combine.right = NULL;
168 stat = clone_element(element->elementdata.combine.left,
169 &(*element2)->elementdata.combine.left);
170 if (stat != Ok) goto clone_out;
171 stat = clone_element(element->elementdata.combine.right,
172 &(*element2)->elementdata.combine.right);
173 if (stat != Ok) goto clone_out;
180 delete_element(*element2);
185 /* Common code for CombineRegion*
186 * All the caller has to do is get its format into an element
188 static inline void fuse_region(GpRegion* region, region_element* left,
189 region_element* right, const CombineMode mode)
191 region->node.type = mode;
192 region->node.elementdata.combine.left = left;
193 region->node.elementdata.combine.right = right;
195 region->header.size = sizeheader_size + get_element_size(®ion->node);
196 region->header.num_children += 2;
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
202 * Creates a deep copy of the region
205 * region [I] source region
206 * clone [O] resulting clone
210 * FAILURE: InvalidParameter or OutOfMemory
212 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
214 region_element *element;
216 TRACE("%p %p\n", region, clone);
218 if (!(region && clone))
219 return InvalidParameter;
221 *clone = GdipAlloc(sizeof(GpRegion));
224 element = &(*clone)->node;
226 (*clone)->header = region->header;
227 return clone_element(®ion->node, &element);
230 /*****************************************************************************
231 * GdipCombineRegionPath [GDIPLUS.@]
233 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
235 GpRegion *path_region;
236 region_element *left, *right = NULL;
239 TRACE("%p %p %d\n", region, path, mode);
241 if (!(region && path))
242 return InvalidParameter;
244 stat = GdipCreateRegionPath(path, &path_region);
248 /* simply replace region data */
249 if(mode == CombineModeReplace){
250 delete_element(®ion->node);
251 memcpy(region, path_region, sizeof(GpRegion));
252 GdipFree(path_region);
256 left = GdipAlloc(sizeof(region_element));
259 *left = region->node;
261 stat = clone_element(&path_region->node, &right);
265 fuse_region(region, left, right, mode);
267 GdipDeleteRegion(path_region);
272 GdipDeleteRegion(path_region);
276 /*****************************************************************************
277 * GdipCombineRegionRect [GDIPLUS.@]
279 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
280 GDIPCONST GpRectF *rect, CombineMode mode)
282 GpRegion *rect_region;
283 region_element *left, *right = NULL;
286 TRACE("%p %p %d\n", region, rect, mode);
288 if (!(region && rect))
289 return InvalidParameter;
291 stat = GdipCreateRegionRect(rect, &rect_region);
295 /* simply replace region data */
296 if(mode == CombineModeReplace){
297 delete_element(®ion->node);
298 memcpy(region, rect_region, sizeof(GpRegion));
299 GdipFree(rect_region);
303 left = GdipAlloc(sizeof(region_element));
306 memcpy(left, ®ion->node, sizeof(region_element));
308 stat = clone_element(&rect_region->node, &right);
312 fuse_region(region, left, right, mode);
314 GdipDeleteRegion(rect_region);
319 GdipDeleteRegion(rect_region);
323 /*****************************************************************************
324 * GdipCombineRegionRectI [GDIPLUS.@]
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327 GDIPCONST GpRect *rect, CombineMode mode)
331 TRACE("%p %p %d\n", region, rect, mode);
334 return InvalidParameter;
336 rectf.X = (REAL)rect->X;
337 rectf.Y = (REAL)rect->Y;
338 rectf.Height = (REAL)rect->Height;
339 rectf.Width = (REAL)rect->Width;
341 return GdipCombineRegionRect(region, &rectf, mode);
344 /*****************************************************************************
345 * GdipCombineRegionRegion [GDIPLUS.@]
347 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
348 GpRegion *region2, CombineMode mode)
350 region_element *left, *right = NULL;
354 TRACE("%p %p %d\n", region1, region2, mode);
356 if(!(region1 && region2))
357 return InvalidParameter;
359 /* simply replace region data */
360 if(mode == CombineModeReplace){
361 stat = GdipCloneRegion(region2, ®2copy);
362 if(stat != Ok) return stat;
364 delete_element(®ion1->node);
365 memcpy(region1, reg2copy, sizeof(GpRegion));
370 left = GdipAlloc(sizeof(region_element));
374 *left = region1->node;
375 stat = clone_element(®ion2->node, &right);
382 fuse_region(region1, left, right, mode);
383 region1->header.num_children += region2->header.num_children;
388 /*****************************************************************************
389 * GdipCreateRegion [GDIPLUS.@]
391 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
393 TRACE("%p\n", region);
396 return InvalidParameter;
398 *region = GdipAlloc(sizeof(GpRegion));
402 return init_region(*region, RegionDataInfiniteRect);
405 /*****************************************************************************
406 * GdipCreateRegionPath [GDIPLUS.@]
408 * Creates a GpRegion from a GpPath
411 * path [I] path to base the region on
412 * region [O] pointer to the newly allocated region
416 * FAILURE: InvalidParameter
419 * If a path has no floating point points, its points will be stored as shorts
422 * If a path is empty, it is considered to be an INTPATH
424 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
426 region_element* element;
431 DWORD flags = FLAGS_INTPATH;
434 TRACE("%p, %p\n", path, region);
436 if (!(path && region))
437 return InvalidParameter;
439 *region = GdipAlloc(sizeof(GpRegion));
442 stat = init_region(*region, RegionDataPath);
445 GdipDeleteRegion(*region);
448 element = &(*region)->node;
449 count = path->pathdata.Count;
451 /* Test to see if the path is an Integer path */
454 pointsi = GdipAlloc(sizeof(GpPoint) * count);
455 pointsf = GdipAlloc(sizeof(GpPointF) * count);
456 if (!(pointsi && pointsf))
460 GdipDeleteRegion(*region);
464 stat = GdipGetPathPointsI(path, pointsi, count);
467 GdipDeleteRegion(*region);
470 stat = GdipGetPathPoints(path, pointsf, count);
473 GdipDeleteRegion(*region);
477 for (i = 0; i < count; i++)
479 if (!(pointsi[i].X == pointsf[i].X &&
480 pointsi[i].Y == pointsf[i].Y ))
482 flags = FLAGS_NOFLAGS;
490 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
493 GdipDeleteRegion(*region);
497 /* 3 for headers, once again size doesn't count itself */
498 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
501 /* Floats, sent out as floats */
503 element->elementdata.pathdata.pathheader.size +=
504 (sizeof(DWORD) * count * 2);
506 /* INTs, sent out as packed shorts */
508 element->elementdata.pathdata.pathheader.size +=
509 (sizeof(DWORD) * count);
512 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
514 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
515 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
516 element->elementdata.pathdata.pathheader.count = count;
517 element->elementdata.pathdata.pathheader.flags = flags;
518 (*region)->header.size = sizeheader_size + get_element_size(element);
523 /*****************************************************************************
524 * GdipCreateRegionRect [GDIPLUS.@]
526 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
531 TRACE("%p, %p\n", rect, region);
533 if (!(rect && region))
534 return InvalidParameter;
536 *region = GdipAlloc(sizeof(GpRegion));
537 stat = init_region(*region, RegionDataRect);
540 GdipDeleteRegion(*region);
544 (*region)->node.elementdata.rect.X = rect->X;
545 (*region)->node.elementdata.rect.Y = rect->Y;
546 (*region)->node.elementdata.rect.Width = rect->Width;
547 (*region)->node.elementdata.rect.Height = rect->Height;
552 /*****************************************************************************
553 * GdipCreateRegionRectI [GDIPLUS.@]
555 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
560 TRACE("%p, %p\n", rect, region);
562 rectf.X = (REAL)rect->X;
563 rectf.Y = (REAL)rect->Y;
564 rectf.Width = (REAL)rect->Width;
565 rectf.Height = (REAL)rect->Height;
567 return GdipCreateRegionRect(&rectf, region);
570 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
572 FIXME("(%p, %d, %p): stub\n", data, size, region);
575 return NotImplemented;
579 /******************************************************************************
580 * GdipCreateRegionHrgn [GDIPLUS.@]
582 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
592 TRACE("(%p, %p)\n", hrgn, region);
594 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
595 return InvalidParameter;
597 buf = GdipAlloc(size);
601 if(!GetRegionData(hrgn, size, buf)){
606 if(buf->rdh.nCount == 0){
607 if((stat = GdipCreateRegion(&local)) != Ok){
611 if((stat = GdipSetEmpty(local)) != Ok){
613 GdipDeleteRegion(local);
621 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
626 rect = (LPRECT)buf->Buffer;
627 for(i = 0; i < buf->rdh.nCount; i++){
628 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
629 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
631 GdipDeletePath(path);
637 stat = GdipCreateRegionPath(path, region);
640 GdipDeletePath(path);
644 /*****************************************************************************
645 * GdipDeleteRegion [GDIPLUS.@]
647 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
649 TRACE("%p\n", region);
652 return InvalidParameter;
654 delete_element(®ion->node);
660 /*****************************************************************************
661 * GdipGetRegionBounds [GDIPLUS.@]
663 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
669 TRACE("(%p, %p, %p)\n", region, graphics, rect);
671 if(!region || !graphics || !rect)
672 return InvalidParameter;
674 /* Contrary to MSDN, native ignores the graphics transform. */
675 status = GdipGetRegionHRgn(region, NULL, &hrgn);
681 rect->X = rect->Y = -(REAL)(1 << 22);
682 rect->Width = rect->Height = (REAL)(1 << 23);
686 if(!GetRgnBox(hrgn, &r)){
693 rect->Width = r.right - r.left;
694 rect->Height = r.bottom - r.top;
699 /*****************************************************************************
700 * GdipGetRegionBoundsI [GDIPLUS.@]
702 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
707 TRACE("(%p, %p, %p)\n", region, graphics, rect);
710 return InvalidParameter;
712 status = GdipGetRegionBounds(region, graphics, &rectf);
714 rect->X = roundr(rectf.X);
715 rect->Y = roundr(rectf.X);
716 rect->Width = roundr(rectf.Width);
717 rect->Height = roundr(rectf.Height);
723 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
725 location[*offset] = write;
729 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
731 ((FLOAT*)location)[*offset] = write;
735 static inline void write_packed_point(DWORD* location, INT* offset,
736 const GpPointF* write)
742 memcpy(location + *offset, &point, sizeof(packed_point));
746 static inline void write_path_types(DWORD* location, INT* offset,
749 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
751 /* The unwritten parts of the DWORD (if any) must be cleared */
752 if (path->pathdata.Count % sizeof(DWORD))
753 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
754 path->pathdata.Count,
755 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
756 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
759 static void write_element(const region_element* element, DWORD *buffer,
762 write_dword(buffer, filled, element->type);
763 switch (element->type)
765 case CombineModeReplace:
766 case CombineModeIntersect:
767 case CombineModeUnion:
769 case CombineModeExclude:
770 case CombineModeComplement:
771 write_element(element->elementdata.combine.left, buffer, filled);
772 write_element(element->elementdata.combine.right, buffer, filled);
775 write_float(buffer, filled, element->elementdata.rect.X);
776 write_float(buffer, filled, element->elementdata.rect.Y);
777 write_float(buffer, filled, element->elementdata.rect.Width);
778 write_float(buffer, filled, element->elementdata.rect.Height);
783 const GpPath* path = element->elementdata.pathdata.path;
785 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
786 sizeof(element->elementdata.pathdata.pathheader));
787 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
788 switch (element->elementdata.pathdata.pathheader.flags)
791 for (i = 0; i < path->pathdata.Count; i++)
793 write_float(buffer, filled, path->pathdata.Points[i].X);
794 write_float(buffer, filled, path->pathdata.Points[i].Y);
798 for (i = 0; i < path->pathdata.Count; i++)
800 write_packed_point(buffer, filled,
801 &path->pathdata.Points[i]);
804 write_path_types(buffer, filled, path);
807 case RegionDataEmptyRect:
808 case RegionDataInfiniteRect:
813 /*****************************************************************************
814 * GdipGetRegionData [GDIPLUS.@]
816 * Returns the header, followed by combining ops and region elements.
819 * region [I] region to retrieve from
820 * buffer [O] buffer to hold the resulting data
821 * size [I] size of the buffer
822 * needed [O] (optional) how much data was written
826 * FAILURE: InvalidParameter
829 * The header contains the size, a checksum, a version string, and the number
830 * of children. The size does not count itself or the checksum.
831 * Version is always something like 0xdbc01001 or 0xdbc01002
833 * An element is a RECT, or PATH; Combining ops are stored as their
834 * CombineMode value. Special regions (infinite, empty) emit just their
835 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
836 * their code followed by a second header for the path followed by the actual
837 * path data. Followed by the flags for each point. The pathheader contains
838 * the size of the data to follow, a version number again, followed by a count
839 * of how many points, and any special flags which may apply. 0x4000 means its
840 * a path of shorts instead of FLOAT.
842 * Combining Ops are stored in reverse order from when they were constructed;
843 * the output is a tree where the left side combining area is always taken
846 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
851 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
853 if (!(region && buffer && size))
854 return InvalidParameter;
856 memcpy(buffer, ®ion->header, sizeof(region->header));
857 filled += sizeof(region->header) / sizeof(DWORD);
858 /* With few exceptions, everything written is DWORD aligned,
859 * so use that as our base */
860 write_element(®ion->node, (DWORD*)buffer, &filled);
863 *needed = filled * sizeof(DWORD);
868 /*****************************************************************************
869 * GdipGetRegionDataSize [GDIPLUS.@]
871 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
873 TRACE("%p, %p\n", region, needed);
875 if (!(region && needed))
876 return InvalidParameter;
878 /* header.size doesn't count header.size and header.checksum */
879 *needed = region->header.size + sizeof(DWORD) * 2;
884 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
887 GpGraphics *new_graphics=NULL;
897 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
898 graphics = new_graphics;
901 ReleaseDC(0, new_hdc);
905 else if (!graphics->hdc)
907 graphics->hdc = new_hdc = GetDC(0);
912 save_state = SaveDC(graphics->hdc);
913 EndPath(graphics->hdc);
915 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
918 stat = trace_path(graphics, path);
921 *hrgn = PathToRegion(graphics->hdc);
922 stat = *hrgn ? Ok : OutOfMemory;
925 RestoreDC(graphics->hdc, save_state);
928 ReleaseDC(0, new_hdc);
930 GdipDeleteGraphics(new_graphics);
932 graphics->hdc = NULL;
938 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
940 switch (element->type)
942 case RegionDataInfiniteRect:
945 case RegionDataEmptyRect:
946 *hrgn = CreateRectRgn(0, 0, 0, 0);
947 return *hrgn ? Ok : OutOfMemory;
949 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
954 GpRectF* rc = &element->elementdata.rect;
956 stat = GdipCreatePath(FillModeAlternate, &path);
959 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
962 stat = get_path_hrgn(path, graphics, hrgn);
964 GdipDeletePath(path);
968 case CombineModeIntersect:
969 case CombineModeUnion:
971 case CombineModeExclude:
972 case CombineModeComplement:
978 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
987 /* existing region is infinite */
988 switch (element->type)
990 case CombineModeIntersect:
991 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
992 case CombineModeXor: case CombineModeExclude:
993 FIXME("cannot exclude from an infinite region\n");
995 case CombineModeUnion: case CombineModeComplement:
1001 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1011 /* new region is infinite */
1012 switch (element->type)
1014 case CombineModeIntersect:
1017 case CombineModeXor: case CombineModeComplement:
1018 FIXME("cannot exclude from an infinite region\n");
1020 case CombineModeUnion: case CombineModeExclude:
1027 switch (element->type)
1029 case CombineModeIntersect:
1030 ret = CombineRgn(left, left, right, RGN_AND);
1032 case CombineModeUnion:
1033 ret = CombineRgn(left, left, right, RGN_OR);
1035 case CombineModeXor:
1036 ret = CombineRgn(left, left, right, RGN_XOR);
1038 case CombineModeExclude:
1039 ret = CombineRgn(left, left, right, RGN_DIFF);
1041 case CombineModeComplement:
1042 ret = CombineRgn(left, right, left, RGN_DIFF);
1048 DeleteObject(right);
1054 return GenericError;
1061 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1063 return NotImplemented;
1067 /*****************************************************************************
1068 * GdipGetRegionHRgn [GDIPLUS.@]
1070 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1072 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1074 if (!region || !hrgn)
1075 return InvalidParameter;
1077 return get_region_hrgn(®ion->node, graphics, hrgn);
1080 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1082 TRACE("(%p, %p, %p)\n", region, graphics, res);
1084 if(!region || !graphics || !res)
1085 return InvalidParameter;
1087 *res = (region->node.type == RegionDataEmptyRect);
1092 /*****************************************************************************
1093 * GdipIsEqualRegion [GDIPLUS.@]
1095 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1101 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1103 if(!region || !region2 || !graphics || !res)
1104 return InvalidParameter;
1106 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1109 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1111 DeleteObject(hrgn1);
1115 *res = EqualRgn(hrgn1, hrgn2);
1117 /* one of GpRegions is infinite */
1119 *res = (!hrgn1 && !hrgn2);
1121 DeleteObject(hrgn1);
1122 DeleteObject(hrgn2);
1127 /*****************************************************************************
1128 * GdipIsInfiniteRegion [GDIPLUS.@]
1130 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1132 /* I think graphics is ignored here */
1133 TRACE("(%p, %p, %p)\n", region, graphics, res);
1135 if(!region || !graphics || !res)
1136 return InvalidParameter;
1138 *res = (region->node.type == RegionDataInfiniteRect);
1143 /*****************************************************************************
1144 * GdipIsVisibleRegionRect [GDIPLUS.@]
1146 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1152 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1155 return InvalidParameter;
1157 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1166 rect.left = ceilr(x);
1167 rect.top = ceilr(y);
1168 rect.right = ceilr(x + w);
1169 rect.bottom = ceilr(y + h);
1171 *res = RectInRegion(hrgn, &rect);
1178 /*****************************************************************************
1179 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1181 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1183 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1185 return InvalidParameter;
1187 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1190 /*****************************************************************************
1191 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1193 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1198 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1201 return InvalidParameter;
1203 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1212 *res = PtInRegion(hrgn, roundr(x), roundr(y));
1219 /*****************************************************************************
1220 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1222 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1224 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1226 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1229 /*****************************************************************************
1230 * GdipSetEmpty [GDIPLUS.@]
1232 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1236 TRACE("%p\n", region);
1239 return InvalidParameter;
1241 delete_element(®ion->node);
1242 stat = init_region(region, RegionDataEmptyRect);
1247 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1251 TRACE("%p\n", region);
1254 return InvalidParameter;
1256 delete_element(®ion->node);
1257 stat = init_region(region, RegionDataInfiniteRect);
1262 /* Transforms GpRegion elements with given matrix */
1263 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1267 switch(element->type)
1269 case RegionDataEmptyRect:
1270 case RegionDataInfiniteRect:
1272 case RegionDataRect:
1274 /* We can't transform a rectangle, so convert it to a path. */
1275 GpRegion *new_region;
1278 stat = GdipCreatePath(FillModeAlternate, &path);
1281 stat = GdipAddPathRectangle(path,
1282 element->elementdata.rect.X, element->elementdata.rect.Y,
1283 element->elementdata.rect.Width, element->elementdata.rect.Height);
1286 stat = GdipCreateRegionPath(path, &new_region);
1288 GdipDeletePath(path);
1293 /* Steal the element from the created region. */
1294 memcpy(element, &new_region->node, sizeof(region_element));
1295 HeapFree(GetProcessHeap(), 0, new_region);
1300 /* Fall-through to do the actual conversion. */
1301 case RegionDataPath:
1302 stat = GdipTransformMatrixPoints(matrix,
1303 element->elementdata.pathdata.path->pathdata.Points,
1304 element->elementdata.pathdata.path->pathdata.Count);
1307 stat = transform_region_element(element->elementdata.combine.left, matrix);
1309 stat = transform_region_element(element->elementdata.combine.right, matrix);
1314 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1316 TRACE("(%p, %p)\n", region, matrix);
1318 if (!region || !matrix)
1319 return InvalidParameter;
1321 return transform_region_element(®ion->node, matrix);
1324 /* Translates GpRegion elements with specified offsets */
1325 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1329 switch(element->type)
1331 case RegionDataEmptyRect:
1332 case RegionDataInfiniteRect:
1334 case RegionDataRect:
1335 element->elementdata.rect.X += dx;
1336 element->elementdata.rect.Y += dy;
1338 case RegionDataPath:
1339 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1340 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1341 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1345 translate_region_element(element->elementdata.combine.left, dx, dy);
1346 translate_region_element(element->elementdata.combine.right, dx, dy);
1351 /*****************************************************************************
1352 * GdipTranslateRegion [GDIPLUS.@]
1354 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1356 TRACE("(%p, %f, %f)\n", region, dx, dy);
1359 return InvalidParameter;
1361 translate_region_element(®ion->node, dx, dy);
1366 /*****************************************************************************
1367 * GdipTranslateRegionI [GDIPLUS.@]
1369 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1371 TRACE("(%p, %d, %d)\n", region, dx, dy);
1373 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1376 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1378 GpRegion *region_copy;
1383 stat = GdipCloneRegion(region, ®ion_copy);
1387 stat = GdipTransformRegion(region_copy, matrix);
1390 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1396 data_size = GetRegionData(hrgn, 0, NULL);
1398 *data = GdipAlloc(data_size);
1401 GetRegionData(hrgn, data_size, *data);
1409 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1411 *data = GdipAlloc(data_size);
1415 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1416 (*data)->rdh.iType = RDH_RECTANGLES;
1417 (*data)->rdh.nCount = 1;
1418 (*data)->rdh.nRgnSize = sizeof(RECT);
1419 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1420 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1422 memcpy(&(*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1429 GdipDeleteRegion(region_copy);
1435 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1440 TRACE("(%p, %p, %p)\n", region, count, matrix);
1442 if (!region || !count || !matrix)
1443 return InvalidParameter;
1445 stat = get_region_scans_data(region, matrix, &data);
1449 *count = data->rdh.nCount;
1456 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1463 if (!region || !count || !matrix)
1464 return InvalidParameter;
1466 stat = get_region_scans_data(region, matrix, &data);
1470 *count = data->rdh.nCount;
1471 rects = (RECT*)&data->Buffer;
1475 for (i=0; i<data->rdh.nCount; i++)
1477 scans[i].X = rects[i].left;
1478 scans[i].Y = rects[i].top;
1479 scans[i].Width = rects[i].right - rects[i].left;
1480 scans[i].Height = rects[i].bottom - rects[i].top;
1490 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1497 if (!region || !count || !matrix)
1498 return InvalidParameter;
1500 stat = get_region_scans_data(region, matrix, &data);
1504 *count = data->rdh.nCount;
1505 rects = (RECT*)&data->Buffer;
1509 for (i=0; i<data->rdh.nCount; i++)
1511 scans[i].X = rects[i].left;
1512 scans[i].Y = rects[i].top;
1513 scans[i].Width = rects[i].right - rects[i].left;
1514 scans[i].Height = rects[i].bottom - rects[i].top;