push 5aff8350ceade24f8243f07a9cf7ecb816236fb1
[wine/hacks.git] / dlls / gdiplus / region.c
blob039e789d6735ed69cee60445b6f07aa1a7054659
1 /*
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
25 #include "objbase.h"
27 #include "gdiplus.h"
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
38 * {
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
43 * };
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
60 * constructed.
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
69 * stored).
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
86 short X;
87 short Y;
88 } 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)
96 needed++;
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)
106 case RegionDataRect:
107 return needed + sizeof(GpRect);
108 case RegionDataPath:
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
111 return needed;
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
114 return needed;
115 default:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
118 return needed;
121 return 0;
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(&region->node);
133 return Ok;
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
139 GpStatus stat;
141 /* root node is allocated with GpRegion */
142 if(!*element2){
143 *element2 = GdipAlloc(sizeof(region_element));
144 if (!*element2)
145 return OutOfMemory;
148 (*element2)->type = element->type;
150 switch (element->type)
152 case RegionDataRect:
153 (*element2)->elementdata.rect = element->elementdata.rect;
154 break;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
157 break;
158 case RegionDataPath:
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;
163 break;
164 default:
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;
174 break;
177 return Ok;
179 clone_out:
180 delete_element(*element2);
181 *element2 = NULL;
182 return stat;
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(&region->node);
196 region->header.num_children += 2;
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
202 * Creates a deep copy of the region
204 * PARAMS
205 * region [I] source region
206 * clone [O] resulting clone
208 * RETURNS
209 * SUCCESS: Ok
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));
222 if (!*clone)
223 return OutOfMemory;
224 element = &(*clone)->node;
226 (*clone)->header = region->header;
227 return clone_element(&region->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;
237 GpStatus stat;
239 TRACE("%p %p %d\n", region, path, mode);
241 if (!(region && path))
242 return InvalidParameter;
244 stat = GdipCreateRegionPath(path, &path_region);
245 if (stat != Ok)
246 return stat;
248 /* simply replace region data */
249 if(mode == CombineModeReplace){
250 delete_element(&region->node);
251 memcpy(region, path_region, sizeof(GpRegion));
252 return Ok;
255 left = GdipAlloc(sizeof(region_element));
256 if (!left)
257 goto out;
258 *left = region->node;
260 stat = clone_element(&path_region->node, &right);
261 if (stat != Ok)
262 goto out;
264 fuse_region(region, left, right, mode);
266 GdipDeleteRegion(path_region);
267 return Ok;
269 out:
270 GdipFree(left);
271 GdipDeleteRegion(path_region);
272 return stat;
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
278 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
279 GDIPCONST GpRectF *rect, CombineMode mode)
281 GpRegion *rect_region;
282 region_element *left, *right = NULL;
283 GpStatus stat;
285 TRACE("%p %p %d\n", region, rect, mode);
287 if (!(region && rect))
288 return InvalidParameter;
290 stat = GdipCreateRegionRect(rect, &rect_region);
291 if (stat != Ok)
292 return stat;
294 /* simply replace region data */
295 if(mode == CombineModeReplace){
296 delete_element(&region->node);
297 memcpy(region, rect_region, sizeof(GpRegion));
298 return Ok;
301 left = GdipAlloc(sizeof(region_element));
302 if (!left)
303 goto out;
304 memcpy(left, &region->node, sizeof(region_element));
306 stat = clone_element(&rect_region->node, &right);
307 if (stat != Ok)
308 goto out;
310 fuse_region(region, left, right, mode);
312 GdipDeleteRegion(rect_region);
313 return Ok;
315 out:
316 GdipFree(left);
317 GdipDeleteRegion(rect_region);
318 return stat;
321 /*****************************************************************************
322 * GdipCombineRegionRectI [GDIPLUS.@]
324 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
325 GDIPCONST GpRect *rect, CombineMode mode)
327 GpRectF rectf;
329 TRACE("%p %p %d\n", region, rect, mode);
331 if (!rect)
332 return InvalidParameter;
334 rectf.X = (REAL)rect->X;
335 rectf.Y = (REAL)rect->Y;
336 rectf.Height = (REAL)rect->Height;
337 rectf.Width = (REAL)rect->Width;
339 return GdipCombineRegionRect(region, &rectf, mode);
342 /*****************************************************************************
343 * GdipCombineRegionRegion [GDIPLUS.@]
345 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
346 GpRegion *region2, CombineMode mode)
348 region_element *left, *right = NULL;
349 GpStatus stat;
350 GpRegion *reg2copy;
352 TRACE("%p %p %d\n", region1, region2, mode);
354 if(!(region1 && region2))
355 return InvalidParameter;
357 /* simply replace region data */
358 if(mode == CombineModeReplace){
359 stat = GdipCloneRegion(region2, &reg2copy);
360 if(stat != Ok) return stat;
362 delete_element(&region1->node);
363 memcpy(region1, reg2copy, sizeof(GpRegion));
364 GdipFree(reg2copy);
365 return Ok;
368 left = GdipAlloc(sizeof(region_element));
369 if (!left)
370 return OutOfMemory;
372 *left = region1->node;
373 stat = clone_element(&region2->node, &right);
374 if (stat != Ok)
376 GdipFree(left);
377 return OutOfMemory;
380 fuse_region(region1, left, right, mode);
381 region1->header.num_children += region2->header.num_children;
383 return Ok;
386 /*****************************************************************************
387 * GdipCreateRegion [GDIPLUS.@]
389 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
391 TRACE("%p\n", region);
393 if(!region)
394 return InvalidParameter;
396 *region = GdipAlloc(sizeof(GpRegion));
397 if(!*region)
398 return OutOfMemory;
400 return init_region(*region, RegionDataInfiniteRect);
403 /*****************************************************************************
404 * GdipCreateRegionPath [GDIPLUS.@]
406 * Creates a GpRegion from a GpPath
408 * PARAMS
409 * path [I] path to base the region on
410 * region [O] pointer to the newly allocated region
412 * RETURNS
413 * SUCCESS: Ok
414 * FAILURE: InvalidParameter
416 * NOTES
417 * If a path has no floating point points, its points will be stored as shorts
418 * (INTPATH)
420 * If a path is empty, it is considered to be an INTPATH
422 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
424 region_element* element;
425 GpPoint *pointsi;
426 GpPointF *pointsf;
428 GpStatus stat;
429 DWORD flags = FLAGS_INTPATH;
430 INT count, i;
432 TRACE("%p, %p\n", path, region);
434 if (!(path && region))
435 return InvalidParameter;
437 *region = GdipAlloc(sizeof(GpRegion));
438 if(!*region)
439 return OutOfMemory;
440 stat = init_region(*region, RegionDataPath);
441 if (stat != Ok)
443 GdipDeleteRegion(*region);
444 return stat;
446 element = &(*region)->node;
447 count = path->pathdata.Count;
449 /* Test to see if the path is an Integer path */
450 if (count)
452 pointsi = GdipAlloc(sizeof(GpPoint) * count);
453 pointsf = GdipAlloc(sizeof(GpPointF) * count);
454 if (!(pointsi && pointsf))
456 GdipFree(pointsi);
457 GdipFree(pointsf);
458 GdipDeleteRegion(*region);
459 return OutOfMemory;
462 stat = GdipGetPathPointsI(path, pointsi, count);
463 if (stat != Ok)
465 GdipDeleteRegion(*region);
466 return stat;
468 stat = GdipGetPathPoints(path, pointsf, count);
469 if (stat != Ok)
471 GdipDeleteRegion(*region);
472 return stat;
475 for (i = 0; i < count; i++)
477 if (!(pointsi[i].X == pointsf[i].X &&
478 pointsi[i].Y == pointsf[i].Y ))
480 flags = FLAGS_NOFLAGS;
481 break;
484 GdipFree(pointsi);
485 GdipFree(pointsf);
488 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
489 if (stat != Ok)
491 GdipDeleteRegion(*region);
492 return stat;
495 /* 3 for headers, once again size doesn't count itself */
496 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
497 switch(flags)
499 /* Floats, sent out as floats */
500 case FLAGS_NOFLAGS:
501 element->elementdata.pathdata.pathheader.size +=
502 (sizeof(DWORD) * count * 2);
503 break;
504 /* INTs, sent out as packed shorts */
505 case FLAGS_INTPATH:
506 element->elementdata.pathdata.pathheader.size +=
507 (sizeof(DWORD) * count);
508 break;
509 default:
510 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
512 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
513 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
514 element->elementdata.pathdata.pathheader.count = count;
515 element->elementdata.pathdata.pathheader.flags = flags;
516 (*region)->header.size = sizeheader_size + get_element_size(element);
518 return Ok;
521 /*****************************************************************************
522 * GdipCreateRegionRect [GDIPLUS.@]
524 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
525 GpRegion **region)
527 GpStatus stat;
529 TRACE("%p, %p\n", rect, region);
531 if (!(rect && region))
532 return InvalidParameter;
534 *region = GdipAlloc(sizeof(GpRegion));
535 stat = init_region(*region, RegionDataRect);
536 if(stat != Ok)
538 GdipDeleteRegion(*region);
539 return stat;
542 (*region)->node.elementdata.rect.X = rect->X;
543 (*region)->node.elementdata.rect.Y = rect->Y;
544 (*region)->node.elementdata.rect.Width = rect->Width;
545 (*region)->node.elementdata.rect.Height = rect->Height;
547 return Ok;
550 /*****************************************************************************
551 * GdipCreateRegionRectI [GDIPLUS.@]
553 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
554 GpRegion **region)
556 GpRectF rectf;
558 TRACE("%p, %p\n", rect, region);
560 rectf.X = (REAL)rect->X;
561 rectf.Y = (REAL)rect->Y;
562 rectf.Width = (REAL)rect->Width;
563 rectf.Height = (REAL)rect->Height;
565 return GdipCreateRegionRect(&rectf, region);
568 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
570 FIXME("(%p, %d, %p): stub\n", data, size, region);
572 *region = NULL;
573 return NotImplemented;
577 /******************************************************************************
578 * GdipCreateRegionHrgn [GDIPLUS.@]
580 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
582 union {
583 RGNDATA data;
584 char buf[sizeof(RGNDATAHEADER) + sizeof(RECT)];
585 } rdata;
586 DWORD size;
587 GpRectF rectf;
588 GpPath *path;
589 GpStatus stat;
591 TRACE("(%p, %p)\n", hrgn, region);
593 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
594 return InvalidParameter;
596 if(size > sizeof(RGNDATAHEADER) + sizeof(RECT)){
597 FIXME("Only simple rect regions supported.\n");
598 *region = NULL;
599 return NotImplemented;
602 if(!GetRegionData(hrgn, sizeof(rdata), &rdata.data))
603 return GenericError;
605 /* return empty region */
606 if(IsRectEmpty(&rdata.data.rdh.rcBound)){
607 stat = GdipCreateRegion(region);
608 if(stat == Ok)
609 GdipSetEmpty(*region);
610 return stat;
613 rectf.X = (REAL)rdata.data.rdh.rcBound.left;
614 rectf.Y = (REAL)rdata.data.rdh.rcBound.top;
615 rectf.Width = (REAL)rdata.data.rdh.rcBound.right - rectf.X;
616 rectf.Height = (REAL)rdata.data.rdh.rcBound.bottom - rectf.Y;
618 stat = GdipCreatePath(FillModeAlternate, &path);
619 if(stat != Ok)
620 return stat;
622 GdipAddPathRectangle(path, rectf.X, rectf.Y, rectf.Width, rectf.Height);
624 stat = GdipCreateRegionPath(path, region);
625 GdipDeletePath(path);
627 return stat;
630 /*****************************************************************************
631 * GdipDeleteRegion [GDIPLUS.@]
633 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
635 TRACE("%p\n", region);
637 if (!region)
638 return InvalidParameter;
640 delete_element(&region->node);
641 GdipFree(region);
643 return Ok;
646 /*****************************************************************************
647 * GdipGetRegionBounds [GDIPLUS.@]
649 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
651 HRGN hrgn;
652 RECT r;
653 GpStatus status;
655 TRACE("(%p, %p, %p)\n", region, graphics, rect);
657 if(!region || !graphics || !rect)
658 return InvalidParameter;
660 /* Contrary to MSDN, native ignores the graphics transform. */
661 status = GdipGetRegionHRgn(region, NULL, &hrgn);
662 if(status != Ok)
663 return status;
665 /* infinite */
666 if(!hrgn){
667 rect->X = rect->Y = -(REAL)(1 << 22);
668 rect->Width = rect->Height = (REAL)(1 << 23);
669 return Ok;
672 if(!GetRgnBox(hrgn, &r)){
673 DeleteObject(hrgn);
674 return GenericError;
677 rect->X = r.left;
678 rect->Y = r.top;
679 rect->Width = r.right - r.left;
680 rect->Height = r.bottom - r.top;
682 return Ok;
685 /*****************************************************************************
686 * GdipGetRegionBoundsI [GDIPLUS.@]
688 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
690 GpRectF rectf;
691 GpStatus status;
693 TRACE("(%p, %p, %p)\n", region, graphics, rect);
695 if(!rect)
696 return InvalidParameter;
698 status = GdipGetRegionBounds(region, graphics, &rectf);
699 if(status == Ok){
700 rect->X = roundr(rectf.X);
701 rect->Y = roundr(rectf.X);
702 rect->Width = roundr(rectf.Width);
703 rect->Height = roundr(rectf.Height);
706 return status;
709 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
711 location[*offset] = write;
712 (*offset)++;
715 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
717 ((FLOAT*)location)[*offset] = write;
718 (*offset)++;
721 static inline void write_packed_point(DWORD* location, INT* offset,
722 const GpPointF* write)
724 packed_point point;
726 point.X = write->X;
727 point.Y = write->Y;
728 memcpy(location + *offset, &point, sizeof(packed_point));
729 (*offset)++;
732 static inline void write_path_types(DWORD* location, INT* offset,
733 const GpPath* path)
735 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
737 /* The unwritten parts of the DWORD (if any) must be cleared */
738 if (path->pathdata.Count % sizeof(DWORD))
739 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
740 path->pathdata.Count,
741 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
742 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
745 static void write_element(const region_element* element, DWORD *buffer,
746 INT* filled)
748 write_dword(buffer, filled, element->type);
749 switch (element->type)
751 case CombineModeReplace:
752 case CombineModeIntersect:
753 case CombineModeUnion:
754 case CombineModeXor:
755 case CombineModeExclude:
756 case CombineModeComplement:
757 write_element(element->elementdata.combine.left, buffer, filled);
758 write_element(element->elementdata.combine.right, buffer, filled);
759 break;
760 case RegionDataRect:
761 write_float(buffer, filled, element->elementdata.rect.X);
762 write_float(buffer, filled, element->elementdata.rect.Y);
763 write_float(buffer, filled, element->elementdata.rect.Width);
764 write_float(buffer, filled, element->elementdata.rect.Height);
765 break;
766 case RegionDataPath:
768 INT i;
769 const GpPath* path = element->elementdata.pathdata.path;
771 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
772 sizeof(element->elementdata.pathdata.pathheader));
773 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
774 switch (element->elementdata.pathdata.pathheader.flags)
776 case FLAGS_NOFLAGS:
777 for (i = 0; i < path->pathdata.Count; i++)
779 write_float(buffer, filled, path->pathdata.Points[i].X);
780 write_float(buffer, filled, path->pathdata.Points[i].Y);
782 break;
783 case FLAGS_INTPATH:
784 for (i = 0; i < path->pathdata.Count; i++)
786 write_packed_point(buffer, filled,
787 &path->pathdata.Points[i]);
790 write_path_types(buffer, filled, path);
791 break;
793 case RegionDataEmptyRect:
794 case RegionDataInfiniteRect:
795 break;
799 /*****************************************************************************
800 * GdipGetRegionData [GDIPLUS.@]
802 * Returns the header, followed by combining ops and region elements.
804 * PARAMS
805 * region [I] region to retrieve from
806 * buffer [O] buffer to hold the resulting data
807 * size [I] size of the buffer
808 * needed [O] (optional) how much data was written
810 * RETURNS
811 * SUCCESS: Ok
812 * FAILURE: InvalidParameter
814 * NOTES
815 * The header contains the size, a checksum, a version string, and the number
816 * of children. The size does not count itself or the checksum.
817 * Version is always something like 0xdbc01001 or 0xdbc01002
819 * An element is a RECT, or PATH; Combining ops are stored as their
820 * CombineMode value. Special regions (infinite, empty) emit just their
821 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
822 * their code followed by a second header for the path followed by the actual
823 * path data. Followed by the flags for each point. The pathheader contains
824 * the size of the data to follow, a version number again, followed by a count
825 * of how many points, and any special flags which may apply. 0x4000 means its
826 * a path of shorts instead of FLOAT.
828 * Combining Ops are stored in reverse order from when they were constructed;
829 * the output is a tree where the left side combining area is always taken
830 * first.
832 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
833 UINT *needed)
835 INT filled = 0;
837 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
839 if (!(region && buffer && size))
840 return InvalidParameter;
842 memcpy(buffer, &region->header, sizeof(region->header));
843 filled += sizeof(region->header) / sizeof(DWORD);
844 /* With few exceptions, everything written is DWORD aligned,
845 * so use that as our base */
846 write_element(&region->node, (DWORD*)buffer, &filled);
848 if (needed)
849 *needed = filled * sizeof(DWORD);
851 return Ok;
854 /*****************************************************************************
855 * GdipGetRegionDataSize [GDIPLUS.@]
857 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
859 TRACE("%p, %p\n", region, needed);
861 if (!(region && needed))
862 return InvalidParameter;
864 /* header.size doesn't count header.size and header.checksum */
865 *needed = region->header.size + sizeof(DWORD) * 2;
867 return Ok;
870 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
872 HDC new_hdc=NULL;
873 GpStatus stat;
874 INT save_state;
876 if (!graphics)
878 new_hdc = GetDC(0);
879 if (!new_hdc)
880 return OutOfMemory;
882 stat = GdipCreateFromHDC(new_hdc, &graphics);
883 if (stat != Ok)
885 ReleaseDC(0, new_hdc);
886 return stat;
890 save_state = SaveDC(graphics->hdc);
891 EndPath(graphics->hdc);
893 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
894 : WINDING));
896 stat = trace_path(graphics, path);
897 if (stat == Ok)
899 *hrgn = PathToRegion(graphics->hdc);
900 stat = *hrgn ? Ok : OutOfMemory;
903 RestoreDC(graphics->hdc, save_state);
904 if (new_hdc)
906 ReleaseDC(0, new_hdc);
907 GdipDeleteGraphics(graphics);
910 return stat;
913 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
915 switch (element->type)
917 case RegionDataInfiniteRect:
918 *hrgn = NULL;
919 return Ok;
920 case RegionDataEmptyRect:
921 *hrgn = CreateRectRgn(0, 0, 0, 0);
922 return *hrgn ? Ok : OutOfMemory;
923 case RegionDataPath:
924 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
925 case RegionDataRect:
927 GpPath* path;
928 GpStatus stat;
929 GpRectF* rc = &element->elementdata.rect;
931 stat = GdipCreatePath(FillModeAlternate, &path);
932 if (stat != Ok)
933 return stat;
934 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
936 if (stat == Ok)
937 stat = get_path_hrgn(path, graphics, hrgn);
939 GdipDeletePath(path);
941 return stat;
943 case CombineModeIntersect:
944 case CombineModeUnion:
945 case CombineModeXor:
946 case CombineModeExclude:
947 case CombineModeComplement:
949 HRGN left, right;
950 GpStatus stat;
951 int ret;
953 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
954 if (stat != Ok)
956 *hrgn = NULL;
957 return stat;
960 if (left == NULL)
962 /* existing region is infinite */
963 switch (element->type)
965 case CombineModeIntersect:
966 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
967 case CombineModeXor: case CombineModeExclude:
968 FIXME("cannot exclude from an infinite region\n");
969 /* fall-through */
970 case CombineModeUnion: case CombineModeComplement:
971 *hrgn = NULL;
972 return Ok;
976 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
977 if (stat != Ok)
979 DeleteObject(left);
980 *hrgn = NULL;
981 return stat;
984 if (right == NULL)
986 /* new region is infinite */
987 switch (element->type)
989 case CombineModeIntersect:
990 *hrgn = left;
991 return Ok;
992 case CombineModeXor: case CombineModeComplement:
993 FIXME("cannot exclude from an infinite region\n");
994 /* fall-through */
995 case CombineModeUnion: case CombineModeExclude:
996 DeleteObject(left);
997 *hrgn = NULL;
998 return Ok;
1002 switch (element->type)
1004 case CombineModeIntersect:
1005 ret = CombineRgn(left, left, right, RGN_AND);
1006 break;
1007 case CombineModeUnion:
1008 ret = CombineRgn(left, left, right, RGN_OR);
1009 break;
1010 case CombineModeXor:
1011 ret = CombineRgn(left, left, right, RGN_XOR);
1012 break;
1013 case CombineModeExclude:
1014 ret = CombineRgn(left, left, right, RGN_DIFF);
1015 break;
1016 case CombineModeComplement:
1017 ret = CombineRgn(left, right, left, RGN_DIFF);
1018 break;
1019 default:
1020 ret = ERROR;
1023 DeleteObject(right);
1025 if (ret == ERROR)
1027 DeleteObject(left);
1028 *hrgn = NULL;
1029 return GenericError;
1032 *hrgn = left;
1033 return Ok;
1035 default:
1036 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1037 *hrgn = NULL;
1038 return NotImplemented;
1042 /*****************************************************************************
1043 * GdipGetRegionHRgn [GDIPLUS.@]
1045 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1047 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1049 if (!region || !hrgn)
1050 return InvalidParameter;
1052 return get_region_hrgn(&region->node, graphics, hrgn);
1055 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1057 TRACE("(%p, %p, %p)\n", region, graphics, res);
1059 if(!region || !graphics || !res)
1060 return InvalidParameter;
1062 *res = (region->node.type == RegionDataEmptyRect);
1064 return Ok;
1067 /*****************************************************************************
1068 * GdipIsEqualRegion [GDIPLUS.@]
1070 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1071 BOOL *res)
1073 HRGN hrgn1, hrgn2;
1074 GpStatus stat;
1076 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1078 if(!region || !region2 || !graphics || !res)
1079 return InvalidParameter;
1081 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1082 if(stat != Ok)
1083 return stat;
1084 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1085 if(stat != Ok){
1086 DeleteObject(hrgn1);
1087 return stat;
1090 *res = EqualRgn(hrgn1, hrgn2);
1092 /* one of GpRegions is infinite */
1093 if(*res == ERROR)
1094 *res = (!hrgn1 && !hrgn2);
1096 DeleteObject(hrgn1);
1097 DeleteObject(hrgn2);
1099 return Ok;
1102 /*****************************************************************************
1103 * GdipIsInfiniteRegion [GDIPLUS.@]
1105 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1107 /* I think graphics is ignored here */
1108 TRACE("(%p, %p, %p)\n", region, graphics, res);
1110 if(!region || !graphics || !res)
1111 return InvalidParameter;
1113 *res = (region->node.type == RegionDataInfiniteRect);
1115 return Ok;
1118 /*****************************************************************************
1119 * GdipSetEmpty [GDIPLUS.@]
1121 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1123 GpStatus stat;
1125 TRACE("%p\n", region);
1127 if (!region)
1128 return InvalidParameter;
1130 delete_element(&region->node);
1131 stat = init_region(region, RegionDataEmptyRect);
1133 return stat;
1136 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1138 GpStatus stat;
1140 TRACE("%p\n", region);
1142 if (!region)
1143 return InvalidParameter;
1145 delete_element(&region->node);
1146 stat = init_region(region, RegionDataInfiniteRect);
1148 return stat;
1151 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1153 FIXME("(%p, %p): stub\n", region, matrix);
1155 return NotImplemented;
1158 /* Translates GpRegion elements with specified offsets */
1159 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1161 INT i;
1163 switch(element->type)
1165 case RegionDataEmptyRect:
1166 case RegionDataInfiniteRect:
1167 return;
1168 case RegionDataRect:
1169 element->elementdata.rect.X += dx;
1170 element->elementdata.rect.Y += dy;
1171 return;
1172 case RegionDataPath:
1173 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1174 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1175 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1177 return;
1178 default:
1179 translate_region_element(element->elementdata.combine.left, dx, dy);
1180 translate_region_element(element->elementdata.combine.right, dx, dy);
1181 return;
1185 /*****************************************************************************
1186 * GdipTranslateRegion [GDIPLUS.@]
1188 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1190 TRACE("(%p, %f, %f)\n", region, dx, dy);
1192 if(!region)
1193 return InvalidParameter;
1195 translate_region_element(&region->node, dx, dy);
1197 return Ok;
1200 /*****************************************************************************
1201 * GdipTranslateRegionI [GDIPLUS.@]
1203 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1205 TRACE("(%p, %d, %d)\n", region, dx, dy);
1207 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);