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