Sniff stream signature to preserve sub-format detection - #540
Merged
Conversation
The streaming load derived the image type from the loader nickname alone, so AVIF (loaded via heifload) reported ImageTypeHEIF and BMP/PSD (loaded via magickload) reported ImageTypeMagick with the original format lost. The buffer path detects these from the file signature; ExportNative on a streamed AVIF would pick the HEIC encoder. Sniff the first 12 bytes up front (vips_source_sniff buffers and rewinds, so nothing is consumed) and refine the HEIF/MAGICK loader types through DetermineImageType, mirroring vipsLoadFromBuffer's current/original format split. Flagged by Macroscope on #539. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
davidbyttow
added a commit
that referenced
this pull request
Aug 8, 2026
Setup, commands, package layout, and the conventions that have caused real bugs when broken: libvips input-ref ownership (#531), band-count background vectors (#534), libvips version guards for new properties (#535), signature-based format detection over loader nicknames (#540). Also documents the golden-file scheme, leak detector, byte-identity requirement for streaming savers, and known macOS-local test failures. CLAUDE.md now defers to AGENTS.md for shared content and keeps only the Claude-specific worktree flow. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #539, fixing the issue Macroscope flagged there: the streaming load derived the image type from the loader nickname alone, so AVIF (via
heifload_source) reportedImageTypeHEIF, and BMP/PSD (viamagickload_source) reportedImageTypeMagickwith the original format lost. The buffer path detects these from the file signature — andExportNativeon a streamed AVIF would have picked the HEIC encoder.Fix: sniff the first 12 bytes before loading (
vips_source_sniffbuffers and rewinds, so nothing is consumed, and it works for every source kind at that point) and refine the ambiguous HEIF/MAGICK loader types through the existingDetermineImageType, mirroringvipsLoadFromBuffer's current/original format split exactly.Tests
TestLoadImageFromReader_SubFormatDetection: foravif-8bit.avif,bmp.bmp, andpsd.example.psd, streamingFormat()/OriginalFormat()must equal the buffer path's. BMP/PSD verified locally; AVIF exercises on CI (local HEIF module is broken — missing libx265).🤖 Generated with Claude Code
Note
Sniff stream signature to preserve sub-format detection in
LoadImageFromReadersource_sniff_headerC helper in stream.c that reads up to N bytes from aVipsSourceCustomwithout consuming the stream, usingvips_source_sniffandmemcpy.LoadImageFromReaderin stream.go now sniffs up to 12 bytes before loading, then usesDetermineImageTypeto refineOriginalFormatwhen the loader reports HEIF or Magick (e.g. resolving to AVIF, BMP, or PSD).Formatis also set to the sniffed sub-format when the detected type does not require Magick, matching the behavior ofLoadImageFromBuffer.FormatandOriginalFormatagree between the buffer and reader load paths for AVIF, BMP, and PSD inputs.Macroscope summarized 14a9aee.