openjp2 0.3.4

Rust port of Openjpeg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
use super::consts::*;
use super::dwt::*;
use super::event::*;
use super::math::*;
use super::mct::*;
use super::openjpeg::*;
use super::pi::*;
use super::t1::*;
use super::t2::*;
use super::tgt::*;
use super::thread::*;

use super::malloc::*;

extern "C" {
  fn pow(_: core::ffi::c_double, _: core::ffi::c_double) -> core::ffi::c_double;

  fn ceil(_: core::ffi::c_double) -> core::ffi::c_double;

  fn memset(_: *mut core::ffi::c_void, _: core::ffi::c_int, _: usize) -> *mut core::ffi::c_void;

  fn memcpy(
    _: *mut core::ffi::c_void,
    _: *const core::ffi::c_void,
    _: usize,
  ) -> *mut core::ffi::c_void;
}

/* ----------------------------------------------------------------------- */
/* *
Create a new TCD handle
*/
#[no_mangle]
pub(crate) unsafe fn opj_tcd_create(mut p_is_decoder: OPJ_BOOL) -> *mut opj_tcd_t {
  let mut l_tcd = std::ptr::null_mut::<opj_tcd_t>();
  /* create the tcd structure */
  l_tcd = opj_calloc(1i32 as size_t, core::mem::size_of::<opj_tcd_t>()) as *mut opj_tcd_t;
  if l_tcd.is_null() {
    return std::ptr::null_mut::<opj_tcd_t>();
  }
  (*l_tcd).set_m_is_decoder(if p_is_decoder != 0 { 1i32 } else { 0i32 } as OPJ_BITFIELD);
  (*l_tcd).tcd_image =
    opj_calloc(1i32 as size_t, core::mem::size_of::<opj_tcd_image_t>()) as *mut opj_tcd_image_t;
  if (*l_tcd).tcd_image.is_null() {
    opj_free(l_tcd as *mut core::ffi::c_void);
    return std::ptr::null_mut::<opj_tcd_t>();
  }
  l_tcd
}
/* ----------------------------------------------------------------------- */
unsafe fn opj_tcd_rateallocate_fixed(mut tcd: *mut opj_tcd_t) {
  let mut layno: OPJ_UINT32 = 0; /* fixed_quality */
  layno = 0 as OPJ_UINT32;
  while layno < (*(*tcd).tcp).numlayers {
    opj_tcd_makelayer_fixed(tcd, layno, 1 as OPJ_UINT32);
    layno += 1;
  }
}

/** Returns OPJ_TRUE if the layer allocation is unchanged w.r.t to the previous
 * invokation with a different threshold */
unsafe fn opj_tcd_makelayer(
  mut tcd: *mut opj_tcd_t,
  mut layno: OPJ_UINT32,
  mut thresh: OPJ_FLOAT64,
  mut final_0: OPJ_UINT32,
) -> bool {
  let mut compno: OPJ_UINT32 = 0;
  let mut resno: OPJ_UINT32 = 0;
  let mut bandno: OPJ_UINT32 = 0;
  let mut precno: OPJ_UINT32 = 0;
  let mut cblkno: OPJ_UINT32 = 0;
  let mut passno: OPJ_UINT32 = 0;
  let mut tcd_tile = (*(*tcd).tcd_image).tiles;
  let mut layer_allocation_is_same = true;
  (*tcd_tile).distolayer[layno as usize] = 0 as OPJ_FLOAT64;
  compno = 0 as OPJ_UINT32;
  while compno < (*tcd_tile).numcomps {
    let mut tilec: *mut opj_tcd_tilecomp_t =
      &mut *(*tcd_tile).comps.offset(compno as isize) as *mut opj_tcd_tilecomp_t;
    resno = 0 as OPJ_UINT32;
    while resno < (*tilec).numresolutions {
      let mut res: *mut opj_tcd_resolution_t =
        &mut *(*tilec).resolutions.offset(resno as isize) as *mut opj_tcd_resolution_t;
      bandno = 0 as OPJ_UINT32;
      while bandno < (*res).numbands {
        let mut band: *mut opj_tcd_band_t =
          &mut *(*res).bands.as_mut_ptr().offset(bandno as isize) as *mut opj_tcd_band_t;
        /* Skip empty bands */
        if opj_tcd_is_band_empty(band) == 0 {
          precno = 0 as OPJ_UINT32;
          while precno < (*res).pw.wrapping_mul((*res).ph) {
            let mut prc: *mut opj_tcd_precinct_t =
              &mut *(*band).precincts.offset(precno as isize) as *mut opj_tcd_precinct_t;
            cblkno = 0 as OPJ_UINT32;
            while cblkno < (*prc).cw.wrapping_mul((*prc).ch) {
              let mut cblk: *mut opj_tcd_cblk_enc_t =
                &mut *(*prc).cblks.enc.offset(cblkno as isize) as *mut opj_tcd_cblk_enc_t;
              let mut layer: *mut opj_tcd_layer_t =
                &mut *(*cblk).layers.offset(layno as isize) as *mut opj_tcd_layer_t;
              let mut n: OPJ_UINT32 = 0;
              if layno == 0u32 {
                (*cblk).numpassesinlayers = 0 as OPJ_UINT32
              }
              n = (*cblk).numpassesinlayers;
              if thresh < 0 as core::ffi::c_double {
                /* Special value to indicate to use all passes */
                n = (*cblk).totalpasses
              } else {
                passno = (*cblk).numpassesinlayers;
                while passno < (*cblk).totalpasses {
                  let mut dr: OPJ_UINT32 = 0;
                  let mut dd: OPJ_FLOAT64 = 0.;
                  let mut pass: *mut opj_tcd_pass_t =
                    &mut *(*cblk).passes.offset(passno as isize) as *mut opj_tcd_pass_t;
                  if n == 0u32 {
                    dr = (*pass).rate;
                    dd = (*pass).distortiondec
                  } else {
                    dr = (*pass)
                      .rate
                      .wrapping_sub((*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize)).rate);
                    dd = (*pass).distortiondec
                      - (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize)).distortiondec
                  }
                  if dr == 0 {
                    if dd != 0 as core::ffi::c_double {
                      n = passno.wrapping_add(1u32)
                    }
                  } else if (thresh - dd / dr as core::ffi::c_double)
                    < 2.220_446_049_250_313e-16_f64
                  {
                    /* do not rely on float equality, check with DBL_EPSILON margin */
                    n = passno.wrapping_add(1u32)
                  }
                  passno += 1;
                }
              } /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */

              if (*layer).numpasses != n - (*cblk).numpassesinlayers {
                layer_allocation_is_same = false;
                (*layer).numpasses = n - (*cblk).numpassesinlayers;
              }
              if (*layer).numpasses == 0 {
                (*layer).disto = 0 as OPJ_FLOAT64
              } else {
                if (*cblk).numpassesinlayers == 0u32 {
                  (*layer).len = (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize)).rate;
                  (*layer).data = (*cblk).data;
                  (*layer).disto =
                    (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize)).distortiondec
                } else {
                  (*layer).len = (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize))
                    .rate
                    .wrapping_sub(
                      (*(*cblk)
                        .passes
                        .offset((*cblk).numpassesinlayers.wrapping_sub(1u32) as isize))
                      .rate,
                    );
                  (*layer).data = (*cblk).data.offset(
                    (*(*cblk)
                      .passes
                      .offset((*cblk).numpassesinlayers.wrapping_sub(1u32) as isize))
                    .rate as isize,
                  );
                  (*layer).disto = (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize))
                    .distortiondec
                    - (*(*cblk)
                      .passes
                      .offset((*cblk).numpassesinlayers.wrapping_sub(1u32) as isize))
                    .distortiondec
                }
                (*tcd_tile).distolayer[layno as usize] += (*layer).disto;
                if final_0 != 0 {
                  (*cblk).numpassesinlayers = n
                }
              }
              cblkno += 1;
            }
            precno += 1;
          }
        }
        bandno += 1;
      }
      resno += 1;
    }
    compno += 1;
  }
  layer_allocation_is_same
}

unsafe fn opj_tcd_makelayer_fixed(
  mut tcd: *mut opj_tcd_t,
  mut layno: OPJ_UINT32,
  mut final_0: OPJ_UINT32,
) {
  let mut compno: OPJ_UINT32 = 0;
  let mut resno: OPJ_UINT32 = 0;
  let mut bandno: OPJ_UINT32 = 0;
  let mut precno: OPJ_UINT32 = 0;
  let mut cblkno: OPJ_UINT32 = 0;
  let mut value: OPJ_INT32 = 0;
  let mut matrice: [[[OPJ_INT32; 3]; j2k::J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT as usize];
    j2k::J2K_TCD_MATRIX_MAX_LAYER_COUNT as usize] = [[[0; 3];
    j2k::J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT as usize];
    j2k::J2K_TCD_MATRIX_MAX_LAYER_COUNT as usize];
  let mut i: OPJ_UINT32 = 0;
  let mut j: OPJ_UINT32 = 0;
  let mut k: OPJ_UINT32 = 0;
  let mut cp = (*tcd).cp;
  let mut tcd_tile = (*(*tcd).tcd_image).tiles;
  let mut tcd_tcp = (*tcd).tcp;
  compno = 0 as OPJ_UINT32;
  while compno < (*tcd_tile).numcomps {
    let mut tilec: *mut opj_tcd_tilecomp_t =
      &mut *(*tcd_tile).comps.offset(compno as isize) as *mut opj_tcd_tilecomp_t;
    i = 0 as OPJ_UINT32;
    while i < (*tcd_tcp).numlayers {
      j = 0 as OPJ_UINT32;
      while j < (*tilec).numresolutions {
        k = 0 as OPJ_UINT32;
        while k < 3u32 {
          matrice[i as usize][j as usize][k as usize] =
            (*(*cp).m_specific_param.m_enc.m_matrice.offset(
              i.wrapping_mul((*tilec).numresolutions)
                .wrapping_mul(3u32)
                .wrapping_add(j.wrapping_mul(3u32))
                .wrapping_add(k) as isize,
            ) as OPJ_FLOAT32
              * ((*(*(*tcd).image).comps.offset(compno as isize)).prec as core::ffi::c_double
                / 16.0f64) as OPJ_FLOAT32) as OPJ_INT32;
          k += 1;
        }
        j += 1;
      }
      i += 1;
    }
    resno = 0 as OPJ_UINT32;
    while resno < (*tilec).numresolutions {
      let mut res: *mut opj_tcd_resolution_t =
        &mut *(*tilec).resolutions.offset(resno as isize) as *mut opj_tcd_resolution_t;
      bandno = 0 as OPJ_UINT32;
      while bandno < (*res).numbands {
        let mut band: *mut opj_tcd_band_t =
          &mut *(*res).bands.as_mut_ptr().offset(bandno as isize) as *mut opj_tcd_band_t;
        /* Skip empty bands */
        if opj_tcd_is_band_empty(band) == 0 {
          precno = 0 as OPJ_UINT32; /* number of bit-plan equal to zero */
          while precno < (*res).pw.wrapping_mul((*res).ph) {
            let mut prc: *mut opj_tcd_precinct_t =
              &mut *(*band).precincts.offset(precno as isize) as *mut opj_tcd_precinct_t;
            cblkno = 0 as OPJ_UINT32;
            while cblkno < (*prc).cw.wrapping_mul((*prc).ch) {
              let mut cblk: *mut opj_tcd_cblk_enc_t =
                &mut *(*prc).cblks.enc.offset(cblkno as isize) as *mut opj_tcd_cblk_enc_t;
              let mut layer: *mut opj_tcd_layer_t =
                &mut *(*cblk).layers.offset(layno as isize) as *mut opj_tcd_layer_t;
              let mut n: OPJ_UINT32 = 0;
              let mut imsb = (*(*(*tcd).image).comps.offset(compno as isize))
                .prec
                .wrapping_sub((*cblk).numbps) as OPJ_INT32;
              /* Correction of the matrix of coefficient to include the IMSB information */
              if layno == 0u32 {
                value = matrice[layno as usize][resno as usize][bandno as usize]; /* fixed_quality */
                if imsb >= value {
                  value = 0i32
                } else {
                  value -= imsb
                }
              } else {
                value = matrice[layno as usize][resno as usize][bandno as usize]
                  - matrice[layno.wrapping_sub(1u32) as usize][resno as usize][bandno as usize]; /* 1.1; fixed_quality */
                if imsb
                  >= matrice[layno.wrapping_sub(1u32) as usize][resno as usize][bandno as usize]
                {
                  value -= imsb
                    - matrice[layno.wrapping_sub(1u32) as usize][resno as usize][bandno as usize]; /* fixed_quality */
                  if value < 0i32 {
                    value = 0i32
                  }
                }
              } /* compno */
              if layno == 0u32 {
                (*cblk).numpassesinlayers = 0 as OPJ_UINT32
              } /* resno */
              n = (*cblk).numpassesinlayers;
              if (*cblk).numpassesinlayers == 0u32 {
                if value != 0i32 {
                  n = (3u32)
                    .wrapping_mul(value as OPJ_UINT32)
                    .wrapping_sub(2u32)
                    .wrapping_add((*cblk).numpassesinlayers)
                } else {
                  n = (*cblk).numpassesinlayers
                }
              } else {
                n = (3u32)
                  .wrapping_mul(value as OPJ_UINT32)
                  .wrapping_add((*cblk).numpassesinlayers)
              }
              (*layer).numpasses = n.wrapping_sub((*cblk).numpassesinlayers);
              if (*layer).numpasses != 0 {
                if (*cblk).numpassesinlayers == 0u32 {
                  (*layer).len = (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize)).rate;
                  (*layer).data = (*cblk).data
                } else {
                  (*layer).len = (*(*cblk).passes.offset(n.wrapping_sub(1u32) as isize))
                    .rate
                    .wrapping_sub(
                      (*(*cblk)
                        .passes
                        .offset((*cblk).numpassesinlayers.wrapping_sub(1u32) as isize))
                      .rate,
                    );
                  (*layer).data = (*cblk).data.offset(
                    (*(*cblk)
                      .passes
                      .offset((*cblk).numpassesinlayers.wrapping_sub(1u32) as isize))
                    .rate as isize,
                  )
                }
                if final_0 != 0 {
                  (*cblk).numpassesinlayers = n
                }
              }
              cblkno += 1;
            }
            precno += 1;
          }
        }
        bandno += 1;
      }
      resno += 1;
    }
    compno += 1;
  }
}

/** Rate allocation for the following methods:
 * - allocation by rate/distortio (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO)
 * - allocation by fixed quality  (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO)
 */
