openjp2 0.3.4

Rust port of Openjpeg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/*
 * 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) 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
 * 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.
 */

pub use super::c_api_types::*;
use super::event::opj_event_mgr;
use super::j2k::*;
use super::jp2::*;
pub(crate) use super::types::*;

use super::codec::*;
pub use super::image::{opj_image_create, opj_image_destroy, opj_image_tile_create};
use super::malloc::*;

#[cfg(feature = "file-io")]
use super::cio::*;

#[cfg(feature = "file-io")]
use ::libc::{fclose, fopen, fread, fseeko, ftello, fwrite, FILE};

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

  fn memcpy(
    _: *mut core::ffi::c_void,
    _: *const core::ffi::c_void,
    _: usize,
  ) -> *mut core::ffi::c_void;
}
/* _WIN32 */
/* ---------------------------------------------------------------------- */
/* Functions to set the message handlers */
#[no_mangle]
pub unsafe fn opj_set_info_handler(
  mut p_codec: *mut opj_codec_t,
  mut p_callback: opj_msg_callback,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_info_handler(p_callback, p_user_data)
}

#[no_mangle]
pub unsafe fn opj_set_warning_handler(
  mut p_codec: *mut opj_codec_t,
  mut p_callback: opj_msg_callback,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_warning_handler(p_callback, p_user_data)
}

#[no_mangle]
pub unsafe fn opj_set_error_handler(
  mut p_codec: *mut opj_codec_t,
  mut p_callback: opj_msg_callback,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_error_handler(p_callback, p_user_data)
}

