openjp2 0.6.1

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
use alloc::collections::BTreeSet;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io::{Read, Write};

use super::consts::*;
use super::event::*;
use super::j2k::*;
use super::openjpeg::*;
use super::stream::*;

pub type C2RustUnnamed_2 = core::ffi::c_uint;
pub const JP2_STATE_UNKNOWN: C2RustUnnamed_2 = 2147483647;
pub const JP2_STATE_END_CODESTREAM: C2RustUnnamed_2 = 16;
pub const JP2_STATE_CODESTREAM: C2RustUnnamed_2 = 8;
pub const JP2_STATE_HEADER: C2RustUnnamed_2 = 4;
pub const JP2_STATE_FILE_TYPE: C2RustUnnamed_2 = 2;
pub const JP2_STATE_SIGNATURE: C2RustUnnamed_2 = 1;
pub const JP2_STATE_NONE: C2RustUnnamed_2 = 0;
pub type C2RustUnnamed_3 = core::ffi::c_uint;
pub const JP2_IMG_STATE_UNKNOWN: C2RustUnnamed_3 = 2147483647;
pub const JP2_IMG_STATE_NONE: C2RustUnnamed_3 = 0;

#[derive(Default, Debug, Clone, Copy)]
pub struct Jp2BoxHeader {
  pub length: u32,
  pub ty: Jp2BoxType,
  pub header_length: u32,
}

impl Jp2BoxHeader {
  fn new(ty: Jp2BoxType) -> Self {
    Self {
      length: 8,
      ty,
      header_length: 8,
    }
  }

  pub fn from_stream(reader: &mut Stream) -> Option<Self> {
    let mut header = Self::default();
    let mut max_size = opj_stream_get_number_byte_left(reader);
    if max_size < 0 {
      max_size = 0;
    }
    header.read(reader, max_size as usize).ok()?;
    Some(header)
  }

  pub fn ty_u32(&self) -> u32 {
    self.ty.to_u32().unwrap_or_default()
  }

  pub fn content_length(&self) -> u32 {
    self.length - self.header_length
  }

  /// Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
  fn read<R: Read + ?Sized>(&mut self, reader: &mut R, max_size: usize) -> Result<(), String> {
    self.length = reader
      .read_u32::<BigEndian>()
      .map_err(|e| format!("Truncated JP2 Box header: {e:?}"))?;
    self.ty = reader
      .read_u32::<BigEndian>()
      .map_err(|e| format!("Truncated JP2 Box header: {e:?}"))?
      .into();
    self.header_length = 8;
    if self.length == 0 {
      if max_size == 0 {
        return Err(format!("Can't handle box of undefined size."));
      }
      /* last box */
      if max_size > u32::MAX as usize {
        // TODO: Handle large boxes?
        return Err(format!("Cannot handle box sizes higher than 2^32"));
      }
      self.length = max_size as u32;
      assert!(self.length as usize == max_size);
      return Ok(());
    }
    /* do we have a "special very large box ?" */
    if self.length == 1 {
      /* read then the XLBox */
      let xl_part_size = reader
        .read_u32::<BigEndian>()
        .map_err(|e| format!("Truncated JP2 XLBox header: {e:?}"))?;
      let length = reader
        .read_u32::<BigEndian>()
        .map_err(|e| format!("Truncated JP2 XLBox header: {e:?}"))?;
      self.header_length += 8;
      if xl_part_size != 0 {
        // TODO: Handle large boxes?
        return Err(format!("Cannot handle box sizes higher than 2^32"));
      }
      self.length = length;
    }
    Ok(())
  }

  fn write<W: Write>(&self, writer: &mut W) -> bool {
    writer.write_u32::<BigEndian>(self.length).is_ok()
      && writer.write_u32::<BigEndian>(self.ty_u32()).is_ok()
  }

  fn read_content(
    &self,
    jp2: &mut opj_jp2,
    buf: &[u8],
    manager: &mut opj_event_mgr,
  ) -> Result<(), String> {
    let res = match self.ty {
      // File boxes.
      Jp2BoxType::JP => opj_jp2_read_jp(jp2, buf, manager),
      Jp2BoxType::FTYP => opj_jp2_read_ftyp(jp2, buf, manager),
      Jp2BoxType::JP2H => opj_jp2_read_jp2h(jp2, buf, manager),
      // Image boxes.
      Jp2BoxType::IHDR => opj_jp2_read_ihdr(jp2, buf, manager),
      Jp2BoxType::COLR => opj_jp2_read_colr(jp2, buf, manager),
      Jp2BoxType::BPCC => opj_jp2_read_bpcc(jp2, buf, manager),
      Jp2BoxType::PCLR => opj_jp2_read_pclr(jp2, buf, manager),
      Jp2BoxType::CDEF => opj_jp2_read_cdef(jp2, buf, manager),
      Jp2BoxType::CMAP => opj_jp2_read_cmap(jp2, buf, manager),
      _ => {
        event_msg!(
          manager,
          EVT_WARNING,
          &format!("Ignoring unsupported box type: {:?}\n", self.ty),
        );
        1
      }
    };
    if res != 0 {
      Ok(())
    } else {
      Err(format!("Failed to read box type: {:?}", self.ty))
    }
  }
}

pub(crate) struct HeaderWriter {
  handler: fn(_: &mut opj_jp2, _: &mut Vec<u8>) -> bool,
  data: Vec<u8>,
}

impl HeaderWriter {
  fn new(handler: fn(_: &mut opj_jp2, _: &mut Vec<u8>) -> bool) -> Self {
    Self {
      handler: handler,
      data: Default::default(),
    }
  }

  fn run(&mut self, jp2: &mut opj_jp2) -> Option<u32> {
    if (self.handler)(jp2, &mut self.data) {
      Some(self.data.len() as u32)
    } else {
      None
    }
  }

  fn write<W: Write>(&self, writer: &mut W) -> bool {
    writer.write_all(self.data.as_slice()).is_ok()
  }
}

/* *
 * Reads a IHDR box - Image Header box
 *
 * @param   p_image_header_data         pointer to actual data (already read from file)
 * @param   jp2                         the jpeg2000 file codec.
 * @param   p_image_header_size         the size of the image header
 * @param   p_manager                   the user event manager.
 *
 * @return  true if the image header is valid, false else.
 */
fn opj_jp2_read_ihdr(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  if !jp2.comps.is_empty() {
    event_msg!(
      p_manager,
      EVT_WARNING,
      "Ignoring ihdr box. First ihdr box already read\n",
    );
    return 1;
  }
  if buf.len() != 14 {
    event_msg!(p_manager, EVT_ERROR, "Bad image header box (bad size)\n",);
    return 0;
  }
  // Height
  jp2.h = buf
    .read_u32::<BigEndian>()
    .expect("Buffer should have enough data");
  // Width
  jp2.w = buf
    .read_u32::<BigEndian>()
    .expect("Buffer should have enough data");
  let numcomps = buf
    .read_u16::<BigEndian>()
    .expect("Buffer should have enough data");

  if jp2.h < 1 || jp2.w < 1 || numcomps < 1 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Wrong values for: w(%d) h(%d) numcomps(%d) (ihdr)\n",
      jp2.w,
      jp2.h,
      numcomps,
    );
    return 0;
  }
  if numcomps - 1 >= 16384 {
    /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Invalid number of components (ihdr)\n",
    );
    return 0;
  }
  /* allocate memory for components */
  jp2.comps = Vec::with_capacity(numcomps as usize);
  jp2
    .comps
    .resize(numcomps as usize, opj_jp2_comps::default());

  // BPC
  jp2.bpc = buf.read_u8().expect("Buffer should have enough data") as u32;
  // C
  jp2.C = buf.read_u8().expect("Buffer should have enough data") as u32;

  /* Should be equal to 7 cf. chapter about image header box of the norm */
  if jp2.C != 7 {
    event_msg!(
      p_manager,
      EVT_INFO,
      "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
      jp2.C
    );
  }
  // UnkC
  jp2.UnkC = buf.read_u8().expect("Buffer should have enough data") as u32;
  // IPR
  jp2.IPR = buf.read_u8().expect("Buffer should have enough data") as u32;

  jp2.j2k.m_cp.allow_different_bit_depth_sign = jp2.bpc == 255;
  jp2.j2k.ihdr_w = jp2.w;
  jp2.j2k.ihdr_h = jp2.h;
  jp2.has_ihdr = 1 as OPJ_BYTE;
  1
}

