openjp2 0.7.0

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
// 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) 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.
//! Tier-2 codec: packet header parsing/writing (inclusion, zero
//! bit-planes, coding passes, segment lengths) and the packet data I/O.
//! See `doc/jpeg2000-design-spec.md` §3.8.
use super::bio::*;
use super::consts::*;
use super::event::*;
use super::j2k::PacketInfo;
use super::math::*;
use super::openjpeg::*;
use super::pi::*;
use super::tcd::*;
use super::tgt::*;
use crate::image::CoreImage;

#[repr(C)]
pub(crate) struct T2<'a> {
  pub image: &'a mut CoreImage,
  pub cp: &'a mut Cp,
}
//
//

// T2 - Implementation of a tier-2 coding

// -----------------------------------------------------------------------
fn t2_putcommacode(bio: &mut Bio, mut n: i32) {
  for _ in 0..n {
    bio_putbit(bio, 1);
  }
  bio_putbit(bio, 0);
}

fn t2_getcommacode(bio: &mut Bio) -> u32 {
  let mut n = 0_u32;
  while bio_read(bio, 1_u32) != 0 {
    n += 1;
  }
  n
}

/// Variable length code for signalling delta Zil (truncation point)
/// `bio` -- Bit Input/Output component
/// `n` -- delta Zil
fn t2_putnumpasses(bio: &mut Bio, mut n: u32) {
  if n == 1u32 {
    bio_putbit(bio, 0);
  } else if n == 2u32 {
    bio_write(bio, 2_u32, 2_u32);
  } else if n <= 5u32 {
    bio_write(bio, 0xcu32 | n.wrapping_sub(3u32), 4_u32);
  } else if n <= 36u32 {
    bio_write(bio, 0x1e0u32 | n.wrapping_sub(6u32), 9_u32);
  } else if n <= 164u32 {
    bio_write(bio, 0xff80u32 | n.wrapping_sub(37u32), 16_u32);
  };
}

fn t2_getnumpasses(bio: &mut Bio) -> u32 {
  let mut n: u32 = 0;
  if bio_read(bio, 1_u32) == 0 {
    return 1_u32;
  }
  if bio_read(bio, 1_u32) == 0 {
    return 2_u32;
  }
  n = bio_read(bio, 2_u32);
  if n != 3u32 {
    return (3u32).wrapping_add(n);
  }
  n = bio_read(bio, 5_u32);
  if n != 31u32 {
    return (6u32).wrapping_add(n);
  }
  (37u32).wrapping_add(bio_read(bio, 7_u32))
}