unsafe fn opj_tcd_rateallocate(
  mut tcd: *mut opj_tcd_t,
  mut dest: *mut OPJ_BYTE,
  mut p_data_written: *mut OPJ_UINT32,
  mut len: OPJ_UINT32,
  mut cstr_info: *mut opj_codestream_info_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut resno: OPJ_UINT32 = 0;
  let mut bandno: OPJ_UINT32 = 0;
  let mut precno: OPJ_UINT32 = 0;
  let mut cblkno: OPJ_UINT32 = 0;
  let mut layno: OPJ_UINT32 = 0;
  let mut passno: OPJ_UINT32 = 0;
  let mut min: OPJ_FLOAT64 = 0.;
  let mut max: OPJ_FLOAT64 = 0.;
  let mut cumdisto: [OPJ_FLOAT64; 100] = [0.; 100];
  let K = 1 as OPJ_FLOAT64;
  let mut maxSE = 0 as OPJ_FLOAT64;
  let mut cp = (*tcd).cp;
  let mut tcd_tile = (*(*tcd).tcd_image).tiles;
  let mut tcd_tcp = (*tcd).tcp;
  min = 1.797_693_134_862_315_7e308_f64;
  max = 0 as OPJ_FLOAT64;
  (*tcd_tile).numpix = 0i32;
  compno = 0 as OPJ_UINT32;
  while compno < (*tcd_tile).numcomps {
    let mut tilec: *mut opj_tcd_tilecomp_t =
      &mut *(*tcd_tile).comps.offset(compno as isize) as *mut opj_tcd_tilecomp_t;
    (*tilec).numpix = 0i32;
    resno = 0 as OPJ_UINT32;
    while resno < (*tilec).numresolutions {
      let mut res: *mut opj_tcd_resolution_t =
        &mut *(*tilec).resolutions.offset(resno as isize) as *mut opj_tcd_resolution_t;
      bandno = 0 as OPJ_UINT32;
      while bandno < (*res).numbands {
        let mut band: *mut opj_tcd_band_t =
          &mut *(*res).bands.as_mut_ptr().offset(bandno as isize) as *mut opj_tcd_band_t;
        /* bandno */
        /* precno */
        /* Skip empty bands */
        if opj_tcd_is_band_empty(band) == 0 {
          precno = 0 as OPJ_UINT32;
          while precno < (*res).pw.wrapping_mul((*res).ph) {
            let mut prc: *mut opj_tcd_precinct_t =
              &mut *(*band).precincts.offset(precno as isize) as *mut opj_tcd_precinct_t;
            cblkno = 0 as OPJ_UINT32;
            while cblkno < (*prc).cw.wrapping_mul((*prc).ch) {
              let mut cblk: *mut opj_tcd_cblk_enc_t =
                &mut *(*prc).cblks.enc.offset(cblkno as isize) as *mut opj_tcd_cblk_enc_t;
              /* cbklno */
              passno = 0 as OPJ_UINT32; /* passno */
              while passno < (*cblk).totalpasses {
                let mut pass: *mut opj_tcd_pass_t =
                  &mut *(*cblk).passes.offset(passno as isize) as *mut opj_tcd_pass_t;
                let mut dr: OPJ_INT32 = 0;
                let mut dd: OPJ_FLOAT64 = 0.;
                let mut rdslope: OPJ_FLOAT64 = 0.;
                if passno == 0u32 {
                  dr = (*pass).rate as OPJ_INT32;
                  dd = (*pass).distortiondec
                } else {
                  dr = (*pass)
                    .rate
                    .wrapping_sub((*(*cblk).passes.offset(passno.wrapping_sub(1u32) as isize)).rate)
                    as OPJ_INT32;
                  dd = (*pass).distortiondec
                    - (*(*cblk).passes.offset(passno.wrapping_sub(1u32) as isize)).distortiondec
                }
                if dr != 0i32 {
                  rdslope = dd / dr as core::ffi::c_double;
                  if rdslope < min {
                    min = rdslope
                  }
                  if rdslope > max {
                    max = rdslope
                  }
                }
                passno += 1;
              }

              {
                let cblk_pix_count = ((*cblk).x1 - (*cblk).x0) * ((*cblk).y1 - (*cblk).y0);
                (*tcd_tile).numpix += cblk_pix_count;
                (*tilec).numpix += cblk_pix_count;
              }
              cblkno += 1;
            }
            precno += 1;
          }
        }
        bandno += 1;
      }
      resno += 1;
    }
    maxSE += (((1i32) << (*(*(*tcd).image).comps.offset(compno as isize)).prec) as OPJ_FLOAT64
      - 1.0f64)
      * (((1i32) << (*(*(*tcd).image).comps.offset(compno as isize)).prec) as OPJ_FLOAT64 - 1.0f64)
      * (*tilec).numpix as OPJ_FLOAT64;
    compno += 1;
  }

  /* index file */
  if !cstr_info.is_null() {
    let mut tile_info: *mut opj_tile_info_t =
      &mut *(*cstr_info).tile.offset((*tcd).tcd_tileno as isize) as *mut opj_tile_info_t;
    (*tile_info).numpix = (*tcd_tile).numpix;
    (*tile_info).distotile = (*tcd_tile).distotile;
    (*tile_info).thresh =
      opj_malloc(((*tcd_tcp).numlayers as usize).wrapping_mul(core::mem::size_of::<OPJ_FLOAT64>()))
        as *mut OPJ_FLOAT64;
    if (*tile_info).thresh.is_null() {
      /* FIXME event manager error callback */
      return 0i32;
    }
  } /* fixed_quality */

  layno = 0 as OPJ_UINT32;
  while layno < (*tcd_tcp).numlayers {
    let mut lo = min;
    let mut hi = max;
    let mut maxlen = if (*tcd_tcp).rates[layno as usize] > 0.0f32 {
      opj_uint_min(
        ceil((*tcd_tcp).rates[layno as usize] as core::ffi::c_double) as OPJ_UINT32,
        len,
      )
    } else {
      len
    };
    let mut goodthresh = 0 as OPJ_FLOAT64;
    let mut stable_thresh = 0 as OPJ_FLOAT64;
    let mut i: OPJ_UINT32 = 0;
    let mut distotarget: OPJ_FLOAT64 = 0.;

    distotarget = (*tcd_tile).distotile
      - K * maxSE
        / pow(
          10 as OPJ_FLOAT32 as core::ffi::c_double,
          ((*tcd_tcp).distoratio[layno as usize] / 10 as core::ffi::c_float) as core::ffi::c_double,
        );

    /* Don't try to find an optimal threshold but rather take everything not included yet, if
    -r xx,yy,zz,0   (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO and rates == NULL)
    -q xx,yy,zz,0   (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO and distoratio == NULL)
    ==> possible to have some lossy layers and the last layer for sure lossless */
    if (*cp).m_specific_param.m_enc.m_quality_layer_alloc_strategy
      == J2K_QUALITY_LAYER_ALLOCATION_STRATEGY::RATE_DISTORTION_RATIO
      && (*tcd_tcp).rates[layno as usize] > 0.0f32
      || (*cp).m_specific_param.m_enc.m_quality_layer_alloc_strategy
        == J2K_QUALITY_LAYER_ALLOCATION_STRATEGY::FIXED_DISTORTION_RATIO
        && (*tcd_tcp).distoratio[layno as usize] as core::ffi::c_double > 0.0f64
    {
      let mut t2 = opj_t2_create((*tcd).image, cp); /* fixed_quality */
      let mut thresh = 0 as OPJ_FLOAT64;
      let mut last_layer_allocation_ok = false;

      if t2.is_null() {
        return 0i32;
      }
      i = 0 as OPJ_UINT32;
      while i < 128u32 {
        let mut distoachieved = 0 as OPJ_FLOAT64;
        let new_thresh = (lo + hi) / 2.0;
        /* Stop iterating when the threshold has stabilized enough */
        /* 0.5 * 1e-5 is somewhat arbitrary, but has been selected */
        /* so that this doesn't change the results of the regression */
        /* test suite. */
        if (new_thresh - thresh).abs() <= 0.5 * 1e-5 * thresh {
          break;
        }
        thresh = new_thresh;

        let layer_allocation_is_same =
          opj_tcd_makelayer(tcd, layno, thresh, 0 as OPJ_UINT32) && i != 0;
        if (*cp).m_specific_param.m_enc.m_quality_layer_alloc_strategy
          == J2K_QUALITY_LAYER_ALLOCATION_STRATEGY::FIXED_DISTORTION_RATIO
        {
          if (*cp).rsiz as core::ffi::c_int >= 0x3i32 && (*cp).rsiz as core::ffi::c_int <= 0x6i32
            || (*cp).rsiz as core::ffi::c_int >= 0x400i32
              && (*cp).rsiz as core::ffi::c_int <= 0x900i32 | 0x9bi32
          {
            if opj_t2_encode_packets(
              t2,
              (*tcd).tcd_tileno,
              tcd_tile,
              layno.wrapping_add(1u32),
              dest,
              p_data_written,
              maxlen,
              cstr_info,
              std::ptr::null_mut::<opj_tcd_marker_info_t>(),
              (*tcd).cur_tp_num,
              (*tcd).tp_pos,
              (*tcd).cur_pino,
              THRESH_CALC,
              p_manager,
            ) == 0
            {
              lo = thresh
            } else {
              distoachieved = if layno == 0u32 {
                (*tcd_tile).distolayer[0_usize]
              } else {
                (cumdisto[layno.wrapping_sub(1u32) as usize])
                  + (*tcd_tile).distolayer[layno as usize]
              };
              if distoachieved < distotarget {
                hi = thresh;
                stable_thresh = thresh
              } else {
                lo = thresh
              }
            }
          } else {
            distoachieved = if layno == 0u32 {
              (*tcd_tile).distolayer[0_usize]
            } else {
              (cumdisto[layno.wrapping_sub(1u32) as usize]) + (*tcd_tile).distolayer[layno as usize]
            };
            if distoachieved < distotarget {
              hi = thresh;
              stable_thresh = thresh
            } else {
              lo = thresh
            }
          }
        } else {
          /* Disto/rate based optimization */
          /* Check if the layer allocation done by opj_tcd_makelayer()
           * is compatible of the maximum rate allocation. If not,
           * retry with a higher threshold.
           * If OK, try with a lower threshold.
           * Call opj_t2_encode_packets() only if opj_tcd_makelayer()
           * has resulted in different truncation points since its last
           * call. */
          if (layer_allocation_is_same && !last_layer_allocation_ok)
            || (!layer_allocation_is_same
              && opj_t2_encode_packets(
                t2,
                (*tcd).tcd_tileno,
                tcd_tile,
                layno.wrapping_add(1u32),
                dest,
                p_data_written,
                maxlen,
                cstr_info,
                std::ptr::null_mut::<opj_tcd_marker_info_t>(),
                (*tcd).cur_tp_num,
                (*tcd).tp_pos,
                (*tcd).cur_pino,
                THRESH_CALC,
                p_manager,
              ) == 0)
          {
            last_layer_allocation_ok = false;
            lo = thresh;
          } else {
            last_layer_allocation_ok = true;
            hi = thresh;
            stable_thresh = thresh
          }
        }
        i += 1;
      }
      goodthresh = if stable_thresh == 0 as core::ffi::c_double {
        thresh
      } else {
        stable_thresh
      };
      opj_t2_destroy(t2);
    } else {
      /* Special value to indicate to use all passes */
      goodthresh = -(1i32) as OPJ_FLOAT64
    }
    if !cstr_info.is_null() {
      /* Threshold for Marcela Index */
      *(*(*cstr_info).tile.offset((*tcd).tcd_tileno as isize))
        .thresh
        .offset(layno as isize) = goodthresh
    }

    opj_tcd_makelayer(tcd, layno, goodthresh, 1 as OPJ_UINT32);

    cumdisto[layno as usize] = if layno == 0u32 {
      (*tcd_tile).distolayer[0_usize]
    } else {
      (cumdisto[layno.wrapping_sub(1u32) as usize]) + (*tcd_tile).distolayer[layno as usize]
    };
    layno += 1;
  }
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_init(
  mut p_tcd: *mut opj_tcd_t,
  mut p_image: *mut opj_image_t,
  mut p_cp: *mut opj_cp_t,
  mut p_tp: *mut opj_thread_pool_t,
) -> OPJ_BOOL {
  (*p_tcd).image = p_image;
  (*p_tcd).cp = p_cp;
  (*(*p_tcd).tcd_image).tiles =
    opj_calloc(1i32 as size_t, core::mem::size_of::<opj_tcd_tile_t>()) as *mut opj_tcd_tile_t;
  if (*(*p_tcd).tcd_image).tiles.is_null() {
    return 0i32;
  }
  (*(*(*p_tcd).tcd_image).tiles).comps = opj_calloc(
    (*p_image).numcomps as size_t,
    core::mem::size_of::<opj_tcd_tilecomp_t>(),
  ) as *mut opj_tcd_tilecomp_t;
  if (*(*(*p_tcd).tcd_image).tiles).comps.is_null() {
    return 0i32;
  }
  (*(*(*p_tcd).tcd_image).tiles).numcomps = (*p_image).numcomps;
  (*p_tcd).tp_pos = (*p_cp).m_specific_param.m_enc.m_tp_pos;
  (*p_tcd).thread_pool = p_tp;
  1i32
}
/* *
Destroy a previously created TCD handle
*/
#[no_mangle]
pub(crate) unsafe fn opj_tcd_destroy(mut tcd: *mut opj_tcd_t) {
  if !tcd.is_null() {
    opj_tcd_free_tile(tcd);
    if !(*tcd).tcd_image.is_null() {
      opj_free((*tcd).tcd_image as *mut core::ffi::c_void);
      (*tcd).tcd_image = std::ptr::null_mut::<opj_tcd_image_t>()
    }
    opj_free((*tcd).used_component as *mut core::ffi::c_void);
    opj_free(tcd as *mut core::ffi::c_void);
  };
}
#[no_mangle]
pub(crate) unsafe fn opj_alloc_tile_component_data(
  mut l_tilec: *mut opj_tcd_tilecomp_t,
) -> OPJ_BOOL {
  if (*l_tilec).data.is_null()
    || (*l_tilec).data_size_needed > (*l_tilec).data_size && (*l_tilec).ownsData == 0i32
  {
    (*l_tilec).data = opj_image_data_alloc((*l_tilec).data_size_needed) as *mut OPJ_INT32;
    if (*l_tilec).data.is_null() && (*l_tilec).data_size_needed != 0 {
      return 0i32;
    }
    /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
    (*l_tilec).data_size = (*l_tilec).data_size_needed;
    (*l_tilec).ownsData = 1i32
  } else if (*l_tilec).data_size_needed > (*l_tilec).data_size {
    /* We don't need to keep old data */
    opj_image_data_free((*l_tilec).data as *mut core::ffi::c_void);
    (*l_tilec).data = opj_image_data_alloc((*l_tilec).data_size_needed) as *mut OPJ_INT32;
    if (*l_tilec).data.is_null() {
      (*l_tilec).data_size = 0i32 as size_t;
      (*l_tilec).data_size_needed = 0i32 as size_t;
      (*l_tilec).ownsData = 0i32;
      return 0i32;
    }
    /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
    (*l_tilec).data_size = (*l_tilec).data_size_needed;
    (*l_tilec).ownsData = 1i32
  }
  1i32
}
/*
 * The copyright in this software is being made available under the 2-clauses
 * BSD License, included below. This software may be subject to other third
 * party and contributor rights, including patent rights, and no such rights
 * are granted under this license.
 *
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
 * Copyright (c) 2002-2014, Professor Benoit Macq
 * Copyright (c) 2001-2003, David Janssens
 * Copyright (c) 2002-2003, Yannick Verschueren
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
 * Copyright (c) 2003-2014, Antonin Descampe
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
 * Copyright (c) 2006-2007, Parvatha Elangovan
 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
 * Copyright (c) 2012, CS Systemes d'Information, France
 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/* ----------------------------------------------------------------------- */
/* TODO MSD: */
/* *
 * Initializes tile coding/decoding
 */
/* ----------------------------------------------------------------------- */
#[inline]
unsafe fn opj_tcd_init_tile(
  mut p_tcd: *mut opj_tcd_t,
  mut p_tile_no: OPJ_UINT32,
  mut isEncoder: OPJ_BOOL,
  mut sizeof_block: OPJ_SIZE_T,
  mut manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut resno: OPJ_UINT32 = 0;
  let mut bandno: OPJ_UINT32 = 0;
  let mut precno: OPJ_UINT32 = 0;
  let mut cblkno: OPJ_UINT32 = 0;
  let mut l_tcp = std::ptr::null_mut::<opj_tcp_t>();
  let mut l_cp = std::ptr::null_mut::<opj_cp_t>();
  let mut l_tile = std::ptr::null_mut::<opj_tcd_tile_t>();
  let mut l_tccp = std::ptr::null_mut::<opj_tccp_t>();
  let mut l_tilec = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_image_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_res = std::ptr::null_mut::<opj_tcd_resolution_t>();
  let mut l_band = std::ptr::null_mut::<opj_tcd_band_t>();
  let mut l_step_size = std::ptr::null_mut::<opj_stepsize_t>();
  let mut l_current_precinct = std::ptr::null_mut::<opj_tcd_precinct_t>();
  let mut l_image = std::ptr::null_mut::<opj_image_t>();
  let mut p: OPJ_UINT32 = 0;
  let mut q: OPJ_UINT32 = 0;
  let mut l_level_no: OPJ_UINT32 = 0;
  let mut l_pdx: OPJ_UINT32 = 0;
  let mut l_pdy: OPJ_UINT32 = 0;
  let mut l_x0b: OPJ_INT32 = 0;
  let mut l_y0b: OPJ_INT32 = 0;
  let mut l_tx0: OPJ_UINT32 = 0;
  let mut l_ty0: OPJ_UINT32 = 0;
  /* extent of precincts , top left, bottom right**/
  let mut l_tl_prc_x_start: OPJ_INT32 = 0;
  let mut l_tl_prc_y_start: OPJ_INT32 = 0;
  let mut l_br_prc_x_end: OPJ_INT32 = 0;
  let mut l_br_prc_y_end: OPJ_INT32 = 0;
  /* number of precinct for a resolution */
  let mut l_nb_precincts: OPJ_UINT32 = 0;
  /* room needed to store l_nb_precinct precinct for a resolution */
  let mut l_nb_precinct_size: OPJ_UINT32 = 0;
  /* number of code blocks for a precinct*/
  let mut l_nb_code_blocks: OPJ_UINT32 = 0;
  /* room needed to store l_nb_code_blocks code blocks for a precinct*/
  let mut l_nb_code_blocks_size: OPJ_UINT32 = 0;
  /* size of data for a tile */
  let mut l_data_size: OPJ_UINT32 = 0; /* tile coordinates */
  l_cp = (*p_tcd).cp;
  l_tcp = &mut *(*l_cp).tcps.offset(p_tile_no as isize) as *mut opj_tcp_t;
  l_tile = (*(*p_tcd).tcd_image).tiles;
  l_tccp = (*l_tcp).tccps;
  l_tilec = (*l_tile).comps;
  l_image = (*p_tcd).image;
  l_image_comp = (*(*p_tcd).image).comps;
  p = p_tile_no.wrapping_rem((*l_cp).tw);
  q = p_tile_no.wrapping_div((*l_cp).tw);
  /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
  /* 4 borders of the tile rescale on the image if necessary */
  l_tx0 = (*l_cp).tx0.wrapping_add(p.wrapping_mul((*l_cp).tdx)); /* can't be greater than l_image->x1 so won't overflow */
  (*l_tile).x0 = opj_uint_max(l_tx0, (*l_image).x0) as OPJ_INT32;
  (*l_tile).x1 = opj_uint_min(opj_uint_adds(l_tx0, (*l_cp).tdx), (*l_image).x1) as OPJ_INT32;
  /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
  if (*l_tile).x0 < 0i32 || (*l_tile).x1 <= (*l_tile).x0 {
    event_msg!(manager, EVT_ERROR, "Tile X coordinates are not supported\n",); /* can't be greater than l_image->y1 so won't overflow */
    return 0i32;
  }
  l_ty0 = (*l_cp).ty0.wrapping_add(q.wrapping_mul((*l_cp).tdy));
  (*l_tile).y0 = opj_uint_max(l_ty0, (*l_image).y0) as OPJ_INT32;
  (*l_tile).y1 = opj_uint_min(opj_uint_adds(l_ty0, (*l_cp).tdy), (*l_image).y1) as OPJ_INT32;
  /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
  if (*l_tile).y0 < 0i32 || (*l_tile).y1 <= (*l_tile).y0 {
    event_msg!(manager, EVT_ERROR, "Tile Y coordinates are not supported\n",);
    return 0i32;
  }
  /* testcase 1888.pdf.asan.35.988 */
  if (*l_tccp).numresolutions == 0u32 {
    event_msg!(
      manager,
      EVT_ERROR,
      "tiles require at least one resolution\n",
    );
    return 0i32;
  }
  /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
  /*tile->numcomps = image->numcomps; */
  compno = 0 as OPJ_UINT32; /* compno */
  while compno < (*l_tile).numcomps {
    /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
    (*l_image_comp).resno_decoded = 0 as OPJ_UINT32;
    /* border of each l_tile component (global) */
    (*l_tilec).x0 = opj_int_ceildiv((*l_tile).x0, (*l_image_comp).dx as OPJ_INT32);
    (*l_tilec).y0 = opj_int_ceildiv((*l_tile).y0, (*l_image_comp).dy as OPJ_INT32);
    (*l_tilec).x1 = opj_int_ceildiv((*l_tile).x1, (*l_image_comp).dx as OPJ_INT32);
    (*l_tilec).y1 = opj_int_ceildiv((*l_tile).y1, (*l_image_comp).dy as OPJ_INT32);
    (*l_tilec).compno = compno;
    /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
    (*l_tilec).numresolutions = (*l_tccp).numresolutions;
    if (*l_tccp).numresolutions < (*l_cp).m_specific_param.m_dec.m_reduce {
      (*l_tilec).minimum_num_resolutions = 1 as OPJ_UINT32
    } else {
      (*l_tilec).minimum_num_resolutions = (*l_tccp)
        .numresolutions
        .wrapping_sub((*l_cp).m_specific_param.m_dec.m_reduce)
    }
    if isEncoder != 0 {
      let mut l_tile_data_size: OPJ_SIZE_T = 0;
      /* compute l_data_size with overflow check */
      let mut w = ((*l_tilec).x1 - (*l_tilec).x0) as OPJ_SIZE_T;
      let mut h = ((*l_tilec).y1 - (*l_tilec).y0) as OPJ_SIZE_T;
      /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
      if h > 0 && w > (usize::MAX).wrapping_div(h) {
        event_msg!(
          manager,
          EVT_ERROR,
          "Size of tile data exceeds system limits\n",
        );
        return 0i32;
      }
      l_tile_data_size = w.wrapping_mul(h);
      if (usize::MAX).wrapping_div(core::mem::size_of::<OPJ_UINT32>()) < l_tile_data_size {
        event_msg!(
          manager,
          EVT_ERROR,
          "Size of tile data exceeds system limits\n",
        );
        return 0i32;
      }
      l_tile_data_size = l_tile_data_size.wrapping_mul(core::mem::size_of::<OPJ_UINT32>());
      (*l_tilec).data_size_needed = l_tile_data_size
    }
    l_data_size = (*l_tilec)
      .numresolutions
      .wrapping_mul(core::mem::size_of::<opj_tcd_resolution_t>() as OPJ_UINT32);
    opj_image_data_free((*l_tilec).data_win as *mut core::ffi::c_void);
    (*l_tilec).data_win = std::ptr::null_mut::<OPJ_INT32>();
    (*l_tilec).win_x0 = 0 as OPJ_UINT32;
    (*l_tilec).win_y0 = 0 as OPJ_UINT32;
    (*l_tilec).win_x1 = 0 as OPJ_UINT32;
    (*l_tilec).win_y1 = 0 as OPJ_UINT32;
    if (*l_tilec).resolutions.is_null() {
      (*l_tilec).resolutions = opj_malloc(l_data_size as size_t) as *mut opj_tcd_resolution_t;
      if (*l_tilec).resolutions.is_null() {
        return 0i32;
      }
      /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
      (*l_tilec).resolutions_size = l_data_size;
      memset(
        (*l_tilec).resolutions as *mut core::ffi::c_void,
        0i32,
        l_data_size as usize,
      );
    } else if l_data_size > (*l_tilec).resolutions_size {
      let mut new_resolutions = opj_realloc(
        (*l_tilec).resolutions as *mut core::ffi::c_void,
        l_data_size as size_t,
      ) as *mut opj_tcd_resolution_t;
      if new_resolutions.is_null() {
        event_msg!(
          manager,
          EVT_ERROR,
          "Not enough memory for tile resolutions\n",
        );
        opj_free((*l_tilec).resolutions as *mut core::ffi::c_void);
        (*l_tilec).resolutions = std::ptr::null_mut::<opj_tcd_resolution_t>();
        (*l_tilec).resolutions_size = 0 as OPJ_UINT32;
        return 0i32;
      }
      (*l_tilec).resolutions = new_resolutions;
      /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
      memset(
        ((*l_tilec).resolutions as *mut OPJ_BYTE).offset((*l_tilec).resolutions_size as isize)
          as *mut core::ffi::c_void,
        0i32,
        l_data_size.wrapping_sub((*l_tilec).resolutions_size) as usize,
      );
      (*l_tilec).resolutions_size = l_data_size
    }
    l_level_no = (*l_tilec).numresolutions;
    l_res = (*l_tilec).resolutions;
    l_step_size = (*l_tccp).stepsizes.as_mut_ptr();
    /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
    resno = 0 as OPJ_UINT32; /* resno */
    while resno < (*l_tilec).numresolutions {
      /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
      let mut tlcbgxstart: OPJ_INT32 = 0;
      let mut tlcbgystart: OPJ_INT32 = 0;
      let mut cbgwidthexpn: OPJ_UINT32 = 0;
      let mut cbgheightexpn: OPJ_UINT32 = 0;
      let mut cblkwidthexpn: OPJ_UINT32 = 0;
      let mut cblkheightexpn: OPJ_UINT32 = 0;
      l_level_no = l_level_no.wrapping_sub(1);
      /*, brcbgxend, brcbgyend*/
      (*l_res).x0 = opj_int_ceildivpow2((*l_tilec).x0, l_level_no as OPJ_INT32);
      (*l_res).y0 = opj_int_ceildivpow2((*l_tilec).y0, l_level_no as OPJ_INT32);
      (*l_res).x1 = opj_int_ceildivpow2((*l_tilec).x1, l_level_no as OPJ_INT32);
      (*l_res).y1 = opj_int_ceildivpow2((*l_tilec).y1, l_level_no as OPJ_INT32);
      l_pdx = (*l_tccp).prcw[resno as usize];
      l_pdy = (*l_tccp).prch[resno as usize];
      l_tl_prc_x_start = opj_int_floordivpow2((*l_res).x0, l_pdx as OPJ_INT32) << l_pdx;
      l_tl_prc_y_start = opj_int_floordivpow2((*l_res).y0, l_pdy as OPJ_INT32) << l_pdy;
      let mut tmp = (opj_int_ceildivpow2((*l_res).x1, l_pdx as OPJ_INT32) as OPJ_UINT32) << l_pdx;
      if tmp > 2147483647 as OPJ_UINT32 {
        event_msg!(manager, EVT_ERROR, "Integer overflow\n",);
        return 0i32;
      }
      l_br_prc_x_end = tmp as OPJ_INT32;
      let mut tmp_0 = (opj_int_ceildivpow2((*l_res).y1, l_pdy as OPJ_INT32) as OPJ_UINT32) << l_pdy;
      if tmp_0 > 2147483647 as OPJ_UINT32 {
        event_msg!(manager, EVT_ERROR, "Integer overflow\n",);
        return 0i32;
      }
      l_br_prc_y_end = tmp_0 as OPJ_INT32;
      (*l_res).pw = if (*l_res).x0 == (*l_res).x1 {
        0u32
      } else {
        ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx) as OPJ_UINT32
      };
      (*l_res).ph = if (*l_res).y0 == (*l_res).y1 {
        0u32
      } else {
        ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy) as OPJ_UINT32
      };
      if (*l_res).pw != 0u32 && (-(1i32) as OPJ_UINT32).wrapping_div((*l_res).pw) < (*l_res).ph {
        event_msg!(
          manager,
          EVT_ERROR,
          "Size of tile data exceeds system limits\n",
        );
        return 0i32;
      }
      l_nb_precincts = (*l_res).pw.wrapping_mul((*l_res).ph);
      if (-(1i32) as OPJ_UINT32)
        .wrapping_div(core::mem::size_of::<opj_tcd_precinct_t>() as OPJ_UINT32)
        < l_nb_precincts
      {
        event_msg!(
          manager,
          EVT_ERROR,
          "Size of tile data exceeds system limits\n",
        );
        return 0i32;
      }
      l_nb_precinct_size =
        l_nb_precincts.wrapping_mul(core::mem::size_of::<opj_tcd_precinct_t>() as OPJ_UINT32);
      if resno == 0u32 {
        tlcbgxstart = l_tl_prc_x_start;
        tlcbgystart = l_tl_prc_y_start;
        /* border for each resolution level (global) */
        /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/
        /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
        /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
        /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
        /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/
        /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
        /*brcbgxend = l_br_prc_x_end;*/
        /* brcbgyend = l_br_prc_y_end;*/
        cbgwidthexpn = l_pdx;
        cbgheightexpn = l_pdy;
        (*l_res).numbands = 1 as OPJ_UINT32
      } else {
        tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1i32);
        tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1i32);
        /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
        /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
        cbgwidthexpn = l_pdx.wrapping_sub(1u32); /* bandno */
        cbgheightexpn = l_pdy.wrapping_sub(1u32);
        (*l_res).numbands = 3 as OPJ_UINT32
      }
      cblkwidthexpn = opj_uint_min((*l_tccp).cblkw, cbgwidthexpn);
      cblkheightexpn = opj_uint_min((*l_tccp).cblkh, cbgheightexpn);
      l_band = (*l_res).bands.as_mut_ptr();
      let mut current_block_246: u64;
      bandno = 0 as OPJ_UINT32;
      while bandno < (*l_res).numbands {
        /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
        if resno == 0u32 {
          (*l_band).bandno = 0 as OPJ_UINT32;
          (*l_band).x0 = opj_int_ceildivpow2((*l_tilec).x0, l_level_no as OPJ_INT32);
          (*l_band).y0 = opj_int_ceildivpow2((*l_tilec).y0, l_level_no as OPJ_INT32);
          (*l_band).x1 = opj_int_ceildivpow2((*l_tilec).x1, l_level_no as OPJ_INT32);
          (*l_band).y1 = opj_int_ceildivpow2((*l_tilec).y1, l_level_no as OPJ_INT32)
        } else {
          (*l_band).bandno = bandno.wrapping_add(1u32);
          /* x0b = 1 if bandno = 1 or 3 */
          l_x0b = ((*l_band).bandno & 1u32) as OPJ_INT32;
          /* y0b = 1 if bandno = 2 or 3 */
          l_y0b = ((*l_band).bandno >> 1i32) as OPJ_INT32;
          /* l_band border (global) */
          (*l_band).x0 = opj_int64_ceildivpow2(
            (*l_tilec).x0 as i64 - ((l_x0b as OPJ_INT64) << l_level_no),
            l_level_no.wrapping_add(1u32) as OPJ_INT32,
          );
          (*l_band).y0 = opj_int64_ceildivpow2(
            (*l_tilec).y0 as i64 - ((l_y0b as OPJ_INT64) << l_level_no),
            l_level_no.wrapping_add(1u32) as OPJ_INT32,
          );
          (*l_band).x1 = opj_int64_ceildivpow2(
            (*l_tilec).x1 as i64 - ((l_x0b as OPJ_INT64) << l_level_no),
            l_level_no.wrapping_add(1u32) as OPJ_INT32,
          );
          (*l_band).y1 = opj_int64_ceildivpow2(
            (*l_tilec).y1 as i64 - ((l_y0b as OPJ_INT64) << l_level_no),
            l_level_no.wrapping_add(1u32) as OPJ_INT32,
          )
        }
        if isEncoder != 0 {
          /* precno */
          /* Skip empty bands */
          if opj_tcd_is_band_empty(l_band) != 0 {
            current_block_246 = 10357520176418200368;
          } else {
            current_block_246 = 13895078145312174667;
          }
        } else {
          current_block_246 = 13895078145312174667;
        }
        match current_block_246 {
          13895078145312174667 => {
            /* Table E-1 - Sub-band gains */
            /* BUG_WEIRD_TWO_INVK (look for this identifier in dwt.c): */
            /* the test (!isEncoder && l_tccp->qmfbid == 0) is strongly */
            /* linked to the use of two_invK instead of invK */
            let log2_gain = if isEncoder == 0 && (*l_tccp).qmfbid == 0u32 {
              0i32
            } else if (*l_band).bandno == 0u32 {
              0i32
            } else if (*l_band).bandno == 3u32 {
              2i32
            } else {
              1i32
            };
            /* Nominal dynamic range. Equation E-4 */
            let Rb = (*l_image_comp).prec as OPJ_INT32 + log2_gain;
            /* Delta_b value of Equation E-3 in "E.1 Inverse quantization
             * procedure" of the standard */
            (*l_band).stepsize = ((1.0f64 + (*l_step_size).mant as core::ffi::c_double / 2048.0f64)
              * pow(2.0f64, (Rb - (*l_step_size).expn) as core::ffi::c_double))
              as OPJ_FLOAT32;
            /* Mb value of Equation E-2 in "E.1 Inverse quantization
             * procedure" of the standard */
            (*l_band).numbps = (*l_step_size).expn + (*l_tccp).numgbits as OPJ_INT32 - 1i32;
            if (*l_band).precincts.is_null() && l_nb_precincts > 0u32 {
              (*l_band).precincts =
                opj_malloc(l_nb_precinct_size as size_t) as *mut opj_tcd_precinct_t;
              if (*l_band).precincts.is_null() {
                event_msg!(
                  manager,
                  EVT_ERROR,
                  "Not enough memory to handle band precints\n",
                );
                return 0i32;
              }
              /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
              memset(
                (*l_band).precincts as *mut core::ffi::c_void,
                0i32,
                l_nb_precinct_size as usize,
              );
              (*l_band).precincts_data_size = l_nb_precinct_size
            } else if (*l_band).precincts_data_size < l_nb_precinct_size {
              let mut new_precincts = opj_realloc(
                (*l_band).precincts as *mut core::ffi::c_void,
                l_nb_precinct_size as size_t,
              ) as *mut opj_tcd_precinct_t;
              if new_precincts.is_null() {
                event_msg!(
                  manager,
                  EVT_ERROR,
                  "Not enough memory to handle band precints\n",
                );
                opj_free((*l_band).precincts as *mut core::ffi::c_void);
                (*l_band).precincts = std::ptr::null_mut::<opj_tcd_precinct_t>();
                (*l_band).precincts_data_size = 0 as OPJ_UINT32;
                return 0i32;
              }
              (*l_band).precincts = new_precincts;
              /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
              memset(
                ((*l_band).precincts as *mut OPJ_BYTE)
                  .offset((*l_band).precincts_data_size as isize)
                  as *mut core::ffi::c_void,
                0i32,
                l_nb_precinct_size.wrapping_sub((*l_band).precincts_data_size) as usize,
              );
              (*l_band).precincts_data_size = l_nb_precinct_size
            }
            l_current_precinct = (*l_band).precincts;
            precno = 0 as OPJ_UINT32;
            while precno < l_nb_precincts {
              let mut tlcblkxstart: OPJ_INT32 = 0;
              let mut tlcblkystart: OPJ_INT32 = 0;
              let mut brcblkxend: OPJ_INT32 = 0;
              let mut brcblkyend: OPJ_INT32 = 0;
              let mut cbgxstart = tlcbgxstart
                + precno.wrapping_rem((*l_res).pw) as OPJ_INT32 * ((1i32) << cbgwidthexpn);
              let mut cbgystart = tlcbgystart
                + precno.wrapping_div((*l_res).pw) as OPJ_INT32 * ((1i32) << cbgheightexpn);
              let mut cbgxend = cbgxstart + ((1i32) << cbgwidthexpn);
              let mut cbgyend = cbgystart + ((1i32) << cbgheightexpn);
              /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
              /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
              /* precinct size (global) */
              /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
              (*l_current_precinct).x0 = opj_int_max(cbgxstart, (*l_band).x0);
              (*l_current_precinct).y0 = opj_int_max(cbgystart, (*l_band).y0);
              (*l_current_precinct).x1 = opj_int_min(cbgxend, (*l_band).x1);
              (*l_current_precinct).y1 = opj_int_min(cbgyend, (*l_band).y1);
              /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/
              tlcblkxstart =
                opj_int_floordivpow2((*l_current_precinct).x0, cblkwidthexpn as OPJ_INT32)
                  << cblkwidthexpn;
              /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
              tlcblkystart =
                opj_int_floordivpow2((*l_current_precinct).y0, cblkheightexpn as OPJ_INT32)
                  << cblkheightexpn;
              /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
              brcblkxend =
                opj_int_ceildivpow2((*l_current_precinct).x1, cblkwidthexpn as OPJ_INT32)
                  << cblkwidthexpn;
              /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
              brcblkyend =
                opj_int_ceildivpow2((*l_current_precinct).y1, cblkheightexpn as OPJ_INT32)
                  << cblkheightexpn;
              /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
              (*l_current_precinct).cw =
                ((brcblkxend - tlcblkxstart) >> cblkwidthexpn) as OPJ_UINT32;
              (*l_current_precinct).ch =
                ((brcblkyend - tlcblkystart) >> cblkheightexpn) as OPJ_UINT32;
              l_nb_code_blocks = (*l_current_precinct)
                .cw
                .wrapping_mul((*l_current_precinct).ch);
              /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
              if (-(1i32) as OPJ_UINT32).wrapping_div(sizeof_block as OPJ_UINT32) < l_nb_code_blocks
              {
                event_msg!(
                  manager,
                  EVT_ERROR,
                  "Size of code block data exceeds system limits\n",
                );
                return 0i32;
              }
              l_nb_code_blocks_size = l_nb_code_blocks.wrapping_mul(sizeof_block as OPJ_UINT32);
              if (*l_current_precinct).cblks.blocks.is_null() && l_nb_code_blocks > 0u32 {
                (*l_current_precinct).cblks.blocks = opj_malloc(l_nb_code_blocks_size as size_t);
                if (*l_current_precinct).cblks.blocks.is_null() {
                  return 0i32;
                }
                /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
                memset(
                  (*l_current_precinct).cblks.blocks,
                  0i32,
                  l_nb_code_blocks_size as usize,
                );
                (*l_current_precinct).block_size = l_nb_code_blocks_size
              } else if l_nb_code_blocks_size > (*l_current_precinct).block_size {
                let mut new_blocks = opj_realloc(
                  (*l_current_precinct).cblks.blocks,
                  l_nb_code_blocks_size as size_t,
                );
                if new_blocks.is_null() {
                  opj_free((*l_current_precinct).cblks.blocks);
                  (*l_current_precinct).cblks.blocks = std::ptr::null_mut::<core::ffi::c_void>();
                  (*l_current_precinct).block_size = 0 as OPJ_UINT32;
                  event_msg!(
                    manager,
                    EVT_ERROR,
                    "Not enough memory for current precinct codeblock element\n",
                  );
                  return 0i32;
                }
                (*l_current_precinct).cblks.blocks = new_blocks;
                /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size);     */
                memset(
                  ((*l_current_precinct).cblks.blocks as *mut OPJ_BYTE)
                    .offset((*l_current_precinct).block_size as isize)
                    as *mut core::ffi::c_void,
                  0i32,
                  l_nb_code_blocks_size.wrapping_sub((*l_current_precinct).block_size) as usize,
                );
                (*l_current_precinct).block_size = l_nb_code_blocks_size
              }
              if (*l_current_precinct).incltree.is_null() {
                (*l_current_precinct).incltree =
                  opj_tgt_create((*l_current_precinct).cw, (*l_current_precinct).ch, manager)
              } else {
                (*l_current_precinct).incltree = opj_tgt_init(
                  (*l_current_precinct).incltree,
                  (*l_current_precinct).cw,
                  (*l_current_precinct).ch,
                  manager,
                )
              }
              if (*l_current_precinct).imsbtree.is_null() {
                (*l_current_precinct).imsbtree =
                  opj_tgt_create((*l_current_precinct).cw, (*l_current_precinct).ch, manager)
              } else {
                (*l_current_precinct).imsbtree = opj_tgt_init(
                  (*l_current_precinct).imsbtree,
                  (*l_current_precinct).cw,
                  (*l_current_precinct).ch,
                  manager,
                )
              }
              cblkno = 0 as OPJ_UINT32;
              while cblkno < l_nb_code_blocks {
                let mut cblkxstart = tlcblkxstart
                  + cblkno.wrapping_rem((*l_current_precinct).cw) as OPJ_INT32
                    * ((1i32) << cblkwidthexpn);
                let mut cblkystart = tlcblkystart
                  + cblkno.wrapping_div((*l_current_precinct).cw) as OPJ_INT32
                    * ((1i32) << cblkheightexpn);
                let mut cblkxend = cblkxstart + ((1i32) << cblkwidthexpn);
                let mut cblkyend = cblkystart + ((1i32) << cblkheightexpn);
                if isEncoder != 0 {
                  let mut l_code_block = (*l_current_precinct).cblks.enc.offset(cblkno as isize);
                  if opj_tcd_code_block_enc_allocate(l_code_block) == 0 {
                    return 0i32;
                  }
                  /* code-block size (global) */
                  (*l_code_block).x0 = opj_int_max(cblkxstart, (*l_current_precinct).x0);
                  (*l_code_block).y0 = opj_int_max(cblkystart, (*l_current_precinct).y0);
                  (*l_code_block).x1 = opj_int_min(cblkxend, (*l_current_precinct).x1);
                  (*l_code_block).y1 = opj_int_min(cblkyend, (*l_current_precinct).y1);
                  if opj_tcd_code_block_enc_allocate_data(l_code_block) == 0 {
                    return 0i32;
                  }
                } else {
                  let mut l_code_block_0 = (*l_current_precinct).cblks.dec.offset(cblkno as isize);
                  if opj_tcd_code_block_dec_allocate(l_code_block_0) == 0 {
                    return 0i32;
                  }
                  /* code-block size (global) */
                  (*l_code_block_0).x0 = opj_int_max(cblkxstart, (*l_current_precinct).x0);
                  (*l_code_block_0).y0 = opj_int_max(cblkystart, (*l_current_precinct).y0);
                  (*l_code_block_0).x1 = opj_int_min(cblkxend, (*l_current_precinct).x1);
                  (*l_code_block_0).y1 = opj_int_min(cblkyend, (*l_current_precinct).y1)
                }
                cblkno += 1;
              }
              l_current_precinct = l_current_precinct.offset(1);
              precno += 1;
            }
          }
          _ => {}
        }
        /* Do not zero l_band->precints to avoid leaks */
        /* but make sure we don't use it later, since */
        /* it will point to precincts of previous bands... */
        bandno = bandno.wrapping_add(1);
        l_band = l_band.offset(1);
        l_step_size = l_step_size.offset(1)
      }
      l_res = l_res.offset(1);
      resno += 1;
    }
    l_tccp = l_tccp.offset(1);
    l_tilec = l_tilec.offset(1);
    l_image_comp = l_image_comp.offset(1);
    compno += 1;
  }
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_init_encode_tile(
  mut p_tcd: *mut opj_tcd_t,
  mut p_tile_no: OPJ_UINT32,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_tcd_init_tile(
    p_tcd,
    p_tile_no,
    1i32,
    core::mem::size_of::<opj_tcd_cblk_enc_t>(),
    p_manager,
  )
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_init_decode_tile(
  mut p_tcd: *mut opj_tcd_t,
  mut p_tile_no: OPJ_UINT32,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_tcd_init_tile(
    p_tcd,
    p_tile_no,
    0i32,
    core::mem::size_of::<opj_tcd_cblk_dec_t>(),
    p_manager,
  )
}
/* *
 * Allocates memory for an encoding code block (but not data).
 */
/* *
 * Allocates memory for an encoding code block (but not data memory).
 */
unsafe fn opj_tcd_code_block_enc_allocate(mut p_code_block: *mut opj_tcd_cblk_enc_t) -> OPJ_BOOL {
  if (*p_code_block).layers.is_null() {
    /* no memset since data */
    (*p_code_block).layers =
      opj_calloc(100i32 as size_t, core::mem::size_of::<opj_tcd_layer_t>()) as *mut opj_tcd_layer_t;
    if (*p_code_block).layers.is_null() {
      return 0i32;
    }
  }
  if (*p_code_block).passes.is_null() {
    (*p_code_block).passes =
      opj_calloc(100i32 as size_t, core::mem::size_of::<opj_tcd_pass_t>()) as *mut opj_tcd_pass_t;
    if (*p_code_block).passes.is_null() {
      return 0i32;
    }
  }
  1i32
}
/* *
 * Allocates data for an encoding code block
 */
/* *
 * Allocates data memory for an encoding code block.
 */
unsafe fn opj_tcd_code_block_enc_allocate_data(
  mut p_code_block: *mut opj_tcd_cblk_enc_t,
) -> OPJ_BOOL {
  let mut l_data_size: OPJ_UINT32 = 0;
  /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
  /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
  /* and +7 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 3) */
  /* and +26 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 7) */
  /* and +28 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 44) */
  /* and +33 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4) */
  /* and +63 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -IMF 2K) */
  /* and +74 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -n 8 -s 7,7 -I) */
  /* TODO: is there a theoretical upper-bound for the compressed code */
  /* block size ? */
  l_data_size = (74u32).wrapping_add(
    (((*p_code_block).x1 - (*p_code_block).x0)
      * ((*p_code_block).y1 - (*p_code_block).y0)
      * core::mem::size_of::<OPJ_UINT32>() as OPJ_INT32) as OPJ_UINT32,
  );
  if l_data_size > (*p_code_block).data_size {
    if !(*p_code_block).data.is_null() {
      /* We refer to data - 1 since below we incremented it */
      opj_free((*p_code_block).data.offset(-1) as *mut core::ffi::c_void);
    }
    (*p_code_block).data = opj_malloc(l_data_size.wrapping_add(1u32) as size_t) as *mut OPJ_BYTE;
    if (*p_code_block).data.is_null() {
      (*p_code_block).data_size = 0u32;
      return 0i32;
    }
    (*p_code_block).data_size = l_data_size;
    /*why +1 ?*/
    *(*p_code_block).data.offset(0) = 0 as OPJ_BYTE;
    (*p_code_block).data = (*p_code_block).data.offset(1)
  }
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_reinit_segment(mut seg: *mut opj_tcd_seg_t) {
  memset(
    seg as *mut core::ffi::c_void,
    0i32,
    core::mem::size_of::<opj_tcd_seg_t>(),
  );
}
/* We reserve the initial byte as a fake byte to a non-FF value */
/* and increment the data pointer, so that opj_mqc_init_enc() */
/* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
/* it. */
/* *
* Allocates memory for a decoding code block.
*/
/* *
 * Allocates memory for a decoding code block.
 */
unsafe fn opj_tcd_code_block_dec_allocate(mut p_code_block: *mut opj_tcd_cblk_dec_t) -> OPJ_BOOL {
  if (*p_code_block).segs.is_null() {
    (*p_code_block).segs =
      opj_calloc(10i32 as size_t, core::mem::size_of::<opj_tcd_seg_t>()) as *mut opj_tcd_seg_t;
    if (*p_code_block).segs.is_null() {
      return 0i32;
    }
    /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
    (*p_code_block).m_current_max_segs = 10 as OPJ_UINT32
  } else {
    /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
    /* sanitize */
    let mut l_segs = (*p_code_block).segs; /*(/ 8)*/
    let mut l_current_max_segs = (*p_code_block).m_current_max_segs; /* (%8) */
    let mut l_chunks = (*p_code_block).chunks;
    let mut l_numchunksalloc = (*p_code_block).numchunksalloc;
    let mut i: OPJ_UINT32 = 0;
    opj_aligned_free((*p_code_block).decoded_data as *mut core::ffi::c_void);
    (*p_code_block).decoded_data = std::ptr::null_mut::<OPJ_INT32>();
    memset(
      p_code_block as *mut core::ffi::c_void,
      0i32,
      core::mem::size_of::<opj_tcd_cblk_dec_t>(),
    );
    (*p_code_block).segs = l_segs;
    (*p_code_block).m_current_max_segs = l_current_max_segs;
    i = 0 as OPJ_UINT32;
    while i < l_current_max_segs {
      opj_tcd_reinit_segment(&mut *l_segs.offset(i as isize));
      i += 1;
    }
    (*p_code_block).chunks = l_chunks;
    (*p_code_block).numchunksalloc = l_numchunksalloc
  }
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_get_decoded_tile_size(
  mut p_tcd: *mut opj_tcd_t,
  mut take_into_account_partial_decoding: OPJ_BOOL,
) -> OPJ_UINT32 {
  let mut i: OPJ_UINT32 = 0;
  let mut l_data_size = 0 as OPJ_UINT32;
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_tile_comp = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_res = std::ptr::null_mut::<opj_tcd_resolution_t>();
  let mut l_size_comp: OPJ_UINT32 = 0;
  let mut l_remaining: OPJ_UINT32 = 0;
  let mut l_temp: OPJ_UINT32 = 0;
  l_tile_comp = (*(*(*p_tcd).tcd_image).tiles).comps;
  l_img_comp = (*(*p_tcd).image).comps;
  i = 0 as OPJ_UINT32;
  while i < (*(*p_tcd).image).numcomps {
    let mut w: OPJ_UINT32 = 0;
    let mut h: OPJ_UINT32 = 0;
    l_size_comp = (*l_img_comp).prec >> 3i32;
    l_remaining = (*l_img_comp).prec & 7u32;
    if l_remaining != 0 {
      l_size_comp += 1;
    }
    if l_size_comp == 3u32 {
      l_size_comp = 4 as OPJ_UINT32
    }
    l_res = (*l_tile_comp)
      .resolutions
      .offset((*l_tile_comp).minimum_num_resolutions as isize)
      .offset(-1);
    if take_into_account_partial_decoding != 0 && (*p_tcd).whole_tile_decoding == 0 {
      w = (*l_res).win_x1.wrapping_sub((*l_res).win_x0);
      h = (*l_res).win_y1.wrapping_sub((*l_res).win_y0)
    } else {
      w = ((*l_res).x1 - (*l_res).x0) as OPJ_UINT32;
      h = ((*l_res).y1 - (*l_res).y0) as OPJ_UINT32
    }
    if h > 0u32
      && (2147483647u32)
        .wrapping_mul(2u32)
        .wrapping_add(1u32)
        .wrapping_div(w)
        < h
    {
      return (2147483647u32).wrapping_mul(2u32).wrapping_add(1u32);
    }
    l_temp = w.wrapping_mul(h);
    if l_size_comp != 0
      && (2147483647u32)
        .wrapping_mul(2u32)
        .wrapping_add(1u32)
        .wrapping_div(l_size_comp)
        < l_temp
    {
      return (2147483647u32).wrapping_mul(2u32).wrapping_add(1u32);
    }
    l_temp = (l_temp as core::ffi::c_uint).wrapping_mul(l_size_comp) as OPJ_UINT32;
    if l_temp
      > (2147483647u32)
        .wrapping_mul(2u32)
        .wrapping_add(1u32)
        .wrapping_sub(l_data_size)
    {
      return (2147483647u32).wrapping_mul(2u32).wrapping_add(1u32);
    }
    l_data_size = (l_data_size as core::ffi::c_uint).wrapping_add(l_temp) as OPJ_UINT32;
    l_img_comp = l_img_comp.offset(1);
    l_tile_comp = l_tile_comp.offset(1);
    i += 1;
  }
  l_data_size
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_encode_tile(
  mut p_tcd: *mut opj_tcd_t,
  mut p_tile_no: OPJ_UINT32,
  mut p_dest: *mut OPJ_BYTE,
  mut p_data_written: *mut OPJ_UINT32,
  mut p_max_length: OPJ_UINT32,
  mut p_cstr_info: *mut opj_codestream_info_t,
  mut p_marker_info: *mut opj_tcd_marker_info_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  if (*p_tcd).cur_tp_num == 0u32 {
    (*p_tcd).tcd_tileno = p_tile_no;
    (*p_tcd).tcp = &mut *(*(*p_tcd).cp).tcps.offset(p_tile_no as isize) as *mut opj_tcp_t;
    /* FIXME _ProfStop(PGROUP_RATE); */
    if !p_cstr_info.is_null() {
      let mut l_num_packs = 0 as OPJ_UINT32;
      let mut i: OPJ_UINT32 = 0;
      /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
      let mut l_tilec_idx: *mut opj_tcd_tilecomp_t =
        &mut *(*(*(*p_tcd).tcd_image).tiles).comps.offset(0) as *mut opj_tcd_tilecomp_t; /* based on component 0 */
      let mut l_tccp = (*(*p_tcd).tcp).tccps; /* based on component 0 */
      i = 0 as OPJ_UINT32;
      while i < (*l_tilec_idx).numresolutions {
        let mut l_res_idx: *mut opj_tcd_resolution_t =
          &mut *(*l_tilec_idx).resolutions.offset(i as isize) as *mut opj_tcd_resolution_t;
        (*(*p_cstr_info).tile.offset(p_tile_no as isize)).pw[i as usize] =
          (*l_res_idx).pw as core::ffi::c_int;
        (*(*p_cstr_info).tile.offset(p_tile_no as isize)).ph[i as usize] =
          (*l_res_idx).ph as core::ffi::c_int;
        l_num_packs = (l_num_packs as core::ffi::c_uint)
          .wrapping_add((*l_res_idx).pw.wrapping_mul((*l_res_idx).ph))
          as OPJ_UINT32;
        (*(*p_cstr_info).tile.offset(p_tile_no as isize)).pdx[i as usize] =
          (*l_tccp).prcw[i as usize] as core::ffi::c_int;
        (*(*p_cstr_info).tile.offset(p_tile_no as isize)).pdy[i as usize] =
          (*l_tccp).prch[i as usize] as core::ffi::c_int;
        i += 1;
      }
      let fresh0 = &mut (*(*p_cstr_info).tile.offset(p_tile_no as isize)).packet;
      *fresh0 = opj_calloc(
        ((*p_cstr_info).numcomps as OPJ_SIZE_T)
          .wrapping_mul((*p_cstr_info).numlayers as OPJ_SIZE_T)
          .wrapping_mul(l_num_packs as usize),
        core::mem::size_of::<opj_packet_info_t>(),
      ) as *mut opj_packet_info_t;
      if (*(*p_cstr_info).tile.offset(p_tile_no as isize))
        .packet
        .is_null()
      {
        /* FIXME event manager error callback */
        return 0i32;
      }
    }
    if opj_tcd_dc_level_shift_encode(p_tcd) == 0 {
      return 0i32;
    }
    if opj_tcd_mct_encode(p_tcd) == 0 {
      return 0i32;
    }
    if opj_tcd_dwt_encode(p_tcd) == 0 {
      return 0i32;
    }
    if opj_tcd_t1_encode(p_tcd) == 0 {
      return 0i32;
    }
    if opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info, p_manager) == 0 {
      return 0i32;
    }
  }
  /* << INDEX */
  /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
  /*---------------TILE-------------------*/
  /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
  /* FIXME _ProfStart(PGROUP_MCT); */
  /* FIXME _ProfStop(PGROUP_MCT); */
  /* FIXME _ProfStart(PGROUP_DWT); */
  /* FIXME  _ProfStop(PGROUP_DWT); */
  /* FIXME  _ProfStart(PGROUP_T1); */
  /* FIXME _ProfStop(PGROUP_T1); */
  /* FIXME _ProfStart(PGROUP_RATE); */
  /*--------------TIER2------------------*/
  /* INDEX */
  if !p_cstr_info.is_null() {
    (*p_cstr_info).index_write = 1i32
  }
  /* FIXME _ProfStart(PGROUP_T2); */
  if opj_tcd_t2_encode(
    p_tcd,
    p_dest,
    p_data_written,
    p_max_length,
    p_cstr_info,
    p_marker_info,
    p_manager,
  ) == 0
  {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_T2); */
  /*---------------CLEAN-------------------*/
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_decode_tile(
  mut p_tcd: *mut opj_tcd_t,
  mut win_x0: OPJ_UINT32,
  mut win_y0: OPJ_UINT32,
  mut win_x1: OPJ_UINT32,
  mut win_y1: OPJ_UINT32,
  mut numcomps_to_decode: OPJ_UINT32,
  mut comps_indices: *const OPJ_UINT32,
  mut p_src: *mut OPJ_BYTE,
  mut p_max_length: OPJ_UINT32,
  mut p_tile_no: OPJ_UINT32,
  mut p_cstr_index: *mut opj_codestream_index_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_data_read: OPJ_UINT32 = 0;
  let mut compno: OPJ_UINT32 = 0;
  (*p_tcd).tcd_tileno = p_tile_no;
  (*p_tcd).tcp = &mut *(*(*p_tcd).cp).tcps.offset(p_tile_no as isize) as *mut opj_tcp_t;
  (*p_tcd).win_x0 = win_x0;
  (*p_tcd).win_y0 = win_y0;
  (*p_tcd).win_x1 = win_x1;
  (*p_tcd).win_y1 = win_y1;
  (*p_tcd).whole_tile_decoding = 1i32;
  opj_free((*p_tcd).used_component as *mut core::ffi::c_void);
  (*p_tcd).used_component = std::ptr::null_mut::<OPJ_BOOL>();
  if numcomps_to_decode != 0 {
    let mut used_component = opj_calloc(
      core::mem::size_of::<OPJ_BOOL>(),
      (*(*p_tcd).image).numcomps as size_t,
    ) as *mut OPJ_BOOL;
    if used_component.is_null() {
      return 0i32;
    }
    compno = 0 as OPJ_UINT32;
    while compno < numcomps_to_decode {
      *used_component.offset(*comps_indices.offset(compno as isize) as isize) = 1i32;
      compno += 1;
    }
    (*p_tcd).used_component = used_component
  }
  compno = 0 as OPJ_UINT32;
  while compno < (*(*p_tcd).image).numcomps {
    if !(!(*p_tcd).used_component.is_null()
      && *(*p_tcd).used_component.offset(compno as isize) == 0)
      && opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno) == 0
    {
      (*p_tcd).whole_tile_decoding = 0i32;
      break;
    }
    compno += 1;
  }
  if (*p_tcd).whole_tile_decoding != 0 {
    compno = 0 as OPJ_UINT32;
    while compno < (*(*p_tcd).image).numcomps {
      let mut tilec: *mut opj_tcd_tilecomp_t =
        &mut *(*(*(*p_tcd).tcd_image).tiles).comps.offset(compno as isize)
          as *mut opj_tcd_tilecomp_t;
      let mut l_res: *mut opj_tcd_resolution_t = &mut *(*tilec)
        .resolutions
        .offset((*tilec).minimum_num_resolutions.wrapping_sub(1u32) as isize)
        as *mut opj_tcd_resolution_t;
      let mut l_data_size: OPJ_SIZE_T = 0;
      /* compute l_data_size with overflow check */
      let mut res_w = ((*l_res).x1 - (*l_res).x0) as OPJ_SIZE_T;
      let mut res_h = ((*l_res).y1 - (*l_res).y0) as OPJ_SIZE_T;
      if !(!(*p_tcd).used_component.is_null()
        && *(*p_tcd).used_component.offset(compno as isize) == 0)
      {
        /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
        if res_h > 0 && res_w > (usize::MAX).wrapping_div(res_h) {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
        l_data_size = res_w.wrapping_mul(res_h);
        if (usize::MAX).wrapping_div(core::mem::size_of::<OPJ_UINT32>()) < l_data_size {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
        l_data_size = (l_data_size as usize).wrapping_mul(core::mem::size_of::<OPJ_UINT32>())
          as OPJ_SIZE_T as OPJ_SIZE_T;
        (*tilec).data_size_needed = l_data_size;
        if opj_alloc_tile_component_data(tilec) == 0 {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
      }
      compno += 1;
    }
  } else {
    /* Compute restricted tile-component and tile-resolution coordinates */
    /* of the window of interest, but defer the memory allocation until */
    /* we know the resno_decoded */
    compno = 0 as OPJ_UINT32;
    while compno < (*(*p_tcd).image).numcomps {
      let mut resno: OPJ_UINT32 = 0;
      let mut tilec_0: *mut opj_tcd_tilecomp_t =
        &mut *(*(*(*p_tcd).tcd_image).tiles).comps.offset(compno as isize)
          as *mut opj_tcd_tilecomp_t;
      let mut image_comp: *mut opj_image_comp_t =
        &mut *(*(*p_tcd).image).comps.offset(compno as isize) as *mut opj_image_comp_t;
      if !(!(*p_tcd).used_component.is_null()
        && *(*p_tcd).used_component.offset(compno as isize) == 0)
      {
        /* Compute the intersection of the area of interest, expressed in tile coordinates */
        /* with the tile coordinates */
        (*tilec_0).win_x0 = opj_uint_max(
          (*tilec_0).x0 as OPJ_UINT32,
          opj_uint_ceildiv((*p_tcd).win_x0, (*image_comp).dx),
        );
        (*tilec_0).win_y0 = opj_uint_max(
          (*tilec_0).y0 as OPJ_UINT32,
          opj_uint_ceildiv((*p_tcd).win_y0, (*image_comp).dy),
        );
        (*tilec_0).win_x1 = opj_uint_min(
          (*tilec_0).x1 as OPJ_UINT32,
          opj_uint_ceildiv((*p_tcd).win_x1, (*image_comp).dx),
        );
        (*tilec_0).win_y1 = opj_uint_min(
          (*tilec_0).y1 as OPJ_UINT32,
          opj_uint_ceildiv((*p_tcd).win_y1, (*image_comp).dy),
        );
        if (*tilec_0).win_x1 < (*tilec_0).win_x0 || (*tilec_0).win_y1 < (*tilec_0).win_y0 {
          /* We should not normally go there. The circumstance is when */
          /* the tile coordinates do not intersect the area of interest */
          /* Upper level logic should not even try to decode that tile */
          event_msg!(p_manager, EVT_ERROR, "Invalid tilec->win_xxx values\n",);
          return 0i32;
        }
        resno = 0 as OPJ_UINT32;
        while resno < (*tilec_0).numresolutions {
          let mut res = (*tilec_0).resolutions.offset(resno as isize);
          (*res).win_x0 = opj_uint_ceildivpow2(
            (*tilec_0).win_x0,
            (*tilec_0)
              .numresolutions
              .wrapping_sub(1u32)
              .wrapping_sub(resno),
          );
          (*res).win_y0 = opj_uint_ceildivpow2(
            (*tilec_0).win_y0,
            (*tilec_0)
              .numresolutions
              .wrapping_sub(1u32)
              .wrapping_sub(resno),
          );
          (*res).win_x1 = opj_uint_ceildivpow2(
            (*tilec_0).win_x1,
            (*tilec_0)
              .numresolutions
              .wrapping_sub(1u32)
              .wrapping_sub(resno),
          );
          (*res).win_y1 = opj_uint_ceildivpow2(
            (*tilec_0).win_y1,
            (*tilec_0)
              .numresolutions
              .wrapping_sub(1u32)
              .wrapping_sub(resno),
          );
          resno += 1;
        }
      }
      compno += 1;
    }
  }
  /* FIXME */
  /*--------------TIER2------------------*/
  /* FIXME _ProfStart(PGROUP_T2); */
  l_data_read = 0 as OPJ_UINT32;
  if opj_tcd_t2_decode(
    p_tcd,
    p_src,
    &mut l_data_read,
    p_max_length,
    p_cstr_index,
    p_manager,
  ) == 0
  {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_T2); */
  /*------------------TIER1-----------------*/
  /* FIXME _ProfStart(PGROUP_T1); */
  if opj_tcd_t1_decode(p_tcd, p_manager) == 0 {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_T1); */
  /* For subtile decoding, now we know the resno_decoded, we can allocate */
  /* the tile data buffer */
  if (*p_tcd).whole_tile_decoding == 0 {
    compno = 0 as OPJ_UINT32;
    while compno < (*(*p_tcd).image).numcomps {
      let mut tilec_1: *mut opj_tcd_tilecomp_t =
        &mut *(*(*(*p_tcd).tcd_image).tiles).comps.offset(compno as isize)
          as *mut opj_tcd_tilecomp_t;
      let mut image_comp_0: *mut opj_image_comp_t =
        &mut *(*(*p_tcd).image).comps.offset(compno as isize) as *mut opj_image_comp_t;
      let mut res_0 = (*tilec_1)
        .resolutions
        .offset((*image_comp_0).resno_decoded as isize);
      let mut w = (*res_0).win_x1.wrapping_sub((*res_0).win_x0) as OPJ_SIZE_T;
      let mut h = (*res_0).win_y1.wrapping_sub((*res_0).win_y0) as OPJ_SIZE_T;
      let mut l_data_size_0: OPJ_SIZE_T = 0;
      opj_image_data_free((*tilec_1).data_win as *mut core::ffi::c_void);
      (*tilec_1).data_win = std::ptr::null_mut::<OPJ_INT32>();
      if !(!(*p_tcd).used_component.is_null()
        && *(*p_tcd).used_component.offset(compno as isize) == 0)
        && w > 0
        && h > 0
      {
        if w > (usize::MAX).wrapping_div(h) {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
        l_data_size_0 = w.wrapping_mul(h);
        if l_data_size_0 > (usize::MAX).wrapping_div(core::mem::size_of::<OPJ_INT32>()) {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
        l_data_size_0 = (l_data_size_0 as usize).wrapping_mul(core::mem::size_of::<OPJ_INT32>())
          as OPJ_SIZE_T as OPJ_SIZE_T;
        (*tilec_1).data_win = opj_image_data_alloc(l_data_size_0) as *mut OPJ_INT32;
        if (*tilec_1).data_win.is_null() {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Size of tile data exceeds system limits\n",
          );
          return 0i32;
        }
      }
      compno += 1;
    }
  }
  /*----------------DWT---------------------*/
  /* FIXME _ProfStart(PGROUP_DWT); */
  if opj_tcd_dwt_decode(p_tcd) == 0 {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_DWT); */
  /*----------------MCT-------------------*/
  /* FIXME _ProfStart(PGROUP_MCT); */
  if opj_tcd_mct_decode(p_tcd, p_manager) == 0 {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_MCT); */
  /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
  if opj_tcd_dc_level_shift_decode(p_tcd) == 0 {
    return 0i32;
  }
  /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
  /*---------------TILE-------------------*/
  1i32 /*(/ 8)*/
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_update_tile_data(
  mut p_tcd: *mut opj_tcd_t,
  mut p_dest: *mut OPJ_BYTE,
  mut p_dest_length: OPJ_UINT32,
) -> OPJ_BOOL {
  let mut i: OPJ_UINT32 = 0; /* (%8) */
  let mut j: OPJ_UINT32 = 0;
  let mut k: OPJ_UINT32 = 0;
  let mut l_data_size = 0 as OPJ_UINT32;
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_tilec = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_res = std::ptr::null_mut::<opj_tcd_resolution_t>();
  let mut l_size_comp: OPJ_UINT32 = 0;
  let mut l_remaining: OPJ_UINT32 = 0;
  let mut l_stride: OPJ_UINT32 = 0;
  let mut l_width: OPJ_UINT32 = 0;
  let mut l_height: OPJ_UINT32 = 0;
  l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, 1i32);
  if l_data_size == (2147483647u32).wrapping_mul(2u32).wrapping_add(1u32)
    || l_data_size > p_dest_length
  {
    return 0i32;
  }
  l_tilec = (*(*(*p_tcd).tcd_image).tiles).comps;
  l_img_comp = (*(*p_tcd).image).comps;
  i = 0 as OPJ_UINT32;
  while i < (*(*p_tcd).image).numcomps {
    let mut l_src_data = std::ptr::null::<OPJ_INT32>();
    l_size_comp = (*l_img_comp).prec >> 3i32;
    l_remaining = (*l_img_comp).prec & 7u32;
    l_res = (*l_tilec)
      .resolutions
      .offset((*l_img_comp).resno_decoded as isize);
    if (*p_tcd).whole_tile_decoding != 0 {
      l_width = ((*l_res).x1 - (*l_res).x0) as OPJ_UINT32;
      l_height = ((*l_res).y1 - (*l_res).y0) as OPJ_UINT32;
      l_stride = (((*(*l_tilec)
        .resolutions
        .offset((*l_tilec).minimum_num_resolutions.wrapping_sub(1u32) as isize))
      .x1
        - (*(*l_tilec)
          .resolutions
          .offset((*l_tilec).minimum_num_resolutions.wrapping_sub(1u32) as isize))
        .x0) as OPJ_UINT32)
        .wrapping_sub(l_width);
      l_src_data = (*l_tilec).data
    } else {
      l_width = (*l_res).win_x1.wrapping_sub((*l_res).win_x0);
      l_height = (*l_res).win_y1.wrapping_sub((*l_res).win_y0);
      l_stride = 0 as OPJ_UINT32;
      l_src_data = (*l_tilec).data_win
    }
    if l_remaining != 0 {
      l_size_comp += 1;
    }
    if l_size_comp == 3u32 {
      l_size_comp = 4 as OPJ_UINT32
    }
    match l_size_comp {
      1 => {
        let mut l_dest_ptr = p_dest as *mut OPJ_CHAR;
        let mut l_src_ptr = l_src_data;
        if (*l_img_comp).sgnd != 0 {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            k = 0 as OPJ_UINT32;
            while k < l_width {
              let fresh1 = l_src_ptr;
              l_src_ptr = l_src_ptr.offset(1);
              let fresh2 = l_dest_ptr;
              l_dest_ptr = l_dest_ptr.offset(1);
              *fresh2 = *fresh1 as OPJ_CHAR;
              k += 1;
            }
            l_src_ptr = l_src_ptr.offset(l_stride as isize);
            j += 1;
          }
        } else {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            k = 0 as OPJ_UINT32;
            while k < l_width {
              let fresh3 = l_src_ptr;
              l_src_ptr = l_src_ptr.offset(1);
              let fresh4 = l_dest_ptr;
              l_dest_ptr = l_dest_ptr.offset(1);
              *fresh4 = (*fresh3 & 0xffi32) as OPJ_CHAR;
              k += 1;
            }
            l_src_ptr = l_src_ptr.offset(l_stride as isize);
            j += 1;
          }
        }
        p_dest = l_dest_ptr as *mut OPJ_BYTE
      }
      2 => {
        let mut l_src_ptr_0 = l_src_data;
        let mut l_dest_ptr_0 = p_dest as *mut OPJ_INT16;
        if (*l_img_comp).sgnd != 0 {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            k = 0 as OPJ_UINT32;
            while k < l_width {
              let fresh5 = l_src_ptr_0;
              l_src_ptr_0 = l_src_ptr_0.offset(1);
              let mut val = *fresh5 as OPJ_INT16;
              memcpy(
                l_dest_ptr_0 as *mut core::ffi::c_void,
                &mut val as *mut OPJ_INT16 as *const core::ffi::c_void,
                core::mem::size_of::<OPJ_INT16>(),
              );
              l_dest_ptr_0 = l_dest_ptr_0.offset(1);
              k += 1;
            }
            l_src_ptr_0 = l_src_ptr_0.offset(l_stride as isize);
            j += 1;
          }
        } else {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            k = 0 as OPJ_UINT32;
            while k < l_width {
              let fresh6 = l_src_ptr_0;
              l_src_ptr_0 = l_src_ptr_0.offset(1);
              let mut val_0 = (*fresh6 & 0xffffi32) as OPJ_INT16;
              memcpy(
                l_dest_ptr_0 as *mut core::ffi::c_void,
                &mut val_0 as *mut OPJ_INT16 as *const core::ffi::c_void,
                core::mem::size_of::<OPJ_INT16>(),
              );
              l_dest_ptr_0 = l_dest_ptr_0.offset(1);
              k += 1;
            }
            l_src_ptr_0 = l_src_ptr_0.offset(l_stride as isize);
            j += 1;
          }
        }
        p_dest = l_dest_ptr_0 as *mut OPJ_BYTE
      }
      4 => {
        let mut l_dest_ptr_1 = p_dest as *mut OPJ_INT32;
        let mut l_src_ptr_1 = l_src_data;
        j = 0 as OPJ_UINT32;
        while j < l_height {
          memcpy(
            l_dest_ptr_1 as *mut core::ffi::c_void,
            l_src_ptr_1 as *const core::ffi::c_void,
            (l_width as usize).wrapping_mul(core::mem::size_of::<OPJ_INT32>()),
          );
          l_dest_ptr_1 = l_dest_ptr_1.offset(l_width as isize);
          l_src_ptr_1 = l_src_ptr_1.offset(l_width.wrapping_add(l_stride) as isize);
          j += 1;
        }
        p_dest = l_dest_ptr_1 as *mut OPJ_BYTE
      }
      _ => {}
    }
    l_img_comp = l_img_comp.offset(1);
    l_tilec = l_tilec.offset(1);
    i += 1;
  }
  1i32
}
/* *
Free the memory allocated for encoding
@param tcd TCD handle
*/
unsafe fn opj_tcd_free_tile(mut p_tcd: *mut opj_tcd_t) {
  let mut compno: OPJ_UINT32 = 0; /* for (resno */
  let mut resno: OPJ_UINT32 = 0;
  let mut bandno: OPJ_UINT32 = 0;
  let mut precno: OPJ_UINT32 = 0;
  let mut l_tile = std::ptr::null_mut::<opj_tcd_tile_t>();
  let mut l_tile_comp = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_res = std::ptr::null_mut::<opj_tcd_resolution_t>();
  let mut l_band = std::ptr::null_mut::<opj_tcd_band_t>();
  let mut l_precinct = std::ptr::null_mut::<opj_tcd_precinct_t>();
  let mut l_nb_resolutions: OPJ_UINT32 = 0;
  let mut l_nb_precincts: OPJ_UINT32 = 0;
  let mut l_tcd_code_block_deallocate: Option<unsafe fn(_: *mut opj_tcd_precinct_t) -> ()> = None;
  if p_tcd.is_null() {
    return;
  }
  if (*p_tcd).tcd_image.is_null() {
    return;
  }
  if (*p_tcd).m_is_decoder() != 0 {
    l_tcd_code_block_deallocate =
      Some(opj_tcd_code_block_dec_deallocate as unsafe fn(_: *mut opj_tcd_precinct_t) -> ())
  } else {
    l_tcd_code_block_deallocate =
      Some(opj_tcd_code_block_enc_deallocate as unsafe fn(_: *mut opj_tcd_precinct_t) -> ())
  }
  l_tile = (*(*p_tcd).tcd_image).tiles;
  if l_tile.is_null() {
    return;
  }
  l_tile_comp = (*l_tile).comps;
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    l_res = (*l_tile_comp).resolutions;
    if !l_res.is_null() {
      l_nb_resolutions = (*l_tile_comp)
        .resolutions_size
        .wrapping_div(core::mem::size_of::<opj_tcd_resolution_t>() as OPJ_UINT32);
      resno = 0 as OPJ_UINT32;
      while resno < l_nb_resolutions {
        l_band = (*l_res).bands.as_mut_ptr();
        bandno = 0 as OPJ_UINT32;
        while bandno < 3u32 {
          l_precinct = (*l_band).precincts;
          if !l_precinct.is_null() {
            l_nb_precincts = (*l_band)
              .precincts_data_size
              .wrapping_div(core::mem::size_of::<opj_tcd_precinct_t>() as OPJ_UINT32);
            precno = 0 as OPJ_UINT32;
            while precno < l_nb_precincts {
              opj_tgt_destroy((*l_precinct).incltree);
              (*l_precinct).incltree = std::ptr::null_mut::<opj_tgt_tree_t>();
              opj_tgt_destroy((*l_precinct).imsbtree);
              (*l_precinct).imsbtree = std::ptr::null_mut::<opj_tgt_tree_t>();
              l_tcd_code_block_deallocate.expect("non-null function pointer")(l_precinct);
              l_precinct = l_precinct.offset(1);
              precno += 1;
            }
            opj_free((*l_band).precincts as *mut core::ffi::c_void);
            (*l_band).precincts = std::ptr::null_mut::<opj_tcd_precinct_t>()
          }
          l_band = l_band.offset(1);
          bandno += 1;
        }
        l_res = l_res.offset(1);
        resno += 1;
      }
      opj_free((*l_tile_comp).resolutions as *mut core::ffi::c_void);
      (*l_tile_comp).resolutions = std::ptr::null_mut::<opj_tcd_resolution_t>()
    }
    if (*l_tile_comp).ownsData != 0 && !(*l_tile_comp).data.is_null() {
      opj_image_data_free((*l_tile_comp).data as *mut core::ffi::c_void);
      (*l_tile_comp).data = std::ptr::null_mut::<OPJ_INT32>();
      (*l_tile_comp).ownsData = 0i32;
      (*l_tile_comp).data_size = 0i32 as size_t;
      (*l_tile_comp).data_size_needed = 0i32 as size_t
    }
    opj_image_data_free((*l_tile_comp).data_win as *mut core::ffi::c_void);
    l_tile_comp = l_tile_comp.offset(1);
    compno += 1;
  }
  opj_free((*l_tile).comps as *mut core::ffi::c_void);
  (*l_tile).comps = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  opj_free((*(*p_tcd).tcd_image).tiles as *mut core::ffi::c_void);
  (*(*p_tcd).tcd_image).tiles = std::ptr::null_mut::<opj_tcd_tile_t>();
}
unsafe fn opj_tcd_t2_decode(
  mut p_tcd: *mut opj_tcd_t,
  mut p_src_data: *mut OPJ_BYTE,
  mut p_data_read: *mut OPJ_UINT32,
  mut p_max_src_size: OPJ_UINT32,
  mut p_cstr_index: *mut opj_codestream_index_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_t2 = std::ptr::null_mut::<opj_t2_t>();
  l_t2 = opj_t2_create((*p_tcd).image, (*p_tcd).cp);
  if l_t2.is_null() {
    return 0i32;
  }
  if opj_t2_decode_packets(
    p_tcd,
    l_t2,
    (*p_tcd).tcd_tileno,
    (*(*p_tcd).tcd_image).tiles,
    p_src_data,
    p_data_read,
    p_max_src_size,
    p_cstr_index,
    p_manager,
  ) == 0
  {
    opj_t2_destroy(l_t2);
    return 0i32;
  }
  opj_t2_destroy(l_t2);
  /*---------------CLEAN-------------------*/
  1i32
}
unsafe fn opj_tcd_t1_decode(
  mut p_tcd: *mut opj_tcd_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut l_tile = (*(*p_tcd).tcd_image).tiles;
  let mut l_tile_comp = (*l_tile).comps;
  let mut l_tccp = (*(*p_tcd).tcp).tccps;
  let mut ret = 1i32;
  let mut check_pterm = 0i32;
  let mut p_manager_mutex = std::ptr::null_mut::<opj_mutex_t>();
  p_manager_mutex = opj_mutex_create();
  /* Only enable PTERM check if we decode all layers */
  if (*(*p_tcd).tcp).num_layers_to_decode == (*(*p_tcd).tcp).numlayers
    && (*l_tccp).cblksty & 0x10u32 != 0u32
  {
    check_pterm = 1i32
  }
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    if !(!(*p_tcd).used_component.is_null()
      && *(*p_tcd).used_component.offset(compno as isize) == 0)
    {
      opj_t1_decode_cblks(
        p_tcd,
        &mut ret,
        l_tile_comp,
        l_tccp,
        p_manager,
        p_manager_mutex,
        check_pterm,
      );
      if ret == 0 {
        break;
      }
    }
    compno = compno.wrapping_add(1);
    l_tile_comp = l_tile_comp.offset(1);
    l_tccp = l_tccp.offset(1)
  }
  opj_thread_pool_wait_completion((*p_tcd).thread_pool, 0i32);
  if !p_manager_mutex.is_null() {
    opj_mutex_destroy(p_manager_mutex);
  }
  ret
}
unsafe fn opj_tcd_dwt_decode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut l_tile = (*(*p_tcd).tcd_image).tiles;
  let mut l_tile_comp = (*l_tile).comps;
  let mut l_tccp = (*(*p_tcd).tcp).tccps;
  let mut l_img_comp = (*(*p_tcd).image).comps;
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    if !(!(*p_tcd).used_component.is_null()
      && *(*p_tcd).used_component.offset(compno as isize) == 0)
    {
      if (*l_tccp).qmfbid == 1u32 {
        if opj_dwt_decode(
          p_tcd,
          l_tile_comp,
          (*l_img_comp).resno_decoded.wrapping_add(1u32),
        ) == 0
        {
          return 0i32;
        }
      } else if opj_dwt_decode_real(
        p_tcd,
        l_tile_comp,
        (*l_img_comp).resno_decoded.wrapping_add(1u32),
      ) == 0
      {
        return 0i32;
      }
    }
    compno = compno.wrapping_add(1);
    l_tile_comp = l_tile_comp.offset(1);
    l_img_comp = l_img_comp.offset(1);
    l_tccp = l_tccp.offset(1)
  }
  1i32
}
unsafe fn opj_tcd_mct_decode(
  mut p_tcd: *mut opj_tcd_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_tile = (*(*p_tcd).tcd_image).tiles;
  let mut l_tcp = (*p_tcd).tcp;
  let mut l_tile_comp = (*l_tile).comps;
  let mut l_samples: OPJ_SIZE_T = 0;
  let mut i: OPJ_UINT32 = 0;
  if (*l_tcp).mct == 0u32 || !(*p_tcd).used_component.is_null() {
    return 1i32;
  }
  if (*p_tcd).whole_tile_decoding != 0 {
    let mut res_comp0 = (*(*l_tile).comps.offset(0))
      .resolutions
      .offset((*l_tile_comp).minimum_num_resolutions as isize)
      .offset(-1);
    /* A bit inefficient: we process more data than needed if */
    /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
    /* but we would need to take into account a stride then */
    l_samples = (((*res_comp0).x1 - (*res_comp0).x0) as OPJ_SIZE_T)
      .wrapping_mul(((*res_comp0).y1 - (*res_comp0).y0) as OPJ_SIZE_T);
    if (*l_tile).numcomps >= 3u32
      && ((*l_tile_comp).minimum_num_resolutions
        != (*(*l_tile).comps.offset(1)).minimum_num_resolutions
        || (*l_tile_comp).minimum_num_resolutions
          != (*(*l_tile).comps.offset(2)).minimum_num_resolutions)
    {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Tiles don\'t all have the same dimension. Skip the MCT step.\n",
      );
      return 0i32;
    }
    if (*l_tile).numcomps >= 3u32 {
      let mut res_comp1 = (*(*l_tile).comps.offset(1))
        .resolutions
        .offset((*l_tile_comp).minimum_num_resolutions as isize)
        .offset(-1);
      let mut res_comp2 = (*(*l_tile).comps.offset(2))
        .resolutions
        .offset((*l_tile_comp).minimum_num_resolutions as isize)
        .offset(-1);
      /* testcase 1336.pdf.asan.47.376 */
      if (*(*(*p_tcd).image).comps.offset(0)).resno_decoded
        != (*(*(*p_tcd).image).comps.offset(1)).resno_decoded
        || (*(*(*p_tcd).image).comps.offset(0)).resno_decoded
          != (*(*(*p_tcd).image).comps.offset(2)).resno_decoded
        || (((*res_comp1).x1 - (*res_comp1).x0) as OPJ_SIZE_T)
          .wrapping_mul(((*res_comp1).y1 - (*res_comp1).y0) as OPJ_SIZE_T)
          != l_samples
        || (((*res_comp2).x1 - (*res_comp2).x0) as OPJ_SIZE_T)
          .wrapping_mul(((*res_comp2).y1 - (*res_comp2).y0) as OPJ_SIZE_T)
          != l_samples
      {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Tiles don\'t all have the same dimension. Skip the MCT step.\n",
        );
        return 0i32;
      }
    }
  } else {
    let mut res_comp0_0 = (*(*l_tile).comps.offset(0))
      .resolutions
      .offset((*(*(*p_tcd).image).comps.offset(0)).resno_decoded as isize);
    l_samples = ((*res_comp0_0).win_x1.wrapping_sub((*res_comp0_0).win_x0) as OPJ_SIZE_T)
      .wrapping_mul((*res_comp0_0).win_y1.wrapping_sub((*res_comp0_0).win_y0) as OPJ_SIZE_T);
    if (*l_tile).numcomps >= 3u32 {
      let mut res_comp1_0 = (*(*l_tile).comps.offset(1))
        .resolutions
        .offset((*(*(*p_tcd).image).comps.offset(1)).resno_decoded as isize);
      let mut res_comp2_0 = (*(*l_tile).comps.offset(2))
        .resolutions
        .offset((*(*(*p_tcd).image).comps.offset(2)).resno_decoded as isize);
      /* testcase 1336.pdf.asan.47.376 */
      if (*(*(*p_tcd).image).comps.offset(0)).resno_decoded
        != (*(*(*p_tcd).image).comps.offset(1)).resno_decoded
        || (*(*(*p_tcd).image).comps.offset(0)).resno_decoded
          != (*(*(*p_tcd).image).comps.offset(2)).resno_decoded
        || ((*res_comp1_0).win_x1.wrapping_sub((*res_comp1_0).win_x0) as OPJ_SIZE_T)
          .wrapping_mul((*res_comp1_0).win_y1.wrapping_sub((*res_comp1_0).win_y0) as OPJ_SIZE_T)
          != l_samples
        || ((*res_comp2_0).win_x1.wrapping_sub((*res_comp2_0).win_x0) as OPJ_SIZE_T)
          .wrapping_mul((*res_comp2_0).win_y1.wrapping_sub((*res_comp2_0).win_y0) as OPJ_SIZE_T)
          != l_samples
      {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Tiles don\'t all have the same dimension. Skip the MCT step.\n",
        );
        return 0i32;
      }
    }
  }
  if (*l_tile).numcomps >= 3u32 {
    if (*l_tcp).mct == 2u32 {
      let mut l_data = std::ptr::null_mut::<*mut OPJ_BYTE>();
      if (*l_tcp).m_mct_decoding_matrix.is_null() {
        return 1i32;
      }
      l_data = opj_malloc(
        ((*l_tile).numcomps as usize).wrapping_mul(core::mem::size_of::<*mut OPJ_BYTE>()),
      ) as *mut *mut OPJ_BYTE;
      if l_data.is_null() {
        return 0i32;
      }
      i = 0 as OPJ_UINT32;
      while i < (*l_tile).numcomps {
        if (*p_tcd).whole_tile_decoding != 0 {
          let fresh7 = &mut (*l_data.offset(i as isize));
          *fresh7 = (*l_tile_comp).data as *mut OPJ_BYTE
        } else {
          let fresh8 = &mut (*l_data.offset(i as isize));
          *fresh8 = (*l_tile_comp).data_win as *mut OPJ_BYTE
        }
        l_tile_comp = l_tile_comp.offset(1);
        i += 1;
      }
      if opj_mct_decode_custom(
        (*l_tcp).m_mct_decoding_matrix as *mut OPJ_BYTE,
        l_samples,
        l_data,
        (*l_tile).numcomps,
        (*(*(*p_tcd).image).comps).sgnd,
      ) == 0
      {
        opj_free(l_data as *mut core::ffi::c_void);
        return 0i32;
      }
      opj_free(l_data as *mut core::ffi::c_void);
    } else if (*(*l_tcp).tccps).qmfbid == 1u32 {
      if (*p_tcd).whole_tile_decoding != 0 {
        opj_mct_decode(
          (*(*l_tile).comps.offset(0)).data,
          (*(*l_tile).comps.offset(1)).data,
          (*(*l_tile).comps.offset(2)).data,
          l_samples,
        );
      } else {
        opj_mct_decode(
          (*(*l_tile).comps.offset(0)).data_win,
          (*(*l_tile).comps.offset(1)).data_win,
          (*(*l_tile).comps.offset(2)).data_win,
          l_samples,
        );
      }
    } else if (*p_tcd).whole_tile_decoding != 0 {
      opj_mct_decode_real(
        (*(*l_tile).comps.offset(0)).data as *mut OPJ_FLOAT32,
        (*(*l_tile).comps.offset(1)).data as *mut OPJ_FLOAT32,
        (*(*l_tile).comps.offset(2)).data as *mut OPJ_FLOAT32,
        l_samples,
      );
    } else {
      opj_mct_decode_real(
        (*(*l_tile).comps.offset(0)).data_win as *mut OPJ_FLOAT32,
        (*(*l_tile).comps.offset(1)).data_win as *mut OPJ_FLOAT32,
        (*(*l_tile).comps.offset(2)).data_win as *mut OPJ_FLOAT32,
        l_samples,
      );
    }
  } else {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
      (*l_tile).numcomps,
    );
  }
  1i32
}
unsafe fn opj_tcd_dc_level_shift_decode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut l_tile_comp = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_tccp = std::ptr::null_mut::<opj_tccp_t>();
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_res = std::ptr::null_mut::<opj_tcd_resolution_t>();
  let mut l_tile = std::ptr::null_mut::<opj_tcd_tile_t>();
  let mut l_width: OPJ_UINT32 = 0;
  let mut l_height: OPJ_UINT32 = 0;
  let mut i: OPJ_UINT32 = 0;
  let mut j: OPJ_UINT32 = 0;
  let mut l_current_ptr = std::ptr::null_mut::<OPJ_INT32>();
  let mut l_min: OPJ_INT32 = 0;
  let mut l_max: OPJ_INT32 = 0;
  let mut l_stride: OPJ_UINT32 = 0;
  l_tile = (*(*p_tcd).tcd_image).tiles;
  l_tile_comp = (*l_tile).comps;
  l_tccp = (*(*p_tcd).tcp).tccps;
  l_img_comp = (*(*p_tcd).image).comps;
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    if !(!(*p_tcd).used_component.is_null()
      && *(*p_tcd).used_component.offset(compno as isize) == 0)
    {
      l_res = (*l_tile_comp)
        .resolutions
        .offset((*l_img_comp).resno_decoded as isize);
      if (*p_tcd).whole_tile_decoding == 0 {
        l_width = (*l_res).win_x1.wrapping_sub((*l_res).win_x0);
        l_height = (*l_res).win_y1.wrapping_sub((*l_res).win_y0);
        l_stride = 0 as OPJ_UINT32;
        l_current_ptr = (*l_tile_comp).data_win
      } else {
        l_width = ((*l_res).x1 - (*l_res).x0) as OPJ_UINT32;
        l_height = ((*l_res).y1 - (*l_res).y0) as OPJ_UINT32;
        l_stride = (((*(*l_tile_comp)
          .resolutions
          .offset((*l_tile_comp).minimum_num_resolutions.wrapping_sub(1u32) as isize))
        .x1
          - (*(*l_tile_comp)
            .resolutions
            .offset((*l_tile_comp).minimum_num_resolutions.wrapping_sub(1u32) as isize))
          .x0) as OPJ_UINT32)
          .wrapping_sub(l_width);
        l_current_ptr = (*l_tile_comp).data;
        assert!(
          l_height == 0u32
            || l_width.wrapping_add(l_stride) as usize
              <= (*l_tile_comp).data_size.wrapping_div(l_height as usize)
        );
        /*MUPDF*/
      }

      if l_width != 0 && l_height != 0 {
        if (*l_img_comp).sgnd != 0 {
          l_min = -((1i32) << (*l_img_comp).prec.wrapping_sub(1u32));
          l_max = ((1i32) << (*l_img_comp).prec.wrapping_sub(1u32)) - 1i32
        } else {
          l_min = 0i32;
          l_max = ((1u32) << (*l_img_comp).prec).wrapping_sub(1u32) as OPJ_INT32
        }

        if (*l_tccp).qmfbid == 1u32 {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            i = 0 as OPJ_UINT32;
            while i < l_width {
              /* TODO: do addition on int64 ? */
              *l_current_ptr =
                opj_int_clamp(*l_current_ptr + (*l_tccp).m_dc_level_shift, l_min, l_max);
              l_current_ptr = l_current_ptr.offset(1);
              i += 1;
            }
            l_current_ptr = l_current_ptr.offset(l_stride as isize);
            j += 1;
          }
        } else {
          j = 0 as OPJ_UINT32;
          while j < l_height {
            i = 0 as OPJ_UINT32;
            while i < l_width {
              let mut l_value = *(l_current_ptr as *mut OPJ_FLOAT32);
              if l_value > 2147483647 as core::ffi::c_float {
                *l_current_ptr = l_max
              } else if l_value < (-(2147483647i32) - 1i32) as core::ffi::c_float {
                *l_current_ptr = l_min
              } else {
                /* Do addition on int64 to avoid overflows */
                let mut l_value_int = opj_lrintf(l_value);
                *l_current_ptr = opj_int64_clamp(
                  l_value_int + (*l_tccp).m_dc_level_shift as i64,
                  l_min as OPJ_INT64,
                  l_max as OPJ_INT64,
                ) as OPJ_INT32
              }
              l_current_ptr = l_current_ptr.offset(1);
              i += 1;
            }
            l_current_ptr = l_current_ptr.offset(l_stride as isize);
            j += 1;
          }
        }
      }
    }
    compno = compno.wrapping_add(1);
    l_img_comp = l_img_comp.offset(1);
    l_tccp = l_tccp.offset(1);
    l_tile_comp = l_tile_comp.offset(1)
  }
  1i32
}
/* *
 * Deallocates the decoding data of the given precinct.
 */