/* *
 * Writes the Image Header box - Image Header box.
 *
*/
fn opj_jp2_write_ihdr(mut jp2: &mut opj_jp2, buf: &mut Vec<u8>) -> bool {
  // IHDR
  let mut header = Jp2BoxHeader::new(Jp2BoxType::IHDR);
  header.length += 14;
  header.write(buf);
  // HEIGHT
  buf.write_u32::<BigEndian>(jp2.h).unwrap();
  // WIDTH
  buf.write_u32::<BigEndian>(jp2.w).unwrap();
  // NC
  buf.write_u16::<BigEndian>(jp2.comps.len() as u16).unwrap();
  // BPC
  buf.push(jp2.bpc as u8);
  // C : Always 7
  buf.push(jp2.C as u8);
  // UnkC, colorspace unknown
  buf.push(jp2.UnkC as u8);
  // IPR, no intellectual property
  buf.push(jp2.IPR as u8);
  true
}

/* *
 * Writes the Bit per Component box.
 *
*/
fn opj_jp2_write_bpcc(mut jp2: &mut opj_jp2, buf: &mut Vec<u8>) -> bool {
  let mut header = Jp2BoxHeader::new(Jp2BoxType::BPCC);
  header.length += jp2.comps.len() as u32;
  header.write(buf);
  for comp in &jp2.comps {
    buf.push(comp.bpcc as u8);
  }
  true
}

/* *
 * Reads a Bit per Component box.
 *
 * @param   p_bpc_header_data           pointer to actual data (already read from file)
 * @param   jp2                         the jpeg2000 file codec.
 * @param   p_bpc_header_size           the size of the bpc header
 * @param   p_manager                   the user event manager.
 *
 * @return  true if the bpc header is valid, false else.
 */
fn opj_jp2_read_bpcc(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  if jp2.bpc != 255 {
    event_msg!(p_manager, EVT_WARNING,
                      "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n", jp2.bpc);
  }
  /* and length is relevant */
  if buf.len() != jp2.comps.len() {
    event_msg!(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n",);
    return 0;
  }
  /* read info for each component */
  for comp in &mut jp2.comps {
    /* read each BPCC component */
    comp.bpcc = buf.read_u8().expect("Buffer should have enough data") as u32;
  }
  1
}

/* *
 * Writes the Channel Definition box.
 *
 */
fn opj_jp2_write_cdef(mut jp2: &mut opj_jp2, buf: &mut Vec<u8>) -> bool {
  let info = if let Some(cdef) = &jp2.color.jp2_cdef {
    &cdef.info
  } else {
    return false;
  };
  let len = info.len() as u32;
  assert!(info.len() > 0);
  let mut header = Jp2BoxHeader::new(Jp2BoxType::CDEF);
  header.length += 2 + (len * 6);
  header.write(buf);
  buf.write_u16::<BigEndian>(len as u16).unwrap();
  for info in info {
    buf.write_u16::<BigEndian>(info.cn as u16).unwrap();
    buf.write_u16::<BigEndian>(info.typ as u16).unwrap();
    buf.write_u16::<BigEndian>(info.asoc as u16).unwrap();
  }
  true
}

/* *
 * Writes the Colour Specification box.
 *
*/
fn opj_jp2_write_colr(mut jp2: &mut opj_jp2, buf: &mut Vec<u8>) -> bool {
  let meth = jp2.meth as u8;
  let mut header = Jp2BoxHeader::new(Jp2BoxType::COLR);
  header.length += 3;
  /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
  assert!(meth == 1 || meth == 2);
  header.length += match meth {
    1 => 4,
    2 => {
      let len = jp2.color.icc_profile_len();
      assert!(len != 0);
      len as u32
    }
    _ => return false,
  };
  header.write(buf);
  buf.push(meth as u8);
  buf.push(jp2.precedence as u8);
  buf.push(jp2.approx as u8);
  match meth {
    1 => {
      /* EnumCS */
      buf.write_u32::<BigEndian>(jp2.enumcs as u32).unwrap();
      // TODO: Support CIELab? (enumcs == 14).
    }
    2 => {
      /* ICC profile */
      if let Some(icc_profile) = &jp2.color.icc_profile {
        buf.extend_from_slice(icc_profile.as_slice());
      } else {
        log::error!("Missing ICC profile");
      }
    }
    _ => return false,
  }
  true
}

fn opj_jp2_check_color(
  image: &mut opj_image_t,
  color: &mut opj_jp2_color,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
  if let Some(cdef) = &color.jp2_cdef {
    /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
    let mut nr_channels = image.numcomps;
    /* cdef applies to cmap channels if any */
    if let Some(pclr) = &color.jp2_pclr {
      if !pclr.cmap.is_empty() {
        nr_channels = pclr.nr_channels as u32
      }
    }
    for info in &cdef.info {
      if info.cn as u32 >= nr_channels {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Invalid component index %d (>= %d).\n",
          info.cn as i32,
          nr_channels,
        );
        return 0;
      }
      if info.asoc != 65535 && info.asoc > 0 && (info.asoc - 1) as u32 >= nr_channels {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Invalid component index %d (>= %d).\n",
          info.asoc as core::ffi::c_int - 1i32,
          nr_channels,
        );
        return 0;
      }
    }
    /* issue 397 */
    /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
    for chan in 1..nr_channels {
      let has = cdef
        .info
        .iter()
        .position(|&x| x.cn as u32 == chan)
        .is_some();
      if !has {
        event_msg!(p_manager, EVT_ERROR, "Incomplete channel definitions.\n",);
        return 0;
      }
    }
  }
  /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
  66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
  if let Some(pclr) = &mut color.jp2_pclr {
    let nr_channels = pclr.nr_channels as usize;
    if pclr.cmap.len() == nr_channels {
      let mut is_sane = true;
      /* verify that all original components match an existing one */
      for cmap in &pclr.cmap {
        if cmap.cmp as u32 >= image.numcomps {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Invalid component index %d (>= %d).\n",
            cmap.cmp as core::ffi::c_int,
            image.numcomps,
          );
          is_sane = false
        }
      }
      let mut pcol_usage = BTreeSet::new();
      /* verify that no component is targeted more than once */
      for i in 0..nr_channels {
        let cmap = pclr.cmap[i];
        let mut mtyp = cmap.mtyp;
        let mut pcol = cmap.pcol;
        /* See ISO 15444-1 Table I.14 – MTYPi field values */
        if mtyp != 0 && mtyp != 1 {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Invalid value for cmap[%d].mtyp = %d.\n",
            i as core::ffi::c_int,
            mtyp as core::ffi::c_int,
          );
          is_sane = false
        } else if pcol as usize >= nr_channels {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Invalid component/palette index for direct mapping %d.\n",
            pcol as core::ffi::c_int,
          );
          is_sane = false
        } else if pcol_usage.contains(&pcol) && mtyp == 1 {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Component %d is mapped twice.\n",
            pcol as core::ffi::c_int,
          );
          is_sane = false
        } else if mtyp == 0 && pcol != 0 {
          /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
           * the value of this field shall be 0. */
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Direct use at #%d however pcol=%d.\n",
            i as core::ffi::c_int,
            pcol as core::ffi::c_int,
          );
          is_sane = false
        } else if mtyp == 1 && pcol as usize != i {
          /* OpenJPEG implementation limitation. See assert(i == pcol); */
          /* in opj_jp2_apply_pclr() */
          event_msg!(p_manager, EVT_ERROR,
                              "Implementation limitation: for palette mapping, pcol[%d] should be equal to %d, but is equal to %d.\n",
                              i as core::ffi::c_int, i as core::ffi::c_int,
                              pcol as core::ffi::c_int);
          is_sane = false
        } else {
          pcol_usage.insert(pcol);
        }
      }
      /* verify that all components are targeted at least once */
      for i in 0..nr_channels {
        let cmap = pclr.cmap[i];
        if !pcol_usage.contains(&(i as u8)) && cmap.mtyp != 0 {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Component %d doesn\'t have a mapping.\n",
            i as core::ffi::c_int,
          );
          is_sane = false
        }
      }
      /* Issue 235/447 weird cmap */
      if is_sane && image.numcomps == 1 {
        for i in 0..nr_channels {
          if !pcol_usage.contains(&(i as u8)) {
            is_sane = false;
            event_msg!(
              p_manager,
              EVT_WARNING,
              "Component mapping seems wrong. Trying to correct.\n",
            );
            break;
          }
        }
        if !is_sane {
          is_sane = true;
          for i in 0..nr_channels {
            let cmap = &mut pclr.cmap[i];
            cmap.mtyp = 1 as OPJ_BYTE;
            cmap.pcol = i as OPJ_BYTE;
          }
        }
      }
      if !is_sane {
        return 0;
      }
    }
  }
  1
}

