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
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
use std::ptr;

use log::{log_enabled, Level};

use super::*;

/// The area of the source image to decode.
///
/// This is useful for loading a small part of a
/// very large image.
///
/// ```rust
/// let area = DecodeArea::new(10, 10, 200, 200);
///
/// // or from a string:
/// let area: DecodeArea = "10:10:200:200".parse()?;
/// let area = DecodeArea::from_str("10:10:200:200")?;
/// ```
#[derive(Default, Clone, Copy)]
pub struct DecodeArea {
  start_x: u32,
  start_y: u32,
  end_x: u32,
  end_y: u32,
}

impl std::str::FromStr for DecodeArea {
  type Err = anyhow::Error;
  fn from_str(s: &str) -> Result<Self, Self::Err> {
    let dim = s
      .splitn(4, ":")
      .map(|v| v.parse::<u32>())
      .collect::<Result<Vec<u32>, _>>()?;
    Ok(Self {
      start_x: dim.get(0).copied().unwrap_or(0),
      start_y: dim.get(1).copied().unwrap_or(0),
      end_x: dim.get(2).copied().unwrap_or(0),
      end_y: dim.get(3).copied().unwrap_or(0),
    })
  }
}

impl DecodeArea {
  pub fn new(start_x: u32, start_y: u32, end_x: u32, end_y: u32) -> Self {
    Self {
      start_x,
      start_y,
      end_x,
      end_y,
    }
  }
}

#[derive(Clone, Copy)]
pub struct DecodeParameters {
  params: sys::opj_dparameters,
  area: Option<DecodeArea>,
  strict: bool,
}

impl Default for DecodeParameters {
  fn default() -> Self {
    let params = unsafe {
      let mut ptr = std::mem::zeroed::<sys::opj_dparameters>();
      sys::opj_set_default_decoder_parameters(&mut ptr as *mut _);
      ptr
    };
    Self {
      params,
      area: Default::default(),
      strict: false,
    }
  }
}

impl DecodeParameters {
  pub fn new() -> Self {
    Default::default()
  }

  /// How much to reduce the image's resolution.
  ///
  /// If `reduce == 0`, image is decoded to the full resolution.  This is the default.
  /// If `reduce > 0`, then original dimension divided by 2^(reduce)
  pub fn reduce(mut self, reduce: u32) -> Self {
    self.params.cp_reduce = reduce;
    self
  }

  /// Enable/disable strict decoing mode.
  ///
  /// If disabled then progressive downloading is supported (truncated codestreams).  This is the default.
  /// If enabled then partial/truncated codestreams will return an error.
  pub fn strict(mut self, strict: bool) -> Self {
    self.strict = strict;
    self
  }

  /// The number of quality layers to decode.
  ///
  /// If there are less quality layers than the specified number,
  /// all the quality layers are decoded.
  ///
  /// If `layers == 0`, all the quality layers are decoded.  This is the default.
  /// If `layers > 0`, then only the first `layers` layers are decoded.
  pub fn layers(mut self, layers: u32) -> Self {
    self.params.cp_layer = layers;
    self
  }

  /// The area to decode.
  ///
  /// If `area == None`, then the whole image will be decoded.  This is the defult.
  pub fn decode_area(mut self, area: Option<DecodeArea>) -> Self {
    self.area = area;
    self
  }

  pub(crate) fn as_ptr(&mut self) -> &mut sys::opj_dparameters {
    &mut self.params
  }
}

#[derive(Clone, Copy)]
pub struct EncodeParameters(sys::opj_cparameters);

impl Default for EncodeParameters {
  fn default() -> Self {
    Self(unsafe {
      let mut ptr = std::mem::zeroed::<sys::opj_cparameters>();
      sys::opj_set_default_encoder_parameters(&mut ptr as *mut _);
      ptr
    })
  }
}

pub struct CodestreamTilePartIndex(pub(crate) sys::opj_tp_index_t);

impl std::fmt::Debug for CodestreamTilePartIndex {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("CodestreamTilePartIndex")
      .field("start_pos", &self.0.start_pos)
      .field("end_header", &self.0.end_header)
      .field("end_pos", &self.0.end_pos)
      .finish()
  }
}

pub struct CodestreamPacketInfo(pub(crate) sys::opj_packet_info_t);

impl std::fmt::Debug for CodestreamPacketInfo {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("CodestreamPacketInfo")
      .field("start_pos", &self.0.start_pos)
      .field("end_ph_pos", &self.0.end_ph_pos)
      .field("end_pos", &self.0.end_pos)
      .field("disto", &self.0.disto)
      .finish()
  }
}

pub struct CodestreamMarker(pub(crate) sys::opj_marker_info_t);

