gdiplus: Print the GdipCreateHalftonePalette FIXME only once.
[wine] / dlls / gdiplus / region.c
1 /*
2  * Copyright (C) 2008 Google (Lei Zhang)
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 /**********************************************************
34  *
35  * Data returned by GdipGetRegionData looks something like this:
36  *
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  * };
44  *
45  * Then follows a sequence of combining ops and region elements.
46  *
47  * A region element is either a RECTF or some path data.
48  *
49  * Combining ops are just stored as their CombineMode value.
50  *
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).
54  *
55  * Path data is preceded by the DWORD 0x10000001.  Then follows a
56  * DWORD size and then size bytes of data.
57  *
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.
61  *
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).
70  *
71  * When a simple region (1 element) is combined, it's treated as if a
72  * single rect/path is being combined.
73  *
74  */
75
76 #define FLAGS_NOFLAGS   0x0
77 #define FLAGS_INTPATH   0x4000
78
79 /* Header size as far as header->size is concerned. This doesn't include
80  * header->size or header->checksum
81  */
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
83
84 typedef struct packed_point
85 {
86     short X;
87     short Y;
88 } packed_point;
89
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
92 {
93     INT needed = path->pathdata.Count / sizeof(DWORD);
94
95     if (path->pathdata.Count % sizeof(DWORD) > 0)
96         needed++;
97
98     return needed * sizeof(DWORD);
99 }
100
101 static inline INT get_element_size(const region_element* element)
102 {
103     INT needed = sizeof(DWORD); /* DWORD for the type */
104     switch(element->type)
105     {
106         case RegionDataRect:
107             return needed + sizeof(GpRect);
108         case RegionDataPath:
109              needed += element->elementdata.pathdata.pathheader.size;
110              needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
111              return needed;
112         case RegionDataEmptyRect:
113         case RegionDataInfiniteRect:
114             return needed;
115         default:
116             needed += get_element_size(element->elementdata.combine.left);
117             needed += get_element_size(element->elementdata.combine.right);
118             return needed;
119     }
120
121     return 0;
122 }
123
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
126 {
127     region->node.type       = type;
128     region->header.checksum = 0xdeadbeef;
129     region->header.magic    = VERSION_MAGIC;
130     region->header.num_children  = 0;
131     region->header.size     = sizeheader_size + get_element_size(&region->node);
132
133     return Ok;
134 }
135
136 static inline GpStatus clone_element(const region_element* element,
137         region_element** element2)
138 {
139     GpStatus stat;
140
141     /* root node is allocated with GpRegion */
142     if(!*element2){
143         *element2 = GdipAlloc(sizeof(region_element));
144         if (!*element2)
145             return OutOfMemory;
146     }
147
148     (*element2)->type = element->type;
149
150     switch (element->type)
151     {
152         case RegionDataRect:
153             (*element2)->elementdata.rect = element->elementdata.rect;
154             break;
155         case RegionDataEmptyRect:
156         case RegionDataInfiniteRect:
157             break;
158         case RegionDataPath:
159             (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
160             stat = GdipClonePath(element->elementdata.pathdata.path,
161                     &(*element2)->elementdata.pathdata.path);
162             if (stat != Ok) goto clone_out;
163             break;
164         default:
165             (*element2)->elementdata.combine.left  = NULL;
166             (*element2)->elementdata.combine.right = NULL;
167
168             stat = clone_element(element->elementdata.combine.left,
169                     &(*element2)->elementdata.combine.left);
170             if (stat != Ok) goto clone_out;
171             stat = clone_element(element->elementdata.combine.right,
172                     &(*element2)->elementdata.combine.right);
173             if (stat != Ok) goto clone_out;
174             break;
175     }
176
177     return Ok;
178
179 clone_out:
180     delete_element(*element2);
181     *element2 = NULL;
182     return stat;
183 }
184
185 /* Common code for CombineRegion*
186  * All the caller has to do is get its format into an element
187  */
188 static inline void fuse_region(GpRegion* region, region_element* left,
189         region_element* right, const CombineMode mode)
190 {
191     region->node.type = mode;
192     region->node.elementdata.combine.left = left;
193     region->node.elementdata.combine.right = right;
194
195     region->header.size = sizeheader_size + get_element_size(&region->node);
196     region->header.num_children += 2;
197 }
198
199 /*****************************************************************************
200  * GdipCloneRegion [GDIPLUS.@]
201  *
202  * Creates a deep copy of the region
203  *
204  * PARAMS
205  *  region  [I] source region
206  *  clone   [O] resulting clone
207  *
208  * RETURNS
209  *  SUCCESS: Ok
210  *  FAILURE: InvalidParameter or OutOfMemory
211  */
212 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
213 {
214     region_element *element;
215
216     TRACE("%p %p\n", region, clone);
217
218     if (!(region && clone))
219         return InvalidParameter;
220
221     *clone = GdipAlloc(sizeof(GpRegion));
222     if (!*clone)
223         return OutOfMemory;
224     element = &(*clone)->node;
225
226     (*clone)->header = region->header;
227     return clone_element(&region->node, &element);
228 }
229
230 /*****************************************************************************
231  * GdipCombineRegionPath [GDIPLUS.@]
232  */
233 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
234 {
235     GpRegion *path_region;
236     region_element *left, *right = NULL;
237     GpStatus stat;
238
239     TRACE("%p %p %d\n", region, path, mode);
240
241     if (!(region && path))
242         return InvalidParameter;
243
244     stat = GdipCreateRegionPath(path, &path_region);
245     if (stat != Ok)
246         return stat;
247
248     /* simply replace region data */
249     if(mode == CombineModeReplace){
250         delete_element(&region->node);
251         memcpy(region, path_region, sizeof(GpRegion));
252         GdipFree(path_region);
253         return Ok;
254     }
255
256     left = GdipAlloc(sizeof(region_element));
257     if (!left)
258         goto out;
259     *left = region->node;
260
261     stat = clone_element(&path_region->node, &right);
262     if (stat != Ok)
263         goto out;
264
265     fuse_region(region, left, right, mode);
266
267     GdipDeleteRegion(path_region);
268     return Ok;
269
270 out:
271     GdipFree(left);
272     GdipDeleteRegion(path_region);
273     return stat;
274 }
275
276 /*****************************************************************************
277  * GdipCombineRegionRect [GDIPLUS.@]
278  */
279 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
280         GDIPCONST GpRectF *rect, CombineMode mode)
281 {
282     GpRegion *rect_region;
283     region_element *left, *right = NULL;
284     GpStatus stat;
285
286     TRACE("%p %p %d\n", region, rect, mode);
287
288     if (!(region && rect))
289         return InvalidParameter;
290
291     stat = GdipCreateRegionRect(rect, &rect_region);
292     if (stat != Ok)
293         return stat;
294
295     /* simply replace region data */
296     if(mode == CombineModeReplace){
297         delete_element(&region->node);
298         memcpy(region, rect_region, sizeof(GpRegion));
299         GdipFree(rect_region);
300         return Ok;
301     }
302
303     left = GdipAlloc(sizeof(region_element));
304     if (!left)
305         goto out;
306     memcpy(left, &region->node, sizeof(region_element));
307
308     stat = clone_element(&rect_region->node, &right);
309     if (stat != Ok)
310         goto out;
311
312     fuse_region(region, left, right, mode);
313
314     GdipDeleteRegion(rect_region);
315     return Ok;
316
317 out:
318     GdipFree(left);
319     GdipDeleteRegion(rect_region);
320     return stat;
321 }
322
323 /*****************************************************************************
324  * GdipCombineRegionRectI [GDIPLUS.@]
325  */
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327         GDIPCONST GpRect *rect, CombineMode mode)
328 {
329     GpRectF rectf;
330
331     TRACE("%p %p %d\n", region, rect, mode);
332
333     if (!rect)
334         return InvalidParameter;
335
336     rectf.X = (REAL)rect->X;
337     rectf.Y = (REAL)rect->Y;
338     rectf.Height = (REAL)rect->Height;
339     rectf.Width = (REAL)rect->Width;
340
341     return GdipCombineRegionRect(region, &rectf, mode);
342 }
343
344 /*****************************************************************************
345  * GdipCombineRegionRegion [GDIPLUS.@]
346  */
347 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
348         GpRegion *region2, CombineMode mode)
349 {
350     region_element *left, *right = NULL;
351     GpStatus stat;
352     GpRegion *reg2copy;
353
354     TRACE("%p %p %d\n", region1, region2, mode);
355
356     if(!(region1 && region2))
357         return InvalidParameter;
358
359     /* simply replace region data */
360     if(mode == CombineModeReplace){
361         stat = GdipCloneRegion(region2, &reg2copy);
362         if(stat != Ok)  return stat;
363
364         delete_element(&region1->node);
365         memcpy(region1, reg2copy, sizeof(GpRegion));
366         GdipFree(reg2copy);
367         return Ok;
368     }
369
370     left  = GdipAlloc(sizeof(region_element));
371     if (!left)
372         return OutOfMemory;
373
374     *left = region1->node;
375     stat = clone_element(&region2->node, &right);
376     if (stat != Ok)
377     {
378         GdipFree(left);
379         return OutOfMemory;
380     }
381
382     fuse_region(region1, left, right, mode);
383     region1->header.num_children += region2->header.num_children;
384
385     return Ok;
386 }
387
388 /*****************************************************************************
389  * GdipCreateRegion [GDIPLUS.@]
390  */
391 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
392 {
393     TRACE("%p\n", region);
394
395     if(!region)
396         return InvalidParameter;
397
398     *region = GdipAlloc(sizeof(GpRegion));
399     if(!*region)
400         return OutOfMemory;
401
402     return init_region(*region, RegionDataInfiniteRect);
403 }
404
405 /*****************************************************************************
406  * GdipCreateRegionPath [GDIPLUS.@]
407  *
408  * Creates a GpRegion from a GpPath
409  *
410  * PARAMS
411  *  path    [I] path to base the region on
412  *  region  [O] pointer to the newly allocated region
413  *
414  * RETURNS
415  *  SUCCESS: Ok
416  *  FAILURE: InvalidParameter
417  *
418  * NOTES
419  *  If a path has no floating point points, its points will be stored as shorts
420  *  (INTPATH)
421  *
422  *  If a path is empty, it is considered to be an INTPATH
423  */
424 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
425 {
426     region_element* element;
427     GpPoint  *pointsi;
428     GpPointF *pointsf;
429
430     GpStatus stat;
431     DWORD flags = FLAGS_INTPATH;
432     INT count, i;
433
434     TRACE("%p, %p\n", path, region);
435
436     if (!(path && region))
437         return InvalidParameter;
438
439     *region = GdipAlloc(sizeof(GpRegion));
440     if(!*region)
441         return OutOfMemory;
442     stat = init_region(*region, RegionDataPath);
443     if (stat != Ok)
444     {
445         GdipDeleteRegion(*region);
446         return stat;
447     }
448     element = &(*region)->node;
449     count = path->pathdata.Count;
450
451     /* Test to see if the path is an Integer path */
452     if (count)
453     {
454         pointsi = GdipAlloc(sizeof(GpPoint) * count);
455         pointsf = GdipAlloc(sizeof(GpPointF) * count);
456         if (!(pointsi && pointsf))
457         {
458             GdipFree(pointsi);
459             GdipFree(pointsf);
460             GdipDeleteRegion(*region);
461             return OutOfMemory;
462         }
463
464         stat = GdipGetPathPointsI(path, pointsi, count);
465         if (stat != Ok)
466         {
467             GdipDeleteRegion(*region);
468             return stat;
469         }
470         stat = GdipGetPathPoints(path, pointsf, count);
471         if (stat != Ok)
472         {
473             GdipDeleteRegion(*region);
474             return stat;
475         }
476
477         for (i = 0; i < count; i++)
478         {
479             if (!(pointsi[i].X == pointsf[i].X &&
480                   pointsi[i].Y == pointsf[i].Y ))
481             {
482                 flags = FLAGS_NOFLAGS;
483                 break;
484             }
485         }
486         GdipFree(pointsi);
487         GdipFree(pointsf);
488     }
489
490     stat = GdipClonePath(path, &element->elementdata.pathdata.path);
491     if (stat != Ok)
492     {
493         GdipDeleteRegion(*region);
494         return stat;
495     }
496
497     /* 3 for headers, once again size doesn't count itself */
498     element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
499     switch(flags)
500     {
501         /* Floats, sent out as floats */
502         case FLAGS_NOFLAGS:
503             element->elementdata.pathdata.pathheader.size +=
504                 (sizeof(DWORD) * count * 2);
505             break;
506         /* INTs, sent out as packed shorts */
507         case FLAGS_INTPATH:
508             element->elementdata.pathdata.pathheader.size +=
509                 (sizeof(DWORD) * count);
510             break;
511         default:
512             FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
513     }
514     element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
515     element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
516     element->elementdata.pathdata.pathheader.count = count;
517     element->elementdata.pathdata.pathheader.flags = flags;
518     (*region)->header.size = sizeheader_size + get_element_size(element);
519
520     return Ok;
521 }
522
523 /*****************************************************************************
524  * GdipCreateRegionRect [GDIPLUS.@]
525  */
526 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
527         GpRegion **region)
528 {
529     GpStatus stat;
530
531     TRACE("%p, %p\n", rect, region);
532
533     if (!(rect && region))
534         return InvalidParameter;
535
536     *region = GdipAlloc(sizeof(GpRegion));
537     stat = init_region(*region, RegionDataRect);
538     if(stat != Ok)
539     {
540         GdipDeleteRegion(*region);
541         return stat;
542     }
543
544     (*region)->node.elementdata.rect.X = rect->X;
545     (*region)->node.elementdata.rect.Y = rect->Y;
546     (*region)->node.elementdata.rect.Width = rect->Width;
547     (*region)->node.elementdata.rect.Height = rect->Height;
548
549     return Ok;
550 }
551
552 /*****************************************************************************
553  * GdipCreateRegionRectI [GDIPLUS.@]
554  */
555 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
556         GpRegion **region)
557 {
558     GpRectF rectf;
559
560     TRACE("%p, %p\n", rect, region);
561
562     rectf.X = (REAL)rect->X;
563     rectf.Y = (REAL)rect->Y;
564     rectf.Width = (REAL)rect->Width;
565     rectf.Height = (REAL)rect->Height;
566
567     return GdipCreateRegionRect(&rectf, region);
568 }
569
570 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
571 {
572     FIXME("(%p, %d, %p): stub\n", data, size, region);
573
574     *region = NULL;
575     return NotImplemented;
576 }
577
578
579 /******************************************************************************
580  * GdipCreateRegionHrgn [GDIPLUS.@]
581  */
582 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
583 {
584     DWORD size;
585     LPRGNDATA buf;
586     LPRECT rect;
587     GpStatus stat;
588     GpPath* path;
589     GpRegion* local;
590     int i;
591
592     TRACE("(%p, %p)\n", hrgn, region);
593
594     if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
595         return InvalidParameter;
596
597     buf = GdipAlloc(size);
598     if(!buf)
599         return OutOfMemory;
600
601     if(!GetRegionData(hrgn, size, buf)){
602         GdipFree(buf);
603         return GenericError;
604     }
605
606     if(buf->rdh.nCount == 0){
607         if((stat = GdipCreateRegion(&local)) != Ok){
608             GdipFree(buf);
609             return stat;
610         }
611         if((stat = GdipSetEmpty(local)) != Ok){
612             GdipFree(buf);
613             GdipDeleteRegion(local);
614             return stat;
615         }
616         *region = local;
617         GdipFree(buf);
618         return Ok;
619     }
620
621     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
622         GdipFree(buf);
623         return stat;
624     }
625
626     rect = (LPRECT)buf->Buffer;
627     for(i = 0; i < buf->rdh.nCount; i++){
628         if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
629                         (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
630             GdipFree(buf);
631             GdipDeletePath(path);
632             return stat;
633         }
634         rect++;
635     }
636
637     stat = GdipCreateRegionPath(path, region);
638
639     GdipFree(buf);
640     GdipDeletePath(path);
641     return stat;
642 }
643
644 /*****************************************************************************
645  * GdipDeleteRegion [GDIPLUS.@]
646  */
647 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
648 {
649     TRACE("%p\n", region);
650
651     if (!region)
652         return InvalidParameter;
653
654     delete_element(&region->node);
655     GdipFree(region);
656
657     return Ok;
658 }
659
660 /*****************************************************************************
661  * GdipGetRegionBounds [GDIPLUS.@]
662  */
663 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
664 {
665     HRGN hrgn;
666     RECT r;
667     GpStatus status;
668
669     TRACE("(%p, %p, %p)\n", region, graphics, rect);
670
671     if(!region || !graphics || !rect)
672         return InvalidParameter;
673
674     /* Contrary to MSDN, native ignores the graphics transform. */
675     status = GdipGetRegionHRgn(region, NULL, &hrgn);
676     if(status != Ok)
677         return status;
678
679     /* infinite */
680     if(!hrgn){
681         rect->X = rect->Y = -(REAL)(1 << 22);
682         rect->Width = rect->Height = (REAL)(1 << 23);
683         return Ok;
684     }
685
686     if(!GetRgnBox(hrgn, &r)){
687         DeleteObject(hrgn);
688         return GenericError;
689     }
690
691     rect->X = r.left;
692     rect->Y = r.top;
693     rect->Width  = r.right  - r.left;
694     rect->Height = r.bottom - r.top;
695
696     return Ok;
697 }
698
699 /*****************************************************************************
700  * GdipGetRegionBoundsI [GDIPLUS.@]
701  */
702 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
703 {
704     GpRectF rectf;
705     GpStatus status;
706
707     TRACE("(%p, %p, %p)\n", region, graphics, rect);
708
709     if(!rect)
710         return InvalidParameter;
711
712     status = GdipGetRegionBounds(region, graphics, &rectf);
713     if(status == Ok){
714         rect->X = roundr(rectf.X);
715         rect->Y = roundr(rectf.X);
716         rect->Width  = roundr(rectf.Width);
717         rect->Height = roundr(rectf.Height);
718     }
719
720     return status;
721 }
722
723 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
724 {
725     location[*offset] = write;
726     (*offset)++;
727 }
728
729 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
730 {
731     ((FLOAT*)location)[*offset] = write;
732     (*offset)++;
733 }
734
735 static inline void write_packed_point(DWORD* location, INT* offset,
736         const GpPointF* write)
737 {
738     packed_point point;
739
740     point.X = write->X;
741     point.Y = write->Y;
742     memcpy(location + *offset, &point, sizeof(packed_point));
743     (*offset)++;
744 }
745
746 static inline void write_path_types(DWORD* location, INT* offset,
747         const GpPath* path)
748 {
749     memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
750
751     /* The unwritten parts of the DWORD (if any) must be cleared */
752     if (path->pathdata.Count % sizeof(DWORD))
753         ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
754                 path->pathdata.Count,
755                 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
756     *offset += (get_pathtypes_size(path) / sizeof(DWORD));
757 }
758
759 static void write_element(const region_element* element, DWORD *buffer,
760         INT* filled)
761 {
762     write_dword(buffer, filled, element->type);
763     switch (element->type)
764     {
765         case CombineModeReplace:
766         case CombineModeIntersect:
767         case CombineModeUnion:
768         case CombineModeXor:
769         case CombineModeExclude:
770         case CombineModeComplement:
771             write_element(element->elementdata.combine.left, buffer, filled);
772             write_element(element->elementdata.combine.right, buffer, filled);
773             break;
774         case RegionDataRect:
775             write_float(buffer, filled, element->elementdata.rect.X);
776             write_float(buffer, filled, element->elementdata.rect.Y);
777             write_float(buffer, filled, element->elementdata.rect.Width);
778             write_float(buffer, filled, element->elementdata.rect.Height);
779             break;
780         case RegionDataPath:
781         {
782             INT i;
783             const GpPath* path = element->elementdata.pathdata.path;
784
785             memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
786                     sizeof(element->elementdata.pathdata.pathheader));
787             *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
788             switch (element->elementdata.pathdata.pathheader.flags)
789             {
790                 case FLAGS_NOFLAGS:
791                     for (i = 0; i < path->pathdata.Count; i++)
792                     {
793                         write_float(buffer, filled, path->pathdata.Points[i].X);
794                         write_float(buffer, filled, path->pathdata.Points[i].Y);
795                     }
796                     break;
797                 case FLAGS_INTPATH:
798                     for (i = 0; i < path->pathdata.Count; i++)
799                     {
800                         write_packed_point(buffer, filled,
801                                 &path->pathdata.Points[i]);
802                     }
803             }
804             write_path_types(buffer, filled, path);
805             break;
806         }
807         case RegionDataEmptyRect:
808         case RegionDataInfiniteRect:
809             break;
810     }
811 }
812
813 /*****************************************************************************
814  * GdipGetRegionData [GDIPLUS.@]
815  *
816  * Returns the header, followed by combining ops and region elements.
817  *
818  * PARAMS
819  *  region  [I] region to retrieve from
820  *  buffer  [O] buffer to hold the resulting data
821  *  size    [I] size of the buffer
822  *  needed  [O] (optional) how much data was written
823  *
824  * RETURNS
825  *  SUCCESS: Ok
826  *  FAILURE: InvalidParameter
827  *
828  * NOTES
829  *  The header contains the size, a checksum, a version string, and the number
830  *  of children. The size does not count itself or the checksum.
831  *  Version is always something like 0xdbc01001 or 0xdbc01002
832  *
833  *  An element is a RECT, or PATH; Combining ops are stored as their
834  *  CombineMode value. Special regions (infinite, empty) emit just their
835  *  op-code; GpRectFs emit their code followed by their points; GpPaths emit
836  *  their code followed by a second header for the path followed by the actual
837  *  path data. Followed by the flags for each point. The pathheader contains
838  *  the size of the data to follow, a version number again, followed by a count
839  *  of how many points, and any special flags which may apply. 0x4000 means its
840  *  a path of shorts instead of FLOAT.
841  *
842  *  Combining Ops are stored in reverse order from when they were constructed;
843  *  the output is a tree where the left side combining area is always taken
844  *  first.
845  */
846 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
847         UINT *needed)
848 {
849     INT filled = 0;
850
851     TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
852
853     if (!(region && buffer && size))
854         return InvalidParameter;
855
856     memcpy(buffer, &region->header, sizeof(region->header));
857     filled += sizeof(region->header) / sizeof(DWORD);
858     /* With few exceptions, everything written is DWORD aligned,
859      * so use that as our base */
860     write_element(&region->node, (DWORD*)buffer, &filled);
861
862     if (needed)
863         *needed = filled * sizeof(DWORD);
864
865     return Ok;
866 }
867
868 /*****************************************************************************
869  * GdipGetRegionDataSize [GDIPLUS.@]
870  */
871 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
872 {
873     TRACE("%p, %p\n", region, needed);
874
875     if (!(region && needed))
876         return InvalidParameter;
877
878     /* header.size doesn't count header.size and header.checksum */
879     *needed = region->header.size + sizeof(DWORD) * 2;
880
881     return Ok;
882 }
883
884 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
885 {
886     HDC new_hdc=NULL;
887     GpStatus stat;
888     INT save_state;
889
890     if (!graphics)
891     {
892         new_hdc = GetDC(0);
893         if (!new_hdc)
894             return OutOfMemory;
895
896         stat = GdipCreateFromHDC(new_hdc, &graphics);
897         if (stat != Ok)
898         {
899             ReleaseDC(0, new_hdc);
900             return stat;
901         }
902     }
903
904     save_state = SaveDC(graphics->hdc);
905     EndPath(graphics->hdc);
906
907     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
908                                                                     : WINDING));
909
910     stat = trace_path(graphics, path);
911     if (stat == Ok)
912     {
913         *hrgn = PathToRegion(graphics->hdc);
914         stat = *hrgn ? Ok : OutOfMemory;
915     }
916
917     RestoreDC(graphics->hdc, save_state);
918     if (new_hdc)
919     {
920         ReleaseDC(0, new_hdc);
921         GdipDeleteGraphics(graphics);
922     }
923
924     return stat;
925 }
926
927 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
928 {
929     switch (element->type)
930     {
931         case RegionDataInfiniteRect:
932             *hrgn = NULL;
933             return Ok;
934         case RegionDataEmptyRect:
935             *hrgn = CreateRectRgn(0, 0, 0, 0);
936             return *hrgn ? Ok : OutOfMemory;
937         case RegionDataPath:
938             return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
939         case RegionDataRect:
940         {
941             GpPath* path;
942             GpStatus stat;
943             GpRectF* rc = &element->elementdata.rect;
944
945             stat = GdipCreatePath(FillModeAlternate, &path);
946             if (stat != Ok)
947                 return stat;
948             stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
949
950             if (stat == Ok)
951                 stat = get_path_hrgn(path, graphics, hrgn);
952
953             GdipDeletePath(path);
954
955             return stat;
956         }
957         case CombineModeIntersect:
958         case CombineModeUnion:
959         case CombineModeXor:
960         case CombineModeExclude:
961         case CombineModeComplement:
962         {
963             HRGN left, right;
964             GpStatus stat;
965             int ret;
966
967             stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
968             if (stat != Ok)
969             {
970                 *hrgn = NULL;
971                 return stat;
972             }
973
974             if (left == NULL)
975             {
976                 /* existing region is infinite */
977                 switch (element->type)
978                 {
979                     case CombineModeIntersect:
980                         return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
981                     case CombineModeXor: case CombineModeExclude:
982                         FIXME("cannot exclude from an infinite region\n");
983                         /* fall-through */
984                     case CombineModeUnion: case CombineModeComplement:
985                         *hrgn = NULL;
986                         return Ok;
987                 }
988             }
989
990             stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
991             if (stat != Ok)
992             {
993                 DeleteObject(left);
994                 *hrgn = NULL;
995                 return stat;
996             }
997
998             if (right == NULL)
999             {
1000                 /* new region is infinite */
1001                 switch (element->type)
1002                 {
1003                     case CombineModeIntersect:
1004                         *hrgn = left;
1005                         return Ok;
1006                     case CombineModeXor: case CombineModeComplement:
1007                         FIXME("cannot exclude from an infinite region\n");
1008                         /* fall-through */
1009                     case CombineModeUnion: case CombineModeExclude:
1010                         DeleteObject(left);
1011                         *hrgn = NULL;
1012                         return Ok;
1013                 }
1014             }
1015
1016             switch (element->type)
1017             {
1018                 case CombineModeIntersect:
1019                     ret = CombineRgn(left, left, right, RGN_AND);
1020                     break;
1021                 case CombineModeUnion:
1022                     ret = CombineRgn(left, left, right, RGN_OR);
1023                     break;
1024                 case CombineModeXor:
1025                     ret = CombineRgn(left, left, right, RGN_XOR);
1026                     break;
1027                 case CombineModeExclude:
1028                     ret = CombineRgn(left, left, right, RGN_DIFF);
1029                     break;
1030                 case CombineModeComplement:
1031                     ret = CombineRgn(left, right, left, RGN_DIFF);
1032                     break;
1033                 default:
1034                     ret = ERROR;
1035             }
1036
1037             DeleteObject(right);
1038
1039             if (ret == ERROR)
1040             {
1041                 DeleteObject(left);
1042                 *hrgn = NULL;
1043                 return GenericError;
1044             }
1045
1046             *hrgn = left;
1047             return Ok;
1048         }
1049         default:
1050             FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1051             *hrgn = NULL;
1052             return NotImplemented;
1053     }
1054 }
1055
1056 /*****************************************************************************
1057  * GdipGetRegionHRgn [GDIPLUS.@]
1058  */
1059 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1060 {
1061     TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1062
1063     if (!region || !hrgn)
1064         return InvalidParameter;
1065
1066     return get_region_hrgn(&region->node, graphics, hrgn);
1067 }
1068
1069 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1070 {
1071     TRACE("(%p, %p, %p)\n", region, graphics, res);
1072
1073     if(!region || !graphics || !res)
1074         return InvalidParameter;
1075
1076     *res = (region->node.type == RegionDataEmptyRect);
1077
1078     return Ok;
1079 }
1080
1081 /*****************************************************************************
1082  * GdipIsEqualRegion [GDIPLUS.@]
1083  */
1084 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1085                                       BOOL *res)
1086 {
1087     HRGN hrgn1, hrgn2;
1088     GpStatus stat;
1089
1090     TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1091
1092     if(!region || !region2 || !graphics || !res)
1093         return InvalidParameter;
1094
1095     stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1096     if(stat != Ok)
1097         return stat;
1098     stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1099     if(stat != Ok){
1100         DeleteObject(hrgn1);
1101         return stat;
1102     }
1103
1104     *res = EqualRgn(hrgn1, hrgn2);
1105
1106     /* one of GpRegions is infinite */
1107     if(*res == ERROR)
1108         *res = (!hrgn1 && !hrgn2);
1109
1110     DeleteObject(hrgn1);
1111     DeleteObject(hrgn2);
1112
1113     return Ok;
1114 }
1115
1116 /*****************************************************************************
1117  * GdipIsInfiniteRegion [GDIPLUS.@]
1118  */
1119 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1120 {
1121     /* I think graphics is ignored here */
1122     TRACE("(%p, %p, %p)\n", region, graphics, res);
1123
1124     if(!region || !graphics || !res)
1125         return InvalidParameter;
1126
1127     *res = (region->node.type == RegionDataInfiniteRect);
1128
1129     return Ok;
1130 }
1131
1132 /*****************************************************************************
1133  * GdipIsVisibleRegionRect [GDIPLUS.@]
1134  */
1135 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1136 {
1137     HRGN hrgn;
1138     GpStatus stat;
1139     RECT rect;
1140
1141     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1142
1143     if(!region || !res)
1144         return InvalidParameter;
1145
1146     if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1147         return stat;
1148
1149     /* infinite */
1150     if(!hrgn){
1151         *res = TRUE;
1152         return Ok;
1153     }
1154
1155     rect.left = ceilr(x);
1156     rect.top = ceilr(y);
1157     rect.right = ceilr(x + w);
1158     rect.bottom = ceilr(y + h);
1159
1160     *res = RectInRegion(hrgn, &rect);
1161
1162     DeleteObject(hrgn);
1163
1164     return Ok;
1165 }
1166
1167 /*****************************************************************************
1168  * GdipIsVisibleRegionRectI [GDIPLUS.@]
1169  */
1170 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1171 {
1172     TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1173     if(!region || !res)
1174         return InvalidParameter;
1175
1176     return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1177 }
1178
1179 /*****************************************************************************
1180  * GdipIsVisibleRegionPoint [GDIPLUS.@]
1181  */
1182 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1183 {
1184     HRGN hrgn;
1185     GpStatus stat;
1186
1187     TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1188
1189     if(!region || !res)
1190         return InvalidParameter;
1191
1192     if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1193         return stat;
1194
1195     /* infinite */
1196     if(!hrgn){
1197         *res = TRUE;
1198         return Ok;
1199     }
1200
1201     *res = PtInRegion(hrgn, roundr(x), roundr(y));
1202
1203     DeleteObject(hrgn);
1204
1205     return Ok;
1206 }
1207
1208 /*****************************************************************************
1209  * GdipIsVisibleRegionPointI [GDIPLUS.@]
1210  */
1211 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1212 {
1213     TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1214
1215     return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1216 }
1217
1218 /*****************************************************************************
1219  * GdipSetEmpty [GDIPLUS.@]
1220  */
1221 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1222 {
1223     GpStatus stat;
1224
1225     TRACE("%p\n", region);
1226
1227     if (!region)
1228         return InvalidParameter;
1229
1230     delete_element(&region->node);
1231     stat = init_region(region, RegionDataEmptyRect);
1232
1233     return stat;
1234 }
1235
1236 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1237 {
1238     GpStatus stat;
1239
1240     TRACE("%p\n", region);
1241
1242     if (!region)
1243         return InvalidParameter;
1244
1245     delete_element(&region->node);
1246     stat = init_region(region, RegionDataInfiniteRect);
1247
1248     return stat;
1249 }
1250
1251 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1252 {
1253     FIXME("(%p, %p): stub\n", region, matrix);
1254
1255     return NotImplemented;
1256 }
1257
1258 /* Translates GpRegion elements with specified offsets */
1259 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1260 {
1261     INT i;
1262
1263     switch(element->type)
1264     {
1265         case RegionDataEmptyRect:
1266         case RegionDataInfiniteRect:
1267             return;
1268         case RegionDataRect:
1269             element->elementdata.rect.X += dx;
1270             element->elementdata.rect.Y += dy;
1271             return;
1272         case RegionDataPath:
1273             for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1274                 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1275                 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1276             }
1277             return;
1278         default:
1279             translate_region_element(element->elementdata.combine.left,  dx, dy);
1280             translate_region_element(element->elementdata.combine.right, dx, dy);
1281             return;
1282     }
1283 }
1284
1285 /*****************************************************************************
1286  * GdipTranslateRegion [GDIPLUS.@]
1287  */
1288 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1289 {
1290     TRACE("(%p, %f, %f)\n", region, dx, dy);
1291
1292     if(!region)
1293         return InvalidParameter;
1294
1295     translate_region_element(&region->node, dx, dy);
1296
1297     return Ok;
1298 }
1299
1300 /*****************************************************************************
1301  * GdipTranslateRegionI [GDIPLUS.@]
1302  */
1303 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1304 {
1305     TRACE("(%p, %d, %d)\n", region, dx, dy);
1306
1307     return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1308 }
1309
1310 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1311 {
1312     static int calls;
1313
1314     TRACE("(%p, %p, %p)\n", region, count, matrix);
1315
1316     if (!(calls++))
1317         FIXME("not implemented\n");
1318
1319     return NotImplemented;
1320 }