Skip to main content

jbig2_decoder/
colour.rs

1//! Explicit RGB presentation of supported generic colour regions.
2
3use crate::{DecodeError, DecodePolicy, DecodeResult, Decoder};
4#[cfg(verus_keep_ghost)]
5use vstd::prelude::*;
6
7mod bitmap;
8#[cfg(verus_keep_ghost)]
9#[path = "../specs/colour.rs"]
10mod colour_spec;
11mod document;
12mod palette;
13
14pub use bitmap::RgbBitmap;
15
16/// One completely decoded, opaque RGB page.
17#[derive(Debug)]
18#[cfg_attr(verus_keep_ghost, verus_verify)]
19pub struct ColourPage {
20    pub(crate) number: u32,
21    pub(crate) bitmap: RgbBitmap,
22}
23
24impl ColourPage {
25    /// Original positive page association.
26    #[must_use]
27    #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out == colour_spec::page_number(self)))]
28    #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
29    pub const fn number(&self) -> u32 {
30        #[cfg(verus_keep_ghost)]
31        proof! { colour_spec::page_fields(self); }
32        self.number
33    }
34
35    /// Row-major RGB8 pixels.
36    #[must_use]
37    #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out == colour_spec::page_bitmap(self)))]
38    #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
39    pub const fn bitmap(&self) -> &RgbBitmap {
40        #[cfg(verus_keep_ghost)]
41        proof! { colour_spec::page_fields(self); }
42        &self.bitmap
43    }
44}
45
46/// Completely decoded pages in source page-information order.
47#[derive(Debug)]
48#[cfg_attr(verus_keep_ghost, verus_verify)]
49pub struct DecodedColourDocument {
50    pub(crate) pages: Vec<ColourPage>,
51}
52
53impl DecodedColourDocument {
54    /// Complete RGB pages; no partially rendered page is returned.
55    #[must_use]
56    #[cfg_attr(verus_keep_ghost, verus_spec(out => ensures out@ == colour_spec::pages(self)))]
57    #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
58    pub fn pages(&self) -> &[ColourPage] {
59        #[cfg(verus_keep_ghost)]
60        proof! { colour_spec::document_fields(self); }
61        &self.pages
62    }
63}
64
65impl Decoder {
66    /// Decodes the supported sequential generic-region RGB8 profile strictly.
67    ///
68    /// Existing bi-level entry points retain their colour `Unsupported` result.
69    /// This byte-only profile supports consecutive known-height unstriped pages, immediate
70    /// generic regions, RGB8 palettes, monochrome OR/REPLACE and transparent
71    /// coloured REPLACE, without deferred retention. Other profiles return typed `Unsupported`.
72    ///
73    /// # Errors
74    ///
75    /// Returns typed framing, palette, entropy, resource or profile errors.
76    #[cfg_attr(verus_keep_ghost, verus_spec(out =>
77        ensures match out {
78            Ok(output) => colour_spec::document_valid(&output),
79            Err(_) => true,
80        },
81    ))]
82    pub fn decode_rgb(&self, input: &[u8]) -> Result<DecodedColourDocument, DecodeError> {
83        Ok(self
84            .decode_rgb_with_policy(input, DecodePolicy::Strict)?
85            .value)
86    }
87
88    /// Decodes RGB8 with explicit colour declaration-bit recovery.
89    ///
90    /// Compatibility may report missing file/page colour declarations only.
91    /// It does not inherit the bi-level decoder's other recovery exceptions.
92    /// No palette ID is clamped and no colour flag is discarded.
93    ///
94    /// # Errors
95    ///
96    /// Every region and page must complete; errors return no pages or recoveries.
97    #[cfg_attr(verus_keep_ghost, verus_spec(out =>
98        ensures match out {
99            Ok(output) => colour_spec::document_valid(&output.value),
100            Err(_) => true,
101        },
102    ))]
103    pub fn decode_rgb_with_policy(
104        &self,
105        input: &[u8],
106        policy: DecodePolicy,
107    ) -> Result<DecodeResult<DecodedColourDocument>, DecodeError> {
108        document::decode(input, policy, self.limits())
109    }
110}