Detect PNG or JPEG by magic bytes, returning extension + (width, height). Both formats are common in glTF .glb embeds.
(b: &[u8])
| 75 | /// Detect PNG or JPEG by magic bytes, returning extension + (width, height). |
| 76 | /// Both formats are common in glTF .glb embeds. |
| 77 | fn detect_image(b: &[u8]) -> (&'static str, (u32, u32)) { |
| 78 | // PNG signature: 89 50 4E 47 0D 0A 1A 0A |
| 79 | if b.len() >= 24 && b[0] == 0x89 && &b[1..4] == b"PNG" && &b[12..16] == b"IHDR" { |
| 80 | let w = u32::from_be_bytes([b[16], b[17], b[18], b[19]]); |
| 81 | let h = u32::from_be_bytes([b[20], b[21], b[22], b[23]]); |
| 82 | return ("png", (w, h)); |
| 83 | } |
| 84 | // JPEG signature: FF D8 ... look for SOF0 / SOF2 marker for dimensions. |
| 85 | if b.len() >= 4 && b[0] == 0xFF && b[1] == 0xD8 { |
| 86 | if let Some(dims) = parse_jpeg_size(b) { |
| 87 | return ("jpg", dims); |
| 88 | } |
| 89 | return ("jpg", (0, 0)); |
| 90 | } |
| 91 | ("", (0, 0)) |
| 92 | } |
| 93 | |
| 94 | /// Walk JPEG segments until a Start-Of-Frame marker, read (h, w). |
| 95 | fn parse_jpeg_size(b: &[u8]) -> Option<(u32, u32)> { |
no test coverage detected