j2k-native 0.7.0

Pure-Rust JPEG 2000 and HTJ2K codec engine for j2k
Documentation
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Native and public JP2/JPH metadata models and move-only conversions.

use alloc::vec::Vec;

use crate::error::Result;
use crate::j2c::ComponentData;

use super::allocation;
use super::cdef::{ChannelAssociation, ChannelDefinitionBox, ChannelType};
use super::cmap::{ComponentMappingBox, ComponentMappingEntry, ComponentMappingType};
use super::colr::{ColorSpace as NativeColorSpace, ColorSpecificationBox};
use super::pclr::PaletteBox;

#[derive(Debug, Default)]
pub(crate) struct ImageBoxes {
    pub(crate) image_header: Option<ImageHeaderBox>,
    pub(crate) bits_per_component: Vec<ComponentDescriptor>,
    pub(crate) color_specifications: Vec<ColorSpecificationBox>,
    pub(crate) channel_definition: Option<ChannelDefinitionBox>,
    pub(crate) palette: Option<PaletteBox>,
    pub(crate) component_mapping: Option<ComponentMappingBox>,
}

impl ImageBoxes {
    pub(crate) fn try_with_synthetic_color_specification(
        header: &crate::j2c::Header<'_>,
        color_specification: ColorSpecificationBox,
        retained_baseline_bytes: usize,
    ) -> Result<Self> {
        let mut retained_header_bytes =
            crate::j2c::codestream::allocation::retained_header_bytes(header)?;
        allocation::checked_add_bytes(
            &mut retained_header_bytes,
            retained_baseline_bytes,
            "retained raw-codestream parse owners",
        )?;
        let mut budget = allocation::Jp2AllocationBudget::from_live_bytes(retained_header_bytes)?;
        let mut color_specifications =
            budget.try_vec(1, "synthetic raw-codestream color specification")?;
        color_specifications.push(color_specification);
        Ok(Self {
            color_specifications,
            ..Self::default()
        })
    }

    pub(crate) fn primary_color_specification(&self) -> Option<&ColorSpecificationBox> {
        self.color_specifications.first()
    }

    pub(crate) fn allocated_bytes(&self) -> Result<usize> {
        use allocation::{capacity_bytes, checked_add_bytes};

        let mut bytes = capacity_bytes::<ComponentDescriptor>(
            self.bits_per_component.capacity(),
            "JP2 BPCC metadata",
        )?;
        checked_add_bytes(
            &mut bytes,
            capacity_bytes::<ColorSpecificationBox>(
                self.color_specifications.capacity(),
                "JP2 COLR metadata",
            )?,
            "JP2 metadata",
        )?;
        for color_spec in &self.color_specifications {
            if let NativeColorSpace::Icc(profile) = &color_spec.color_space {
                checked_add_bytes(&mut bytes, profile.capacity(), "JP2 ICC metadata")?;
            }
        }
        if let Some(palette) = &self.palette {
            checked_add_bytes(
                &mut bytes,
                capacity_bytes::<crate::jp2::pclr::PaletteColumn>(
                    palette.columns.capacity(),
                    "JP2 palette columns",
                )?,
                "JP2 metadata",
            )?;
            checked_add_bytes(
                &mut bytes,
                capacity_bytes::<Vec<u64>>(palette.entries.capacity(), "JP2 palette rows")?,
                "JP2 metadata",
            )?;
            for row in &palette.entries {
                checked_add_bytes(
                    &mut bytes,
                    capacity_bytes::<u64>(row.capacity(), "JP2 palette entries")?,
                    "JP2 metadata",
                )?;
            }
        }
        if let Some(mapping) = &self.component_mapping {
            checked_add_bytes(
                &mut bytes,
                capacity_bytes::<ComponentMappingEntry>(
                    mapping.entries.capacity(),
                    "JP2 component mappings",
                )?,
                "JP2 metadata",
            )?;
        }
        if let Some(definition) = &self.channel_definition {
            checked_add_bytes(
                &mut bytes,
                capacity_bytes::<crate::jp2::cdef::ChannelDefinition>(
                    definition.channel_definitions.capacity(),
                    "JP2 channel definitions",
                )?,
                "JP2 metadata",
            )?;
        }
        Ok(bytes)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ImageHeaderBox {
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) components: u16,
    pub(crate) bits_per_component: Option<ComponentDescriptor>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ComponentDescriptor {
    pub(crate) bit_depth: u8,
    pub(crate) signed: bool,
}
/// Parsed JP2/JPH image header metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Jp2ImageHeaderMetadata {
    /// Width from the Image Header box.
    pub width: u32,
    /// Height from the Image Header box.
    pub height: u32,
    /// Component count from the Image Header box.
    pub components: u16,
    /// Explicit bits-per-component descriptor, or `None` when BPCC is required.
    pub bits_per_component: Option<Jp2ComponentMetadata>,
}

/// Parsed component precision metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Jp2ComponentMetadata {
    /// Significant bits in this component.
    pub bit_depth: u8,
    /// Whether this component stores signed sample values.
    pub signed: bool,
}

