hidclass.sys: Implement IRP_MN_QUERY_ID for HID devices.
[wine.git] / dlls / gdiplus / gdiplus.c
blob128145581dfebbbef20231f7489fd23cc3b96475
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
20 #include <math.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "wine/debug.h"
26 #include "wingdi.h"
28 #include "objbase.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
38 static const REAL mm_per_inch = 25.4;
39 static const REAL point_per_inch = 72.0;
41 static Status WINAPI NotificationHook(ULONG_PTR *token)
43 TRACE("%p\n", token);
44 if(!token)
45 return InvalidParameter;
47 return Ok;
50 static void WINAPI NotificationUnhook(ULONG_PTR token)
52 TRACE("%ld\n", token);
55 /*****************************************************
56 * DllMain
58 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
60 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
62 switch(reason)
64 case DLL_PROCESS_ATTACH:
65 DisableThreadLibraryCalls( hinst );
66 break;
68 case DLL_PROCESS_DETACH:
69 if (reserved) break;
70 free_installed_fonts();
71 break;
73 return TRUE;
76 /*****************************************************
77 * GdiplusStartup [GDIPLUS.@]
79 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
80 struct GdiplusStartupOutput *output)
82 if(!token || !input)
83 return InvalidParameter;
85 TRACE("%p %p %p\n", token, input, output);
86 TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
87 input->DebugEventCallback, input->SuppressBackgroundThread,
88 input->SuppressExternalCodecs);
90 if(input->GdiplusVersion < 1 || input->GdiplusVersion > 2)
91 return UnsupportedGdiplusVersion;
93 if(input->SuppressBackgroundThread){
94 if(!output)
95 return InvalidParameter;
97 output->NotificationHook = NotificationHook;
98 output->NotificationUnhook = NotificationUnhook;
101 *token = 0xdeadbeef;
103 /* FIXME: DebugEventCallback ignored */
105 return Ok;
108 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
110 FIXME("%p\n", token);
111 return NotificationHook(token);
114 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
116 FIXME("%ld\n", token);
117 NotificationUnhook(token);
120 /*****************************************************
121 * GdiplusShutdown [GDIPLUS.@]
123 ULONG WINAPI GdiplusShutdown_wrapper(ULONG_PTR token)
125 /* Notice the slightly different prototype from the official
126 * signature which forces us to use the _wrapper suffix.
129 /* FIXME: no object tracking */
131 /* "bricksntiles" expects a return value of 0, which native
132 * coincidentally gives.
134 return 0;
137 /*****************************************************
138 * GdipAlloc [GDIPLUS.@]
140 void* WINGDIPAPI GdipAlloc(SIZE_T size)
142 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
145 /*****************************************************
146 * GdipFree [GDIPLUS.@]
148 void WINGDIPAPI GdipFree(void* ptr)
150 HeapFree(GetProcessHeap(), 0, ptr);
153 /* Calculates the bezier points needed to fill in the arc portion starting at
154 * angle start and ending at end. These two angles should be no more than 90
155 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
156 * left and width and height). Angles must be in radians. write_first indicates
157 * that the first bezier point should be written out (usually this is false).
158 * pt is the array of GpPointFs that gets written to.
160 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
161 REAL start, REAL end, BOOL write_first)
163 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
164 sin_start, sin_end, a, half;
165 INT i;
167 rad_x = x2 / 2.0;
168 rad_y = y2 / 2.0;
169 center_x = x1 + rad_x;
170 center_y = y1 + rad_y;
172 cos_start = cos(start);
173 cos_end = cos(end);
174 sin_start = sin(start);
175 sin_end = sin(end);
177 half = (end - start) / 2.0;
178 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
180 if(write_first){
181 pt[0].X = cos_start;
182 pt[0].Y = sin_start;
184 pt[1].X = cos_start - a * sin_start;
185 pt[1].Y = sin_start + a * cos_start;
187 pt[3].X = cos_end;
188 pt[3].Y = sin_end;
189 pt[2].X = cos_end + a * sin_end;
190 pt[2].Y = sin_end - a * cos_end;
192 /* expand the points back from the unit circle to the ellipse */
193 for(i = (write_first ? 0 : 1); i < 4; i ++){
194 pt[i].X = pt[i].X * rad_x + center_x;
195 pt[i].Y = pt[i].Y * rad_y + center_y;
199 /* We plot the curve as if it is on a circle then stretch the points. This
200 * adjusts the angles so that when we stretch the points they will end in the
201 * right place. This is only complicated because atan and atan2 do not behave
202 * conveniently. */
203 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
205 REAL stretched;
206 INT revs_off;
208 *angle = deg2rad(*angle);
210 if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
211 return;
213 stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
214 revs_off = gdip_round(*angle / (2.0 * M_PI)) - gdip_round(stretched / (2.0 * M_PI));
215 stretched += ((REAL)revs_off) * M_PI * 2.0;
216 *angle = stretched;
219 /* Stores the bezier points that correspond to the arc in points. If points is
220 * null, just return the number of points needed to represent the arc. */
221 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
222 REAL startAngle, REAL sweepAngle)
224 INT i;
225 REAL end_angle, start_angle, endAngle;
227 endAngle = startAngle + sweepAngle;
228 unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
229 unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
231 /* start_angle and end_angle are the iterative variables */
232 start_angle = startAngle;
234 for(i = 0; i < MAX_ARC_PTS - 1; i += 3){
235 /* check if we've overshot the end angle */
236 if( sweepAngle > 0.0 )
238 if (start_angle >= endAngle) break;
239 end_angle = min(start_angle + M_PI_2, endAngle);
241 else
243 if (start_angle <= endAngle) break;
244 end_angle = max(start_angle - M_PI_2, endAngle);
247 if (points)
248 add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
250 start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
253 if (i == 0) return 0;
254 else return i+1;
257 COLORREF ARGB2COLORREF(ARGB color)
260 Packing of these color structures:
261 COLORREF: 00bbggrr
262 ARGB: aarrggbb
263 FIXME:doesn't handle alpha channel
265 return ((color & 0x0000ff) << 16) +
266 (color & 0x00ff00) +
267 ((color & 0xff0000) >> 16);
270 HBITMAP ARGB2BMP(ARGB color)
272 BITMAPINFO bi;
273 HBITMAP result;
274 RGBQUAD *bits;
275 int alpha;
277 if ((color & 0xff000000) == 0xff000000) return 0;
279 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
280 bi.bmiHeader.biWidth = 1;
281 bi.bmiHeader.biHeight = 1;
282 bi.bmiHeader.biPlanes = 1;
283 bi.bmiHeader.biBitCount = 32;
284 bi.bmiHeader.biCompression = BI_RGB;
285 bi.bmiHeader.biSizeImage = 0;
286 bi.bmiHeader.biXPelsPerMeter = 0;
287 bi.bmiHeader.biYPelsPerMeter = 0;
288 bi.bmiHeader.biClrUsed = 0;
289 bi.bmiHeader.biClrImportant = 0;
291 result = CreateDIBSection(0, &bi, DIB_RGB_COLORS, (void*)&bits, NULL, 0);
293 bits[0].rgbReserved = alpha = (color>>24)&0xff;
294 bits[0].rgbRed = ((color>>16)&0xff)*alpha/255;
295 bits[0].rgbGreen = ((color>>8)&0xff)*alpha/255;
296 bits[0].rgbBlue = (color&0xff)*alpha/255;
298 return result;
301 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
302 REAL gdiplus_atan2(REAL dy, REAL dx)
304 if((dx == 0.0) && (dy != 0.0))
305 return dy > 0.0 ? M_PI_2 : -M_PI_2;
307 return atan2(dy, dx);
310 GpStatus hresult_to_status(HRESULT res)
312 switch(res){
313 case S_OK:
314 return Ok;
315 case E_OUTOFMEMORY:
316 return OutOfMemory;
317 case E_INVALIDARG:
318 return InvalidParameter;
319 default:
320 return GenericError;
324 /* converts a given unit to its value in pixels */
325 REAL units_to_pixels(REAL units, GpUnit unit, REAL dpi)
327 switch (unit)
329 case UnitPixel:
330 case UnitWorld:
331 case UnitDisplay:
332 return units;
333 case UnitPoint:
334 return units * dpi / point_per_inch;
335 case UnitInch:
336 return units * dpi;
337 case UnitDocument:
338 return units * dpi / 300.0; /* Per MSDN */
339 case UnitMillimeter:
340 return units * dpi / mm_per_inch;
341 default:
342 FIXME("Unhandled unit type: %d\n", unit);
343 return 0;
347 /* converts value in pixels to a given unit */
348 REAL pixels_to_units(REAL pixels, GpUnit unit, REAL dpi)
350 switch (unit)
352 case UnitPixel:
353 case UnitWorld:
354 case UnitDisplay:
355 return pixels;
356 case UnitPoint:
357 return pixels * point_per_inch / dpi;
358 case UnitInch:
359 return pixels / dpi;
360 case UnitDocument:
361 return pixels * 300.0 / dpi;
362 case UnitMillimeter:
363 return pixels * mm_per_inch / dpi;
364 default:
365 FIXME("Unhandled unit type: %d\n", unit);
366 return 0;
370 REAL units_scale(GpUnit from, GpUnit to, REAL dpi)
372 REAL pixels = units_to_pixels(1.0, from, dpi);
373 return pixels_to_units(pixels, to, dpi);
376 /* Calculates Bezier points from cardinal spline points. */
377 void calc_curve_bezier(const GpPointF *pts, REAL tension, REAL *x1,
378 REAL *y1, REAL *x2, REAL *y2)
380 REAL xdiff, ydiff;
382 /* calculate tangent */
383 xdiff = pts[2].X - pts[0].X;
384 ydiff = pts[2].Y - pts[0].Y;
386 /* apply tangent to get control points */
387 *x1 = pts[1].X - tension * xdiff;
388 *y1 = pts[1].Y - tension * ydiff;
389 *x2 = pts[1].X + tension * xdiff;
390 *y2 = pts[1].Y + tension * ydiff;
393 /* Calculates Bezier points from cardinal spline endpoints. */
394 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
395 REAL tension, REAL *x, REAL *y)
397 /* tangent at endpoints is the line from the endpoint to the adjacent point */
398 *x = gdip_round(tension * (xadj - xend) + xend);
399 *y = gdip_round(tension * (yadj - yend) + yend);
402 /* make sure path has enough space for len more points */
403 BOOL lengthen_path(GpPath *path, INT len)
405 /* initial allocation */
406 if(path->datalen == 0){
407 path->datalen = len * 2;
409 path->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
410 if(!path->pathdata.Points) return FALSE;
412 path->pathdata.Types = heap_alloc_zero(path->datalen);
413 if(!path->pathdata.Types){
414 heap_free(path->pathdata.Points);
415 return FALSE;
418 /* reallocation, double size of arrays */
419 else if(path->datalen - path->pathdata.Count < len){
420 while(path->datalen - path->pathdata.Count < len)
421 path->datalen *= 2;
423 path->pathdata.Points = heap_realloc(path->pathdata.Points, path->datalen * sizeof(PointF));
424 if(!path->pathdata.Points) return FALSE;
426 path->pathdata.Types = heap_realloc(path->pathdata.Types, path->datalen);
427 if(!path->pathdata.Types) return FALSE;
430 return TRUE;
433 void convert_32bppARGB_to_32bppPARGB(UINT width, UINT height,
434 BYTE *dst_bits, INT dst_stride, const BYTE *src_bits, INT src_stride)
436 INT x, y;
437 for (y=0; y<height; y++)
439 const BYTE *src=src_bits+y*src_stride;
440 BYTE *dst=dst_bits+y*dst_stride;
441 for (x=0; x<width; x++)
443 BYTE alpha=src[3];
444 *dst++ = (*src++ * alpha + 127) / 255;
445 *dst++ = (*src++ * alpha + 127) / 255;
446 *dst++ = (*src++ * alpha + 127) / 255;
447 *dst++ = *src++;
452 /* recursive deletion of GpRegion nodes */
453 void delete_element(region_element* element)
455 switch(element->type)
457 case RegionDataRect:
458 break;
459 case RegionDataPath:
460 GdipDeletePath(element->elementdata.path);
461 break;
462 case RegionDataEmptyRect:
463 case RegionDataInfiniteRect:
464 break;
465 default:
466 delete_element(element->elementdata.combine.left);
467 delete_element(element->elementdata.combine.right);
468 heap_free(element->elementdata.combine.left);
469 heap_free(element->elementdata.combine.right);
470 break;
474 const char *debugstr_rectf(const RectF* rc)
476 if (!rc) return "(null)";
477 return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc->X, rc->Y, rc->Width, rc->Height);
480 const char *debugstr_pointf(const PointF* pt)
482 if (!pt) return "(null)";
483 return wine_dbg_sprintf("(%0.2f,%0.2f)", pt->X, pt->Y);