push 4901f8df60e614eec36809b422720d1d91fd69fe
[wine/hacks.git] / dlls / gdiplus / region.c
blob45d0e51fac4e88ce54fef58be907847448e37440
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
25 #include "objbase.h"
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33 /**********************************************************
35 * Data returned by GdipGetRegionData looks something like this:
37 * struct region_data_header
38 * {
39 * DWORD size; size in bytes of the data - 8.
40 * DWORD magic1; probably a checksum.
41 * DWORD magic2; always seems to be 0xdbc01001 - version?
42 * DWORD num_ops; number of combining ops * 2
43 * };
45 * Then follows a sequence of combining ops and region elements.
47 * A region element is either a RECTF or some path data.
49 * Combining ops are just stored as their CombineMode value.
51 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
52 * stored as 0x10000002 (with no following RECTF) and an infinite rect
53 * is stored as 0x10000003 (again with no following RECTF).
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
58 * The combining ops are stored in the reverse order to the region
59 * elements and in the reverse order to which the region was
60 * constructed.
62 * When two or more complex regions (ie those with more than one
63 * element) are combined, the combining op for the two regions comes
64 * first, then the combining ops for the region elements in region 1,
65 * followed by the region elements for region 1, then follows the
66 * combining ops for region 2 and finally region 2's region elements.
67 * Presumably you're supposed to use the 0x1000000x header to find the
68 * end of the op list (the count of the elements in each region is not
69 * stored).
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
76 typedef enum RegionType
78 RegionDataRect = 0x10000000,
79 RegionDataPath = 0x10000001,
80 RegionDataEmptyRect = 0x10000002,
81 RegionDataInfiniteRect = 0x10000003,
82 } RegionType;
84 #define FLAGS_NOFLAGS 0x0
85 #define FLAGS_INTPATH 0x4000
87 /* Header size as far as header->size is concerned. This doesn't include
88 * header->size or header->checksum
90 static const INT sizeheader_size = sizeof(DWORD) * 2;
92 typedef struct packed_point
94 short X;
95 short Y;
96 } packed_point;
98 /* Everything is measured in DWORDS; round up if there's a remainder */
99 static inline INT get_pathtypes_size(const GpPath* path)
101 INT needed = path->pathdata.Count / sizeof(DWORD);
103 if (path->pathdata.Count % sizeof(DWORD) > 0)
104 needed++;
106 return needed * sizeof(DWORD);
109 static inline INT get_element_size(const region_element* element)
111 INT needed = sizeof(DWORD); /* DWORD for the type */
112 switch(element->type)
114 case RegionDataRect:
115 return needed + sizeof(GpRect);
116 case RegionDataPath:
117 needed += element->elementdata.pathdata.pathheader.size;
118 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
119 return needed;
120 case RegionDataEmptyRect:
121 case RegionDataInfiniteRect:
122 return needed;
123 default:
124 needed += get_element_size(element->elementdata.combine.left);
125 needed += get_element_size(element->elementdata.combine.right);
126 return needed;
129 return 0;
132 /* Does not check parameters, caller must do that */
133 static inline GpStatus init_region(GpRegion* region, const RegionType type)
135 region->node.type = type;
136 region->header.checksum = 0xdeadbeef;
137 region->header.magic = VERSION_MAGIC;
138 region->header.num_children = 0;
139 region->header.size = sizeheader_size + get_element_size(&region->node);
141 return Ok;
144 static inline void delete_element(region_element* element)
146 switch(element->type)
148 case RegionDataRect:
149 break;
150 case RegionDataPath:
151 GdipDeletePath(element->elementdata.pathdata.path);
152 break;
153 case RegionDataEmptyRect:
154 case RegionDataInfiniteRect:
155 break;
156 default:
157 delete_element(element->elementdata.combine.left);
158 delete_element(element->elementdata.combine.right);
159 GdipFree(element->elementdata.combine.left);
160 GdipFree(element->elementdata.combine.right);
161 break;
165 static inline GpStatus clone_element(const region_element* element,
166 region_element** element2)
168 GpStatus stat;
170 *element2 = GdipAlloc(sizeof(region_element));
171 if (!*element2)
172 return OutOfMemory;
174 (*element2)->type = element->type;
176 switch (element->type)
178 case RegionDataRect:
179 (*element2)->elementdata.rect = element->elementdata.rect;
180 break;
181 case RegionDataEmptyRect:
182 case RegionDataInfiniteRect:
183 break;
184 case RegionDataPath:
185 (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
186 stat = GdipClonePath(element->elementdata.pathdata.path,
187 &(*element2)->elementdata.pathdata.path);
188 if (stat != Ok) goto clone_out;
189 break;
190 default:
191 stat = clone_element(element->elementdata.combine.left,
192 &(*element2)->elementdata.combine.left);
193 if (stat != Ok) goto clone_out;
194 stat = clone_element(element->elementdata.combine.right,
195 &(*element2)->elementdata.combine.right);
196 if (stat != Ok) goto clone_out;
197 break;
200 return Ok;
202 clone_out:
203 delete_element(*element2);
204 *element2 = NULL;
205 return stat;
208 /* Common code for CombineRegion*
209 * All the caller has to do is get its format into an element
211 static inline void fuse_region(GpRegion* region, region_element* left,
212 region_element* right, const CombineMode mode)
214 region->node.type = mode;
215 region->node.elementdata.combine.left = left;
216 region->node.elementdata.combine.right = right;
218 region->header.size = sizeheader_size + get_element_size(&region->node);
219 region->header.num_children += 2;
222 /*****************************************************************************
223 * GdipCloneRegion [GDIPLUS.@]
225 * Creates a deep copy of the region
227 * PARAMS
228 * region [I] source region
229 * clone [O] resulting clone
231 * RETURNS
232 * SUCCESS: Ok
233 * FAILURE: InvalidParameter or OutOfMemory
235 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
237 region_element *element;
239 TRACE("%p %p\n", region, clone);
241 if (!(region && clone))
242 return InvalidParameter;
244 *clone = GdipAlloc(sizeof(GpRegion));
245 if (!*clone)
246 return OutOfMemory;
247 element = &(*clone)->node;
249 (*clone)->header = region->header;
250 return clone_element(&region->node, &element);
253 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
255 GpRegion *path_region;
256 region_element *left, *right = NULL;
257 GpStatus stat;
259 TRACE("%p %p %d\n", region, path, mode);
261 if (!(region && path))
262 return InvalidParameter;
264 stat = GdipCreateRegionPath(path, &path_region);
265 if (stat != Ok)
266 return stat;
268 left = GdipAlloc(sizeof(region_element));
269 if (!left)
270 goto out;
271 *left = region->node;
273 stat = clone_element(&path_region->node, &right);
274 if (stat != Ok)
275 goto out;
277 fuse_region(region, left, right, mode);
279 GdipDeleteRegion(path_region);
280 return Ok;
282 out:
283 GdipFree(left);
284 delete_element(right);
285 GdipDeleteRegion(path_region);
286 return stat;
289 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
290 GDIPCONST GpRectF *rect, CombineMode mode)
292 GpRegion *rect_region;
293 region_element *left, *right = NULL;
294 GpStatus stat;
296 TRACE("%p %p %d\n", region, rect, mode);
298 if (!(region && rect))
299 return InvalidParameter;
301 stat = GdipCreateRegionRect(rect, &rect_region);
302 if (stat != Ok)
303 return stat;
305 left = GdipAlloc(sizeof(region_element));
306 if (!left)
307 goto out;
308 memcpy(left, &region->node, sizeof(region_element));
310 stat = clone_element(&rect_region->node, &right);
311 if (stat != Ok)
312 goto out;
314 fuse_region(region, left, right, mode);
316 GdipDeleteRegion(rect_region);
317 return Ok;
319 out:
320 GdipFree(left);
321 delete_element(right);
322 GdipDeleteRegion(rect_region);
323 return stat;
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327 GDIPCONST GpRect *rect, CombineMode mode)
329 GpRectF rectf;
331 TRACE("%p %p %d\n", region, rect, mode);
333 if (!rect)
334 return InvalidParameter;
336 rectf.X = (REAL)rect->X;
337 rectf.Y = (REAL)rect->Y;
338 rectf.Height = (REAL)rect->Height;
339 rectf.Width = (REAL)rect->Width;
341 return GdipCombineRegionRect(region, &rectf, mode);
344 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
345 GpRegion *region2, CombineMode mode)
347 region_element *left, *right;
348 GpStatus stat;
350 TRACE("%p %p %d\n", region1, region2, mode);
352 if(!(region1 && region2))
353 return InvalidParameter;
355 left = GdipAlloc(sizeof(region_element));
356 if (!left)
357 return OutOfMemory;
359 *left = region1->node;
360 stat = clone_element(&region2->node, &right);
361 if (stat != Ok)
363 GdipFree(left);
364 delete_element(right);
365 return OutOfMemory;
368 fuse_region(region1, left, right, mode);
369 region1->header.num_children += region2->header.num_children;
371 return Ok;
374 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
376 if(!region)
377 return InvalidParameter;
379 TRACE("%p\n", region);
381 *region = GdipAlloc(sizeof(GpRegion));
382 if(!*region)
383 return OutOfMemory;
385 return init_region(*region, RegionDataInfiniteRect);
388 /*****************************************************************************
389 * GdipCreateRegionPath [GDIPLUS.@]
391 * Creates a GpRegion from a GpPath
393 * PARAMS
394 * path [I] path to base the region on
395 * region [O] pointer to the newly allocated region
397 * RETURNS
398 * SUCCESS: Ok
399 * FAILURE: InvalidParameter
401 * NOTES
402 * If a path has no floating point points, its points will be stored as shorts
403 * (INTPATH)
405 * If a path is empty, it is considered to be an INTPATH
407 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
409 region_element* element;
410 GpPoint *pointsi;
411 GpPointF *pointsf;
413 GpStatus stat;
414 DWORD flags = FLAGS_INTPATH;
415 INT count, i;
417 TRACE("%p, %p\n", path, region);
419 if (!(path && region))
420 return InvalidParameter;
422 *region = GdipAlloc(sizeof(GpRegion));
423 if(!*region)
424 return OutOfMemory;
425 stat = init_region(*region, RegionDataPath);
426 if (stat != Ok)
428 GdipDeleteRegion(*region);
429 return stat;
431 element = &(*region)->node;
432 count = path->pathdata.Count;
434 /* Test to see if the path is an Integer path */
435 if (count)
437 pointsi = GdipAlloc(sizeof(GpPoint) * count);
438 pointsf = GdipAlloc(sizeof(GpPointF) * count);
439 if (!(pointsi && pointsf))
441 GdipFree(pointsi);
442 GdipFree(pointsf);
443 GdipDeleteRegion(*region);
444 return OutOfMemory;
447 stat = GdipGetPathPointsI(path, pointsi, count);
448 if (stat != Ok)
450 GdipDeleteRegion(*region);
451 return stat;
453 stat = GdipGetPathPoints(path, pointsf, count);
454 if (stat != Ok)
456 GdipDeleteRegion(*region);
457 return stat;
460 for (i = 0; i < count; i++)
462 if (!(pointsi[i].X == pointsf[i].X &&
463 pointsi[i].Y == pointsf[i].Y ))
465 flags = FLAGS_NOFLAGS;
466 break;
469 GdipFree(pointsi);
470 GdipFree(pointsf);
473 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
474 if (stat != Ok)
476 GdipDeleteRegion(*region);
477 return stat;
480 /* 3 for headers, once again size doesn't count itself */
481 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
482 switch(flags)
484 /* Floats, sent out as floats */
485 case FLAGS_NOFLAGS:
486 element->elementdata.pathdata.pathheader.size +=
487 (sizeof(DWORD) * count * 2);
488 break;
489 /* INTs, sent out as packed shorts */
490 case FLAGS_INTPATH:
491 element->elementdata.pathdata.pathheader.size +=
492 (sizeof(DWORD) * count);
493 break;
494 default:
495 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
497 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
498 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
499 element->elementdata.pathdata.pathheader.count = count;
500 element->elementdata.pathdata.pathheader.flags = flags;
501 (*region)->header.size = sizeheader_size + get_element_size(element);
503 return Ok;
506 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
507 GpRegion **region)
509 GpStatus stat;
511 TRACE("%p, %p\n", rect, region);
513 if (!(rect && region))
514 return InvalidParameter;
516 *region = GdipAlloc(sizeof(GpRegion));
517 stat = init_region(*region, RegionDataRect);
518 if(stat != Ok)
520 GdipDeleteRegion(*region);
521 return stat;
524 (*region)->node.elementdata.rect.X = rect->X;
525 (*region)->node.elementdata.rect.Y = rect->Y;
526 (*region)->node.elementdata.rect.Width = rect->Width;
527 (*region)->node.elementdata.rect.Height = rect->Height;
529 return Ok;
532 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
533 GpRegion **region)
535 GpRectF rectf;
537 TRACE("%p, %p\n", rect, region);
539 rectf.X = (REAL)rect->X;
540 rectf.Y = (REAL)rect->Y;
541 rectf.Width = (REAL)rect->Width;
542 rectf.Height = (REAL)rect->Height;
544 return GdipCreateRegionRect(&rectf, region);
547 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
549 FIXME("(%p, %d, %p): stub\n", data, size, region);
551 *region = NULL;
552 return NotImplemented;
555 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
557 FIXME("(%p, %p): stub\n", hrgn, region);
559 *region = NULL;
560 return NotImplemented;
563 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
565 TRACE("%p\n", region);
567 if (!region)
568 return InvalidParameter;
570 delete_element(&region->node);
571 GdipFree(region);
573 return Ok;
576 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
578 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
580 return NotImplemented;
583 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
585 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
587 return NotImplemented;
590 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
592 location[*offset] = write;
593 (*offset)++;
596 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
598 ((FLOAT*)location)[*offset] = write;
599 (*offset)++;
602 static inline void write_packed_point(DWORD* location, INT* offset,
603 const GpPointF* write)
605 packed_point point;
607 point.X = write->X;
608 point.Y = write->Y;
609 memcpy(location + *offset, &point, sizeof(packed_point));
610 (*offset)++;
613 static inline void write_path_types(DWORD* location, INT* offset,
614 const GpPath* path)
616 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
618 /* The unwritten parts of the DWORD (if any) must be cleared */
619 if (path->pathdata.Count % sizeof(DWORD))
620 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
621 path->pathdata.Count,
622 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
623 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
626 static void write_element(const region_element* element, DWORD *buffer,
627 INT* filled)
629 write_dword(buffer, filled, element->type);
630 switch (element->type)
632 case CombineModeReplace:
633 case CombineModeIntersect:
634 case CombineModeUnion:
635 case CombineModeXor:
636 case CombineModeExclude:
637 case CombineModeComplement:
638 write_element(element->elementdata.combine.left, buffer, filled);
639 write_element(element->elementdata.combine.right, buffer, filled);
640 break;
641 case RegionDataRect:
642 write_float(buffer, filled, element->elementdata.rect.X);
643 write_float(buffer, filled, element->elementdata.rect.Y);
644 write_float(buffer, filled, element->elementdata.rect.Width);
645 write_float(buffer, filled, element->elementdata.rect.Height);
646 break;
647 case RegionDataPath:
649 INT i;
650 const GpPath* path = element->elementdata.pathdata.path;
652 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
653 sizeof(element->elementdata.pathdata.pathheader));
654 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
655 switch (element->elementdata.pathdata.pathheader.flags)
657 case FLAGS_NOFLAGS:
658 for (i = 0; i < path->pathdata.Count; i++)
660 write_float(buffer, filled, path->pathdata.Points[i].X);
661 write_float(buffer, filled, path->pathdata.Points[i].Y);
663 break;
664 case FLAGS_INTPATH:
665 for (i = 0; i < path->pathdata.Count; i++)
667 write_packed_point(buffer, filled,
668 &path->pathdata.Points[i]);
671 write_path_types(buffer, filled, path);
672 break;
674 case RegionDataEmptyRect:
675 case RegionDataInfiniteRect:
676 break;
680 /*****************************************************************************
681 * GdipGetRegionData [GDIPLUS.@]
683 * Returns the header, followed by combining ops and region elements.
685 * PARAMS
686 * region [I] region to retrieve from
687 * buffer [O] buffer to hold the resulting data
688 * size [I] size of the buffer
689 * needed [O] (optional) how much data was written
691 * RETURNS
692 * SUCCESS: Ok
693 * FAILURE: InvalidParamter
695 * NOTES
696 * The header contains the size, a checksum, a version string, and the number
697 * of children. The size does not count itself or the checksum.
698 * Version is always something like 0xdbc01001 or 0xdbc01002
700 * An element is a RECT, or PATH; Combining ops are stored as their
701 * CombineMode value. Special regions (infinite, empty) emit just their
702 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
703 * their code followed by a second header for the path followed by the actual
704 * path data. Followed by the flags for each point. The pathheader contains
705 * the size of the data to follow, a version number again, followed by a count
706 * of how many points, and any special flags which may apply. 0x4000 means its
707 * a path of shorts instead of FLOAT.
709 * Combining Ops are stored in reverse order from when they were constructed;
710 * the output is a tree where the left side combining area is always taken
711 * first.
713 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
714 UINT *needed)
716 INT filled = 0;
718 if (!(region && buffer && size))
719 return InvalidParameter;
721 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
722 memcpy(buffer, &region->header, sizeof(region->header));
723 filled += sizeof(region->header) / sizeof(DWORD);
724 /* With few exceptions, everything written is DWORD aligned,
725 * so use that as our base */
726 write_element(&region->node, (DWORD*)buffer, &filled);
728 if (needed)
729 *needed = filled * sizeof(DWORD);
731 return Ok;
734 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
736 if (!(region && needed))
737 return InvalidParameter;
739 TRACE("%p, %p\n", region, needed);
741 /* header.size doesn't count header.size and header.checksum */
742 *needed = region->header.size + sizeof(DWORD) * 2;
744 return Ok;
747 /*****************************************************************************
748 * GdipGetRegionHRgn [GDIPLUS.@]
750 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
752 FIXME("(%p, %p, %p): stub\n", region, graphics, hrgn);
754 *hrgn = NULL;
755 return NotImplemented;
758 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
760 FIXME("(%p, %p, %p): stub\n", region, graphics, res);
762 return NotImplemented;
765 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
766 BOOL *res)
768 FIXME("(%p, %p, %p, %p): stub\n", region, region2, graphics, res);
770 return NotImplemented;
773 /* I think graphics is ignored here */
774 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
776 TRACE("(%p, %p, %p)\n", region, graphics, res);
778 if(!region || !graphics || !res)
779 return InvalidParameter;
781 *res = (region->node.type == RegionDataInfiniteRect);
783 return Ok;
786 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
788 GpStatus stat;
790 TRACE("%p\n", region);
792 if (!region)
793 return InvalidParameter;
795 delete_element(&region->node);
796 stat = init_region(region, RegionDataEmptyRect);
798 return stat;
801 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
803 GpStatus stat;
805 if (!region)
806 return InvalidParameter;
808 TRACE("%p\n", region);
810 delete_element(&region->node);
811 stat = init_region(region, RegionDataInfiniteRect);
813 return stat;
816 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
818 FIXME("(%p, %p): stub\n", region, matrix);
820 return NotImplemented;
823 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
825 FIXME("(%p, %f, %f): stub\n", region, dx, dy);
827 return NotImplemented;
830 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
832 FIXME("(%p, %d, %d): stub\n", region, dx, dy);
834 return NotImplemented;