Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / wineps / graphics.c
blobbd4b34b5b9002be3d4f4efdd1f8e87dcd0a50ea4
1 /*
2 * PostScript driver graphics functions
4 * Copyright 1998 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #if defined(HAVE_FLOAT_H)
27 # include <float.h>
28 #endif
29 #if !defined(PI)
30 # define PI M_PI
31 #endif
32 #include "psdrv.h"
33 #include "wine/debug.h"
34 #include "winspool.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 /***********************************************************************
39 * PSDRV_XWStoDS
41 * Performs a world-to-viewport transformation on the specified width.
43 INT PSDRV_XWStoDS( PSDRV_PDEVICE *physDev, INT width )
45 POINT pt[2];
47 pt[0].x = 0;
48 pt[0].y = 0;
49 pt[1].x = width;
50 pt[1].y = 0;
51 LPtoDP( physDev->hdc, pt, 2 );
52 return pt[1].x - pt[0].x;
55 /***********************************************************************
56 * PSDRV_YWStoDS
58 * Performs a world-to-viewport transformation on the specified height.
60 INT PSDRV_YWStoDS( PSDRV_PDEVICE *physDev, INT height )
62 POINT pt[2];
64 pt[0].x = 0;
65 pt[0].y = 0;
66 pt[1].x = 0;
67 pt[1].y = height;
68 LPtoDP( physDev->hdc, pt, 2 );
69 return pt[1].y - pt[0].y;
72 /***********************************************************************
73 * PSDRV_DrawLine
75 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
77 if(physDev->pathdepth)
78 return;
80 if (physDev->pen.style == PS_NULL)
81 PSDRV_WriteNewPath(physDev);
82 else
83 PSDRV_WriteStroke(physDev);
86 /***********************************************************************
87 * PSDRV_LineTo
89 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
91 POINT pt[2];
93 TRACE("%d %d\n", x, y);
95 GetCurrentPositionEx( physDev->hdc, pt );
96 pt[1].x = x;
97 pt[1].y = y;
98 LPtoDP( physDev->hdc, pt, 2 );
100 PSDRV_SetPen(physDev);
102 PSDRV_SetClip(physDev);
103 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
104 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
105 PSDRV_DrawLine(physDev);
106 PSDRV_ResetClip(physDev);
108 return TRUE;
112 /***********************************************************************
113 * PSDRV_Rectangle
115 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
117 RECT rect;
119 TRACE("%d %d - %d %d\n", left, top, right, bottom);
121 rect.left = left;
122 rect.top = top;
123 rect.right = right;
124 rect.bottom = bottom;
125 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
127 /* Windows does something truly hacky here. If we're in passthrough mode
128 and our rop is R2_NOP, then we output the string below. This is used in
129 Office 2k when inserting eps files */
130 if(physDev->job.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(physDev->hdc) == R2_NOP) {
131 char buf[256];
132 sprintf(buf, "N %ld %ld %ld %ld B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
133 WriteSpool16(physDev->job.hJob, buf, strlen(buf));
134 physDev->job.had_passthrough_rect = TRUE;
135 return TRUE;
138 PSDRV_SetPen(physDev);
140 PSDRV_SetClip(physDev);
141 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
142 rect.bottom - rect.top );
143 PSDRV_Brush(physDev,0);
144 PSDRV_DrawLine(physDev);
145 PSDRV_ResetClip(physDev);
146 return TRUE;
150 /***********************************************************************
151 * PSDRV_RoundRect
153 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
154 INT bottom, INT ell_width, INT ell_height )
156 RECT rect[2];
158 rect[0].left = left;
159 rect[0].top = top;
160 rect[0].right = right;
161 rect[0].bottom = bottom;
162 rect[1].left = 0;
163 rect[1].top = 0;
164 rect[1].right = ell_width;
165 rect[1].bottom = ell_height;
166 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
168 left = rect[0].left;
169 top = rect[0].top;
170 right = rect[0].right;
171 bottom = rect[0].bottom;
172 if (left > right) { INT tmp = left; left = right; right = tmp; }
173 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
175 ell_width = rect[1].right - rect[1].left;
176 ell_height = rect[1].bottom - rect[1].top;
177 if (ell_width > right - left) ell_width = right - left;
178 if (ell_height > bottom - top) ell_height = bottom - top;
180 PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
181 PSDRV_SetPen(physDev);
183 PSDRV_SetClip(physDev);
184 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
185 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
186 ell_height, 90.0, 180.0);
187 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
188 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
189 ell_height, 0.0, 90.0);
190 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
191 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
192 ell_height, -90.0, 0.0);
193 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
194 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
195 ell_height, 180.0, -90.0);
196 PSDRV_WriteClosePath( physDev );
198 PSDRV_Brush(physDev,0);
199 PSDRV_DrawLine(physDev);
200 PSDRV_ResetClip(physDev);
201 return TRUE;
204 /***********************************************************************
205 * PSDRV_DrawArc
207 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
209 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
210 INT right, INT bottom, INT xstart, INT ystart,
211 INT xend, INT yend, int lines )
213 INT x, y, h, w;
214 double start_angle, end_angle, ratio;
215 RECT rect;
216 POINT start, end;
218 rect.left = left;
219 rect.top = top;
220 rect.right = right;
221 rect.bottom = bottom;
222 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
223 start.x = xstart;
224 start.y = ystart;
225 end.x = xend;
226 end.y = yend;
227 LPtoDP( physDev->hdc, &start, 1 );
228 LPtoDP( physDev->hdc, &end, 1 );
230 x = (rect.left + rect.right) / 2;
231 y = (rect.top + rect.bottom) / 2;
232 w = rect.right - rect.left;
233 h = rect.bottom - rect.top;
235 if(w < 0) w = -w;
236 if(h < 0) h = -h;
237 ratio = ((double)w)/h;
239 /* angle is the angle after the rectangle is transformed to a square and is
240 measured anticlockwise from the +ve x-axis */
242 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
243 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
245 start_angle *= 180.0 / PI;
246 end_angle *= 180.0 / PI;
248 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
249 PSDRV_SetPen(physDev);
251 PSDRV_SetClip(physDev);
252 if(lines == 2) /* pie */
253 PSDRV_WriteMoveTo(physDev, x, y);
254 else
255 PSDRV_WriteNewPath( physDev );
257 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
258 if(lines == 1 || lines == 2) { /* chord or pie */
259 PSDRV_WriteClosePath(physDev);
260 PSDRV_Brush(physDev,0);
262 PSDRV_DrawLine(physDev);
263 PSDRV_ResetClip(physDev);
265 return TRUE;
269 /***********************************************************************
270 * PSDRV_Arc
272 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
273 INT xstart, INT ystart, INT xend, INT yend )
275 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
278 /***********************************************************************
279 * PSDRV_Chord
281 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
282 INT xstart, INT ystart, INT xend, INT yend )
284 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
288 /***********************************************************************
289 * PSDRV_Pie
291 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
292 INT xstart, INT ystart, INT xend, INT yend )
294 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
298 /***********************************************************************
299 * PSDRV_Ellipse
301 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
303 INT x, y, w, h;
304 RECT rect;
306 TRACE("%d %d - %d %d\n", left, top, right, bottom);
308 rect.left = left;
309 rect.top = top;
310 rect.right = right;
311 rect.bottom = bottom;
312 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
314 x = (rect.left + rect.right) / 2;
315 y = (rect.top + rect.bottom) / 2;
316 w = rect.right - rect.left;
317 h = rect.bottom - rect.top;
319 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
320 PSDRV_SetPen(physDev);
322 PSDRV_SetClip(physDev);
323 PSDRV_WriteNewPath(physDev);
324 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
325 PSDRV_WriteClosePath(physDev);
326 PSDRV_Brush(physDev,0);
327 PSDRV_DrawLine(physDev);
328 PSDRV_ResetClip(physDev);
329 return TRUE;
333 /***********************************************************************
334 * PSDRV_PolyPolyline
336 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
337 DWORD polylines )
339 DWORD polyline, line, total;
340 POINT *dev_pts, *pt;
342 TRACE("\n");
344 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
345 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
346 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
347 LPtoDP( physDev->hdc, dev_pts, total );
349 pt = dev_pts;
351 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
352 PSDRV_SetPen(physDev);
353 PSDRV_SetClip(physDev);
355 for(polyline = 0; polyline < polylines; polyline++) {
356 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
357 pt++;
358 for(line = 1; line < counts[polyline]; line++, pt++)
359 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
361 HeapFree( GetProcessHeap(), 0, dev_pts );
363 PSDRV_DrawLine(physDev);
364 PSDRV_ResetClip(physDev);
365 return TRUE;
369 /***********************************************************************
370 * PSDRV_Polyline
372 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
374 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
378 /***********************************************************************
379 * PSDRV_PolyPolygon
381 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
382 UINT polygons )
384 DWORD polygon, total;
385 INT line;
386 POINT *dev_pts, *pt;
388 TRACE("\n");
390 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
391 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
392 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
393 LPtoDP( physDev->hdc, dev_pts, total );
395 pt = dev_pts;
397 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
398 PSDRV_SetPen(physDev);
399 PSDRV_SetClip(physDev);
401 for(polygon = 0; polygon < polygons; polygon++) {
402 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
403 pt++;
404 for(line = 1; line < counts[polygon]; line++, pt++)
405 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
406 PSDRV_WriteClosePath(physDev);
408 HeapFree( GetProcessHeap(), 0, dev_pts );
410 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
411 PSDRV_Brush(physDev, 1);
412 else /* WINDING */
413 PSDRV_Brush(physDev, 0);
415 PSDRV_DrawLine(physDev);
416 PSDRV_ResetClip(physDev);
417 return TRUE;
421 /***********************************************************************
422 * PSDRV_Polygon
424 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
426 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
430 /***********************************************************************
431 * PSDRV_SetPixel
433 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
435 PSCOLOR pscolor;
436 POINT pt;
438 pt.x = x;
439 pt.y = y;
440 LPtoDP( physDev->hdc, &pt, 1 );
442 PSDRV_SetClip(physDev);
443 /* we bracket the setcolor in gsave/grestore so that we don't trash
444 the current pen colour */
445 PSDRV_WriteGSave(physDev);
446 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
447 PSDRV_CreateColor( physDev, &pscolor, color );
448 PSDRV_WriteSetColor( physDev, &pscolor );
449 PSDRV_WriteFill( physDev );
450 PSDRV_WriteGRestore(physDev);
451 PSDRV_ResetClip(physDev);
452 return color;
455 /***********************************************************************
456 * PSDRV_PaintRgn
458 BOOL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
461 RGNDATA *rgndata = NULL;
462 RECT *pRect;
463 DWORD size, i;
465 TRACE("hdc=%p\n", physDev->hdc);
467 size = GetRegionData(hrgn, 0, NULL);
468 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
469 if(!rgndata) {
470 ERR("Can't allocate buffer\n");
471 return FALSE;
474 GetRegionData(hrgn, size, rgndata);
475 if (rgndata->rdh.nCount == 0)
476 goto end;
478 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
480 PSDRV_SetClip(physDev);
481 PSDRV_WriteNewPath(physDev);
482 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
483 PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
485 PSDRV_Brush(physDev, 0);
486 PSDRV_ResetClip(physDev);
488 end:
489 HeapFree(GetProcessHeap(), 0, rgndata);
490 return TRUE;