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
//! # Jpeg 2000 image support.
//!
//! A safe wrapper of `openjpeg-sys` for loading/saving Jpeg 2000 images.
//!
//! ## Example: Convert a Jpeg 2000 image to a png image.
//!
//! ```rust
//! use image::DynamicImage;
//!
//! use jpeg2k::*;
//!
//! fn main() {
//!   // Load jpeg 2000 file from file.
//!   let jp2_image = Image::from_file("./assets/example.j2k")
//! 		.expect("Failed to load j2k file.");
//!
//!   // Convert to a `image::DynamicImage`
//!   let img: DynamicImage = jp2_image.try_into()?;
//!
//!   // Save as png file.
//!   img.save("out.png")?;
//! }
//! ```

/// File format detection.
pub mod format;
pub(crate) use format::*;

pub mod error;
pub(crate) use error::*;

#[cfg(feature = "openjpeg-sys")]
pub(crate) use openjpeg_sys as sys;

#[cfg(feature = "openjp2")]
pub(crate) mod sys {
  pub use openjp2::openjpeg::*;
}

impl From<J2KFormat> for sys::CODEC_FORMAT {
  fn from(format: J2KFormat) -> Self {
    use J2KFormat::*;
    match format {
      JP2 => sys::CODEC_FORMAT::OPJ_CODEC_JP2,
      J2K => sys::CODEC_FORMAT::OPJ_CODEC_J2K,
    }
  }
}

pub(crate) mod codec;
pub(crate) mod dump;
pub(crate) mod j2k_image;
pub(crate) mod stream;

pub use codec::*;
pub use dump::*;
pub(crate) use stream::*;

pub use self::j2k_image::*;

/// Image color space.
#[derive(Debug, Clone, Copy)]
pub enum ColorSpace {
  Unknown,
  Unspecified,
  SRGB,
  Gray,
  SYCC,
  EYCC,
  CMYK,
}

/// From `ColorSpace` to OpenJpeg `COLOR_SPACE`.
impl From<ColorSpace> for sys::COLOR_SPACE {
  fn from(color: ColorSpace) -> Self {
    use sys::COLOR_SPACE::*;
    use ColorSpace::*;
    match color {
      Unknown => OPJ_CLRSPC_UNKNOWN,
      Unspecified => OPJ_CLRSPC_UNSPECIFIED,
      SRGB => OPJ_CLRSPC_SRGB,
      Gray => OPJ_CLRSPC_GRAY,
      SYCC => OPJ_CLRSPC_SYCC,
      EYCC => OPJ_CLRSPC_EYCC,
      CMYK => OPJ_CLRSPC_CMYK,
    }
  }
}

/// From OpenJpeg `COLOR_SPACE` to `ColorSpace`.
impl From<sys::COLOR_SPACE> for ColorSpace {
  fn from(color: sys::COLOR_SPACE) -> Self {
    use sys::COLOR_SPACE::*;
    use ColorSpace::*;
    match color {
      OPJ_CLRSPC_UNKNOWN => Unknown,
      OPJ_CLRSPC_UNSPECIFIED => Unspecified,
      OPJ_CLRSPC_SRGB => SRGB,
      OPJ_CLRSPC_GRAY => Gray,
      OPJ_CLRSPC_SYCC => SYCC,
      OPJ_CLRSPC_EYCC => EYCC,
      OPJ_CLRSPC_CMYK => CMYK,
    }
  }
}