/* *
 * Deallocates the encoding data of the given precinct.
 */
unsafe fn opj_tcd_code_block_dec_deallocate(mut p_precinct: *mut opj_tcd_precinct_t) {
  let mut cblkno: OPJ_UINT32 = 0;
  let mut l_nb_code_blocks: OPJ_UINT32 = 0;
  let mut l_code_block = (*p_precinct).cblks.dec;
  if !l_code_block.is_null() {
    /*fprintf(stderr,"deallocate codeblock:{\n");*/
    /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
    /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
    l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
    l_nb_code_blocks = (*p_precinct)
      .block_size
      .wrapping_div(core::mem::size_of::<opj_tcd_cblk_dec_t>() as OPJ_UINT32);
    /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
    cblkno = 0 as OPJ_UINT32;
    while cblkno < l_nb_code_blocks {
      if !(*l_code_block).segs.is_null() {
        opj_free((*l_code_block).segs as *mut core::ffi::c_void);
        (*l_code_block).segs = std::ptr::null_mut::<opj_tcd_seg_t>()
      }
      if !(*l_code_block).chunks.is_null() {
        opj_free((*l_code_block).chunks as *mut core::ffi::c_void);
        (*l_code_block).chunks = std::ptr::null_mut::<opj_tcd_seg_data_chunk_t>()
      }
      opj_aligned_free((*l_code_block).decoded_data as *mut core::ffi::c_void);
      (*l_code_block).decoded_data = std::ptr::null_mut::<OPJ_INT32>();
      l_code_block = l_code_block.offset(1);
      cblkno += 1;
    }
    opj_free((*p_precinct).cblks.dec as *mut core::ffi::c_void);
    (*p_precinct).cblks.dec = std::ptr::null_mut::<opj_tcd_cblk_dec_t>()
  };
}
/* *
 * Deallocates the encoding data of the given precinct.
 */
