Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-gdi / include / line.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * GDI LineTo
4  *
5  * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /* do not include this file directly! */
21
22 int LINE_TO(HGDI_DC hdc, int nXEnd, int nYEnd)
23 {
24         int x, y;
25         int x1, y1;
26         int x2, y2;
27         int e, e2;
28         int dx, dy;
29         int sx, sy;
30         int bx1, by1;
31         int bx2, by2;
32         PIXEL_TYPE pen;
33         PIXEL_TYPE* pixel;
34         HGDI_BITMAP bmp;
35
36         x1 = hdc->pen->posX;
37         y1 = hdc->pen->posY;
38         x2 = nXEnd;
39         y2 = nYEnd;
40
41         dx = (x1 > x2) ? x1 - x2 : x2 - x1;
42         dy = (y1 > y2) ? y1 - y2 : y2 - y1;
43
44         sx = (x1 < x2) ? 1 : -1;
45         sy = (y1 < y2) ? 1 : -1;
46
47         e = dx - dy;
48
49         x = x1;
50         y = y1;
51
52         bmp = (HGDI_BITMAP) hdc->selectedObject;
53
54         if (hdc->clip->null)
55         {
56                 bx1 = (x1 < x2) ? x1 : x2;
57                 by1 = (y1 < y2) ? y1 : y2;
58                 bx2 = (x1 > x2) ? x1 : x2;
59                 by2 = (y1 > y2) ? y1 : y2;
60         }
61         else
62         {
63                 bx1 = hdc->clip->x;
64                 by1 = hdc->clip->y;
65                 bx2 = bx1 + hdc->clip->w - 1;
66                 by2 = by1 + hdc->clip->h - 1;
67         }
68
69         pen = GDI_GET_PEN_COLOR(hdc->pen);
70
71         while (1)
72         {
73                 if (!(x == x2 && y == y2))
74                 {
75                         if ((x >= bx1 && x <= bx2) && (y >= by1 && y <= by2))
76                         {
77                                 pixel = GDI_GET_POINTER(bmp, x, y);
78                                 SET_PIXEL_ROP2(pixel, &pen);
79                         }
80                 }
81                 else
82                 {
83                         break;
84                 }
85
86                 e2 = 2 * e;
87
88                 if (e2 > -dy)
89                 {
90                         e -= dy;
91                         x += sx;
92                 }
93
94                 if (e2 < dx)
95                 {
96                         e += dx;
97                         y += sy;
98                 }
99         }
100
101         return 1;
102 }
103
104 /*
105 #undef LINE_TO
106 #undef PIXEL_TYPE
107 #undef SET_PIXEL_ROP2
108 #undef GDI_GET_POINTER
109 #undef GDI_GET_PEN_COLOR
110 */