/* *
Apply collected palette data
@param image Image.
@param color Collector for profile, cdef and pclr data.
@param p_manager the user event manager.
@return true in case of success
*/
fn opj_jp2_apply_pclr(
  image: &mut opj_image_t,
  pclr: &opj_jp2_pclr,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let nr_channels = pclr.nr_channels as usize;
  let comps = if let Some(comps) = image.comps() {
    comps
  } else {
    event_msg!(p_manager, EVT_ERROR, "No components in image.\n",);
    return 0;
  };
  for (i, cmap) in pclr.cmap.iter().enumerate() {
    // Ensure that the component data is not null.
    if comps[cmap.cmp as usize].data.is_null() {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n",
        i as core::ffi::c_int,
      );
      return 0;
    }
    // Ensure valid palette index.
    if cmap.mtyp != 0 && i != cmap.pcol as usize {
      event_msg!(p_manager, EVT_ERROR, "Invalid cmap: pcol != i.\n",);
      return 0;
    }
  }

  // Take the components out of the image.
  let old = image.take_comps();
  let old_comps = old.comps().expect("Just taken");

  // Allocate new components.
  if !image.alloc_comps(nr_channels as u32) {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Memory allocation failure in opj_jp2_apply_pclr().\n",
    );
    return 0i32;
  }
  let new_comps = image.comps_mut().expect("Just allocated");

  let top_k = pclr.nr_entries as i32 - 1;
  for ((cmap, channel), new_comp) in pclr
    .cmap
    .iter()
    .zip(&pclr.channel)
    .zip(new_comps.iter_mut())
  {
    let old_comp = &old_comps[cmap.cmp as usize];

    // Palette mapping:
    *new_comp = old_comp.clone();
    new_comp.prec = channel.size as OPJ_UINT32;
    new_comp.sgnd = channel.sign as OPJ_UINT32;

    // Palette mapping:
    let src = old_comp.data().expect("Source data shouldn't be null"); // verified above
    let dst = new_comp
      .data_mut()
      .expect("Destination data shouldn't be null");
    let max = dst.len();

    // Direct use:
    if cmap.mtyp == 0 {
      dst.copy_from_slice(&src[..max]);
    } else {
      let pcol = cmap.pcol as u16;
      for (dst, src) in dst.iter_mut().zip(src.iter().take(max)) {
        // The index
        let mut k = *src as i32;
        if k < 0 {
          k = 0
        } else if k > top_k {
          k = top_k
        }
        // The colour
        *dst = pclr.entries[(k * nr_channels as i32 + pcol as i32) as usize] as i32;
      }
    }
  }
  1
}

/* *
 * Collect palette data
 *
 * @param jp2 JP2 handle
 * @param p_pclr_header_data    FIXME DOC
 * @param p_pclr_header_size    FIXME DOC
 * @param p_manager
 *
 * @return Returns true if successful, returns false otherwise
*/
fn opj_jp2_read_pclr(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  if jp2.color.jp2_pclr.is_some() {
    return 0;
  }
  if buf.len() < 3 {
    return 0;
  }
  let nr_entries = buf
    .read_u16::<BigEndian>()
    .expect("Buffer should have enough data") as u16;
  if nr_entries == 0 || nr_entries > 1024 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Invalid PCLR box. Reports %d entries\n",
      nr_entries as core::ffi::c_int,
    );
    return 0;
  }
  let nr_channels = buf.read_u8().expect("Buffer should have enough data");
  if nr_channels == 0 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Invalid PCLR box. Reports 0 palette columns\n",
    );
    return 0;
  }
  if buf.len() < nr_channels as usize {
    return 0;
  }
  let mut entries = Vec::with_capacity((nr_channels * nr_channels) as usize);
  let mut channel = Vec::with_capacity(nr_channels as usize);
  for _ in 0..nr_channels {
    let value = buf.read_u8().expect("Buffer should have enough data");
    let size = (value & 0x7f) + 1;
    let sign = if value & 0x80 != 0 { 1 } else { 0 } as u8;
    channel.push(Jp2ChannelSign { size, sign });
  }
  for _ in 0..nr_entries {
    for channel in &channel {
      let size = channel.size;
      // Convert channel size in bits to bytes
      let mut bytes_to_read = (size as usize + 7) >> 3;
      if bytes_to_read > core::mem::size_of::<u32>() {
        bytes_to_read = core::mem::size_of::<u32>()
      }
      if let Ok(value) = buf.read_uint::<BigEndian>(bytes_to_read) {
        entries.push(value as u32);
      } else {
        // Truncated data.
        return 0;
      }
    }
  }
  let jp2_pclr = opj_jp2_pclr {
    channel,
    entries,
    nr_entries,
    nr_channels,
    cmap: Default::default(),
  };
  jp2.color.jp2_pclr = Some(jp2_pclr);
  1
}

