winebuild: Append the correct options for as and ld when forcing a 32/64-bit build.
[wine/testsucceed.git] / dlls / gdiplus / region.c
blobe42c5cd12ab9df6966b00283d1df140b4c3b072b
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 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
343 GpRegion *region2, CombineMode mode)
345 region_element *left, *right = NULL;
346 GpStatus stat;
347 GpRegion *reg2copy;
349 TRACE("%p %p %d\n", region1, region2, mode);
351 if(!(region1 && region2))
352 return InvalidParameter;
354 /* simply replace region data */
355 if(mode == CombineModeReplace){
356 stat = GdipCloneRegion(region2, &reg2copy);
357 if(stat != Ok) return stat;
359 delete_element(&region1->node);
360 memcpy(region1, reg2copy, sizeof(GpRegion));
361 GdipFree(reg2copy);
362 return Ok;
365 left = GdipAlloc(sizeof(region_element));
366 if (!left)
367 return OutOfMemory;
369 *left = region1->node;
370 stat = clone_element(&region2->node, &right);
371 if (stat != Ok)
373 GdipFree(left);
374 return OutOfMemory;
377 fuse_region(region1, left, right, mode);
378 region1->header.num_children += region2->header.num_children;
380 return Ok;
383 /*****************************************************************************
384 * GdipCreateRegion [GDIPLUS.@]
386 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
388 TRACE("%p\n", region);
390 if(!region)
391 return InvalidParameter;
393 *region = GdipAlloc(sizeof(GpRegion));
394 if(!*region)
395 return OutOfMemory;
397 return init_region(*region, RegionDataInfiniteRect);
400 /*****************************************************************************
401 * GdipCreateRegionPath [GDIPLUS.@]
403 * Creates a GpRegion from a GpPath
405 * PARAMS
406 * path [I] path to base the region on
407 * region [O] pointer to the newly allocated region
409 * RETURNS
410 * SUCCESS: Ok
411 * FAILURE: InvalidParameter
413 * NOTES
414 * If a path has no floating point points, its points will be stored as shorts
415 * (INTPATH)
417 * If a path is empty, it is considered to be an INTPATH
419 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
421 region_element* element;
422 GpPoint *pointsi;
423 GpPointF *pointsf;
425 GpStatus stat;
426 DWORD flags = FLAGS_INTPATH;
427 INT count, i;
429 TRACE("%p, %p\n", path, region);
431 if (!(path && region))
432 return InvalidParameter;
434 *region = GdipAlloc(sizeof(GpRegion));
435 if(!*region)
436 return OutOfMemory;
437 stat = init_region(*region, RegionDataPath);
438 if (stat != Ok)
440 GdipDeleteRegion(*region);
441 return stat;
443 element = &(*region)->node;
444 count = path->pathdata.Count;
446 /* Test to see if the path is an Integer path */
447 if (count)
449 pointsi = GdipAlloc(sizeof(GpPoint) * count);
450 pointsf = GdipAlloc(sizeof(GpPointF) * count);
451 if (!(pointsi && pointsf))
453 GdipFree(pointsi);
454 GdipFree(pointsf);
455 GdipDeleteRegion(*region);
456 return OutOfMemory;
459 stat = GdipGetPathPointsI(path, pointsi, count);
460 if (stat != Ok)
462 GdipDeleteRegion(*region);
463 return stat;
465 stat = GdipGetPathPoints(path, pointsf, count);
466 if (stat != Ok)
468 GdipDeleteRegion(*region);
469 return stat;
472 for (i = 0; i < count; i++)
474 if (!(pointsi[i].X == pointsf[i].X &&
475 pointsi[i].Y == pointsf[i].Y ))
477 flags = FLAGS_NOFLAGS;
478 break;
481 GdipFree(pointsi);
482 GdipFree(pointsf);
485 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
486 if (stat != Ok)
488 GdipDeleteRegion(*region);
489 return stat;
492 /* 3 for headers, once again size doesn't count itself */
493 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
494 switch(flags)
496 /* Floats, sent out as floats */
497 case FLAGS_NOFLAGS:
498 element->elementdata.pathdata.pathheader.size +=
499 (sizeof(DWORD) * count * 2);
500 break;
501 /* INTs, sent out as packed shorts */
502 case FLAGS_INTPATH:
503 element->elementdata.pathdata.pathheader.size +=
504 (sizeof(DWORD) * count);
505 break;
506 default:
507 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
509 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
510 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
511 element->elementdata.pathdata.pathheader.count = count;
512 element->elementdata.pathdata.pathheader.flags = flags;
513 (*region)->header.size = sizeheader_size + get_element_size(element);
515 return Ok;
518 /*****************************************************************************
519 * GdipCreateRegionRect [GDIPLUS.@]
521 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
522 GpRegion **region)
524 GpStatus stat;
526 TRACE("%p, %p\n", rect, region);
528 if (!(rect && region))
529 return InvalidParameter;
531 *region = GdipAlloc(sizeof(GpRegion));
532 stat = init_region(*region, RegionDataRect);
533 if(stat != Ok)
535 GdipDeleteRegion(*region);
536 return stat;
539 (*region)->node.elementdata.rect.X = rect->X;
540 (*region)->node.elementdata.rect.Y = rect->Y;
541 (*region)->node.elementdata.rect.Width = rect->Width;
542 (*region)->node.elementdata.rect.Height = rect->Height;
544 return Ok;
547 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
548 GpRegion **region)
550 GpRectF rectf;
552 TRACE("%p, %p\n", rect, region);
554 rectf.X = (REAL)rect->X;
555 rectf.Y = (REAL)rect->Y;
556 rectf.Width = (REAL)rect->Width;
557 rectf.Height = (REAL)rect->Height;
559 return GdipCreateRegionRect(&rectf, region);
562 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
564 FIXME("(%p, %d, %p): stub\n", data, size, region);
566 *region = NULL;
567 return NotImplemented;
571 /******************************************************************************
572 * GdipCreateRegionHrgn [GDIPLUS.@]
574 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
576 union {
577 RGNDATA data;
578 char buf[sizeof(RGNDATAHEADER) + sizeof(RECT)];
579 } rdata;
580 DWORD size;
581 GpRectF rectf;
582 GpPath *path;
583 GpStatus stat;
585 TRACE("(%p, %p)\n", hrgn, region);
587 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
588 return InvalidParameter;
590 if(size > sizeof(RGNDATAHEADER) + sizeof(RECT)){
591 FIXME("Only simple rect regions supported.\n");
592 *region = NULL;
593 return NotImplemented;
596 if(!GetRegionData(hrgn, sizeof(rdata), &rdata.data))
597 return GenericError;
599 /* return empty region */
600 if(IsRectEmpty(&rdata.data.rdh.rcBound)){
601 stat = GdipCreateRegion(region);
602 if(stat == Ok)
603 GdipSetEmpty(*region);
604 return stat;
607 rectf.X = (REAL)rdata.data.rdh.rcBound.left;
608 rectf.Y = (REAL)rdata.data.rdh.rcBound.top;
609 rectf.Width = (REAL)rdata.data.rdh.rcBound.right - rectf.X;
610 rectf.Height = (REAL)rdata.data.rdh.rcBound.bottom - rectf.Y;
612 stat = GdipCreatePath(FillModeAlternate, &path);
613 if(stat != Ok)
614 return stat;
616 GdipAddPathRectangle(path, rectf.X, rectf.Y, rectf.Width, rectf.Height);
618 stat = GdipCreateRegionPath(path, region);
619 GdipDeletePath(path);
621 return stat;
624 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
626 TRACE("%p\n", region);
628 if (!region)
629 return InvalidParameter;
631 delete_element(&region->node);
632 GdipFree(region);
634 return Ok;
637 /*****************************************************************************
638 * GdipGetRegionBounds [GDIPLUS.@]
640 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
642 HRGN hrgn;
643 RECT r;
644 GpStatus status;
646 TRACE("(%p, %p, %p)\n", region, graphics, rect);
648 if(!region || !graphics || !rect)
649 return InvalidParameter;
651 status = GdipGetRegionHRgn(region, graphics, &hrgn);
652 if(status != Ok)
653 return status;
655 /* infinite */
656 if(!hrgn){
657 rect->X = rect->Y = -(REAL)(1 << 22);
658 rect->Width = rect->Height = (REAL)(1 << 23);
659 return Ok;
662 if(!GetRgnBox(hrgn, &r)){
663 DeleteObject(hrgn);
664 return GenericError;
667 rect->X = r.left;
668 rect->Y = r.top;
669 rect->Width = r.right - r.left;
670 rect->Height = r.bottom - r.top;
672 return Ok;
675 /*****************************************************************************
676 * GdipGetRegionBoundsI [GDIPLUS.@]
678 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
680 GpRectF rectf;
681 GpStatus status;
683 TRACE("(%p, %p, %p)\n", region, graphics, rect);
685 if(!rect)
686 return InvalidParameter;
688 status = GdipGetRegionBounds(region, graphics, &rectf);
689 if(status == Ok){
690 rect->X = roundr(rectf.X);
691 rect->Y = roundr(rectf.X);
692 rect->Width = roundr(rectf.Width);
693 rect->Height = roundr(rectf.Height);
696 return status;
699 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
701 location[*offset] = write;
702 (*offset)++;
705 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
707 ((FLOAT*)location)[*offset] = write;
708 (*offset)++;
711 static inline void write_packed_point(DWORD* location, INT* offset,
712 const GpPointF* write)
714 packed_point point;
716 point.X = write->X;
717 point.Y = write->Y;
718 memcpy(location + *offset, &point, sizeof(packed_point));
719 (*offset)++;
722 static inline void write_path_types(DWORD* location, INT* offset,
723 const GpPath* path)
725 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
727 /* The unwritten parts of the DWORD (if any) must be cleared */
728 if (path->pathdata.Count % sizeof(DWORD))
729 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
730 path->pathdata.Count,
731 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
732 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
735 static void write_element(const region_element* element, DWORD *buffer,
736 INT* filled)
738 write_dword(buffer, filled, element->type);
739 switch (element->type)
741 case CombineModeReplace:
742 case CombineModeIntersect:
743 case CombineModeUnion:
744 case CombineModeXor:
745 case CombineModeExclude:
746 case CombineModeComplement:
747 write_element(element->elementdata.combine.left, buffer, filled);
748 write_element(element->elementdata.combine.right, buffer, filled);
749 break;
750 case RegionDataRect:
751 write_float(buffer, filled, element->elementdata.rect.X);
752 write_float(buffer, filled, element->elementdata.rect.Y);
753 write_float(buffer, filled, element->elementdata.rect.Width);
754 write_float(buffer, filled, element->elementdata.rect.Height);
755 break;
756 case RegionDataPath:
758 INT i;
759 const GpPath* path = element->elementdata.pathdata.path;
761 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
762 sizeof(element->elementdata.pathdata.pathheader));
763 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
764 switch (element->elementdata.pathdata.pathheader.flags)
766 case FLAGS_NOFLAGS:
767 for (i = 0; i < path->pathdata.Count; i++)
769 write_float(buffer, filled, path->pathdata.Points[i].X);
770 write_float(buffer, filled, path->pathdata.Points[i].Y);
772 break;
773 case FLAGS_INTPATH:
774 for (i = 0; i < path->pathdata.Count; i++)
776 write_packed_point(buffer, filled,
777 &path->pathdata.Points[i]);
780 write_path_types(buffer, filled, path);
781 break;
783 case RegionDataEmptyRect:
784 case RegionDataInfiniteRect:
785 break;
789 /*****************************************************************************
790 * GdipGetRegionData [GDIPLUS.@]
792 * Returns the header, followed by combining ops and region elements.
794 * PARAMS
795 * region [I] region to retrieve from
796 * buffer [O] buffer to hold the resulting data
797 * size [I] size of the buffer
798 * needed [O] (optional) how much data was written
800 * RETURNS
801 * SUCCESS: Ok
802 * FAILURE: InvalidParamter
804 * NOTES
805 * The header contains the size, a checksum, a version string, and the number
806 * of children. The size does not count itself or the checksum.
807 * Version is always something like 0xdbc01001 or 0xdbc01002
809 * An element is a RECT, or PATH; Combining ops are stored as their
810 * CombineMode value. Special regions (infinite, empty) emit just their
811 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
812 * their code followed by a second header for the path followed by the actual
813 * path data. Followed by the flags for each point. The pathheader contains
814 * the size of the data to follow, a version number again, followed by a count
815 * of how many points, and any special flags which may apply. 0x4000 means its
816 * a path of shorts instead of FLOAT.
818 * Combining Ops are stored in reverse order from when they were constructed;
819 * the output is a tree where the left side combining area is always taken
820 * first.
822 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
823 UINT *needed)
825 INT filled = 0;
827 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
829 if (!(region && buffer && size))
830 return InvalidParameter;
832 memcpy(buffer, &region->header, sizeof(region->header));
833 filled += sizeof(region->header) / sizeof(DWORD);
834 /* With few exceptions, everything written is DWORD aligned,
835 * so use that as our base */
836 write_element(&region->node, (DWORD*)buffer, &filled);
838 if (needed)
839 *needed = filled * sizeof(DWORD);
841 return Ok;
844 /*****************************************************************************
845 * GdipGetRegionDataSize [GDIPLUS.@]
847 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
849 TRACE("%p, %p\n", region, needed);
851 if (!(region && needed))
852 return InvalidParameter;
854 /* header.size doesn't count header.size and header.checksum */
855 *needed = region->header.size + sizeof(DWORD) * 2;
857 return Ok;
860 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
862 HDC new_hdc=NULL;
863 GpStatus stat;
864 INT save_state;
866 if (!graphics)
868 new_hdc = GetDC(0);
869 if (!new_hdc)
870 return OutOfMemory;
872 stat = GdipCreateFromHDC(new_hdc, &graphics);
873 if (stat != Ok)
875 ReleaseDC(0, new_hdc);
876 return stat;
880 save_state = SaveDC(graphics->hdc);
881 EndPath(graphics->hdc);
883 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
884 : WINDING));
886 stat = trace_path(graphics, path);
887 if (stat == Ok)
889 *hrgn = PathToRegion(graphics->hdc);
890 stat = *hrgn ? Ok : OutOfMemory;
893 RestoreDC(graphics->hdc, save_state);
894 if (new_hdc)
896 ReleaseDC(0, new_hdc);
897 GdipDeleteGraphics(graphics);
900 return stat;
903 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
905 switch (element->type)
907 case RegionDataInfiniteRect:
908 *hrgn = NULL;
909 return Ok;
910 case RegionDataEmptyRect:
911 *hrgn = CreateRectRgn(0, 0, 0, 0);
912 return *hrgn ? Ok : OutOfMemory;
913 case RegionDataPath:
914 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
915 case RegionDataRect:
917 GpPath* path;
918 GpStatus stat;
919 GpRectF* rc = &element->elementdata.rect;
921 stat = GdipCreatePath(FillModeAlternate, &path);
922 if (stat != Ok)
923 return stat;
924 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
926 if (stat == Ok)
927 stat = get_path_hrgn(path, graphics, hrgn);
929 GdipDeletePath(path);
931 return stat;
933 case CombineModeIntersect:
934 case CombineModeUnion:
935 case CombineModeXor:
936 case CombineModeExclude:
937 case CombineModeComplement:
939 HRGN left, right;
940 GpStatus stat;
941 int ret;
943 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
944 if (stat != Ok)
946 *hrgn = NULL;
947 return stat;
950 if (left == NULL)
952 /* existing region is infinite */
953 switch (element->type)
955 case CombineModeIntersect:
956 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
957 case CombineModeXor: case CombineModeExclude:
958 FIXME("cannot exclude from an infinite region\n");
959 /* fall-through */
960 case CombineModeUnion: case CombineModeComplement:
961 *hrgn = NULL;
962 return Ok;
966 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
967 if (stat != Ok)
969 DeleteObject(left);
970 *hrgn = NULL;
971 return stat;
974 if (right == NULL)
976 /* new region is infinite */
977 switch (element->type)
979 case CombineModeIntersect:
980 *hrgn = left;
981 return Ok;
982 case CombineModeXor: case CombineModeComplement:
983 FIXME("cannot exclude from an infinite region\n");
984 /* fall-through */
985 case CombineModeUnion: case CombineModeExclude:
986 DeleteObject(left);
987 *hrgn = NULL;
988 return Ok;
992 switch (element->type)
994 case CombineModeIntersect:
995 ret = CombineRgn(left, left, right, RGN_AND);
996 break;
997 case CombineModeUnion:
998 ret = CombineRgn(left, left, right, RGN_OR);
999 break;
1000 case CombineModeXor:
1001 ret = CombineRgn(left, left, right, RGN_XOR);
1002 break;
1003 case CombineModeExclude:
1004 ret = CombineRgn(left, left, right, RGN_DIFF);
1005 break;
1006 case CombineModeComplement:
1007 ret = CombineRgn(left, right, left, RGN_DIFF);
1008 break;
1009 default:
1010 ret = ERROR;
1013 DeleteObject(right);
1015 if (ret == ERROR)
1017 DeleteObject(left);
1018 *hrgn = NULL;
1019 return GenericError;
1022 *hrgn = left;
1023 return Ok;
1025 default:
1026 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1027 *hrgn = NULL;
1028 return NotImplemented;
1032 /*****************************************************************************
1033 * GdipGetRegionHRgn [GDIPLUS.@]
1035 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1037 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1039 if (!region || !hrgn)
1040 return InvalidParameter;
1042 return get_region_hrgn(&region->node, graphics, hrgn);
1045 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1047 TRACE("(%p, %p, %p)\n", region, graphics, res);
1049 if(!region || !graphics || !res)
1050 return InvalidParameter;
1052 *res = (region->node.type == RegionDataEmptyRect);
1054 return Ok;
1057 /*****************************************************************************
1058 * GdipIsEqualRegion [GDIPLUS.@]
1060 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1061 BOOL *res)
1063 HRGN hrgn1, hrgn2;
1064 GpStatus stat;
1066 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1068 if(!region || !region2 || !graphics || !res)
1069 return InvalidParameter;
1071 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1072 if(stat != Ok)
1073 return stat;
1074 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1075 if(stat != Ok){
1076 DeleteObject(hrgn1);
1077 return stat;
1080 *res = EqualRgn(hrgn1, hrgn2);
1082 /* one of GpRegions is infinite */
1083 if(*res == ERROR)
1084 *res = (!hrgn1 && !hrgn2);
1086 DeleteObject(hrgn1);
1087 DeleteObject(hrgn2);
1089 return Ok;
1092 /*****************************************************************************
1093 * GdipIsInfiniteRegion [GDIPLUS.@]
1095 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1097 /* I think graphics is ignored here */
1098 TRACE("(%p, %p, %p)\n", region, graphics, res);
1100 if(!region || !graphics || !res)
1101 return InvalidParameter;
1103 *res = (region->node.type == RegionDataInfiniteRect);
1105 return Ok;
1108 /*****************************************************************************
1109 * GdipSetEmpty [GDIPLUS.@]
1111 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1113 GpStatus stat;
1115 TRACE("%p\n", region);
1117 if (!region)
1118 return InvalidParameter;
1120 delete_element(&region->node);
1121 stat = init_region(region, RegionDataEmptyRect);
1123 return stat;
1126 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1128 GpStatus stat;
1130 TRACE("%p\n", region);
1132 if (!region)
1133 return InvalidParameter;
1135 delete_element(&region->node);
1136 stat = init_region(region, RegionDataInfiniteRect);
1138 return stat;
1141 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1143 FIXME("(%p, %p): stub\n", region, matrix);
1145 return NotImplemented;
1148 /* Translates GpRegion elements with specified offsets */
1149 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1151 INT i;
1153 switch(element->type)
1155 case RegionDataEmptyRect:
1156 case RegionDataInfiniteRect:
1157 return;
1158 case RegionDataRect:
1159 element->elementdata.rect.X += dx;
1160 element->elementdata.rect.Y += dy;
1161 return;
1162 case RegionDataPath:
1163 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1164 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1165 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1167 return;
1168 default:
1169 translate_region_element(element->elementdata.combine.left, dx, dy);
1170 translate_region_element(element->elementdata.combine.right, dx, dy);
1171 return;
1175 /*****************************************************************************
1176 * GdipTranslateRegion [GDIPLUS.@]
1178 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1180 TRACE("(%p, %f, %f)\n", region, dx, dy);
1182 if(!region)
1183 return InvalidParameter;
1185 translate_region_element(&region->node, dx, dy);
1187 return Ok;
1190 /*****************************************************************************
1191 * GdipTranslateRegionI [GDIPLUS.@]
1193 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1195 TRACE("(%p, %d, %d)\n", region, dx, dy);
1197 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);