ddraw: Don't try to enumerate WINED3DFMT_R8G8B8A8_SNORM.
[wine/multimedia.git] / dlls / gdiplus / gdiplus.c
blobfb159be55b243736fbf68a7d22352fbb5ec894c8
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 Status WINAPI NotificationHook(ULONG_PTR *token)
40 TRACE("%p\n", token);
41 if(!token)
42 return InvalidParameter;
44 return Ok;
47 static void WINAPI NotificationUnhook(ULONG_PTR token)
49 TRACE("%ld\n", token);
52 /*****************************************************
53 * DllMain
55 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
57 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
59 switch(reason)
61 case DLL_PROCESS_ATTACH:
62 DisableThreadLibraryCalls( hinst );
63 break;
65 case DLL_PROCESS_DETACH:
66 free_installed_fonts();
67 break;
69 return TRUE;
72 /*****************************************************
73 * GdiplusStartup [GDIPLUS.@]
75 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
76 struct GdiplusStartupOutput *output)
78 if(!token || !input)
79 return InvalidParameter;
81 TRACE("%p %p %p\n", token, input, output);
82 TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
83 input->DebugEventCallback, input->SuppressBackgroundThread,
84 input->SuppressExternalCodecs);
86 if(input->GdiplusVersion < 1 || input->GdiplusVersion > 2)
87 return UnsupportedGdiplusVersion;
89 if(input->SuppressBackgroundThread){
90 if(!output)
91 return InvalidParameter;
93 output->NotificationHook = NotificationHook;
94 output->NotificationUnhook = NotificationUnhook;
97 *token = 0xdeadbeef;
99 /* FIXME: DebugEventCallback ignored */
101 return Ok;
104 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
106 FIXME("%p\n", token);
107 return NotificationHook(token);
110 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
112 FIXME("%ld\n", token);
113 NotificationUnhook(token);
116 /*****************************************************
117 * GdiplusShutdown [GDIPLUS.@]
119 void WINAPI GdiplusShutdown(ULONG_PTR token)
121 /* FIXME: no object tracking */
124 /* "bricksntiles" expects a return value of 0, which native coincidentally gives */
125 ULONG WINAPI GdiplusShutdown_wrapper(ULONG_PTR token)
127 GdiplusShutdown(token);
129 return 0;
132 /*****************************************************
133 * GdipAlloc [GDIPLUS.@]
135 void* WINGDIPAPI GdipAlloc(SIZE_T size)
137 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
140 /*****************************************************
141 * GdipFree [GDIPLUS.@]
143 void WINGDIPAPI GdipFree(void* ptr)
145 HeapFree(GetProcessHeap(), 0, ptr);
148 /* Calculates the bezier points needed to fill in the arc portion starting at
149 * angle start and ending at end. These two angles should be no more than 90
150 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
151 * left and width and height). Angles must be in radians. write_first indicates
152 * that the first bezier point should be written out (usually this is false).
153 * pt is the array of GpPointFs that gets written to.
155 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
156 REAL start, REAL end, BOOL write_first)
158 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
159 sin_start, sin_end, a, half;
160 INT i;
162 rad_x = x2 / 2.0;
163 rad_y = y2 / 2.0;
164 center_x = x1 + rad_x;
165 center_y = y1 + rad_y;
167 cos_start = cos(start);
168 cos_end = cos(end);
169 sin_start = sin(start);
170 sin_end = sin(end);
172 half = (end - start) / 2.0;
173 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
175 if(write_first){
176 pt[0].X = cos_start;
177 pt[0].Y = sin_start;
179 pt[1].X = cos_start - a * sin_start;
180 pt[1].Y = sin_start + a * cos_start;
182 pt[3].X = cos_end;
183 pt[3].Y = sin_end;
184 pt[2].X = cos_end + a * sin_end;
185 pt[2].Y = sin_end - a * cos_end;
187 /* expand the points back from the unit circle to the ellipse */
188 for(i = (write_first ? 0 : 1); i < 4; i ++){
189 pt[i].X = pt[i].X * rad_x + center_x;
190 pt[i].Y = pt[i].Y * rad_y + center_y;
194 /* We plot the curve as if it is on a circle then stretch the points. This
195 * adjusts the angles so that when we stretch the points they will end in the
196 * right place. This is only complicated because atan and atan2 do not behave
197 * conveniently. */
198 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
200 REAL stretched;
201 INT revs_off;
203 *angle = deg2rad(*angle);
205 if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
206 return;
208 stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
209 revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
210 stretched += ((REAL)revs_off) * M_PI * 2.0;
211 *angle = stretched;
214 /* Stores the bezier points that correspond to the arc in points. If points is
215 * null, just return the number of points needed to represent the arc. */
216 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
217 REAL startAngle, REAL sweepAngle)
219 INT i;
220 REAL end_angle, start_angle, endAngle;
222 endAngle = startAngle + sweepAngle;
223 unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
224 unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
226 /* start_angle and end_angle are the iterative variables */
227 start_angle = startAngle;
229 for(i = 0; i < MAX_ARC_PTS - 1; i += 3){
230 /* check if we've overshot the end angle */
231 if( sweepAngle > 0.0 )
233 if (start_angle >= endAngle) break;
234 end_angle = min(start_angle + M_PI_2, endAngle);
236 else
238 if (start_angle <= endAngle) break;
239 end_angle = max(start_angle - M_PI_2, endAngle);
242 if (points)
243 add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
245 start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
248 if (i == 0) return 0;
249 else return i+1;
252 COLORREF ARGB2COLORREF(ARGB color)
255 Packing of these color structures:
256 COLORREF: 00bbggrr
257 ARGB: aarrggbb
258 FIXME:doesn't handle alpha channel
260 return ((color & 0x0000ff) << 16) +
261 (color & 0x00ff00) +
262 ((color & 0xff0000) >> 16);
265 HBITMAP ARGB2BMP(ARGB color)
267 HDC hdc;
268 BITMAPINFO bi;
269 HBITMAP result;
270 RGBQUAD *bits;
271 int alpha;
273 if ((color & 0xff000000) == 0xff000000) return 0;
275 hdc = CreateCompatibleDC(NULL);
277 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
278 bi.bmiHeader.biWidth = 1;
279 bi.bmiHeader.biHeight = 1;
280 bi.bmiHeader.biPlanes = 1;
281 bi.bmiHeader.biBitCount = 32;
282 bi.bmiHeader.biCompression = BI_RGB;
283 bi.bmiHeader.biSizeImage = 0;
284 bi.bmiHeader.biXPelsPerMeter = 0;
285 bi.bmiHeader.biYPelsPerMeter = 0;
286 bi.bmiHeader.biClrUsed = 0;
287 bi.bmiHeader.biClrImportant = 0;
289 result = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void*)&bits, NULL, 0);
291 bits[0].rgbReserved = alpha = (color>>24)&0xff;
292 bits[0].rgbRed = ((color>>16)&0xff)*alpha/255;
293 bits[0].rgbGreen = ((color>>8)&0xff)*alpha/255;
294 bits[0].rgbBlue = (color&0xff)*alpha/255;
296 DeleteDC(hdc);
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 convert_unit(REAL logpixels, GpUnit unit)
327 switch(unit)
329 case UnitInch:
330 return logpixels;
331 case UnitPoint:
332 return logpixels / 72.0;
333 case UnitDocument:
334 return logpixels / 300.0;
335 case UnitMillimeter:
336 return logpixels / 25.4;
337 case UnitWorld:
338 ERR("cannot convert UnitWorld\n");
339 return 0.0;
340 case UnitPixel:
341 case UnitDisplay:
342 default:
343 return 1.0;
347 /* Calculates Bezier points from cardinal spline points. */
348 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
349 REAL *y1, REAL *x2, REAL *y2)
351 REAL xdiff, ydiff;
353 /* calculate tangent */
354 xdiff = pts[2].X - pts[0].X;
355 ydiff = pts[2].Y - pts[0].Y;
357 /* apply tangent to get control points */
358 *x1 = pts[1].X - tension * xdiff;
359 *y1 = pts[1].Y - tension * ydiff;
360 *x2 = pts[1].X + tension * xdiff;
361 *y2 = pts[1].Y + tension * ydiff;
364 /* Calculates Bezier points from cardinal spline endpoints. */
365 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
366 REAL tension, REAL *x, REAL *y)
368 /* tangent at endpoints is the line from the endpoint to the adjacent point */
369 *x = roundr(tension * (xadj - xend) + xend);
370 *y = roundr(tension * (yadj - yend) + yend);
373 /* make sure path has enough space for len more points */
374 BOOL lengthen_path(GpPath *path, INT len)
376 /* initial allocation */
377 if(path->datalen == 0){
378 path->datalen = len * 2;
380 path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
381 if(!path->pathdata.Points) return FALSE;
383 path->pathdata.Types = GdipAlloc(path->datalen);
384 if(!path->pathdata.Types){
385 GdipFree(path->pathdata.Points);
386 return FALSE;
389 /* reallocation, double size of arrays */
390 else if(path->datalen - path->pathdata.Count < len){
391 while(path->datalen - path->pathdata.Count < len)
392 path->datalen *= 2;
394 path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
395 path->pathdata.Points, path->datalen * sizeof(PointF));
396 if(!path->pathdata.Points) return FALSE;
398 path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
399 path->pathdata.Types, path->datalen);
400 if(!path->pathdata.Types) return FALSE;
403 return TRUE;
406 void convert_32bppARGB_to_32bppPARGB(UINT width, UINT height,
407 BYTE *dst_bits, INT dst_stride, const BYTE *src_bits, INT src_stride)
409 UINT x, y;
410 for (y=0; y<height; y++)
412 const BYTE *src=src_bits+y*src_stride;
413 BYTE *dst=dst_bits+y*dst_stride;
414 for (x=0; x<width; x++)
416 BYTE alpha=src[3];
417 *dst++ = *src++ * alpha / 255;
418 *dst++ = *src++ * alpha / 255;
419 *dst++ = *src++ * alpha / 255;
420 *dst++ = *src++;
425 /* recursive deletion of GpRegion nodes */
426 void delete_element(region_element* element)
428 switch(element->type)
430 case RegionDataRect:
431 break;
432 case RegionDataPath:
433 GdipDeletePath(element->elementdata.pathdata.path);
434 break;
435 case RegionDataEmptyRect:
436 case RegionDataInfiniteRect:
437 break;
438 default:
439 delete_element(element->elementdata.combine.left);
440 delete_element(element->elementdata.combine.right);
441 GdipFree(element->elementdata.combine.left);
442 GdipFree(element->elementdata.combine.right);
443 break;
447 const char *debugstr_rectf(CONST RectF* rc)
449 if (!rc) return "(null)";
450 return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc->X, rc->Y, rc->Width, rc->Height);
453 const char *debugstr_pointf(CONST PointF* pt)
455 if (!pt) return "(null)";
456 return wine_dbg_sprintf("(%0.2f,%0.2f)", pt->X, pt->Y);