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 "gdiplus_private.h"
22 /**********************************************************
24 * Data returned by GdipGetRegionData looks something like this:
26 * struct region_data_header
28 * DWORD size; size in bytes of the data - 8.
29 * DWORD magic1; probably a checksum.
30 * DWORD magic2; always seems to be 0xdbc01001 - version?
31 * DWORD num_ops; number of combining ops * 2
34 * Then follows a sequence of combining ops and region elements.
36 * A region element is either a RECTF or some path data.
38 * Combining ops are just stored as their CombineMode value.
40 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
41 * stored as 0x10000002 (with no following RECTF) and an infinite rect
42 * is stored as 0x10000003 (again with no following RECTF).
44 * Path data is preceded by the DWORD 0x10000001. Then follows a
45 * DWORD size and then size bytes of data.
47 * The combining ops are stored in the reverse order to the region
48 * elements and in the reverse order to which the region was
51 * When two or more complex regions (ie those with more than one
52 * element) are combined, the combining op for the two regions comes
53 * first, then the combining ops for the region elements in region 1,
54 * followed by the region elements for region 1, then follows the
55 * combining ops for region 2 and finally region 2's region elements.
56 * Presumably you're supposed to use the 0x1000000x header to find the
57 * end of the op list (the count of the elements in each region is not
60 * When a simple region (1 element) is combined, it's treated as if a
61 * single rect/path is being combined.
65 #define FLAGS_NOFLAGS 0x0
66 #define FLAGS_INTPATH 0x4000
90 /* Header size as far as header->size is concerned. This doesn't include
91 * header->size or header->checksum
93 static const INT sizeheader_size
= sizeof(DWORD
) * 2;
95 typedef struct packed_point
101 /* Test to see if the path could be stored as an array of shorts */
102 static BOOL
is_integer_path(const GpPath
*path
)
106 if (!path
->pathdata
.Count
) return FALSE
;
108 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
111 x
= gdip_round(path
->pathdata
.Points
[i
].X
);
112 y
= gdip_round(path
->pathdata
.Points
[i
].Y
);
113 if (path
->pathdata
.Points
[i
].X
!= (REAL
)x
|| path
->pathdata
.Points
[i
].Y
!= (REAL
)y
)
119 /* Everything is measured in DWORDS; round up if there's a remainder */
120 static inline INT
get_pathtypes_size(const GpPath
* path
)
122 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
124 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
127 return needed
* sizeof(DWORD
);
130 static inline INT
get_element_size(const region_element
* element
)
132 INT needed
= sizeof(DWORD
); /* DWORD for the type */
133 switch(element
->type
)
136 return needed
+ sizeof(GpRect
);
139 const GpPath
*path
= element
->elementdata
.path
;
140 DWORD flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
141 /* 3 for headers, once again size doesn't count itself */
142 needed
+= sizeof(DWORD
) * 3;
143 if (flags
& FLAGS_INTPATH
)
144 needed
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
146 needed
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
148 needed
+= get_pathtypes_size(path
);
149 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
152 case RegionDataEmptyRect
:
153 case RegionDataInfiniteRect
:
156 needed
+= get_element_size(element
->elementdata
.combine
.left
);
157 needed
+= get_element_size(element
->elementdata
.combine
.right
);
164 /* Does not check parameters, caller must do that */
165 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
167 region
->node
.type
= type
;
168 region
->num_children
= 0;
173 static inline GpStatus
clone_element(const region_element
* element
,
174 region_element
** element2
)
178 /* root node is allocated with GpRegion */
180 *element2
= GdipAlloc(sizeof(region_element
));
185 (*element2
)->type
= element
->type
;
187 switch (element
->type
)
190 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
192 case RegionDataEmptyRect
:
193 case RegionDataInfiniteRect
:
196 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
197 if (stat
== Ok
) return Ok
;
200 (*element2
)->elementdata
.combine
.left
= NULL
;
201 (*element2
)->elementdata
.combine
.right
= NULL
;
203 stat
= clone_element(element
->elementdata
.combine
.left
,
204 &(*element2
)->elementdata
.combine
.left
);
207 stat
= clone_element(element
->elementdata
.combine
.right
,
208 &(*element2
)->elementdata
.combine
.right
);
209 if (stat
== Ok
) return Ok
;
214 delete_element(*element2
);
219 /* Common code for CombineRegion*
220 * All the caller has to do is get its format into an element
222 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
223 region_element
* right
, const CombineMode mode
)
225 region
->node
.type
= mode
;
226 region
->node
.elementdata
.combine
.left
= left
;
227 region
->node
.elementdata
.combine
.right
= right
;
228 region
->num_children
+= 2;
231 /*****************************************************************************
232 * GdipCloneRegion [GDIPLUS.@]
234 * Creates a deep copy of the region
237 * region [I] source region
238 * clone [O] resulting clone
242 * FAILURE: InvalidParameter or OutOfMemory
244 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
246 region_element
*element
;
248 TRACE("%p %p\n", region
, clone
);
250 if (!(region
&& clone
))
251 return InvalidParameter
;
253 *clone
= GdipAlloc(sizeof(GpRegion
));
256 element
= &(*clone
)->node
;
258 (*clone
)->num_children
= region
->num_children
;
259 return clone_element(®ion
->node
, &element
);
262 /*****************************************************************************
263 * GdipCombineRegionPath [GDIPLUS.@]
265 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
267 GpRegion
*path_region
;
268 region_element
*left
, *right
= NULL
;
271 TRACE("%p %p %d\n", region
, path
, mode
);
273 if (!(region
&& path
))
274 return InvalidParameter
;
276 stat
= GdipCreateRegionPath(path
, &path_region
);
280 /* simply replace region data */
281 if(mode
== CombineModeReplace
){
282 delete_element(®ion
->node
);
283 memcpy(region
, path_region
, sizeof(GpRegion
));
284 GdipFree(path_region
);
288 left
= GdipAlloc(sizeof(region_element
));
291 *left
= region
->node
;
292 stat
= clone_element(&path_region
->node
, &right
);
295 fuse_region(region
, left
, right
, mode
);
296 GdipDeleteRegion(path_region
);
304 GdipDeleteRegion(path_region
);
308 /*****************************************************************************
309 * GdipCombineRegionRect [GDIPLUS.@]
311 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
312 GDIPCONST GpRectF
*rect
, CombineMode mode
)
314 GpRegion
*rect_region
;
315 region_element
*left
, *right
= NULL
;
318 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
320 if (!(region
&& rect
))
321 return InvalidParameter
;
323 stat
= GdipCreateRegionRect(rect
, &rect_region
);
327 /* simply replace region data */
328 if(mode
== CombineModeReplace
){
329 delete_element(®ion
->node
);
330 memcpy(region
, rect_region
, sizeof(GpRegion
));
331 GdipFree(rect_region
);
335 left
= GdipAlloc(sizeof(region_element
));
338 memcpy(left
, ®ion
->node
, sizeof(region_element
));
339 stat
= clone_element(&rect_region
->node
, &right
);
342 fuse_region(region
, left
, right
, mode
);
343 GdipDeleteRegion(rect_region
);
351 GdipDeleteRegion(rect_region
);
355 /*****************************************************************************
356 * GdipCombineRegionRectI [GDIPLUS.@]
358 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
359 GDIPCONST GpRect
*rect
, CombineMode mode
)
363 TRACE("%p %p %d\n", region
, rect
, mode
);
366 return InvalidParameter
;
368 rectf
.X
= (REAL
)rect
->X
;
369 rectf
.Y
= (REAL
)rect
->Y
;
370 rectf
.Height
= (REAL
)rect
->Height
;
371 rectf
.Width
= (REAL
)rect
->Width
;
373 return GdipCombineRegionRect(region
, &rectf
, mode
);
376 /*****************************************************************************
377 * GdipCombineRegionRegion [GDIPLUS.@]
379 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
380 GpRegion
*region2
, CombineMode mode
)
382 region_element
*left
, *right
= NULL
;
386 TRACE("%p %p %d\n", region1
, region2
, mode
);
388 if(!(region1
&& region2
))
389 return InvalidParameter
;
391 /* simply replace region data */
392 if(mode
== CombineModeReplace
){
393 stat
= GdipCloneRegion(region2
, ®2copy
);
394 if(stat
!= Ok
) return stat
;
396 delete_element(®ion1
->node
);
397 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
402 left
= GdipAlloc(sizeof(region_element
));
406 *left
= region1
->node
;
407 stat
= clone_element(®ion2
->node
, &right
);
414 fuse_region(region1
, left
, right
, mode
);
415 region1
->num_children
+= region2
->num_children
;
420 /*****************************************************************************
421 * GdipCreateRegion [GDIPLUS.@]
423 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
425 TRACE("%p\n", region
);
428 return InvalidParameter
;
430 *region
= GdipAlloc(sizeof(GpRegion
));
434 TRACE("=> %p\n", *region
);
436 return init_region(*region
, RegionDataInfiniteRect
);
439 /*****************************************************************************
440 * GdipCreateRegionPath [GDIPLUS.@]
442 * Creates a GpRegion from a GpPath
445 * path [I] path to base the region on
446 * region [O] pointer to the newly allocated region
450 * FAILURE: InvalidParameter
453 * If a path has no floating point points, its points will be stored as shorts
456 * If a path is empty, it is considered to be an INTPATH
458 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
460 region_element
* element
;
463 TRACE("%p, %p\n", path
, region
);
465 if (!(path
&& region
))
466 return InvalidParameter
;
468 *region
= GdipAlloc(sizeof(GpRegion
));
471 stat
= init_region(*region
, RegionDataPath
);
474 GdipDeleteRegion(*region
);
477 element
= &(*region
)->node
;
479 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
482 GdipDeleteRegion(*region
);
489 /*****************************************************************************
490 * GdipCreateRegionRect [GDIPLUS.@]
492 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
497 TRACE("%p, %p\n", rect
, region
);
499 if (!(rect
&& region
))
500 return InvalidParameter
;
502 *region
= GdipAlloc(sizeof(GpRegion
));
503 stat
= init_region(*region
, RegionDataRect
);
506 GdipDeleteRegion(*region
);
510 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
511 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
512 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
513 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
518 /*****************************************************************************
519 * GdipCreateRegionRectI [GDIPLUS.@]
521 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
526 TRACE("%p, %p\n", rect
, region
);
528 rectf
.X
= (REAL
)rect
->X
;
529 rectf
.Y
= (REAL
)rect
->Y
;
530 rectf
.Width
= (REAL
)rect
->Width
;
531 rectf
.Height
= (REAL
)rect
->Height
;
533 return GdipCreateRegionRect(&rectf
, region
);
536 /******************************************************************************
537 * GdipCreateRegionHrgn [GDIPLUS.@]
539 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
549 TRACE("(%p, %p)\n", hrgn
, region
);
551 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
552 return InvalidParameter
;
554 buf
= GdipAlloc(size
);
558 if(!GetRegionData(hrgn
, size
, buf
)){
563 if(buf
->rdh
.nCount
== 0){
564 if((stat
= GdipCreateRegion(&local
)) != Ok
){
568 if((stat
= GdipSetEmpty(local
)) != Ok
){
570 GdipDeleteRegion(local
);
578 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
583 rect
= (LPRECT
)buf
->Buffer
;
584 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
585 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
586 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
588 GdipDeletePath(path
);
594 stat
= GdipCreateRegionPath(path
, region
);
597 GdipDeletePath(path
);
601 /*****************************************************************************
602 * GdipDeleteRegion [GDIPLUS.@]
604 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
606 TRACE("%p\n", region
);
609 return InvalidParameter
;
611 delete_element(®ion
->node
);
617 /*****************************************************************************
618 * GdipGetRegionBounds [GDIPLUS.@]
620 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
626 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
628 if(!region
|| !graphics
|| !rect
)
629 return InvalidParameter
;
631 /* Contrary to MSDN, native ignores the graphics transform. */
632 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
638 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
639 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
640 TRACE("%p => infinite\n", region
);
644 if(GetRgnBox(hrgn
, &r
)){
647 rect
->Width
= r
.right
- r
.left
;
648 rect
->Height
= r
.bottom
- r
.top
;
649 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
652 status
= GenericError
;
659 /*****************************************************************************
660 * GdipGetRegionBoundsI [GDIPLUS.@]
662 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
667 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
670 return InvalidParameter
;
672 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
674 rect
->X
= gdip_round(rectf
.X
);
675 rect
->Y
= gdip_round(rectf
.Y
);
676 rect
->Width
= gdip_round(rectf
.Width
);
677 rect
->Height
= gdip_round(rectf
.Height
);
683 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
685 location
[*offset
] = write
;
689 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
691 ((FLOAT
*)location
)[*offset
] = write
;
695 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
696 const GpPointF
* write
)
698 packed_point
*point
= (packed_point
*)(location
+ *offset
);
699 point
->X
= gdip_round(write
->X
);
700 point
->Y
= gdip_round(write
->Y
);
704 static inline void write_path_types(DWORD
* location
, INT
* offset
,
707 INT rounded_size
= get_pathtypes_size(path
);
709 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
711 /* The unwritten parts of the DWORD (if any) must be cleared */
712 if (rounded_size
- path
->pathdata
.Count
)
713 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
714 path
->pathdata
.Count
, rounded_size
- path
->pathdata
.Count
);
715 *offset
+= rounded_size
/ sizeof(DWORD
);
718 static void write_element(const region_element
* element
, DWORD
*buffer
,
721 write_dword(buffer
, filled
, element
->type
);
722 switch (element
->type
)
724 case CombineModeReplace
:
725 case CombineModeIntersect
:
726 case CombineModeUnion
:
728 case CombineModeExclude
:
729 case CombineModeComplement
:
730 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
731 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
734 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
735 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
736 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
737 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
742 const GpPath
* path
= element
->elementdata
.path
;
743 struct path_header
*pathheader
;
745 pathheader
= (struct path_header
*)(buffer
+ *filled
);
747 pathheader
->flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
748 /* 3 for headers, once again size doesn't count itself */
749 pathheader
->size
= sizeof(DWORD
) * 3;
750 if (pathheader
->flags
& FLAGS_INTPATH
)
751 pathheader
->size
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
753 pathheader
->size
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
754 pathheader
->size
+= get_pathtypes_size(path
);
755 pathheader
->magic
= VERSION_MAGIC
;
756 pathheader
->count
= path
->pathdata
.Count
;
760 switch (pathheader
->flags
& FLAGS_INTPATH
)
763 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
765 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
766 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
770 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
772 write_packed_point(buffer
, filled
,
773 &path
->pathdata
.Points
[i
]);
777 write_path_types(buffer
, filled
, path
);
780 case RegionDataEmptyRect
:
781 case RegionDataInfiniteRect
:
786 /*****************************************************************************
787 * GdipGetRegionData [GDIPLUS.@]
789 * Returns the header, followed by combining ops and region elements.
792 * region [I] region to retrieve from
793 * buffer [O] buffer to hold the resulting data
794 * size [I] size of the buffer
795 * needed [O] (optional) how much data was written
799 * FAILURE: InvalidParameter
802 * The header contains the size, a checksum, a version string, and the number
803 * of children. The size does not count itself or the checksum.
804 * Version is always something like 0xdbc01001 or 0xdbc01002
806 * An element is a RECT, or PATH; Combining ops are stored as their
807 * CombineMode value. Special regions (infinite, empty) emit just their
808 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
809 * their code followed by a second header for the path followed by the actual
810 * path data. Followed by the flags for each point. The pathheader contains
811 * the size of the data to follow, a version number again, followed by a count
812 * of how many points, and any special flags which may apply. 0x4000 means its
813 * a path of shorts instead of FLOAT.
815 * Combining Ops are stored in reverse order from when they were constructed;
816 * the output is a tree where the left side combining area is always taken
819 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
822 struct region_header
*region_header
;
827 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
829 if (!region
|| !buffer
|| !size
)
830 return InvalidParameter
;
832 status
= GdipGetRegionDataSize(region
, &required
);
833 if (status
!= Ok
) return status
;
836 if (needed
) *needed
= size
;
837 return InsufficientBuffer
;
840 region_header
= (struct region_header
*)buffer
;
841 region_header
->size
= sizeheader_size
+ get_element_size(®ion
->node
);
842 region_header
->checksum
= 0;
843 region_header
->magic
= VERSION_MAGIC
;
844 region_header
->num_children
= region
->num_children
;
846 /* With few exceptions, everything written is DWORD aligned,
847 * so use that as our base */
848 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
851 *needed
= filled
* sizeof(DWORD
);
856 static inline void init_memory_buffer(struct memory_buffer
*mbuf
, const BYTE
*buffer
, INT size
)
858 mbuf
->buffer
= buffer
;
863 static inline const void *buffer_read(struct memory_buffer
*mbuf
, INT size
)
865 if (mbuf
->size
- mbuf
->pos
>= size
)
867 const void *data
= mbuf
->buffer
+ mbuf
->pos
;
874 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
879 type
= buffer_read(mbuf
, sizeof(*type
));
880 if (!type
) return Ok
;
882 TRACE("type %#x\n", *type
);
888 case CombineModeReplace
:
889 case CombineModeIntersect
:
890 case CombineModeUnion
:
892 case CombineModeExclude
:
893 case CombineModeComplement
:
895 region_element
*left
, *right
;
897 left
= GdipAlloc(sizeof(region_element
));
898 if (!left
) return OutOfMemory
;
899 right
= GdipAlloc(sizeof(region_element
));
906 status
= read_element(mbuf
, region
, left
, count
);
909 status
= read_element(mbuf
, region
, right
, count
);
912 node
->elementdata
.combine
.left
= left
;
913 node
->elementdata
.combine
.right
= right
;
914 region
->num_children
+= 2;
928 rc
= buffer_read(mbuf
, sizeof(*rc
));
931 ERR("failed to read rect data\n");
932 return InvalidParameter
;
935 node
->elementdata
.rect
= *rc
;
943 const struct path_header
*path_header
;
946 path_header
= buffer_read(mbuf
, sizeof(*path_header
));
949 ERR("failed to read path header\n");
950 return InvalidParameter
;
952 if (path_header
->magic
!= VERSION_MAGIC
)
954 ERR("invalid path header magic %#x\n", path_header
->magic
);
955 return InvalidParameter
;
958 /* Windows always fails to create an empty path in a region */
959 if (!path_header
->count
)
961 TRACE("refusing to create an empty path in a region\n");
965 status
= GdipCreatePath(FillModeAlternate
, &path
);
966 if (status
) return status
;
968 node
->elementdata
.path
= path
;
970 if (!lengthen_path(path
, path_header
->count
))
973 path
->pathdata
.Count
= path_header
->count
;
975 if (path_header
->flags
& ~FLAGS_INTPATH
)
976 FIXME("unhandled path flags %#x\n", path_header
->flags
);
978 if (path_header
->flags
& FLAGS_INTPATH
)
980 const packed_point
*pt
;
983 pt
= buffer_read(mbuf
, sizeof(*pt
) * path_header
->count
);
986 ERR("failed to read packed %u path points\n", path_header
->count
);
987 return InvalidParameter
;
990 for (i
= 0; i
< path_header
->count
; i
++)
992 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
993 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
1000 ptf
= buffer_read(mbuf
, sizeof(*ptf
) * path_header
->count
);
1003 ERR("failed to read %u path points\n", path_header
->count
);
1004 return InvalidParameter
;
1006 memcpy(path
->pathdata
.Points
, ptf
, sizeof(*ptf
) * path_header
->count
);
1009 types
= buffer_read(mbuf
, path_header
->count
);
1012 ERR("failed to read %u path types\n", path_header
->count
);
1013 return InvalidParameter
;
1015 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
1016 if (path_header
->count
& 3)
1018 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
1020 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
1021 return InvalidParameter
;
1029 case RegionDataEmptyRect
:
1030 case RegionDataInfiniteRect
:
1035 FIXME("element type %#x is not supported\n", *type
);
1039 return InvalidParameter
;
1042 /*****************************************************************************
1043 * GdipCreateRegionRgnData [GDIPLUS.@]
1045 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
1047 const struct region_header
*region_header
;
1048 struct memory_buffer mbuf
;
1052 TRACE("(%p, %d, %p)\n", data
, size
, region
);
1055 return InvalidParameter
;
1057 init_memory_buffer(&mbuf
, data
, size
);
1059 region_header
= buffer_read(&mbuf
, sizeof(*region_header
));
1060 if (!region_header
|| (region_header
->magic
!= VERSION_MAGIC
&&
1061 region_header
->magic
!= VERSION_MAGIC2
))
1062 return InvalidParameter
;
1064 status
= GdipCreateRegion(region
);
1069 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
1070 if (status
== Ok
&& !count
)
1071 status
= InvalidParameter
;
1075 GdipDeleteRegion(*region
);
1082 /*****************************************************************************
1083 * GdipGetRegionDataSize [GDIPLUS.@]
1085 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
1087 TRACE("%p, %p\n", region
, needed
);
1089 if (!(region
&& needed
))
1090 return InvalidParameter
;
1092 /* header.size doesn't count header.size and header.checksum */
1093 *needed
= sizeof(DWORD
) * 2 + sizeheader_size
+ get_element_size(®ion
->node
);
1098 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
1101 GpGraphics
*new_graphics
=NULL
;
1107 new_hdc
= CreateCompatibleDC(0);
1111 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1112 graphics
= new_graphics
;
1119 else if (!graphics
->hdc
)
1121 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1126 save_state
= SaveDC(graphics
->hdc
);
1127 EndPath(graphics
->hdc
);
1129 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1132 stat
= trace_path(graphics
, path
);
1135 *hrgn
= PathToRegion(graphics
->hdc
);
1136 stat
= *hrgn
? Ok
: OutOfMemory
;
1139 RestoreDC(graphics
->hdc
, save_state
);
1144 GdipDeleteGraphics(new_graphics
);
1146 graphics
->hdc
= NULL
;
1152 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1154 switch (element
->type
)
1156 case RegionDataInfiniteRect
:
1159 case RegionDataEmptyRect
:
1160 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1161 return *hrgn
? Ok
: OutOfMemory
;
1162 case RegionDataPath
:
1163 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1164 case RegionDataRect
:
1168 GpRectF
* rc
= &element
->elementdata
.rect
;
1170 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1173 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1176 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1178 GdipDeletePath(path
);
1182 case CombineModeIntersect
:
1183 case CombineModeUnion
:
1184 case CombineModeXor
:
1185 case CombineModeExclude
:
1186 case CombineModeComplement
:
1192 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1201 /* existing region is infinite */
1202 switch (element
->type
)
1204 case CombineModeIntersect
:
1205 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1206 case CombineModeXor
: case CombineModeExclude
:
1207 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1209 case CombineModeUnion
: case CombineModeComplement
:
1215 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1225 /* new region is infinite */
1226 switch (element
->type
)
1228 case CombineModeIntersect
:
1231 case CombineModeXor
: case CombineModeComplement
:
1232 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1234 case CombineModeUnion
: case CombineModeExclude
:
1241 switch (element
->type
)
1243 case CombineModeIntersect
:
1244 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1246 case CombineModeUnion
:
1247 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1249 case CombineModeXor
:
1250 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1252 case CombineModeExclude
:
1253 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1255 case CombineModeComplement
:
1256 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1262 DeleteObject(right
);
1268 return GenericError
;
1275 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1277 return NotImplemented
;
1281 /*****************************************************************************
1282 * GdipGetRegionHRgn [GDIPLUS.@]
1284 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1286 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1288 if (!region
|| !hrgn
)
1289 return InvalidParameter
;
1291 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1294 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1299 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1301 if(!region
|| !graphics
|| !res
)
1302 return InvalidParameter
;
1304 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1305 if (status
!= Ok
) return status
;
1307 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1308 TRACE("=> %d\n", *res
);
1313 /*****************************************************************************
1314 * GdipIsEqualRegion [GDIPLUS.@]
1316 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1322 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1324 if(!region
|| !region2
|| !graphics
|| !res
)
1325 return InvalidParameter
;
1327 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1330 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1332 DeleteObject(hrgn1
);
1336 *res
= EqualRgn(hrgn1
, hrgn2
);
1338 /* one of GpRegions is infinite */
1340 *res
= (!hrgn1
&& !hrgn2
);
1342 DeleteObject(hrgn1
);
1343 DeleteObject(hrgn2
);
1348 /*****************************************************************************
1349 * GdipIsInfiniteRegion [GDIPLUS.@]
1351 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1353 /* I think graphics is ignored here */
1354 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1356 if(!region
|| !graphics
|| !res
)
1357 return InvalidParameter
;
1359 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1364 /*****************************************************************************
1365 * GdipIsVisibleRegionRect [GDIPLUS.@]
1367 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1373 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1376 return InvalidParameter
;
1378 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1387 rect
.left
= ceilr(x
);
1388 rect
.top
= ceilr(y
);
1389 rect
.right
= ceilr(x
+ w
);
1390 rect
.bottom
= ceilr(y
+ h
);
1392 *res
= RectInRegion(hrgn
, &rect
);
1399 /*****************************************************************************
1400 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1402 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1404 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1406 return InvalidParameter
;
1408 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1411 /*****************************************************************************
1412 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1414 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1419 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1422 return InvalidParameter
;
1424 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1433 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1440 /*****************************************************************************
1441 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1443 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1445 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1447 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1450 /*****************************************************************************
1451 * GdipSetEmpty [GDIPLUS.@]
1453 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1457 TRACE("%p\n", region
);
1460 return InvalidParameter
;
1462 delete_element(®ion
->node
);
1463 stat
= init_region(region
, RegionDataEmptyRect
);
1468 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1472 TRACE("%p\n", region
);
1475 return InvalidParameter
;
1477 delete_element(®ion
->node
);
1478 stat
= init_region(region
, RegionDataInfiniteRect
);
1483 /* Transforms GpRegion elements with given matrix */
1484 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1488 switch(element
->type
)
1490 case RegionDataEmptyRect
:
1491 case RegionDataInfiniteRect
:
1493 case RegionDataRect
:
1495 /* We can't transform a rectangle, so convert it to a path. */
1496 GpRegion
*new_region
;
1499 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1502 stat
= GdipAddPathRectangle(path
,
1503 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1504 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1507 stat
= GdipCreateRegionPath(path
, &new_region
);
1509 GdipDeletePath(path
);
1514 /* Steal the element from the created region. */
1515 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1516 GdipFree(new_region
);
1521 /* Fall-through to do the actual conversion. */
1522 case RegionDataPath
:
1523 if (!element
->elementdata
.path
->pathdata
.Count
)
1526 stat
= GdipTransformMatrixPoints(matrix
,
1527 element
->elementdata
.path
->pathdata
.Points
,
1528 element
->elementdata
.path
->pathdata
.Count
);
1531 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1533 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1538 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1540 TRACE("(%p, %p)\n", region
, matrix
);
1542 if (!region
|| !matrix
)
1543 return InvalidParameter
;
1545 return transform_region_element(®ion
->node
, matrix
);
1548 /* Translates GpRegion elements with specified offsets */
1549 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1553 switch(element
->type
)
1555 case RegionDataEmptyRect
:
1556 case RegionDataInfiniteRect
:
1558 case RegionDataRect
:
1559 element
->elementdata
.rect
.X
+= dx
;
1560 element
->elementdata
.rect
.Y
+= dy
;
1562 case RegionDataPath
:
1563 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1564 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1565 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1569 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1570 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1575 /*****************************************************************************
1576 * GdipTranslateRegion [GDIPLUS.@]
1578 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1580 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1583 return InvalidParameter
;
1585 translate_region_element(®ion
->node
, dx
, dy
);
1590 /*****************************************************************************
1591 * GdipTranslateRegionI [GDIPLUS.@]
1593 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1595 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1597 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1600 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1602 GpRegion
*region_copy
;
1607 stat
= GdipCloneRegion(region
, ®ion_copy
);
1611 stat
= GdipTransformRegion(region_copy
, matrix
);
1614 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1620 data_size
= GetRegionData(hrgn
, 0, NULL
);
1622 *data
= GdipAlloc(data_size
);
1625 GetRegionData(hrgn
, data_size
, *data
);
1633 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1635 *data
= GdipAlloc(data_size
);
1639 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1640 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1641 (*data
)->rdh
.nCount
= 1;
1642 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1643 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1644 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1646 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1653 GdipDeleteRegion(region_copy
);
1659 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1664 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1666 if (!region
|| !count
|| !matrix
)
1667 return InvalidParameter
;
1669 stat
= get_region_scans_data(region
, matrix
, &data
);
1673 *count
= data
->rdh
.nCount
;
1680 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1687 if (!region
|| !count
|| !matrix
)
1688 return InvalidParameter
;
1690 stat
= get_region_scans_data(region
, matrix
, &data
);
1694 *count
= data
->rdh
.nCount
;
1695 rects
= (RECT
*)data
->Buffer
;
1699 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1701 scans
[i
].X
= rects
[i
].left
;
1702 scans
[i
].Y
= rects
[i
].top
;
1703 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1704 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1714 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1721 if (!region
|| !count
|| !matrix
)
1722 return InvalidParameter
;
1724 stat
= get_region_scans_data(region
, matrix
, &data
);
1728 *count
= data
->rdh
.nCount
;
1729 rects
= (RECT
*)data
->Buffer
;
1733 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1735 scans
[i
].X
= rects
[i
].left
;
1736 scans
[i
].Y
= rects
[i
].top
;
1737 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1738 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;