// -----------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
pub(crate) fn t2_encode_packets(
  t2: &mut T2,
  mut p_tile_no: u32,
  tile: &mut TcdTile,
  mut p_maxlayers: u32,
  mut dest: &mut [u8],
  mut p_data_written: &mut u32,
  mut p_marker_info: Option<&mut TcdMarkerInfo>,
  mut p_tp_num: u32,
  mut p_tp_pos: i32,
  mut p_pino: u32,
  mut p_t2_mode: J2K_T2_MODE,
  mut p_manager: &mut EventMgr,
) -> OpResult {
  let mut dest = &mut dest[..];
  let image = &mut *t2.image;
  let cp = &mut *t2.cp;
  let mut l_nb_bytes = 0_u32;
  let mut compno: u32 = 0;
  let mut poc: u32 = 0;
  let pocno = if cp.rsiz as core::ffi::c_int == 0x4i32 {
    2i32
  } else {
    1i32
  } as u32;
  let l_max_comp = if cp.m_specific_param.m_enc.m_max_comp_size > 0u32 {
    image.numcomps
  } else {
    1u32
  };
  let mut pi_vec = pi_initialise_encode(image, cp, p_tile_no, p_t2_mode);
  if pi_vec.is_empty() {
    return Err(());
  }
  *p_data_written = 0_u32;
  if p_t2_mode as core::ffi::c_uint == THRESH_CALC as core::ffi::c_uint {
    /* Calculating threshold */
    compno = 0_u32;
    while compno < l_max_comp {
      let mut l_comp_len = 0_u32;
      poc = 0_u32;
      while poc < pocno {
        let mut l_tp_num = compno;
        /* Note: in v2 this cannot fail (allocations are infallible); the
        return value is kept for parity with v1 */
        pi_create_encode(
          pi_vec.as_mut_slice(),
          cp,
          p_tile_no,
          poc,
          l_tp_num,
          p_tp_pos,
          p_t2_mode,
        );
        /* (Invalid progression orders are rejected when the POC marker is
        read; the native enum cannot hold them.) */
        loop {
          let pi = &mut pi_vec[poc as usize];
          if pi_next(pi, p_manager).is_err() {
            break;
          }
          if pi.layno < p_maxlayers {
            l_nb_bytes = 0_u32;
            if t2_encode_packet(
              p_tile_no,
              tile,
              &mut cp.tcps[p_tile_no as usize],
              pi,
              &mut dest[..],
              &mut l_nb_bytes,
              p_t2_mode,
              p_manager,
            )
            .is_err()
            {
              pi_destroy(&mut pi_vec);
              return Err(());
            }
            l_comp_len = (l_comp_len as core::ffi::c_uint).wrapping_add(l_nb_bytes);
            let (_, rest) = dest.split_at_mut(l_nb_bytes as usize);
            dest = rest;
            *p_data_written = (*p_data_written as core::ffi::c_uint).wrapping_add(l_nb_bytes)
          }
        }
        if cp.m_specific_param.m_enc.m_max_comp_size != 0
          && l_comp_len > cp.m_specific_param.m_enc.m_max_comp_size
        {
          pi_destroy(&mut pi_vec);
          return Err(());
        }
        poc += 1;
      }
      compno += 1;
    }
  } else {
    pi_create_encode(
      pi_vec.as_mut_slice(),
      cp,
      p_tile_no,
      p_pino,
      p_tp_num,
      p_tp_pos,
      p_t2_mode,
    );
    /* (Invalid progression orders are rejected when the POC marker is
    read; the native enum cannot hold them.) */
    if let Some(marker_info) = p_marker_info.as_deref_mut() {
      if marker_info.need_plt {
        /* One time use intended */
        assert!(marker_info.packet_count == 0u32);
        assert!(marker_info.p_packet_size.is_empty());
        let count = get_encoding_packet_count(image, cp, p_tile_no) as usize;
        marker_info.p_packet_size = vec![0; count];
      }
    }
    loop {
      let pi = &mut pi_vec[p_pino as usize];
      if pi_next(pi, p_manager).is_err() {
        break;
      }
      if pi.layno < p_maxlayers {
        l_nb_bytes = 0_u32;
        if t2_encode_packet(
          p_tile_no,
          tile,
          &mut cp.tcps[p_tile_no as usize],
          pi,
          &mut dest[..],
          &mut l_nb_bytes,
          p_t2_mode,
          p_manager,
        )
        .is_err()
        {
          pi_destroy(&mut pi_vec);
          return Err(());
        }
        let (_, rest) = dest.split_at_mut(l_nb_bytes as usize);
        dest = rest;
        *p_data_written = (*p_data_written as core::ffi::c_uint).wrapping_add(l_nb_bytes);
        if let Some(marker_info) = p_marker_info.as_deref_mut() {
          if marker_info.need_plt {
            marker_info.p_packet_size[marker_info.packet_count as usize] = l_nb_bytes;
            marker_info.packet_count = marker_info.packet_count.wrapping_add(1)
          }
        }
        /* INDEX >> */
        /* << INDEX */
        tile.packno = tile.packno.wrapping_add(1)
      }
    }
  }
  pi_destroy(&mut pi_vec);
  Ok(())
}
pub(crate) fn t2_decode_packets(
  mut tcd: &mut Tcd,
  t2: &mut T2,
  mut p_tile_no: u32,
  mut src: &mut [u8],
  mut p_data_read: &mut u32,
  mut p_manager: &mut EventMgr,
  p_data_start: i64,
) -> Result<Vec<PacketInfo>, ()> {
  let mut packet_index: Vec<PacketInfo> = Vec::new();
  let p_data_read_initial = src.len();
  let image = &mut *t2.image;
  let cp = &mut *t2.cp;
  let mut src_base: usize = 0;
  let mut pino: u32 = 0;
  let mut l_nb_bytes_read: u32 = 0;
  /* create a packet iterator */
  let mut pi_vec = pi_create_decode(image, cp, p_tile_no);
  if pi_vec.is_empty() {
    return Err(());
  }
  let (num_layers_to_decode, numpocs) = {
    let tcp = &cp.tcps[p_tile_no as usize];
    (tcp.num_layers_to_decode, tcp.numpocs)
  };
  pino = 0_u32;
  while pino <= numpocs {
    /* if the resolution needed is too low, one dim of the tilec could be equal to zero
     * and no packets are used to decode this resolution and
     * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
     * and no l_img_comp->resno_decoded are computed
     */
    let mut first_pass_failed: Vec<bool> = Vec::new();
    /* (The C also rejects `poc.prg == ProgressionOrder::Unknown` here.  The native
    `Poc` cannot hold an invalid progression order: `read_poc` converts the
    raw byte with `ProgressionOrder::from`, which collapses invalid values to
    `Lrcp` - so the decode-time check is unrepresentable and the error path
    never fires.) */
    first_pass_failed = vec![true; image.numcomps as usize];
    loop {
      let pi = &mut pi_vec[pino as usize];
      if pi_next(pi, p_manager).is_err() {
        break;
      }
      let mut skip_packet = 0i32;
      log::debug!(
        "packet offset=00000166 prg={} cmptno={:02} rlvlno={:02} prcno={:03} lyrno={:02}",
        pi.poc.prg1 as core::ffi::c_int,
        pi.compno,
        pi.resno,
        pi.precno,
        pi.layno,
      );
      /* INDEX >> */
      /* << INDEX */
      if pi.layno >= num_layers_to_decode
        || pi.resno >= tcd.tcd_image.tiles.comps[pi.compno as usize].minimum_num_resolutions
      {
        skip_packet = 1i32
      } else {
        /* If the packet layer is greater or equal than the maximum */
        /* number of layers, skip the packet */
        /* If the packet resolution number is greater than the minimum */
        /* number of resolution allowed, skip the packet */
        /* If no precincts of any band intersects the area of interest, */
        /* skip the packet */
        let mut bandno: u32 = 0;
        let tilec = &tcd.tcd_image.tiles.comps[pi.compno as usize];
        let res = &tilec.resolutions[pi.resno as usize];
        skip_packet = 1i32;
        bandno = 0_u32;
        while bandno < res.numbands {
          let band = &res.bands[bandno as usize];
          if (pi.precno as usize) >= band.precincts.len() {
            event_msg!(p_manager, EVT_ERROR, "Invalid precinct\n",);
            pi_destroy(&mut pi_vec);
            return Err(());
          }
          let prec = &band.precincts[pi.precno as usize];
          if tcd_is_subband_area_of_interest(
            &TcdTilecView {
              win: [tcd.win_x0, tcd.win_y0, tcd.win_x1, tcd.win_y1],
              whole_tile_decoding: tcd.whole_tile_decoding,
              x0: tilec.x0,
              y0: tilec.y0,
              x1: tilec.x1,
              y1: tilec.y1,
              numresolutions: tilec.numresolutions,
              minimum_num_resolutions: tilec.minimum_num_resolutions,
              compno: pi.compno,
            },
            &cp.tcps[p_tile_no as usize].tccps[pi.compno as usize],
            &image.comps,
            pi.resno,
            band.bandno,
            prec.x0 as u32,
            prec.y0 as u32,
            prec.x1 as u32,
            prec.y1 as u32,
          ) {
            skip_packet = 0i32;
            break;
          } else {
            bandno += 1;
          }
        }
      }
      let mut l_header_len: u32 = 0;
      if skip_packet == 0 {
        l_nb_bytes_read = 0_u32;
        first_pass_failed[pi.compno as usize] = false;
        if t2_decode_packet(
          cp,
          p_tile_no,
          &mut tcd.tcd_image.tiles,
          pi,
          &mut src[..],
          src_base,
          &mut l_nb_bytes_read,
          &mut l_header_len,
          p_manager,
        )
        .is_err()
        {
          pi_destroy(&mut pi_vec);

          return Err(());
        }
        image.comps[pi.compno as usize].resno_decoded =
          uint_max(pi.resno, image.comps[pi.compno as usize].resno_decoded)
      } else {
        l_nb_bytes_read = 0_u32;
        if t2_skip_packet(
          cp,
          p_tile_no,
          &mut tcd.tcd_image.tiles,
          pi,
          &mut src[..],
          &mut l_nb_bytes_read,
          &mut l_header_len,
          p_manager,
        )
        .is_err()
        {
          pi_destroy(&mut pi_vec);

          return Err(());
        }
      }
      if first_pass_failed[pi.compno as usize]
        && image.comps[pi.compno as usize].resno_decoded == 0u32
      {
        image.comps[pi.compno as usize].resno_decoded = tcd.tcd_image.tiles.comps
          [pi.compno as usize]
          .minimum_num_resolutions
          .wrapping_sub(1u32)
      }
      let packet_start = p_data_start + src_base as i64;
      packet_index.push(PacketInfo {
        start_pos: packet_start,
        end_ph_pos: packet_start + l_header_len as i64,
        end_pos: packet_start + l_nb_bytes_read as i64,
        disto: 0.0,
        layno: pi.layno,
        resno: pi.resno,
        compno: pi.compno,
        precno: pi.precno,
      });
      let (_, rest) = src.split_at_mut(l_nb_bytes_read as usize);
      src = rest;
      src_base += l_nb_bytes_read as usize;
    }
    pino += 1;
  }
  /* don't forget to release pi */
  pi_destroy(&mut pi_vec);
  *p_data_read = (p_data_read_initial - src.len()) as u32;
  Ok(packet_index)
}
pub(crate) fn t2_create<'a>(image: &'a mut CoreImage, cp: &'a mut Cp) -> Box<T2<'a>> {
  /* create the t2 structure */
  Box::new(T2 { image, cp })
}