/// JP2/JPH file-wrapper metadata parsed from the JP2 Header box.
#[derive(Debug, PartialEq, Eq)]
pub struct Jp2FileMetadata {
    /// Bits-per-component box entries, when BPCC is present.
    pub bits_per_component: Vec<Jp2ComponentMetadata>,
    /// Colour Specification boxes in file order.
    pub color_specs: Vec<Jp2ColorSpec>,
    /// Palette box metadata, when PCLR is present.
    pub palette: Option<Jp2PaletteMetadata>,
    /// Component Mapping box entries in file order.
    pub component_mappings: Vec<Jp2ComponentMapping>,
    /// Channel Definition box entries in file order.
    pub channel_definitions: Vec<Jp2ChannelDefinition>,
    /// Whether a Palette box is present.
    pub has_palette: bool,
    /// Whether a Component Mapping box is present.
    pub has_component_mapping: bool,
    /// Whether a Channel Definition box is present.
    pub has_channel_definition: bool,
}

/// Parsed JP2/JPH Colour Specification box.
#[derive(Debug, PartialEq, Eq)]
pub enum Jp2ColorSpec {
    /// Enumerated color space value from a method-1 COLR box.
    Enumerated {
        /// Raw JP2 enumerated color-space value.
        value: u32,
    },
    /// ICC profile bytes from a method-2 COLR box.
    IccProfile {
        /// ICC profile byte payload.
        profile: Vec<u8>,
    },
    /// Unknown or currently unsupported COLR method.
    Unknown {
        /// Raw COLR method byte.
        method: u8,
    },
}

/// Parsed JP2/JPH Palette box.
#[derive(Debug, PartialEq, Eq)]
pub struct Jp2PaletteMetadata {
    /// Palette column descriptors in box order.
    pub columns: Vec<Jp2PaletteColumn>,
    /// Palette entries in row-major order: entry, then column.
    pub entries: Vec<Vec<u64>>,
}

/// Parsed JP2/JPH Palette column descriptor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Jp2PaletteColumn {
    /// Significant bits in this palette column.
    pub bit_depth: u8,
    /// Whether this palette column stores signed values.
    pub signed: bool,
}

/// Parsed JP2/JPH Component Mapping box entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Jp2ComponentMapping {
    /// Source codestream component index.
    pub component_index: u16,
    /// Mapping operation for this output channel.
    pub mapping_type: Jp2ComponentMappingType,
}

/// JP2/JPH Component Mapping operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Jp2ComponentMappingType {
    /// Directly map the codestream component.
    Direct,
    /// Map the codestream component through a palette column.
    Palette {
        /// Palette column index.
        column: u8,
    },
    /// Unknown mapping type preserved for inspection.
    Unknown {
        /// Raw mapping type value.
        value: u8,
        /// Raw palette-column byte carried by the entry.
        column: u8,
    },
}