impl std::fmt::Debug for CodestreamMarker {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("CodestreamMarker")
      .field("type", &self.0.type_)
      .field("pos", &self.0.pos)
      .field("len", &self.0.len)
      .finish()
  }
}

pub struct TileCodingParamInfo(ptr::NonNull<sys::opj_tccp_info_t>);

impl std::fmt::Debug for TileCodingParamInfo {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let info = self.as_ref();
    f.debug_struct("TileCodingParamInfo")
      .field("compno", &info.compno)
      .field("csty", &info.csty)
      .field("numresolutions", &info.numresolutions)
      .field("cblkw", &info.cblkw)
      .field("cblkh", &info.cblkh)
      .field("cblksty", &info.cblksty)
      .field("qmfbid", &info.qmfbid)
      .field("qntsty", &info.qntsty)
      .field("stepsizes_mant", &info.stepsizes_mant)
      .field("stepsizes_expn", &info.stepsizes_expn)
      .field("numgbits", &info.numgbits)
      .field("roishift", &info.roishift)
      .field("prcw", &info.prcw)
      .field("prch", &info.prch)
      .finish()
  }
}

impl TileCodingParamInfo {
  fn as_ref(&self) -> &sys::opj_tccp_info_t {
    unsafe { &(*self.0.as_ref()) }
  }
}

pub struct TileInfo<'a>(pub(crate) &'a sys::opj_tile_info_v2_t);

impl<'a> std::fmt::Debug for TileInfo<'a> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("TileInfo")
      .field("tileno", &self.0.tileno)
      .field("csty", &self.0.csty)
      .field("prg", &self.0.prg)
      .field("numlayers", &self.0.numlayers)
      .field("mct", &self.0.mct)
      .field("tccp_info", &self.tccp_info())
      .finish()
  }
}

impl<'a> TileInfo<'a> {
  fn tccp_info(&self) -> Option<TileCodingParamInfo> {
    ptr::NonNull::new(self.0.tccp_info).map(|info| TileCodingParamInfo(info))
  }
}

pub struct CodestreamTileIndex(pub(crate) sys::opj_tile_index_t);

impl std::fmt::Debug for CodestreamTileIndex {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("CodestreamTileIndex")
      .field("tileno", &self.0.tileno)
      .field("nb_tps", &self.0.nb_tps)
      .field("current_nb_tps", &self.0.current_nb_tps)
      .field("current_tpsno", &self.0.current_tpsno)
      .field("tp_index", &self.tile_parts())
      .field("marknum", &self.0.marknum)
      .field("marker", &self.markers())
      .field("maxmarknum", &self.0.maxmarknum)
      .field("nb_packet", &self.0.nb_packet)
      .field("packet_info", &self.packets())
      .finish()
  }
}

impl CodestreamTileIndex {
  /// Tile part index.
  pub fn tile_parts(&self) -> &[CodestreamTilePartIndex] {
    let num = self.0.nb_tps;
    unsafe {
      std::slice::from_raw_parts(
        self.0.tp_index as *mut CodestreamTilePartIndex,
        num as usize,
      )
    }
  }

  /// Tile markers.
  pub fn markers(&self) -> &[CodestreamMarker] {
    let num = self.0.marknum;
    unsafe { std::slice::from_raw_parts(self.0.marker as *mut CodestreamMarker, num as usize) }
  }

  /// Codestream packet info.
  pub fn packets(&self) -> &[CodestreamPacketInfo] {
    let num = self.0.nb_packet;
    unsafe {
      std::slice::from_raw_parts(
        self.0.packet_index as *mut CodestreamPacketInfo,
        num as usize,
      )
    }
  }
}

pub struct CodestreamIndex(ptr::NonNull<sys::opj_codestream_index_t>);

impl Drop for CodestreamIndex {
  fn drop(&mut self) {
    unsafe {
      sys::opj_destroy_cstr_index(&mut self.0.as_ptr());
    }
  }
}

impl std::fmt::Debug for CodestreamIndex {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let idx = self.as_ref();
    f.debug_struct("CodestreamIndex")
      .field("main_head_start", &idx.main_head_start)
      .field("main_head_end", &idx.main_head_end)
      .field("codestream_size", &idx.codestream_size)
      .field("marknum", &idx.marknum)
      .field("marker", &self.markers())
      .field("maxmarknum", &idx.maxmarknum)
      .field("nb_of_tiles", &idx.nb_of_tiles)
      .field("tile_index", &self.tile_indices())
      .finish()
  }
}

impl CodestreamIndex {
  fn as_ref(&self) -> &sys::opj_codestream_index_t {
    unsafe { &(*self.0.as_ref()) }
  }