/* *
 * Collect component mapping data
 *
 * @param jp2                 JP2 handle
 * @param p_cmap_header_data  FIXME DOC
 * @param p_cmap_header_size  FIXME DOC
 * @param p_manager           FIXME DOC
 *
 * @return Returns true if successful, returns false otherwise
*/
fn opj_jp2_read_cmap(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  /* Need nr_channels: */
  let mut pclr = if let Some(pclr) = &mut jp2.color.jp2_pclr {
    pclr
  } else {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Need to read a PCLR box before the CMAP box.\n",
    );
    return 0;
  };
  /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
   * inside a JP2 Header box' :
   */
  if !pclr.cmap.is_empty() {
    event_msg!(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n",); /* CMP^i */
    return 0;
  }
  let nr_channels = pclr.nr_channels as usize;
  if buf.len() < nr_channels * 4 {
    event_msg!(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n",);
    return 0;
  }
  let mut cmap = Vec::with_capacity(nr_channels);
  for _ in 0..nr_channels {
    // CMP^i
    let cmp = buf
      .read_u16::<BigEndian>()
      .expect("buffer should have enough data");
    // MTYP^i
    let mtyp = buf.read_u8().expect("buffer should have enough data");
    // PCOL^i
    let pcol = buf.read_u8().expect("buffer should have enough data");
    cmap.push(opj_jp2_cmap_comp { cmp, mtyp, pcol });
  }
  pclr.cmap = cmap;
  1
}

fn opj_jp2_apply_cdef(
  image: &mut opj_image_t,
  color: &mut opj_jp2_color,
  manager: &mut opj_event_mgr,
) {
  let cdef = if let Some(cdef) = &mut color.jp2_cdef {
    cdef
  } else {
    return;
  };
  let n = cdef.info.len();
  let comps = if let Some(comps) = image.comps_mut() {
    comps
  } else {
    return;
  };
  let numcomps = comps.len() as u32;
  for i in 0..n {
    let info = cdef.info[i];
    /* WATCH: acn = asoc - 1 ! */
    let asoc = info.asoc;
    let cn = info.cn;
    if cn as u32 >= numcomps {
      event_msg!(
        manager,
        EVT_WARNING,
        "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
        cn as core::ffi::c_int,
        numcomps,
      );
    } else if asoc as core::ffi::c_int == 0i32 || asoc as core::ffi::c_int == 65535i32 {
      comps[cn as usize].alpha = info.typ
    } else {
      let acn = asoc - 1;
      if acn as u32 >= numcomps {
        event_msg!(
          manager,
          EVT_WARNING,
          "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
          acn as core::ffi::c_int,
          numcomps,
        );
      } else {
        let mut cn_comp = comps[cn as usize];
        /* Swap only if color channel */
        if cn != acn && info.typ == 0 {
          let acn_comp = &mut comps[acn as usize];
          let saved = cn_comp;
          cn_comp = *acn_comp;
          *acn_comp = saved;
          /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
          for j in i..n {
            let info = &mut cdef.info[j];
            if info.cn == cn {
              info.cn = acn
            } else if info.cn == acn {
              info.cn = cn
            }
            /* asoc is related to color index. Do not update. */
          }
        }
        cn_comp.alpha = info.typ;
        comps[cn as usize] = cn_comp;
      }
    }
  }
  cdef.info.clear();
  color.jp2_cdef = None;
}

/* jp2_apply_cdef() */
fn opj_jp2_read_cdef(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
   * inside a JP2 Header box.'*/
  if jp2.color.jp2_cdef.is_some() {
    return 0;
  }
  if buf.len() < 2 {
    event_msg!(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n",);
    return 0;
  }
  // N
  let n = buf
    .read_u16::<BigEndian>()
    .expect("Buffer should have enough data") as usize;
  if n == 0 {
    /* szukw000: FIXME */
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Number of channel description is equal to zero in CDEF box.\n",
    );
    return 0;
  }
  if buf.len() < n * 6 {
    event_msg!(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n",);
    return 0;
  }
  let mut info = Vec::with_capacity(n);
  for _i in 0..n {
    // Cn^i
    let cn = buf
      .read_u16::<BigEndian>()
      .expect("Buffer should have enough data");
    // Typ^i
    let typ = buf
      .read_u16::<BigEndian>()
      .expect("Buffer should have enough data");
    // Asoc^i
    let asoc = buf
      .read_u16::<BigEndian>()
      .expect("Buffer should have enough data");
    info.push(opj_jp2_cdef_info { cn, typ, asoc });
  }
  jp2.color.jp2_cdef = Some(opj_jp2_cdef { info });
  1
}

/* *
 * Reads the Color Specification box.
 *
 * @param   p_colr_header_data          pointer to actual data (already read from file)
 * @param   jp2                         the jpeg2000 file codec.
 * @param   p_colr_header_size          the size of the color header
 * @param   p_manager                   the user event manager.
 *
 * @return  true if the bpc header is valid, false else.
*/
fn opj_jp2_read_colr(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  let total_size = buf.len() as u32;
  if buf.len() < 3 {
    event_msg!(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n",);
    return 0;
  }
  /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
   * Specification boxes after the first.'
   */
  if jp2.color.jp2_has_colr != 0 {
    event_msg!(p_manager, EVT_INFO,
                      "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
    return 1;
  }
  // METH
  jp2.meth = buf.read_u8().expect("Buffer should have enough data") as u32;
  // PRECEDENCE
  jp2.precedence = buf.read_u8().expect("Buffer should have enough data") as u32;
  // APPROX
  jp2.approx = buf.read_u8().expect("Buffer should have enough data") as u32;
  if jp2.meth == 1 {
    if buf.len() < 4 {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Bad COLR header box (bad size: %d)\n",
        total_size,
      );
      return 0i32;
    }
    // EnumCS
    jp2.enumcs = buf
      .read_u32::<BigEndian>()
      .expect("Buffer should have enough data");

    if buf.len() > 4 && jp2.enumcs != 14 {
      /* handled below for CIELab) */
      /* testcase Altona_Technical_v20_x4.pdf */
      event_msg!(
        p_manager,
        EVT_WARNING,
        "Bad COLR header box (bad size: %d)\n",
        total_size,
      );
    }

    // CIELab
    if jp2.enumcs == 14 {
      // default values
      let mut rl = 0u32;
      let mut ol = 0u32;
      let mut ra = 0u32;
      let mut oa = 0u32;
      let mut rb = 0u32;
      let mut ob = 0u32;
      let mut il = 0x443530u32;
      let mut icc_profile = Vec::with_capacity(9 * core::mem::size_of::<OPJ_UINT32>());
      icc_profile.write_all(&14u32.to_ne_bytes()).unwrap();
      if buf.len() == 28 {
        rl = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        ol = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        ra = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        oa = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        rb = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        ob = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        il = buf
          .read_u32::<BigEndian>()
          .expect("Buffer should have enough data");
        icc_profile.write_all(&0u32.to_ne_bytes()).unwrap();
      } else if buf.len() != 0 {
        icc_profile.write_all(&0x44454600u32.to_ne_bytes()).unwrap();
        event_msg!(
          p_manager,
          EVT_WARNING,
          "Bad COLR header box (CIELab, bad size: %d)\n",
          total_size,
        );
      }
      icc_profile.write_all(&rl.to_ne_bytes()).unwrap();
      icc_profile.write_all(&ol.to_ne_bytes()).unwrap();
      icc_profile.write_all(&ra.to_ne_bytes()).unwrap();
      icc_profile.write_all(&oa.to_ne_bytes()).unwrap();
      icc_profile.write_all(&rb.to_ne_bytes()).unwrap();
      icc_profile.write_all(&ob.to_ne_bytes()).unwrap();
      icc_profile.write_all(&il.to_ne_bytes()).unwrap();
      jp2.color.set_cielab_profile(icc_profile);
    }
    jp2.color.jp2_has_colr = 1 as OPJ_BYTE
  } else if jp2.meth == 2 {
    /* ICC profile */
    let mut icc_profile = Vec::from(buf);
    jp2.color.set_icc_profile(icc_profile);
    jp2.color.jp2_has_colr = 1 as OPJ_BYTE
  } else if jp2.meth > 2 {
    /*  ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
    conforming JP2 reader shall ignore the entire Colour Specification box.*/
    event_msg!(p_manager, EVT_INFO,
                      "COLR BOX meth value is not a regular value (%d), so we will ignore the entire Colour Specification box. \n", jp2.meth);
  }
  1
}

pub(crate) fn opj_jp2_apply_color_postprocessing(
  jp2: &mut opj_jp2,
  p_image: &mut opj_image,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  unsafe {
    if jp2.j2k.m_specific_param.m_decoder.m_numcomps_to_decode != 0 {
      /* Bypass all JP2 component transforms */
      return 1;
    }
  }
  if jp2.ignore_pclr_cmap_cdef == 0 {
    if opj_jp2_check_color(p_image, &mut jp2.color, p_manager) == 0 {
      return 0;
    }

    if let Some(pclr) = &jp2.color.jp2_pclr {
      /* Part 1, I.5.3.4: Either both or none : */
      if pclr.cmap.is_empty() {
        jp2.color.jp2_pclr = None;
      } else if opj_jp2_apply_pclr(p_image, pclr, p_manager) == 0 {
        return 0;
      }
    }
    /* Apply the color space if needed */
    if jp2.color.jp2_cdef.is_some() {
      opj_jp2_apply_cdef(p_image, &mut jp2.color, p_manager);
    }
  }
  1
}

pub(crate) fn opj_jp2_decode(
  jp2: &mut opj_jp2,
  p_stream: &mut Stream,
  p_image: &mut opj_image,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* J2K decoding */
  if opj_j2k_decode(&mut jp2.j2k, p_stream, p_image, p_manager) == 0 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Failed to decode the codestream in the JP2 file\n",
    );
    return 0i32;
  }

  opj_jp2_apply_color_postprocessing(jp2, p_image, p_manager)
}

/* *
 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
 *
 * @param  jp2      the jpeg2000 file codec.
 * @param  stream      the stream to write data to.
 * @param  p_manager  user event manager.
 *
 * @return true if writing was successful.
 */
fn opj_jp2_write_jp2h(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut writers = Vec::with_capacity(4);
  // JP2H box type
  let mut jp2h = Jp2BoxHeader::new(Jp2BoxType::JP2H);

  writers.push(HeaderWriter::new(opj_jp2_write_ihdr));
  if jp2.bpc == 255u32 {
    writers.push(HeaderWriter::new(opj_jp2_write_bpcc));
  }
  writers.push(HeaderWriter::new(opj_jp2_write_colr));
  if jp2.color.jp2_cdef.is_some() {
    writers.push(HeaderWriter::new(opj_jp2_write_cdef));
  }
  /* write box header */
  for writer in &mut writers {
    match writer.run(jp2) {
      Some(size) => {
        jp2h.length += size;
      }
      None => {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Not enough memory to hold JP2 Header data\n",
        );
        return 0i32;
      }
    }
  }
  // write super box header to stream
  if !jp2h.write(stream) {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Stream error while writing JP2 Header box\n",
    );
    return 0;
  }
  for writer in &writers {
    if !writer.write(stream) {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Stream error while writing JP2 Header box\n",
      );
      return 0i32;
    }
  }
  1
}

