MCPcopy Create free account
hub / github.com/Bloom-Engine/engine / detect_image

Function detect_image

native/watchos/src/textures.rs:77–92  ·  view source on GitHub ↗

Detect PNG or JPEG by magic bytes, returning extension + (width, height). Both formats are common in glTF .glb embeds.

(b: &[u8])

Source from the content-addressed store, hash-verified

75/// Detect PNG or JPEG by magic bytes, returning extension + (width, height).
76/// Both formats are common in glTF .glb embeds.
77fn 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).
95fn parse_jpeg_size(b: &[u8]) -> Option<(u32, u32)> {

Callers 1

register_bytesFunction · 0.85

Calls 1

parse_jpeg_sizeFunction · 0.85

Tested by

no test coverage detected