gdiplus: Avoid signed-unsigned integer comparisons.
[wine.git] / dlls / gdiplus / region.c
blobf7bad57fd22611ac7f73243355d8a04718480513
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 return Ok;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
157 return Ok;
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) return Ok;
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)
172 stat = clone_element(element->elementdata.combine.right,
173 &(*element2)->elementdata.combine.right);
174 if (stat == Ok) return Ok;
176 break;
179 delete_element(*element2);
180 *element2 = NULL;
181 return stat;
184 /* Common code for CombineRegion*
185 * All the caller has to do is get its format into an element
187 static inline void fuse_region(GpRegion* region, region_element* left,
188 region_element* right, const CombineMode mode)
190 region->node.type = mode;
191 region->node.elementdata.combine.left = left;
192 region->node.elementdata.combine.right = right;
194 region->header.size = sizeheader_size + get_element_size(&region->node);
195 region->header.num_children += 2;
198 /*****************************************************************************
199 * GdipCloneRegion [GDIPLUS.@]
201 * Creates a deep copy of the region
203 * PARAMS
204 * region [I] source region
205 * clone [O] resulting clone
207 * RETURNS
208 * SUCCESS: Ok
209 * FAILURE: InvalidParameter or OutOfMemory
211 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
213 region_element *element;
215 TRACE("%p %p\n", region, clone);
217 if (!(region && clone))
218 return InvalidParameter;
220 *clone = GdipAlloc(sizeof(GpRegion));
221 if (!*clone)
222 return OutOfMemory;
223 element = &(*clone)->node;
225 (*clone)->header = region->header;
226 return clone_element(&region->node, &element);
229 /*****************************************************************************
230 * GdipCombineRegionPath [GDIPLUS.@]
232 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
234 GpRegion *path_region;
235 region_element *left, *right = NULL;
236 GpStatus stat;
238 TRACE("%p %p %d\n", region, path, mode);
240 if (!(region && path))
241 return InvalidParameter;
243 stat = GdipCreateRegionPath(path, &path_region);
244 if (stat != Ok)
245 return stat;
247 /* simply replace region data */
248 if(mode == CombineModeReplace){
249 delete_element(&region->node);
250 memcpy(region, path_region, sizeof(GpRegion));
251 GdipFree(path_region);
252 return Ok;
255 left = GdipAlloc(sizeof(region_element));
256 if (left)
258 *left = region->node;
259 stat = clone_element(&path_region->node, &right);
260 if (stat == Ok)
262 fuse_region(region, left, right, mode);
263 GdipDeleteRegion(path_region);
264 return Ok;
267 else
268 stat = OutOfMemory;
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 GdipFree(rect_region);
299 return Ok;
302 left = GdipAlloc(sizeof(region_element));
303 if (left)
305 memcpy(left, &region->node, sizeof(region_element));
306 stat = clone_element(&rect_region->node, &right);
307 if (stat == Ok)
309 fuse_region(region, left, right, mode);
310 GdipDeleteRegion(rect_region);
311 return Ok;
314 else
315 stat = OutOfMemory;
317 GdipFree(left);
318 GdipDeleteRegion(rect_region);
319 return stat;
322 /*****************************************************************************
323 * GdipCombineRegionRectI [GDIPLUS.@]
325 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
326 GDIPCONST GpRect *rect, CombineMode mode)
328 GpRectF rectf;
330 TRACE("%p %p %d\n", region, rect, mode);
332 if (!rect)
333 return InvalidParameter;
335 rectf.X = (REAL)rect->X;
336 rectf.Y = (REAL)rect->Y;
337 rectf.Height = (REAL)rect->Height;
338 rectf.Width = (REAL)rect->Width;
340 return GdipCombineRegionRect(region, &rectf, mode);
343 /*****************************************************************************
344 * GdipCombineRegionRegion [GDIPLUS.@]
346 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
347 GpRegion *region2, CombineMode mode)
349 region_element *left, *right = NULL;
350 GpStatus stat;
351 GpRegion *reg2copy;
353 TRACE("%p %p %d\n", region1, region2, mode);
355 if(!(region1 && region2))
356 return InvalidParameter;
358 /* simply replace region data */
359 if(mode == CombineModeReplace){
360 stat = GdipCloneRegion(region2, &reg2copy);
361 if(stat != Ok) return stat;
363 delete_element(&region1->node);
364 memcpy(region1, reg2copy, sizeof(GpRegion));
365 GdipFree(reg2copy);
366 return Ok;
369 left = GdipAlloc(sizeof(region_element));
370 if (!left)
371 return OutOfMemory;
373 *left = region1->node;
374 stat = clone_element(&region2->node, &right);
375 if (stat != Ok)
377 GdipFree(left);
378 return OutOfMemory;
381 fuse_region(region1, left, right, mode);
382 region1->header.num_children += region2->header.num_children;
384 return Ok;
387 /*****************************************************************************
388 * GdipCreateRegion [GDIPLUS.@]
390 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
392 TRACE("%p\n", region);
394 if(!region)
395 return InvalidParameter;
397 *region = GdipAlloc(sizeof(GpRegion));
398 if(!*region)
399 return OutOfMemory;
401 TRACE("=> %p\n", *region);
403 return init_region(*region, RegionDataInfiniteRect);
406 /*****************************************************************************
407 * GdipCreateRegionPath [GDIPLUS.@]
409 * Creates a GpRegion from a GpPath
411 * PARAMS
412 * path [I] path to base the region on
413 * region [O] pointer to the newly allocated region
415 * RETURNS
416 * SUCCESS: Ok
417 * FAILURE: InvalidParameter
419 * NOTES
420 * If a path has no floating point points, its points will be stored as shorts
421 * (INTPATH)
423 * If a path is empty, it is considered to be an INTPATH
425 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
427 region_element* element;
428 GpPoint *pointsi;
429 GpPointF *pointsf;
431 GpStatus stat;
432 DWORD flags = FLAGS_INTPATH;
433 INT count, i;
435 TRACE("%p, %p\n", path, region);
437 if (!(path && region))
438 return InvalidParameter;
440 *region = GdipAlloc(sizeof(GpRegion));
441 if(!*region)
442 return OutOfMemory;
443 stat = init_region(*region, RegionDataPath);
444 if (stat != Ok)
446 GdipDeleteRegion(*region);
447 return stat;
449 element = &(*region)->node;
450 count = path->pathdata.Count;
452 /* Test to see if the path is an Integer path */
453 if (count)
455 pointsi = GdipAlloc(sizeof(GpPoint) * count);
456 pointsf = GdipAlloc(sizeof(GpPointF) * count);
457 if (!(pointsi && pointsf))
459 GdipFree(pointsi);
460 GdipFree(pointsf);
461 GdipDeleteRegion(*region);
462 return OutOfMemory;
465 stat = GdipGetPathPointsI(path, pointsi, count);
466 if (stat != Ok)
468 GdipDeleteRegion(*region);
469 return stat;
471 stat = GdipGetPathPoints(path, pointsf, count);
472 if (stat != Ok)
474 GdipDeleteRegion(*region);
475 return stat;
478 for (i = 0; i < count; i++)
480 if (!(pointsi[i].X == pointsf[i].X &&
481 pointsi[i].Y == pointsf[i].Y ))
483 flags = FLAGS_NOFLAGS;
484 break;
487 GdipFree(pointsi);
488 GdipFree(pointsf);
491 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
492 if (stat != Ok)
494 GdipDeleteRegion(*region);
495 return stat;
498 /* 3 for headers, once again size doesn't count itself */
499 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
500 switch(flags)
502 /* Floats, sent out as floats */
503 case FLAGS_NOFLAGS:
504 element->elementdata.pathdata.pathheader.size +=
505 (sizeof(DWORD) * count * 2);
506 break;
507 /* INTs, sent out as packed shorts */
508 case FLAGS_INTPATH:
509 element->elementdata.pathdata.pathheader.size +=
510 (sizeof(DWORD) * count);
511 break;
512 default:
513 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
515 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
516 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
517 element->elementdata.pathdata.pathheader.count = count;
518 element->elementdata.pathdata.pathheader.flags = flags;
519 (*region)->header.size = sizeheader_size + get_element_size(element);
521 return Ok;
524 /*****************************************************************************
525 * GdipCreateRegionRect [GDIPLUS.@]
527 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
528 GpRegion **region)
530 GpStatus stat;
532 TRACE("%p, %p\n", rect, region);
534 if (!(rect && region))
535 return InvalidParameter;
537 *region = GdipAlloc(sizeof(GpRegion));
538 stat = init_region(*region, RegionDataRect);
539 if(stat != Ok)
541 GdipDeleteRegion(*region);
542 return stat;
545 (*region)->node.elementdata.rect.X = rect->X;
546 (*region)->node.elementdata.rect.Y = rect->Y;
547 (*region)->node.elementdata.rect.Width = rect->Width;
548 (*region)->node.elementdata.rect.Height = rect->Height;
550 return Ok;
553 /*****************************************************************************
554 * GdipCreateRegionRectI [GDIPLUS.@]
556 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
557 GpRegion **region)
559 GpRectF rectf;
561 TRACE("%p, %p\n", rect, region);
563 rectf.X = (REAL)rect->X;
564 rectf.Y = (REAL)rect->Y;
565 rectf.Width = (REAL)rect->Width;
566 rectf.Height = (REAL)rect->Height;
568 return GdipCreateRegionRect(&rectf, region);
571 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
573 FIXME("(%p, %d, %p): stub\n", data, size, region);
575 *region = NULL;
576 return NotImplemented;
580 /******************************************************************************
581 * GdipCreateRegionHrgn [GDIPLUS.@]
583 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
585 DWORD size;
586 LPRGNDATA buf;
587 LPRECT rect;
588 GpStatus stat;
589 GpPath* path;
590 GpRegion* local;
591 DWORD i;
593 TRACE("(%p, %p)\n", hrgn, region);
595 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
596 return InvalidParameter;
598 buf = GdipAlloc(size);
599 if(!buf)
600 return OutOfMemory;
602 if(!GetRegionData(hrgn, size, buf)){
603 GdipFree(buf);
604 return GenericError;
607 if(buf->rdh.nCount == 0){
608 if((stat = GdipCreateRegion(&local)) != Ok){
609 GdipFree(buf);
610 return stat;
612 if((stat = GdipSetEmpty(local)) != Ok){
613 GdipFree(buf);
614 GdipDeleteRegion(local);
615 return stat;
617 *region = local;
618 GdipFree(buf);
619 return Ok;
622 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
623 GdipFree(buf);
624 return stat;
627 rect = (LPRECT)buf->Buffer;
628 for(i = 0; i < buf->rdh.nCount; i++){
629 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
630 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
631 GdipFree(buf);
632 GdipDeletePath(path);
633 return stat;
635 rect++;
638 stat = GdipCreateRegionPath(path, region);
640 GdipFree(buf);
641 GdipDeletePath(path);
642 return stat;
645 /*****************************************************************************
646 * GdipDeleteRegion [GDIPLUS.@]
648 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
650 TRACE("%p\n", region);
652 if (!region)
653 return InvalidParameter;
655 delete_element(&region->node);
656 GdipFree(region);
658 return Ok;
661 /*****************************************************************************
662 * GdipGetRegionBounds [GDIPLUS.@]
664 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
666 HRGN hrgn;
667 RECT r;
668 GpStatus status;
670 TRACE("(%p, %p, %p)\n", region, graphics, rect);
672 if(!region || !graphics || !rect)
673 return InvalidParameter;
675 /* Contrary to MSDN, native ignores the graphics transform. */
676 status = GdipGetRegionHRgn(region, NULL, &hrgn);
677 if(status != Ok)
678 return status;
680 /* infinite */
681 if(!hrgn){
682 rect->X = rect->Y = -(REAL)(1 << 22);
683 rect->Width = rect->Height = (REAL)(1 << 23);
684 TRACE("%p => infinite\n", region);
685 return Ok;
688 if(GetRgnBox(hrgn, &r)){
689 rect->X = r.left;
690 rect->Y = r.top;
691 rect->Width = r.right - r.left;
692 rect->Height = r.bottom - r.top;
693 TRACE("%p => %s\n", region, debugstr_rectf(rect));
695 else
696 status = GenericError;
698 DeleteObject(hrgn);
700 return status;
703 /*****************************************************************************
704 * GdipGetRegionBoundsI [GDIPLUS.@]
706 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
708 GpRectF rectf;
709 GpStatus status;
711 TRACE("(%p, %p, %p)\n", region, graphics, rect);
713 if(!rect)
714 return InvalidParameter;
716 status = GdipGetRegionBounds(region, graphics, &rectf);
717 if(status == Ok){
718 rect->X = gdip_round(rectf.X);
719 rect->Y = gdip_round(rectf.X);
720 rect->Width = gdip_round(rectf.Width);
721 rect->Height = gdip_round(rectf.Height);
724 return status;
727 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
729 location[*offset] = write;
730 (*offset)++;
733 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
735 ((FLOAT*)location)[*offset] = write;
736 (*offset)++;
739 static inline void write_packed_point(DWORD* location, INT* offset,
740 const GpPointF* write)
742 packed_point point;
744 point.X = write->X;
745 point.Y = write->Y;
746 memcpy(location + *offset, &point, sizeof(packed_point));
747 (*offset)++;
750 static inline void write_path_types(DWORD* location, INT* offset,
751 const GpPath* path)
753 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
755 /* The unwritten parts of the DWORD (if any) must be cleared */
756 if (path->pathdata.Count % sizeof(DWORD))
757 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
758 path->pathdata.Count,
759 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
760 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
763 static void write_element(const region_element* element, DWORD *buffer,
764 INT* filled)
766 write_dword(buffer, filled, element->type);
767 switch (element->type)
769 case CombineModeReplace:
770 case CombineModeIntersect:
771 case CombineModeUnion:
772 case CombineModeXor:
773 case CombineModeExclude:
774 case CombineModeComplement:
775 write_element(element->elementdata.combine.left, buffer, filled);
776 write_element(element->elementdata.combine.right, buffer, filled);
777 break;
778 case RegionDataRect:
779 write_float(buffer, filled, element->elementdata.rect.X);
780 write_float(buffer, filled, element->elementdata.rect.Y);
781 write_float(buffer, filled, element->elementdata.rect.Width);
782 write_float(buffer, filled, element->elementdata.rect.Height);
783 break;
784 case RegionDataPath:
786 INT i;
787 const GpPath* path = element->elementdata.pathdata.path;
789 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
790 sizeof(element->elementdata.pathdata.pathheader));
791 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
792 switch (element->elementdata.pathdata.pathheader.flags)
794 case FLAGS_NOFLAGS:
795 for (i = 0; i < path->pathdata.Count; i++)
797 write_float(buffer, filled, path->pathdata.Points[i].X);
798 write_float(buffer, filled, path->pathdata.Points[i].Y);
800 break;
801 case FLAGS_INTPATH:
802 for (i = 0; i < path->pathdata.Count; i++)
804 write_packed_point(buffer, filled,
805 &path->pathdata.Points[i]);
808 write_path_types(buffer, filled, path);
809 break;
811 case RegionDataEmptyRect:
812 case RegionDataInfiniteRect:
813 break;
817 /*****************************************************************************
818 * GdipGetRegionData [GDIPLUS.@]
820 * Returns the header, followed by combining ops and region elements.
822 * PARAMS
823 * region [I] region to retrieve from
824 * buffer [O] buffer to hold the resulting data
825 * size [I] size of the buffer
826 * needed [O] (optional) how much data was written
828 * RETURNS
829 * SUCCESS: Ok
830 * FAILURE: InvalidParameter
832 * NOTES
833 * The header contains the size, a checksum, a version string, and the number
834 * of children. The size does not count itself or the checksum.
835 * Version is always something like 0xdbc01001 or 0xdbc01002
837 * An element is a RECT, or PATH; Combining ops are stored as their
838 * CombineMode value. Special regions (infinite, empty) emit just their
839 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
840 * their code followed by a second header for the path followed by the actual
841 * path data. Followed by the flags for each point. The pathheader contains
842 * the size of the data to follow, a version number again, followed by a count
843 * of how many points, and any special flags which may apply. 0x4000 means its
844 * a path of shorts instead of FLOAT.
846 * Combining Ops are stored in reverse order from when they were constructed;
847 * the output is a tree where the left side combining area is always taken
848 * first.
850 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
851 UINT *needed)
853 INT filled = 0;
855 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
857 if (!(region && buffer && size))
858 return InvalidParameter;
860 memcpy(buffer, &region->header, sizeof(region->header));
861 filled += sizeof(region->header) / sizeof(DWORD);
862 /* With few exceptions, everything written is DWORD aligned,
863 * so use that as our base */
864 write_element(&region->node, (DWORD*)buffer, &filled);
866 if (needed)
867 *needed = filled * sizeof(DWORD);
869 return Ok;
872 /*****************************************************************************
873 * GdipGetRegionDataSize [GDIPLUS.@]
875 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
877 TRACE("%p, %p\n", region, needed);
879 if (!(region && needed))
880 return InvalidParameter;
882 /* header.size doesn't count header.size and header.checksum */
883 *needed = region->header.size + sizeof(DWORD) * 2;
885 return Ok;
888 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
890 HDC new_hdc=NULL;
891 GpGraphics *new_graphics=NULL;
892 GpStatus stat;
893 INT save_state;
895 if (!graphics)
897 new_hdc = GetDC(0);
898 if (!new_hdc)
899 return OutOfMemory;
901 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
902 graphics = new_graphics;
903 if (stat != Ok)
905 ReleaseDC(0, new_hdc);
906 return stat;
909 else if (!graphics->hdc)
911 graphics->hdc = new_hdc = GetDC(0);
912 if (!new_hdc)
913 return OutOfMemory;
916 save_state = SaveDC(graphics->hdc);
917 EndPath(graphics->hdc);
919 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
920 : WINDING));
922 stat = trace_path(graphics, path);
923 if (stat == Ok)
925 *hrgn = PathToRegion(graphics->hdc);
926 stat = *hrgn ? Ok : OutOfMemory;
929 RestoreDC(graphics->hdc, save_state);
930 if (new_hdc)
932 ReleaseDC(0, new_hdc);
933 if (new_graphics)
934 GdipDeleteGraphics(new_graphics);
935 else
936 graphics->hdc = NULL;
939 return stat;
942 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
944 switch (element->type)
946 case RegionDataInfiniteRect:
947 *hrgn = NULL;
948 return Ok;
949 case RegionDataEmptyRect:
950 *hrgn = CreateRectRgn(0, 0, 0, 0);
951 return *hrgn ? Ok : OutOfMemory;
952 case RegionDataPath:
953 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
954 case RegionDataRect:
956 GpPath* path;
957 GpStatus stat;
958 GpRectF* rc = &element->elementdata.rect;
960 stat = GdipCreatePath(FillModeAlternate, &path);
961 if (stat != Ok)
962 return stat;
963 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
965 if (stat == Ok)
966 stat = get_path_hrgn(path, graphics, hrgn);
968 GdipDeletePath(path);
970 return stat;
972 case CombineModeIntersect:
973 case CombineModeUnion:
974 case CombineModeXor:
975 case CombineModeExclude:
976 case CombineModeComplement:
978 HRGN left, right;
979 GpStatus stat;
980 int ret;
982 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
983 if (stat != Ok)
985 *hrgn = NULL;
986 return stat;
989 if (left == NULL)
991 /* existing region is infinite */
992 switch (element->type)
994 case CombineModeIntersect:
995 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
996 case CombineModeXor: case CombineModeExclude:
997 FIXME("cannot exclude from an infinite region\n");
998 /* fall-through */
999 case CombineModeUnion: case CombineModeComplement:
1000 *hrgn = NULL;
1001 return Ok;
1005 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1006 if (stat != Ok)
1008 DeleteObject(left);
1009 *hrgn = NULL;
1010 return stat;
1013 if (right == NULL)
1015 /* new region is infinite */
1016 switch (element->type)
1018 case CombineModeIntersect:
1019 *hrgn = left;
1020 return Ok;
1021 case CombineModeXor: case CombineModeComplement:
1022 FIXME("cannot exclude from an infinite region\n");
1023 /* fall-through */
1024 case CombineModeUnion: case CombineModeExclude:
1025 DeleteObject(left);
1026 *hrgn = NULL;
1027 return Ok;
1031 switch (element->type)
1033 case CombineModeIntersect:
1034 ret = CombineRgn(left, left, right, RGN_AND);
1035 break;
1036 case CombineModeUnion:
1037 ret = CombineRgn(left, left, right, RGN_OR);
1038 break;
1039 case CombineModeXor:
1040 ret = CombineRgn(left, left, right, RGN_XOR);
1041 break;
1042 case CombineModeExclude:
1043 ret = CombineRgn(left, left, right, RGN_DIFF);
1044 break;
1045 case CombineModeComplement:
1046 ret = CombineRgn(left, right, left, RGN_DIFF);
1047 break;
1048 default:
1049 ret = ERROR;
1052 DeleteObject(right);
1054 if (ret == ERROR)
1056 DeleteObject(left);
1057 *hrgn = NULL;
1058 return GenericError;
1061 *hrgn = left;
1062 return Ok;
1064 default:
1065 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1066 *hrgn = NULL;
1067 return NotImplemented;
1071 /*****************************************************************************
1072 * GdipGetRegionHRgn [GDIPLUS.@]
1074 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1076 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1078 if (!region || !hrgn)
1079 return InvalidParameter;
1081 return get_region_hrgn(&region->node, graphics, hrgn);
1084 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1086 GpStatus status;
1087 GpRectF rect;
1089 TRACE("(%p, %p, %p)\n", region, graphics, res);
1091 if(!region || !graphics || !res)
1092 return InvalidParameter;
1094 status = GdipGetRegionBounds(region, graphics, &rect);
1095 if (status != Ok) return status;
1097 *res = rect.Width == 0.0 && rect.Height == 0.0;
1098 TRACE("=> %d\n", *res);
1100 return Ok;
1103 /*****************************************************************************
1104 * GdipIsEqualRegion [GDIPLUS.@]
1106 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1107 BOOL *res)
1109 HRGN hrgn1, hrgn2;
1110 GpStatus stat;
1112 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1114 if(!region || !region2 || !graphics || !res)
1115 return InvalidParameter;
1117 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1118 if(stat != Ok)
1119 return stat;
1120 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1121 if(stat != Ok){
1122 DeleteObject(hrgn1);
1123 return stat;
1126 *res = EqualRgn(hrgn1, hrgn2);
1128 /* one of GpRegions is infinite */
1129 if(*res == ERROR)
1130 *res = (!hrgn1 && !hrgn2);
1132 DeleteObject(hrgn1);
1133 DeleteObject(hrgn2);
1135 return Ok;
1138 /*****************************************************************************
1139 * GdipIsInfiniteRegion [GDIPLUS.@]
1141 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1143 /* I think graphics is ignored here */
1144 TRACE("(%p, %p, %p)\n", region, graphics, res);
1146 if(!region || !graphics || !res)
1147 return InvalidParameter;
1149 *res = (region->node.type == RegionDataInfiniteRect);
1151 return Ok;
1154 /*****************************************************************************
1155 * GdipIsVisibleRegionRect [GDIPLUS.@]
1157 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1159 HRGN hrgn;
1160 GpStatus stat;
1161 RECT rect;
1163 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1165 if(!region || !res)
1166 return InvalidParameter;
1168 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1169 return stat;
1171 /* infinite */
1172 if(!hrgn){
1173 *res = TRUE;
1174 return Ok;
1177 rect.left = ceilr(x);
1178 rect.top = ceilr(y);
1179 rect.right = ceilr(x + w);
1180 rect.bottom = ceilr(y + h);
1182 *res = RectInRegion(hrgn, &rect);
1184 DeleteObject(hrgn);
1186 return Ok;
1189 /*****************************************************************************
1190 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1192 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1194 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1195 if(!region || !res)
1196 return InvalidParameter;
1198 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1201 /*****************************************************************************
1202 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1204 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1206 HRGN hrgn;
1207 GpStatus stat;
1209 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1211 if(!region || !res)
1212 return InvalidParameter;
1214 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1215 return stat;
1217 /* infinite */
1218 if(!hrgn){
1219 *res = TRUE;
1220 return Ok;
1223 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1225 DeleteObject(hrgn);
1227 return Ok;
1230 /*****************************************************************************
1231 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1233 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1235 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1237 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1240 /*****************************************************************************
1241 * GdipSetEmpty [GDIPLUS.@]
1243 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1245 GpStatus stat;
1247 TRACE("%p\n", region);
1249 if (!region)
1250 return InvalidParameter;
1252 delete_element(&region->node);
1253 stat = init_region(region, RegionDataEmptyRect);
1255 return stat;
1258 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1260 GpStatus stat;
1262 TRACE("%p\n", region);
1264 if (!region)
1265 return InvalidParameter;
1267 delete_element(&region->node);
1268 stat = init_region(region, RegionDataInfiniteRect);
1270 return stat;
1273 /* Transforms GpRegion elements with given matrix */
1274 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1276 GpStatus stat;
1278 switch(element->type)
1280 case RegionDataEmptyRect:
1281 case RegionDataInfiniteRect:
1282 return Ok;
1283 case RegionDataRect:
1285 /* We can't transform a rectangle, so convert it to a path. */
1286 GpRegion *new_region;
1287 GpPath *path;
1289 stat = GdipCreatePath(FillModeAlternate, &path);
1290 if (stat == Ok)
1292 stat = GdipAddPathRectangle(path,
1293 element->elementdata.rect.X, element->elementdata.rect.Y,
1294 element->elementdata.rect.Width, element->elementdata.rect.Height);
1296 if (stat == Ok)
1297 stat = GdipCreateRegionPath(path, &new_region);
1299 GdipDeletePath(path);
1302 if (stat == Ok)
1304 /* Steal the element from the created region. */
1305 memcpy(element, &new_region->node, sizeof(region_element));
1306 HeapFree(GetProcessHeap(), 0, new_region);
1308 else
1309 return stat;
1311 /* Fall-through to do the actual conversion. */
1312 case RegionDataPath:
1313 stat = GdipTransformMatrixPoints(matrix,
1314 element->elementdata.pathdata.path->pathdata.Points,
1315 element->elementdata.pathdata.path->pathdata.Count);
1316 return stat;
1317 default:
1318 stat = transform_region_element(element->elementdata.combine.left, matrix);
1319 if (stat == Ok)
1320 stat = transform_region_element(element->elementdata.combine.right, matrix);
1321 return stat;
1325 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1327 TRACE("(%p, %p)\n", region, matrix);
1329 if (!region || !matrix)
1330 return InvalidParameter;
1332 return transform_region_element(&region->node, matrix);
1335 /* Translates GpRegion elements with specified offsets */
1336 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1338 INT i;
1340 switch(element->type)
1342 case RegionDataEmptyRect:
1343 case RegionDataInfiniteRect:
1344 return;
1345 case RegionDataRect:
1346 element->elementdata.rect.X += dx;
1347 element->elementdata.rect.Y += dy;
1348 return;
1349 case RegionDataPath:
1350 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1351 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1352 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1354 return;
1355 default:
1356 translate_region_element(element->elementdata.combine.left, dx, dy);
1357 translate_region_element(element->elementdata.combine.right, dx, dy);
1358 return;
1362 /*****************************************************************************
1363 * GdipTranslateRegion [GDIPLUS.@]
1365 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1367 TRACE("(%p, %f, %f)\n", region, dx, dy);
1369 if(!region)
1370 return InvalidParameter;
1372 translate_region_element(&region->node, dx, dy);
1374 return Ok;
1377 /*****************************************************************************
1378 * GdipTranslateRegionI [GDIPLUS.@]
1380 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1382 TRACE("(%p, %d, %d)\n", region, dx, dy);
1384 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1387 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1389 GpRegion *region_copy;
1390 GpStatus stat;
1391 HRGN hrgn;
1392 DWORD data_size;
1394 stat = GdipCloneRegion(region, &region_copy);
1396 if (stat == Ok)
1398 stat = GdipTransformRegion(region_copy, matrix);
1400 if (stat == Ok)
1401 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1403 if (stat == Ok)
1405 if (hrgn)
1407 data_size = GetRegionData(hrgn, 0, NULL);
1409 *data = GdipAlloc(data_size);
1411 if (*data)
1412 GetRegionData(hrgn, data_size, *data);
1413 else
1414 stat = OutOfMemory;
1416 DeleteObject(hrgn);
1418 else
1420 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1422 *data = GdipAlloc(data_size);
1424 if (*data)
1426 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1427 (*data)->rdh.iType = RDH_RECTANGLES;
1428 (*data)->rdh.nCount = 1;
1429 (*data)->rdh.nRgnSize = sizeof(RECT);
1430 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1431 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1433 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1435 else
1436 stat = OutOfMemory;
1440 GdipDeleteRegion(region_copy);
1443 return stat;
1446 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1448 GpStatus stat;
1449 LPRGNDATA data;
1451 TRACE("(%p, %p, %p)\n", region, count, matrix);
1453 if (!region || !count || !matrix)
1454 return InvalidParameter;
1456 stat = get_region_scans_data(region, matrix, &data);
1458 if (stat == Ok)
1460 *count = data->rdh.nCount;
1461 GdipFree(data);
1464 return stat;
1467 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1469 GpStatus stat;
1470 DWORD i;
1471 LPRGNDATA data;
1472 RECT *rects;
1474 if (!region || !count || !matrix)
1475 return InvalidParameter;
1477 stat = get_region_scans_data(region, matrix, &data);
1479 if (stat == Ok)
1481 *count = data->rdh.nCount;
1482 rects = (RECT*)data->Buffer;
1484 if (scans)
1486 for (i=0; i<data->rdh.nCount; i++)
1488 scans[i].X = rects[i].left;
1489 scans[i].Y = rects[i].top;
1490 scans[i].Width = rects[i].right - rects[i].left;
1491 scans[i].Height = rects[i].bottom - rects[i].top;
1495 GdipFree(data);
1498 return Ok;
1501 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1503 GpStatus stat;
1504 DWORD i;
1505 LPRGNDATA data;
1506 RECT *rects;
1508 if (!region || !count || !matrix)
1509 return InvalidParameter;
1511 stat = get_region_scans_data(region, matrix, &data);
1513 if (stat == Ok)
1515 *count = data->rdh.nCount;
1516 rects = (RECT*)data->Buffer;
1518 if (scans)
1520 for (i=0; i<data->rdh.nCount; i++)
1522 scans[i].X = rects[i].left;
1523 scans[i].Y = rects[i].top;
1524 scans[i].Width = rects[i].right - rects[i].left;
1525 scans[i].Height = rects[i].bottom - rects[i].top;
1529 GdipFree(data);
1532 return Ok;