Skip to main content

openjp2/
params.rs

1//! The C-ABI encoder parameters mirror (`opj_cparameters_t`). The core uses
2//! the safe owned `openjp2_core::EncoderParameters`; this module owns the
3//! `#[repr(C)]` struct C callers fill in, plus the marshalling setters.
4
5use openjp2_ctypes::*;
6use openjp2_ctypes::{OPJ_BOOL, OPJ_UINT16, OPJ_UINT32};
7use openjp2_ctypes::consts::opj::OPJ_PATH_LEN;
8
9extern "C" {
10  fn malloc(size: usize) -> *mut core::ffi::c_void;
11  fn free(ptr: *mut core::ffi::c_void);
12}
13
14pub(crate) fn c_malloc(size: usize) -> *mut core::ffi::c_void {
15  if size == 0 {
16    return core::ptr::null_mut();
17  }
18  // SAFETY: plain C malloc.
19  unsafe { malloc(size) }
20}
21
22pub(crate) fn c_free(ptr: *mut core::ffi::c_void) {
23  if !ptr.is_null() {
24    // SAFETY: the pointer came from c_malloc.
25    unsafe { free(ptr) }
26  }
27}
28
29// Note: no `Clone`/`Copy` — the `Drop` impl frees the raw pointer fields
30// (`cp_matrice`, `cp_comment`, `mct_data`), so a bitwise clone would
31// double-free them (the old #10 bug; the C ABI never clones this struct).
32#[repr(C)]
33#[derive(Debug)]
34pub struct opj_cparameters {
35  pub tile_size_on: OPJ_BOOL,
36  pub cp_tx0: core::ffi::c_int,
37  pub cp_ty0: core::ffi::c_int,
38  pub cp_tdx: core::ffi::c_int,
39  pub cp_tdy: core::ffi::c_int,
40  pub cp_disto_alloc: core::ffi::c_int,
41  pub cp_fixed_alloc: core::ffi::c_int,
42  pub cp_fixed_quality: core::ffi::c_int,
43  pub cp_matrice: *mut core::ffi::c_int,
44  pub cp_comment: *mut core::ffi::c_char,
45  pub csty: core::ffi::c_int,
46  pub prog_order: OPJ_PROG_ORDER,
47  pub POC: [opj_poc_t; 32],
48  pub numpocs: OPJ_UINT32,
49  pub tcp_numlayers: core::ffi::c_int,
50  pub tcp_rates: [core::ffi::c_float; 100],
51  pub tcp_distoratio: [core::ffi::c_float; 100],
52  pub numresolution: core::ffi::c_int,
53  pub cblockw_init: core::ffi::c_int,
54  pub cblockh_init: core::ffi::c_int,
55  pub mode: core::ffi::c_int,
56  pub irreversible: core::ffi::c_int,
57  pub roi_compno: core::ffi::c_int,
58  pub roi_shift: core::ffi::c_int,
59  pub res_spec: core::ffi::c_int,
60  pub prcw_init: [core::ffi::c_int; 33],
61  pub prch_init: [core::ffi::c_int; 33],
62  pub infile: [core::ffi::c_char; OPJ_PATH_LEN as usize],
63  pub outfile: [core::ffi::c_char; OPJ_PATH_LEN as usize],
64  pub index_on: core::ffi::c_int,
65  pub index: [core::ffi::c_char; OPJ_PATH_LEN as usize],
66  pub image_offset_x0: core::ffi::c_int,
67  pub image_offset_y0: core::ffi::c_int,
68  pub subsampling_dx: core::ffi::c_int,
69  pub subsampling_dy: core::ffi::c_int,
70  pub decod_format: core::ffi::c_int,
71  pub cod_format: core::ffi::c_int,
72  pub jpwl_epc_on: OPJ_BOOL,
73  pub jpwl_hprot_MH: core::ffi::c_int,
74  pub jpwl_hprot_TPH_tileno: [core::ffi::c_int; 16],
75  pub jpwl_hprot_TPH: [core::ffi::c_int; 16],
76  pub jpwl_pprot_tileno: [core::ffi::c_int; 16],
77  pub jpwl_pprot_packno: [core::ffi::c_int; 16],
78  pub jpwl_pprot: [core::ffi::c_int; 16],
79  pub jpwl_sens_size: core::ffi::c_int,
80  pub jpwl_sens_addr: core::ffi::c_int,
81  pub jpwl_sens_range: core::ffi::c_int,
82  pub jpwl_sens_MH: core::ffi::c_int,
83  pub jpwl_sens_TPH_tileno: [core::ffi::c_int; 16],
84  pub jpwl_sens_TPH: [core::ffi::c_int; 16],
85  pub cp_cinema: OPJ_CINEMA_MODE,
86  pub max_comp_size: core::ffi::c_int,
87  pub cp_rsiz: OPJ_RSIZ_CAPABILITIES,
88  pub tp_on: core::ffi::c_char,
89  pub tp_flag: core::ffi::c_char,
90  pub tcp_mct: core::ffi::c_char,
91  pub jpip_on: OPJ_BOOL,
92  pub mct_data: *mut core::ffi::c_void,
93  pub max_cs_size: core::ffi::c_int,
94  pub rsiz: OPJ_UINT16,
95}
96pub type opj_cparameters_t = opj_cparameters;
97
98impl Default for opj_cparameters_t {
99  fn default() -> Self {
100    Self {
101      tile_size_on: Default::default(),
102      cp_tx0: Default::default(),
103      cp_ty0: Default::default(),
104      cp_tdx: Default::default(),
105      cp_tdy: Default::default(),
106      cp_disto_alloc: Default::default(),
107      cp_fixed_alloc: Default::default(),
108      cp_fixed_quality: Default::default(),
109      cp_matrice: core::ptr::null_mut(),
110      cp_comment: core::ptr::null_mut(),
111      csty: Default::default(),
112      prog_order: OPJ_LRCP,
113      POC: Default::default(),
114      numpocs: Default::default(),
115      tcp_numlayers: Default::default(),
116      tcp_rates: [Default::default(); 100],
117      tcp_distoratio: [Default::default(); 100],
118      numresolution: 6i32,
119      cblockw_init: 64i32,
120      cblockh_init: 64i32,
121      mode: Default::default(),
122      irreversible: Default::default(),
123      roi_compno: -1,
124      roi_shift: Default::default(),
125      res_spec: Default::default(),
126      prcw_init: [Default::default(); 33],
127      prch_init: [Default::default(); 33],
128      infile: [0; OPJ_PATH_LEN as usize],
129      outfile: [0; OPJ_PATH_LEN as usize],
130      index_on: Default::default(),
131      index: [0; OPJ_PATH_LEN as usize],
132      image_offset_x0: Default::default(),
133      image_offset_y0: Default::default(),
134      subsampling_dx: 1,
135      subsampling_dy: 1,
136      decod_format: -1,
137      cod_format: -1,
138      jpwl_epc_on: 0,
139      jpwl_hprot_MH: 0,
140      jpwl_hprot_TPH_tileno: [0; 16],
141      jpwl_hprot_TPH: [0; 16],
142      jpwl_pprot_tileno: [0; 16],
143      jpwl_pprot_packno: [0; 16],
144      jpwl_pprot: [0; 16],
145      jpwl_sens_size: 0,
146      jpwl_sens_addr: 0,
147      jpwl_sens_range: 0,
148      jpwl_sens_MH: 0,
149      jpwl_sens_TPH_tileno: [0; 16],
150      jpwl_sens_TPH: [0; 16],
151      cp_cinema: OPJ_OFF,
152      max_comp_size: Default::default(),
153      cp_rsiz: OPJ_STD_RSIZ,
154      tp_on: Default::default(),
155      tp_flag: Default::default(),
156      tcp_mct: Default::default(),
157      jpip_on: Default::default(),
158      mct_data: core::ptr::null_mut(),
159      max_cs_size: Default::default(),
160      rsiz: Default::default(),
161    }
162  }
163}
164
165
166impl opj_cparameters_t {
167  pub fn set_defaults(&mut self) {
168    *self = Default::default()
169  }
170
171  pub fn set_comment(&mut self, comment: &str) {
172    let comment = std::ffi::CString::new(comment).expect("CString::new failed");
173    if !self.cp_comment.is_null() {
174      // SAFETY: a previous set_comment allocated this string.
175      unsafe {
176        drop(std::ffi::CString::from_raw(self.cp_comment));
177      }
178    }
179    self.cp_comment = comment.into_raw();
180  }
181
182  pub fn set_MCT(
183    &mut self,
184    encoding_matrix: &[f32],
185    dc_shift: &[i32],
186    nb_comps: u32,
187  ) -> bool {
188    let l_matrix_size = core::mem::size_of_val(encoding_matrix);
189    let l_dc_shift_size = core::mem::size_of_val(dc_shift);
190    let l_mct_total_size = l_matrix_size + l_dc_shift_size;
191    /* add MCT capability */
192    if self.rsiz as core::ffi::c_int & 0x8000i32 != 0 {
193      self.rsiz = (self.rsiz as core::ffi::c_int | 0x100i32) as OPJ_UINT16
194    } else {
195      self.rsiz = (0x8000i32 | 0x100i32) as OPJ_UINT16
196    }
197    self.irreversible = 1i32;
198    /* use array based MCT */
199    self.tcp_mct = 2 as core::ffi::c_char;
200    self.mct_data = c_malloc(l_mct_total_size);
201    if self.mct_data.is_null() {
202      return false;
203    }
204    // SAFETY: mct_data was allocated with exactly l_mct_total_size bytes above.
205    unsafe {
206      core::slice::from_raw_parts_mut(self.mct_data as *mut f32, encoding_matrix.len())
207        .copy_from_slice(encoding_matrix);
208      core::slice::from_raw_parts_mut(
209        (self.mct_data as *mut u8).add(l_matrix_size) as *mut i32,
210        nb_comps as usize,
211      )
212      .copy_from_slice(dc_shift);
213    }
214    true
215  }
216
217  pub fn get_matrix_mut(&mut self, nb_comps: usize) -> &mut [f32] {
218    let l_matrix_size = nb_comps * nb_comps;
219    // SAFETY: the buffer was sized for nb_comps^2 floats by set_MCT.
220    unsafe { core::slice::from_raw_parts_mut(self.mct_data as *mut f32, l_matrix_size) }
221  }
222
223  pub fn get_dc_shift_mut(&mut self, nb_comps: usize) -> &mut [i32] {
224    let l_matrix_byte_size = nb_comps * nb_comps * core::mem::size_of::<OPJ_FLOAT32>();
225    // SAFETY: the dc shifts follow the matrix in the set_MCT buffer.
226    unsafe {
227      core::slice::from_raw_parts_mut(
228        (self.mct_data as *mut u8).add(l_matrix_byte_size) as *mut i32,
229        nb_comps,
230      )
231    }
232  }
233}
234
235impl Drop for opj_cparameters {
236  fn drop(&mut self) {
237    if !self.cp_matrice.is_null() {
238      c_free(self.cp_matrice as *mut core::ffi::c_void);
239      self.cp_matrice = core::ptr::null_mut();
240    }
241    if !self.cp_comment.is_null() {
242      // SAFETY: allocated by set_comment.
243      unsafe {
244        drop(std::ffi::CString::from_raw(self.cp_comment));
245      }
246      self.cp_comment = core::ptr::null_mut();
247    }
248    if !self.mct_data.is_null() {
249      c_free(self.mct_data);
250      self.mct_data = core::ptr::null_mut();
251    }
252  }
253}