  /// Codestream markers.
  pub fn markers(&self) -> &[CodestreamMarker] {
    let idx = self.as_ref();
    let num = idx.marknum;
    unsafe { std::slice::from_raw_parts(idx.marker as *mut CodestreamMarker, num as usize) }
  }

  /// Codestream tile indices.
  pub fn tile_indices(&self) -> &[CodestreamTileIndex] {
    let idx = self.as_ref();
    let num = idx.nb_of_tiles;
    unsafe { std::slice::from_raw_parts(idx.tile_index as *mut CodestreamTileIndex, num as usize) }
  }
}

pub struct CodestreamInfo(ptr::NonNull<sys::opj_codestream_info_v2_t>);

impl Drop for CodestreamInfo {
  fn drop(&mut self) {
    unsafe {
      sys::opj_destroy_cstr_info(&mut self.0.as_ptr());
    }
  }
}

impl std::fmt::Debug for CodestreamInfo {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let info = self.as_ref();
    let tile_info = if info.tile_info.is_null() {
      TileInfo(&info.m_default_tile_info)
    } else {
      TileInfo(unsafe { &*info.tile_info })
    };
    f.debug_struct("CodestreamInfo")
      .field("tx0", &info.tx0)
      .field("ty0", &info.ty0)
      .field("tdx", &info.tdx)
      .field("tdy", &info.tdy)
      .field("tw", &info.tw)
      .field("th", &info.th)
      .field("nbcomps", &info.nbcomps)
      .field("tile_info", &tile_info)
      .finish()
  }
}

impl CodestreamInfo {
  fn as_ref(&self) -> &sys::opj_codestream_info_v2_t {
    unsafe { &(*self.0.as_ref()) }
  }
}

pub(crate) struct Codec {
  codec: ptr::NonNull<sys::opj_codec_t>,
}

impl Drop for Codec {
  fn drop(&mut self) {
    unsafe {
      sys::opj_destroy_codec(self.codec.as_ptr());
    }
  }
}

extern "C" fn log_info(msg: *const c_char, _data: *mut c_void) {
  unsafe {
    log::info!("{:?}", CStr::from_ptr(msg).to_string_lossy());
  }
}

extern "C" fn log_warn(msg: *const c_char, _data: *mut c_void) {
  unsafe {
    log::warn!("{:?}", CStr::from_ptr(msg).to_string_lossy());
  }
}

extern "C" fn log_error(msg: *const c_char, _data: *mut c_void) {
  unsafe {
    log::error!("{:?}", CStr::from_ptr(msg).to_string_lossy());
  }
}

impl Codec {
  fn new(fmt: J2KFormat, is_decoder: bool) -> Result<Self> {
    let format: sys::CODEC_FORMAT = fmt.into();
    let ptr = unsafe {
      if is_decoder {
        ptr::NonNull::new(sys::opj_create_decompress(format))
      } else {
        ptr::NonNull::new(sys::opj_create_compress(format))
      }
    };
    if let Some(ptr) = ptr {
      let null = ptr::null_mut();
      unsafe {
        if log_enabled!(Level::Info) {
          sys::opj_set_info_handler(ptr.as_ptr(), Some(log_info), null);
        }
        if log_enabled!(Level::Warn) {
          sys::opj_set_warning_handler(ptr.as_ptr(), Some(log_warn), null);
        }
        sys::opj_set_error_handler(ptr.as_ptr(), Some(log_error), null);

        #[cfg(feature = "threads")]
        if sys::opj_has_thread_support() == 1 {
          let num_cpus = sys::opj_get_num_cpus();
          if sys::opj_codec_set_threads(ptr.as_ptr(), num_cpus) != 1 {
            log::warn!("Failed to set number of threads: {:?}", num_cpus);
          }
        }
      }

      Ok(Self { codec: ptr })
    } else {
      Err(Error::CreateCodecError(format!(
        "Codec not supported: {:?}",
        fmt
      )))
    }
  }

  pub(crate) fn as_ptr(&self) -> *mut sys::opj_codec_t {
    self.codec.as_ptr()
  }
}

pub(crate) struct Decoder<'a> {
  codec: Codec,
  stream: Stream<'a>,
}

impl<'a> Decoder<'a> {
  pub(crate) fn new(stream: Stream<'a>) -> Result<Self> {
    assert!(stream.is_input());
    let fmt = stream.format();
    let codec = Codec::new(fmt, true)?;
    Ok(Self { codec, stream })
  }