/* ---------------------------------------------------------------------- */
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_read_from_file(
  mut p_buffer: *mut core::ffi::c_void,
  mut p_nb_bytes: OPJ_SIZE_T,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_SIZE_T {
  let mut p_file = p_user_data as *mut FILE;
  let l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
  if l_nb_read != 0 {
    l_nb_read
  } else {
    -(1i32) as OPJ_SIZE_T
  }
}
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_get_data_length_from_file(
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_UINT64 {
  let mut p_file = p_user_data as *mut FILE;
  let mut file_length = 0 as OPJ_OFF_T;
  fseeko(p_file, 0, 2);
  file_length = ftello(p_file);
  fseeko(p_file, 0, 0);
  file_length as OPJ_UINT64
}
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_write_from_file(
  mut p_buffer: *mut core::ffi::c_void,
  mut p_nb_bytes: OPJ_SIZE_T,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_SIZE_T {
  let mut p_file = p_user_data as *mut FILE;
  fwrite(p_buffer, 1, p_nb_bytes, p_file)
}
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_skip_from_file(
  mut p_nb_bytes: OPJ_OFF_T,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_OFF_T {
  let mut p_file = p_user_data as *mut FILE;
  if fseeko(p_file, p_nb_bytes, 1i32) != 0 {
    return -(1i32) as OPJ_OFF_T;
  }
  p_nb_bytes
}
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_seek_from_file(
  mut p_nb_bytes: OPJ_OFF_T,
  mut p_user_data: *mut core::ffi::c_void,
) -> OPJ_BOOL {
  let mut p_file = p_user_data as *mut FILE;
  if fseeko(p_file, p_nb_bytes, 0i32) != 0 {
    return 0i32;
  }
  1i32
}
#[cfg(feature = "file-io")]
unsafe extern "C" fn opj_close_from_file(mut p_user_data: *mut core::ffi::c_void) {
  let mut p_file = p_user_data as *mut FILE;
  fclose(p_file);
}
/* ---------------------------------------------------------------------- */
/* _WIN32 */
/* ---------------------------------------------------------------------- */
pub const OPJ_VERSION: &str = "2.5.2";
pub const OPJ_VERSION_C: *const u8 = b"2.5.2\x00" as *const u8;

#[no_mangle]
pub unsafe fn opj_version() -> *const core::ffi::c_char {
  OPJ_VERSION_C as *const core::ffi::c_char
}

/* ---------------------------------------------------------------------- */
/* DECOMPRESSION FUNCTIONS*/
#[no_mangle]
pub unsafe fn opj_create_decompress(mut p_format: OPJ_CODEC_FORMAT) -> *mut opj_codec_t {
  let mut l_codec = Box::<opj_codec_private_t>::new_zeroed().assume_init();
  match p_format as core::ffi::c_int {
    0 => {
      if let Some(codec) = opj_j2k_create_decompress() {
        l_codec.m_codec = Codec::Decoder(CodecFormat::J2K(codec));
      } else {
        return std::ptr::null_mut();
      }
    }
    2 => {
      /* get a JP2 decoder handle */
      if let Some(codec) = opj_jp2_create(1i32) {
        l_codec.m_codec = Codec::Decoder(CodecFormat::JP2(codec));
      } else {
        return std::ptr::null_mut();
      }
    }
    -1 | 1 | _ => {
      return std::ptr::null_mut::<opj_codec_t>();
    }
  }
  l_codec.m_event_mgr.set_default_event_handler();
  Box::into_raw(l_codec) as *mut opj_codec_t
}
#[no_mangle]
pub unsafe fn opj_set_default_decoder_parameters(mut parameters: *mut opj_dparameters_t) {
  if !parameters.is_null() {
    memset(
      parameters as *mut core::ffi::c_void,
      0i32,
      core::mem::size_of::<opj_dparameters_t>(),
    );
    /* UniPG>> */
    /* USE_JPWL */
    /* <<UniPG */
    (*parameters).cp_layer = 0 as OPJ_UINT32;
    (*parameters).cp_reduce = 0 as OPJ_UINT32;
    (*parameters).decod_format = -(1i32);
    (*parameters).cod_format = -(1i32);
    (*parameters).flags = 0u32
  };
}

#[no_mangle]
pub unsafe fn opj_codec_set_threads(
  mut p_codec: *mut opj_codec_t,
  mut num_threads: core::ffi::c_int,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_threads(num_threads)
}

#[no_mangle]
pub unsafe fn opj_setup_decoder(
  mut p_codec: *mut opj_codec_t,
  mut parameters: *mut opj_dparameters_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.setup_decoder(parameters)
}

#[no_mangle]
pub unsafe fn opj_decoder_set_strict_mode(
  mut p_codec: *mut opj_codec_t,
  mut strict: OPJ_BOOL,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.decoder_set_strict_mode(strict)
}

#[no_mangle]
pub unsafe fn opj_read_header(
  mut p_stream: *mut opj_stream_t,
  mut p_codec: *mut opj_codec_t,
  mut p_image: *mut *mut opj_image_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.read_header(p_stream, p_image)
}

#[no_mangle]
pub unsafe fn opj_set_decoded_components(
  mut p_codec: *mut opj_codec_t,
  mut numcomps: OPJ_UINT32,
  mut comps_indices: *const OPJ_UINT32,
  mut apply_color_transforms: OPJ_BOOL,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_decoded_components(numcomps, comps_indices, apply_color_transforms)
}

#[no_mangle]
pub unsafe fn opj_decode(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
  mut p_image: *mut opj_image_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.decode(p_stream, p_image)
}

#[no_mangle]
pub unsafe fn opj_set_decode_area(
  mut p_codec: *mut opj_codec_t,
  mut p_image: *mut opj_image_t,
  mut p_start_x: OPJ_INT32,
  mut p_start_y: OPJ_INT32,
  mut p_end_x: OPJ_INT32,
  mut p_end_y: OPJ_INT32,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_decode_area(p_image, p_start_x, p_start_y, p_end_x, p_end_y)
}

#[no_mangle]
pub unsafe fn opj_read_tile_header(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
  mut p_tile_index: *mut OPJ_UINT32,
  mut p_data_size: *mut OPJ_UINT32,
  mut p_tile_x0: *mut OPJ_INT32,
  mut p_tile_y0: *mut OPJ_INT32,
  mut p_tile_x1: *mut OPJ_INT32,
  mut p_tile_y1: *mut OPJ_INT32,
  mut p_nb_comps: *mut OPJ_UINT32,
  mut p_should_go_on: *mut OPJ_BOOL,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.read_tile_header(
    p_stream,
    p_tile_index,
    p_data_size,
    p_tile_x0,
    p_tile_y0,
    p_tile_x1,
    p_tile_y1,
    p_nb_comps,
    p_should_go_on,
  )
}

#[no_mangle]
pub unsafe fn opj_decode_tile_data(
  mut p_codec: *mut opj_codec_t,
  mut p_tile_index: OPJ_UINT32,
  mut p_data: *mut OPJ_BYTE,
  mut p_data_size: OPJ_UINT32,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.decode_tile_data(p_stream, p_tile_index, p_data, p_data_size)
}

#[no_mangle]
pub unsafe fn opj_get_decoded_tile(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
  mut p_image: *mut opj_image_t,
  mut tile_index: OPJ_UINT32,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.get_decoded_tile(p_stream, p_image, tile_index)
}

#[no_mangle]
pub unsafe fn opj_set_decoded_resolution_factor(
  mut p_codec: *mut opj_codec_t,
  mut res_factor: OPJ_UINT32,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.set_decoded_resolution_factor(res_factor)
}

/* default decoding parameters */
/* ---------------------------------------------------------------------- */
/* COMPRESSION FUNCTIONS*/
#[no_mangle]
pub unsafe fn opj_create_compress(mut p_format: OPJ_CODEC_FORMAT) -> *mut opj_codec_t {
  let mut l_codec = Box::<opj_codec_private_t>::new_zeroed().assume_init();
  match p_format as core::ffi::c_int {
    0 => {
      if let Some(codec) = opj_j2k_create_compress() {
        l_codec.m_codec = Codec::Encoder(CodecFormat::J2K(codec));
      } else {
        return std::ptr::null_mut();
      }
    }
    2 => {
      /* get a JP2 decoder handle */
      if let Some(codec) = opj_jp2_create(0i32) {
        l_codec.m_codec = Codec::Encoder(CodecFormat::JP2(codec));
      } else {
        return std::ptr::null_mut();
      }
    }
    -1 | 1 | _ => {
      return std::ptr::null_mut::<opj_codec_t>();
    }
  }
  l_codec.m_event_mgr.set_default_event_handler();
  Box::into_raw(l_codec) as *mut opj_codec_t
}

/* default coding parameters */
#[no_mangle]
pub unsafe fn opj_set_default_encoder_parameters(mut parameters: *mut opj_cparameters_t) {
  if !parameters.is_null() {
    memset(
      parameters as *mut core::ffi::c_void,
      0i32,
      core::mem::size_of::<opj_cparameters_t>(),
    );
    /* UniPG>> */
    /* USE_JPWL */
    /* <<UniPG */
    (*parameters).cp_cinema = OPJ_OFF;
    (*parameters).rsiz = 0 as OPJ_UINT16;
    (*parameters).max_comp_size = 0i32;
    (*parameters).numresolution = 6i32;
    (*parameters).cp_rsiz = OPJ_STD_RSIZ;
    (*parameters).cblockw_init = 64i32;
    (*parameters).cblockh_init = 64i32;
    (*parameters).prog_order = OPJ_LRCP;
    (*parameters).roi_compno = -(1i32);
    (*parameters).subsampling_dx = 1i32;
    (*parameters).subsampling_dy = 1i32;
    (*parameters).tp_on = 0 as core::ffi::c_char;
    (*parameters).decod_format = -(1i32);
    (*parameters).cod_format = -(1i32);
    (*parameters).tcp_rates[0_usize] = 0 as core::ffi::c_float;
    (*parameters).tcp_numlayers = 0i32;
    (*parameters).cp_disto_alloc = 0i32;
    (*parameters).cp_fixed_alloc = 0i32;
    (*parameters).cp_fixed_quality = 0i32;
    (*parameters).jpip_on = 0i32
  };
}

#[no_mangle]
pub unsafe fn opj_setup_encoder(
  mut p_codec: *mut opj_codec_t,
  mut parameters: *mut opj_cparameters_t,
  mut p_image: *mut opj_image_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.setup_encoder(parameters, p_image)
}

/* ----------------------------------------------------------------------- */
#[no_mangle]
pub unsafe fn opj_encoder_set_extra_options(
  mut p_codec: *mut opj_codec_t,
  mut options: *const *const core::ffi::c_char,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.encoder_set_extra_options(options)
}

/* ----------------------------------------------------------------------- */
#[no_mangle]
pub unsafe fn opj_start_compress(
  mut p_codec: *mut opj_codec_t,
  mut p_image: *mut opj_image_t,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.start_compress(p_image, p_stream)
}

#[no_mangle]
pub unsafe fn opj_encode(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.encode(p_stream)
}

#[no_mangle]
pub unsafe fn opj_end_compress(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.end_compress(p_stream)
}

#[no_mangle]
pub unsafe fn opj_end_decompress(
  mut p_codec: *mut opj_codec_t,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.end_decompress(p_stream)
}

#[no_mangle]
pub unsafe fn opj_set_MCT(
  mut parameters: *mut opj_cparameters_t,
  mut pEncodingMatrix: *mut OPJ_FLOAT32,
  mut p_dc_shift: *mut OPJ_INT32,
  mut pNbComp: OPJ_UINT32,
) -> OPJ_BOOL {
  let mut l_matrix_size = pNbComp
    .wrapping_mul(pNbComp)
    .wrapping_mul(core::mem::size_of::<OPJ_FLOAT32>() as OPJ_UINT32);
  let mut l_dc_shift_size = pNbComp.wrapping_mul(core::mem::size_of::<OPJ_INT32>() as OPJ_UINT32);
  let mut l_mct_total_size = l_matrix_size.wrapping_add(l_dc_shift_size);
  /* add MCT capability */
  if (*parameters).rsiz as core::ffi::c_int & 0x8000i32 != 0 {
    (*parameters).rsiz = ((*parameters).rsiz as core::ffi::c_int | 0x100i32) as OPJ_UINT16
  } else {
    (*parameters).rsiz = (0x8000i32 | 0x100i32) as OPJ_UINT16
  }
  (*parameters).irreversible = 1i32;
  /* use array based MCT */
  (*parameters).tcp_mct = 2 as core::ffi::c_char;
  (*parameters).mct_data = opj_malloc(l_mct_total_size as size_t);
  if (*parameters).mct_data.is_null() {
    return 0i32;
  }
  memcpy(
    (*parameters).mct_data,
    pEncodingMatrix as *const core::ffi::c_void,
    l_matrix_size as usize,
  );
  memcpy(
    ((*parameters).mct_data as *mut OPJ_BYTE).offset(l_matrix_size as isize)
      as *mut core::ffi::c_void,
    p_dc_shift as *const core::ffi::c_void,
    l_dc_shift_size as usize,
  );
  1i32
}

#[no_mangle]
pub unsafe fn opj_write_tile(
  mut p_codec: *mut opj_codec_t,
  mut p_tile_index: OPJ_UINT32,
  mut p_data: *mut OPJ_BYTE,
  mut p_data_size: OPJ_UINT32,
  mut p_stream: *mut opj_stream_t,
) -> OPJ_BOOL {
  if p_codec.is_null() {
    return 0i32;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.write_tile(p_tile_index, p_data, p_data_size, p_stream)
}

/* ---------------------------------------------------------------------- */
#[no_mangle]
pub unsafe fn opj_destroy_codec(mut p_codec: *mut opj_codec_t) {
  if p_codec.is_null() {
    return;
  }
  let _ = Box::from_raw(p_codec as *mut opj_codec_private_t);
}

/* ---------------------------------------------------------------------- */
#[cfg(feature = "file-io")]
#[no_mangle]
pub unsafe fn opj_dump_codec(
  mut p_codec: *mut opj_codec_t,
  mut info_flag: OPJ_INT32,
  mut output_stream: *mut FILE,
) {
  if p_codec.is_null() {
    return;
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.dump_codec(info_flag, output_stream)
}

#[no_mangle]
pub unsafe fn opj_get_cstr_info(mut p_codec: *mut opj_codec_t) -> *mut opj_codestream_info_v2_t {
  if p_codec.is_null() {
    return std::ptr::null_mut::<opj_codestream_info_v2_t>();
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.get_cstr_info()
}

#[no_mangle]
pub unsafe fn opj_destroy_cstr_info(mut cstr_info: *mut *mut opj_codestream_info_v2_t) {
  if !cstr_info.is_null() {
    if !(**cstr_info).m_default_tile_info.tccp_info.is_null() {
      opj_free((**cstr_info).m_default_tile_info.tccp_info as *mut core::ffi::c_void);
    }
    if !(**cstr_info).tile_info.is_null() {
      opj_free((**cstr_info).tile_info as *mut core::ffi::c_void);
    }
    opj_free(*cstr_info as *mut core::ffi::c_void);
    *cstr_info = std::ptr::null_mut::<opj_codestream_info_v2_t>()
  };
}

#[no_mangle]
pub unsafe fn opj_get_cstr_index(mut p_codec: *mut opj_codec_t) -> *mut opj_codestream_index_t {
  if p_codec.is_null() {
    return std::ptr::null_mut::<opj_codestream_index_t>();
  }
  let mut l_codec = &mut *(p_codec as *mut opj_codec_private_t);
  l_codec.get_cstr_index()
}

#[no_mangle]
pub unsafe fn opj_destroy_cstr_index(mut p_cstr_index: *mut *mut opj_codestream_index_t) {
  if !(*p_cstr_index).is_null() {
    j2k_destroy_cstr_index(*p_cstr_index);
    *p_cstr_index = std::ptr::null_mut::<opj_codestream_index_t>()
  };
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_create(
  mut p_buffer_size: OPJ_SIZE_T,
  mut l_is_input: OPJ_BOOL,
) -> *mut opj_stream_t {
  let mut l_stream = std::ptr::null_mut::<opj_stream_private_t>();
  l_stream = opj_calloc(1i32 as size_t, core::mem::size_of::<opj_stream_private_t>())
    as *mut opj_stream_private_t;
  if l_stream.is_null() {
    return std::ptr::null_mut::<opj_stream_t>();
  }
  (*l_stream).m_buffer_size = p_buffer_size;
  (*l_stream).m_stored_data = opj_malloc(p_buffer_size) as *mut OPJ_BYTE;
  if (*l_stream).m_stored_data.is_null() {
    opj_free(l_stream as *mut core::ffi::c_void);
    return std::ptr::null_mut::<opj_stream_t>();
  }
  (*l_stream).m_current_data = (*l_stream).m_stored_data;
  if l_is_input != 0 {
    (*l_stream).m_status |= 0x2u32;
    (*l_stream).m_opj_skip = Some(
      opj_stream_read_skip
        as unsafe extern "C" fn(
          _: *mut opj_stream_private_t,
          _: OPJ_OFF_T,
          _: &mut opj_event_mgr,
        ) -> OPJ_OFF_T,
    );
    (*l_stream).m_opj_seek = Some(
      opj_stream_read_seek
        as unsafe extern "C" fn(
          _: *mut opj_stream_private_t,
          _: OPJ_OFF_T,
          _: &mut opj_event_mgr,
        ) -> OPJ_BOOL,
    )
  } else {
    (*l_stream).m_status |= 0x1u32;
    (*l_stream).m_opj_skip = Some(
      opj_stream_write_skip
        as unsafe extern "C" fn(
          _: *mut opj_stream_private_t,
          _: OPJ_OFF_T,
          _: &mut opj_event_mgr,
        ) -> OPJ_OFF_T,
    );
    (*l_stream).m_opj_seek = Some(
      opj_stream_write_seek
        as unsafe extern "C" fn(
          _: *mut opj_stream_private_t,
          _: OPJ_OFF_T,
          _: &mut opj_event_mgr,
        ) -> OPJ_BOOL,
    )
  }
  (*l_stream).m_read_fn = Some(
    opj_stream_default_read
      as unsafe extern "C" fn(
        _: *mut core::ffi::c_void,
        _: OPJ_SIZE_T,
        _: *mut core::ffi::c_void,
      ) -> OPJ_SIZE_T,
  );
  (*l_stream).m_write_fn = Some(
    opj_stream_default_write
      as unsafe extern "C" fn(
        _: *mut core::ffi::c_void,
        _: OPJ_SIZE_T,
        _: *mut core::ffi::c_void,
      ) -> OPJ_SIZE_T,
  );
  (*l_stream).m_skip_fn = Some(
    opj_stream_default_skip
      as unsafe extern "C" fn(_: OPJ_OFF_T, _: *mut core::ffi::c_void) -> OPJ_OFF_T,
  );
  (*l_stream).m_seek_fn = Some(
    opj_stream_default_seek
      as unsafe extern "C" fn(_: OPJ_OFF_T, _: *mut core::ffi::c_void) -> OPJ_BOOL,
  );
  l_stream as *mut opj_stream_t
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_default_create(mut l_is_input: OPJ_BOOL) -> *mut opj_stream_t {
  opj_stream_create(0x100000 as OPJ_SIZE_T, l_is_input)
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_destroy(mut p_stream: *mut opj_stream_t) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if !l_stream.is_null() {
    if (*l_stream).m_free_user_data_fn.is_some() {
      (*l_stream)
        .m_free_user_data_fn
        .expect("non-null function pointer")((*l_stream).m_user_data);
    }
    opj_free((*l_stream).m_stored_data as *mut core::ffi::c_void);
    (*l_stream).m_stored_data = std::ptr::null_mut::<OPJ_BYTE>();
    opj_free(l_stream as *mut core::ffi::c_void);
  };
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_read_function(
  mut p_stream: *mut opj_stream_t,
  mut p_function: opj_stream_read_fn,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() || (*l_stream).m_status & 0x2u32 == 0 {
    return;
  }
  (*l_stream).m_read_fn = p_function;
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_seek_function(
  mut p_stream: *mut opj_stream_t,
  mut p_function: opj_stream_seek_fn,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() {
    return;
  }
  (*l_stream).m_seek_fn = p_function;
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_write_function(
  mut p_stream: *mut opj_stream_t,
  mut p_function: opj_stream_write_fn,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() || (*l_stream).m_status & 0x1u32 == 0 {
    return;
  }
  (*l_stream).m_write_fn = p_function;
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_skip_function(
  mut p_stream: *mut opj_stream_t,
  mut p_function: opj_stream_skip_fn,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() {
    return;
  }
  (*l_stream).m_skip_fn = p_function;
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_user_data(
  mut p_stream: *mut opj_stream_t,
  mut p_data: *mut core::ffi::c_void,
  mut p_function: opj_stream_free_user_data_fn,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() {
    return;
  }
  (*l_stream).m_user_data = p_data;
  (*l_stream).m_free_user_data_fn = p_function;
}

#[no_mangle]
pub unsafe extern "C" fn opj_stream_set_user_data_length(
  mut p_stream: *mut opj_stream_t,
  mut data_length: OPJ_UINT64,
) {
  let mut l_stream = p_stream as *mut opj_stream_private_t;
  if l_stream.is_null() {
    return;
  }
  (*l_stream).m_user_data_length = data_length;
}

#[cfg(feature = "file-io")]
#[no_mangle]
pub unsafe fn opj_stream_create_default_file_stream(
  mut fname: *const core::ffi::c_char,
  mut p_is_read_stream: OPJ_BOOL,
) -> *mut opj_stream_t {
  opj_stream_create_file_stream(fname, 0x100000 as OPJ_SIZE_T, p_is_read_stream)
}

#[cfg(feature = "file-io")]
#[no_mangle]
pub unsafe fn opj_stream_create_file_stream(
  mut fname: *const core::ffi::c_char,
  mut p_size: OPJ_SIZE_T,
  mut p_is_read_stream: OPJ_BOOL,
) -> *mut opj_stream_t {
  let mut l_stream = std::ptr::null_mut::<opj_stream_t>();
  let mut p_file = std::ptr::null_mut::<FILE>();
  let mut mode = std::ptr::null::<core::ffi::c_char>();
  if fname.is_null() {
    return std::ptr::null_mut::<opj_stream_t>();
  }
  if p_is_read_stream != 0 {
    mode = b"rb\x00" as *const u8 as *const core::ffi::c_char
  } else {
    mode = b"wb\x00" as *const u8 as *const core::ffi::c_char
  }
  p_file = fopen(fname, mode);
  if p_file.is_null() {
    return std::ptr::null_mut::<opj_stream_t>();
  }
  l_stream = opj_stream_create(p_size, p_is_read_stream);
  if l_stream.is_null() {
    fclose(p_file);
    return std::ptr::null_mut::<opj_stream_t>();
  }
  opj_stream_set_user_data(
    l_stream,
    p_file as *mut core::ffi::c_void,
    Some(opj_close_from_file as unsafe extern "C" fn(_: *mut core::ffi::c_void) -> ()),
  );
  opj_stream_set_user_data_length(
    l_stream,
    opj_get_data_length_from_file(p_file as *mut core::ffi::c_void),
  );
  opj_stream_set_read_function(
    l_stream,
    Some(
      opj_read_from_file
        as unsafe extern "C" fn(
          _: *mut core::ffi::c_void,
          _: OPJ_SIZE_T,
          _: *mut core::ffi::c_void,
        ) -> OPJ_SIZE_T,
    ),
  );
  opj_stream_set_write_function(
    l_stream,
    core::mem::transmute::<
      Option<
        unsafe extern "C" fn(
          _: *mut core::ffi::c_void,
          _: OPJ_SIZE_T,
          _: *mut core::ffi::c_void,
        ) -> OPJ_SIZE_T,
      >,
      opj_stream_write_fn,
    >(Some(
      opj_write_from_file
        as unsafe extern "C" fn(
          _: *mut core::ffi::c_void,
          _: OPJ_SIZE_T,
          _: *mut core::ffi::c_void,
        ) -> OPJ_SIZE_T,
    )),
  );
  opj_stream_set_skip_function(
    l_stream,
    Some(
      opj_skip_from_file
        as unsafe extern "C" fn(_: OPJ_OFF_T, _: *mut core::ffi::c_void) -> OPJ_OFF_T,
    ),
  );
  opj_stream_set_seek_function(
    l_stream,
    Some(
      opj_seek_from_file
        as unsafe extern "C" fn(_: OPJ_OFF_T, _: *mut core::ffi::c_void) -> OPJ_BOOL,
    ),
  );
  l_stream
}
#[no_mangle]
pub unsafe fn opj_image_data_alloc(mut size: OPJ_SIZE_T) -> *mut core::ffi::c_void {
  /* printf("opj_image_data_alloc %p\n", ret); */
  opj_aligned_malloc(size)
}
#[no_mangle]
pub unsafe fn opj_image_data_free(mut ptr: *mut core::ffi::c_void) {
  /* printf("opj_image_data_free %p\n", ptr); */
  opj_aligned_free(ptr);
}

/* Stub implementation */
#[no_mangle]
pub fn opj_has_thread_support() -> OPJ_BOOL {
  0i32
}
#[no_mangle]
pub fn opj_get_num_cpus() -> core::ffi::c_int {
  1i32
}