ABMLoader.c
94.3 KB
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
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*******************************************************************************
* Filename:
* ---------
* ABMLoader.c
*
* Project:
* --------
* PlutoMMI
*
* Description:
* ------------
* Alpha BitMap (ABM) encoder
*
* 1. The search algorithm of the color index table is binary search.
* 2. The ABM encoder only support 8, 24, and 32-bit bitmap input.
* 3. ABM only supports up to 65536 colors.
* 4. The ABM palette color format is:
* a. 16-bit for 16-bit MAIN BASE LAYER (MOST CASE)
* b. 24-bit for 24-bit and 32-bit MAIN BASE LAYER
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
/*
* THIS FILE IS USED FOR RESOURCE GENERATOR AND MTE ONLY.
*/
#if !defined(__MTK_TARGET__) || defined(__RESOURCE_GEN_)
/*****************************************************************************
* Include
*****************************************************************************/
#include <stdio.h>
#include "MMI_features.h"
#include "ABMLoader.h"
#include "gui_resource_type.h"
#include "gdi_include.h"
#include "gdi_image_alpha_bmp_v2_internal.h"
#include "WriteResUtil.h" // for ResgenImageOutStream
#ifdef __MMI_RESOURCE_ENFB_SUPPORT__
#include "ImageGetDimension.h"
#endif /* __MMI_RESOURCE_ENFB_SUPPORT__ */
/*RHR*/
#include "MMIDataType.h"
#ifdef __RESOURCE_GEN_
#include "ResgenLogCAPI.h" // Resgen Log APIs
#include "OfflineResPopCAPI.h"
#endif //__RESOURCE_GEN_
/*****************************************************************************
* extern variable
*****************************************************************************/
extern int toolFlag;
extern FILE *dest_file;
#ifdef __MMI_RESOURCE_ENFB_SUPPORT__
extern FILE *enfb_img_data_file;
extern U32 enfb_offset;
extern U32 enfb_size;
extern MMI_BOOL ENFBAssociatedIDAdded;
#endif
/*****************************************************************************
* Define
*****************************************************************************/
#ifdef __RESOURCE_GEN_
#define ABMLDR_TAG "ABMLOADER"
#define ABMLDR_LOG_D(format, args...) RES_LOG_D((ABMLDR_TAG), (format) , ##args)
#define ABMLDR_LOG_V(format, args...) RES_LOG_V((ABMLDR_TAG), (format) , ##args)
#define ABMLDR_LOG_W(format, args...) RES_LOG_W((ABMLDR_TAG), (format) , ##args)
#define ABMLDR_LOG_E(format, args...) RES_LOG_E((ABMLDR_TAG), (format) , ##args)
#else
#define ABMLDR_LOG_D(format, ...) printf((format), ##__VA_ARGS__)
#define ABMLDR_LOG_V(format, ...) printf((format), ##__VA_ARGS__)
#define ABMLDR_LOG_W(format, ...) printf((format), ##__VA_ARGS__)
#define ABMLDR_LOG_E(format, ...) printf((format), ##__VA_ARGS__)
#endif //__RESOURCE_GEN_
/* check 16-bit RGB color format */
#define RGB_16_BIT_TYPE DRV_RGB_TO_HW(0, 255, 0)
#define COLOR_FORMAT_RGB3553 0xE007 /* 6205B, 6218 */
#define COLOR_FORMAT_RGB565 0x07E0 /* other than 6205B and 6218 */
/*
* color primitives
*/
#define GET_32(bp) ((U32)((bp)[0] | ((bp)[1] << 8) | ((bp)[2] << 16) | ((bp)[3] << 24)))
#define GET_24(bp) ((U32)((bp)[0] | ((bp)[1] << 8)) | ((bp)[2] << 16))
#define GET_16(bp) ((U32)((bp)[0] | ((bp)[1] << 8)))
#define PUT_16(mem, c) *((U16*)(mem)) = (c)
#define PUT_24(mem, c) \
do \
{ \
U8 *mem_8 = (U8*)(mem); \
mem_8[0] = (c); \
mem_8[1] = (c) >> 8; \
mem_8[2] = (c) >> 16; \
} while (0)
#define ARGB(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
#define RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))
/* RGB888 */
#define R_OF_RGB888(c) (((c) << 8) >> 24)
#define G_OF_RGB888(c) (((c) << 16) >> 24)
#define B_OF_RGB888(c) (((c) << 24) >> 24)
/* ARGB8888 */
#define A_OF_ARGB8888(c) ((c) >> 24)
#define R_OF_ARGB8888(c) R_OF_RGB888(c)
#define G_OF_ARGB8888(c) G_OF_RGB888(c)
#define B_OF_ARGB8888(c) B_OF_RGB888(c)
/* RGB565 */
#define R_OF_RGB565(c) (((c) << 16) >> 27)
#define G_OF_RGB565(c) (((c) << 21) >> 26)
#define B_OF_RGB565(c) (((c) << 27) >> 27)
/* RGB3553 */
#define R_OF_RGB3553(c) (((c) << 24) >> 27)
#define G_OF_RGB3553(c) ((((c) & 0x7) << 3) | (((c) >> 13) & 0x7))
#define B_OF_RGB3553(c) (((c) << 19) >> 27)
/* RGB16 */
#define R_OF_RGB16(c) ((RGB_16_BIT_TYPE == COLOR_FORMAT_RGB565) ? R_OF_RGB565(c) : R_OF_RGB3553(c))
#define G_OF_RGB16(c) ((RGB_16_BIT_TYPE == COLOR_FORMAT_RGB565) ? G_OF_RGB565(c) : G_OF_RGB3553(c))
#define B_OF_RGB16(c) ((RGB_16_BIT_TYPE == COLOR_FORMAT_RGB565) ? B_OF_RGB565(c) : B_OF_RGB3553(c))
#define RGB565(r, g, b) (((r) << 11) | ((g) << 5) | (b))
#define RGB3553(r, g, b) (((r) << 3) | ((b) << 8) | ((g) >> 3) | (((g) & 0x7) << 13))
#define ARGB8888_TO_RGB565(c) RGB565(R_OF_ARGB8888(c) >> 3, G_OF_ARGB8888(c) >> 2, B_OF_ARGB8888(c) >> 3)
#define ARGB8888_TO_RGB3553(c) RGB3553(R_OF_ARGB8888(c) >> 3, G_OF_ARGB8888(c) >> 2, B_OF_ARGB8888(c) >> 3)
#define ARGB8888_TO_RGB16(c) ((RGB_16_BIT_TYPE == COLOR_FORMAT_RGB565) ? ARGB8888_TO_RGB565(c) : ARGB8888_TO_RGB3553(c))
/*
* ABM bitstream writer
*/
/* initialize bitstream variables */
#define BW_INIT(mem_ptr) \
{ \
U32 bits_buf; \
U16 *bits_mem_ptr; \
U32 bits_left; \
\
bits_mem_ptr = mem_ptr; \
bits_buf = 0; \
bits_left = 0;
/* flush bitstream buffer */
#define BW_END() \
if (bits_left > 0) \
{ \
*bits_mem_ptr++ = bits_buf; \
} \
}
/* write bpp bits of data into the bitstream */
#define BW_WRITE(data, bpp) \
bits_buf |= ((data) & ((1 << (bpp)) - 1)) << bits_left; \
bits_left += bpp; \
if (bits_left >= 16) \
{ \
*bits_mem_ptr++ = bits_buf; \
bits_buf >>= 16; \
bits_left -= 16; \
}
/* PBM header size */
#define PBM_HEADER_SIZE 13
/*
* windows bitmap
*/
#define BITMAPFILEHEADER_SIZE 14
#define BITMAPINFO_HEADER_SIZE 40
/*
* ABM encoder
*/
#define ABM_MAX_COLOR_NUM 384000 /* ABM supports up to 800*480 colors */
#define ABM_HEADER_SIZE 12
#if defined(MAIN_MEDIA_LAYER_BITS_PER_PIXEL) && (MAIN_MEDIA_LAYER_BITS_PER_PIXEL == 24) && defined(__COSMOS_MMI__)
#define ABM_PALETTE_BPP 24
#else
#define ABM_PALETTE_BPP 16
#endif
#define TRANSPARENT_COLOR_24 RGB(10, 11, 12) /* transparent color of 24-bit bitmap */
#define TRANSPARENT_COLOR_32 ARGB(255, 10, 11, 12) /* transparent color of 32-bit bitmap */
/*****************************************************************************
* Typedef
*****************************************************************************/
typedef U32 mycolor;
/* abm encoder structure*/
typedef struct
{
S32 bmp_width; /* bitmap width */
S32 bmp_height; /* bitmap height */
S32 bmp_bpp; /* bitmap bits per pixel */
S32 bmp_palette_num; /* number of colors in the palette */
mycolor bmp_palette[256]; /* bitmap palette (8-bit bpp max) */
U8 *bmp_pixel_start_p; /* pointer to bitmap data */
U32 line_padding_bytes; /* line padding bytes */
S32 x1, y1, x2, y2; /* image bounding box */
S32 src_key_color_enable; /* for PBM source key color */
S32 is_windows_bmp; /* windows BMP 32-bit is always XRGB8888, not expected ARGB8888. */
S32 is_fully_opaque;
} abm_enc_struct;
/* color index table */
typedef struct
{
mycolor color_table[ABM_MAX_COLOR_NUM]; /* color index table */
S32 color_num; /* number of colors in the table */
S32 last_search_idx; /* last search index */
} color_index_table_struct;
/* Bit-Stream data structure */
typedef struct
{
U8 *mem_ptr;
U32 buf;
U32 buf_bits;
} ab2_bs_struct;
/*****************************************************************************
* Static Declaration
*****************************************************************************/
/* file buffer to keep the whole bitmap file */
/* color index table */
/* abm encoder context */
/* table to calculate ceiling(log2(x)) */
static const U16 log2_table[15] = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
typedef struct
{
abm_enc_struct abm_enc;
color_index_table_struct color_idx_tab;
U32 abm_palette_bpp;
} ABM_Context;
/*****************************************************************************
* static Function
*****************************************************************************/
static S32 search_color(mycolor search_color, ABM_Context * abm_context);
static void insert_color_at(mycolor c, S32 insert_idx, ABM_Context * abm_context);
static U32 add_color_if_new(mycolor c, ABM_Context * abm_context);
static U32 get_normal_color_num(ABM_Context * abm_context);
static U32 get_file_size(FILE *fp);
static S32 parse_bitmap(U8* bmp_data_p, U32 input_bmp_file_size, ABM_Context * abm_context);
static void update_image_bounding_box(S32 x, S32 y, ABM_Context * abm_context);
static void limit_image_bounding_box(ABM_Context * abm_context);
static U32 log2_ceiling(U32 x);
static U32 abmenc_build_color_index_table(ABM_Context * abm_context);
static U32 abmenc_encode(U8* out_mem_p, ABM_Context * abm_context);
/*
* color reduction algorithms
*/
static U32 COLOR_REDUCE_24_TO_16(U32 c)
{
if(A_OF_ARGB8888(c) == 0xFF || A_OF_ARGB8888(c) == 0x00)
{
return c &= 0xFFF8FCF8;
}
else
{
#if defined(__LOW_COST_SUPPORT_COMMON__) && defined(__COSMOS_MMI_PACKAGE__) && defined(GDI_LAYER_PARGB6666_SUPPORT)
return c &= 0xFCF8FCF8;
#else
return c &= 0xFFF8FCF8;
#endif
}
}
static U32 COLOR_REDUCE_24_TO_18(U32 c)
{
return ((c) & 0xFFFCFCFC); /* clear R[1:0], G[1:0], B[1:0] */
}
static U32 COLOR_REDUCE(U32 c, ABM_Context * abm_context)
{
return ((abm_context->abm_palette_bpp == 16) ? COLOR_REDUCE_24_TO_16(c) : (c));
}
/*****************************************************************************
* FUNCTION
* search_color
* DESCRIPTION
* search a color in the color table (binary search)
* PARAMETERS
* search_color [IN] color to search
* RETURNS
* color index in the color table if found, otherwise ABMENC_COLOR_NOT_FOUND (-1)
*****************************************************************************/
static S32 search_color(mycolor search_color, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 start_idx, end_idx, center_idx;
mycolor c;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
start_idx = 0;
end_idx = abm_context->color_idx_tab.color_num - 1;
while (start_idx <= end_idx)
{
center_idx = (start_idx + end_idx) / 2;
c = abm_context->color_idx_tab.color_table[center_idx];
if (search_color < c)
{
start_idx = center_idx + 1;
}
else if (search_color > c)
{
end_idx = center_idx - 1;
}
else
{
return center_idx;
}
}
/* keep the search index to speed up the search-and-insert operation, add_color_if_new() */
abm_context->color_idx_tab.last_search_idx = start_idx;
return ABMENC_COLOR_NOT_FOUND;
}
/*****************************************************************************
* FUNCTION
* insert_color_at
* DESCRIPTION
* insert a color at a specified index in the color table
* PARAMETERS
* c [IN] color to add
* insert_idx [IN] insertion index of the table
* RETURNS
* void
*****************************************************************************/
static void insert_color_at(mycolor c, S32 insert_idx, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (insert_idx >= ABM_MAX_COLOR_NUM)
{
return;
}
/* insert at insert_idx */
for (i = abm_context->color_idx_tab.color_num - 1; i >= insert_idx; i--)
{
abm_context->color_idx_tab.color_table[i + 1] = abm_context->color_idx_tab.color_table[i];
}
abm_context->color_idx_tab.color_table[insert_idx] = c;
abm_context->color_idx_tab.color_num++;
}
/*****************************************************************************
* FUNCTION
* add_color_if_new
* DESCRIPTION
* add a color if the color does not exist in the table
* PARAMETERS
* c [IN] color to add
* RETURNS
* return ABM_ENC_OK if succeed
*****************************************************************************/
static U32 add_color_if_new(mycolor c, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 insert_idx;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
insert_idx = search_color(c, abm_context);
if (insert_idx == ABMENC_COLOR_NOT_FOUND)
{
insert_idx = abm_context->color_idx_tab.last_search_idx;
}
else
{ /* found in the table, return */
return ABM_ENC_OK;
}
if (abm_context->color_idx_tab.color_num >= ABM_MAX_COLOR_NUM)
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
else
{
insert_color_at(c, insert_idx, abm_context);
return ABM_ENC_OK;
}
}
/*****************************************************************************
* FUNCTION
* get_normal_color_num
* DESCRIPTION
* get the number of NORMAL colors
* PARAMETERS
* void
* RETURNS
* number of OPAQUE colors (alpha = 255) in the color index table
*****************************************************************************/
static U32 get_normal_color_num(ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
for (i = 0; i < abm_context->color_idx_tab.color_num; i++)
{
if (A_OF_ARGB8888(abm_context->color_idx_tab.color_table[i]) != 255)
{
break;
}
}
return i;
}
/*****************************************************************************
* FUNCTION
* get_file_size
* DESCRIPTION
* return the file size in bytes
* PARAMETERS
* fp [IN] file pointer
* RETURNS
* file size in bytes
*****************************************************************************/
static U32 get_file_size(FILE *fp)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 fpos, file_size;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* save the current file position */
fpos = ftell(fp);
/* seek to the EOF */
fseek(fp, 0, SEEK_END);
/* get file size */
file_size = ftell(fp);
/* restore the old file position */
fseek(fp, fpos, SEEK_SET);
return file_size;
}
/*****************************************************************************
* FUNCTION
* parse_bitmap
* DESCRIPTION
* parse and verify the bitmap header (BITMAPFILEHEADER and BITMAPINFO)
* PARAMETERS
* bitmap_p [IN]
* RETURNS
* void
*****************************************************************************/
static S32 parse_bitmap(U8* bmp_data_p, U32 input_bmp_file_size, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 bmp_w, bmp_h, bmp_bpp;
S32 i;
U32 bi_size;
U32 compression;
U32 line_size;
U8 *pal_color_p;
mycolor pal_first_color;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* check the "BM" magic number */
if ((bmp_data_p[0] != 'B') || (bmp_data_p[1] != 'M'))
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* verify the bitmap file size */
if (GET_32(bmp_data_p + 2) != input_bmp_file_size)
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* verify the BITMAPINFO HEADER size field */
bi_size = GET_32(bmp_data_p + 14);
if (bi_size < BITMAPINFO_HEADER_SIZE)
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* get bitmap width, height, and bits per pixel */
bmp_w = GET_32(bmp_data_p + 18);
bmp_h = GET_32(bmp_data_p + 22);
bmp_bpp = GET_16(bmp_data_p + 28);
/* only support 8, 24, and 32 bpp */
if ((bmp_bpp != 4) && (bmp_bpp != 8) && (bmp_bpp != 24) && (bmp_bpp != 32))
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* only support bottom-up bitmap */
if (((S32)bmp_h <= 0) || ((S32)bmp_w <= 0))
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* limit image dimension to 1024x1024 */
if (((S32)bmp_h > 1024) || ((S32)bmp_w > 2048))
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* must be uncompressed (BI_RGB = 0 and BI_BITFIELDS = 3) */
compression = GET_32(bmp_data_p + 30);
if ((compression != 0) && (compression != 3))
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* get the palette color num */
if (bmp_bpp <= 8)
{
abm_context->abm_enc.bmp_palette_num = GET_32(bmp_data_p + 46);
if (abm_context->abm_enc.bmp_palette_num == 0)
{
abm_context->abm_enc.bmp_palette_num = 256;
}
}
else
{
abm_context->abm_enc.bmp_palette_num = 0;
}
/* make sure palette color number is 256 maximally */
if (abm_context->abm_enc.bmp_palette_num > 256)
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* line size in bytes */
if(bmp_bpp == 4)
{
abm_context->abm_enc.line_padding_bytes = ((bmp_w + 7) / 8 * 8 - bmp_w)/2;
}
else
{
line_size = bmp_bpp * bmp_w / 8;
abm_context->abm_enc.line_padding_bytes = (line_size + 3) / 4 * 4 - line_size;
}
abm_context->abm_enc.bmp_width = bmp_w;
abm_context->abm_enc.bmp_height = bmp_h;
abm_context->abm_enc.bmp_bpp = bmp_bpp;
/* get palette */
pal_color_p = bmp_data_p + BITMAPFILEHEADER_SIZE + bi_size;
pal_first_color = GET_32(pal_color_p);
for (i = 0; i < abm_context->abm_enc.bmp_palette_num; i++)
{
abm_context->abm_enc.bmp_palette[i] = GET_32(pal_color_p + i * 4);
/* A of ARGB quad is reserved as 0, convert it to opacity 255 */
if (abm_context->abm_enc.src_key_color_enable && (abm_context->abm_enc.bmp_palette[i] == pal_first_color))
{
/*
* The transparency color is the first color in the palette.
* Any equivalent color is also the transpareny color.
*
* ONLY VALID FOR PBM AND BMP FILES.
*/
abm_context->abm_enc.bmp_palette[i] = 0; /* force alpha value 0 */
}
else
{
abm_context->abm_enc.bmp_palette[i] |= 0xFF000000; /* opaque color, alpha value 255 */
}
}
/* bitmap pixel start offset */
abm_context->abm_enc.bmp_pixel_start_p = bmp_data_p + GET_32(bmp_data_p + 10);
return ABM_ENC_OK;
}
/*****************************************************************************
* FUNCTION
* load_bmp_file
* DESCRIPTION
* check the givn bitmap file and read into buffer
* PARAMETERS
* buf [IN] pointer to buffer
* filename [IN] the file name to check
* RETURNS
* return 0 if error. otherwise return the size of the file.
*****************************************************************************/
static U32 load_bmp_file(U8 *buf, const U8 *filename, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
FILE *fp = NULL;
S32 file_size = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* open and check the input bitmap file */
fp = fopen(filename, "rb");
if (fp == NULL)
{
ABMLDR_LOG_E("[ABM enc] open file err: %s", filename);
return 0;
}
/* get the file size of the input BMP file */
file_size = get_file_size(fp);
/* make sure the file buffer size is enough */
if (file_size > ABMENC_BMP_FILE_BUFFER_SIZE)
{
fclose(fp);
ABMLDR_LOG_E("[ABM enc] over buffer size: %s", filename);
return 0;
}
/* read the whole file into the memory */
if (fread(buf, file_size, 1, fp) != 1)
{
fclose(fp);
ABMLDR_LOG_E("[ABM enc] read buffer err: %s", filename);
return 0;
}
/* parse bitmap header */
if (parse_bitmap(buf, file_size, abm_context) == ABM_ENC_INPUT_BITMAP_ERROR)
{
fclose(fp);
ABMLDR_LOG_E("[ABM enc] parse BMP err, %s", filename);
return 0;
}
fclose(fp);
return file_size;
}
/*****************************************************************************
* FUNCTION
* update_image_bounding_box
* DESCRIPTION
* update image bounding box (piggy-back in the first pass)
* PARAMETERS
* x [IN] x coordinate of non-transparent color
* y [IN] y coordinate of non-transparent color
* RETURNS
* void
*****************************************************************************/
static void update_image_bounding_box(S32 x, S32 y, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 x1, x2, y1, y2;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
x1 = abm_context->abm_enc.x1;
y1 = abm_context->abm_enc.y1;
x2 = abm_context->abm_enc.x2;
y2 = abm_context->abm_enc.y2;
/* x1 = min(x of non-transparent color) */
if (x < x1)
{
x1 = x;
}
/* y1 = min(y of non-transparent color) */
if (y < y1)
{
y1 = y;
}
/* x2 = max(x of non-transparent color) */
if (x > x2)
{
x2 = x;
}
/* y2 = max(y of non-transparent color) */
if (y > y2)
{
y2 = y;
}
abm_context->abm_enc.x1 = x1;
abm_context->abm_enc.y1 = y1;
abm_context->abm_enc.x2 = x2;
abm_context->abm_enc.y2 = y2;
}
/*****************************************************************************
* FUNCTION
* limit_image_bounding_box
* DESCRIPTION
* limit the x1, y1, x2, and y2 within 8-bit
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
static void limit_image_bounding_box(ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 w, h, bottom_right_off_w, bottom_right_off_h;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
w = abm_context->abm_enc.bmp_width;
h = abm_context->abm_enc.bmp_height;
if (abm_context->abm_enc.x1 > 255)
{
abm_context->abm_enc.x1 = 255;
}
if (abm_context->abm_enc.y1 > 255)
{
abm_context->abm_enc.y1 = 255;
}
bottom_right_off_w = w - 1 - abm_context->abm_enc.x2;
if (bottom_right_off_w > 255)
{
bottom_right_off_w = 255;
abm_context->abm_enc.x2 = w - 1 - bottom_right_off_w;
}
bottom_right_off_h = h - 1 - abm_context->abm_enc.y2;
if (bottom_right_off_h > 255)
{
bottom_right_off_h = 255;
abm_context->abm_enc.y2 = h - 1 - bottom_right_off_h;
}
}
/*****************************************************************************
* FUNCTION
* abmenc_build_color_index_table
* DESCRIPTION
* build the color index table (first pass)
* PARAMETERS
* void
* RETURNS
* return ABM_ENC_OK if succee
*****************************************************************************/
static U32 abmenc_build_color_index_table(ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 x, y, w, h;
S32 line_padding_bytes;
U8 *pixel_p, *img_p;
U8 pixel_nibble_index, last_nibble;
U32 bmp_bpp;
mycolor c;
U32 add_color_ret;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
w = abm_context->abm_enc.bmp_width;
h = abm_context->abm_enc.bmp_height;
line_padding_bytes = abm_context->abm_enc.line_padding_bytes;
bmp_bpp = abm_context->abm_enc.bmp_bpp;
img_p = abm_context->abm_enc.bmp_pixel_start_p;
/* initialize image bounding box */
abm_context->abm_enc.x1 = w;
abm_context->abm_enc.y1 = h;
abm_context->abm_enc.x2 = 0;
abm_context->abm_enc.y2 = 0;
/* first pass: build color index table and find the image bounding box */
pixel_p = img_p;
/* Pixel nibble index holds the byte position for pixels.
* last_nibble flag is used to determine which nibble to read for the index. (First or last).
*/
pixel_nibble_index = 0;
last_nibble = 0;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
/* read pixel */
if (bmp_bpp == 32)
{ /* 32-bit bitmap */
c = GET_32(pixel_p);
pixel_p += 4;
/* Windows 32-bit BMP is always 0x00RRGGBB. */
if (abm_context->abm_enc.is_windows_bmp)
{
c |= 0xFF000000;
}
if (abm_context->abm_enc.src_key_color_enable && (c == TRANSPARENT_COLOR_32))
{ /* transparent color, alpha = 0 */
c = 0;
}
}
else if (bmp_bpp == 24)
{ /* 24-bit bitmap */
c = GET_24(pixel_p);
pixel_p += 3;
if (abm_context->abm_enc.src_key_color_enable && (c == TRANSPARENT_COLOR_24))
{ /* transparent color, alpha = 0 */
c = 0;
}
else
{ /* opaque color, alpha = 255 */
c |= 0xFF000000;
}
}
else if(bmp_bpp == 4)
{ /* 4-bit bitmap */
/* Alternate reading of nibbles, which are the index for the colors */
if(!last_nibble)
{
pixel_nibble_index = (U8)*pixel_p; /* reads the byte for two pixels' indices */
pixel_p++; /* Advances the pixel pointer */
/* Set last nibble flag ON to skip reading the byte again in the next pass.
* The index will be read from the last nibble of pixel_nibble_index
*/
last_nibble = 1;
}
else
{
last_nibble = 0;
}
/* Read the nibbles, first nibble */
c = abm_context->abm_enc.bmp_palette[(U8)(pixel_nibble_index >> 4)];
/* Puts the value of last nibble in first nibble */
pixel_nibble_index = pixel_nibble_index << 4;
}
else
{ /* 8-bit bitmap */
c = abm_context->abm_enc.bmp_palette[*pixel_p++];
}
/* add color if the color is not transparent color */
if (A_OF_ARGB8888(c) != 0)
{
/* only add unique color */
add_color_ret = add_color_if_new(COLOR_REDUCE(c,abm_context), abm_context);
if (add_color_ret != ABM_ENC_OK)
{
return ABM_ENC_INPUT_BITMAP_ERROR;
}
/* update image bounding box (notice bitmap is flipped, so we have to invert y here) */
update_image_bounding_box(x, h - 1 - y, abm_context);
}
/* containing a transparent color, this image is not fully opaque! */
if (A_OF_ARGB8888(c) != 255)
{
abm_context->abm_enc.is_fully_opaque = 0;
}
}
/* End of line. Taking care of padding. Will read first nibble for the next line. */
last_nibble = 0;
pixel_p += line_padding_bytes;
}
/* handle special case, all transparent color */
if (abm_context->abm_enc.x2 < abm_context->abm_enc.x1)
{
/* 1 dummy point at the center of image to make best use of the bounding box*/
abm_context->abm_enc.x1 = w / 2;
abm_context->abm_enc.y1 = h / 2;
abm_context->abm_enc.x2 = abm_context->abm_enc.x1;
abm_context->abm_enc.y2 = abm_context->abm_enc.y1;
}
limit_image_bounding_box(abm_context);
return ABM_ENC_OK;
}
/*****************************************************************************
* FUNCTION
* log2_ceiling
* DESCRIPTION
* calculate ceiling(log2(x))
* PARAMETERS
* x [IN] x must be in the range of [0, 65535]
* RETURNS
* ceiling(log2(x))
*****************************************************************************/
static U32 log2_ceiling(U32 x)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
for (i = 0; i < 15; i++)
{
if (x <= log2_table[i])
break;
}
return i + 1;
}
/*****************************************************************************
* FUNCTION
* ab2_bs_init
* DESCRIPTION
* Initialize the given bit-stream data structure.
* The first data will start at given memory pointer.
* PARAMETERS
* bs [INOUT] the bit-stream structure to be initialized
* mem_ptr [IN] the start memory to be a bit-stream
* RETURNS
* void
*****************************************************************************/
static GDI_IMAGE_AB2_INLINE void ab2_bs_init(ab2_bs_struct *bs, U8 *mem_ptr)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
//MMI_ASSERT(bs != NULL);
bs->mem_ptr = mem_ptr;
bs->buf = 0;
bs->buf_bits = 0;
}
/*****************************************************************************
* FUNCTION
* ab2_bs_write
* DESCRIPTION
* Write given bits data to bit-stream. The data must less or equal 16 bits.
* PARAMETERS
* bs [IN] the bit-stream
* data [IN] the bit data to write
* bits [IN] the number of bits to write (must <= 16)
* RETURNS
* void
*****************************************************************************/
static GDI_IMAGE_AB2_INLINE void ab2_bs_write(ab2_bs_struct *bs, U32 data, U32 bits)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
//MMI_ASSERT(bs != NULL);
//MMI_ASSERT(bits <= 16);
//MMI_ASSERT(data < (1 << bits));
bs->buf |= (data << bs->buf_bits);
bs->buf_bits += bits;
/* write out 16 bits data if enough */
if (bs->buf_bits >= 16)
{
*(U16 *)bs->mem_ptr = (U16)bs->buf;
bs->buf >>= 16;
bs->buf_bits -= 16;
bs->mem_ptr += sizeof(U16);
}
}
/*****************************************************************************
* FUNCTION
* ab2_bs_flush
* DESCRIPTION
* Flush all data to bit-stream, it will be align to 2-bytes
* PARAMETERS
* bs [IN] the bit-stream
* RETURNS
* void
*****************************************************************************/
static void ab2_bs_flush(ab2_bs_struct *bs)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
//MMI_ASSERT(bs != NULL);
if (bs->buf_bits > 0)
{
*(U16 *)bs->mem_ptr = (U16)bs->buf;
bs->buf = 0;
bs->buf_bits = 0;
bs->mem_ptr += sizeof(U16);
}
}
/*****************************************************************************
* FUNCTION
* ab2_bs_offset
* DESCRIPTION
* Caculate the offset according by given base memory location
* PARAMETERS
* bs [IN] the bit-stream
* base_ptr [IN] the base memory location
* RETURNS
* The number of offset
*****************************************************************************/
static GDI_IMAGE_AB2_INLINE U32 ab2_bs_offset(ab2_bs_struct *bs, const U8 *base_ptr)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
//MMI_ASSERT(bs != NULL);
//MMI_ASSERT(bs->buf_bits == 0);
return bs->mem_ptr - base_ptr;
}
/*****************************************************************************
* FUNCTION
* ab2enc_color_preprocess
* DESCRIPTION
* Preprocess the color to save to color table:
* - Prevent blue channel from saturation to get rid of source key
* checking in the decoder
* - Pre-multiply alpha color
* PARAMETERS
* color [IN] the color to preprocess
* RETURNS
* The color has been processed
*****************************************************************************/
static mycolor ab2enc_color_preprocess(mycolor color, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
mycolor a, r, g, b;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/*
* prevent blue channel
*/
if (abm_context->abm_palette_bpp == 16)
{
mycolor color16 = ARGB8888_TO_RGB16(color);
if ((color16) == 0x1F)
{
/* 5-bit B: 0x1F -> 0x1E */
color -= 1 << 3;
}
}
else
{
/* 24-bit and 32-bit ABM both use 24-bit palette */
if (B_OF_ARGB8888(color) == 0xFF)
{
/* 8-bit B: 0xFF -> 0xFE */
color -= 1;
}
}
/*
* pre-multiply alpha color
*/
a = A_OF_ARGB8888(color);
r = R_OF_ARGB8888(color);
g = G_OF_ARGB8888(color);
b = B_OF_ARGB8888(color);
r = GDI_IMAGE_AB2_DIV_255(r * a);
g = GDI_IMAGE_AB2_DIV_255(g * a);
b = GDI_IMAGE_AB2_DIV_255(b * a);
return ARGB(0xff - a, r, g, b);
}
/*****************************************************************************
* FUNCTION
* ab2enc_get_pixel_format
* DESCRIPTION
* Decide the pixel format to save to ABM file
* PARAMETERS
* none
* RETURNS
* The pixel format to save
*****************************************************************************/
static U32 ab2enc_get_pixel_format(ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 normal_color_cnt;
U32 alpha_color_cnt;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
normal_color_cnt = get_normal_color_num(abm_context);
alpha_color_cnt = abm_context->color_idx_tab.color_num - normal_color_cnt;
if (abm_context->abm_enc.src_key_color_enable || alpha_color_cnt == 0)
{
if (abm_context->abm_enc.is_fully_opaque)
{
if (abm_context->abm_palette_bpp == 16)
{
return GDI_IMAGE_AB2_FORMAT_RGB565_FULLY_OPAQUE;
}
return GDI_IMAGE_AB2_FORMAT_RGB888_FULLY_OPAQUE;
}
else
{
if (abm_context->abm_palette_bpp == 16)
{
return GDI_IMAGE_AB2_FORMAT_RGB565_SRC_KEY;
}
return GDI_IMAGE_AB2_FORMAT_RGB888_SRC_KEY;
}
}
else
{
if (abm_context->abm_palette_bpp == 16)
{
return GDI_IMAGE_AB2_FORMAT_PARGB8565;
}
return GDI_IMAGE_AB2_FORMAT_PARGB8888;
}
}
/*****************************************************************************
* FUNCTION
* ab2enc_get_img_color_idx
* DESCRIPTION
* Get the color index at poisition (x, y) of input image.
* PARAMETERS
* param [IN] param usage
* RETURNS
* The color index.
* It will return GDI_IMAGE_AB2_INVALID_COLOR_IDX if error.
*****************************************************************************/
static GDI_IMAGE_AB2_INLINE U32 ab2enc_get_img_color_idx(U32 x, U32 y, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
const bytes_per_pixel = abm_context->abm_enc.bmp_bpp / 8;
const line_pitch_bytes =
bytes_per_pixel * abm_context->abm_enc.bmp_width + abm_context->abm_enc.line_padding_bytes;
U8 *pixel_ptr;
U8 pixel_nibble_index, last_nibble;
S32 line_pitch_bytes_4bpp;
mycolor color;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* Pixel ptr should be specially handled for 4bpp as it will be chosen from nibbles */
if(abm_context->abm_enc.bmp_bpp == 4)
{
line_pitch_bytes_4bpp =
((abm_context->abm_enc.bmp_width + 7) / 8 * 8) /2;
/* Pixel byte position = start address + reverse calculated Y + nibble index in byte for X */
pixel_ptr =
abm_context->abm_enc.bmp_pixel_start_p +
line_pitch_bytes_4bpp * (abm_context->abm_enc.bmp_height - y - 1) +
(x - (x % 2))/2;
/* If x is odd, pixel color index lies in the last nibble. Set the last nibble flag ON */
last_nibble = x % 2;
}
else
{
/* BMP file is saved by bottom-up format */
pixel_ptr =
abm_context->abm_enc.bmp_pixel_start_p +
line_pitch_bytes * (abm_context->abm_enc.bmp_height - y - 1) +
bytes_per_pixel * x;
}
/* read pixel */
if (abm_context->abm_enc.bmp_bpp == 32)
{
color = GET_32(pixel_ptr);
/* Windows 32-bit BMP is always 0x00RRGGBB. */
if (abm_context->abm_enc.is_windows_bmp)
{
color |= 0xFF000000;
}
if (abm_context->abm_enc.src_key_color_enable && (color == TRANSPARENT_COLOR_32))
{
/* transparent color, alpha = 0 */
color = 0;
}
}
else if (abm_context->abm_enc.bmp_bpp == 24)
{
color = GET_24(pixel_ptr);
if (abm_context->abm_enc.src_key_color_enable && (color == TRANSPARENT_COLOR_24))
{
/* transparent color, alpha = 0 */
color = 0;
}
else
{
/* opaque color, alpha = 255 */
color |= 0xFF000000;
}
}
else if (abm_context->abm_enc.bmp_bpp == 8)
{
color = abm_context->abm_enc.bmp_palette[*pixel_ptr];
}
else if(abm_context->abm_enc.bmp_bpp == 4)
{
/* Use the last_nibble flag as it is an indicator for whther it is for first nibble or last nibble */
if(!last_nibble )
{
/* First nibble contains the color index */
pixel_nibble_index = (U8)((*pixel_ptr) >> 4);
}
else
{
/* Last nibble contains the color index */
pixel_nibble_index = (U8)((*pixel_ptr) & 0x0F);
}
color = abm_context->abm_enc.bmp_palette[pixel_nibble_index];
}
else
{
return GDI_IMAGE_AB2_INVALID_COLOR_IDX;
}
if (A_OF_ARGB8888(color) == 0)
{
/* index 0 is transparent color */
return 0;
}
/* index 0 is reserved for transparent color, so +1 */
return (U32)search_color(COLOR_REDUCE(color,abm_context), abm_context) + 1;
}
/*****************************************************************************
* FUNCTION
* ab2_encode_rle_repeat_code
* DESCRIPTION
* Encode the RLE repeat code.
* PARAMETERS
* os [IN] the bit stream to write
* color_idx [IN] the repeat color index to encode
* color_idx_bits [IN] the bits of the repeat color index
* cnt [IN] the number of repeat pixels
* RETURNS
* void
*****************************************************************************/
static void ab2_encode_rle_repeat_code(
ab2_bs_struct *os,
U32 color_idx,
U32 color_idx_bits,
U32 cnt)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
const U32 max_cnt = 128;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
while (cnt > max_cnt)
{
ab2_bs_write(os, max_cnt - 1, 8);
ab2_bs_write(os, color_idx, color_idx_bits);
cnt -= max_cnt;
}
if (cnt > 0)
{
ab2_bs_write(os, cnt - 1, 8);
ab2_bs_write(os, color_idx, color_idx_bits);
}
}
/*****************************************************************************
* FUNCTION
* ab2_encode_rle_discount_code
* DESCRIPTION
* Encode the RLE discontinue code.
* PARAMETERS
* os [IN] the bit stream to write
* x [IN] x of the start poisition to encode
* y [IN] y of the start poisition to encode
* color_idx_bits [IN] the bits of the repeat color index
* cnt [IN] the number pixels to encode
* RETURNS
* void
*****************************************************************************/
static void ab2_encode_rle_discount_code(
ab2_bs_struct *os,
U32 x,
U32 y,
U32 color_idx_bits,
U32 cnt,
ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
const U32 max_cnt = 128;
U32 x0 = abm_context->abm_enc.x1;
U32 x1 = abm_context->abm_enc.x2;
U32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
while (cnt > max_cnt)
{
ab2_bs_write(os, 0x80 | (max_cnt - 1), 8);
for (i = max_cnt; i != 0; i--)
{
U32 color_idx = ab2enc_get_img_color_idx(x, y, abm_context);
ab2_bs_write(os, color_idx, color_idx_bits);
/* seek to next image pixel */
x++;
if (x > x1)
{
x = x0;
y++;
}
}
cnt -= max_cnt;
} /* while (cnt > max_cnt) */
if (cnt > 0)
{
ab2_bs_write(os, 0x80 | (cnt - 1), 8);
for (i = cnt; i != 0; i--)
{
U32 color_idx = ab2enc_get_img_color_idx(x, y, abm_context);
ab2_bs_write(os, color_idx, color_idx_bits);
/* seek to next image pixel */
x++;
if (x > x1)
{
x = x0;
y++;
}
}
} /* if (cnt > 0) */
}
/*****************************************************************************
* FUNCTION
* ab2enc_encode_header
* DESCRIPTION
* Encode the ABM v2 header
* PARAMETERS
* mem_ptr [IN] buffer to write header
* RETURNS
* Return the bytes written. If error, it will return 0
*****************************************************************************/
static U32 ab2enc_encode_header(U8 *mem_ptr, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
ab2_bs_struct os;
U32 boundary_left = abm_context->abm_enc.x1;
U32 boundary_top = abm_context->abm_enc.y1;
U32 boundary_right = abm_context->abm_enc.bmp_width - 1 - abm_context->abm_enc.x2;
U32 boundary_bottom = abm_context->abm_enc.bmp_height - 1 - abm_context->abm_enc.y2;
U32 pixel_format = ab2enc_get_pixel_format(abm_context);
U32 algorithm = GDI_IMAGE_AB2_ALGORITHM_RLE;
U32 header_size;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
ab2_bs_init(&os, mem_ptr);
ab2_bs_write(&os, (U32)abm_context->abm_enc.bmp_width, GDI_IMAGE_AB2_HEADER_WIDTH_BITS);
ab2_bs_write(&os, (U32)abm_context->abm_enc.bmp_height, GDI_IMAGE_AB2_HEADER_HEIGHT_BITS);
ab2_bs_write(&os, boundary_left, GDI_IMAGE_AB2_HEADER_BOUND_LEFT_BITS);
ab2_bs_write(&os, boundary_top, GDI_IMAGE_AB2_HEADER_BOUND_TOP_BITS);
ab2_bs_write(&os, boundary_right, GDI_IMAGE_AB2_HEADER_BOUND_RIGHT_BITS);
ab2_bs_write(&os, boundary_bottom, GDI_IMAGE_AB2_HEADER_BOUND_BOTTOM_BITS);
ab2_bs_write(&os, pixel_format, GDI_IMAGE_AB2_HEADER_FORMAT_BITS);
ab2_bs_write(&os, algorithm, GDI_IMAGE_AB2_HEADER_ALGORITHM_BITS);
ab2_bs_write(&os, 0, GDI_IMAGE_AB2_HEADER_RESERVED_1_BITS);
header_size = ab2_bs_offset(&os, mem_ptr);
//MMI_ASSERT(header_size == GDI_IMAGE_AB2_HEADER_SIZE);
return header_size;
}
/*****************************************************************************
* FUNCTION
* ab2enc_rle_encode_header
* DESCRIPTION
* Encode ABM v2 RLE header
* PARAMETERS
* mem_ptr [OUT] buffer to write header
* RETURNS
* Return the bytes written. If error, it will return 0
*****************************************************************************/
static U32 ab2enc_rle_encode_header(U8 *mem_ptr, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
ab2_bs_struct os;
U32 normal_color_cnt;
U32 alpha_color_cnt;
U32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
ab2_bs_init(&os, mem_ptr);
normal_color_cnt = get_normal_color_num(abm_context);
alpha_color_cnt = abm_context->color_idx_tab.color_num - normal_color_cnt;
ab2_bs_write(&os, normal_color_cnt, 16);
ab2_bs_write(&os, alpha_color_cnt, 16);
for (i = 0; i < normal_color_cnt; i++)
{
mycolor color = ab2enc_color_preprocess(abm_context->color_idx_tab.color_table[i], abm_context);
if (abm_context->abm_palette_bpp == 16)
{
ab2_bs_write(&os, ARGB8888_TO_RGB16(color), 16);
}
else if (abm_context->abm_palette_bpp == 24)
{
ab2_bs_write(&os, color & 0xffff, 16);
ab2_bs_write(&os, color >> 16, 8);
}
else
{
return 0;
}
} /* for (i = 0; i < normal_color_cnt; i++) */
/* flush buffer to align to 2-bytes */
ab2_bs_flush(&os);
/* align alpha color palette ptr to 4 bytes alignment in palette color format: 8888 */
if (abm_context->abm_palette_bpp == 24)
{
os.mem_ptr = ((U8 *)(((U32)(os.mem_ptr) + 3) & ~0x3));
}
for (; i < normal_color_cnt + alpha_color_cnt; i++)
{
mycolor color = ab2enc_color_preprocess(abm_context->color_idx_tab.color_table[i], abm_context);
if (abm_context->abm_palette_bpp == 16)
{
ab2_bs_write(&os, ARGB8888_TO_RGB16(color), 16);
ab2_bs_write(&os, A_OF_ARGB8888(color), 8);
}
else if (abm_context->abm_palette_bpp == 24)
{
ab2_bs_write(&os, color & 0xffff, 16);
ab2_bs_write(&os, color >> 16, 16);
}
else
{
return 0;
}
} /* for (; i < normal_color_cnt + alpha_color_cnt; i++) */
/* align to 2-bytes */
ab2_bs_flush(&os);
return ab2_bs_offset(&os, mem_ptr);
}
/*****************************************************************************
* FUNCTION
* ab2enc_rle_encode_body
* DESCRIPTION
* Encode body of ABM v2 image by RLE
* PARAMETERS
* mem_ptr [OUT] buffer to write header
* RETURNS
* Return the bytes written. If error, it will return 0
*****************************************************************************/
static U32 ab2enc_rle_encode_body(U8 *mem_ptr, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
ab2_bs_struct os;
U32 total_color_cnt;
U32 color_idx_bits;
U32 min_repeat_cnt;
U32 last_color_idx;
U32 counter;
U32 x0 = abm_context->abm_enc.x1;
U32 y0 = abm_context->abm_enc.y1;
U32 x1 = abm_context->abm_enc.x2;
U32 y1 = abm_context->abm_enc.y2;
U32 discount_start_x, discount_start_y;
U32 discount_sure;
U32 discount_cnt;
U32 i, j;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/*
* prepare encoded data
*/
ab2_bs_init(&os, mem_ptr);
/* 1 for transparent color */
/* caculate the color index bits and the threshole of repeat number*/
total_color_cnt = abm_context->color_idx_tab.color_num + 1;
color_idx_bits = log2_ceiling(total_color_cnt);
min_repeat_cnt = 1 + ((8 + (color_idx_bits - 1)) / color_idx_bits);
/*
* start to encode RLE body
*/
last_color_idx = GDI_IMAGE_AB2_INVALID_COLOR_IDX;
counter = 0;
discount_start_x = x0;
discount_start_y = y0;
discount_sure = 0;
discount_cnt = 0;
/* for all image pixels */
for (j = y0; j <= y1; j++)
{
for (i = x0; i <= x1; i++)
{
U32 color_idx = ab2enc_get_img_color_idx(i, j, abm_context);
if (color_idx != last_color_idx)
{
/* discontinue at current position */
if (counter < min_repeat_cnt)
{
/* repeat pixels is less than threshole,
* counted into discountinue pixels */
discount_sure = (discount_cnt != 0);
discount_cnt += counter;
}
else
{
/* output discount pixels if needed */
if (discount_cnt > 0)
{
if (discount_sure)
{
ab2_encode_rle_discount_code(
&os,
discount_start_x,
discount_start_y,
color_idx_bits,
discount_cnt,
abm_context);
}
else
{
U32 iscount_color_index =
ab2enc_get_img_color_idx(
discount_start_x,
discount_start_y,
abm_context);
ab2_encode_rle_repeat_code(
&os,
iscount_color_index,
color_idx_bits,
discount_cnt);
}
discount_sure = 0;
discount_cnt = 0;
}
/* output repeat pixels if needed */
if (counter > 0)
{
ab2_encode_rle_repeat_code(
&os,
last_color_idx,
color_idx_bits,
counter);
counter = 0;
}
discount_start_x = i;
discount_start_y = j;
}
last_color_idx = color_idx;
counter = 1;
}
else
{
/* same as previous pixel color */
counter++;
}
} /* for (i = x0; i <= x1; i++) */
} /* for(j = y0; j <= y1; j++) */
/*
* Send the retail data
*/
if (discount_cnt > 0)
{
if (counter < min_repeat_cnt)
{
discount_cnt += counter;
counter = 0;
}
ab2_encode_rle_discount_code(
&os,
discount_start_x,
discount_start_y,
color_idx_bits,
discount_cnt,
abm_context);
}
if (counter > 0)
{
ab2_encode_rle_repeat_code(
&os,
last_color_idx,
color_idx_bits,
counter);
}
/* flush all data of bitstream */
ab2_bs_flush(&os);
return ab2_bs_offset(&os, mem_ptr);
}
/*****************************************************************************
* FUNCTION
* ab2enc_encode
* DESCRIPTION
* ABM v2 encoder main routine.
* Must prepare the image data and color table by calling
* abmenc_build_color_index_table() before.
* PARAMETERS
* out_mem_p [OUT] output memory pointer
* RETURNS
* Total ABMv2 data size. If error, it will return 0xffffffff.
*****************************************************************************/
static U32 ab2enc_encode(U8* out_mem_p, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 ab2_header_size = 0;
U32 rle_header_size = 0;
U32 rle_body_size = 0;
U32 ab2_total_size = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* out of ABM limit, return a large size to prevent choosing ABM */
if (abm_context->color_idx_tab.color_num >= ABM_MAX_COLOR_NUM)
{
return 0xffffffff;
}
/* write header */
ab2_header_size = ab2enc_encode_header(out_mem_p, abm_context);
if (ab2_header_size == 0)
{
return 0xffffffff;
}
out_mem_p += ab2_header_size;
/* write AB2 RLE header and color table */
rle_header_size = ab2enc_rle_encode_header(out_mem_p, abm_context);
if (rle_header_size == 0)
{
return 0xffffffff;
}
out_mem_p += rle_header_size;
/* write AB2 RLE body */
rle_body_size = ab2enc_rle_encode_body(out_mem_p, abm_context);
if (rle_body_size == 0)
{
return 0xffffffff;
}
out_mem_p += rle_body_size;
ab2_total_size = ab2_header_size + rle_header_size + rle_body_size;
return ab2_total_size;
}
/*****************************************************************************
* FUNCTION
* abmenc_encode
* DESCRIPTION
* abm encoder main routine
* PARAMETERS
* out_mem_p [OUT] output memory pointer
* RETURNS
* ABM data size
*****************************************************************************/
static U32 abmenc_encode(U8* out_mem_p, ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 x, y, w, h, bottom_right_off_w, bottom_right_off_h;
S32 bound_box_w, bound_box_h;
S32 line_padding_bytes;
U8 *img_p, *mem8_p;
U32 p;
U32 bpp;
U16 *mem16_p;
S32 normal_color_num, alpha_color_num, color_num;
S32 i;
U32 abm_size;
U32 bmp_bpp;
mycolor c, color16;
mycolor a, r, g, b;
U32 bytes_per_pixel;
U32 bitstream_padding_byte;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
w = abm_context->abm_enc.bmp_width;
h = abm_context->abm_enc.bmp_height;
bmp_bpp = abm_context->abm_enc.bmp_bpp;
line_padding_bytes = abm_context->abm_enc.line_padding_bytes;
/* out of ABM limit, return a large size to prevent choosing ABM */
if (abm_context->color_idx_tab.color_num >= ABM_MAX_COLOR_NUM)
{
return 0xFFFFFFFF;
}
color_num = abm_context->color_idx_tab.color_num + 1; /* 1 for transparent color 0 */
normal_color_num = get_normal_color_num(abm_context) + 1; /* 1 for transparent color 0 */
alpha_color_num = color_num - normal_color_num;
bpp = log2_ceiling(color_num);
/*
* fill the ABM 12-byte header
*/
mem16_p = (U16*)out_mem_p;
mem16_p[0] = w;
mem16_p[1] = h;
/* store fully opaque flag on the width MSB */
if (abm_context->abm_enc.is_fully_opaque)
{
mem16_p[0] |= 0x8000;
}
if (abm_context->abm_palette_bpp!= 16)
{
mem16_p[1] |= 0x8000; /* bit 15 is 1 to indicate it's 24-bit palette. */
}
mem16_p[2] = normal_color_num; /* number of normal colors */
mem16_p[3] = alpha_color_num; /* number of alpha colors */
/* bounding box info header (width and height offsets, not bounding box coordinates) */
mem16_p[4] = (abm_context->abm_enc.y1 << 8) | abm_context->abm_enc.x1;
bottom_right_off_w = w - 1 - abm_context->abm_enc.x2;
bottom_right_off_h = h - 1 - abm_context->abm_enc.y2;
mem16_p[5] = (bottom_right_off_h << 8) | bottom_right_off_w;
mem16_p += 6;
mem8_p = (U8*)mem16_p;
/* output color palette */
for (i = 0; i < color_num - 1; i++)
{
c = abm_context->color_idx_tab.color_table[i];
a = A_OF_ARGB8888(c);
/* prevent blue channel from saturation to get rid of source key checking in the decoder */
if (abm_context->abm_palette_bpp== 16)
{ /* 16-bit */
color16 = ARGB8888_TO_RGB16(c);
if (B_OF_RGB16(color16) == 0x1F)
{
c -= 1 << 3; /* 5-bit B: 0x1F -> 0x1E */
}
}
else
{ /* 24-bit and 32-bit ABM both use 24-bit palette */
if (B_OF_ARGB8888(c) == 0xFF)
{
c -= 1; /* 8-bit B: 0xFF -> 0xFE */
}
}
/* pre-calculate alpha color */
r = R_OF_ARGB8888(c);
g = G_OF_ARGB8888(c);
b = B_OF_ARGB8888(c);
r = GDI_IMAGE_AB2_DIV_255(r * a);
g = GDI_IMAGE_AB2_DIV_255(g * a);
b = GDI_IMAGE_AB2_DIV_255(b * a);
c = RGB(r, g, b);
if (abm_context->abm_palette_bpp == 16)
{ /* 16-bit palette */
PUT_16(mem8_p, ARGB8888_TO_RGB16(c));
mem8_p += 2;
}
else
{ /* 24-bit palette */
PUT_24(mem8_p, c);
mem8_p += 3;
}
}
/* output 1-byte alpha values of alpha colors */
for (i = normal_color_num - 1; i < color_num - 1; i++)
{
/* inverted alpha due to precalculation */
*mem8_p++ = 255 - A_OF_ARGB8888(abm_context->color_idx_tab.color_table[i]);
}
/* add a padding byte to meet 2-byte alignment for the bitstream */
if ((U32)mem8_p & 1)
{
/* padding with 0xFF */
*mem8_p++ = 0xFF;
bitstream_padding_byte = 1;
}
else
{
bitstream_padding_byte = 0;
}
img_p = abm_context->abm_enc.bmp_pixel_start_p;
bytes_per_pixel = abm_context->abm_enc.bmp_bpp / 8;
/* calculate bounding box width and height */
bound_box_w = abm_context->abm_enc.x2 - abm_context->abm_enc.x1 + 1;
bound_box_h = abm_context->abm_enc.y2 - abm_context->abm_enc.y1 + 1;
/* initialize the bitstream writer */
BW_INIT((U16*)mem8_p);
/* 2nd pass, output ABM packed pixels, and do vertical flipping (BMP nature) */
for (y = abm_context->abm_enc.y1; y <= abm_context->abm_enc.y2; y++)
{
for (x = abm_context->abm_enc.x1; x <= abm_context->abm_enc.x2; x++)
{
p = ab2enc_get_img_color_idx(x, y, abm_context);
/* write the ABM pixel data into the bitstream */
BW_WRITE(p, bpp);
}
}
/* flush bitstream writer */
BW_END();
abm_size = ABM_HEADER_SIZE + /* header size */
(color_num - 1) * abm_context->abm_palette_bpp / 8 + /* palette size */
alpha_color_num + /* 8-bit alpha channel */
bitstream_padding_byte + /* a padding byte or not */
(bound_box_w * bound_box_h * bpp + 15) / 16 * 16 / 8; /* pixel data size (2-byte align) */
return abm_size;
}
/*****************************************************************************
* FUNCTION
* caculate_abm_size
* DESCRIPTION
* caculate the image in ABM format size without data encoding.
* PARAMETERS
* width [IN] width of image
* height [IN] height of image
* color_cnt [IN] bits per pixel of image
* RETURNS
* The result size
*****************************************************************************/
static U32 caculate_abm_size(ABM_Context * abm_context)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 bound_width, bound_height;
U32 normal_color_cnt, alpha_color_cnt;
U32 color_index_bits;
U32 palette_size;
U32 stream_size;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
bound_width = abm_context->abm_enc.x2 - abm_context->abm_enc.x1 + 1;
bound_height = abm_context->abm_enc.y2 - abm_context->abm_enc.y1 + 1;
normal_color_cnt = get_normal_color_num(abm_context);
alpha_color_cnt = abm_context->color_idx_tab.color_num - normal_color_cnt;
color_index_bits = log2_ceiling(1 + normal_color_cnt + alpha_color_cnt);
palette_size =
(normal_color_cnt + alpha_color_cnt) * abm_context->abm_palette_bpp / 8 + /* palette size */
alpha_color_cnt; /* 8-bit alpha channel */
palette_size = (palette_size + 1) & ~0x1; /* 2-bytes alignment */
stream_size = ((bound_width * bound_height * color_index_bits + 7) / 8);
stream_size = (stream_size + 1) & ~0x1; /* 2-bytes alignment */
return ABM_HEADER_SIZE + palette_size + stream_size;
}
/*****************************************************************************
* FUNCTION
* caculate_pbm_size
* DESCRIPTION
* caculate the image in PBM format size without data encoding.
* PARAMETERS
* width [IN] width of image
* height [IN] height of image
* bpp [IN] bits per pixel of image
* RETURNS
* The result size
*****************************************************************************/
static U32 caculate_pbm_size(U32 width, U32 height, U32 bpp)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
return PBM_HEADER_SIZE + width * height * bpp / 8;
}
/*****************************************************************************
* FUNCTION
* ABM_load
* DESCRIPTION
* ABM encoder interface
* PARAMETERS
* in_bmp_filename [IN] input bitmap filename
* dev_bmp_bpp [IN] PBM color depth in bits
* image_type [IN] for decision whether to use ABM
* option [IN] select file format can be return
* data [OUT] output memory
* size [OUT] size of ABM image
* width [OUT] width of ABM image
* height [OUT] height of ABM image
* RETURNS
* return code to use ABM or not
* + ABM_ENC_RETURN_KEEP_ORIGINAL 0 donot use ABM
* + ABM_ENC_RETURN_USE_ABM 1 use ABM
* + ABM_ENC_RETURN_USE_AB2 2 use ABMv2
*****************************************************************************/
S32 ABM_load(
U8 *in_bmp_filename,
S32 dev_bmp_bpp,
U8 image_type,
abm_enc_option_flag_type option,
U8 *data,
S32 *size,
S32 *width,
S32 *height,
MMI_BOOL is9slice,
SetImageProcessData * processData)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U32 input_bmp_file_size;
U32 ret = ABM_ENC_RETURN_KEEP_ORIGINAL;
U32 ret_size = ABM_ENC_MAX_FILE_SIZE;
ABM_Context * abm_context;
U8 *file_buf;
U32 normal_color_cnt;
U32 alpha_color_cnt;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* no file format can be encode,
do nothing and return the original format */
#if !defined(__OP01_FWPBW__)
if (option == 0)
#endif
{
return ABM_ENC_RETURN_KEEP_ORIGINAL;
}
/* no support bpps lower than 16-bit,
do nothing and return the original format */
#if defined(DRV_MAINLCD_BIT_PER_PIXEL) && (DRV_MAINLCD_BIT_PER_PIXEL < 16)
return ABM_ENC_RETURN_KEEP_ORIGINAL;
#endif
abm_context = malloc(sizeof(ABM_Context));
memset(abm_context, 0, sizeof(ABM_Context));
/* initialize color index table and abm encoder context */
abm_context->color_idx_tab.color_num = 0;
memset(&abm_context->abm_enc, 0, sizeof(abm_enc_struct));
abm_context->abm_palette_bpp = ABM_PALETTE_BPP;
abm_context->abm_enc.is_fully_opaque = 1;
/* enable source key color only for PBM and BMP */
if ((image_type == IMAGE_TYPE_DEVICE_BITMAP) || (image_type == IMAGE_TYPE_BMP))
{
abm_context->abm_enc.src_key_color_enable = 1;
}
/* It is necessary to identify PNG to BMP or originally BMP for 32-bit BMP files. */
if (image_type != IMAGE_TYPE_PNG)
{
abm_context->abm_enc.is_windows_bmp = 1;
}
/* load input bitmap file */
file_buf = malloc(ABMENC_BMP_FILE_BUFFER_SIZE);
input_bmp_file_size = load_bmp_file(file_buf, in_bmp_filename, abm_context);
if (input_bmp_file_size == 0)
{
free(file_buf);
free(abm_context);
return ABM_ENC_RETURN_KEEP_ORIGINAL;
}
#if defined(__RESOURCE_GEN_) && defined(__COSMOS_MMI__)
if (is9slice)
{
option &= ~ABM_ENC_OPTION_FLAG_AB2;
option |= ABM_ENC_OPTION_FLAG_24BIT;
abm_context->abm_palette_bpp = 24;
}
#endif
/* Prepare image data and color table */
ret = abmenc_build_color_index_table(abm_context);
/* Check whether the normal/alpha color number exceed AB2/ABM design limitation or not. */
/* If so, reduce color depth to 16bits to make sure resource can be genreated. */
normal_color_cnt = get_normal_color_num(abm_context);
alpha_color_cnt = abm_context->color_idx_tab.color_num - normal_color_cnt;
if (normal_color_cnt > 65535 || alpha_color_cnt > 65535)
{
free(abm_context);
free(file_buf);
abm_context = malloc(sizeof(ABM_Context));
memset(abm_context, 0, sizeof(ABM_Context));
/* initialize color index table and abm encoder context */
abm_context->color_idx_tab.color_num = 0;
memset(&abm_context->abm_enc, 0, sizeof(abm_enc_struct));
abm_context->abm_palette_bpp = 16;
abm_context->abm_enc.is_fully_opaque = 1;
/* enable source key color only for PBM and BMP */
if ((image_type == IMAGE_TYPE_DEVICE_BITMAP) || (image_type == IMAGE_TYPE_BMP))
{
abm_context->abm_enc.src_key_color_enable = 1;
}
/* It is necessary to identify PNG to BMP or originally BMP for 32-bit BMP files. */
if (image_type != IMAGE_TYPE_PNG)
{
abm_context->abm_enc.is_windows_bmp = 1;
}
/* load input bitmap file */
file_buf = malloc(ABMENC_BMP_FILE_BUFFER_SIZE);
input_bmp_file_size = load_bmp_file(file_buf, in_bmp_filename, abm_context);
if (input_bmp_file_size == 0)
{
free(file_buf);
free(abm_context);
return ABM_ENC_RETURN_KEEP_ORIGINAL;
}
ret = abmenc_build_color_index_table(abm_context);
}
/* Save color number for image information report */
if(processData!=NULL){
processData->color_num = abm_context->color_idx_tab.color_num;
}
if (ret != ABM_ENC_OK)
{
free(file_buf);
free(abm_context);
ABMLDR_LOG_E("[ABM enc] Unique color number exceed limitation(384000):%s", in_bmp_filename);
return ABM_ENC_INPUT_BITMAP_ERROR;
}
*width = abm_context->abm_enc.bmp_width;
*height = abm_context->abm_enc.bmp_height;
do
{
#if defined(__COSMOS_MMI__)
if (option & ABM_ENC_OPTION_FLAG_24BIT)
{
ret_size = abmenc_encode(data, abm_context);
ret = ABM_ENC_RETURN_USE_ABM;
break;
}
#endif
//#if (MAIN_MEDIA_LAYER_BITS_PER_PIXEL != 24)
/* check if AB2 size is smaller */
if (option & ABM_ENC_OPTION_FLAG_AB2)
{
U32 ab2_size = ab2enc_encode(data, abm_context);
if (ab2_size < ret_size)
{
ret_size = ab2_size;
ret = ABM_ENC_RETURN_USE_AB2;
}
}
//#endif
/* check if ABM size is smaller */
if (option & ABM_ENC_OPTION_FLAG_ABM)
{
U32 abm_size = caculate_abm_size(abm_context);
if (abm_size < ret_size)
{
ret_size = abmenc_encode(data, abm_context);
ret = ABM_ENC_RETURN_USE_ABM;
}
}
/* check if original size is smaller */
if (option & ABM_ENC_OPTION_FLAG_ORIGINAL)
{
switch (image_type)
{
case IMAGE_TYPE_PNG:
{
/* no information, do nothing */
break;
}
case IMAGE_TYPE_BMP:
{
if (input_bmp_file_size < ret_size)
{
ret_size = input_bmp_file_size;
ret = ABM_ENC_RETURN_KEEP_ORIGINAL;
}
break;
}
case IMAGE_TYPE_DEVICE_BITMAP:
{
/*
U32 pbm_size = caculate_pbm_size(*width, *height, dev_bmp_bpp);
if (pbm_size < ret_size)
{
ret_size = pbm_size;
ret = ABM_ENC_RETURN_KEEP_ORIGINAL;
}
*/
break;
}
default:
/* not supported, do nothing */
break;
}
} /* if (option & ABM_ENC_OPTION_FLAG_ORIGINAL) */
}while(0);
#if defined(__RESOURCE_GEN_) && defined(__COSMOS_MMI__)
if (is9slice)
{
abm_context->abm_palette_bpp = ABM_PALETTE_BPP;
}
#endif
/* Error handling */
if (ret_size == ABM_ENC_MAX_FILE_SIZE)
{
*width = 0;
*height = 0;
*size = 0;
free(file_buf);
free(abm_context);
return ABM_ENC_RETURN_KEEP_ORIGINAL;
}
*size = (S32)ret_size;
free(file_buf);
free(abm_context);
return ret;
}
/*
* RESOURCE GENERATOR ONLY FUNCTIONS
*/
#if defined(__RESOURCE_GEN_)
#ifdef ABM_ENC_UNIT_TEST
#include <time.h>
/*
* unit test for abm encoder
*
* return 0: successful, otherwise: failed
*/
S32 abmenc_unit_test(void)
{
S32 si, i;
abm_context->color_idx_tab.color_num = 0;
srand((unsigned)time(NULL));
si = search_color(ARGB(255, 255, 255, 255));
add_color_if_new(ARGB(255, 255, 255, 255));
si = search_color(ARGB(255, 255, 255, 255));
add_color_if_new(ARGB(255, 255, 255, 254));
si = search_color(ARGB(255, 255, 255, 0));
add_color_if_new(ARGB(0, 255, 255, 254));
si = search_color(ARGB(255, 255, 255, 254));
for (i = 0; i < 1000; i++)
{
add_color_if_new(rand());
}
/* make sure the color table is descending */
for (i = 0; i < abm_context->color_idx_tab.color_num - 1; i++)
{
if (abm_context->color_idx_tab.color_table[i + 1] > abm_context->color_idx_tab.color_table[i])
{
return ABMENC_UNIT_TEST_FAILED;
}
}
return ABMENC_UNIT_TEST_SUCCESSFUL;
}
#endif /* ABM_ENC_UNIT_TEST */
#if defined(__MMI_RESOURCE_IMAGE_COMPRESS__)
extern U8* mmi_rg_compress_image(U8 *buffer, S32 org_size, S32 *dest_size, S32 image_type, U32 width_height, U8* symbol, image_type_enum extImageFlag, BOOL is9slice, U8* _9slice_filename, int n_frames);
extern U8* mmi_rg_output_compressed_image(U8 *dest_buf, S32 dest_size, FILE *outfile);
#endif /* __MMI_RESOURCE_IMAGE_COMPRESS__ */
/*****************************************************************************
* FUNCTION
* ABMLoader
* DESCRIPTION
* ABM loader
* PARAMETERS
* in_filename [IN] input filename
* out_filename [IN] output filename
* encode_option [IN] encode options
* RETURNS
* return value
*****************************************************************************/
S32 ABMLoader(U8 *in_filename, U8 *out_filename, S32 encode_option, MMI_BOOL is9slice, U8* _9slice_filename, SetImageProcessData * processData)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
U8 *buffer = NULL/*[ABMENC_BMP_FILE_BUFFER_SIZE]*/;
S32 width, height, size, ret, i;
U32 width_height;
FILE *outfile;
U8 image_type = 0;
#ifdef __MMI_RESOURCE_ENFB_SUPPORT__
U32 enfb_width, enfb_height;
S32 enfb_ret;
#endif
S32 data_size;
// The quality may be OFFLINE_IMAGE_PARAM_QUALITY_HIGH, OFFLINE_IMAGE_PARAM_QUALITY_MEDIUM or OFFLINE_IMAGE_PARAM_QUALITY_LOW
S32 quality = OFFLINE_IMAGE_PARAM_QUALITY_MEDIUM;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if(in_filename != NULL)
{
ABMLDR_LOG_V("ABMLoader: trying to load %s", in_filename);
}
/* reduce call stack size and avoid the risk of ABMENC_BMP_FILE_BUFFER_SIZE increase */
buffer = malloc(ABMENC_BMP_FILE_BUFFER_SIZE);
memset(buffer, 0, ABMENC_BMP_FILE_BUFFER_SIZE);
/* Retrive quality passed from Resgen*/
quality = processData->quality;
ABMLDR_LOG_D("[ABMLoader][%s] quality = %d\n",in_filename, quality);
/* temp solution:
* check whether the input file is PBM */
{
int l;
l = strlen(in_filename);
if ((in_filename[l - 1] == 'M') &&
(in_filename[l - 2] == 'B') &&
(in_filename[l - 3] == 'P'))
{
image_type = IMAGE_TYPE_DEVICE_BITMAP;
}
else if ((in_filename[l - 1] == 'P') &&
(in_filename[l - 2] == 'M') &&
(in_filename[l - 3] == 'B'))
{
image_type = IMAGE_TYPE_BMP;
}
else if ((in_filename[l - 1] == 'A') &&
(in_filename[l - 2] == 'P') &&
(in_filename[l - 3] == 'M') &&
(in_filename[l - 4] == 'B'))
{
image_type = IMAGE_TYPE_PNG;
}
}
/*
* After we tested on the porject with hardware PNG, the PNG is larger
* and slower than ABM/ABMv2. So we disable this checking.
*/
#if 0
#ifdef GDI_USING_HW_PNG
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* GDI_USING_HW_PNG */
#endif
ret = ABM_load(
in_filename,
__MMI_DEVICE_BMP_FORMAT__,
image_type,
encode_option,
buffer,
&size,
&width,
&height,
is9slice,
processData);
switch(ret)
{
case ABM_ENC_RETURN_USE_ABM:
image_type = IMAGE_TYPE_ABM;
break;
case ABM_ENC_RETURN_USE_AB2:
image_type = IMAGE_TYPE_AB2;
break;
default:
free(buffer);
return ret;
}
if ((size >> 24) > 0)
{
ABMLDR_LOG_E("image is too big:%s\n", in_filename);
}
processData->res_size = size + 8;
width_height = (((U32)width & 0XFFF) << 12) | ((U32)height & 0XFFF);
#ifdef __MMI_RESOURCE_ENFB_SUPPORT__
enfb_ret = Image_Test(in_filename, &enfb_width, &enfb_height, processData->disable_enfb);
if (enfb_ret != ENFB_IMAGE_NONE)
{
processData->enfb_flag = 1;
if (enfb_ret == ENFB_IMAGE_ASSOCIATE)
{
ENFBAssociatedIDAdded = MMI_TRUE;
}
}
#endif /* __MMI_RESOURCE_ENFB_SUPPORT__ */
if (toolFlag != 1)
{
/* open output file */
outfile = fopen(out_filename, "wb+");
if (outfile == NULL)
{
ABMLDR_LOG_E("\nError creating %s", out_filename);
free(buffer);
return 0;
}
}
data_size = size;
if (is9slice)
{
data_size += 20;
}
/* write to CustPack */
if (toolFlag == 1)
{
/* resource header */
if (is9slice)
fprintf(dest_file, "%c", IMAGE_TYPE_9SLICE);
else
fprintf(dest_file, "%c", image_type);
fprintf(dest_file, "%c", 0x01);
fprintf(dest_file, "%c%c%c",
(data_size & 0x000000ff),
(data_size & 0x0000ff00) >> 8,
(data_size & 0x00ff0000) >> 16);
fprintf(dest_file, "%c%c%c",
(width_height & 0x000000ff),
(width_height & 0x0000ff00) >> 8,
(width_height & 0x00ff0000) >> 16);
if (is9slice)
{
/* Write 9slice file header */
ResgenImageOutStream * outstream;
ImageOutStreamInit(&outstream);
mmi_rg_write_9slice_header(image_type, _9slice_filename, outstream, MMI_TRUE);
ImageOutStreamOutputFile(outstream, dest_file);
ImageOutStreamDeinit(&outstream);
}
/* ABM raw data */
for (i = 0; i < size; i++)
{
fprintf(dest_file, "%c", buffer[i]);
}
}
#ifdef __MMI_RESOURCE_ENFB_SUPPORT__
/* write ENFB header to CustImgDataxxx.h and
* image header/data to ENFB image data file */
else if (processData->enfb_flag == 1)
{
enfb_size = 8 + data_size;
/* write ENFB header to CustImgDataxxx.h */
fprintf(outfile,
"\n{\t0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\t};\n",
(U8) 255, //type
(U8) ENFBAssociatedIDAdded, //associated id
(U8) 0, //reserved
(U8) 0, //reserved
(U8) (enfb_offset & 0xff),
(U8) ((enfb_offset >> 8) & 0xff),
(U8) ((enfb_offset >> 16) & 0xff),
(U8) ((enfb_offset >> 24) & 0xff),
(U8) (enfb_size & 0xff),
(U8) ((enfb_size >> 8) & 0xff),
(U8) ((enfb_size >> 16) & 0xff),
(U8) ((enfb_size >> 24) & 0xff));
/* image header/data to ENFB image data file */
/* resource header */
if (is9slice)
fprintf(enfb_img_data_file, "%c", IMAGE_TYPE_9SLICE);
else
fprintf(enfb_img_data_file, "%c", image_type);
fprintf(enfb_img_data_file, "%c", 0x01);
fprintf(enfb_img_data_file,
"%c%c%c",
(data_size & 0x000000ff),
(data_size & 0x0000ff00) >> 8,
(data_size & 0x00ff0000) >> 16);
fprintf(enfb_img_data_file,
"%c%c%c",
(width_height & 0x000000ff),
(width_height & 0x0000ff00) >> 8,
(width_height & 0x00ff0000) >> 16);
if (is9slice)
{
/* Write 9slice file header */
mmi_rg_write_9slice_header(image_type, _9slice_filename, enfb_img_data_file, MMI_TRUE);
}
for (i = 0; i < size; i++)
{
fprintf(enfb_img_data_file, "%c", buffer[i]);
}
enfb_offset += enfb_size;
processData->enfb_flag = 0;
}
#endif /* __MMI_RESOURCE_ENFB_SUPPORT__ */
else
{
U8 *dest_buf;
S32 dest_size = 0;
fprintf(outfile, "{\n\t");
#if defined(__MMI_RESOURCE_IMAGE_COMPRESS__)
dest_buf = mmi_rg_compress_image(buffer, size, &dest_size, image_type, width_height, in_filename, SYSTEM_IMAGE, is9slice, _9slice_filename, 1);
if (dest_buf != NULL)
{
mmi_rg_output_compressed_image(dest_buf, dest_size, outfile);
}
else
#endif /* __MMI_RESOURCE_IMAGE_COMPRESS__ */
{
/* resource header */
if (is9slice)
fprintf(outfile, "0x%02X, ", IMAGE_TYPE_9SLICE);
else
fprintf(outfile, "0x%02X, ", image_type);
fprintf(outfile, "0x%02X, ", 0x01);
fprintf(outfile,
"0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X,",
(data_size & 0x000000ff),
(data_size & 0x0000ff00) >> 8,
(data_size & 0x00ff0000) >> 16,
(width_height & 0x000000ff),
(width_height & 0x0000ff00) >> 8,
(width_height & 0x00ff0000) >> 16);
if (is9slice)
{
/* Write 9slice file header */
ResgenImageOutStream * outstream;
ImageOutStreamInit(&outstream);
mmi_rg_write_9slice_header(image_type, _9slice_filename, outstream, MMI_FALSE);
ImageOutStreamOutputFile(outstream, outfile);
ImageOutStreamDeinit(&outstream);
}
for (i = 0; i < size; i++)
{
if ((i % 16) == 0)
{
fprintf(outfile, "\n\t");
}
fprintf(outfile, "0x%02X, ", buffer[i]);
}
}
fprintf(outfile, "\n};");
}
if (toolFlag != 1)
{
fclose(outfile);
}
// TODO: Fix the return code
free(buffer);
return ABM_ENC_RETURN_USE_ABM;
}
#endif /* defined(__RESOURCE_GEN_) */
#else /* !defined(__MTK_TARGET__) || defined(__RESOURCE_GEN_) */
/* avoid the target compilation warning */
char g_abm_loader_avoid_warning;
#endif /* !defined(__MTK_TARGET__) || defined(__RESOURCE_GEN_) */