Expand description
Decoder for image formats of retro computers.
Give decode a file name and the bytes of a file and it returns the
picture as 8-bit RGB, plus an alpha plane if the picture has
transparency. The file name chooses which of the supported formats to
try (see formats); many formats are also recognized by their content,
so a file with the wrong extension still decodes.
// A 1x1 PAM picture, held in memory: the library never touches files.
let file = b"P7\nWIDTH 1\nHEIGHT 1\nDEPTH 3\nMAXVAL 255\nTUPLTYPE RGB\nENDHDR\n\xff\x80\x00";
let decoded = retro_image::decode("dot.pam", file)?;
let image = decoded.image();
assert_eq!((image.width(), image.height()), (1, 1));
assert_eq!(image.rgb(), [0xff, 0x80, 0x00]); // 3 bytes per pixel, row by row
assert!(!image.has_alpha());
assert_eq!(decoded.format().platform(), "Unix");§Guarantees
- Decoding never panics on malformed input. Truncated, corrupt and
hostile files make a decoder return a
DecodeError; a panic would be a bug. Every decoder is tested against truncated and mutated real files and fuzzed for this. - Memory is bounded. A picture may take at most
max_image_bytes(64 MiB, 4 bytes per pixel) of memory unless the program callsset_max_image_bytes. Dimensions come from untrusted headers, so the limit is checked before anything is allocated for them; a bigger picture fails withDecodeError::TooLarge. A decoder holds some temporary buffers while it works, so the peak memory of a decode is a small multiple of the limit, not of the file size. - Types are thread-safe.
Image,FormatandDecodeErrorareSendandSync. - Errors can grow.
DecodeErroris#[non_exhaustive], so new reasons are not breaking changes.
The crate is no_std (it needs alloc) and has no dependencies.
This file holds no external format knowledge: it is the public API.
Each decoder module cites the documents its layouts come from; the
platform surveys are in docs/research/.
Structs§
- Attempt
- One format that
decodetried and that failed. - Decoded
- A decoded picture and the format that decoded it, as
decodereturns them. - Format
- One supported file format.
- Format
Id - An identifier for a
Formatthat stays the same between releases, so a program can store it, for example to remember which format a user picked. - Image
- A decoded picture: 8-bit color, row-major, top row first, and an alpha plane when some pixel is not opaque.
- NoCompanions
- No companion files: every lookup finds nothing.
Enums§
- Decode
Error - Why a file could not be decoded.
Traits§
- Companions
- Files that accompany the main one, such as a palette file next to a
picture (
PIC.COLnext toPIC.MIC).
Functions§
- candidates
- The formats
decodetries forfilename, in order: those matching its extension, then the other formats with a signature.filenameis only looked at for its extension. - decode
- Decodes
dataon its own: the picture, and the format that accepted it. Seedecode_withfor how the format is chosen. - decode_
with - Decodes
data, choosing the format fromfilenameand the content. - formats
- Every supported format.
- max_
image_ bytes - The most memory one decoded picture may take, in bytes: 64 MiB until
set_max_image_byteschanges it. - set_
max_ image_ bytes - Sets the most memory one decoded picture may take, in bytes. See
max_image_bytesfor how pictures are counted.