/// Parsed JP2/JPH Channel Definition box entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Jp2ChannelDefinition {
    /// Output channel index.
    pub channel_index: u16,
    /// Channel type.
    pub channel_type: Jp2ChannelType,
    /// Channel association.
    pub association: Jp2ChannelAssociation,
}

/// JP2/JPH channel type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Jp2ChannelType {
    /// Color channel.
    Color,
    /// Opacity channel.
    Opacity,
    /// Premultiplied opacity channel.
    PremultipliedOpacity,
    /// Channel type is unspecified.
    Unspecified,
    /// Unknown raw channel type.
    Unknown {
        /// Raw channel type value.
        value: u16,
    },
}

/// JP2/JPH channel association.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Jp2ChannelAssociation {
    /// Applies to the whole image.
    WholeImage,
    /// Associated with a one-based color channel index from CDEF.
    Color {
        /// One-based color channel index.
        index: u16,
    },
    /// Association is unspecified.
    Unspecified,
}

pub(crate) struct DecodedImage<'components, 'boxes> {
    /// The raw decoded JPEG2000 codestream components.
    pub(crate) decoded_components: &'components mut Vec<ComponentData>,
    /// The JP2 boxes of the image. In the case of a raw codestream, we
    /// will synthesize the necessary boxes.
    pub(crate) boxes: &'boxes ImageBoxes,
}
pub(super) fn public_image_header(header: ImageHeaderBox) -> Jp2ImageHeaderMetadata {
    Jp2ImageHeaderMetadata {
        width: header.width,
        height: header.height,
        components: header.components,
        bits_per_component: header.bits_per_component.map(public_component_metadata),
    }
}

pub(super) fn public_metadata_from_boxes(boxes: ImageBoxes) -> Result<Jp2FileMetadata> {
    let mut budget = allocation::Jp2AllocationBudget::from_live_bytes(boxes.allocated_bytes()?)?;
    let has_palette = boxes.palette.is_some();
    let has_component_mapping = boxes.component_mapping.is_some();
    let has_channel_definition = boxes.channel_definition.is_some();
    let ImageBoxes {
        image_header: _,
        bits_per_component,
        color_specifications,
        channel_definition,
        palette,
        component_mapping,
    } = boxes;

    Ok(Jp2FileMetadata {
        bits_per_component: public_component_metadata_list(bits_per_component, &mut budget)?,
        color_specs: public_color_specs(color_specifications, &mut budget)?,
        palette: public_palette_metadata(palette, &mut budget)?,
        component_mappings: public_component_mappings(component_mapping, &mut budget)?,
        channel_definitions: public_channel_definitions(channel_definition, &mut budget)?,
        has_palette,
        has_component_mapping,
        has_channel_definition,
    })
}

fn public_component_metadata(component: ComponentDescriptor) -> Jp2ComponentMetadata {
    Jp2ComponentMetadata {
        bit_depth: component.bit_depth,
        signed: component.signed,
    }
}

fn public_component_metadata_list(
    components: Vec<ComponentDescriptor>,
    budget: &mut allocation::Jp2AllocationBudget,
) -> Result<Vec<Jp2ComponentMetadata>> {
    let source_capacity = components.capacity();
    let mut public = budget.try_vec(components.len(), "public JP2 BPCC metadata")?;
    for component in components {
        public.push(public_component_metadata(component));
    }
    budget.release_capacity::<ComponentDescriptor>(source_capacity)?;
    Ok(public)
}

fn public_color_specs(
    color_specs: Vec<ColorSpecificationBox>,
    budget: &mut allocation::Jp2AllocationBudget,
) -> Result<Vec<Jp2ColorSpec>> {
    let source_capacity = color_specs.capacity();
    let mut public = budget.try_vec(color_specs.len(), "public JP2 COLR metadata")?;
    for color_spec in color_specs {
        public.push(public_color_spec(color_spec));
    }
    budget.release_capacity::<ColorSpecificationBox>(source_capacity)?;
    Ok(public)
}

