gdiplus: Support GdipSetClipRegion in metafiles.
[wine.git] / dlls / gdiplus / region.c
blob015b67788e0250640fa9849d3fa4d9de2b439ae2
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
3 * Copyright (C) 2013 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
26 #include "objbase.h"
28 #include "gdiplus.h"
29 #include "gdiplus_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34 /**********************************************************
36 * Data returned by GdipGetRegionData looks something like this:
38 * struct region_data_header
39 * {
40 * DWORD size; size in bytes of the data - 8.
41 * DWORD magic1; probably a checksum.
42 * DWORD magic2; always seems to be 0xdbc01001 - version?
43 * DWORD num_ops; number of combining ops * 2
44 * };
46 * Then follows a sequence of combining ops and region elements.
48 * A region element is either a RECTF or some path data.
50 * Combining ops are just stored as their CombineMode value.
52 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
53 * stored as 0x10000002 (with no following RECTF) and an infinite rect
54 * is stored as 0x10000003 (again with no following RECTF).
56 * Path data is preceded by the DWORD 0x10000001. Then follows a
57 * DWORD size and then size bytes of data.
59 * The combining ops are stored in the reverse order to the region
60 * elements and in the reverse order to which the region was
61 * constructed.
63 * When two or more complex regions (ie those with more than one
64 * element) are combined, the combining op for the two regions comes
65 * first, then the combining ops for the region elements in region 1,
66 * followed by the region elements for region 1, then follows the
67 * combining ops for region 2 and finally region 2's region elements.
68 * Presumably you're supposed to use the 0x1000000x header to find the
69 * end of the op list (the count of the elements in each region is not
70 * stored).
72 * When a simple region (1 element) is combined, it's treated as if a
73 * single rect/path is being combined.
77 #define FLAGS_INTPATH 0x4000
79 struct memory_buffer
81 const BYTE *buffer;
82 INT size, pos;
85 struct region_header
87 DWORD magic;
88 DWORD num_children;
91 struct region_data_header
93 DWORD size;
94 DWORD checksum;
95 struct region_header header;
98 struct path_header
100 DWORD size;
101 DWORD magic;
102 DWORD count;
103 DWORD flags;
106 typedef struct packed_point
108 short X;
109 short Y;
110 } packed_point;
112 static inline INT get_element_size(const region_element* element)
114 INT needed = sizeof(DWORD); /* DWORD for the type */
115 switch(element->type)
117 case RegionDataRect:
118 return needed + sizeof(GpRect);
119 case RegionDataPath:
121 needed += write_path_data(element->elementdata.path, NULL);
122 needed += sizeof(DWORD); /* Extra DWORD for path size */
123 return needed;
125 case RegionDataEmptyRect:
126 case RegionDataInfiniteRect:
127 return needed;
128 default:
129 needed += get_element_size(element->elementdata.combine.left);
130 needed += get_element_size(element->elementdata.combine.right);
131 return needed;
134 return 0;
137 /* Does not check parameters, caller must do that */
138 static inline GpStatus init_region(GpRegion* region, const RegionType type)
140 region->node.type = type;
141 region->num_children = 0;
143 return Ok;
146 static inline GpStatus clone_element(const region_element* element,
147 region_element** element2)
149 GpStatus stat;
151 /* root node is allocated with GpRegion */
152 if(!*element2){
153 *element2 = heap_alloc_zero(sizeof(region_element));
154 if (!*element2)
155 return OutOfMemory;
158 (*element2)->type = element->type;
160 switch (element->type)
162 case RegionDataRect:
163 (*element2)->elementdata.rect = element->elementdata.rect;
164 return Ok;
165 case RegionDataEmptyRect:
166 case RegionDataInfiniteRect:
167 return Ok;
168 case RegionDataPath:
169 stat = GdipClonePath(element->elementdata.path, &(*element2)->elementdata.path);
170 if (stat == Ok) return Ok;
171 break;
172 default:
173 (*element2)->elementdata.combine.left = NULL;
174 (*element2)->elementdata.combine.right = NULL;
176 stat = clone_element(element->elementdata.combine.left,
177 &(*element2)->elementdata.combine.left);
178 if (stat == Ok)
180 stat = clone_element(element->elementdata.combine.right,
181 &(*element2)->elementdata.combine.right);
182 if (stat == Ok) return Ok;
184 break;
187 delete_element(*element2);
188 *element2 = NULL;
189 return stat;
192 /* Common code for CombineRegion*
193 * All the caller has to do is get its format into an element
195 static inline void fuse_region(GpRegion* region, region_element* left,
196 region_element* right, const CombineMode mode)
198 region->node.type = mode;
199 region->node.elementdata.combine.left = left;
200 region->node.elementdata.combine.right = right;
201 region->num_children += 2;
204 /*****************************************************************************
205 * GdipCloneRegion [GDIPLUS.@]
207 * Creates a deep copy of the region
209 * PARAMS
210 * region [I] source region
211 * clone [O] resulting clone
213 * RETURNS
214 * SUCCESS: Ok
215 * FAILURE: InvalidParameter or OutOfMemory
217 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
219 region_element *element;
221 TRACE("%p %p\n", region, clone);
223 if (!(region && clone))
224 return InvalidParameter;
226 *clone = heap_alloc_zero(sizeof(GpRegion));
227 if (!*clone)
228 return OutOfMemory;
229 element = &(*clone)->node;
231 (*clone)->num_children = region->num_children;
232 return clone_element(&region->node, &element);
235 /*****************************************************************************
236 * GdipCombineRegionPath [GDIPLUS.@]
238 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
240 GpRegion *path_region;
241 region_element *left, *right = NULL;
242 GpStatus stat;
244 TRACE("%p %p %d\n", region, path, mode);
246 if (!(region && path))
247 return InvalidParameter;
249 stat = GdipCreateRegionPath(path, &path_region);
250 if (stat != Ok)
251 return stat;
253 /* simply replace region data */
254 if(mode == CombineModeReplace){
255 delete_element(&region->node);
256 memcpy(region, path_region, sizeof(GpRegion));
257 heap_free(path_region);
258 return Ok;
261 left = heap_alloc_zero(sizeof(region_element));
262 if (left)
264 *left = region->node;
265 stat = clone_element(&path_region->node, &right);
266 if (stat == Ok)
268 fuse_region(region, left, right, mode);
269 GdipDeleteRegion(path_region);
270 return Ok;
273 else
274 stat = OutOfMemory;
276 heap_free(left);
277 GdipDeleteRegion(path_region);
278 return stat;
281 /*****************************************************************************
282 * GdipCombineRegionRect [GDIPLUS.@]
284 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
285 GDIPCONST GpRectF *rect, CombineMode mode)
287 GpRegion *rect_region;
288 region_element *left, *right = NULL;
289 GpStatus stat;
291 TRACE("%p %s %d\n", region, debugstr_rectf(rect), mode);
293 if (!(region && rect))
294 return InvalidParameter;
296 stat = GdipCreateRegionRect(rect, &rect_region);
297 if (stat != Ok)
298 return stat;
300 /* simply replace region data */
301 if(mode == CombineModeReplace){
302 delete_element(&region->node);
303 memcpy(region, rect_region, sizeof(GpRegion));
304 heap_free(rect_region);
305 return Ok;
308 left = heap_alloc_zero(sizeof(region_element));
309 if (left)
311 memcpy(left, &region->node, sizeof(region_element));
312 stat = clone_element(&rect_region->node, &right);
313 if (stat == Ok)
315 fuse_region(region, left, right, mode);
316 GdipDeleteRegion(rect_region);
317 return Ok;
320 else
321 stat = OutOfMemory;
323 heap_free(left);
324 GdipDeleteRegion(rect_region);
325 return stat;
328 /*****************************************************************************
329 * GdipCombineRegionRectI [GDIPLUS.@]
331 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
332 GDIPCONST GpRect *rect, CombineMode mode)
334 GpRectF rectf;
336 TRACE("%p %p %d\n", region, rect, mode);
338 if (!rect)
339 return InvalidParameter;
341 rectf.X = (REAL)rect->X;
342 rectf.Y = (REAL)rect->Y;
343 rectf.Height = (REAL)rect->Height;
344 rectf.Width = (REAL)rect->Width;
346 return GdipCombineRegionRect(region, &rectf, mode);
349 /*****************************************************************************
350 * GdipCombineRegionRegion [GDIPLUS.@]
352 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
353 GpRegion *region2, CombineMode mode)
355 region_element *left, *right = NULL;
356 GpStatus stat;
357 GpRegion *reg2copy;
359 TRACE("%p %p %d\n", region1, region2, mode);
361 if(!(region1 && region2))
362 return InvalidParameter;
364 /* simply replace region data */
365 if(mode == CombineModeReplace){
366 stat = GdipCloneRegion(region2, &reg2copy);
367 if(stat != Ok) return stat;
369 delete_element(&region1->node);
370 memcpy(region1, reg2copy, sizeof(GpRegion));
371 heap_free(reg2copy);
372 return Ok;
375 left = heap_alloc_zero(sizeof(region_element));
376 if (!left)
377 return OutOfMemory;
379 *left = region1->node;
380 stat = clone_element(&region2->node, &right);
381 if (stat != Ok)
383 heap_free(left);
384 return OutOfMemory;
387 fuse_region(region1, left, right, mode);
388 region1->num_children += region2->num_children;
390 return Ok;
393 /*****************************************************************************
394 * GdipCreateRegion [GDIPLUS.@]
396 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
398 TRACE("%p\n", region);
400 if(!region)
401 return InvalidParameter;
403 *region = heap_alloc_zero(sizeof(GpRegion));
404 if(!*region)
405 return OutOfMemory;
407 TRACE("=> %p\n", *region);
409 return init_region(*region, RegionDataInfiniteRect);
412 /*****************************************************************************
413 * GdipCreateRegionPath [GDIPLUS.@]
415 * Creates a GpRegion from a GpPath
417 * PARAMS
418 * path [I] path to base the region on
419 * region [O] pointer to the newly allocated region
421 * RETURNS
422 * SUCCESS: Ok
423 * FAILURE: InvalidParameter
425 * NOTES
426 * If a path has no floating point points, its points will be stored as shorts
427 * (INTPATH)
429 * If a path is empty, it is considered to be an INTPATH
431 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
433 region_element* element;
434 GpStatus stat;
436 TRACE("%p, %p\n", path, region);
438 if (!(path && region))
439 return InvalidParameter;
441 *region = heap_alloc_zero(sizeof(GpRegion));
442 if(!*region)
443 return OutOfMemory;
444 stat = init_region(*region, RegionDataPath);
445 if (stat != Ok)
447 GdipDeleteRegion(*region);
448 return stat;
450 element = &(*region)->node;
452 stat = GdipClonePath(path, &element->elementdata.path);
453 if (stat != Ok)
455 GdipDeleteRegion(*region);
456 return stat;
459 return Ok;
462 /*****************************************************************************
463 * GdipCreateRegionRect [GDIPLUS.@]
465 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
466 GpRegion **region)
468 GpStatus stat;
470 TRACE("%p, %p\n", rect, region);
472 if (!(rect && region))
473 return InvalidParameter;
475 *region = heap_alloc_zero(sizeof(GpRegion));
476 stat = init_region(*region, RegionDataRect);
477 if(stat != Ok)
479 GdipDeleteRegion(*region);
480 return stat;
483 (*region)->node.elementdata.rect.X = rect->X;
484 (*region)->node.elementdata.rect.Y = rect->Y;
485 (*region)->node.elementdata.rect.Width = rect->Width;
486 (*region)->node.elementdata.rect.Height = rect->Height;
488 return Ok;
491 /*****************************************************************************
492 * GdipCreateRegionRectI [GDIPLUS.@]
494 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
495 GpRegion **region)
497 GpRectF rectf;
499 TRACE("%p, %p\n", rect, region);
501 rectf.X = (REAL)rect->X;
502 rectf.Y = (REAL)rect->Y;
503 rectf.Width = (REAL)rect->Width;
504 rectf.Height = (REAL)rect->Height;
506 return GdipCreateRegionRect(&rectf, region);
509 /******************************************************************************
510 * GdipCreateRegionHrgn [GDIPLUS.@]
512 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
514 DWORD size;
515 LPRGNDATA buf;
516 LPRECT rect;
517 GpStatus stat;
518 GpPath* path;
519 GpRegion* local;
520 DWORD i;
522 TRACE("(%p, %p)\n", hrgn, region);
524 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
525 return InvalidParameter;
527 buf = heap_alloc_zero(size);
528 if(!buf)
529 return OutOfMemory;
531 if(!GetRegionData(hrgn, size, buf)){
532 heap_free(buf);
533 return GenericError;
536 if(buf->rdh.nCount == 0){
537 if((stat = GdipCreateRegion(&local)) != Ok){
538 heap_free(buf);
539 return stat;
541 if((stat = GdipSetEmpty(local)) != Ok){
542 heap_free(buf);
543 GdipDeleteRegion(local);
544 return stat;
546 *region = local;
547 heap_free(buf);
548 return Ok;
551 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
552 heap_free(buf);
553 return stat;
556 rect = (LPRECT)buf->Buffer;
557 for(i = 0; i < buf->rdh.nCount; i++){
558 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
559 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
560 heap_free(buf);
561 GdipDeletePath(path);
562 return stat;
564 rect++;
567 stat = GdipCreateRegionPath(path, region);
569 heap_free(buf);
570 GdipDeletePath(path);
571 return stat;
574 /*****************************************************************************
575 * GdipDeleteRegion [GDIPLUS.@]
577 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
579 TRACE("%p\n", region);
581 if (!region)
582 return InvalidParameter;
584 delete_element(&region->node);
585 heap_free(region);
587 return Ok;
590 /*****************************************************************************
591 * GdipGetRegionBounds [GDIPLUS.@]
593 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
595 HRGN hrgn;
596 RECT r;
597 GpStatus status;
599 TRACE("(%p, %p, %p)\n", region, graphics, rect);
601 if(!region || !graphics || !rect)
602 return InvalidParameter;
604 /* Contrary to MSDN, native ignores the graphics transform. */
605 status = GdipGetRegionHRgn(region, NULL, &hrgn);
606 if(status != Ok)
607 return status;
609 /* infinite */
610 if(!hrgn){
611 rect->X = rect->Y = -(REAL)(1 << 22);
612 rect->Width = rect->Height = (REAL)(1 << 23);
613 TRACE("%p => infinite\n", region);
614 return Ok;
617 if(GetRgnBox(hrgn, &r)){
618 rect->X = r.left;
619 rect->Y = r.top;
620 rect->Width = r.right - r.left;
621 rect->Height = r.bottom - r.top;
622 TRACE("%p => %s\n", region, debugstr_rectf(rect));
624 else
625 status = GenericError;
627 DeleteObject(hrgn);
629 return status;
632 /*****************************************************************************
633 * GdipGetRegionBoundsI [GDIPLUS.@]
635 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
637 GpRectF rectf;
638 GpStatus status;
640 TRACE("(%p, %p, %p)\n", region, graphics, rect);
642 if(!rect)
643 return InvalidParameter;
645 status = GdipGetRegionBounds(region, graphics, &rectf);
646 if(status == Ok){
647 rect->X = gdip_round(rectf.X);
648 rect->Y = gdip_round(rectf.Y);
649 rect->Width = gdip_round(rectf.Width);
650 rect->Height = gdip_round(rectf.Height);
653 return status;
656 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
658 location[*offset] = write;
659 (*offset)++;
662 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
664 ((FLOAT*)location)[*offset] = write;
665 (*offset)++;
668 static void write_element(const region_element* element, DWORD *buffer,
669 INT* filled)
671 write_dword(buffer, filled, element->type);
672 switch (element->type)
674 case CombineModeReplace:
675 case CombineModeIntersect:
676 case CombineModeUnion:
677 case CombineModeXor:
678 case CombineModeExclude:
679 case CombineModeComplement:
680 write_element(element->elementdata.combine.left, buffer, filled);
681 write_element(element->elementdata.combine.right, buffer, filled);
682 break;
683 case RegionDataRect:
684 write_float(buffer, filled, element->elementdata.rect.X);
685 write_float(buffer, filled, element->elementdata.rect.Y);
686 write_float(buffer, filled, element->elementdata.rect.Width);
687 write_float(buffer, filled, element->elementdata.rect.Height);
688 break;
689 case RegionDataPath:
691 DWORD size = write_path_data(element->elementdata.path, buffer + *filled + 1);
692 write_dword(buffer, filled, size);
693 *filled += size / sizeof(DWORD);
694 break;
696 case RegionDataEmptyRect:
697 case RegionDataInfiniteRect:
698 break;
702 DWORD write_region_data(const GpRegion *region, void *data)
704 struct region_header *header = data;
705 INT filled = 0;
706 DWORD size;
708 size = sizeof(struct region_header) + get_element_size(&region->node);
709 if (!data) return size;
711 header->magic = VERSION_MAGIC2;
712 header->num_children = region->num_children;
713 filled += 2;
714 /* With few exceptions, everything written is DWORD aligned,
715 * so use that as our base */
716 write_element(&region->node, (DWORD*)data, &filled);
717 return size;
720 /*****************************************************************************
721 * GdipGetRegionData [GDIPLUS.@]
723 * Returns the header, followed by combining ops and region elements.
725 * PARAMS
726 * region [I] region to retrieve from
727 * buffer [O] buffer to hold the resulting data
728 * size [I] size of the buffer
729 * needed [O] (optional) how much data was written
731 * RETURNS
732 * SUCCESS: Ok
733 * FAILURE: InvalidParameter
735 * NOTES
736 * The header contains the size, a checksum, a version string, and the number
737 * of children. The size does not count itself or the checksum.
738 * Version is always something like 0xdbc01001 or 0xdbc01002
740 * An element is a RECT, or PATH; Combining ops are stored as their
741 * CombineMode value. Special regions (infinite, empty) emit just their
742 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
743 * their code followed by a second header for the path followed by the actual
744 * path data. Followed by the flags for each point. The pathheader contains
745 * the size of the data to follow, a version number again, followed by a count
746 * of how many points, and any special flags which may apply. 0x4000 means it's
747 * a path of shorts instead of FLOAT.
749 * Combining Ops are stored in reverse order from when they were constructed;
750 * the output is a tree where the left side combining area is always taken
751 * first.
753 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
754 UINT *needed)
756 struct region_data_header *region_data_header;
757 UINT required;
759 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
761 if (!region || !buffer || !size)
762 return InvalidParameter;
764 required = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
765 if (size < required)
767 if (needed) *needed = size;
768 return InsufficientBuffer;
771 region_data_header = (struct region_data_header *)buffer;
772 region_data_header->size = write_region_data(region, &region_data_header->header);
773 region_data_header->checksum = 0;
775 if (needed)
776 *needed = required;
778 return Ok;
781 static inline void init_memory_buffer(struct memory_buffer *mbuf, const BYTE *buffer, INT size)
783 mbuf->buffer = buffer;
784 mbuf->size = size;
785 mbuf->pos = 0;
788 static inline const void *buffer_read(struct memory_buffer *mbuf, INT size)
790 if (mbuf->size - mbuf->pos >= size)
792 const void *data = mbuf->buffer + mbuf->pos;
793 mbuf->pos += size;
794 return data;
796 return NULL;
799 static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count)
801 GpStatus status;
802 const DWORD *type;
804 type = buffer_read(mbuf, sizeof(*type));
805 if (!type) return Ok;
807 TRACE("type %#x\n", *type);
809 node->type = *type;
811 switch (node->type)
813 case CombineModeReplace:
814 case CombineModeIntersect:
815 case CombineModeUnion:
816 case CombineModeXor:
817 case CombineModeExclude:
818 case CombineModeComplement:
820 region_element *left, *right;
822 left = heap_alloc_zero(sizeof(region_element));
823 if (!left) return OutOfMemory;
824 right = heap_alloc_zero(sizeof(region_element));
825 if (!right)
827 heap_free(left);
828 return OutOfMemory;
831 status = read_element(mbuf, region, left, count);
832 if (status == Ok)
834 status = read_element(mbuf, region, right, count);
835 if (status == Ok)
837 node->elementdata.combine.left = left;
838 node->elementdata.combine.right = right;
839 region->num_children += 2;
840 return Ok;
844 heap_free(left);
845 heap_free(right);
846 return status;
849 case RegionDataRect:
851 const GpRectF *rc;
853 rc = buffer_read(mbuf, sizeof(*rc));
854 if (!rc)
856 ERR("failed to read rect data\n");
857 return InvalidParameter;
860 node->elementdata.rect = *rc;
861 *count += 1;
862 return Ok;
865 case RegionDataPath:
867 GpPath *path;
868 const struct path_header *path_header;
869 const BYTE *types;
871 path_header = buffer_read(mbuf, sizeof(*path_header));
872 if (!path_header)
874 ERR("failed to read path header\n");
875 return InvalidParameter;
877 if (!VALID_MAGIC(path_header->magic))
879 ERR("invalid path header magic %#x\n", path_header->magic);
880 return InvalidParameter;
883 /* Windows always fails to create an empty path in a region */
884 if (!path_header->count)
886 TRACE("refusing to create an empty path in a region\n");
887 return GenericError;
890 status = GdipCreatePath(FillModeAlternate, &path);
891 if (status) return status;
893 node->elementdata.path = path;
895 if (!lengthen_path(path, path_header->count))
896 return OutOfMemory;
898 path->pathdata.Count = path_header->count;
900 if (path_header->flags & ~FLAGS_INTPATH)
901 FIXME("unhandled path flags %#x\n", path_header->flags);
903 if (path_header->flags & FLAGS_INTPATH)
905 const packed_point *pt;
906 DWORD i;
908 pt = buffer_read(mbuf, sizeof(*pt) * path_header->count);
909 if (!pt)
911 ERR("failed to read packed %u path points\n", path_header->count);
912 return InvalidParameter;
915 for (i = 0; i < path_header->count; i++)
917 path->pathdata.Points[i].X = (REAL)pt[i].X;
918 path->pathdata.Points[i].Y = (REAL)pt[i].Y;
921 else
923 const GpPointF *ptf;
925 ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count);
926 if (!ptf)
928 ERR("failed to read %u path points\n", path_header->count);
929 return InvalidParameter;
931 memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count);
934 types = buffer_read(mbuf, path_header->count);
935 if (!types)
937 ERR("failed to read %u path types\n", path_header->count);
938 return InvalidParameter;
940 memcpy(path->pathdata.Types, types, path_header->count);
941 if (path_header->count & 3)
943 if (!buffer_read(mbuf, 4 - (path_header->count & 3)))
945 ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3));
946 return InvalidParameter;
950 *count += 1;
951 return Ok;
954 case RegionDataEmptyRect:
955 case RegionDataInfiniteRect:
956 *count += 1;
957 return Ok;
959 default:
960 FIXME("element type %#x is not supported\n", *type);
961 break;
964 return InvalidParameter;
967 /*****************************************************************************
968 * GdipCreateRegionRgnData [GDIPLUS.@]
970 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
972 const struct region_data_header *region_data_header;
973 struct memory_buffer mbuf;
974 GpStatus status;
975 INT count;
977 TRACE("(%p, %d, %p)\n", data, size, region);
979 if (!data || !size)
980 return InvalidParameter;
982 init_memory_buffer(&mbuf, data, size);
984 region_data_header = buffer_read(&mbuf, sizeof(*region_data_header));
985 if (!region_data_header || !VALID_MAGIC(region_data_header->header.magic))
986 return InvalidParameter;
988 status = GdipCreateRegion(region);
989 if (status != Ok)
990 return status;
992 count = 0;
993 status = read_element(&mbuf, *region, &(*region)->node, &count);
994 if (status == Ok && !count)
995 status = InvalidParameter;
997 if (status != Ok)
999 GdipDeleteRegion(*region);
1000 *region = NULL;
1003 return status;
1006 /*****************************************************************************
1007 * GdipGetRegionDataSize [GDIPLUS.@]
1009 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
1011 TRACE("%p, %p\n", region, needed);
1013 if (!(region && needed))
1014 return InvalidParameter;
1016 /* header.size doesn't count header.size and header.checksum */
1017 *needed = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
1019 return Ok;
1022 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
1024 HDC new_hdc=NULL;
1025 GpGraphics *new_graphics=NULL;
1026 GpStatus stat;
1027 INT save_state;
1029 if (!path->pathdata.Count) /* PathToRegion doesn't support empty paths */
1031 *hrgn = CreateRectRgn( 0, 0, 0, 0 );
1032 return *hrgn ? Ok : OutOfMemory;
1035 if (!graphics)
1037 new_hdc = CreateCompatibleDC(0);
1038 if (!new_hdc)
1039 return OutOfMemory;
1041 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
1042 graphics = new_graphics;
1043 if (stat != Ok)
1045 DeleteDC(new_hdc);
1046 return stat;
1049 else if (!graphics->hdc)
1051 graphics->hdc = new_hdc = CreateCompatibleDC(0);
1052 if (!new_hdc)
1053 return OutOfMemory;
1056 save_state = SaveDC(graphics->hdc);
1057 EndPath(graphics->hdc);
1059 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1060 : WINDING));
1062 stat = trace_path(graphics, path);
1063 if (stat == Ok)
1065 *hrgn = PathToRegion(graphics->hdc);
1066 stat = *hrgn ? Ok : OutOfMemory;
1069 RestoreDC(graphics->hdc, save_state);
1070 if (new_hdc)
1072 DeleteDC(new_hdc);
1073 if (new_graphics)
1074 GdipDeleteGraphics(new_graphics);
1075 else
1076 graphics->hdc = NULL;
1079 return stat;
1082 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
1084 switch (element->type)
1086 case RegionDataInfiniteRect:
1087 *hrgn = NULL;
1088 return Ok;
1089 case RegionDataEmptyRect:
1090 *hrgn = CreateRectRgn(0, 0, 0, 0);
1091 return *hrgn ? Ok : OutOfMemory;
1092 case RegionDataPath:
1093 return get_path_hrgn(element->elementdata.path, graphics, hrgn);
1094 case RegionDataRect:
1096 GpPath* path;
1097 GpStatus stat;
1098 GpRectF* rc = &element->elementdata.rect;
1100 stat = GdipCreatePath(FillModeAlternate, &path);
1101 if (stat != Ok)
1102 return stat;
1103 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
1105 if (stat == Ok)
1106 stat = get_path_hrgn(path, graphics, hrgn);
1108 GdipDeletePath(path);
1110 return stat;
1112 case CombineModeIntersect:
1113 case CombineModeUnion:
1114 case CombineModeXor:
1115 case CombineModeExclude:
1116 case CombineModeComplement:
1118 HRGN left, right;
1119 GpStatus stat;
1120 int ret;
1122 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
1123 if (stat != Ok)
1125 *hrgn = NULL;
1126 return stat;
1129 if (left == NULL)
1131 /* existing region is infinite */
1132 switch (element->type)
1134 case CombineModeIntersect:
1135 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
1136 case CombineModeXor: case CombineModeExclude:
1137 left = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1138 break;
1139 case CombineModeUnion: case CombineModeComplement:
1140 *hrgn = NULL;
1141 return Ok;
1145 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1146 if (stat != Ok)
1148 DeleteObject(left);
1149 *hrgn = NULL;
1150 return stat;
1153 if (right == NULL)
1155 /* new region is infinite */
1156 switch (element->type)
1158 case CombineModeIntersect:
1159 *hrgn = left;
1160 return Ok;
1161 case CombineModeXor: case CombineModeComplement:
1162 right = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1163 break;
1164 case CombineModeUnion: case CombineModeExclude:
1165 DeleteObject(left);
1166 *hrgn = NULL;
1167 return Ok;
1171 switch (element->type)
1173 case CombineModeIntersect:
1174 ret = CombineRgn(left, left, right, RGN_AND);
1175 break;
1176 case CombineModeUnion:
1177 ret = CombineRgn(left, left, right, RGN_OR);
1178 break;
1179 case CombineModeXor:
1180 ret = CombineRgn(left, left, right, RGN_XOR);
1181 break;
1182 case CombineModeExclude:
1183 ret = CombineRgn(left, left, right, RGN_DIFF);
1184 break;
1185 case CombineModeComplement:
1186 ret = CombineRgn(left, right, left, RGN_DIFF);
1187 break;
1188 default:
1189 ret = ERROR;
1192 DeleteObject(right);
1194 if (ret == ERROR)
1196 DeleteObject(left);
1197 *hrgn = NULL;
1198 return GenericError;
1201 *hrgn = left;
1202 return Ok;
1204 default:
1205 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1206 *hrgn = NULL;
1207 return NotImplemented;
1211 /*****************************************************************************
1212 * GdipGetRegionHRgn [GDIPLUS.@]
1214 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1216 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1218 if (!region || !hrgn)
1219 return InvalidParameter;
1221 return get_region_hrgn(&region->node, graphics, hrgn);
1224 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1226 GpStatus status;
1227 GpRectF rect;
1229 TRACE("(%p, %p, %p)\n", region, graphics, res);
1231 if(!region || !graphics || !res)
1232 return InvalidParameter;
1234 status = GdipGetRegionBounds(region, graphics, &rect);
1235 if (status != Ok) return status;
1237 *res = rect.Width == 0.0 && rect.Height == 0.0;
1238 TRACE("=> %d\n", *res);
1240 return Ok;
1243 /*****************************************************************************
1244 * GdipIsEqualRegion [GDIPLUS.@]
1246 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1247 BOOL *res)
1249 HRGN hrgn1, hrgn2;
1250 GpStatus stat;
1252 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1254 if(!region || !region2 || !graphics || !res)
1255 return InvalidParameter;
1257 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1258 if(stat != Ok)
1259 return stat;
1260 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1261 if(stat != Ok){
1262 DeleteObject(hrgn1);
1263 return stat;
1266 *res = EqualRgn(hrgn1, hrgn2);
1268 /* one of GpRegions is infinite */
1269 if(*res == ERROR)
1270 *res = (!hrgn1 && !hrgn2);
1272 DeleteObject(hrgn1);
1273 DeleteObject(hrgn2);
1275 return Ok;
1278 /*****************************************************************************
1279 * GdipIsInfiniteRegion [GDIPLUS.@]
1281 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1283 /* I think graphics is ignored here */
1284 TRACE("(%p, %p, %p)\n", region, graphics, res);
1286 if(!region || !graphics || !res)
1287 return InvalidParameter;
1289 *res = (region->node.type == RegionDataInfiniteRect);
1291 return Ok;
1294 /*****************************************************************************
1295 * GdipIsVisibleRegionRect [GDIPLUS.@]
1297 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1299 HRGN hrgn;
1300 GpStatus stat;
1301 RECT rect;
1303 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1305 if(!region || !res)
1306 return InvalidParameter;
1308 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1309 return stat;
1311 /* infinite */
1312 if(!hrgn){
1313 *res = TRUE;
1314 return Ok;
1317 SetRect(&rect, ceilr(x), ceilr(y), ceilr(x + w), ceilr(y + h));
1318 *res = RectInRegion(hrgn, &rect);
1320 DeleteObject(hrgn);
1322 return Ok;
1325 /*****************************************************************************
1326 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1328 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1330 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1331 if(!region || !res)
1332 return InvalidParameter;
1334 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1337 /*****************************************************************************
1338 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1340 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1342 HRGN hrgn;
1343 GpStatus stat;
1345 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1347 if(!region || !res)
1348 return InvalidParameter;
1350 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1351 return stat;
1353 /* infinite */
1354 if(!hrgn){
1355 *res = TRUE;
1356 return Ok;
1359 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1361 DeleteObject(hrgn);
1363 return Ok;
1366 /*****************************************************************************
1367 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1369 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1371 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1373 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1376 /*****************************************************************************
1377 * GdipSetEmpty [GDIPLUS.@]
1379 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1381 GpStatus stat;
1383 TRACE("%p\n", region);
1385 if (!region)
1386 return InvalidParameter;
1388 delete_element(&region->node);
1389 stat = init_region(region, RegionDataEmptyRect);
1391 return stat;
1394 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1396 GpStatus stat;
1398 TRACE("%p\n", region);
1400 if (!region)
1401 return InvalidParameter;
1403 delete_element(&region->node);
1404 stat = init_region(region, RegionDataInfiniteRect);
1406 return stat;
1409 /* Transforms GpRegion elements with given matrix */
1410 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1412 GpStatus stat;
1414 switch(element->type)
1416 case RegionDataEmptyRect:
1417 case RegionDataInfiniteRect:
1418 return Ok;
1419 case RegionDataRect:
1421 /* We can't transform a rectangle, so convert it to a path. */
1422 GpRegion *new_region;
1423 GpPath *path;
1425 stat = GdipCreatePath(FillModeAlternate, &path);
1426 if (stat == Ok)
1428 stat = GdipAddPathRectangle(path,
1429 element->elementdata.rect.X, element->elementdata.rect.Y,
1430 element->elementdata.rect.Width, element->elementdata.rect.Height);
1432 if (stat == Ok)
1433 stat = GdipCreateRegionPath(path, &new_region);
1435 GdipDeletePath(path);
1438 if (stat == Ok)
1440 /* Steal the element from the created region. */
1441 memcpy(element, &new_region->node, sizeof(region_element));
1442 heap_free(new_region);
1444 else
1445 return stat;
1447 /* Fall-through to do the actual conversion. */
1448 case RegionDataPath:
1449 if (!element->elementdata.path->pathdata.Count)
1450 return Ok;
1452 stat = GdipTransformMatrixPoints(matrix,
1453 element->elementdata.path->pathdata.Points,
1454 element->elementdata.path->pathdata.Count);
1455 return stat;
1456 default:
1457 stat = transform_region_element(element->elementdata.combine.left, matrix);
1458 if (stat == Ok)
1459 stat = transform_region_element(element->elementdata.combine.right, matrix);
1460 return stat;
1464 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1466 TRACE("(%p, %p)\n", region, matrix);
1468 if (!region || !matrix)
1469 return InvalidParameter;
1471 return transform_region_element(&region->node, matrix);
1474 /* Translates GpRegion elements with specified offsets */
1475 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1477 INT i;
1479 switch(element->type)
1481 case RegionDataEmptyRect:
1482 case RegionDataInfiniteRect:
1483 return;
1484 case RegionDataRect:
1485 element->elementdata.rect.X += dx;
1486 element->elementdata.rect.Y += dy;
1487 return;
1488 case RegionDataPath:
1489 for(i = 0; i < element->elementdata.path->pathdata.Count; i++){
1490 element->elementdata.path->pathdata.Points[i].X += dx;
1491 element->elementdata.path->pathdata.Points[i].Y += dy;
1493 return;
1494 default:
1495 translate_region_element(element->elementdata.combine.left, dx, dy);
1496 translate_region_element(element->elementdata.combine.right, dx, dy);
1497 return;
1501 /*****************************************************************************
1502 * GdipTranslateRegion [GDIPLUS.@]
1504 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1506 TRACE("(%p, %f, %f)\n", region, dx, dy);
1508 if(!region)
1509 return InvalidParameter;
1511 translate_region_element(&region->node, dx, dy);
1513 return Ok;
1516 /*****************************************************************************
1517 * GdipTranslateRegionI [GDIPLUS.@]
1519 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1521 TRACE("(%p, %d, %d)\n", region, dx, dy);
1523 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1526 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1528 GpRegion *region_copy;
1529 GpStatus stat;
1530 HRGN hrgn;
1531 DWORD data_size;
1533 stat = GdipCloneRegion(region, &region_copy);
1535 if (stat == Ok)
1537 stat = GdipTransformRegion(region_copy, matrix);
1539 if (stat == Ok)
1540 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1542 if (stat == Ok)
1544 if (hrgn)
1546 data_size = GetRegionData(hrgn, 0, NULL);
1548 *data = heap_alloc_zero(data_size);
1550 if (*data)
1551 GetRegionData(hrgn, data_size, *data);
1552 else
1553 stat = OutOfMemory;
1555 DeleteObject(hrgn);
1557 else
1559 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1561 *data = heap_alloc_zero(data_size);
1563 if (*data)
1565 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1566 (*data)->rdh.iType = RDH_RECTANGLES;
1567 (*data)->rdh.nCount = 1;
1568 (*data)->rdh.nRgnSize = sizeof(RECT);
1569 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1570 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1572 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1574 else
1575 stat = OutOfMemory;
1579 GdipDeleteRegion(region_copy);
1582 return stat;
1585 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1587 GpStatus stat;
1588 LPRGNDATA data;
1590 TRACE("(%p, %p, %p)\n", region, count, matrix);
1592 if (!region || !count || !matrix)
1593 return InvalidParameter;
1595 stat = get_region_scans_data(region, matrix, &data);
1597 if (stat == Ok)
1599 *count = data->rdh.nCount;
1600 heap_free(data);
1603 return stat;
1606 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1608 GpStatus stat;
1609 DWORD i;
1610 LPRGNDATA data;
1611 RECT *rects;
1613 if (!region || !count || !matrix)
1614 return InvalidParameter;
1616 stat = get_region_scans_data(region, matrix, &data);
1618 if (stat == Ok)
1620 *count = data->rdh.nCount;
1621 rects = (RECT*)data->Buffer;
1623 if (scans)
1625 for (i=0; i<data->rdh.nCount; i++)
1627 scans[i].X = rects[i].left;
1628 scans[i].Y = rects[i].top;
1629 scans[i].Width = rects[i].right - rects[i].left;
1630 scans[i].Height = rects[i].bottom - rects[i].top;
1634 heap_free(data);
1637 return Ok;
1640 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1642 GpStatus stat;
1643 DWORD i;
1644 LPRGNDATA data;
1645 RECT *rects;
1647 if (!region || !count || !matrix)
1648 return InvalidParameter;
1650 stat = get_region_scans_data(region, matrix, &data);
1652 if (stat == Ok)
1654 *count = data->rdh.nCount;
1655 rects = (RECT*)data->Buffer;
1657 if (scans)
1659 for (i=0; i<data->rdh.nCount; i++)
1661 scans[i].X = rects[i].left;
1662 scans[i].Y = rects[i].top;
1663 scans[i].Width = rects[i].right - rects[i].left;
1664 scans[i].Height = rects[i].bottom - rects[i].top;
1668 heap_free(data);
1671 return Ok;