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
|
// VirtualDub - Video processing and capture application
// Graphics support library
// Copyright (C) 1998-2009 Avery Lee
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <stdafx.h>
#include <vd2/system/memory.h>
#include <vd2/system/cpuaccel.h>
#include <vd2/Kasumi/pixmap.h>
#include <vd2/Kasumi/pixmaputils.h>
namespace {
struct VDPixmapReferenceStretchBltParameters {
void *dst;
ptrdiff_t dstpitch;
const void *src;
ptrdiff_t srcpitch;
ptrdiff_t srcinc;
sint32 dx;
sint32 dy;
uint32 u;
uint32 uinc;
uint32 dudx;
uint32 v;
uint32 dvdy;
sint32 xprecopy;
sint32 xpostcopy;
ptrdiff_t xprepos;
ptrdiff_t xpostpos;
void advance() {
dst = (char *)dst + dstpitch;
src = (char *)src + srcinc;
uint32 vt = v + dvdy;
if (vt < v)
src = (char *)src + srcpitch;
v = vt;
}
};
}
void VDPixmapStretchBlt_Any8_nearest_reference(VDPixmapReferenceStretchBltParameters params) {
do {
uint8 *dstp = (uint8 *)params.dst;
const uint8 *srcp = (const uint8 *)params.src;
uint32 u = params.u;
if (params.xprecopy) {
VDMemset8(dstp, *(const uint8 *)((const char *)params.src + params.xprepos), params.xprecopy);
dstp += params.xprecopy;
}
sint32 wt = params.dx;
if (wt > 0)
do {
*dstp++ = *srcp;
uint32 ut = u + params.dudx;
srcp += ut<u;
srcp += params.uinc;
u = ut;
} while(--wt);
if (params.xpostcopy)
VDMemset8(dstp, *(const uint8 *)((const char *)params.src + params.xpostpos), params.xpostcopy);
params.advance();
} while(--params.dy);
}
void VDPixmapStretchBlt_Any16_nearest_reference(VDPixmapReferenceStretchBltParameters params) {
do {
uint16 *dstp = (uint16 *)params.dst;
const uint16 *srcp = (const uint16 *)params.src;
uint32 u = params.u;
if (params.xprecopy) {
VDMemset16(dstp, *(const uint16 *)((const char *)params.src + params.xprepos), params.xprecopy);
dstp += params.xprecopy;
}
sint32 wt = params.dx;
if (wt > 0)
do {
*dstp++ = *srcp;
uint32 ut = u + params.dudx;
srcp += ut<u;
srcp += params.uinc;
u = ut;
} while(--wt);
if (params.xpostcopy)
VDMemset16(dstp, *(const uint16 *)((const char *)params.src + params.xpostpos), params.xpostcopy);
params.advance();
} while(--params.dy);
}
void VDPixmapStretchBlt_Any24_nearest_reference(VDPixmapReferenceStretchBltParameters params) {
do {
uint8 *dstp = (uint8 *)params.dst;
const uint8 *srcp = (const uint8 *)params.src;
uint32 u = params.u;
if (params.xprecopy) {
const uint8 *repsrc = (const uint8 *)params.src + params.xprepos;
const uint8 p0 = repsrc[0];
const uint8 p1 = repsrc[1];
const uint8 p2 = repsrc[2];
for(sint32 i=0; i<params.xprecopy; ++i) {
dstp[0] = p0;
dstp[1] = p1;
dstp[2] = p2;
dstp += 3;
}
}
sint32 wt = params.dx;
if (wt > 0)
do {
dstp[0] = srcp[0];
dstp[1] = srcp[1];
dstp[2] = srcp[2];
dstp += 3;
uint32 ut = u + params.dudx;
srcp += (ut<u)*3;
srcp += params.uinc*3;
u = ut;
} while(--wt);
if (params.xpostcopy) {
const uint8 *repsrc = (const uint8 *)params.src + params.xpostpos;
const uint8 p0 = repsrc[0];
const uint8 p1 = repsrc[1];
const uint8 p2 = repsrc[2];
for(sint32 i=0; i<params.xpostcopy; ++i) {
dstp[0] = p0;
dstp[1] = p1;
dstp[2] = p2;
dstp += 3;
}
}
params.advance();
} while(--params.dy);
}
void VDPixmapStretchBlt_Any32_nearest_reference(VDPixmapReferenceStretchBltParameters params) {
do {
uint32 *dstp = (uint32 *)params.dst;
const uint32 *srcp = (const uint32 *)params.src;
uint32 u = params.u;
if (params.xprecopy) {
VDMemset32(dstp, *(const uint32 *)((const char *)params.src + params.xprepos), params.xprecopy);
dstp += params.xprecopy;
}
sint32 wt = params.dx;
if (wt > 0)
do {
*dstp++ = *srcp;
uint32 ut = u + params.dudx;
srcp += ut<u;
srcp += params.uinc;
u = ut;
} while(--wt);
if (params.xpostcopy)
VDMemset32(dstp, *(const uint32 *)((const char *)params.src + params.xpostpos), params.xpostcopy);
params.advance();
} while(--params.dy);
}
///////////////////////////////////////////////////////////////////////////
namespace {
void VDSetupNearestSamplingParameters(sint64& u64, sint64 dudx, sint32 dx, sint32 du, sint32& xprecopy, sint32& xprepos, sint32& xmain, sint32& xpostcopy, sint32& xpostpos) {
sint64 ulo = u64;
sint64 uhi = u64 + dudx * (dx - 1);
sint64 tdudx = dudx;
const sint64 ulimit = ((sint64)du << 32);
xprepos = 0;
xpostpos = du-1;
if (!tdudx) {
if (u64 < 0)
xprecopy = dx;
else if (u64 >= ulimit)
xprecopy = dx;
else
xmain = dx;
} else {
if (tdudx < 0) {
std::swap(ulo, uhi);
tdudx = -tdudx;
}
if (ulo < 0) {
if (uhi < 0)
xprecopy = dx;
else
xprecopy = (sint32)((-ulo-1) / tdudx) + 1;
VDASSERT(xprecopy <= 0 || (uint64)ulo >= (uint64)ulimit);
VDASSERT(xprecopy <= 0 || (uint64)(ulo + tdudx * (xprecopy-1)) >= (uint64)ulimit);
}
if (uhi >= ulimit) {
if (ulo >= ulimit)
xpostcopy = dx;
else
xpostcopy = (sint32)((uhi - ulimit) / tdudx) + 1;
VDASSERT(xpostcopy <= 0 || (uint64)uhi >= (uint64)ulimit);
VDASSERT(xpostcopy <= 0 || (uint64)(uhi - tdudx * (xpostcopy - 1)) >= (uint64)ulimit);
}
if (dudx < 0) {
std::swap(xprecopy, xpostcopy);
std::swap(xprepos, xpostpos);
}
xmain = dx - (xprecopy + xpostcopy);
}
// sanity-check parameters
VDASSERT(xprecopy>=0 && xprecopy <= dx);
VDASSERT(xpostcopy>=0 && xpostcopy <= dx);
VDASSERT(xmain>=0 && xmain <= dx);
VDASSERT(xprecopy <= 0 || (uint64)u64 >= (uint64)ulimit);
VDASSERT(xprecopy <= 0 || (uint64)(u64 + dudx * (xprecopy-1)) >= (uint64)ulimit);
VDASSERT(xmain <= 0 || (uint64)(u64 + dudx * xprecopy) < (uint64)ulimit);
VDASSERT(xmain <= 0 || (uint64)(u64 + dudx * (xprecopy+xmain-1)) < (uint64)ulimit);
VDASSERT(xpostcopy <= 0 || (uint64)(u64 + dudx * (xprecopy + xmain)) >= (uint64)ulimit);
VDASSERT(xpostcopy <= 0 || (uint64)(u64 + dudx * (xprecopy + xmain + xpostcopy - 1)) >= (uint64)ulimit);
u64 += dudx * xprecopy;
}
}
bool VDPixmapStretchBltNearest_reference(const VDPixmap& dst, sint32 x1, sint32 y1, sint32 x2, sint32 y2, const VDPixmap& src, sint32 u1, sint32 v1, sint32 u2, sint32 v2) {
// we don't support format conversion
if (dst.format != src.format)
return false;
void (*pBlitter)(VDPixmapReferenceStretchBltParameters);
int bpp;
switch(src.format) {
case nsVDPixmap::kPixFormat_Pal8:
pBlitter = VDPixmapStretchBlt_Any8_nearest_reference;
bpp = 1;
break;
case nsVDPixmap::kPixFormat_XRGB1555:
case nsVDPixmap::kPixFormat_RGB565:
pBlitter = VDPixmapStretchBlt_Any16_nearest_reference;
bpp = 2;
break;
case nsVDPixmap::kPixFormat_RGB888:
pBlitter = VDPixmapStretchBlt_Any24_nearest_reference;
bpp = 3;
break;
case nsVDPixmap::kPixFormat_XRGB8888:
pBlitter = VDPixmapStretchBlt_Any32_nearest_reference;
bpp = 4;
break;
default:
return false;
}
// preemptive clip to prevent gradient calculations from crashing
if (x2 == x1 || y2 == y1)
return true;
// translate destination flips into source flips
if (x1 > x2) {
std::swap(x1, x2);
std::swap(u1, u2);
}
if (y1 > y2) {
std::swap(y1, y2);
std::swap(v1, v2);
}
// compute gradients
sint32 dx = x2 - x1;
sint32 dy = y2 - y1;
sint32 du = u2 - u1;
sint32 dv = v2 - v1;
sint64 dudx = ((sint64)du << 32) / dx; // must truncate toward zero to prevent overflow
sint64 dvdy = ((sint64)dv << 32) / dy;
// prestep top-left point to pixel center and convert destination coordinates to integer
sint64 u64 = (sint64)u1 << 16;
sint64 v64 = (sint64)v1 << 16;
sint32 prestepx = (0x8000 - x1) & 0xffff;
sint32 prestepy = (0x8000 - y1) & 0xffff;
u64 += (dudx * prestepx) >> 16;
v64 += (dvdy * prestepy) >> 16;
sint32 x1i = (x1 + 0x8000) >> 16;
sint32 y1i = (y1 + 0x8000) >> 16;
sint32 x2i = (x2 + 0x8000) >> 16;
sint32 y2i = (y2 + 0x8000) >> 16;
// destination clipping
if (x1i < 0) {
u64 -= dudx * x1i;
x1i = 0;
}
if (y1i < 0) {
v64 -= dvdy * y1i;
y1i = 0;
}
if (x2i > dst.w)
x2i = dst.w;
if (y2i > dst.h)
y2i = dst.h;
if (x1i >= x2i || y1i >= y2i)
return true;
// Calculate horizontal clip parameters
sint32 xprecopy = 0, xpostcopy = 0;
int xprepos = 0;
int xpostpos = src.w-1;
int xmain = 0;
VDSetupNearestSamplingParameters(u64, dudx, x2i-x1i, src.w, xprecopy, xprepos, xmain, xpostcopy, xpostpos);
// Calculate vertical clip parameters
sint32 yprecopy = 0, ypostcopy = 0;
int yprepos = 0;
int ypostpos = src.h-1;
int ymain = 0;
VDSetupNearestSamplingParameters(v64, dvdy, y2i-y1i, src.h, yprecopy, yprepos, ymain, ypostcopy, ypostpos);
// set up parameter block
VDPixmapReferenceStretchBltParameters params;
char *srcbase = (char *)src.data + (sint32)(u64 >> 32) * bpp;
params.dst = (char *)dst.data + y1i * dst.pitch + x1i * bpp;
params.dstpitch = dst.pitch;
params.src = srcbase + (sint32)(v64 >> 32) * src.pitch;
params.srcpitch = src.pitch;
params.srcinc = (sint32)(dvdy >> 32) * src.pitch;
params.dx = xmain;
params.dy = ymain;
params.u = (uint32)u64;
params.uinc = (uint32)(dudx >> 32);
params.dudx = (uint32)dudx;
params.v = (uint32)v64;
params.dvdy = (uint32)dvdy;
params.xprecopy = xprecopy;
params.xprepos = (xprepos - (sint32)(u64 >> 32)) * bpp;
params.xpostcopy = xpostcopy;
params.xpostpos = (xpostpos - (sint32)(u64 >> 32)) * bpp;
if (yprecopy > 0) {
VDPixmapReferenceStretchBltParameters preparams(params);
preparams.src = srcbase + yprepos * src.pitch;
preparams.srcinc = 0;
preparams.dy = yprecopy;
preparams.v = 0;
preparams.dvdy = 0;
pBlitter(preparams);
params.dst = (char *)params.dst + params.dstpitch * yprecopy;
}
if (ymain > 0)
pBlitter(params);
if (ypostcopy > 0) {
VDPixmapReferenceStretchBltParameters postparams(params);
postparams.dst = (char *)params.dst + params.dstpitch * params.dy;
postparams.src = srcbase + ypostpos * src.pitch;
postparams.srcpitch = 0;
postparams.srcinc = 0;
postparams.dy = ypostcopy;
postparams.v = 0;
postparams.dvdy = 0;
pBlitter(postparams);
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace {
uint32 lerp_XRGB1555(sint32 a, sint32 b, sint32 f) {
sint32 a_rb = a & 0x7c1f;
sint32 a_g = a & 0x03e0;
sint32 b_rb = b & 0x7c1f;
sint32 b_g = b & 0x03e0;
const sint32 rb = (a_rb + (((b_rb - a_rb)*f + 0x4010) >> 5)) & 0x7c1f;
const sint32 g = (a_g + (((b_g - a_g )*f + 0x0200) >> 5)) & 0x03e0;
return rb + g;
}
uint32 lerp_XRGB8888(sint32 a, sint32 b, sint32 f) {
sint32 a_rb = a & 0xff00ff;
sint32 a_g = a & 0x00ff00;
sint32 b_rb = b & 0xff00ff;
sint32 b_g = b & 0x00ff00;
const uint32 rb = (a_rb + (((b_rb - a_rb)*f + 0x00800080) >> 8)) & 0xff00ff;
const uint32 g = (a_g + (((b_g - a_g )*f + 0x00008000) >> 8)) & 0x00ff00;
return rb + g;
}
uint32 bilerp_RGB888(sint32 a, sint32 b, sint32 c, sint32 d, sint32 x, sint32 y) {
sint32 a_rb = a & 0xff00ff;
sint32 a_g = a & 0x00ff00;
sint32 b_rb = b & 0xff00ff;
sint32 b_g = b & 0x00ff00;
sint32 c_rb = c & 0xff00ff;
sint32 c_g = c & 0x00ff00;
sint32 d_rb = d & 0xff00ff;
sint32 d_g = d & 0x00ff00;
const uint32 top_rb = (a_rb + (((b_rb - a_rb)*x + 0x00800080) >> 8)) & 0xff00ff;
const uint32 top_g = (a_g + (((b_g - a_g )*x + 0x00008000) >> 8)) & 0x00ff00;
const uint32 bot_rb = (c_rb + (((d_rb - c_rb)*x + 0x00800080) >> 8)) & 0xff00ff;
const uint32 bot_g = (c_g + (((d_g - c_g )*x + 0x00008000) >> 8)) & 0x00ff00;
const uint32 final_rb = (top_rb + (((bot_rb - top_rb)*y) >> 8)) & 0xff00ff;
const uint32 final_g = (top_g + (((bot_g - top_g )*y) >> 8)) & 0x00ff00;
return final_rb + final_g;
}
uint32 bilerp_XRGB1555(sint32 a, sint32 b, sint32 c, sint32 d, sint32 x, sint32 y) {
sint32 a_rb = a & 0x7c1f;
sint32 a_g = a & 0x03e0;
sint32 b_rb = b & 0x7c1f;
sint32 b_g = b & 0x03e0;
sint32 c_rb = c & 0x7c1f;
sint32 c_g = c & 0x03e0;
sint32 d_rb = d & 0x7c1f;
sint32 d_g = d & 0x03e0;
const sint32 top_rb = (a_rb + (((b_rb - a_rb)*x + 0x4010) >> 5)) & 0x7c1f;
const sint32 top_g = (a_g + (((b_g - a_g )*x + 0x0200) >> 5)) & 0x03e0;
const sint32 bot_rb = (c_rb + (((d_rb - c_rb)*x + 0x4010) >> 5)) & 0x7c1f;
const sint32 bot_g = (c_g + (((d_g - c_g )*x + 0x0200) >> 5)) & 0x03e0;
const sint32 final_rb = (top_rb + (((bot_rb - top_rb)*y + 0x4010) >> 5)) & 0x7c1f;
const sint32 final_g = (top_g + (((bot_g - top_g )*y + 0x0200) >> 5)) & 0x03e0;
return final_rb + final_g;
}
uint32 bilerp_RGB565(sint32 a, sint32 b, sint32 c, sint32 d, sint32 x, sint32 y) {
sint32 a_rb = a & 0xf81f;
sint32 a_g = a & 0x07e0;
sint32 b_rb = b & 0xf81f;
sint32 b_g = b & 0x07e0;
sint32 c_rb = c & 0xf81f;
sint32 c_g = c & 0x07e0;
sint32 d_rb = d & 0xf81f;
sint32 d_g = d & 0x07e0;
const sint32 top_rb = (a_rb + (((b_rb - a_rb)*x + 0x8010) >> 6)) & 0xf81f;
const sint32 top_g = (a_g + (((b_g - a_g )*x + 0x0400) >> 6)) & 0x07e0;
const sint32 bot_rb = (c_rb + (((d_rb - c_rb)*x + 0x8010) >> 6)) & 0xf81f;
const sint32 bot_g = (c_g + (((d_g - c_g )*x + 0x0400) >> 6)) & 0x07e0;
const sint32 final_rb = (top_rb + (((bot_rb - top_rb)*y + 0x8010) >> 6)) & 0xf81f;
const sint32 final_g = (top_g + (((bot_g - top_g )*y + 0x0400) >> 6)) & 0x07e0;
return final_rb + final_g;
}
}
///////////////////////////////////////////////////////////////////////////
namespace {
struct VDPixmapReferenceStretchBltBilinearParameters {
void *dst;
const void *src;
uint32 u;
uint32 uinc;
uint32 dudx;
ptrdiff_t xprepos;
ptrdiff_t xpostpos;
sint32 xprecopy;
sint32 xpostcopy;
sint32 xmidsize;
};
void VDPixmapStretchBiH_XRGB1555_to_XRGB1555(const VDPixmapReferenceStretchBltBilinearParameters& params) {
uint16 *dst = (uint16 *)params.dst;
const uint16 *src = (const uint16 *)params.src;
if (params.xprecopy)
VDMemset16(dst - params.xprecopy, *(const uint16 *)((const char *)params.src + params.xprepos), params.xprecopy);
if (params.xmidsize) {
sint32 w = params.xmidsize;
uint32 u = params.u;
const uint32 dudx = params.dudx;
const ptrdiff_t uinc = params.uinc;
do {
*dst++ = lerp_XRGB1555(src[0], src[1], u >> 27);
const uint32 ut = u + dudx;
src += uinc + (ut < u);
u = ut;
} while(--w);
}
if (params.xpostcopy)
VDMemset16(dst, *(const uint16 *)((const char *)params.src + params.xpostpos), params.xpostcopy);
}
void VDPixmapStretchBiH_XRGB8888_to_XRGB8888(const VDPixmapReferenceStretchBltBilinearParameters& params) {
uint32 *dst = (uint32 *)params.dst;
const uint32 *src = (const uint32 *)params.src;
if (params.xprecopy)
VDMemset32(dst - params.xprecopy, *(const uint32 *)((const char *)params.src + params.xprepos), params.xprecopy);
if (params.xmidsize) {
sint32 w = params.xmidsize;
uint32 u = params.u;
const uint32 dudx = params.dudx;
const ptrdiff_t uinc = params.uinc;
do {
*dst++ = lerp_XRGB8888(src[0], src[1], u >> 24);
const uint32 ut = u + dudx;
src += uinc + (ut < u);
u = ut;
} while(--w);
}
if (params.xpostcopy)
VDMemset32(dst, *(const uint32 *)((const char *)params.src + params.xpostpos), params.xpostcopy);
}
void VDPixmapStretchBiV_XRGB1555_to_XRGB1555(void *dstv, const void *src1v, const void *src2v, sint32 w, uint32 f) {
uint16 *dst = (uint16 *)dstv;
const uint16 *src1 = (const uint16 *)src1v;
const uint16 *src2 = (const uint16 *)src2v;
f >>= 27;
do {
*dst++ = lerp_XRGB1555(*src1++, *src2++, f);
} while(--w);
}
void VDPixmapStretchBiV_XRGB8888_to_XRGB8888(void *dstv, const void *src1v, const void *src2v, sint32 w, uint32 f) {
uint32 *dst = (uint32 *)dstv;
const uint32 *src1 = (const uint32 *)src1v;
const uint32 *src2 = (const uint32 *)src2v;
f >>= 24;
do {
*dst++ = lerp_XRGB8888(*src1++, *src2++, f);
} while(--w);
}
}
#ifdef _M_IX86
extern "C" void vdasm_stretchbltH_XRGB8888_to_XRGB8888_MMX(const VDPixmapReferenceStretchBltBilinearParameters&);
extern "C" void vdasm_stretchbltV_XRGB1555_to_XRGB1555_MMX(void *dstv, const void *src1v, const void *src2v, sint32 w, uint32 f);
extern "C" void vdasm_stretchbltV_XRGB8888_to_XRGB8888_MMX(void *dstv, const void *src1v, const void *src2v, sint32 w, uint32 f);
#endif
bool VDPixmapStretchBltBilinear_reference(const VDPixmap& dst, sint32 x1, sint32 y1, sint32 x2, sint32 y2, const VDPixmap& src, sint32 u1, sint32 v1, sint32 u2, sint32 v2) {
// preemptive clip to prevent gradient calculations from crashing
if (x2 == x1 || y2 == y1)
return true;
// we don't support source clipping
if ((uint32)u1 > (uint32)(src.w << 16) || (uint32)v1 > (uint32)(src.h << 16))
return false;
if ((uint32)u2 > (uint32)(src.w << 16) || (uint32)v2 > (uint32)(src.h << 16))
return false;
// we don't support format changes (yet)
if (dst.format != src.format)
return false;
// format determination
void (*pHorizontalFilter)(const VDPixmapReferenceStretchBltBilinearParameters& params);
void (*pVerticalFilter)(void *dstv, const void *src1v, const void *src2v, sint32 w, uint32 f);
int bpp;
#pragma vdpragma_TODO("fixme this is b0rken")
switch(src.format) {
case nsVDPixmap::kPixFormat_XRGB1555:
pHorizontalFilter = VDPixmapStretchBiH_XRGB1555_to_XRGB1555;
#ifdef _M_IX86
if (CPUGetEnabledExtensions() & CPUF_SUPPORTS_MMX)
pVerticalFilter = vdasm_stretchbltV_XRGB1555_to_XRGB1555_MMX;
else
#endif
pVerticalFilter = VDPixmapStretchBiV_XRGB1555_to_XRGB1555;
bpp = 2;
break;
case nsVDPixmap::kPixFormat_XRGB8888:
#ifdef _M_IX86
if (CPUGetEnabledExtensions() & CPUF_SUPPORTS_MMX) {
pHorizontalFilter = vdasm_stretchbltH_XRGB8888_to_XRGB8888_MMX;
pVerticalFilter = vdasm_stretchbltV_XRGB8888_to_XRGB8888_MMX;
} else
#endif
{
pHorizontalFilter = VDPixmapStretchBiH_XRGB8888_to_XRGB8888;
pVerticalFilter = VDPixmapStretchBiV_XRGB8888_to_XRGB8888;
}
bpp = 4;
break;
default:
return false;
}
// translate destination flips into source flips
if (x1 > x2) {
std::swap(x1, x2);
std::swap(u1, u2);
}
if (y1 > y2) {
std::swap(y1, y2);
std::swap(v1, v2);
}
// compute gradients
sint32 dx = x2 - x1;
sint32 dy = y2 - y1;
sint32 du = u2 - u1;
sint32 dv = v2 - v1;
sint64 dudx = ((sint64)du << 32) / dx; // must truncate toward zero to prevent overflow
sint64 dvdy = ((sint64)dv << 32) / dy;
// prestep top-left point to pixel center and convert destination coordinates to integer
sint64 u64 = (sint64)u1 << 16;
sint64 v64 = (sint64)v1 << 16;
sint32 prestepx = (0x8000 - x1) & 0xffff;
sint32 prestepy = (0x8000 - y1) & 0xffff;
u64 += (dudx * prestepx) >> 16;
v64 += (dvdy * prestepy) >> 16;
sint32 x1i = (x1 + 0x8000) >> 16;
sint32 y1i = (y1 + 0x8000) >> 16;
sint32 x2i = (x2 + 0x8000) >> 16;
sint32 y2i = (y2 + 0x8000) >> 16;
// destination clipping
if (x1i < 0) {
u64 -= dudx * x1i;
x1i = 0;
}
if (y1i < 0) {
v64 -= dvdy * y1i;
y1i = 0;
}
if (x2i > dst.w)
x2i = dst.w;
if (y2i > dst.h)
y2i = dst.h;
if (x1i >= x2i || y1i >= y2i)
return true;
u64 -= 0x80000000;
v64 -= 0x80000000;
int xprepos = 0;
int xpostpos = src.w-1;
sint64 ulo = u64;
sint64 uhi = u64 + dudx * (x2i - x1i - 1);
sint64 tdudx = dudx;
if (ulo > uhi) {
std::swap(ulo, uhi);
tdudx = -tdudx;
}
int xprecopy = 0;
int xpostcopy = 0;
if (ulo < 0) {
xprecopy = (int)((1 - ulo) / tdudx) + 1;
}
const sint64 ulimit = ((sint64)(src.w-1) << 32);
if (uhi >= ulimit)
xpostcopy = (int)((uhi - ulimit - 1) / tdudx) + 1;
if (dudx < 0) {
std::swap(xprecopy, xpostcopy);
std::swap(xprepos, xpostpos);
}
u64 += dudx * xprecopy;
const int xtotal = x2i - x1i;
int xmidcopy = (x2i - x1i) - (xprecopy + xpostcopy);
const sint32 ui = (sint32)(u64 >> 32);
// set up parameter block
VDPixmapReferenceStretchBltBilinearParameters params;
params.u = (uint32)u64;
params.uinc = (sint32)(dudx >> 32);
params.dudx = (sint32)dudx;
params.xprecopy = xprecopy;
params.xprepos = (xprepos - ui) * bpp;
params.xpostcopy = xpostcopy;
params.xpostpos = (xpostpos - ui) * bpp;
params.xmidsize = xmidcopy;
void *dstp = (char *)dst.data + y1i * dst.pitch + x1i * bpp;
const void *srcp = (char *)src.data + ui * bpp;
VDPixmapBuffer window(xtotal, 2, src.format);
void *pTempRow1 = window.data;
void *pTempRow2 = (char *)window.data + window.pitch;
int windowbottom = dvdy > 0 ? -0x7fffffff : 0x7fffffff;
do {
sint32 iv = (sint32)(v64 >> 32);
sint32 iv_bottom = iv + 1;
if (iv < 0)
iv = iv_bottom = 0;
if (iv >= src.h-1)
iv = iv_bottom = src.h-1;
if (dvdy < 0) {
if (windowbottom > iv_bottom+1)
windowbottom = iv_bottom+1;
while(windowbottom > iv) {
std::swap(pTempRow1, pTempRow2);
--windowbottom;
params.dst = (char *)pTempRow1 + bpp * params.xprecopy;
params.src = vdptroffset(srcp, windowbottom * src.pitch);
pHorizontalFilter(params);
}
} else {
if (windowbottom < iv-1)
windowbottom = iv-1;
while(windowbottom < iv_bottom) {
std::swap(pTempRow1, pTempRow2);
++windowbottom;
params.dst = (char *)pTempRow2 + bpp * params.xprecopy;
params.src = vdptroffset(srcp, windowbottom * src.pitch);
pHorizontalFilter(params);
}
}
if (iv == iv_bottom)
if (dvdy < 0)
pVerticalFilter(dstp, pTempRow1, pTempRow1, xtotal, 0);
else
pVerticalFilter(dstp, pTempRow2, pTempRow2, xtotal, 0);
else
pVerticalFilter(dstp, pTempRow1, pTempRow2, xtotal, (uint32)v64);
v64 += dvdy;
dstp = (char *)dstp + dst.pitch;
} while(++y1i < y2i);
return true;
}
|