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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$id $
*
* Copyright (C) 2009 by Christophe Gouiran <bechris13250 -at- gmail -dot- com>
*
* Based on lodepng, a lightweight png decoder/encoder
* (c) 2005-2008 Lode Vandevenne
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*
LodePNG version 20080927
Copyright (c) 2005-2008 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
/*
The manual and changelog can be found in the header file "lodepng.h"
You are free to name this file lodepng.cpp or lodepng.c depending on your usage.
*/
#include "plugin.h"
#include "lcd.h"
#include <lib/playback_control.h>
#include <lib/helper.h>
#include <lib/configfile.h>
#include <lib/grey.h>
#include <lib/pluginlib_bmp.h>
#include "zlib.h"
#include "png.h"
PLUGIN_HEADER
/* ////////////////////////////////////////////////////////////////////////// */
/* LodeFlate & LodeZlib Setting structs */
/* ////////////////////////////////////////////////////////////////////////// */
typedef struct LodePNG_InfoColor /*info about the color type of an image*/
{
/*header (IHDR)*/
unsigned colorType; /*color type*/
unsigned bitDepth; /*bits per sample*/
/*palette (PLTE)*/
unsigned char palette[256 * 4]; /*palette in RGBARGBA... order*/
size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/
/*transparent color key (tRNS)*/
unsigned key_defined; /*is a transparent color key given?*/
unsigned key_r; /*red component of color key*/
unsigned key_g; /*green component of color key*/
unsigned key_b; /*blue component of color key*/
} LodePNG_InfoColor;
typedef struct LodePNG_Time /*LodePNG's encoder does not generate the current time. To make it add a time chunk the correct time has to be provided*/
{
unsigned year; /*2 bytes*/
unsigned char month; /*1-12*/
unsigned char day; /*1-31*/
unsigned char hour; /*0-23*/
unsigned char minute; /*0-59*/
unsigned char second; /*0-60 (to allow for leap seconds)*/
} LodePNG_Time;
typedef struct LodePNG_InfoPng /*information about the PNG image, except pixels and sometimes except width and height*/
{
/*header (IHDR), palette (PLTE) and transparency (tRNS)*/
unsigned width; /*width of the image in pixels (ignored by encoder, but filled in by decoder)*/
unsigned height; /*height of the image in pixels (ignored by encoder, but filled in by decoder)*/
unsigned compressionMethod; /*compression method of the original file*/
unsigned filterMethod; /*filter method of the original file*/
unsigned interlaceMethod; /*interlace method of the original file*/
LodePNG_InfoColor color; /*color type and bits, palette, transparency*/
/*suggested background color (bKGD)*/
unsigned background_defined; /*is a suggested background color given?*/
unsigned background_r; /*red component of suggested background color*/
unsigned background_g; /*green component of suggested background color*/
unsigned background_b; /*blue component of suggested background color*/
/*time chunk (tIME)*/
unsigned char time_defined; /*if 0, no tIME chunk was or will be generated in the PNG image*/
LodePNG_Time time;
/*phys chunk (pHYs)*/
unsigned phys_defined; /*is pHYs chunk defined?*/
unsigned phys_x;
unsigned phys_y;
unsigned char phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/
} LodePNG_InfoPng;
typedef struct LodePNG_InfoRaw /*contains user-chosen information about the raw image data, which is independent of the PNG image*/
{
LodePNG_InfoColor color;
} LodePNG_InfoRaw;
typedef struct LodePNG_DecodeSettings
{
unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/
} LodePNG_DecodeSettings;
typedef struct LodePNG_Decoder
{
LodePNG_DecodeSettings settings;
LodePNG_InfoRaw infoRaw;
LodePNG_InfoPng infoPng; /*info of the PNG image obtained after decoding*/
long error;
char error_msg[128];
int x,y;
int width,height;
} LodePNG_Decoder;
#define VERSION_STRING "20080927"
/* Min memory allowing us to use the plugin buffer
* and thus not stopping the music
* *Very* rough estimation:
* Max 10 000 dir entries * 4bytes/entry (char **) = 40000 bytes
* + 30k code size = 70 000
* + 50k min for png = 130 000
*/
#define MIN_MEM 130000
/* Headings */
#define DIR_PREV 1
#define DIR_NEXT -1
#define DIR_NONE 0
/* decompressed image in the possible sizes (1,2,4,8), wasting the other */
static fb_data *disp[9];
/* up to here currently used by image(s) */
static fb_data *disp_buf;
/* my memory pool (from the mp3 buffer) */
static char print[128]; /* use a common snprintf() buffer */
unsigned char *memory, *memory_max;
static size_t memory_size;
static unsigned char *image; /* where we put the content of the file */
static size_t image_size;
static fb_data *converted_image; /* the (color) converted image */
static size_t converted_image_size;
static unsigned char *decoded_image; /* the decoded image */
static size_t decoded_image_size;
static fb_data *resized_image; /* the decoded image */
static struct tree_context *tree;
/* the current full file name */
static char np_file[MAX_PATH];
static int curfile = 0, direction = DIR_NONE, entries = 0;
static LodePNG_Decoder decoder;
/* list of the png files */
static char **file_pt;
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
/* are we using the plugin buffer or the audio buffer? */
static bool plug_buf = true;
#endif
/* Persistent configuration */
#define PNG_CONFIGFILE "png.cfg"
#define PNG_SETTINGS_MINVERSION 1
#define PNG_SETTINGS_VERSION 1
/* Slideshow times */
#define SS_MIN_TIMEOUT 1
#define SS_MAX_TIMEOUT 20
#define SS_DEFAULT_TIMEOUT 5
struct png_settings
{
int ss_timeout;
};
static struct png_settings png_settings =
{
SS_DEFAULT_TIMEOUT
};
static struct png_settings old_settings;
static struct configdata png_config[] =
{
{ TYPE_INT, SS_MIN_TIMEOUT, SS_MAX_TIMEOUT,
{ .int_p = &png_settings.ss_timeout }, "Slideshow Time", NULL
},
};
#if LCD_DEPTH > 1
static fb_data* old_backdrop;
#endif
static int slideshow_enabled = false; /* run slideshow */
static int running_slideshow = false; /* loading image because of slideshw */
#ifndef SIMULATOR
static int immediate_ata_off = false; /* power down disk after loading */
#endif
static unsigned ds, ds_min, ds_max; /* downscaling and limits */
/*
The two functions below (LodePNG_decompress and LodePNG_compress) directly call the
LodeZlib_decompress and LodeZlib_compress functions. The only purpose of the functions
below, is to provide the ability to let LodePNG use a different Zlib encoder by only
changing the two functions below, instead of changing it inside the vareous places
in the other LodePNG functions.
*out must be NULL and *outsize must be 0 initially, and after the function is done,
*out must point to the decompressed data, *outsize must be the size of it, and must
be the size of the useful data in bytes, not the alloc size.
*/
static unsigned LodePNG_decompress(unsigned char* out, size_t* outsize, const unsigned char* in, size_t insize, char *error_msg)
{
z_stream stream;
int err;
rb->strcpy(error_msg, "");
stream.next_in = (Bytef*)in;
stream.avail_in = (uInt)insize;
stream.next_out = out;
stream.avail_out = (uInt)*outsize;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit(&stream);
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*outsize = stream.total_out;
err = inflateEnd(&stream);
if (stream.msg != Z_NULL)
rb->strcpy(error_msg, stream.msg);
return err;
}
/* ////////////////////////////////////////////////////////////////////////// */
/* / Reading and writing single bits and bytes from/to stream for LodePNG / */
/* ////////////////////////////////////////////////////////////////////////// */
static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream)
{
unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
(*bitpointer)++;
return result;
}
static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
{
unsigned result = 0;
size_t i;
for (i = nbits - 1; i < nbits; i--) result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i;
return result;
}
static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
{
/*the current bit in bitstream must be 0 for this to work*/
if (bit) bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/
(*bitpointer)++;
}
static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
{
/*the current bit in bitstream may be 0 or 1 for this to work*/
if (bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
(*bitpointer)++;
}
static unsigned LodePNG_read32bitInt(const unsigned char* buffer)
{
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
}
/* ////////////////////////////////////////////////////////////////////////// */
/* / PNG chunks / */
/* ////////////////////////////////////////////////////////////////////////// */
unsigned LodePNG_chunk_length(const unsigned char* chunk) /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/
{
return LodePNG_read32bitInt(&chunk[0]);
}
void LodePNG_chunk_type(char type[5], const unsigned char* chunk) /*puts the 4-byte type in null terminated string*/
{
unsigned i;
for (i = 0; i < 4; i++) type[i] = chunk[4 + i];
type[4] = 0; /*null termination char*/
}
unsigned char LodePNG_chunk_type_equals(const unsigned char* chunk, const char* type) /*check if the type is the given type*/
{
if (type[4] != 0) return 0;
return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
}
/*properties of PNG chunks gotten from capitalization of chunk type name, as defined by the standard*/
unsigned char LodePNG_chunk_critical(const unsigned char* chunk) /*0: ancillary chunk, 1: it's one of the critical chunk types*/
{
return((chunk[4] & 32) == 0);
}
unsigned char LodePNG_chunk_private(const unsigned char* chunk) /*0: public, 1: private*/
{
return((chunk[6] & 32) != 0);
}
unsigned char LodePNG_chunk_safetocopy(const unsigned char* chunk) /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy*/
{
return((chunk[7] & 32) != 0);
}
unsigned char* LodePNG_chunk_data(unsigned char* chunk) /*get pointer to the data of the chunk*/
{
return &chunk[8];
}
const unsigned char* LodePNG_chunk_data_const(const unsigned char* chunk) /*get pointer to the data of the chunk*/
{
return &chunk[8];
}
unsigned LodePNG_chunk_check_crc(const unsigned char* chunk) /*returns 0 if the crc is correct, error code if it's incorrect*/
{
unsigned length = LodePNG_chunk_length(chunk);
unsigned CRC = LodePNG_read32bitInt(&chunk[length + 8]);
unsigned checksum = crc32(0L, &chunk[4], length + 4); /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
if (CRC != checksum) return 1;
else return 0;
}
unsigned char* LodePNG_chunk_next(unsigned char* chunk) /*don't use on IEND chunk, as there is no next chunk then*/
{
unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
return &chunk[total_chunk_length];
}
const unsigned char* LodePNG_chunk_next_const(const unsigned char* chunk) /*don't use on IEND chunk, as there is no next chunk then*/
{
unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
return &chunk[total_chunk_length];
}
/* ////////////////////////////////////////////////////////////////////////// */
/* / Color types and such / */
/* ////////////////////////////////////////////////////////////////////////// */
/*return type is a LodePNG error code*/
static unsigned checkColorValidity(unsigned colorType, unsigned bd) /*bd = bitDepth*/
{
switch (colorType)
{
case 0:
if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/
case 2:
if (!( bd == 8 || bd == 16)) return 37; break; /*RGB*/
case 3:
if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/
case 4:
if (!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/
case 6:
if (!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/
default:
return 31;
}
return 0; /*allowed color type / bits combination*/
}
static unsigned getNumColorChannels(unsigned colorType)
{
switch (colorType)
{
case 0:
return 1; /*grey*/
case 2:
return 3; /*RGB*/
case 3:
return 1; /*palette*/
case 4:
return 2; /*grey + alpha*/
case 6:
return 4; /*RGBA*/
}
return 0; /*unexisting color type*/
}
static unsigned getBpp(unsigned colorType, unsigned bitDepth)
{
return getNumColorChannels(colorType) * bitDepth; /*bits per pixel is amount of channels * bits per channel*/
}
/* ////////////////////////////////////////////////////////////////////////// */
void LodePNG_InfoColor_init(LodePNG_InfoColor* info)
{
info->key_defined = 0;
info->key_r = info->key_g = info->key_b = 0;
info->colorType = 6;
info->bitDepth = 8;
memset(info->palette, 0, 256 * 4 * sizeof(unsigned char));
info->palettesize = 0;
}
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info)
{
info->palettesize = 0;
}
unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor* info) { return getBpp(info->colorType, info->bitDepth); } /*calculate bits per pixel out of colorType and bitDepth*/
unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor* info) { return info->colorType == 0 || info->colorType == 4; }
unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor* info1, const LodePNG_InfoColor* info2)
{
return info1->colorType == info2->colorType
&& info1->bitDepth == info2->bitDepth; /*palette and color key not compared*/
}
void LodePNG_InfoPng_init(LodePNG_InfoPng* info)
{
info->width = info->height = 0;
LodePNG_InfoColor_init(&info->color);
info->interlaceMethod = 0;
info->compressionMethod = 0;
info->filterMethod = 0;
info->background_defined = 0;
info->background_r = info->background_g = info->background_b = 0;
info->time_defined = 0;
info->phys_defined = 0;
}
void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info)
{
LodePNG_InfoColor_cleanup(&info->color);
}
unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor* dest, const LodePNG_InfoColor* source)
{
size_t i;
LodePNG_InfoColor_cleanup(dest);
*dest = *source;
for (i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i];
return 0;
}
unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng* dest, const LodePNG_InfoPng* source)
{
unsigned error = 0;
LodePNG_InfoPng_cleanup(dest);
*dest = *source;
LodePNG_InfoColor_init(&dest->color);
error = LodePNG_InfoColor_copy(&dest->color, &source->color); if (error) return error;
return error;
}
void LodePNG_InfoPng_swap(LodePNG_InfoPng* a, LodePNG_InfoPng* b)
{
LodePNG_InfoPng temp = *a;
*a = *b;
*b = temp;
}
void LodePNG_InfoRaw_init(LodePNG_InfoRaw* info)
{
LodePNG_InfoColor_init(&info->color);
}
void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw* info)
{
LodePNG_InfoColor_cleanup(&info->color);
}
unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw* dest, const LodePNG_InfoRaw* source)
{
unsigned error = 0;
LodePNG_InfoRaw_cleanup(dest);
*dest = *source;
LodePNG_InfoColor_init(&dest->color);
error = LodePNG_InfoColor_copy(&dest->color, &source->color); if (error) return error;
return error;
}
/* ////////////////////////////////////////////////////////////////////////// */
/*
converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code
the out buffer must have (w * h * bpp + 7) / 8 bytes, where bpp is the bits per pixel of the output color type (LodePNG_InfoColor_getBpp)
for < 8 bpp images, there may _not_ be padding bits at the end of scanlines.
*/
unsigned LodePNG_convert(fb_data* out, const unsigned char* in, LodePNG_InfoColor* infoOut, LodePNG_InfoColor* infoIn, unsigned w, unsigned h)
{
size_t i, j, bp = 0; /*bitpointer, used by less-than-8-bit color types*/
size_t x, y;
unsigned char c;
if (!running_slideshow)
{
rb->snprintf(print, sizeof(print), "color conversion in progress");
rb->lcd_puts(0, 3, print);
rb->lcd_update();
}
/*cases where in and out already have the same format*/
if (LodePNG_InfoColor_equal(infoIn, infoOut))
{
i = 0;
j = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
unsigned char r = in[i++];
unsigned char g = in[i++];
unsigned char b = in[i++];
out[j++] = LCD_RGBPACK(r,g,b);
}
}
return 0;
}
if ((infoOut->colorType == 2 || infoOut->colorType == 6) && infoOut->bitDepth == 8)
{
if (infoIn->bitDepth == 8)
{
switch (infoIn->colorType)
{
case 0: /*greyscale color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c=in[i];
//unsigned char r = in[i];
//unsigned char g = in[i];
//unsigned char b = in[i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 2: /*RGB color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
j = 3 * i;
unsigned char r = in[j];
unsigned char g = in[j + 1];
unsigned char b = in[j + 2];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
case 3: /*indexed color (palette)*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
if (in[i] >= infoIn->palettesize) return 46;
j = in[i] << 2;
unsigned char r = infoIn->palette[j];
unsigned char g = infoIn->palette[j + 1];
unsigned char b = infoIn->palette[j + 2];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
case 4: /*greyscale with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i << 1];
//unsigned char r = in[i<<1];
//unsigned char g = in[i<<1];
//unsigned char b = in[i<<1];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 6: /*RGB with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
j = i << 2;
unsigned char r = in[j];
unsigned char g = in[j + 1];
unsigned char b = in[j + 2];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
default:
break;
}
}
else if (infoIn->bitDepth == 16)
{
switch (infoIn->colorType)
{
case 0: /*greyscale color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i << 1];
//unsigned char r = in[2 * i];
//unsigned char g = in[2 * i];
//unsigned char b = in[2 * i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 2: /*RGB color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
j = 6 * i;
unsigned char r = in[j];
unsigned char g = in[j + 2];
unsigned char b = in[j + 4];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
case 4: /*greyscale with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i << 2];
//unsigned char r = in[4 * i];
//unsigned char g = in[4 * i];
//unsigned char b = in[4 * i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 6: /*RGB with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
j = i << 3;
unsigned char r = in[j];
unsigned char g = in[j + 2];
unsigned char b = in[j + 4];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
default:
break;
}
}
else /*infoIn->bitDepth is less than 8 bit per channel*/
{
switch (infoIn->colorType)
{
case 0: /*greyscale color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
unsigned char r = (unsigned char)value;
unsigned char g = (unsigned char)value;
unsigned char b = (unsigned char)value;
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
case 3: /*indexed color (palette)*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
if (value >= infoIn->palettesize) return 47;
j = value << 2;
unsigned char r = infoIn->palette[j];
unsigned char g = infoIn->palette[j + 1];
unsigned char b = infoIn->palette[j + 2];
out[i++] = LCD_RGBPACK(r,g,b);
}
}
break;
default:
break;
}
}
}
else if (LodePNG_InfoColor_isGreyscaleType(infoOut) && infoOut->bitDepth == 8) /*conversion from greyscale to greyscale*/
{
if (!LodePNG_InfoColor_isGreyscaleType(infoIn)) return 62;
if (infoIn->bitDepth == 8)
{
switch (infoIn->colorType)
{
case 0: /*greyscale color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i];
//unsigned char r = in[i];
//unsigned char g = in[i];
//unsigned char b = in[i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 4: /*greyscale with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[(i << 1) + 1];
//unsigned char r = in[2 * i + 1];
//unsigned char g = in[2 * i + 1];
//unsigned char b = in[2 * i + 1];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
default:
return 31;
}
}
else if (infoIn->bitDepth == 16)
{
switch (infoIn->colorType)
{
case 0: /*greyscale color*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i << 1];
//unsigned char r = in[2 * i];
//unsigned char g = in[2 * i];
//unsigned char b = in[2 * i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
case 4: /*greyscale with alpha*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
c = in[i << 2];
//unsigned char r = in[4 * i];
//unsigned char g = in[4 * i];
//unsigned char b = in[4 * i];
out[i++] = LCD_RGBPACK(c,c,c);
}
}
break;
default:
return 31;
}
}
else /*infoIn->bitDepth is less than 8 bit per channel*/
{
if (infoIn->colorType != 0) return 31; /*colorType 0 is the only greyscale type with < 8 bits per channel*/
i = 0;
for (y = 0 ; y < h ; y++) {
for (x = 0 ; x < w ; x++) {
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
unsigned char r = (unsigned char)value;
unsigned char g = (unsigned char)value;
unsigned char b = (unsigned char)value;
out[i++] = LCD_RGBPACK(r,g,b);
}
}
}
}
else return 59;
return 0;
}
/*Paeth predicter, used by PNG filter type 4*/
static int paethPredictor(int a, int b, int c)
{
int p = a + b - c;
int pa = p > a ? p - a : a - p;
int pb = p > b ? p - b : b - p;
int pc = p > c ? p - c : c - p;
if (pa <= pb && pa <= pc) return a;
else if (pb <= pc) return b;
else return c;
}
/*shared values used by multiple Adam7 related functions*/
static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp)
{
/*the passstart values have 8 values: the 8th one actually indicates the byte after the end of the 7th (= last) pass*/
unsigned i;
/*calculate width and height in pixels of each pass*/
for (i = 0; i < 7; i++)
{
passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
if (passw[i] == 0) passh[i] = 0;
if (passh[i] == 0) passw[i] = 0;
}
filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
for (i = 0; i < 7; i++)
{
filter_passstart[i + 1] = filter_passstart[i] + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/
padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); /*bits padded if needed to fill full byte at end of each scanline*/
passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; /*only padded at end of reduced image*/
}
}
/* ////////////////////////////////////////////////////////////////////////// */
/* / PNG Decoder / */
/* ////////////////////////////////////////////////////////////////////////// */
/*read the information from the header and store it in the LodePNG_Info. return value is error*/
void LodePNG_inspect(LodePNG_Decoder* decoder, const unsigned char* in, size_t inlength)
{
if (inlength == 0 || in == 0) { decoder->error = 48; return; } /*the given data is empty*/
if (inlength < 29) { decoder->error = 27; return; } /*error: the data length is smaller than the length of the header*/
/*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/
LodePNG_InfoPng_cleanup(&decoder->infoPng);
LodePNG_InfoPng_init(&decoder->infoPng);
decoder->error = 0;
if (in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) { decoder->error = 28; return; } /*error: the first 8 bytes are not the correct PNG signature*/
if (in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') { decoder->error = 29; return; } /*error: it doesn't start with a IHDR chunk!*/
/*read the values given in the header*/
decoder->infoPng.width = LodePNG_read32bitInt(&in[16]);
decoder->infoPng.height = LodePNG_read32bitInt(&in[20]);
decoder->infoPng.color.bitDepth = in[24];
decoder->infoPng.color.colorType = in[25];
decoder->infoPng.compressionMethod = in[26];
decoder->infoPng.filterMethod = in[27];
decoder->infoPng.interlaceMethod = in[28];
unsigned CRC = LodePNG_read32bitInt(&in[29]);
unsigned checksum = crc32(0L, &in[12], 17);
if (CRC != checksum) { decoder->error = 57; return; }
if (decoder->infoPng.compressionMethod != 0) { decoder->error = 32; return; } /*error: only compression method 0 is allowed in the specification*/
if (decoder->infoPng.filterMethod != 0) { decoder->error = 33; return; } /*error: only filter method 0 is allowed in the specification*/
if (decoder->infoPng.interlaceMethod > 1) { decoder->error = 34; return; } /*error: only interlace methods 0 and 1 exist in the specification*/
decoder->error = checkColorValidity(decoder->infoPng.color.colorType, decoder->infoPng.color.bitDepth);
}
static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, size_t bytewidth, unsigned char filterType, size_t length)
{
/*
For PNG filter method 0
unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte, the filter works byte per byte (bytewidth = 1)
precon is the previous unfiltered scanline, recon the result, scanline the current one
the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead
recon and scanline MAY be the same memory address! precon must be disjoint.
*/
size_t i;
switch (filterType)
{
case 0:
//for(i = 0; i < length; i++) recon[i] = scanline[i];
memcpy(recon, scanline, length * sizeof(unsigned char));
break;
case 1:
//for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
memcpy(recon, scanline, bytewidth * sizeof(unsigned char));
for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth];
break;
case 2:
if (precon) for (i = 0; i < length; i++) recon[i] = scanline[i] + precon[i];
else //for(i = 0; i < length; i++) recon[i] = scanline[i];
memcpy(recon, scanline, length * sizeof(unsigned char));
break;
case 3:
if (precon)
{
for (i = 0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2;
for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
}
else
{
//for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
memcpy(recon, scanline, bytewidth * sizeof(unsigned char));
for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
}
break;
case 4:
if (precon)
{
for (i = 0; i < bytewidth; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(0, precon[i], 0));
for (i = bytewidth; i < length; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
}
else
{
//for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
memcpy(recon, scanline, bytewidth * sizeof(unsigned char));
for (i = bytewidth; i < length; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(recon[i - bytewidth], 0, 0));
}
break;
default:
return 36; /*error: unexisting filter type given*/
}
return 0;
}
static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
{
/*
For PNG filter method 0
this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 it's called 7 times)
out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline
w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel
in and out are allowed to be the same memory address!
*/
unsigned y;
unsigned char* prevline = 0;
size_t bytewidth = (bpp + 7) / 8; /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
size_t linebytes = (w * bpp + 7) / 8;
for (y = 0; y < h; y++)
{
size_t outindex = linebytes * y;
size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
unsigned char filterType = in[inindex];
unsigned error = unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes);
if (error) return error;
prevline = &out[outindex];
}
return 0;
}
static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
{
/*Note: this function works on image buffers WITHOUT padding bits at end of scanlines with non-multiple-of-8 bit amounts, only between reduced images is padding
out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation (because that's likely a little bit faster)*/
unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
unsigned i;
Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
if (bpp >= 8)
{
for (i = 0; i < 7; i++)
{
unsigned x, y, b;
size_t bytewidth = bpp / 8;
for (y = 0; y < passh[i]; y++)
for (x = 0; x < passw[i]; x++)
{
size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
for (b = 0; b < bytewidth; b++)
{
out[pixeloutstart + b] = in[pixelinstart + b];
}
}
}
}
else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
{
for (i = 0; i < 7; i++)
{
unsigned x, y, b;
unsigned ilinebits = bpp * passw[i];
unsigned olinebits = bpp * w;
size_t obp, ibp; /*bit pointers (for out and in buffer)*/
for (y = 0; y < passh[i]; y++)
for (x = 0; x < passw[i]; x++)
{
ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
for (b = 0; b < bpp; b++)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
setBitOfReversedStream0(&obp, out, bit); /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/
}
}
}
}
}
static void removePaddingBits(unsigned char* out, const unsigned char* in, size_t olinebits, size_t ilinebits, unsigned h)
{
/*
After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers for the Adam7 code, the color convert code and the output to the user.
in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits
also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7
only useful if (ilinebits - olinebits) is a value in the range 1..7
*/
unsigned y;
size_t diff = ilinebits - olinebits;
size_t obp = 0, ibp = 0; /*bit pointers*/
for (y = 0; y < h; y++)
{
size_t x;
for (x = 0; x < olinebits; x++)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
setBitOfReversedStream(&obp, out, bit);
}
ibp += diff;
}
}
/*out must be buffer big enough to contain full image, and in must contain the full decompressed data from the IDAT chunks*/
static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, const LodePNG_Decoder* decoder) /*return value is error*/
{
/*
This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. Steps:
*) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8)
*) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
NOTE: the in buffer will be overwritten with intermediate data!
*/
unsigned bpp = LodePNG_InfoColor_getBpp(&decoder->infoPng.color);
unsigned w = decoder->infoPng.width;
unsigned h = decoder->infoPng.height;
unsigned error = 0;
if (bpp == 0) return 31; /*error: invalid colortype*/
if (decoder->infoPng.interlaceMethod == 0)
{
if (bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
{
error = unfilter(in, in, w, h, bpp);
if (error) return error;
removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
}
else error = unfilter(out, in, w, h, bpp); /*we can immediatly filter into the out buffer, no other steps needed*/
}
else /*interlaceMethod is 1 (Adam7)*/
{
unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
unsigned i;
Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
for (i = 0; i < 7; i++)
{
error = unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp);
if (error) return error;
if (bpp < 8) /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, move bytes instead of bits or move not at all*/
{
/*remove padding bits in scanlines; after this there still may be padding bits between the different reduced images: each reduced image still starts nicely at a byte*/
removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, ((passw[i] * bpp + 7) / 8) * 8, passh[i]);
}
}
Adam7_deinterlace(out, in, w, h, bpp);
}
return error;
}
/*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/
static void decodeGeneric(LodePNG_Decoder* decoder, unsigned char* in, size_t size, void (*pf_progress)(int current, int total))
{
if (pf_progress != NULL)
pf_progress(0, 100);
unsigned char IEND = 0;
const unsigned char* chunk;
size_t i;
unsigned char *idat = memory;
size_t idat_size = 0;
/*for unknown chunk order*/
unsigned unknown = 0;
unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/
/*provide some proper output values if error will happen*/
decoded_image_size = 0;
if (size == 0 || in == 0) { decoder->error = 48; return; } /*the given data is empty*/
LodePNG_inspect(decoder, in, size); /*reads header and resets other parameters in decoder->infoPng*/
if (decoder->error) return;
chunk = &in[33]; /*first byte of the first chunk after the header*/
while (!IEND) /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is put at the start of the in buffer*/
{
unsigned chunkLength;
const unsigned char* data; /*the data in the chunk*/
if ((size_t)((chunk - in) + 12) > size || chunk < in) { decoder->error = 30; break; } /*error: size of the in buffer too small to contain next chunk*/
chunkLength = LodePNG_chunk_length(chunk); /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/
if (chunkLength > 2147483647) { decoder->error = 63; break; }
if ((size_t)((chunk - in) + chunkLength + 12) > size || (chunk + chunkLength + 12) < in) { decoder->error = 35; break; } /*error: size of the in buffer too small to contain next chunk*/
data = LodePNG_chunk_data_const(chunk);
/*IDAT chunk, containing compressed image data*/
if (LodePNG_chunk_type_equals(chunk, "IDAT"))
{
size_t oldsize = idat_size;
idat_size += chunkLength;
if (idat + idat_size >= image) { decoder->error = OUT_OF_MEMORY; break; }
memcpy(idat+oldsize, data, chunkLength * sizeof(unsigned char));
critical_pos = 3;
}
/*IEND chunk*/
else if (LodePNG_chunk_type_equals(chunk, "IEND"))
{
IEND = 1;
}
/*palette chunk (PLTE)*/
else if (LodePNG_chunk_type_equals(chunk, "PLTE"))
{
unsigned pos = 0;
decoder->infoPng.color.palettesize = chunkLength / 3;
if (decoder->infoPng.color.palettesize > 256) { decoder->error = 38; break; } /*error: palette too big*/
for (i = 0; i < decoder->infoPng.color.palettesize; i++)
{
decoder->infoPng.color.palette[4 * i + 0] = data[pos++]; /*R*/
decoder->infoPng.color.palette[4 * i + 1] = data[pos++]; /*G*/
decoder->infoPng.color.palette[4 * i + 2] = data[pos++]; /*B*/
decoder->infoPng.color.palette[4 * i + 3] = 255; /*alpha*/
}
critical_pos = 2;
}
/*palette transparency chunk (tRNS)*/
else if (LodePNG_chunk_type_equals(chunk, "tRNS"))
{
if (decoder->infoPng.color.colorType == 3)
{
if (chunkLength > decoder->infoPng.color.palettesize) { decoder->error = 39; break; } /*error: more alpha values given than there are palette entries*/
for (i = 0; i < chunkLength; i++) decoder->infoPng.color.palette[4 * i + 3] = data[i];
}
else if (decoder->infoPng.color.colorType == 0)
{
if (chunkLength != 2) { decoder->error = 40; break; } /*error: this chunk must be 2 bytes for greyscale image*/
decoder->infoPng.color.key_defined = 1;
decoder->infoPng.color.key_r = decoder->infoPng.color.key_g = decoder->infoPng.color.key_b = 256 * data[0] + data[1];
}
else if (decoder->infoPng.color.colorType == 2)
{
if (chunkLength != 6) { decoder->error = 41; break; } /*error: this chunk must be 6 bytes for RGB image*/
decoder->infoPng.color.key_defined = 1;
decoder->infoPng.color.key_r = 256 * data[0] + data[1];
decoder->infoPng.color.key_g = 256 * data[2] + data[3];
decoder->infoPng.color.key_b = 256 * data[4] + data[5];
}
else { decoder->error = 42; break; } /*error: tRNS chunk not allowed for other color models*/
}
/*background color chunk (bKGD)*/
else if (LodePNG_chunk_type_equals(chunk, "bKGD"))
{
if (decoder->infoPng.color.colorType == 3)
{
if (chunkLength != 1) { decoder->error = 43; break; } /*error: this chunk must be 1 byte for indexed color image*/
decoder->infoPng.background_defined = 1;
decoder->infoPng.background_r = decoder->infoPng.background_g = decoder->infoPng.background_g = data[0];
}
else if (decoder->infoPng.color.colorType == 0 || decoder->infoPng.color.colorType == 4)
{
if (chunkLength != 2) { decoder->error = 44; break; } /*error: this chunk must be 2 bytes for greyscale image*/
decoder->infoPng.background_defined = 1;
decoder->infoPng.background_r = decoder->infoPng.background_g = decoder->infoPng.background_b = 256 * data[0] + data[1];
}
else if (decoder->infoPng.color.colorType == 2 || decoder->infoPng.color.colorType == 6)
{
if (chunkLength != 6) { decoder->error = 45; break; } /*error: this chunk must be 6 bytes for greyscale image*/
decoder->infoPng.background_defined = 1;
decoder->infoPng.background_r = 256 * data[0] + data[1];
decoder->infoPng.background_g = 256 * data[2] + data[3];
decoder->infoPng.background_b = 256 * data[4] + data[5];
}
}
else if (LodePNG_chunk_type_equals(chunk, "tIME"))
{
if (chunkLength != 7) { decoder->error = 73; break; }
decoder->infoPng.time_defined = 1;
decoder->infoPng.time.year = 256 * data[0] + data[+ 1];
decoder->infoPng.time.month = data[2];
decoder->infoPng.time.day = data[3];
decoder->infoPng.time.hour = data[4];
decoder->infoPng.time.minute = data[5];
decoder->infoPng.time.second = data[6];
}
else if (LodePNG_chunk_type_equals(chunk, "pHYs"))
{
if (chunkLength != 9) { decoder->error = 74; break; }
decoder->infoPng.phys_defined = 1;
decoder->infoPng.phys_x = 16777216 * data[0] + 65536 * data[1] + 256 * data[2] + data[3];
decoder->infoPng.phys_y = 16777216 * data[4] + 65536 * data[5] + 256 * data[6] + data[7];
decoder->infoPng.phys_unit = data[8];
}
else /*it's not an implemented chunk type, so ignore it: skip over the data*/
{
if (LodePNG_chunk_critical(chunk)) { decoder->error = 69; break; } /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/
unknown = 1;
}
if (!unknown) /*check CRC if wanted, only on known chunk types*/
{
long time = *rb->current_tick;
if (LodePNG_chunk_check_crc(chunk)) { decoder->error = 57; break; }
time = *rb->current_tick-time;
}
if (!IEND) chunk = LodePNG_chunk_next_const(chunk);
}
if (!decoder->error)
{
unsigned char *scanlines = idat + idat_size;
size_t scanlines_size = (size_t)memory_max - idat_size + 1;
long time = *rb->current_tick;
decoder->error = LodePNG_decompress(scanlines, &scanlines_size, idat, idat_size, decoder->error_msg); /*decompress with the Zlib decompressor*/
if (pf_progress) pf_progress(100, 100);
time = *rb->current_tick-time;
if (!decoder->error)
{
decoded_image_size = (decoder->infoPng.height * decoder->infoPng.width * LodePNG_InfoColor_getBpp(&decoder->infoPng.color) + 7) / 8;
if (decoded_image_size > memory_size) { decoder->error = OUT_OF_MEMORY; return; }
decoded_image = memory_max - decoded_image_size + 1;
if (scanlines + scanlines_size >= decoded_image) { decoder->error = OUT_OF_MEMORY; return; }
memset(decoded_image, 0, decoded_image_size * sizeof(unsigned char));
if (!running_slideshow)
{
rb->snprintf(print, sizeof(print), "unfiltering scanlines");
rb->lcd_puts(0, 3, print);
rb->lcd_update();
}
decoder->error = postProcessScanlines(decoded_image, scanlines, decoder);
}
}
}
void LodePNG_decode(LodePNG_Decoder* decoder, unsigned char* in, size_t insize, void (*pf_progress)(int current, int total))
{
decodeGeneric(decoder, in, insize, pf_progress);
if (decoder->error) return;
/*TODO: check if this works according to the statement in the documentation: "The converter can convert from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/
if (!(decoder->infoRaw.color.colorType == 2 || decoder->infoRaw.color.colorType == 6) && !(decoder->infoRaw.color.bitDepth == 8)) { decoder->error = 56; return; }
converted_image = (fb_data *)((intptr_t)(memory + 3) & ~3);
converted_image_size = decoder->infoPng.width*decoder->infoPng.height;
if ((unsigned char *)(converted_image + converted_image_size) >= decoded_image) { decoder->error = OUT_OF_MEMORY; }
if (!decoder->error) decoder->error = LodePNG_convert(converted_image, decoded_image, &decoder->infoRaw.color, &decoder->infoPng.color, decoder->infoPng.width, decoder->infoPng.height);
}
void LodePNG_DecodeSettings_init(LodePNG_DecodeSettings* settings)
{
settings->color_convert = 1;
}
void LodePNG_Decoder_init(LodePNG_Decoder* decoder)
{
LodePNG_DecodeSettings_init(&decoder->settings);
LodePNG_InfoRaw_init(&decoder->infoRaw);
LodePNG_InfoPng_init(&decoder->infoPng);
decoder->error = 1;
}
void LodePNG_Decoder_cleanup(LodePNG_Decoder* decoder)
{
LodePNG_InfoRaw_cleanup(&decoder->infoRaw);
LodePNG_InfoPng_cleanup(&decoder->infoPng);
}
#define PNG_ERROR_MIN 27
#define PNG_ERROR_MAX 74
static const unsigned char *png_error_messages[PNG_ERROR_MAX-PNG_ERROR_MIN+1] =
{
"png file smaller than a png header", /*27*/
"incorrect png signature", /*28*/
"first chunk is not IHDR", /*29*/
"chunk length too large", /*30*/
"illegal PNG color type or bpp", /*31*/
"illegal PNG compression method", /*32*/
"illegal PNG filter method", /*33*/
"illegal PNG interlace method", /*34*/
"chunk length of a chunk is too large or the chunk too small", /*35*/
"illegal PNG filter type encountered", /*36*/
"illegal bit depth for this color type given", /*37*/
"the palette is too big (more than 256 colors)", /*38*/
"more palette alpha values given in tRNS, than there are colors in the palette", /*39*/
"tRNS chunk has wrong size for greyscale image", /*40*/
"tRNS chunk has wrong size for RGB image", /*41*/
"tRNS chunk appeared while it was not allowed for this color type", /*42*/
"bKGD chunk has wrong size for palette image", /*43*/
"bKGD chunk has wrong size for greyscale image", /*44*/
"bKGD chunk has wrong size for RGB image", /*45*/
"value encountered in indexed image is larger than the palette size", /*46*/
"value encountered in indexed image is larger than the palette size", /*47*/
"input file is empty", /*48*/
NULL, /*49*/
NULL, /*50*/
NULL, /*51*/
NULL, /*52*/
NULL, /*53*/
NULL, /*54*/
NULL, /*55*/
NULL, /*56*/
"invalid CRC", /*57*/
NULL, /*58*/
"conversion to unexisting or unsupported color type or bit depth", /*59*/
NULL, /*60*/
NULL, /*61*/
NULL, /*62*/
"png chunk too long", /*63*/
NULL, /*64*/
NULL, /*65*/
NULL, /*66*/
NULL, /*67*/
NULL, /*68*/
"unknown critical chunk", /*69*/
NULL, /*70*/
NULL, /*71*/
NULL, /*72*/
"invalid tIME chunk size", /*73*/
"invalid pHYs chunk size", /*74*/
};
bool png_ext(const char ext[])
{
if (!ext)
return false;
if (!rb->strcasecmp(ext,".png"))
return true;
else
return false;
}
/*Read directory contents for scrolling. */
void get_pic_list(void)
{
int i;
struct entry *dircache;
char *pname;
tree = rb->tree_get_context();
dircache = tree->dircache;
file_pt = (char **) memory;
/* Remove path and leave only the name.*/
pname = rb->strrchr(np_file,'/');
pname++;
for (i = 0; i < tree->filesindir; i++)
{
if (!(dircache[i].attr & ATTR_DIRECTORY)
&& png_ext(rb->strrchr(dircache[i].name, '.')))
{
file_pt[entries] = dircache[i].name;
/* Set Selected File. */
if (!rb->strcmp(file_pt[entries], pname))
curfile = entries;
entries++;
}
}
memory += (entries * sizeof(char**));
memory_size -= (entries * sizeof(char**));
}
int change_filename(int direct)
{
bool file_erased = (file_pt[curfile] == NULL);
direction = direct;
curfile += (direct == DIR_PREV? entries - 1: 1);
if (curfile >= entries)
curfile -= entries;
if (file_erased)
{
/* remove 'erased' file names from list. */
int count, i;
for (count = i = 0; i < entries; i++)
{
if (curfile == i)
curfile = count;
if (file_pt[i] != NULL)
file_pt[count++] = file_pt[i];
}
entries = count;
}
if (entries == 0)
{
rb->splash(HZ, "No supported files");
return PLUGIN_ERROR;
}
if (rb->strlen(tree->currdir) > 1)
{
rb->strcpy(np_file, tree->currdir);
rb->strcat(np_file, "/");
}
else
rb->strcpy(np_file, tree->currdir);
rb->strcat(np_file, file_pt[curfile]);
return PLUGIN_OTHER;
}
/* switch off overlay, for handling SYS_ events */
void cleanup(void *parameter)
{
(void)parameter;
}
#define VSCROLL (LCD_HEIGHT/8)
#define HSCROLL (LCD_WIDTH/10)
#define ZOOM_IN 100 /* return codes for below function */
#define ZOOM_OUT 101
int show_menu(void) /* return 1 to quit */
{
#if LCD_DEPTH > 1
rb->lcd_set_backdrop(old_backdrop);
#ifdef HAVE_LCD_COLOR
rb->lcd_set_foreground(rb->global_settings->fg_color);
rb->lcd_set_background(rb->global_settings->bg_color);
#else
rb->lcd_set_foreground(LCD_BLACK);
rb->lcd_set_background(LCD_WHITE);
#endif
#endif
int result;
enum menu_id
{
MIID_RETURN = 0,
MIID_TOGGLE_SS_MODE,
MIID_CHANGE_SS_MODE,
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
MIID_SHOW_PLAYBACK_MENU,
#endif
MIID_QUIT,
};
MENUITEM_STRINGLIST(menu, "Png Menu", NULL,
"Return", "Toggle Slideshow Mode",
"Change Slideshow Time",
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
"Show Playback Menu",
#endif
"Quit");
static const struct opt_items slideshow[2] = {
{ "Disable", -1 },
{ "Enable", -1 },
};
result=rb->do_menu(&menu, NULL, NULL, false);
switch (result)
{
case MIID_RETURN:
break;
case MIID_TOGGLE_SS_MODE:
rb->set_option("Toggle Slideshow", &slideshow_enabled, INT,
slideshow , 2, NULL);
break;
case MIID_CHANGE_SS_MODE:
rb->set_int("Slideshow Time", "s", UNIT_SEC,
&png_settings.ss_timeout, NULL, 1,
SS_MIN_TIMEOUT, SS_MAX_TIMEOUT, NULL);
break;
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
case MIID_SHOW_PLAYBACK_MENU:
if (plug_buf)
{
playback_control(NULL);
}
else
{
rb->splash(HZ, "Cannot restart playback");
}
break;
#endif
case MIID_QUIT:
return 1;
break;
}
#if !defined(SIMULATOR) && defined(HAVE_DISK_STORAGE)
/* change ata spindown time based on slideshow time setting */
immediate_ata_off = false;
rb->storage_spindown(rb->global_settings->disk_spindown);
if (slideshow_enabled)
{
if (png_settings.ss_timeout < 10)
{
/* slideshow times < 10s keep disk spinning */
rb->storage_spindown(0);
}
else if (!rb->mp3_is_playing())
{
/* slideshow times > 10s and not playing: ata_off after load */
immediate_ata_off = true;
}
}
#endif
#if LCD_DEPTH > 1
rb->lcd_set_backdrop(NULL);
rb->lcd_set_foreground(LCD_WHITE);
rb->lcd_set_background(LCD_BLACK);
#endif
rb->lcd_clear_display();
return 0;
}
void draw_image(struct LodePNG_Decoder* decoder)
{
rb->lcd_bitmap_part(resized_image, decoder->x, decoder->y, decoder->width,
MAX(0, (LCD_WIDTH - decoder->width) / 2),
MAX(0, (LCD_HEIGHT - decoder->height) / 2),
decoder->width - decoder->x,
decoder->height - decoder->y);
}
/* Pan the viewing window right - move image to the left and fill in
the right-hand side */
static void pan_view_right(struct LodePNG_Decoder* decoder)
{
int move;
move = MIN(HSCROLL, decoder->width - decoder->x - LCD_WIDTH);
if (move > 0)
{
decoder->x += move;
draw_image(decoder);
rb->lcd_update();
}
}
/* Pan the viewing window left - move image to the right and fill in
the left-hand side */
static void pan_view_left(struct LodePNG_Decoder* decoder)
{
int move;
move = MIN(HSCROLL, decoder->x);
if (move > 0)
{
decoder->x -= move;
draw_image(decoder);
rb->lcd_update();
}
}
/* Pan the viewing window up - move image down and fill in
the top */
static void pan_view_up(struct LodePNG_Decoder* decoder)
{
int move;
move = MIN(VSCROLL, decoder->y);
if (move > 0)
{
decoder->y -= move;
draw_image(decoder);
rb->lcd_update();
}
}
/* Pan the viewing window down - move image up and fill in
the bottom */
static void pan_view_down(struct LodePNG_Decoder* decoder)
{
int move;
move = MIN(VSCROLL, decoder->height - decoder->y - LCD_HEIGHT);
if (move > 0)
{
decoder->y += move;
draw_image(decoder);
rb->lcd_update();
}
}
/* interactively scroll around the image */
int scroll_bmp(struct LodePNG_Decoder* decoder)
{
int button;
int lastbutton = 0;
while (true)
{
if (slideshow_enabled)
button = rb->button_get_w_tmo(png_settings.ss_timeout * HZ);
else
button = rb->button_get(true);
running_slideshow = false;
switch (button)
{
case PNG_LEFT:
if (entries > 1 && decoder->width <= LCD_WIDTH
&& decoder->height <= LCD_HEIGHT)
return change_filename(DIR_PREV);
case PNG_LEFT | BUTTON_REPEAT:
pan_view_left(decoder);
break;
case PNG_RIGHT:
if (entries > 1 && decoder->width <= LCD_WIDTH
&& decoder->height <= LCD_HEIGHT)
return change_filename(DIR_NEXT);
case PNG_RIGHT | BUTTON_REPEAT:
pan_view_right(decoder);
break;
case PNG_UP:
case PNG_UP | BUTTON_REPEAT:
pan_view_up(decoder);
break;
case PNG_DOWN:
case PNG_DOWN | BUTTON_REPEAT:
pan_view_down(decoder);
break;
case BUTTON_NONE:
if (!slideshow_enabled)
break;
running_slideshow = true;
if (entries > 1)
return change_filename(DIR_NEXT);
break;
#ifdef PNG_SLIDE_SHOW
case PNG_SLIDE_SHOW:
slideshow_enabled = !slideshow_enabled;
running_slideshow = slideshow_enabled;
break;
#endif
#ifdef PNG_NEXT_REPEAT
case PNG_NEXT_REPEAT:
#endif
case PNG_NEXT:
if (entries > 1)
return change_filename(DIR_NEXT);
break;
#ifdef PNG_PREVIOUS_REPEAT
case PNG_PREVIOUS_REPEAT:
#endif
case PNG_PREVIOUS:
if (entries > 1)
return change_filename(DIR_PREV);
break;
case PNG_ZOOM_IN:
#ifdef PNG_ZOOM_PRE
if (lastbutton != PNG_ZOOM_PRE)
break;
#endif
return ZOOM_IN;
break;
case PNG_ZOOM_OUT:
#ifdef PNG_ZOOM_PRE
if (lastbutton != PNG_ZOOM_PRE)
break;
#endif
return ZOOM_OUT;
break;
#ifdef PNG_RC_MENU
case PNG_RC_MENU:
#endif
case PNG_MENU:
if (show_menu() == 1)
return PLUGIN_OK;
draw_image(decoder);
rb->lcd_update();
break;
default:
if (rb->default_event_handler_ex(button, cleanup, NULL)
== SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED;
break;
} /* switch */
if (button != BUTTON_NONE)
lastbutton = button;
} /* while (true) */
}
/* callback updating a progress meter while PNG decoding */
void cb_progress(int current, int total)
{
if (current & 1) rb->yield(); /* be nice to the other threads */
if (!running_slideshow)
{
rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],
0, LCD_HEIGHT-8, LCD_WIDTH, 8,
total, 0, current, HORIZONTAL);
rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
}
else
{
/* in slideshow mode, keep gui interference to a minimum */
rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],
0, LCD_HEIGHT-4, LCD_WIDTH, 4,
total, 0, current, HORIZONTAL);
rb->lcd_update_rect(0, LCD_HEIGHT-4, LCD_WIDTH, 4);
}
}
int pngmem(struct LodePNG_Decoder* decoder, int ds)
{
return (decoder->infoPng.width/ds) * (decoder->infoPng.height/ds) * FB_DATA_SZ;
}
/* how far can we zoom in without running out of memory */
int min_downscale(struct LodePNG_Decoder* decoder, int bufsize)
{
int downscale = 8;
if (pngmem(decoder, 8) > bufsize)
return 0; /* error, too large, even 1:8 doesn't fit */
while (downscale > 1 && pngmem(decoder, downscale/2) <= bufsize)
downscale /= 2;
return downscale;
}
/* how far can we zoom out, to fit image into the LCD */
unsigned max_downscale(struct LodePNG_Decoder* decoder)
{
unsigned downscale = 1;
while (downscale < 8 && (decoder->infoPng.width/downscale > LCD_WIDTH
|| decoder->infoPng.height/downscale > LCD_HEIGHT))
{
downscale *= 2;
}
return downscale;
}
/* load image from filename. */
int load_image(char* filename, struct LodePNG_Decoder* decoder)
{
int fd;
long time = 0; /* measured ticks */
int w, h; /* used to center output */
fd = rb->open(filename, O_RDONLY);
if (fd < 0)
{
rb->splashf(HZ, "err opening %s:%d", filename, fd);
return PLUGIN_ERROR;
}
image_size = rb->filesize(fd);
DEBUGF("reading file '%s'\n", filename);
if (!running_slideshow) {
rb->snprintf(print, sizeof(print), "%s:", rb->strrchr(filename,'/')+1);
rb->lcd_puts(0, 0, print);
rb->lcd_update();
}
if (image_size > memory_size) {
decoder->error = FILE_TOO_LARGE;
rb->close(fd);
#ifndef SIMULATOR
if (running_slideshow && immediate_ata_off) {
/* running slideshow and time is long enough: power down disk */
rb->storage_sleep();
}
#endif
} else {
if (!running_slideshow) {
rb->snprintf(print, sizeof(print), "loading %lu bytes", image_size);
rb->lcd_puts(0, 1, print);
rb->lcd_update();
}
image = memory_max - image_size + 1;
rb->read(fd, image, image_size);
rb->close(fd);
if (!running_slideshow) {
rb->snprintf(print, sizeof(print), "decoding image");
rb->lcd_puts(0, 2, print);
rb->lcd_update();
}
#ifndef SIMULATOR
else if (immediate_ata_off) {
/* running slideshow and time is long enough: power down disk */
rb->storage_sleep();
}
#endif
decoder->settings.color_convert = 1;
decoder->infoRaw.color.colorType = 2;
decoder->infoRaw.color.bitDepth = 8;
LodePNG_inspect(decoder, image, image_size);
if (!decoder->error) {
if (!running_slideshow) {
rb->snprintf(print, sizeof(print), "image %dx%d",
decoder->infoPng.width, decoder->infoPng.height);
rb->lcd_puts(0, 2, print);
rb->lcd_update();
rb->snprintf(print, sizeof(print), "decoding %d*%d",
decoder->infoPng.width, decoder->infoPng.height);
rb->lcd_puts(0, 3, print);
rb->lcd_update();
}
/* the actual decoding */
time = *rb->current_tick;
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(true);
LodePNG_decode(decoder, image, image_size, cb_progress);
rb->cpu_boost(false);
#else
LodePNG_decode(decoder, image, image_size, cb_progress);
#endif /*HAVE_ADJUSTABLE_CPU_FREQ*/
}
}
time = *rb->current_tick - time;
if (!running_slideshow && !decoder->error)
{
rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ);
rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */
rb->lcd_putsxy((LCD_WIDTH - w)/2, LCD_HEIGHT - h, print);
rb->lcd_update();
}
if (decoder->error) {
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
if (plug_buf && (decoder->error == FILE_TOO_LARGE
|| decoder->error == OUT_OF_MEMORY || decoder->error == Z_MEM_ERROR))
return PLUGIN_OUTOFMEM;
#endif
if (decoder->error >= PNG_ERROR_MIN && decoder->error <= PNG_ERROR_MAX
&& png_error_messages[decoder->error-PNG_ERROR_MIN] != NULL)
{
rb->splash(HZ, png_error_messages[decoder->error-PNG_ERROR_MIN]);
}
else
{
switch (decoder->error) {
case PLUGIN_ABORT:
break;
case OUT_OF_MEMORY:
case Z_MEM_ERROR:
rb->splash(HZ, "Out of Memory");break;
case FILE_TOO_LARGE:
rb->splash(HZ, "File too large");break;
case Z_DATA_ERROR:
rb->splash(HZ, decoder->error_msg);break;
default:
rb->splashf(HZ, "other error : %ld", decoder->error);break;
}
}
if (decoder->error == PLUGIN_ABORT)
return PLUGIN_ABORT;
else
return PLUGIN_ERROR;
}
return PLUGIN_OK;
}
/* return decoded or cached image */
fb_data *get_image(struct LodePNG_Decoder* decoder, int ds)
{
fb_data * p_disp = disp[ds]; /* short cut */
decoder->width = decoder->infoPng.width / ds;
decoder->height = decoder->infoPng.height / ds;
if (p_disp != NULL)
{
DEBUGF("Found an image in cache\n");
return p_disp; /* we still have it */
}
/* assign image buffer */
if (ds > 1) {
if (!running_slideshow)
{
rb->snprintf(print, sizeof(print), "resizing %d*%d",
decoder->width, decoder->height);
rb->lcd_puts(0, 3, print);
rb->lcd_update();
}
struct bitmap bmp_src, bmp_dst;
int size = decoder->width * decoder->height;
if ((unsigned char *)(disp_buf + size) >= memory_max) {
/* have to discard the current */
int i;
for (i=1; i<=8; i++)
disp[i] = NULL; /* invalidate all bitmaps */
/* start again from the beginning of the buffer */
disp_buf = (fb_data *)((intptr_t)(converted_image + converted_image_size + 3) & ~3);
}
disp[ds] = disp_buf;
disp_buf = (fb_data *)((intptr_t)(disp[ds] + size + 3) & ~3);
bmp_src.width = decoder->infoPng.width;
bmp_src.height = decoder->infoPng.height;
bmp_src.data = (unsigned char *)converted_image;
bmp_dst.width = decoder->width;
bmp_dst.height = decoder->height;
bmp_dst.data = (unsigned char *)disp[ds];
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(true);
smooth_resize_bitmap(&bmp_src, &bmp_dst);
rb->cpu_boost(false);
#else
smooth_resize_bitmap(&bmp_src, &bmp_dst);
#endif /*HAVE_ADJUSTABLE_CPU_FREQ*/
} else {
disp[ds] = converted_image;
return converted_image;
}
return disp[ds];
}
/* set the view to the given center point, limit if necessary */
void set_view (struct LodePNG_Decoder* decoder, int cx, int cy)
{
int x, y;
/* plain center to available width/height */
x = cx - MIN(LCD_WIDTH, decoder->width) / 2;
y = cy - MIN(LCD_HEIGHT, decoder->height) / 2;
/* limit against upper image size */
x = MIN(decoder->width - LCD_WIDTH, x);
y = MIN(decoder->height - LCD_HEIGHT, y);
/* limit against negative side */
x = MAX(0, x);
y = MAX(0, y);
decoder->x = x; /* set the values */
decoder->y = y;
}
/* calculate the view center based on the bitmap position */
void get_view(struct LodePNG_Decoder* decoder, int* p_cx, int* p_cy)
{
*p_cx = decoder->x + MIN(LCD_WIDTH, decoder->width) / 2;
*p_cy = decoder->y + MIN(LCD_HEIGHT, decoder->height) / 2;
}
/* load, decode, display the image */
int load_and_show(char* filename)
{
int status;
int cx=0, cy=0; /* view center */
#if LCD_DEPTH > 1
rb->lcd_set_foreground(LCD_WHITE);
rb->lcd_set_background(LCD_BLACK);
rb->lcd_set_backdrop(NULL);
#endif
rb->lcd_clear_display();
memset(&disp, 0, sizeof(disp));
LodePNG_Decoder_init(&decoder);
if (rb->button_get(false) == PNG_MENU)
status = PLUGIN_ABORT;
else
status = load_image(filename, &decoder);
if (status == PLUGIN_OUTOFMEM)
{
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
if (plug_buf)
{
rb->lcd_setfont(FONT_SYSFIXED);
rb->lcd_clear_display();
rb->snprintf(print,sizeof(print),"%s:",rb->strrchr(filename,'/')+1);
rb->lcd_puts(0,0,print);
rb->lcd_puts(0,1,"Not enough plugin memory!");
rb->lcd_puts(0,2,"Zoom In: Stop playback.");
if (entries>1)
rb->lcd_puts(0,3,"Left/Right: Skip File.");
rb->lcd_puts(0,4,"Show Menu: Quit.");
rb->lcd_update();
rb->lcd_setfont(FONT_UI);
rb->button_clear_queue();
while (1)
{
int button = rb->button_get(true);
switch (button)
{
case PNG_ZOOM_IN:
plug_buf = false;
memory = rb->plugin_get_audio_buffer((size_t *)&memory_size);
memory_max = memory + memory_size - 1;
/*try again this file, now using the audio buffer */
return PLUGIN_OTHER;
#ifdef PNG_RC_MENU
case PNG_RC_MENU:
#endif
case PNG_MENU:
return PLUGIN_OK;
case PNG_LEFT:
if (entries>1)
{
rb->lcd_clear_display();
return change_filename(DIR_PREV);
}
break;
case PNG_RIGHT:
if (entries>1)
{
rb->lcd_clear_display();
return change_filename(DIR_NEXT);
}
break;
default:
if (rb->default_event_handler_ex(button, cleanup, NULL)
== SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED;
}
}
}
else
#endif
{
rb->splash(HZ, "Out of Memory");
file_pt[curfile] = NULL;
return change_filename(direction);
}
}
else if (status == PLUGIN_ERROR)
{
file_pt[curfile] = NULL;
return change_filename(direction);
}
else if (status == PLUGIN_ABORT) {
rb->splash(HZ, "aborted");
return PLUGIN_OK;
}
disp_buf = (fb_data *)((intptr_t)(converted_image + converted_image_size + 3) & ~3);
ds_max = max_downscale(&decoder); /* check display constraint */
ds_min = min_downscale(&decoder, memory_max - (unsigned char*)disp_buf); /* check memory constraint */
if (ds_min == 0) {
/* Can not resize the image */
ds_min = ds_max = 1;
} else if (ds_max < ds_min) {
ds_max = ds_min;
}
ds = ds_max; /* initialize setting */
cx = decoder.infoPng.width/ds/2; /* center the view */
cy = decoder.infoPng.height/ds/2;
do {
resized_image = get_image(&decoder, ds); /* decode or fetch from cache */
set_view(&decoder, cx, cy);
if (!running_slideshow)
{
rb->snprintf(print, sizeof(print), "showing %dx%d",
decoder.width, decoder.height);
rb->lcd_puts(0, 3, print);
rb->lcd_update();
}
rb->lcd_clear_display();
draw_image(&decoder);
rb->lcd_update();
/* drawing is now finished, play around with scrolling
* until you press OFF or connect USB
*/
while (1)
{
status = scroll_bmp(&decoder);
if (status == ZOOM_IN)
{
if (ds > 1)
{
/* as 1/1 is always available, jump ds to 1 if ds is ds_min. */
int zoom = (ds == ds_min)? ds_min: 2;
ds /= zoom; /* reduce downscaling to zoom in */
get_view(&decoder, &cx, &cy);
cx *= zoom; /* prepare the position in the new image */
cy *= zoom;
}
else
continue;
}
if (status == ZOOM_OUT)
{
if (ds < ds_max)
{
/* if ds is 1 and ds_min is greater than 1, jump ds to ds_min. */
int zoom = (ds < ds_min)? ds_min: 2;
ds *= zoom; /* increase downscaling to zoom out */
get_view(&decoder, &cx, &cy);
cx /= zoom; /* prepare the position in the new image */
cy /= zoom;
}
else
continue;
}
break;
}
rb->lcd_clear_display();
}
while (status != PLUGIN_OK && status != PLUGIN_USB_CONNECTED
&& status != PLUGIN_OTHER);
return status;
}
/******************** Plugin entry point *********************/
enum plugin_status plugin_start(const void* parameter)
{
int condition;
#if LCD_DEPTH > 1
old_backdrop = rb->lcd_get_backdrop();
#endif
if (!parameter) return PLUGIN_ERROR;
#if PLUGIN_BUFFER_SIZE >= MIN_MEM
memory = rb->plugin_get_buffer((size_t *)&memory_size);
#else
memory = rb->plugin_get_audio_buffer((size_t *)&memory_size);
#endif
rb->strcpy(np_file, parameter);
get_pic_list();
if (!entries) return PLUGIN_ERROR;
#if (PLUGIN_BUFFER_SIZE >= MIN_MEM) && !defined(SIMULATOR)
if (!rb->audio_status()) {
plug_buf = false;
memory = rb->plugin_get_audio_buffer((size_t *)&memory_size);
}
#endif
memory_max = memory + memory_size - 1;
/* should be ok to just load settings since the plugin itself has
just been loaded from disk and the drive should be spinning */
configfile_load(PNG_CONFIGFILE, png_config,
ARRAYLEN(png_config), PNG_SETTINGS_MINVERSION);
old_settings = png_settings;
/* Turn off backlight timeout */
backlight_force_on(); /* backlight control in lib/helper.c */
do
{
condition = load_and_show(np_file);
} while (condition != PLUGIN_OK && condition != PLUGIN_USB_CONNECTED
&& condition != PLUGIN_ERROR);
if (rb->memcmp(&png_settings, &old_settings, sizeof (png_settings)))
{
/* Just in case drive has to spin, keep it from looking locked */
rb->splash(0, "Saving Settings");
configfile_save(PNG_CONFIGFILE, png_config,
ARRAYLEN(png_config), PNG_SETTINGS_VERSION);
}
#if !defined(SIMULATOR) && defined(HAVE_DISK_STORAGE)
/* set back ata spindown time in case we changed it */
rb->storage_spindown(rb->global_settings->disk_spindown);
#endif
/* Turn on backlight timeout (revert to settings) */
backlight_use_settings(); /* backlight control in lib/helper.c */
return condition;
}
|