gdi32: Use ntgdi name for GetPixel.
[wine.git] / dlls / gdi32 / painting.c
blobc879aeb95824c91922d5a54173d9eeade4aaf02c
1 /*
2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
6 * 1999 Huw D M Davies
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winerror.h"
31 #include "ntgdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
37 /***********************************************************************
38 * null driver fallback implementations
41 BOOL CDECL nulldrv_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
43 INT x1 = GDI_ROUND( x + cos( start * M_PI / 180 ) * radius );
44 INT y1 = GDI_ROUND( y - sin( start * M_PI / 180 ) * radius );
45 INT x2 = GDI_ROUND( x + cos( (start + sweep) * M_PI / 180) * radius );
46 INT y2 = GDI_ROUND( y - sin( (start + sweep) * M_PI / 180) * radius );
47 INT arcdir = SetArcDirection( dev->hdc, sweep >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE );
48 BOOL ret = ArcTo( dev->hdc, x - radius, y - radius, x + radius, y + radius, x1, y1, x2, y2 );
49 SetArcDirection( dev->hdc, arcdir );
50 return ret;
53 BOOL CDECL nulldrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
54 INT xstart, INT ystart, INT xend, INT yend )
56 INT width = abs( right - left );
57 INT height = abs( bottom - top );
58 double xradius = width / 2.0;
59 double yradius = height / 2.0;
60 double xcenter = right > left ? left + xradius : right + xradius;
61 double ycenter = bottom > top ? top + yradius : bottom + yradius;
62 double angle;
64 if (!height || !width) return FALSE;
65 /* draw a line from the current position to the starting point of the arc, then draw the arc */
66 angle = atan2( (ystart - ycenter) / height, (xstart - xcenter) / width );
67 LineTo( dev->hdc, GDI_ROUND( xcenter + cos(angle) * xradius ),
68 GDI_ROUND( ycenter + sin(angle) * yradius ));
69 return Arc( dev->hdc, left, top, right, bottom, xstart, ystart, xend, yend );
72 BOOL CDECL nulldrv_FillRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush )
74 BOOL ret = FALSE;
75 HBRUSH prev;
77 if ((prev = NtGdiSelectBrush( dev->hdc, brush )))
79 ret = PaintRgn( dev->hdc, rgn );
80 NtGdiSelectBrush( dev->hdc, prev );
82 return ret;
85 BOOL CDECL nulldrv_FrameRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush, INT width, INT height )
87 BOOL ret = FALSE;
88 HRGN tmp = NtGdiCreateRectRgn( 0, 0, 0, 0 );
90 if (tmp)
92 if (REGION_FrameRgn( tmp, rgn, width, height )) ret = FillRgn( dev->hdc, tmp, brush );
93 DeleteObject( tmp );
95 return ret;
98 BOOL CDECL nulldrv_InvertRgn( PHYSDEV dev, HRGN rgn )
100 HBRUSH prev_brush = NtGdiSelectBrush( dev->hdc, GetStockObject(BLACK_BRUSH) );
101 INT prev_rop = SetROP2( dev->hdc, R2_NOT );
102 BOOL ret = PaintRgn( dev->hdc, rgn );
103 NtGdiSelectBrush( dev->hdc, prev_brush );
104 SetROP2( dev->hdc, prev_rop );
105 return ret;
108 static BOOL polyline( HDC hdc, const POINT *points, UINT count )
110 return NtGdiPolyPolyDraw( hdc, points, &count, 1, NtGdiPolyPolyline );
113 BOOL CDECL nulldrv_PolyBezier( PHYSDEV dev, const POINT *points, DWORD count )
115 BOOL ret = FALSE;
116 POINT *pts;
117 INT n;
119 if ((pts = GDI_Bezier( points, count, &n )))
121 ret = polyline( dev->hdc, pts, n );
122 HeapFree( GetProcessHeap(), 0, pts );
124 return ret;
127 BOOL CDECL nulldrv_PolyBezierTo( PHYSDEV dev, const POINT *points, DWORD count )
129 DC *dc = get_nulldrv_dc( dev );
130 BOOL ret = FALSE;
131 POINT *pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) );
133 if (pts)
135 pts[0] = dc->attr->cur_pos;
136 memcpy( pts + 1, points, sizeof(POINT) * count );
137 count++;
138 ret = NtGdiPolyPolyDraw( dev->hdc, pts, &count, 1, NtGdiPolyBezier );
139 HeapFree( GetProcessHeap(), 0, pts );
141 return ret;
144 BOOL CDECL nulldrv_PolyDraw( PHYSDEV dev, const POINT *points, const BYTE *types, DWORD count )
146 DC *dc = get_nulldrv_dc( dev );
147 POINT *line_pts = NULL, *bzr_pts = NULL, bzr[4];
148 DWORD i;
149 INT num_pts, num_bzr_pts, space, size;
151 /* check for valid point types */
152 for (i = 0; i < count; i++)
154 switch (types[i])
156 case PT_MOVETO:
157 case PT_LINETO | PT_CLOSEFIGURE:
158 case PT_LINETO:
159 break;
160 case PT_BEZIERTO:
161 if (i + 2 >= count) return FALSE;
162 if (types[i + 1] != PT_BEZIERTO) return FALSE;
163 if ((types[i + 2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) return FALSE;
164 i += 2;
165 break;
166 default:
167 return FALSE;
171 space = count + 300;
172 line_pts = HeapAlloc( GetProcessHeap(), 0, space * sizeof(POINT) );
173 num_pts = 1;
175 line_pts[0] = dc->attr->cur_pos;
176 for (i = 0; i < count; i++)
178 switch (types[i])
180 case PT_MOVETO:
181 if (num_pts >= 2) polyline( dev->hdc, line_pts, num_pts );
182 num_pts = 0;
183 line_pts[num_pts++] = points[i];
184 break;
185 case PT_LINETO:
186 case (PT_LINETO | PT_CLOSEFIGURE):
187 line_pts[num_pts++] = points[i];
188 break;
189 case PT_BEZIERTO:
190 bzr[0].x = line_pts[num_pts - 1].x;
191 bzr[0].y = line_pts[num_pts - 1].y;
192 memcpy( &bzr[1], &points[i], 3 * sizeof(POINT) );
194 if ((bzr_pts = GDI_Bezier( bzr, 4, &num_bzr_pts )))
196 size = num_pts + (count - i) + num_bzr_pts;
197 if (space < size)
199 space = size * 2;
200 line_pts = HeapReAlloc( GetProcessHeap(), 0, line_pts, space * sizeof(POINT) );
202 memcpy( &line_pts[num_pts], &bzr_pts[1], (num_bzr_pts - 1) * sizeof(POINT) );
203 num_pts += num_bzr_pts - 1;
204 HeapFree( GetProcessHeap(), 0, bzr_pts );
206 i += 2;
207 break;
209 if (types[i] & PT_CLOSEFIGURE) line_pts[num_pts++] = line_pts[0];
212 if (num_pts >= 2) polyline( dev->hdc, line_pts, num_pts );
213 HeapFree( GetProcessHeap(), 0, line_pts );
214 return TRUE;
217 BOOL CDECL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
219 DC *dc = get_nulldrv_dc( dev );
220 BOOL ret = FALSE;
221 POINT *pts;
223 if (!count) return FALSE;
224 if ((pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) )))
226 pts[0] = dc->attr->cur_pos;
227 memcpy( pts + 1, points, sizeof(POINT) * count );
228 ret = polyline( dev->hdc, pts, count + 1 );
229 HeapFree( GetProcessHeap(), 0, pts );
231 return ret;
234 /***********************************************************************
235 * NtGdiLineTo (win32u.@)
237 BOOL WINAPI NtGdiLineTo( HDC hdc, INT x, INT y )
239 DC * dc = get_dc_ptr( hdc );
240 PHYSDEV physdev;
241 BOOL ret;
243 if(!dc) return FALSE;
245 update_dc( dc );
246 physdev = GET_DC_PHYSDEV( dc, pLineTo );
247 ret = physdev->funcs->pLineTo( physdev, x, y );
249 if(ret)
251 dc->attr->cur_pos.x = x;
252 dc->attr->cur_pos.y = y;
254 release_dc_ptr( dc );
255 return ret;
259 /***********************************************************************
260 * NtGdiMoveTo (win32u.@)
262 BOOL WINAPI NtGdiMoveTo( HDC hdc, INT x, INT y, POINT *pt )
264 BOOL ret;
265 PHYSDEV physdev;
266 DC * dc = get_dc_ptr( hdc );
268 if(!dc) return FALSE;
270 if(pt)
271 *pt = dc->attr->cur_pos;
273 dc->attr->cur_pos.x = x;
274 dc->attr->cur_pos.y = y;
276 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
277 ret = physdev->funcs->pMoveTo( physdev, x, y );
278 release_dc_ptr( dc );
279 return ret;
283 /***********************************************************************
284 * NtGdiArcInternal (win32u.@)
286 BOOL WINAPI NtGdiArcInternal( UINT type, HDC hdc, INT left, INT top, INT right,
287 INT bottom, INT xstart, INT ystart, INT xend, INT yend )
289 PHYSDEV physdev;
290 BOOL ret;
291 DC *dc;
293 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
294 update_dc( dc );
296 switch (type)
298 case NtGdiArc:
299 physdev = GET_DC_PHYSDEV( dc, pArc );
300 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
301 break;
303 case NtGdiArcTo:
305 double width = abs( right - left );
306 double height = abs( bottom - top );
307 double xradius = width / 2;
308 double yradius = height / 2;
309 double xcenter = right > left ? left + xradius : right + xradius;
310 double ycenter = bottom > top ? top + yradius : bottom + yradius;
312 physdev = GET_DC_PHYSDEV( dc, pArcTo );
313 ret = physdev->funcs->pArcTo( physdev, left, top, right, bottom,
314 xstart, ystart, xend, yend );
315 if (ret)
317 double angle = atan2(((yend - ycenter) / height),
318 ((xend - xcenter) / width));
319 dc->attr->cur_pos.x = GDI_ROUND( xcenter + (cos( angle ) * xradius) );
320 dc->attr->cur_pos.y = GDI_ROUND( ycenter + (sin( angle ) * yradius) );
322 break;
325 case NtGdiChord:
326 physdev = GET_DC_PHYSDEV( dc, pChord );
327 ret = physdev->funcs->pChord( physdev, left, top, right, bottom,
328 xstart, ystart, xend, yend );
329 break;
331 case NtGdiPie:
332 physdev = GET_DC_PHYSDEV( dc, pPie );
333 ret = physdev->funcs->pPie( physdev, left, top, right, bottom,
334 xstart, ystart, xend, yend );
335 break;
337 default:
338 WARN( "invalid arc type %u\n", type );
339 ret = FALSE;
342 release_dc_ptr( dc );
343 return ret;
347 /***********************************************************************
348 * NtGdiEllipse (win32u.@)
350 BOOL WINAPI NtGdiEllipse( HDC hdc, INT left, INT top, INT right, INT bottom )
352 BOOL ret;
353 PHYSDEV physdev;
354 DC * dc = get_dc_ptr( hdc );
356 if (!dc) return FALSE;
357 update_dc( dc );
358 physdev = GET_DC_PHYSDEV( dc, pEllipse );
359 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
360 release_dc_ptr( dc );
361 return ret;
365 /***********************************************************************
366 * NtGdiRectangle (win32u.@)
368 BOOL WINAPI NtGdiRectangle( HDC hdc, INT left, INT top, INT right, INT bottom )
370 PHYSDEV physdev;
371 BOOL ret;
372 DC * dc = get_dc_ptr( hdc );
374 if (!dc) return FALSE;
375 update_dc( dc );
376 physdev = GET_DC_PHYSDEV( dc, pRectangle );
377 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
378 release_dc_ptr( dc );
379 return ret;
383 /***********************************************************************
384 * NtGdiRoundRect (win32u.@)
386 BOOL WINAPI NtGdiRoundRect( HDC hdc, INT left, INT top, INT right,
387 INT bottom, INT ell_width, INT ell_height )
389 PHYSDEV physdev;
390 BOOL ret;
391 DC *dc = get_dc_ptr( hdc );
393 if (!dc) return FALSE;
394 update_dc( dc );
395 physdev = GET_DC_PHYSDEV( dc, pRoundRect );
396 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
397 release_dc_ptr( dc );
398 return ret;
401 /***********************************************************************
402 * NtGdiSetPixel (win32u.@)
404 COLORREF WINAPI NtGdiSetPixel( HDC hdc, INT x, INT y, COLORREF color )
406 PHYSDEV physdev;
407 COLORREF ret;
408 DC * dc = get_dc_ptr( hdc );
410 if (!dc) return CLR_INVALID;
411 update_dc( dc );
412 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
413 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
414 release_dc_ptr( dc );
415 return ret;
418 /***********************************************************************
419 * NtGdiGetPixel (win32u.@)
421 COLORREF WINAPI NtGdiGetPixel( HDC hdc, INT x, INT y )
423 PHYSDEV physdev;
424 COLORREF ret;
425 DC * dc = get_dc_ptr( hdc );
427 if (!dc) return CLR_INVALID;
428 update_dc( dc );
429 physdev = GET_DC_PHYSDEV( dc, pGetPixel );
430 ret = physdev->funcs->pGetPixel( physdev, x, y );
431 release_dc_ptr( dc );
432 return ret;
436 /******************************************************************************
437 * GdiSetPixelFormat [GDI32.@]
439 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
441 BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
443 DC *dc;
444 BOOL ret = TRUE;
446 TRACE("(%p,%d,%p)\n", hdc, format, descr);
448 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
450 if (!dc->pixel_format) dc->pixel_format = format;
451 else ret = (dc->pixel_format == format);
452 release_dc_ptr( dc );
453 return ret;
457 /******************************************************************************
458 * GdiDescribePixelFormat [GDI32.@]
460 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
462 INT WINAPI GdiDescribePixelFormat( HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
464 FIXME( "(%p,%d,%d,%p): stub\n", hdc, format, size, descr );
465 return 0;
469 /******************************************************************************
470 * GdiSwapBuffers [GDI32.@]
472 * Probably not the correct semantics, it's supposed to be an internal backend for SwapBuffers.
474 BOOL WINAPI GdiSwapBuffers( HDC hdc )
476 FIXME( "(%p): stub\n", hdc );
477 return FALSE;
481 /***********************************************************************
482 * PaintRgn (GDI32.@)
484 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
486 PHYSDEV physdev;
487 BOOL ret;
488 DC * dc = get_dc_ptr( hdc );
490 TRACE( "%p, %p\n", hdc, hrgn );
492 if (!dc) return FALSE;
493 update_dc( dc );
494 physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
495 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
496 release_dc_ptr( dc );
497 return ret;
501 /***********************************************************************
502 * FillRgn (GDI32.@)
504 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
506 PHYSDEV physdev;
507 BOOL retval;
508 DC * dc = get_dc_ptr( hdc );
510 TRACE( "%p, %p, %p\n", hdc, hrgn, hbrush );
512 if (!dc) return FALSE;
513 update_dc( dc );
514 physdev = GET_DC_PHYSDEV( dc, pFillRgn );
515 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
516 release_dc_ptr( dc );
517 return retval;
521 /***********************************************************************
522 * FrameRgn (GDI32.@)
524 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
525 INT nWidth, INT nHeight )
527 PHYSDEV physdev;
528 BOOL ret;
529 DC *dc = get_dc_ptr( hdc );
531 TRACE( "%p, %p, %p, %dx%d\n", hdc, hrgn, hbrush, nWidth, nHeight );
533 if (!dc) return FALSE;
534 update_dc( dc );
535 physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
536 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
537 release_dc_ptr( dc );
538 return ret;
542 /***********************************************************************
543 * InvertRgn (GDI32.@)
545 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
547 PHYSDEV physdev;
548 BOOL ret;
549 DC *dc = get_dc_ptr( hdc );
551 TRACE( "%p, %p\n", hdc, hrgn );
553 if (!dc) return FALSE;
554 update_dc( dc );
555 physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
556 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
557 release_dc_ptr( dc );
558 return ret;
562 /**********************************************************************
563 * NtGdiPolyPolyDraw (win32u.@)
565 ULONG WINAPI NtGdiPolyPolyDraw( HDC hdc, const POINT *points, const UINT *counts,
566 UINT count, UINT function )
568 PHYSDEV physdev;
569 ULONG ret;
570 DC *dc;
572 if (function == NtGdiPolyPolygonRgn)
573 return HandleToULong( create_polypolygon_region( points, (const INT *)counts, count,
574 HandleToULong(hdc), NULL ));
576 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
577 update_dc( dc );
579 switch (function)
581 case NtGdiPolyPolygon:
582 physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
583 ret = physdev->funcs->pPolyPolygon( physdev, points, (const INT *)counts, count );
584 break;
586 case NtGdiPolyPolyline:
587 physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
588 ret = physdev->funcs->pPolyPolyline( physdev, points, counts, count );
589 break;
591 case NtGdiPolyBezier:
592 /* *counts must be 3 * n + 1 (where n >= 1) */
593 if (count == 1 && *counts != 1 && *counts % 3 == 1)
595 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
596 ret = physdev->funcs->pPolyBezier( physdev, points, *counts );
597 if (ret) dc->attr->cur_pos = points[*counts - 1];
599 else ret = FALSE;
600 break;
602 case NtGdiPolyBezierTo:
603 if (count == 1 && *counts && *counts % 3 == 0)
605 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
606 ret = physdev->funcs->pPolyBezierTo( physdev, points, *counts );
607 if (ret) dc->attr->cur_pos = points[*counts - 1];
609 else ret = FALSE;
610 break;
612 case NtGdiPolylineTo:
613 if (count == 1)
615 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
616 ret = physdev->funcs->pPolylineTo( physdev, points, *counts );
617 if (ret && *counts) dc->attr->cur_pos = points[*counts - 1];
619 else ret = FALSE;
620 break;
622 default:
623 WARN( "invalid function %u\n", function );
624 ret = FALSE;
625 break;
628 release_dc_ptr( dc );
629 return ret;
632 /**********************************************************************
633 * ExtFloodFill (GDI32.@)
635 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
636 UINT fillType )
638 PHYSDEV physdev;
639 BOOL ret;
640 DC * dc = get_dc_ptr( hdc );
642 TRACE( "%p, (%d, %d), %08x, %x\n", hdc, x, y, color, fillType );
644 if (!dc) return FALSE;
645 update_dc( dc );
646 physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
647 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
648 release_dc_ptr( dc );
649 return ret;
653 /**********************************************************************
654 * FloodFill (GDI32.@)
656 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
658 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
661 /***********************************************************************
662 * NtGdiAngleArc (win32u.@)
664 BOOL WINAPI NtGdiAngleArc( HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle )
666 PHYSDEV physdev;
667 BOOL result;
668 DC *dc;
670 if( (signed int)dwRadius < 0 )
671 return FALSE;
673 dc = get_dc_ptr( hdc );
674 if(!dc) return FALSE;
676 update_dc( dc );
677 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
678 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
680 if (result)
682 dc->attr->cur_pos.x = GDI_ROUND( x + cos( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
683 dc->attr->cur_pos.y = GDI_ROUND( y - sin( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
685 release_dc_ptr( dc );
686 return result;
689 /***********************************************************************
690 * NtGdiPolyDraw (win32u.@)
692 BOOL WINAPI NtGdiPolyDraw( HDC hdc, const POINT *points, const BYTE *types, DWORD count )
694 DC *dc = get_dc_ptr( hdc );
695 PHYSDEV physdev;
696 BOOL result;
698 if(!dc) return FALSE;
700 update_dc( dc );
701 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
702 result = physdev->funcs->pPolyDraw( physdev, points, types, count );
703 if (result && count)
704 dc->attr->cur_pos = points[count - 1];
706 release_dc_ptr( dc );
707 return result;
711 /**********************************************************************
712 * LineDDA (GDI32.@)
714 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
715 LINEDDAPROC callback, LPARAM lParam )
717 INT xadd = 1, yadd = 1;
718 INT err,erradd;
719 INT cnt;
720 INT dx = nXEnd - nXStart;
721 INT dy = nYEnd - nYStart;
723 TRACE( "(%d, %d), (%d, %d), %p, %lx\n", nXStart, nYStart, nXEnd, nYEnd, callback, lParam );
725 if (dx < 0)
727 dx = -dx;
728 xadd = -1;
730 if (dy < 0)
732 dy = -dy;
733 yadd = -1;
735 if (dx > dy) /* line is "more horizontal" */
737 err = 2*dy - dx; erradd = 2*dy - 2*dx;
738 for(cnt = 0;cnt < dx; cnt++)
740 callback(nXStart,nYStart,lParam);
741 if (err > 0)
743 nYStart += yadd;
744 err += erradd;
746 else err += 2*dy;
747 nXStart += xadd;
750 else /* line is "more vertical" */
752 err = 2*dx - dy; erradd = 2*dx - 2*dy;
753 for(cnt = 0;cnt < dy; cnt++)
755 callback(nXStart,nYStart,lParam);
756 if (err > 0)
758 nXStart += xadd;
759 err += erradd;
761 else err += 2*dx;
762 nYStart += yadd;
765 return TRUE;
769 /******************************************************************
771 * *Very* simple bezier drawing code,
773 * It uses a recursive algorithm to divide the curve in a series
774 * of straight line segments. Not ideal but sufficient for me.
775 * If you are in need for something better look for some incremental
776 * algorithm.
778 * 7 July 1998 Rein Klazes
782 * some macro definitions for bezier drawing
784 * to avoid truncation errors the coordinates are
785 * shifted upwards. When used in drawing they are
786 * shifted down again, including correct rounding
787 * and avoiding floating point arithmetic
788 * 4 bits should allow 27 bits coordinates which I saw
789 * somewhere in the win32 doc's
793 #define BEZIERSHIFTBITS 4
794 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
795 #define BEZIERPIXEL BEZIERSHIFTUP(1)
796 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
797 /* maximum depth of recursion */
798 #define BEZIERMAXDEPTH 8
800 /* size of array to store points on */
801 /* enough for one curve */
802 #define BEZIER_INITBUFSIZE (150)
804 /* calculate Bezier average, in this case the middle
805 * correctly rounded...
806 * */
808 #define BEZIERMIDDLE(Mid, P1, P2) \
809 (Mid).x=((P1).x+(P2).x + 1)/2;\
810 (Mid).y=((P1).y+(P2).y + 1)/2;
812 /**********************************************************
813 * BezierCheck helper function to check
814 * that recursion can be terminated
815 * Points[0] and Points[3] are begin and endpoint
816 * Points[1] and Points[2] are control points
817 * level is the recursion depth
818 * returns true if the recursion can be terminated
820 static BOOL BezierCheck( int level, POINT *Points)
822 INT dx, dy;
823 dx=Points[3].x-Points[0].x;
824 dy=Points[3].y-Points[0].y;
825 if(abs(dy)<=abs(dx)){/* shallow line */
826 /* check that control points are between begin and end */
827 if(Points[1].x < Points[0].x){
828 if(Points[1].x < Points[3].x)
829 return FALSE;
830 }else
831 if(Points[1].x > Points[3].x)
832 return FALSE;
833 if(Points[2].x < Points[0].x){
834 if(Points[2].x < Points[3].x)
835 return FALSE;
836 }else
837 if(Points[2].x > Points[3].x)
838 return FALSE;
839 dx=BEZIERSHIFTDOWN(dx);
840 if(!dx) return TRUE;
841 if(abs(Points[1].y-Points[0].y-(dy/dx)*
842 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
843 abs(Points[2].y-Points[0].y-(dy/dx)*
844 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
845 return FALSE;
846 else
847 return TRUE;
848 }else{ /* steep line */
849 /* check that control points are between begin and end */
850 if(Points[1].y < Points[0].y){
851 if(Points[1].y < Points[3].y)
852 return FALSE;
853 }else
854 if(Points[1].y > Points[3].y)
855 return FALSE;
856 if(Points[2].y < Points[0].y){
857 if(Points[2].y < Points[3].y)
858 return FALSE;
859 }else
860 if(Points[2].y > Points[3].y)
861 return FALSE;
862 dy=BEZIERSHIFTDOWN(dy);
863 if(!dy) return TRUE;
864 if(abs(Points[1].x-Points[0].x-(dx/dy)*
865 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
866 abs(Points[2].x-Points[0].x-(dx/dy)*
867 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
868 return FALSE;
869 else
870 return TRUE;
874 /* Helper for GDI_Bezier.
875 * Just handles one Bezier, so Points should point to four POINTs
877 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
878 INT *nPtsOut, INT level )
880 if(*nPtsOut == *dwOut) {
881 *dwOut *= 2;
882 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
883 *dwOut * sizeof(POINT) );
886 if(!level || BezierCheck(level, Points)) {
887 if(*nPtsOut == 0) {
888 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
889 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
890 *nPtsOut = 1;
892 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
893 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
894 (*nPtsOut) ++;
895 } else {
896 POINT Points2[4]; /* for the second recursive call */
897 Points2[3]=Points[3];
898 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
899 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
900 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
902 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
903 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
904 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
906 Points2[0]=Points[3];
908 /* do the two halves */
909 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
910 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
916 /***********************************************************************
917 * GDI_Bezier [INTERNAL]
918 * Calculate line segments that approximate -what microsoft calls- a bezier
919 * curve.
920 * The routine recursively divides the curve in two parts until a straight
921 * line can be drawn
923 * PARAMS
925 * Points [I] Ptr to count POINTs which are the end and control points
926 * of the set of Bezier curves to flatten.
927 * count [I] Number of Points. Must be 3n+1.
928 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
929 * lines+1).
931 * RETURNS
933 * Ptr to an array of POINTs that contain the lines that approximate the
934 * Beziers. The array is allocated on the process heap and it is the caller's
935 * responsibility to HeapFree it. [this is not a particularly nice interface
936 * but since we can't know in advance how many points we will generate, the
937 * alternative would be to call the function twice, once to determine the size
938 * and a second time to do the work - I decided this was too much of a pain].
940 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
942 POINT *out;
943 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
945 if (count == 1 || (count - 1) % 3 != 0) {
946 ERR("Invalid no. of points %d\n", count);
947 return NULL;
949 *nPtsOut = 0;
950 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
951 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
952 POINT ptBuf[4];
953 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
954 for(i = 0; i < 4; i++) {
955 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
956 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
958 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
960 TRACE("Produced %d points\n", *nPtsOut);
961 return out;
964 /******************************************************************************
965 * GdiGradientFill (GDI32.@)
967 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
968 void *grad_array, ULONG ngrad, ULONG mode )
970 DC *dc;
971 PHYSDEV physdev;
972 BOOL ret;
973 ULONG i;
975 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc, vert_array, nvert, grad_array, ngrad);
977 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
979 SetLastError( ERROR_INVALID_PARAMETER );
980 return FALSE;
982 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
983 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
985 if (!(dc = get_dc_ptr( hdc )))
987 SetLastError( ERROR_INVALID_PARAMETER );
988 return FALSE;
990 update_dc( dc );
991 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
992 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
993 release_dc_ptr( dc );
994 return ret;
997 /******************************************************************************
998 * GdiDrawStream (GDI32.@)
1001 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1003 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);
1004 return FALSE;