/// Decode a packet of a tile from a source buffer
/// `t2` -- T2 handle
/// `tile` -- Tile for which to write the packets
/// `tcp` -- Tile coding parameters
/// `pi` -- Packet identity
/// `src` -- Source buffer
/// `data_read`
/// `max_length`
/// `pack_info` -- Packet information
/// `p_manager` -- the user event manager
fn t2_decode_packet(
  cp: &mut Cp,
  p_tile_no: u32,
  tile: &mut TcdTile,
  pi: &mut PiIterator,
  src: &mut [u8],
  src_base: usize,
  p_data_read: &mut u32,
  p_header_len: &mut u32,
  p_manager: &mut EventMgr,
) -> OpResult {
  let mut l_read_data: bool = false;
  let mut l_nb_bytes_read = 0_u32;
  let mut l_nb_total_bytes_read = 0_u32;
  *p_data_read = 0_u32;
  if t2_read_packet_header(
    cp,
    p_tile_no,
    tile,
    pi,
    &mut l_read_data,
    src,
    &mut l_nb_bytes_read,
    p_manager,
  )
  .is_err()
  {
    return Err(());
  }
  *p_header_len = l_nb_bytes_read;
  let src = &mut src[l_nb_bytes_read as usize..];
  let src_base = src_base + l_nb_bytes_read as usize;
  l_nb_total_bytes_read =
    (l_nb_total_bytes_read as core::ffi::c_uint).wrapping_add(l_nb_bytes_read);
  /* we should read data for the packet */
  if l_read_data {
    l_nb_bytes_read = 0_u32;
    if t2_read_packet_data(cp, tile, pi, src, src_base, &mut l_nb_bytes_read, p_manager).is_err() {
      return Err(());
    }
    l_nb_total_bytes_read =
      (l_nb_total_bytes_read as core::ffi::c_uint).wrapping_add(l_nb_bytes_read)
  }
  *p_data_read = l_nb_total_bytes_read;
  Ok(())
}

