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