render: normalize high precision texture inputs (#1267)
Adds a bounded normalization path for PNG16 and ICC-bearing JPEG texture inputs, producing float RGBA with recorded color and orientation decisions. Co-authored-by: 0xlillith <0xlillith@users.noreply.github.com>
This commit is contained in:
1 parent
a47e82ba94
commit
1e984c8e46
13 files changed
+1616
-17
No files matched your search
@@ -0,0 +1,75 @@
|
||||
//! Metadata presence/framing admission from T.81 and ICC's JPEG embedding note.
|
||||
//! This does not decode entropy data or replace the established JPEG codec.
|
||||
use crate::texture_input::{Result, invalid};
|
||||
|
||||
pub(super) fn read(bytes: &[u8]) -> Result<(bool, bool)> {
|
||||
if bytes.get(..2) != Some(b"\xff\xd8") {
|
||||
return Err(invalid("invalid JPEG SOI"));
|
||||
}
|
||||
let mut offset = 2;
|
||||
let mut entropy = false;
|
||||
let mut scan_seen = false;
|
||||
let mut exif = false;
|
||||
let mut icc = false;
|
||||
let mut segments = 0;
|
||||
while offset < bytes.len() {
|
||||
if bytes.get(offset) != Some(&255) {
|
||||
if !entropy {
|
||||
return Err(invalid("JPEG marker expected"));
|
||||
}
|
||||
offset += 1;
|
||||
continue;
|
||||
}
|
||||
while bytes.get(offset) == Some(&255) {
|
||||
offset += 1;
|
||||
}
|
||||
let marker = *bytes.get(offset).ok_or_else(|| invalid("truncated JPEG marker"))?;
|
||||
offset += 1;
|
||||
if entropy && (marker == 0 || (0xd0..=0xd7).contains(&marker)) {
|
||||
continue;
|
||||
}
|
||||
entropy = false;
|
||||
segments += 1;
|
||||
if segments > 4096 {
|
||||
return Err(invalid("JPEG normalization exceeds 4096 marker segments"));
|
||||
}
|
||||
if marker == 0xd9 {
|
||||
return if offset == bytes.len() { Ok((icc, exif)) } else { Err(invalid("JPEG EOI must be final")) };
|
||||
}
|
||||
if marker == 0 || marker == 0xd8 || marker == 1 || (0xd0..=0xd7).contains(&marker) {
|
||||
return Err(invalid("unsupported JPEG standalone marker"));
|
||||
}
|
||||
let header_end = offset.checked_add(2).ok_or_else(|| invalid("JPEG offset overflow"))?;
|
||||
let length: [u8; 2] = bytes.get(offset..header_end).and_then(|v| v.try_into().ok()).ok_or_else(|| invalid("truncated JPEG length"))?;
|
||||
let length = usize::from(u16::from_be_bytes(length));
|
||||
let end = offset.checked_add(length).ok_or_else(|| invalid("JPEG segment overflow"))?;
|
||||
if length < 2 {
|
||||
return Err(invalid("invalid JPEG segment length"));
|
||||
}
|
||||
let data = bytes.get(header_end..end).ok_or_else(|| invalid("truncated JPEG segment"))?;
|
||||
if marker == 0xe2 && data.starts_with(b"ICC_PROFILE\0") {
|
||||
if scan_seen || data.len() <= 14 {
|
||||
return Err(invalid("late or truncated JPEG ICC metadata"));
|
||||
}
|
||||
icc = true; // The codec assembles sequences; presence must survive that validation.
|
||||
}
|
||||
if marker == 0xe1 && data.starts_with(b"Exif\0\0") {
|
||||
if scan_seen || exif || data.len() <= 6 {
|
||||
return Err(invalid("duplicate, late or truncated JPEG EXIF metadata"));
|
||||
}
|
||||
exif = true;
|
||||
}
|
||||
if (0xc0..=0xcf).contains(&marker)
|
||||
&& !matches!(marker, 0xc4 | 0xc8 | 0xcc)
|
||||
&& (!matches!(marker, 0xc0..=0xc2) || data.first() != Some(&8) || !matches!(data.get(5), Some(1 | 3)))
|
||||
{
|
||||
return Err(invalid("initial JPEG normalization requires 8-bit grayscale or three-component DCT input"));
|
||||
}
|
||||
if marker == 0xda {
|
||||
scan_seen = true;
|
||||
entropy = true;
|
||||
}
|
||||
offset = end;
|
||||
}
|
||||
Err(invalid("JPEG EOI missing"))
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//! Bounded framing/presence checks; the PNG crate still decodes and validates chunks.
|
||||
use crate::texture_input::{Result, invalid};
|
||||
|
||||
const SEMANTIC: [[u8; 4]; 8] = [*b"iCCP", *b"sRGB", *b"gAMA", *b"cHRM", *b"eXIf", *b"cICP", *b"mDCV", *b"cLLI"];
|
||||
|
||||
pub(super) struct Markers([bool; 8]);
|
||||
|
||||
impl Markers {
|
||||
pub fn check(&self, info: &png::Info<'_>) -> Result<()> {
|
||||
let parsed = [
|
||||
info.icc_profile.is_some(),
|
||||
info.srgb.is_some(),
|
||||
info.gama_chunk.is_some(),
|
||||
info.chrm_chunk.is_some(),
|
||||
info.exif_metadata.is_some(),
|
||||
info.coding_independent_code_points.is_some(),
|
||||
info.mastering_display_color_volume.is_some(),
|
||||
info.content_light_level.is_some(),
|
||||
];
|
||||
if self.0 != parsed {
|
||||
return Err(invalid("PNG decoder did not retain declared color/orientation metadata"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn read(bytes: &[u8]) -> Result<Markers> {
|
||||
if bytes.get(..8) != Some(b"\x89PNG\r\n\x1a\n") {
|
||||
return Err(invalid("invalid PNG signature"));
|
||||
}
|
||||
let mut offset = 8_usize;
|
||||
let mut markers = [false; 8];
|
||||
let mut pixels_seen = false;
|
||||
for _ in 0..4096 {
|
||||
let header_end = offset.checked_add(8).ok_or_else(|| invalid("PNG chunk offset overflow"))?;
|
||||
let header = bytes.get(offset..header_end).ok_or_else(|| invalid("truncated PNG chunk header"))?;
|
||||
let length: [u8; 4] = header.get(..4).and_then(|v| v.try_into().ok()).ok_or_else(|| invalid("invalid PNG length"))?;
|
||||
let length = usize::try_from(u32::from_be_bytes(length)).map_err(|_| invalid("PNG length overflow"))?;
|
||||
let kind = header.get(4..8).ok_or_else(|| invalid("invalid PNG chunk type"))?;
|
||||
let end = header_end.checked_add(length).and_then(|v| v.checked_add(4)).ok_or_else(|| invalid("PNG chunk size overflow"))?;
|
||||
if end > bytes.len() {
|
||||
return Err(invalid("truncated PNG chunk"));
|
||||
}
|
||||
if let Some(index) = SEMANTIC.iter().position(|tag| tag == kind) {
|
||||
let seen = markers.get_mut(index).ok_or_else(|| invalid("PNG metadata index overflow"))?;
|
||||
if *seen || pixels_seen {
|
||||
return Err(invalid("duplicate or late PNG color/orientation metadata is unsupported"));
|
||||
}
|
||||
*seen = true;
|
||||
}
|
||||
if kind == b"IDAT" {
|
||||
pixels_seen = true;
|
||||
}
|
||||
if kind == b"IEND" {
|
||||
if length != 0 || end != bytes.len() {
|
||||
return Err(invalid("PNG IEND must be empty and final"));
|
||||
}
|
||||
if matches!(markers, [true, true, ..]) {
|
||||
return Err(invalid("PNG has conflicting ICC and sRGB declarations"));
|
||||
}
|
||||
return Ok(Markers(markers));
|
||||
}
|
||||
offset = end;
|
||||
}
|
||||
Err(invalid("PNG normalization exceeds 4096 chunks"))
|
||||
}
|
||||
Reference in new issue
Block a user