/* *
 * Writes a FTYP box - File type box
 *
 * @param   stream         the stream to write data to.
 * @param   jp2         the jpeg2000 file codec.
 * @param   p_manager   the user event manager.
 *
 * @return  true if writing was successful.
 */
fn opj_jp2_write_ftyp(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* FTYP */
  let mut header = Jp2BoxHeader::new(Jp2BoxType::FTYP);
  header.length += 8 + (4 * jp2.cl.len() as u32);
  let mut buf = Vec::with_capacity(header.length as usize);
  header.write(&mut buf);

  buf.write_u32::<BigEndian>(jp2.brand).unwrap();
  buf.write_u32::<BigEndian>(jp2.minversion).unwrap();
  /* CL */
  for cl in &jp2.cl {
    buf.write_u32::<BigEndian>(*cl).unwrap();
  }
  if stream.write_all(buf.as_slice()).is_err() {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Error while writing ftyp data to stream\n",
    );
    return 0;
  }
  1
}

/* *
 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
 *
 * @param   stream         the stream to write data to.
 * @param   jp2         the jpeg2000 file codec.
 * @param   p_manager   user event manager.
 *
 * @return true if writing was successful.
*/
fn opj_jp2_write_jp2c(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  if opj_stream_has_seek(stream) == 0 {
    event_msg!(p_manager, EVT_ERROR, "Stream doesn't support seeking.\n",);
    return 0;
  }
  let j2k_codestream_exit = opj_stream_tell(stream);
  let mut header = Jp2BoxHeader::new(Jp2BoxType::JP2C);
  header.length = (j2k_codestream_exit - jp2.j2k_codestream_offset) as u32;
  if opj_stream_seek(stream, jp2.j2k_codestream_offset, p_manager) == 0 {
    event_msg!(p_manager, EVT_ERROR, "Failed to seek in the stream.\n",);
    return 0;
  }
  if !header.write(stream) {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Error while writing jp2c header to stream\n",
    );
    return 0;
  }
  if opj_stream_seek(stream, j2k_codestream_exit, p_manager) == 0 {
    event_msg!(p_manager, EVT_ERROR, "Failed to seek in the stream.\n",);
    return 0;
  }
  1
}

/* *
 * Writes a jpeg2000 file signature box.
 *
 * @param stream the stream to write data to.
 * @param   jp2         the jpeg2000 file codec.
 * @param p_manager the user event manager.
 *
 * @return true if writing was successful.
 */
fn opj_jp2_write_jp(
  _jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut tmp_buf = [0u8; 12];
  let mut buf = &mut tmp_buf[..];
  let mut header = Jp2BoxHeader::new(Jp2BoxType::JP);
  header.length += 4;
  header.write(&mut buf);
  // writes magic number
  buf.write_u32::<BigEndian>(0xd0a870a).unwrap();

  if stream.write_all(&tmp_buf).is_err() {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Error while writing jp data to stream\n",
    );
    return 0;
  }
  1
}

/* ----------------------------------------------------------------------- */
/* JP2 decoder interface                                             */
/* ----------------------------------------------------------------------- */
pub(crate) fn opj_jp2_setup_decoder(mut jp2: &mut opj_jp2, mut parameters: &mut opj_dparameters_t) {
  /* setup the J2K codec */
  opj_j2k_setup_decoder(&mut jp2.j2k, parameters);
  /* further JP2 initializations go here */
  jp2.color.jp2_has_colr = 0 as OPJ_BYTE;
  jp2.ignore_pclr_cmap_cdef = (parameters.flags & 0x1u32) as OPJ_BOOL;
}

pub(crate) fn opj_jp2_decoder_set_strict_mode(mut jp2: &mut opj_jp2, mut strict: OPJ_BOOL) {
  opj_j2k_decoder_set_strict_mode(&mut jp2.j2k, strict);
}

pub(crate) fn opj_jp2_set_threads(mut jp2: &mut opj_jp2, mut num_threads: OPJ_UINT32) -> OPJ_BOOL {
  opj_j2k_set_threads(&mut jp2.j2k, num_threads)
}

/* ----------------------------------------------------------------------- */
/* JP2 encoder interface                                             */
/* ----------------------------------------------------------------------- */
pub(crate) fn opj_jp2_setup_encoder(
  jp2: &mut opj_jp2,
  parameters: &mut opj_cparameters_t,
  image: &mut opj_image_t,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* setup the J2K codec */
  /* ------------------- */
  /* Check if number of components respects standard */
  let numcomps = image.numcomps;
  if numcomps < 1 || numcomps > 16384 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Invalid number of components specified while setting up JP2 encoder\n",
    );
    return 0;
  }
  if opj_j2k_setup_encoder(&mut jp2.j2k, parameters, image, p_manager) == 0 {
    return 0;
  }
  /* setup the JP2 codec */

  /* Profile box */

  jp2.brand = Jp2BoxType::JP2.to_u32().unwrap(); /* BR */
  jp2.minversion = 0 as OPJ_UINT32; /* MinV */
  jp2.cl = Vec::with_capacity(1);
  /* CL0 : JP2 */
  jp2.cl.push(Jp2BoxType::JP2.to_u32().unwrap());

  /* Image Header box */
  let comps = image.comps().unwrap();
  jp2.comps = Vec::with_capacity(comps.len());
  jp2.comps.resize(comps.len(), opj_jp2_comps::default());

  /* HEIGHT */
  jp2.h = image.y1.wrapping_sub(image.y0);
  /* WIDTH */
  jp2.w = image.x1.wrapping_sub(image.x0);

  /* BPC */
  let depth_0 = comps[0].prec.wrapping_sub(1);
  let sign = comps[0].sgnd;
  jp2.bpc = depth_0.wrapping_add(sign << 7);
  for comp in &comps[1..] {
    let mut depth = comp.prec.wrapping_sub(1);
    //sign = comp.sgnd;
    if depth_0 != depth {
      jp2.bpc = 255 as OPJ_UINT32
    }
  }
  /* C : Always 7 */
  jp2.C = 7 as OPJ_UINT32;
  /* UnkC, colorspace specified in colr box */
  jp2.UnkC = 0 as OPJ_UINT32;
  /* IPR, no intellectual property */
  jp2.IPR = 0 as OPJ_UINT32;

  /* BitsPerComponent box */
  for (j_comp, comp) in jp2.comps.iter_mut().zip(&comps[..]) {
    j_comp.bpcc = comp.prec.wrapping_sub(1).wrapping_add(comp.sgnd << 7);
  }

  /* Colour Specification box */
  if image.icc_profile_len != 0 {
    jp2.meth = 2 as OPJ_UINT32;
    jp2.enumcs = 0 as OPJ_UINT32
  } else {
    jp2.meth = 1 as OPJ_UINT32;
    if image.color_space as core::ffi::c_int == 1 {
      jp2.enumcs = 16 as OPJ_UINT32
    /* sRGB as defined by IEC 61966-2-1 */
    } else if image.color_space as core::ffi::c_int == 2 {
      jp2.enumcs = 17 as OPJ_UINT32
    /* greyscale */
    } else if image.color_space as core::ffi::c_int == 3 {
      jp2.enumcs = 18 as OPJ_UINT32
      /* YUV */
    }
  }
  /* Channel Definition box */
  /* FIXME not provided by parameters */
  /* We try to do what we can... */
  let mut alpha_count = 0;
  let mut alpha_channel = 0;
  let mut color_channels = 0u32;
  for (i, comp) in comps.iter().enumerate() {
    if comp.alpha != 0 {
      alpha_count = alpha_count + 1;
      alpha_channel = i as u32
    }
  }
  if alpha_count == 1 {
    /* no way to deal with more than 1 alpha channel */
    match jp2.enumcs {
      16 | 18 => color_channels = 3,
      17 => color_channels = 1,
      _ => alpha_count = 0,
    }
    if alpha_count == 0 {
      event_msg!(
        p_manager,
        EVT_WARNING,
        "Alpha channel specified but unknown enumcs. No cdef box will be created.\n",
      );
    } else if numcomps < color_channels + 1 {
      event_msg!(p_manager, EVT_WARNING,
                          "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
      alpha_count = 0
    } else if alpha_channel < color_channels {
      event_msg!(
        p_manager,
        EVT_WARNING,
        "Alpha channel position conflicts with color channel. No cdef box will be created.\n",
      );
      alpha_count = 0
    }
  } else if alpha_count > 1 {
    event_msg!(
      p_manager,
      EVT_WARNING,
      "Multiple alpha channels specified. No cdef box will be created.\n",
    );
  }
  if alpha_count == 1 {
    /* if here, we know what we can do */
    let len = numcomps as usize;
    let mut cdef = opj_jp2_cdef {
      info: Vec::with_capacity(len),
    };
    /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
    /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
    for i in 0..len {
      let cn = i as u16;
      if i < color_channels as usize {
        cdef.info.push(opj_jp2_cdef_info {
          cn,
          typ: 0,
          asoc: cn + 1,
        })
      } else {
        if comps[i].alpha != 0 {
          /* we'll be here exactly once */
          cdef.info.push(opj_jp2_cdef_info {
            cn,
            typ: 1,  /* Opacity channel */
            asoc: 0, /* Apply alpha channel to the whole image */
          })
        } else {
          /* Unknown channel */
          cdef.info.push(opj_jp2_cdef_info {
            cn,
            typ: u16::MAX,
            asoc: u16::MAX,
          })
        }
      }
    }
    jp2.color.jp2_cdef = Some(cdef);
  }
  jp2.precedence = 0 as OPJ_UINT32;
  jp2.approx = 0 as OPJ_UINT32;
  jp2.jpip_on = parameters.jpip_on;
  1
}

