Skip to main content

openjp2/
codec.rs

1//! The merged codec handle behind the single `opj_codec_t*` C-ABI pointer
2//! contract.
3//!
4//! The core split its merged `Codec` into `openjp2::Decoder`/`Encoder` (0.7)
5//! with a C-type-free `Result` surface.  This crate keeps one handle type by
6//! wrapping either direction, and is the only place that translates between
7//! the C-shaped parameters (`opj_dparameters_t`, `opj_cparameters_t`) and the
8//! core API.
9
10use openjp2_core::{
11  DecodeArea, DecodeLimits, Decoder, DecoderSettings, Encoder, HeaderImage, J2KFormat,
12};
13use openjp2_ctypes::OPJ_BOOL;
14
15/// The `opj_codec_t*` handle: either a decompressor or a compressor.
16pub enum Codec {
17  Dec(Decoder),
18  Enc(Encoder),
19}
20
21fn map_format(format: openjp2_ctypes::OPJ_CODEC_FORMAT) -> Option<J2KFormat> {
22  match format {
23    openjp2_ctypes::OPJ_CODEC_J2K => Some(J2KFormat::J2K),
24    openjp2_ctypes::OPJ_CODEC_JP2 => Some(J2KFormat::JP2),
25    _ => None,
26  }
27}
28
29impl Codec {
30  pub fn new_decoder(format: openjp2_ctypes::OPJ_CODEC_FORMAT) -> Option<Self> {
31    Some(Codec::Dec(Decoder::new(map_format(format)?).ok()?))
32  }
33
34  pub fn new_encoder(format: openjp2_ctypes::OPJ_CODEC_FORMAT) -> Option<Self> {
35    Some(Codec::Enc(Encoder::new(map_format(format)?).ok()?))
36  }
37
38  pub fn set_info_handler(&mut self, handler: impl FnMut(&str) + Send + 'static) -> OPJ_BOOL {
39    match self {
40      Codec::Dec(dec) => dec.set_info_handler(handler),
41      Codec::Enc(enc) => enc.set_info_handler(handler),
42    }
43    1i32
44  }
45
46  pub fn set_warning_handler(&mut self, handler: impl FnMut(&str) + Send + 'static) -> OPJ_BOOL {
47    match self {
48      Codec::Dec(dec) => dec.set_warning_handler(handler),
49      Codec::Enc(enc) => enc.set_warning_handler(handler),
50    }
51    1i32
52  }
53
54  pub fn set_error_handler(&mut self, handler: impl FnMut(&str) + Send + 'static) -> OPJ_BOOL {
55    match self {
56      Codec::Dec(dec) => dec.set_error_handler(handler),
57      Codec::Enc(enc) => enc.set_error_handler(handler),
58    }
59    1i32
60  }
61
62  pub fn set_threads(&mut self, num_threads: core::ffi::c_int) -> OPJ_BOOL {
63    let ok = match self {
64      Codec::Dec(dec) => dec.set_threads(num_threads),
65      Codec::Enc(enc) => enc.set_threads(num_threads),
66    };
67    ok.is_ok() as OPJ_BOOL
68  }
69
70  pub fn dump_codec(&mut self, info_flag: i32, output_stream: &mut impl openjp2_core::StringWriter) {
71    match self {
72      Codec::Dec(dec) => dec.dump_codec(info_flag, output_stream),
73      Codec::Enc(enc) => enc.dump_codec(info_flag, output_stream),
74    }
75  }
76
77  pub fn get_cstr_info_v2(&self) -> Option<openjp2_core::CstrInfoV2> {
78    match self {
79      Codec::Dec(dec) => dec.cstr_info(),
80      Codec::Enc(enc) => enc.cstr_info(),
81    }
82  }
83
84  pub fn get_cstr_index_owned(&self) -> Option<openjp2_core::CodestreamIndex> {
85    match self {
86      Codec::Dec(dec) => dec.cstr_index(),
87      Codec::Enc(enc) => enc.cstr_index(),
88    }
89  }
90
91  // ---- decompressor surface (wrong-direction calls reproduce the C's
92  //      error events through the arm's event manager) ----
93
94  pub fn setup_decoder(&mut self, parameters: &mut openjp2_ctypes::opj_dparameters_t) -> OPJ_BOOL {
95    match self {
96      Codec::Dec(dec) => {
97        let settings = DecoderSettings {
98          layers: parameters.cp_layer,
99          reduce: parameters.cp_reduce,
100          ignore_pclr_cmap_cdef:
101            parameters.flags & openjp2_ctypes::consts::opj::OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG
102              != 0,
103          dump_state: parameters.flags & openjp2_ctypes::consts::opj::OPJ_DPARAMETERS_DUMP_FLAG
104            != 0,
105          /* The C-ABI thread control stays the process-wide pool
106          (opj_codec_set_threads); the native per-decode limit is unused
107          here. */
108          threads: 0,
109        };
110        dec.apply_settings(&settings).is_ok() as OPJ_BOOL
111      }
112      Codec::Enc(enc) => {
113        enc.emit_error_event(
114          "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n",
115        );
116        0
117      }
118    }
119  }
120
121  pub fn decoder_set_strict_mode(&mut self, strict: OPJ_BOOL) -> OPJ_BOOL {
122    match self {
123      Codec::Dec(dec) => dec.set_strict(strict != 0).is_ok() as OPJ_BOOL,
124      Codec::Enc(enc) => {
125        enc.emit_error_event(
126          "Codec provided to the opj_decoder_set_strict_mode function is not a decompressor handler.\n",
127        );
128        0
129      }
130    }
131  }
132
133  pub fn set_decode_limits(&mut self, limits: DecodeLimits) -> OPJ_BOOL {
134    match self {
135      Codec::Dec(dec) => dec.set_limits(limits).is_ok() as OPJ_BOOL,
136      Codec::Enc(enc) => {
137        enc.emit_error_event(
138          "Codec provided to the opj_set_decode_limits function is not a decompressor handler.\n",
139        );
140        0
141      }
142    }
143  }
144
145  pub fn read_header(
146    &mut self,
147    p_stream: &mut openjp2_core::Stream,
148  ) -> Option<openjp2_core::CoreImage> {
149    match self {
150      Codec::Dec(dec) => dec.read_header(p_stream).ok().map(HeaderImage::into_core_image),
151      Codec::Enc(enc) => {
152        enc.emit_error_event(
153          "Codec provided to the opj_read_header function is not a decompressor handler.\n",
154        );
155        None
156      }
157    }
158  }
159
160  pub fn set_decoded_components(
161    &mut self,
162    components: &[u32],
163    apply_color_transforms: OPJ_BOOL,
164  ) -> OPJ_BOOL {
165    match self {
166      Codec::Dec(dec) => {
167        if apply_color_transforms != 0 {
168          dec.emit_error_event("apply_color_transforms = OPJ_TRUE is not supported.\n");
169          return 0;
170        }
171        dec.set_components(components).is_ok() as OPJ_BOOL
172      }
173      Codec::Enc(enc) => {
174        enc.emit_error_event(
175          "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n",
176        );
177        0
178      }
179    }
180  }
181
182  pub fn decode(
183    &mut self,
184    p_stream: &mut openjp2_core::Stream,
185  ) -> Option<openjp2_core::CoreImage> {
186    match self {
187      Codec::Dec(dec) => dec.decode(p_stream).ok(),
188      Codec::Enc(_) => None,
189    }
190  }
191
192  pub fn end_decompress(&mut self, p_stream: &mut openjp2_core::Stream) -> OPJ_BOOL {
193    match self {
194      Codec::Dec(dec) => dec.end_decompress(p_stream).is_ok() as OPJ_BOOL,
195      Codec::Enc(_) => 0,
196    }
197  }
198
199  pub fn set_decode_area(
200    &mut self,
201    p_image: &mut openjp2_core::CoreImage,
202    p_start_x: i32,
203    p_start_y: i32,
204    p_end_x: i32,
205    p_end_y: i32,
206  ) -> OPJ_BOOL {
207    match self {
208      Codec::Dec(dec) => dec
209        .set_decode_area(
210          p_image,
211          Some(DecodeArea::new(
212            p_start_x as u32,
213            p_start_y as u32,
214            p_end_x as u32,
215            p_end_y as u32,
216          )),
217        )
218        .is_ok() as OPJ_BOOL,
219      Codec::Enc(_) => 0,
220    }
221  }
222
223  pub fn read_tile_header(
224    &mut self,
225    p_stream: &mut openjp2_core::Stream,
226  ) -> Option<openjp2_core::TileInfo> {
227    match self {
228      Codec::Dec(dec) => dec.read_tile_header(p_stream).ok(),
229      Codec::Enc(_) => None,
230    }
231  }
232
233  pub fn decode_tile_data(
234    &mut self,
235    p_stream: &mut openjp2_core::Stream,
236    p_tile_index: u32,
237    p_data: Option<&mut [u8]>,
238  ) -> OPJ_BOOL {
239    match self {
240      Codec::Dec(dec) => dec.decode_tile_data(p_stream, p_tile_index, p_data).is_ok() as OPJ_BOOL,
241      Codec::Enc(_) => 0,
242    }
243  }
244
245  pub fn get_decoded_tile(
246    &mut self,
247    p_stream: &mut openjp2_core::Stream,
248    tile_index: u32,
249  ) -> Option<openjp2_core::CoreImage> {
250    match self {
251      Codec::Dec(dec) => dec.get_decoded_tile(p_stream, tile_index).ok(),
252      Codec::Enc(_) => None,
253    }
254  }
255
256  pub fn set_decoded_resolution_factor(&mut self, res_factor: u32) -> OPJ_BOOL {
257    match self {
258      Codec::Dec(dec) => dec.set_resolution_factor(res_factor).is_ok() as OPJ_BOOL,
259      Codec::Enc(_) => 0,
260    }
261  }
262
263  // ---- compressor surface ----
264
265  pub fn setup_encoder(
266    &mut self,
267    parameters: &mut openjp2_core::EncoderParameters,
268    p_image: &mut openjp2_core::CoreImage,
269  ) -> OPJ_BOOL {
270    match self {
271      Codec::Enc(enc) => enc.setup(parameters, p_image).is_ok() as OPJ_BOOL,
272      Codec::Dec(_) => 0,
273    }
274  }
275
276  pub fn encoder_set_extra_options(&mut self, options: &[&str]) -> bool {
277    match self {
278      Codec::Enc(enc) => enc.extra_options(options).is_ok(),
279      Codec::Dec(_) => false,
280    }
281  }
282
283  pub fn start_compress(
284    &mut self,
285    p_image: &mut openjp2_core::CoreImage,
286    p_stream: &mut openjp2_core::Stream,
287  ) -> OPJ_BOOL {
288    match self {
289      Codec::Enc(enc) => enc.start(p_image, p_stream).is_ok() as OPJ_BOOL,
290      Codec::Dec(_) => 0,
291    }
292  }
293
294  pub fn encode(&mut self, p_stream: &mut openjp2_core::Stream) -> OPJ_BOOL {
295    match self {
296      Codec::Enc(enc) => enc.encode(p_stream).is_ok() as OPJ_BOOL,
297      Codec::Dec(_) => 0,
298    }
299  }
300
301  pub fn end_compress(&mut self, p_stream: &mut openjp2_core::Stream) -> OPJ_BOOL {
302    match self {
303      Codec::Enc(enc) => enc.finish(p_stream).is_ok() as OPJ_BOOL,
304      Codec::Dec(_) => 0,
305    }
306  }
307
308  pub fn write_tile(
309    &mut self,
310    p_tile_index: u32,
311    p_data: &[u8],
312    p_stream: &mut openjp2_core::Stream,
313  ) -> OPJ_BOOL {
314    match self {
315      Codec::Enc(enc) => enc.write_tile(p_tile_index, p_data, p_stream).is_ok() as OPJ_BOOL,
316      Codec::Dec(_) => 0,
317    }
318  }
319}