/// Encode a packet of a tile to a destination buffer
/// `tileno` -- Number of the tile encoded
/// `tile` -- Tile for which to write the packets
/// `tcp` -- Tile coding parameters
/// `pi` -- Packet identity
/// `dest` -- Destination buffer
/// `p_data_written`
/// `len` -- Length of the destination buffer
/// `cstr_info` -- Codestream information structure
/// `p_t2_mode` -- If == THRESH_CALC In threshold calculation ,If == FINAL_PASS Final pass
/// `p_manager` -- the user event manager
fn t2_skip_packet(
  cp: &mut Cp,
  p_tile_no: u32,
  tile: &mut TcdTile,
  pi: &mut PiIterator,
  src: &mut [u8],
  p_data_read: &mut u32,
  p_header_len: &mut u32,
  p_manager: &mut EventMgr,
) -> OpResult {
  let mut l_read_data: bool = false;
  let mut l_nb_bytes_read = 0_u32;
  let mut l_nb_total_bytes_read = 0_u32;
  *p_data_read = 0_u32;
  if t2_read_packet_header(
    cp,
    p_tile_no,
    tile,
    pi,
    &mut l_read_data,
    src,
    &mut l_nb_bytes_read,
    p_manager,
  )
  .is_err()
  {
    return Err(());
  }
  *p_header_len = l_nb_bytes_read;
  let (_, src_rest) = src.split_at_mut(l_nb_bytes_read as usize);
  l_nb_total_bytes_read =
    (l_nb_total_bytes_read as core::ffi::c_uint).wrapping_add(l_nb_bytes_read);
  let p_max_length = src_rest.len() as u32;
  /* we should read data for the packet */
  if l_read_data {
    l_nb_bytes_read = 0_u32;
    if t2_skip_packet_data(cp, tile, pi, &mut l_nb_bytes_read, p_max_length, p_manager).is_err() {
      return Err(());
    }
    l_nb_total_bytes_read =
      (l_nb_total_bytes_read as core::ffi::c_uint).wrapping_add(l_nb_bytes_read)
  }
  *p_data_read = l_nb_total_bytes_read;
  Ok(())
}
fn t2_encode_packet(
  _tileno: u32,
  tile: &mut TcdTile,
  tcp: &mut Tcp,
  pi: &mut PiIterator,
  mut dest: &mut [u8],
  p_data_written: &mut u32,
  mut p_t2_mode: J2K_T2_MODE,
  mut p_manager: &mut EventMgr,
) -> OpResult {
  let mut bandno: u32 = 0; /* component value */
  let mut cblkno: u32 = 0; /* resolution level value */
  let mut pos = 0usize; /* precinct value */
  let mut length = dest.len() as u32;
  let mut l_nb_bytes: u32 = 0; /* quality layer value */
  let compno = pi.compno; /* BIO component */
  let resno = pi.resno;
  let precno = pi.precno;
  let layno = pi.layno;
  let mut l_nb_blocks: u32 = 0;
  let mut packet_empty = 0i32;
  let packno = tile.packno;
  /* <SOP 0xff91> */
  if tcp.csty & J2K_CP_CSTY_SOP != 0 {
    if length < 6u32 {
      if p_t2_mode as core::ffi::c_uint == FINAL_PASS as core::ffi::c_uint {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "t2_encode_packet(): only {} bytes remaining in output buffer. {} needed.\n",
          length,
          6i32,
        ); /* packno is uint32_t */
      }
      return Err(());
    }
    dest[0] = 255_u8;
    dest[1] = 145_u8;
    dest[2] = 0_u8;
    dest[3] = 4_u8;
    dest[4] = (packno >> 8i32 & 0xffu32) as u8;
    dest[5] = (packno & 0xffu32) as u8;
    pos += 6;
    length = (length as core::ffi::c_uint).wrapping_sub(6u32)
  }
  /* </SOP> */
  let tilec = &mut tile.comps[compno as usize];
  let res = &mut tilec.resolutions[resno as usize];
  if layno == 0 {
    bandno = 0_u32;
    while bandno < res.numbands {
      let band = &mut res.bands[bandno as usize];
      /* Skip empty bands */
      if !tcd_is_band_empty(band) {
        /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1294 */
        /* but likely not a proper fix. */
        if precno >= res.pw.wrapping_mul(res.ph) {
          event_msg!(
            p_manager,
            EVT_ERROR,
            "t2_encode_packet(): accessing precno={} >= {}\n",
            precno,
            res.pw.wrapping_mul(res.ph),
          );
          return Err(());
        }
        let prc = &mut band.precincts[precno as usize];
        /* A degenerate precinct (cw*ch == 0) has no tag trees; the C code no-ops here. */
        if let Some(tree) = prc.incltree.as_mut() {
          tgt_reset(tree);
        }
        if let Some(tree) = prc.imsbtree.as_mut() {
          tgt_reset(tree);
        }
        l_nb_blocks = prc.cw.wrapping_mul(prc.ch);
        cblkno = 0_u32;
        while cblkno < l_nb_blocks {
          let cblk = prc.cblks.enc_mut(cblkno as usize);
          cblk.numpasses = 0_u32;
          // SAFETY: imsbtree is the precinct's tag tree, owned by the precinct.
          tgt_setvalue(
            prc.imsbtree.as_mut().expect("imsbtree"),
            cblkno,
            band.numbps - cblk.numbps as i32,
          );
          cblkno += 1;
        }
      }
      bandno = bandno.wrapping_add(1);
    }
  }
  let mut bio = Bio::new_enc(&mut dest[pos..]);
  let bio = &mut bio;
  /* Empty header bit */
  bio_putbit(bio, if packet_empty != 0 { 0 } else { 1 });

  /* Writing Packet header */
  bandno = 0_u32;
  while packet_empty == 0 && bandno < res.numbands {
    let band = &mut res.bands[bandno as usize];
    /* Skip empty bands */
    if !tcd_is_band_empty(band) {
      /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1297 */
      /* but likely not a proper fix. */
      if precno >= res.pw.wrapping_mul(res.ph) {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "t2_encode_packet(): accessing precno={} >= {}\n",
          precno,
          res.pw.wrapping_mul(res.ph),
        );
        return Err(());
      }
      let prc = &mut band.precincts[precno as usize];
      l_nb_blocks = prc.cw.wrapping_mul(prc.ch);
      cblkno = 0_u32;
      while cblkno < l_nb_blocks {
        let cblk = prc.cblks.enc_mut(cblkno as usize);
        let layer = &mut cblk.layers[layno as usize];
        if cblk.numpasses == 0 && layer.numpasses != 0 {
          // SAFETY: incltree is the precinct's tag tree, owned by the precinct.
          tgt_setvalue(
            prc.incltree.as_mut().expect("incltree"),
            cblkno,
            layno as i32,
          );
        }
        cblkno += 1;
      }
      cblkno = 0_u32;
      while cblkno < l_nb_blocks {
        let cblk = prc.cblks.enc_mut(cblkno as usize);
        let mut increment = 0_u32;
        let mut nump = 0_u32;
        let mut len = 0_u32;
        let mut passno: u32 = 0;
        let mut l_nb_passes: u32 = 0;
        /* cblk inclusion bits */
        if cblk.numpasses == 0 {
          tgt_encode(
            bio,
            prc.incltree.as_mut().expect("incltree"),
            cblkno,
            layno.wrapping_add(1u32) as i32,
          );
        } else {
          bio_putbit(bio, (cblk.layers[layno as usize].numpasses != 0u32) as u32);
        }
        /* if cblk not included, go to the next cblk  */
        if cblk.layers[layno as usize].numpasses == 0 {
        } else {
          /* if first instance of cblk --> zero bit-planes information */
          if cblk.numpasses == 0 {
            cblk.numlenbits = 3_u32;
            tgt_encode(
              bio,
              prc.imsbtree.as_mut().expect("imsbtree"),
              cblkno,
              999i32,
            );
          }
          /* number of coding passes included */
          t2_putnumpasses(bio, cblk.layers[layno as usize].numpasses);
          l_nb_passes = cblk
            .numpasses
            .wrapping_add(cblk.layers[layno as usize].numpasses);
          /* computation of the increase of the length indicator and insertion in the header     */
          passno = cblk.numpasses;
          while passno < l_nb_passes {
            nump = nump.wrapping_add(1);
            len = (len as core::ffi::c_uint).wrapping_add(cblk.passes[passno as usize].len);
            if cblk.passes[passno as usize].term
              || passno
                == cblk
                  .numpasses
                  .wrapping_add(cblk.layers[layno as usize].numpasses)
                  .wrapping_sub(1u32)
            {
              increment = int_max(
                increment as i32,
                int_floorlog2(len as i32) + 1i32
                  - (cblk.numlenbits as i32 + int_floorlog2(nump as i32)),
              ) as u32;
              len = 0_u32;
              nump = 0_u32
            }
            passno += 1;
          }
          t2_putcommacode(bio, increment as i32);
          /* computation of the new Length indicator */
          cblk.numlenbits = (cblk.numlenbits as core::ffi::c_uint).wrapping_add(increment);
          /* insertion of the codeword segment length */
          passno = cblk.numpasses;
          while passno < l_nb_passes {
            nump = nump.wrapping_add(1);
            len = (len as core::ffi::c_uint).wrapping_add(cblk.passes[passno as usize].len);
            if cblk.passes[passno as usize].term
              || passno
                == cblk
                  .numpasses
                  .wrapping_add(cblk.layers[layno as usize].numpasses)
                  .wrapping_sub(1u32)
            {
              bio_write(
                bio,
                len,
                cblk
                  .numlenbits
                  .wrapping_add(int_floorlog2(nump as i32) as u32),
              );
              len = 0_u32;
              nump = 0_u32
            }
            passno += 1;
          }
        }
        cblkno += 1;
      }
    }
    bandno = bandno.wrapping_add(1);
  }
  if bio_flush(bio).is_err() {
    return Err(());
    /* modified to eliminate longjmp !! */
  }
  l_nb_bytes = bio_numbytes(bio) as u32;
  pos += l_nb_bytes as usize;
  length = (length as core::ffi::c_uint).wrapping_sub(l_nb_bytes);
  /* <EPH 0xff92> */
  if tcp.csty & J2K_CP_CSTY_EPH != 0 {
    if length < 2u32 {
      if p_t2_mode as core::ffi::c_uint == FINAL_PASS as core::ffi::c_uint {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "t2_encode_packet(): only {} bytes remaining in output buffer. {} needed.\n",
          length,
          2i32,
        );
      }
      return Err(());
    }
    dest[pos] = 255_u8;
    dest[pos + 1] = 146_u8;
    pos += 2;
    length = (length as core::ffi::c_uint).wrapping_sub(2u32)
  }
  /* </EPH> */
  /* << INDEX */
  /* End of packet header position. Currently only represents the distance to start of packet
  Will be updated later by incrementing with packet start value*/
  /* INDEX >> */
  /* Writing the packet body */
  bandno = 0_u32;
  while packet_empty == 0 && bandno < res.numbands {
    let band = &mut res.bands[bandno as usize];
    /* Skip empty bands */
    if !tcd_is_band_empty(band) {
      let prc = &mut band.precincts[precno as usize];
      l_nb_blocks = prc.cw.wrapping_mul(prc.ch);
      cblkno = 0_u32;
      while cblkno < l_nb_blocks {
        let cblk = prc.cblks.enc_mut(cblkno as usize);
        if cblk.layers[layno as usize].numpasses == 0 {
        } else {
          if cblk.layers[layno as usize].len > length {
            if p_t2_mode as core::ffi::c_uint == FINAL_PASS as core::ffi::c_uint {
              event_msg!(
                p_manager,
                EVT_ERROR,
                "t2_encode_packet(): only {} bytes remaining in output buffer. {} needed.\n",
                length,
                cblk.layers[layno as usize].len
              );
            }
            return Err(());
          }

          if p_t2_mode == FINAL_PASS {
            let len = cblk.layers[layno as usize].len as usize;
            let off = cblk.layers[layno as usize].data_offset;
            dest[pos..pos + len].copy_from_slice(&cblk.data[off..off + len]);
          }
          cblk.numpasses = (cblk.numpasses as core::ffi::c_uint)
            .wrapping_add(cblk.layers[layno as usize].numpasses);
          pos += cblk.layers[layno as usize].len as usize;
          length = (length as core::ffi::c_uint).wrapping_sub(cblk.layers[layno as usize].len);
          /* INDEX >> */
          /* << INDEX */
        }
        cblkno += 1;
      }
    }
    bandno += 1;
  }
  *p_data_written = pos as u32;
  Ok(())
}
fn t2_read_packet_header(
  cp: &mut Cp,
  p_tile_no: u32,
  tile: &mut TcdTile,
  pi: &mut PiIterator,
  p_is_data_present: &mut bool,
  p_src_data: &mut [u8],
  p_data_read: &mut u32,
  p_manager: &mut EventMgr,
) -> OpResult {
  /* loop */
  let mut bandno: u32 = 0; /* BIO component */
  let mut cblkno: u32 = 0;
  let mut l_nb_code_blocks: u32 = 0;
  let mut l_header_length: u32 = 0;
  let p_max_length = p_src_data.len() as u32;
  let mut l_pos = 0usize;
  let l_cp = cp;
  let csty = l_cp.tcps[p_tile_no as usize].csty;
  let tccp_cblksty = l_cp.tcps[p_tile_no as usize].tccps[pi.compno as usize].cblksty;
  let res = &mut tile.comps[pi.compno as usize].resolutions[pi.resno as usize];
  let mut l_present: u32 = 0;
  if pi.layno == 0u32 {
    /* reset tagtrees */
    bandno = 0_u32;
    while bandno < res.numbands {
      let band = &mut res.bands[bandno as usize];
      if !tcd_is_band_empty(band) {
        if (pi.precno as usize) >= band.precincts.len() {
          event_msg!(p_manager, EVT_ERROR, "Invalid precinct\n",);
          return Err(());
        }
        let prc = &mut band.precincts[pi.precno as usize];
        /* A degenerate precinct (cw*ch == 0) has no tag trees; the C code no-ops here. */
        if let Some(tree) = prc.incltree.as_mut() {
          tgt_reset(tree);
        }
        if let Some(tree) = prc.imsbtree.as_mut() {
          tgt_reset(tree);
        }
        l_nb_code_blocks = prc.cw.wrapping_mul(prc.ch);
        cblkno = 0_u32;
        while cblkno < l_nb_code_blocks {
          let cblk = prc.cblks.dec_mut(cblkno as usize);
          cblk.numsegs = 0_u32;
          cblk.real_num_segs = 0_u32;
          cblkno += 1;
        }
      }
      bandno += 1;
    }
  }

  /* SOP markers */
  if csty & J2K_CP_CSTY_SOP != 0 {
    if p_max_length < 6u32 {
      event_msg!(
        p_manager,
        EVT_WARNING,
        "Not enough space for expected SOP marker\n",
      );
    } else if p_src_data[l_pos] as core::ffi::c_int != 0xffi32
      || p_src_data[l_pos + 1] as core::ffi::c_int != 0x91i32
    {
      event_msg!(p_manager, EVT_WARNING, "Expected SOP marker\n",);
    } else {
      l_pos += 6
    }
    /* Note (work plan): the Nsop value is not validated */
  }

  /*
  When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
  This part deal with this characteristic
  step 1: Read packet header in the saved structure
  step 2: Return to codestream for decoding
  */
  /* The header lives either in the PPM buffer, the PPT buffer, or inline in the codestream. */
  let l_header: &mut [u8] = if l_cp.ppm {
    &mut l_cp.ppm_buffer[l_cp.ppm_data_pos..][..l_cp.ppm_len as usize]
  } else if l_cp.tcps[p_tile_no as usize].ppt {
    let tcp = &mut l_cp.tcps[p_tile_no as usize];
    &mut tcp.ppt_buffer[tcp.ppt_data_pos..][..tcp.ppt_len as usize]
  } else {
    &mut p_src_data[l_pos..]
  };
  let mut l_hdr_pos = 0usize;

  let mut l_bio = Bio::new_dec(&mut l_header[..]);
  let l_bio = &mut l_bio;

  l_present = bio_read(l_bio, 1_u32);
  log::debug!("present={}", l_present);
  if l_present == 0 {
    /* Note: no output check here (parity with upstream) */
    let _ = bio_inalign(l_bio);
    l_hdr_pos += bio_numbytes(l_bio) as usize;
    /* EPH markers */
    if csty & J2K_CP_CSTY_EPH != 0 {
      /* EPH markers are required when specified (upstream #1547) */
      if l_header.len() - l_hdr_pos < 2usize {
        event_msg!(
          p_manager,
          EVT_ERROR,
          "Not enough space for required EPH marker\n",
        );
        return Err(());
      } else if l_header[l_hdr_pos] as core::ffi::c_int != 0xffi32
        || l_header[l_hdr_pos + 1] as core::ffi::c_int != 0x92i32
      {
        event_msg!(p_manager, EVT_ERROR, "Expected EPH marker\n",);
        return Err(());
      } else {
        l_hdr_pos += 2
      }
    }
    l_header_length = l_hdr_pos as u32;
    if l_cp.ppm {
      l_cp.ppm_data_pos += l_header_length as usize;
      l_cp.ppm_len -= l_header_length;
    } else if l_cp.tcps[p_tile_no as usize].ppt {
      let tcp = &mut l_cp.tcps[p_tile_no as usize];
      tcp.ppt_data_pos += l_header_length as usize;
      tcp.ppt_len -= l_header_length;
    } else {
      l_pos += l_header_length as usize;
    }
    /* << INDEX */
    /* End of packet header position. Currently only represents the distance to start of packet
    Will be updated later by incrementing with packet start value */
    /* INDEX >> */
    *p_is_data_present = false;
    *p_data_read = l_pos as u32;
    return Ok(());
  }

  bandno = 0_u32;
  while bandno < res.numbands {
    let band = &mut res.bands[bandno as usize];
    if !tcd_is_band_empty(band) {
      if (pi.precno as usize) >= band.precincts.len() {
        event_msg!(p_manager, EVT_ERROR, "Invalid precinct\n",);
        return Err(());
      }
      let prc = &mut band.precincts[pi.precno as usize];
      l_nb_code_blocks = prc.cw.wrapping_mul(prc.ch);
      cblkno = 0_u32;
      while cblkno < l_nb_code_blocks {
        let mut l_included: u32 = 0;
        let mut l_increment: u32 = 0;
        let mut l_segno: u32 = 0;
        let mut n: i32 = 0;
        let cblk = prc.cblks.dec_mut(cblkno as usize);

        /* if cblk not yet included before --> inclusion tagtree */
        if cblk.numsegs == 0 {
          l_included = tgt_decode(
            l_bio,
            prc.incltree.as_mut().expect("incltree"),
            cblkno,
            pi.layno.wrapping_add(1u32) as i32,
          )
        /* else one bit */
        } else {
          l_included = bio_read(l_bio, 1_u32)
        }

        /* if cblk not included */
        if l_included == 0 {
          cblk.numnewpasses = 0_u32;
          log::debug!("included={}", l_included);
        } else {
          /* if cblk not yet included --> zero-bitplane tagtree */
          if cblk.numsegs == 0 {
            let mut i = 0_u32;
            while tgt_decode(
              l_bio,
              // SAFETY: imsbtree is the precinct's tag tree, owned by the precinct.
              prc.imsbtree.as_mut().expect("imsbtree"),
              cblkno,
              i as i32,
            ) == 0
            {
              i += 1;
            }
            cblk.mb = band.numbps as u32;
            /* Both C branches compute the same wrapping arithmetic; the
             * branch exists upstream only to document the PR #1488 integer
             * overflow while keeping the regression suite happy. */
            cblk.numbps = band.numbps.wrapping_add(1).wrapping_sub(i as i32) as u32;
            cblk.numlenbits = 3_u32
          }
          /* number of coding passes */
          cblk.numnewpasses = t2_getnumpasses(l_bio);
          l_increment = t2_getcommacode(l_bio);
          /* length indicator increment */
          cblk.numlenbits = (cblk.numlenbits as core::ffi::c_uint).wrapping_add(l_increment);
          l_segno = 0_u32;
          if cblk.numsegs == 0 {
            if t2_init_seg(cblk, l_segno, tccp_cblksty, 1_u32).is_err() {
              return Err(());
            }
          } else {
            l_segno = cblk.numsegs.wrapping_sub(1u32);
            if cblk.segs[l_segno as usize].numpasses == cblk.segs[l_segno as usize].maxpasses {
              l_segno = l_segno.wrapping_add(1);
              if t2_init_seg(cblk, l_segno, tccp_cblksty, 0_u32).is_err() {
                return Err(());
              }
            }
          }
          n = cblk.numnewpasses as i32;
          if tccp_cblksty & 0x40u32 != 0u32 {
            loop {
              let mut bit_number: u32 = 0;
              cblk.segs[l_segno as usize].numnewpasses =
                if l_segno == 0u32 { 1u32 } else { n as u32 };
              bit_number = cblk
                .numlenbits
                .wrapping_add(uint_floorlog2(cblk.segs[l_segno as usize].numnewpasses));
              if bit_number > 32u32 {
                event_msg!(
                  p_manager,
                  EVT_ERROR,
                  "Invalid bit number {} in t2_read_packet_header()\n",
                  bit_number,
                );
                return Err(());
              }
              cblk.segs[l_segno as usize].newlen = bio_read(l_bio, bit_number);
              log::debug!(
                "included={} numnewpasses={} increment={} len={}",
                l_included,
                cblk.segs[l_segno as usize].numnewpasses,
                l_increment,
                cblk.segs[l_segno as usize].newlen,
              );
              n -= cblk.segs[l_segno as usize].numnewpasses as i32;
              if n > 0i32 {
                l_segno = l_segno.wrapping_add(1);
                if t2_init_seg(cblk, l_segno, tccp_cblksty, 0_u32).is_err() {
                  return Err(());
                }
              }
              if n <= 0i32 {
                break;
              }
            }
          } else {
            loop {
              let mut bit_number_0: u32 = 0;
              cblk.segs[l_segno as usize].numnewpasses = int_min(
                cblk.segs[l_segno as usize]
                  .maxpasses
                  .wrapping_sub(cblk.segs[l_segno as usize].numpasses) as i32,
                n,
              ) as u32;
              bit_number_0 = cblk
                .numlenbits
                .wrapping_add(uint_floorlog2(cblk.segs[l_segno as usize].numnewpasses));
              if bit_number_0 > 32u32 {
                event_msg!(
                  p_manager,
                  EVT_ERROR,
                  "Invalid bit number {} in t2_read_packet_header()\n",
                  bit_number_0,
                );
                return Err(());
              }
              cblk.segs[l_segno as usize].newlen = bio_read(l_bio, bit_number_0);
              log::debug!(
                "included={} numnewpasses={} increment={} len={}",
                l_included,
                cblk.segs[l_segno as usize].numnewpasses,
                l_increment,
                cblk.segs[l_segno as usize].newlen,
              );
              n -= cblk.segs[l_segno as usize].numnewpasses as i32;
              if n > 0i32 {
                l_segno = l_segno.wrapping_add(1);
                if t2_init_seg(cblk, l_segno, tccp_cblksty, 0_u32).is_err() {
                  return Err(());
                }
              }
              if n <= 0i32 {
                break;
              }
            }
          }
        }
        cblkno += 1;
      }
    }
    bandno = bandno.wrapping_add(1);
  }
  if bio_inalign(l_bio).is_err() {
    return Err(());
  }
  l_hdr_pos += bio_numbytes(l_bio) as usize;
  /* EPH markers */
  if csty & J2K_CP_CSTY_EPH != 0 {
    /* EPH markers are required when specified (upstream #1547) */
    if l_header.len() - l_hdr_pos < 2usize {
      event_msg!(
        p_manager,
        EVT_ERROR,
        "Not enough space for required EPH marker\n",
      );
      return Err(());
    } else if l_header[l_hdr_pos] as core::ffi::c_int != 0xffi32
      || l_header[l_hdr_pos + 1] as core::ffi::c_int != 0x92i32
    {
      event_msg!(p_manager, EVT_ERROR, "Expected EPH marker\n",);
      return Err(());
    } else {
      l_hdr_pos += 2
    }
  }
  l_header_length = l_hdr_pos as u32;
  /* Do not allow a zero-length header in a non-zero-length packet
  (upstream #1526) */
  if l_header_length == 0 {
    return Err(());
  }
  log::debug!("hdrlen={}", l_header_length);
  log::debug!("packet body");
  if l_cp.ppm {
    l_cp.ppm_data_pos += l_header_length as usize;
    l_cp.ppm_len -= l_header_length;
  } else if l_cp.tcps[p_tile_no as usize].ppt {
    let tcp = &mut l_cp.tcps[p_tile_no as usize];
    tcp.ppt_data_pos += l_header_length as usize;
    tcp.ppt_len -= l_header_length;
  } else {
    l_pos += l_header_length as usize;
  }
  /* << INDEX */
  /* End of packet header position. Currently only represents the distance to start of packet
  Will be updated later by incrementing with packet start value */
  /* INDEX >> */
  *p_is_data_present = true; /* next code_block */
  *p_data_read = l_pos as u32;
  Ok(())
}
fn t2_read_packet_data(
  cp: &mut Cp,
  tile: &mut TcdTile,
  pi: &mut PiIterator,
  p_src_data: &[u8],
  src_base: usize,
  p_data_read: &mut u32,
  p_manager: &mut EventMgr,
) -> OpResult {
  let mut bandno: u32 = 0;
  let mut cblkno: u32 = 0;
  let mut l_nb_code_blocks: u32 = 0;
  let p_max_length = p_src_data.len() as u32;
  let mut l_pos = 0usize;
  let res = &mut tile.comps[pi.compno as usize].resolutions[pi.resno as usize];
  let mut partial_buffer = 0i32;
  bandno = 0_u32;
  while bandno < res.numbands {
    let band = &mut res.bands[bandno as usize];
    if band.x1 - band.x0 == 0i32 || band.y1 - band.y0 == 0i32 {
      bandno += 1;
      continue;
    }
    if (pi.precno as usize) >= band.precincts.len() {
      event_msg!(p_manager, EVT_ERROR, "Invalid precinct\n",);
      return Err(());
    }
    let prc = &mut band.precincts[pi.precno as usize];
    l_nb_code_blocks = prc.cw.wrapping_mul(prc.ch);
    cblkno = 0_u32;
    while cblkno < l_nb_code_blocks {
      let cblk = prc.cblks.dec_mut(cblkno as usize);
      if cblk.numnewpasses == 0 {
        /* nothing to do */
        cblkno += 1;
        continue;
      }
      // if a previous segment in this packet couldn't be decoded, or if this
      // code block was corrupted in a previous layer, then mark it corrupted
      // since we have no data to actually decode (upstream #1536).
      if partial_buffer != 0 || cblk.corrupted {
        cblk.numchunks = 0_u32;
        cblk.corrupted = true;
        cblkno += 1;
        continue;
      }
      {
        let mut l_segno: usize;
        if cblk.numsegs == 0 {
          l_segno = 0;
          cblk.numsegs = cblk.numsegs.wrapping_add(1)
        } else {
          l_segno = cblk.numsegs.wrapping_sub(1u32) as usize;
          if cblk.segs[l_segno].numpasses == cblk.segs[l_segno].maxpasses {
            l_segno += 1;
            cblk.numsegs = cblk.numsegs.wrapping_add(1)
          }
        }
        loop
        /* Check the remaining size of the source buffer */
        {
          let seg = &mut cblk.segs[l_segno];
          if seg.newlen as usize > p_src_data.len() - l_pos || partial_buffer != 0 {
            if cp.strict {
              event_msg!(p_manager, EVT_ERROR,
                                            "read: segment too long ({}) with max ({}) for codeblock {} (p={}, b={}, r={}, c={})\n",
                                            seg.newlen, p_max_length,
                                            cblkno, pi.precno, bandno,
                                            pi.resno, pi.compno);
              return Err(());
            } else {
              event_msg!(p_manager, EVT_WARNING,
                                            "read: segment too long ({}) with max ({}) for codeblock {} (p={}, b={}, r={}, c={})\n",
                                            seg.newlen, p_max_length,
                                            cblkno, pi.precno, bandno,
                                            pi.resno, pi.compno);
              /* skip this codeblock (and the following ones in this packet)
              since it is a partial read (upstream #1533) */
              partial_buffer = 1i32;
              cblk.corrupted = true;
              cblk.numchunks = 0_u32;
              break;
            }
          } else {
            if cblk.numchunks as usize == cblk.chunks.len() {
              let new_len = cblk.chunks.len() * 2 + 1;
              cblk.chunks.resize(
                new_len,
                TcdSegDataChunk {
                  data_offset: 0,
                  len: 0,
                },
              );
            }
            let chunk = &mut cblk.chunks[cblk.numchunks as usize];
            chunk.data_offset = src_base + l_pos;
            chunk.len = seg.newlen;
            cblk.numchunks = cblk.numchunks.wrapping_add(1);
            l_pos += seg.newlen as usize;
            seg.len = (seg.len as core::ffi::c_uint).wrapping_add(seg.newlen);
            seg.numpasses = (seg.numpasses as core::ffi::c_uint).wrapping_add(seg.numnewpasses);
            cblk.numnewpasses =
              (cblk.numnewpasses as core::ffi::c_uint).wrapping_sub(seg.numnewpasses);
            seg.real_num_passes = seg.numpasses;
            if cblk.numnewpasses > 0u32 {
              l_segno += 1;
              cblk.numsegs = cblk.numsegs.wrapping_add(1)
            }
          }
          if cblk.numnewpasses == 0u32 {
            break;
          }
        }
        cblk.real_num_segs = cblk.numsegs;
      }
      cblkno += 1;
    }
    bandno += 1;
  }
  // return the number of bytes read
  if partial_buffer != 0 {
    *p_data_read = p_max_length
  } else {
    *p_data_read = l_pos as u32
  }
  Ok(())
}