pub(crate) fn opj_jp2_encode(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_encode(&mut jp2.j2k, stream, p_manager)
}

pub(crate) fn opj_jp2_end_decompress(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* preconditions */
  let mut procedure_list = opj_jp2_proc_list_t::new();

  /* customization of the end encoding */
  if opj_jp2_setup_end_header_reading(jp2, &mut procedure_list, p_manager) == 0 {
    return 0i32;
  }
  /* write header */
  if opj_jp2_exec(jp2, &mut procedure_list, stream, p_manager) == 0 {
    return 0i32;
  }
  opj_j2k_end_decompress(&mut jp2.j2k, stream, p_manager)
}

pub(crate) fn opj_jp2_end_compress(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut procedure_list = opj_jp2_proc_list_t::new();
  /* preconditions */

  /* customization of the end encoding */
  if opj_jp2_setup_end_header_writing(jp2, &mut procedure_list, p_manager) == 0 {
    return 0i32;
  }
  if opj_j2k_end_compress(&mut jp2.j2k, stream, p_manager) == 0 {
    return 0i32;
  }
  /* write header */
  opj_jp2_exec(jp2, &mut procedure_list, stream, p_manager)
}

/* *
 * Sets up the procedures to do on writing header after the codestream.
 * Developers wanting to extend the library can add their own writing procedures.
 */
fn opj_jp2_setup_end_header_writing(
  _jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.add(opj_jp2_write_jp2c);
  /* DEVELOPER CORNER, add your custom procedures */
  1i32
}

/* *
 * Sets up the procedures to do on reading header after the codestream.
 * Developers wanting to extend the library can add their own writing procedures.
 */
fn opj_jp2_setup_end_header_reading(
  _jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.add(opj_jp2_read_header_procedure);
  /* DEVELOPER CORNER, add your custom procedures */
  1i32
}

fn opj_jp2_default_validation(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut l_is_valid = 1i32;

  /* JPEG2000 codec validation */
  /* STATE checking */
  /* make sure the state is at 0 */
  l_is_valid &= (jp2.jp2_state == JP2_STATE_NONE as core::ffi::c_uint) as core::ffi::c_int;
  /* make sure not reading a jp2h ???? WEIRD */
  l_is_valid &= (jp2.jp2_img_state == JP2_IMG_STATE_NONE as core::ffi::c_uint) as core::ffi::c_int;
  /* PARAMETER VALIDATION */
  /* number of components */
  l_is_valid &= (jp2.cl.len() > 0) as core::ffi::c_int;
  /* width */
  l_is_valid &= (jp2.h > 0u32) as core::ffi::c_int;
  /* height */
  l_is_valid &= (jp2.w > 0u32) as core::ffi::c_int;
  /* precision */
  for i in 0..jp2.comps.len() {
    l_is_valid &= ((jp2.comps[i].bpcc & 0x7fu32) < 38u32) as core::ffi::c_int;
    /* 0 is valid, ignore sign for check */
  }
  /* METH */
  l_is_valid &= (jp2.meth > 0u32 && jp2.meth < 3u32) as core::ffi::c_int;
  /* stream validation */
  /* back and forth is needed */
  l_is_valid &= opj_stream_has_seek(stream);
  l_is_valid
}

/* *
 * Reads a jpeg2000 file header structure.
 *
 * @param jp2 the jpeg2000 file header structure.
 * @param stream the stream to read data from.
 * @param p_manager the user event manager.
 *
 * @return true if the box is valid.
 */
fn opj_jp2_read_header_procedure(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut data = Vec::<u8>::new();

  while let Some(header) = Jp2BoxHeader::from_stream(stream) {
    /* is it the codestream box ? */
    if header.ty == Jp2BoxType::JP2C {
      if jp2.jp2_state & JP2_STATE_HEADER as core::ffi::c_uint != 0 {
        jp2.jp2_state |= JP2_STATE_CODESTREAM as core::ffi::c_uint;
        return 1i32;
      } else {
        event_msg!(p_manager, EVT_ERROR, "bad placed jpeg codestream\n",);
        return 0i32;
      }
    } else if header.length == 0u32 {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Cannot handle box of undefined sizes\n",
      );
      return 0i32;
    } else {
      /* testcase 1851.pdf.SIGSEGV.ce9.948 */
      if header.length < header.header_length {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "invalid box size %d (%x)\n",
          header.length,
          header.ty_u32(),
        );
        return 0i32;
      }
    }
    let data_size = header.content_length() as usize;
    if header.ty.is_file_header() || header.ty.is_image_header() {
      if !header.ty.is_file_header() {
        event_msg!(
          p_manager,
          EVT_WARNING,
          "Found a misplaced \'%x\' box outside jp2h box\n",
          header.ty_u32(),
        );
        if jp2.jp2_state & JP2_STATE_HEADER != 0 {
          /* read anyway, we already have jp2h */
        } else {
          event_msg!(
            p_manager,
            EVT_WARNING,
            "JPEG2000 Header box not read yet, \'%x\' box will be ignored\n",
            header.ty_u32(),
          );
          jp2.jp2_state |= JP2_STATE_UNKNOWN;
          if opj_stream_skip(stream, data_size as OPJ_OFF_T, p_manager) != data_size as i64 {
            event_msg!(
              p_manager,
              EVT_ERROR,
              "Problem with skipping JPEG2000 box, stream error\n",
            );
            return 0i32;
          }
          continue;
        }
      }
      if data_size as OPJ_OFF_T > opj_stream_get_number_byte_left(stream) {
        /* do not even try to malloc if we can't read */
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Invalid box size %d for box \'%x\'. Need %d bytes, %d bytes remaining \n",
          header.length,
          header.ty_u32(),
          data_size,
          opj_stream_get_number_byte_left(stream) as OPJ_UINT32,
        );
        return 0i32;
      }
      data.resize(data_size as usize, 0);
      if stream.read_exact(data.as_mut_slice()).is_err() {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Problem with reading JPEG2000 box, stream error\n",
        );
        return 0i32;
      }
      if header.read_content(jp2, &data, p_manager).is_err() {
        return 0;
      }
    } else {
      if jp2.jp2_state & JP2_STATE_SIGNATURE == 0 {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Malformed JP2 file format: first box must be JPEG 2000 signature box\n",
        );
        return 0i32;
      }
      if jp2.jp2_state & JP2_STATE_FILE_TYPE == 0 {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Malformed JP2 file format: second box must be file type box\n",
        );
        return 0i32;
      }
      jp2.jp2_state |= JP2_STATE_UNKNOWN;
      if opj_stream_skip(stream, data_size as OPJ_OFF_T, p_manager) != data_size as i64 {
        if jp2.jp2_state & JP2_STATE_CODESTREAM != 0 {
          /* If we already read the codestream, do not error out */
          /* Needed for data/input/nonregression/issue254.jp2 */
          event_msg!(
            p_manager,
            EVT_WARNING,
            "Problem with skipping JPEG2000 box, stream error\n",
          );
          return 1i32;
        } else {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "Problem with skipping JPEG2000 box, stream error\n",
          );
          return 0i32;
        }
      }
    }
  }
  1i32
}

