use super::{be, byte_pushed_peak, len};
const FILE_HEADER_ID: [u8; 8] = [0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A];
pub(super) fn working_bytes(data: &[u8], globals: Option<&[u8]>) -> Option<u64> {
if data.starts_with(&FILE_HEADER_ID) {
return None;
}
let mut walk = Walk::default();
if let Some(g) = globals {
walk.segments(g)?;
walk.held = walk.held.saturating_add(len(g));
}
walk.segments(data)?;
let page = walk.page?;
let output = byte_pushed_peak(page.width.div_ceil(8).saturating_mul(page.height));
let held = walk.held.saturating_add(page.bytes());
Some(walk.peak.max(held.saturating_add(output)))
}
#[derive(Debug, Clone, Copy)]
struct Page {
width: u64,
height: u64,
default_pixel_zero: bool,
}
impl Page {
fn bytes(self) -> u64 {
bitmap(self.width, self.height)
}
}
#[derive(Default)]
struct Walk {
page: Option<Page>,
painted: bool,
held: u64,
peak: u64,
}
fn bitmap(width: u64, height: u64) -> u64 {
width.div_ceil(32).saturating_mul(4).saturating_mul(height)
}
impl Walk {
fn segments(&mut self, d: &[u8]) -> Option<()> {
let mut at = 0usize;
while at < d.len() {
let number = be::<4>(d, at)?;
let flags = *d.get(at + 4)?;
let count_byte = *d.get(at + 5)?;
let (refs, mut pos) = if count_byte >> 5 == 7 {
let r = be::<4>(d, at + 5)? & 0x1FFF_FFFF;
(r, at + 9 + usize::try_from((r + 1).div_ceil(8)).ok()?)
} else {
(u64::from(count_byte >> 5), at + 6)
};
let ref_size = match number {
0..=256 => 1,
257..=65536 => 2,
_ => 4,
};
pos = pos.checked_add(usize::try_from(refs.checked_mul(ref_size)?).ok()?)?;
pos += if flags & 0x40 != 0 { 4 } else { 1 };
let length = be::<4>(d, pos)?;
if length == 0xFFFF_FFFF {
return None;
}
pos += 4;
let end = pos.checked_add(usize::try_from(length).ok()?)?;
self.segment(flags & 0x3F, d.get(pos..end)?)?;
at = end;
}
Some(())
}
fn segment(&mut self, kind: u8, body: &[u8]) -> Option<()> {
match kind {
0 => return None,
48 => {
if self.page.is_some() {
return None;
}
let height = be::<4>(body, 4)?;
if height == 0xFFFF_FFFF {
return None;
}
self.page = Some(Page {
width: be::<4>(body, 0)?,
height,
default_pixel_zero: body.get(16)? & 0x04 == 0,
});
}
16 => self.held = self.held.saturating_add(pattern_dictionary(body)?),
4 | 20 | 36 | 40 => {
let extra = if kind == 20 { halftone_extra(body)? } else { 0 };
let region = region(body)?.bytes();
self.transient(region.saturating_add(extra));
self.held = self.held.saturating_add(region);
}
6 | 7 | 22 | 23 | 38 | 39 | 42 | 43 => self.immediate(kind, body)?,
_ => {}
}
Some(())
}
fn immediate(&mut self, kind: u8, body: &[u8]) -> Option<()> {
let info = region(body)?;
let (default_pixel, extra) = match kind {
6 | 7 => (be::<2>(body, 17)? & 0x0200 != 0, 0),
22 | 23 => (body.get(17)? & 0x80 != 0, halftone_extra(body)?),
_ => (false, 0),
};
let direct = self.page.is_some_and(|p| info.direct(p, default_pixel)) && !self.painted;
self.painted = true;
let own = if direct { 0 } else { info.bytes() };
self.transient(own.saturating_add(extra));
Some(())
}
fn transient(&mut self, bytes: u64) {
let page = self.page.map_or(0, Page::bytes);
let now = self.held.saturating_add(page).saturating_add(bytes);
self.peak = self.peak.max(now);
}
}
struct Region {
width: u64,
height: u64,
x: u64,
y: u64,
op: u8,
}
fn region(body: &[u8]) -> Option<Region> {
Some(Region {
width: be::<4>(body, 0)?,
height: be::<4>(body, 4)?,
x: be::<4>(body, 8)?,
y: be::<4>(body, 12)?,
op: body.get(16)? & 0x07,
})
}
impl Region {
fn bytes(&self) -> u64 {
bitmap(self.width, self.height)
}
fn direct(&self, page: Page, default_pixel: bool) -> bool {
let covers =
self.x == 0 && self.y == 0 && self.width == page.width && self.height == page.height;
covers
&& default_pixel != page.default_pixel_zero
&& match self.op {
4 => true,
0 | 2 => page.default_pixel_zero,
_ => !page.default_pixel_zero,
}
}
}
fn pattern_dictionary(body: &[u8]) -> Option<u64> {
let (pw, ph) = (u64::from(*body.get(1)?), u64::from(*body.get(2)?));
let patterns = be::<4>(body, 3)?.saturating_add(1);
let collective = bitmap(patterns.saturating_mul(pw), ph);
let each = patterns.saturating_mul(bitmap(pw, ph));
let shifted = if pw <= 32 && ph <= 32 {
patterns.saturating_mul(32 * ph * 2 * 4)
} else {
0
};
Some(collective.saturating_add(each).saturating_add(shifted))
}
fn halftone_extra(body: &[u8]) -> Option<u64> {
let flags = *body.get(17)?;
let (gw, gh) = (be::<4>(body, 18)?, be::<4>(body, 22)?);
let plane = bitmap(gw, gh);
let planes = if flags & 0x08 != 0 { 3 } else { 2 };
Some(
gw.saturating_mul(gh)
.saturating_mul(4)
.saturating_add(plane.saturating_mul(planes)),
)
}