45de034423766512258e2abaf2b82fb64e9c8041
[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 GpStatus WINGDIPAPI GdipCloneBrush(GpBrush *brush, GpBrush **clone)
38 {
39 if(!brush || !clone)
40 return InvalidParameter;
41
42 switch(brush->bt){
43 case BrushTypeSolidColor:
44 *clone = GdipAlloc(sizeof(GpSolidFill));
45 if (!*clone) return OutOfMemory;
46
47 memcpy(*clone, brush, sizeof(GpSolidFill));
48
49 (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
50 break;
51 case BrushTypePathGradient:{
52 GpPathGradient *src, *dest;
53 INT count;
54
55 *clone = GdipAlloc(sizeof(GpPathGradient));
56 if (!*clone) return OutOfMemory;
57
58 src = (GpPathGradient*) brush,
59 dest = (GpPathGradient*) *clone;
60 count = src->pathdata.Count;
61
62 memcpy(dest, src, sizeof(GpPathGradient));
63
64 dest->pathdata.Count = count;
65 dest->pathdata.Points = GdipAlloc(count * sizeof(PointF));
66 dest->pathdata.Types = GdipAlloc(count);
67
68 if(!dest->pathdata.Points || !dest->pathdata.Types){
69 GdipFree(dest->pathdata.Points);
70 GdipFree(dest->pathdata.Types);
71 GdipFree(dest);
72 return OutOfMemory;
73 }
74
75 memcpy(dest->pathdata.Points, src->pathdata.Points, count * sizeof(PointF));
76 memcpy(dest->pathdata.Types, src->pathdata.Types, count);
77
78 break;
79 }
80 case BrushTypeLinearGradient:
81 *clone = GdipAlloc(sizeof(GpLineGradient));
82 if(!*clone) return OutOfMemory;
83
84 memcpy(*clone, brush, sizeof(GpLineGradient));
85
86 (*clone)->gdibrush = CreateSolidBrush((*clone)->lb.lbColor);
87 break;
88 case BrushTypeTextureFill:
89 *clone = GdipAlloc(sizeof(GpTexture));
90 if(!*clone) return OutOfMemory;
91
92 memcpy(*clone, brush, sizeof(GpTexture));
93
94 (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
95 break;
96 default:
97 ERR("not implemented for brush type %d\n", brush->bt);
98 return NotImplemented;
99 }
100
101 return Ok;
102 }
103
104 GpStatus WINGDIPAPI GdipCreateLineBrush(GDIPCONST GpPointF* startpoint,
105 GDIPCONST GpPointF* endpoint, ARGB startcolor, ARGB endcolor,
106 GpWrapMode wrap, GpLineGradient **line)
107 {
108 COLORREF col = ARGB2COLORREF(startcolor);
109
110 if(!line || !startpoint || !endpoint || wrap == WrapModeClamp)
111 return InvalidParameter;
112
113 *line = GdipAlloc(sizeof(GpLineGradient));
114 if(!*line) return OutOfMemory;
115
116 (*line)->brush.lb.lbStyle = BS_SOLID;
117 (*line)->brush.lb.lbColor = col;
118 (*line)->brush.lb.lbHatch = 0;
119 (*line)->brush.gdibrush = CreateSolidBrush(col);
120 (*line)->brush.bt = BrushTypeLinearGradient;
121
122 (*line)->startpoint.X = startpoint->X;
123 (*line)->startpoint.Y = startpoint->Y;
124 (*line)->endpoint.X = endpoint->X;
125 (*line)->endpoint.Y = endpoint->Y;
126 (*line)->startcolor = startcolor;
127 (*line)->endcolor = endcolor;
128 (*line)->wrap = wrap;
129 (*line)->gamma = FALSE;
130
131 return Ok;
132 }
133
134 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectI(GDIPCONST GpRect* rect,
135 ARGB startcolor, ARGB endcolor, LinearGradientMode mode, GpWrapMode wrap,
136 GpLineGradient **line)
137 {
138 GpPointF start, end;
139
140 if(!line || !rect)
141 return InvalidParameter;
142
143 start.X = (REAL) rect->X;
144 start.Y = (REAL) rect->Y;
145 end.X = (REAL) (rect->X + rect->Width);
146 end.Y = (REAL) (rect->Y + rect->Height);
147
148 return GdipCreateLineBrush(&start, &end, startcolor, endcolor, wrap, line);
149 }
150
151 GpStatus WINGDIPAPI GdipCreatePathGradient(GDIPCONST GpPointF* points,
152 INT count, GpWrapMode wrap, GpPathGradient **grad)
153 {
154 COLORREF col = ARGB2COLORREF(0xffffffff);
155
156 if(!points || !grad)
157 return InvalidParameter;
158
159 if(count <= 0)
160 return OutOfMemory;
161
162 *grad = GdipAlloc(sizeof(GpPathGradient));
163 if (!*grad) return OutOfMemory;
164
165 (*grad)->pathdata.Count = count;
166 (*grad)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
167 (*grad)->pathdata.Types = GdipAlloc(count);
168
169 if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
170 GdipFree((*grad)->pathdata.Points);
171 GdipFree((*grad)->pathdata.Types);
172 GdipFree(*grad);
173 return OutOfMemory;
174 }
175
176 memcpy((*grad)->pathdata.Points, points, count * sizeof(PointF));
177 memset((*grad)->pathdata.Types, PathPointTypeLine, count);
178
179 (*grad)->brush.lb.lbStyle = BS_SOLID;
180 (*grad)->brush.lb.lbColor = col;
181 (*grad)->brush.lb.lbHatch = 0;
182
183 (*grad)->brush.gdibrush = CreateSolidBrush(col);
184 (*grad)->brush.bt = BrushTypePathGradient;
185 (*grad)->centercolor = 0xffffffff;
186 (*grad)->wrap = wrap;
187 (*grad)->gamma = FALSE;
188 (*grad)->center.X = 0.0;
189 (*grad)->center.Y = 0.0;
190 (*grad)->focus.X = 0.0;
191 (*grad)->focus.Y = 0.0;
192
193 return Ok;
194 }
195
196 /* FIXME: path gradient brushes not truly supported (drawn as solid brushes) */
197 GpStatus WINGDIPAPI GdipCreatePathGradientFromPath(GDIPCONST GpPath* path,
198 GpPathGradient **grad)
199 {
200 COLORREF col = ARGB2COLORREF(0xffffffff);
201
202 if(!path || !grad)
203 return InvalidParameter;
204
205 *grad = GdipAlloc(sizeof(GpPathGradient));
206 if (!*grad) return OutOfMemory;
207
208 (*grad)->pathdata.Count = path->pathdata.Count;
209 (*grad)->pathdata.Points = GdipAlloc(path->pathdata.Count * sizeof(PointF));
210 (*grad)->pathdata.Types = GdipAlloc(path->pathdata.Count);
211
212 if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
213 GdipFree((*grad)->pathdata.Points);
214 GdipFree((*grad)->pathdata.Types);
215 GdipFree(*grad);
216 return OutOfMemory;
217 }
218
219 memcpy((*grad)->pathdata.Points, path->pathdata.Points,
220 path->pathdata.Count * sizeof(PointF));
221 memcpy((*grad)->pathdata.Types, path->pathdata.Types, path->pathdata.Count);
222
223 (*grad)->brush.lb.lbStyle = BS_SOLID;
224 (*grad)->brush.lb.lbColor = col;
225 (*grad)->brush.lb.lbHatch = 0;
226
227 (*grad)->brush.gdibrush = CreateSolidBrush(col);
228 (*grad)->brush.bt = BrushTypePathGradient;
229 (*grad)->centercolor = 0xffffffff;
230 (*grad)->wrap = WrapModeClamp;
231 (*grad)->gamma = FALSE;
232 /* FIXME: this should be set to the "centroid" of the path by default */
233 (*grad)->center.X = 0.0;
234 (*grad)->center.Y = 0.0;
235 (*grad)->focus.X = 0.0;
236 (*grad)->focus.Y = 0.0;
237
238 return Ok;
239 }
240
241 GpStatus WINGDIPAPI GdipCreateSolidFill(ARGB color, GpSolidFill **sf)
242 {
243 COLORREF col = ARGB2COLORREF(color);
244
245 if(!sf) return InvalidParameter;
246
247 *sf = GdipAlloc(sizeof(GpSolidFill));
248 if (!*sf) return OutOfMemory;
249
250 (*sf)->brush.lb.lbStyle = BS_SOLID;
251 (*sf)->brush.lb.lbColor = col;
252 (*sf)->brush.lb.lbHatch = 0;
253
254 (*sf)->brush.gdibrush = CreateSolidBrush(col);
255 (*sf)->brush.bt = BrushTypeSolidColor;
256 (*sf)->color = color;
257
258 return Ok;
259 }
260
261 /* FIXME: imageattr ignored */
262 GpStatus WINGDIPAPI GdipCreateTextureIA(GpImage *image,
263 GDIPCONST GpImageAttributes *imageattr, REAL x, REAL y, REAL width,
264 REAL height, GpTexture **texture)
265 {
266 HDC hdc;
267 OLE_HANDLE hbm;
268 HBITMAP old = NULL;
269 BITMAPINFO bmi;
270 BITMAPINFOHEADER *bmih;
271 INT n_x, n_y, n_width, n_height, abs_height, stride, image_stride, i, bytespp;
272 BOOL bm_is_selected;
273 BYTE *dibits, *buff, *textbits;
274
275 if(!image || !texture || x < 0.0 || y < 0.0 || width < 0.0 || height < 0.0)
276 return InvalidParameter;
277
278 if(image->type != ImageTypeBitmap){
279 FIXME("not implemented for image type %d\n", image->type);
280 return NotImplemented;
281 }
282
283 n_x = roundr(x);
284 n_y = roundr(y);
285 n_width = roundr(width);
286 n_height = roundr(height);
287
288 if(n_x + n_width > ((GpBitmap*)image)->width ||
289 n_y + n_height > ((GpBitmap*)image)->height)
290 return InvalidParameter;
291
292 IPicture_get_Handle(image->picture, &hbm);
293 if(!hbm) return GenericError;
294 IPicture_get_CurDC(image->picture, &hdc);
295 bm_is_selected = (hdc != 0);
296
297 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
298 bmi.bmiHeader.biBitCount = 0;
299
300 if(!bm_is_selected){
301 hdc = CreateCompatibleDC(0);
302 old = SelectObject(hdc, (HBITMAP)hbm);
303 }
304
305 /* fill out bmi */
306 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
307
308 bytespp = bmi.bmiHeader.biBitCount / 8;
309 abs_height = abs(bmi.bmiHeader.biHeight);
310
311 if(n_x > bmi.bmiHeader.biWidth || n_x + n_width > bmi.bmiHeader.biWidth ||
312 n_y > abs_height || n_y + n_height > abs_height)
313 return InvalidParameter;
314
315 dibits = GdipAlloc(bmi.bmiHeader.biSizeImage);
316
317 if(dibits) /* this is not a good place to error out */
318 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, dibits, &bmi, DIB_RGB_COLORS);
319
320 if(!bm_is_selected){
321 SelectObject(hdc, old);
322 DeleteDC(hdc);
323 }
324
325 if(!dibits)
326 return OutOfMemory;
327
328 image_stride = (bmi.bmiHeader.biWidth * bytespp + 3) & ~3;
329 stride = (n_width * bytespp + 3) & ~3;
330 buff = GdipAlloc(sizeof(BITMAPINFOHEADER) + stride * n_height);
331 if(!buff){
332 GdipFree(dibits);
333 return OutOfMemory;
334 }
335
336 bmih = (BITMAPINFOHEADER*)buff;
337 textbits = (BYTE*) (bmih + 1);
338 bmih->biSize = sizeof(BITMAPINFOHEADER);
339 bmih->biWidth = n_width;
340 bmih->biHeight = n_height;
341 bmih->biCompression = BI_RGB;
342 bmih->biSizeImage = stride * n_height;
343 bmih->biBitCount = bmi.bmiHeader.biBitCount;
344 bmih->biClrUsed = 0;
345 bmih->biPlanes = 1;
346
347 /* image is flipped */
348 if(bmi.bmiHeader.biHeight > 0){
349 dibits += bmi.bmiHeader.biSizeImage;
350 image_stride *= -1;
351 textbits += stride * (n_height - 1);
352 stride *= -1;
353 }
354
355 for(i = 0; i < n_height; i++)
356 memcpy(&textbits[i * stride],
357 &dibits[n_x * bytespp + (n_y + i) * image_stride],
358 abs(stride));
359
360 *texture = GdipAlloc(sizeof(GpTexture));
361 if (!*texture) return OutOfMemory;
362
363 (*texture)->brush.lb.lbStyle = BS_DIBPATTERNPT;
364 (*texture)->brush.lb.lbColor = DIB_RGB_COLORS;
365 (*texture)->brush.lb.lbHatch = (ULONG_PTR)buff;
366
367 (*texture)->brush.gdibrush = CreateBrushIndirect(&(*texture)->brush.lb);
368 (*texture)->brush.bt = BrushTypeTextureFill;
369
370 GdipFree(dibits);
371 GdipFree(buff);
372
373 return Ok;
374 }
375
376 GpStatus WINGDIPAPI GdipGetBrushType(GpBrush *brush, GpBrushType *type)
377 {
378 if(!brush || !type) return InvalidParameter;
379
380 *type = brush->bt;
381
382 return Ok;
383 }
384
385 GpStatus WINGDIPAPI GdipDeleteBrush(GpBrush *brush)
386 {
387 if(!brush) return InvalidParameter;
388
389 switch(brush->bt)
390 {
391 case BrushTypePathGradient:
392 GdipFree(((GpPathGradient*) brush)->pathdata.Points);
393 GdipFree(((GpPathGradient*) brush)->pathdata.Types);
394 break;
395 case BrushTypeSolidColor:
396 case BrushTypeLinearGradient:
397 case BrushTypeTextureFill:
398 default:
399 break;
400 }
401
402 DeleteObject(brush->gdibrush);
403 GdipFree(brush);
404
405 return Ok;
406 }
407
408 GpStatus WINGDIPAPI GdipGetLineGammaCorrection(GpLineGradient *line,
409 BOOL *usinggamma)
410 {
411 if(!line)
412 return InvalidParameter;
413
414 *usinggamma = line->gamma;
415
416 return Ok;
417 }
418
419 GpStatus WINGDIPAPI GdipGetPathGradientCenterPoint(GpPathGradient *grad,
420 GpPointF *point)
421 {
422 if(!grad || !point)
423 return InvalidParameter;
424
425 point->X = grad->center.X;
426 point->Y = grad->center.Y;
427
428 return Ok;
429 }
430
431 GpStatus WINGDIPAPI GdipGetPathGradientFocusScales(GpPathGradient *grad,
432 REAL *x, REAL *y)
433 {
434 if(!grad || !x || !y)
435 return InvalidParameter;
436
437 *x = grad->focus.X;
438 *y = grad->focus.Y;
439
440 return Ok;
441 }
442
443 GpStatus WINGDIPAPI GdipGetPathGradientGammaCorrection(GpPathGradient *grad,
444 BOOL *gamma)
445 {
446 if(!grad || !gamma)
447 return InvalidParameter;
448
449 *gamma = grad->gamma;
450
451 return Ok;
452 }
453
454 GpStatus WINGDIPAPI GdipGetPathGradientPointCount(GpPathGradient *grad,
455 INT *count)
456 {
457 if(!grad || !count)
458 return InvalidParameter;
459
460 *count = grad->pathdata.Count;
461
462 return Ok;
463 }
464
465 GpStatus WINGDIPAPI GdipGetPathGradientSurroundColorsWithCount(GpPathGradient
466 *grad, ARGB *argb, INT *count)
467 {
468 static int calls;
469
470 if(!grad || !argb || !count || (*count < grad->pathdata.Count))
471 return InvalidParameter;
472
473 if(!(calls++))
474 FIXME("not implemented\n");
475
476 return NotImplemented;
477 }
478
479 GpStatus WINGDIPAPI GdipGetSolidFillColor(GpSolidFill *sf, ARGB *argb)
480 {
481 if(!sf || !argb)
482 return InvalidParameter;
483
484 *argb = sf->color;
485
486 return Ok;
487 }
488
489 GpStatus WINGDIPAPI GdipSetLineBlend(GpLineGradient *brush,
490 GDIPCONST REAL *blend, GDIPCONST REAL* positions, INT count)
491 {
492 static int calls;
493
494 if(!brush || !blend || !positions || count <= 0)
495 return InvalidParameter;
496
497 if(!(calls++))
498 FIXME("not implemented\n");
499
500 return Ok;
501 }
502
503 GpStatus WINGDIPAPI GdipSetLineGammaCorrection(GpLineGradient *line,
504 BOOL usegamma)
505 {
506 if(!line)
507 return InvalidParameter;
508
509 line->gamma = usegamma;
510
511 return Ok;
512 }
513
514 GpStatus WINGDIPAPI GdipSetLineSigmaBlend(GpLineGradient *line, REAL focus,
515 REAL scale)
516 {
517 static int calls;
518
519 if(!line || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
520 return InvalidParameter;
521
522 if(!(calls++))
523 FIXME("not implemented\n");
524
525 return NotImplemented;
526 }
527
528 GpStatus WINGDIPAPI GdipSetLineWrapMode(GpLineGradient *line,
529 GpWrapMode wrap)
530 {
531 if(!line || wrap == WrapModeClamp)
532 return InvalidParameter;
533
534 line->wrap = wrap;
535
536 return Ok;
537 }
538
539 GpStatus WINGDIPAPI GdipSetPathGradientCenterColor(GpPathGradient *grad,
540 ARGB argb)
541 {
542 if(!grad)
543 return InvalidParameter;
544
545 grad->centercolor = argb;
546 grad->brush.lb.lbColor = ARGB2COLORREF(argb);
547
548 DeleteObject(grad->brush.gdibrush);
549 grad->brush.gdibrush = CreateSolidBrush(grad->brush.lb.lbColor);
550
551 return Ok;
552 }
553
554 GpStatus WINGDIPAPI GdipSetPathGradientCenterPoint(GpPathGradient *grad,
555 GpPointF *point)
556 {
557 if(!grad || !point)
558 return InvalidParameter;
559
560 grad->center.X = point->X;
561 grad->center.Y = point->Y;
562
563 return Ok;
564 }
565
566 GpStatus WINGDIPAPI GdipSetPathGradientFocusScales(GpPathGradient *grad,
567 REAL x, REAL y)
568 {
569 if(!grad)
570 return InvalidParameter;
571
572 grad->focus.X = x;
573 grad->focus.Y = y;
574
575 return Ok;
576 }
577
578 GpStatus WINGDIPAPI GdipSetPathGradientGammaCorrection(GpPathGradient *grad,
579 BOOL gamma)
580 {
581 if(!grad)
582 return InvalidParameter;
583
584 grad->gamma = gamma;
585
586 return Ok;
587 }
588
589 GpStatus WINGDIPAPI GdipSetPathGradientSigmaBlend(GpPathGradient *grad,
590 REAL focus, REAL scale)
591 {
592 static int calls;
593
594 if(!grad || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
595 return InvalidParameter;
596
597 if(!(calls++))
598 FIXME("not implemented\n");
599
600 return NotImplemented;
601 }
602
603 GpStatus WINGDIPAPI GdipSetPathGradientSurroundColorsWithCount(GpPathGradient
604 *grad, ARGB *argb, INT *count)
605 {
606 static int calls;
607
608 if(!grad || !argb || !count || (*count <= 0) ||
609 (*count > grad->pathdata.Count))
610 return InvalidParameter;
611
612 if(!(calls++))
613 FIXME("not implemented\n");
614
615 return NotImplemented;
616 }
617
618 GpStatus WINGDIPAPI GdipSetPathGradientWrapMode(GpPathGradient *grad,
619 GpWrapMode wrap)
620 {
621 if(!grad)
622 return InvalidParameter;
623
624 grad->wrap = wrap;
625
626 return Ok;
627 }
628
629 GpStatus WINGDIPAPI GdipSetSolidFillColor(GpSolidFill *sf, ARGB argb)
630 {
631 if(!sf)
632 return InvalidParameter;
633
634 sf->color = argb;
635 sf->brush.lb.lbColor = ARGB2COLORREF(argb);
636
637 DeleteObject(sf->brush.gdibrush);
638 sf->brush.gdibrush = CreateSolidBrush(sf->brush.lb.lbColor);
639
640 return Ok;
641 }
642
643 GpStatus WINGDIPAPI GdipSetTextureTransform(GpTexture *texture,
644 GDIPCONST GpMatrix *matrix)
645 {
646 static int calls;
647
648 if(!texture || !matrix)
649 return InvalidParameter;
650
651 if(!(calls++))
652 FIXME("not implemented\n");
653
654 return Ok;
655 }