15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
/* This file is automatically rebuilt by the Cesium build process. */
define(['./Matrix2-0e286ffc', './AxisAlignedBoundingBox-96fb2a8b', './Transforms-de823166', './when-8166c7dd', './RuntimeError-4fdc4459', './TerrainEncoding-6e967e8e', './ComponentDatatype-9ed50558', './OrientedBoundingBox-7045a823', './WebMercatorProjection-37aaa17f', './createTaskProcessorWorker', './combine-a5c4cc47', './AttributeCompression-a3d02c34', './WebGLConstants-0664004c', './EllipsoidTangentPlane-892d7b0a', './IntersectionTests-30f5d388', './Plane-456cf3fd'], (function (Matrix2, AxisAlignedBoundingBox, Transforms, when, RuntimeError, TerrainEncoding, ComponentDatatype, OrientedBoundingBox, WebMercatorProjection, createTaskProcessorWorker, combine, AttributeCompression, WebGLConstants, EllipsoidTangentPlane, IntersectionTests, Plane) { 'use strict';
 
  /**
   * The encoding that is used for a heightmap
   *
   * @enum {Number}
   */
  var HeightmapEncoding = {
    /**
     * No encoding
     *
     * @type {Number}
     * @constant
     */
    NONE: 0,
 
    /**
     * LERC encoding
     *
     * @type {Number}
     * @constant
     *
     * @see {@link https://github.com/Esri/lerc|The LERC specification}
     */
    LERC: 1,
  };
  var HeightmapEncoding$1 = Object.freeze(HeightmapEncoding);
 
  /**
   * Contains functions to create a mesh from a heightmap image.
   *
   * @namespace HeightmapTessellator
   *
   * @private
   */
  var HeightmapTessellator = {};
 
  /**
   * The default structure of a heightmap, as given to {@link HeightmapTessellator.computeVertices}.
   *
   * @constant
   */
  HeightmapTessellator.DEFAULT_STRUCTURE = Object.freeze({
    heightScale: 1.0,
    heightOffset: 0.0,
    elementsPerHeight: 1,
    stride: 1,
    elementMultiplier: 256.0,
    isBigEndian: false,
  });
 
  var cartesian3Scratch = new Matrix2.Cartesian3();
  var matrix4Scratch = new Matrix2.Matrix4();
  var minimumScratch = new Matrix2.Cartesian3();
  var maximumScratch = new Matrix2.Cartesian3();
 
  /**
   * Fills an array of vertices from a heightmap image.
   *
   * @param {Object} options Object with the following properties:
   * @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} options.heightmap The heightmap to tessellate.
   * @param {Number} options.width The width of the heightmap, in height samples.
   * @param {Number} options.height The height of the heightmap, in height samples.
   * @param {Number} options.skirtHeight The height of skirts to drape at the edges of the heightmap.
   * @param {Rectangle} options.nativeRectangle A rectangle in the native coordinates of the heightmap's projection.  For
   *                 a heightmap with a geographic projection, this is degrees.  For the web mercator
   *                 projection, this is meters.
   * @param {Number} [options.exaggeration=1.0] The scale used to exaggerate the terrain.
   * @param {Number} [options.exaggerationRelativeHeight=0.0] The height from which terrain is exaggerated.
   * @param {Rectangle} [options.rectangle] The rectangle covered by the heightmap, in geodetic coordinates with north, south, east and
   *                 west properties in radians.  Either rectangle or nativeRectangle must be provided.  If both
   *                 are provided, they're assumed to be consistent.
   * @param {Boolean} [options.isGeographic=true] True if the heightmap uses a {@link GeographicProjection}, or false if it uses
   *                  a {@link WebMercatorProjection}.
   * @param {Cartesian3} [options.relativeToCenter=Cartesian3.ZERO] The positions will be computed as <code>Cartesian3.subtract(worldPosition, relativeToCenter)</code>.
   * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to which the heightmap applies.
   * @param {Object} [options.structure] An object describing the structure of the height data.
   * @param {Number} [options.structure.heightScale=1.0] The factor by which to multiply height samples in order to obtain
   *                 the height above the heightOffset, in meters.  The heightOffset is added to the resulting
   *                 height after multiplying by the scale.
   * @param {Number} [options.structure.heightOffset=0.0] The offset to add to the scaled height to obtain the final
   *                 height in meters.  The offset is added after the height sample is multiplied by the
   *                 heightScale.
   * @param {Number} [options.structure.elementsPerHeight=1] The number of elements in the buffer that make up a single height
   *                 sample.  This is usually 1, indicating that each element is a separate height sample.  If
   *                 it is greater than 1, that number of elements together form the height sample, which is
   *                 computed according to the structure.elementMultiplier and structure.isBigEndian properties.
   * @param {Number} [options.structure.stride=1] The number of elements to skip to get from the first element of
   *                 one height to the first element of the next height.
   * @param {Number} [options.structure.elementMultiplier=256.0] The multiplier used to compute the height value when the
   *                 stride property is greater than 1.  For example, if the stride is 4 and the strideMultiplier
   *                 is 256, the height is computed as follows:
   *                 `height = buffer[index] + buffer[index + 1] * 256 + buffer[index + 2] * 256 * 256 + buffer[index + 3] * 256 * 256 * 256`
   *                 This is assuming that the isBigEndian property is false.  If it is true, the order of the
   *                 elements is reversed.
   * @param {Number} [options.structure.lowestEncodedHeight] The lowest value that can be stored in the height buffer.  Any heights that are lower
   *                 than this value after encoding with the `heightScale` and `heightOffset` are clamped to this value.  For example, if the height
   *                 buffer is a `Uint16Array`, this value should be 0 because a `Uint16Array` cannot store negative numbers.  If this parameter is
   *                 not specified, no minimum value is enforced.
   * @param {Number} [options.structure.highestEncodedHeight] The highest value that can be stored in the height buffer.  Any heights that are higher
   *                 than this value after encoding with the `heightScale` and `heightOffset` are clamped to this value.  For example, if the height
   *                 buffer is a `Uint16Array`, this value should be `256 * 256 - 1` or 65535 because a `Uint16Array` cannot store numbers larger
   *                 than 65535.  If this parameter is not specified, no maximum value is enforced.
   * @param {Boolean} [options.structure.isBigEndian=false] Indicates endianness of the elements in the buffer when the
   *                  stride property is greater than 1.  If this property is false, the first element is the
   *                  low-order element.  If it is true, the first element is the high-order element.
   *
   * @example
   * var width = 5;
   * var height = 5;
   * var statistics = Cesium.HeightmapTessellator.computeVertices({
   *     heightmap : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
   *     width : width,
   *     height : height,
   *     skirtHeight : 0.0,
   *     nativeRectangle : {
   *         west : 10.0,
   *         east : 20.0,
   *         south : 30.0,
   *         north : 40.0
   *     }
   * });
   *
   * var encoding = statistics.encoding;
   * var position = encoding.decodePosition(statistics.vertices, index);
   */
  HeightmapTessellator.computeVertices = function (options) {
    //>>includeStart('debug', pragmas.debug);
    if (!when.defined(options) || !when.defined(options.heightmap)) {
      throw new RuntimeError.DeveloperError("options.heightmap is required.");
    }
    if (!when.defined(options.width) || !when.defined(options.height)) {
      throw new RuntimeError.DeveloperError("options.width and options.height are required.");
    }
    if (!when.defined(options.nativeRectangle)) {
      throw new RuntimeError.DeveloperError("options.nativeRectangle is required.");
    }
    if (!when.defined(options.skirtHeight)) {
      throw new RuntimeError.DeveloperError("options.skirtHeight is required.");
    }
    //>>includeEnd('debug');
 
    // This function tends to be a performance hotspot for terrain rendering,
    // so it employs a lot of inlining and unrolling as an optimization.
    // In particular, the functionality of Ellipsoid.cartographicToCartesian
    // is inlined.
 
    var cos = Math.cos;
    var sin = Math.sin;
    var sqrt = Math.sqrt;
    var atan = Math.atan;
    var exp = Math.exp;
    var piOverTwo = ComponentDatatype.CesiumMath.PI_OVER_TWO;
    var toRadians = ComponentDatatype.CesiumMath.toRadians;
 
    var heightmap = options.heightmap;
    var width = options.width;
    var height = options.height;
    var skirtHeight = options.skirtHeight;
    var hasSkirts = skirtHeight > 0.0;
 
    var isGeographic = when.defaultValue(options.isGeographic, true);
    var ellipsoid = when.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84);
 
    var oneOverGlobeSemimajorAxis = 1.0 / ellipsoid.maximumRadius;
 
    var nativeRectangle = Matrix2.Rectangle.clone(options.nativeRectangle);
    var rectangle = Matrix2.Rectangle.clone(options.rectangle);
 
    var geographicWest;
    var geographicSouth;
    var geographicEast;
    var geographicNorth;
 
    if (!when.defined(rectangle)) {
      if (isGeographic) {
        geographicWest = toRadians(nativeRectangle.west);
        geographicSouth = toRadians(nativeRectangle.south);
        geographicEast = toRadians(nativeRectangle.east);
        geographicNorth = toRadians(nativeRectangle.north);
      } else {
        geographicWest = nativeRectangle.west * oneOverGlobeSemimajorAxis;
        geographicSouth =
          piOverTwo -
          2.0 * atan(exp(-nativeRectangle.south * oneOverGlobeSemimajorAxis));
        geographicEast = nativeRectangle.east * oneOverGlobeSemimajorAxis;
        geographicNorth =
          piOverTwo -
          2.0 * atan(exp(-nativeRectangle.north * oneOverGlobeSemimajorAxis));
      }
    } else {
      geographicWest = rectangle.west;
      geographicSouth = rectangle.south;
      geographicEast = rectangle.east;
      geographicNorth = rectangle.north;
    }
 
    var relativeToCenter = options.relativeToCenter;
    var hasRelativeToCenter = when.defined(relativeToCenter);
    relativeToCenter = hasRelativeToCenter ? relativeToCenter : Matrix2.Cartesian3.ZERO;
    var includeWebMercatorT = when.defaultValue(options.includeWebMercatorT, false);
 
    var exaggeration = when.defaultValue(options.exaggeration, 1.0);
    var exaggerationRelativeHeight = when.defaultValue(
      options.exaggerationRelativeHeight,
      0.0
    );
    var hasExaggeration = exaggeration !== 1.0;
    var includeGeodeticSurfaceNormals = hasExaggeration;
 
    var structure = when.defaultValue(
      options.structure,
      HeightmapTessellator.DEFAULT_STRUCTURE
    );
    var heightScale = when.defaultValue(
      structure.heightScale,
      HeightmapTessellator.DEFAULT_STRUCTURE.heightScale
    );
    var heightOffset = when.defaultValue(
      structure.heightOffset,
      HeightmapTessellator.DEFAULT_STRUCTURE.heightOffset
    );
    var elementsPerHeight = when.defaultValue(
      structure.elementsPerHeight,
      HeightmapTessellator.DEFAULT_STRUCTURE.elementsPerHeight
    );
    var stride = when.defaultValue(
      structure.stride,
      HeightmapTessellator.DEFAULT_STRUCTURE.stride
    );
    var elementMultiplier = when.defaultValue(
      structure.elementMultiplier,
      HeightmapTessellator.DEFAULT_STRUCTURE.elementMultiplier
    );
    var isBigEndian = when.defaultValue(
      structure.isBigEndian,
      HeightmapTessellator.DEFAULT_STRUCTURE.isBigEndian
    );
 
    var rectangleWidth = Matrix2.Rectangle.computeWidth(nativeRectangle);
    var rectangleHeight = Matrix2.Rectangle.computeHeight(nativeRectangle);
 
    var granularityX = rectangleWidth / (width - 1);
    var granularityY = rectangleHeight / (height - 1);
 
    if (!isGeographic) {
      rectangleWidth *= oneOverGlobeSemimajorAxis;
      rectangleHeight *= oneOverGlobeSemimajorAxis;
    }
 
    var radiiSquared = ellipsoid.radiiSquared;
    var radiiSquaredX = radiiSquared.x;
    var radiiSquaredY = radiiSquared.y;
    var radiiSquaredZ = radiiSquared.z;
 
    var minimumHeight = 65536.0;
    var maximumHeight = -65536.0;
 
    var fromENU = Transforms.Transforms.eastNorthUpToFixedFrame(relativeToCenter, ellipsoid);
    var toENU = Matrix2.Matrix4.inverseTransformation(fromENU, matrix4Scratch);
 
    var southMercatorY;
    var oneOverMercatorHeight;
    if (includeWebMercatorT) {
      southMercatorY = WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(
        geographicSouth
      );
      oneOverMercatorHeight =
        1.0 /
        (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(geographicNorth) -
          southMercatorY);
    }
 
    var minimum = minimumScratch;
    minimum.x = Number.POSITIVE_INFINITY;
    minimum.y = Number.POSITIVE_INFINITY;
    minimum.z = Number.POSITIVE_INFINITY;
 
    var maximum = maximumScratch;
    maximum.x = Number.NEGATIVE_INFINITY;
    maximum.y = Number.NEGATIVE_INFINITY;
    maximum.z = Number.NEGATIVE_INFINITY;
 
    var hMin = Number.POSITIVE_INFINITY;
 
    var gridVertexCount = width * height;
    var edgeVertexCount = skirtHeight > 0.0 ? width * 2 + height * 2 : 0;
    var vertexCount = gridVertexCount + edgeVertexCount;
 
    var positions = new Array(vertexCount);
    var heights = new Array(vertexCount);
    var uvs = new Array(vertexCount);
    var webMercatorTs = includeWebMercatorT ? new Array(vertexCount) : [];
    var geodeticSurfaceNormals = includeGeodeticSurfaceNormals
      ? new Array(vertexCount)
      : [];
 
    var startRow = 0;
    var endRow = height;
    var startCol = 0;
    var endCol = width;
 
    if (hasSkirts) {
      --startRow;
      ++endRow;
      --startCol;
      ++endCol;
    }
 
    var skirtOffsetPercentage = 0.00001;
 
    for (var rowIndex = startRow; rowIndex < endRow; ++rowIndex) {
      var row = rowIndex;
      if (row < 0) {
        row = 0;
      }
      if (row >= height) {
        row = height - 1;
      }
 
      var latitude = nativeRectangle.north - granularityY * row;
 
      if (!isGeographic) {
        latitude =
          piOverTwo - 2.0 * atan(exp(-latitude * oneOverGlobeSemimajorAxis));
      } else {
        latitude = toRadians(latitude);
      }
 
      var v = (latitude - geographicSouth) / (geographicNorth - geographicSouth);
      v = ComponentDatatype.CesiumMath.clamp(v, 0.0, 1.0);
 
      var isNorthEdge = rowIndex === startRow;
      var isSouthEdge = rowIndex === endRow - 1;
      if (skirtHeight > 0.0) {
        if (isNorthEdge) {
          latitude += skirtOffsetPercentage * rectangleHeight;
        } else if (isSouthEdge) {
          latitude -= skirtOffsetPercentage * rectangleHeight;
        }
      }
 
      var cosLatitude = cos(latitude);
      var nZ = sin(latitude);
      var kZ = radiiSquaredZ * nZ;
 
      var webMercatorT;
      if (includeWebMercatorT) {
        webMercatorT =
          (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(latitude) -
            southMercatorY) *
          oneOverMercatorHeight;
      }
 
      for (var colIndex = startCol; colIndex < endCol; ++colIndex) {
        var col = colIndex;
        if (col < 0) {
          col = 0;
        }
        if (col >= width) {
          col = width - 1;
        }
 
        var terrainOffset = row * (width * stride) + col * stride;
 
        var heightSample;
        if (elementsPerHeight === 1) {
          heightSample = heightmap[terrainOffset];
        } else {
          heightSample = 0;
 
          var elementOffset;
          if (isBigEndian) {
            for (
              elementOffset = 0;
              elementOffset < elementsPerHeight;
              ++elementOffset
            ) {
              heightSample =
                heightSample * elementMultiplier +
                heightmap[terrainOffset + elementOffset];
            }
          } else {
            for (
              elementOffset = elementsPerHeight - 1;
              elementOffset >= 0;
              --elementOffset
            ) {
              heightSample =
                heightSample * elementMultiplier +
                heightmap[terrainOffset + elementOffset];
            }
          }
        }
 
        heightSample = heightSample * heightScale + heightOffset;
 
        maximumHeight = Math.max(maximumHeight, heightSample);
        minimumHeight = Math.min(minimumHeight, heightSample);
 
        var longitude = nativeRectangle.west + granularityX * col;
 
        if (!isGeographic) {
          longitude = longitude * oneOverGlobeSemimajorAxis;
        } else {
          longitude = toRadians(longitude);
        }
 
        var u = (longitude - geographicWest) / (geographicEast - geographicWest);
        u = ComponentDatatype.CesiumMath.clamp(u, 0.0, 1.0);
 
        var index = row * width + col;
 
        if (skirtHeight > 0.0) {
          var isWestEdge = colIndex === startCol;
          var isEastEdge = colIndex === endCol - 1;
          var isEdge = isNorthEdge || isSouthEdge || isWestEdge || isEastEdge;
          var isCorner =
            (isNorthEdge || isSouthEdge) && (isWestEdge || isEastEdge);
          if (isCorner) {
            // Don't generate skirts on the corners.
            continue;
          } else if (isEdge) {
            heightSample -= skirtHeight;
 
            if (isWestEdge) {
              // The outer loop iterates north to south but the indices are ordered south to north, hence the index flip below
              index = gridVertexCount + (height - row - 1);
              longitude -= skirtOffsetPercentage * rectangleWidth;
            } else if (isSouthEdge) {
              // Add after west indices. South indices are ordered east to west.
              index = gridVertexCount + height + (width - col - 1);
            } else if (isEastEdge) {
              // Add after west and south indices. East indices are ordered north to south. The index is flipped like above.
              index = gridVertexCount + height + width + row;
              longitude += skirtOffsetPercentage * rectangleWidth;
            } else if (isNorthEdge) {
              // Add after west, south, and east indices. North indices are ordered west to east.
              index = gridVertexCount + height + width + height + col;
            }
          }
        }
 
        var nX = cosLatitude * cos(longitude);
        var nY = cosLatitude * sin(longitude);
 
        var kX = radiiSquaredX * nX;
        var kY = radiiSquaredY * nY;
 
        var gamma = sqrt(kX * nX + kY * nY + kZ * nZ);
        var oneOverGamma = 1.0 / gamma;
 
        var rSurfaceX = kX * oneOverGamma;
        var rSurfaceY = kY * oneOverGamma;
        var rSurfaceZ = kZ * oneOverGamma;
 
        var position = new Matrix2.Cartesian3();
        position.x = rSurfaceX + nX * heightSample;
        position.y = rSurfaceY + nY * heightSample;
        position.z = rSurfaceZ + nZ * heightSample;
 
        Matrix2.Matrix4.multiplyByPoint(toENU, position, cartesian3Scratch);
        Matrix2.Cartesian3.minimumByComponent(cartesian3Scratch, minimum, minimum);
        Matrix2.Cartesian3.maximumByComponent(cartesian3Scratch, maximum, maximum);
        hMin = Math.min(hMin, heightSample);
 
        positions[index] = position;
        uvs[index] = new Matrix2.Cartesian2(u, v);
        heights[index] = heightSample;
 
        if (includeWebMercatorT) {
          webMercatorTs[index] = webMercatorT;
        }
 
        if (includeGeodeticSurfaceNormals) {
          geodeticSurfaceNormals[index] = ellipsoid.geodeticSurfaceNormal(
            position
          );
        }
      }
    }
 
    var boundingSphere3D = Transforms.BoundingSphere.fromPoints(positions);
    var orientedBoundingBox;
    if (when.defined(rectangle)) {
      orientedBoundingBox = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(
        rectangle,
        minimumHeight,
        maximumHeight,
        ellipsoid
      );
    }
 
    var occludeePointInScaledSpace;
    if (hasRelativeToCenter) {
      var occluder = new TerrainEncoding.EllipsoidalOccluder(ellipsoid);
      occludeePointInScaledSpace = occluder.computeHorizonCullingPointPossiblyUnderEllipsoid(
        relativeToCenter,
        positions,
        minimumHeight
      );
    }
 
    var aaBox = new AxisAlignedBoundingBox.AxisAlignedBoundingBox(minimum, maximum, relativeToCenter);
    var encoding = new TerrainEncoding.TerrainEncoding(
      relativeToCenter,
      aaBox,
      hMin,
      maximumHeight,
      fromENU,
      false,
      includeWebMercatorT,
      includeGeodeticSurfaceNormals,
      exaggeration,
      exaggerationRelativeHeight
    );
    var vertices = new Float32Array(vertexCount * encoding.stride);
 
    var bufferIndex = 0;
    for (var j = 0; j < vertexCount; ++j) {
      bufferIndex = encoding.encode(
        vertices,
        bufferIndex,
        positions[j],
        uvs[j],
        heights[j],
        undefined,
        webMercatorTs[j],
        geodeticSurfaceNormals[j]
      );
    }
 
    return {
      vertices: vertices,
      maximumHeight: maximumHeight,
      minimumHeight: minimumHeight,
      encoding: encoding,
      boundingSphere3D: boundingSphere3D,
      orientedBoundingBox: orientedBoundingBox,
      occludeePointInScaledSpace: occludeePointInScaledSpace,
    };
  };
 
  /* This file is automatically rebuilt by the Cesium build process. */
 
  var LercDecode = when.createCommonjsModule(function (module) {
  /* jshint forin: false, bitwise: false */
  /*
  Copyright 2015-2018 Esri
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 
  A copy of the license and additional notices are located with the
  source distribution at:
 
  http://github.com/Esri/lerc/
 
  Contributors:  Johannes Schmid, (LERC v1)
                 Chayanika Khatua, (LERC v1)
                 Wenxue Ju (LERC v1, v2.x)
  */
 
  /* Copyright 2015-2018 Esri. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 @preserve */
 
  /**
   * a module for decoding LERC blobs
   * @module Lerc
   */
  (function() {
    //the original LercDecode for Version 1
    var LercDecode = (function() {
 
      // WARNING: This decoder version can only read old version 1 Lerc blobs. Use with caution.
 
      // Note: currently, this module only has an implementation for decoding LERC data, not encoding. The name of
      // the class was chosen to be future proof.
 
      var CntZImage = {};
 
      CntZImage.defaultNoDataValue = -3.4027999387901484e+38; // smallest Float32 value
 
      /**
       * Decode a LERC byte stream and return an object containing the pixel data and some required and optional
       * information about it, such as the image's width and height.
       *
       * @param {ArrayBuffer} input The LERC input byte stream
       * @param {object} [options] Decoding options, containing any of the following properties:
       * @config {number} [inputOffset = 0]
       *        Skip the first inputOffset bytes of the input byte stream. A valid LERC file is expected at that position.
       * @config {Uint8Array} [encodedMask = null]
       *        If specified, the decoder will not read mask information from the input and use the specified encoded
       *        mask data instead. Mask header/data must not be present in the LERC byte stream in this case.
       * @config {number} [noDataValue = LercCode.defaultNoDataValue]
       *        Pixel value to use for masked pixels.
       * @config {ArrayBufferView|Array} [pixelType = Float32Array]
       *        The desired type of the pixelData array in the return value. Note that it is the caller's responsibility to
       *        provide an appropriate noDataValue if the default pixelType is overridden.
       * @config {boolean} [returnMask = false]
       *        If true, the return value will contain a maskData property of type Uint8Array which has one element per
       *        pixel, the value of which is 1 or 0 depending on whether that pixel's data is present or masked. If the
       *        input LERC data does not contain a mask, maskData will not be returned.
       * @config {boolean} [returnEncodedMask = false]
       *        If true, the return value will contain a encodedMaskData property, which can be passed into encode() as
       *        encodedMask.
       * @config {boolean} [returnFileInfo = false]
       *        If true, the return value will have a fileInfo property that contains metadata obtained from the
       *        LERC headers and the decoding process.
       * @config {boolean} [computeUsedBitDepths = false]
       *        If true, the fileInfo property in the return value will contain the set of all block bit depths
       *        encountered during decoding. Will only have an effect if returnFileInfo option is true.
       * @returns {{width, height, pixelData, minValue, maxValue, noDataValue, maskData, encodedMaskData, fileInfo}}
       */
      CntZImage.decode = function(input, options) {
        options = options || {};
 
        var skipMask = options.encodedMaskData || (options.encodedMaskData === null);
        var parsedData = parse(input, options.inputOffset || 0, skipMask);
 
        var noDataValue = (options.noDataValue !== null) ? options.noDataValue : CntZImage.defaultNoDataValue;
 
        var uncompressedData = uncompressPixelValues(parsedData, options.pixelType || Float32Array,
          options.encodedMaskData, noDataValue, options.returnMask);
 
        var result = {
          width: parsedData.width,
          height: parsedData.height,
          pixelData: uncompressedData.resultPixels,
          minValue: uncompressedData.minValue,
          maxValue: parsedData.pixels.maxValue,
          noDataValue: noDataValue
        };
 
        if (uncompressedData.resultMask) {
          result.maskData = uncompressedData.resultMask;
        }
 
        if (options.returnEncodedMask && parsedData.mask) {
          result.encodedMaskData = parsedData.mask.bitset ? parsedData.mask.bitset : null;
        }
 
        if (options.returnFileInfo) {
          result.fileInfo = formatFileInfo(parsedData);
          if (options.computeUsedBitDepths) {
            result.fileInfo.bitDepths = computeUsedBitDepths(parsedData);
          }
        }
 
        return result;
      };
 
      var uncompressPixelValues = function(data, TypedArrayClass, maskBitset, noDataValue, storeDecodedMask) {
        var blockIdx = 0;
        var numX = data.pixels.numBlocksX;
        var numY = data.pixels.numBlocksY;
        var blockWidth = Math.floor(data.width / numX);
        var blockHeight = Math.floor(data.height / numY);
        var scale = 2 * data.maxZError;
        var minValue = Number.MAX_VALUE, currentValue;
        maskBitset = maskBitset || ((data.mask) ? data.mask.bitset : null);
 
        var resultPixels, resultMask;
        resultPixels = new TypedArrayClass(data.width * data.height);
        if (storeDecodedMask && maskBitset) {
          resultMask = new Uint8Array(data.width * data.height);
        }
        var blockDataBuffer = new Float32Array(blockWidth * blockHeight);
 
        var xx, yy;
        for (var y = 0; y <= numY; y++) {
          var thisBlockHeight = (y !== numY) ? blockHeight : (data.height % numY);
          if (thisBlockHeight === 0) {
            continue;
          }
          for (var x = 0; x <= numX; x++) {
            var thisBlockWidth = (x !== numX) ? blockWidth : (data.width % numX);
            if (thisBlockWidth === 0) {
              continue;
            }
 
            var outPtr = y * data.width * blockHeight + x * blockWidth;
            var outStride = data.width - thisBlockWidth;
 
            var block = data.pixels.blocks[blockIdx];
 
            var blockData, blockPtr, constValue;
            if (block.encoding < 2) {
              // block is either uncompressed or bit-stuffed (encodings 0 and 1)
              if (block.encoding === 0) {
                // block is uncompressed
                blockData = block.rawData;
              } else {
                // block is bit-stuffed
                unstuff(block.stuffedData, block.bitsPerPixel, block.numValidPixels, block.offset, scale, blockDataBuffer, data.pixels.maxValue);
                blockData = blockDataBuffer;
              }
              blockPtr = 0;
            }
            else if (block.encoding === 2) {
              // block is all 0
              constValue = 0;
            }
            else {
              // block has constant value (encoding === 3)
              constValue = block.offset;
            }
 
            var maskByte;
            if (maskBitset) {
              for (yy = 0; yy < thisBlockHeight; yy++) {
                if (outPtr & 7) {
                  //
                  maskByte = maskBitset[outPtr >> 3];
                  maskByte <<= outPtr & 7;
                }
                for (xx = 0; xx < thisBlockWidth; xx++) {
                  if (!(outPtr & 7)) {
                    // read next byte from mask
                    maskByte = maskBitset[outPtr >> 3];
                  }
                  if (maskByte & 128) {
                    // pixel data present
                    if (resultMask) {
                      resultMask[outPtr] = 1;
                    }
                    currentValue = (block.encoding < 2) ? blockData[blockPtr++] : constValue;
                    minValue = minValue > currentValue ? currentValue : minValue;
                    resultPixels[outPtr++] = currentValue;
                  } else {
                    // pixel data not present
                    if (resultMask) {
                      resultMask[outPtr] = 0;
                    }
                    resultPixels[outPtr++] = noDataValue;
                  }
                  maskByte <<= 1;
                }
                outPtr += outStride;
              }
            } else {
              // mask not present, simply copy block over
              if (block.encoding < 2) {
                // duplicating this code block for performance reasons
                // blockData case:
                for (yy = 0; yy < thisBlockHeight; yy++) {
                  for (xx = 0; xx < thisBlockWidth; xx++) {
                    currentValue = blockData[blockPtr++];
                    minValue = minValue > currentValue ? currentValue : minValue;
                    resultPixels[outPtr++] = currentValue;
                  }
                  outPtr += outStride;
                }
              }
              else {
                // constValue case:
                minValue = minValue > constValue ? constValue : minValue;
                for (yy = 0; yy < thisBlockHeight; yy++) {
                  for (xx = 0; xx < thisBlockWidth; xx++) {
                    resultPixels[outPtr++] = constValue;
                  }
                  outPtr += outStride;
                }
              }
            }
            if ((block.encoding === 1) && (blockPtr !== block.numValidPixels)) {
              throw "Block and Mask do not match";
            }
            blockIdx++;
          }
        }
 
        return {
          resultPixels: resultPixels,
          resultMask: resultMask,
          minValue: minValue
        };
      };
 
      var formatFileInfo = function(data) {
        return {
          "fileIdentifierString": data.fileIdentifierString,
          "fileVersion": data.fileVersion,
          "imageType": data.imageType,
          "height": data.height,
          "width": data.width,
          "maxZError": data.maxZError,
          "eofOffset": data.eofOffset,
          "mask": data.mask ? {
            "numBlocksX": data.mask.numBlocksX,
            "numBlocksY": data.mask.numBlocksY,
            "numBytes": data.mask.numBytes,
            "maxValue": data.mask.maxValue
          } : null,
          "pixels": {
            "numBlocksX": data.pixels.numBlocksX,
            "numBlocksY": data.pixels.numBlocksY,
            "numBytes": data.pixels.numBytes,
            "maxValue": data.pixels.maxValue,
            "noDataValue": data.noDataValue
          }
        };
      };
 
      var computeUsedBitDepths = function(data) {
        var numBlocks = data.pixels.numBlocksX * data.pixels.numBlocksY;
        var bitDepths = {};
        for (var i = 0; i < numBlocks; i++) {
          var block = data.pixels.blocks[i];
          if (block.encoding === 0) {
            bitDepths.float32 = true;
          } else if (block.encoding === 1) {
            bitDepths[block.bitsPerPixel] = true;
          } else {
            bitDepths[0] = true;
          }
        }
 
        return Object.keys(bitDepths);
      };
 
      var parse = function(input, fp, skipMask) {
        var data = {};
 
        // File header
        var fileIdView = new Uint8Array(input, fp, 10);
        data.fileIdentifierString = String.fromCharCode.apply(null, fileIdView);
        if (data.fileIdentifierString.trim() !== "CntZImage") {
          throw "Unexpected file identifier string: " + data.fileIdentifierString;
        }
        fp += 10;
        var view = new DataView(input, fp, 24);
        data.fileVersion = view.getInt32(0, true);
        data.imageType = view.getInt32(4, true);
        data.height = view.getUint32(8, true);
        data.width = view.getUint32(12, true);
        data.maxZError = view.getFloat64(16, true);
        fp += 24;
 
        // Mask Header
        if (!skipMask) {
          view = new DataView(input, fp, 16);
          data.mask = {};
          data.mask.numBlocksY = view.getUint32(0, true);
          data.mask.numBlocksX = view.getUint32(4, true);
          data.mask.numBytes = view.getUint32(8, true);
          data.mask.maxValue = view.getFloat32(12, true);
          fp += 16;
 
          // Mask Data
          if (data.mask.numBytes > 0) {
            var bitset = new Uint8Array(Math.ceil(data.width * data.height / 8));
            view = new DataView(input, fp, data.mask.numBytes);
            var cnt = view.getInt16(0, true);
            var ip = 2, op = 0;
            do {
              if (cnt > 0) {
                while (cnt--) { bitset[op++] = view.getUint8(ip++); }
              } else {
                var val = view.getUint8(ip++);
                cnt = -cnt;
                while (cnt--) { bitset[op++] = val; }
              }
              cnt = view.getInt16(ip, true);
              ip += 2;
            } while (ip < data.mask.numBytes);
            if ((cnt !== -32768) || (op < bitset.length)) {
              throw "Unexpected end of mask RLE encoding";
            }
            data.mask.bitset = bitset;
            fp += data.mask.numBytes;
          }
          else if ((data.mask.numBytes | data.mask.numBlocksY | data.mask.maxValue) === 0) {  // Special case, all nodata
            data.mask.bitset = new Uint8Array(Math.ceil(data.width * data.height / 8));
          }
        }
 
        // Pixel Header
        view = new DataView(input, fp, 16);
        data.pixels = {};
        data.pixels.numBlocksY = view.getUint32(0, true);
        data.pixels.numBlocksX = view.getUint32(4, true);
        data.pixels.numBytes = view.getUint32(8, true);
        data.pixels.maxValue = view.getFloat32(12, true);
        fp += 16;
 
        var numBlocksX = data.pixels.numBlocksX;
        var numBlocksY = data.pixels.numBlocksY;
        // the number of blocks specified in the header does not take into account the blocks at the end of
        // each row/column with a special width/height that make the image complete in case the width is not
        // evenly divisible by the number of blocks.
        var actualNumBlocksX = numBlocksX + ((data.width % numBlocksX) > 0 ? 1 : 0);
        var actualNumBlocksY = numBlocksY + ((data.height % numBlocksY) > 0 ? 1 : 0);
        data.pixels.blocks = new Array(actualNumBlocksX * actualNumBlocksY);
        var blockI = 0;
        for (var blockY = 0; blockY < actualNumBlocksY; blockY++) {
          for (var blockX = 0; blockX < actualNumBlocksX; blockX++) {
 
            // Block
            var size = 0;
            var bytesLeft = input.byteLength - fp;
            view = new DataView(input, fp, Math.min(10, bytesLeft));
            var block = {};
            data.pixels.blocks[blockI++] = block;
            var headerByte = view.getUint8(0); size++;
            block.encoding = headerByte & 63;
            if (block.encoding > 3) {
              throw "Invalid block encoding (" + block.encoding + ")";
            }
            if (block.encoding === 2) {
              fp++;
              continue;
            }
            if ((headerByte !== 0) && (headerByte !== 2)) {
              headerByte >>= 6;
              block.offsetType = headerByte;
              if (headerByte === 2) {
                block.offset = view.getInt8(1); size++;
              } else if (headerByte === 1) {
                block.offset = view.getInt16(1, true); size += 2;
              } else if (headerByte === 0) {
                block.offset = view.getFloat32(1, true); size += 4;
              } else {
                throw "Invalid block offset type";
              }
 
              if (block.encoding === 1) {
                headerByte = view.getUint8(size); size++;
                block.bitsPerPixel = headerByte & 63;
                headerByte >>= 6;
                block.numValidPixelsType = headerByte;
                if (headerByte === 2) {
                  block.numValidPixels = view.getUint8(size); size++;
                } else if (headerByte === 1) {
                  block.numValidPixels = view.getUint16(size, true); size += 2;
                } else if (headerByte === 0) {
                  block.numValidPixels = view.getUint32(size, true); size += 4;
                } else {
                  throw "Invalid valid pixel count type";
                }
              }
            }
            fp += size;
 
            if (block.encoding === 3) {
              continue;
            }
 
            var arrayBuf, store8;
            if (block.encoding === 0) {
              var numPixels = (data.pixels.numBytes - 1) / 4;
              if (numPixels !== Math.floor(numPixels)) {
                throw "uncompressed block has invalid length";
              }
              arrayBuf = new ArrayBuffer(numPixels * 4);
              store8 = new Uint8Array(arrayBuf);
              store8.set(new Uint8Array(input, fp, numPixels * 4));
              var rawData = new Float32Array(arrayBuf);
              block.rawData = rawData;
              fp += numPixels * 4;
            } else if (block.encoding === 1) {
              var dataBytes = Math.ceil(block.numValidPixels * block.bitsPerPixel / 8);
              var dataWords = Math.ceil(dataBytes / 4);
              arrayBuf = new ArrayBuffer(dataWords * 4);
              store8 = new Uint8Array(arrayBuf);
              store8.set(new Uint8Array(input, fp, dataBytes));
              block.stuffedData = new Uint32Array(arrayBuf);
              fp += dataBytes;
            }
          }
        }
        data.eofOffset = fp;
        return data;
      };
 
      var unstuff = function(src, bitsPerPixel, numPixels, offset, scale, dest, maxValue) {
        var bitMask = (1 << bitsPerPixel) - 1;
        var i = 0, o;
        var bitsLeft = 0;
        var n, buffer;
        var nmax = Math.ceil((maxValue - offset) / scale);
        // get rid of trailing bytes that are already part of next block
        var numInvalidTailBytes = src.length * 4 - Math.ceil(bitsPerPixel * numPixels / 8);
        src[src.length - 1] <<= 8 * numInvalidTailBytes;
 
        for (o = 0; o < numPixels; o++) {
          if (bitsLeft === 0) {
            buffer = src[i++];
            bitsLeft = 32;
          }
          if (bitsLeft >= bitsPerPixel) {
            n = (buffer >>> (bitsLeft - bitsPerPixel)) & bitMask;
            bitsLeft -= bitsPerPixel;
          } else {
            var missingBits = (bitsPerPixel - bitsLeft);
            n = ((buffer & bitMask) << missingBits) & bitMask;
            buffer = src[i++];
            bitsLeft = 32 - missingBits;
            n += (buffer >>> bitsLeft);
          }
          //pixel values may exceed max due to quantization
          dest[o] = n < nmax ? offset + n * scale : maxValue;
        }
        return dest;
      };
 
      return CntZImage;
    })();
 
    //version 2. Supports 2.1, 2.2, 2.3
    var Lerc2Decode = (function() {
      // Note: currently, this module only has an implementation for decoding LERC data, not encoding. The name of
      // the class was chosen to be future proof, following LercDecode.
 
      /*****************************************
      * private static class bitsutffer used by Lerc2Decode
      *******************************************/
      var BitStuffer = {
        //methods ending with 2 are for the new byte order used by Lerc2.3 and above.
        //originalUnstuff is used to unpack Huffman code table. code is duplicated to unstuffx for performance reasons.
        unstuff: function(src, dest, bitsPerPixel, numPixels, lutArr, offset, scale, maxValue) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o;
          var bitsLeft = 0;
          var n, buffer, missingBits, nmax;
 
          // get rid of trailing bytes that are already part of next block
          var numInvalidTailBytes = src.length * 4 - Math.ceil(bitsPerPixel * numPixels / 8);
          src[src.length - 1] <<= 8 * numInvalidTailBytes;
          if (lutArr) {
            for (o = 0; o < numPixels; o++) {
              if (bitsLeft === 0) {
                buffer = src[i++];
                bitsLeft = 32;
              }
              if (bitsLeft >= bitsPerPixel) {
                n = (buffer >>> (bitsLeft - bitsPerPixel)) & bitMask;
                bitsLeft -= bitsPerPixel;
              }
              else {
                missingBits = (bitsPerPixel - bitsLeft);
                n = ((buffer & bitMask) << missingBits) & bitMask;
                buffer = src[i++];
                bitsLeft = 32 - missingBits;
                n += (buffer >>> bitsLeft);
              }
              dest[o] = lutArr[n];//offset + lutArr[n] * scale;
            }
          }
          else {
            nmax = Math.ceil((maxValue - offset) / scale);
            for (o = 0; o < numPixels; o++) {
              if (bitsLeft === 0) {
                buffer = src[i++];
                bitsLeft = 32;
              }
              if (bitsLeft >= bitsPerPixel) {
                n = (buffer >>> (bitsLeft - bitsPerPixel)) & bitMask;
                bitsLeft -= bitsPerPixel;
              }
              else {
                missingBits = (bitsPerPixel - bitsLeft);
                n = ((buffer & bitMask) << missingBits) & bitMask;
                buffer = src[i++];
                bitsLeft = 32 - missingBits;
                n += (buffer >>> bitsLeft);
              }
              //pixel values may exceed max due to quantization
              dest[o] = n < nmax ? offset + n * scale : maxValue;
            }
          }
        },
 
        unstuffLUT: function(src, bitsPerPixel, numPixels, offset, scale, maxValue) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o = 0, missingBits = 0, bitsLeft = 0, n = 0;
          var buffer;
          var dest = [];
 
          // get rid of trailing bytes that are already part of next block
          var numInvalidTailBytes = src.length * 4 - Math.ceil(bitsPerPixel * numPixels / 8);
          src[src.length - 1] <<= 8 * numInvalidTailBytes;
 
          var nmax = Math.ceil((maxValue - offset) / scale);
          for (o = 0; o < numPixels; o++) {
            if (bitsLeft === 0) {
              buffer = src[i++];
              bitsLeft = 32;
            }
            if (bitsLeft >= bitsPerPixel) {
              n = (buffer >>> (bitsLeft - bitsPerPixel)) & bitMask;
              bitsLeft -= bitsPerPixel;
            } else {
              missingBits = (bitsPerPixel - bitsLeft);
              n = ((buffer & bitMask) << missingBits) & bitMask;
              buffer = src[i++];
              bitsLeft = 32 - missingBits;
              n += (buffer >>> bitsLeft);
            }
            //dest.push(n);
            dest[o] = n < nmax ? offset + n * scale : maxValue;
          }
          dest.unshift(offset);//1st one
          return dest;
        },
 
        unstuff2: function(src, dest, bitsPerPixel, numPixels, lutArr, offset, scale, maxValue) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o;
          var bitsLeft = 0, bitPos = 0;
          var n, buffer, missingBits;
          if (lutArr) {
            for (o = 0; o < numPixels; o++) {
              if (bitsLeft === 0) {
                buffer = src[i++];
                bitsLeft = 32;
                bitPos = 0;
              }
              if (bitsLeft >= bitsPerPixel) {
                n = ((buffer >>> bitPos) & bitMask);
                bitsLeft -= bitsPerPixel;
                bitPos += bitsPerPixel;
              } else {
                missingBits = (bitsPerPixel - bitsLeft);
                n = (buffer >>> bitPos) & bitMask;
                buffer = src[i++];
                bitsLeft = 32 - missingBits;
                n |= (buffer & ((1 << missingBits) - 1)) << (bitsPerPixel - missingBits);
                bitPos = missingBits;
              }
              dest[o] = lutArr[n];
            }
          }
          else {
            var nmax = Math.ceil((maxValue - offset) / scale);
            for (o = 0; o < numPixels; o++) {
              if (bitsLeft === 0) {
                buffer = src[i++];
                bitsLeft = 32;
                bitPos = 0;
              }
              if (bitsLeft >= bitsPerPixel) {
                //no unsigned left shift
                n = ((buffer >>> bitPos) & bitMask);
                bitsLeft -= bitsPerPixel;
                bitPos += bitsPerPixel;
              } else {
                missingBits = (bitsPerPixel - bitsLeft);
                n = (buffer >>> bitPos) & bitMask;//((buffer & bitMask) << missingBits) & bitMask;
                buffer = src[i++];
                bitsLeft = 32 - missingBits;
                n |= (buffer & ((1 << missingBits) - 1)) << (bitsPerPixel - missingBits);
                bitPos = missingBits;
              }
              //pixel values may exceed max due to quantization
              dest[o] = n < nmax ? offset + n * scale : maxValue;
            }
          }
          return dest;
        },
 
        unstuffLUT2: function(src, bitsPerPixel, numPixels, offset, scale, maxValue) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o = 0, missingBits = 0, bitsLeft = 0, n = 0, bitPos = 0;
          var buffer;
          var dest = [];
          var nmax = Math.ceil((maxValue - offset) / scale);
          for (o = 0; o < numPixels; o++) {
            if (bitsLeft === 0) {
              buffer = src[i++];
              bitsLeft = 32;
              bitPos = 0;
            }
            if (bitsLeft >= bitsPerPixel) {
              //no unsigned left shift
              n = ((buffer >>> bitPos) & bitMask);
              bitsLeft -= bitsPerPixel;
              bitPos += bitsPerPixel;
            } else {
              missingBits = (bitsPerPixel - bitsLeft);
              n = (buffer >>> bitPos) & bitMask;//((buffer & bitMask) << missingBits) & bitMask;
              buffer = src[i++];
              bitsLeft = 32 - missingBits;
              n |= (buffer & ((1 << missingBits) - 1)) << (bitsPerPixel - missingBits);
              bitPos = missingBits;
            }
            //dest.push(n);
            dest[o] = n < nmax ? offset + n * scale : maxValue;
          }
          dest.unshift(offset);
          return dest;
        },
 
        originalUnstuff: function(src, dest, bitsPerPixel, numPixels) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o;
          var bitsLeft = 0;
          var n, buffer, missingBits;
 
          // get rid of trailing bytes that are already part of next block
          var numInvalidTailBytes = src.length * 4 - Math.ceil(bitsPerPixel * numPixels / 8);
          src[src.length - 1] <<= 8 * numInvalidTailBytes;
 
          for (o = 0; o < numPixels; o++) {
            if (bitsLeft === 0) {
              buffer = src[i++];
              bitsLeft = 32;
            }
            if (bitsLeft >= bitsPerPixel) {
              n = (buffer >>> (bitsLeft - bitsPerPixel)) & bitMask;
              bitsLeft -= bitsPerPixel;
            }
            else {
              missingBits = (bitsPerPixel - bitsLeft);
              n = ((buffer & bitMask) << missingBits) & bitMask;
              buffer = src[i++];
              bitsLeft = 32 - missingBits;
              n += (buffer >>> bitsLeft);
            }
            dest[o] = n;
          }
          return dest;
        },
 
        originalUnstuff2: function(src, dest, bitsPerPixel, numPixels) {
          var bitMask = (1 << bitsPerPixel) - 1;
          var i = 0, o;
          var bitsLeft = 0, bitPos = 0;
          var n, buffer, missingBits;
          //micro-optimizations
          for (o = 0; o < numPixels; o++) {
            if (bitsLeft === 0) {
              buffer = src[i++];
              bitsLeft = 32;
              bitPos = 0;
            }
            if (bitsLeft >= bitsPerPixel) {
              //no unsigned left shift
              n = ((buffer >>> bitPos) & bitMask);
              bitsLeft -= bitsPerPixel;
              bitPos += bitsPerPixel;
            } else {
              missingBits = (bitsPerPixel - bitsLeft);
              n = (buffer >>> bitPos) & bitMask;//((buffer & bitMask) << missingBits) & bitMask;
              buffer = src[i++];
              bitsLeft = 32 - missingBits;
              n |= (buffer & ((1 << missingBits) - 1)) << (bitsPerPixel - missingBits);
              bitPos = missingBits;
            }
            dest[o] = n;
          }
          return dest;
        }
      };
 
      /*****************************************
      *private static class used by Lerc2Decode
      ******************************************/
      var Lerc2Helpers = {
        HUFFMAN_LUT_BITS_MAX: 12, //use 2^12 lut, treat it like constant
        computeChecksumFletcher32: function(input) {
 
          var sum1 = 0xffff, sum2 = 0xffff;
          var len = input.length;
          var words = Math.floor(len / 2);
          var i = 0;
          while (words) {
            var tlen = (words >= 359) ? 359 : words;
            words -= tlen;
            do {
              sum1 += (input[i++] << 8);
              sum2 += sum1 += input[i++];
            } while (--tlen);
 
            sum1 = (sum1 & 0xffff) + (sum1 >>> 16);
            sum2 = (sum2 & 0xffff) + (sum2 >>> 16);
          }
 
          // add the straggler byte if it exists
          if (len & 1) {
            sum2 += sum1 += (input[i] << 8);
          }
          // second reduction step to reduce sums to 16 bits
          sum1 = (sum1 & 0xffff) + (sum1 >>> 16);
          sum2 = (sum2 & 0xffff) + (sum2 >>> 16);
 
          return (sum2 << 16 | sum1) >>> 0;
        },
 
        readHeaderInfo: function(input, data) {
          var ptr = data.ptr;
          var fileIdView = new Uint8Array(input, ptr, 6);
          var headerInfo = {};
          headerInfo.fileIdentifierString = String.fromCharCode.apply(null, fileIdView);
          if (headerInfo.fileIdentifierString.lastIndexOf("Lerc2", 0) !== 0) {
            throw "Unexpected file identifier string (expect Lerc2 ): " + headerInfo.fileIdentifierString;
          }
          ptr += 6;
          var view = new DataView(input, ptr, 8);
          var fileVersion = view.getInt32(0, true);
          headerInfo.fileVersion = fileVersion;
          ptr += 4;
          if (fileVersion >= 3) {
            headerInfo.checksum = view.getUint32(4, true); //nrows
            ptr += 4;
          }
 
          //keys start from here
          view = new DataView(input, ptr, 12);
          headerInfo.height = view.getUint32(0, true); //nrows
          headerInfo.width = view.getUint32(4, true); //ncols
          ptr += 8;
          if (fileVersion >= 4) {
            headerInfo.numDims = view.getUint32(8, true);
            ptr += 4;
          }
          else {
            headerInfo.numDims = 1;
          }
 
          view = new DataView(input, ptr, 40);
          headerInfo.numValidPixel = view.getUint32(0, true);
          headerInfo.microBlockSize = view.getInt32(4, true);
          headerInfo.blobSize = view.getInt32(8, true);
          headerInfo.imageType = view.getInt32(12, true);
 
          headerInfo.maxZError = view.getFloat64(16, true);
          headerInfo.zMin = view.getFloat64(24, true);
          headerInfo.zMax = view.getFloat64(32, true);
          ptr += 40;
          data.headerInfo = headerInfo;
          data.ptr = ptr;
 
          var checksum, keyLength;
          if (fileVersion >= 3) {
            keyLength = fileVersion >= 4 ? 52 : 48;
            checksum = this.computeChecksumFletcher32(new Uint8Array(input, ptr - keyLength, headerInfo.blobSize - 14));
            if (checksum !== headerInfo.checksum) {
              throw "Checksum failed.";
            }
          }
          return true;
        },
 
        checkMinMaxRanges: function(input, data) {
          var headerInfo = data.headerInfo;
          var OutPixelTypeArray = this.getDataTypeArray(headerInfo.imageType);
          var rangeBytes = headerInfo.numDims * this.getDataTypeSize(headerInfo.imageType);
          var minValues = this.readSubArray(input, data.ptr, OutPixelTypeArray, rangeBytes);
          var maxValues = this.readSubArray(input, data.ptr + rangeBytes, OutPixelTypeArray, rangeBytes);
          data.ptr += (2 * rangeBytes);
          var i, equal = true;
          for (i = 0; i < headerInfo.numDims; i++) {
            if (minValues[i] !== maxValues[i]) {
              equal = false;
              break;
            }
          }
          headerInfo.minValues = minValues;
          headerInfo.maxValues = maxValues;
          return equal;
        },
 
        readSubArray: function(input, ptr, OutPixelTypeArray, numBytes) {
          var rawData;
          if (OutPixelTypeArray === Uint8Array) {
            rawData = new Uint8Array(input, ptr, numBytes);
          }
          else {
            var arrayBuf = new ArrayBuffer(numBytes);
            var store8 = new Uint8Array(arrayBuf);
            store8.set(new Uint8Array(input, ptr, numBytes));
            rawData = new OutPixelTypeArray(arrayBuf);
          }
          return rawData;
        },
 
        readMask: function(input, data) {
          var ptr = data.ptr;
          var headerInfo = data.headerInfo;
          var numPixels = headerInfo.width * headerInfo.height;
          var numValidPixel = headerInfo.numValidPixel;
 
          var view = new DataView(input, ptr, 4);
          var mask = {};
          mask.numBytes = view.getUint32(0, true);
          ptr += 4;
 
          // Mask Data
          if ((0 === numValidPixel || numPixels === numValidPixel) && 0 !== mask.numBytes) {
            throw ("invalid mask");
          }
          var bitset, resultMask;
          if (numValidPixel === 0) {
            bitset = new Uint8Array(Math.ceil(numPixels / 8));
            mask.bitset = bitset;
            resultMask = new Uint8Array(numPixels);
            data.pixels.resultMask = resultMask;
            ptr += mask.numBytes;
          }// ????? else if (data.mask.numBytes > 0 && data.mask.numBytes< data.numValidPixel) {
          else if (mask.numBytes > 0) {
            bitset = new Uint8Array(Math.ceil(numPixels / 8));
            view = new DataView(input, ptr, mask.numBytes);
            var cnt = view.getInt16(0, true);
            var ip = 2, op = 0, val = 0;
            do {
              if (cnt > 0) {
                while (cnt--) { bitset[op++] = view.getUint8(ip++); }
              } else {
                val = view.getUint8(ip++);
                cnt = -cnt;
                while (cnt--) { bitset[op++] = val; }
              }
              cnt = view.getInt16(ip, true);
              ip += 2;
            } while (ip < mask.numBytes);
            if ((cnt !== -32768) || (op < bitset.length)) {
              throw "Unexpected end of mask RLE encoding";
            }
 
            resultMask = new Uint8Array(numPixels);
            var mb = 0, k = 0;
 
            for (k = 0; k < numPixels; k++) {
              if (k & 7) {
                mb = bitset[k >> 3];
                mb <<= k & 7;
              }
              else {
                mb = bitset[k >> 3];
              }
              if (mb & 128) {
                resultMask[k] = 1;
              }
            }
            data.pixels.resultMask = resultMask;
 
            mask.bitset = bitset;
            ptr += mask.numBytes;
          }
          data.ptr = ptr;
          data.mask = mask;
          return true;
        },
 
        readDataOneSweep: function(input, data, OutPixelTypeArray) {
          var ptr = data.ptr;
          var headerInfo = data.headerInfo;
          var numDims = headerInfo.numDims;
          var numPixels = headerInfo.width * headerInfo.height;
          var imageType = headerInfo.imageType;
          var numBytes = headerInfo.numValidPixel * Lerc2Helpers.getDataTypeSize(imageType) * numDims;
          //data.pixels.numBytes = numBytes;
          var rawData;
          var mask = data.pixels.resultMask;
          if (OutPixelTypeArray === Uint8Array) {
            rawData = new Uint8Array(input, ptr, numBytes);
          }
          else {
            var arrayBuf = new ArrayBuffer(numBytes);
            var store8 = new Uint8Array(arrayBuf);
            store8.set(new Uint8Array(input, ptr, numBytes));
            rawData = new OutPixelTypeArray(arrayBuf);
          }
          if (rawData.length === numPixels * numDims) {
            data.pixels.resultPixels = rawData;
          }
          else  //mask
          {
            data.pixels.resultPixels = new OutPixelTypeArray(numPixels * numDims);
            var z = 0, k = 0, i = 0, nStart = 0;
            if (numDims > 1) {
              for (i=0; i < numDims; i++) {
                nStart = i * numPixels;
                for (k = 0; k < numPixels; k++) {
                  if (mask[k]) {
                    data.pixels.resultPixels[nStart + k] = rawData[z++];
                  }
                }
              }
            }
            else {
              for (k = 0; k < numPixels; k++) {
                if (mask[k]) {
                  data.pixels.resultPixels[k] = rawData[z++];
                }
              }
            }
          }
          ptr += numBytes;
          data.ptr = ptr;       //return data;
          return true;
        },
 
        readHuffmanTree: function(input, data) {
          var BITS_MAX = this.HUFFMAN_LUT_BITS_MAX; //8 is slow for the large test image
          //var size_max = 1 << BITS_MAX;
          /* ************************
           * reading code table
           *************************/
          var view = new DataView(input, data.ptr, 16);
          data.ptr += 16;
          var version = view.getInt32(0, true);
          if (version < 2) {
            throw "unsupported Huffman version";
          }
          var size = view.getInt32(4, true);
          var i0 = view.getInt32(8, true);
          var i1 = view.getInt32(12, true);
          if (i0 >= i1) {
            return false;
          }
          var blockDataBuffer = new Uint32Array(i1 - i0);
          Lerc2Helpers.decodeBits(input, data, blockDataBuffer);
          var codeTable = []; //size
          var i, j, k, len;
 
          for (i = i0; i < i1; i++) {
            j = i - (i < size ? 0 : size);//wrap around
            codeTable[j] = { first: blockDataBuffer[i - i0], second: null };
          }
 
          var dataBytes = input.byteLength - data.ptr;
          var dataWords = Math.ceil(dataBytes / 4);
          var arrayBuf = new ArrayBuffer(dataWords * 4);
          var store8 = new Uint8Array(arrayBuf);
          store8.set(new Uint8Array(input, data.ptr, dataBytes));
          var stuffedData = new Uint32Array(arrayBuf); //must start from x*4
          var bitPos = 0, word, srcPtr = 0;
          word = stuffedData[0];
          for (i = i0; i < i1; i++) {
            j = i - (i < size ? 0 : size);//wrap around
            len = codeTable[j].first;
            if (len > 0) {
              codeTable[j].second = (word << bitPos) >>> (32 - len);
 
              if (32 - bitPos >= len) {
                bitPos += len;
                if (bitPos === 32) {
                  bitPos = 0;
                  srcPtr++;
                  word = stuffedData[srcPtr];
                }
              }
              else {
                bitPos += len - 32;
                srcPtr++;
                word = stuffedData[srcPtr];
                codeTable[j].second |= word >>> (32 - bitPos);
              }
            }
          }
 
          //finished reading code table
 
          /* ************************
           * building lut
           *************************/
          var numBitsLUT = 0, numBitsLUTQick = 0;
          var tree = new TreeNode();
          for (i = 0; i < codeTable.length; i++) {
            if (codeTable[i] !== undefined) {
              numBitsLUT = Math.max(numBitsLUT, codeTable[i].first);
            }
          }
          if (numBitsLUT >= BITS_MAX) {
            numBitsLUTQick = BITS_MAX;
          }
          else {
            numBitsLUTQick = numBitsLUT;
          }
          if (numBitsLUT >= 30) {
            console.log("WARning, large NUM LUT BITS IS " + numBitsLUT);
          }
          var decodeLut = [], entry, code, numEntries, jj, currentBit, node;
          for (i = i0; i < i1; i++) {
            j = i - (i < size ? 0 : size);//wrap around
            len = codeTable[j].first;
            if (len > 0) {
              entry = [len, j];
              if (len <= numBitsLUTQick) {
                code = codeTable[j].second << (numBitsLUTQick - len);
                numEntries = 1 << (numBitsLUTQick - len);
                for (k = 0; k < numEntries; k++) {
                  decodeLut[code | k] = entry;
                }
              }
              else {
                //build tree
                code = codeTable[j].second;
                node = tree;
                for (jj = len - 1; jj >= 0; jj--) {
                  currentBit = code >>> jj & 1; //no left shift as length could be 30,31
                  if (currentBit) {
                    if (!node.right) {
                      node.right = new TreeNode();
                    }
                    node = node.right;
                  }
                  else {
                    if (!node.left) {
                      node.left = new TreeNode();
                    }
                    node = node.left;
                  }
                  if (jj === 0 && !node.val) {
                    node.val = entry[1];
                  }
                }
              }
            }
          }
          return {
            decodeLut: decodeLut,
            numBitsLUTQick: numBitsLUTQick,
            numBitsLUT: numBitsLUT,
            tree: tree,
            stuffedData: stuffedData,
            srcPtr: srcPtr,
            bitPos: bitPos
          };
        },
 
        readHuffman: function(input, data, OutPixelTypeArray) {
          var headerInfo = data.headerInfo;
          var numDims = headerInfo.numDims;
          var height = data.headerInfo.height;
          var width = data.headerInfo.width;
          var numPixels = width * height;
          //var size_max = 1 << BITS_MAX;
          /* ************************
           * reading huffman structure info
           *************************/
          var huffmanInfo = this.readHuffmanTree(input, data);
          var decodeLut = huffmanInfo.decodeLut;
          var tree = huffmanInfo.tree;
          //stuffedData includes huffman headers
          var stuffedData = huffmanInfo.stuffedData;
          var srcPtr = huffmanInfo.srcPtr;
          var bitPos = huffmanInfo.bitPos;
          var numBitsLUTQick = huffmanInfo.numBitsLUTQick;
          var numBitsLUT = huffmanInfo.numBitsLUT;
          var offset = data.headerInfo.imageType === 0 ? 128 : 0;
          /*************************
          *  decode
          ***************************/
          var node, val, delta, mask = data.pixels.resultMask, valTmp, valTmpQuick, currentBit;
          var i, j, k, ii;
          var prevVal = 0;
          if (bitPos > 0) {
            srcPtr++;
            bitPos = 0;
          }
          var word = stuffedData[srcPtr];
          var deltaEncode = data.encodeMode === 1;
          var resultPixelsAllDim = new OutPixelTypeArray(numPixels * numDims);
          var resultPixels = resultPixelsAllDim;
          var iDim;
          for (iDim = 0; iDim < headerInfo.numDims; iDim++) {
            if (numDims > 1) {
              //get the mem block of current dimension
              resultPixels = new OutPixelTypeArray(resultPixelsAllDim.buffer, numPixels * iDim, numPixels);
              prevVal = 0;
            }
            if (data.headerInfo.numValidPixel === width * height) { //all valid
              for (k = 0, i = 0; i < height; i++) {
                for (j = 0; j < width; j++, k++) {
                  val = 0;
                  valTmp = (word << bitPos) >>> (32 - numBitsLUTQick);
                  valTmpQuick = valTmp;// >>> deltaBits;
                  if (32 - bitPos < numBitsLUTQick) {
                    valTmp |= ((stuffedData[srcPtr + 1]) >>> (64 - bitPos - numBitsLUTQick));
                    valTmpQuick = valTmp;// >>> deltaBits;
                  }
                  if (decodeLut[valTmpQuick])    // if there, move the correct number of bits and done
                  {
                    val = decodeLut[valTmpQuick][1];
                    bitPos += decodeLut[valTmpQuick][0];
                  }
                  else {
                    valTmp = (word << bitPos) >>> (32 - numBitsLUT);
                    valTmpQuick = valTmp;// >>> deltaBits;
                    if (32 - bitPos < numBitsLUT) {
                      valTmp |= ((stuffedData[srcPtr + 1]) >>> (64 - bitPos - numBitsLUT));
                      valTmpQuick = valTmp;// >>> deltaBits;
                    }
                    node = tree;
                    for (ii = 0; ii < numBitsLUT; ii++) {
                      currentBit = valTmp >>> (numBitsLUT - ii - 1) & 1;
                      node = currentBit ? node.right : node.left;
                      if (!(node.left || node.right)) {
                        val = node.val;
                        bitPos = bitPos + ii + 1;
                        break;
                      }
                    }
                  }
 
                  if (bitPos >= 32) {
                    bitPos -= 32;
                    srcPtr++;
                    word = stuffedData[srcPtr];
                  }
 
                  delta = val - offset;
                  if (deltaEncode) {
                    if (j > 0) {
                      delta += prevVal;    // use overflow
                    }
                    else if (i > 0) {
                      delta += resultPixels[k - width];
                    }
                    else {
                      delta += prevVal;
                    }
                    delta &= 0xFF; //overflow
                    resultPixels[k] = delta;//overflow
                    prevVal = delta;
                  }
                  else {
                    resultPixels[k] = delta;
                  }
                }
              }
            }
            else { //not all valid, use mask
              for (k = 0, i = 0; i < height; i++) {
                for (j = 0; j < width; j++, k++) {
                  if (mask[k]) {
                    val = 0;
                    valTmp = (word << bitPos) >>> (32 - numBitsLUTQick);
                    valTmpQuick = valTmp;// >>> deltaBits;
                    if (32 - bitPos < numBitsLUTQick) {
                      valTmp |= ((stuffedData[srcPtr + 1]) >>> (64 - bitPos - numBitsLUTQick));
                      valTmpQuick = valTmp;// >>> deltaBits;
                    }
                    if (decodeLut[valTmpQuick])    // if there, move the correct number of bits and done
                    {
                      val = decodeLut[valTmpQuick][1];
                      bitPos += decodeLut[valTmpQuick][0];
                    }
                    else {
                      valTmp = (word << bitPos) >>> (32 - numBitsLUT);
                      valTmpQuick = valTmp;// >>> deltaBits;
                      if (32 - bitPos < numBitsLUT) {
                        valTmp |= ((stuffedData[srcPtr + 1]) >>> (64 - bitPos - numBitsLUT));
                        valTmpQuick = valTmp;// >>> deltaBits;
                      }
                      node = tree;
                      for (ii = 0; ii < numBitsLUT; ii++) {
                        currentBit = valTmp >>> (numBitsLUT - ii - 1) & 1;
                        node = currentBit ? node.right : node.left;
                        if (!(node.left || node.right)) {
                          val = node.val;
                          bitPos = bitPos + ii + 1;
                          break;
                        }
                      }
                    }
 
                    if (bitPos >= 32) {
                      bitPos -= 32;
                      srcPtr++;
                      word = stuffedData[srcPtr];
                    }
 
                    delta = val - offset;
                    if (deltaEncode) {
                      if (j > 0 && mask[k - 1]) {
                        delta += prevVal;    // use overflow
                      }
                      else if (i > 0 && mask[k - width]) {
                        delta += resultPixels[k - width];
                      }
                      else {
                        delta += prevVal;
                      }
 
                      delta &= 0xFF; //overflow
                      resultPixels[k] = delta;//overflow
                      prevVal = delta;
                    }
                    else {
                      resultPixels[k] = delta;
                    }
                  }
                }
              }
            }
            data.ptr = data.ptr + (srcPtr + 1) * 4 + (bitPos > 0 ? 4 : 0);
          }
          data.pixels.resultPixels = resultPixelsAllDim;
        },
 
        decodeBits: function(input, data, blockDataBuffer, offset, iDim) {
          {
            //bitstuff encoding is 3
            var headerInfo = data.headerInfo;
            var fileVersion = headerInfo.fileVersion;
            //var block = {};
            var blockPtr = 0;
            var view = new DataView(input, data.ptr, 5);//to do
            var headerByte = view.getUint8(0);
            blockPtr++;
            var bits67 = headerByte >> 6;
            var n = (bits67 === 0) ? 4 : 3 - bits67;
            var doLut = (headerByte & 32) > 0 ? true : false;//5th bit
            var numBits = headerByte & 31;
            var numElements = 0;
            if (n === 1) {
              numElements = view.getUint8(blockPtr); blockPtr++;
            } else if (n === 2) {
              numElements = view.getUint16(blockPtr, true); blockPtr += 2;
            } else if (n === 4) {
              numElements = view.getUint32(blockPtr, true); blockPtr += 4;
            } else {
              throw "Invalid valid pixel count type";
            }
            //fix: huffman codes are bit stuffed, but not bound by data's max value, so need to use originalUnstuff
            //offset = offset || 0;
            var scale = 2 * headerInfo.maxZError;
            var stuffedData, arrayBuf, store8, dataBytes, dataWords;
            var lutArr, lutData, lutBytes, bitsPerPixel;
            var zMax = headerInfo.numDims > 1 ? headerInfo.maxValues[iDim] : headerInfo.zMax;
            if (doLut) {
              data.counter.lut++;
              lutBytes = view.getUint8(blockPtr);
              blockPtr++;
              dataBytes = Math.ceil((lutBytes - 1) * numBits / 8);
              dataWords = Math.ceil(dataBytes / 4);
              arrayBuf = new ArrayBuffer(dataWords * 4);
              store8 = new Uint8Array(arrayBuf);
 
              data.ptr += blockPtr;
              store8.set(new Uint8Array(input, data.ptr, dataBytes));
 
              lutData = new Uint32Array(arrayBuf);
              data.ptr += dataBytes;
 
              bitsPerPixel = 0;
              while ((lutBytes - 1) >>> bitsPerPixel) {
                bitsPerPixel++;
              }
              dataBytes = Math.ceil(numElements * bitsPerPixel / 8);
              dataWords = Math.ceil(dataBytes / 4);
              arrayBuf = new ArrayBuffer(dataWords * 4);
              store8 = new Uint8Array(arrayBuf);
              store8.set(new Uint8Array(input, data.ptr, dataBytes));
              stuffedData = new Uint32Array(arrayBuf);
              data.ptr += dataBytes;
              if (fileVersion >= 3) {
                lutArr = BitStuffer.unstuffLUT2(lutData, numBits, lutBytes - 1, offset, scale, zMax);
              }
              else {
                lutArr = BitStuffer.unstuffLUT(lutData, numBits, lutBytes - 1, offset, scale, zMax);
              }
              //lutArr.unshift(0);
              if (fileVersion >= 3) {
                //BitStuffer.unstuff2(block, blockDataBuffer, headerInfo.zMax);
                BitStuffer.unstuff2(stuffedData, blockDataBuffer, bitsPerPixel, numElements, lutArr);
              }
              else {
                BitStuffer.unstuff(stuffedData, blockDataBuffer, bitsPerPixel, numElements, lutArr);
              }
            }
            else {
              //console.debug("bitstuffer");
              data.counter.bitstuffer++;
              bitsPerPixel = numBits;
              data.ptr += blockPtr;
              if (bitsPerPixel > 0) {
                dataBytes = Math.ceil(numElements * bitsPerPixel / 8);
                dataWords = Math.ceil(dataBytes / 4);
                arrayBuf = new ArrayBuffer(dataWords * 4);
                store8 = new Uint8Array(arrayBuf);
                store8.set(new Uint8Array(input, data.ptr, dataBytes));
                stuffedData = new Uint32Array(arrayBuf);
                data.ptr += dataBytes;
                if (fileVersion >= 3) {
                  if (offset == null) {
                    BitStuffer.originalUnstuff2(stuffedData, blockDataBuffer, bitsPerPixel, numElements);
                  }
                  else {
                    BitStuffer.unstuff2(stuffedData, blockDataBuffer, bitsPerPixel, numElements, false, offset, scale, zMax);
                  }
                }
                else {
                  if (offset == null) {
                    BitStuffer.originalUnstuff(stuffedData, blockDataBuffer, bitsPerPixel, numElements);
                  }
                  else {
                    BitStuffer.unstuff(stuffedData, blockDataBuffer, bitsPerPixel, numElements, false, offset, scale, zMax);
                  }
                }
              }
            }
          }
 
        },
 
        readTiles: function(input, data, OutPixelTypeArray) {
          var headerInfo = data.headerInfo;
          var width = headerInfo.width;
          var height = headerInfo.height;
          var microBlockSize = headerInfo.microBlockSize;
          var imageType = headerInfo.imageType;
          var dataTypeSize = Lerc2Helpers.getDataTypeSize(imageType);
          var numBlocksX = Math.ceil(width / microBlockSize);
          var numBlocksY = Math.ceil(height / microBlockSize);
          data.pixels.numBlocksY = numBlocksY;
          data.pixels.numBlocksX = numBlocksX;
          data.pixels.ptr = 0;
          var row = 0, col = 0, blockY = 0, blockX = 0, thisBlockHeight = 0, thisBlockWidth = 0, bytesLeft = 0, headerByte = 0, bits67 = 0, testCode = 0, outPtr = 0, outStride = 0, numBytes = 0, bytesleft = 0, z = 0, blockPtr = 0;
          var view, block, arrayBuf, store8, rawData;
          var blockEncoding;
          var blockDataBuffer = new OutPixelTypeArray(microBlockSize * microBlockSize);
          var lastBlockHeight = (height % microBlockSize) || microBlockSize;
          var lastBlockWidth = (width % microBlockSize) || microBlockSize;
          var offsetType, offset;
          var numDims = headerInfo.numDims, iDim;
          var mask = data.pixels.resultMask;
          var resultPixels = data.pixels.resultPixels;
          for (blockY = 0; blockY < numBlocksY; blockY++) {
            thisBlockHeight = (blockY !== numBlocksY - 1) ? microBlockSize : lastBlockHeight;
            for (blockX = 0; blockX < numBlocksX; blockX++) {
              //console.debug("y" + blockY + " x" + blockX);
              thisBlockWidth = (blockX !== numBlocksX - 1) ? microBlockSize : lastBlockWidth;
 
              outPtr = blockY * width * microBlockSize + blockX * microBlockSize;
              outStride = width - thisBlockWidth;
 
 
              for (iDim = 0; iDim < numDims; iDim++) {
                if (numDims > 1) {
                  resultPixels = new OutPixelTypeArray(data.pixels.resultPixels.buffer, width * height * iDim * dataTypeSize, width * height);
                }
                bytesLeft = input.byteLength - data.ptr;
                view = new DataView(input, data.ptr, Math.min(10, bytesLeft));
                block = {};
                blockPtr = 0;
                headerByte = view.getUint8(0);
                blockPtr++;
                bits67 = (headerByte >> 6) & 0xFF;
                testCode = (headerByte >> 2) & 15;    // use bits 2345 for integrity check
                if (testCode !== (((blockX * microBlockSize) >> 3) & 15)) {
                  throw "integrity issue";
                  //return false;
                }
 
                blockEncoding = headerByte & 3;
                if (blockEncoding > 3) {
                  data.ptr += blockPtr;
                  throw "Invalid block encoding (" + blockEncoding + ")";
                }
                else if (blockEncoding === 2) { //constant 0
                  data.counter.constant++;
                  data.ptr += blockPtr;
                  continue;
                }
                else if (blockEncoding === 0) {  //uncompressed
                  data.counter.uncompressed++;
                  data.ptr += blockPtr;
                  numBytes = thisBlockHeight * thisBlockWidth * dataTypeSize;
                  bytesleft = input.byteLength - data.ptr;
                  numBytes = numBytes < bytesleft ? numBytes : bytesleft;
                  //bit alignment
                  arrayBuf = new ArrayBuffer((numBytes % dataTypeSize) === 0 ? numBytes : (numBytes + dataTypeSize - numBytes % dataTypeSize));
                  store8 = new Uint8Array(arrayBuf);
                  store8.set(new Uint8Array(input, data.ptr, numBytes));
                  rawData = new OutPixelTypeArray(arrayBuf);
                  z = 0;
                  if (mask) {
                    for (row = 0; row < thisBlockHeight; row++) {
                      for (col = 0; col < thisBlockWidth; col++) {
                        if (mask[outPtr]) {
                          resultPixels[outPtr] = rawData[z++];
                        }
                        outPtr++;
                      }
                      outPtr += outStride;
                    }
                  }
                  else {//all valid
                    for (row = 0; row < thisBlockHeight; row++) {
                      for (col = 0; col < thisBlockWidth; col++) {
                        resultPixels[outPtr++] = rawData[z++];
                      }
                      outPtr += outStride;
                    }
                  }
                  data.ptr += z * dataTypeSize;
                }
                else { //1 or 3
                  offsetType = Lerc2Helpers.getDataTypeUsed(imageType, bits67);
                  offset = Lerc2Helpers.getOnePixel(block, blockPtr, offsetType, view);
                  blockPtr += Lerc2Helpers.getDataTypeSize(offsetType);
                  if (blockEncoding === 3) //constant offset value
                  {
                    data.ptr += blockPtr;
                    data.counter.constantoffset++;
                    //you can delete the following resultMask case in favor of performance because val is constant and users use nodata mask, otherwise nodatavalue post processing handles it too.
                    //while the above statement is true, we're not doing it as we want to keep invalid pixel value at 0 rather than arbitrary values
                    if (mask) {
                      for (row = 0; row < thisBlockHeight; row++) {
                        for (col = 0; col < thisBlockWidth; col++) {
                          if (mask[outPtr]) {
                            resultPixels[outPtr] = offset;
                          }
                          outPtr++;
                        }
                        outPtr += outStride;
                      }
                    }
                    else {
                      for (row = 0; row < thisBlockHeight; row++) {
                        for (col = 0; col < thisBlockWidth; col++) {
                          resultPixels[outPtr++] = offset;
                        }
                        outPtr += outStride;
                      }
                    }
                  }
                  else { //bitstuff encoding is 3
                    data.ptr += blockPtr;
                    //heavy lifting
                    Lerc2Helpers.decodeBits(input, data, blockDataBuffer, offset, iDim);
                    blockPtr = 0;
                    if (mask) {
                      for (row = 0; row < thisBlockHeight; row++) {
                        for (col = 0; col < thisBlockWidth; col++) {
                          if (mask[outPtr]) {
                            resultPixels[outPtr] = blockDataBuffer[blockPtr++];
                          }
                          outPtr++;
                        }
                        outPtr += outStride;
                      }
                    }
                    else {
                      for (row = 0; row < thisBlockHeight; row++) {
                        for (col = 0; col < thisBlockWidth; col++) {
                          resultPixels[outPtr++] = blockDataBuffer[blockPtr++];
                        }
                        outPtr += outStride;
                      }
                    }
                  }
                }
              }
            }
          }
        },
 
        /*****************
        *  private methods (helper methods)
        *****************/
 
        formatFileInfo: function(data) {
          return {
            "fileIdentifierString": data.headerInfo.fileIdentifierString,
            "fileVersion": data.headerInfo.fileVersion,
            "imageType": data.headerInfo.imageType,
            "height": data.headerInfo.height,
            "width": data.headerInfo.width,
            "numValidPixel": data.headerInfo.numValidPixel,
            "microBlockSize": data.headerInfo.microBlockSize,
            "blobSize": data.headerInfo.blobSize,
            "maxZError": data.headerInfo.maxZError,
            "pixelType": Lerc2Helpers.getPixelType(data.headerInfo.imageType),
            "eofOffset": data.eofOffset,
            "mask": data.mask ? {
              "numBytes": data.mask.numBytes
            } : null,
            "pixels": {
              "numBlocksX": data.pixels.numBlocksX,
              "numBlocksY": data.pixels.numBlocksY,
              //"numBytes": data.pixels.numBytes,
              "maxValue": data.headerInfo.zMax,
              "minValue": data.headerInfo.zMin,
              "noDataValue": data.noDataValue
            }
          };
        },
 
        constructConstantSurface: function(data) {
          var val = data.headerInfo.zMax;
          var numDims =  data.headerInfo.numDims;
          var numPixels = data.headerInfo.height * data.headerInfo.width;
          var numPixelAllDims = numPixels * numDims;
          var i=0, k = 0, nStart=0;
          var mask = data.pixels.resultMask;
          if (mask) {
            if (numDims > 1) {
              for (i=0; i < numDims; i++) {
                nStart = i * numPixels;
                for (k = 0; k < numPixels; k++) {
                  if (mask[k]) {
                    data.pixels.resultPixels[nStart + k] = val;
                  }
                }
              }
            }
            else {
              for (k = 0; k < numPixels; k++) {
                if (mask[k]) {
                  data.pixels.resultPixels[k] = val;
                }
              }
            }
          }
          else {
            if (data.pixels.resultPixels.fill) {
              data.pixels.resultPixels.fill(val);
            }
            else {
              for (k = 0; k < numPixelAllDims; k++) {
                data.pixels.resultPixels[k] = val;
              }
            }
          }
          return;
        },
 
        getDataTypeArray: function(t) {
          var tp;
          switch (t) {
            case 0: //char
              tp = Int8Array;
              break;
            case 1: //byte
              tp = Uint8Array;
              break;
            case 2: //short
              tp = Int16Array;
              break;
            case 3: //ushort
              tp = Uint16Array;
              break;
            case 4:
              tp = Int32Array;
              break;
            case 5:
              tp = Uint32Array;
              break;
            case 6:
              tp = Float32Array;
              break;
            case 7:
              tp = Float64Array;
              break;
            default:
              tp = Float32Array;
          }
          return tp;
        },
 
        getPixelType: function(t) {
          var tp;
          switch (t) {
            case 0: //char
              tp = "S8";
              break;
            case 1: //byte
              tp = "U8";
              break;
            case 2: //short
              tp = "S16";
              break;
            case 3: //ushort
              tp = "U16";
              break;
            case 4:
              tp = "S32";
              break;
            case 5:
              tp = "U32";
              break;
            case 6:
              tp = "F32";
              break;
            case 7:
              tp = "F64"; //not supported
              break;
            default:
              tp = "F32";
          }
          return tp;
        },
 
        isValidPixelValue: function(t, val) {
          if (val == null) {
            return false;
          }
          var isValid;
          switch (t) {
            case 0: //char
              isValid = val >= -128 && val <= 127;
              break;
            case 1: //byte  (unsigned char)
              isValid = val >= 0 && val <= 255;
              break;
            case 2: //short
              isValid = val >= -32768 && val <= 32767;
              break;
            case 3: //ushort
              isValid = val >= 0 && val <= 65536;
              break;
            case 4: //int 32
              isValid = val >= -2147483648 && val <= 2147483647;
              break;
            case 5: //uinit 32
              isValid = val >= 0 && val <= 4294967296;
              break;
            case 6:
              isValid = val >= -3.4027999387901484e+38 && val <= 3.4027999387901484e+38;
              break;
            case 7:
              isValid = val >= 5e-324 && val <= 1.7976931348623157e+308;
              break;
            default:
              isValid = false;
          }
          return isValid;
        },
 
        getDataTypeSize: function(t) {
          var s = 0;
          switch (t) {
            case 0: //ubyte
            case 1: //byte
              s = 1;
              break;
            case 2: //short
            case 3: //ushort
              s = 2;
              break;
            case 4:
            case 5:
            case 6:
              s = 4;
              break;
            case 7:
              s = 8;
              break;
            default:
              s = t;
          }
          return s;
        },
 
        getDataTypeUsed: function(dt, tc) {
          var t = dt;
          switch (dt) {
            case 2: //short
            case 4: //long
              t = dt - tc;
              break;
            case 3: //ushort
            case 5: //ulong
              t = dt - 2 * tc;
              break;
            case 6: //float
              if (0 === tc) {
                t = dt;
              }
              else if (1 === tc) {
                t = 2;
              }
              else {
                t = 1;//byte
              }
              break;
            case 7: //double
              if (0 === tc) {
                t = dt;
              }
              else {
                t = dt - 2 * tc + 1;
              }
              break;
            default:
              t = dt;
              break;
          }
          return t;
        },
 
        getOnePixel: function(block, blockPtr, offsetType, view) {
          var temp = 0;
          switch (offsetType) {
            case 0: //char
              temp = view.getInt8(blockPtr);
              break;
            case 1: //byte
              temp = view.getUint8(blockPtr);
              break;
            case 2:
              temp = view.getInt16(blockPtr, true);
              break;
            case 3:
              temp = view.getUint16(blockPtr, true);
              break;
            case 4:
              temp = view.getInt32(blockPtr, true);
              break;
            case 5:
              temp = view.getUInt32(blockPtr, true);
              break;
            case 6:
              temp = view.getFloat32(blockPtr, true);
              break;
            case 7:
              //temp = view.getFloat64(blockPtr, true);
              //blockPtr += 8;
              //lerc2 encoding doesnt handle float 64, force to float32???
              temp = view.getFloat64(blockPtr, true);
              break;
            default:
              throw ("the decoder does not understand this pixel type");
          }
          return temp;
        }
      };
 
      /***************************************************
      *private class for a tree node. Huffman code is in Lerc2Helpers
      ****************************************************/
      var TreeNode = function(val, left, right) {
        this.val = val;
        this.left = left;
        this.right = right;
      };
 
      var Lerc2Decode = {
        /*
        * ********removed options compared to LERC1. We can bring some of them back if needed.
         * removed pixel type. LERC2 is typed and doesn't require user to give pixel type
         * changed encodedMaskData to maskData. LERC2 's js version make it faster to use maskData directly.
         * removed returnMask. mask is used by LERC2 internally and is cost free. In case of user input mask, it's returned as well and has neglible cost.
         * removed nodatavalue. Because LERC2 pixels are typed, nodatavalue will sacrify a useful value for many types (8bit, 16bit) etc,
         *       user has to be knowledgable enough about raster and their data to avoid usability issues. so nodata value is simply removed now.
         *       We can add it back later if their's a clear requirement.
         * removed encodedMask. This option was not implemented in LercDecode. It can be done after decoding (less efficient)
         * removed computeUsedBitDepths.
         *
         *
         * response changes compared to LERC1
         * 1. encodedMaskData is not available
         * 2. noDataValue is optional (returns only if user's noDataValue is with in the valid data type range)
         * 3. maskData is always available
        */
        /*****************
        *  public properties
        ******************/
        //HUFFMAN_LUT_BITS_MAX: 12, //use 2^12 lut, not configurable
 
        /*****************
        *  public methods
        *****************/
 
        /**
         * Decode a LERC2 byte stream and return an object containing the pixel data and optional metadata.
         *
         * @param {ArrayBuffer} input The LERC input byte stream
         * @param {object} [options] options Decoding options
         * @param {number} [options.inputOffset] The number of bytes to skip in the input byte stream. A valid LERC file is expected at that position
         * @param {boolean} [options.returnFileInfo] If true, the return value will have a fileInfo property that contains metadata obtained from the LERC headers and the decoding process
         */
        decode: function(/*byte array*/ input, /*object*/ options) {
          //currently there's a bug in the sparse array, so please do not set to false
          options = options || {};
          var noDataValue = options.noDataValue;
 
          //initialize
          var i = 0, data = {};
          data.ptr = options.inputOffset || 0;
          data.pixels = {};
 
          // File header
          if (!Lerc2Helpers.readHeaderInfo(input, data)) ;
          var headerInfo = data.headerInfo;
          var fileVersion = headerInfo.fileVersion;
          var OutPixelTypeArray = Lerc2Helpers.getDataTypeArray(headerInfo.imageType);
 
          // Mask Header
          Lerc2Helpers.readMask(input, data);
          if (headerInfo.numValidPixel !== headerInfo.width * headerInfo.height && !data.pixels.resultMask) {
            data.pixels.resultMask = options.maskData;
          }
 
          var numPixels = headerInfo.width * headerInfo.height;
          data.pixels.resultPixels = new OutPixelTypeArray(numPixels * headerInfo.numDims);
 
          data.counter = {
            onesweep: 0,
            uncompressed: 0,
            lut: 0,
            bitstuffer: 0,
            constant: 0,
            constantoffset: 0
          };
          if (headerInfo.numValidPixel !== 0) {
            //not tested
            if (headerInfo.zMax === headerInfo.zMin) //constant surface
            {
              Lerc2Helpers.constructConstantSurface(data);
            }
            else if (fileVersion >= 4 && Lerc2Helpers.checkMinMaxRanges(input, data)) {
              Lerc2Helpers.constructConstantSurface(data);
            }
            else {
              var view = new DataView(input, data.ptr, 2);
              var bReadDataOneSweep = view.getUint8(0);
              data.ptr++;
              if (bReadDataOneSweep) {
                //console.debug("OneSweep");
                Lerc2Helpers.readDataOneSweep(input, data, OutPixelTypeArray);
              }
              else {
                //lerc2.1: //bitstuffing + lut
                //lerc2.2: //bitstuffing + lut + huffman
                //lerc2.3: new bitstuffer
                if (fileVersion > 1 && headerInfo.imageType <= 1 && Math.abs(headerInfo.maxZError - 0.5) < 0.00001) {
                  //this is 2.x plus 8 bit (unsigned and signed) data, possiblity of Huffman
                  var flagHuffman = view.getUint8(1);
                  data.ptr++;
                  data.encodeMode = flagHuffman;
                  if (flagHuffman > 2 || (fileVersion < 4 && flagHuffman > 1)) {
                    throw "Invalid Huffman flag " + flagHuffman;
                  }
                  if (flagHuffman) {//1 - delta Huffman, 2 - Huffman
                    //console.log("Huffman");
                    Lerc2Helpers.readHuffman(input, data, OutPixelTypeArray);
                  }
                  else {
                    //console.log("Tiles");
                    Lerc2Helpers.readTiles(input, data, OutPixelTypeArray);
                  }
                }
                else { //lerc2.x non-8 bit data
                  //console.log("Tiles");
                  Lerc2Helpers.readTiles(input, data, OutPixelTypeArray);
                }
              }
            }
          }
 
          data.eofOffset = data.ptr;
          var diff;
          if (options.inputOffset) {
            diff = data.headerInfo.blobSize + options.inputOffset - data.ptr;
            if (Math.abs(diff) >= 1) {
              //console.debug("incorrect eof: dataptr " + data.ptr + " offset " + options.inputOffset + " blobsize " + data.headerInfo.blobSize + " diff: " + diff);
              data.eofOffset = options.inputOffset + data.headerInfo.blobSize;
            }
          }
          else {
            diff = data.headerInfo.blobSize - data.ptr;
            if (Math.abs(diff) >= 1) {
              //console.debug("incorrect first band eof: dataptr " + data.ptr + " blobsize " + data.headerInfo.blobSize + " diff: " + diff);
              data.eofOffset = data.headerInfo.blobSize;
            }
          }
 
          var result = {
            width: headerInfo.width,
            height: headerInfo.height,
            pixelData: data.pixels.resultPixels,
            minValue: headerInfo.zMin,
            maxValue: headerInfo.zMax,
            validPixelCount: headerInfo.numValidPixel,
            dimCount: headerInfo.numDims,
            dimStats: {
              minValues: headerInfo.minValues,
              maxValues: headerInfo.maxValues
            },
            maskData: data.pixels.resultMask
            //noDataValue: noDataValue
          };
 
          //we should remove this if there's no existing client
          //optional noDataValue processing, it's user's responsiblity
          if (data.pixels.resultMask && Lerc2Helpers.isValidPixelValue(headerInfo.imageType, noDataValue)) {
            var mask = data.pixels.resultMask;
            for (i = 0; i < numPixels; i++) {
              if (!mask[i]) {
                result.pixelData[i] = noDataValue;
              }
            }
            result.noDataValue = noDataValue;
          }
          data.noDataValue = noDataValue;
          if (options.returnFileInfo) {
            result.fileInfo = Lerc2Helpers.formatFileInfo(data);
          }
          return result;
        },
 
        getBandCount: function(/*byte array*/ input) {
          var count = 0;
          var i = 0;
          var temp = {};
          temp.ptr = 0;
          temp.pixels = {};
          while (i < input.byteLength - 58) {
            Lerc2Helpers.readHeaderInfo(input, temp);
            i += temp.headerInfo.blobSize;
            count++;
            temp.ptr = i;
          }
          return count;
        }
      };
 
      return Lerc2Decode;
    })();
 
    var isPlatformLittleEndian = (function() {
      var a = new ArrayBuffer(4);
      var b = new Uint8Array(a);
      var c = new Uint32Array(a);
      c[0] = 1;
      return b[0] === 1;
    })();
 
    var Lerc = {
      /************wrapper**********************************************/
      /**
       * A wrapper for decoding both LERC1 and LERC2 byte streams capable of handling multiband pixel blocks for various pixel types.
       *
       * @alias module:Lerc
       * @param {ArrayBuffer} input The LERC input byte stream
       * @param {object} [options] The decoding options below are optional.
       * @param {number} [options.inputOffset] The number of bytes to skip in the input byte stream. A valid Lerc file is expected at that position.
       * @param {string} [options.pixelType] (LERC1 only) Default value is F32. Valid pixel types for input are U8/S8/S16/U16/S32/U32/F32.
       * @param {number} [options.noDataValue] (LERC1 only). It is recommended to use the returned mask instead of setting this value.
       * @returns {{width, height, pixels, pixelType, mask, statistics}}
         * @property {number} width Width of decoded image.
         * @property {number} height Height of decoded image.
         * @property {array} pixels [band1, band2, …] Each band is a typed array of width*height.
         * @property {string} pixelType The type of pixels represented in the output.
         * @property {mask} mask Typed array with a size of width*height, or null if all pixels are valid.
         * @property {array} statistics [statistics_band1, statistics_band2, …] Each element is a statistics object representing min and max values
      **/
      decode: function(encodedData, options) {
        if (!isPlatformLittleEndian) {
          throw "Big endian system is not supported.";
        }
        options = options || {};
        var inputOffset = options.inputOffset || 0;
        var fileIdView = new Uint8Array(encodedData, inputOffset, 10);
        var fileIdentifierString = String.fromCharCode.apply(null, fileIdView);
        var lerc, majorVersion;
        if (fileIdentifierString.trim() === "CntZImage") {
          lerc = LercDecode;
          majorVersion = 1;
        }
        else if (fileIdentifierString.substring(0, 5) === "Lerc2") {
          lerc = Lerc2Decode;
          majorVersion = 2;
        }
        else {
          throw "Unexpected file identifier string: " + fileIdentifierString;
        }
 
        var iPlane = 0, eof = encodedData.byteLength - 10, encodedMaskData, bandMasks = [], bandMask, maskData;
        var decodedPixelBlock = {
          width: 0,
          height: 0,
          pixels: [],
          pixelType: options.pixelType,
          mask: null,
          statistics: []
        };
 
        while (inputOffset < eof) {
          var result = lerc.decode(encodedData, {
            inputOffset: inputOffset,//for both lerc1 and lerc2
            encodedMaskData: encodedMaskData,//lerc1 only
            maskData: maskData,//lerc2 only
            returnMask: iPlane === 0 ? true : false,//lerc1 only
            returnEncodedMask: iPlane === 0 ? true : false,//lerc1 only
            returnFileInfo: true,//for both lerc1 and lerc2
            pixelType: options.pixelType || null,//lerc1 only
            noDataValue: options.noDataValue || null//lerc1 only
          });
 
          inputOffset = result.fileInfo.eofOffset;
          if (iPlane === 0) {
            encodedMaskData = result.encodedMaskData;//lerc1
            maskData = result.maskData;//lerc2
            decodedPixelBlock.width = result.width;
            decodedPixelBlock.height = result.height;
            decodedPixelBlock.dimCount = result.dimCount || 1;
            //decodedPixelBlock.dimStats = decodedPixelBlock.dimStats;
            decodedPixelBlock.pixelType = result.pixelType || result.fileInfo.pixelType;
            decodedPixelBlock.mask = result.maskData;
          }
          if (majorVersion >1 && result.fileInfo.mask && result.fileInfo.mask.numBytes > 0) {
            bandMasks.push(result.maskData);
          }
 
          iPlane++;
          decodedPixelBlock.pixels.push(result.pixelData);
          decodedPixelBlock.statistics.push({
            minValue: result.minValue,
            maxValue: result.maxValue,
            noDataValue: result.noDataValue,
            dimStats: result.dimStats
          });
        }
        var i, j, numPixels;
        if (majorVersion > 1 && bandMasks.length > 1) {
          numPixels = decodedPixelBlock.width * decodedPixelBlock.height;
          decodedPixelBlock.bandMasks = bandMasks;
          maskData = new Uint8Array(numPixels);
          maskData.set(bandMasks[0]);
          for (i = 1; i < bandMasks.length; i++) {
            bandMask = bandMasks[i];
            for (j = 0; j < numPixels; j++) {
              maskData[j] = maskData[j] & bandMask[j];
            }
          }
          decodedPixelBlock.maskData = maskData;
        }
 
        return decodedPixelBlock;
      }
    };
 
    if (module.exports) {/* jshint ignore:line */
      //commonJS module 1.0/1.1/1.1.1 systems, such as nodeJS
      //http://wiki.commonjs.org/wiki/Modules
      module.exports = Lerc;/* jshint ignore:line */
    }
    else {
      //assign to this, most likely window
      this.Lerc = Lerc;
    }
 
  })();
  });
 
  function createVerticesFromHeightmap(parameters, transferableObjects) {
    // LERC encoded buffers must be decoded, then we can process them like normal
    if (parameters.encoding === HeightmapEncoding$1.LERC) {
      var result;
      try {
        result = LercDecode.decode(parameters.heightmap);
      } catch (error) {
        throw new RuntimeError.RuntimeError(error);
      }
 
      var lercStatistics = result.statistics[0];
      if (lercStatistics.minValue === Number.MAX_VALUE) {
        throw new RuntimeError.RuntimeError("Invalid tile data");
      }
 
      parameters.heightmap = result.pixels[0];
      parameters.width = result.width;
      parameters.height = result.height;
    }
 
    parameters.ellipsoid = Matrix2.Ellipsoid.clone(parameters.ellipsoid);
    parameters.rectangle = Matrix2.Rectangle.clone(parameters.rectangle);
 
    var statistics = HeightmapTessellator.computeVertices(parameters);
    var vertices = statistics.vertices;
    transferableObjects.push(vertices.buffer);
 
    return {
      vertices: vertices.buffer,
      numberOfAttributes: statistics.encoding.stride,
      minimumHeight: statistics.minimumHeight,
      maximumHeight: statistics.maximumHeight,
      gridWidth: parameters.width,
      gridHeight: parameters.height,
      boundingSphere3D: statistics.boundingSphere3D,
      orientedBoundingBox: statistics.orientedBoundingBox,
      occludeePointInScaledSpace: statistics.occludeePointInScaledSpace,
      encoding: statistics.encoding,
      westIndicesSouthToNorth: statistics.westIndicesSouthToNorth,
      southIndicesEastToWest: statistics.southIndicesEastToWest,
      eastIndicesNorthToSouth: statistics.eastIndicesNorthToSouth,
      northIndicesWestToEast: statistics.northIndicesWestToEast,
    };
  }
  var createVerticesFromHeightmap$1 = createTaskProcessorWorker(createVerticesFromHeightmap);
 
  return createVerticesFromHeightmap$1;
 
}));