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
25 #include "wine/debug.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
)
45 return InvalidParameter
;
50 static void WINAPI
NotificationUnhook(ULONG_PTR token
)
52 TRACE("%ld\n", token
);
55 /*****************************************************
58 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
60 TRACE("(%p, %d, %p)\n", hinst
, reason
, reserved
);
64 case DLL_PROCESS_ATTACH
:
65 DisableThreadLibraryCalls( hinst
);
66 init_generic_string_formats();
69 case DLL_PROCESS_DETACH
:
71 free_installed_fonts();
72 free_generic_string_formats();
78 /*****************************************************
79 * GdiplusStartup [GDIPLUS.@]
81 Status WINAPI
GdiplusStartup(ULONG_PTR
*token
, const struct GdiplusStartupInput
*input
,
82 struct GdiplusStartupOutput
*output
)
85 return InvalidParameter
;
87 TRACE("%p %p %p\n", token
, input
, output
);
88 TRACE("GdiplusStartupInput %d %p %d %d\n", input
->GdiplusVersion
,
89 input
->DebugEventCallback
, input
->SuppressBackgroundThread
,
90 input
->SuppressExternalCodecs
);
92 if(input
->GdiplusVersion
< 1 || input
->GdiplusVersion
> 2)
93 return UnsupportedGdiplusVersion
;
95 if(input
->SuppressBackgroundThread
){
97 return InvalidParameter
;
99 output
->NotificationHook
= NotificationHook
;
100 output
->NotificationUnhook
= NotificationUnhook
;
105 /* FIXME: DebugEventCallback ignored */
110 GpStatus WINAPI
GdiplusNotificationHook(ULONG_PTR
*token
)
112 FIXME("%p\n", token
);
113 return NotificationHook(token
);
116 void WINAPI
GdiplusNotificationUnhook(ULONG_PTR token
)
118 FIXME("%ld\n", token
);
119 NotificationUnhook(token
);
122 /*****************************************************
123 * GdiplusShutdown [GDIPLUS.@]
125 ULONG WINAPI
GdiplusShutdown_wrapper(ULONG_PTR token
)
127 /* Notice the slightly different prototype from the official
128 * signature which forces us to use the _wrapper suffix.
131 /* FIXME: no object tracking */
133 /* "bricksntiles" expects a return value of 0, which native
134 * coincidentally gives.
139 /*****************************************************
140 * GdipAlloc [GDIPLUS.@]
142 void* WINGDIPAPI
GdipAlloc(SIZE_T size
)
144 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
147 /*****************************************************
148 * GdipFree [GDIPLUS.@]
150 void WINGDIPAPI
GdipFree(void* ptr
)
152 HeapFree(GetProcessHeap(), 0, ptr
);
155 /* Calculates the bezier points needed to fill in the arc portion starting at
156 * angle start and ending at end. These two angles should be no more than 90
157 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
158 * left and width and height). Angles must be in radians. write_first indicates
159 * that the first bezier point should be written out (usually this is false).
160 * pt is the array of GpPointFs that gets written to.
162 static void add_arc_part(GpPointF
* pt
, REAL x1
, REAL y1
, REAL x2
, REAL y2
,
163 REAL start
, REAL end
, BOOL write_first
)
165 REAL center_x
, center_y
, rad_x
, rad_y
, cos_start
, cos_end
,
166 sin_start
, sin_end
, a
, half
;
171 center_x
= x1
+ rad_x
;
172 center_y
= y1
+ rad_y
;
174 cos_start
= cos(start
);
176 sin_start
= sin(start
);
179 half
= (end
- start
) / 2.0;
180 a
= 4.0 / 3.0 * (1 - cos(half
)) / sin(half
);
186 pt
[1].X
= cos_start
- a
* sin_start
;
187 pt
[1].Y
= sin_start
+ a
* cos_start
;
191 pt
[2].X
= cos_end
+ a
* sin_end
;
192 pt
[2].Y
= sin_end
- a
* cos_end
;
194 /* expand the points back from the unit circle to the ellipse */
195 for(i
= (write_first
? 0 : 1); i
< 4; i
++){
196 pt
[i
].X
= pt
[i
].X
* rad_x
+ center_x
;
197 pt
[i
].Y
= pt
[i
].Y
* rad_y
+ center_y
;
201 /* We plot the curve as if it is on a circle then stretch the points. This
202 * adjusts the angles so that when we stretch the points they will end in the
203 * right place. This is only complicated because atan and atan2 do not behave
205 static REAL
unstretch_angle(REAL angle
, REAL dia_x
, REAL dia_y
)
210 if(fabs(cos(angle
)) < 0.00001 || fabs(sin(angle
)) < 0.00001)
213 stretched
= gdiplus_atan2(sin(angle
) / fabs(dia_y
), cos(angle
) / fabs(dia_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;
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 left
, REAL top
, REAL width
, REAL height
,
222 REAL start_angle
, REAL sweep_angle
)
225 REAL partial_end_angle
, end_angle
;
227 end_angle
= deg2rad(start_angle
+ sweep_angle
);
228 start_angle
= deg2rad(start_angle
);
232 start_angle
= unstretch_angle(start_angle
, width
, height
);
233 end_angle
= unstretch_angle(end_angle
, width
, height
);
236 for(i
= 0; i
< MAX_ARC_PTS
- 1; i
+= 3){
237 /* check if we've overshot the end angle */
238 if( sweep_angle
> 0.0 )
240 if (start_angle
>= end_angle
) break;
241 partial_end_angle
= min(start_angle
+ M_PI_2
, end_angle
);
245 if (start_angle
<= end_angle
) break;
246 partial_end_angle
= max(start_angle
- M_PI_2
, end_angle
);
250 add_arc_part(&points
[i
], left
, top
, width
, height
, start_angle
, partial_end_angle
, i
== 0);
252 start_angle
= partial_end_angle
;
255 if (i
== 0) return 0;
259 COLORREF
ARGB2COLORREF(ARGB color
)
262 Packing of these color structures:
265 FIXME:doesn't handle alpha channel
267 return ((color
& 0x0000ff) << 16) +
269 ((color
& 0xff0000) >> 16);
272 HBITMAP
ARGB2BMP(ARGB color
)
279 if ((color
& 0xff000000) == 0xff000000) return 0;
281 bi
.bmiHeader
.biSize
= sizeof(bi
.bmiHeader
);
282 bi
.bmiHeader
.biWidth
= 1;
283 bi
.bmiHeader
.biHeight
= 1;
284 bi
.bmiHeader
.biPlanes
= 1;
285 bi
.bmiHeader
.biBitCount
= 32;
286 bi
.bmiHeader
.biCompression
= BI_RGB
;
287 bi
.bmiHeader
.biSizeImage
= 0;
288 bi
.bmiHeader
.biXPelsPerMeter
= 0;
289 bi
.bmiHeader
.biYPelsPerMeter
= 0;
290 bi
.bmiHeader
.biClrUsed
= 0;
291 bi
.bmiHeader
.biClrImportant
= 0;
293 result
= CreateDIBSection(0, &bi
, DIB_RGB_COLORS
, (void*)&bits
, NULL
, 0);
295 bits
[0].rgbReserved
= alpha
= (color
>>24)&0xff;
296 bits
[0].rgbRed
= ((color
>>16)&0xff)*alpha
/255;
297 bits
[0].rgbGreen
= ((color
>>8)&0xff)*alpha
/255;
298 bits
[0].rgbBlue
= (color
&0xff)*alpha
/255;
303 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
304 REAL
gdiplus_atan2(REAL dy
, REAL dx
)
306 if((dx
== 0.0) && (dy
!= 0.0))
307 return dy
> 0.0 ? M_PI_2
: -M_PI_2
;
309 return atan2(dy
, dx
);
312 GpStatus
hresult_to_status(HRESULT res
)
320 return InvalidParameter
;
326 /* converts a given unit to its value in pixels */
327 REAL
units_to_pixels(REAL units
, GpUnit unit
, REAL dpi
)
336 return units
* dpi
/ point_per_inch
;
340 return units
* dpi
/ 300.0; /* Per MSDN */
342 return units
* dpi
/ mm_per_inch
;
344 FIXME("Unhandled unit type: %d\n", unit
);
349 /* converts value in pixels to a given unit */
350 REAL
pixels_to_units(REAL pixels
, GpUnit unit
, REAL dpi
)
359 return pixels
* point_per_inch
/ dpi
;
363 return pixels
* 300.0 / dpi
;
365 return pixels
* mm_per_inch
/ dpi
;
367 FIXME("Unhandled unit type: %d\n", unit
);
372 REAL
units_scale(GpUnit from
, GpUnit to
, REAL dpi
)
374 REAL pixels
= units_to_pixels(1.0, from
, dpi
);
375 return pixels_to_units(pixels
, to
, dpi
);
378 /* Calculates Bezier points from cardinal spline points. */
379 void calc_curve_bezier(const GpPointF
*pts
, REAL tension
, REAL
*x1
,
380 REAL
*y1
, REAL
*x2
, REAL
*y2
)
384 /* calculate tangent */
385 xdiff
= pts
[2].X
- pts
[0].X
;
386 ydiff
= pts
[2].Y
- pts
[0].Y
;
388 /* apply tangent to get control points */
389 *x1
= pts
[1].X
- tension
* xdiff
;
390 *y1
= pts
[1].Y
- tension
* ydiff
;
391 *x2
= pts
[1].X
+ tension
* xdiff
;
392 *y2
= pts
[1].Y
+ tension
* ydiff
;
395 /* Calculates Bezier points from cardinal spline endpoints. */
396 void calc_curve_bezier_endp(REAL xend
, REAL yend
, REAL xadj
, REAL yadj
,
397 REAL tension
, REAL
*x
, REAL
*y
)
399 /* tangent at endpoints is the line from the endpoint to the adjacent point */
400 *x
= gdip_round(tension
* (xadj
- xend
) + xend
);
401 *y
= gdip_round(tension
* (yadj
- yend
) + yend
);
404 /* make sure path has enough space for len more points */
405 BOOL
lengthen_path(GpPath
*path
, INT len
)
407 /* initial allocation */
408 if(path
->datalen
== 0){
409 path
->datalen
= len
* 2;
411 path
->pathdata
.Points
= heap_alloc_zero(path
->datalen
* sizeof(PointF
));
412 if(!path
->pathdata
.Points
) return FALSE
;
414 path
->pathdata
.Types
= heap_alloc_zero(path
->datalen
);
415 if(!path
->pathdata
.Types
){
416 heap_free(path
->pathdata
.Points
);
420 /* reallocation, double size of arrays */
421 else if(path
->datalen
- path
->pathdata
.Count
< len
){
422 while(path
->datalen
- path
->pathdata
.Count
< len
)
425 path
->pathdata
.Points
= heap_realloc(path
->pathdata
.Points
, path
->datalen
* sizeof(PointF
));
426 if(!path
->pathdata
.Points
) return FALSE
;
428 path
->pathdata
.Types
= heap_realloc(path
->pathdata
.Types
, path
->datalen
);
429 if(!path
->pathdata
.Types
) return FALSE
;
435 void convert_32bppARGB_to_32bppPARGB(UINT width
, UINT height
,
436 BYTE
*dst_bits
, INT dst_stride
, const BYTE
*src_bits
, INT src_stride
)
439 for (y
=0; y
<height
; y
++)
441 const BYTE
*src
=src_bits
+y
*src_stride
;
442 BYTE
*dst
=dst_bits
+y
*dst_stride
;
443 for (x
=0; x
<width
; x
++)
446 *dst
++ = (*src
++ * alpha
+ 127) / 255;
447 *dst
++ = (*src
++ * alpha
+ 127) / 255;
448 *dst
++ = (*src
++ * alpha
+ 127) / 255;
454 /* recursive deletion of GpRegion nodes */
455 void delete_element(region_element
* element
)
457 switch(element
->type
)
462 GdipDeletePath(element
->elementdata
.path
);
464 case RegionDataEmptyRect
:
465 case RegionDataInfiniteRect
:
468 delete_element(element
->elementdata
.combine
.left
);
469 delete_element(element
->elementdata
.combine
.right
);
470 heap_free(element
->elementdata
.combine
.left
);
471 heap_free(element
->elementdata
.combine
.right
);
476 const char *debugstr_rectf(const RectF
* rc
)
478 if (!rc
) return "(null)";
479 return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
482 const char *debugstr_pointf(const PointF
* pt
)
484 if (!pt
) return "(null)";
485 return wine_dbg_sprintf("(%0.2f,%0.2f)", pt
->X
, pt
->Y
);