user32: Reimplement 16-bit clipboard functions on top of the 32-bit ones.
[wine] / dlls / gdiplus / brush.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
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 "winuser.h"
24 #include "wingdi.h"
25
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
30
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
36
37 /******************************************************************************
38  * GdipCloneBrush [GDIPLUS.@]
39  */
40 GpStatus WINGDIPAPI GdipCloneBrush(GpBrush *brush, GpBrush **clone)
41 {
42     TRACE("(%p, %p)\n", brush, clone);
43
44     if(!brush || !clone)
45         return InvalidParameter;
46
47     switch(brush->bt){
48         case BrushTypeSolidColor:
49         {
50             GpSolidFill *fill;
51             *clone = GdipAlloc(sizeof(GpSolidFill));
52             if (!*clone) return OutOfMemory;
53
54             fill = (GpSolidFill*)*clone;
55
56             memcpy(*clone, brush, sizeof(GpSolidFill));
57
58             (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
59             fill->bmp = ARGB2BMP(fill->color);
60             break;
61         }
62         case BrushTypeHatchFill:
63             *clone = GdipAlloc(sizeof(GpHatch));
64             if (!*clone) return OutOfMemory;
65
66             memcpy(*clone, brush, sizeof(GpHatch));
67
68             (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
69             break;
70         case BrushTypePathGradient:{
71             GpPathGradient *src, *dest;
72             INT count;
73
74             *clone = GdipAlloc(sizeof(GpPathGradient));
75             if (!*clone) return OutOfMemory;
76
77             src = (GpPathGradient*) brush,
78             dest = (GpPathGradient*) *clone;
79             count = src->pathdata.Count;
80
81             memcpy(dest, src, sizeof(GpPathGradient));
82
83             dest->pathdata.Count = count;
84             dest->pathdata.Points = GdipAlloc(count * sizeof(PointF));
85             dest->pathdata.Types = GdipAlloc(count);
86
87             if(!dest->pathdata.Points || !dest->pathdata.Types){
88                 GdipFree(dest->pathdata.Points);
89                 GdipFree(dest->pathdata.Types);
90                 GdipFree(dest);
91                 return OutOfMemory;
92             }
93
94             memcpy(dest->pathdata.Points, src->pathdata.Points, count * sizeof(PointF));
95             memcpy(dest->pathdata.Types, src->pathdata.Types, count);
96
97             /* blending */
98             count = src->blendcount;
99             dest->blendcount = count;
100             dest->blendfac = GdipAlloc(count * sizeof(REAL));
101             dest->blendpos = GdipAlloc(count * sizeof(REAL));
102
103             if(!dest->blendfac || !dest->blendpos){
104                 GdipFree(dest->pathdata.Points);
105                 GdipFree(dest->pathdata.Types);
106                 GdipFree(dest->blendfac);
107                 GdipFree(dest->blendpos);
108                 GdipFree(dest);
109                 return OutOfMemory;
110             }
111
112             memcpy(dest->blendfac, src->blendfac, count * sizeof(REAL));
113             memcpy(dest->blendpos, src->blendpos, count * sizeof(REAL));
114
115             break;
116         }
117         case BrushTypeLinearGradient:{
118             GpLineGradient *dest, *src;
119             INT count, pcount;
120
121             dest = GdipAlloc(sizeof(GpLineGradient));
122             if(!dest)    return OutOfMemory;
123
124             src = (GpLineGradient*)brush;
125
126             memcpy(dest, src, sizeof(GpLineGradient));
127
128             dest->brush.gdibrush = CreateSolidBrush(dest->brush.lb.lbColor);
129
130             count = dest->blendcount;
131             dest->blendfac = GdipAlloc(count * sizeof(REAL));
132             dest->blendpos = GdipAlloc(count * sizeof(REAL));
133             pcount = dest->pblendcount;
134             if (pcount)
135             {
136                 dest->pblendcolor = GdipAlloc(pcount * sizeof(ARGB));
137                 dest->pblendpos = GdipAlloc(pcount * sizeof(REAL));
138             }
139
140             if (!dest->blendfac || !dest->blendpos ||
141                 (pcount && (!dest->pblendcolor || !dest->pblendpos)))
142             {
143                 GdipFree(dest->blendfac);
144                 GdipFree(dest->blendpos);
145                 GdipFree(dest->pblendcolor);
146                 GdipFree(dest->pblendpos);
147                 DeleteObject(dest->brush.gdibrush);
148                 GdipFree(dest);
149                 return OutOfMemory;
150             }
151
152             memcpy(dest->blendfac, src->blendfac, count * sizeof(REAL));
153             memcpy(dest->blendpos, src->blendpos, count * sizeof(REAL));
154
155             if (pcount)
156             {
157                 memcpy(dest->pblendcolor, src->pblendcolor, pcount * sizeof(ARGB));
158                 memcpy(dest->pblendpos, src->pblendpos, pcount * sizeof(REAL));
159             }
160
161             *clone = &dest->brush;
162             break;
163         }
164         case BrushTypeTextureFill:
165             *clone = GdipAlloc(sizeof(GpTexture));
166             if(!*clone)    return OutOfMemory;
167
168             memcpy(*clone, brush, sizeof(GpTexture));
169
170             (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
171             break;
172         default:
173             ERR("not implemented for brush type %d\n", brush->bt);
174             return NotImplemented;
175     }
176
177     TRACE("<-- %p\n", *clone);
178     return Ok;
179 }
180
181 static const char HatchBrushes[][8] = {
182     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HatchStyleHorizontal */
183     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HatchStyleVertical */
184     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HatchStyleForwardDiagonal */
185     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HatchStyleBackwardDiagonal */
186     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HatchStyleCross */
187     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HatchStyleDiagonalCross */
188     { 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80 }, /* HatchStyle05Percent */
189     { 0x00, 0x02, 0x00, 0x88, 0x00, 0x20, 0x00, 0x88 }, /* HatchStyle10Percent */
190     { 0x00, 0x22, 0x00, 0xcc, 0x00, 0x22, 0x00, 0xcc }, /* HatchStyle20Percent */
191     { 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc }, /* HatchStyle25Percent */
192     { 0x00, 0xcc, 0x04, 0xcc, 0x00, 0xcc, 0x40, 0xcc }, /* HatchStyle30Percent */
193     { 0x44, 0xcc, 0x22, 0xcc, 0x44, 0xcc, 0x22, 0xcc }, /* HatchStyle40Percent */
194     { 0x55, 0xcc, 0x55, 0xcc, 0x55, 0xcc, 0x55, 0xcc }, /* HatchStyle50Percent */
195     { 0x55, 0xcd, 0x55, 0xee, 0x55, 0xdc, 0x55, 0xee }, /* HatchStyle60Percent */
196     { 0x55, 0xdd, 0x55, 0xff, 0x55, 0xdd, 0x55, 0xff }, /* HatchStyle70Percent */
197     { 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff }, /* HatchStyle75Percent */
198     { 0x55, 0xff, 0x59, 0xff, 0x55, 0xff, 0x99, 0xff }, /* HatchStyle80Percent */
199     { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xfd, 0xff }, /* HatchStyle90Percent */
200     { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 }, /* HatchStyleLightDownwardDiagonal */
201     { 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11 }, /* HatchStyleLightUpwardDiagonal */
202     { 0x99, 0x33, 0x66, 0xcc, 0x99, 0x33, 0x66, 0xcc }, /* HatchStyleDarkDownwardDiagonal */
203     { 0xcc, 0x66, 0x33, 0x99, 0xcc, 0x66, 0x33, 0x99 }, /* HatchStyleDarkUpwardDiagonal */
204     { 0xc1, 0x83, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0 }, /* HatchStyleWideDownwardDiagonal */
205     { 0xe0, 0x70, 0x38, 0x1c, 0x0e, 0x07, 0x83, 0xc1 }, /* HatchStyleWideUpwardDiagonal */
206     { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 }, /* HatchStyleLightVertical */
207     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff }, /* HatchStyleLightHorizontal */
208     { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, /* HatchStyleNarrowVertical */
209     { 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff }, /* HatchStyleNarrowHorizontal */
210     { 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc }, /* HatchStyleDarkVertical */
211     { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff }, /* HatchStyleDarkHorizontal */
212 };
213
214 /******************************************************************************
215  * GdipCreateHatchBrush [GDIPLUS.@]
216  */
217 GpStatus WINGDIPAPI GdipCreateHatchBrush(HatchStyle hatchstyle, ARGB forecol, ARGB backcol, GpHatch **brush)
218 {
219     COLORREF fgcol = ARGB2COLORREF(forecol);
220     GpStatus stat = Ok;
221
222     TRACE("(%d, %d, %d, %p)\n", hatchstyle, forecol, backcol, brush);
223
224     if(!brush)  return InvalidParameter;
225
226     *brush = GdipAlloc(sizeof(GpHatch));
227     if (!*brush) return OutOfMemory;
228
229     if (hatchstyle < sizeof(HatchBrushes) / sizeof(HatchBrushes[0]))
230     {
231         HBITMAP hbmp;
232         HDC hdc;
233         BITMAPINFOHEADER bmih;
234         DWORD* bits;
235         int x, y;
236
237         hdc = CreateCompatibleDC(0);
238
239         if (hdc)
240         {
241             bmih.biSize = sizeof(bmih);
242             bmih.biWidth = 8;
243             bmih.biHeight = 8;
244             bmih.biPlanes = 1;
245             bmih.biBitCount = 32;
246             bmih.biCompression = BI_RGB;
247             bmih.biSizeImage = 0;
248
249             hbmp = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
250
251             if (hbmp)
252             {
253                 for (y=0; y<8; y++)
254                     for (x=0; x<8; x++)
255                         if ((HatchBrushes[hatchstyle][y] & (0x80 >> x)) != 0)
256                             bits[y*8+x] = forecol;
257                         else
258                             bits[y*8+x] = backcol;
259             }
260             else
261                 stat = GenericError;
262
263             DeleteDC(hdc);
264         }
265         else
266             stat = GenericError;
267
268         if (stat == Ok)
269         {
270             (*brush)->brush.lb.lbStyle = BS_PATTERN;
271             (*brush)->brush.lb.lbColor = 0;
272             (*brush)->brush.lb.lbHatch = (ULONG_PTR)hbmp;
273             (*brush)->brush.gdibrush = CreateBrushIndirect(&(*brush)->brush.lb);
274
275             DeleteObject(hbmp);
276         }
277     }
278     else
279     {
280         FIXME("Unimplemented hatch style %d\n", hatchstyle);
281
282         (*brush)->brush.lb.lbStyle = BS_SOLID;
283         (*brush)->brush.lb.lbColor = fgcol;
284         (*brush)->brush.lb.lbHatch = 0;
285         (*brush)->brush.gdibrush = CreateBrushIndirect(&(*brush)->brush.lb);
286     }
287
288     if (stat == Ok)
289     {
290         (*brush)->brush.bt = BrushTypeHatchFill;
291         (*brush)->forecol = forecol;
292         (*brush)->backcol = backcol;
293         (*brush)->hatchstyle = hatchstyle;
294         TRACE("<-- %p\n", *brush);
295     }
296     else
297     {
298         GdipFree(*brush);
299         *brush = NULL;
300     }
301
302     return stat;
303 }
304
305 /******************************************************************************
306  * GdipCreateLineBrush [GDIPLUS.@]
307  */
308 GpStatus WINGDIPAPI GdipCreateLineBrush(GDIPCONST GpPointF* startpoint,
309     GDIPCONST GpPointF* endpoint, ARGB startcolor, ARGB endcolor,
310     GpWrapMode wrap, GpLineGradient **line)
311 {
312     COLORREF col = ARGB2COLORREF(startcolor);
313
314     TRACE("(%p, %p, %x, %x, %d, %p)\n", startpoint, endpoint,
315           startcolor, endcolor, wrap, line);
316
317     if(!line || !startpoint || !endpoint || wrap == WrapModeClamp)
318         return InvalidParameter;
319
320     *line = GdipAlloc(sizeof(GpLineGradient));
321     if(!*line)  return OutOfMemory;
322
323     (*line)->brush.lb.lbStyle = BS_SOLID;
324     (*line)->brush.lb.lbColor = col;
325     (*line)->brush.lb.lbHatch = 0;
326     (*line)->brush.gdibrush = CreateSolidBrush(col);
327     (*line)->brush.bt = BrushTypeLinearGradient;
328
329     (*line)->startpoint.X = startpoint->X;
330     (*line)->startpoint.Y = startpoint->Y;
331     (*line)->endpoint.X = endpoint->X;
332     (*line)->endpoint.Y = endpoint->Y;
333     (*line)->startcolor = startcolor;
334     (*line)->endcolor = endcolor;
335     (*line)->wrap = wrap;
336     (*line)->gamma = FALSE;
337
338     (*line)->rect.X = (startpoint->X < endpoint->X ? startpoint->X: endpoint->X);
339     (*line)->rect.Y = (startpoint->Y < endpoint->Y ? startpoint->Y: endpoint->Y);
340     (*line)->rect.Width  = fabs(startpoint->X - endpoint->X);
341     (*line)->rect.Height = fabs(startpoint->Y - endpoint->Y);
342
343     if ((*line)->rect.Width == 0)
344     {
345         (*line)->rect.X -= (*line)->rect.Height / 2.0f;
346         (*line)->rect.Width = (*line)->rect.Height;
347     }
348     else if ((*line)->rect.Height == 0)
349     {
350         (*line)->rect.Y -= (*line)->rect.Width / 2.0f;
351         (*line)->rect.Height = (*line)->rect.Width;
352     }
353
354     (*line)->blendcount = 1;
355     (*line)->blendfac = GdipAlloc(sizeof(REAL));
356     (*line)->blendpos = GdipAlloc(sizeof(REAL));
357
358     if (!(*line)->blendfac || !(*line)->blendpos)
359     {
360         GdipFree((*line)->blendfac);
361         GdipFree((*line)->blendpos);
362         DeleteObject((*line)->brush.gdibrush);
363         GdipFree(*line);
364         *line = NULL;
365         return OutOfMemory;
366     }
367
368     (*line)->blendfac[0] = 1.0f;
369     (*line)->blendpos[0] = 1.0f;
370
371     (*line)->pblendcolor = NULL;
372     (*line)->pblendpos = NULL;
373     (*line)->pblendcount = 0;
374
375     TRACE("<-- %p\n", *line);
376
377     return Ok;
378 }
379
380 GpStatus WINGDIPAPI GdipCreateLineBrushI(GDIPCONST GpPoint* startpoint,
381     GDIPCONST GpPoint* endpoint, ARGB startcolor, ARGB endcolor,
382     GpWrapMode wrap, GpLineGradient **line)
383 {
384     GpPointF stF;
385     GpPointF endF;
386
387     TRACE("(%p, %p, %x, %x, %d, %p)\n", startpoint, endpoint,
388           startcolor, endcolor, wrap, line);
389
390     if(!startpoint || !endpoint)
391         return InvalidParameter;
392
393     stF.X  = (REAL)startpoint->X;
394     stF.Y  = (REAL)startpoint->Y;
395     endF.X = (REAL)endpoint->X;
396     endF.X = (REAL)endpoint->Y;
397
398     return GdipCreateLineBrush(&stF, &endF, startcolor, endcolor, wrap, line);
399 }
400
401 GpStatus WINGDIPAPI GdipCreateLineBrushFromRect(GDIPCONST GpRectF* rect,
402     ARGB startcolor, ARGB endcolor, LinearGradientMode mode, GpWrapMode wrap,
403     GpLineGradient **line)
404 {
405     GpPointF start, end;
406     GpStatus stat;
407
408     TRACE("(%p, %x, %x, %d, %d, %p)\n", rect, startcolor, endcolor, mode,
409           wrap, line);
410
411     if(!line || !rect)
412         return InvalidParameter;
413
414     switch (mode)
415     {
416     case LinearGradientModeHorizontal:
417         start.X = rect->X;
418         start.Y = rect->Y;
419         end.X = rect->X + rect->Width;
420         end.Y = rect->Y;
421         break;
422     case LinearGradientModeVertical:
423         start.X = rect->X;
424         start.Y = rect->Y;
425         end.X = rect->X;
426         end.Y = rect->Y + rect->Height;
427         break;
428     case LinearGradientModeForwardDiagonal:
429         start.X = rect->X;
430         start.Y = rect->Y;
431         end.X = rect->X + rect->Width;
432         end.Y = rect->Y + rect->Height;
433         break;
434     case LinearGradientModeBackwardDiagonal:
435         start.X = rect->X + rect->Width;
436         start.Y = rect->Y;
437         end.X = rect->X;
438         end.Y = rect->Y + rect->Height;
439         break;
440     default:
441         return InvalidParameter;
442     }
443
444     stat = GdipCreateLineBrush(&start, &end, startcolor, endcolor, wrap, line);
445
446     if (stat == Ok)
447         (*line)->rect = *rect;
448
449     return stat;
450 }
451
452 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectI(GDIPCONST GpRect* rect,
453     ARGB startcolor, ARGB endcolor, LinearGradientMode mode, GpWrapMode wrap,
454     GpLineGradient **line)
455 {
456     GpRectF rectF;
457
458     TRACE("(%p, %x, %x, %d, %d, %p)\n", rect, startcolor, endcolor, mode,
459           wrap, line);
460
461     rectF.X      = (REAL) rect->X;
462     rectF.Y      = (REAL) rect->Y;
463     rectF.Width  = (REAL) rect->Width;
464     rectF.Height = (REAL) rect->Height;
465
466     return GdipCreateLineBrushFromRect(&rectF, startcolor, endcolor, mode, wrap, line);
467 }
468
469 /******************************************************************************
470  * GdipCreateLineBrushFromRectWithAngle [GDIPLUS.@]
471  *
472  * FIXME: angle value completely ignored. Don't know how to use it since native
473  *        always set Brush rectangle to rect (independetly of this angle).
474  *        Maybe it's used only on drawing.
475  */
476 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectWithAngle(GDIPCONST GpRectF* rect,
477     ARGB startcolor, ARGB endcolor, REAL angle, BOOL isAngleScalable, GpWrapMode wrap,
478     GpLineGradient **line)
479 {
480     TRACE("(%p, %x, %x, %.2f, %d, %d, %p)\n", rect, startcolor, endcolor, angle, isAngleScalable,
481           wrap, line);
482
483     return GdipCreateLineBrushFromRect(rect, startcolor, endcolor, LinearGradientModeForwardDiagonal,
484                                        wrap, line);
485 }
486
487 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectWithAngleI(GDIPCONST GpRect* rect,
488     ARGB startcolor, ARGB endcolor, REAL angle, BOOL isAngleScalable, GpWrapMode wrap,
489     GpLineGradient **line)
490 {
491     TRACE("(%p, %x, %x, %.2f, %d, %d, %p)\n", rect, startcolor, endcolor, angle, isAngleScalable,
492           wrap, line);
493
494     return GdipCreateLineBrushFromRectI(rect, startcolor, endcolor, LinearGradientModeForwardDiagonal,
495                                         wrap, line);
496 }
497
498 GpStatus WINGDIPAPI GdipCreatePathGradient(GDIPCONST GpPointF* points,
499     INT count, GpWrapMode wrap, GpPathGradient **grad)
500 {
501     COLORREF col = ARGB2COLORREF(0xffffffff);
502
503     TRACE("(%p, %d, %d, %p)\n", points, count, wrap, grad);
504
505     if(!points || !grad)
506         return InvalidParameter;
507
508     if(count <= 0)
509         return OutOfMemory;
510
511     *grad = GdipAlloc(sizeof(GpPathGradient));
512     if (!*grad) return OutOfMemory;
513
514     (*grad)->blendfac = GdipAlloc(sizeof(REAL));
515     if(!(*grad)->blendfac){
516         GdipFree(*grad);
517         return OutOfMemory;
518     }
519     (*grad)->blendfac[0] = 1.0;
520     (*grad)->blendpos    = NULL;
521     (*grad)->blendcount  = 1;
522
523     (*grad)->pathdata.Count = count;
524     (*grad)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
525     (*grad)->pathdata.Types = GdipAlloc(count);
526
527     if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
528         GdipFree((*grad)->pathdata.Points);
529         GdipFree((*grad)->pathdata.Types);
530         GdipFree(*grad);
531         return OutOfMemory;
532     }
533
534     memcpy((*grad)->pathdata.Points, points, count * sizeof(PointF));
535     memset((*grad)->pathdata.Types, PathPointTypeLine, count);
536
537     (*grad)->brush.lb.lbStyle = BS_SOLID;
538     (*grad)->brush.lb.lbColor = col;
539     (*grad)->brush.lb.lbHatch = 0;
540
541     (*grad)->brush.gdibrush = CreateSolidBrush(col);
542     (*grad)->brush.bt = BrushTypePathGradient;
543     (*grad)->centercolor = 0xffffffff;
544     (*grad)->wrap = wrap;
545     (*grad)->gamma = FALSE;
546     (*grad)->center.X = 0.0;
547     (*grad)->center.Y = 0.0;
548     (*grad)->focus.X = 0.0;
549     (*grad)->focus.Y = 0.0;
550
551     TRACE("<-- %p\n", *grad);
552
553     return Ok;
554 }
555
556 GpStatus WINGDIPAPI GdipCreatePathGradientI(GDIPCONST GpPoint* points,
557     INT count, GpWrapMode wrap, GpPathGradient **grad)
558 {
559     GpPointF *pointsF;
560     GpStatus ret;
561     INT i;
562
563     TRACE("(%p, %d, %d, %p)\n", points, count, wrap, grad);
564
565     if(!points || !grad)
566         return InvalidParameter;
567
568     if(count <= 0)
569         return OutOfMemory;
570
571     pointsF = GdipAlloc(sizeof(GpPointF) * count);
572     if(!pointsF)
573         return OutOfMemory;
574
575     for(i = 0; i < count; i++){
576         pointsF[i].X = (REAL)points[i].X;
577         pointsF[i].Y = (REAL)points[i].Y;
578     }
579
580     ret = GdipCreatePathGradient(pointsF, count, wrap, grad);
581     GdipFree(pointsF);
582
583     return ret;
584 }
585
586 /******************************************************************************
587  * GdipCreatePathGradientFromPath [GDIPLUS.@]
588  *
589  * FIXME: path gradient brushes not truly supported (drawn as solid brushes)
590  */
591 GpStatus WINGDIPAPI GdipCreatePathGradientFromPath(GDIPCONST GpPath* path,
592     GpPathGradient **grad)
593 {
594     COLORREF col = ARGB2COLORREF(0xffffffff);
595
596     TRACE("(%p, %p)\n", path, grad);
597
598     if(!path || !grad)
599         return InvalidParameter;
600
601     *grad = GdipAlloc(sizeof(GpPathGradient));
602     if (!*grad) return OutOfMemory;
603
604     (*grad)->blendfac = GdipAlloc(sizeof(REAL));
605     if(!(*grad)->blendfac){
606         GdipFree(*grad);
607         return OutOfMemory;
608     }
609     (*grad)->blendfac[0] = 1.0;
610     (*grad)->blendpos    = NULL;
611     (*grad)->blendcount  = 1;
612
613     (*grad)->pathdata.Count = path->pathdata.Count;
614     (*grad)->pathdata.Points = GdipAlloc(path->pathdata.Count * sizeof(PointF));
615     (*grad)->pathdata.Types = GdipAlloc(path->pathdata.Count);
616
617     if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
618         GdipFree((*grad)->pathdata.Points);
619         GdipFree((*grad)->pathdata.Types);
620         GdipFree(*grad);
621         return OutOfMemory;
622     }
623
624     memcpy((*grad)->pathdata.Points, path->pathdata.Points,
625            path->pathdata.Count * sizeof(PointF));
626     memcpy((*grad)->pathdata.Types, path->pathdata.Types, path->pathdata.Count);
627
628     (*grad)->brush.lb.lbStyle = BS_SOLID;
629     (*grad)->brush.lb.lbColor = col;
630     (*grad)->brush.lb.lbHatch = 0;
631
632     (*grad)->brush.gdibrush = CreateSolidBrush(col);
633     (*grad)->brush.bt = BrushTypePathGradient;
634     (*grad)->centercolor = 0xffffffff;
635     (*grad)->wrap = WrapModeClamp;
636     (*grad)->gamma = FALSE;
637     /* FIXME: this should be set to the "centroid" of the path by default */
638     (*grad)->center.X = 0.0;
639     (*grad)->center.Y = 0.0;
640     (*grad)->focus.X = 0.0;
641     (*grad)->focus.Y = 0.0;
642
643     TRACE("<-- %p\n", *grad);
644
645     return Ok;
646 }
647
648 /******************************************************************************
649  * GdipCreateSolidFill [GDIPLUS.@]
650  */
651 GpStatus WINGDIPAPI GdipCreateSolidFill(ARGB color, GpSolidFill **sf)
652 {
653     COLORREF col = ARGB2COLORREF(color);
654
655     TRACE("(%x, %p)\n", color, sf);
656
657     if(!sf)  return InvalidParameter;
658
659     *sf = GdipAlloc(sizeof(GpSolidFill));
660     if (!*sf) return OutOfMemory;
661
662     (*sf)->brush.lb.lbStyle = BS_SOLID;
663     (*sf)->brush.lb.lbColor = col;
664     (*sf)->brush.lb.lbHatch = 0;
665
666     (*sf)->brush.gdibrush = CreateSolidBrush(col);
667     (*sf)->brush.bt = BrushTypeSolidColor;
668     (*sf)->color = color;
669     (*sf)->bmp = ARGB2BMP(color);
670
671     TRACE("<-- %p\n", *sf);
672
673     return Ok;
674 }
675
676 /******************************************************************************
677  * GdipCreateTexture [GDIPLUS.@]
678  *
679  * PARAMS
680  *  image       [I] image to use
681  *  wrapmode    [I] optional
682  *  texture     [O] pointer to the resulting texturebrush
683  *
684  * RETURNS
685  *  SUCCESS: Ok
686  *  FAILURE: element of GpStatus
687  */
688 GpStatus WINGDIPAPI GdipCreateTexture(GpImage *image, GpWrapMode wrapmode,
689         GpTexture **texture)
690 {
691     UINT width, height;
692     GpImageAttributes attributes;
693     GpStatus stat;
694
695     TRACE("%p, %d %p\n", image, wrapmode, texture);
696
697     if (!(image && texture))
698         return InvalidParameter;
699
700     stat = GdipGetImageWidth(image, &width);
701     if (stat != Ok) return stat;
702     stat = GdipGetImageHeight(image, &height);
703     if (stat != Ok) return stat;
704     attributes.wrap = wrapmode;
705
706     return GdipCreateTextureIA(image, &attributes, 0, 0, width, height,
707             texture);
708 }
709
710 /******************************************************************************
711  * GdipCreateTexture2 [GDIPLUS.@]
712  */
713 GpStatus WINGDIPAPI GdipCreateTexture2(GpImage *image, GpWrapMode wrapmode,
714         REAL x, REAL y, REAL width, REAL height, GpTexture **texture)
715 {
716     GpImageAttributes attributes;
717
718     TRACE("%p %d %f %f %f %f %p\n", image, wrapmode,
719             x, y, width, height, texture);
720
721     attributes.wrap = wrapmode;
722     return GdipCreateTextureIA(image, &attributes, x, y, width, height,
723             texture);
724 }
725
726 /******************************************************************************
727  * GdipCreateTextureIA [GDIPLUS.@]
728  *
729  * FIXME: imageattr ignored
730  */
731 GpStatus WINGDIPAPI GdipCreateTextureIA(GpImage *image,
732     GDIPCONST GpImageAttributes *imageattr, REAL x, REAL y, REAL width,
733     REAL height, GpTexture **texture)
734 {
735     HDC hdc;
736     HBITMAP hbm, old = NULL;
737     BITMAPINFO *pbmi;
738     BITMAPINFOHEADER *bmih;
739     INT n_x, n_y, n_width, n_height, abs_height, stride, image_stride, i, bytespp;
740     BOOL bm_is_selected;
741     BYTE *dibits, *buff, *textbits;
742     GpStatus status;
743
744     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %p)\n", image, imageattr, x, y, width, height,
745            texture);
746
747     if(!image || !texture || x < 0.0 || y < 0.0 || width < 0.0 || height < 0.0)
748         return InvalidParameter;
749
750     if(image->type != ImageTypeBitmap){
751         FIXME("not implemented for image type %d\n", image->type);
752         return NotImplemented;
753     }
754
755     n_x = roundr(x);
756     n_y = roundr(y);
757     n_width = roundr(width);
758     n_height = roundr(height);
759
760     if(n_x + n_width > ((GpBitmap*)image)->width ||
761        n_y + n_height > ((GpBitmap*)image)->height)
762         return InvalidParameter;
763
764     hbm = ((GpBitmap*)image)->hbitmap;
765     if(!hbm)   return GenericError;
766     hdc = ((GpBitmap*)image)->hdc;
767     bm_is_selected = (hdc != 0);
768
769     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
770     if (!pbmi)
771         return OutOfMemory;
772     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
773     pbmi->bmiHeader.biBitCount = 0;
774
775     if(!bm_is_selected){
776         hdc = CreateCompatibleDC(0);
777         old = SelectObject(hdc, hbm);
778     }
779
780     /* fill out bmi */
781     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
782
783     bytespp = pbmi->bmiHeader.biBitCount / 8;
784     abs_height = abs(pbmi->bmiHeader.biHeight);
785
786     if(n_x > pbmi->bmiHeader.biWidth || n_x + n_width > pbmi->bmiHeader.biWidth ||
787        n_y > abs_height || n_y + n_height > abs_height){
788         GdipFree(pbmi);
789         return InvalidParameter;
790     }
791
792     dibits = GdipAlloc(pbmi->bmiHeader.biSizeImage);
793
794     if(dibits)  /* this is not a good place to error out */
795         GetDIBits(hdc, hbm, 0, abs_height, dibits, pbmi, DIB_RGB_COLORS);
796
797     if(!bm_is_selected){
798         SelectObject(hdc, old);
799         DeleteDC(hdc);
800     }
801
802     if(!dibits){
803         GdipFree(pbmi);
804         return OutOfMemory;
805     }
806
807     image_stride = (pbmi->bmiHeader.biWidth * bytespp + 3) & ~3;
808     stride = (n_width * bytespp + 3) & ~3;
809     buff = GdipAlloc(sizeof(BITMAPINFOHEADER) + stride * n_height);
810     if(!buff){
811         GdipFree(pbmi);
812         GdipFree(dibits);
813         return OutOfMemory;
814     }
815
816     bmih = (BITMAPINFOHEADER*)buff;
817     textbits = (BYTE*) (bmih + 1);
818     bmih->biSize = sizeof(BITMAPINFOHEADER);
819     bmih->biWidth = n_width;
820     bmih->biHeight = n_height;
821     bmih->biCompression = BI_RGB;
822     bmih->biSizeImage = stride * n_height;
823     bmih->biBitCount = pbmi->bmiHeader.biBitCount;
824     bmih->biClrUsed = 0;
825     bmih->biPlanes = 1;
826
827     /* image is flipped */
828     if(pbmi->bmiHeader.biHeight > 0){
829         dibits += image_stride * (pbmi->bmiHeader.biHeight - 1);
830         image_stride *= -1;
831         textbits += stride * (n_height - 1);
832         stride *= -1;
833     }
834
835     GdipFree(pbmi);
836
837     for(i = 0; i < n_height; i++)
838         memcpy(&textbits[i * stride],
839                &dibits[n_x * bytespp + (n_y + i) * image_stride],
840                abs(stride));
841
842     *texture = GdipAlloc(sizeof(GpTexture));
843     if (!*texture){
844         GdipFree(dibits);
845         GdipFree(buff);
846         return OutOfMemory;
847     }
848
849     if((status = GdipCreateMatrix(&(*texture)->transform)) != Ok){
850         GdipFree(*texture);
851         GdipFree(dibits);
852         GdipFree(buff);
853         return status;
854     }
855
856     (*texture)->brush.lb.lbStyle = BS_DIBPATTERNPT;
857     (*texture)->brush.lb.lbColor = DIB_RGB_COLORS;
858     (*texture)->brush.lb.lbHatch = (ULONG_PTR)buff;
859
860     (*texture)->brush.gdibrush = CreateBrushIndirect(&(*texture)->brush.lb);
861     (*texture)->brush.bt = BrushTypeTextureFill;
862     (*texture)->wrap = imageattr->wrap;
863
864     GdipFree(dibits);
865     GdipFree(buff);
866
867     TRACE("<-- %p\n", *texture);
868
869     return Ok;
870 }
871
872 /******************************************************************************
873  * GdipCreateTextureIAI [GDIPLUS.@]
874  */
875 GpStatus WINGDIPAPI GdipCreateTextureIAI(GpImage *image, GDIPCONST GpImageAttributes *imageattr,
876     INT x, INT y, INT width, INT height, GpTexture **texture)
877 {
878     TRACE("(%p, %p, %d, %d, %d, %d, %p)\n", image, imageattr, x, y, width, height,
879            texture);
880
881     return GdipCreateTextureIA(image,imageattr,(REAL)x,(REAL)y,(REAL)width,(REAL)height,texture);
882 }
883
884 GpStatus WINGDIPAPI GdipCreateTexture2I(GpImage *image, GpWrapMode wrapmode,
885         INT x, INT y, INT width, INT height, GpTexture **texture)
886 {
887     GpImageAttributes imageattr;
888
889     TRACE("%p %d %d %d %d %d %p\n", image, wrapmode, x, y, width, height,
890             texture);
891
892     imageattr.wrap = wrapmode;
893
894     return GdipCreateTextureIA(image, &imageattr, x, y, width, height, texture);
895 }
896
897 GpStatus WINGDIPAPI GdipGetBrushType(GpBrush *brush, GpBrushType *type)
898 {
899     TRACE("(%p, %p)\n", brush, type);
900
901     if(!brush || !type)  return InvalidParameter;
902
903     *type = brush->bt;
904
905     return Ok;
906 }
907
908 GpStatus WINGDIPAPI GdipGetHatchBackgroundColor(GpHatch *brush, ARGB *backcol)
909 {
910     TRACE("(%p, %p)\n", brush, backcol);
911
912     if(!brush || !backcol)  return InvalidParameter;
913
914     *backcol = brush->backcol;
915
916     return Ok;
917 }
918
919 GpStatus WINGDIPAPI GdipGetHatchForegroundColor(GpHatch *brush, ARGB *forecol)
920 {
921     TRACE("(%p, %p)\n", brush, forecol);
922
923     if(!brush || !forecol)  return InvalidParameter;
924
925     *forecol = brush->forecol;
926
927     return Ok;
928 }
929
930 GpStatus WINGDIPAPI GdipGetHatchStyle(GpHatch *brush, HatchStyle *hatchstyle)
931 {
932     TRACE("(%p, %p)\n", brush, hatchstyle);
933
934     if(!brush || !hatchstyle)  return InvalidParameter;
935
936     *hatchstyle = brush->hatchstyle;
937
938     return Ok;
939 }
940
941 GpStatus WINGDIPAPI GdipDeleteBrush(GpBrush *brush)
942 {
943     TRACE("(%p)\n", brush);
944
945     if(!brush)  return InvalidParameter;
946
947     switch(brush->bt)
948     {
949         case BrushTypePathGradient:
950             GdipFree(((GpPathGradient*) brush)->pathdata.Points);
951             GdipFree(((GpPathGradient*) brush)->pathdata.Types);
952             GdipFree(((GpPathGradient*) brush)->blendfac);
953             GdipFree(((GpPathGradient*) brush)->blendpos);
954             break;
955         case BrushTypeSolidColor:
956             if (((GpSolidFill*)brush)->bmp)
957                 DeleteObject(((GpSolidFill*)brush)->bmp);
958             break;
959         case BrushTypeLinearGradient:
960             GdipFree(((GpLineGradient*)brush)->blendfac);
961             GdipFree(((GpLineGradient*)brush)->blendpos);
962             GdipFree(((GpLineGradient*)brush)->pblendcolor);
963             GdipFree(((GpLineGradient*)brush)->pblendpos);
964             break;
965         case BrushTypeTextureFill:
966             GdipDeleteMatrix(((GpTexture*)brush)->transform);
967             break;
968         default:
969             break;
970     }
971
972     DeleteObject(brush->gdibrush);
973     GdipFree(brush);
974
975     return Ok;
976 }
977
978 GpStatus WINGDIPAPI GdipGetLineGammaCorrection(GpLineGradient *line,
979     BOOL *usinggamma)
980 {
981     TRACE("(%p, %p)\n", line, usinggamma);
982
983     if(!line || !usinggamma)
984         return InvalidParameter;
985
986     *usinggamma = line->gamma;
987
988     return Ok;
989 }
990
991 GpStatus WINGDIPAPI GdipGetLineWrapMode(GpLineGradient *brush, GpWrapMode *wrapmode)
992 {
993     TRACE("(%p, %p)\n", brush, wrapmode);
994
995     if(!brush || !wrapmode)
996         return InvalidParameter;
997
998     *wrapmode = brush->wrap;
999
1000     return Ok;
1001 }
1002
1003 GpStatus WINGDIPAPI GdipGetPathGradientBlend(GpPathGradient *brush, REAL *blend,
1004     REAL *positions, INT count)
1005 {
1006     TRACE("(%p, %p, %p, %d)\n", brush, blend, positions, count);
1007
1008     if(!brush || !blend || !positions || count <= 0)
1009         return InvalidParameter;
1010
1011     if(count < brush->blendcount)
1012         return InsufficientBuffer;
1013
1014     memcpy(blend, brush->blendfac, count*sizeof(REAL));
1015     if(brush->blendcount > 1){
1016         memcpy(positions, brush->blendpos, count*sizeof(REAL));
1017     }
1018
1019     return Ok;
1020 }
1021
1022 GpStatus WINGDIPAPI GdipGetPathGradientBlendCount(GpPathGradient *brush, INT *count)
1023 {
1024     TRACE("(%p, %p)\n", brush, count);
1025
1026     if(!brush || !count)
1027         return InvalidParameter;
1028
1029     *count = brush->blendcount;
1030
1031     return Ok;
1032 }
1033
1034 GpStatus WINGDIPAPI GdipGetPathGradientCenterPoint(GpPathGradient *grad,
1035     GpPointF *point)
1036 {
1037     TRACE("(%p, %p)\n", grad, point);
1038
1039     if(!grad || !point)
1040         return InvalidParameter;
1041
1042     point->X = grad->center.X;
1043     point->Y = grad->center.Y;
1044
1045     return Ok;
1046 }
1047
1048 GpStatus WINGDIPAPI GdipGetPathGradientCenterPointI(GpPathGradient *grad,
1049     GpPoint *point)
1050 {
1051     GpStatus ret;
1052     GpPointF ptf;
1053
1054     TRACE("(%p, %p)\n", grad, point);
1055
1056     if(!point)
1057         return InvalidParameter;
1058
1059     ret = GdipGetPathGradientCenterPoint(grad,&ptf);
1060
1061     if(ret == Ok){
1062         point->X = roundr(ptf.X);
1063         point->Y = roundr(ptf.Y);
1064     }
1065
1066     return ret;
1067 }
1068
1069 GpStatus WINGDIPAPI GdipGetPathGradientFocusScales(GpPathGradient *grad,
1070     REAL *x, REAL *y)
1071 {
1072     TRACE("(%p, %p, %p)\n", grad, x, y);
1073
1074     if(!grad || !x || !y)
1075         return InvalidParameter;
1076
1077     *x = grad->focus.X;
1078     *y = grad->focus.Y;
1079
1080     return Ok;
1081 }
1082
1083 GpStatus WINGDIPAPI GdipGetPathGradientGammaCorrection(GpPathGradient *grad,
1084     BOOL *gamma)
1085 {
1086     TRACE("(%p, %p)\n", grad, gamma);
1087
1088     if(!grad || !gamma)
1089         return InvalidParameter;
1090
1091     *gamma = grad->gamma;
1092
1093     return Ok;
1094 }
1095
1096 GpStatus WINGDIPAPI GdipGetPathGradientPointCount(GpPathGradient *grad,
1097     INT *count)
1098 {
1099     TRACE("(%p, %p)\n", grad, count);
1100
1101     if(!grad || !count)
1102         return InvalidParameter;
1103
1104     *count = grad->pathdata.Count;
1105
1106     return Ok;
1107 }
1108
1109 GpStatus WINGDIPAPI GdipGetPathGradientRect(GpPathGradient *brush, GpRectF *rect)
1110 {
1111     GpRectF r;
1112     GpPath* path;
1113     GpStatus stat;
1114
1115     TRACE("(%p, %p)\n", brush, rect);
1116
1117     if(!brush || !rect)
1118         return InvalidParameter;
1119
1120     stat = GdipCreatePath2(brush->pathdata.Points, brush->pathdata.Types,
1121                            brush->pathdata.Count, FillModeAlternate, &path);
1122     if(stat != Ok)  return stat;
1123
1124     stat = GdipGetPathWorldBounds(path, &r, NULL, NULL);
1125     if(stat != Ok){
1126         GdipDeletePath(path);
1127         return stat;
1128     }
1129
1130     memcpy(rect, &r, sizeof(GpRectF));
1131
1132     GdipDeletePath(path);
1133
1134     return Ok;
1135 }
1136
1137 GpStatus WINGDIPAPI GdipGetPathGradientRectI(GpPathGradient *brush, GpRect *rect)
1138 {
1139     GpRectF rectf;
1140     GpStatus stat;
1141
1142     TRACE("(%p, %p)\n", brush, rect);
1143
1144     if(!brush || !rect)
1145         return InvalidParameter;
1146
1147     stat = GdipGetPathGradientRect(brush, &rectf);
1148     if(stat != Ok)  return stat;
1149
1150     rect->X = roundr(rectf.X);
1151     rect->Y = roundr(rectf.Y);
1152     rect->Width  = roundr(rectf.Width);
1153     rect->Height = roundr(rectf.Height);
1154
1155     return Ok;
1156 }
1157
1158 GpStatus WINGDIPAPI GdipGetPathGradientSurroundColorsWithCount(GpPathGradient
1159     *grad, ARGB *argb, INT *count)
1160 {
1161     static int calls;
1162
1163     if(!grad || !argb || !count || (*count < grad->pathdata.Count))
1164         return InvalidParameter;
1165
1166     if(!(calls++))
1167         FIXME("not implemented\n");
1168
1169     return NotImplemented;
1170 }
1171
1172 GpStatus WINGDIPAPI GdipGetPathGradientWrapMode(GpPathGradient *brush,
1173     GpWrapMode *wrapmode)
1174 {
1175     TRACE("(%p, %p)\n", brush, wrapmode);
1176
1177     if(!brush || !wrapmode)
1178         return InvalidParameter;
1179
1180     *wrapmode = brush->wrap;
1181
1182     return Ok;
1183 }
1184
1185 GpStatus WINGDIPAPI GdipGetSolidFillColor(GpSolidFill *sf, ARGB *argb)
1186 {
1187     TRACE("(%p, %p)\n", sf, argb);
1188
1189     if(!sf || !argb)
1190         return InvalidParameter;
1191
1192     *argb = sf->color;
1193
1194     return Ok;
1195 }
1196
1197 /******************************************************************************
1198  * GdipGetTextureTransform [GDIPLUS.@]
1199  */
1200 GpStatus WINGDIPAPI GdipGetTextureTransform(GpTexture *brush, GpMatrix *matrix)
1201 {
1202     TRACE("(%p, %p)\n", brush, matrix);
1203
1204     if(!brush || !matrix)
1205         return InvalidParameter;
1206
1207     memcpy(matrix, brush->transform, sizeof(GpMatrix));
1208
1209     return Ok;
1210 }
1211
1212 /******************************************************************************
1213  * GdipGetTextureWrapMode [GDIPLUS.@]
1214  */
1215 GpStatus WINGDIPAPI GdipGetTextureWrapMode(GpTexture *brush, GpWrapMode *wrapmode)
1216 {
1217     TRACE("(%p, %p)\n", brush, wrapmode);
1218
1219     if(!brush || !wrapmode)
1220         return InvalidParameter;
1221
1222     *wrapmode = brush->wrap;
1223
1224     return Ok;
1225 }
1226
1227 /******************************************************************************
1228  * GdipMultiplyTextureTransform [GDIPLUS.@]
1229  */
1230 GpStatus WINGDIPAPI GdipMultiplyTextureTransform(GpTexture* brush,
1231     GDIPCONST GpMatrix *matrix, GpMatrixOrder order)
1232 {
1233     TRACE("(%p, %p, %d)\n", brush, matrix, order);
1234
1235     if(!brush || !matrix)
1236         return InvalidParameter;
1237
1238     return GdipMultiplyMatrix(brush->transform, matrix, order);
1239 }
1240
1241 /******************************************************************************
1242  * GdipResetTextureTransform [GDIPLUS.@]
1243  */
1244 GpStatus WINGDIPAPI GdipResetTextureTransform(GpTexture* brush)
1245 {
1246     TRACE("(%p)\n", brush);
1247
1248     if(!brush)
1249         return InvalidParameter;
1250
1251     return GdipSetMatrixElements(brush->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1252 }
1253
1254 /******************************************************************************
1255  * GdipScaleTextureTransform [GDIPLUS.@]
1256  */
1257 GpStatus WINGDIPAPI GdipScaleTextureTransform(GpTexture* brush,
1258     REAL sx, REAL sy, GpMatrixOrder order)
1259 {
1260     TRACE("(%p, %.2f, %.2f, %d)\n", brush, sx, sy, order);
1261
1262     if(!brush)
1263         return InvalidParameter;
1264
1265     return GdipScaleMatrix(brush->transform, sx, sy, order);
1266 }
1267
1268 GpStatus WINGDIPAPI GdipSetLineBlend(GpLineGradient *brush,
1269     GDIPCONST REAL *factors, GDIPCONST REAL* positions, INT count)
1270 {
1271     REAL *new_blendfac, *new_blendpos;
1272
1273     TRACE("(%p, %p, %p, %i)\n", brush, factors, positions, count);
1274
1275     if(!brush || !factors || !positions || count <= 0 ||
1276        (count >= 2 && (positions[0] != 0.0f || positions[count-1] != 1.0f)))
1277         return InvalidParameter;
1278
1279     new_blendfac = GdipAlloc(count * sizeof(REAL));
1280     new_blendpos = GdipAlloc(count * sizeof(REAL));
1281
1282     if (!new_blendfac || !new_blendpos)
1283     {
1284         GdipFree(new_blendfac);
1285         GdipFree(new_blendpos);
1286         return OutOfMemory;
1287     }
1288
1289     memcpy(new_blendfac, factors, count * sizeof(REAL));
1290     memcpy(new_blendpos, positions, count * sizeof(REAL));
1291
1292     GdipFree(brush->blendfac);
1293     GdipFree(brush->blendpos);
1294
1295     brush->blendcount = count;
1296     brush->blendfac = new_blendfac;
1297     brush->blendpos = new_blendpos;
1298
1299     return Ok;
1300 }
1301
1302 GpStatus WINGDIPAPI GdipGetLineBlend(GpLineGradient *brush, REAL *factors,
1303     REAL *positions, INT count)
1304 {
1305     TRACE("(%p, %p, %p, %i)\n", brush, factors, positions, count);
1306
1307     if (!brush || !factors || !positions || count <= 0)
1308         return InvalidParameter;
1309
1310     if (count < brush->blendcount)
1311         return InsufficientBuffer;
1312
1313     memcpy(factors, brush->blendfac, brush->blendcount * sizeof(REAL));
1314     memcpy(positions, brush->blendpos, brush->blendcount * sizeof(REAL));
1315
1316     return Ok;
1317 }
1318
1319 GpStatus WINGDIPAPI GdipGetLineBlendCount(GpLineGradient *brush, INT *count)
1320 {
1321     TRACE("(%p, %p)\n", brush, count);
1322
1323     if (!brush || !count)
1324         return InvalidParameter;
1325
1326     *count = brush->blendcount;
1327
1328     return Ok;
1329 }
1330
1331 GpStatus WINGDIPAPI GdipSetLineGammaCorrection(GpLineGradient *line,
1332     BOOL usegamma)
1333 {
1334     TRACE("(%p, %d)\n", line, usegamma);
1335
1336     if(!line)
1337         return InvalidParameter;
1338
1339     line->gamma = usegamma;
1340
1341     return Ok;
1342 }
1343
1344 GpStatus WINGDIPAPI GdipSetLineSigmaBlend(GpLineGradient *line, REAL focus,
1345     REAL scale)
1346 {
1347     REAL factors[33];
1348     REAL positions[33];
1349     int num_points = 0;
1350     int i;
1351     const int precision = 16;
1352     REAL erf_range; /* we use values erf(-erf_range) through erf(+erf_range) */
1353     REAL min_erf;
1354     REAL scale_erf;
1355
1356     TRACE("(%p, %0.2f, %0.2f)\n", line, focus, scale);
1357
1358     if(!line || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
1359         return InvalidParameter;
1360
1361     /* we want 2 standard deviations */
1362     erf_range = 2.0 / sqrt(2);
1363
1364     /* calculate the constants we need to normalize the error function to be
1365         between 0.0 and scale over the range we need */
1366     min_erf = erf(-erf_range);
1367     scale_erf = scale / (-2.0 * min_erf);
1368
1369     if (focus != 0.0)
1370     {
1371         positions[0] = 0.0;
1372         factors[0] = 0.0;
1373         for (i=1; i<precision; i++)
1374         {
1375             positions[i] = focus * i / precision;
1376             factors[i] = scale_erf * (erf(2 * erf_range * i / precision - erf_range) - min_erf);
1377         }
1378         num_points += precision;
1379     }
1380
1381     positions[num_points] = focus;
1382     factors[num_points] = scale;
1383     num_points += 1;
1384
1385     if (focus != 1.0)
1386     {
1387         for (i=1; i<precision; i++)
1388         {
1389             positions[i+num_points-1] = (focus + ((1.0-focus) * i / precision));
1390             factors[i+num_points-1] = scale_erf * (erf(erf_range - 2 * erf_range * i / precision) - min_erf);
1391         }
1392         num_points += precision;
1393         positions[num_points-1] = 1.0;
1394         factors[num_points-1] = 0.0;
1395     }
1396
1397     return GdipSetLineBlend(line, factors, positions, num_points);
1398 }
1399
1400 GpStatus WINGDIPAPI GdipSetLineWrapMode(GpLineGradient *line,
1401     GpWrapMode wrap)
1402 {
1403     TRACE("(%p, %d)\n", line, wrap);
1404
1405     if(!line || wrap == WrapModeClamp)
1406         return InvalidParameter;
1407
1408     line->wrap = wrap;
1409
1410     return Ok;
1411 }
1412
1413 GpStatus WINGDIPAPI GdipSetPathGradientBlend(GpPathGradient *brush, GDIPCONST REAL *blend,
1414     GDIPCONST REAL *pos, INT count)
1415 {
1416     static int calls;
1417
1418     if(!(calls++))
1419         FIXME("not implemented\n");
1420
1421     return NotImplemented;
1422 }
1423
1424 GpStatus WINGDIPAPI GdipSetPathGradientPresetBlend(GpPathGradient *brush,
1425     GDIPCONST ARGB *blend, GDIPCONST REAL *pos, INT count)
1426 {
1427     FIXME("(%p,%p,%p,%i): stub\n", brush, blend, pos, count);
1428     return NotImplemented;
1429 }
1430
1431 GpStatus WINGDIPAPI GdipSetPathGradientCenterColor(GpPathGradient *grad,
1432     ARGB argb)
1433 {
1434     TRACE("(%p, %x)\n", grad, argb);
1435
1436     if(!grad)
1437         return InvalidParameter;
1438
1439     grad->centercolor = argb;
1440     grad->brush.lb.lbColor = ARGB2COLORREF(argb);
1441
1442     DeleteObject(grad->brush.gdibrush);
1443     grad->brush.gdibrush = CreateSolidBrush(grad->brush.lb.lbColor);
1444
1445     return Ok;
1446 }
1447
1448 GpStatus WINGDIPAPI GdipSetPathGradientCenterPoint(GpPathGradient *grad,
1449     GpPointF *point)
1450 {
1451     TRACE("(%p, %p)\n", grad, point);
1452
1453     if(!grad || !point)
1454         return InvalidParameter;
1455
1456     grad->center.X = point->X;
1457     grad->center.Y = point->Y;
1458
1459     return Ok;
1460 }
1461
1462 GpStatus WINGDIPAPI GdipSetPathGradientCenterPointI(GpPathGradient *grad,
1463     GpPoint *point)
1464 {
1465     GpPointF ptf;
1466
1467     TRACE("(%p, %p)\n", grad, point);
1468
1469     if(!point)
1470         return InvalidParameter;
1471
1472     ptf.X = (REAL)point->X;
1473     ptf.Y = (REAL)point->Y;
1474
1475     return GdipSetPathGradientCenterPoint(grad,&ptf);
1476 }
1477
1478 GpStatus WINGDIPAPI GdipSetPathGradientFocusScales(GpPathGradient *grad,
1479     REAL x, REAL y)
1480 {
1481     TRACE("(%p, %.2f, %.2f)\n", grad, x, y);
1482
1483     if(!grad)
1484         return InvalidParameter;
1485
1486     grad->focus.X = x;
1487     grad->focus.Y = y;
1488
1489     return Ok;
1490 }
1491
1492 GpStatus WINGDIPAPI GdipSetPathGradientGammaCorrection(GpPathGradient *grad,
1493     BOOL gamma)
1494 {
1495     TRACE("(%p, %d)\n", grad, gamma);
1496
1497     if(!grad)
1498         return InvalidParameter;
1499
1500     grad->gamma = gamma;
1501
1502     return Ok;
1503 }
1504
1505 GpStatus WINGDIPAPI GdipSetPathGradientSigmaBlend(GpPathGradient *grad,
1506     REAL focus, REAL scale)
1507 {
1508     static int calls;
1509
1510     if(!grad || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
1511         return InvalidParameter;
1512
1513     if(!(calls++))
1514         FIXME("not implemented\n");
1515
1516     return NotImplemented;
1517 }
1518
1519 GpStatus WINGDIPAPI GdipSetPathGradientSurroundColorsWithCount(GpPathGradient
1520     *grad, ARGB *argb, INT *count)
1521 {
1522     static int calls;
1523
1524     if(!grad || !argb || !count || (*count <= 0) ||
1525         (*count > grad->pathdata.Count))
1526         return InvalidParameter;
1527
1528     if(!(calls++))
1529         FIXME("not implemented\n");
1530
1531     return NotImplemented;
1532 }
1533
1534 GpStatus WINGDIPAPI GdipSetPathGradientWrapMode(GpPathGradient *grad,
1535     GpWrapMode wrap)
1536 {
1537     TRACE("(%p, %d)\n", grad, wrap);
1538
1539     if(!grad)
1540         return InvalidParameter;
1541
1542     grad->wrap = wrap;
1543
1544     return Ok;
1545 }
1546
1547 GpStatus WINGDIPAPI GdipSetSolidFillColor(GpSolidFill *sf, ARGB argb)
1548 {
1549     TRACE("(%p, %x)\n", sf, argb);
1550
1551     if(!sf)
1552         return InvalidParameter;
1553
1554     sf->color = argb;
1555     sf->brush.lb.lbColor = ARGB2COLORREF(argb);
1556
1557     DeleteObject(sf->brush.gdibrush);
1558     sf->brush.gdibrush = CreateSolidBrush(sf->brush.lb.lbColor);
1559
1560     return Ok;
1561 }
1562
1563 /******************************************************************************
1564  * GdipSetTextureTransform [GDIPLUS.@]
1565  */
1566 GpStatus WINGDIPAPI GdipSetTextureTransform(GpTexture *texture,
1567     GDIPCONST GpMatrix *matrix)
1568 {
1569     TRACE("(%p, %p)\n", texture, matrix);
1570
1571     if(!texture || !matrix)
1572         return InvalidParameter;
1573
1574     memcpy(texture->transform, matrix, sizeof(GpMatrix));
1575
1576     return Ok;
1577 }
1578
1579 /******************************************************************************
1580  * GdipSetTextureWrapMode [GDIPLUS.@]
1581  *
1582  * WrapMode not used, only stored
1583  */
1584 GpStatus WINGDIPAPI GdipSetTextureWrapMode(GpTexture *brush, GpWrapMode wrapmode)
1585 {
1586     TRACE("(%p, %d)\n", brush, wrapmode);
1587
1588     if(!brush)
1589         return InvalidParameter;
1590
1591     brush->wrap = wrapmode;
1592
1593     return Ok;
1594 }
1595
1596 GpStatus WINGDIPAPI GdipSetLineColors(GpLineGradient *brush, ARGB color1,
1597     ARGB color2)
1598 {
1599     TRACE("(%p, %x, %x)\n", brush, color1, color2);
1600
1601     if(!brush)
1602         return InvalidParameter;
1603
1604     brush->startcolor = color1;
1605     brush->endcolor   = color2;
1606
1607     return Ok;
1608 }
1609
1610 GpStatus WINGDIPAPI GdipGetLineColors(GpLineGradient *brush, ARGB *colors)
1611 {
1612     TRACE("(%p, %p)\n", brush, colors);
1613
1614     if(!brush || !colors)
1615         return InvalidParameter;
1616
1617     colors[0] = brush->startcolor;
1618     colors[1] = brush->endcolor;
1619
1620     return Ok;
1621 }
1622
1623 /******************************************************************************
1624  * GdipRotateTextureTransform [GDIPLUS.@]
1625  */
1626 GpStatus WINGDIPAPI GdipRotateTextureTransform(GpTexture* brush, REAL angle,
1627     GpMatrixOrder order)
1628 {
1629     TRACE("(%p, %.2f, %d)\n", brush, angle, order);
1630
1631     if(!brush)
1632         return InvalidParameter;
1633
1634     return GdipRotateMatrix(brush->transform, angle, order);
1635 }
1636
1637 GpStatus WINGDIPAPI GdipSetLineLinearBlend(GpLineGradient *brush, REAL focus,
1638     REAL scale)
1639 {
1640     REAL factors[3];
1641     REAL positions[3];
1642     int num_points = 0;
1643
1644     TRACE("(%p,%.2f,%.2f)\n", brush, focus, scale);
1645
1646     if (!brush) return InvalidParameter;
1647
1648     if (focus != 0.0)
1649     {
1650         factors[num_points] = 0.0;
1651         positions[num_points] = 0.0;
1652         num_points++;
1653     }
1654
1655     factors[num_points] = scale;
1656     positions[num_points] = focus;
1657     num_points++;
1658
1659     if (focus != 1.0)
1660     {
1661         factors[num_points] = 0.0;
1662         positions[num_points] = 1.0;
1663         num_points++;
1664     }
1665
1666     return GdipSetLineBlend(brush, factors, positions, num_points);
1667 }
1668
1669 GpStatus WINGDIPAPI GdipSetLinePresetBlend(GpLineGradient *brush,
1670     GDIPCONST ARGB *blend, GDIPCONST REAL* positions, INT count)
1671 {
1672     ARGB *new_color;
1673     REAL *new_pos;
1674     TRACE("(%p,%p,%p,%i)\n", brush, blend, positions, count);
1675
1676     if (!brush || !blend || !positions || count < 2 ||
1677         positions[0] != 0.0f || positions[count-1] != 1.0f)
1678     {
1679         return InvalidParameter;
1680     }
1681
1682     new_color = GdipAlloc(count * sizeof(ARGB));
1683     new_pos = GdipAlloc(count * sizeof(REAL));
1684     if (!new_color || !new_pos)
1685     {
1686         GdipFree(new_color);
1687         GdipFree(new_pos);
1688         return OutOfMemory;
1689     }
1690
1691     memcpy(new_color, blend, sizeof(ARGB) * count);
1692     memcpy(new_pos, positions, sizeof(REAL) * count);
1693
1694     GdipFree(brush->pblendcolor);
1695     GdipFree(brush->pblendpos);
1696
1697     brush->pblendcolor = new_color;
1698     brush->pblendpos = new_pos;
1699     brush->pblendcount = count;
1700
1701     return Ok;
1702 }
1703
1704 GpStatus WINGDIPAPI GdipGetLinePresetBlend(GpLineGradient *brush,
1705     ARGB *blend, REAL* positions, INT count)
1706 {
1707     if (!brush || !blend || !positions || count < 2)
1708         return InvalidParameter;
1709
1710     if (brush->pblendcount == 0)
1711         return GenericError;
1712
1713     if (count < brush->pblendcount)
1714         return InsufficientBuffer;
1715
1716     memcpy(blend, brush->pblendcolor, sizeof(ARGB) * brush->pblendcount);
1717     memcpy(positions, brush->pblendpos, sizeof(REAL) * brush->pblendcount);
1718
1719     return Ok;
1720 }
1721
1722 GpStatus WINGDIPAPI GdipGetLinePresetBlendCount(GpLineGradient *brush,
1723     INT *count)
1724 {
1725     if (!brush || !count)
1726         return InvalidParameter;
1727
1728     *count = brush->pblendcount;
1729
1730     return Ok;
1731 }
1732
1733 GpStatus WINGDIPAPI GdipResetLineTransform(GpLineGradient *brush)
1734 {
1735     static int calls;
1736
1737     if(!(calls++))
1738         FIXME("not implemented\n");
1739
1740     return NotImplemented;
1741 }
1742
1743 GpStatus WINGDIPAPI GdipSetLineTransform(GpLineGradient *brush,
1744     GDIPCONST GpMatrix *matrix)
1745 {
1746     static int calls;
1747
1748     if(!(calls++))
1749         FIXME("not implemented\n");
1750
1751     return NotImplemented;
1752 }
1753
1754 GpStatus WINGDIPAPI GdipScaleLineTransform(GpLineGradient *brush, REAL sx, REAL sy,
1755     GpMatrixOrder order)
1756 {
1757     static int calls;
1758
1759     if(!(calls++))
1760         FIXME("not implemented\n");
1761
1762     return NotImplemented;
1763 }
1764
1765 GpStatus WINGDIPAPI GdipTranslateLineTransform(GpLineGradient* brush,
1766         REAL dx, REAL dy, GpMatrixOrder order)
1767 {
1768     FIXME("stub: %p %f %f %d\n", brush, dx, dy, order);
1769
1770     return NotImplemented;
1771 }
1772
1773 /******************************************************************************
1774  * GdipTranslateTextureTransform [GDIPLUS.@]
1775  */
1776 GpStatus WINGDIPAPI GdipTranslateTextureTransform(GpTexture* brush, REAL dx, REAL dy,
1777     GpMatrixOrder order)
1778 {
1779     TRACE("(%p, %.2f, %.2f, %d)\n", brush, dx, dy, order);
1780
1781     if(!brush)
1782         return InvalidParameter;
1783
1784     return GdipTranslateMatrix(brush->transform, dx, dy, order);
1785 }
1786
1787 GpStatus WINGDIPAPI GdipGetLineRect(GpLineGradient *brush, GpRectF *rect)
1788 {
1789     TRACE("(%p, %p)\n", brush, rect);
1790
1791     if(!brush || !rect)
1792         return InvalidParameter;
1793
1794     *rect = brush->rect;
1795
1796     return Ok;
1797 }
1798
1799 GpStatus WINGDIPAPI GdipGetLineRectI(GpLineGradient *brush, GpRect *rect)
1800 {
1801     GpRectF  rectF;
1802     GpStatus ret;
1803
1804     TRACE("(%p, %p)\n", brush, rect);
1805
1806     if(!rect)
1807         return InvalidParameter;
1808
1809     ret = GdipGetLineRect(brush, &rectF);
1810
1811     if(ret == Ok){
1812         rect->X      = roundr(rectF.X);
1813         rect->Y      = roundr(rectF.Y);
1814         rect->Width  = roundr(rectF.Width);
1815         rect->Height = roundr(rectF.Height);
1816     }
1817
1818     return ret;
1819 }
1820
1821 GpStatus WINGDIPAPI GdipRotateLineTransform(GpLineGradient* brush,
1822     REAL angle, GpMatrixOrder order)
1823 {
1824     static int calls;
1825
1826     if(!brush)
1827         return InvalidParameter;
1828
1829     if(!(calls++))
1830         FIXME("(%p, %.2f, %d) stub\n", brush, angle, order);
1831
1832     return NotImplemented;
1833 }