1e2aff14f4f4c01bb92b89cfee48cdd8c5c89b63
[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 void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1160 {
1161 switch (brush->bt)
1162 {
1163 case BrushTypeSolidColor:
1164 {
1165 GpSolidFill *fill = (GpSolidFill*)brush;
1166 HBITMAP bmp = ARGB2BMP(fill->color);
1167
1168 if (bmp)
1169 {
1170 RECT rc;
1171 /* partially transparent fill */
1172
1173 SelectClipPath(graphics->hdc, RGN_AND);
1174 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1175 {
1176 HDC hdc = CreateCompatibleDC(NULL);
1177
1178 if (!hdc) break;
1179
1180 SelectObject(hdc, bmp);
1181 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1182 hdc, 0, 0, 1, 1);
1183 DeleteDC(hdc);
1184 }
1185
1186 DeleteObject(bmp);
1187 break;
1188 }
1189 /* else fall through */
1190 }
1191 default:
1192 {
1193 HBRUSH gdibrush, old_brush;
1194
1195 gdibrush = create_gdi_brush(brush);
1196 if (!gdibrush) return;
1197
1198 old_brush = SelectObject(graphics->hdc, gdibrush);
1199 FillPath(graphics->hdc);
1200 SelectObject(graphics->hdc, old_brush);
1201 DeleteObject(gdibrush);
1202 break;
1203 }
1204 }
1205 }
1206
1207 static BOOL brush_can_fill_pixels(GpBrush *brush)
1208 {
1209 switch (brush->bt)
1210 {
1211 case BrushTypeSolidColor:
1212 case BrushTypeHatchFill:
1213 case BrushTypeLinearGradient:
1214 case BrushTypeTextureFill:
1215 case BrushTypePathGradient:
1216 return TRUE;
1217 default:
1218 return FALSE;
1219 }
1220 }
1221
1222 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1223 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1224 {
1225 switch (brush->bt)
1226 {
1227 case BrushTypeSolidColor:
1228 {
1229 int x, y;
1230 GpSolidFill *fill = (GpSolidFill*)brush;
1231 for (x=0; x<fill_area->Width; x++)
1232 for (y=0; y<fill_area->Height; y++)
1233 argb_pixels[x + y*cdwStride] = fill->color;
1234 return Ok;
1235 }
1236 case BrushTypeHatchFill:
1237 {
1238 int x, y;
1239 GpHatch *fill = (GpHatch*)brush;
1240 const char *hatch_data;
1241
1242 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1243 return NotImplemented;
1244
1245 for (x=0; x<fill_area->Width; x++)
1246 for (y=0; y<fill_area->Height; y++)
1247 {
1248 int hx, hy;
1249
1250 /* FIXME: Account for the rendering origin */
1251 hx = (x + fill_area->X) % 8;
1252 hy = (y + fill_area->Y) % 8;
1253
1254 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1255 argb_pixels[x + y*cdwStride] = fill->forecol;
1256 else
1257 argb_pixels[x + y*cdwStride] = fill->backcol;
1258 }
1259
1260 return Ok;
1261 }
1262 case BrushTypeLinearGradient:
1263 {
1264 GpLineGradient *fill = (GpLineGradient*)brush;
1265 GpPointF draw_points[3];
1266 GpStatus stat;
1267 int x, y;
1268
1269 draw_points[0].X = fill_area->X;
1270 draw_points[0].Y = fill_area->Y;
1271 draw_points[1].X = fill_area->X+1;
1272 draw_points[1].Y = fill_area->Y;
1273 draw_points[2].X = fill_area->X;
1274 draw_points[2].Y = fill_area->Y+1;
1275
1276 /* Transform the points to a co-ordinate space where X is the point's
1277 * position in the gradient, 0.0 being the start point and 1.0 the
1278 * end point. */
1279 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1280 WineCoordinateSpaceGdiDevice, draw_points, 3);
1281
1282 if (stat == Ok)
1283 {
1284 GpMatrix world_to_gradient = fill->transform;
1285
1286 stat = GdipInvertMatrix(&world_to_gradient);
1287 if (stat == Ok)
1288 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1289 }
1290
1291 if (stat == Ok)
1292 {
1293 REAL x_delta = draw_points[1].X - draw_points[0].X;
1294 REAL y_delta = draw_points[2].X - draw_points[0].X;
1295
1296 for (y=0; y<fill_area->Height; y++)
1297 {
1298 for (x=0; x<fill_area->Width; x++)
1299 {
1300 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1301
1302 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1303 }
1304 }
1305 }
1306
1307 return stat;
1308 }
1309 case BrushTypeTextureFill:
1310 {
1311 GpTexture *fill = (GpTexture*)brush;
1312 GpPointF draw_points[3];
1313 GpStatus stat;
1314 int x, y;
1315 GpBitmap *bitmap;
1316 int src_stride;
1317 GpRect src_area;
1318
1319 if (fill->image->type != ImageTypeBitmap)
1320 {
1321 FIXME("metafile texture brushes not implemented\n");
1322 return NotImplemented;
1323 }
1324
1325 bitmap = (GpBitmap*)fill->image;
1326 src_stride = sizeof(ARGB) * bitmap->width;
1327
1328 src_area.X = src_area.Y = 0;
1329 src_area.Width = bitmap->width;
1330 src_area.Height = bitmap->height;
1331
1332 draw_points[0].X = fill_area->X;
1333 draw_points[0].Y = fill_area->Y;
1334 draw_points[1].X = fill_area->X+1;
1335 draw_points[1].Y = fill_area->Y;
1336 draw_points[2].X = fill_area->X;
1337 draw_points[2].Y = fill_area->Y+1;
1338
1339 /* Transform the points to the co-ordinate space of the bitmap. */
1340 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1341 WineCoordinateSpaceGdiDevice, draw_points, 3);
1342
1343 if (stat == Ok)
1344 {
1345 GpMatrix world_to_texture = fill->transform;
1346
1347 stat = GdipInvertMatrix(&world_to_texture);
1348 if (stat == Ok)
1349 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1350 }
1351
1352 if (stat == Ok && !fill->bitmap_bits)
1353 {
1354 BitmapData lockeddata;
1355
1356 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1357 if (!fill->bitmap_bits)
1358 stat = OutOfMemory;
1359
1360 if (stat == Ok)
1361 {
1362 lockeddata.Width = bitmap->width;
1363 lockeddata.Height = bitmap->height;
1364 lockeddata.Stride = src_stride;
1365 lockeddata.PixelFormat = PixelFormat32bppARGB;
1366 lockeddata.Scan0 = fill->bitmap_bits;
1367
1368 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1369 PixelFormat32bppARGB, &lockeddata);
1370 }
1371
1372 if (stat == Ok)
1373 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1374
1375 if (stat == Ok)
1376 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1377 bitmap->width, bitmap->height,
1378 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1379
1380 if (stat != Ok)
1381 {
1382 heap_free(fill->bitmap_bits);
1383 fill->bitmap_bits = NULL;
1384 }
1385 }
1386
1387 if (stat == Ok)
1388 {
1389 REAL x_dx = draw_points[1].X - draw_points[0].X;
1390 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1391 REAL y_dx = draw_points[2].X - draw_points[0].X;
1392 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1393
1394 for (y=0; y<fill_area->Height; y++)
1395 {
1396 for (x=0; x<fill_area->Width; x++)
1397 {
1398 GpPointF point;
1399 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1400 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1401
1402 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1403 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1404 &point, fill->imageattributes, graphics->interpolation,
1405 graphics->pixeloffset);
1406 }
1407 }
1408 }
1409
1410 return stat;
1411 }
1412 case BrushTypePathGradient:
1413 {
1414 GpPathGradient *fill = (GpPathGradient*)brush;
1415 GpPath *flat_path;
1416 GpMatrix world_to_device;
1417 GpStatus stat;
1418 int i, figure_start=0;
1419 GpPointF start_point, end_point, center_point;
1420 BYTE type;
1421 REAL min_yf, max_yf, line1_xf, line2_xf;
1422 INT min_y, max_y, min_x, max_x;
1423 INT x, y;
1424 ARGB outer_color;
1425 static BOOL transform_fixme_once;
1426
1427 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1428 {
1429 static int once;
1430 if (!once++)
1431 FIXME("path gradient focus not implemented\n");
1432 }
1433
1434 if (fill->gamma)
1435 {
1436 static int once;
1437 if (!once++)
1438 FIXME("path gradient gamma correction not implemented\n");
1439 }
1440
1441 if (fill->blendcount)
1442 {
1443 static int once;
1444 if (!once++)
1445 FIXME("path gradient blend not implemented\n");
1446 }
1447
1448 if (fill->pblendcount)
1449 {
1450 static int once;
1451 if (!once++)
1452 FIXME("path gradient preset blend not implemented\n");
1453 }
1454
1455 if (!transform_fixme_once)
1456 {
1457 BOOL is_identity=TRUE;
1458 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1459 if (!is_identity)
1460 {
1461 FIXME("path gradient transform not implemented\n");
1462 transform_fixme_once = TRUE;
1463 }
1464 }
1465
1466 stat = GdipClonePath(fill->path, &flat_path);
1467
1468 if (stat != Ok)
1469 return stat;
1470
1471 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1472 CoordinateSpaceWorld, &world_to_device);
1473 if (stat == Ok)
1474 {
1475 stat = GdipTransformPath(flat_path, &world_to_device);
1476
1477 if (stat == Ok)
1478 {
1479 center_point = fill->center;
1480 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1481 }
1482
1483 if (stat == Ok)
1484 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1485 }
1486
1487 if (stat != Ok)
1488 {
1489 GdipDeletePath(flat_path);
1490 return stat;
1491 }
1492
1493 for (i=0; i<flat_path->pathdata.Count; i++)
1494 {
1495 int start_center_line=0, end_center_line=0;
1496 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1497 REAL center_distance;
1498 ARGB start_color, end_color;
1499 REAL dy, dx;
1500
1501 type = flat_path->pathdata.Types[i];
1502
1503 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1504 figure_start = i;
1505
1506 start_point = flat_path->pathdata.Points[i];
1507
1508 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1509
1510 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1511 {
1512 end_point = flat_path->pathdata.Points[figure_start];
1513 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1514 }
1515 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1516 {
1517 end_point = flat_path->pathdata.Points[i+1];
1518 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1519 }
1520 else
1521 continue;
1522
1523 outer_color = start_color;
1524
1525 min_yf = center_point.Y;
1526 if (min_yf > start_point.Y) min_yf = start_point.Y;
1527 if (min_yf > end_point.Y) min_yf = end_point.Y;
1528
1529 if (min_yf < fill_area->Y)
1530 min_y = fill_area->Y;
1531 else
1532 min_y = (INT)ceil(min_yf);
1533
1534 max_yf = center_point.Y;
1535 if (max_yf < start_point.Y) max_yf = start_point.Y;
1536 if (max_yf < end_point.Y) max_yf = end_point.Y;
1537
1538 if (max_yf > fill_area->Y + fill_area->Height)
1539 max_y = fill_area->Y + fill_area->Height;
1540 else
1541 max_y = (INT)ceil(max_yf);
1542
1543 dy = end_point.Y - start_point.Y;
1544 dx = end_point.X - start_point.X;
1545
1546 /* This is proportional to the distance from start-end line to center point. */
1547 center_distance = dy * (start_point.X - center_point.X) +
1548 dx * (center_point.Y - start_point.Y);
1549
1550 for (y=min_y; y<max_y; y++)
1551 {
1552 REAL yf = (REAL)y;
1553
1554 if (!seen_start && yf >= start_point.Y)
1555 {
1556 seen_start = TRUE;
1557 start_center_line ^= 1;
1558 }
1559 if (!seen_end && yf >= end_point.Y)
1560 {
1561 seen_end = TRUE;
1562 end_center_line ^= 1;
1563 }
1564 if (!seen_center && yf >= center_point.Y)
1565 {
1566 seen_center = TRUE;
1567 start_center_line ^= 1;
1568 end_center_line ^= 1;
1569 }
1570
1571 if (start_center_line)
1572 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1573 else
1574 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1575
1576 if (end_center_line)
1577 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1578 else
1579 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1580
1581 if (line1_xf < line2_xf)
1582 {
1583 min_x = (INT)ceil(line1_xf);
1584 max_x = (INT)ceil(line2_xf);
1585 }
1586 else
1587 {
1588 min_x = (INT)ceil(line2_xf);
1589 max_x = (INT)ceil(line1_xf);
1590 }
1591
1592 if (min_x < fill_area->X)
1593 min_x = fill_area->X;
1594 if (max_x > fill_area->X + fill_area->Width)
1595 max_x = fill_area->X + fill_area->Width;
1596
1597 for (x=min_x; x<max_x; x++)
1598 {
1599 REAL xf = (REAL)x;
1600 REAL distance;
1601
1602 if (start_color != end_color)
1603 {
1604 REAL blend_amount, pdy, pdx;
1605 pdy = yf - center_point.Y;
1606 pdx = xf - center_point.X;
1607
1608 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1609 {
1610 /* Too close to center point, don't try to calculate outer color */
1611 outer_color = start_color;
1612 }
1613 else
1614 {
1615 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1616 outer_color = blend_colors(start_color, end_color, blend_amount);
1617 }
1618 }
1619
1620 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1621 (end_point.X - start_point.X) * (yf - start_point.Y);
1622
1623 distance = distance / center_distance;
1624
1625 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1626 blend_colors(outer_color, fill->centercolor, distance);
1627 }
1628 }
1629 }
1630
1631 GdipDeletePath(flat_path);
1632 return stat;
1633 }
1634 default:
1635 return NotImplemented;
1636 }
1637 }
1638
1639 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1640 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1641 * should not be called on an hdc that has a path you care about. */
1642 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1643 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1644 {
1645 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1646 GpMatrix matrix;
1647 HBRUSH brush = NULL;
1648 HPEN pen = NULL;
1649 PointF ptf[4], *custptf = NULL;
1650 POINT pt[4], *custpt = NULL;
1651 BYTE *tp = NULL;
1652 REAL theta, dsmall, dbig, dx, dy = 0.0;
1653 INT i, count;
1654 LOGBRUSH lb;
1655 BOOL customstroke;
1656
1657 if((x1 == x2) && (y1 == y2))
1658 return;
1659
1660 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1661
1662 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1663 if(!customstroke){
1664 brush = CreateSolidBrush(color);
1665 lb.lbStyle = BS_SOLID;
1666 lb.lbColor = color;
1667 lb.lbHatch = 0;
1668 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1669 PS_JOIN_MITER, 1, &lb, 0,
1670 NULL);
1671 oldbrush = SelectObject(graphics->hdc, brush);
1672 oldpen = SelectObject(graphics->hdc, pen);
1673 }
1674
1675 switch(cap){
1676 case LineCapFlat:
1677 break;
1678 case LineCapSquare:
1679 case LineCapSquareAnchor:
1680 case LineCapDiamondAnchor:
1681 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1682 if(cap == LineCapDiamondAnchor){
1683 dsmall = cos(theta + M_PI_2) * size;
1684 dbig = sin(theta + M_PI_2) * size;
1685 }
1686 else{
1687 dsmall = cos(theta + M_PI_4) * size;
1688 dbig = sin(theta + M_PI_4) * size;
1689 }
1690
1691 ptf[0].X = x2 - dsmall;
1692 ptf[1].X = x2 + dbig;
1693
1694 ptf[0].Y = y2 - dbig;
1695 ptf[3].Y = y2 + dsmall;
1696
1697 ptf[1].Y = y2 - dsmall;
1698 ptf[2].Y = y2 + dbig;
1699
1700 ptf[3].X = x2 - dbig;
1701 ptf[2].X = x2 + dsmall;
1702
1703 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1704
1705 round_points(pt, ptf, 4);
1706
1707 Polygon(graphics->hdc, pt, 4);
1708
1709 break;
1710 case LineCapArrowAnchor:
1711 size = size * 4.0 / sqrt(3.0);
1712
1713 dx = cos(M_PI / 6.0 + theta) * size;
1714 dy = sin(M_PI / 6.0 + theta) * size;
1715
1716 ptf[0].X = x2 - dx;
1717 ptf[0].Y = y2 - dy;
1718
1719 dx = cos(- M_PI / 6.0 + theta) * size;
1720 dy = sin(- M_PI / 6.0 + theta) * size;
1721
1722 ptf[1].X = x2 - dx;
1723 ptf[1].Y = y2 - dy;
1724
1725 ptf[2].X = x2;
1726 ptf[2].Y = y2;
1727
1728 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1729
1730 round_points(pt, ptf, 3);
1731
1732 Polygon(graphics->hdc, pt, 3);
1733
1734 break;
1735 case LineCapRoundAnchor:
1736 dx = dy = ANCHOR_WIDTH * size / 2.0;
1737
1738 ptf[0].X = x2 - dx;
1739 ptf[0].Y = y2 - dy;
1740 ptf[1].X = x2 + dx;
1741 ptf[1].Y = y2 + dy;
1742
1743 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1744
1745 round_points(pt, ptf, 2);
1746
1747 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1748
1749 break;
1750 case LineCapTriangle:
1751 size = size / 2.0;
1752 dx = cos(M_PI_2 + theta) * size;
1753 dy = sin(M_PI_2 + theta) * size;
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 dx = cos(theta) * size;
1761 dy = sin(theta) * size;
1762
1763 ptf[2].X = x2 + dx;
1764 ptf[2].Y = y2 + dy;
1765
1766 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1767
1768 round_points(pt, ptf, 3);
1769
1770 Polygon(graphics->hdc, pt, 3);
1771
1772 break;
1773 case LineCapRound:
1774 dx = dy = size / 2.0;
1775
1776 ptf[0].X = x2 - dx;
1777 ptf[0].Y = y2 - dy;
1778 ptf[1].X = x2 + dx;
1779 ptf[1].Y = y2 + dy;
1780
1781 dx = -cos(M_PI_2 + theta) * size;
1782 dy = -sin(M_PI_2 + theta) * size;
1783
1784 ptf[2].X = x2 - dx;
1785 ptf[2].Y = y2 - dy;
1786 ptf[3].X = x2 + dx;
1787 ptf[3].Y = y2 + dy;
1788
1789 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1790
1791 round_points(pt, ptf, 4);
1792
1793 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1794 pt[2].y, pt[3].x, pt[3].y);
1795
1796 break;
1797 case LineCapCustom:
1798 if(!custom)
1799 break;
1800
1801 if (custom->type == CustomLineCapTypeAdjustableArrow)
1802 {
1803 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1804 if (arrow->cap.fill && arrow->height <= 0.0)
1805 break;
1806 }
1807
1808 count = custom->pathdata.Count;
1809 custptf = heap_alloc_zero(count * sizeof(PointF));
1810 custpt = heap_alloc_zero(count * sizeof(POINT));
1811 tp = heap_alloc_zero(count);
1812
1813 if(!custptf || !custpt || !tp)
1814 goto custend;
1815
1816 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1817
1818 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1819 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1820 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1821 MatrixOrderAppend);
1822 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1823 GdipTransformMatrixPoints(&matrix, custptf, count);
1824
1825 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1826
1827 round_points(custpt, custptf, count);
1828
1829 for(i = 0; i < count; i++)
1830 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1831
1832 if(custom->fill){
1833 BeginPath(graphics->hdc);
1834 PolyDraw(graphics->hdc, custpt, tp, count);
1835 EndPath(graphics->hdc);
1836 StrokeAndFillPath(graphics->hdc);
1837 }
1838 else
1839 PolyDraw(graphics->hdc, custpt, tp, count);
1840
1841 custend:
1842 heap_free(custptf);
1843 heap_free(custpt);
1844 heap_free(tp);
1845 break;
1846 default:
1847 break;
1848 }
1849
1850 if(!customstroke){
1851 SelectObject(graphics->hdc, oldbrush);
1852 SelectObject(graphics->hdc, oldpen);
1853 DeleteObject(brush);
1854 DeleteObject(pen);
1855 }
1856 }
1857
1858 /* Shortens the line by the given percent by changing x2, y2.
1859 * If percent is > 1.0 then the line will change direction.
1860 * If percent is negative it can lengthen the line. */
1861 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1862 {
1863 REAL dist, theta, dx, dy;
1864
1865 if((y1 == *y2) && (x1 == *x2))
1866 return;
1867
1868 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1869 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1870 dx = cos(theta) * dist;
1871 dy = sin(theta) * dist;
1872
1873 *x2 = *x2 + dx;
1874 *y2 = *y2 + dy;
1875 }
1876
1877 /* Shortens the line by the given amount by changing x2, y2.
1878 * If the amount is greater than the distance, the line will become length 0.
1879 * If the amount is negative, it can lengthen the line. */
1880 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1881 {
1882 REAL dx, dy, percent;
1883
1884 dx = *x2 - x1;
1885 dy = *y2 - y1;
1886 if(dx == 0 && dy == 0)
1887 return;
1888
1889 percent = amt / sqrt(dx * dx + dy * dy);
1890 if(percent >= 1.0){
1891 *x2 = x1;
1892 *y2 = y1;
1893 return;
1894 }
1895
1896 shorten_line_percent(x1, y1, x2, y2, percent);
1897 }
1898
1899 /* Conducts a linear search to find the bezier points that will back off
1900 * the endpoint of the curve by a distance of amt. Linear search works
1901 * better than binary in this case because there are multiple solutions,
1902 * and binary searches often find a bad one. I don't think this is what
1903 * Windows does but short of rendering the bezier without GDI's help it's
1904 * the best we can do. If rev then work from the start of the passed points
1905 * instead of the end. */
1906 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1907 {
1908 GpPointF origpt[4];
1909 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1910 INT i, first = 0, second = 1, third = 2, fourth = 3;
1911
1912 if(rev){
1913 first = 3;
1914 second = 2;
1915 third = 1;
1916 fourth = 0;
1917 }
1918
1919 origx = pt[fourth].X;
1920 origy = pt[fourth].Y;
1921 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1922
1923 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1924 /* reset bezier points to original values */
1925 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1926 /* Perform magic on bezier points. Order is important here.*/
1927 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1928 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1929 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1930 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1931 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1932 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1933
1934 dx = pt[fourth].X - origx;
1935 dy = pt[fourth].Y - origy;
1936
1937 diff = sqrt(dx * dx + dy * dy);
1938 percent += 0.0005 * amt;
1939 }
1940 }
1941
1942 /* Draws a combination of bezier curves and lines between points. */
1943 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1944 GDIPCONST BYTE * types, INT count, BOOL caps)
1945 {
1946 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1947 BYTE *tp = heap_alloc_zero(count);
1948 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1949 INT i, j;
1950 GpStatus status = GenericError;
1951
1952 if(!count){
1953 status = Ok;
1954 goto end;
1955 }
1956 if(!pti || !tp || !ptcopy){
1957 status = OutOfMemory;
1958 goto end;
1959 }
1960
1961 for(i = 1; i < count; i++){
1962 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1963 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1964 || !(types[i + 2] & PathPointTypeBezier)){
1965 ERR("Bad bezier points\n");
1966 goto end;
1967 }
1968 i += 2;
1969 }
1970 }
1971
1972 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1973
1974 /* If we are drawing caps, go through the points and adjust them accordingly,
1975 * and draw the caps. */
1976 if(caps){
1977 switch(types[count - 1] & PathPointTypePathTypeMask){
1978 case PathPointTypeBezier:
1979 if(pen->endcap == LineCapArrowAnchor)
1980 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1981 else if((pen->endcap == LineCapCustom) && pen->customend)
1982 shorten_bezier_amt(&ptcopy[count - 4],
1983 pen->width * pen->customend->inset, FALSE);
1984
1985 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1986 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1987 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1988 pt[count - 1].X, pt[count - 1].Y);
1989
1990 break;
1991 case PathPointTypeLine:
1992 if(pen->endcap == LineCapArrowAnchor)
1993 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1994 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1995 pen->width);
1996 else if((pen->endcap == LineCapCustom) && pen->customend)
1997 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1998 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1999 pen->customend->inset * pen->width);
2000
2001 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
2002 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
2003 pt[count - 1].Y);
2004
2005 break;
2006 default:
2007 ERR("Bad path last point\n");
2008 goto end;
2009 }
2010
2011 /* Find start of points */
2012 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
2013 == PathPointTypeStart); j++);
2014
2015 switch(types[j] & PathPointTypePathTypeMask){
2016 case PathPointTypeBezier:
2017 if(pen->startcap == LineCapArrowAnchor)
2018 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
2019 else if((pen->startcap == LineCapCustom) && pen->customstart)
2020 shorten_bezier_amt(&ptcopy[j - 1],
2021 pen->width * pen->customstart->inset, TRUE);
2022
2023 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2024 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2025 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2026 pt[j - 1].X, pt[j - 1].Y);
2027
2028 break;
2029 case PathPointTypeLine:
2030 if(pen->startcap == LineCapArrowAnchor)
2031 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2032 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2033 pen->width);
2034 else if((pen->startcap == LineCapCustom) && pen->customstart)
2035 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2036 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2037 pen->customstart->inset * pen->width);
2038
2039 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2040 pt[j].X, pt[j].Y, pt[j - 1].X,
2041 pt[j - 1].Y);
2042
2043 break;
2044 default:
2045 ERR("Bad path points\n");
2046 goto end;
2047 }
2048 }
2049
2050 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2051
2052 round_points(pti, ptcopy, count);
2053
2054 for(i = 0; i < count; i++){
2055 tp[i] = convert_path_point_type(types[i]);
2056 }
2057
2058 PolyDraw(graphics->hdc, pti, tp, count);
2059
2060 status = Ok;
2061
2062 end:
2063 heap_free(pti);
2064 heap_free(ptcopy);
2065 heap_free(tp);
2066
2067 return status;
2068 }
2069
2070 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2071 {
2072 GpStatus result;
2073
2074 BeginPath(graphics->hdc);
2075 result = draw_poly(graphics, NULL, path->pathdata.Points,
2076 path->pathdata.Types, path->pathdata.Count, FALSE);
2077 EndPath(graphics->hdc);
2078 return result;
2079 }
2080
2081 typedef enum GraphicsContainerType {
2082 BEGIN_CONTAINER,
2083 SAVE_GRAPHICS
2084 } GraphicsContainerType;
2085
2086 typedef struct _GraphicsContainerItem {
2087 struct list entry;
2088 GraphicsContainer contid;
2089 GraphicsContainerType type;
2090
2091 SmoothingMode smoothing;
2092 CompositingQuality compqual;
2093 InterpolationMode interpolation;
2094 CompositingMode compmode;
2095 TextRenderingHint texthint;
2096 REAL scale;
2097 GpUnit unit;
2098 PixelOffsetMode pixeloffset;
2099 UINT textcontrast;
2100 GpMatrix worldtrans;
2101 GpRegion* clip;
2102 INT origin_x, origin_y;
2103 } GraphicsContainerItem;
2104
2105 static GpStatus init_container(GraphicsContainerItem** container,
2106 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2107 GpStatus sts;
2108
2109 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2110 if(!(*container))
2111 return OutOfMemory;
2112
2113 (*container)->contid = graphics->contid + 1;
2114 (*container)->type = type;
2115
2116 (*container)->smoothing = graphics->smoothing;
2117 (*container)->compqual = graphics->compqual;
2118 (*container)->interpolation = graphics->interpolation;
2119 (*container)->compmode = graphics->compmode;
2120 (*container)->texthint = graphics->texthint;
2121 (*container)->scale = graphics->scale;
2122 (*container)->unit = graphics->unit;
2123 (*container)->textcontrast = graphics->textcontrast;
2124 (*container)->pixeloffset = graphics->pixeloffset;
2125 (*container)->origin_x = graphics->origin_x;
2126 (*container)->origin_y = graphics->origin_y;
2127 (*container)->worldtrans = graphics->worldtrans;
2128
2129 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2130 if(sts != Ok){
2131 heap_free(*container);
2132 *container = NULL;
2133 return sts;
2134 }
2135
2136 return Ok;
2137 }
2138
2139 static void delete_container(GraphicsContainerItem* container)
2140 {
2141 GdipDeleteRegion(container->clip);
2142 heap_free(container);
2143 }
2144
2145 static GpStatus restore_container(GpGraphics* graphics,
2146 GDIPCONST GraphicsContainerItem* container){
2147 GpStatus sts;
2148 GpRegion *newClip;
2149
2150 sts = GdipCloneRegion(container->clip, &newClip);
2151 if(sts != Ok) return sts;
2152
2153 graphics->worldtrans = container->worldtrans;
2154
2155 GdipDeleteRegion(graphics->clip);
2156 graphics->clip = newClip;
2157
2158 graphics->contid = container->contid - 1;
2159
2160 graphics->smoothing = container->smoothing;
2161 graphics->compqual = container->compqual;
2162 graphics->interpolation = container->interpolation;
2163 graphics->compmode = container->compmode;
2164 graphics->texthint = container->texthint;
2165 graphics->scale = container->scale;
2166 graphics->unit = container->unit;
2167 graphics->textcontrast = container->textcontrast;
2168 graphics->pixeloffset = container->pixeloffset;
2169 graphics->origin_x = container->origin_x;
2170 graphics->origin_y = container->origin_y;
2171
2172 return Ok;
2173 }
2174
2175 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2176 {
2177 RECT wnd_rect;
2178 GpStatus stat=Ok;
2179 GpUnit unit;
2180
2181 if(graphics->hwnd) {
2182 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2183 return GenericError;
2184
2185 rect->X = wnd_rect.left;
2186 rect->Y = wnd_rect.top;
2187 rect->Width = wnd_rect.right - wnd_rect.left;
2188 rect->Height = wnd_rect.bottom - wnd_rect.top;
2189 }else if (graphics->image){
2190 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2191 if (stat == Ok && unit != UnitPixel)
2192 FIXME("need to convert from unit %i\n", unit);
2193 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2194 HBITMAP hbmp;
2195 BITMAP bmp;
2196
2197 rect->X = 0;
2198 rect->Y = 0;
2199
2200 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2201 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2202 {
2203 rect->Width = bmp.bmWidth;
2204 rect->Height = bmp.bmHeight;
2205 }
2206 else
2207 {
2208 /* FIXME: ??? */
2209 rect->Width = 1;
2210 rect->Height = 1;
2211 }
2212 }else{
2213 rect->X = 0;
2214 rect->Y = 0;
2215 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2216 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2217 }
2218
2219 return stat;
2220 }
2221
2222 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2223 {
2224 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2225
2226 if (stat == Ok && graphics->hdc)
2227 {
2228 GpPointF points[4], min_point, max_point;
2229 int i;
2230
2231 points[0].X = points[2].X = rect->X;
2232 points[0].Y = points[1].Y = rect->Y;
2233 points[1].X = points[3].X = rect->X + rect->Width;
2234 points[2].Y = points[3].Y = rect->Y + rect->Height;
2235
2236 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2237
2238 min_point = max_point = points[0];
2239
2240 for (i=1; i<4; i++)
2241 {
2242 if (points[i].X < min_point.X) min_point.X = points[i].X;
2243 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2244 if (points[i].X > max_point.X) max_point.X = points[i].X;
2245 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2246 }
2247
2248 rect->X = min_point.X;
2249 rect->Y = min_point.Y;
2250 rect->Width = max_point.X - min_point.X;
2251 rect->Height = max_point.Y - min_point.Y;
2252 }
2253
2254 return stat;
2255 }
2256
2257 /* on success, rgn will contain the region of the graphics object which
2258 * is visible after clipping has been applied */
2259 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2260 {
2261 GpStatus stat;
2262 GpRectF rectf;
2263 GpRegion* tmp;
2264
2265 /* Ignore graphics image bounds for metafiles */
2266 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2267 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2268
2269 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2270 return stat;
2271
2272 if((stat = GdipCreateRegion(&tmp)) != Ok)
2273 return stat;
2274
2275 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2276 goto end;
2277
2278 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2279 goto end;
2280
2281 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2282
2283 end:
2284 GdipDeleteRegion(tmp);
2285 return stat;
2286 }
2287
2288 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2289 {
2290 REAL height;
2291
2292 if (font->unit == UnitPixel)
2293 {
2294 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2295 }
2296 else
2297 {
2298 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2299 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2300 else
2301 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2302 }
2303
2304 lf->lfHeight = -(height + 0.5);
2305 lf->lfWidth = 0;
2306 lf->lfEscapement = 0;
2307 lf->lfOrientation = 0;
2308 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2309 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2310 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2311 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2312 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2313 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2314 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2315 lf->lfQuality = DEFAULT_QUALITY;
2316 lf->lfPitchAndFamily = 0;
2317 strcpyW(lf->lfFaceName, font->family->FamilyName);
2318 }
2319
2320 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2321 GDIPCONST GpStringFormat *format, HFONT *hfont,
2322 GDIPCONST GpMatrix *matrix)
2323 {
2324 HDC hdc = CreateCompatibleDC(0);
2325 GpPointF pt[3];
2326 REAL angle, rel_width, rel_height, font_height;
2327 LOGFONTW lfw;
2328 HFONT unscaled_font;
2329 TEXTMETRICW textmet;
2330
2331 if (font->unit == UnitPixel || font->unit == UnitWorld)
2332 font_height = font->emSize;
2333 else
2334 {
2335 REAL unit_scale, res;
2336
2337 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2338 unit_scale = units_scale(font->unit, graphics->unit, res);
2339
2340 font_height = font->emSize * unit_scale;
2341 }
2342
2343 pt[0].X = 0.0;
2344 pt[0].Y = 0.0;
2345 pt[1].X = 1.0;
2346 pt[1].Y = 0.0;
2347 pt[2].X = 0.0;
2348 pt[2].Y = 1.0;
2349 if (matrix)
2350 {
2351 GpMatrix xform = *matrix;
2352 GdipTransformMatrixPoints(&xform, pt, 3);
2353 }
2354
2355 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2356 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2357 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2358 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2359 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2360 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2361
2362 get_log_fontW(font, graphics, &lfw);
2363 lfw.lfHeight = -gdip_round(font_height * rel_height);
2364 unscaled_font = CreateFontIndirectW(&lfw);
2365
2366 SelectObject(hdc, unscaled_font);
2367 GetTextMetricsW(hdc, &textmet);
2368
2369 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2370 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2371
2372 *hfont = CreateFontIndirectW(&lfw);
2373
2374 DeleteDC(hdc);
2375 DeleteObject(unscaled_font);
2376 }
2377
2378 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2379 {
2380 TRACE("(%p, %p)\n", hdc, graphics);
2381
2382 return GdipCreateFromHDC2(hdc, NULL, graphics);
2383 }
2384
2385 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2386 {
2387 XFORM xform;
2388
2389 if (graphics->hdc == NULL)
2390 {
2391 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2392 return;
2393 }
2394
2395 GetTransform(graphics->hdc, 0x204, &xform);
2396 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2397 }
2398
2399 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2400 {
2401 GpStatus retval;
2402 HBITMAP hbitmap;
2403 DIBSECTION dib;
2404
2405 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2406
2407 if(hDevice != NULL)
2408 FIXME("Don't know how to handle parameter hDevice\n");
2409
2410 if(hdc == NULL)
2411 return OutOfMemory;
2412
2413 if(graphics == NULL)
2414 return InvalidParameter;
2415
2416 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2417 if(!*graphics) return OutOfMemory;
2418
2419 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2420
2421 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2422 heap_free(*graphics);
2423 return retval;
2424 }
2425
2426 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2427 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2428 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2429 {
2430 (*graphics)->alpha_hdc = 1;
2431 }
2432
2433 (*graphics)->hdc = hdc;
2434 (*graphics)->hwnd = WindowFromDC(hdc);
2435 (*graphics)->owndc = FALSE;
2436 (*graphics)->smoothing = SmoothingModeDefault;
2437 (*graphics)->compqual = CompositingQualityDefault;
2438 (*graphics)->interpolation = InterpolationModeBilinear;
2439 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2440 (*graphics)->compmode = CompositingModeSourceOver;
2441 (*graphics)->unit = UnitDisplay;
2442 (*graphics)->scale = 1.0;
2443 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2444 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2445 (*graphics)->busy = FALSE;
2446 (*graphics)->textcontrast = 4;
2447 list_init(&(*graphics)->containers);
2448 #ifdef __REACTOS__
2449 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2450 #else
2451 (*graphics)->contid = 0;
2452 #endif
2453 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2454
2455 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2456 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2457 {
2458 DeleteObject((*graphics)->gdi_clip);
2459 (*graphics)->gdi_clip = NULL;
2460 }
2461
2462 TRACE("<-- %p\n", *graphics);
2463
2464 return Ok;
2465 }
2466
2467 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2468 {
2469 GpStatus retval;
2470
2471 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2472 if(!*graphics) return OutOfMemory;
2473
2474 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2475 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2476
2477 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2478 heap_free(*graphics);
2479 return retval;
2480 }
2481
2482 (*graphics)->hdc = NULL;
2483 (*graphics)->hwnd = NULL;
2484 (*graphics)->owndc = FALSE;
2485 (*graphics)->image = image;
2486 /* We have to store the image type here because the image may be freed
2487 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2488 (*graphics)->image_type = image->type;
2489 (*graphics)->smoothing = SmoothingModeDefault;
2490 (*graphics)->compqual = CompositingQualityDefault;
2491 (*graphics)->interpolation = InterpolationModeBilinear;
2492 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2493 (*graphics)->compmode = CompositingModeSourceOver;
2494 (*graphics)->unit = UnitDisplay;
2495 (*graphics)->scale = 1.0;
2496 (*graphics)->xres = image->xres;
2497 (*graphics)->yres = image->yres;
2498 (*graphics)->busy = FALSE;
2499 (*graphics)->textcontrast = 4;
2500 list_init(&(*graphics)->containers);
2501 #ifdef __REACTOS__
2502 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2503 #else
2504 (*graphics)->contid = 0;
2505 #endif
2506
2507 TRACE("<-- %p\n", *graphics);
2508
2509 return Ok;
2510 }
2511
2512 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2513 {
2514 GpStatus ret;
2515 HDC hdc;
2516
2517 TRACE("(%p, %p)\n", hwnd, graphics);
2518
2519 hdc = GetDC(hwnd);
2520
2521 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2522 {
2523 ReleaseDC(hwnd, hdc);
2524 return ret;
2525 }
2526
2527 (*graphics)->hwnd = hwnd;
2528 (*graphics)->owndc = TRUE;
2529
2530 return Ok;
2531 }
2532
2533 /* FIXME: no icm handling */
2534 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2535 {
2536 TRACE("(%p, %p)\n", hwnd, graphics);
2537
2538 return GdipCreateFromHWND(hwnd, graphics);
2539 }
2540
2541 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2542 UINT access, IStream **stream)
2543 {
2544 DWORD dwMode;
2545 HRESULT ret;
2546
2547 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2548
2549 if(!stream || !filename)
2550 return InvalidParameter;
2551
2552 if(access & GENERIC_WRITE)
2553 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2554 else if(access & GENERIC_READ)
2555 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2556 else
2557 return InvalidParameter;
2558
2559 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2560
2561 return hresult_to_status(ret);
2562 }
2563
2564 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2565 {
2566 GraphicsContainerItem *cont, *next;
2567 GpStatus stat;
2568 TRACE("(%p)\n", graphics);
2569
2570 if(!graphics) return InvalidParameter;
2571 if(graphics->busy) return ObjectBusy;
2572
2573 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2574 {
2575 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2576 if (stat != Ok)
2577 return stat;
2578 }
2579
2580 if(graphics->owndc)
2581 ReleaseDC(graphics->hwnd, graphics->hdc);
2582
2583 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2584 list_remove(&cont->entry);
2585 delete_container(cont);
2586 }
2587
2588 GdipDeleteRegion(graphics->clip);
2589
2590 DeleteObject(graphics->gdi_clip);
2591
2592 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2593 * do otherwise, but we can't have that in the test suite because it means
2594 * accessing freed memory. */
2595 graphics->busy = TRUE;
2596
2597 heap_free(graphics);
2598
2599 return Ok;
2600 }
2601
2602 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2603 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2604 {
2605 GpStatus status;
2606 GpPath *path;
2607
2608 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2609 width, height, startAngle, sweepAngle);
2610
2611 if(!graphics || !pen || width <= 0 || height <= 0)
2612 return InvalidParameter;
2613
2614 if(graphics->busy)
2615 return ObjectBusy;
2616
2617 status = GdipCreatePath(FillModeAlternate, &path);
2618 if (status != Ok) return status;
2619
2620 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2621 if (status == Ok)
2622 status = GdipDrawPath(graphics, pen, path);
2623
2624 GdipDeletePath(path);
2625 return status;
2626 }
2627
2628 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2629 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2630 {
2631 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2632 width, height, startAngle, sweepAngle);
2633
2634 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2635 }
2636
2637 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2638 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2639 {
2640 GpPointF pt[4];
2641
2642 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2643 x2, y2, x3, y3, x4, y4);
2644
2645 if(!graphics || !pen)
2646 return InvalidParameter;
2647
2648 if(graphics->busy)
2649 return ObjectBusy;
2650
2651 pt[0].X = x1;
2652 pt[0].Y = y1;
2653 pt[1].X = x2;
2654 pt[1].Y = y2;
2655 pt[2].X = x3;
2656 pt[2].Y = y3;
2657 pt[3].X = x4;
2658 pt[3].Y = y4;
2659 return GdipDrawBeziers(graphics, pen, pt, 4);
2660 }
2661
2662 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2663 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2664 {
2665 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2666 x2, y2, x3, y3, x4, y4);
2667
2668 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2669 }
2670
2671 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2672 GDIPCONST GpPointF *points, INT count)
2673 {
2674 GpStatus status;
2675 GpPath *path;
2676
2677 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2678
2679 if(!graphics || !pen || !points || (count <= 0))
2680 return InvalidParameter;
2681
2682 if(graphics->busy)
2683 return ObjectBusy;
2684
2685 status = GdipCreatePath(FillModeAlternate, &path);
2686 if (status != Ok) return status;
2687
2688 status = GdipAddPathBeziers(path, points, count);
2689 if (status == Ok)
2690 status = GdipDrawPath(graphics, pen, path);
2691
2692 GdipDeletePath(path);
2693 return status;
2694 }
2695
2696 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2697 GDIPCONST GpPoint *points, INT count)
2698 {
2699 GpPointF *pts;
2700 GpStatus ret;
2701 INT i;
2702
2703 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2704
2705 if(!graphics || !pen || !points || (count <= 0))
2706 return InvalidParameter;
2707
2708 if(graphics->busy)
2709 return ObjectBusy;
2710
2711 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2712 if(!pts)
2713 return OutOfMemory;
2714
2715 for(i = 0; i < count; i++){
2716 pts[i].X = (REAL)points[i].X;
2717 pts[i].Y = (REAL)points[i].Y;
2718 }
2719
2720 ret = GdipDrawBeziers(graphics,pen,pts,count);
2721
2722 heap_free(pts);
2723
2724 return ret;
2725 }
2726
2727 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2728 GDIPCONST GpPointF *points, INT count)
2729 {
2730 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2731
2732 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2733 }
2734
2735 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2736 GDIPCONST GpPoint *points, INT count)
2737 {
2738 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2739
2740 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2741 }
2742
2743 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2744 GDIPCONST GpPointF *points, INT count, REAL tension)
2745 {
2746 GpPath *path;
2747 GpStatus status;
2748
2749 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2750
2751 if(!graphics || !pen || !points || count <= 0)
2752 return InvalidParameter;
2753
2754 if(graphics->busy)
2755 return ObjectBusy;
2756
2757 status = GdipCreatePath(FillModeAlternate, &path);
2758 if (status != Ok) return status;
2759
2760 status = GdipAddPathClosedCurve2(path, points, count, tension);
2761 if (status == Ok)
2762 status = GdipDrawPath(graphics, pen, path);
2763
2764 GdipDeletePath(path);
2765
2766 return status;
2767 }
2768
2769 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2770 GDIPCONST GpPoint *points, INT count, REAL tension)
2771 {
2772 GpPointF *ptf;
2773 GpStatus stat;
2774 INT i;
2775
2776 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2777
2778 if(!points || count <= 0)
2779 return InvalidParameter;
2780
2781 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2782 if(!ptf)
2783 return OutOfMemory;
2784
2785 for(i = 0; i < count; i++){
2786 ptf[i].X = (REAL)points[i].X;
2787 ptf[i].Y = (REAL)points[i].Y;
2788 }
2789
2790 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2791
2792 heap_free(ptf);
2793
2794 return stat;
2795 }
2796
2797 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2798 GDIPCONST GpPointF *points, INT count)
2799 {
2800 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2801
2802 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2803 }
2804
2805 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2806 GDIPCONST GpPoint *points, INT count)
2807 {
2808 GpPointF *pointsF;
2809 GpStatus ret;
2810 INT i;
2811
2812 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2813
2814 if(!points)
2815 return InvalidParameter;
2816
2817 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2818 if(!pointsF)
2819 return OutOfMemory;
2820
2821 for(i = 0; i < count; i++){
2822 pointsF[i].X = (REAL)points[i].X;
2823 pointsF[i].Y = (REAL)points[i].Y;
2824 }
2825
2826 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2827 heap_free(pointsF);
2828
2829 return ret;
2830 }
2831
2832 /* Approximates cardinal spline with Bezier curves. */
2833 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2834 GDIPCONST GpPointF *points, INT count, REAL tension)
2835 {
2836 GpPath *path;
2837 GpStatus status;
2838
2839 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2840
2841 if(!graphics || !pen)
2842 return InvalidParameter;
2843
2844 if(graphics->busy)
2845 return ObjectBusy;
2846
2847 if(count < 2)
2848 return InvalidParameter;
2849
2850 status = GdipCreatePath(FillModeAlternate, &path);
2851 if (status != Ok) return status;
2852
2853 status = GdipAddPathCurve2(path, points, count, tension);
2854 if (status == Ok)
2855 status = GdipDrawPath(graphics, pen, path);
2856
2857 GdipDeletePath(path);
2858 return status;
2859 }
2860
2861 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2862 GDIPCONST GpPoint *points, INT count, REAL tension)
2863 {
2864 GpPointF *pointsF;
2865 GpStatus ret;
2866 INT i;
2867
2868 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2869
2870 if(!points)
2871 return InvalidParameter;
2872
2873 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2874 if(!pointsF)
2875 return OutOfMemory;
2876
2877 for(i = 0; i < count; i++){
2878 pointsF[i].X = (REAL)points[i].X;
2879 pointsF[i].Y = (REAL)points[i].Y;
2880 }
2881
2882 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2883 heap_free(pointsF);
2884
2885 return ret;
2886 }
2887
2888 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2889 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2890 REAL tension)
2891 {
2892 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2893
2894 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2895 return InvalidParameter;
2896 }
2897
2898 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2899 }
2900
2901 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2902 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2903 REAL tension)
2904 {
2905 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2906
2907 if(count < 0){
2908 return OutOfMemory;
2909 }
2910
2911 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2912 return InvalidParameter;
2913 }
2914
2915 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2916 }
2917
2918 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2919 REAL y, REAL width, REAL height)
2920 {
2921 GpPath *path;
2922 GpStatus status;
2923
2924 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2925
2926 if(!graphics || !pen)
2927 return InvalidParameter;
2928
2929 if(graphics->busy)
2930 return ObjectBusy;
2931
2932 status = GdipCreatePath(FillModeAlternate, &path);
2933 if (status != Ok) return status;
2934
2935 status = GdipAddPathEllipse(path, x, y, width, height);
2936 if (status == Ok)
2937 status = GdipDrawPath(graphics, pen, path);
2938
2939 GdipDeletePath(path);
2940 return status;
2941 }
2942
2943 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2944 INT y, INT width, INT height)
2945 {
2946 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2947
2948 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2949 }
2950
2951
2952 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2953 {
2954 UINT width, height;
2955
2956 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2957
2958 if(!graphics || !image)
2959 return InvalidParameter;
2960
2961 GdipGetImageWidth(image, &width);
2962 GdipGetImageHeight(image, &height);
2963
2964 return GdipDrawImagePointRect(graphics, image, x, y,
2965 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2966 }
2967
2968 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2969 INT y)
2970 {
2971 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2972
2973 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2974 }
2975
2976 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2977 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2978 GpUnit srcUnit)
2979 {
2980 GpPointF points[3];
2981 REAL scale_x, scale_y, width, height;
2982
2983 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2984
2985 if (!graphics || !image) return InvalidParameter;
2986
2987 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2988 scale_x *= graphics->xres / image->xres;
2989 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2990 scale_y *= graphics->yres / image->yres;
2991 width = srcwidth * scale_x;
2992 height = srcheight * scale_y;
2993
2994 points[0].X = points[2].X = x;
2995 points[0].Y = points[1].Y = y;
2996 points[1].X = x + width;
2997 points[2].Y = y + height;
2998
2999 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3000 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3001 }
3002
3003 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3004 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3005 GpUnit srcUnit)
3006 {
3007 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3008 }
3009
3010 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3011 GDIPCONST GpPointF *dstpoints, INT count)
3012 {
3013 UINT width, height;
3014
3015 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3016
3017 if(!image)
3018 return InvalidParameter;
3019
3020 GdipGetImageWidth(image, &width);
3021 GdipGetImageHeight(image, &height);
3022
3023 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3024 width, height, UnitPixel, NULL, NULL, NULL);
3025 }
3026
3027 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3028 GDIPCONST GpPoint *dstpoints, INT count)
3029 {
3030 GpPointF ptf[3];
3031
3032 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3033
3034 if (count != 3 || !dstpoints)
3035 return InvalidParameter;
3036
3037 ptf[0].X = (REAL)dstpoints[0].X;
3038 ptf[0].Y = (REAL)dstpoints[0].Y;
3039 ptf[1].X = (REAL)dstpoints[1].X;
3040 ptf[1].Y = (REAL)dstpoints[1].Y;
3041 ptf[2].X = (REAL)dstpoints[2].X;
3042 ptf[2].Y = (REAL)dstpoints[2].Y;
3043
3044 return GdipDrawImagePoints(graphics, image, ptf, count);
3045 }
3046
3047 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3048 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3049 {
3050 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3051 return TRUE;
3052 }
3053
3054 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3055 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3056 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3057 DrawImageAbort callback, VOID * callbackData)
3058 {
3059 GpPointF ptf[4];
3060 POINT pti[4];
3061 GpStatus stat;
3062
3063 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3064 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3065 callbackData);
3066
3067 if (count > 3)
3068 return NotImplemented;
3069
3070 if(!graphics || !image || !points || count != 3)
3071 return InvalidParameter;
3072
3073 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3074 debugstr_pointf(&points[2]));
3075
3076 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3077 {
3078 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3079 image, points, count, srcx, srcy, srcwidth, srcheight,
3080 srcUnit, imageAttributes, callback, callbackData);
3081 }
3082
3083 memcpy(ptf, points, 3 * sizeof(GpPointF));
3084
3085 /* Ensure source width/height is positive */
3086 if (srcwidth < 0)
3087 {
3088 GpPointF tmp = ptf[1];
3089 srcx = srcx + srcwidth;
3090 srcwidth = -srcwidth;
3091 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3092 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3093 ptf[1] = ptf[0];
3094 ptf[0] = tmp;
3095 }
3096
3097 if (srcheight < 0)
3098 {
3099 GpPointF tmp = ptf[2];
3100 srcy = srcy + srcheight;
3101 srcheight = -srcheight;
3102 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3103 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3104 ptf[2] = ptf[0];
3105 ptf[0] = tmp;
3106 }
3107
3108 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3109 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3110 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3111 return Ok;
3112 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3113 round_points(pti, ptf, 4);
3114
3115 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3116 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3117
3118 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3119 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3120 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3121 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3122 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3123
3124 if (image->type == ImageTypeBitmap)
3125 {
3126 GpBitmap* bitmap = (GpBitmap*)image;
3127 BOOL do_resampling = FALSE;
3128 BOOL use_software = FALSE;
3129
3130 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3131 graphics->xres, graphics->yres,
3132 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3133 graphics->scale, image->xres, image->yres, bitmap->format,
3134 imageAttributes ? imageAttributes->outside_color : 0);
3135
3136 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3137 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3138 srcx < 0 || srcy < 0 ||
3139 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3140 do_resampling = TRUE;
3141
3142 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3143 (graphics->image && graphics->image->type == ImageTypeBitmap))
3144 use_software = TRUE;
3145
3146 if (use_software)
3147 {
3148 RECT dst_area;
3149 GpRectF graphics_bounds;
3150 GpRect src_area;
3151 int i, x, y, src_stride, dst_stride;
3152 GpMatrix dst_to_src;
3153 REAL m11, m12, m21, m22, mdx, mdy;
3154 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3155 BitmapData lockeddata;
3156 InterpolationMode interpolation = graphics->interpolation;
3157 PixelOffsetMode offset_mode = graphics->pixeloffset;
3158 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3159 REAL x_dx, x_dy, y_dx, y_dy;
3160 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3161
3162 if (!imageAttributes)
3163 imageAttributes = &defaultImageAttributes;
3164
3165 dst_area.left = dst_area.right = pti[0].x;
3166 dst_area.top = dst_area.bottom = pti[0].y;
3167 for (i=1; i<4; i++)
3168 {
3169 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3170 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3171 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3172 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3173 }
3174
3175 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3176 if (stat != Ok) return stat;
3177
3178 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3179 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3180 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3181 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3182
3183 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3184
3185 if (IsRectEmpty(&dst_area)) return Ok;
3186
3187 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3188 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3189 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3190 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3191 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3192 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3193
3194 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3195
3196 stat = GdipInvertMatrix(&dst_to_src);
3197 if (stat != Ok) return stat;
3198
3199 if (do_resampling)
3200 {
3201 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3202 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3203 }
3204 else
3205 {
3206 /* Make sure src_area is equal in size to dst_area. */
3207 src_area.X = srcx + dst_area.left - pti[0].x;
3208 src_area.Y = srcy + dst_area.top - pti[0].y;
3209 src_area.Width = dst_area.right - dst_area.left;
3210 src_area.Height = dst_area.bottom - dst_area.top;
3211 }
3212
3213 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3214
3215 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3216 if (!src_data)
3217 return OutOfMemory;
3218 src_stride = sizeof(ARGB) * src_area.Width;
3219
3220 /* Read the bits we need from the source bitmap into a compatible buffer. */
3221 lockeddata.Width = src_area.Width;
3222 lockeddata.Height = src_area.Height;
3223 lockeddata.Stride = src_stride;
3224 lockeddata.Scan0 = src_data;
3225 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3226 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3227 else if (imageAttributes != &defaultImageAttributes)
3228 lockeddata.PixelFormat = PixelFormat32bppARGB;
3229 else
3230 lockeddata.PixelFormat = PixelFormat32bppPARGB;
3231
3232 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3233 lockeddata.PixelFormat, &lockeddata);
3234
3235 if (stat == Ok)
3236 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3237
3238 if (stat != Ok)
3239 {
3240 heap_free(src_data);
3241 return stat;
3242 }
3243
3244 apply_image_attributes(imageAttributes, src_data,
3245 src_area.Width, src_area.Height,
3246 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3247
3248 if (do_resampling)
3249 {
3250 REAL delta_xx, delta_xy, delta_yx, delta_yy;
3251
3252 /* Transform the bits as needed to the destination. */
3253 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3254 if (!dst_data)
3255 {
3256 heap_free(src_data);
3257 return OutOfMemory;
3258 }
3259
3260 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3261
3262 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3263
3264 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3265 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3266 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3267 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3268
3269 delta_yy = dst_area.top * y_dy;
3270 delta_yx = dst_area.top * y_dx;
3271
3272 for (y=dst_area.top; y<dst_area.bottom; y++)
3273 {
3274 delta_xx = dst_area.left * x_dx;
3275 delta_xy = dst_area.left * x_dy;
3276
3277 for (x=dst_area.left; x<dst_area.right; x++)
3278 {
3279 GpPointF src_pointf;
3280 ARGB *dst_color;
3281
3282 src_pointf.X = dst_to_src_points[0].X + delta_xx + delta_yx;
3283 src_pointf.Y = dst_to_src_points[0].Y + delta_xy + delta_yy;
3284
3285 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3286
3287 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3288 {
3289 if (lockeddata.PixelFormat != PixelFormat32bppPARGB)
3290 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3291 imageAttributes, interpolation, offset_mode);
3292 else
3293 *dst_color = resample_bitmap_pixel_premult(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3294 imageAttributes, interpolation, offset_mode);
3295 }
3296 else
3297 *dst_color = 0;
3298
3299 delta_xx += x_dx;
3300 delta_yx += y_dx;
3301 }
3302
3303 delta_xy += x_dy;
3304 delta_yy += y_dy;
3305 }
3306 }
3307 else
3308 {
3309 dst_data = src_data;
3310 dst_stride = src_stride;
3311 }
3312
3313 gdi_transform_acquire(graphics);
3314
3315 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3316 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3317 lockeddata.PixelFormat);
3318
3319 gdi_transform_release(graphics);
3320
3321 heap_free(src_data);
3322
3323 heap_free(dst_dyn_data);
3324
3325 return stat;
3326 }
3327 else
3328 {
3329 HDC hdc;
3330 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3331 HBITMAP hbitmap, old_hbm=NULL;
3332 HRGN hrgn;
3333 INT save_state;
3334
3335 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3336 bitmap->format == PixelFormat24bppRGB ||
3337 bitmap->format == PixelFormat32bppRGB ||
3338 bitmap->format == PixelFormat32bppPARGB))
3339 {
3340 BITMAPINFOHEADER bih;
3341 BYTE *temp_bits;
3342 PixelFormat dst_format;
3343
3344 /* we can't draw a bitmap of this format directly */
3345 hdc = CreateCompatibleDC(0);
3346 temp_hdc = TRUE;
3347 temp_bitmap = TRUE;
3348
3349 bih.biSize = sizeof(BITMAPINFOHEADER);
3350 bih.biWidth = bitmap->width;
3351 bih.biHeight = -bitmap->height;
3352 bih.biPlanes = 1;
3353 bih.biBitCount = 32;
3354 bih.biCompression = BI_RGB;
3355 bih.biSizeImage = 0;
3356 bih.biXPelsPerMeter = 0;
3357 bih.biYPelsPerMeter = 0;
3358 bih.biClrUsed = 0;
3359 bih.biClrImportant = 0;
3360
3361 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3362 (void**)&temp_bits, NULL, 0);
3363
3364 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3365 dst_format = PixelFormat32bppPARGB;
3366 else
3367 dst_format = PixelFormat32bppRGB;
3368
3369 convert_pixels(bitmap->width, bitmap->height,
3370 bitmap->width*4, temp_bits, dst_format,
3371 bitmap->stride, bitmap->bits, bitmap->format,
3372 bitmap->image.palette);
3373 }
3374 else
3375 {
3376 if (bitmap->hbitmap)
3377 hbitmap = bitmap->hbitmap;
3378 else
3379 {
3380 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3381 temp_bitmap = TRUE;
3382 }
3383
3384 hdc = bitmap->hdc;
3385 temp_hdc = (hdc == 0);
3386 }
3387
3388 if (temp_hdc)
3389 {
3390 if (!hdc) hdc = CreateCompatibleDC(0);
3391 old_hbm = SelectObject(hdc, hbitmap);
3392 }
3393
3394 save_state = SaveDC(graphics->hdc);
3395
3396 stat = get_clip_hrgn(graphics, &hrgn);
3397
3398 if (stat == Ok)
3399 {
3400 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3401 DeleteObject(hrgn);
3402 }
3403
3404 gdi_transform_acquire(graphics);
3405
3406 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3407 {
3408 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3409 hdc, srcx, srcy, srcwidth, srcheight);
3410 }
3411 else
3412 {
3413 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3414 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3415 }
3416
3417 gdi_transform_release(graphics);
3418
3419 RestoreDC(graphics->hdc, save_state);
3420
3421 if (temp_hdc)
3422 {
3423 SelectObject(hdc, old_hbm);
3424 DeleteDC(hdc);
3425 }
3426
3427 if (temp_bitmap)
3428 DeleteObject(hbitmap);
3429 }
3430 }
3431 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3432 {
3433 GpRectF rc;
3434
3435 rc.X = srcx;
3436 rc.Y = srcy;
3437 rc.Width = srcwidth;
3438 rc.Height = srcheight;
3439
3440 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3441 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3442 }
3443 else
3444 {
3445 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3446 return InvalidParameter;
3447 }
3448
3449 return Ok;
3450 }
3451
3452 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3453 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3454 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3455 DrawImageAbort callback, VOID * callbackData)
3456 {
3457 GpPointF pointsF[3];
3458 INT i;
3459
3460 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3461 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3462 callbackData);
3463
3464 if(!points || count!=3)
3465 return InvalidParameter;
3466
3467 for(i = 0; i < count; i++){
3468 pointsF[i].X = (REAL)points[i].X;
3469 pointsF[i].Y = (REAL)points[i].Y;
3470 }
3471
3472 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3473 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3474 callback, callbackData);
3475 }
3476
3477 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3478 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3479 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3480 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3481 VOID * callbackData)
3482 {
3483 GpPointF points[3];
3484
3485 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3486 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3487 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3488
3489 points[0].X = dstx;
3490 points[0].Y = dsty;