user32: Don't use HIWORD() on a HINSTANCE.
[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("(%s, %s, %x, %x, %d, %p)\n", debugstr_pointf(startpoint),
315           debugstr_pointf(endpoint), 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 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectWithAngle(GDIPCONST GpRectF* rect,
473     ARGB startcolor, ARGB endcolor, REAL angle, BOOL isAngleScalable, GpWrapMode wrap,
474     GpLineGradient **line)
475 {
476     GpStatus stat;
477     LinearGradientMode mode;
478     REAL width, height, exofs, eyofs;
479     REAL sin_angle, cos_angle, sin_cos_angle;
480
481     TRACE("(%p, %x, %x, %.2f, %d, %d, %p)\n", rect, startcolor, endcolor, angle, isAngleScalable,
482           wrap, line);
483
484     sin_angle = sinf(deg2rad(angle));
485     cos_angle = cosf(deg2rad(angle));
486     sin_cos_angle = sin_angle * cos_angle;
487
488     if (isAngleScalable)
489     {
490         width = height = 1.0;
491     }
492     else
493     {
494         width = rect->Width;
495         height = rect->Height;
496     }
497
498     if (sin_cos_angle >= 0)
499         mode = LinearGradientModeForwardDiagonal;
500     else
501         mode = LinearGradientModeBackwardDiagonal;
502
503     stat = GdipCreateLineBrushFromRect(rect, startcolor, endcolor, mode, wrap, line);
504
505     if (stat == Ok)
506     {
507         if (sin_cos_angle >= 0)
508         {
509             exofs = width * sin_cos_angle + height * cos_angle * cos_angle;
510             eyofs = width * sin_angle * sin_angle + height * sin_cos_angle;
511         }
512         else
513         {
514             exofs = width * sin_angle * sin_angle + height * sin_cos_angle;
515             eyofs = -width * sin_cos_angle + height * sin_angle * sin_angle;
516         }
517
518         if (isAngleScalable)
519         {
520             exofs = exofs * rect->Width;
521             eyofs = eyofs * rect->Height;
522         }
523
524         if (sin_angle >= 0)
525         {
526             (*line)->endpoint.X = rect->X + exofs;
527             (*line)->endpoint.Y = rect->Y + eyofs;
528         }
529         else
530         {
531             (*line)->endpoint.X = (*line)->startpoint.X;
532             (*line)->endpoint.Y = (*line)->startpoint.Y;
533             (*line)->startpoint.X = rect->X + exofs;
534             (*line)->startpoint.Y = rect->Y + eyofs;
535         }
536     }
537
538     return stat;
539 }
540
541 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectWithAngleI(GDIPCONST GpRect* rect,
542     ARGB startcolor, ARGB endcolor, REAL angle, BOOL isAngleScalable, GpWrapMode wrap,
543     GpLineGradient **line)
544 {
545     TRACE("(%p, %x, %x, %.2f, %d, %d, %p)\n", rect, startcolor, endcolor, angle, isAngleScalable,
546           wrap, line);
547
548     return GdipCreateLineBrushFromRectI(rect, startcolor, endcolor, LinearGradientModeForwardDiagonal,
549                                         wrap, line);
550 }
551
552 GpStatus WINGDIPAPI GdipCreatePathGradient(GDIPCONST GpPointF* points,
553     INT count, GpWrapMode wrap, GpPathGradient **grad)
554 {
555     COLORREF col = ARGB2COLORREF(0xffffffff);
556
557     TRACE("(%p, %d, %d, %p)\n", points, count, wrap, grad);
558
559     if(!points || !grad)
560         return InvalidParameter;
561
562     if(count <= 0)
563         return OutOfMemory;
564
565     *grad = GdipAlloc(sizeof(GpPathGradient));
566     if (!*grad) return OutOfMemory;
567
568     (*grad)->blendfac = GdipAlloc(sizeof(REAL));
569     if(!(*grad)->blendfac){
570         GdipFree(*grad);
571         return OutOfMemory;
572     }
573     (*grad)->blendfac[0] = 1.0;
574     (*grad)->blendpos    = NULL;
575     (*grad)->blendcount  = 1;
576
577     (*grad)->pathdata.Count = count;
578     (*grad)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
579     (*grad)->pathdata.Types = GdipAlloc(count);
580
581     if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
582         GdipFree((*grad)->pathdata.Points);
583         GdipFree((*grad)->pathdata.Types);
584         GdipFree(*grad);
585         return OutOfMemory;
586     }
587
588     memcpy((*grad)->pathdata.Points, points, count * sizeof(PointF));
589     memset((*grad)->pathdata.Types, PathPointTypeLine, count);
590
591     (*grad)->brush.lb.lbStyle = BS_SOLID;
592     (*grad)->brush.lb.lbColor = col;
593     (*grad)->brush.lb.lbHatch = 0;
594
595     (*grad)->brush.gdibrush = CreateSolidBrush(col);
596     (*grad)->brush.bt = BrushTypePathGradient;
597     (*grad)->centercolor = 0xffffffff;
598     (*grad)->wrap = wrap;
599     (*grad)->gamma = FALSE;
600     (*grad)->center.X = 0.0;
601     (*grad)->center.Y = 0.0;
602     (*grad)->focus.X = 0.0;
603     (*grad)->focus.Y = 0.0;
604
605     TRACE("<-- %p\n", *grad);
606
607     return Ok;
608 }
609
610 GpStatus WINGDIPAPI GdipCreatePathGradientI(GDIPCONST GpPoint* points,
611     INT count, GpWrapMode wrap, GpPathGradient **grad)
612 {
613     GpPointF *pointsF;
614     GpStatus ret;
615     INT i;
616
617     TRACE("(%p, %d, %d, %p)\n", points, count, wrap, grad);
618
619     if(!points || !grad)
620         return InvalidParameter;
621
622     if(count <= 0)
623         return OutOfMemory;
624
625     pointsF = GdipAlloc(sizeof(GpPointF) * count);
626     if(!pointsF)
627         return OutOfMemory;
628
629     for(i = 0; i < count; i++){
630         pointsF[i].X = (REAL)points[i].X;
631         pointsF[i].Y = (REAL)points[i].Y;
632     }
633
634     ret = GdipCreatePathGradient(pointsF, count, wrap, grad);
635     GdipFree(pointsF);
636
637     return ret;
638 }
639
640 /******************************************************************************
641  * GdipCreatePathGradientFromPath [GDIPLUS.@]
642  *
643  * FIXME: path gradient brushes not truly supported (drawn as solid brushes)
644  */
645 GpStatus WINGDIPAPI GdipCreatePathGradientFromPath(GDIPCONST GpPath* path,
646     GpPathGradient **grad)
647 {
648     COLORREF col = ARGB2COLORREF(0xffffffff);
649
650     TRACE("(%p, %p)\n", path, grad);
651
652     if(!path || !grad)
653         return InvalidParameter;
654
655     *grad = GdipAlloc(sizeof(GpPathGradient));
656     if (!*grad) return OutOfMemory;
657
658     (*grad)->blendfac = GdipAlloc(sizeof(REAL));
659     if(!(*grad)->blendfac){
660         GdipFree(*grad);
661         return OutOfMemory;
662     }
663     (*grad)->blendfac[0] = 1.0;
664     (*grad)->blendpos    = NULL;
665     (*grad)->blendcount  = 1;
666
667     (*grad)->pathdata.Count = path->pathdata.Count;
668     (*grad)->pathdata.Points = GdipAlloc(path->pathdata.Count * sizeof(PointF));
669     (*grad)->pathdata.Types = GdipAlloc(path->pathdata.Count);
670
671     if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
672         GdipFree((*grad)->pathdata.Points);
673         GdipFree((*grad)->pathdata.Types);
674         GdipFree(*grad);
675         return OutOfMemory;
676     }
677
678     memcpy((*grad)->pathdata.Points, path->pathdata.Points,
679            path->pathdata.Count * sizeof(PointF));
680     memcpy((*grad)->pathdata.Types, path->pathdata.Types, path->pathdata.Count);
681
682     (*grad)->brush.lb.lbStyle = BS_SOLID;
683     (*grad)->brush.lb.lbColor = col;
684     (*grad)->brush.lb.lbHatch = 0;
685
686     (*grad)->brush.gdibrush = CreateSolidBrush(col);
687     (*grad)->brush.bt = BrushTypePathGradient;
688     (*grad)->centercolor = 0xffffffff;
689     (*grad)->wrap = WrapModeClamp;
690     (*grad)->gamma = FALSE;
691     /* FIXME: this should be set to the "centroid" of the path by default */
692     (*grad)->center.X = 0.0;
693     (*grad)->center.Y = 0.0;
694     (*grad)->focus.X = 0.0;
695     (*grad)->focus.Y = 0.0;
696
697     TRACE("<-- %p\n", *grad);
698
699     return Ok;
700 }
701
702 /******************************************************************************
703  * GdipCreateSolidFill [GDIPLUS.@]
704  */
705 GpStatus WINGDIPAPI GdipCreateSolidFill(ARGB color, GpSolidFill **sf)
706 {
707     COLORREF col = ARGB2COLORREF(color);
708
709     TRACE("(%x, %p)\n", color, sf);
710
711     if(!sf)  return InvalidParameter;
712
713     *sf = GdipAlloc(sizeof(GpSolidFill));
714     if (!*sf) return OutOfMemory;
715
716     (*sf)->brush.lb.lbStyle = BS_SOLID;
717     (*sf)->brush.lb.lbColor = col;
718     (*sf)->brush.lb.lbHatch = 0;
719
720     (*sf)->brush.gdibrush = CreateSolidBrush(col);
721     (*sf)->brush.bt = BrushTypeSolidColor;
722     (*sf)->color = color;
723     (*sf)->bmp = ARGB2BMP(color);
724
725     TRACE("<-- %p\n", *sf);
726
727     return Ok;
728 }
729
730 /******************************************************************************
731  * GdipCreateTexture [GDIPLUS.@]
732  *
733  * PARAMS
734  *  image       [I] image to use
735  *  wrapmode    [I] optional
736  *  texture     [O] pointer to the resulting texturebrush
737  *
738  * RETURNS
739  *  SUCCESS: Ok
740  *  FAILURE: element of GpStatus
741  */
742 GpStatus WINGDIPAPI GdipCreateTexture(GpImage *image, GpWrapMode wrapmode,
743         GpTexture **texture)
744 {
745     UINT width, height;
746     GpImageAttributes attributes;
747     GpStatus stat;
748
749     TRACE("%p, %d %p\n", image, wrapmode, texture);
750
751     if (!(image && texture))
752         return InvalidParameter;
753
754     stat = GdipGetImageWidth(image, &width);
755     if (stat != Ok) return stat;
756     stat = GdipGetImageHeight(image, &height);
757     if (stat != Ok) return stat;
758     attributes.wrap = wrapmode;
759
760     return GdipCreateTextureIA(image, &attributes, 0, 0, width, height,
761             texture);
762 }
763
764 /******************************************************************************
765  * GdipCreateTexture2 [GDIPLUS.@]
766  */
767 GpStatus WINGDIPAPI GdipCreateTexture2(GpImage *image, GpWrapMode wrapmode,
768         REAL x, REAL y, REAL width, REAL height, GpTexture **texture)
769 {
770     GpImageAttributes attributes;
771
772     TRACE("%p %d %f %f %f %f %p\n", image, wrapmode,
773             x, y, width, height, texture);
774
775     attributes.wrap = wrapmode;
776     return GdipCreateTextureIA(image, &attributes, x, y, width, height,
777             texture);
778 }
779
780 /******************************************************************************
781  * GdipCreateTextureIA [GDIPLUS.@]
782  *
783  * FIXME: imageattr ignored
784  */
785 GpStatus WINGDIPAPI GdipCreateTextureIA(GpImage *image,
786     GDIPCONST GpImageAttributes *imageattr, REAL x, REAL y, REAL width,
787     REAL height, GpTexture **texture)
788 {
789     HDC hdc;
790     HBITMAP hbm, old = NULL;
791     BITMAPINFO *pbmi;
792     BITMAPINFOHEADER *bmih;
793     INT n_x, n_y, n_width, n_height, abs_height, stride, image_stride, i, bytespp;
794     BOOL bm_is_selected;
795     BYTE *dibits, *buff, *textbits;
796     GpStatus status;
797
798     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %p)\n", image, imageattr, x, y, width, height,
799            texture);
800
801     if(!image || !texture || x < 0.0 || y < 0.0 || width < 0.0 || height < 0.0)
802         return InvalidParameter;
803
804     if(image->type != ImageTypeBitmap){
805         FIXME("not implemented for image type %d\n", image->type);
806         return NotImplemented;
807     }
808
809     n_x = roundr(x);
810     n_y = roundr(y);
811     n_width = roundr(width);
812     n_height = roundr(height);
813
814     if(n_x + n_width > ((GpBitmap*)image)->width ||
815        n_y + n_height > ((GpBitmap*)image)->height)
816         return InvalidParameter;
817
818     hbm = ((GpBitmap*)image)->hbitmap;
819     if(!hbm)   return GenericError;
820     hdc = ((GpBitmap*)image)->hdc;
821     bm_is_selected = (hdc != 0);
822
823     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
824     if (!pbmi)
825         return OutOfMemory;
826     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
827     pbmi->bmiHeader.biBitCount = 0;
828
829     if(!bm_is_selected){
830         hdc = CreateCompatibleDC(0);
831         old = SelectObject(hdc, hbm);
832     }
833
834     /* fill out bmi */
835     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
836
837     bytespp = pbmi->bmiHeader.biBitCount / 8;
838     abs_height = abs(pbmi->bmiHeader.biHeight);
839
840     if(n_x > pbmi->bmiHeader.biWidth || n_x + n_width > pbmi->bmiHeader.biWidth ||
841        n_y > abs_height || n_y + n_height > abs_height){
842         GdipFree(pbmi);
843         return InvalidParameter;
844     }
845
846     dibits = GdipAlloc(pbmi->bmiHeader.biSizeImage);
847
848     if(dibits)  /* this is not a good place to error out */
849         GetDIBits(hdc, hbm, 0, abs_height, dibits, pbmi, DIB_RGB_COLORS);
850
851     if(!bm_is_selected){
852         SelectObject(hdc, old);
853         DeleteDC(hdc);
854     }
855
856     if(!dibits){
857         GdipFree(pbmi);
858         return OutOfMemory;
859     }
860
861     image_stride = (pbmi->bmiHeader.biWidth * bytespp + 3) & ~3;
862     stride = (n_width * bytespp + 3) & ~3;
863     buff = GdipAlloc(sizeof(BITMAPINFOHEADER) + stride * n_height);
864     if(!buff){
865         GdipFree(pbmi);
866         GdipFree(dibits);
867         return OutOfMemory;
868     }
869
870     bmih = (BITMAPINFOHEADER*)buff;
871     textbits = (BYTE*) (bmih + 1);
872     bmih->biSize = sizeof(BITMAPINFOHEADER);
873     bmih->biWidth = n_width;
874     bmih->biHeight = n_height;
875     bmih->biCompression = BI_RGB;
876     bmih->biSizeImage = stride * n_height;
877     bmih->biBitCount = pbmi->bmiHeader.biBitCount;
878     bmih->biClrUsed = 0;
879     bmih->biPlanes = 1;
880
881     /* image is flipped */
882     if(pbmi->bmiHeader.biHeight > 0){
883         dibits += image_stride * (pbmi->bmiHeader.biHeight - 1);
884         image_stride *= -1;
885         textbits += stride * (n_height - 1);
886         stride *= -1;
887     }
888
889     GdipFree(pbmi);
890
891     for(i = 0; i < n_height; i++)
892         memcpy(&textbits[i * stride],
893                &dibits[n_x * bytespp + (n_y + i) * image_stride],
894                abs(stride));
895
896     *texture = GdipAlloc(sizeof(GpTexture));
897     if (!*texture){
898         GdipFree(dibits);
899         GdipFree(buff);
900         return OutOfMemory;
901     }
902
903     if((status = GdipCreateMatrix(&(*texture)->transform)) != Ok){
904         GdipFree(*texture);
905         GdipFree(dibits);
906         GdipFree(buff);
907         return status;
908     }
909
910     (*texture)->brush.lb.lbStyle = BS_DIBPATTERNPT;
911     (*texture)->brush.lb.lbColor = DIB_RGB_COLORS;
912     (*texture)->brush.lb.lbHatch = (ULONG_PTR)buff;
913
914     (*texture)->brush.gdibrush = CreateBrushIndirect(&(*texture)->brush.lb);
915     (*texture)->brush.bt = BrushTypeTextureFill;
916     (*texture)->wrap = imageattr->wrap;
917
918     GdipFree(dibits);
919     GdipFree(buff);
920
921     TRACE("<-- %p\n", *texture);
922
923     return Ok;
924 }
925
926 /******************************************************************************
927  * GdipCreateTextureIAI [GDIPLUS.@]
928  */
929 GpStatus WINGDIPAPI GdipCreateTextureIAI(GpImage *image, GDIPCONST GpImageAttributes *imageattr,
930     INT x, INT y, INT width, INT height, GpTexture **texture)
931 {
932     TRACE("(%p, %p, %d, %d, %d, %d, %p)\n", image, imageattr, x, y, width, height,
933            texture);
934
935     return GdipCreateTextureIA(image,imageattr,(REAL)x,(REAL)y,(REAL)width,(REAL)height,texture);
936 }
937
938 GpStatus WINGDIPAPI GdipCreateTexture2I(GpImage *image, GpWrapMode wrapmode,
939         INT x, INT y, INT width, INT height, GpTexture **texture)
940 {
941     GpImageAttributes imageattr;
942
943     TRACE("%p %d %d %d %d %d %p\n", image, wrapmode, x, y, width, height,
944             texture);
945
946     imageattr.wrap = wrapmode;
947
948     return GdipCreateTextureIA(image, &imageattr, x, y, width, height, texture);
949 }
950
951 GpStatus WINGDIPAPI GdipGetBrushType(GpBrush *brush, GpBrushType *type)
952 {
953     TRACE("(%p, %p)\n", brush, type);
954
955     if(!brush || !type)  return InvalidParameter;
956
957     *type = brush->bt;
958
959     return Ok;
960 }
961
962 GpStatus WINGDIPAPI GdipGetHatchBackgroundColor(GpHatch *brush, ARGB *backcol)
963 {
964     TRACE("(%p, %p)\n", brush, backcol);
965
966     if(!brush || !backcol)  return InvalidParameter;
967
968     *backcol = brush->backcol;
969
970     return Ok;
971 }
972
973 GpStatus WINGDIPAPI GdipGetHatchForegroundColor(GpHatch *brush, ARGB *forecol)
974 {
975     TRACE("(%p, %p)\n", brush, forecol);
976
977     if(!brush || !forecol)  return InvalidParameter;
978
979     *forecol = brush->forecol;
980
981     return Ok;
982 }
983
984 GpStatus WINGDIPAPI GdipGetHatchStyle(GpHatch *brush, HatchStyle *hatchstyle)
985 {
986     TRACE("(%p, %p)\n", brush, hatchstyle);
987
988     if(!brush || !hatchstyle)  return InvalidParameter;
989
990     *hatchstyle = brush->hatchstyle;
991
992     return Ok;
993 }
994
995 GpStatus WINGDIPAPI GdipDeleteBrush(GpBrush *brush)
996 {
997     TRACE("(%p)\n", brush);
998
999     if(!brush)  return InvalidParameter;
1000
1001     switch(brush->bt)
1002     {
1003         case BrushTypePathGradient:
1004             GdipFree(((GpPathGradient*) brush)->pathdata.Points);
1005             GdipFree(((GpPathGradient*) brush)->pathdata.Types);
1006             GdipFree(((GpPathGradient*) brush)->blendfac);
1007             GdipFree(((GpPathGradient*) brush)->blendpos);
1008             break;
1009         case BrushTypeSolidColor:
1010             if (((GpSolidFill*)brush)->bmp)
1011                 DeleteObject(((GpSolidFill*)brush)->bmp);
1012             break;
1013         case BrushTypeLinearGradient:
1014             GdipFree(((GpLineGradient*)brush)->blendfac);
1015             GdipFree(((GpLineGradient*)brush)->blendpos);
1016             GdipFree(((GpLineGradient*)brush)->pblendcolor);
1017             GdipFree(((GpLineGradient*)brush)->pblendpos);
1018             break;
1019         case BrushTypeTextureFill:
1020             GdipDeleteMatrix(((GpTexture*)brush)->transform);
1021             break;
1022         default:
1023             break;
1024     }
1025
1026     DeleteObject(brush->gdibrush);
1027     GdipFree(brush);
1028
1029     return Ok;
1030 }
1031
1032 GpStatus WINGDIPAPI GdipGetLineGammaCorrection(GpLineGradient *line,
1033     BOOL *usinggamma)
1034 {
1035     TRACE("(%p, %p)\n", line, usinggamma);
1036
1037     if(!line || !usinggamma)
1038         return InvalidParameter;
1039
1040     *usinggamma = line->gamma;
1041
1042     return Ok;
1043 }
1044
1045 GpStatus WINGDIPAPI GdipGetLineWrapMode(GpLineGradient *brush, GpWrapMode *wrapmode)
1046 {
1047     TRACE("(%p, %p)\n", brush, wrapmode);
1048
1049     if(!brush || !wrapmode)
1050         return InvalidParameter;
1051
1052     *wrapmode = brush->wrap;
1053
1054     return Ok;
1055 }
1056
1057 GpStatus WINGDIPAPI GdipGetPathGradientBlend(GpPathGradient *brush, REAL *blend,
1058     REAL *positions, INT count)
1059 {
1060     TRACE("(%p, %p, %p, %d)\n", brush, blend, positions, count);
1061
1062     if(!brush || !blend || !positions || count <= 0)
1063         return InvalidParameter;
1064
1065     if(count < brush->blendcount)
1066         return InsufficientBuffer;
1067
1068     memcpy(blend, brush->blendfac, count*sizeof(REAL));
1069     if(brush->blendcount > 1){
1070         memcpy(positions, brush->blendpos, count*sizeof(REAL));
1071     }
1072
1073     return Ok;
1074 }
1075
1076 GpStatus WINGDIPAPI GdipGetPathGradientBlendCount(GpPathGradient *brush, INT *count)
1077 {
1078     TRACE("(%p, %p)\n", brush, count);
1079
1080     if(!brush || !count)
1081         return InvalidParameter;
1082
1083     *count = brush->blendcount;
1084
1085     return Ok;
1086 }
1087
1088 GpStatus WINGDIPAPI GdipGetPathGradientCenterPoint(GpPathGradient *grad,
1089     GpPointF *point)
1090 {
1091     TRACE("(%p, %p)\n", grad, point);
1092
1093     if(!grad || !point)
1094         return InvalidParameter;
1095
1096     point->X = grad->center.X;
1097     point->Y = grad->center.Y;
1098
1099     return Ok;
1100 }
1101
1102 GpStatus WINGDIPAPI GdipGetPathGradientCenterPointI(GpPathGradient *grad,
1103     GpPoint *point)
1104 {
1105     GpStatus ret;
1106     GpPointF ptf;
1107
1108     TRACE("(%p, %p)\n", grad, point);
1109
1110     if(!point)
1111         return InvalidParameter;
1112
1113     ret = GdipGetPathGradientCenterPoint(grad,&ptf);
1114
1115     if(ret == Ok){
1116         point->X = roundr(ptf.X);
1117         point->Y = roundr(ptf.Y);
1118     }
1119
1120     return ret;
1121 }
1122
1123 GpStatus WINGDIPAPI GdipGetPathGradientFocusScales(GpPathGradient *grad,
1124     REAL *x, REAL *y)
1125 {
1126     TRACE("(%p, %p, %p)\n", grad, x, y);
1127
1128     if(!grad || !x || !y)
1129         return InvalidParameter;
1130
1131     *x = grad->focus.X;
1132     *y = grad->focus.Y;
1133
1134     return Ok;
1135 }
1136
1137 GpStatus WINGDIPAPI GdipGetPathGradientGammaCorrection(GpPathGradient *grad,
1138     BOOL *gamma)
1139 {
1140     TRACE("(%p, %p)\n", grad, gamma);
1141
1142     if(!grad || !gamma)
1143         return InvalidParameter;
1144
1145     *gamma = grad->gamma;
1146
1147     return Ok;
1148 }
1149
1150 GpStatus WINGDIPAPI GdipGetPathGradientPointCount(GpPathGradient *grad,
1151     INT *count)
1152 {
1153     TRACE("(%p, %p)\n", grad, count);
1154
1155     if(!grad || !count)
1156         return InvalidParameter;
1157
1158     *count = grad->pathdata.Count;
1159
1160     return Ok;
1161 }
1162
1163 GpStatus WINGDIPAPI GdipGetPathGradientRect(GpPathGradient *brush, GpRectF *rect)
1164 {
1165     GpRectF r;
1166     GpPath* path;
1167     GpStatus stat;
1168
1169     TRACE("(%p, %p)\n", brush, rect);
1170
1171     if(!brush || !rect)
1172         return InvalidParameter;
1173
1174     stat = GdipCreatePath2(brush->pathdata.Points, brush->pathdata.Types,
1175                            brush->pathdata.Count, FillModeAlternate, &path);
1176     if(stat != Ok)  return stat;
1177
1178     stat = GdipGetPathWorldBounds(path, &r, NULL, NULL);
1179     if(stat != Ok){
1180         GdipDeletePath(path);
1181         return stat;
1182     }
1183
1184     memcpy(rect, &r, sizeof(GpRectF));
1185
1186     GdipDeletePath(path);
1187
1188     return Ok;
1189 }
1190
1191 GpStatus WINGDIPAPI GdipGetPathGradientRectI(GpPathGradient *brush, GpRect *rect)
1192 {
1193     GpRectF rectf;
1194     GpStatus stat;
1195
1196     TRACE("(%p, %p)\n", brush, rect);
1197
1198     if(!brush || !rect)
1199         return InvalidParameter;
1200
1201     stat = GdipGetPathGradientRect(brush, &rectf);
1202     if(stat != Ok)  return stat;
1203
1204     rect->X = roundr(rectf.X);
1205     rect->Y = roundr(rectf.Y);
1206     rect->Width  = roundr(rectf.Width);
1207     rect->Height = roundr(rectf.Height);
1208
1209     return Ok;
1210 }
1211
1212 GpStatus WINGDIPAPI GdipGetPathGradientSurroundColorsWithCount(GpPathGradient
1213     *grad, ARGB *argb, INT *count)
1214 {
1215     static int calls;
1216
1217     TRACE("(%p,%p,%p)\n", grad, argb, count);
1218
1219     if(!grad || !argb || !count || (*count < grad->pathdata.Count))
1220         return InvalidParameter;
1221
1222     if(!(calls++))
1223         FIXME("not implemented\n");
1224
1225     return NotImplemented;
1226 }
1227
1228 GpStatus WINGDIPAPI GdipGetPathGradientWrapMode(GpPathGradient *brush,
1229     GpWrapMode *wrapmode)
1230 {
1231     TRACE("(%p, %p)\n", brush, wrapmode);
1232
1233     if(!brush || !wrapmode)
1234         return InvalidParameter;
1235
1236     *wrapmode = brush->wrap;
1237
1238     return Ok;
1239 }
1240
1241 GpStatus WINGDIPAPI GdipGetSolidFillColor(GpSolidFill *sf, ARGB *argb)
1242 {
1243     TRACE("(%p, %p)\n", sf, argb);
1244
1245     if(!sf || !argb)
1246         return InvalidParameter;
1247
1248     *argb = sf->color;
1249
1250     return Ok;
1251 }
1252
1253 /******************************************************************************
1254  * GdipGetTextureTransform [GDIPLUS.@]
1255  */
1256 GpStatus WINGDIPAPI GdipGetTextureTransform(GpTexture *brush, GpMatrix *matrix)
1257 {
1258     TRACE("(%p, %p)\n", brush, matrix);
1259
1260     if(!brush || !matrix)
1261         return InvalidParameter;
1262
1263     memcpy(matrix, brush->transform, sizeof(GpMatrix));
1264
1265     return Ok;
1266 }
1267
1268 /******************************************************************************
1269  * GdipGetTextureWrapMode [GDIPLUS.@]
1270  */
1271 GpStatus WINGDIPAPI GdipGetTextureWrapMode(GpTexture *brush, GpWrapMode *wrapmode)
1272 {
1273     TRACE("(%p, %p)\n", brush, wrapmode);
1274
1275     if(!brush || !wrapmode)
1276         return InvalidParameter;
1277
1278     *wrapmode = brush->wrap;
1279
1280     return Ok;
1281 }
1282
1283 /******************************************************************************
1284  * GdipMultiplyTextureTransform [GDIPLUS.@]
1285  */
1286 GpStatus WINGDIPAPI GdipMultiplyTextureTransform(GpTexture* brush,
1287     GDIPCONST GpMatrix *matrix, GpMatrixOrder order)
1288 {
1289     TRACE("(%p, %p, %d)\n", brush, matrix, order);
1290
1291     if(!brush || !matrix)
1292         return InvalidParameter;
1293
1294     return GdipMultiplyMatrix(brush->transform, matrix, order);
1295 }
1296
1297 /******************************************************************************
1298  * GdipResetTextureTransform [GDIPLUS.@]
1299  */
1300 GpStatus WINGDIPAPI GdipResetTextureTransform(GpTexture* brush)
1301 {
1302     TRACE("(%p)\n", brush);
1303
1304     if(!brush)
1305         return InvalidParameter;
1306
1307     return GdipSetMatrixElements(brush->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1308 }
1309
1310 /******************************************************************************
1311  * GdipScaleTextureTransform [GDIPLUS.@]
1312  */
1313 GpStatus WINGDIPAPI GdipScaleTextureTransform(GpTexture* brush,
1314     REAL sx, REAL sy, GpMatrixOrder order)
1315 {
1316     TRACE("(%p, %.2f, %.2f, %d)\n", brush, sx, sy, order);
1317
1318     if(!brush)
1319         return InvalidParameter;
1320
1321     return GdipScaleMatrix(brush->transform, sx, sy, order);
1322 }
1323
1324 GpStatus WINGDIPAPI GdipSetLineBlend(GpLineGradient *brush,
1325     GDIPCONST REAL *factors, GDIPCONST REAL* positions, INT count)
1326 {
1327     REAL *new_blendfac, *new_blendpos;
1328
1329     TRACE("(%p, %p, %p, %i)\n", brush, factors, positions, count);
1330
1331     if(!brush || !factors || !positions || count <= 0 ||
1332        (count >= 2 && (positions[0] != 0.0f || positions[count-1] != 1.0f)))
1333         return InvalidParameter;
1334
1335     new_blendfac = GdipAlloc(count * sizeof(REAL));
1336     new_blendpos = GdipAlloc(count * sizeof(REAL));
1337
1338     if (!new_blendfac || !new_blendpos)
1339     {
1340         GdipFree(new_blendfac);
1341         GdipFree(new_blendpos);
1342         return OutOfMemory;
1343     }
1344
1345     memcpy(new_blendfac, factors, count * sizeof(REAL));
1346     memcpy(new_blendpos, positions, count * sizeof(REAL));
1347
1348     GdipFree(brush->blendfac);
1349     GdipFree(brush->blendpos);
1350
1351     brush->blendcount = count;
1352     brush->blendfac = new_blendfac;
1353     brush->blendpos = new_blendpos;
1354
1355     return Ok;
1356 }
1357
1358 GpStatus WINGDIPAPI GdipGetLineBlend(GpLineGradient *brush, REAL *factors,
1359     REAL *positions, INT count)
1360 {
1361     TRACE("(%p, %p, %p, %i)\n", brush, factors, positions, count);
1362
1363     if (!brush || !factors || !positions || count <= 0)
1364         return InvalidParameter;
1365
1366     if (count < brush->blendcount)
1367         return InsufficientBuffer;
1368
1369     memcpy(factors, brush->blendfac, brush->blendcount * sizeof(REAL));
1370     memcpy(positions, brush->blendpos, brush->blendcount * sizeof(REAL));
1371
1372     return Ok;
1373 }
1374
1375 GpStatus WINGDIPAPI GdipGetLineBlendCount(GpLineGradient *brush, INT *count)
1376 {
1377     TRACE("(%p, %p)\n", brush, count);
1378
1379     if (!brush || !count)
1380         return InvalidParameter;
1381
1382     *count = brush->blendcount;
1383
1384     return Ok;
1385 }
1386
1387 GpStatus WINGDIPAPI GdipSetLineGammaCorrection(GpLineGradient *line,
1388     BOOL usegamma)
1389 {
1390     TRACE("(%p, %d)\n", line, usegamma);
1391
1392     if(!line)
1393         return InvalidParameter;
1394
1395     line->gamma = usegamma;
1396
1397     return Ok;
1398 }
1399
1400 GpStatus WINGDIPAPI GdipSetLineSigmaBlend(GpLineGradient *line, REAL focus,
1401     REAL scale)
1402 {
1403     REAL factors[33];
1404     REAL positions[33];
1405     int num_points = 0;
1406     int i;
1407     const int precision = 16;
1408     REAL erf_range; /* we use values erf(-erf_range) through erf(+erf_range) */
1409     REAL min_erf;
1410     REAL scale_erf;
1411
1412     TRACE("(%p, %0.2f, %0.2f)\n", line, focus, scale);
1413
1414     if(!line || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
1415         return InvalidParameter;
1416
1417     /* we want 2 standard deviations */
1418     erf_range = 2.0 / sqrt(2);
1419
1420     /* calculate the constants we need to normalize the error function to be
1421         between 0.0 and scale over the range we need */
1422     min_erf = erf(-erf_range);
1423     scale_erf = scale / (-2.0 * min_erf);
1424
1425     if (focus != 0.0)
1426     {
1427         positions[0] = 0.0;
1428         factors[0] = 0.0;
1429         for (i=1; i<precision; i++)
1430         {
1431             positions[i] = focus * i / precision;
1432             factors[i] = scale_erf * (erf(2 * erf_range * i / precision - erf_range) - min_erf);
1433         }
1434         num_points += precision;
1435     }
1436
1437     positions[num_points] = focus;
1438     factors[num_points] = scale;
1439     num_points += 1;
1440
1441     if (focus != 1.0)
1442     {
1443         for (i=1; i<precision; i++)
1444         {
1445             positions[i+num_points-1] = (focus + ((1.0-focus) * i / precision));
1446             factors[i+num_points-1] = scale_erf * (erf(erf_range - 2 * erf_range * i / precision) - min_erf);
1447         }
1448         num_points += precision;
1449         positions[num_points-1] = 1.0;
1450         factors[num_points-1] = 0.0;
1451     }
1452
1453     return GdipSetLineBlend(line, factors, positions, num_points);
1454 }
1455
1456 GpStatus WINGDIPAPI GdipSetLineWrapMode(GpLineGradient *line,
1457     GpWrapMode wrap)
1458 {
1459     TRACE("(%p, %d)\n", line, wrap);
1460
1461     if(!line || wrap == WrapModeClamp)
1462         return InvalidParameter;
1463
1464     line->wrap = wrap;
1465
1466     return Ok;
1467 }
1468
1469 GpStatus WINGDIPAPI GdipSetPathGradientBlend(GpPathGradient *brush, GDIPCONST REAL *blend,
1470     GDIPCONST REAL *pos, INT count)
1471 {
1472     static int calls;
1473
1474     TRACE("(%p,%p,%p,%i)\n", brush, blend, pos, count);
1475
1476     if(!(calls++))
1477         FIXME("not implemented\n");
1478
1479     return NotImplemented;
1480 }
1481
1482 GpStatus WINGDIPAPI GdipSetPathGradientPresetBlend(GpPathGradient *brush,
1483     GDIPCONST ARGB *blend, GDIPCONST REAL *pos, INT count)
1484 {
1485     FIXME("(%p,%p,%p,%i): stub\n", brush, blend, pos, count);
1486     return NotImplemented;
1487 }
1488
1489 GpStatus WINGDIPAPI GdipSetPathGradientCenterColor(GpPathGradient *grad,
1490     ARGB argb)
1491 {
1492     TRACE("(%p, %x)\n", grad, argb);
1493
1494     if(!grad)
1495         return InvalidParameter;
1496
1497     grad->centercolor = argb;
1498     grad->brush.lb.lbColor = ARGB2COLORREF(argb);
1499
1500     DeleteObject(grad->brush.gdibrush);
1501     grad->brush.gdibrush = CreateSolidBrush(grad->brush.lb.lbColor);
1502
1503     return Ok;
1504 }
1505
1506 GpStatus WINGDIPAPI GdipSetPathGradientCenterPoint(GpPathGradient *grad,
1507     GpPointF *point)
1508 {
1509     TRACE("(%p, %s)\n", grad, debugstr_pointf(point));
1510
1511     if(!grad || !point)
1512         return InvalidParameter;
1513
1514     grad->center.X = point->X;
1515     grad->center.Y = point->Y;
1516
1517     return Ok;
1518 }
1519
1520 GpStatus WINGDIPAPI GdipSetPathGradientCenterPointI(GpPathGradient *grad,
1521     GpPoint *point)
1522 {
1523     GpPointF ptf;
1524
1525     TRACE("(%p, %p)\n", grad, point);
1526
1527     if(!point)
1528         return InvalidParameter;
1529
1530     ptf.X = (REAL)point->X;
1531     ptf.Y = (REAL)point->Y;
1532
1533     return GdipSetPathGradientCenterPoint(grad,&ptf);
1534 }
1535
1536 GpStatus WINGDIPAPI GdipSetPathGradientFocusScales(GpPathGradient *grad,
1537     REAL x, REAL y)
1538 {
1539     TRACE("(%p, %.2f, %.2f)\n", grad, x, y);
1540
1541     if(!grad)
1542         return InvalidParameter;
1543
1544     grad->focus.X = x;
1545     grad->focus.Y = y;
1546
1547     return Ok;
1548 }
1549
1550 GpStatus WINGDIPAPI GdipSetPathGradientGammaCorrection(GpPathGradient *grad,
1551     BOOL gamma)
1552 {
1553     TRACE("(%p, %d)\n", grad, gamma);
1554
1555     if(!grad)
1556         return InvalidParameter;
1557
1558     grad->gamma = gamma;
1559
1560     return Ok;
1561 }
1562
1563 GpStatus WINGDIPAPI GdipSetPathGradientSigmaBlend(GpPathGradient *grad,
1564     REAL focus, REAL scale)
1565 {
1566     static int calls;
1567
1568     TRACE("(%p,%0.2f,%0.2f)\n", grad, focus, scale);
1569
1570     if(!grad || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
1571         return InvalidParameter;
1572
1573     if(!(calls++))
1574         FIXME("not implemented\n");
1575
1576     return NotImplemented;
1577 }
1578
1579 GpStatus WINGDIPAPI GdipSetPathGradientSurroundColorsWithCount(GpPathGradient
1580     *grad, ARGB *argb, INT *count)
1581 {
1582     static int calls;
1583
1584     TRACE("(%p,%p,%p)\n", grad, argb, count);
1585
1586     if(!grad || !argb || !count || (*count <= 0) ||
1587         (*count > grad->pathdata.Count))
1588         return InvalidParameter;
1589
1590     if(!(calls++))
1591         FIXME("not implemented\n");
1592
1593     return NotImplemented;
1594 }
1595
1596 GpStatus WINGDIPAPI GdipSetPathGradientWrapMode(GpPathGradient *grad,
1597     GpWrapMode wrap)
1598 {
1599     TRACE("(%p, %d)\n", grad, wrap);
1600
1601     if(!grad)
1602         return InvalidParameter;
1603
1604     grad->wrap = wrap;
1605
1606     return Ok;
1607 }
1608
1609 GpStatus WINGDIPAPI GdipSetSolidFillColor(GpSolidFill *sf, ARGB argb)
1610 {
1611     TRACE("(%p, %x)\n", sf, argb);
1612
1613     if(!sf)
1614         return InvalidParameter;
1615
1616     sf->color = argb;
1617     sf->brush.lb.lbColor = ARGB2COLORREF(argb);
1618
1619     DeleteObject(sf->brush.gdibrush);
1620     sf->brush.gdibrush = CreateSolidBrush(sf->brush.lb.lbColor);
1621
1622     return Ok;
1623 }
1624
1625 /******************************************************************************
1626  * GdipSetTextureTransform [GDIPLUS.@]
1627  */
1628 GpStatus WINGDIPAPI GdipSetTextureTransform(GpTexture *texture,
1629     GDIPCONST GpMatrix *matrix)
1630 {
1631     TRACE("(%p, %p)\n", texture, matrix);
1632
1633     if(!texture || !matrix)
1634         return InvalidParameter;
1635
1636     memcpy(texture->transform, matrix, sizeof(GpMatrix));
1637
1638     return Ok;
1639 }
1640
1641 /******************************************************************************
1642  * GdipSetTextureWrapMode [GDIPLUS.@]
1643  *
1644  * WrapMode not used, only stored
1645  */
1646 GpStatus WINGDIPAPI GdipSetTextureWrapMode(GpTexture *brush, GpWrapMode wrapmode)
1647 {
1648     TRACE("(%p, %d)\n", brush, wrapmode);
1649
1650     if(!brush)
1651         return InvalidParameter;
1652
1653     brush->wrap = wrapmode;
1654
1655     return Ok;
1656 }
1657
1658 GpStatus WINGDIPAPI GdipSetLineColors(GpLineGradient *brush, ARGB color1,
1659     ARGB color2)
1660 {
1661     TRACE("(%p, %x, %x)\n", brush, color1, color2);
1662
1663     if(!brush)
1664         return InvalidParameter;
1665
1666     brush->startcolor = color1;
1667     brush->endcolor   = color2;
1668
1669     return Ok;
1670 }
1671
1672 GpStatus WINGDIPAPI GdipGetLineColors(GpLineGradient *brush, ARGB *colors)
1673 {
1674     TRACE("(%p, %p)\n", brush, colors);
1675
1676     if(!brush || !colors)
1677         return InvalidParameter;
1678
1679     colors[0] = brush->startcolor;
1680     colors[1] = brush->endcolor;
1681
1682     return Ok;
1683 }
1684
1685 /******************************************************************************
1686  * GdipRotateTextureTransform [GDIPLUS.@]
1687  */
1688 GpStatus WINGDIPAPI GdipRotateTextureTransform(GpTexture* brush, REAL angle,
1689     GpMatrixOrder order)
1690 {
1691     TRACE("(%p, %.2f, %d)\n", brush, angle, order);
1692
1693     if(!brush)
1694         return InvalidParameter;
1695
1696     return GdipRotateMatrix(brush->transform, angle, order);
1697 }
1698
1699 GpStatus WINGDIPAPI GdipSetLineLinearBlend(GpLineGradient *brush, REAL focus,
1700     REAL scale)
1701 {
1702     REAL factors[3];
1703     REAL positions[3];
1704     int num_points = 0;
1705
1706     TRACE("(%p,%.2f,%.2f)\n", brush, focus, scale);
1707
1708     if (!brush) return InvalidParameter;
1709
1710     if (focus != 0.0)
1711     {
1712         factors[num_points] = 0.0;
1713         positions[num_points] = 0.0;
1714         num_points++;
1715     }
1716
1717     factors[num_points] = scale;
1718     positions[num_points] = focus;
1719     num_points++;
1720
1721     if (focus != 1.0)
1722     {
1723         factors[num_points] = 0.0;
1724         positions[num_points] = 1.0;
1725         num_points++;
1726     }
1727
1728     return GdipSetLineBlend(brush, factors, positions, num_points);
1729 }
1730
1731 GpStatus WINGDIPAPI GdipSetLinePresetBlend(GpLineGradient *brush,
1732     GDIPCONST ARGB *blend, GDIPCONST REAL* positions, INT count)
1733 {
1734     ARGB *new_color;
1735     REAL *new_pos;
1736     TRACE("(%p,%p,%p,%i)\n", brush, blend, positions, count);
1737
1738     if (!brush || !blend || !positions || count < 2 ||
1739         positions[0] != 0.0f || positions[count-1] != 1.0f)
1740     {
1741         return InvalidParameter;
1742     }
1743
1744     new_color = GdipAlloc(count * sizeof(ARGB));
1745     new_pos = GdipAlloc(count * sizeof(REAL));
1746     if (!new_color || !new_pos)
1747     {
1748         GdipFree(new_color);
1749         GdipFree(new_pos);
1750         return OutOfMemory;
1751     }
1752
1753     memcpy(new_color, blend, sizeof(ARGB) * count);
1754     memcpy(new_pos, positions, sizeof(REAL) * count);
1755
1756     GdipFree(brush->pblendcolor);
1757     GdipFree(brush->pblendpos);
1758
1759     brush->pblendcolor = new_color;
1760     brush->pblendpos = new_pos;
1761     brush->pblendcount = count;
1762
1763     return Ok;
1764 }
1765
1766 GpStatus WINGDIPAPI GdipGetLinePresetBlend(GpLineGradient *brush,
1767     ARGB *blend, REAL* positions, INT count)
1768 {
1769     if (!brush || !blend || !positions || count < 2)
1770         return InvalidParameter;
1771
1772     if (brush->pblendcount == 0)
1773         return GenericError;
1774
1775     if (count < brush->pblendcount)
1776         return InsufficientBuffer;
1777
1778     memcpy(blend, brush->pblendcolor, sizeof(ARGB) * brush->pblendcount);
1779     memcpy(positions, brush->pblendpos, sizeof(REAL) * brush->pblendcount);
1780
1781     return Ok;
1782 }
1783
1784 GpStatus WINGDIPAPI GdipGetLinePresetBlendCount(GpLineGradient *brush,
1785     INT *count)
1786 {
1787     if (!brush || !count)
1788         return InvalidParameter;
1789
1790     *count = brush->pblendcount;
1791
1792     return Ok;
1793 }
1794
1795 GpStatus WINGDIPAPI GdipResetLineTransform(GpLineGradient *brush)
1796 {
1797     static int calls;
1798
1799     TRACE("(%p)\n", brush);
1800
1801     if(!(calls++))
1802         FIXME("not implemented\n");
1803
1804     return NotImplemented;
1805 }
1806
1807 GpStatus WINGDIPAPI GdipSetLineTransform(GpLineGradient *brush,
1808     GDIPCONST GpMatrix *matrix)
1809 {
1810     static int calls;
1811
1812     TRACE("(%p,%p)\n", brush,  matrix);
1813
1814     if(!(calls++))
1815         FIXME("not implemented\n");
1816
1817     return NotImplemented;
1818 }
1819
1820 GpStatus WINGDIPAPI GdipScaleLineTransform(GpLineGradient *brush, REAL sx, REAL sy,
1821     GpMatrixOrder order)
1822 {
1823     static int calls;
1824
1825     TRACE("(%p,%0.2f,%0.2f,%u)\n", brush, sx, sy, order);
1826
1827     if(!(calls++))
1828         FIXME("not implemented\n");
1829
1830     return NotImplemented;
1831 }
1832
1833 GpStatus WINGDIPAPI GdipTranslateLineTransform(GpLineGradient* brush,
1834         REAL dx, REAL dy, GpMatrixOrder order)
1835 {
1836     FIXME("stub: %p %f %f %d\n", brush, dx, dy, order);
1837
1838     return NotImplemented;
1839 }
1840
1841 /******************************************************************************
1842  * GdipTranslateTextureTransform [GDIPLUS.@]
1843  */
1844 GpStatus WINGDIPAPI GdipTranslateTextureTransform(GpTexture* brush, REAL dx, REAL dy,
1845     GpMatrixOrder order)
1846 {
1847     TRACE("(%p, %.2f, %.2f, %d)\n", brush, dx, dy, order);
1848
1849     if(!brush)
1850         return InvalidParameter;
1851
1852     return GdipTranslateMatrix(brush->transform, dx, dy, order);
1853 }
1854
1855 GpStatus WINGDIPAPI GdipGetLineRect(GpLineGradient *brush, GpRectF *rect)
1856 {
1857     TRACE("(%p, %p)\n", brush, rect);
1858
1859     if(!brush || !rect)
1860         return InvalidParameter;
1861
1862     *rect = brush->rect;
1863
1864     return Ok;
1865 }
1866
1867 GpStatus WINGDIPAPI GdipGetLineRectI(GpLineGradient *brush, GpRect *rect)
1868 {
1869     GpRectF  rectF;
1870     GpStatus ret;
1871
1872     TRACE("(%p, %p)\n", brush, rect);
1873
1874     if(!rect)
1875         return InvalidParameter;
1876
1877     ret = GdipGetLineRect(brush, &rectF);
1878
1879     if(ret == Ok){
1880         rect->X      = roundr(rectF.X);
1881         rect->Y      = roundr(rectF.Y);
1882         rect->Width  = roundr(rectF.Width);
1883         rect->Height = roundr(rectF.Height);
1884     }
1885
1886     return ret;
1887 }
1888
1889 GpStatus WINGDIPAPI GdipRotateLineTransform(GpLineGradient* brush,
1890     REAL angle, GpMatrixOrder order)
1891 {
1892     static int calls;
1893
1894     TRACE("(%p,%0.2f,%u)\n", brush, angle, order);
1895
1896     if(!brush)
1897         return InvalidParameter;
1898
1899     if(!(calls++))
1900         FIXME("(%p, %.2f, %d) stub\n", brush, angle, order);
1901
1902     return NotImplemented;
1903 }