/* *
 * Deallocates the encoding data of the given precinct.
 */
unsafe fn opj_tcd_code_block_enc_deallocate(mut p_precinct: *mut opj_tcd_precinct_t) {
  let mut cblkno: OPJ_UINT32 = 0;
  let mut l_nb_code_blocks: OPJ_UINT32 = 0;
  let mut l_code_block = (*p_precinct).cblks.enc;
  if !l_code_block.is_null() {
    l_nb_code_blocks = (*p_precinct)
      .block_size
      .wrapping_div(core::mem::size_of::<opj_tcd_cblk_enc_t>() as OPJ_UINT32);
    cblkno = 0 as OPJ_UINT32;
    while cblkno < l_nb_code_blocks {
      if !(*l_code_block).data.is_null() {
        /* We refer to data - 1 since below we incremented it */
        /* in opj_tcd_code_block_enc_allocate_data() */
        opj_free((*l_code_block).data.offset(-1) as *mut core::ffi::c_void); /*(/ 8)*/
        (*l_code_block).data = std::ptr::null_mut::<OPJ_BYTE>()
      } /* (%8) */
      if !(*l_code_block).layers.is_null() {
        opj_free((*l_code_block).layers as *mut core::ffi::c_void);
        (*l_code_block).layers = std::ptr::null_mut::<opj_tcd_layer_t>()
      }
      if !(*l_code_block).passes.is_null() {
        opj_free((*l_code_block).passes as *mut core::ffi::c_void);
        (*l_code_block).passes = std::ptr::null_mut::<opj_tcd_pass_t>()
      }
      l_code_block = l_code_block.offset(1);
      cblkno += 1;
    }
    opj_free((*p_precinct).cblks.enc as *mut core::ffi::c_void);
    (*p_precinct).cblks.enc = std::ptr::null_mut::<opj_tcd_cblk_enc_t>()
  };
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_get_encoder_input_buffer_size(
  mut p_tcd: *mut opj_tcd_t,
) -> OPJ_SIZE_T {
  let mut i: OPJ_UINT32 = 0;
  let mut l_data_size = 0 as OPJ_SIZE_T;
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_tilec = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_size_comp: OPJ_UINT32 = 0;
  let mut l_remaining: OPJ_UINT32 = 0;
  l_tilec = (*(*(*p_tcd).tcd_image).tiles).comps;
  l_img_comp = (*(*p_tcd).image).comps;
  i = 0 as OPJ_UINT32;
  while i < (*(*p_tcd).image).numcomps {
    l_size_comp = (*l_img_comp).prec >> 3i32;
    l_remaining = (*l_img_comp).prec & 7u32;
    if l_remaining != 0 {
      l_size_comp += 1;
    }
    if l_size_comp == 3u32 {
      l_size_comp = 4 as OPJ_UINT32
    }
    l_data_size = (l_data_size as usize).wrapping_add(
      (l_size_comp as usize).wrapping_mul(
        (((*l_tilec).x1 - (*l_tilec).x0) as OPJ_SIZE_T)
          .wrapping_mul(((*l_tilec).y1 - (*l_tilec).y0) as OPJ_SIZE_T),
      ),
    ) as OPJ_SIZE_T as OPJ_SIZE_T;
    l_img_comp = l_img_comp.offset(1);
    l_tilec = l_tilec.offset(1);
    i += 1;
  }
  l_data_size
}
unsafe fn opj_tcd_dc_level_shift_encode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut compno: OPJ_UINT32 = 0;
  let mut l_tile_comp = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_tccp = std::ptr::null_mut::<opj_tccp_t>();
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_tile = std::ptr::null_mut::<opj_tcd_tile_t>();
  let mut l_nb_elem: OPJ_SIZE_T = 0;
  let mut i: OPJ_SIZE_T = 0;
  let mut l_current_ptr = std::ptr::null_mut::<OPJ_INT32>();
  l_tile = (*(*p_tcd).tcd_image).tiles;
  l_tile_comp = (*l_tile).comps;
  l_tccp = (*(*p_tcd).tcp).tccps;
  l_img_comp = (*(*p_tcd).image).comps;
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    l_current_ptr = (*l_tile_comp).data;
    l_nb_elem = (((*l_tile_comp).x1 - (*l_tile_comp).x0) as OPJ_SIZE_T)
      .wrapping_mul(((*l_tile_comp).y1 - (*l_tile_comp).y0) as OPJ_SIZE_T);
    if (*l_tccp).qmfbid == 1u32 {
      i = 0 as OPJ_SIZE_T;
      while i < l_nb_elem {
        *l_current_ptr -= (*l_tccp).m_dc_level_shift;
        l_current_ptr = l_current_ptr.offset(1);
        i += 1;
      }
    } else {
      i = 0 as OPJ_SIZE_T;
      while i < l_nb_elem {
        *(l_current_ptr as *mut OPJ_FLOAT32) =
          (*l_current_ptr - (*l_tccp).m_dc_level_shift) as OPJ_FLOAT32;
        l_current_ptr = l_current_ptr.offset(1);
        i += 1;
      }
    }
    l_img_comp = l_img_comp.offset(1);
    l_tccp = l_tccp.offset(1);
    l_tile_comp = l_tile_comp.offset(1);
    compno += 1;
  }
  1i32
}
unsafe fn opj_tcd_mct_encode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut l_tile = (*(*p_tcd).tcd_image).tiles;
  let mut l_tile_comp = (*(*(*p_tcd).tcd_image).tiles).comps;
  let mut samples = (((*l_tile_comp).x1 - (*l_tile_comp).x0) as OPJ_SIZE_T)
    .wrapping_mul(((*l_tile_comp).y1 - (*l_tile_comp).y0) as OPJ_SIZE_T);
  let mut i: OPJ_UINT32 = 0;
  let mut l_data = std::ptr::null_mut::<*mut OPJ_BYTE>();
  let mut l_tcp = (*p_tcd).tcp;
  if (*(*p_tcd).tcp).mct == 0 {
    return 1i32;
  }
  if (*(*p_tcd).tcp).mct == 2u32 {
    if (*(*p_tcd).tcp).m_mct_coding_matrix.is_null() {
      return 1i32;
    }
    l_data =
      opj_malloc(((*l_tile).numcomps as usize).wrapping_mul(core::mem::size_of::<*mut OPJ_BYTE>()))
        as *mut *mut OPJ_BYTE;
    if l_data.is_null() {
      return 0i32;
    }
    i = 0 as OPJ_UINT32;
    while i < (*l_tile).numcomps {
      let fresh9 = &mut (*l_data.offset(i as isize));
      *fresh9 = (*l_tile_comp).data as *mut OPJ_BYTE;
      l_tile_comp = l_tile_comp.offset(1);
      i += 1;
    }
    if opj_mct_encode_custom(
      (*(*p_tcd).tcp).m_mct_coding_matrix as *mut OPJ_BYTE,
      samples,
      l_data,
      (*l_tile).numcomps,
      (*(*(*p_tcd).image).comps).sgnd,
    ) == 0
    {
      opj_free(l_data as *mut core::ffi::c_void);
      return 0i32;
    }
    opj_free(l_data as *mut core::ffi::c_void);
  } else if (*(*l_tcp).tccps).qmfbid == 0u32 {
    opj_mct_encode_real(
      (*(*l_tile).comps.offset(0)).data as *mut OPJ_FLOAT32,
      (*(*l_tile).comps.offset(1)).data as *mut OPJ_FLOAT32,
      (*(*l_tile).comps.offset(2)).data as *mut OPJ_FLOAT32,
      samples,
    );
  } else {
    opj_mct_encode(
      (*(*l_tile).comps.offset(0)).data,
      (*(*l_tile).comps.offset(1)).data,
      (*(*l_tile).comps.offset(2)).data,
      samples,
    );
  }
  1i32
}
unsafe fn opj_tcd_dwt_encode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut l_tile = (*(*p_tcd).tcd_image).tiles;
  let mut l_tile_comp = (*(*(*p_tcd).tcd_image).tiles).comps;
  let mut l_tccp = (*(*p_tcd).tcp).tccps;
  let mut compno: OPJ_UINT32 = 0;
  compno = 0 as OPJ_UINT32;
  while compno < (*l_tile).numcomps {
    if (*l_tccp).qmfbid == 1u32 {
      if opj_dwt_encode(p_tcd, l_tile_comp) == 0 {
        return 0i32;
      }
    } else if (*l_tccp).qmfbid == 0u32 && opj_dwt_encode_real(p_tcd, l_tile_comp) == 0 {
      return 0i32;
    }
    l_tile_comp = l_tile_comp.offset(1);
    l_tccp = l_tccp.offset(1);
    compno += 1;
  }
  1i32
}
unsafe fn opj_tcd_t1_encode(mut p_tcd: *mut opj_tcd_t) -> OPJ_BOOL {
  let mut l_mct_norms = std::ptr::null::<OPJ_FLOAT64>();
  let mut l_mct_numcomps = 0u32;
  let mut l_tcp = (*p_tcd).tcp;
  if (*l_tcp).mct == 1u32 {
    l_mct_numcomps = 3u32;
    /* irreversible encoding */
    if (*(*l_tcp).tccps).qmfbid == 0u32 {
      l_mct_norms = opj_mct_get_mct_norms_real()
    } else {
      l_mct_norms = opj_mct_get_mct_norms()
    }
  } else {
    l_mct_numcomps = (*(*p_tcd).image).numcomps;
    l_mct_norms = (*l_tcp).mct_norms as *const OPJ_FLOAT64
  }
  opj_t1_encode_cblks(
    p_tcd,
    (*(*p_tcd).tcd_image).tiles,
    l_tcp,
    l_mct_norms,
    l_mct_numcomps,
  )
}
unsafe fn opj_tcd_t2_encode(
  mut p_tcd: *mut opj_tcd_t,
  mut p_dest_data: *mut OPJ_BYTE,
  mut p_data_written: *mut OPJ_UINT32,
  mut p_max_dest_size: OPJ_UINT32,
  mut p_cstr_info: *mut opj_codestream_info_t,
  mut p_marker_info: *mut opj_tcd_marker_info_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_t2 = std::ptr::null_mut::<opj_t2_t>();
  l_t2 = opj_t2_create((*p_tcd).image, (*p_tcd).cp);
  if l_t2.is_null() {
    return 0i32;
  }
  if opj_t2_encode_packets(
    l_t2,
    (*p_tcd).tcd_tileno,
    (*(*p_tcd).tcd_image).tiles,
    (*(*p_tcd).tcp).numlayers,
    p_dest_data,
    p_data_written,
    p_max_dest_size,
    p_cstr_info,
    p_marker_info,
    (*p_tcd).tp_num,
    (*p_tcd).tp_pos,
    (*p_tcd).cur_pino,
    FINAL_PASS,
    p_manager,
  ) == 0
  {
    opj_t2_destroy(l_t2);
    return 0i32;
  }
  opj_t2_destroy(l_t2);
  /*---------------CLEAN-------------------*/
  1i32
}
unsafe fn opj_tcd_rate_allocate_encode(
  mut p_tcd: *mut opj_tcd_t,
  mut p_dest_data: *mut OPJ_BYTE,
  mut p_max_dest_size: OPJ_UINT32,
  mut p_cstr_info: *mut opj_codestream_info_t,
  mut p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_cp = (*p_tcd).cp;
  let mut l_nb_written = 0 as OPJ_UINT32;
  if !p_cstr_info.is_null() {
    (*p_cstr_info).index_write = 0i32
  }

  if (*l_cp)
    .m_specific_param
    .m_enc
    .m_quality_layer_alloc_strategy
    == J2K_QUALITY_LAYER_ALLOCATION_STRATEGY::RATE_DISTORTION_RATIO
    || (*l_cp)
      .m_specific_param
      .m_enc
      .m_quality_layer_alloc_strategy
      == J2K_QUALITY_LAYER_ALLOCATION_STRATEGY::FIXED_DISTORTION_RATIO
  {
    if opj_tcd_rateallocate(
      p_tcd,
      p_dest_data,
      &mut l_nb_written,
      p_max_dest_size,
      p_cstr_info,
      p_manager,
    ) == 0
    {
      return 0i32;
    }
  } else {
    /* Fixed layer allocation */
    opj_tcd_rateallocate_fixed(p_tcd); /*(/ 8)*/
  } /* (%8) */
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_copy_tile_data(
  mut p_tcd: *mut opj_tcd_t,
  mut p_src: *mut OPJ_BYTE,
  mut p_src_length: OPJ_SIZE_T,
) -> OPJ_BOOL {
  let mut i: OPJ_UINT32 = 0;
  let mut j: OPJ_SIZE_T = 0;
  let mut l_data_size = 0 as OPJ_SIZE_T;
  let mut l_img_comp = std::ptr::null_mut::<opj_image_comp_t>();
  let mut l_tilec = std::ptr::null_mut::<opj_tcd_tilecomp_t>();
  let mut l_size_comp: OPJ_UINT32 = 0;
  let mut l_remaining: OPJ_UINT32 = 0;
  let mut l_nb_elem: OPJ_SIZE_T = 0;
  l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd);
  if l_data_size != p_src_length {
    return 0i32;
  }
  l_tilec = (*(*(*p_tcd).tcd_image).tiles).comps;
  l_img_comp = (*(*p_tcd).image).comps;
  i = 0 as OPJ_UINT32;
  while i < (*(*p_tcd).image).numcomps {
    l_size_comp = (*l_img_comp).prec >> 3i32;
    l_remaining = (*l_img_comp).prec & 7u32;
    l_nb_elem = (((*l_tilec).x1 - (*l_tilec).x0) as OPJ_SIZE_T)
      .wrapping_mul(((*l_tilec).y1 - (*l_tilec).y0) as OPJ_SIZE_T);
    if l_remaining != 0 {
      l_size_comp += 1;
    }
    if l_size_comp == 3u32 {
      l_size_comp = 4 as OPJ_UINT32
    }
    match l_size_comp {
      1 => {
        let mut l_src_ptr = p_src as *mut OPJ_CHAR;
        let mut l_dest_ptr = (*l_tilec).data;
        if (*l_img_comp).sgnd != 0 {
          j = 0 as OPJ_SIZE_T;
          while j < l_nb_elem {
            let fresh10 = l_src_ptr;
            l_src_ptr = l_src_ptr.offset(1);
            let fresh11 = l_dest_ptr;
            l_dest_ptr = l_dest_ptr.offset(1);
            *fresh11 = *fresh10 as OPJ_INT32;
            j += 1;
          }
        } else {
          j = 0 as OPJ_SIZE_T;
          while j < l_nb_elem {
            let fresh12 = l_src_ptr;
            l_src_ptr = l_src_ptr.offset(1);
            let fresh13 = l_dest_ptr;
            l_dest_ptr = l_dest_ptr.offset(1);
            *fresh13 = *fresh12 as core::ffi::c_int & 0xffi32;
            j += 1;
          }
        }
        p_src = l_src_ptr as *mut OPJ_BYTE
      }
      2 => {
        let mut l_dest_ptr_0 = (*l_tilec).data;
        let mut l_src_ptr_0 = p_src as *mut OPJ_INT16;
        if (*l_img_comp).sgnd != 0 {
          j = 0 as OPJ_SIZE_T;
          while j < l_nb_elem {
            let fresh14 = l_src_ptr_0;
            l_src_ptr_0 = l_src_ptr_0.offset(1);
            let fresh15 = l_dest_ptr_0;
            l_dest_ptr_0 = l_dest_ptr_0.offset(1);
            *fresh15 = *fresh14 as OPJ_INT32;
            j += 1;
          }
        } else {
          j = 0 as OPJ_SIZE_T;
          while j < l_nb_elem {
            let fresh16 = l_src_ptr_0;
            l_src_ptr_0 = l_src_ptr_0.offset(1);
            let fresh17 = l_dest_ptr_0;
            l_dest_ptr_0 = l_dest_ptr_0.offset(1);
            *fresh17 = *fresh16 as core::ffi::c_int & 0xffffi32;
            j += 1;
          }
        }
        p_src = l_src_ptr_0 as *mut OPJ_BYTE
      }
      4 => {
        let mut l_src_ptr_1 = p_src as *mut OPJ_INT32;
        let mut l_dest_ptr_1 = (*l_tilec).data;
        j = 0 as OPJ_SIZE_T;
        while j < l_nb_elem {
          let fresh18 = l_src_ptr_1;
          l_src_ptr_1 = l_src_ptr_1.offset(1);
          let fresh19 = l_dest_ptr_1;
          l_dest_ptr_1 = l_dest_ptr_1.offset(1);
          *fresh19 = *fresh18;
          j += 1;
        }
        p_src = l_src_ptr_1 as *mut OPJ_BYTE
      }
      _ => {}
    }
    l_img_comp = l_img_comp.offset(1);
    l_tilec = l_tilec.offset(1);
    i += 1;
  }
  1i32
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_is_band_empty(mut band: *mut opj_tcd_band_t) -> OPJ_BOOL {
  ((*band).x1 - (*band).x0 == 0i32 || (*band).y1 - (*band).y0 == 0i32) as core::ffi::c_int
}
#[no_mangle]
pub(crate) unsafe fn opj_tcd_is_subband_area_of_interest(
  mut tcd: *mut opj_tcd_t,
  mut compno: OPJ_UINT32,
  mut resno: OPJ_UINT32,
  mut bandno: OPJ_UINT32,
  mut band_x0: OPJ_UINT32,
  mut band_y0: OPJ_UINT32,
  mut band_x1: OPJ_UINT32,
  mut band_y1: OPJ_UINT32,
) -> OPJ_BOOL {
  /* Note: those values for filter_margin are in part the result of */
  /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
  /* to the maximum left/right extension given in tables F.2 and F.3 of the */
  /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
  /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
  /* needed to be bumped to 4, in case inconsistencies are found while */
  /* decoding parts of irreversible coded images. */
  /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
  let mut filter_margin = if (*(*(*tcd).tcp).tccps.offset(compno as isize)).qmfbid == 1u32 {
    2i32
  } else {
    3i32
  } as OPJ_UINT32;
  let mut tilec: *mut opj_tcd_tilecomp_t =
    &mut *(*(*(*tcd).tcd_image).tiles).comps.offset(compno as isize) as *mut opj_tcd_tilecomp_t;
  let mut image_comp: *mut opj_image_comp_t =
    &mut *(*(*tcd).image).comps.offset(compno as isize) as *mut opj_image_comp_t;
  /* Compute the intersection of the area of interest, expressed in tile coordinates */
  /* with the tile coordinates */
  let mut tcx0 = opj_uint_max(
    (*tilec).x0 as OPJ_UINT32,
    opj_uint_ceildiv((*tcd).win_x0, (*image_comp).dx),
  );
  let mut tcy0 = opj_uint_max(
    (*tilec).y0 as OPJ_UINT32,
    opj_uint_ceildiv((*tcd).win_y0, (*image_comp).dy),
  );
  let mut tcx1 = opj_uint_min(
    (*tilec).x1 as OPJ_UINT32,
    opj_uint_ceildiv((*tcd).win_x1, (*image_comp).dx),
  );
  let mut tcy1 = opj_uint_min(
    (*tilec).y1 as OPJ_UINT32,
    opj_uint_ceildiv((*tcd).win_y1, (*image_comp).dy),
  );
  /* Compute number of decomposition for this band. See table F-1 */
  let mut nb = if resno == 0u32 {
    (*tilec).numresolutions.wrapping_sub(1u32)
  } else {
    (*tilec).numresolutions.wrapping_sub(resno)
  };
  /* Map above tile-based coordinates to sub-band-based coordinates per */
  /* equation B-15 of the standard */
  let mut x0b = bandno & 1u32;
  let mut y0b = bandno >> 1i32;
  let mut tbx0 = if nb == 0u32 {
    tcx0
  } else if tcx0 <= ((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(x0b) {
    0u32
  } else {
    opj_uint_ceildivpow2(
      tcx0.wrapping_sub(((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(x0b)),
      nb,
    )
  };
  let mut tby0 = if nb == 0u32 {
    tcy0
  } else if tcy0 <= ((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(y0b) {
    0u32
  } else {
    opj_uint_ceildivpow2(
      tcy0.wrapping_sub(((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(y0b)),
      nb,
    )
  };
  let mut tbx1 = if nb == 0u32 {
    tcx1
  } else if tcx1 <= ((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(x0b) {
    0u32
  } else {
    opj_uint_ceildivpow2(
      tcx1.wrapping_sub(((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(x0b)),
      nb,
    )
  };
  let mut tby1 = if nb == 0u32 {
    tcy1
  } else if tcy1 <= ((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(y0b) {
    0u32
  } else {
    opj_uint_ceildivpow2(
      tcy1.wrapping_sub(((1u32) << nb.wrapping_sub(1u32)).wrapping_mul(y0b)),
      nb,
    )
  };
  let mut intersects: OPJ_BOOL = 0;
  if tbx0 < filter_margin {
    tbx0 = 0 as OPJ_UINT32
  } else {
    tbx0 = (tbx0 as core::ffi::c_uint).wrapping_sub(filter_margin) as OPJ_UINT32
  }
  if tby0 < filter_margin {
    tby0 = 0 as OPJ_UINT32
  } else {
    tby0 = (tby0 as core::ffi::c_uint).wrapping_sub(filter_margin) as OPJ_UINT32
  }
  tbx1 = opj_uint_adds(tbx1, filter_margin);
  tby1 = opj_uint_adds(tby1, filter_margin);
  intersects =
    (band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 && band_y1 > tby0) as core::ffi::c_int;
  intersects
}
/* * Returns whether a tile componenent is fully decoded, taking into account
 * p_tcd->win_* members.
 *
 * @param p_tcd    TCD handle.
 * @param compno Component number
 * @return OPJ_TRUE whether the tile componenent is fully decoded
 */
unsafe fn opj_tcd_is_whole_tilecomp_decoding(
  mut p_tcd: *mut opj_tcd_t,
  mut compno: OPJ_UINT32,
) -> OPJ_BOOL {
  let mut tilec: *mut opj_tcd_tilecomp_t =
    &mut *(*(*(*p_tcd).tcd_image).tiles).comps.offset(compno as isize) as *mut opj_tcd_tilecomp_t;
  let mut image_comp: *mut opj_image_comp_t =
    &mut *(*(*p_tcd).image).comps.offset(compno as isize) as *mut opj_image_comp_t;
  /* Compute the intersection of the area of interest, expressed in tile coordinates */
  /* with the tile coordinates */
  let mut tcx0 = opj_uint_max(
    (*tilec).x0 as OPJ_UINT32,
    opj_uint_ceildiv((*p_tcd).win_x0, (*image_comp).dx),
  );
  let mut tcy0 = opj_uint_max(
    (*tilec).y0 as OPJ_UINT32,
    opj_uint_ceildiv((*p_tcd).win_y0, (*image_comp).dy),
  );
  let mut tcx1 = opj_uint_min(
    (*tilec).x1 as OPJ_UINT32,
    opj_uint_ceildiv((*p_tcd).win_x1, (*image_comp).dx),
  );
  let mut tcy1 = opj_uint_min(
    (*tilec).y1 as OPJ_UINT32,
    opj_uint_ceildiv((*p_tcd).win_y1, (*image_comp).dy),
  );
  let mut shift = (*tilec)
    .numresolutions
    .wrapping_sub((*tilec).minimum_num_resolutions);
  /* Tolerate small margin within the reduced resolution factor to consider if */
  /* the whole tile path must be taken */
  (tcx0 >= (*tilec).x0 as OPJ_UINT32
    && tcy0 >= (*tilec).y0 as OPJ_UINT32
    && tcx1 <= (*tilec).x1 as OPJ_UINT32
    && tcy1 <= (*tilec).y1 as OPJ_UINT32
    && (shift >= 32u32
      || tcx0.wrapping_sub((*tilec).x0 as OPJ_UINT32) >> shift == 0u32
        && tcy0.wrapping_sub((*tilec).y0 as OPJ_UINT32) >> shift == 0u32
        && ((*tilec).x1 as OPJ_UINT32).wrapping_sub(tcx1) >> shift == 0u32
        && ((*tilec).y1 as OPJ_UINT32).wrapping_sub(tcy1) >> shift == 0u32)) as core::ffi::c_int
}
/* ----------------------------------------------------------------------- */
#[no_mangle]
pub(crate) unsafe fn opj_tcd_marker_info_create(
  mut need_PLT: OPJ_BOOL,
) -> *mut opj_tcd_marker_info_t {
  let mut l_tcd_marker_info = opj_calloc(
    1i32 as size_t,
    core::mem::size_of::<opj_tcd_marker_info_t>(),
  ) as *mut opj_tcd_marker_info_t;
  if l_tcd_marker_info.is_null() {
    return std::ptr::null_mut::<opj_tcd_marker_info_t>();
  }
  (*l_tcd_marker_info).need_PLT = need_PLT;
  l_tcd_marker_info
}
/* ----------------------------------------------------------------------- */
#[no_mangle]
pub(crate) unsafe fn opj_tcd_marker_info_destroy(
  mut p_tcd_marker_info: *mut opj_tcd_marker_info_t,
) {
  if !p_tcd_marker_info.is_null() {
    opj_free((*p_tcd_marker_info).p_packet_size as *mut core::ffi::c_void);
    opj_free(p_tcd_marker_info as *mut core::ffi::c_void);
  };
}
/* ----------------------------------------------------------------------- */