wined3d: Only unload the surface when we're using GL for rendering in updateSurfaceDe...
[wine/multimedia.git] / dlls / gdiplus / region.c
blob485c3a3e16530b27c0a7228cb7411e8a0540c103
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 GdipFree(path_region);
253 return Ok;
256 left = GdipAlloc(sizeof(region_element));
257 if (!left)
258 goto out;
259 *left = region->node;
261 stat = clone_element(&path_region->node, &right);
262 if (stat != Ok)
263 goto out;
265 fuse_region(region, left, right, mode);
267 GdipDeleteRegion(path_region);
268 return Ok;
270 out:
271 GdipFree(left);
272 GdipDeleteRegion(path_region);
273 return stat;
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;
284 GpStatus stat;
286 TRACE("%p %p %d\n", region, rect, mode);
288 if (!(region && rect))
289 return InvalidParameter;
291 stat = GdipCreateRegionRect(rect, &rect_region);
292 if (stat != Ok)
293 return stat;
295 /* simply replace region data */
296 if(mode == CombineModeReplace){
297 delete_element(&region->node);
298 memcpy(region, rect_region, sizeof(GpRegion));
299 GdipFree(rect_region);
300 return Ok;
303 left = GdipAlloc(sizeof(region_element));
304 if (!left)
305 goto out;
306 memcpy(left, &region->node, sizeof(region_element));
308 stat = clone_element(&rect_region->node, &right);
309 if (stat != Ok)
310 goto out;
312 fuse_region(region, left, right, mode);
314 GdipDeleteRegion(rect_region);
315 return Ok;
317 out:
318 GdipFree(left);
319 GdipDeleteRegion(rect_region);
320 return stat;
323 /*****************************************************************************
324 * GdipCombineRegionRectI [GDIPLUS.@]
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327 GDIPCONST GpRect *rect, CombineMode mode)
329 GpRectF rectf;
331 TRACE("%p %p %d\n", region, rect, mode);
333 if (!rect)
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;
351 GpStatus stat;
352 GpRegion *reg2copy;
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, &reg2copy);
362 if(stat != Ok) return stat;
364 delete_element(&region1->node);
365 memcpy(region1, reg2copy, sizeof(GpRegion));
366 GdipFree(reg2copy);
367 return Ok;
370 left = GdipAlloc(sizeof(region_element));
371 if (!left)
372 return OutOfMemory;
374 *left = region1->node;
375 stat = clone_element(&region2->node, &right);
376 if (stat != Ok)
378 GdipFree(left);
379 return OutOfMemory;
382 fuse_region(region1, left, right, mode);
383 region1->header.num_children += region2->header.num_children;
385 return Ok;
388 /*****************************************************************************
389 * GdipCreateRegion [GDIPLUS.@]
391 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
393 TRACE("%p\n", region);
395 if(!region)
396 return InvalidParameter;
398 *region = GdipAlloc(sizeof(GpRegion));
399 if(!*region)
400 return OutOfMemory;
402 return init_region(*region, RegionDataInfiniteRect);
405 /*****************************************************************************
406 * GdipCreateRegionPath [GDIPLUS.@]
408 * Creates a GpRegion from a GpPath
410 * PARAMS
411 * path [I] path to base the region on
412 * region [O] pointer to the newly allocated region
414 * RETURNS
415 * SUCCESS: Ok
416 * FAILURE: InvalidParameter
418 * NOTES
419 * If a path has no floating point points, its points will be stored as shorts
420 * (INTPATH)
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;
427 GpPoint *pointsi;
428 GpPointF *pointsf;
430 GpStatus stat;
431 DWORD flags = FLAGS_INTPATH;
432 INT count, i;
434 TRACE("%p, %p\n", path, region);
436 if (!(path && region))
437 return InvalidParameter;
439 *region = GdipAlloc(sizeof(GpRegion));
440 if(!*region)
441 return OutOfMemory;
442 stat = init_region(*region, RegionDataPath);
443 if (stat != Ok)
445 GdipDeleteRegion(*region);
446 return stat;
448 element = &(*region)->node;
449 count = path->pathdata.Count;
451 /* Test to see if the path is an Integer path */
452 if (count)
454 pointsi = GdipAlloc(sizeof(GpPoint) * count);
455 pointsf = GdipAlloc(sizeof(GpPointF) * count);
456 if (!(pointsi && pointsf))
458 GdipFree(pointsi);
459 GdipFree(pointsf);
460 GdipDeleteRegion(*region);
461 return OutOfMemory;
464 stat = GdipGetPathPointsI(path, pointsi, count);
465 if (stat != Ok)
467 GdipDeleteRegion(*region);
468 return stat;
470 stat = GdipGetPathPoints(path, pointsf, count);
471 if (stat != Ok)
473 GdipDeleteRegion(*region);
474 return stat;
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;
483 break;
486 GdipFree(pointsi);
487 GdipFree(pointsf);
490 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
491 if (stat != Ok)
493 GdipDeleteRegion(*region);
494 return stat;
497 /* 3 for headers, once again size doesn't count itself */
498 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
499 switch(flags)
501 /* Floats, sent out as floats */
502 case FLAGS_NOFLAGS:
503 element->elementdata.pathdata.pathheader.size +=
504 (sizeof(DWORD) * count * 2);
505 break;
506 /* INTs, sent out as packed shorts */
507 case FLAGS_INTPATH:
508 element->elementdata.pathdata.pathheader.size +=
509 (sizeof(DWORD) * count);
510 break;
511 default:
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);
520 return Ok;
523 /*****************************************************************************
524 * GdipCreateRegionRect [GDIPLUS.@]
526 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
527 GpRegion **region)
529 GpStatus stat;
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);
538 if(stat != Ok)
540 GdipDeleteRegion(*region);
541 return stat;
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;
549 return Ok;
552 /*****************************************************************************
553 * GdipCreateRegionRectI [GDIPLUS.@]
555 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
556 GpRegion **region)
558 GpRectF rectf;
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);
574 *region = NULL;
575 return NotImplemented;
579 /******************************************************************************
580 * GdipCreateRegionHrgn [GDIPLUS.@]
582 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
584 DWORD size;
585 LPRGNDATA buf;
586 LPRECT rect;
587 GpStatus stat;
588 GpPath* path;
589 GpRegion* local;
590 int i;
592 TRACE("(%p, %p)\n", hrgn, region);
594 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
595 return InvalidParameter;
597 buf = GdipAlloc(size);
598 if(!buf)
599 return OutOfMemory;
601 if(!GetRegionData(hrgn, size, buf)){
602 GdipFree(buf);
603 return GenericError;
606 if(buf->rdh.nCount == 0){
607 if((stat = GdipCreateRegion(&local)) != Ok){
608 GdipFree(buf);
609 return stat;
611 if((stat = GdipSetEmpty(local)) != Ok){
612 GdipFree(buf);
613 GdipDeleteRegion(local);
614 return stat;
616 *region = local;
617 GdipFree(buf);
618 return Ok;
621 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
622 GdipFree(buf);
623 return stat;
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){
630 GdipFree(buf);
631 GdipDeletePath(path);
632 return stat;
634 rect++;
637 stat = GdipCreateRegionPath(path, region);
639 GdipFree(buf);
640 GdipDeletePath(path);
641 return stat;
644 /*****************************************************************************
645 * GdipDeleteRegion [GDIPLUS.@]
647 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
649 TRACE("%p\n", region);
651 if (!region)
652 return InvalidParameter;
654 delete_element(&region->node);
655 GdipFree(region);
657 return Ok;
660 /*****************************************************************************
661 * GdipGetRegionBounds [GDIPLUS.@]
663 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
665 HRGN hrgn;
666 RECT r;
667 GpStatus status;
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);
676 if(status != Ok)
677 return status;
679 /* infinite */
680 if(!hrgn){
681 rect->X = rect->Y = -(REAL)(1 << 22);
682 rect->Width = rect->Height = (REAL)(1 << 23);
683 return Ok;
686 if(GetRgnBox(hrgn, &r)){
687 rect->X = r.left;
688 rect->Y = r.top;
689 rect->Width = r.right - r.left;
690 rect->Height = r.bottom - r.top;
692 else
693 status = GenericError;
695 DeleteObject(hrgn);
697 return status;
700 /*****************************************************************************
701 * GdipGetRegionBoundsI [GDIPLUS.@]
703 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
705 GpRectF rectf;
706 GpStatus status;
708 TRACE("(%p, %p, %p)\n", region, graphics, rect);
710 if(!rect)
711 return InvalidParameter;
713 status = GdipGetRegionBounds(region, graphics, &rectf);
714 if(status == Ok){
715 rect->X = roundr(rectf.X);
716 rect->Y = roundr(rectf.X);
717 rect->Width = roundr(rectf.Width);
718 rect->Height = roundr(rectf.Height);
721 return status;
724 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
726 location[*offset] = write;
727 (*offset)++;
730 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
732 ((FLOAT*)location)[*offset] = write;
733 (*offset)++;
736 static inline void write_packed_point(DWORD* location, INT* offset,
737 const GpPointF* write)
739 packed_point point;
741 point.X = write->X;
742 point.Y = write->Y;
743 memcpy(location + *offset, &point, sizeof(packed_point));
744 (*offset)++;
747 static inline void write_path_types(DWORD* location, INT* offset,
748 const GpPath* path)
750 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
752 /* The unwritten parts of the DWORD (if any) must be cleared */
753 if (path->pathdata.Count % sizeof(DWORD))
754 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
755 path->pathdata.Count,
756 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
757 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
760 static void write_element(const region_element* element, DWORD *buffer,
761 INT* filled)
763 write_dword(buffer, filled, element->type);
764 switch (element->type)
766 case CombineModeReplace:
767 case CombineModeIntersect:
768 case CombineModeUnion:
769 case CombineModeXor:
770 case CombineModeExclude:
771 case CombineModeComplement:
772 write_element(element->elementdata.combine.left, buffer, filled);
773 write_element(element->elementdata.combine.right, buffer, filled);
774 break;
775 case RegionDataRect:
776 write_float(buffer, filled, element->elementdata.rect.X);
777 write_float(buffer, filled, element->elementdata.rect.Y);
778 write_float(buffer, filled, element->elementdata.rect.Width);
779 write_float(buffer, filled, element->elementdata.rect.Height);
780 break;
781 case RegionDataPath:
783 INT i;
784 const GpPath* path = element->elementdata.pathdata.path;
786 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
787 sizeof(element->elementdata.pathdata.pathheader));
788 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
789 switch (element->elementdata.pathdata.pathheader.flags)
791 case FLAGS_NOFLAGS:
792 for (i = 0; i < path->pathdata.Count; i++)
794 write_float(buffer, filled, path->pathdata.Points[i].X);
795 write_float(buffer, filled, path->pathdata.Points[i].Y);
797 break;
798 case FLAGS_INTPATH:
799 for (i = 0; i < path->pathdata.Count; i++)
801 write_packed_point(buffer, filled,
802 &path->pathdata.Points[i]);
805 write_path_types(buffer, filled, path);
806 break;
808 case RegionDataEmptyRect:
809 case RegionDataInfiniteRect:
810 break;
814 /*****************************************************************************
815 * GdipGetRegionData [GDIPLUS.@]
817 * Returns the header, followed by combining ops and region elements.
819 * PARAMS
820 * region [I] region to retrieve from
821 * buffer [O] buffer to hold the resulting data
822 * size [I] size of the buffer
823 * needed [O] (optional) how much data was written
825 * RETURNS
826 * SUCCESS: Ok
827 * FAILURE: InvalidParameter
829 * NOTES
830 * The header contains the size, a checksum, a version string, and the number
831 * of children. The size does not count itself or the checksum.
832 * Version is always something like 0xdbc01001 or 0xdbc01002
834 * An element is a RECT, or PATH; Combining ops are stored as their
835 * CombineMode value. Special regions (infinite, empty) emit just their
836 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
837 * their code followed by a second header for the path followed by the actual
838 * path data. Followed by the flags for each point. The pathheader contains
839 * the size of the data to follow, a version number again, followed by a count
840 * of how many points, and any special flags which may apply. 0x4000 means its
841 * a path of shorts instead of FLOAT.
843 * Combining Ops are stored in reverse order from when they were constructed;
844 * the output is a tree where the left side combining area is always taken
845 * first.
847 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
848 UINT *needed)
850 INT filled = 0;
852 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
854 if (!(region && buffer && size))
855 return InvalidParameter;
857 memcpy(buffer, &region->header, sizeof(region->header));
858 filled += sizeof(region->header) / sizeof(DWORD);
859 /* With few exceptions, everything written is DWORD aligned,
860 * so use that as our base */
861 write_element(&region->node, (DWORD*)buffer, &filled);
863 if (needed)
864 *needed = filled * sizeof(DWORD);
866 return Ok;
869 /*****************************************************************************
870 * GdipGetRegionDataSize [GDIPLUS.@]
872 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
874 TRACE("%p, %p\n", region, needed);
876 if (!(region && needed))
877 return InvalidParameter;
879 /* header.size doesn't count header.size and header.checksum */
880 *needed = region->header.size + sizeof(DWORD) * 2;
882 return Ok;
885 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
887 HDC new_hdc=NULL;
888 GpGraphics *new_graphics=NULL;
889 GpStatus stat;
890 INT save_state;
892 if (!graphics)
894 new_hdc = GetDC(0);
895 if (!new_hdc)
896 return OutOfMemory;
898 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
899 graphics = new_graphics;
900 if (stat != Ok)
902 ReleaseDC(0, new_hdc);
903 return stat;
906 else if (!graphics->hdc)
908 graphics->hdc = new_hdc = GetDC(0);
909 if (!new_hdc)
910 return OutOfMemory;
913 save_state = SaveDC(graphics->hdc);
914 EndPath(graphics->hdc);
916 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
917 : WINDING));
919 stat = trace_path(graphics, path);
920 if (stat == Ok)
922 *hrgn = PathToRegion(graphics->hdc);
923 stat = *hrgn ? Ok : OutOfMemory;
926 RestoreDC(graphics->hdc, save_state);
927 if (new_hdc)
929 ReleaseDC(0, new_hdc);
930 if (new_graphics)
931 GdipDeleteGraphics(new_graphics);
932 else
933 graphics->hdc = NULL;
936 return stat;
939 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
941 switch (element->type)
943 case RegionDataInfiniteRect:
944 *hrgn = NULL;
945 return Ok;
946 case RegionDataEmptyRect:
947 *hrgn = CreateRectRgn(0, 0, 0, 0);
948 return *hrgn ? Ok : OutOfMemory;
949 case RegionDataPath:
950 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
951 case RegionDataRect:
953 GpPath* path;
954 GpStatus stat;
955 GpRectF* rc = &element->elementdata.rect;
957 stat = GdipCreatePath(FillModeAlternate, &path);
958 if (stat != Ok)
959 return stat;
960 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
962 if (stat == Ok)
963 stat = get_path_hrgn(path, graphics, hrgn);
965 GdipDeletePath(path);
967 return stat;
969 case CombineModeIntersect:
970 case CombineModeUnion:
971 case CombineModeXor:
972 case CombineModeExclude:
973 case CombineModeComplement:
975 HRGN left, right;
976 GpStatus stat;
977 int ret;
979 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
980 if (stat != Ok)
982 *hrgn = NULL;
983 return stat;
986 if (left == NULL)
988 /* existing region is infinite */
989 switch (element->type)
991 case CombineModeIntersect:
992 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
993 case CombineModeXor: case CombineModeExclude:
994 FIXME("cannot exclude from an infinite region\n");
995 /* fall-through */
996 case CombineModeUnion: case CombineModeComplement:
997 *hrgn = NULL;
998 return Ok;
1002 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1003 if (stat != Ok)
1005 DeleteObject(left);
1006 *hrgn = NULL;
1007 return stat;
1010 if (right == NULL)
1012 /* new region is infinite */
1013 switch (element->type)
1015 case CombineModeIntersect:
1016 *hrgn = left;
1017 return Ok;
1018 case CombineModeXor: case CombineModeComplement:
1019 FIXME("cannot exclude from an infinite region\n");
1020 /* fall-through */
1021 case CombineModeUnion: case CombineModeExclude:
1022 DeleteObject(left);
1023 *hrgn = NULL;
1024 return Ok;
1028 switch (element->type)
1030 case CombineModeIntersect:
1031 ret = CombineRgn(left, left, right, RGN_AND);
1032 break;
1033 case CombineModeUnion:
1034 ret = CombineRgn(left, left, right, RGN_OR);
1035 break;
1036 case CombineModeXor:
1037 ret = CombineRgn(left, left, right, RGN_XOR);
1038 break;
1039 case CombineModeExclude:
1040 ret = CombineRgn(left, left, right, RGN_DIFF);
1041 break;
1042 case CombineModeComplement:
1043 ret = CombineRgn(left, right, left, RGN_DIFF);
1044 break;
1045 default:
1046 ret = ERROR;
1049 DeleteObject(right);
1051 if (ret == ERROR)
1053 DeleteObject(left);
1054 *hrgn = NULL;
1055 return GenericError;
1058 *hrgn = left;
1059 return Ok;
1061 default:
1062 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1063 *hrgn = NULL;
1064 return NotImplemented;
1068 /*****************************************************************************
1069 * GdipGetRegionHRgn [GDIPLUS.@]
1071 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1073 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1075 if (!region || !hrgn)
1076 return InvalidParameter;
1078 return get_region_hrgn(&region->node, graphics, hrgn);
1081 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1083 TRACE("(%p, %p, %p)\n", region, graphics, res);
1085 if(!region || !graphics || !res)
1086 return InvalidParameter;
1088 *res = (region->node.type == RegionDataEmptyRect);
1090 return Ok;
1093 /*****************************************************************************
1094 * GdipIsEqualRegion [GDIPLUS.@]
1096 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1097 BOOL *res)
1099 HRGN hrgn1, hrgn2;
1100 GpStatus stat;
1102 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1104 if(!region || !region2 || !graphics || !res)
1105 return InvalidParameter;
1107 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1108 if(stat != Ok)
1109 return stat;
1110 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1111 if(stat != Ok){
1112 DeleteObject(hrgn1);
1113 return stat;
1116 *res = EqualRgn(hrgn1, hrgn2);
1118 /* one of GpRegions is infinite */
1119 if(*res == ERROR)
1120 *res = (!hrgn1 && !hrgn2);
1122 DeleteObject(hrgn1);
1123 DeleteObject(hrgn2);
1125 return Ok;
1128 /*****************************************************************************
1129 * GdipIsInfiniteRegion [GDIPLUS.@]
1131 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1133 /* I think graphics is ignored here */
1134 TRACE("(%p, %p, %p)\n", region, graphics, res);
1136 if(!region || !graphics || !res)
1137 return InvalidParameter;
1139 *res = (region->node.type == RegionDataInfiniteRect);
1141 return Ok;
1144 /*****************************************************************************
1145 * GdipIsVisibleRegionRect [GDIPLUS.@]
1147 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1149 HRGN hrgn;
1150 GpStatus stat;
1151 RECT rect;
1153 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1155 if(!region || !res)
1156 return InvalidParameter;
1158 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1159 return stat;
1161 /* infinite */
1162 if(!hrgn){
1163 *res = TRUE;
1164 return Ok;
1167 rect.left = ceilr(x);
1168 rect.top = ceilr(y);
1169 rect.right = ceilr(x + w);
1170 rect.bottom = ceilr(y + h);
1172 *res = RectInRegion(hrgn, &rect);
1174 DeleteObject(hrgn);
1176 return Ok;
1179 /*****************************************************************************
1180 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1182 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1184 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1185 if(!region || !res)
1186 return InvalidParameter;
1188 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1191 /*****************************************************************************
1192 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1194 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1196 HRGN hrgn;
1197 GpStatus stat;
1199 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1201 if(!region || !res)
1202 return InvalidParameter;
1204 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1205 return stat;
1207 /* infinite */
1208 if(!hrgn){
1209 *res = TRUE;
1210 return Ok;
1213 *res = PtInRegion(hrgn, roundr(x), roundr(y));
1215 DeleteObject(hrgn);
1217 return Ok;
1220 /*****************************************************************************
1221 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1223 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1225 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1227 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1230 /*****************************************************************************
1231 * GdipSetEmpty [GDIPLUS.@]
1233 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1235 GpStatus stat;
1237 TRACE("%p\n", region);
1239 if (!region)
1240 return InvalidParameter;
1242 delete_element(&region->node);
1243 stat = init_region(region, RegionDataEmptyRect);
1245 return stat;
1248 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1250 GpStatus stat;
1252 TRACE("%p\n", region);
1254 if (!region)
1255 return InvalidParameter;
1257 delete_element(&region->node);
1258 stat = init_region(region, RegionDataInfiniteRect);
1260 return stat;
1263 /* Transforms GpRegion elements with given matrix */
1264 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1266 GpStatus stat;
1268 switch(element->type)
1270 case RegionDataEmptyRect:
1271 case RegionDataInfiniteRect:
1272 return Ok;
1273 case RegionDataRect:
1275 /* We can't transform a rectangle, so convert it to a path. */
1276 GpRegion *new_region;
1277 GpPath *path;
1279 stat = GdipCreatePath(FillModeAlternate, &path);
1280 if (stat == Ok)
1282 stat = GdipAddPathRectangle(path,
1283 element->elementdata.rect.X, element->elementdata.rect.Y,
1284 element->elementdata.rect.Width, element->elementdata.rect.Height);
1286 if (stat == Ok)
1287 stat = GdipCreateRegionPath(path, &new_region);
1289 GdipDeletePath(path);
1292 if (stat == Ok)
1294 /* Steal the element from the created region. */
1295 memcpy(element, &new_region->node, sizeof(region_element));
1296 HeapFree(GetProcessHeap(), 0, new_region);
1298 else
1299 return stat;
1301 /* Fall-through to do the actual conversion. */
1302 case RegionDataPath:
1303 stat = GdipTransformMatrixPoints(matrix,
1304 element->elementdata.pathdata.path->pathdata.Points,
1305 element->elementdata.pathdata.path->pathdata.Count);
1306 return stat;
1307 default:
1308 stat = transform_region_element(element->elementdata.combine.left, matrix);
1309 if (stat == Ok)
1310 stat = transform_region_element(element->elementdata.combine.right, matrix);
1311 return stat;
1315 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1317 TRACE("(%p, %p)\n", region, matrix);
1319 if (!region || !matrix)
1320 return InvalidParameter;
1322 return transform_region_element(&region->node, matrix);
1325 /* Translates GpRegion elements with specified offsets */
1326 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1328 INT i;
1330 switch(element->type)
1332 case RegionDataEmptyRect:
1333 case RegionDataInfiniteRect:
1334 return;
1335 case RegionDataRect:
1336 element->elementdata.rect.X += dx;
1337 element->elementdata.rect.Y += dy;
1338 return;
1339 case RegionDataPath:
1340 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1341 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1342 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1344 return;
1345 default:
1346 translate_region_element(element->elementdata.combine.left, dx, dy);
1347 translate_region_element(element->elementdata.combine.right, dx, dy);
1348 return;
1352 /*****************************************************************************
1353 * GdipTranslateRegion [GDIPLUS.@]
1355 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1357 TRACE("(%p, %f, %f)\n", region, dx, dy);
1359 if(!region)
1360 return InvalidParameter;
1362 translate_region_element(&region->node, dx, dy);
1364 return Ok;
1367 /*****************************************************************************
1368 * GdipTranslateRegionI [GDIPLUS.@]
1370 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1372 TRACE("(%p, %d, %d)\n", region, dx, dy);
1374 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1377 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1379 GpRegion *region_copy;
1380 GpStatus stat;
1381 HRGN hrgn;
1382 DWORD data_size;
1384 stat = GdipCloneRegion(region, &region_copy);
1386 if (stat == Ok)
1388 stat = GdipTransformRegion(region_copy, matrix);
1390 if (stat == Ok)
1391 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1393 if (stat == Ok)
1395 if (hrgn)
1397 data_size = GetRegionData(hrgn, 0, NULL);
1399 *data = GdipAlloc(data_size);
1401 if (*data)
1402 GetRegionData(hrgn, data_size, *data);
1403 else
1404 stat = OutOfMemory;
1406 DeleteObject(hrgn);
1408 else
1410 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1412 *data = GdipAlloc(data_size);
1414 if (*data)
1416 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1417 (*data)->rdh.iType = RDH_RECTANGLES;
1418 (*data)->rdh.nCount = 1;
1419 (*data)->rdh.nRgnSize = sizeof(RECT);
1420 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1421 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1423 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1425 else
1426 stat = OutOfMemory;
1430 GdipDeleteRegion(region_copy);
1433 return stat;
1436 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1438 GpStatus stat;
1439 LPRGNDATA data;
1441 TRACE("(%p, %p, %p)\n", region, count, matrix);
1443 if (!region || !count || !matrix)
1444 return InvalidParameter;
1446 stat = get_region_scans_data(region, matrix, &data);
1448 if (stat == Ok)
1450 *count = data->rdh.nCount;
1451 GdipFree(data);
1454 return stat;
1457 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1459 GpStatus stat;
1460 INT i;
1461 LPRGNDATA data;
1462 RECT *rects;
1464 if (!region || !count || !matrix)
1465 return InvalidParameter;
1467 stat = get_region_scans_data(region, matrix, &data);
1469 if (stat == Ok)
1471 *count = data->rdh.nCount;
1472 rects = (RECT*)data->Buffer;
1474 if (scans)
1476 for (i=0; i<data->rdh.nCount; i++)
1478 scans[i].X = rects[i].left;
1479 scans[i].Y = rects[i].top;
1480 scans[i].Width = rects[i].right - rects[i].left;
1481 scans[i].Height = rects[i].bottom - rects[i].top;
1485 GdipFree(data);
1488 return Ok;
1491 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1493 GpStatus stat;
1494 INT i;
1495 LPRGNDATA data;
1496 RECT *rects;
1498 if (!region || !count || !matrix)
1499 return InvalidParameter;
1501 stat = get_region_scans_data(region, matrix, &data);
1503 if (stat == Ok)
1505 *count = data->rdh.nCount;
1506 rects = (RECT*)data->Buffer;
1508 if (scans)
1510 for (i=0; i<data->rdh.nCount; i++)
1512 scans[i].X = rects[i].left;
1513 scans[i].Y = rects[i].top;
1514 scans[i].Width = rects[i].right - rects[i].left;
1515 scans[i].Height = rects[i].bottom - rects[i].top;
1519 GdipFree(data);
1522 return Ok;