f2adad31b05967d64373fd15bd491ea83e58fe9a
[reactos.git] / reactos / win32ss / gdi / ntgdi / xformobj.c
1 /*
2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/xformobj.c
5 * PURPOSE: XFORMOBJ API
6 * PROGRAMMER: Timo Kreuzer
7 */
8
9 /** Includes ******************************************************************/
10
11 #include <win32k.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 C_ASSERT(sizeof(FIX) == sizeof(LONG));
16 #define FIX2LONG(x) ((x) >> 4)
17 #define LONG2FIX(x) ((x) << 4)
18
19 #define FLOATOBJ_Equal _FLOATOBJ_Equal
20 #define FLOATOBJ_GetLong _FLOATOBJ_GetLong
21 #define FLOATOBJ_GetFix _FLOATOBJ_GetFix
22 #define FLOATOBJ_IsLong _FLOATOBJ_IsLong
23 #define FLOATOBJ_Equal0 _FLOATOBJ_Equal0
24 #define FLOATOBJ_Equal1 _FLOATOBJ_Equal1
25
26 /** Inline helper functions ***************************************************/
27
28 /*
29 * Inline helper to calculate pfo1 * pfo2 + pfo3 * pfo4
30 */
31 FORCEINLINE
32 VOID
33 MulAdd(
34 PFLOATOBJ pfoDest,
35 PFLOATOBJ pfo1,
36 PFLOATOBJ pfo2,
37 PFLOATOBJ pfo3,
38 PFLOATOBJ pfo4)
39 {
40 FLOATOBJ foTmp;
41
42 *pfoDest = *pfo1;
43 FLOATOBJ_Mul(pfoDest, pfo2);
44 foTmp = *pfo3;
45 FLOATOBJ_Mul(&foTmp, pfo4);
46 FLOATOBJ_Add(pfoDest, &foTmp);
47 }
48
49 /*
50 * Inline helper to calculate pfo1 * l2 + pfo3 * l4
51 */
52 FORCEINLINE
53 VOID
54 MulAddLong(
55 PFLOATOBJ pfoDest,
56 PFLOATOBJ pfo1,
57 LONG l2,
58 PFLOATOBJ pfo3,
59 LONG l4)
60 {
61 FLOATOBJ foTmp;
62
63 *pfoDest = *pfo1;
64 FLOATOBJ_MulLong(pfoDest, l2);
65 foTmp = *pfo3;
66 FLOATOBJ_MulLong(&foTmp, l4);
67 FLOATOBJ_Add(pfoDest, &foTmp);
68 }
69
70 /*
71 * Inline helper to calculate pfo1 * pfo2 - pfo3 * pfo4
72 */
73 FORCEINLINE
74 VOID
75 MulSub(
76 PFLOATOBJ pfoDest,
77 PFLOATOBJ pfo1,
78 PFLOATOBJ pfo2,
79 PFLOATOBJ pfo3,
80 PFLOATOBJ pfo4)
81 {
82 FLOATOBJ foTmp;
83
84 *pfoDest = *pfo1;
85 FLOATOBJ_Mul(pfoDest, pfo2);
86 foTmp = *pfo3;
87 FLOATOBJ_Mul(&foTmp, pfo4);
88 FLOATOBJ_Sub(pfoDest, &foTmp);
89 }
90
91 /*
92 * Inline helper to get the complexity hint from flAccel
93 */
94 FORCEINLINE
95 ULONG
96 HintFromAccel(ULONG flAccel)
97 {
98 switch (flAccel & (XFORM_SCALE|XFORM_UNITY|XFORM_NO_TRANSLATION))
99 {
100 case (XFORM_SCALE|XFORM_UNITY|XFORM_NO_TRANSLATION):
101 return GX_IDENTITY;
102 case (XFORM_SCALE|XFORM_UNITY):
103 return GX_OFFSET;
104 case XFORM_SCALE:
105 return GX_SCALE;
106 default:
107 return GX_GENERAL;
108 }
109 }
110
111 /** Internal functions ********************************************************/
112
113 ULONG
114 NTAPI
115 XFORMOBJ_UpdateAccel(
116 IN XFORMOBJ *pxo)
117 {
118 PMATRIX pmx = XFORMOBJ_pmx(pxo);
119
120 /* Copy Dx and Dy to FIX format */
121 pmx->fxDx = FLOATOBJ_GetFix(&pmx->efDx);
122 pmx->fxDy = FLOATOBJ_GetFix(&pmx->efDy);
123
124 pmx->flAccel = 0;
125
126 if (FLOATOBJ_Equal0(&pmx->efDx) &&
127 FLOATOBJ_Equal0(&pmx->efDy))
128 {
129 pmx->flAccel |= XFORM_NO_TRANSLATION;
130 }
131
132 if (FLOATOBJ_Equal0(&pmx->efM12) &&
133 FLOATOBJ_Equal0(&pmx->efM21))
134 {
135 pmx->flAccel |= XFORM_SCALE;
136 }
137
138 if (FLOATOBJ_Equal1(&pmx->efM11) &&
139 FLOATOBJ_Equal1(&pmx->efM22))
140 {
141 pmx->flAccel |= XFORM_UNITY;
142 }
143
144 if (FLOATOBJ_IsLong(&pmx->efM11) && FLOATOBJ_IsLong(&pmx->efM12) &&
145 FLOATOBJ_IsLong(&pmx->efM21) && FLOATOBJ_IsLong(&pmx->efM22))
146 {
147 pmx->flAccel |= XFORM_INTEGER;
148 }
149
150 return HintFromAccel(pmx->flAccel);
151 }
152
153
154 ULONG
155 NTAPI
156 XFORMOBJ_iSetXform(
157 OUT XFORMOBJ *pxo,
158 IN const XFORML *pxform)
159 {
160 PMATRIX pmx = XFORMOBJ_pmx(pxo);
161
162 /* Check parameters */
163 if (!pxo || !pxform) return DDI_ERROR;
164
165 /* Check if the xform is valid */
166 if ((pxform->eM11 == 0) || (pxform->eM22 == 0)) return DDI_ERROR;
167
168 /* Copy members */
169 FLOATOBJ_SetFloat(&pmx->efM11, pxform->eM11);
170 FLOATOBJ_SetFloat(&pmx->efM12, pxform->eM12);
171 FLOATOBJ_SetFloat(&pmx->efM21, pxform->eM21);
172 FLOATOBJ_SetFloat(&pmx->efM22, pxform->eM22);
173 FLOATOBJ_SetFloat(&pmx->efDx, pxform->eDx);
174 FLOATOBJ_SetFloat(&pmx->efDy, pxform->eDy);
175
176 /* Update accelerators and return complexity */
177 return XFORMOBJ_UpdateAccel(pxo);
178 }
179
180
181 /*
182 * Multiplies pxo1 with pxo2 and stores the result in pxo.
183 * returns complexity hint
184 * | efM11 efM12 0 |
185 * | efM21 efM22 0 |
186 * | efDx efDy 1 |
187 */
188 ULONG
189 NTAPI
190 XFORMOBJ_iCombine(
191 IN XFORMOBJ *pxo,
192 IN XFORMOBJ *pxo1,
193 IN XFORMOBJ *pxo2)
194 {
195 MATRIX mx;
196 PMATRIX pmx, pmx1, pmx2;
197
198 pmx = XFORMOBJ_pmx(pxo);
199 pmx1 =XFORMOBJ_pmx(pxo1);
200 pmx2 = XFORMOBJ_pmx(pxo2);
201
202 /* Do a 3 x 3 matrix multiplication with mx as destinantion */
203 MulAdd(&mx.efM11, &pmx1->efM11, &pmx2->efM11, &pmx1->efM12, &pmx2->efM21);
204 MulAdd(&mx.efM12, &pmx1->efM11, &pmx2->efM12, &pmx1->efM12, &pmx2->efM22);
205 MulAdd(&mx.efM21, &pmx1->efM21, &pmx2->efM11, &pmx1->efM22, &pmx2->efM21);
206 MulAdd(&mx.efM22, &pmx1->efM21, &pmx2->efM12, &pmx1->efM22, &pmx2->efM22);
207 MulAdd(&mx.efDx, &pmx1->efDx, &pmx2->efM11, &pmx1->efDy, &pmx2->efM21);
208 FLOATOBJ_Add(&mx.efDx, &pmx2->efDx);
209 MulAdd(&mx.efDy, &pmx1->efDx, &pmx2->efM12, &pmx1->efDy, &pmx2->efM22);
210 FLOATOBJ_Add(&mx.efDy, &pmx2->efDy);
211
212 /* Copy back */
213 *pmx = mx;
214
215 /* Update accelerators and return complexity */
216 return XFORMOBJ_UpdateAccel(pxo);
217 }
218
219
220 ULONG
221 NTAPI
222 XFORMOBJ_iCombineXform(
223 IN XFORMOBJ *pxo,
224 IN XFORMOBJ *pxo1,
225 IN XFORML *pxform,
226 IN BOOL bLeftMultiply)
227 {
228 MATRIX mx;
229 XFORMOBJ xo2;
230
231 XFORMOBJ_vInit(&xo2, &mx);
232 XFORMOBJ_iSetXform(&xo2, pxform);
233
234 if (bLeftMultiply)
235 {
236 return XFORMOBJ_iCombine(pxo, &xo2, pxo1);
237 }
238 else
239 {
240 return XFORMOBJ_iCombine(pxo, pxo1, &xo2);
241 }
242 }
243
244 /*
245 * A^-1 = adj(A) / det(AT)
246 * A^-1 = 1/(a*d - b*c) * (a22,-a12,a21,-a11)
247 */
248 ULONG
249 NTAPI
250 XFORMOBJ_iInverse(
251 OUT XFORMOBJ *pxoDst,
252 IN XFORMOBJ *pxoSrc)
253 {
254 PMATRIX pmxDst, pmxSrc;
255 FLOATOBJ foDet;
256 XFORM xformSrc;
257
258 pmxDst = XFORMOBJ_pmx(pxoDst);
259 pmxSrc = XFORMOBJ_pmx(pxoSrc);
260
261 XFORMOBJ_iGetXform(pxoSrc, (XFORML*)&xformSrc);
262
263 /* det = M11 * M22 - M12 * M21 */
264 MulSub(&foDet, &pmxSrc->efM11, &pmxSrc->efM22, &pmxSrc->efM12, &pmxSrc->efM21);
265
266 if (FLOATOBJ_Equal0(&foDet))
267 {
268 /* Determinant is 0! */
269 return DDI_ERROR;
270 }
271
272 /* Calculate adj(A) / det(A) */
273 pmxDst->efM11 = pmxSrc->efM22;
274 FLOATOBJ_Div(&pmxDst->efM11, &foDet);
275 pmxDst->efM22 = pmxSrc->efM11;
276 FLOATOBJ_Div(&pmxDst->efM22, &foDet);
277
278 /* The other 2 are negative, negate foDet for that */
279 FLOATOBJ_Neg(&foDet);
280 pmxDst->efM12 = pmxSrc->efM12;
281 FLOATOBJ_Div(&pmxDst->efM12, &foDet);
282 pmxDst->efM21 = pmxSrc->efM21;
283 FLOATOBJ_Div(&pmxDst->efM21, &foDet);
284
285 /* Calculate the inverted x shift: Dx' = -Dx * M11' - Dy * M21' */
286 pmxDst->efDx = pmxSrc->efDx;
287 FLOATOBJ_Neg(&pmxDst->efDx);
288 MulSub(&pmxDst->efDx, &pmxDst->efDx, &pmxDst->efM11, &pmxSrc->efDy, &pmxDst->efM21);
289
290 /* Calculate the inverted y shift: Dy' = -Dy * M22' - Dx * M12' */
291 pmxDst->efDy = pmxSrc->efDy;
292 FLOATOBJ_Neg(&pmxDst->efDy);
293 MulSub(&pmxDst->efDy, &pmxDst->efDy, &pmxDst->efM22, &pmxSrc->efDx, &pmxDst->efM12);
294
295 /* Update accelerators and return complexity */
296 return XFORMOBJ_UpdateAccel(pxoDst);
297 }
298
299
300 BOOL
301 NTAPI
302 XFORMOBJ_bXformFixPoints(
303 IN XFORMOBJ *pxo,
304 IN ULONG cPoints,
305 IN PPOINTL pptIn,
306 OUT PPOINTL pptOut)
307 {
308 PMATRIX pmx;
309 INT i;
310 FLOATOBJ fo1, fo2;
311 FLONG flAccel;
312
313 pmx = XFORMOBJ_pmx(pxo);
314 flAccel = pmx->flAccel;
315
316 if ((flAccel & (XFORM_SCALE|XFORM_UNITY)) == (XFORM_SCALE|XFORM_UNITY))
317 {
318 /* Identity transformation, nothing todo */
319 }
320 else if (flAccel & XFORM_INTEGER)
321 {
322 if (flAccel & XFORM_UNITY)
323 {
324 /* 1-scale integer transform */
325 i = cPoints - 1;
326 do
327 {
328 LONG x = pptIn[i].x + pptIn[i].y * FLOATOBJ_GetLong(&pmx->efM21);
329 LONG y = pptIn[i].y + pptIn[i].x * FLOATOBJ_GetLong(&pmx->efM12);
330 pptOut[i].y = y;
331 pptOut[i].x = x;
332 }
333 while (--i >= 0);
334 }
335 else if (flAccel & XFORM_SCALE)
336 {
337 /* Diagonal integer transform */
338 i = cPoints - 1;
339 do
340 {
341 pptOut[i].x = pptIn[i].x * FLOATOBJ_GetLong(&pmx->efM11);
342 pptOut[i].y = pptIn[i].y * FLOATOBJ_GetLong(&pmx->efM22);
343 }
344 while (--i >= 0);
345 }
346 else
347 {
348 /* Full integer transform */
349 i = cPoints - 1;
350 do
351 {
352 LONG x;
353 x = pptIn[i].x * FLOATOBJ_GetLong(&pmx->efM11);
354 x += pptIn[i].y * FLOATOBJ_GetLong(&pmx->efM21);
355 pptOut[i].y = pptIn[i].y * FLOATOBJ_GetLong(&pmx->efM22);
356 pptOut[i].y += pptIn[i].x * FLOATOBJ_GetLong(&pmx->efM12);
357 pptOut[i].x = x;
358 }
359 while (--i >= 0);
360 }
361 }
362 else if (flAccel & XFORM_UNITY)
363 {
364 /* 1-scale transform */
365 i = cPoints - 1;
366 do
367 {
368 fo1 = pmx->efM21;
369 FLOATOBJ_MulLong(&fo1, pptIn[i].y);
370 fo2 = pmx->efM12;
371 FLOATOBJ_MulLong(&fo2, pptIn[i].x);
372 pptOut[i].x = pptIn[i].x + FLOATOBJ_GetLong(&fo1);
373 pptOut[i].y = pptIn[i].y + FLOATOBJ_GetLong(&fo2);
374 }
375 while (--i >= 0);
376 }
377 else if (flAccel & XFORM_SCALE)
378 {
379 /* Diagonal float transform */
380 i = cPoints - 1;
381 do
382 {
383 fo1 = pmx->efM11;
384 FLOATOBJ_MulLong(&fo1, pptIn[i].x);
385 pptOut[i].x = FLOATOBJ_GetLong(&fo1);
386 fo2 = pmx->efM22;
387 FLOATOBJ_MulLong(&fo2, pptIn[i].y);
388 pptOut[i].y = FLOATOBJ_GetLong(&fo2);
389 }
390 while (--i >= 0);
391 }
392 else
393 {
394 /* Full float transform */
395 i = cPoints - 1;
396 do
397 {
398 MulAddLong(&fo1, &pmx->efM11, pptIn[i].x, &pmx->efM21, pptIn[i].y);
399 MulAddLong(&fo2, &pmx->efM12, pptIn[i].x, &pmx->efM22, pptIn[i].y);
400 pptOut[i].x = FLOATOBJ_GetLong(&fo1);
401 pptOut[i].y = FLOATOBJ_GetLong(&fo2);
402 }
403 while (--i >= 0);
404 }
405
406 if (!(pmx->flAccel & XFORM_NO_TRANSLATION))
407 {
408 /* Translate points */
409 i = cPoints - 1;
410 do
411 {
412 pptOut[i].x += pmx->fxDx;
413 pptOut[i].y += pmx->fxDy;
414 }
415 while (--i >= 0);
416 }
417
418 return TRUE;
419 }
420
421 /** Public functions **********************************************************/
422
423 // www.osr.com/ddk/graphics/gdifncs_0s2v.htm
424 ULONG
425 APIENTRY
426 XFORMOBJ_iGetXform(
427 IN XFORMOBJ *pxo,
428 OUT XFORML *pxform)
429 {
430 PMATRIX pmx = XFORMOBJ_pmx(pxo);
431
432 /* Check parameters */
433 if (!pxo || !pxform)
434 {
435 return DDI_ERROR;
436 }
437
438 /* Copy members */
439 pxform->eM11 = FLOATOBJ_GetFloat(&pmx->efM11);
440 pxform->eM12 = FLOATOBJ_GetFloat(&pmx->efM12);
441 pxform->eM21 = FLOATOBJ_GetFloat(&pmx->efM21);
442 pxform->eM22 = FLOATOBJ_GetFloat(&pmx->efM22);
443 pxform->eDx = FLOATOBJ_GetFloat(&pmx->efDx);
444 pxform->eDy = FLOATOBJ_GetFloat(&pmx->efDy);
445
446 /* Return complexity hint */
447 return HintFromAccel(pmx->flAccel);
448 }
449
450
451 // www.osr.com/ddk/graphics/gdifncs_5ig7.htm
452 ULONG
453 APIENTRY
454 XFORMOBJ_iGetFloatObjXform(
455 IN XFORMOBJ *pxo,
456 OUT FLOATOBJ_XFORM *pxfo)
457 {
458 PMATRIX pmx = XFORMOBJ_pmx(pxo);
459
460 /* Check parameters */
461 if (!pxo || !pxfo)
462 {
463 return DDI_ERROR;
464 }
465
466 /* Copy members */
467 pxfo->eM11 = pmx->efM11;
468 pxfo->eM12 = pmx->efM12;
469 pxfo->eM21 = pmx->efM21;
470 pxfo->eM22 = pmx->efM22;
471 pxfo->eDx = pmx->efDx;
472 pxfo->eDy = pmx->efDy;
473
474 /* Return complexity hint */
475 return HintFromAccel(pmx->flAccel);
476 }
477
478
479 // www.osr.com/ddk/graphics/gdifncs_027b.htm
480 BOOL
481 APIENTRY
482 XFORMOBJ_bApplyXform(
483 IN XFORMOBJ *pxo,
484 IN ULONG iMode,
485 IN ULONG cPoints,
486 IN PVOID pvIn,
487 OUT PVOID pvOut)
488 {
489 MATRIX mx;
490 XFORMOBJ xoInv;
491 POINTL *pptl;
492 INT i;
493
494 /* Check parameters */
495 if (!pxo || !pvIn || !pvOut || cPoints < 1)
496 {
497 return FALSE;
498 }
499
500 /* Use inverse xform? */
501 if (iMode == XF_INV_FXTOL || iMode == XF_INV_LTOL)
502 {
503 XFORMOBJ_vInit(&xoInv, &mx);
504 if (XFORMOBJ_iInverse(&xoInv, pxo) == DDI_ERROR)
505 {
506 return FALSE;
507 }
508 pxo = &xoInv;
509 }
510
511 /* Convert POINTL to POINTFIX? */
512 if (iMode == XF_LTOFX || iMode == XF_LTOL || iMode == XF_INV_LTOL)
513 {
514 pptl = pvIn;
515 for (i = cPoints - 1; i >= 0; i--)
516 {
517 pptl[i].x = LONG2FIX(pptl[i].x);
518 pptl[i].y = LONG2FIX(pptl[i].y);
519 }
520 }
521
522 /* Do the actual fixpoint transformation */
523 if (!XFORMOBJ_bXformFixPoints(pxo, cPoints, pvIn, pvOut))
524 {
525 return FALSE;
526 }
527
528 /* Convert POINTFIX to POINTL? */
529 if (iMode == XF_INV_FXTOL || iMode == XF_INV_LTOL || iMode == XF_LTOL)
530 {
531 pptl = pvOut;
532 for (i = cPoints - 1; i >= 0; i--)
533 {
534 pptl[i].x = FIX2LONG(pptl[i].x);
535 pptl[i].y = FIX2LONG(pptl[i].y);
536 }
537 }
538
539 return TRUE;
540 }
541
542 /* EOF */