fn public_color_spec(color_spec: ColorSpecificationBox) -> Jp2ColorSpec {
    match color_spec.color_space {
        NativeColorSpace::Enumerated(_) => Jp2ColorSpec::Enumerated {
            value: color_spec.enumerated_value.unwrap_or(0),
        },
        NativeColorSpace::Icc(profile) => Jp2ColorSpec::IccProfile { profile },
        NativeColorSpace::Unknown => Jp2ColorSpec::Unknown {
            method: color_spec.method,
        },
    }
}

fn public_palette_metadata(
    palette: Option<PaletteBox>,
    budget: &mut allocation::Jp2AllocationBudget,
) -> Result<Option<Jp2PaletteMetadata>> {
    let Some(PaletteBox { entries, columns }) = palette else {
        return Ok(None);
    };
    let source_capacity = columns.capacity();
    let mut public_columns = budget.try_vec(columns.len(), "public JP2 palette columns")?;
    for column in columns {
        public_columns.push(Jp2PaletteColumn {
            bit_depth: column.bit_depth,
            signed: column.signed,
        });
    }
    budget.release_capacity::<crate::jp2::pclr::PaletteColumn>(source_capacity)?;
    Ok(Some(Jp2PaletteMetadata {
        columns: public_columns,
        entries,
    }))
}

fn public_component_mappings(
    mapping: Option<ComponentMappingBox>,
    budget: &mut allocation::Jp2AllocationBudget,
) -> Result<Vec<Jp2ComponentMapping>> {
    let Some(mapping) = mapping else {
        return Ok(Vec::new());
    };
    let source_capacity = mapping.entries.capacity();
    let mut public = budget.try_vec(mapping.entries.len(), "public JP2 component mappings")?;
    for entry in mapping.entries {
        public.push(public_component_mapping(&entry));
    }
    budget.release_capacity::<ComponentMappingEntry>(source_capacity)?;
    Ok(public)
}

fn public_component_mapping(mapping: &ComponentMappingEntry) -> Jp2ComponentMapping {
    Jp2ComponentMapping {
        component_index: mapping.component_index,
        mapping_type: match mapping.mapping_type {
            ComponentMappingType::Direct => Jp2ComponentMappingType::Direct,
            ComponentMappingType::Palette { column } => Jp2ComponentMappingType::Palette { column },
            ComponentMappingType::Unknown { value, column } => {
                Jp2ComponentMappingType::Unknown { value, column }
            }
        },
    }
}

fn public_channel_definitions(
    definition: Option<ChannelDefinitionBox>,
    budget: &mut allocation::Jp2AllocationBudget,
) -> Result<Vec<Jp2ChannelDefinition>> {
    let Some(definition) = definition else {
        return Ok(Vec::new());
    };
    let source_capacity = definition.channel_definitions.capacity();
    let mut public = budget.try_vec(
        definition.channel_definitions.len(),
        "public JP2 channel definitions",
    )?;
    for definition in definition.channel_definitions {
        public.push(public_channel_definition(&definition));
    }
    budget.release_capacity::<crate::jp2::cdef::ChannelDefinition>(source_capacity)?;
    Ok(public)
}

fn public_channel_definition(
    definition: &crate::jp2::cdef::ChannelDefinition,
) -> Jp2ChannelDefinition {
    Jp2ChannelDefinition {
        channel_index: definition.channel_index,
        channel_type: match definition.channel_type {
            ChannelType::Colour => Jp2ChannelType::Color,
            ChannelType::Opacity => Jp2ChannelType::Opacity,
            ChannelType::PremultipliedOpacity => Jp2ChannelType::PremultipliedOpacity,
            ChannelType::Unspecified => Jp2ChannelType::Unspecified,
            ChannelType::Unknown(value) => Jp2ChannelType::Unknown { value },
        },
        association: match definition.association {
            ChannelAssociation::WholeImage => Jp2ChannelAssociation::WholeImage,
            ChannelAssociation::Colour(index) => Jp2ChannelAssociation::Color { index },
            ChannelAssociation::Unspecified => Jp2ChannelAssociation::Unspecified,
        },
    }
}