rsaenh: Use LocalFree to free memory return by Crypt(Un)ProtectData.
[wine/multimedia.git] / dlls / gdiplus / brush.c
blob45de034423766512258e2abaf2b82fb64e9c8041
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
37 GpStatus WINGDIPAPI GdipCloneBrush(GpBrush *brush, GpBrush **clone)
39 if(!brush || !clone)
40 return InvalidParameter;
42 switch(brush->bt){
43 case BrushTypeSolidColor:
44 *clone = GdipAlloc(sizeof(GpSolidFill));
45 if (!*clone) return OutOfMemory;
47 memcpy(*clone, brush, sizeof(GpSolidFill));
49 (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
50 break;
51 case BrushTypePathGradient:{
52 GpPathGradient *src, *dest;
53 INT count;
55 *clone = GdipAlloc(sizeof(GpPathGradient));
56 if (!*clone) return OutOfMemory;
58 src = (GpPathGradient*) brush,
59 dest = (GpPathGradient*) *clone;
60 count = src->pathdata.Count;
62 memcpy(dest, src, sizeof(GpPathGradient));
64 dest->pathdata.Count = count;
65 dest->pathdata.Points = GdipAlloc(count * sizeof(PointF));
66 dest->pathdata.Types = GdipAlloc(count);
68 if(!dest->pathdata.Points || !dest->pathdata.Types){
69 GdipFree(dest->pathdata.Points);
70 GdipFree(dest->pathdata.Types);
71 GdipFree(dest);
72 return OutOfMemory;
75 memcpy(dest->pathdata.Points, src->pathdata.Points, count * sizeof(PointF));
76 memcpy(dest->pathdata.Types, src->pathdata.Types, count);
78 break;
80 case BrushTypeLinearGradient:
81 *clone = GdipAlloc(sizeof(GpLineGradient));
82 if(!*clone) return OutOfMemory;
84 memcpy(*clone, brush, sizeof(GpLineGradient));
86 (*clone)->gdibrush = CreateSolidBrush((*clone)->lb.lbColor);
87 break;
88 case BrushTypeTextureFill:
89 *clone = GdipAlloc(sizeof(GpTexture));
90 if(!*clone) return OutOfMemory;
92 memcpy(*clone, brush, sizeof(GpTexture));
94 (*clone)->gdibrush = CreateBrushIndirect(&(*clone)->lb);
95 break;
96 default:
97 ERR("not implemented for brush type %d\n", brush->bt);
98 return NotImplemented;
101 return Ok;
104 GpStatus WINGDIPAPI GdipCreateLineBrush(GDIPCONST GpPointF* startpoint,
105 GDIPCONST GpPointF* endpoint, ARGB startcolor, ARGB endcolor,
106 GpWrapMode wrap, GpLineGradient **line)
108 COLORREF col = ARGB2COLORREF(startcolor);
110 if(!line || !startpoint || !endpoint || wrap == WrapModeClamp)
111 return InvalidParameter;
113 *line = GdipAlloc(sizeof(GpLineGradient));
114 if(!*line) return OutOfMemory;
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;
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;
131 return Ok;
134 GpStatus WINGDIPAPI GdipCreateLineBrushFromRectI(GDIPCONST GpRect* rect,
135 ARGB startcolor, ARGB endcolor, LinearGradientMode mode, GpWrapMode wrap,
136 GpLineGradient **line)
138 GpPointF start, end;
140 if(!line || !rect)
141 return InvalidParameter;
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);
148 return GdipCreateLineBrush(&start, &end, startcolor, endcolor, wrap, line);
151 GpStatus WINGDIPAPI GdipCreatePathGradient(GDIPCONST GpPointF* points,
152 INT count, GpWrapMode wrap, GpPathGradient **grad)
154 COLORREF col = ARGB2COLORREF(0xffffffff);
156 if(!points || !grad)
157 return InvalidParameter;
159 if(count <= 0)
160 return OutOfMemory;
162 *grad = GdipAlloc(sizeof(GpPathGradient));
163 if (!*grad) return OutOfMemory;
165 (*grad)->pathdata.Count = count;
166 (*grad)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
167 (*grad)->pathdata.Types = GdipAlloc(count);
169 if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
170 GdipFree((*grad)->pathdata.Points);
171 GdipFree((*grad)->pathdata.Types);
172 GdipFree(*grad);
173 return OutOfMemory;
176 memcpy((*grad)->pathdata.Points, points, count * sizeof(PointF));
177 memset((*grad)->pathdata.Types, PathPointTypeLine, count);
179 (*grad)->brush.lb.lbStyle = BS_SOLID;
180 (*grad)->brush.lb.lbColor = col;
181 (*grad)->brush.lb.lbHatch = 0;
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;
193 return Ok;
196 /* FIXME: path gradient brushes not truly supported (drawn as solid brushes) */
197 GpStatus WINGDIPAPI GdipCreatePathGradientFromPath(GDIPCONST GpPath* path,
198 GpPathGradient **grad)
200 COLORREF col = ARGB2COLORREF(0xffffffff);
202 if(!path || !grad)
203 return InvalidParameter;
205 *grad = GdipAlloc(sizeof(GpPathGradient));
206 if (!*grad) return OutOfMemory;
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);
212 if(!(*grad)->pathdata.Points || !(*grad)->pathdata.Types){
213 GdipFree((*grad)->pathdata.Points);
214 GdipFree((*grad)->pathdata.Types);
215 GdipFree(*grad);
216 return OutOfMemory;
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);
223 (*grad)->brush.lb.lbStyle = BS_SOLID;
224 (*grad)->brush.lb.lbColor = col;
225 (*grad)->brush.lb.lbHatch = 0;
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;
238 return Ok;
241 GpStatus WINGDIPAPI GdipCreateSolidFill(ARGB color, GpSolidFill **sf)
243 COLORREF col = ARGB2COLORREF(color);
245 if(!sf) return InvalidParameter;
247 *sf = GdipAlloc(sizeof(GpSolidFill));
248 if (!*sf) return OutOfMemory;
250 (*sf)->brush.lb.lbStyle = BS_SOLID;
251 (*sf)->brush.lb.lbColor = col;
252 (*sf)->brush.lb.lbHatch = 0;
254 (*sf)->brush.gdibrush = CreateSolidBrush(col);
255 (*sf)->brush.bt = BrushTypeSolidColor;
256 (*sf)->color = color;
258 return Ok;
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)
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;
275 if(!image || !texture || x < 0.0 || y < 0.0 || width < 0.0 || height < 0.0)
276 return InvalidParameter;
278 if(image->type != ImageTypeBitmap){
279 FIXME("not implemented for image type %d\n", image->type);
280 return NotImplemented;
283 n_x = roundr(x);
284 n_y = roundr(y);
285 n_width = roundr(width);
286 n_height = roundr(height);
288 if(n_x + n_width > ((GpBitmap*)image)->width ||
289 n_y + n_height > ((GpBitmap*)image)->height)
290 return InvalidParameter;
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);
297 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
298 bmi.bmiHeader.biBitCount = 0;
300 if(!bm_is_selected){
301 hdc = CreateCompatibleDC(0);
302 old = SelectObject(hdc, (HBITMAP)hbm);
305 /* fill out bmi */
306 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
308 bytespp = bmi.bmiHeader.biBitCount / 8;
309 abs_height = abs(bmi.bmiHeader.biHeight);
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;
315 dibits = GdipAlloc(bmi.bmiHeader.biSizeImage);
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);
320 if(!bm_is_selected){
321 SelectObject(hdc, old);
322 DeleteDC(hdc);
325 if(!dibits)
326 return OutOfMemory;
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;
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;
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;
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));
360 *texture = GdipAlloc(sizeof(GpTexture));
361 if (!*texture) return OutOfMemory;
363 (*texture)->brush.lb.lbStyle = BS_DIBPATTERNPT;
364 (*texture)->brush.lb.lbColor = DIB_RGB_COLORS;
365 (*texture)->brush.lb.lbHatch = (ULONG_PTR)buff;
367 (*texture)->brush.gdibrush = CreateBrushIndirect(&(*texture)->brush.lb);
368 (*texture)->brush.bt = BrushTypeTextureFill;
370 GdipFree(dibits);
371 GdipFree(buff);
373 return Ok;
376 GpStatus WINGDIPAPI GdipGetBrushType(GpBrush *brush, GpBrushType *type)
378 if(!brush || !type) return InvalidParameter;
380 *type = brush->bt;
382 return Ok;
385 GpStatus WINGDIPAPI GdipDeleteBrush(GpBrush *brush)
387 if(!brush) return InvalidParameter;
389 switch(brush->bt)
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;
402 DeleteObject(brush->gdibrush);
403 GdipFree(brush);
405 return Ok;
408 GpStatus WINGDIPAPI GdipGetLineGammaCorrection(GpLineGradient *line,
409 BOOL *usinggamma)
411 if(!line)
412 return InvalidParameter;
414 *usinggamma = line->gamma;
416 return Ok;
419 GpStatus WINGDIPAPI GdipGetPathGradientCenterPoint(GpPathGradient *grad,
420 GpPointF *point)
422 if(!grad || !point)
423 return InvalidParameter;
425 point->X = grad->center.X;
426 point->Y = grad->center.Y;
428 return Ok;
431 GpStatus WINGDIPAPI GdipGetPathGradientFocusScales(GpPathGradient *grad,
432 REAL *x, REAL *y)
434 if(!grad || !x || !y)
435 return InvalidParameter;
437 *x = grad->focus.X;
438 *y = grad->focus.Y;
440 return Ok;
443 GpStatus WINGDIPAPI GdipGetPathGradientGammaCorrection(GpPathGradient *grad,
444 BOOL *gamma)
446 if(!grad || !gamma)
447 return InvalidParameter;
449 *gamma = grad->gamma;
451 return Ok;
454 GpStatus WINGDIPAPI GdipGetPathGradientPointCount(GpPathGradient *grad,
455 INT *count)
457 if(!grad || !count)
458 return InvalidParameter;
460 *count = grad->pathdata.Count;
462 return Ok;
465 GpStatus WINGDIPAPI GdipGetPathGradientSurroundColorsWithCount(GpPathGradient
466 *grad, ARGB *argb, INT *count)
468 static int calls;
470 if(!grad || !argb || !count || (*count < grad->pathdata.Count))
471 return InvalidParameter;
473 if(!(calls++))
474 FIXME("not implemented\n");
476 return NotImplemented;
479 GpStatus WINGDIPAPI GdipGetSolidFillColor(GpSolidFill *sf, ARGB *argb)
481 if(!sf || !argb)
482 return InvalidParameter;
484 *argb = sf->color;
486 return Ok;
489 GpStatus WINGDIPAPI GdipSetLineBlend(GpLineGradient *brush,
490 GDIPCONST REAL *blend, GDIPCONST REAL* positions, INT count)
492 static int calls;
494 if(!brush || !blend || !positions || count <= 0)
495 return InvalidParameter;
497 if(!(calls++))
498 FIXME("not implemented\n");
500 return Ok;
503 GpStatus WINGDIPAPI GdipSetLineGammaCorrection(GpLineGradient *line,
504 BOOL usegamma)
506 if(!line)
507 return InvalidParameter;
509 line->gamma = usegamma;
511 return Ok;
514 GpStatus WINGDIPAPI GdipSetLineSigmaBlend(GpLineGradient *line, REAL focus,
515 REAL scale)
517 static int calls;
519 if(!line || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
520 return InvalidParameter;
522 if(!(calls++))
523 FIXME("not implemented\n");
525 return NotImplemented;
528 GpStatus WINGDIPAPI GdipSetLineWrapMode(GpLineGradient *line,
529 GpWrapMode wrap)
531 if(!line || wrap == WrapModeClamp)
532 return InvalidParameter;
534 line->wrap = wrap;
536 return Ok;
539 GpStatus WINGDIPAPI GdipSetPathGradientCenterColor(GpPathGradient *grad,
540 ARGB argb)
542 if(!grad)
543 return InvalidParameter;
545 grad->centercolor = argb;
546 grad->brush.lb.lbColor = ARGB2COLORREF(argb);
548 DeleteObject(grad->brush.gdibrush);
549 grad->brush.gdibrush = CreateSolidBrush(grad->brush.lb.lbColor);
551 return Ok;
554 GpStatus WINGDIPAPI GdipSetPathGradientCenterPoint(GpPathGradient *grad,
555 GpPointF *point)
557 if(!grad || !point)
558 return InvalidParameter;
560 grad->center.X = point->X;
561 grad->center.Y = point->Y;
563 return Ok;
566 GpStatus WINGDIPAPI GdipSetPathGradientFocusScales(GpPathGradient *grad,
567 REAL x, REAL y)
569 if(!grad)
570 return InvalidParameter;
572 grad->focus.X = x;
573 grad->focus.Y = y;
575 return Ok;
578 GpStatus WINGDIPAPI GdipSetPathGradientGammaCorrection(GpPathGradient *grad,
579 BOOL gamma)
581 if(!grad)
582 return InvalidParameter;
584 grad->gamma = gamma;
586 return Ok;
589 GpStatus WINGDIPAPI GdipSetPathGradientSigmaBlend(GpPathGradient *grad,
590 REAL focus, REAL scale)
592 static int calls;
594 if(!grad || focus < 0.0 || focus > 1.0 || scale < 0.0 || scale > 1.0)
595 return InvalidParameter;
597 if(!(calls++))
598 FIXME("not implemented\n");
600 return NotImplemented;
603 GpStatus WINGDIPAPI GdipSetPathGradientSurroundColorsWithCount(GpPathGradient
604 *grad, ARGB *argb, INT *count)
606 static int calls;
608 if(!grad || !argb || !count || (*count <= 0) ||
609 (*count > grad->pathdata.Count))
610 return InvalidParameter;
612 if(!(calls++))
613 FIXME("not implemented\n");
615 return NotImplemented;
618 GpStatus WINGDIPAPI GdipSetPathGradientWrapMode(GpPathGradient *grad,
619 GpWrapMode wrap)
621 if(!grad)
622 return InvalidParameter;
624 grad->wrap = wrap;
626 return Ok;
629 GpStatus WINGDIPAPI GdipSetSolidFillColor(GpSolidFill *sf, ARGB argb)
631 if(!sf)
632 return InvalidParameter;
634 sf->color = argb;
635 sf->brush.lb.lbColor = ARGB2COLORREF(argb);
637 DeleteObject(sf->brush.gdibrush);
638 sf->brush.gdibrush = CreateSolidBrush(sf->brush.lb.lbColor);
640 return Ok;
643 GpStatus WINGDIPAPI GdipSetTextureTransform(GpTexture *texture,
644 GDIPCONST GpMatrix *matrix)
646 static int calls;
648 if(!texture || !matrix)
649 return InvalidParameter;
651 if(!(calls++))
652 FIXME("not implemented\n");
654 return Ok;