[GDIPLUS] Sync with Wine Staging 4.0. CORE-15682
[reactos.git] / dll / win32 / gdiplus / graphics.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 #include <math.h>
21 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44
45 /* Mike "tamlin" Nordell 2012-09-14 for ReactOS:
46 * NOTE: Wine uses per-GpGraphics id's ('contid' starting from zero in
47 * every GpGraphics). Windows seems to use process-global id's, or at
48 * least more unique id's.
49 * This have the following implications. It:
50 * 1. fails the current gdiplus test case.
51 * 2. is not what Windows does.
52 *
53 * We therefore "obfuscate" the 'contid' a little to more match Windows'
54 * behaviour. The observable behviour should still remain the same,
55 * except for handing out more "unique" id's.
56 */
57 #define GDIP_CONTID_STEP 64
58 static volatile LONG g_priv_contid = GDIP_CONTID_STEP;
59 #define GDIP_GET_NEW_CONTID_FOR(pGpGraphics) \
60 (UINT)(InterlockedExchangeAdd(&g_priv_contid,GDIP_CONTID_STEP))
61
62
63 /* ReactOS FIXME: Inspect */
64 #define fmax max
65
66 /* looks-right constants */
67 #define ANCHOR_WIDTH (2.0)
68 #define MAX_ITERS (50)
69
70 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
71 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
72 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
73 INT flags, GDIPCONST GpMatrix *matrix);
74
75 /* Converts from gdiplus path point type to gdi path point type. */
76 static BYTE convert_path_point_type(BYTE type)
77 {
78 BYTE ret;
79
80 switch(type & PathPointTypePathTypeMask){
81 case PathPointTypeBezier:
82 ret = PT_BEZIERTO;
83 break;
84 case PathPointTypeLine:
85 ret = PT_LINETO;
86 break;
87 case PathPointTypeStart:
88 ret = PT_MOVETO;
89 break;
90 default:
91 ERR("Bad point type\n");
92 return 0;
93 }
94
95 if(type & PathPointTypeCloseSubpath)
96 ret |= PT_CLOSEFIGURE;
97
98 return ret;
99 }
100
101 static COLORREF get_gdi_brush_color(const GpBrush *brush)
102 {
103 ARGB argb;
104
105 switch (brush->bt)
106 {
107 case BrushTypeSolidColor:
108 {
109 const GpSolidFill *sf = (const GpSolidFill *)brush;
110 argb = sf->color;
111 break;
112 }
113 case BrushTypeHatchFill:
114 {
115 const GpHatch *hatch = (const GpHatch *)brush;
116 argb = hatch->forecol;
117 break;
118 }
119 case BrushTypeLinearGradient:
120 {
121 const GpLineGradient *line = (const GpLineGradient *)brush;
122 argb = line->startcolor;
123 break;
124 }
125 case BrushTypePathGradient:
126 {
127 const GpPathGradient *grad = (const GpPathGradient *)brush;
128 argb = grad->centercolor;
129 break;
130 }
131 default:
132 FIXME("unhandled brush type %d\n", brush->bt);
133 argb = 0;
134 break;
135 }
136 return ARGB2COLORREF(argb);
137 }
138
139 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
140 {
141 HBITMAP hbmp;
142 BITMAPINFOHEADER bmih;
143 DWORD *bits;
144 int x, y;
145
146 bmih.biSize = sizeof(bmih);
147 bmih.biWidth = 8;
148 bmih.biHeight = 8;
149 bmih.biPlanes = 1;
150 bmih.biBitCount = 32;
151 bmih.biCompression = BI_RGB;
152 bmih.biSizeImage = 0;
153
154 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
155 if (hbmp)
156 {
157 const char *hatch_data;
158
159 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
160 {
161 for (y = 0; y < 8; y++)
162 {
163 for (x = 0; x < 8; x++)
164 {
165 if (hatch_data[y] & (0x80 >> x))
166 bits[y * 8 + x] = hatch->forecol;
167 else
168 bits[y * 8 + x] = hatch->backcol;
169 }
170 }
171 }
172 else
173 {
174 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
175
176 for (y = 0; y < 64; y++)
177 bits[y] = hatch->forecol;
178 }
179 }
180
181 return hbmp;
182 }
183
184 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
185 {
186 switch (brush->bt)
187 {
188 case BrushTypeSolidColor:
189 {
190 const GpSolidFill *sf = (const GpSolidFill *)brush;
191 lb->lbStyle = BS_SOLID;
192 lb->lbColor = ARGB2COLORREF(sf->color);
193 lb->lbHatch = 0;
194 return Ok;
195 }
196
197 case BrushTypeHatchFill:
198 {
199 const GpHatch *hatch = (const GpHatch *)brush;
200 HBITMAP hbmp;
201
202 hbmp = create_hatch_bitmap(hatch);
203 if (!hbmp) return OutOfMemory;
204
205 lb->lbStyle = BS_PATTERN;
206 lb->lbColor = 0;
207 lb->lbHatch = (ULONG_PTR)hbmp;
208 return Ok;
209 }
210
211 default:
212 FIXME("unhandled brush type %d\n", brush->bt);
213 lb->lbStyle = BS_SOLID;
214 lb->lbColor = get_gdi_brush_color(brush);
215 lb->lbHatch = 0;
216 return Ok;
217 }
218 }
219
220 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
221 {
222 switch (lb->lbStyle)
223 {
224 case BS_PATTERN:
225 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
226 break;
227 }
228 return Ok;
229 }
230
231 static HBRUSH create_gdi_brush(const GpBrush *brush)
232 {
233 LOGBRUSH lb;
234 HBRUSH gdibrush;
235
236 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
237
238 gdibrush = CreateBrushIndirect(&lb);
239 free_gdi_logbrush(&lb);
240
241 return gdibrush;
242 }
243
244 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
245 {
246 LOGBRUSH lb;
247 HPEN gdipen;
248 REAL width;
249 INT save_state, i, numdashes;
250 GpPointF pt[2];
251 DWORD dash_array[MAX_DASHLEN];
252
253 save_state = SaveDC(graphics->hdc);
254
255 EndPath(graphics->hdc);
256
257 if(pen->unit == UnitPixel){
258 width = pen->width;
259 }
260 else{
261 /* Get an estimate for the amount the pen width is affected by the world
262 * transform. (This is similar to what some of the wine drivers do.) */
263 pt[0].X = 0.0;
264 pt[0].Y = 0.0;
265 pt[1].X = 1.0;
266 pt[1].Y = 1.0;
267 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
268 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
269 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
270
271 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
272 width *= graphics->scale;
273
274 pt[0].X = 0.0;
275 pt[0].Y = 0.0;
276 pt[1].X = 1.0;
277 pt[1].Y = 1.0;
278 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
279 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
280 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
281 }
282
283 if(pen->dash == DashStyleCustom){
284 numdashes = min(pen->numdashes, MAX_DASHLEN);
285
286 TRACE("dashes are: ");
287 for(i = 0; i < numdashes; i++){
288 dash_array[i] = gdip_round(width * pen->dashes[i]);
289 TRACE("%d, ", dash_array[i]);
290 }
291 TRACE("\n and the pen style is %x\n", pen->style);
292
293 create_gdi_logbrush(pen->brush, &lb);
294 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
295 numdashes, dash_array);
296 free_gdi_logbrush(&lb);
297 }
298 else
299 {
300 create_gdi_logbrush(pen->brush, &lb);
301 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
302 free_gdi_logbrush(&lb);
303 }
304
305 SelectObject(graphics->hdc, gdipen);
306
307 return save_state;
308 }
309
310 static void restore_dc(GpGraphics *graphics, INT state)
311 {
312 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
313 RestoreDC(graphics->hdc, state);
314 }
315
316 static void round_points(POINT *pti, GpPointF *ptf, INT count)
317 {
318 int i;
319
320 for(i = 0; i < count; i++){
321 pti[i].x = gdip_round(ptf[i].X);
322 pti[i].y = gdip_round(ptf[i].Y);
323 }
324 }
325
326 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
327 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
328 {
329 if (GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
330 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
331 {
332 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
333
334 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
335 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
336 }
337 else
338 {
339 BLENDFUNCTION bf;
340
341 bf.BlendOp = AC_SRC_OVER;
342 bf.BlendFlags = 0;
343 bf.SourceConstantAlpha = 255;
344 bf.AlphaFormat = AC_SRC_ALPHA;
345
346 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
347 hdc, src_x, src_y, src_width, src_height, bf);
348 }
349 }
350
351 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
352 {
353 GpRegion *rgn;
354 GpMatrix transform;
355 GpStatus stat;
356 BOOL identity;
357
358 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
359
360 if (stat == Ok)
361 stat = GdipIsMatrixIdentity(&transform, &identity);
362
363 if (stat == Ok)
364 stat = GdipCloneRegion(graphics->clip, &rgn);
365
366 if (stat == Ok)
367 {
368 if (!identity)
369 stat = GdipTransformRegion(rgn, &transform);
370
371 if (stat == Ok)
372 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
373
374 GdipDeleteRegion(rgn);
375 }
376
377 if (stat == Ok && graphics->gdi_clip)
378 {
379 if (*hrgn)
380 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
381 else
382 {
383 *hrgn = CreateRectRgn(0,0,0,0);
384 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
385 }
386 }
387
388 return stat;
389 }
390
391 /* Draw ARGB data to the given graphics object */
392 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
393 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
394 {
395 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
396 INT x, y;
397
398 for (y=0; y<src_height; y++)
399 {
400 for (x=0; x<src_width; x++)
401 {
402 ARGB dst_color, src_color;
403 src_color = ((ARGB*)(src + src_stride * y))[x];
404
405 if (!(src_color & 0xff000000))
406 continue;
407
408 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
409 if (fmt & PixelFormatPAlpha)
410 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
411 else
412 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
413 }
414 }
415
416 return Ok;
417 }
418
419 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
420 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
421 {
422 HDC hdc;
423 HBITMAP hbitmap;
424 BITMAPINFOHEADER bih;
425 BYTE *temp_bits;
426
427 hdc = CreateCompatibleDC(0);
428
429 bih.biSize = sizeof(BITMAPINFOHEADER);
430 bih.biWidth = src_width;
431 bih.biHeight = -src_height;
432 bih.biPlanes = 1;
433 bih.biBitCount = 32;
434 bih.biCompression = BI_RGB;
435 bih.biSizeImage = 0;
436 bih.biXPelsPerMeter = 0;
437 bih.biYPelsPerMeter = 0;
438 bih.biClrUsed = 0;
439 bih.biClrImportant = 0;
440
441 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
442 (void**)&temp_bits, NULL, 0);
443
444 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
445 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
446 fmt & PixelFormatPAlpha)
447 memcpy(temp_bits, src, src_width * src_height * 4);
448 else
449 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
450 4 * src_width, src, src_stride);
451
452 SelectObject(hdc, hbitmap);
453 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
454 hdc, 0, 0, src_width, src_height);
455 DeleteDC(hdc);
456 DeleteObject(hbitmap);
457
458 return Ok;
459 }
460
461 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
462 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
463 {
464 GpStatus stat=Ok;
465
466 if (graphics->image && graphics->image->type == ImageTypeBitmap)
467 {
468 DWORD i;
469 int size;
470 RGNDATA *rgndata;
471 RECT *rects;
472 HRGN hrgn, visible_rgn;
473
474 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
475 if (!hrgn)
476 return OutOfMemory;
477
478 stat = get_clip_hrgn(graphics, &visible_rgn);
479 if (stat != Ok)
480 {
481 DeleteObject(hrgn);
482 return stat;
483 }
484
485 if (visible_rgn)
486 {
487 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
488 DeleteObject(visible_rgn);
489 }
490
491 if (hregion)
492 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
493
494 size = GetRegionData(hrgn, 0, NULL);
495
496 rgndata = heap_alloc_zero(size);
497 if (!rgndata)
498 {
499 DeleteObject(hrgn);
500 return OutOfMemory;
501 }
502
503 GetRegionData(hrgn, size, rgndata);
504
505 rects = (RECT*)rgndata->Buffer;
506
507 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
508 {
509 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
510 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
511 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
512 src_stride, fmt);
513 }
514
515 heap_free(rgndata);
516
517 DeleteObject(hrgn);
518
519 return stat;
520 }
521 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
522 {
523 ERR("This should not be used for metafiles; fix caller\n");
524 return NotImplemented;
525 }
526 else
527 {
528 HRGN hrgn;
529 int save;
530
531 stat = get_clip_hrgn(graphics, &hrgn);
532
533 if (stat != Ok)
534 return stat;
535
536 save = SaveDC(graphics->hdc);
537
538 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
539
540 if (hregion)
541 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
542
543 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
544 src_height, src_stride, fmt);
545
546 RestoreDC(graphics->hdc, save);
547
548 DeleteObject(hrgn);
549
550 return stat;
551 }
552 }
553
554 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
555 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
556 {
557 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
558 }
559
560 /* NOTE: start and end pixels must be in pre-multiplied ARGB format */
561 static inline ARGB blend_colors_premult(ARGB start, ARGB end, REAL position)
562 {
563 UINT pos = position * 255.0f + 0.5f;
564 return
565 (((((start >> 24) ) << 8) + (((end >> 24) ) - ((start >> 24) )) * pos) >> 8) << 24 |
566 (((((start >> 16) & 0xff) << 8) + (((end >> 16) & 0xff) - ((start >> 16) & 0xff)) * pos) >> 8) << 16 |
567 (((((start >> 8) & 0xff) << 8) + (((end >> 8) & 0xff) - ((start >> 8) & 0xff)) * pos) >> 8) << 8 |
568 (((((start ) & 0xff) << 8) + (((end ) & 0xff) - ((start ) & 0xff)) * pos) >> 8);
569 }
570
571 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
572 {
573 INT start_a, end_a, final_a;
574 INT pos;
575
576 pos = (INT)(position * 255.0f + 0.5f);
577
578 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
579 end_a = ((end >> 24) & 0xff) * pos;
580
581 final_a = start_a + end_a;
582
583 if (final_a < 0xff) return 0;
584
585 return (final_a / 0xff) << 24 |
586 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
587 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
588 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
589 }
590
591 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
592 {
593 REAL blendfac;
594
595 /* clamp to between 0.0 and 1.0, using the wrap mode */
596 position = (position - brush->rect.X) / brush->rect.Width;
597 if (brush->wrap == WrapModeTile)
598 {
599 position = fmodf(position, 1.0f);
600 if (position < 0.0f) position += 1.0f;
601 }
602 else /* WrapModeFlip* */
603 {
604 position = fmodf(position, 2.0f);
605 if (position < 0.0f) position += 2.0f;
606 if (position > 1.0f) position = 2.0f - position;
607 }
608
609 if (brush->blendcount == 1)
610 blendfac = position;
611 else
612 {
613 int i=1;
614 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
615 REAL range;
616
617 /* locate the blend positions surrounding this position */
618 while (position > brush->blendpos[i])
619 i++;
620
621 /* interpolate between the blend positions */
622 left_blendpos = brush->blendpos[i-1];
623 left_blendfac = brush->blendfac[i-1];
624 right_blendpos = brush->blendpos[i];
625 right_blendfac = brush->blendfac[i];
626 range = right_blendpos - left_blendpos;
627 blendfac = (left_blendfac * (right_blendpos - position) +
628 right_blendfac * (position - left_blendpos)) / range;
629 }
630
631 if (brush->pblendcount == 0)
632 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
633 else
634 {
635 int i=1;
636 ARGB left_blendcolor, right_blendcolor;
637 REAL left_blendpos, right_blendpos;
638
639 /* locate the blend colors surrounding this position */
640 while (blendfac > brush->pblendpos[i])
641 i++;
642
643 /* interpolate between the blend colors */
644 left_blendpos = brush->pblendpos[i-1];
645 left_blendcolor = brush->pblendcolor[i-1];
646 right_blendpos = brush->pblendpos[i];
647 right_blendcolor = brush->pblendcolor[i];
648 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
649 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
650 }
651 }
652
653 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
654 {
655 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
656 BOOL identity = TRUE;
657 int i, j;
658
659 for (i=0; i<4; i++)
660 for (j=0; j<5; j++)
661 {
662 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
663 identity = FALSE;
664 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
665 }
666
667 return identity;
668 }
669
670 static ARGB transform_color(ARGB color, int matrix[5][5])
671 {
672 int val[5], res[4];
673 int i, j;
674 unsigned char a, r, g, b;
675
676 val[0] = ((color >> 16) & 0xff); /* red */
677 val[1] = ((color >> 8) & 0xff); /* green */
678 val[2] = (color & 0xff); /* blue */
679 val[3] = ((color >> 24) & 0xff); /* alpha */
680 val[4] = 255; /* translation */
681
682 for (i=0; i<4; i++)
683 {
684 res[i] = 0;
685
686 for (j=0; j<5; j++)
687 res[i] += matrix[j][i] * val[j];
688 }
689
690 a = min(max(res[3] / 256, 0), 255);
691 r = min(max(res[0] / 256, 0), 255);
692 g = min(max(res[1] / 256, 0), 255);
693 b = min(max(res[2] / 256, 0), 255);
694
695 return (a << 24) | (r << 16) | (g << 8) | b;
696 }
697
698 static BOOL color_is_gray(ARGB color)
699 {
700 unsigned char r, g, b;
701
702 r = (color >> 16) & 0xff;
703 g = (color >> 8) & 0xff;
704 b = color & 0xff;
705
706 return (r == g) && (g == b);
707 }
708
709 /* returns preferred pixel format for the applied attributes */
710 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
711 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
712 {
713 UINT x, y;
714 INT i;
715
716 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
717 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
718 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
719 return fmt;
720
721 if (attributes->colorkeys[type].enabled ||
722 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
723 {
724 const struct color_key *key;
725 BYTE min_blue, min_green, min_red;
726 BYTE max_blue, max_green, max_red;
727
728 if (!data || fmt != PixelFormat32bppARGB)
729 return PixelFormat32bppARGB;
730
731 if (attributes->colorkeys[type].enabled)
732 key = &attributes->colorkeys[type];
733 else
734 key = &attributes->colorkeys[ColorAdjustTypeDefault];
735
736 min_blue = key->low&0xff;
737 min_green = (key->low>>8)&0xff;
738 min_red = (key->low>>16)&0xff;
739
740 max_blue = key->high&0xff;
741 max_green = (key->high>>8)&0xff;
742 max_red = (key->high>>16)&0xff;
743
744 for (x=0; x<width; x++)
745 for (y=0; y<height; y++)
746 {
747 ARGB *src_color;
748 BYTE blue, green, red;
749 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
750 blue = *src_color&0xff;
751 green = (*src_color>>8)&0xff;
752 red = (*src_color>>16)&0xff;
753 if (blue >= min_blue && green >= min_green && red >= min_red &&
754 blue <= max_blue && green <= max_green && red <= max_red)
755 *src_color = 0x00000000;
756 }
757 }
758
759 if (attributes->colorremaptables[type].enabled ||
760 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
761 {
762 const struct color_remap_table *table;
763
764 if (!data || fmt != PixelFormat32bppARGB)
765 return PixelFormat32bppARGB;
766
767 if (attributes->colorremaptables[type].enabled)
768 table = &attributes->colorremaptables[type];
769 else
770 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
771
772 for (x=0; x<width; x++)
773 for (y=0; y<height; y++)
774 {
775 ARGB *src_color;
776 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
777 for (i=0; i<table->mapsize; i++)
778 {
779 if (*src_color == table->colormap[i].oldColor.Argb)
780 {
781 *src_color = table->colormap[i].newColor.Argb;
782 break;
783 }
784 }
785 }
786 }
787
788 if (attributes->colormatrices[type].enabled ||
789 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
790 {
791 const struct color_matrix *colormatrices;
792 int color_matrix[5][5];
793 int gray_matrix[5][5];
794 BOOL identity;
795
796 if (!data || fmt != PixelFormat32bppARGB)
797 return PixelFormat32bppARGB;
798
799 if (attributes->colormatrices[type].enabled)
800 colormatrices = &attributes->colormatrices[type];
801 else
802 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
803
804 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
805
806 if (colormatrices->flags == ColorMatrixFlagsAltGray)
807 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
808
809 if (!identity)
810 {
811 for (x=0; x<width; x++)
812 {
813 for (y=0; y<height; y++)
814 {
815 ARGB *src_color;
816 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
817
818 if (colormatrices->flags == ColorMatrixFlagsDefault ||
819 !color_is_gray(*src_color))
820 {
821 *src_color = transform_color(*src_color, color_matrix);
822 }
823 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
824 {
825 *src_color = transform_color(*src_color, gray_matrix);
826 }
827 }
828 }
829 }
830 }
831
832 if (attributes->gamma_enabled[type] ||
833 attributes->gamma_enabled[ColorAdjustTypeDefault])
834 {
835 REAL gamma;
836
837 if (!data || fmt != PixelFormat32bppARGB)
838 return PixelFormat32bppARGB;
839
840 if (attributes->gamma_enabled[type])
841 gamma = attributes->gamma[type];
842 else
843 gamma = attributes->gamma[ColorAdjustTypeDefault];
844
845 for (x=0; x<width; x++)
846 for (y=0; y<height; y++)
847 {
848 ARGB *src_color;
849 BYTE blue, green, red;
850 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
851
852 blue = *src_color&0xff;
853 green = (*src_color>>8)&0xff;
854 red = (*src_color>>16)&0xff;
855
856 /* FIXME: We should probably use a table for this. */
857 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
858 green = floorf(powf(green / 255.0, gamma) * 255.0);
859 red = floorf(powf(red / 255.0, gamma) * 255.0);
860
861 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
862 }
863 }
864
865 return fmt;
866 }
867
868 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
869 * bitmap that contains all the pixels we may need to draw it. */
870 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
871 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
872 GpRect *rect)
873 {
874 INT left, top, right, bottom;
875
876 switch (interpolation)
877 {
878 case InterpolationModeHighQualityBilinear:
879 case InterpolationModeHighQualityBicubic:
880 /* FIXME: Include a greater range for the prefilter? */
881 case InterpolationModeBicubic:
882 case InterpolationModeBilinear:
883 left = (INT)(floorf(srcx));
884 top = (INT)(floorf(srcy));
885 right = (INT)(ceilf(srcx+srcwidth));
886 bottom = (INT)(ceilf(srcy+srcheight));
887 break;
888 case InterpolationModeNearestNeighbor:
889 default:
890 left = gdip_round(srcx);
891 top = gdip_round(srcy);
892 right = gdip_round(srcx+srcwidth);
893 bottom = gdip_round(srcy+srcheight);
894 break;
895 }
896
897 if (wrap == WrapModeClamp)
898 {
899 if (left < 0)
900 left = 0;
901 if (top < 0)
902 top = 0;
903 if (right >= bitmap->width)
904 right = bitmap->width-1;
905 if (bottom >= bitmap->height)
906 bottom = bitmap->height-1;
907 if (bottom < top || right < left)
908 /* entirely outside image, just sample a pixel so we don't have to
909 * special-case this later */
910 left = top = right = bottom = 0;
911 }
912 else
913 {
914 /* In some cases we can make the rectangle smaller here, but the logic
915 * is hard to get right, and tiling suggests we're likely to use the
916 * entire source image. */
917 if (left < 0 || right >= bitmap->width)
918 {
919 left = 0;
920 right = bitmap->width-1;
921 }
922
923 if (top < 0 || bottom >= bitmap->height)
924 {
925 top = 0;
926 bottom = bitmap->height-1;
927 }
928 }
929
930 rect->X = left;
931 rect->Y = top;
932 rect->Width = right - left + 1;
933 rect->Height = bottom - top + 1;
934 }
935
936 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
937 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
938 {
939 if (attributes->wrap == WrapModeClamp)
940 {
941 if (x < 0 || y < 0 || x >= width || y >= height)
942 return attributes->outside_color;
943 }
944 else
945 {
946 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
947 if (x < 0)
948 x = width*2 + x % (width * 2);
949 if (y < 0)
950 y = height*2 + y % (height * 2);
951
952 if (attributes->wrap & WrapModeTileFlipX)
953 {
954 if ((x / width) % 2 == 0)
955 x = x % width;
956 else
957 x = width - 1 - x % width;
958 }
959 else
960 x = x % width;
961
962 if (attributes->wrap & WrapModeTileFlipY)
963 {
964 if ((y / height) % 2 == 0)
965 y = y % height;
966 else
967 y = height - 1 - y % height;
968 }
969 else
970 y = y % height;
971 }
972
973 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
974 {
975 ERR("out of range pixel requested\n");
976 return 0xffcd0084;
977 }
978
979 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
980 }
981
982 static inline int positive_ceilf(float f)
983 {
984 return f - (int)f > 0.0f ? f + 1.0f : f;
985 }
986
987 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
988 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
989 InterpolationMode interpolation, PixelOffsetMode offset_mode)
990 {
991 static int fixme;
992
993 switch (interpolation)
994 {
995 default:
996 if (!fixme++)
997 FIXME("Unimplemented interpolation %i\n", interpolation);
998 /* fall-through */
999 case InterpolationModeBilinear:
1000 {
1001 REAL leftxf, topyf;
1002 INT leftx, rightx, topy, bottomy;
1003 ARGB topleft, topright, bottomleft, bottomright;
1004 ARGB top, bottom;
1005 float x_offset;
1006
1007 leftx = (INT)point->X;
1008 leftxf = (REAL)leftx;
1009 rightx = positive_ceilf(point->X);
1010 topy = (INT)point->Y;
1011 topyf = (REAL)topy;
1012 bottomy = positive_ceilf(point->Y);
1013
1014 if (leftx == rightx && topy == bottomy)
1015 return sample_bitmap_pixel(src_rect, bits, width, height,
1016 leftx, topy, attributes);
1017
1018 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1019 leftx, topy, attributes);
1020 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1021 rightx, topy, attributes);
1022 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1023 leftx, bottomy, attributes);
1024 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1025 rightx, bottomy, attributes);
1026
1027 x_offset = point->X - leftxf;
1028 top = blend_colors(topleft, topright, x_offset);
1029 bottom = blend_colors(bottomleft, bottomright, x_offset);
1030
1031 return blend_colors(top, bottom, point->Y - topyf);
1032 }
1033 case InterpolationModeNearestNeighbor:
1034 {
1035 FLOAT pixel_offset;
1036 switch (offset_mode)
1037 {
1038 default:
1039 case PixelOffsetModeNone:
1040 case PixelOffsetModeHighSpeed:
1041 pixel_offset = 0.5;
1042 break;
1043
1044 case PixelOffsetModeHalf:
1045 case PixelOffsetModeHighQuality:
1046 pixel_offset = 0.0;
1047 break;
1048 }
1049 return sample_bitmap_pixel(src_rect, bits, width, height,
1050 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1051 }
1052
1053 }
1054 }
1055
1056 static ARGB resample_bitmap_pixel_premult(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
1057 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
1058 InterpolationMode interpolation, PixelOffsetMode offset_mode)
1059 {
1060 static int fixme;
1061
1062 switch (interpolation)
1063 {
1064 default:
1065 if (!fixme++)
1066 FIXME("Unimplemented interpolation %i\n", interpolation);
1067 /* fall-through */
1068 case InterpolationModeBilinear:
1069 {
1070 REAL leftxf, topyf;
1071 INT leftx, rightx, topy, bottomy;
1072 ARGB topleft, topright, bottomleft, bottomright;
1073 ARGB top, bottom;
1074 float x_offset;
1075
1076 leftx = (INT)point->X;
1077 leftxf = (REAL)leftx;
1078 rightx = positive_ceilf(point->X);
1079 topy = (INT)point->Y;
1080 topyf = (REAL)topy;
1081 bottomy = positive_ceilf(point->Y);
1082
1083 if (leftx == rightx && topy == bottomy)
1084 return sample_bitmap_pixel(src_rect, bits, width, height,
1085 leftx, topy, attributes);
1086
1087 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1088 leftx, topy, attributes);
1089 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1090 rightx, topy, attributes);
1091 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1092 leftx, bottomy, attributes);
1093 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1094 rightx, bottomy, attributes);
1095
1096 x_offset = point->X - leftxf;
1097 top = blend_colors_premult(topleft, topright, x_offset);
1098 bottom = blend_colors_premult(bottomleft, bottomright, x_offset);
1099
1100 return blend_colors_premult(top, bottom, point->Y - topyf);
1101 }
1102 case InterpolationModeNearestNeighbor:
1103 {
1104 FLOAT pixel_offset;
1105 switch (offset_mode)
1106 {
1107 default:
1108 case PixelOffsetModeNone:
1109 case PixelOffsetModeHighSpeed:
1110 pixel_offset = 0.5;
1111 break;
1112
1113 case PixelOffsetModeHalf:
1114 case PixelOffsetModeHighQuality:
1115 pixel_offset = 0.0;
1116 break;
1117 }
1118 return sample_bitmap_pixel(src_rect, bits, width, height,
1119 floorf(point->X + pixel_offset), point->Y + pixel_offset, attributes);
1120 }
1121
1122 }
1123 }
1124
1125 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1126 {
1127 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1128 }
1129
1130 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1131 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1132 {
1133 switch (brush->bt)
1134 {
1135 case BrushTypeSolidColor:
1136 {
1137 if (is_fill)
1138 return TRUE;
1139 else
1140 {
1141 /* cannot draw semi-transparent colors */
1142 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1143 }
1144 }
1145 case BrushTypeHatchFill:
1146 {
1147 GpHatch *hatch = (GpHatch*)brush;
1148 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1149 ((hatch->backcol & 0xff000000) == 0xff000000);
1150 }
1151 case BrushTypeLinearGradient:
1152 case BrushTypeTextureFill:
1153 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1154 default:
1155 return FALSE;
1156 }
1157 }
1158
1159 static GpStatus brush_fill_path(GpGraphics *graphics, GpBrush *brush)
1160 {
1161 GpStatus status = Ok;
1162 switch (brush->bt)
1163 {
1164 case BrushTypeSolidColor:
1165 {
1166 GpSolidFill *fill = (GpSolidFill*)brush;
1167 HBITMAP bmp = ARGB2BMP(fill->color);
1168
1169 if (bmp)
1170 {
1171 RECT rc;
1172 /* partially transparent fill */
1173
1174 if (!SelectClipPath(graphics->hdc, RGN_AND))
1175 {
1176 status = GenericError;
1177 DeleteObject(bmp);
1178 break;
1179 }
1180 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1181 {
1182 HDC hdc = CreateCompatibleDC(NULL);
1183
1184 if (!hdc)
1185 {
1186 status = OutOfMemory;
1187 DeleteObject(bmp);
1188 break;
1189 }
1190
1191 SelectObject(hdc, bmp);
1192 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1193 hdc, 0, 0, 1, 1);
1194 DeleteDC(hdc);
1195 }
1196
1197 DeleteObject(bmp);
1198 break;
1199 }
1200 /* else fall through */
1201 }
1202 default:
1203 {
1204 HBRUSH gdibrush, old_brush;
1205
1206 gdibrush = create_gdi_brush(brush);
1207 if (!gdibrush)
1208 {
1209 status = OutOfMemory;
1210 break;
1211 }
1212
1213 old_brush = SelectObject(graphics->hdc, gdibrush);
1214 FillPath(graphics->hdc);
1215 SelectObject(graphics->hdc, old_brush);
1216 DeleteObject(gdibrush);
1217 break;
1218 }
1219 }
1220
1221 return status;
1222 }
1223
1224 static BOOL brush_can_fill_pixels(GpBrush *brush)
1225 {
1226 switch (brush->bt)
1227 {
1228 case BrushTypeSolidColor:
1229 case BrushTypeHatchFill:
1230 case BrushTypeLinearGradient:
1231 case BrushTypeTextureFill:
1232 case BrushTypePathGradient:
1233 return TRUE;
1234 default:
1235 return FALSE;
1236 }
1237 }
1238
1239 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1240 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1241 {
1242 switch (brush->bt)
1243 {
1244 case BrushTypeSolidColor:
1245 {
1246 int x, y;
1247 GpSolidFill *fill = (GpSolidFill*)brush;
1248 for (x=0; x<fill_area->Width; x++)
1249 for (y=0; y<fill_area->Height; y++)
1250 argb_pixels[x + y*cdwStride] = fill->color;
1251 return Ok;
1252 }
1253 case BrushTypeHatchFill:
1254 {
1255 int x, y;
1256 GpHatch *fill = (GpHatch*)brush;
1257 const char *hatch_data;
1258
1259 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1260 return NotImplemented;
1261
1262 for (x=0; x<fill_area->Width; x++)
1263 for (y=0; y<fill_area->Height; y++)
1264 {
1265 int hx, hy;
1266
1267 /* FIXME: Account for the rendering origin */
1268 hx = (x + fill_area->X) % 8;
1269 hy = (y + fill_area->Y) % 8;
1270
1271 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1272 argb_pixels[x + y*cdwStride] = fill->forecol;
1273 else
1274 argb_pixels[x + y*cdwStride] = fill->backcol;
1275 }
1276
1277 return Ok;
1278 }
1279 case BrushTypeLinearGradient:
1280 {
1281 GpLineGradient *fill = (GpLineGradient*)brush;
1282 GpPointF draw_points[3];
1283 GpStatus stat;
1284 int x, y;
1285
1286 draw_points[0].X = fill_area->X;
1287 draw_points[0].Y = fill_area->Y;
1288 draw_points[1].X = fill_area->X+1;
1289 draw_points[1].Y = fill_area->Y;
1290 draw_points[2].X = fill_area->X;
1291 draw_points[2].Y = fill_area->Y+1;
1292
1293 /* Transform the points to a co-ordinate space where X is the point's
1294 * position in the gradient, 0.0 being the start point and 1.0 the
1295 * end point. */
1296 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1297 WineCoordinateSpaceGdiDevice, draw_points, 3);
1298
1299 if (stat == Ok)
1300 {
1301 GpMatrix world_to_gradient = fill->transform;
1302
1303 stat = GdipInvertMatrix(&world_to_gradient);
1304 if (stat == Ok)
1305 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1306 }
1307
1308 if (stat == Ok)
1309 {
1310 REAL x_delta = draw_points[1].X - draw_points[0].X;
1311 REAL y_delta = draw_points[2].X - draw_points[0].X;
1312
1313 for (y=0; y<fill_area->Height; y++)
1314 {
1315 for (x=0; x<fill_area->Width; x++)
1316 {
1317 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1318
1319 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1320 }
1321 }
1322 }
1323
1324 return stat;
1325 }
1326 case BrushTypeTextureFill:
1327 {
1328 GpTexture *fill = (GpTexture*)brush;
1329 GpPointF draw_points[3];
1330 GpStatus stat;
1331 int x, y;
1332 GpBitmap *bitmap;
1333 int src_stride;
1334 GpRect src_area;
1335
1336 if (fill->image->type != ImageTypeBitmap)
1337 {
1338 FIXME("metafile texture brushes not implemented\n");
1339 return NotImplemented;
1340 }
1341
1342 bitmap = (GpBitmap*)fill->image;
1343 src_stride = sizeof(ARGB) * bitmap->width;
1344
1345 src_area.X = src_area.Y = 0;
1346 src_area.Width = bitmap->width;
1347 src_area.Height = bitmap->height;
1348
1349 draw_points[0].X = fill_area->X;
1350 draw_points[0].Y = fill_area->Y;
1351 draw_points[1].X = fill_area->X+1;
1352 draw_points[1].Y = fill_area->Y;
1353 draw_points[2].X = fill_area->X;
1354 draw_points[2].Y = fill_area->Y+1;
1355
1356 /* Transform the points to the co-ordinate space of the bitmap. */
1357 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1358 WineCoordinateSpaceGdiDevice, draw_points, 3);
1359
1360 if (stat == Ok)
1361 {
1362 GpMatrix world_to_texture = fill->transform;
1363
1364 stat = GdipInvertMatrix(&world_to_texture);
1365 if (stat == Ok)
1366 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1367 }
1368
1369 if (stat == Ok && !fill->bitmap_bits)
1370 {
1371 BitmapData lockeddata;
1372
1373 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1374 if (!fill->bitmap_bits)
1375 stat = OutOfMemory;
1376
1377 if (stat == Ok)
1378 {
1379 lockeddata.Width = bitmap->width;
1380 lockeddata.Height = bitmap->height;
1381 lockeddata.Stride = src_stride;
1382 lockeddata.PixelFormat = PixelFormat32bppARGB;
1383 lockeddata.Scan0 = fill->bitmap_bits;
1384
1385 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1386 PixelFormat32bppARGB, &lockeddata);
1387 }
1388
1389 if (stat == Ok)
1390 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1391
1392 if (stat == Ok)
1393 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1394 bitmap->width, bitmap->height,
1395 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1396
1397 if (stat != Ok)
1398 {
1399 heap_free(fill->bitmap_bits);
1400 fill->bitmap_bits = NULL;
1401 }
1402 }
1403
1404 if (stat == Ok)
1405 {
1406 REAL x_dx = draw_points[1].X - draw_points[0].X;
1407 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1408 REAL y_dx = draw_points[2].X - draw_points[0].X;
1409 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1410
1411 for (y=0; y<fill_area->Height; y++)
1412 {
1413 for (x=0; x<fill_area->Width; x++)
1414 {
1415 GpPointF point;
1416 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1417 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1418
1419 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1420 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1421 &point, fill->imageattributes, graphics->interpolation,
1422 graphics->pixeloffset);
1423 }
1424 }
1425 }
1426
1427 return stat;
1428 }
1429 case BrushTypePathGradient:
1430 {
1431 GpPathGradient *fill = (GpPathGradient*)brush;
1432 GpPath *flat_path;
1433 GpMatrix world_to_device;
1434 GpStatus stat;
1435 int i, figure_start=0;
1436 GpPointF start_point, end_point, center_point;
1437 BYTE type;
1438 REAL min_yf, max_yf, line1_xf, line2_xf;
1439 INT min_y, max_y, min_x, max_x;
1440 INT x, y;
1441 ARGB outer_color;
1442 static BOOL transform_fixme_once;
1443
1444 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1445 {
1446 static int once;
1447 if (!once++)
1448 FIXME("path gradient focus not implemented\n");
1449 }
1450
1451 if (fill->gamma)
1452 {
1453 static int once;
1454 if (!once++)
1455 FIXME("path gradient gamma correction not implemented\n");
1456 }
1457
1458 if (fill->blendcount)
1459 {
1460 static int once;
1461 if (!once++)
1462 FIXME("path gradient blend not implemented\n");
1463 }
1464
1465 if (fill->pblendcount)
1466 {
1467 static int once;
1468 if (!once++)
1469 FIXME("path gradient preset blend not implemented\n");
1470 }
1471
1472 if (!transform_fixme_once)
1473 {
1474 BOOL is_identity=TRUE;
1475 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1476 if (!is_identity)
1477 {
1478 FIXME("path gradient transform not implemented\n");
1479 transform_fixme_once = TRUE;
1480 }
1481 }
1482
1483 stat = GdipClonePath(fill->path, &flat_path);
1484
1485 if (stat != Ok)
1486 return stat;
1487
1488 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1489 CoordinateSpaceWorld, &world_to_device);
1490 if (stat == Ok)
1491 {
1492 stat = GdipTransformPath(flat_path, &world_to_device);
1493
1494 if (stat == Ok)
1495 {
1496 center_point = fill->center;
1497 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1498 }
1499
1500 if (stat == Ok)
1501 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1502 }
1503
1504 if (stat != Ok)
1505 {
1506 GdipDeletePath(flat_path);
1507 return stat;
1508 }
1509
1510 for (i=0; i<flat_path->pathdata.Count; i++)
1511 {
1512 int start_center_line=0, end_center_line=0;
1513 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1514 REAL center_distance;
1515 ARGB start_color, end_color;
1516 REAL dy, dx;
1517
1518 type = flat_path->pathdata.Types[i];
1519
1520 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1521 figure_start = i;
1522
1523 start_point = flat_path->pathdata.Points[i];
1524
1525 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1526
1527 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1528 {
1529 end_point = flat_path->pathdata.Points[figure_start];
1530 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1531 }
1532 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1533 {
1534 end_point = flat_path->pathdata.Points[i+1];
1535 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1536 }
1537 else
1538 continue;
1539
1540 outer_color = start_color;
1541
1542 min_yf = center_point.Y;
1543 if (min_yf > start_point.Y) min_yf = start_point.Y;
1544 if (min_yf > end_point.Y) min_yf = end_point.Y;
1545
1546 if (min_yf < fill_area->Y)
1547 min_y = fill_area->Y;
1548 else
1549 min_y = (INT)ceil(min_yf);
1550
1551 max_yf = center_point.Y;
1552 if (max_yf < start_point.Y) max_yf = start_point.Y;
1553 if (max_yf < end_point.Y) max_yf = end_point.Y;
1554
1555 if (max_yf > fill_area->Y + fill_area->Height)
1556 max_y = fill_area->Y + fill_area->Height;
1557 else
1558 max_y = (INT)ceil(max_yf);
1559
1560 dy = end_point.Y - start_point.Y;
1561 dx = end_point.X - start_point.X;
1562
1563 /* This is proportional to the distance from start-end line to center point. */
1564 center_distance = dy * (start_point.X - center_point.X) +
1565 dx * (center_point.Y - start_point.Y);
1566
1567 for (y=min_y; y<max_y; y++)
1568 {
1569 REAL yf = (REAL)y;
1570
1571 if (!seen_start && yf >= start_point.Y)
1572 {
1573 seen_start = TRUE;
1574 start_center_line ^= 1;
1575 }
1576 if (!seen_end && yf >= end_point.Y)
1577 {
1578 seen_end = TRUE;
1579 end_center_line ^= 1;
1580 }
1581 if (!seen_center && yf >= center_point.Y)
1582 {
1583 seen_center = TRUE;
1584 start_center_line ^= 1;
1585 end_center_line ^= 1;
1586 }
1587
1588 if (start_center_line)
1589 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1590 else
1591 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1592
1593 if (end_center_line)
1594 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1595 else
1596 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1597
1598 if (line1_xf < line2_xf)
1599 {
1600 min_x = (INT)ceil(line1_xf);
1601 max_x = (INT)ceil(line2_xf);
1602 }
1603 else
1604 {
1605 min_x = (INT)ceil(line2_xf);
1606 max_x = (INT)ceil(line1_xf);
1607 }
1608
1609 if (min_x < fill_area->X)
1610 min_x = fill_area->X;
1611 if (max_x > fill_area->X + fill_area->Width)
1612 max_x = fill_area->X + fill_area->Width;
1613
1614 for (x=min_x; x<max_x; x++)
1615 {
1616 REAL xf = (REAL)x;
1617 REAL distance;
1618
1619 if (start_color != end_color)
1620 {
1621 REAL blend_amount, pdy, pdx;
1622 pdy = yf - center_point.Y;
1623 pdx = xf - center_point.X;
1624
1625 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1626 {
1627 /* Too close to center point, don't try to calculate outer color */
1628 outer_color = start_color;
1629 }
1630 else
1631 {
1632 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1633 outer_color = blend_colors(start_color, end_color, blend_amount);
1634 }
1635 }
1636
1637 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1638 (end_point.X - start_point.X) * (yf - start_point.Y);
1639
1640 distance = distance / center_distance;
1641
1642 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1643 blend_colors(outer_color, fill->centercolor, distance);
1644 }
1645 }
1646 }
1647
1648 GdipDeletePath(flat_path);
1649 return stat;
1650 }
1651 default:
1652 return NotImplemented;
1653 }
1654 }
1655
1656 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1657 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1658 * should not be called on an hdc that has a path you care about. */
1659 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1660 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1661 {
1662 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1663 GpMatrix matrix;
1664 HBRUSH brush = NULL;
1665 HPEN pen = NULL;
1666 PointF ptf[4], *custptf = NULL;
1667 POINT pt[4], *custpt = NULL;
1668 BYTE *tp = NULL;
1669 REAL theta, dsmall, dbig, dx, dy = 0.0;
1670 INT i, count;
1671 LOGBRUSH lb;
1672 BOOL customstroke;
1673
1674 if((x1 == x2) && (y1 == y2))
1675 return;
1676
1677 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1678
1679 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1680 if(!customstroke){
1681 brush = CreateSolidBrush(color);
1682 lb.lbStyle = BS_SOLID;
1683 lb.lbColor = color;
1684 lb.lbHatch = 0;
1685 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1686 PS_JOIN_MITER, 1, &lb, 0,
1687 NULL);
1688 oldbrush = SelectObject(graphics->hdc, brush);
1689 oldpen = SelectObject(graphics->hdc, pen);
1690 }
1691
1692 switch(cap){
1693 case LineCapFlat:
1694 break;
1695 case LineCapSquare:
1696 case LineCapSquareAnchor:
1697 case LineCapDiamondAnchor:
1698 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1699 if(cap == LineCapDiamondAnchor){
1700 dsmall = cos(theta + M_PI_2) * size;
1701 dbig = sin(theta + M_PI_2) * size;
1702 }
1703 else{
1704 dsmall = cos(theta + M_PI_4) * size;
1705 dbig = sin(theta + M_PI_4) * size;
1706 }
1707
1708 ptf[0].X = x2 - dsmall;
1709 ptf[1].X = x2 + dbig;
1710
1711 ptf[0].Y = y2 - dbig;
1712 ptf[3].Y = y2 + dsmall;
1713
1714 ptf[1].Y = y2 - dsmall;
1715 ptf[2].Y = y2 + dbig;
1716
1717 ptf[3].X = x2 - dbig;
1718 ptf[2].X = x2 + dsmall;
1719
1720 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1721
1722 round_points(pt, ptf, 4);
1723
1724 Polygon(graphics->hdc, pt, 4);
1725
1726 break;
1727 case LineCapArrowAnchor:
1728 size = size * 4.0 / sqrt(3.0);
1729
1730 dx = cos(M_PI / 6.0 + theta) * size;
1731 dy = sin(M_PI / 6.0 + theta) * size;
1732
1733 ptf[0].X = x2 - dx;
1734 ptf[0].Y = y2 - dy;
1735
1736 dx = cos(- M_PI / 6.0 + theta) * size;
1737 dy = sin(- M_PI / 6.0 + theta) * size;
1738
1739 ptf[1].X = x2 - dx;
1740 ptf[1].Y = y2 - dy;
1741
1742 ptf[2].X = x2;
1743 ptf[2].Y = y2;
1744
1745 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1746
1747 round_points(pt, ptf, 3);
1748
1749 Polygon(graphics->hdc, pt, 3);
1750
1751 break;
1752 case LineCapRoundAnchor:
1753 dx = dy = ANCHOR_WIDTH * size / 2.0;
1754
1755 ptf[0].X = x2 - dx;
1756 ptf[0].Y = y2 - dy;
1757 ptf[1].X = x2 + dx;
1758 ptf[1].Y = y2 + dy;
1759
1760 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1761
1762 round_points(pt, ptf, 2);
1763
1764 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1765
1766 break;
1767 case LineCapTriangle:
1768 size = size / 2.0;
1769 dx = cos(M_PI_2 + theta) * size;
1770 dy = sin(M_PI_2 + theta) * size;
1771
1772 ptf[0].X = x2 - dx;
1773 ptf[0].Y = y2 - dy;
1774 ptf[1].X = x2 + dx;
1775 ptf[1].Y = y2 + dy;
1776
1777 dx = cos(theta) * size;
1778 dy = sin(theta) * size;
1779
1780 ptf[2].X = x2 + dx;
1781 ptf[2].Y = y2 + dy;
1782
1783 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1784
1785 round_points(pt, ptf, 3);
1786
1787 Polygon(graphics->hdc, pt, 3);
1788
1789 break;
1790 case LineCapRound:
1791 dx = dy = size / 2.0;
1792
1793 ptf[0].X = x2 - dx;
1794 ptf[0].Y = y2 - dy;
1795 ptf[1].X = x2 + dx;
1796 ptf[1].Y = y2 + dy;
1797
1798 dx = -cos(M_PI_2 + theta) * size;
1799 dy = -sin(M_PI_2 + theta) * size;
1800
1801 ptf[2].X = x2 - dx;
1802 ptf[2].Y = y2 - dy;
1803 ptf[3].X = x2 + dx;
1804 ptf[3].Y = y2 + dy;
1805
1806 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1807
1808 round_points(pt, ptf, 4);
1809
1810 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1811 pt[2].y, pt[3].x, pt[3].y);
1812
1813 break;
1814 case LineCapCustom:
1815 if(!custom)
1816 break;
1817
1818 if (custom->type == CustomLineCapTypeAdjustableArrow)
1819 {
1820 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1821 if (arrow->cap.fill && arrow->height <= 0.0)
1822 break;
1823 }
1824
1825 count = custom->pathdata.Count;
1826 custptf = heap_alloc_zero(count * sizeof(PointF));
1827 custpt = heap_alloc_zero(count * sizeof(POINT));
1828 tp = heap_alloc_zero(count);
1829
1830 if(!custptf || !custpt || !tp)
1831 goto custend;
1832
1833 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1834
1835 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1836 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1837 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1838 MatrixOrderAppend);
1839 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1840 GdipTransformMatrixPoints(&matrix, custptf, count);
1841
1842 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1843
1844 round_points(custpt, custptf, count);
1845
1846 for(i = 0; i < count; i++)
1847 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1848
1849 if(custom->fill){
1850 BeginPath(graphics->hdc);
1851 PolyDraw(graphics->hdc, custpt, tp, count);
1852 EndPath(graphics->hdc);
1853 StrokeAndFillPath(graphics->hdc);
1854 }
1855 else
1856 PolyDraw(graphics->hdc, custpt, tp, count);
1857
1858 custend:
1859 heap_free(custptf);
1860 heap_free(custpt);
1861 heap_free(tp);
1862 break;
1863 default:
1864 break;
1865 }
1866
1867 if(!customstroke){
1868 SelectObject(graphics->hdc, oldbrush);
1869 SelectObject(graphics->hdc, oldpen);
1870 DeleteObject(brush);
1871 DeleteObject(pen);
1872 }
1873 }
1874
1875 /* Shortens the line by the given percent by changing x2, y2.
1876 * If percent is > 1.0 then the line will change direction.
1877 * If percent is negative it can lengthen the line. */
1878 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1879 {
1880 REAL dist, theta, dx, dy;
1881
1882 if((y1 == *y2) && (x1 == *x2))
1883 return;
1884
1885 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1886 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1887 dx = cos(theta) * dist;
1888 dy = sin(theta) * dist;
1889
1890 *x2 = *x2 + dx;
1891 *y2 = *y2 + dy;
1892 }
1893
1894 /* Shortens the line by the given amount by changing x2, y2.
1895 * If the amount is greater than the distance, the line will become length 0.
1896 * If the amount is negative, it can lengthen the line. */
1897 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1898 {
1899 REAL dx, dy, percent;
1900
1901 dx = *x2 - x1;
1902 dy = *y2 - y1;
1903 if(dx == 0 && dy == 0)
1904 return;
1905
1906 percent = amt / sqrt(dx * dx + dy * dy);
1907 if(percent >= 1.0){
1908 *x2 = x1;
1909 *y2 = y1;
1910 return;
1911 }
1912
1913 shorten_line_percent(x1, y1, x2, y2, percent);
1914 }
1915
1916 /* Conducts a linear search to find the bezier points that will back off
1917 * the endpoint of the curve by a distance of amt. Linear search works
1918 * better than binary in this case because there are multiple solutions,
1919 * and binary searches often find a bad one. I don't think this is what
1920 * Windows does but short of rendering the bezier without GDI's help it's
1921 * the best we can do. If rev then work from the start of the passed points
1922 * instead of the end. */
1923 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1924 {
1925 GpPointF origpt[4];
1926 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1927 INT i, first = 0, second = 1, third = 2, fourth = 3;
1928
1929 if(rev){
1930 first = 3;
1931 second = 2;
1932 third = 1;
1933 fourth = 0;
1934 }
1935
1936 origx = pt[fourth].X;
1937 origy = pt[fourth].Y;
1938 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1939
1940 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1941 /* reset bezier points to original values */
1942 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1943 /* Perform magic on bezier points. Order is important here.*/
1944 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1945 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1946 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1947 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1948 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1949 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1950
1951 dx = pt[fourth].X - origx;
1952 dy = pt[fourth].Y - origy;
1953
1954 diff = sqrt(dx * dx + dy * dy);
1955 percent += 0.0005 * amt;
1956 }
1957 }
1958
1959 /* Draws a combination of bezier curves and lines between points. */
1960 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1961 GDIPCONST BYTE * types, INT count, BOOL caps)
1962 {
1963 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1964 BYTE *tp = heap_alloc_zero(count);
1965 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1966 INT i, j;
1967 GpStatus status = GenericError;
1968
1969 if(!count){
1970 status = Ok;
1971 goto end;
1972 }
1973 if(!pti || !tp || !ptcopy){
1974 status = OutOfMemory;
1975 goto end;
1976 }
1977
1978 for(i = 1; i < count; i++){
1979 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1980 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1981 || !(types[i + 2] & PathPointTypeBezier)){
1982 ERR("Bad bezier points\n");
1983 goto end;
1984 }
1985 i += 2;
1986 }
1987 }
1988
1989 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1990
1991 /* If we are drawing caps, go through the points and adjust them accordingly,
1992 * and draw the caps. */
1993 if(caps){
1994 switch(types[count - 1] & PathPointTypePathTypeMask){
1995 case PathPointTypeBezier:
1996 if(pen->endcap == LineCapArrowAnchor)
1997 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1998 else if((pen->endcap == LineCapCustom) && pen->customend)
1999 shorten_bezier_amt(&ptcopy[count - 4],
2000 pen->width * pen->customend->inset, FALSE);
2001
2002 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
2003 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
2004 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
2005 pt[count - 1].X, pt[count - 1].Y);
2006
2007 break;
2008 case PathPointTypeLine:
2009 if(pen->endcap == LineCapArrowAnchor)
2010 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
2011 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
2012 pen->width);
2013 else if((pen->endcap == LineCapCustom) && pen->customend)
2014 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
2015 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
2016 pen->customend->inset * pen->width);
2017
2018 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
2019 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
2020 pt[count - 1].Y);
2021
2022 break;
2023 default:
2024 ERR("Bad path last point\n");
2025 goto end;
2026 }
2027
2028 /* Find start of points */
2029 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
2030 == PathPointTypeStart); j++);
2031
2032 switch(types[j] & PathPointTypePathTypeMask){
2033 case PathPointTypeBezier:
2034 if(pen->startcap == LineCapArrowAnchor)
2035 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
2036 else if((pen->startcap == LineCapCustom) && pen->customstart)
2037 shorten_bezier_amt(&ptcopy[j - 1],
2038 pen->width * pen->customstart->inset, TRUE);
2039
2040 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2041 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2042 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2043 pt[j - 1].X, pt[j - 1].Y);
2044
2045 break;
2046 case PathPointTypeLine:
2047 if(pen->startcap == LineCapArrowAnchor)
2048 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2049 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2050 pen->width);
2051 else if((pen->startcap == LineCapCustom) && pen->customstart)
2052 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2053 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2054 pen->customstart->inset * pen->width);
2055
2056 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2057 pt[j].X, pt[j].Y, pt[j - 1].X,
2058 pt[j - 1].Y);
2059
2060 break;
2061 default:
2062 ERR("Bad path points\n");
2063 goto end;
2064 }
2065 }
2066
2067 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2068
2069 round_points(pti, ptcopy, count);
2070
2071 for(i = 0; i < count; i++){
2072 tp[i] = convert_path_point_type(types[i]);
2073 }
2074
2075 PolyDraw(graphics->hdc, pti, tp, count);
2076
2077 status = Ok;
2078
2079 end:
2080 heap_free(pti);
2081 heap_free(ptcopy);
2082 heap_free(tp);
2083
2084 return status;
2085 }
2086
2087 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2088 {
2089 GpStatus result;
2090
2091 BeginPath(graphics->hdc);
2092 result = draw_poly(graphics, NULL, path->pathdata.Points,
2093 path->pathdata.Types, path->pathdata.Count, FALSE);
2094 EndPath(graphics->hdc);
2095 return result;
2096 }
2097
2098 typedef enum GraphicsContainerType {
2099 BEGIN_CONTAINER,
2100 SAVE_GRAPHICS
2101 } GraphicsContainerType;
2102
2103 typedef struct _GraphicsContainerItem {
2104 struct list entry;
2105 GraphicsContainer contid;
2106 GraphicsContainerType type;
2107
2108 SmoothingMode smoothing;
2109 CompositingQuality compqual;
2110 InterpolationMode interpolation;
2111 CompositingMode compmode;
2112 TextRenderingHint texthint;
2113 REAL scale;
2114 GpUnit unit;
2115 PixelOffsetMode pixeloffset;
2116 UINT textcontrast;
2117 GpMatrix worldtrans;
2118 GpRegion* clip;
2119 INT origin_x, origin_y;
2120 } GraphicsContainerItem;
2121
2122 static GpStatus init_container(GraphicsContainerItem** container,
2123 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2124 GpStatus sts;
2125
2126 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2127 if(!(*container))
2128 return OutOfMemory;
2129
2130 (*container)->contid = graphics->contid + 1;
2131 (*container)->type = type;
2132
2133 (*container)->smoothing = graphics->smoothing;
2134 (*container)->compqual = graphics->compqual;
2135 (*container)->interpolation = graphics->interpolation;
2136 (*container)->compmode = graphics->compmode;
2137 (*container)->texthint = graphics->texthint;
2138 (*container)->scale = graphics->scale;
2139 (*container)->unit = graphics->unit;
2140 (*container)->textcontrast = graphics->textcontrast;
2141 (*container)->pixeloffset = graphics->pixeloffset;
2142 (*container)->origin_x = graphics->origin_x;
2143 (*container)->origin_y = graphics->origin_y;
2144 (*container)->worldtrans = graphics->worldtrans;
2145
2146 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2147 if(sts != Ok){
2148 heap_free(*container);
2149 *container = NULL;
2150 return sts;
2151 }
2152
2153 return Ok;
2154 }
2155
2156 static void delete_container(GraphicsContainerItem* container)
2157 {
2158 GdipDeleteRegion(container->clip);
2159 heap_free(container);
2160 }
2161
2162 static GpStatus restore_container(GpGraphics* graphics,
2163 GDIPCONST GraphicsContainerItem* container){
2164 GpStatus sts;
2165 GpRegion *newClip;
2166
2167 sts = GdipCloneRegion(container->clip, &newClip);
2168 if(sts != Ok) return sts;
2169
2170 graphics->worldtrans = container->worldtrans;
2171
2172 GdipDeleteRegion(graphics->clip);
2173 graphics->clip = newClip;
2174
2175 graphics->contid = container->contid - 1;
2176
2177 graphics->smoothing = container->smoothing;
2178 graphics->compqual = container->compqual;
2179 graphics->interpolation = container->interpolation;
2180 graphics->compmode = container->compmode;
2181 graphics->texthint = container->texthint;
2182 graphics->scale = container->scale;
2183 graphics->unit = container->unit;
2184 graphics->textcontrast = container->textcontrast;
2185 graphics->pixeloffset = container->pixeloffset;
2186 graphics->origin_x = container->origin_x;
2187 graphics->origin_y = container->origin_y;
2188
2189 return Ok;
2190 }
2191
2192 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2193 {
2194 RECT wnd_rect;
2195 GpStatus stat=Ok;
2196 GpUnit unit;
2197
2198 if(graphics->hwnd) {
2199 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2200 return GenericError;
2201
2202 rect->X = wnd_rect.left;
2203 rect->Y = wnd_rect.top;
2204 rect->Width = wnd_rect.right - wnd_rect.left;
2205 rect->Height = wnd_rect.bottom - wnd_rect.top;
2206 }else if (graphics->image){
2207 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2208 if (stat == Ok && unit != UnitPixel)
2209 FIXME("need to convert from unit %i\n", unit);
2210 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2211 HBITMAP hbmp;
2212 BITMAP bmp;
2213
2214 rect->X = 0;
2215 rect->Y = 0;
2216
2217 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2218 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2219 {
2220 rect->Width = bmp.bmWidth;
2221 rect->Height = bmp.bmHeight;
2222 }
2223 else
2224 {
2225 /* FIXME: ??? */
2226 rect->Width = 1;
2227 rect->Height = 1;
2228 }
2229 }else{
2230 rect->X = 0;
2231 rect->Y = 0;
2232 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2233 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2234 }
2235
2236 return stat;
2237 }
2238
2239 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2240 {
2241 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2242
2243 if (stat == Ok && graphics->hdc)
2244 {
2245 GpPointF points[4], min_point, max_point;
2246 int i;
2247
2248 points[0].X = points[2].X = rect->X;
2249 points[0].Y = points[1].Y = rect->Y;
2250 points[1].X = points[3].X = rect->X + rect->Width;
2251 points[2].Y = points[3].Y = rect->Y + rect->Height;
2252
2253 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2254
2255 min_point = max_point = points[0];
2256
2257 for (i=1; i<4; i++)
2258 {
2259 if (points[i].X < min_point.X) min_point.X = points[i].X;
2260 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2261 if (points[i].X > max_point.X) max_point.X = points[i].X;
2262 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2263 }
2264
2265 rect->X = min_point.X;
2266 rect->Y = min_point.Y;
2267 rect->Width = max_point.X - min_point.X;
2268 rect->Height = max_point.Y - min_point.Y;
2269 }
2270
2271 return stat;
2272 }
2273
2274 /* on success, rgn will contain the region of the graphics object which
2275 * is visible after clipping has been applied */
2276 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2277 {
2278 GpStatus stat;
2279 GpRectF rectf;
2280 GpRegion* tmp;
2281
2282 /* Ignore graphics image bounds for metafiles */
2283 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2284 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2285
2286 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2287 return stat;
2288
2289 if((stat = GdipCreateRegion(&tmp)) != Ok)
2290 return stat;
2291
2292 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2293 goto end;
2294
2295 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2296 goto end;
2297
2298 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2299
2300 end:
2301 GdipDeleteRegion(tmp);
2302 return stat;
2303 }
2304
2305 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2306 {
2307 REAL height;
2308
2309 if (font->unit == UnitPixel)
2310 {
2311 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2312 }
2313 else
2314 {
2315 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2316 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2317 else
2318 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2319 }
2320
2321 lf->lfHeight = -(height + 0.5);
2322 lf->lfWidth = 0;
2323 lf->lfEscapement = 0;
2324 lf->lfOrientation = 0;
2325 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2326 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2327 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2328 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2329 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2330 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2331 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2332 lf->lfQuality = DEFAULT_QUALITY;
2333 lf->lfPitchAndFamily = 0;
2334 strcpyW(lf->lfFaceName, font->family->FamilyName);
2335 }
2336
2337 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2338 GDIPCONST GpStringFormat *format, HFONT *hfont,
2339 GDIPCONST GpMatrix *matrix)
2340 {
2341 HDC hdc = CreateCompatibleDC(0);
2342 GpPointF pt[3];
2343 REAL angle, rel_width, rel_height, font_height;
2344 LOGFONTW lfw;
2345 HFONT unscaled_font;
2346 TEXTMETRICW textmet;
2347
2348 if (font->unit == UnitPixel || font->unit == UnitWorld)
2349 font_height = font->emSize;
2350 else
2351 {
2352 REAL unit_scale, res;
2353
2354 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2355 unit_scale = units_scale(font->unit, graphics->unit, res);
2356
2357 font_height = font->emSize * unit_scale;
2358 }
2359
2360 pt[0].X = 0.0;
2361 pt[0].Y = 0.0;
2362 pt[1].X = 1.0;
2363 pt[1].Y = 0.0;
2364 pt[2].X = 0.0;
2365 pt[2].Y = 1.0;
2366 if (matrix)
2367 {
2368 GpMatrix xform = *matrix;
2369 GdipTransformMatrixPoints(&xform, pt, 3);
2370 }
2371
2372 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2373 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2374 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2375 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2376 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2377 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2378
2379 get_log_fontW(font, graphics, &lfw);
2380 lfw.lfHeight = -gdip_round(font_height * rel_height);
2381 unscaled_font = CreateFontIndirectW(&lfw);
2382
2383 SelectObject(hdc, unscaled_font);
2384 GetTextMetricsW(hdc, &textmet);
2385
2386 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2387 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2388
2389 *hfont = CreateFontIndirectW(&lfw);
2390
2391 DeleteDC(hdc);
2392 DeleteObject(unscaled_font);
2393 }
2394
2395 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2396 {
2397 TRACE("(%p, %p)\n", hdc, graphics);
2398
2399 return GdipCreateFromHDC2(hdc, NULL, graphics);
2400 }
2401
2402 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2403 {
2404 XFORM xform;
2405
2406 if (graphics->hdc == NULL)
2407 {
2408 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2409 return;
2410 }
2411
2412 GetTransform(graphics->hdc, 0x204, &xform);
2413 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2414 }
2415
2416 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2417 {
2418 GpStatus retval;
2419 HBITMAP hbitmap;
2420 DIBSECTION dib;
2421
2422 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2423
2424 if(hDevice != NULL)
2425 FIXME("Don't know how to handle parameter hDevice\n");
2426
2427 if(hdc == NULL)
2428 return OutOfMemory;
2429
2430 if(graphics == NULL)
2431 return InvalidParameter;
2432
2433 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2434 if(!*graphics) return OutOfMemory;
2435
2436 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2437
2438 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2439 heap_free(*graphics);
2440 return retval;
2441 }
2442
2443 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2444 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2445 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2446 {
2447 (*graphics)->alpha_hdc = 1;
2448 }
2449
2450 (*graphics)->hdc = hdc;
2451 (*graphics)->hwnd = WindowFromDC(hdc);
2452 (*graphics)->owndc = FALSE;
2453 (*graphics)->smoothing = SmoothingModeDefault;
2454 (*graphics)->compqual = CompositingQualityDefault;
2455 (*graphics)->interpolation = InterpolationModeBilinear;
2456 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2457 (*graphics)->compmode = CompositingModeSourceOver;
2458 (*graphics)->unit = UnitDisplay;
2459 (*graphics)->scale = 1.0;
2460 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2461 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2462 (*graphics)->busy = FALSE;
2463 (*graphics)->textcontrast = 4;
2464 list_init(&(*graphics)->containers);
2465 #ifdef __REACTOS__
2466 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2467 #else
2468 (*graphics)->contid = 0;
2469 #endif
2470 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2471
2472 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2473 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2474 {
2475 DeleteObject((*graphics)->gdi_clip);
2476 (*graphics)->gdi_clip = NULL;
2477 }
2478
2479 TRACE("<-- %p\n", *graphics);
2480
2481 return Ok;
2482 }
2483
2484 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2485 {
2486 GpStatus retval;
2487
2488 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2489 if(!*graphics) return OutOfMemory;
2490
2491 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2492 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2493
2494 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2495 heap_free(*graphics);
2496 return retval;
2497 }
2498
2499 (*graphics)->hdc = NULL;
2500 (*graphics)->hwnd = NULL;
2501 (*graphics)->owndc = FALSE;
2502 (*graphics)->image = image;
2503 /* We have to store the image type here because the image may be freed
2504 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2505 (*graphics)->image_type = image->type;
2506 (*graphics)->smoothing = SmoothingModeDefault;
2507 (*graphics)->compqual = CompositingQualityDefault;
2508 (*graphics)->interpolation = InterpolationModeBilinear;
2509 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2510 (*graphics)->compmode = CompositingModeSourceOver;
2511 (*graphics)->unit = UnitDisplay;
2512 (*graphics)->scale = 1.0;
2513 (*graphics)->xres = image->xres;
2514 (*graphics)->yres = image->yres;
2515 (*graphics)->busy = FALSE;
2516 (*graphics)->textcontrast = 4;
2517 list_init(&(*graphics)->containers);
2518 #ifdef __REACTOS__
2519 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2520 #else
2521 (*graphics)->contid = 0;
2522 #endif
2523
2524 TRACE("<-- %p\n", *graphics);
2525
2526 return Ok;
2527 }
2528
2529 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2530 {
2531 GpStatus ret;
2532 HDC hdc;
2533
2534 TRACE("(%p, %p)\n", hwnd, graphics);
2535
2536 hdc = GetDC(hwnd);
2537
2538 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2539 {
2540 ReleaseDC(hwnd, hdc);
2541 return ret;
2542 }
2543
2544 (*graphics)->hwnd = hwnd;
2545 (*graphics)->owndc = TRUE;
2546
2547 return Ok;
2548 }
2549
2550 /* FIXME: no icm handling */
2551 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2552 {
2553 TRACE("(%p, %p)\n", hwnd, graphics);
2554
2555 return GdipCreateFromHWND(hwnd, graphics);
2556 }
2557
2558 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2559 UINT access, IStream **stream)
2560 {
2561 DWORD dwMode;
2562 HRESULT ret;
2563
2564 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2565
2566 if(!stream || !filename)
2567 return InvalidParameter;
2568
2569 if(access & GENERIC_WRITE)
2570 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2571 else if(access & GENERIC_READ)
2572 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2573 else
2574 return InvalidParameter;
2575
2576 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2577
2578 return hresult_to_status(ret);
2579 }
2580
2581 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2582 {
2583 GraphicsContainerItem *cont, *next;
2584 GpStatus stat;
2585 TRACE("(%p)\n", graphics);
2586
2587 if(!graphics) return InvalidParameter;
2588 if(graphics->busy) return ObjectBusy;
2589
2590 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2591 {
2592 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2593 if (stat != Ok)
2594 return stat;
2595 }
2596
2597 if(graphics->owndc)
2598 ReleaseDC(graphics->hwnd, graphics->hdc);
2599
2600 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2601 list_remove(&cont->entry);
2602 delete_container(cont);
2603 }
2604
2605 GdipDeleteRegion(graphics->clip);
2606
2607 DeleteObject(graphics->gdi_clip);
2608
2609 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2610 * do otherwise, but we can't have that in the test suite because it means
2611 * accessing freed memory. */
2612 graphics->busy = TRUE;
2613
2614 heap_free(graphics);
2615
2616 return Ok;
2617 }
2618
2619 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2620 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2621 {
2622 GpStatus status;
2623 GpPath *path;
2624
2625 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2626 width, height, startAngle, sweepAngle);
2627
2628 if(!graphics || !pen || width <= 0 || height <= 0)
2629 return InvalidParameter;
2630
2631 if(graphics->busy)
2632 return ObjectBusy;
2633
2634 status = GdipCreatePath(FillModeAlternate, &path);
2635 if (status != Ok) return status;
2636
2637 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2638 if (status == Ok)
2639 status = GdipDrawPath(graphics, pen, path);
2640
2641 GdipDeletePath(path);
2642 return status;
2643 }
2644
2645 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2646 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2647 {
2648 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2649 width, height, startAngle, sweepAngle);
2650
2651 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2652 }
2653
2654 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2655 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2656 {
2657 GpPointF pt[4];
2658
2659 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2660 x2, y2, x3, y3, x4, y4);
2661
2662 if(!graphics || !pen)
2663 return InvalidParameter;
2664
2665 if(graphics->busy)
2666 return ObjectBusy;
2667
2668 pt[0].X = x1;
2669 pt[0].Y = y1;
2670 pt[1].X = x2;
2671 pt[1].Y = y2;
2672 pt[2].X = x3;
2673 pt[2].Y = y3;
2674 pt[3].X = x4;
2675 pt[3].Y = y4;
2676 return GdipDrawBeziers(graphics, pen, pt, 4);
2677 }
2678
2679 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2680 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2681 {
2682 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2683 x2, y2, x3, y3, x4, y4);
2684
2685 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2686 }
2687
2688 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2689 GDIPCONST GpPointF *points, INT count)
2690 {
2691 GpStatus status;
2692 GpPath *path;
2693
2694 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2695
2696 if(!graphics || !pen || !points || (count <= 0))
2697 return InvalidParameter;
2698
2699 if(graphics->busy)
2700 return ObjectBusy;
2701
2702 status = GdipCreatePath(FillModeAlternate, &path);
2703 if (status != Ok) return status;
2704
2705 status = GdipAddPathBeziers(path, points, count);
2706 if (status == Ok)
2707 status = GdipDrawPath(graphics, pen, path);
2708
2709 GdipDeletePath(path);
2710 return status;
2711 }
2712
2713 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2714 GDIPCONST GpPoint *points, INT count)
2715 {
2716 GpPointF *pts;
2717 GpStatus ret;
2718 INT i;
2719
2720 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2721
2722 if(!graphics || !pen || !points || (count <= 0))
2723 return InvalidParameter;
2724
2725 if(graphics->busy)
2726 return ObjectBusy;
2727
2728 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2729 if(!pts)
2730 return OutOfMemory;
2731
2732 for(i = 0; i < count; i++){
2733 pts[i].X = (REAL)points[i].X;
2734 pts[i].Y = (REAL)points[i].Y;
2735 }
2736
2737 ret = GdipDrawBeziers(graphics,pen,pts,count);
2738
2739 heap_free(pts);
2740
2741 return ret;
2742 }
2743
2744 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2745 GDIPCONST GpPointF *points, INT count)
2746 {
2747 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2748
2749 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2750 }
2751
2752 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2753 GDIPCONST GpPoint *points, INT count)
2754 {
2755 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2756
2757 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2758 }
2759
2760 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2761 GDIPCONST GpPointF *points, INT count, REAL tension)
2762 {
2763 GpPath *path;
2764 GpStatus status;
2765
2766 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2767
2768 if(!graphics || !pen || !points || count <= 0)
2769 return InvalidParameter;
2770
2771 if(graphics->busy)
2772 return ObjectBusy;
2773
2774 status = GdipCreatePath(FillModeAlternate, &path);
2775 if (status != Ok) return status;
2776
2777 status = GdipAddPathClosedCurve2(path, points, count, tension);
2778 if (status == Ok)
2779 status = GdipDrawPath(graphics, pen, path);
2780
2781 GdipDeletePath(path);
2782
2783 return status;
2784 }
2785
2786 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2787 GDIPCONST GpPoint *points, INT count, REAL tension)
2788 {
2789 GpPointF *ptf;
2790 GpStatus stat;
2791 INT i;
2792
2793 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2794
2795 if(!points || count <= 0)
2796 return InvalidParameter;
2797
2798 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2799 if(!ptf)
2800 return OutOfMemory;
2801
2802 for(i = 0; i < count; i++){
2803 ptf[i].X = (REAL)points[i].X;
2804 ptf[i].Y = (REAL)points[i].Y;
2805 }
2806
2807 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2808
2809 heap_free(ptf);
2810
2811 return stat;
2812 }
2813
2814 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2815 GDIPCONST GpPointF *points, INT count)
2816 {
2817 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2818
2819 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2820 }
2821
2822 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2823 GDIPCONST GpPoint *points, INT count)
2824 {
2825 GpPointF *pointsF;
2826 GpStatus ret;
2827 INT i;
2828
2829 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2830
2831 if(!points)
2832 return InvalidParameter;
2833
2834 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2835 if(!pointsF)
2836 return OutOfMemory;
2837
2838 for(i = 0; i < count; i++){
2839 pointsF[i].X = (REAL)points[i].X;
2840 pointsF[i].Y = (REAL)points[i].Y;
2841 }
2842
2843 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2844 heap_free(pointsF);
2845
2846 return ret;
2847 }
2848
2849 /* Approximates cardinal spline with Bezier curves. */
2850 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2851 GDIPCONST GpPointF *points, INT count, REAL tension)
2852 {
2853 GpPath *path;
2854 GpStatus status;
2855
2856 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2857
2858 if(!graphics || !pen)
2859 return InvalidParameter;
2860
2861 if(graphics->busy)
2862 return ObjectBusy;
2863
2864 if(count < 2)
2865 return InvalidParameter;
2866
2867 status = GdipCreatePath(FillModeAlternate, &path);
2868 if (status != Ok) return status;
2869
2870 status = GdipAddPathCurve2(path, points, count, tension);
2871 if (status == Ok)
2872 status = GdipDrawPath(graphics, pen, path);
2873
2874 GdipDeletePath(path);
2875 return status;
2876 }
2877
2878 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2879 GDIPCONST GpPoint *points, INT count, REAL tension)
2880 {
2881 GpPointF *pointsF;
2882 GpStatus ret;
2883 INT i;
2884
2885 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2886
2887 if(!points)
2888 return InvalidParameter;
2889
2890 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2891 if(!pointsF)
2892 return OutOfMemory;
2893
2894 for(i = 0; i < count; i++){
2895 pointsF[i].X = (REAL)points[i].X;
2896 pointsF[i].Y = (REAL)points[i].Y;
2897 }
2898
2899 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2900 heap_free(pointsF);
2901
2902 return ret;
2903 }
2904
2905 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2906 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2907 REAL tension)
2908 {
2909 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2910
2911 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2912 return InvalidParameter;
2913 }
2914
2915 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2916 }
2917
2918 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2919 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2920 REAL tension)
2921 {
2922 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2923
2924 if(count < 0){
2925 return OutOfMemory;
2926 }
2927
2928 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2929 return InvalidParameter;
2930 }
2931
2932 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2933 }
2934
2935 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2936 REAL y, REAL width, REAL height)
2937 {
2938 GpPath *path;
2939 GpStatus status;
2940
2941 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2942
2943 if(!graphics || !pen)
2944 return InvalidParameter;
2945
2946 if(graphics->busy)
2947 return ObjectBusy;
2948
2949 status = GdipCreatePath(FillModeAlternate, &path);
2950 if (status != Ok) return status;
2951
2952 status = GdipAddPathEllipse(path, x, y, width, height);
2953 if (status == Ok)
2954 status = GdipDrawPath(graphics, pen, path);
2955
2956 GdipDeletePath(path);
2957 return status;
2958 }
2959
2960 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2961 INT y, INT width, INT height)
2962 {
2963 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2964
2965 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2966 }
2967
2968
2969 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2970 {
2971 UINT width, height;
2972
2973 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2974
2975 if(!graphics || !image)
2976 return InvalidParameter;
2977
2978 GdipGetImageWidth(image, &width);
2979 GdipGetImageHeight(image, &height);
2980
2981 return GdipDrawImagePointRect(graphics, image, x, y,
2982 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2983 }
2984
2985 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2986 INT y)
2987 {
2988 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2989
2990 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2991 }
2992
2993 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2994 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2995 GpUnit srcUnit)
2996 {
2997 GpPointF points[3];
2998 REAL scale_x, scale_y, width, height;
2999
3000 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3001
3002 if (!graphics || !image) return InvalidParameter;
3003
3004 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3005 scale_x *= graphics->xres / image->xres;
3006 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3007 scale_y *= graphics->yres / image->yres;
3008 width = srcwidth * scale_x;
3009 height = srcheight * scale_y;
3010
3011 points[0].X = points[2].X = x;
3012 points[0].Y = points[1].Y = y;
3013 points[1].X = x + width;
3014 points[2].Y = y + height;
3015
3016 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3017 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3018 }
3019
3020 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3021 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3022 GpUnit srcUnit)
3023 {
3024 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3025 }
3026
3027 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3028 GDIPCONST GpPointF *dstpoints, INT count)
3029 {
3030 UINT width, height;
3031
3032 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3033
3034 if(!image)
3035 return InvalidParameter;
3036
3037 GdipGetImageWidth(image, &width);
3038 GdipGetImageHeight(image, &height);
3039
3040 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3041 width, height, UnitPixel, NULL, NULL, NULL);
3042 }
3043
3044 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3045 GDIPCONST GpPoint *dstpoints, INT count)
3046 {
3047 GpPointF ptf[3];
3048
3049 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3050
3051 if (count != 3 || !dstpoints)
3052 return InvalidParameter;
3053
3054 ptf[0].X = (REAL)dstpoints[0].X;
3055 ptf[0].Y = (REAL)dstpoints[0].Y;
3056 ptf[1].X = (REAL)dstpoints[1].X;
3057 ptf[1].Y = (REAL)dstpoints[1].Y;
3058 ptf[2].X = (REAL)dstpoints[2].X;
3059 ptf[2].Y = (REAL)dstpoints[2].Y;
3060
3061 return GdipDrawImagePoints(graphics, image, ptf, count);
3062 }
3063
3064 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3065 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3066 {
3067 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3068 return TRUE;
3069 }
3070
3071 GpStatus WINGDIPAPI GdipDrawImagePointsRect