Summary
guess_format cannot identify TGA files, which is expected and documented in the source ("TGA is not supported by this function"). TGA has no header magic, so it does not fit the front-of-buffer MAGIC_BYTES table.
However, TGA 2.0 files do carry a fixed signature: an 18-byte footer, TRUEVISION-XFILE.\0, at the very end of the file. This proposes an optional fallback that checks for that footer after the existing prefix scan misses, so guess_format can recognise TGA 2.0 while leaving the front-prefix table untouched.
Why this is worth considering
I maintain a tool that processes a large library of files containing embedded images of mixed formats. Sniffing by content (not extension) is essential because extensions are unreliable. Across the corpus, TGA is the only format guess_format fails to identify; the images decode fine once the format is forced via load_from_memory_with_format(_, ImageFormat::Tga).
Checking real files: every TGA in the corpus (17 images across 6 sources) carries the TGA 2.0 footer. None were header-only 1.0 files. So a footer check would have resolved 100% of the misses.
Scope and honesty about limits
This only helps TGA 2.0. TGA 1.0 has neither header magic nor a footer and stays genuinely unsniffable by any content-based method, so this is a partial solution for the format as a whole. The upside is precision: the footer is a fixed, spec-defined signature, so matching it should not produce false positives.
Spec reference
Truevision TGA File Format Specification 2.0 defines the 26-byte footer whose final 18 bytes are the fixed signature TRUEVISION-XFILE. followed by a null terminator. See the Library of Congress format description (fdd000180) and the Wikipedia summary of the Truevision TGA footer.
Design note
The current guess_format_impl iterates a front-of-buffer prefix/mask table, which does not fit an end-of-buffer check. The public guess_format receives the whole &[u8], and its doc comment already notes it is "not to be trusted on the validity of the whole memory block", so a tail scan is compatible with the current contract. I recognise the front-prefix structure is deliberate and a footer scan sits outside it, so a fallback branch (only reached when the prefix scan returns None) would keep the existing path untouched. It would not help streaming callers that lack the file tail.
Offer
Happy to send a PR adding a footer-signature fallback if the approach is welcome. Wanted to check the design direction first, since it steps outside the front-prefix model. If you would rather leave guess_format as is and keep TGA detection a caller responsibility, that is completely understandable.
Summary
guess_formatcannot identify TGA files, which is expected and documented in the source ("TGA is not supported by this function"). TGA has no header magic, so it does not fit the front-of-bufferMAGIC_BYTEStable.However, TGA 2.0 files do carry a fixed signature: an 18-byte footer,
TRUEVISION-XFILE.\0, at the very end of the file. This proposes an optional fallback that checks for that footer after the existing prefix scan misses, soguess_formatcan recognise TGA 2.0 while leaving the front-prefix table untouched.Why this is worth considering
I maintain a tool that processes a large library of files containing embedded images of mixed formats. Sniffing by content (not extension) is essential because extensions are unreliable. Across the corpus, TGA is the only format
guess_formatfails to identify; the images decode fine once the format is forced viaload_from_memory_with_format(_, ImageFormat::Tga).Checking real files: every TGA in the corpus (17 images across 6 sources) carries the TGA 2.0 footer. None were header-only 1.0 files. So a footer check would have resolved 100% of the misses.
Scope and honesty about limits
This only helps TGA 2.0. TGA 1.0 has neither header magic nor a footer and stays genuinely unsniffable by any content-based method, so this is a partial solution for the format as a whole. The upside is precision: the footer is a fixed, spec-defined signature, so matching it should not produce false positives.
Spec reference
Truevision TGA File Format Specification 2.0 defines the 26-byte footer whose final 18 bytes are the fixed signature
TRUEVISION-XFILE.followed by a null terminator. See the Library of Congress format description (fdd000180) and the Wikipedia summary of the Truevision TGA footer.Design note
The current
guess_format_impliterates a front-of-buffer prefix/mask table, which does not fit an end-of-buffer check. The publicguess_formatreceives the whole&[u8], and its doc comment already notes it is "not to be trusted on the validity of the whole memory block", so a tail scan is compatible with the current contract. I recognise the front-prefix structure is deliberate and a footer scan sits outside it, so a fallback branch (only reached when the prefix scan returnsNone) would keep the existing path untouched. It would not help streaming callers that lack the file tail.Offer
Happy to send a PR adding a footer-signature fallback if the approach is welcome. Wanted to check the design direction first, since it steps outside the front-prefix model. If you would rather leave
guess_formatas is and keep TGA detection a caller responsibility, that is completely understandable.