mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / gdiplus / region.c
blob0f14e8aae323be16c81afb0b1fe470774774e35e
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 region_header
81 DWORD magic;
82 DWORD num_children;
85 struct region_data_header
87 DWORD size;
88 DWORD checksum;
89 struct region_header header;
92 struct path_header
94 DWORD size;
95 DWORD magic;
96 DWORD count;
97 DWORD flags;
100 typedef struct packed_point
102 short X;
103 short Y;
104 } packed_point;
106 static inline INT get_element_size(const region_element* element)
108 INT needed = sizeof(DWORD); /* DWORD for the type */
109 switch(element->type)
111 case RegionDataRect:
112 return needed + sizeof(GpRect);
113 case RegionDataPath:
115 needed += write_path_data(element->elementdata.path, NULL);
116 needed += sizeof(DWORD); /* Extra DWORD for path size */
117 return needed;
119 case RegionDataEmptyRect:
120 case RegionDataInfiniteRect:
121 return needed;
122 default:
123 needed += get_element_size(element->elementdata.combine.left);
124 needed += get_element_size(element->elementdata.combine.right);
125 return needed;
128 return 0;
131 /* Does not check parameters, caller must do that */
132 static inline GpStatus init_region(GpRegion* region, const RegionType type)
134 region->node.type = type;
135 region->num_children = 0;
137 return Ok;
140 static inline GpStatus clone_element(const region_element* element,
141 region_element** element2)
143 GpStatus stat;
145 /* root node is allocated with GpRegion */
146 if(!*element2){
147 *element2 = heap_alloc_zero(sizeof(region_element));
148 if (!*element2)
149 return OutOfMemory;
152 (*element2)->type = element->type;
154 switch (element->type)
156 case RegionDataRect:
157 (*element2)->elementdata.rect = element->elementdata.rect;
158 return Ok;
159 case RegionDataEmptyRect:
160 case RegionDataInfiniteRect:
161 return Ok;
162 case RegionDataPath:
163 stat = GdipClonePath(element->elementdata.path, &(*element2)->elementdata.path);
164 if (stat == Ok) return Ok;
165 break;
166 default:
167 (*element2)->elementdata.combine.left = NULL;
168 (*element2)->elementdata.combine.right = NULL;
170 stat = clone_element(element->elementdata.combine.left,
171 &(*element2)->elementdata.combine.left);
172 if (stat == Ok)
174 stat = clone_element(element->elementdata.combine.right,
175 &(*element2)->elementdata.combine.right);
176 if (stat == Ok) return Ok;
178 break;
181 delete_element(*element2);
182 *element2 = NULL;
183 return stat;
186 /* Common code for CombineRegion*
187 * All the caller has to do is get its format into an element
189 static inline void fuse_region(GpRegion* region, region_element* left,
190 region_element* right, const CombineMode mode)
192 region->node.type = mode;
193 region->node.elementdata.combine.left = left;
194 region->node.elementdata.combine.right = right;
195 region->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 = heap_alloc_zero(sizeof(GpRegion));
221 if (!*clone)
222 return OutOfMemory;
223 element = &(*clone)->node;
225 (*clone)->num_children = region->num_children;
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 heap_free(path_region);
252 return Ok;
255 left = heap_alloc_zero(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 heap_free(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 %s %d\n", region, debugstr_rectf(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 heap_free(rect_region);
299 return Ok;
302 left = heap_alloc_zero(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 heap_free(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 set_rect(&rectf, rect->X, rect->Y, rect->Width, rect->Height);
336 return GdipCombineRegionRect(region, &rectf, mode);
339 /*****************************************************************************
340 * GdipCombineRegionRegion [GDIPLUS.@]
342 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
343 GpRegion *region2, CombineMode mode)
345 region_element *left, *right = NULL;
346 GpStatus stat;
347 GpRegion *reg2copy;
349 TRACE("%p %p %d\n", region1, region2, mode);
351 if(!(region1 && region2))
352 return InvalidParameter;
354 /* simply replace region data */
355 if(mode == CombineModeReplace){
356 stat = GdipCloneRegion(region2, &reg2copy);
357 if(stat != Ok) return stat;
359 delete_element(&region1->node);
360 memcpy(region1, reg2copy, sizeof(GpRegion));
361 heap_free(reg2copy);
362 return Ok;
365 left = heap_alloc_zero(sizeof(region_element));
366 if (!left)
367 return OutOfMemory;
369 *left = region1->node;
370 stat = clone_element(&region2->node, &right);
371 if (stat != Ok)
373 heap_free(left);
374 return OutOfMemory;
377 fuse_region(region1, left, right, mode);
378 region1->num_children += region2->num_children;
380 return Ok;
383 /*****************************************************************************
384 * GdipCreateRegion [GDIPLUS.@]
386 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
388 TRACE("%p\n", region);
390 if(!region)
391 return InvalidParameter;
393 *region = heap_alloc_zero(sizeof(GpRegion));
394 if(!*region)
395 return OutOfMemory;
397 TRACE("=> %p\n", *region);
399 return init_region(*region, RegionDataInfiniteRect);
402 /*****************************************************************************
403 * GdipCreateRegionPath [GDIPLUS.@]
405 * Creates a GpRegion from a GpPath
407 * PARAMS
408 * path [I] path to base the region on
409 * region [O] pointer to the newly allocated region
411 * RETURNS
412 * SUCCESS: Ok
413 * FAILURE: InvalidParameter
415 * NOTES
416 * If a path has no floating point points, its points will be stored as shorts
417 * (INTPATH)
419 * If a path is empty, it is considered to be an INTPATH
421 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
423 region_element* element;
424 GpStatus stat;
426 TRACE("%p, %p\n", path, region);
428 if (!(path && region))
429 return InvalidParameter;
431 *region = heap_alloc_zero(sizeof(GpRegion));
432 if(!*region)
433 return OutOfMemory;
434 stat = init_region(*region, RegionDataPath);
435 if (stat != Ok)
437 GdipDeleteRegion(*region);
438 return stat;
440 element = &(*region)->node;
442 stat = GdipClonePath(path, &element->elementdata.path);
443 if (stat != Ok)
445 GdipDeleteRegion(*region);
446 return stat;
449 return Ok;
452 /*****************************************************************************
453 * GdipCreateRegionRect [GDIPLUS.@]
455 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
456 GpRegion **region)
458 GpStatus stat;
460 TRACE("%s, %p\n", debugstr_rectf(rect), region);
462 if (!(rect && region))
463 return InvalidParameter;
465 *region = heap_alloc_zero(sizeof(GpRegion));
466 stat = init_region(*region, RegionDataRect);
467 if(stat != Ok)
469 GdipDeleteRegion(*region);
470 return stat;
473 (*region)->node.elementdata.rect.X = rect->X;
474 (*region)->node.elementdata.rect.Y = rect->Y;
475 (*region)->node.elementdata.rect.Width = rect->Width;
476 (*region)->node.elementdata.rect.Height = rect->Height;
478 return Ok;
481 /*****************************************************************************
482 * GdipCreateRegionRectI [GDIPLUS.@]
484 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
485 GpRegion **region)
487 GpRectF rectf;
489 TRACE("%p, %p\n", rect, region);
491 set_rect(&rectf, rect->X, rect->Y, rect->Width, rect->Height);
492 return GdipCreateRegionRect(&rectf, region);
495 /******************************************************************************
496 * GdipCreateRegionHrgn [GDIPLUS.@]
498 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
500 DWORD size;
501 LPRGNDATA buf;
502 LPRECT rect;
503 GpStatus stat;
504 GpPath* path;
505 GpRegion* local;
506 DWORD i;
508 TRACE("(%p, %p)\n", hrgn, region);
510 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
511 return InvalidParameter;
513 buf = heap_alloc_zero(size);
514 if(!buf)
515 return OutOfMemory;
517 if(!GetRegionData(hrgn, size, buf)){
518 heap_free(buf);
519 return GenericError;
522 if(buf->rdh.nCount == 0){
523 if((stat = GdipCreateRegion(&local)) != Ok){
524 heap_free(buf);
525 return stat;
527 if((stat = GdipSetEmpty(local)) != Ok){
528 heap_free(buf);
529 GdipDeleteRegion(local);
530 return stat;
532 *region = local;
533 heap_free(buf);
534 return Ok;
537 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
538 heap_free(buf);
539 return stat;
542 rect = (LPRECT)buf->Buffer;
543 for(i = 0; i < buf->rdh.nCount; i++){
544 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
545 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
546 heap_free(buf);
547 GdipDeletePath(path);
548 return stat;
550 rect++;
553 stat = GdipCreateRegionPath(path, region);
555 heap_free(buf);
556 GdipDeletePath(path);
557 return stat;
560 /*****************************************************************************
561 * GdipDeleteRegion [GDIPLUS.@]
563 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
565 TRACE("%p\n", region);
567 if (!region)
568 return InvalidParameter;
570 delete_element(&region->node);
571 heap_free(region);
573 return Ok;
576 /*****************************************************************************
577 * GdipGetRegionBounds [GDIPLUS.@]
579 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
581 HRGN hrgn;
582 RECT r;
583 GpStatus status;
585 TRACE("(%p, %p, %p)\n", region, graphics, rect);
587 if(!region || !graphics || !rect)
588 return InvalidParameter;
590 /* Contrary to MSDN, native ignores the graphics transform. */
591 status = GdipGetRegionHRgn(region, NULL, &hrgn);
592 if(status != Ok)
593 return status;
595 /* infinite */
596 if(!hrgn){
597 rect->X = rect->Y = -(REAL)(1 << 22);
598 rect->Width = rect->Height = (REAL)(1 << 23);
599 TRACE("%p => infinite\n", region);
600 return Ok;
603 if(GetRgnBox(hrgn, &r)){
604 rect->X = r.left;
605 rect->Y = r.top;
606 rect->Width = r.right - r.left;
607 rect->Height = r.bottom - r.top;
608 TRACE("%p => %s\n", region, debugstr_rectf(rect));
610 else
611 status = GenericError;
613 DeleteObject(hrgn);
615 return status;
618 /*****************************************************************************
619 * GdipGetRegionBoundsI [GDIPLUS.@]
621 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
623 GpRectF rectf;
624 GpStatus status;
626 TRACE("(%p, %p, %p)\n", region, graphics, rect);
628 if(!rect)
629 return InvalidParameter;
631 status = GdipGetRegionBounds(region, graphics, &rectf);
632 if(status == Ok){
633 rect->X = gdip_round(rectf.X);
634 rect->Y = gdip_round(rectf.Y);
635 rect->Width = gdip_round(rectf.Width);
636 rect->Height = gdip_round(rectf.Height);
639 return status;
642 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
644 location[*offset] = write;
645 (*offset)++;
648 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
650 ((FLOAT*)location)[*offset] = write;
651 (*offset)++;
654 static void write_element(const region_element* element, DWORD *buffer,
655 INT* filled)
657 write_dword(buffer, filled, element->type);
658 switch (element->type)
660 case CombineModeReplace:
661 case CombineModeIntersect:
662 case CombineModeUnion:
663 case CombineModeXor:
664 case CombineModeExclude:
665 case CombineModeComplement:
666 write_element(element->elementdata.combine.left, buffer, filled);
667 write_element(element->elementdata.combine.right, buffer, filled);
668 break;
669 case RegionDataRect:
670 write_float(buffer, filled, element->elementdata.rect.X);
671 write_float(buffer, filled, element->elementdata.rect.Y);
672 write_float(buffer, filled, element->elementdata.rect.Width);
673 write_float(buffer, filled, element->elementdata.rect.Height);
674 break;
675 case RegionDataPath:
677 DWORD size = write_path_data(element->elementdata.path, buffer + *filled + 1);
678 write_dword(buffer, filled, size);
679 *filled += size / sizeof(DWORD);
680 break;
682 case RegionDataEmptyRect:
683 case RegionDataInfiniteRect:
684 break;
688 DWORD write_region_data(const GpRegion *region, void *data)
690 struct region_header *header = data;
691 INT filled = 0;
692 DWORD size;
694 size = sizeof(struct region_header) + get_element_size(&region->node);
695 if (!data) return size;
697 header->magic = VERSION_MAGIC2;
698 header->num_children = region->num_children;
699 filled += 2;
700 /* With few exceptions, everything written is DWORD aligned,
701 * so use that as our base */
702 write_element(&region->node, (DWORD*)data, &filled);
703 return size;
706 /*****************************************************************************
707 * GdipGetRegionData [GDIPLUS.@]
709 * Returns the header, followed by combining ops and region elements.
711 * PARAMS
712 * region [I] region to retrieve from
713 * buffer [O] buffer to hold the resulting data
714 * size [I] size of the buffer
715 * needed [O] (optional) how much data was written
717 * RETURNS
718 * SUCCESS: Ok
719 * FAILURE: InvalidParameter
721 * NOTES
722 * The header contains the size, a checksum, a version string, and the number
723 * of children. The size does not count itself or the checksum.
724 * Version is always something like 0xdbc01001 or 0xdbc01002
726 * An element is a RECT, or PATH; Combining ops are stored as their
727 * CombineMode value. Special regions (infinite, empty) emit just their
728 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
729 * their code followed by a second header for the path followed by the actual
730 * path data. Followed by the flags for each point. The pathheader contains
731 * the size of the data to follow, a version number again, followed by a count
732 * of how many points, and any special flags which may apply. 0x4000 means it's
733 * a path of shorts instead of FLOAT.
735 * Combining Ops are stored in reverse order from when they were constructed;
736 * the output is a tree where the left side combining area is always taken
737 * first.
739 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
740 UINT *needed)
742 struct region_data_header *region_data_header;
743 UINT required;
745 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
747 if (!region || !buffer || !size)
748 return InvalidParameter;
750 required = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
751 if (size < required)
753 if (needed) *needed = size;
754 return InsufficientBuffer;
757 region_data_header = (struct region_data_header *)buffer;
758 region_data_header->size = write_region_data(region, &region_data_header->header);
759 region_data_header->checksum = 0;
761 if (needed)
762 *needed = required;
764 return Ok;
767 static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count)
769 GpStatus status;
770 const DWORD *type;
772 type = buffer_read(mbuf, sizeof(*type));
773 if (!type) return Ok;
775 TRACE("type %#x\n", *type);
777 node->type = *type;
779 switch (node->type)
781 case CombineModeReplace:
782 case CombineModeIntersect:
783 case CombineModeUnion:
784 case CombineModeXor:
785 case CombineModeExclude:
786 case CombineModeComplement:
788 region_element *left, *right;
790 left = heap_alloc_zero(sizeof(region_element));
791 if (!left) return OutOfMemory;
792 right = heap_alloc_zero(sizeof(region_element));
793 if (!right)
795 heap_free(left);
796 return OutOfMemory;
799 status = read_element(mbuf, region, left, count);
800 if (status == Ok)
802 status = read_element(mbuf, region, right, count);
803 if (status == Ok)
805 node->elementdata.combine.left = left;
806 node->elementdata.combine.right = right;
807 region->num_children += 2;
808 return Ok;
812 heap_free(left);
813 heap_free(right);
814 return status;
817 case RegionDataRect:
819 const GpRectF *rc;
821 rc = buffer_read(mbuf, sizeof(*rc));
822 if (!rc)
824 ERR("failed to read rect data\n");
825 return InvalidParameter;
828 node->elementdata.rect = *rc;
829 *count += 1;
830 return Ok;
833 case RegionDataPath:
835 GpPath *path;
836 const struct path_header *path_header;
837 const BYTE *types;
839 path_header = buffer_read(mbuf, sizeof(*path_header));
840 if (!path_header)
842 ERR("failed to read path header\n");
843 return InvalidParameter;
845 if (!VALID_MAGIC(path_header->magic))
847 ERR("invalid path header magic %#x\n", path_header->magic);
848 return InvalidParameter;
851 /* Windows always fails to create an empty path in a region */
852 if (!path_header->count)
854 TRACE("refusing to create an empty path in a region\n");
855 return GenericError;
858 status = GdipCreatePath(FillModeAlternate, &path);
859 if (status) return status;
861 node->elementdata.path = path;
863 if (!lengthen_path(path, path_header->count))
864 return OutOfMemory;
866 path->pathdata.Count = path_header->count;
868 if (path_header->flags & ~FLAGS_INTPATH)
869 FIXME("unhandled path flags %#x\n", path_header->flags);
871 if (path_header->flags & FLAGS_INTPATH)
873 const packed_point *pt;
874 DWORD i;
876 pt = buffer_read(mbuf, sizeof(*pt) * path_header->count);
877 if (!pt)
879 ERR("failed to read packed %u path points\n", path_header->count);
880 return InvalidParameter;
883 for (i = 0; i < path_header->count; i++)
885 path->pathdata.Points[i].X = (REAL)pt[i].X;
886 path->pathdata.Points[i].Y = (REAL)pt[i].Y;
889 else
891 const GpPointF *ptf;
893 ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count);
894 if (!ptf)
896 ERR("failed to read %u path points\n", path_header->count);
897 return InvalidParameter;
899 memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count);
902 types = buffer_read(mbuf, path_header->count);
903 if (!types)
905 ERR("failed to read %u path types\n", path_header->count);
906 return InvalidParameter;
908 memcpy(path->pathdata.Types, types, path_header->count);
909 if (path_header->count & 3)
911 if (!buffer_read(mbuf, 4 - (path_header->count & 3)))
913 ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3));
914 return InvalidParameter;
918 *count += 1;
919 return Ok;
922 case RegionDataEmptyRect:
923 case RegionDataInfiniteRect:
924 *count += 1;
925 return Ok;
927 default:
928 FIXME("element type %#x is not supported\n", *type);
929 break;
932 return InvalidParameter;
935 /*****************************************************************************
936 * GdipCreateRegionRgnData [GDIPLUS.@]
938 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
940 const struct region_data_header *region_data_header;
941 struct memory_buffer mbuf;
942 GpStatus status;
943 INT count;
945 TRACE("(%p, %d, %p)\n", data, size, region);
947 if (!data || !size)
948 return InvalidParameter;
950 init_memory_buffer(&mbuf, data, size);
952 region_data_header = buffer_read(&mbuf, sizeof(*region_data_header));
953 if (!region_data_header || !VALID_MAGIC(region_data_header->header.magic))
954 return InvalidParameter;
956 status = GdipCreateRegion(region);
957 if (status != Ok)
958 return status;
960 count = 0;
961 status = read_element(&mbuf, *region, &(*region)->node, &count);
962 if (status == Ok && !count)
963 status = InvalidParameter;
965 if (status != Ok)
967 GdipDeleteRegion(*region);
968 *region = NULL;
971 return status;
974 /*****************************************************************************
975 * GdipGetRegionDataSize [GDIPLUS.@]
977 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
979 TRACE("%p, %p\n", region, needed);
981 if (!(region && needed))
982 return InvalidParameter;
984 /* header.size doesn't count header.size and header.checksum */
985 *needed = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
987 return Ok;
990 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
992 HDC new_hdc=NULL;
993 GpGraphics *new_graphics=NULL;
994 GpStatus stat;
995 INT save_state;
997 if (!path->pathdata.Count) /* PathToRegion doesn't support empty paths */
999 *hrgn = CreateRectRgn( 0, 0, 0, 0 );
1000 return *hrgn ? Ok : OutOfMemory;
1003 if (!graphics)
1005 new_hdc = CreateCompatibleDC(0);
1006 if (!new_hdc)
1007 return OutOfMemory;
1009 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
1010 graphics = new_graphics;
1011 if (stat != Ok)
1013 DeleteDC(new_hdc);
1014 return stat;
1017 else if (!graphics->hdc)
1019 graphics->hdc = new_hdc = CreateCompatibleDC(0);
1020 if (!new_hdc)
1021 return OutOfMemory;
1024 save_state = SaveDC(graphics->hdc);
1025 EndPath(graphics->hdc);
1027 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1028 : WINDING));
1030 gdi_transform_acquire(graphics);
1032 stat = trace_path(graphics, path);
1033 if (stat == Ok)
1035 *hrgn = PathToRegion(graphics->hdc);
1036 stat = *hrgn ? Ok : OutOfMemory;
1039 gdi_transform_release(graphics);
1041 RestoreDC(graphics->hdc, save_state);
1042 if (new_hdc)
1044 DeleteDC(new_hdc);
1045 if (new_graphics)
1046 GdipDeleteGraphics(new_graphics);
1047 else
1048 graphics->hdc = NULL;
1051 return stat;
1054 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
1056 switch (element->type)
1058 case RegionDataInfiniteRect:
1059 *hrgn = NULL;
1060 return Ok;
1061 case RegionDataEmptyRect:
1062 *hrgn = CreateRectRgn(0, 0, 0, 0);
1063 return *hrgn ? Ok : OutOfMemory;
1064 case RegionDataPath:
1065 return get_path_hrgn(element->elementdata.path, graphics, hrgn);
1066 case RegionDataRect:
1068 GpPath* path;
1069 GpStatus stat;
1070 GpRectF* rc = &element->elementdata.rect;
1072 stat = GdipCreatePath(FillModeAlternate, &path);
1073 if (stat != Ok)
1074 return stat;
1075 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
1077 if (stat == Ok)
1078 stat = get_path_hrgn(path, graphics, hrgn);
1080 GdipDeletePath(path);
1082 return stat;
1084 case CombineModeIntersect:
1085 case CombineModeUnion:
1086 case CombineModeXor:
1087 case CombineModeExclude:
1088 case CombineModeComplement:
1090 HRGN left, right;
1091 GpStatus stat;
1092 int ret;
1094 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
1095 if (stat != Ok)
1097 *hrgn = NULL;
1098 return stat;
1101 if (left == NULL)
1103 /* existing region is infinite */
1104 switch (element->type)
1106 case CombineModeIntersect:
1107 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
1108 case CombineModeXor: case CombineModeExclude:
1109 left = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1110 break;
1111 case CombineModeUnion: case CombineModeComplement:
1112 *hrgn = NULL;
1113 return Ok;
1117 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1118 if (stat != Ok)
1120 DeleteObject(left);
1121 *hrgn = NULL;
1122 return stat;
1125 if (right == NULL)
1127 /* new region is infinite */
1128 switch (element->type)
1130 case CombineModeIntersect:
1131 *hrgn = left;
1132 return Ok;
1133 case CombineModeXor: case CombineModeComplement:
1134 right = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1135 break;
1136 case CombineModeUnion: case CombineModeExclude:
1137 DeleteObject(left);
1138 *hrgn = NULL;
1139 return Ok;
1143 switch (element->type)
1145 case CombineModeIntersect:
1146 ret = CombineRgn(left, left, right, RGN_AND);
1147 break;
1148 case CombineModeUnion:
1149 ret = CombineRgn(left, left, right, RGN_OR);
1150 break;
1151 case CombineModeXor:
1152 ret = CombineRgn(left, left, right, RGN_XOR);
1153 break;
1154 case CombineModeExclude:
1155 ret = CombineRgn(left, left, right, RGN_DIFF);
1156 break;
1157 case CombineModeComplement:
1158 ret = CombineRgn(left, right, left, RGN_DIFF);
1159 break;
1160 default:
1161 ret = ERROR;
1164 DeleteObject(right);
1166 if (ret == ERROR)
1168 DeleteObject(left);
1169 *hrgn = NULL;
1170 return GenericError;
1173 *hrgn = left;
1174 return Ok;
1176 default:
1177 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1178 *hrgn = NULL;
1179 return NotImplemented;
1183 /*****************************************************************************
1184 * GdipGetRegionHRgn [GDIPLUS.@]
1186 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1188 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1190 if (!region || !hrgn)
1191 return InvalidParameter;
1193 return get_region_hrgn(&region->node, graphics, hrgn);
1196 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1198 GpStatus status;
1199 GpRectF rect;
1201 TRACE("(%p, %p, %p)\n", region, graphics, res);
1203 if(!region || !graphics || !res)
1204 return InvalidParameter;
1206 status = GdipGetRegionBounds(region, graphics, &rect);
1207 if (status != Ok) return status;
1209 *res = rect.Width == 0.0 && rect.Height == 0.0;
1210 TRACE("=> %d\n", *res);
1212 return Ok;
1215 /*****************************************************************************
1216 * GdipIsEqualRegion [GDIPLUS.@]
1218 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1219 BOOL *res)
1221 HRGN hrgn1, hrgn2;
1222 GpStatus stat;
1224 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1226 if(!region || !region2 || !graphics || !res)
1227 return InvalidParameter;
1229 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1230 if(stat != Ok)
1231 return stat;
1232 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1233 if(stat != Ok){
1234 DeleteObject(hrgn1);
1235 return stat;
1238 *res = EqualRgn(hrgn1, hrgn2);
1240 /* one of GpRegions is infinite */
1241 if(*res == ERROR)
1242 *res = (!hrgn1 && !hrgn2);
1244 DeleteObject(hrgn1);
1245 DeleteObject(hrgn2);
1247 return Ok;
1250 /*****************************************************************************
1251 * GdipIsInfiniteRegion [GDIPLUS.@]
1253 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1255 /* I think graphics is ignored here */
1256 TRACE("(%p, %p, %p)\n", region, graphics, res);
1258 if(!region || !graphics || !res)
1259 return InvalidParameter;
1261 *res = (region->node.type == RegionDataInfiniteRect);
1263 return Ok;
1266 /*****************************************************************************
1267 * GdipIsVisibleRegionRect [GDIPLUS.@]
1269 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1271 HRGN hrgn;
1272 GpStatus stat;
1273 RECT rect;
1275 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1277 if(!region || !res)
1278 return InvalidParameter;
1280 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1281 return stat;
1283 /* infinite */
1284 if(!hrgn){
1285 *res = TRUE;
1286 return Ok;
1289 SetRect(&rect, ceilr(x), ceilr(y), ceilr(x + w), ceilr(y + h));
1290 *res = RectInRegion(hrgn, &rect);
1292 DeleteObject(hrgn);
1294 return Ok;
1297 /*****************************************************************************
1298 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1300 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1302 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1303 if(!region || !res)
1304 return InvalidParameter;
1306 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1309 /*****************************************************************************
1310 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1312 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1314 HRGN hrgn;
1315 GpStatus stat;
1317 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1319 if(!region || !res)
1320 return InvalidParameter;
1322 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1323 return stat;
1325 /* infinite */
1326 if(!hrgn){
1327 *res = TRUE;
1328 return Ok;
1331 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1333 DeleteObject(hrgn);
1335 return Ok;
1338 /*****************************************************************************
1339 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1341 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1343 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1345 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1348 /*****************************************************************************
1349 * GdipSetEmpty [GDIPLUS.@]
1351 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1353 GpStatus stat;
1355 TRACE("%p\n", region);
1357 if (!region)
1358 return InvalidParameter;
1360 delete_element(&region->node);
1361 stat = init_region(region, RegionDataEmptyRect);
1363 return stat;
1366 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1368 GpStatus stat;
1370 TRACE("%p\n", region);
1372 if (!region)
1373 return InvalidParameter;
1375 delete_element(&region->node);
1376 stat = init_region(region, RegionDataInfiniteRect);
1378 return stat;
1381 /* Transforms GpRegion elements with given matrix */
1382 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1384 GpStatus stat;
1386 switch(element->type)
1388 case RegionDataEmptyRect:
1389 case RegionDataInfiniteRect:
1390 return Ok;
1391 case RegionDataRect:
1393 GpRegion *new_region;
1394 GpPath *path;
1396 if (matrix->matrix[1] == 0.0 && matrix->matrix[2] == 0.0)
1398 GpPointF points[2];
1400 points[0].X = element->elementdata.rect.X;
1401 points[0].Y = element->elementdata.rect.Y;
1402 points[1].X = element->elementdata.rect.X + element->elementdata.rect.Width;
1403 points[1].Y = element->elementdata.rect.Y + element->elementdata.rect.Height;
1405 stat = GdipTransformMatrixPoints(matrix, points, 2);
1406 if (stat != Ok)
1407 return stat;
1409 if (points[0].X > points[1].X)
1411 REAL temp;
1412 temp = points[0].X;
1413 points[0].X = points[1].X;
1414 points[1].X = temp;
1417 if (points[0].Y > points[1].Y)
1419 REAL temp;
1420 temp = points[0].Y;
1421 points[0].Y = points[1].Y;
1422 points[1].Y = temp;
1425 element->elementdata.rect.X = points[0].X;
1426 element->elementdata.rect.Y = points[0].Y;
1427 element->elementdata.rect.Width = points[1].X - points[0].X;
1428 element->elementdata.rect.Height = points[1].Y - points[0].Y;
1429 return Ok;
1432 /* We can't rotate/shear a rectangle, so convert it to a path. */
1433 stat = GdipCreatePath(FillModeAlternate, &path);
1434 if (stat == Ok)
1436 stat = GdipAddPathRectangle(path,
1437 element->elementdata.rect.X, element->elementdata.rect.Y,
1438 element->elementdata.rect.Width, element->elementdata.rect.Height);
1440 if (stat == Ok)
1441 stat = GdipCreateRegionPath(path, &new_region);
1443 GdipDeletePath(path);
1446 if (stat == Ok)
1448 /* Steal the element from the created region. */
1449 memcpy(element, &new_region->node, sizeof(region_element));
1450 heap_free(new_region);
1452 else
1453 return stat;
1455 /* Fall-through to do the actual conversion. */
1456 case RegionDataPath:
1457 if (!element->elementdata.path->pathdata.Count)
1458 return Ok;
1460 stat = GdipTransformMatrixPoints(matrix,
1461 element->elementdata.path->pathdata.Points,
1462 element->elementdata.path->pathdata.Count);
1463 return stat;
1464 default:
1465 stat = transform_region_element(element->elementdata.combine.left, matrix);
1466 if (stat == Ok)
1467 stat = transform_region_element(element->elementdata.combine.right, matrix);
1468 return stat;
1472 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1474 TRACE("(%p, %p)\n", region, matrix);
1476 if (!region || !matrix)
1477 return InvalidParameter;
1479 return transform_region_element(&region->node, matrix);
1482 /* Translates GpRegion elements with specified offsets */
1483 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1485 INT i;
1487 switch(element->type)
1489 case RegionDataEmptyRect:
1490 case RegionDataInfiniteRect:
1491 return;
1492 case RegionDataRect:
1493 element->elementdata.rect.X += dx;
1494 element->elementdata.rect.Y += dy;
1495 return;
1496 case RegionDataPath:
1497 for(i = 0; i < element->elementdata.path->pathdata.Count; i++){
1498 element->elementdata.path->pathdata.Points[i].X += dx;
1499 element->elementdata.path->pathdata.Points[i].Y += dy;
1501 return;
1502 default:
1503 translate_region_element(element->elementdata.combine.left, dx, dy);
1504 translate_region_element(element->elementdata.combine.right, dx, dy);
1505 return;
1509 /*****************************************************************************
1510 * GdipTranslateRegion [GDIPLUS.@]
1512 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1514 TRACE("(%p, %f, %f)\n", region, dx, dy);
1516 if(!region)
1517 return InvalidParameter;
1519 translate_region_element(&region->node, dx, dy);
1521 return Ok;
1524 /*****************************************************************************
1525 * GdipTranslateRegionI [GDIPLUS.@]
1527 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1529 TRACE("(%p, %d, %d)\n", region, dx, dy);
1531 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1534 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1536 GpRegion *region_copy;
1537 GpStatus stat;
1538 HRGN hrgn;
1539 DWORD data_size;
1541 stat = GdipCloneRegion(region, &region_copy);
1543 if (stat == Ok)
1545 stat = GdipTransformRegion(region_copy, matrix);
1547 if (stat == Ok)
1548 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1550 if (stat == Ok)
1552 if (hrgn)
1554 data_size = GetRegionData(hrgn, 0, NULL);
1556 *data = heap_alloc_zero(data_size);
1558 if (*data)
1559 GetRegionData(hrgn, data_size, *data);
1560 else
1561 stat = OutOfMemory;
1563 DeleteObject(hrgn);
1565 else
1567 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1569 *data = heap_alloc_zero(data_size);
1571 if (*data)
1573 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1574 (*data)->rdh.iType = RDH_RECTANGLES;
1575 (*data)->rdh.nCount = 1;
1576 (*data)->rdh.nRgnSize = sizeof(RECT);
1577 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1578 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1580 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1582 else
1583 stat = OutOfMemory;
1587 GdipDeleteRegion(region_copy);
1590 return stat;
1593 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1595 GpStatus stat;
1596 LPRGNDATA data;
1598 TRACE("(%p, %p, %p)\n", region, count, matrix);
1600 if (!region || !count || !matrix)
1601 return InvalidParameter;
1603 stat = get_region_scans_data(region, matrix, &data);
1605 if (stat == Ok)
1607 *count = data->rdh.nCount;
1608 heap_free(data);
1611 return stat;
1614 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1616 GpStatus stat;
1617 DWORD i;
1618 LPRGNDATA data;
1619 RECT *rects;
1621 if (!region || !count || !matrix)
1622 return InvalidParameter;
1624 stat = get_region_scans_data(region, matrix, &data);
1626 if (stat == Ok)
1628 *count = data->rdh.nCount;
1629 rects = (RECT*)data->Buffer;
1631 if (scans)
1633 for (i=0; i<data->rdh.nCount; i++)
1635 scans[i].X = rects[i].left;
1636 scans[i].Y = rects[i].top;
1637 scans[i].Width = rects[i].right - rects[i].left;
1638 scans[i].Height = rects[i].bottom - rects[i].top;
1642 heap_free(data);
1645 return Ok;
1648 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1650 GpStatus stat;
1651 DWORD i;
1652 LPRGNDATA data;
1653 RECT *rects;
1655 if (!region || !count || !matrix)
1656 return InvalidParameter;
1658 stat = get_region_scans_data(region, matrix, &data);
1660 if (stat == Ok)
1662 *count = data->rdh.nCount;
1663 rects = (RECT*)data->Buffer;
1665 if (scans)
1667 for (i=0; i<data->rdh.nCount; i++)
1669 scans[i].X = rects[i].left;
1670 scans[i].Y = rects[i].top;
1671 scans[i].Width = rects[i].right - rects[i].left;
1672 scans[i].Height = rects[i].bottom - rects[i].top;
1676 heap_free(data);
1679 return Ok;