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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
|
/* gameplaySP
*
* Copyright (C) 2006 Exophase <exophase@gmail.com>
*
* 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
*/
#include "common.h"
struct StringCut {
uint32_t Start; // Starting character index of the cut, inclusive.
uint32_t End; // Ending character index of the cut, exclusive.
};
uint16_t* GBAScreen;
uint32_t GBAScreenPitch = GBA_SCREEN_WIDTH;
volatile unsigned int VideoFastForwarded;
uint_fast8_t AudioFrameskip = 0;
uint_fast8_t AudioFrameskipControl = 0;
uint_fast8_t SufficientAudioControl = 0;
uint_fast8_t UserFrameskipControl = 0;
uint_fast8_t FramesBordered = 0;
SDL_Surface *GBAScreenSurface = NULL;
SDL_Surface *OutputSurface = NULL;
SDL_Surface *BorderSurface = NULL;
video_scale_type PerGameScaleMode = 0;
video_scale_type ScaleMode = scaled_aspect;
#define COLOR_PROGRESS_BACKGROUND RGB888_TO_RGB565( 0, 0, 0)
#define COLOR_PROGRESS_TEXT_CONTENT RGB888_TO_RGB565(255, 255, 255)
#define COLOR_PROGRESS_TEXT_OUTLINE RGB888_TO_RGB565( 0, 0, 0)
#define COLOR_PROGRESS_CONTENT RGB888_TO_RGB565( 0, 128, 255)
#define COLOR_PROGRESS_OUTLINE RGB888_TO_RGB565(255, 255, 255)
#define PROGRESS_WIDTH 240
#define PROGRESS_HEIGHT 18
static bool InFileAction = false;
static enum ReGBA_FileAction CurrentFileAction;
static struct timespec LastProgressUpdate;
void init_video()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
{
printf("Failed to initialize SDL !!\n");
return; // for debug
// exit(1);
}
SDL_ShowCursor(SDL_DISABLE);
OutputSurface = SDL_SetVideoMode(GCW0_SCREEN_WIDTH, GCW0_SCREEN_HEIGHT, 16, SDL_HWSURFACE |
#ifdef SDL_TRIPLEBUF
SDL_TRIPLEBUF
#else
SDL_DOUBLEBUF
#endif
);
GBAScreenSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, 16,
GBA_RED_MASK,
GBA_GREEN_MASK,
GBA_BLUE_MASK,
0 /* alpha: none */);
GBAScreen = (uint16_t*) GBAScreenSurface->pixels;
}
void SetMenuResolution()
{
#ifdef GCW_ZERO
if (SDL_MUSTLOCK(OutputSurface))
SDL_UnlockSurface(OutputSurface);
OutputSurface = SDL_SetVideoMode(GCW0_SCREEN_WIDTH, GCW0_SCREEN_HEIGHT, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (SDL_MUSTLOCK(OutputSurface))
SDL_LockSurface(OutputSurface);
#endif
}
void SetGameResolution()
{
#ifdef GCW_ZERO
video_scale_type ResolvedScaleMode = ResolveSetting(ScaleMode, PerGameScaleMode);
unsigned int Width = GBA_SCREEN_WIDTH, Height = GBA_SCREEN_HEIGHT;
if (ResolvedScaleMode != hardware)
{
Width = GCW0_SCREEN_WIDTH;
Height = GCW0_SCREEN_HEIGHT;
}
if (SDL_MUSTLOCK(OutputSurface))
SDL_UnlockSurface(OutputSurface);
OutputSurface = SDL_SetVideoMode(Width, Height, 16, SDL_HWSURFACE |
#ifdef SDL_TRIPLEBUF
SDL_TRIPLEBUF
#else
SDL_DOUBLEBUF
#endif
);
if (SDL_MUSTLOCK(OutputSurface))
SDL_LockSurface(OutputSurface);
#endif
}
bool ApplyBorder(const char* Filename)
{
SDL_Surface* JustLoaded = loadPNG(Filename, GCW0_SCREEN_WIDTH, GCW0_SCREEN_HEIGHT);
bool Result = false;
if (JustLoaded != NULL)
{
if (JustLoaded->w == GCW0_SCREEN_WIDTH && JustLoaded->h == GCW0_SCREEN_HEIGHT)
{
if (BorderSurface != NULL)
{
SDL_FreeSurface(BorderSurface);
BorderSurface = NULL;
}
BorderSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, GCW0_SCREEN_WIDTH, GCW0_SCREEN_HEIGHT, 16,
OutputSurface->format->Rmask,
OutputSurface->format->Gmask,
OutputSurface->format->Bmask,
OutputSurface->format->Amask);
SDL_BlitSurface(JustLoaded, NULL, BorderSurface, NULL);
Result = true;
}
SDL_FreeSurface(JustLoaded);
JustLoaded = NULL;
}
return Result;
}
/***************************************************************************
* Scaler copyright (C) 2013 by Paul Cercueil *
* paul@crapouillou.net *
***************************************************************************/
static inline uint32_t bgr555_to_rgb565(uint32_t px)
{
return ((px & 0x7c007c00) >> 10)
| ((px & 0x03e003e0) << 1)
| ((px & 0x001f001f) << 11);
}
/***************************************************************************
* 16-bit I/O version used by the sub-pixel and bilinear scalers *
* (C) 2013 kuwanger *
***************************************************************************/
static inline uint16_t bgr555_to_rgb565_16(uint16_t px)
{
return ((px & 0x7c00) >> 10)
| ((px & 0x03e0) << 1)
| ((px & 0x001f) << 11);
}
// Explaining the magic constants:
// F7DEh is the mask to remove the lower bit of all color
// components before dividing them by 2. Otherwise, the lower bit
// would bleed into the high bit of the next component.
// RRRRR GGGGGG BBBBB RRRRR GGGGGG BBBBB
// 11110 111110 11110 [>> 1] 01111 011111 01111
// 0821h is the mask to gather the low bits again for averaging
// after discarding them.
// RRRRR GGGGGG BBBBB RRRRR GGGGGG BBBBB
// 00001 000001 00001 [+ X] 00010 000010 00010
// E79Ch is the mask to remove the lower 2 bits of all color
// components before dividing them by 4. Otherwise, the lower bits
// would bleed into the high bits of the next component.
// RRRRR GGGGGG BBBBB RRRRR GGGGGG BBBBB
// 11100 111100 11100 [>> 2] 00111 001111 00111
// 1863h is the mask to gather the low bits again for averaging
// after discarding them.
// RRRRR GGGGGG BBBBB RRRRR GGGGGG BBBBB
// 00011 000011 00011 [+ X] 00110 000110 00110
/* Calculates the average of two RGB565 pixels. The source of the pixels is
* the lower 16 bits of both parameters. The result is in the lower 16 bits.
*/
#define Average(A, B) ((((A) & 0xF7DE) >> 1) + (((B) & 0xF7DE) >> 1) + ((A) & (B) & 0x0821))
/* Calculates the average of two pairs of RGB565 pixels. The result is, in
* the lower bits, the average of both lower pixels, and in the upper bits,
* the average of both upper pixels. */
#define Average32(A, B) ((((A) & 0xF7DEF7DE) >> 1) + (((B) & 0xF7DEF7DE) >> 1) + ((A) & (B) & 0x08210821))
/* Raises a pixel from the lower half to the upper half of a pair. */
#define Raise(N) ((N) << 16)
/* Extracts the upper pixel of a pair into the lower pixel of a pair. */
#define Hi(N) ((N) >> 16)
/* Extracts the lower pixel of a pair. */
#define Lo(N) ((N) & 0xFFFF)
/* Calculates the average of two RGB565 pixels. The source of the pixels is
* the lower 16 bits of both parameters. The result is in the lower 16 bits.
* The average is weighted so that the first pixel contributes 3/4 of its
* color and the second pixel contributes 1/4. */
#define AverageQuarters3_1(A, B) ( (((A) & 0xF7DE) >> 1) + (((A) & 0xE79C) >> 2) + (((B) & 0xE79C) >> 2) + ((( (( ((A) & 0x1863) + ((A) & 0x0821) ) << 1) + ((B) & 0x1863) ) >> 2) & 0x1863) )
#define RED_FROM_RGB565(rgb565) (((rgb565) >> 11) & 0x1F)
#define RED_TO_RGB565(r) (((r) & 0x1F) << 11)
#define GREEN_FROM_RGB565(rgb565) (((rgb565) >> 5) & 0x3F)
#define GREEN_TO_RGB565(g) (((g) & 0x3F) << 5)
#define BLUE_FROM_RGB565(rgb565) ((rgb565) & 0x1F)
#define BLUE_TO_RGB565(b) ((b) & 0x1F)
/*
* Blends, with sub-pixel accuracy, 3/4 of the first argument with 1/4 of the
* second argument. The pixel format of both arguments is RGB 565. The first
* pixel is assumed to be to the left of the second pixel.
*/
static inline uint16_t SubpixelRGB3_1(uint16_t A, uint16_t B)
{
return RED_TO_RGB565(RED_FROM_RGB565(A))
| GREEN_TO_RGB565(GREEN_FROM_RGB565(A) * 3 / 4 + GREEN_FROM_RGB565(B) * 1 / 4)
| BLUE_TO_RGB565(BLUE_FROM_RGB565(A) * 1 / 4 + BLUE_FROM_RGB565(B) * 3 / 4);
}
/*
* Blends, with sub-pixel accuracy, 1/2 of the first argument with 1/2 of the
* second argument. The pixel format of both arguments is RGB 565. The first
* pixel is assumed to be to the left of the second pixel.
*/
static inline uint16_t SubpixelRGB1_1(uint16_t A, uint16_t B)
{
return RED_TO_RGB565(RED_FROM_RGB565(A) * 3 / 4 + RED_FROM_RGB565(B) * 1 / 4)
| GREEN_TO_RGB565(GREEN_FROM_RGB565(A) * 1 / 2 + GREEN_FROM_RGB565(B) * 1 / 2)
| BLUE_TO_RGB565(BLUE_FROM_RGB565(A) * 1 / 4 + BLUE_FROM_RGB565(B) * 3 / 4);
}
/*
* Blends, with sub-pixel accuracy, 1/4 of the first argument with 3/4 of the
* second argument. The pixel format of both arguments is RGB 565. The first
* pixel is assumed to be to the left of the second pixel.
*/
static inline uint16_t SubpixelRGB1_3(uint16_t A, uint16_t B)
{
return RED_TO_RGB565(RED_FROM_RGB565(B) * 1 / 4 + RED_FROM_RGB565(A) * 3 / 4)
| GREEN_TO_RGB565(GREEN_FROM_RGB565(B) * 3 / 4 + GREEN_FROM_RGB565(A) * 1 / 4)
| BLUE_TO_RGB565(BLUE_FROM_RGB565(B));
}
/* Upscales an image by 33% in width and 50% in height; also does color
* conversion using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 3/2)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
/* Before:
* a b c d e f
* g h i j k l
*
* After (multiple letters = average):
* a ab bc c d de ef f
* ag abgh bchi ci dj dejk efkl fl
* g gh hi i j jk kl l
*/
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint32_t x, y;
for (y = 0; y < src_y; y += 2) {
for (x = 0; x < src_x / 6; x++) {
// -- Row 1 --
// Read RGB565 elements in the source grid.
// The notation is high_low (little-endian).
uint32_t b_a = bgr555_to_rgb565(*(uint32_t*) (from )),
d_c = bgr555_to_rgb565(*(uint32_t*) (from + 2)),
f_e = bgr555_to_rgb565(*(uint32_t*) (from + 4));
// Generate ab_a from b_a.
*(uint32_t*) (to) = likely(Hi(b_a) == Lo(b_a))
? b_a
: Lo(b_a) /* 'a' verbatim to low pixel */ |
Raise(Average(Hi(b_a), Lo(b_a))) /* ba to high pixel */;
// Generate c_bc from b_a and d_c.
*(uint32_t*) (to + 2) = likely(Hi(b_a) == Lo(d_c))
? Lo(d_c) | Raise(Lo(d_c))
: Raise(Lo(d_c)) /* 'c' verbatim to high pixel */ |
Average(Lo(d_c), Hi(b_a)) /* bc to low pixel */;
// Generate de_d from d_c and f_e.
*(uint32_t*) (to + 4) = likely(Hi(d_c) == Lo(f_e))
? Lo(f_e) | Raise(Lo(f_e))
: Hi(d_c) /* 'd' verbatim to low pixel */ |
Raise(Average(Lo(f_e), Hi(d_c))) /* de to high pixel */;
// Generate f_ef from f_e.
*(uint32_t*) (to + 6) = likely(Hi(f_e) == Lo(f_e))
? f_e
: Raise(Hi(f_e)) /* 'f' verbatim to high pixel */ |
Average(Hi(f_e), Lo(f_e)) /* ef to low pixel */;
if (likely(y + 1 < src_y)) // Is there a source row 2?
{
// -- Row 2 --
uint32_t h_g = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch )),
j_i = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch + 4)),
l_k = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch + 8));
// Generate abgh_ag from b_a and h_g.
uint32_t bh_ag = Average32(b_a, h_g);
*(uint32_t*) ((uint8_t*) to + dst_pitch) = likely(Hi(bh_ag) == Lo(bh_ag))
? bh_ag
: Lo(bh_ag) /* ag verbatim to low pixel */ |
Raise(Average(Hi(bh_ag), Lo(bh_ag))) /* abgh to high pixel */;
// Generate ci_bchi from b_a, d_c, h_g and j_i.
uint32_t ci_bh =
Hi(bh_ag) /* bh verbatim to low pixel */ |
Raise(Average(Lo(d_c), Lo(j_i))) /* ci to high pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch + 4) = likely(Hi(ci_bh) == Lo(ci_bh))
? ci_bh
: Raise(Hi(ci_bh)) /* ci verbatim to high pixel */ |
Average(Hi(ci_bh), Lo(ci_bh)) /* bchi to low pixel */;
// Generate fl_efkl from f_e and l_k.
uint32_t fl_ek = Average32(f_e, l_k);
*(uint32_t*) ((uint8_t*) to + dst_pitch + 12) = likely(Hi(fl_ek) == Lo(fl_ek))
? fl_ek
: Raise(Hi(fl_ek)) /* fl verbatim to high pixel */ |
Average(Hi(fl_ek), Lo(fl_ek)) /* efkl to low pixel */;
// Generate dejk_dj from d_c, f_e, j_i and l_k.
uint32_t ek_dj =
Raise(Lo(fl_ek)) /* ek verbatim to high pixel */ |
Average(Hi(d_c), Hi(j_i)) /* dj to low pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch + 8) = likely(Hi(ek_dj) == Lo(ek_dj))
? ek_dj
: Lo(ek_dj) /* dj verbatim to low pixel */ |
Raise(Average(Hi(ek_dj), Lo(ek_dj))) /* dejk to high pixel */;
// -- Row 3 --
// Generate gh_g from h_g.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2) = likely(Hi(h_g) == Lo(h_g))
? h_g
: Lo(h_g) /* 'g' verbatim to low pixel */ |
Raise(Average(Hi(h_g), Lo(h_g))) /* gh to high pixel */;
// Generate i_hi from g_h and j_i.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = likely(Hi(h_g) == Lo(j_i))
? Lo(j_i) | Raise(Lo(j_i))
: Raise(Lo(j_i)) /* 'i' verbatim to high pixel */ |
Average(Lo(j_i), Hi(h_g)) /* hi to low pixel */;
// Generate jk_j from j_i and l_k.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 8) = likely(Hi(j_i) == Lo(l_k))
? Lo(l_k) | Raise(Lo(l_k))
: Hi(j_i) /* 'j' verbatim to low pixel */ |
Raise(Average(Hi(j_i), Lo(l_k))) /* jk to high pixel */;
// Generate l_kl from l_k.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 12) = likely(Hi(l_k) == Lo(l_k))
? l_k
: Raise(Hi(l_k)) /* 'l' verbatim to high pixel */ |
Average(Hi(l_k), Lo(l_k)) /* kl to low pixel */;
}
from += 6;
to += 8;
}
// Skip past the waste at the end of the first line, if any,
// then past 1 whole lines of source and 2 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 2 * dst_pitch);
}
}
/* Upscales an image by 33% in width and in height; also does color conversion
* using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 4/3)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale_aspect(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
/* Before:
* a b c d e f
* g h i j k l
* m n o p q r
*
* After (multiple letters = average):
* a ab bc c d de ef f
* ag abgh bchi ci dj dejk efkl fl
* gm ghmn hino io jp jkpq klqr lr
* m mn no o p pq qr r
*/
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint32_t x, y;
for (y = 0; y < src_y; y += 3) {
for (x = 0; x < src_x / 6; x++) {
// -- Row 1 --
// Read RGB565 elements in the source grid.
// The notation is high_low (little-endian).
uint32_t b_a = bgr555_to_rgb565(*(uint32_t*) (from )),
d_c = bgr555_to_rgb565(*(uint32_t*) (from + 2)),
f_e = bgr555_to_rgb565(*(uint32_t*) (from + 4));
// Generate ab_a from b_a.
*(uint32_t*) (to) = likely(Hi(b_a) == Lo(b_a))
? b_a
: Lo(b_a) /* 'a' verbatim to low pixel */ |
Raise(Average(Hi(b_a), Lo(b_a))) /* ba to high pixel */;
// Generate c_bc from b_a and d_c.
*(uint32_t*) (to + 2) = likely(Hi(b_a) == Lo(d_c))
? Lo(d_c) | Raise(Lo(d_c))
: Raise(Lo(d_c)) /* 'c' verbatim to high pixel */ |
Average(Lo(d_c), Hi(b_a)) /* bc to low pixel */;
// Generate de_d from d_c and f_e.
*(uint32_t*) (to + 4) = likely(Hi(d_c) == Lo(f_e))
? Lo(f_e) | Raise(Lo(f_e))
: Hi(d_c) /* 'd' verbatim to low pixel */ |
Raise(Average(Lo(f_e), Hi(d_c))) /* de to high pixel */;
// Generate f_ef from f_e.
*(uint32_t*) (to + 6) = likely(Hi(f_e) == Lo(f_e))
? f_e
: Raise(Hi(f_e)) /* 'f' verbatim to high pixel */ |
Average(Hi(f_e), Lo(f_e)) /* ef to low pixel */;
if (likely(y + 1 < src_y)) // Is there a source row 2?
{
// -- Row 2 --
uint32_t h_g = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch )),
j_i = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch + 4)),
l_k = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch + 8));
// Generate abgh_ag from b_a and h_g.
uint32_t bh_ag = Average32(b_a, h_g);
*(uint32_t*) ((uint8_t*) to + dst_pitch) = likely(Hi(bh_ag) == Lo(bh_ag))
? bh_ag
: Lo(bh_ag) /* ag verbatim to low pixel */ |
Raise(Average(Hi(bh_ag), Lo(bh_ag))) /* abgh to high pixel */;
// Generate ci_bchi from b_a, d_c, h_g and j_i.
uint32_t ci_bh =
Hi(bh_ag) /* bh verbatim to low pixel */ |
Raise(Average(Lo(d_c), Lo(j_i))) /* ci to high pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch + 4) = likely(Hi(ci_bh) == Lo(ci_bh))
? ci_bh
: Raise(Hi(ci_bh)) /* ci verbatim to high pixel */ |
Average(Hi(ci_bh), Lo(ci_bh)) /* bchi to low pixel */;
// Generate fl_efkl from f_e and l_k.
uint32_t fl_ek = Average32(f_e, l_k);
*(uint32_t*) ((uint8_t*) to + dst_pitch + 12) = likely(Hi(fl_ek) == Lo(fl_ek))
? fl_ek
: Raise(Hi(fl_ek)) /* fl verbatim to high pixel */ |
Average(Hi(fl_ek), Lo(fl_ek)) /* efkl to low pixel */;
// Generate dejk_dj from d_c, f_e, j_i and l_k.
uint32_t ek_dj =
Raise(Lo(fl_ek)) /* ek verbatim to high pixel */ |
Average(Hi(d_c), Hi(j_i)) /* dj to low pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch + 8) = likely(Hi(ek_dj) == Lo(ek_dj))
? ek_dj
: Lo(ek_dj) /* dj verbatim to low pixel */ |
Raise(Average(Hi(ek_dj), Lo(ek_dj))) /* dejk to high pixel */;
if (likely(y + 2 < src_y)) // Is there a source row 3?
{
// -- Row 3 --
uint32_t n_m = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch * 2 )),
p_o = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch * 2 + 4)),
r_q = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) from + src_pitch * 2 + 8));
// Generate ghmn_gm from h_g and n_m.
uint32_t hn_gm = Average32(h_g, n_m);
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2) = likely(Hi(hn_gm) == Lo(hn_gm))
? hn_gm
: Lo(hn_gm) /* gm verbatim to low pixel */ |
Raise(Average(Hi(hn_gm), Lo(hn_gm))) /* ghmn to high pixel */;
// Generate io_hino from h_g, j_i, n_m and p_o.
uint32_t io_hn =
Hi(hn_gm) /* hn verbatim to low pixel */ |
Raise(Average(Lo(j_i), Lo(p_o))) /* io to high pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = likely(Hi(io_hn) == Lo(io_hn))
? io_hn
: Raise(Hi(io_hn)) /* io verbatim to high pixel */ |
Average(Hi(io_hn), Lo(io_hn)) /* hino to low pixel */;
// Generate lr_klqr from l_k and r_q.
uint32_t lr_kq = Average32(l_k, r_q);
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 12) = likely(Hi(lr_kq) == Lo(lr_kq))
? lr_kq
: Raise(Hi(lr_kq)) /* lr verbatim to high pixel */ |
Average(Hi(lr_kq), Lo(lr_kq)) /* klqr to low pixel */;
// Generate jkpq_jp from j_i, l_k, p_o and r_q.
uint32_t kq_jp =
Raise(Lo(lr_kq)) /* kq verbatim to high pixel */ |
Average(Hi(j_i), Hi(p_o)) /* jp to low pixel */;
*(uint32_t*) ((uint8_t*) to + dst_pitch * 2 + 8) = likely(Hi(kq_jp) == Lo(kq_jp))
? kq_jp
: Lo(kq_jp) /* jp verbatim to low pixel */ |
Raise(Average(Hi(kq_jp), Lo(kq_jp))) /* jkpq to high pixel */;
// -- Row 4 --
// Generate mn_m from n_m.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 3) = likely(Hi(n_m) == Lo(n_m))
? n_m
: Lo(n_m) /* 'm' verbatim to low pixel */ |
Raise(Average(Hi(n_m), Lo(n_m))) /* mn to high pixel */;
// Generate o_no from n_m and p_o.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 3 + 4) = likely(Hi(n_m) == Lo(p_o))
? Lo(p_o) | Raise(Lo(p_o))
: Raise(Lo(p_o)) /* 'o' verbatim to high pixel */ |
Average(Lo(p_o), Hi(n_m)) /* no to low pixel */;
// Generate pq_p from p_o and r_q.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 3 + 8) = likely(Hi(p_o) == Lo(r_q))
? Lo(r_q) | Raise(Lo(r_q))
: Hi(p_o) /* 'p' verbatim to low pixel */ |
Raise(Average(Hi(p_o), Lo(r_q))) /* pq to high pixel */;
// Generate r_qr from r_q.
*(uint32_t*) ((uint8_t*) to + dst_pitch * 3 + 12) = likely(Hi(r_q) == Lo(r_q))
? r_q
: Raise(Hi(r_q)) /* 'r' verbatim to high pixel */ |
Average(Hi(r_q), Lo(r_q)) /* qr to low pixel */;
}
}
from += 6;
to += 8;
}
// Skip past the waste at the end of the first line, if any,
// then past 2 whole lines of source and 3 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + 2 * src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 3 * dst_pitch);
}
}
/* Upscales an image based on subpixel rendering; also does color conversion
* using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 3/2)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale_subpixel(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint_fast16_t sectY;
for (sectY = 0; sectY < src_y / 2; sectY++)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section.
*
* a b c | d
* e f g | h
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 --
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: SubpixelRGB1_3(a, b);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: SubpixelRGB1_1(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: SubpixelRGB3_1(c, d);
// -- Row 2 --
// All pixels in this row are blended from the two rows.
uint32_t e = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch )),
f = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 2)),
g = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 4)),
h = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + rightCol));
// -- Row 2 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch) = likely(a == e)
? a
: Average(a, e);
// -- Row 2 pixel 2 (X = 0.75) --
uint16_t e1f3 = likely(e == f)
? e
: SubpixelRGB1_3(e, f);
uint16_t a1b3 = likely(a == b)
? a
: SubpixelRGB1_3(a, b);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 2) = likely(a1b3 == e1f3)
? a1b3
: Average(a1b3, e1f3);
// -- Row 2 pixel 3 (X = 1.5) --
uint16_t fg = likely(f == g)
? f
: SubpixelRGB1_1(f, g);
uint16_t bc = likely(b == c)
? b
: SubpixelRGB1_1(b, c);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 4) = likely(bc == fg)
? bc
: Average(bc, fg);
// -- Row 2 pixel 4 (X = 2.25) --
uint16_t g3h1 = likely(g == h)
? g
: SubpixelRGB3_1(g, h);
uint16_t c3d1 = likely(c == d)
? c
: SubpixelRGB3_1(c, d);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 6) = /* in Y */ likely(g3h1 == c3d1)
? c3d1
: Average(c3d1, g3h1);
// -- Row 3 --
// -- Row 3 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2) = e;
// -- Row 3 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 2) = likely(e == f)
? e
: SubpixelRGB1_3(e, f);
// -- Row 3 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = likely(f == g)
? f
: SubpixelRGB1_1(f, g);
// -- Row 3 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 6) = likely(g == h)
? g
: SubpixelRGB3_1(g, h);
from += 3;
to += 4;
}
// Skip past the waste at the end of the first line, if any,
// then past 1 whole lines of source and 2 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + 1 * src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 2 * dst_pitch);
}
}
/* Upscales an image by 33% in width and in height, based on subpixel
* rendering; also does color conversion using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 4/3)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale_aspect_subpixel(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint_fast16_t sectY;
for (sectY = 0; sectY < src_y / 3; sectY++)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section. The last row does the same thing.
*
* a b c | d
* e f g | h
* i j k | l
* ---------
* m n o | p
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 --
// All pixels in this row use 0 as the Y coordinate.
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: SubpixelRGB1_3(a, b);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: SubpixelRGB1_1(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: SubpixelRGB3_1(c, d);
// -- Row 2 --
// All pixels in this row use 0.75 as the Y coordinate.
uint32_t e = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch )),
f = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 2)),
g = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 4)),
h = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + rightCol));
// -- Row 2 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch) = likely(a == e)
? a
: AverageQuarters3_1(e, a);
// -- Row 2 pixel 2 (X = 0.75) --
uint16_t e1f3 = likely(e == f)
? e
: SubpixelRGB1_3(e, f);
uint16_t a1b3 = likely(a == b)
? a
: SubpixelRGB1_3(a, b);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 2) = /* in Y */ likely(a1b3 == e1f3)
? a1b3
: AverageQuarters3_1(/* in X, bottom */ e1f3, /* in X, top */ a1b3);
// -- Row 2 pixel 3 (X = 1.5) --
uint16_t fg = likely(f == g)
? f
: SubpixelRGB1_1(f, g);
uint16_t bc = likely(b == c)
? b
: SubpixelRGB1_1(b, c);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 4) = /* in Y */ likely(bc == fg)
? bc
: AverageQuarters3_1(/* in X, bottom */ fg, /* in X, top */ bc);
// -- Row 2 pixel 4 (X = 2.25) --
uint16_t g3h1 = likely(g == h)
? g
: SubpixelRGB3_1(g, h);
uint16_t c3d1 = likely(c == d)
? c
: SubpixelRGB3_1(c, d);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 6) = /* in Y */ likely(g3h1 == c3d1)
? c3d1
: AverageQuarters3_1(/* in X, bottom */ g3h1, /* in X, top */ c3d1);
// -- Row 3 --
// All pixels in this row use 1.5 as the Y coordinate.
uint32_t i = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 )),
j = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + 2)),
k = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + 4)),
l = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + rightCol));
// -- Row 3 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2) = likely(e == i)
? e
: Average(e, i);
// -- Row 3 pixel 2 (X = 0.75) --
uint16_t i1j3 = likely(i == j)
? i
: SubpixelRGB1_3(i, j);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 2) = /* in Y */ likely(e1f3 == i1j3)
? e1f3
: Average(e1f3, i1j3);
// -- Row 3 pixel 3 (X = 1.5) --
uint16_t jk = likely(j == k)
? j
: SubpixelRGB1_1(j, k);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = /* in Y */ likely(fg == jk)
? fg
: Average(fg, jk);
// -- Row 3 pixel 4 (X = 2.25) --
uint16_t k3l1 = likely(k == l)
? k
: SubpixelRGB3_1(k, l);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 6) = /* in Y */ likely(g3h1 == k3l1)
? g3h1
: Average(g3h1, k3l1);
// -- Row 4 --
// All pixels in this row use 2.25 as the Y coordinate.
uint32_t m = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 )),
n = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + 2)),
o = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + 4)),
p = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + rightCol));
// -- Row 4 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3) = likely(i == m)
? i
: AverageQuarters3_1(i, m);
// -- Row 4 pixel 2 (X = 0.75) --
uint16_t m1n3 = likely(m == n)
? m
: SubpixelRGB1_3(m, n);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 2) = /* in Y */ likely(i1j3 == m1n3)
? i1j3
: AverageQuarters3_1(/* in X, top */ i1j3, /* in X, bottom */ m1n3);
// -- Row 4 pixel 3 (X = 1.5) --
uint16_t no = likely(n == o)
? n
: SubpixelRGB1_1(n, o);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 4) = /* in Y */ likely(jk == no)
? jk
: AverageQuarters3_1(/* in X, top */ jk, /* in X, bottom */ no);
// -- Row 4 pixel 4 (X = 2.25) --
uint16_t o3p1 = likely(o == p)
? o
: SubpixelRGB3_1(o, p);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 6) = /* in Y */ likely(k3l1 == o3p1)
? k3l1
: AverageQuarters3_1(/* in X, top */ k3l1, /* in X, bottom */ o3p1);
from += 3;
to += 4;
}
// Skip past the waste at the end of the first line, if any,
// then past 2 whole lines of source and 3 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + 2 * src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 3 * dst_pitch);
}
if (src_y % 3 == 1)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section. The last row does the same thing.
*
* a b c | d
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: AverageQuarters3_1(b, a);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: Average(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: AverageQuarters3_1(c, d);
from += 3;
to += 4;
}
}
}
/* Upscales an image by 33% in width and in height with bilinear filtering;
* also does color conversion using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 4/3)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale_aspect_bilinear(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint_fast16_t sectY;
for (sectY = 0; sectY < src_y / 3; sectY++)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section. The last row does the same thing.
*
* a b c | d
* e f g | h
* i j k | l
* ---------
* m n o | p
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 --
// All pixels in this row use 0 as the Y coordinate.
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: AverageQuarters3_1(b, a);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: Average(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: AverageQuarters3_1(c, d);
// -- Row 2 --
// All pixels in this row use 0.75 as the Y coordinate.
uint32_t e = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch )),
f = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 2)),
g = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 4)),
h = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + rightCol));
// -- Row 2 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch) = likely(a == e)
? a
: AverageQuarters3_1(e, a);
// -- Row 2 pixel 2 (X = 0.75) --
uint16_t e1f3 = likely(e == f)
? e
: AverageQuarters3_1(f, e);
uint16_t a1b3 = likely(a == b)
? a
: AverageQuarters3_1(b, a);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 2) = /* in Y */ likely(a1b3 == e1f3)
? a1b3
: AverageQuarters3_1(/* in X, bottom */ e1f3, /* in X, top */ a1b3);
// -- Row 2 pixel 3 (X = 1.5) --
uint16_t fg = likely(f == g)
? f
: Average(f, g);
uint16_t bc = likely(b == c)
? b
: Average(b, c);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 4) = /* in Y */ likely(bc == fg)
? bc
: AverageQuarters3_1(/* in X, bottom */ fg, /* in X, top */ bc);
// -- Row 2 pixel 4 (X = 2.25) --
uint16_t g3h1 = likely(g == h)
? g
: AverageQuarters3_1(g, h);
uint16_t c3d1 = likely(c == d)
? c
: AverageQuarters3_1(c, d);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 6) = /* in Y */ likely(g3h1 == c3d1)
? c3d1
: AverageQuarters3_1(/* in X, bottom */ g3h1, /* in X, top */ c3d1);
// -- Row 3 --
// All pixels in this row use 1.5 as the Y coordinate.
uint32_t i = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 )),
j = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + 2)),
k = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + 4)),
l = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 2 + rightCol));
// -- Row 3 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2) = likely(e == i)
? e
: Average(e, i);
// -- Row 3 pixel 2 (X = 0.75) --
uint16_t i1j3 = likely(i == j)
? i
: AverageQuarters3_1(j, i);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 2) = /* in Y */ likely(e1f3 == i1j3)
? e1f3
: Average(e1f3, i1j3);
// -- Row 3 pixel 3 (X = 1.5) --
uint16_t jk = likely(j == k)
? j
: Average(j, k);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = /* in Y */ likely(fg == jk)
? fg
: Average(fg, jk);
// -- Row 3 pixel 4 (X = 2.25) --
uint16_t k3l1 = likely(k == l)
? k
: AverageQuarters3_1(k, l);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 6) = /* in Y */ likely(g3h1 == k3l1)
? g3h1
: Average(g3h1, k3l1);
// -- Row 4 --
// All pixels in this row use 2.25 as the Y coordinate.
uint32_t m = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 )),
n = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + 2)),
o = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + 4)),
p = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch * 3 + rightCol));
// -- Row 4 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3) = likely(i == m)
? i
: AverageQuarters3_1(i, m);
// -- Row 4 pixel 2 (X = 0.75) --
uint16_t m1n3 = likely(m == n)
? m
: AverageQuarters3_1(n, m);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 2) = /* in Y */ likely(i1j3 == m1n3)
? i1j3
: AverageQuarters3_1(/* in X, top */ i1j3, /* in X, bottom */ m1n3);
// -- Row 4 pixel 3 (X = 1.5) --
uint16_t no = likely(n == o)
? n
: Average(n, o);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 4) = /* in Y */ likely(jk == no)
? jk
: AverageQuarters3_1(/* in X, top */ jk, /* in X, bottom */ no);
// -- Row 4 pixel 4 (X = 2.25) --
uint16_t o3p1 = likely(o == p)
? o
: AverageQuarters3_1(o, p);
*(uint16_t*) ((uint8_t*) to + dst_pitch * 3 + 6) = /* in Y */ likely(k3l1 == o3p1)
? k3l1
: AverageQuarters3_1(/* in X, top */ k3l1, /* in X, bottom */ o3p1);
from += 3;
to += 4;
}
// Skip past the waste at the end of the first line, if any,
// then past 2 whole lines of source and 3 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + 2 * src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 3 * dst_pitch);
}
if (src_y % 3 == 1)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section. The last row does the same thing.
*
* a b c | d
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: AverageQuarters3_1(b, a);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: Average(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: AverageQuarters3_1(c, d);
from += 3;
to += 4;
}
}
}
/* Upscales an image by 33% in width and 50% in height with bilinear
* filtering; also does color conversion using the function above.
* Input:
* from: A pointer to the pixels member of a src_x by src_y surface to be
* read by this function. The pixel format of this surface is XBGR 1555.
* src_x: The width of the source.
* src_y: The height of the source.
* src_pitch: The number of bytes making up a scanline in the source
* surface.
* dst_pitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* to: A pointer to the pixels member of a (src_x * 4/3) by (src_y * 3/2)
* surface to be filled with the upscaled GBA image. The pixel format of
* this surface is RGB 565.
*/
static inline void gba_upscale_bilinear(uint16_t *to, uint16_t *from,
uint32_t src_x, uint32_t src_y, uint32_t src_pitch, uint32_t dst_pitch)
{
const uint32_t dst_x = src_x * 4 / 3;
const uint32_t src_skip = src_pitch - src_x * sizeof(uint16_t),
dst_skip = dst_pitch - dst_x * sizeof(uint16_t);
uint_fast16_t sectY;
for (sectY = 0; sectY < src_y / 2; sectY++)
{
uint_fast16_t sectX;
for (sectX = 0; sectX < src_x / 3; sectX++)
{
uint_fast16_t rightCol = (sectX == src_x / 3 - 1) ? 4 : 6;
/* Read RGB565 elements in the source grid.
* The last column blends with the first column of the next
* section.
*
* a b c | d
* e f g | h
*/
uint32_t a = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from )),
b = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 2)),
c = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + 4)),
d = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + rightCol));
// The 4 output pixels in a row use 0.75, 1.5 then 2.25 as the X
// coordinate for interpolation.
// -- Row 1 --
// -- Row 1 pixel 1 (X = 0) --
*to = a;
// -- Row 1 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + 2) = likely(a == b)
? a
: AverageQuarters3_1(b, a);
// -- Row 1 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + 4) = likely(b == c)
? b
: Average(b, c);
// -- Row 1 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + 6) = likely(c == d)
? c
: AverageQuarters3_1(c, d);
// -- Row 2 --
// All pixels in this row are blended from the two rows.
uint32_t e = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch )),
f = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 2)),
g = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + 4)),
h = bgr555_to_rgb565_16(*(uint16_t*) ((uint8_t*) from + src_pitch + rightCol));
// -- Row 2 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch) = likely(a == e)
? a
: Average(a, e);
// -- Row 2 pixel 2 (X = 0.75) --
uint16_t e1f3 = likely(e == f)
? e
: AverageQuarters3_1(f, e);
uint16_t a1b3 = likely(a == b)
? a
: AverageQuarters3_1(b, a);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 2) = likely(a1b3 == e1f3)
? a1b3
: Average(a1b3, e1f3);
// -- Row 2 pixel 3 (X = 1.5) --
uint16_t fg = likely(f == g)
? f
: Average(f, g);
uint16_t bc = likely(b == c)
? b
: Average(b, c);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 4) = likely(bc == fg)
? bc
: Average(bc, fg);
// -- Row 2 pixel 4 (X = 2.25) --
uint16_t g3h1 = likely(g == h)
? g
: AverageQuarters3_1(g, h);
uint16_t c3d1 = likely(c == d)
? c
: AverageQuarters3_1(c, d);
*(uint16_t*) ((uint8_t*) to + dst_pitch + 6) = /* in Y */ likely(g3h1 == c3d1)
? c3d1
: Average(c3d1, g3h1);
// -- Row 3 --
// -- Row 3 pixel 1 (X = 0) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2) = e;
// -- Row 3 pixel 2 (X = 0.75) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 2) = likely(e == f)
? e
: AverageQuarters3_1(f, e);
// -- Row 3 pixel 3 (X = 1.5) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 4) = likely(f == g)
? f
: Average(f, g);
// -- Row 3 pixel 4 (X = 2.25) --
*(uint16_t*) ((uint8_t*) to + dst_pitch * 2 + 6) = likely(g == h)
? g
: AverageQuarters3_1(g, h);
from += 3;
to += 4;
}
// Skip past the waste at the end of the first line, if any,
// then past 1 whole lines of source and 2 of destination.
from = (uint16_t*) ((uint8_t*) from + src_skip + 1 * src_pitch);
to = (uint16_t*) ((uint8_t*) to + dst_skip + 2 * dst_pitch);
}
}
static inline void gba_render(uint16_t* Dest, uint16_t* Src,
uint32_t SrcPitch, uint32_t DestPitch)
{
Dest = (uint16_t*) ((uint8_t*) Dest
+ ((GCW0_SCREEN_HEIGHT - GBA_SCREEN_HEIGHT) / 2 * DestPitch)
+ ((GCW0_SCREEN_WIDTH - GBA_SCREEN_WIDTH) / 2 * sizeof(uint16_t))
);
uint32_t SrcSkip = SrcPitch - GBA_SCREEN_WIDTH * sizeof(uint16_t);
uint32_t DestSkip = DestPitch - GBA_SCREEN_WIDTH * sizeof(uint16_t);
uint32_t X, Y;
for (Y = 0; Y < GBA_SCREEN_HEIGHT; Y++)
{
for (X = 0; X < GBA_SCREEN_WIDTH * sizeof(uint16_t) / sizeof(uint32_t); X++)
{
*(uint32_t*) Dest = bgr555_to_rgb565(*(uint32_t*) Src);
Dest += 2;
Src += 2;
}
Src = (uint16_t*) ((uint8_t*) Src + SrcSkip);
Dest = (uint16_t*) ((uint8_t*) Dest + DestSkip);
}
}
static inline void gba_convert(uint16_t* Dest, uint16_t* Src,
uint32_t SrcPitch, uint32_t DestPitch)
{
uint32_t SrcSkip = SrcPitch - GBA_SCREEN_WIDTH * sizeof(uint16_t);
uint32_t DestSkip = DestPitch - GBA_SCREEN_WIDTH * sizeof(uint16_t);
uint32_t X, Y;
for (Y = 0; Y < GBA_SCREEN_HEIGHT; Y++)
{
for (X = 0; X < GBA_SCREEN_WIDTH * sizeof(uint16_t) / sizeof(uint32_t); X++)
{
*(uint32_t*) Dest = bgr555_to_rgb565(*(uint32_t*) Src);
Dest += 2;
Src += 2;
}
Src = (uint16_t*) ((uint8_t*) Src + SrcSkip);
Dest = (uint16_t*) ((uint8_t*) Dest + DestSkip);
}
}
/* Downscales an image by half in width and in height; also does color
* conversion using the function above.
* Input:
* Src: A pointer to the pixels member of a 240x160 surface to be read by
* this function. The pixel format of this surface is XBGR 1555.
* DestX: The column to start the thumbnail at in the destination surface.
* DestY: The row to start the thumbnail at in the destination surface.
* SrcPitch: The number of bytes making up a scanline in the source
* surface.
* DstPitch: The number of bytes making up a scanline in the destination
* surface.
* Output:
* Dest: A pointer to the pixels member of a surface to be filled with the
* downscaled GBA image. The pixel format of this surface is RGB 565.
*/
void gba_render_half(uint16_t* Dest, uint16_t* Src, uint32_t DestX, uint32_t DestY,
uint32_t SrcPitch, uint32_t DestPitch)
{
Dest = (uint16_t*) ((uint8_t*) Dest
+ (DestY * DestPitch)
+ (DestX * sizeof(uint16_t))
);
uint32_t SrcSkip = SrcPitch - GBA_SCREEN_WIDTH * sizeof(uint16_t);
uint32_t DestSkip = DestPitch - (GBA_SCREEN_WIDTH / 2) * sizeof(uint16_t);
uint32_t X, Y;
for (Y = 0; Y < GBA_SCREEN_HEIGHT / 2; Y++)
{
for (X = 0; X < GBA_SCREEN_WIDTH * sizeof(uint16_t) / (sizeof(uint32_t) * 2); X++)
{
/* Before:
* a b c d
* e f g h
*
* After (multiple letters = average):
* abef cdgh
*/
uint32_t b_a = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) Src )),
d_c = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) Src + 4)),
f_e = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) Src + SrcPitch )),
h_g = bgr555_to_rgb565(*(uint32_t*) ((uint8_t*) Src + SrcPitch + 4));
uint32_t bf_ae = likely(b_a == f_e)
? b_a
: Average32(b_a, f_e);
uint32_t dh_cg = likely(d_c == h_g)
? d_c
: Average32(d_c, h_g);
*(uint32_t*) Dest = likely(bf_ae == dh_cg)
? bf_ae
: Average(Hi(bf_ae), Lo(bf_ae)) |
Raise(Average(Hi(dh_cg), Lo(dh_cg)));
Dest += 2;
Src += 4;
}
Src = (uint16_t*) ((uint8_t*) Src + SrcSkip + SrcPitch);
Dest = (uint16_t*) ((uint8_t*) Dest + DestSkip);
}
}
void ApplyScaleMode(video_scale_type NewMode)
{
switch (NewMode)
{
case unscaled:
// Either show the border
if (BorderSurface != NULL)
{
if (SDL_MUSTLOCK(OutputSurface))
SDL_UnlockSurface(OutputSurface);
SDL_BlitSurface(BorderSurface, NULL, OutputSurface, NULL);
if (SDL_MUSTLOCK(OutputSurface))
SDL_LockSurface(OutputSurface);
}
// or clear the rest of the screen to prevent image remanence.
else
memset(OutputSurface->pixels, 0, OutputSurface->pitch * GCW0_SCREEN_HEIGHT);
break;
case scaled_aspect:
case scaled_aspect_subpixel:
case scaled_aspect_bilinear:
memset(OutputSurface->pixels, 0, OutputSurface->pitch * GCW0_SCREEN_HEIGHT);
break;
case fullscreen:
case fullscreen_subpixel:
case fullscreen_bilinear:
case hardware:
break;
}
}
void ScaleModeUnapplied()
{
FramesBordered = 0;
}
void ReGBA_RenderScreen(void)
{
if (ReGBA_IsRenderingNextFrame())
{
Stats.TotalRenderedFrames++;
Stats.RenderedFrames++;
video_scale_type ResolvedScaleMode = ResolveSetting(ScaleMode, PerGameScaleMode);
if (FramesBordered < 3)
{
ApplyScaleMode(ResolvedScaleMode);
FramesBordered++;
}
switch (ResolvedScaleMode)
{
#ifndef GCW_ZERO
case hardware: /* Hardware, when there's no hardware to scale
images, acts as unscaled */
#endif
case unscaled:
gba_render(OutputSurface->pixels, GBAScreen, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case fullscreen:
gba_upscale(OutputSurface->pixels, GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case fullscreen_bilinear:
gba_upscale_bilinear(OutputSurface->pixels, GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case fullscreen_subpixel:
gba_upscale_subpixel(OutputSurface->pixels, GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case scaled_aspect:
gba_upscale_aspect((uint16_t*) ((uint8_t*)
OutputSurface->pixels +
(((GCW0_SCREEN_HEIGHT - (GBA_SCREEN_HEIGHT) * 4 / 3) / 2) * OutputSurface->pitch)) /* center vertically */,
GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case scaled_aspect_bilinear:
gba_upscale_aspect_bilinear((uint16_t*) ((uint8_t*)
OutputSurface->pixels +
(((GCW0_SCREEN_HEIGHT - (GBA_SCREEN_HEIGHT) * 4 / 3) / 2) * OutputSurface->pitch)) /* center vertically */,
GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
case scaled_aspect_subpixel:
gba_upscale_aspect_subpixel((uint16_t*) ((uint8_t*)
OutputSurface->pixels +
(((GCW0_SCREEN_HEIGHT - (GBA_SCREEN_HEIGHT) * 4 / 3) / 2) * OutputSurface->pitch)) /* center vertically */,
GBAScreen, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT, GBAScreenSurface->pitch, OutputSurface->pitch);
break;
#ifdef GCW_ZERO
case hardware:
gba_convert(OutputSurface->pixels, GBAScreen, GBAScreenSurface->pitch, OutputSurface->pitch);
#endif
}
ReGBA_DisplayFPS();
ReGBA_VideoFlip();
while (true)
{
unsigned int AudioFastForwardedCopy = AudioFastForwarded;
unsigned int FramesAhead = (VideoFastForwarded >= AudioFastForwardedCopy)
? /* no overflow */ VideoFastForwarded - AudioFastForwardedCopy
: /* overflow */ 0x100 - (AudioFastForwardedCopy - VideoFastForwarded);
uint32_t Quota = AUDIO_OUTPUT_BUFFER_SIZE * 3 * OUTPUT_FREQUENCY_DIVISOR + (uint32_t) (FramesAhead * (SOUND_FREQUENCY / 59.73f));
if (ReGBA_GetAudioSamplesAvailable() <= Quota)
break;
usleep(1000);
}
}
if (ReGBA_GetAudioSamplesAvailable() < AUDIO_OUTPUT_BUFFER_SIZE * 2 * OUTPUT_FREQUENCY_DIVISOR)
{
if (AudioFrameskip < MAX_AUTO_FRAMESKIP)
AudioFrameskip++;
SufficientAudioControl = 0;
}
else
{
SufficientAudioControl++;
if (SufficientAudioControl >= 10)
{
SufficientAudioControl = 0;
if (AudioFrameskip > 0)
AudioFrameskip--;
}
}
if (FastForwardFrameskip > 0 && FastForwardFrameskipControl > 0)
{
FastForwardFrameskipControl--;
VideoFastForwarded = (VideoFastForwarded + 1) & 0xFF;
}
else
{
FastForwardFrameskipControl = FastForwardFrameskip;
uint32_t ResolvedUserFrameskip = ResolveSetting(UserFrameskip, PerGameUserFrameskip);
if (ResolvedUserFrameskip != 0)
{
if (UserFrameskipControl == 0)
UserFrameskipControl = ResolvedUserFrameskip - 1;
else
UserFrameskipControl--;
}
}
if (AudioFrameskipControl > 0)
AudioFrameskipControl--;
else
AudioFrameskipControl = AudioFrameskip;
}
u16 *copy_screen()
{
u32 pitch = GBAScreenPitch;
u16 *copy = malloc(GBA_SCREEN_WIDTH * GBA_SCREEN_HEIGHT * sizeof(uint16_t));
u16 *dest_ptr = copy;
u16 *src_ptr = GBAScreen;
u32 x, y;
for(y = 0; y < GBA_SCREEN_HEIGHT; y++)
{
memcpy(dest_ptr, src_ptr, GBA_SCREEN_WIDTH * sizeof(u16));
src_ptr += pitch;
dest_ptr += GBA_SCREEN_WIDTH;
}
return copy;
}
void blit_to_screen(u16 *src, u32 w, u32 h, u32 dest_x, u32 dest_y)
{
u32 pitch = GBAScreenPitch;
u16 *dest_ptr = GBAScreen;
u16 *src_ptr = src;
u32 line_skip = pitch - w;
u32 x, y;
for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++, src_ptr++, dest_ptr++)
{
*dest_ptr = *src_ptr;
}
dest_ptr += line_skip;
}
}
static uint32_t CutString(const char* String, const uint32_t MaxWidth,
struct StringCut* Cuts, uint32_t CutsAllocated)
{
uint32_t Cut = 0;
uint32_t CutStart = 0, Cur = 0, CutWidth = 0;
uint32_t LastSpace = -1;
bool SpaceInCut = false;
while (String[Cur] != '\0')
{
if (String[Cur] != '\n')
{
if (String[Cur] == ' ')
{
LastSpace = Cur;
SpaceInCut = true;
}
CutWidth += _font_width[(uint8_t) String[Cur]];
}
if (String[Cur] == '\n' || CutWidth > MaxWidth)
{
if (Cut < CutsAllocated)
Cuts[Cut].Start = CutStart;
if (String[Cur] == '\n')
{
if (Cut < CutsAllocated)
Cuts[Cut].End = Cur;
}
else if (CutWidth > MaxWidth)
{
if (SpaceInCut)
{
if (Cut < CutsAllocated)
Cuts[Cut].End = LastSpace;
Cur = LastSpace;
}
else
{
if (Cut < CutsAllocated)
Cuts[Cut].End = Cur;
Cur--; // Next iteration redoes this character
}
}
CutStart = Cur + 1;
CutWidth = 0;
SpaceInCut = false;
Cut++;
}
Cur++;
}
if (Cut < CutsAllocated)
{
Cuts[Cut].Start = CutStart;
Cuts[Cut].End = Cur;
}
return Cut + 1;
}
uint32_t GetSectionRenderedWidth(const char* String, const uint32_t Start, const uint32_t End)
{
uint32_t Result = 0, i;
for (i = Start; i < End; i++)
Result += _font_width[(uint8_t) String[i]];
return Result;
}
void PrintString(const char* String, uint16_t TextColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
struct StringCut* Cuts = malloc((Height / _font_height) * sizeof(struct StringCut));
uint32_t CutCount = CutString(String, Width, Cuts, Height / _font_height), Cut;
if (CutCount > Height / _font_height)
CutCount = Height / _font_height;
for (Cut = 0; Cut < CutCount; Cut++)
{
uint32_t TextWidth = GetSectionRenderedWidth(String, Cuts[Cut].Start, Cuts[Cut].End);
uint32_t LineX, LineY;
switch (HorizontalAlignment)
{
case LEFT: LineX = X; break;
case CENTER: LineX = X + (Width - TextWidth) / 2; break;
case RIGHT: LineX = (X + Width) - TextWidth; break;
default: LineX = 0; /* shouldn't happen */ break;
}
switch (VerticalAlignment)
{
case TOP:
LineY = Y + Cut * _font_height;
break;
case MIDDLE:
LineY = Y + (Height - CutCount * _font_height) / 2 + Cut * _font_height;
break;
case BOTTOM:
LineY = (Y + Height) - (CutCount - Cut) * _font_height;
break;
default:
LineY = 0; /* shouldn't happen */
break;
}
uint32_t Cur;
for (Cur = Cuts[Cut].Start; Cur < Cuts[Cut].End; Cur++)
{
uint32_t glyph_offset = (uint32_t) String[Cur] * _font_height;
uint32_t glyph_width = _font_width[(uint8_t) String[Cur]];
uint32_t glyph_column, glyph_row;
uint16_t current_halfword;
for(glyph_row = 0; glyph_row < _font_height; glyph_row++, glyph_offset++)
{
current_halfword = _font_bits[glyph_offset];
for (glyph_column = 0; glyph_column < glyph_width; glyph_column++)
{
if ((current_halfword >> (15 - glyph_column)) & 0x01)
*(uint16_t*) ((uint8_t*) Dest + (LineY + glyph_row) * DestPitch + (LineX + glyph_column) * sizeof(uint16_t)) = TextColor;
}
}
LineX += glyph_width;
}
}
free(Cuts);
}
uint32_t GetRenderedWidth(const char* str)
{
struct StringCut* Cuts = malloc(sizeof(struct StringCut));
uint32_t CutCount = CutString(str, UINT32_MAX, Cuts, 1);
if (CutCount > 1)
{
Cuts = realloc(Cuts, CutCount * sizeof(struct StringCut));
CutString(str, UINT32_MAX, Cuts, CutCount);
}
uint32_t Result = 0, LineWidth, Cut;
for (Cut = 0; Cut < CutCount; Cut++)
{
LineWidth = 0;
uint32_t Cur;
for (Cur = Cuts[Cut].Start; Cur < Cuts[Cut].End; Cur++)
{
LineWidth += _font_width[(uint8_t) str[Cur]];
}
if (LineWidth > Result)
Result = LineWidth;
}
free(Cuts);
return Result;
}
uint32_t GetRenderedHeight(const char* str)
{
return CutString(str, UINT32_MAX, NULL, 0) * _font_height;
}
void PrintStringOutline(const char* String, uint16_t TextColor, uint16_t OutlineColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
uint32_t sx, sy;
for (sx = 0; sx <= 2; sx++)
for (sy = 0; sy <= 2; sy++)
if (!(sx == 1 && sy == 1))
PrintString(String, OutlineColor, Dest, DestPitch, X + sx, Y + sy, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
PrintString(String, TextColor, Dest, DestPitch, X + 1, Y + 1, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
}
static void ProgressUpdateInternal(uint32_t Current, uint32_t Total)
{
char* Line;
switch (CurrentFileAction)
{
case FILE_ACTION_LOAD_BIOS:
Line = "Reading the GBA BIOS";
break;
case FILE_ACTION_LOAD_BATTERY:
Line = "Reading saved data";
break;
case FILE_ACTION_SAVE_BATTERY:
Line = "Writing saved data";
break;
case FILE_ACTION_LOAD_STATE:
Line = "Reading saved state";
break;
case FILE_ACTION_SAVE_STATE:
Line = "Writing saved state";
break;
case FILE_ACTION_LOAD_ROM_FROM_FILE:
Line = "Reading ROM from a file";
break;
case FILE_ACTION_DECOMPRESS_ROM_TO_RAM:
Line = "Decompressing ROM";
break;
case FILE_ACTION_DECOMPRESS_ROM_TO_FILE:
Line = "Decompressing ROM into a file";
break;
case FILE_ACTION_APPLY_GAME_COMPATIBILITY:
Line = "Applying compatibility fixes";
break;
case FILE_ACTION_LOAD_GLOBAL_SETTINGS:
Line = "Reading global settings";
break;
case FILE_ACTION_SAVE_GLOBAL_SETTINGS:
Line = "Writing global settings";
break;
case FILE_ACTION_LOAD_GAME_SETTINGS:
Line = "Loading per-game settings";
break;
case FILE_ACTION_SAVE_GAME_SETTINGS:
Line = "Writing per-game settings";
break;
default:
Line = "File action ongoing";
break;
}
SDL_FillRect(OutputSurface, NULL, COLOR_PROGRESS_BACKGROUND);
SDL_Rect TopLine = { (GCW0_SCREEN_WIDTH - PROGRESS_WIDTH) / 2, (GCW0_SCREEN_HEIGHT - PROGRESS_HEIGHT) / 2, PROGRESS_WIDTH, 1 };
SDL_FillRect(OutputSurface, &TopLine, COLOR_PROGRESS_OUTLINE);
SDL_Rect BottomLine = { (GCW0_SCREEN_WIDTH - PROGRESS_WIDTH) / 2, (GCW0_SCREEN_HEIGHT - PROGRESS_HEIGHT) / 2 + PROGRESS_HEIGHT - 1, PROGRESS_WIDTH, 1 };
SDL_FillRect(OutputSurface, &BottomLine, COLOR_PROGRESS_OUTLINE);
SDL_Rect LeftLine = { (GCW0_SCREEN_WIDTH - PROGRESS_WIDTH) / 2, (GCW0_SCREEN_HEIGHT - PROGRESS_HEIGHT) / 2, 1, PROGRESS_HEIGHT };
SDL_FillRect(OutputSurface, &LeftLine, COLOR_PROGRESS_OUTLINE);
SDL_Rect RightLine = { (GCW0_SCREEN_WIDTH + PROGRESS_WIDTH) / 2 - 1, (GCW0_SCREEN_HEIGHT - PROGRESS_HEIGHT) / 2, 1, PROGRESS_HEIGHT };
SDL_FillRect(OutputSurface, &RightLine, COLOR_PROGRESS_OUTLINE);
SDL_Rect Content = { (GCW0_SCREEN_WIDTH - PROGRESS_WIDTH) / 2 + 1, (GCW0_SCREEN_HEIGHT - PROGRESS_HEIGHT) / 2 + 1, (uint32_t) ((uint64_t) Current * (PROGRESS_WIDTH - 2) / Total), PROGRESS_HEIGHT - 2 };
SDL_FillRect(OutputSurface, &Content, COLOR_PROGRESS_CONTENT);
PrintStringOutline(Line, COLOR_PROGRESS_TEXT_CONTENT, COLOR_PROGRESS_TEXT_OUTLINE, OutputSurface->pixels, OutputSurface->pitch, 0, 0, GCW0_SCREEN_WIDTH, GCW0_SCREEN_HEIGHT, CENTER, MIDDLE);
ReGBA_VideoFlip();
}
void ReGBA_ProgressInitialise(enum ReGBA_FileAction Action)
{
if (Action == FILE_ACTION_SAVE_BATTERY)
return; // Ignore this completely, because it flashes in-game
clock_gettime(CLOCK_MONOTONIC, &LastProgressUpdate);
CurrentFileAction = Action;
InFileAction = true;
ScaleModeUnapplied();
ProgressUpdateInternal(0, 1);
}
void ReGBA_ProgressUpdate(uint32_t Current, uint32_t Total)
{
struct timespec Now, Difference;
clock_gettime(CLOCK_MONOTONIC, &Now);
Difference = TimeDifference(LastProgressUpdate, Now);
if (InFileAction &&
(Difference.tv_sec > 0 || Difference.tv_nsec > 50000000 || Current == Total)
)
{
ProgressUpdateInternal(Current, Total);
LastProgressUpdate = Now;
}
}
void ReGBA_ProgressFinalise()
{
InFileAction = false;
}
void ReGBA_VideoFlip()
{
if (SDL_MUSTLOCK(OutputSurface))
SDL_UnlockSurface(OutputSurface);
SDL_Flip(OutputSurface);
if (SDL_MUSTLOCK(OutputSurface))
SDL_LockSurface(OutputSurface);
}
|