  #[cfg(feature = "strict-mode")]
  fn set_strict_mode(&self, mode: bool) -> Result<()> {
    let res = unsafe { sys::opj_decoder_set_strict_mode(self.as_ptr(), mode as i32) == 1 };
    if res {
      Ok(())
    } else {
      Err(Error::CreateCodecError(format!(
        "Failed to set strict mode on decoder."
      )))
    }
  }

  #[cfg(not(feature = "strict-mode"))]
  fn set_strict_mode(&self, _mode: bool) -> Result<()> {
    Ok(())
  }

  pub(crate) fn setup(&self, params: &mut DecodeParameters) -> Result<()> {
    let res = unsafe { sys::opj_setup_decoder(self.as_ptr(), params.as_ptr()) == 1 };
    if res {
      self.set_strict_mode(params.strict)?;
      Ok(())
    } else {
      Err(Error::CreateCodecError(format!(
        "Failed to setup decoder with parameters."
      )))
    }
  }

  pub(crate) fn read_header(&self) -> Result<Image> {
    let mut img: *mut sys::opj_image_t = ptr::null_mut();

    let res = unsafe { sys::opj_read_header(self.stream.as_ptr(), self.as_ptr(), &mut img) };
    // Try wrapping the image pointer before handling any errors.
    // Since the read header function might have allocated the image structure.
    let img = Image::new(img)?;
    if res == 1 {
      Ok(img)
    } else {
      Err(Error::CodecError("Failed to read header".into()))
    }
  }

  pub(crate) fn get_codestream_index(&self) -> Result<CodestreamIndex> {
    let index = ptr::NonNull::new(unsafe { sys::opj_get_cstr_index(self.as_ptr()) })
      .ok_or_else(|| Error::CodecError("Failed to get codestream index".into()))?;
    Ok(CodestreamIndex(index))
  }

  pub(crate) fn get_codestream_info(&self) -> Result<CodestreamInfo> {
    let info = ptr::NonNull::new(unsafe { sys::opj_get_cstr_info(self.as_ptr()) })
      .ok_or_else(|| Error::CodecError("Failed to get codestream info".into()))?;
    Ok(CodestreamInfo(info))
  }

  pub(crate) fn set_decode_area(&self, img: &Image, params: &DecodeParameters) -> Result<()> {
    if let Some(area) = &params.area {
      let res = unsafe {
        sys::opj_set_decode_area(
          self.as_ptr(),
          img.as_ptr(),
          area.start_x as i32,
          area.start_y as i32,
          area.end_x as i32,
          area.end_y as i32,
        )
      };
      if res != 1 {
        return Err(Error::CreateCodecError(format!(
          "Failed to set decode area."
        )));
      }
    }
    Ok(())
  }

  pub(crate) fn decode(&self, img: &Image) -> Result<()> {
    let res = unsafe {
      sys::opj_decode(self.as_ptr(), self.stream.as_ptr(), img.as_ptr()) == 1
        && sys::opj_end_decompress(self.as_ptr(), self.stream.as_ptr()) == 1
    };
    if res {
      Ok(())
    } else {
      Err(Error::CodecError("Failed to decode image".into()))
    }
  }

  pub(crate) fn as_ptr(&self) -> *mut sys::opj_codec_t {
    self.codec.as_ptr()
  }
}

#[cfg(feature = "file-io")]
pub(crate) struct Encoder<'a> {
  codec: Codec,
  stream: Stream<'a>,
}

#[cfg(feature = "file-io")]
impl<'a> Encoder<'a> {
  pub(crate) fn new(stream: Stream<'a>) -> Result<Self> {
    assert!(!stream.is_input());
    let fmt = stream.format();
    let codec = Codec::new(fmt, false)?;
    Ok(Self { codec, stream })
  }

  pub(crate) fn setup(&self, mut params: EncodeParameters, img: &Image) -> Result<()> {
    let res = unsafe { sys::opj_setup_encoder(self.as_ptr(), &mut params.0, img.as_ptr()) };
    if res == 1 {
      Ok(())
    } else {
      Err(Error::CreateCodecError(format!(
        "Failed to setup encoder with parameters."
      )))
    }
  }

  pub(crate) fn encode(&self, img: &Image) -> Result<()> {
    let res = unsafe {
      sys::opj_start_compress(self.as_ptr(), img.as_ptr(), self.stream.as_ptr()) == 1
        && sys::opj_encode(self.as_ptr(), self.stream.as_ptr()) == 1
        && sys::opj_end_compress(self.as_ptr(), self.stream.as_ptr()) == 1
    };
    if res {
      Ok(())
    } else {
      Err(Error::CodecError("Failed to encode image".into()))
    }
  }

  pub(crate) fn as_ptr(&self) -> *mut sys::opj_codec_t {
    self.codec.as_ptr()
  }
}