fn t2_skip_packet_data(
  cp: &mut Cp,
  tile: &mut TcdTile,
  pi: &mut PiIterator,
  p_data_read: &mut u32,
  p_max_length: u32,
  p_manager: &mut EventMgr,
) -> OpResult {
  let mut bandno: u32 = 0;
  let mut cblkno: u32 = 0;
  let mut l_nb_code_blocks: u32 = 0;
  let res = &mut tile.comps[pi.compno as usize].resolutions[pi.resno as usize];
  *p_data_read = 0_u32;
  bandno = 0_u32;
  while bandno < res.numbands {
    let band = &mut res.bands[bandno as usize];
    if band.x1 - band.x0 == 0i32 || band.y1 - band.y0 == 0i32 {
      bandno += 1;
      continue;
    }
    if (pi.precno as usize) >= band.precincts.len() {
      event_msg!(p_manager, EVT_ERROR, "Invalid precinct\n",);
      return Err(());
    }
    let prc = &mut band.precincts[pi.precno as usize];
    l_nb_code_blocks = prc.cw.wrapping_mul(prc.ch);
    cblkno = 0_u32;
    while cblkno < l_nb_code_blocks {
      let cblk = prc.cblks.dec_mut(cblkno as usize);
      if cblk.numnewpasses == 0 {
        /* nothing to do */
      } else {
        let mut l_segno: usize;
        if cblk.numsegs == 0 {
          l_segno = 0;
          cblk.numsegs = cblk.numsegs.wrapping_add(1)
        } else {
          l_segno = cblk.numsegs.wrapping_sub(1u32) as usize;
          if cblk.segs[l_segno].numpasses == cblk.segs[l_segno].maxpasses {
            l_segno += 1;
            cblk.numsegs = cblk.numsegs.wrapping_add(1)
          }
        }
        loop {
          /* Check possible overflow then size */
          if (*p_data_read).wrapping_add(cblk.segs[l_segno].newlen) < *p_data_read
            || (*p_data_read).wrapping_add(cblk.segs[l_segno].newlen) > p_max_length
          {
            if cp.strict {
              event_msg!(p_manager, EVT_ERROR,
                                            "skip: segment too long ({}) with max ({}) for codeblock {} (p={}, b={}, r={}, c={})\n",
                                            cblk.segs[l_segno].newlen, p_max_length,
                                            cblkno, pi.precno, bandno,
                                            pi.resno, pi.compno);
              return Err(());
            } else {
              event_msg!(p_manager, EVT_WARNING,
                                            "skip: segment too long ({}) with max ({}) for codeblock {} (p={}, b={}, r={}, c={})\n",
                                            cblk.segs[l_segno].newlen, p_max_length,
                                            cblkno, pi.precno, bandno,
                                            pi.resno, pi.compno);
              *p_data_read = p_max_length;
              return Ok(());
            }
          }
          log::debug!(
            "p_data_read ({}) newlen ({})",
            *p_data_read,
            cblk.segs[l_segno].newlen
          );
          *p_data_read =
            (*p_data_read as core::ffi::c_uint).wrapping_add(cblk.segs[l_segno].newlen);
          {
            let seg = &mut cblk.segs[l_segno];
            seg.numpasses = (seg.numpasses as core::ffi::c_uint).wrapping_add(seg.numnewpasses);
          }
          cblk.numnewpasses =
            (cblk.numnewpasses as core::ffi::c_uint).wrapping_sub(cblk.segs[l_segno].numnewpasses);
          if cblk.numnewpasses > 0u32 {
            l_segno += 1;
            cblk.numsegs = cblk.numsegs.wrapping_add(1)
          }
          if cblk.numnewpasses == 0u32 {
            break;
          }
        }
      }
      cblkno += 1;
    }
    bandno += 1;
  }
  Ok(())
}
fn t2_init_seg(cblk: &mut TcdCblkDec, index: u32, cblksty: u32, first: u32) -> OpResult {
  let l_nb_segs = index.wrapping_add(1u32);
  if l_nb_segs as usize > cblk.segs.len() {
    let new_len = cblk.segs.len() + 10;
    cblk.segs.resize(new_len, Default::default());
  }
  if cblksty & 0x4u32 != 0 {
    let seg = &mut cblk.segs[index as usize];
    tcd_reinit_segment(seg);
    seg.maxpasses = 1_u32
  } else if cblksty & 0x1u32 != 0 {
    if first != 0 {
      let seg = &mut cblk.segs[index as usize];
      tcd_reinit_segment(seg);
      seg.maxpasses = 10_u32
    } else {
      let prev_maxpasses = cblk.segs[index as usize - 1].maxpasses;
      let seg = &mut cblk.segs[index as usize];
      tcd_reinit_segment(seg);
      seg.maxpasses = if prev_maxpasses == 1u32 || prev_maxpasses == 10u32 {
        2i32
      } else {
        1i32
      } as u32
    }
  } else {
    /* See paragraph "B.10.6 Number of coding passes" of the standard.
     * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
     * Mb being the maximum number of bit-planes available for the
     * representation of coefficients in the sub-band */
    let seg = &mut cblk.segs[index as usize];
    tcd_reinit_segment(seg);
    seg.maxpasses = 109_u32
  }
  Ok(())
}