1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
|
/*
* File : dc_trans.c
* This file is part of RT-Thread GUI
* COPYRIGHT (C) 2006 - 2014, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2014-03-15 Grissom The first version
*/
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/dc.h>
#include <rtgui/dc_trans.h>
struct rtgui_dc_trans
{
struct rtgui_matrix m;
struct rtgui_dc *owner;
int use_aa;
};
struct rtgui_dc_trans* rtgui_dc_trans_create(struct rtgui_dc *owner)
{
struct rtgui_dc_trans *dct;
dct = (struct rtgui_dc_trans*)rtgui_malloc(sizeof(*dct));
if (!dct)
return RT_NULL;
rtgu_matrix_identity(&dct->m);
dct->owner = owner;
dct->use_aa = 0;
return dct;
}
RTM_EXPORT(rtgui_dc_trans_create);
void rtgui_dc_trans_destroy(struct rtgui_dc_trans *dct)
{
rtgui_free(dct);
}
RTM_EXPORT(rtgui_dc_trans_destroy);
void rtgui_dc_trans_set_aa(struct rtgui_dc_trans *dct, int use_aa)
{
RT_ASSERT(dct);
dct->use_aa = use_aa;
}
RTM_EXPORT(rtgui_dc_trans_set_aa);
void rtgui_dc_trans_rotate(struct rtgui_dc_trans *dct, double degree)
{
RT_ASSERT(dct);
rtgui_matrix_rotate(&dct->m, degree * RTGUI_MATRIX_FRAC / 360.0);
}
RTM_EXPORT(rtgui_dc_trans_rotate);
void rtgui_dc_trans_scale(struct rtgui_dc_trans *dct,
double sx,
double sy)
{
RT_ASSERT(dct);
rtgui_matrix_scale(&dct->m, sx * RTGUI_MATRIX_FRAC, sy * RTGUI_MATRIX_FRAC);
}
RTM_EXPORT(rtgui_dc_trans_scale);
void rtgui_dc_trans_move(struct rtgui_dc_trans *dct,
int dx,
int dy)
{
RT_ASSERT(dct);
rtgui_matrix_move(&dct->m, dx, dy);
}
RTM_EXPORT(rtgui_dc_trans_move);
void rtgui_dc_trans_get_new_wh(struct rtgui_dc_trans *dct,
int *new_wp,
int *new_hp)
{
struct rtgui_rect rect;
struct rtgui_point topleft, topright, bottomright;
RT_ASSERT(dct);
if (!new_wp && !new_hp)
return;
rtgui_dc_get_rect(dct->owner, &rect);
/* We ignore the movement components in the matrix. */
/* Transform result of (0, h). */
rtgui_matrix_mul_point_nomove(&topleft, 0, rect.y2,
&dct->m);
/* Transform result of (w, h). */
rtgui_matrix_mul_point_nomove(&topright, rect.x2, rect.y2,
&dct->m);
/* Transform result of (w, 0). */
rtgui_matrix_mul_point_nomove(&bottomright,
rect.x2, 0, &dct->m);
/* Transform result of (0, 0) is always (0, 0). */
#define NORMALIZE(x) do { if (x < 0) x = 0; } while (0)
if (new_wp)
{
int neww;
/* Ignore the nagtive parts. */
NORMALIZE(topright.x);
NORMALIZE(topleft.x);
NORMALIZE(bottomright.x);
neww = _UI_MAX(topright.x, _UI_ABS(topleft.x - bottomright.x))
+ dct->m.m[4];
NORMALIZE(neww);
*new_wp = neww;
}
if (new_hp)
{
int newh;
NORMALIZE(topright.y);
NORMALIZE(topleft.y);
NORMALIZE(bottomright.y);
newh = _UI_MAX(topright.y, _UI_ABS(topleft.y - bottomright.y))
+ dct->m.m[5];
NORMALIZE(newh);
*new_hp = newh;
}
#undef NORMALIZE
}
RTM_EXPORT(rtgui_dc_trans_get_new_wh);
struct _fb_rect
{
void *fb;
/* unit: pixel */
rt_uint16_t width, height;
/* unit: pixel */
rt_uint16_t skip;
};
/* FrameRect to FrameRect, same format, 2 Bytes/pixel. */
static void _blit_rotate_FR2FR_SF2B(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint16_t* RTGUI_RESTRICT srcp = (rt_uint16_t*)src->fb;
rt_uint16_t* RTGUI_RESTRICT dstp = (rt_uint16_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
/* Delta of bx/by when nx++. */
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
/* The coordinate in the source frame. */
int rx, ry;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw <= rx || orih <= ry)
continue;
/* We take the source as a whole and ignore the src->skip. */
*dstp = srcp[ry * oriw + rx];
}
dstp += dst->skip - neww;
}
}
/* FrameRect to FrameRect, same format, 2 Bytes/pixel, with AA. */
static void _blit_rotate_FR2FR_SF2B_AA(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint16_t* RTGUI_RESTRICT srcp = (rt_uint16_t*)src->fb;
rt_uint16_t* RTGUI_RESTRICT dstp = (rt_uint16_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
/* Color of pixels:
* c00 c01
* c10 c11 */
rt_uint32_t c00, c01, c10, c11;
int rx, ry, sx, sy;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw - 1 <= rx || orih - 1 <= ry)
continue;
c00 = srcp[ry * oriw + rx];
c01 = srcp[ry * oriw + rx + 1];
c10 = srcp[(ry + 1) * oriw + rx];
c11 = srcp[(ry + 1) * oriw + rx + 1];
c00 = (c00 | c00 << 16) & 0x07e0f81f;
c01 = (c01 | c01 << 16) & 0x07e0f81f;
c10 = (c10 | c10 << 16) & 0x07e0f81f;
c11 = (c11 | c11 << 16) & 0x07e0f81f;
/* We down scale the interpolate factor to 5 bits to avoid color
* corruption. */
sx = ((unsigned int)bx % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
sy = ((unsigned int)by % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
if (sx)
c00 = ((c01 - c00) * sx / 32 + c00) & 0x07e0f81f;
if (sx && sy)
c10 = ((c11 - c10) * sx / 32 + c10) & 0x07e0f81f;
if (sy)
c00 = ((c10 - c00) * sy / 32 + c00) & 0x07e0f81f;
/* We take the source as a whole and ignore the src->skip. */
*dstp = c00 | (c00 >> 16);
}
dstp += dst->skip - neww;
}
}
union _rgba
{
rt_uint32_t blk;
struct
{
rt_uint8_t r, g, b, a;
} d;
};
/* FrameRect to FrameRect, same format, 4 Bytes/pixel. */
static void _blit_rotate_FR2FR_SF4B(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint32_t* RTGUI_RESTRICT srcp = (rt_uint32_t*)src->fb;
rt_uint32_t* RTGUI_RESTRICT dstp = (rt_uint32_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
union _rgba spix, dpix;
int rx, ry, a;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw <= rx || orih <= ry)
continue;
spix.blk = srcp[ry * oriw + rx];
/* Down scale the alpha to 5 bits. */
a = spix.d.a >> 3;
if (a == 0)
continue;
if (a == 31)
{
*dstp = spix.blk;
continue;
}
dpix.blk = *dstp;
dpix.d.r = (spix.d.r - dpix.d.r) * a / 32 + dpix.d.r;
dpix.d.g = (spix.d.g - dpix.d.g) * a / 32 + dpix.d.g;
dpix.d.b = (spix.d.b - dpix.d.b) * a / 32 + dpix.d.b;
*dstp = dpix.blk;
}
dstp += dst->skip - neww;
}
}
/* FrameRect to FrameRect, same format, 4 Bytes/pixel, with AA. */
static void _blit_rotate_FR2FR_SF4B_AA(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint32_t* RTGUI_RESTRICT srcp = (rt_uint32_t*)src->fb;
rt_uint32_t* RTGUI_RESTRICT dstp = (rt_uint32_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
union _rgba spix00, spix01, spix10, spix11, dpix;
int rx, ry, a, sx, sy;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw - 1 <= rx || orih - 1 <= ry)
continue;
spix00.blk = srcp[ry * oriw + rx];
/* Down scale the interpolate factor to 5 bits. */
sx = ((unsigned int)bx % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
sy = ((unsigned int)by % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
spix01.blk = srcp[ry * oriw + rx + 1];
spix10.blk = srcp[(ry + 1) * oriw + rx];
spix11.blk = srcp[(ry + 1) * oriw + rx + 1];
if (sx)
spix00.d.a = (spix01.d.a - spix00.d.a) * sx / 32 + spix00.d.a;
if (sx && sy)
spix10.d.a = (spix11.d.a - spix10.d.a) * sx / 32 + spix10.d.a;
if (sy)
spix00.d.a = (spix10.d.a - spix00.d.a) * sy / 32 + spix00.d.a;
a = spix00.d.a >> 3;
if (a == 0)
continue;
if (sx)
{
spix00.d.r = (spix01.d.r - spix00.d.r) * sx / 32 + spix00.d.r;
spix00.d.g = (spix01.d.g - spix00.d.g) * sx / 32 + spix00.d.g;
spix00.d.b = (spix01.d.b - spix00.d.b) * sx / 32 + spix00.d.b;
}
if (sx && sy)
{
spix10.d.r = (spix11.d.r - spix10.d.r) * sx / 32 + spix10.d.r;
spix10.d.g = (spix11.d.g - spix10.d.g) * sx / 32 + spix10.d.g;
spix10.d.b = (spix11.d.b - spix10.d.b) * sx / 32 + spix10.d.b;
}
if (sy)
{
spix00.d.r = (spix10.d.r - spix00.d.r) * sy / 32 + spix00.d.r;
spix00.d.g = (spix10.d.g - spix00.d.g) * sy / 32 + spix00.d.g;
spix00.d.b = (spix10.d.b - spix00.d.b) * sy / 32 + spix00.d.b;
}
if (a == (255 >> 3))
{
*dstp = spix00.blk;
continue;
}
dpix.blk = *dstp;
dpix.d.r = (spix00.d.r - dpix.d.r) * a / 32 + dpix.d.r;
dpix.d.g = (spix00.d.g - dpix.d.g) * a / 32 + dpix.d.g;
dpix.d.b = (spix00.d.b - dpix.d.b) * a / 32 + dpix.d.b;
*dstp = dpix.blk;
}
dstp += dst->skip - neww;
}
}
/* FrameRect to FrameRect, from ARGB8888 to RGB565. */
static void _blit_rotate_FR2FR_ARGB2RGB565(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint32_t* RTGUI_RESTRICT srcp = (rt_uint32_t*)src->fb;
rt_uint16_t* RTGUI_RESTRICT dstp = (rt_uint16_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
int rx, ry;
int alpha;
rt_uint32_t op;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw <= rx || orih <= ry)
continue;
/* We take the source as a whole and ignore the src->skip. */
op = srcp[ry * oriw + rx];
/* downscale alpha to 5 bits */
alpha = op >> 27;
if (alpha == (255 >> 3))
{
*dstp = (rt_uint16_t)((op >> 8 & 0xf800) +
(op >> 5 & 0x7e0) +
(op >> 3 & 0x1f));
}
else if (alpha != 0)
{
/* We take the source as a whole and ignore the src->skip. */
rt_uint32_t d = *dstp;
/*
* convert source and destination to G0RAB65565
* and blend all components at the same time
*/
op = ((op & 0xfc00) << 11) + (op >> 8 & 0xf800)
+ (op >> 3 & 0x1f);
d = (d | d << 16) & 0x07e0f81f;
d += (op - d) * alpha >> 5;
d &= 0x07e0f81f;
*dstp = (rt_uint16_t)(d | d >> 16);
}
}
dstp += dst->skip - neww;
}
}
/* FrameRect to FrameRect, from ARGB8888 to RGB565. */
static void _blit_rotate_FR2FR_ARGB2RGB565_AA(struct _fb_rect* RTGUI_RESTRICT src,
const struct rtgui_point *dc_point,
struct _fb_rect* RTGUI_RESTRICT dst,
const struct rtgui_matrix *invm)
{
rt_uint32_t* RTGUI_RESTRICT srcp = (rt_uint32_t*)src->fb;
rt_uint16_t* RTGUI_RESTRICT dstp = (rt_uint16_t*)dst->fb;
int neww = dst->width;
int newh = dst->height;
int oriw = src->width;
int orih = src->height;
int nx, ny;
int dx, dy;
dx = invm->m[0];
dy = invm->m[1];
for (ny = dc_point->y; ny < newh; ny++)
{
/* Base x, y. */
int bx, by;
bx = dc_point->x * dx + ny * invm->m[2] + RTGUI_MATRIX_FRAC * invm->m[4];
by = dc_point->x * dy + ny * invm->m[3] + RTGUI_MATRIX_FRAC * invm->m[5];
for (nx = dc_point->x; nx < neww; nx++, dstp++)
{
rt_uint32_t op00, op01, op10, op11;
rt_uint8_t a00, a01, a10, a11;
int rx, ry, sx, sy;
bx += dx;
by += dy;
if (bx < 0 || by < 0)
continue;
rx = bx / RTGUI_MATRIX_FRAC;
ry = by / RTGUI_MATRIX_FRAC;
if (oriw - 1 <= rx || orih - 1 <= ry)
continue;
op00 = srcp[ry * oriw + rx];
op01 = srcp[ry * oriw + rx + 1];
op10 = srcp[(ry + 1) * oriw + rx];
op11 = srcp[(ry + 1) * oriw + rx + 1];
/* Down scale the interpolate factor to 5 bits. */
sx = ((unsigned int)bx % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
sy = ((unsigned int)by % RTGUI_MATRIX_FRAC) >> (RTGUI_MATRIX_FRAC_BITS - 5);
a00 = op00 >> 27;
a01 = op01 >> 27;
a11 = op11 >> 27;
a10 = op10 >> 27;
if (sx)
a00 = (a01 - a00) * sx / 32 + a00;
if (sx && sy)
a10 = (a11 - a10) * sx / 32 + a10;
if (sy)
a00 = (a10 - a00) * sy / 32 + a00;
if (a00 == 0)
continue;
op00 = (((op00 >> 10) & 0x3f) << 21) | (((op00 >> 19) & 0x1f) << 11) | (op00 >> 3 & 0x1f);
op10 = (((op10 >> 10) & 0x3f) << 21) | (((op10 >> 19) & 0x1f) << 11) | (op10 >> 3 & 0x1f);
op01 = (((op01 >> 10) & 0x3f) << 21) | (((op01 >> 19) & 0x1f) << 11) | (op01 >> 3 & 0x1f);
op11 = (((op11 >> 10) & 0x3f) << 21) | (((op11 >> 19) & 0x1f) << 11) | (op11 >> 3 & 0x1f);
if (sx)
{
op00 = ((op01 - op00) * sx / 32 + op00) & 0x07e0f81f;
}
if (sx && sy)
{
op10 = ((op11 - op10) * sx / 32 + op10) & 0x07e0f81f;
}
if (sy)
{
op00 = ((op10 - op00) * sy / 32 + op00) & 0x07e0f81f;
}
if (a00 == (255 >> 3))
{
*dstp = op00 | (op00 >> 16);
}
else if (a00 != 0)
{
rt_uint32_t d = *dstp;
d = (d | d << 16) & 0x07e0f81f;
d += (op00 - d) * a00 >> 5;
d &= 0x07e0f81f;
*dstp = (rt_uint16_t)(d | d >> 16);
}
}
dstp += dst->skip - neww;
}
}
static void _blit_rotate_B2B(struct rtgui_dc_trans *dct,
const struct rtgui_point *dc_point,
struct rtgui_dc_buffer* RTGUI_RESTRICT dest,
struct rtgui_rect *rect,
const struct rtgui_matrix *invm,
int neww, int newh)
{
struct rtgui_rect srcrect;
struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer*)dct->owner;
struct _fb_rect srcfb, dstfb;
rtgui_dc_get_rect(dct->owner, &srcrect);
srcfb.fb = ((struct rtgui_dc_buffer*)dct->owner)->pixel;
srcfb.width = srcrect.x2;
srcfb.height = srcrect.y2;
srcfb.skip = 0;
dstfb.fb = dest->pixel + rtgui_color_get_bpp(dest->pixel_format) * (rect->x1 + rect->y1 * dest->width);
dstfb.width = neww;
dstfb.height = newh;
dstfb.skip = dest->width;
if (dc->pixel_format == dest->pixel_format)
{
switch (rtgui_color_get_bpp(dest->pixel_format))
{
case 2:
if (dct->use_aa)
_blit_rotate_FR2FR_SF2B_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_SF2B(&srcfb, dc_point,
&dstfb, invm);
break;
case 4:
if (dct->use_aa)
_blit_rotate_FR2FR_SF4B_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_SF4B(&srcfb, dc_point,
&dstfb, invm);
break;
default:
rt_kprintf("could not handle bpp: %d\n",
rtgui_color_get_bpp(dest->pixel_format));
return;
}
}
else if (dc->pixel_format == RTGRAPHIC_PIXEL_FORMAT_ARGB888 &&
dest->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
{
if (dct->use_aa)
_blit_rotate_FR2FR_ARGB2RGB565_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_ARGB2RGB565(&srcfb, dc_point,
&dstfb, invm);
}
else
{
rt_kprintf("not implemented yet\n");
return;
}
}
static void _blit_rotate_B2H(struct rtgui_dc_trans *dct,
const struct rtgui_point *dc_point,
struct rtgui_dc_hw* dest,
struct rtgui_rect *rect,
const struct rtgui_matrix *invm,
int neww, int newh)
{
struct rtgui_rect srcrect;
int start_pix;
struct _fb_rect srcfb, dstfb;
struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer*)dct->owner;
if (dest->hw_driver->framebuffer == RT_NULL)
{
rt_kprintf("Only support framebuffer hw dc\n");
return;
}
rtgui_dc_get_rect(dct->owner, &srcrect);
srcfb.fb = ((struct rtgui_dc_buffer*)dct->owner)->pixel;
srcfb.width = srcrect.x2;
srcfb.height = srcrect.y2;
srcfb.skip = 0;
/* Start point of the widget. */
start_pix = dest->owner->extent.x1 + dest->owner->extent.y1 * dest->hw_driver->width;
/* Start point of the inner rect. */
start_pix += rect->x1 + rect->y1 * dest->hw_driver->width;
dstfb.fb = (void*)(dest->hw_driver->framebuffer
+ rtgui_color_get_bpp(dest->hw_driver->pixel_format) * start_pix);
dstfb.width = neww;
dstfb.height = newh;
dstfb.skip = dest->hw_driver->width;
if (dc->pixel_format == dest->hw_driver->pixel_format)
{
switch (rtgui_color_get_bpp(dest->hw_driver->pixel_format))
{
case 2:
if (dct->use_aa)
_blit_rotate_FR2FR_SF2B_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_SF2B(&srcfb, dc_point,
&dstfb, invm);
break;
case 4:
if (dct->use_aa)
_blit_rotate_FR2FR_SF4B_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_SF4B(&srcfb, dc_point,
&dstfb, invm);
break;
default:
rt_kprintf("could not handle bpp: %d\n",
rtgui_color_get_bpp(dest->hw_driver->pixel_format));
return;
}
}
else if (dc->pixel_format == RTGRAPHIC_PIXEL_FORMAT_ARGB888 &&
dest->hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
{
if (dct->use_aa)
_blit_rotate_FR2FR_ARGB2RGB565_AA(&srcfb, dc_point,
&dstfb, invm);
else
_blit_rotate_FR2FR_ARGB2RGB565(&srcfb, dc_point,
&dstfb, invm);
}
else
{
rt_kprintf("not implemented yet\n");
return;
}
}
void rtgui_dc_trans_blit(struct rtgui_dc_trans *dct,
struct rtgui_point *dc_point,
struct rtgui_dc *dest,
struct rtgui_rect *rect)
{
struct rtgui_rect bkrect;
struct rtgui_matrix invm;
struct rtgui_point dp;
int neww, newh;
RT_ASSERT(dct);
RT_ASSERT(dest);
if (dc_point == RT_NULL)
{
dp.x = dp.y = 0;
dc_point = &dp;
}
if (rect == RT_NULL)
{
rtgui_dc_get_rect(dest, &bkrect);
rect = &bkrect;
}
rtgui_dc_trans_get_new_wh(dct, &neww, &newh);
if (dc_point->x < 0)
{
neww -= dc_point->x;
if (neww < 0)
return;
dc_point->x = 0;
}
else if (dc_point->x > neww)
return;
if (dc_point->y < 0)
{
newh -= dc_point->y;
if (newh < 0)
return;
dc_point->y = 0;
}
else if (dc_point->y > newh)
return;
if (rtgui_matrix_inverse(&dct->m, &invm))
return;
if (rtgui_rect_width(*rect) < neww - dc_point->x)
neww = dc_point->x + rtgui_rect_width(*rect);
if (rtgui_rect_height(*rect) < newh - dc_point->y)
newh = dc_point->y + rtgui_rect_height(*rect);
/* Route to different optimized routines. */
if (dct->owner->type == RTGUI_DC_BUFFER)
{
if (dest->type == RTGUI_DC_BUFFER)
_blit_rotate_B2B(dct, dc_point,
(struct rtgui_dc_buffer*)dest,
rect, &invm, neww, newh);
else if (dest->type == RTGUI_DC_HW)
_blit_rotate_B2H(dct, dc_point,
(struct rtgui_dc_hw*)dest,
rect, &invm, neww, newh);
else if (dest->type == RTGUI_DC_CLIENT)
// TODO:
;
else
rt_kprintf("unknown dc for dc_trans\n");
}
else
rt_kprintf("not implemented yet\n");
}
RTM_EXPORT(rtgui_dc_trans_blit);
|