d3d11: Report D3D11_FORMAT_SUPPORT_TYPED_UNORDERED_ACCESS_VIEW in CheckFormatSupport().
[wine.git] / dlls / gdiplus / region.c
blobdcc40002836f74519af6a2b37c271355acae5348
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 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 heap_free(reg2copy);
366 return Ok;
369 left = heap_alloc_zero(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 heap_free(left);
378 return OutOfMemory;
381 fuse_region(region1, left, right, mode);
382 region1->num_children += region2->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 = heap_alloc_zero(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 GpStatus stat;
430 TRACE("%p, %p\n", path, region);
432 if (!(path && region))
433 return InvalidParameter;
435 *region = heap_alloc_zero(sizeof(GpRegion));
436 if(!*region)
437 return OutOfMemory;
438 stat = init_region(*region, RegionDataPath);
439 if (stat != Ok)
441 GdipDeleteRegion(*region);
442 return stat;
444 element = &(*region)->node;
446 stat = GdipClonePath(path, &element->elementdata.path);
447 if (stat != Ok)
449 GdipDeleteRegion(*region);
450 return stat;
453 return Ok;
456 /*****************************************************************************
457 * GdipCreateRegionRect [GDIPLUS.@]
459 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
460 GpRegion **region)
462 GpStatus stat;
464 TRACE("%s, %p\n", debugstr_rectf(rect), region);
466 if (!(rect && region))
467 return InvalidParameter;
469 *region = heap_alloc_zero(sizeof(GpRegion));
470 stat = init_region(*region, RegionDataRect);
471 if(stat != Ok)
473 GdipDeleteRegion(*region);
474 return stat;
477 (*region)->node.elementdata.rect.X = rect->X;
478 (*region)->node.elementdata.rect.Y = rect->Y;
479 (*region)->node.elementdata.rect.Width = rect->Width;
480 (*region)->node.elementdata.rect.Height = rect->Height;
482 return Ok;
485 /*****************************************************************************
486 * GdipCreateRegionRectI [GDIPLUS.@]
488 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
489 GpRegion **region)
491 GpRectF rectf;
493 TRACE("%p, %p\n", rect, region);
495 rectf.X = (REAL)rect->X;
496 rectf.Y = (REAL)rect->Y;
497 rectf.Width = (REAL)rect->Width;
498 rectf.Height = (REAL)rect->Height;
500 return GdipCreateRegionRect(&rectf, region);
503 /******************************************************************************
504 * GdipCreateRegionHrgn [GDIPLUS.@]
506 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
508 DWORD size;
509 LPRGNDATA buf;
510 LPRECT rect;
511 GpStatus stat;
512 GpPath* path;
513 GpRegion* local;
514 DWORD i;
516 TRACE("(%p, %p)\n", hrgn, region);
518 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
519 return InvalidParameter;
521 buf = heap_alloc_zero(size);
522 if(!buf)
523 return OutOfMemory;
525 if(!GetRegionData(hrgn, size, buf)){
526 heap_free(buf);
527 return GenericError;
530 if(buf->rdh.nCount == 0){
531 if((stat = GdipCreateRegion(&local)) != Ok){
532 heap_free(buf);
533 return stat;
535 if((stat = GdipSetEmpty(local)) != Ok){
536 heap_free(buf);
537 GdipDeleteRegion(local);
538 return stat;
540 *region = local;
541 heap_free(buf);
542 return Ok;
545 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
546 heap_free(buf);
547 return stat;
550 rect = (LPRECT)buf->Buffer;
551 for(i = 0; i < buf->rdh.nCount; i++){
552 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
553 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
554 heap_free(buf);
555 GdipDeletePath(path);
556 return stat;
558 rect++;
561 stat = GdipCreateRegionPath(path, region);
563 heap_free(buf);
564 GdipDeletePath(path);
565 return stat;
568 /*****************************************************************************
569 * GdipDeleteRegion [GDIPLUS.@]
571 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
573 TRACE("%p\n", region);
575 if (!region)
576 return InvalidParameter;
578 delete_element(&region->node);
579 heap_free(region);
581 return Ok;
584 /*****************************************************************************
585 * GdipGetRegionBounds [GDIPLUS.@]
587 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
589 HRGN hrgn;
590 RECT r;
591 GpStatus status;
593 TRACE("(%p, %p, %p)\n", region, graphics, rect);
595 if(!region || !graphics || !rect)
596 return InvalidParameter;
598 /* Contrary to MSDN, native ignores the graphics transform. */
599 status = GdipGetRegionHRgn(region, NULL, &hrgn);
600 if(status != Ok)
601 return status;
603 /* infinite */
604 if(!hrgn){
605 rect->X = rect->Y = -(REAL)(1 << 22);
606 rect->Width = rect->Height = (REAL)(1 << 23);
607 TRACE("%p => infinite\n", region);
608 return Ok;
611 if(GetRgnBox(hrgn, &r)){
612 rect->X = r.left;
613 rect->Y = r.top;
614 rect->Width = r.right - r.left;
615 rect->Height = r.bottom - r.top;
616 TRACE("%p => %s\n", region, debugstr_rectf(rect));
618 else
619 status = GenericError;
621 DeleteObject(hrgn);
623 return status;
626 /*****************************************************************************
627 * GdipGetRegionBoundsI [GDIPLUS.@]
629 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
631 GpRectF rectf;
632 GpStatus status;
634 TRACE("(%p, %p, %p)\n", region, graphics, rect);
636 if(!rect)
637 return InvalidParameter;
639 status = GdipGetRegionBounds(region, graphics, &rectf);
640 if(status == Ok){
641 rect->X = gdip_round(rectf.X);
642 rect->Y = gdip_round(rectf.Y);
643 rect->Width = gdip_round(rectf.Width);
644 rect->Height = gdip_round(rectf.Height);
647 return status;
650 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
652 location[*offset] = write;
653 (*offset)++;
656 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
658 ((FLOAT*)location)[*offset] = write;
659 (*offset)++;
662 static void write_element(const region_element* element, DWORD *buffer,
663 INT* filled)
665 write_dword(buffer, filled, element->type);
666 switch (element->type)
668 case CombineModeReplace:
669 case CombineModeIntersect:
670 case CombineModeUnion:
671 case CombineModeXor:
672 case CombineModeExclude:
673 case CombineModeComplement:
674 write_element(element->elementdata.combine.left, buffer, filled);
675 write_element(element->elementdata.combine.right, buffer, filled);
676 break;
677 case RegionDataRect:
678 write_float(buffer, filled, element->elementdata.rect.X);
679 write_float(buffer, filled, element->elementdata.rect.Y);
680 write_float(buffer, filled, element->elementdata.rect.Width);
681 write_float(buffer, filled, element->elementdata.rect.Height);
682 break;
683 case RegionDataPath:
685 DWORD size = write_path_data(element->elementdata.path, buffer + *filled + 1);
686 write_dword(buffer, filled, size);
687 *filled += size / sizeof(DWORD);
688 break;
690 case RegionDataEmptyRect:
691 case RegionDataInfiniteRect:
692 break;
696 DWORD write_region_data(const GpRegion *region, void *data)
698 struct region_header *header = data;
699 INT filled = 0;
700 DWORD size;
702 size = sizeof(struct region_header) + get_element_size(&region->node);
703 if (!data) return size;
705 header->magic = VERSION_MAGIC2;
706 header->num_children = region->num_children;
707 filled += 2;
708 /* With few exceptions, everything written is DWORD aligned,
709 * so use that as our base */
710 write_element(&region->node, (DWORD*)data, &filled);
711 return size;
714 /*****************************************************************************
715 * GdipGetRegionData [GDIPLUS.@]
717 * Returns the header, followed by combining ops and region elements.
719 * PARAMS
720 * region [I] region to retrieve from
721 * buffer [O] buffer to hold the resulting data
722 * size [I] size of the buffer
723 * needed [O] (optional) how much data was written
725 * RETURNS
726 * SUCCESS: Ok
727 * FAILURE: InvalidParameter
729 * NOTES
730 * The header contains the size, a checksum, a version string, and the number
731 * of children. The size does not count itself or the checksum.
732 * Version is always something like 0xdbc01001 or 0xdbc01002
734 * An element is a RECT, or PATH; Combining ops are stored as their
735 * CombineMode value. Special regions (infinite, empty) emit just their
736 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
737 * their code followed by a second header for the path followed by the actual
738 * path data. Followed by the flags for each point. The pathheader contains
739 * the size of the data to follow, a version number again, followed by a count
740 * of how many points, and any special flags which may apply. 0x4000 means it's
741 * a path of shorts instead of FLOAT.
743 * Combining Ops are stored in reverse order from when they were constructed;
744 * the output is a tree where the left side combining area is always taken
745 * first.
747 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
748 UINT *needed)
750 struct region_data_header *region_data_header;
751 UINT required;
753 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
755 if (!region || !buffer || !size)
756 return InvalidParameter;
758 required = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
759 if (size < required)
761 if (needed) *needed = size;
762 return InsufficientBuffer;
765 region_data_header = (struct region_data_header *)buffer;
766 region_data_header->size = write_region_data(region, &region_data_header->header);
767 region_data_header->checksum = 0;
769 if (needed)
770 *needed = required;
772 return Ok;
775 static GpStatus read_element(struct memory_buffer *mbuf, GpRegion *region, region_element *node, INT *count)
777 GpStatus status;
778 const DWORD *type;
780 type = buffer_read(mbuf, sizeof(*type));
781 if (!type) return Ok;
783 TRACE("type %#x\n", *type);
785 node->type = *type;
787 switch (node->type)
789 case CombineModeReplace:
790 case CombineModeIntersect:
791 case CombineModeUnion:
792 case CombineModeXor:
793 case CombineModeExclude:
794 case CombineModeComplement:
796 region_element *left, *right;
798 left = heap_alloc_zero(sizeof(region_element));
799 if (!left) return OutOfMemory;
800 right = heap_alloc_zero(sizeof(region_element));
801 if (!right)
803 heap_free(left);
804 return OutOfMemory;
807 status = read_element(mbuf, region, left, count);
808 if (status == Ok)
810 status = read_element(mbuf, region, right, count);
811 if (status == Ok)
813 node->elementdata.combine.left = left;
814 node->elementdata.combine.right = right;
815 region->num_children += 2;
816 return Ok;
820 heap_free(left);
821 heap_free(right);
822 return status;
825 case RegionDataRect:
827 const GpRectF *rc;
829 rc = buffer_read(mbuf, sizeof(*rc));
830 if (!rc)
832 ERR("failed to read rect data\n");
833 return InvalidParameter;
836 node->elementdata.rect = *rc;
837 *count += 1;
838 return Ok;
841 case RegionDataPath:
843 GpPath *path;
844 const struct path_header *path_header;
845 const BYTE *types;
847 path_header = buffer_read(mbuf, sizeof(*path_header));
848 if (!path_header)
850 ERR("failed to read path header\n");
851 return InvalidParameter;
853 if (!VALID_MAGIC(path_header->magic))
855 ERR("invalid path header magic %#x\n", path_header->magic);
856 return InvalidParameter;
859 /* Windows always fails to create an empty path in a region */
860 if (!path_header->count)
862 TRACE("refusing to create an empty path in a region\n");
863 return GenericError;
866 status = GdipCreatePath(FillModeAlternate, &path);
867 if (status) return status;
869 node->elementdata.path = path;
871 if (!lengthen_path(path, path_header->count))
872 return OutOfMemory;
874 path->pathdata.Count = path_header->count;
876 if (path_header->flags & ~FLAGS_INTPATH)
877 FIXME("unhandled path flags %#x\n", path_header->flags);
879 if (path_header->flags & FLAGS_INTPATH)
881 const packed_point *pt;
882 DWORD i;
884 pt = buffer_read(mbuf, sizeof(*pt) * path_header->count);
885 if (!pt)
887 ERR("failed to read packed %u path points\n", path_header->count);
888 return InvalidParameter;
891 for (i = 0; i < path_header->count; i++)
893 path->pathdata.Points[i].X = (REAL)pt[i].X;
894 path->pathdata.Points[i].Y = (REAL)pt[i].Y;
897 else
899 const GpPointF *ptf;
901 ptf = buffer_read(mbuf, sizeof(*ptf) * path_header->count);
902 if (!ptf)
904 ERR("failed to read %u path points\n", path_header->count);
905 return InvalidParameter;
907 memcpy(path->pathdata.Points, ptf, sizeof(*ptf) * path_header->count);
910 types = buffer_read(mbuf, path_header->count);
911 if (!types)
913 ERR("failed to read %u path types\n", path_header->count);
914 return InvalidParameter;
916 memcpy(path->pathdata.Types, types, path_header->count);
917 if (path_header->count & 3)
919 if (!buffer_read(mbuf, 4 - (path_header->count & 3)))
921 ERR("failed to read rounding %u bytes\n", 4 - (path_header->count & 3));
922 return InvalidParameter;
926 *count += 1;
927 return Ok;
930 case RegionDataEmptyRect:
931 case RegionDataInfiniteRect:
932 *count += 1;
933 return Ok;
935 default:
936 FIXME("element type %#x is not supported\n", *type);
937 break;
940 return InvalidParameter;
943 /*****************************************************************************
944 * GdipCreateRegionRgnData [GDIPLUS.@]
946 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
948 const struct region_data_header *region_data_header;
949 struct memory_buffer mbuf;
950 GpStatus status;
951 INT count;
953 TRACE("(%p, %d, %p)\n", data, size, region);
955 if (!data || !size)
956 return InvalidParameter;
958 init_memory_buffer(&mbuf, data, size);
960 region_data_header = buffer_read(&mbuf, sizeof(*region_data_header));
961 if (!region_data_header || !VALID_MAGIC(region_data_header->header.magic))
962 return InvalidParameter;
964 status = GdipCreateRegion(region);
965 if (status != Ok)
966 return status;
968 count = 0;
969 status = read_element(&mbuf, *region, &(*region)->node, &count);
970 if (status == Ok && !count)
971 status = InvalidParameter;
973 if (status != Ok)
975 GdipDeleteRegion(*region);
976 *region = NULL;
979 return status;
982 /*****************************************************************************
983 * GdipGetRegionDataSize [GDIPLUS.@]
985 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
987 TRACE("%p, %p\n", region, needed);
989 if (!(region && needed))
990 return InvalidParameter;
992 /* header.size doesn't count header.size and header.checksum */
993 *needed = FIELD_OFFSET(struct region_data_header, header) + write_region_data(region, NULL);
995 return Ok;
998 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
1000 HDC new_hdc=NULL;
1001 GpGraphics *new_graphics=NULL;
1002 GpStatus stat;
1003 INT save_state;
1005 if (!path->pathdata.Count) /* PathToRegion doesn't support empty paths */
1007 *hrgn = CreateRectRgn( 0, 0, 0, 0 );
1008 return *hrgn ? Ok : OutOfMemory;
1011 if (!graphics)
1013 new_hdc = CreateCompatibleDC(0);
1014 if (!new_hdc)
1015 return OutOfMemory;
1017 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
1018 graphics = new_graphics;
1019 if (stat != Ok)
1021 DeleteDC(new_hdc);
1022 return stat;
1025 else if (!graphics->hdc)
1027 graphics->hdc = new_hdc = CreateCompatibleDC(0);
1028 if (!new_hdc)
1029 return OutOfMemory;
1032 save_state = SaveDC(graphics->hdc);
1033 EndPath(graphics->hdc);
1035 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1036 : WINDING));
1038 gdi_transform_acquire(graphics);
1040 stat = trace_path(graphics, path);
1041 if (stat == Ok)
1043 *hrgn = PathToRegion(graphics->hdc);
1044 stat = *hrgn ? Ok : OutOfMemory;
1047 gdi_transform_release(graphics);
1049 RestoreDC(graphics->hdc, save_state);
1050 if (new_hdc)
1052 DeleteDC(new_hdc);
1053 if (new_graphics)
1054 GdipDeleteGraphics(new_graphics);
1055 else
1056 graphics->hdc = NULL;
1059 return stat;
1062 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
1064 switch (element->type)
1066 case RegionDataInfiniteRect:
1067 *hrgn = NULL;
1068 return Ok;
1069 case RegionDataEmptyRect:
1070 *hrgn = CreateRectRgn(0, 0, 0, 0);
1071 return *hrgn ? Ok : OutOfMemory;
1072 case RegionDataPath:
1073 return get_path_hrgn(element->elementdata.path, graphics, hrgn);
1074 case RegionDataRect:
1076 GpPath* path;
1077 GpStatus stat;
1078 GpRectF* rc = &element->elementdata.rect;
1080 stat = GdipCreatePath(FillModeAlternate, &path);
1081 if (stat != Ok)
1082 return stat;
1083 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
1085 if (stat == Ok)
1086 stat = get_path_hrgn(path, graphics, hrgn);
1088 GdipDeletePath(path);
1090 return stat;
1092 case CombineModeIntersect:
1093 case CombineModeUnion:
1094 case CombineModeXor:
1095 case CombineModeExclude:
1096 case CombineModeComplement:
1098 HRGN left, right;
1099 GpStatus stat;
1100 int ret;
1102 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
1103 if (stat != Ok)
1105 *hrgn = NULL;
1106 return stat;
1109 if (left == NULL)
1111 /* existing region is infinite */
1112 switch (element->type)
1114 case CombineModeIntersect:
1115 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
1116 case CombineModeXor: case CombineModeExclude:
1117 left = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1118 break;
1119 case CombineModeUnion: case CombineModeComplement:
1120 *hrgn = NULL;
1121 return Ok;
1125 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1126 if (stat != Ok)
1128 DeleteObject(left);
1129 *hrgn = NULL;
1130 return stat;
1133 if (right == NULL)
1135 /* new region is infinite */
1136 switch (element->type)
1138 case CombineModeIntersect:
1139 *hrgn = left;
1140 return Ok;
1141 case CombineModeXor: case CombineModeComplement:
1142 right = CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1143 break;
1144 case CombineModeUnion: case CombineModeExclude:
1145 DeleteObject(left);
1146 *hrgn = NULL;
1147 return Ok;
1151 switch (element->type)
1153 case CombineModeIntersect:
1154 ret = CombineRgn(left, left, right, RGN_AND);
1155 break;
1156 case CombineModeUnion:
1157 ret = CombineRgn(left, left, right, RGN_OR);
1158 break;
1159 case CombineModeXor:
1160 ret = CombineRgn(left, left, right, RGN_XOR);
1161 break;
1162 case CombineModeExclude:
1163 ret = CombineRgn(left, left, right, RGN_DIFF);
1164 break;
1165 case CombineModeComplement:
1166 ret = CombineRgn(left, right, left, RGN_DIFF);
1167 break;
1168 default:
1169 ret = ERROR;
1172 DeleteObject(right);
1174 if (ret == ERROR)
1176 DeleteObject(left);
1177 *hrgn = NULL;
1178 return GenericError;
1181 *hrgn = left;
1182 return Ok;
1184 default:
1185 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1186 *hrgn = NULL;
1187 return NotImplemented;
1191 /*****************************************************************************
1192 * GdipGetRegionHRgn [GDIPLUS.@]
1194 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1196 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1198 if (!region || !hrgn)
1199 return InvalidParameter;
1201 return get_region_hrgn(&region->node, graphics, hrgn);
1204 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1206 GpStatus status;
1207 GpRectF rect;
1209 TRACE("(%p, %p, %p)\n", region, graphics, res);
1211 if(!region || !graphics || !res)
1212 return InvalidParameter;
1214 status = GdipGetRegionBounds(region, graphics, &rect);
1215 if (status != Ok) return status;
1217 *res = rect.Width == 0.0 && rect.Height == 0.0;
1218 TRACE("=> %d\n", *res);
1220 return Ok;
1223 /*****************************************************************************
1224 * GdipIsEqualRegion [GDIPLUS.@]
1226 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1227 BOOL *res)
1229 HRGN hrgn1, hrgn2;
1230 GpStatus stat;
1232 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1234 if(!region || !region2 || !graphics || !res)
1235 return InvalidParameter;
1237 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1238 if(stat != Ok)
1239 return stat;
1240 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1241 if(stat != Ok){
1242 DeleteObject(hrgn1);
1243 return stat;
1246 *res = EqualRgn(hrgn1, hrgn2);
1248 /* one of GpRegions is infinite */
1249 if(*res == ERROR)
1250 *res = (!hrgn1 && !hrgn2);
1252 DeleteObject(hrgn1);
1253 DeleteObject(hrgn2);
1255 return Ok;
1258 /*****************************************************************************
1259 * GdipIsInfiniteRegion [GDIPLUS.@]
1261 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1263 /* I think graphics is ignored here */
1264 TRACE("(%p, %p, %p)\n", region, graphics, res);
1266 if(!region || !graphics || !res)
1267 return InvalidParameter;
1269 *res = (region->node.type == RegionDataInfiniteRect);
1271 return Ok;
1274 /*****************************************************************************
1275 * GdipIsVisibleRegionRect [GDIPLUS.@]
1277 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1279 HRGN hrgn;
1280 GpStatus stat;
1281 RECT rect;
1283 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1285 if(!region || !res)
1286 return InvalidParameter;
1288 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1289 return stat;
1291 /* infinite */
1292 if(!hrgn){
1293 *res = TRUE;
1294 return Ok;
1297 SetRect(&rect, ceilr(x), ceilr(y), ceilr(x + w), ceilr(y + h));
1298 *res = RectInRegion(hrgn, &rect);
1300 DeleteObject(hrgn);
1302 return Ok;
1305 /*****************************************************************************
1306 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1308 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1310 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1311 if(!region || !res)
1312 return InvalidParameter;
1314 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1317 /*****************************************************************************
1318 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1320 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1322 HRGN hrgn;
1323 GpStatus stat;
1325 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1327 if(!region || !res)
1328 return InvalidParameter;
1330 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1331 return stat;
1333 /* infinite */
1334 if(!hrgn){
1335 *res = TRUE;
1336 return Ok;
1339 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1341 DeleteObject(hrgn);
1343 return Ok;
1346 /*****************************************************************************
1347 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1349 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1351 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1353 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1356 /*****************************************************************************
1357 * GdipSetEmpty [GDIPLUS.@]
1359 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1361 GpStatus stat;
1363 TRACE("%p\n", region);
1365 if (!region)
1366 return InvalidParameter;
1368 delete_element(&region->node);
1369 stat = init_region(region, RegionDataEmptyRect);
1371 return stat;
1374 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1376 GpStatus stat;
1378 TRACE("%p\n", region);
1380 if (!region)
1381 return InvalidParameter;
1383 delete_element(&region->node);
1384 stat = init_region(region, RegionDataInfiniteRect);
1386 return stat;
1389 /* Transforms GpRegion elements with given matrix */
1390 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1392 GpStatus stat;
1394 switch(element->type)
1396 case RegionDataEmptyRect:
1397 case RegionDataInfiniteRect:
1398 return Ok;
1399 case RegionDataRect:
1401 GpRegion *new_region;
1402 GpPath *path;
1404 if (matrix->matrix[1] == 0.0 && matrix->matrix[2] == 0.0)
1406 GpPointF points[2];
1408 points[0].X = element->elementdata.rect.X;
1409 points[0].Y = element->elementdata.rect.Y;
1410 points[1].X = element->elementdata.rect.X + element->elementdata.rect.Width;
1411 points[1].Y = element->elementdata.rect.Y + element->elementdata.rect.Height;
1413 stat = GdipTransformMatrixPoints(matrix, points, 2);
1414 if (stat != Ok)
1415 return stat;
1417 if (points[0].X > points[1].X)
1419 REAL temp;
1420 temp = points[0].X;
1421 points[0].X = points[1].X;
1422 points[1].X = temp;
1425 if (points[0].Y > points[1].Y)
1427 REAL temp;
1428 temp = points[0].Y;
1429 points[0].Y = points[1].Y;
1430 points[1].Y = temp;
1433 element->elementdata.rect.X = points[0].X;
1434 element->elementdata.rect.Y = points[0].Y;
1435 element->elementdata.rect.Width = points[1].X - points[0].X;
1436 element->elementdata.rect.Height = points[1].Y - points[0].Y;
1437 return Ok;
1440 /* We can't rotate/shear a rectangle, so convert it to a path. */
1441 stat = GdipCreatePath(FillModeAlternate, &path);
1442 if (stat == Ok)
1444 stat = GdipAddPathRectangle(path,
1445 element->elementdata.rect.X, element->elementdata.rect.Y,
1446 element->elementdata.rect.Width, element->elementdata.rect.Height);
1448 if (stat == Ok)
1449 stat = GdipCreateRegionPath(path, &new_region);
1451 GdipDeletePath(path);
1454 if (stat == Ok)
1456 /* Steal the element from the created region. */
1457 memcpy(element, &new_region->node, sizeof(region_element));
1458 heap_free(new_region);
1460 else
1461 return stat;
1463 /* Fall-through to do the actual conversion. */
1464 case RegionDataPath:
1465 if (!element->elementdata.path->pathdata.Count)
1466 return Ok;
1468 stat = GdipTransformMatrixPoints(matrix,
1469 element->elementdata.path->pathdata.Points,
1470 element->elementdata.path->pathdata.Count);
1471 return stat;
1472 default:
1473 stat = transform_region_element(element->elementdata.combine.left, matrix);
1474 if (stat == Ok)
1475 stat = transform_region_element(element->elementdata.combine.right, matrix);
1476 return stat;
1480 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1482 TRACE("(%p, %p)\n", region, matrix);
1484 if (!region || !matrix)
1485 return InvalidParameter;
1487 return transform_region_element(&region->node, matrix);
1490 /* Translates GpRegion elements with specified offsets */
1491 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1493 INT i;
1495 switch(element->type)
1497 case RegionDataEmptyRect:
1498 case RegionDataInfiniteRect:
1499 return;
1500 case RegionDataRect:
1501 element->elementdata.rect.X += dx;
1502 element->elementdata.rect.Y += dy;
1503 return;
1504 case RegionDataPath:
1505 for(i = 0; i < element->elementdata.path->pathdata.Count; i++){
1506 element->elementdata.path->pathdata.Points[i].X += dx;
1507 element->elementdata.path->pathdata.Points[i].Y += dy;
1509 return;
1510 default:
1511 translate_region_element(element->elementdata.combine.left, dx, dy);
1512 translate_region_element(element->elementdata.combine.right, dx, dy);
1513 return;
1517 /*****************************************************************************
1518 * GdipTranslateRegion [GDIPLUS.@]
1520 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1522 TRACE("(%p, %f, %f)\n", region, dx, dy);
1524 if(!region)
1525 return InvalidParameter;
1527 translate_region_element(&region->node, dx, dy);
1529 return Ok;
1532 /*****************************************************************************
1533 * GdipTranslateRegionI [GDIPLUS.@]
1535 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1537 TRACE("(%p, %d, %d)\n", region, dx, dy);
1539 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1542 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1544 GpRegion *region_copy;
1545 GpStatus stat;
1546 HRGN hrgn;
1547 DWORD data_size;
1549 stat = GdipCloneRegion(region, &region_copy);
1551 if (stat == Ok)
1553 stat = GdipTransformRegion(region_copy, matrix);
1555 if (stat == Ok)
1556 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1558 if (stat == Ok)
1560 if (hrgn)
1562 data_size = GetRegionData(hrgn, 0, NULL);
1564 *data = heap_alloc_zero(data_size);
1566 if (*data)
1567 GetRegionData(hrgn, data_size, *data);
1568 else
1569 stat = OutOfMemory;
1571 DeleteObject(hrgn);
1573 else
1575 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1577 *data = heap_alloc_zero(data_size);
1579 if (*data)
1581 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1582 (*data)->rdh.iType = RDH_RECTANGLES;
1583 (*data)->rdh.nCount = 1;
1584 (*data)->rdh.nRgnSize = sizeof(RECT);
1585 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1586 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1588 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1590 else
1591 stat = OutOfMemory;
1595 GdipDeleteRegion(region_copy);
1598 return stat;
1601 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1603 GpStatus stat;
1604 LPRGNDATA data;
1606 TRACE("(%p, %p, %p)\n", region, count, matrix);
1608 if (!region || !count || !matrix)
1609 return InvalidParameter;
1611 stat = get_region_scans_data(region, matrix, &data);
1613 if (stat == Ok)
1615 *count = data->rdh.nCount;
1616 heap_free(data);
1619 return stat;
1622 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1624 GpStatus stat;
1625 DWORD i;
1626 LPRGNDATA data;
1627 RECT *rects;
1629 if (!region || !count || !matrix)
1630 return InvalidParameter;
1632 stat = get_region_scans_data(region, matrix, &data);
1634 if (stat == Ok)
1636 *count = data->rdh.nCount;
1637 rects = (RECT*)data->Buffer;
1639 if (scans)
1641 for (i=0; i<data->rdh.nCount; i++)
1643 scans[i].X = rects[i].left;
1644 scans[i].Y = rects[i].top;
1645 scans[i].Width = rects[i].right - rects[i].left;
1646 scans[i].Height = rects[i].bottom - rects[i].top;
1650 heap_free(data);
1653 return Ok;
1656 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1658 GpStatus stat;
1659 DWORD i;
1660 LPRGNDATA data;
1661 RECT *rects;
1663 if (!region || !count || !matrix)
1664 return InvalidParameter;
1666 stat = get_region_scans_data(region, matrix, &data);
1668 if (stat == Ok)
1670 *count = data->rdh.nCount;
1671 rects = (RECT*)data->Buffer;
1673 if (scans)
1675 for (i=0; i<data->rdh.nCount; i++)
1677 scans[i].X = rects[i].left;
1678 scans[i].Y = rects[i].top;
1679 scans[i].Width = rects[i].right - rects[i].left;
1680 scans[i].Height = rects[i].bottom - rects[i].top;
1684 heap_free(data);
1687 return Ok;