/* *
 * Executes the given procedures on the given codec.
 *
 * @param   p_procedure_list    the list of procedures to execute
 * @param   jp2                 the jpeg2000 file codec to execute the procedures on.
 * @param   stream                  the stream to execute the procedures on.
 * @param   p_manager           the user manager.
 *
 * @return  true                if all the procedures were successfully executed.
 */
fn opj_jp2_exec(
  jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.execute(|p| (p)(jp2, stream, p_manager) != 0) as i32
}

pub(crate) fn opj_jp2_start_compress(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_image: &mut opj_image,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut validation_list = opj_jp2_proc_list_t::new();
  let mut procedure_list = opj_jp2_proc_list_t::new();
  /* preconditions */

  /* customization of the validation */
  if opj_jp2_setup_encoding_validation(jp2, &mut validation_list, p_manager) == 0 {
    return 0i32;
  }
  /* validation of the parameters codec */
  if opj_jp2_exec(jp2, &mut validation_list, stream, p_manager) == 0 {
    return 0i32;
  }
  /* customization of the encoding */
  if opj_jp2_setup_header_writing(jp2, &mut procedure_list, p_manager) == 0 {
    return 0i32;
  }
  /* write header */
  if opj_jp2_exec(jp2, &mut procedure_list, stream, p_manager) == 0 {
    return 0i32;
  }
  opj_j2k_start_compress(&mut jp2.j2k, stream, p_image, p_manager)
}

/* *
 * Reads a jpeg2000 file signature box.
 *
 * @param   p_header_data   the data contained in the signature box.
 * @param   jp2             the jpeg2000 file codec.
 * @param   p_header_size   the size of the data contained in the signature box.
 * @param   p_manager       the user event manager.
 *
 * @return true if the file signature box is valid.
 */
fn opj_jp2_read_jp(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  if jp2.jp2_state != JP2_STATE_NONE {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "The signature box must be the first box in the file.\n",
    );
    return 0;
  }
  /* assure length of data is correct (4 -> magic number) */
  if buf.len() != 4 {
    event_msg!(p_manager, EVT_ERROR, "Error with JP signature Box size\n",);
    return 0;
  }
  /* rearrange data */
  let magic_number = buf
    .read_u32::<BigEndian>()
    .expect("Buffer does not have enough data");
  if magic_number != 0xd0a870au32 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Error with JP Signature : bad magic number\n",
    );
    return 0;
  }
  jp2.jp2_state |= JP2_STATE_SIGNATURE;
  1
}

/* *
 * Reads a a FTYP box - File type box
 *
 * @param   p_header_data   the data contained in the FTYP box.
 * @param   jp2             the jpeg2000 file codec.
 * @param   p_header_size   the size of the data contained in the FTYP box.
 * @param   p_manager       the user event manager.
 *
 * @return true if the FTYP box is valid.
 */
fn opj_jp2_read_ftyp(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  if jp2.jp2_state != JP2_STATE_SIGNATURE {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "The ftyp box must be the second box in the file.\n",
    );
    return 0;
  }
  /* assure length of data is correct */
  if buf.len() < 8 {
    event_msg!(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n",);
    return 0;
  }
  // BR
  jp2.brand = buf
    .read_u32::<BigEndian>()
    .expect("Buffer does not have enough data");
  // MinV
  jp2.minversion = buf
    .read_u32::<BigEndian>()
    .expect("Buffer does not have enough data");
  /* the number of remaining bytes should be a multiple of 4 */
  if buf.len() & 0x3 != 0 {
    event_msg!(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n",);
    return 0;
  }
  /* div by 4 */
  let numcl = buf.len() >> 2;
  jp2.cl = Vec::with_capacity(numcl);
  for _ in 0..numcl {
    /* CLi */
    let value = buf
      .read_u32::<BigEndian>()
      .expect("Buffer does not have enough data");
    jp2.cl.push(value);
  }
  jp2.jp2_state |= JP2_STATE_FILE_TYPE;
  1
}

fn opj_jp2_skip_jp2c(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* preconditions */

  jp2.j2k_codestream_offset = opj_stream_tell(stream);
  if opj_stream_skip(stream, 8 as OPJ_OFF_T, p_manager) != 8i64 {
    return 0i32;
  }
  1i32
}

fn opj_jpip_skip_iptr(
  jp2: &mut opj_jp2,
  stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* preconditions */

  jp2.jpip_iptr_offset = opj_stream_tell(stream);
  if opj_stream_skip(stream, 24 as OPJ_OFF_T, p_manager) != 24i64 {
    return 0i32;
  }
  1i32
}

/* *
 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
 *
 * @param   p_header_data   the data contained in the file header box.
 * @param   jp2             the jpeg2000 file codec.
 * @param   p_header_size   the size of the data contained in the file header box.
 * @param   p_manager       the user event manager.
 *
 * @return true if the JP2 Header box was successfully recognized.
*/
fn opj_jp2_read_jp2h(jp2: &mut opj_jp2, mut buf: &[u8], p_manager: &mut opj_event_mgr) -> OPJ_BOOL {
  let mut header = Jp2BoxHeader::default();
  let mut l_has_ihdr = false;

  /* make sure the box is well placed */
  if jp2.jp2_state & JP2_STATE_FILE_TYPE != JP2_STATE_FILE_TYPE {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "The  box must be the first box in the file.\n",
    );
    return 0;
  }
  jp2.jp2_img_state = JP2_IMG_STATE_NONE;
  /* iterate while remaining data */
  while buf.len() > 0 {
    // Try reading box header.
    if let Err(e) = header.read(&mut buf, 0) {
      let msg = format!("Stream error while reading JP2 Header box: {}", e);
      event_msg!(p_manager, EVT_ERROR, &msg);
      return 0;
    }
    if header.length < header.header_length {
      event_msg!(p_manager, EVT_ERROR, "Box length is inconsistent.\n",);
      return 0;
    }
    let content_length = header.content_length() as usize;
    if buf.len() < content_length {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Stream error while reading JP2 Header box: box length is inconsistent.\n",
      );
      return 0;
    }
    let (content, rest) = buf.split_at(content_length);
    buf = rest;
    if header.ty.is_image_header() {
      if header.read_content(jp2, content, p_manager).is_err() {
        return 0;
      }
    } else {
      jp2.jp2_img_state |= JP2_IMG_STATE_UNKNOWN
    }
    if header.ty == Jp2BoxType::IHDR {
      l_has_ihdr = true
    }
  }
  if !l_has_ihdr {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Stream error while reading JP2 Header box: no \'ihdr\' box.\n",
    );
    return 0;
  }
  jp2.jp2_state |= JP2_STATE_HEADER;
  jp2.has_jp2h = 1 as OPJ_BYTE;
  1
}

