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