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
//! Pure Rust JPEG2000 (JP2/J2K) driver for OxiGDAL
//!
//! This crate provides a Pure Rust implementation of JPEG2000 image decoding,
//! supporting both JP2 (JPEG2000 Part 1) and raw J2K codestream formats.
//!
//! # Features
//!
//! - Pure Rust implementation (no C/C++ dependencies)
//! - **Full JP2 box structure parsing** with support for all standard boxes
//! - JPEG2000 codestream decoding
//! - Wavelet transforms (5/3 reversible and 9/7 irreversible)
//! - Multi-component images (RGB, RGBA, grayscale)
//! - Tiling support with partial tile decoding
//! - **Complete metadata extraction** (file type, resolution, color spec, XML, UUID)
//! - **Error resilience modes** for handling corrupted files (Basic and Full)
//! - **Progressive decoding** with quality layer support
//! - **Region of Interest (ROI) decoding** for spatial and resolution-level extraction
//!
//! # Limitations
//!
//! This is a reference implementation with simplified decoding for common cases.
//! Full JPEG2000 compliance (especially tier-1 EBCOT encoding) requires extensive
//! additional implementation. For production use with complex JPEG2000 files,
//! consider using a more complete decoder.
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```no_run
//! use oxigdal_jpeg2000::Jpeg2000Reader;
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("image.jp2")?;
//! let reader = BufReader::new(file);
//!
//! let mut decoder = Jpeg2000Reader::new(reader)?;
//! decoder.parse_headers()?;
//!
//! let width = decoder.width()?;
//! let height = decoder.height()?;
//! println!("Image size: {}x{}", width, height);
//!
//! let info = decoder.info()?;
//! println!("Color space: {:?}", info.color_space);
//! println!("Decomposition levels: {}", info.num_decomposition_levels);
//!
//! // Access metadata
//! if let Some(res) = decoder.capture_resolution_dpi() {
//! println!("Resolution: {:.1} x {:.1} DPI", res.0, res.1);
//! }
//!
//! // Note: Full decoding not yet implemented
//! // let rgb_data = decoder.decode_rgb()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Progressive Decoding
//!
//! ```no_run
//! use oxigdal_jpeg2000::Jpeg2000Reader;
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("image.jp2")?;
//! let reader = BufReader::new(file);
//! let mut decoder = Jpeg2000Reader::new(reader)?;
//! decoder.parse_headers()?;
//!
//! // Decode progressively layer by layer
//! let mut progressive = decoder.decode_progressive()?;
//! while let Some(image_data) = progressive.next_layer()? {
//! println!("Decoded layer {} of {}",
//! progressive.current_layer(),
//! progressive.total_layers());
//! // Display or process intermediate quality image
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Region of Interest Decoding
//!
//! ```no_run
//! use oxigdal_jpeg2000::Jpeg2000Reader;
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("image.jp2")?;
//! let reader = BufReader::new(file);
//! let mut decoder = Jpeg2000Reader::new(reader)?;
//! decoder.parse_headers()?;
//!
//! // Decode only a specific region (more efficient than full decode)
//! let region = decoder.decode_region(100, 100, 256, 256)?;
//!
//! // Decode at lower resolution for thumbnail
//! let thumbnail = decoder.decode_region_at_resolution(0, 0, 64, 64, 2)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Resilience
//!
//! ```no_run
//! use oxigdal_jpeg2000::{Jpeg2000Reader, ResilienceMode};
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("corrupted.jp2")?;
//! let reader = BufReader::new(file);
//! let mut decoder = Jpeg2000Reader::new(reader)?;
//!
//! // Enable error resilience for corrupted files
//! decoder.enable_full_error_resilience();
//!
//! // Parser will attempt to recover from errors
//! decoder.parse_headers()?;
//! # Ok(())
//! # }
//! ```
//!
//! # Architecture
//!
//! The decoder is organized into several layers:
//!
//! - **Box Reader** ([`box_reader`]): Parses JP2 box structure
//! - **Codestream** ([`codestream`]): Parses JPEG2000 codestream markers
//! - **Tier-2** ([`tier2`]): Packet decoding and layer management
//! - **Tier-1** ([`tier1`]): Code-block decoding (EBCOT)
//! - **Wavelet** ([`wavelet`]): Inverse wavelet transforms
//! - **Color** ([`color`]): Color space conversions
//! - **Metadata** ([`metadata`]): JP2 metadata boxes
//! - **Reader** ([`reader`]): High-level decoding interface
//!
//! # JPEG2000 Standard
//!
//! JPEG2000 is defined in ISO/IEC 15444-1:2019. This implementation follows
//! the standard for basic decoding functionality.
//!
//! # Performance Considerations
//!
//! - Wavelet transforms are implemented with minimal optimizations
//! - SIMD optimizations are not yet implemented
//! - Memory usage is not optimized for large images
//! - For high-performance applications, consider using native implementations
//!
//! # TODO
//!
//! - Add writing/encoding support for JP2/J2K files
//! - 9/7 irreversible (lossy) wavelet decode path
//! - SIMD optimization for wavelet transforms
//! - Parallel tile decoding with multi-threading support
//! - JPX (JPEG2000 Part 2) extended features
//! - Memory-mapped file support for large images
//!
//! # Recently Implemented
//!
//! - ✅ Full JP2 format support (all standard boxes, complete metadata parsing)
//! - ✅ Error resilience modes (None, Basic, Full) with packet-level error handling
//! - ✅ Progressive decoding with quality layer support
//! - ✅ ROI decoding support (spatial regions and resolution levels)
//! - ✅ Real JPEG2000 decode chain: tier-1 EBCOT → inverse 5/3 DWT → RCT → level-shift
// Re-exports
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Version information
pub const VERSION: &str = env!;
/// Check if a file is likely a JP2 file based on magic bytes
///
/// # Example
///
/// ```
/// use oxigdal_jpeg2000::is_jp2;
/// use std::io::Cursor;
///
/// let data = vec![
/// 0x00, 0x00, 0x00, 0x0C,
/// 0x6A, 0x50, 0x20, 0x20,
/// 0x0D, 0x0A, 0x87, 0x0A,
/// ];
///
/// assert!(is_jp2(&mut Cursor::new(data)).expect("check failed"));
/// ```
/// Check if a file is likely a J2K codestream based on SOC marker
///
/// # Example
///
/// ```
/// use oxigdal_jpeg2000::is_j2k;
/// use std::io::Cursor;
///
/// let data = vec![0xFF, 0x4F]; // SOC marker
///
/// assert!(is_j2k(&mut Cursor::new(data)).expect("check failed"));
/// ```