pub(crate) fn opj_jp2_read_header(
  p_stream: &mut Stream,
  jp2: &mut opj_jp2,
  p_image: *mut *mut opj_image_t,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  let mut validation_list = opj_jp2_proc_list_t::new();
  let mut procedure_list = opj_jp2_proc_list_t::new();

  /* customization of the validation */
  if opj_jp2_setup_decoding_validation(jp2, &mut validation_list, p_manager) == 0 {
    return 0i32;
  }
  /* customization of the encoding */
  if opj_jp2_setup_header_reading(jp2, &mut procedure_list, p_manager) == 0 {
    return 0i32;
  }
  /* validation of the parameters codec */
  if opj_jp2_exec(jp2, &mut validation_list, p_stream, p_manager) == 0 {
    return 0i32;
  }
  /* read header */
  if opj_jp2_exec(jp2, &mut procedure_list, p_stream, p_manager) == 0 {
    return 0i32;
  }
  if jp2.has_jp2h as core::ffi::c_int == 0i32 {
    event_msg!(p_manager, EVT_ERROR, "JP2H box missing. Required.\n",);
    return 0i32;
  }
  if jp2.has_ihdr as core::ffi::c_int == 0i32 {
    event_msg!(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n",);
    return 0i32;
  }

  let ret = opj_j2k_read_header(p_stream, &mut jp2.j2k, p_image, p_manager);

  let image = unsafe {
    if !p_image.is_null() && !(*p_image).is_null() {
      Some(&mut *(*p_image))
    } else {
      None
    }
  };
  if let Some(image) = image {
    /* Set Image Color Space */
    if jp2.enumcs == 16u32 {
      image.color_space = OPJ_CLRSPC_SRGB
    } else if jp2.enumcs == 17u32 {
      image.color_space = OPJ_CLRSPC_GRAY
    } else if jp2.enumcs == 18u32 {
      image.color_space = OPJ_CLRSPC_SYCC
    } else if jp2.enumcs == 24u32 {
      image.color_space = OPJ_CLRSPC_EYCC
    } else if jp2.enumcs == 12u32 {
      image.color_space = OPJ_CLRSPC_CMYK
    } else {
      image.color_space = OPJ_CLRSPC_UNKNOWN
    }

    if let Some(icc_profile) = jp2.color.icc_profile() {
      image.copy_icc_profile(icc_profile);
    }
  }
  ret
}

/* *
 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
 * are valid. Developers wanting to extend the library can add their own validation procedures.
 */
fn opj_jp2_setup_encoding_validation(
  _jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.add(opj_jp2_default_validation);
  /* DEVELOPER CORNER, add your custom validation procedure */
  1i32
}

/* *
 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
 * are valid. Developers wanting to extend the library can add their own validation procedures.
 */
fn opj_jp2_setup_decoding_validation(
  _jp2: &mut opj_jp2,
  _list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  /* DEVELOPER CORNER, add your custom validation procedure */
  1i32
}

/* *
 * Sets up the procedures to do on writing header. Developers wanting to extend the library can add their own writing procedures.
 */
fn opj_jp2_setup_header_writing(
  jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.add(opj_jp2_write_jp);
  list.add(opj_jp2_write_ftyp);
  list.add(opj_jp2_write_jp2h);
  if jp2.jpip_on != 0 {
    list.add(opj_jpip_skip_iptr);
  }
  list.add(opj_jp2_skip_jp2c);
  /* DEVELOPER CORNER, insert your custom procedures */
  1i32
}

/* *
 * Sets up the procedures to do on reading header.
 * Developers wanting to extend the library can add their own writing procedures.
 */
fn opj_jp2_setup_header_reading(
  _jp2: &mut opj_jp2,
  list: &mut opj_jp2_proc_list_t,
  _p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  list.add(opj_jp2_read_header_procedure);
  /* DEVELOPER CORNER, add your custom procedures */
  1i32
}

pub(crate) fn opj_jp2_read_tile_header(
  p_jp2: &mut opj_jp2,
  p_stream: &mut Stream,
  tile_info: &mut TileInfo,
  p_manager: &mut opj_event_mgr,
) -> bool {
  opj_j2k_read_tile_header(&mut p_jp2.j2k, p_stream, tile_info, p_manager)
}

pub(crate) fn opj_jp2_write_tile(
  p_jp2: &mut opj_jp2,
  p_tile_index: OPJ_UINT32,
  p_data: &[u8],
  p_stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_write_tile(&mut p_jp2.j2k, p_tile_index, p_data, p_stream, p_manager)
}

pub(crate) fn opj_jp2_decode_tile(
  p_jp2: &mut opj_jp2,
  p_tile_index: OPJ_UINT32,
  p_data: Option<&mut [u8]>,
  p_stream: &mut Stream,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_decode_tile(&mut p_jp2.j2k, p_tile_index, p_data, p_stream, p_manager)
}

pub(crate) fn opj_jp2_set_decoded_components(
  p_jp2: &mut opj_jp2,
  components: &[u32],
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_set_decoded_components(&mut p_jp2.j2k, components, p_manager)
}

pub(crate) fn opj_jp2_set_decode_area(
  p_jp2: &mut opj_jp2,
  p_image: &mut opj_image,
  p_start_x: OPJ_INT32,
  p_start_y: OPJ_INT32,
  p_end_x: OPJ_INT32,
  p_end_y: OPJ_INT32,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_set_decode_area(
    &mut p_jp2.j2k,
    p_image,
    p_start_x,
    p_start_y,
    p_end_x,
    p_end_y,
    p_manager,
  )
}

pub(crate) fn opj_jp2_get_tile(
  p_jp2: &mut opj_jp2,
  p_stream: &mut Stream,
  p_image: &mut opj_image,
  p_manager: &mut opj_event_mgr,
  tile_index: OPJ_UINT32,
) -> OPJ_BOOL {
  event_msg!(
    p_manager,
    EVT_WARNING,
    "JP2 box which are after the codestream will not be read by this function.\n",
  );
  if opj_j2k_get_tile(&mut p_jp2.j2k, p_stream, p_image, p_manager, tile_index) == 0 {
    event_msg!(
      p_manager,
      EVT_ERROR,
      "Failed to decode the codestream in the JP2 file\n",
    );
    return 0i32;
  }

  opj_jp2_apply_color_postprocessing(p_jp2, p_image, p_manager)
}

/// JP2 encoder interface
pub(crate) fn opj_jp2_create(mut p_is_decoder: OPJ_BOOL) -> Option<opj_jp2> {
  /* create the J2K codec */
  let mut jp2 = opj_jp2 {
    j2k: if p_is_decoder == 0 {
      opj_j2k_create_compress()?
    } else {
      opj_j2k_create_decompress()?
    },
    w: 0,
    h: 0,
    bpc: 0,
    C: 0,
    UnkC: 0,
    IPR: 0,
    meth: 0,
    approx: 0,
    enumcs: 0,
    precedence: 0,
    brand: 0,
    minversion: 0,
    cl: Vec::new(),
    comps: Vec::new(),
    j2k_codestream_offset: 0,
    jpip_iptr_offset: 0,
    jpip_on: 0,
    jp2_state: 0,
    jp2_img_state: 0,
    ignore_pclr_cmap_cdef: 0,
    has_jp2h: 0,
    has_ihdr: 0,
    /* Color structure */
    color: opj_jp2_color {
      icc_profile: None,
      jp2_cdef: None,
      jp2_pclr: None,
      jp2_has_colr: 0 as OPJ_BYTE,
    },
  };
  Some(jp2)
}

#[cfg(feature = "file-io")]
pub(crate) fn jp2_dump(p_jp2: &mut opj_jp2, flag: OPJ_INT32, out_stream: *mut ::libc::FILE) {
  /* preconditions */
  j2k_dump(&mut p_jp2.j2k, flag, out_stream);
}

pub(crate) fn jp2_get_cstr_index(mut p_jp2: &mut opj_jp2) -> *mut opj_codestream_index_t {
  j2k_get_cstr_index(&mut p_jp2.j2k)
}

pub(crate) fn jp2_get_cstr_info(mut p_jp2: &mut opj_jp2) -> *mut opj_codestream_info_v2_t {
  j2k_get_cstr_info(&mut p_jp2.j2k)
}

pub(crate) fn opj_jp2_set_decoded_resolution_factor(
  p_jp2: &mut opj_jp2,
  res_factor: OPJ_UINT32,
  p_manager: &mut opj_event_mgr,
) -> OPJ_BOOL {
  opj_j2k_set_decoded_resolution_factor(&mut p_jp2.j2k, res_factor, p_manager)
}

pub(crate) fn opj_jp2_encoder_set_extra_options(
  p_jp2: &mut opj_jp2,
  options: &[&str],
  p_manager: &mut opj_event_mgr,
) -> bool {
  opj_j2k_encoder_set_extra_options(&mut p_jp2.j2k, options, p_manager)
}