decoder

package
v0.22.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package decoder provides the decoder registration and resolution mechanisms for iv.

A decoder converts some input (a byte stream, or a bare string such as a data: URL) into something the pipeline knows how to finish: an image, a set of images, or another input to feed back through the pipeline.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSupported is returned when no registered decoder handles the
	// input.
	ErrNotSupported = errors.New("not supported")
	// ErrUnsupportedFormat is returned when a decoder handles the input's
	// format but the underlying library was not built with support for it.
	ErrUnsupportedFormat = errors.New("format not supported by this build")
	// ErrNotAnimated is returned when an animation was asked for and the
	// input decoded to a still image instead.
	ErrNotAnimated = errors.New("no animation")
)

Functions

func BuiltinExt

func BuiltinExt(name string) bool

BuiltinExt reports whether the extension of the name is registered by a builtin (image.Decode) decoder. Containers such as comic archives use this to pick the entries they can render directly.

func Close

func Close(ctx context.Context) error

Close closes every decoder that was initialized, returning the joined errors.

A close func is handed its decoder's state the same way a decode func is, through State -- it is what there is to close, and the close runs long after the decode that built it.

func Decode

func Decode(ctx context.Context, mime, ext string, r io.Reader) (image.Image, string, error)

Decode decodes an image from the reader using the decoders registered for the mime type and file extension, returning the image and the mime type it was decoded as.

func DecodeFile

func DecodeFile(ctx context.Context, pathName string) (image.Image, string, error)

DecodeFile decodes the image from the file, returning the image and the detected mime type.

func DecodeString

func DecodeString(ctx context.Context, s string) (image.Image, string, error)

DecodeString decodes a bare string argument -- a data: URL, a WIFI: code, a http URL -- returning the image and the mime type it was decoded as.

func Describe

func Describe(ctx context.Context, buf []byte) (string, error)

Describe returns the description of the buffer that libmagic gives, as the file command prints it.

func Detect

func Detect(ctx context.Context, rs io.ReadSeeker) (string, error)

Detect determines the mime type of the reader.

The forced mime type wins, then any decoder supplied MimeDetector, then libmagic's mime type, and finally libmagic's description of the content matched against the patterns registered with RegisterMimeType -- which is what identifies the formats libmagic describes but does not type, fonts among them.

The reader is rewound to its start before returning.

func Extensions

func Extensions() []string

Extensions returns the sorted, deduplicated set of every registered file extension. Used to decide which files in a directory are worth opening.

func MimeMatch

func MimeMatch(pattern, mime string) bool

MimeMatch reports whether the mime type matches the pattern. A pattern may end in /* to match an entire type, and a "+suffix" (image/svg+xml) matches its unsuffixed form.

func Produce added in v0.18.0

func Produce(f func(context.Context, *Sink) error) func(context.Context) iter.Seq2[Frame, error]

Produce returns a frame func for Anim.Frames that runs f in a goroutine of its own, feeding the frames it sends to the playback as they are made.

f is started afresh for every pass and is handed a context that is cancelled when the pass ends, so that a producer blocked on Sink.Send -- and the external commands it is running -- stop along with the playback rather than running on to the end of an animation that is no longer being watched.

func RegisterMimeType

func RegisterMimeType(mime string, patterns ...string)

RegisterMimeType registers regular expressions matched against libmagic's description of content whose mime type could not otherwise be determined. The first matching pattern wins.

func State

func State(ctx context.Context) any

State returns the value produced by the running decoder's Init func. It is on the context of a decode func and of a close func alike.

func SupportedExt

func SupportedExt(name string) bool

SupportedExt reports whether the extension of the name is registered by any decoder.

func Unregister

func Unregister(name string) bool

Unregister removes the named decoder. Returns whether it was registered. Not used by iv itself; provided for tests.

Types

type Anim added in v0.18.0

type Anim struct {
	// Frames returns the animation's frames, in order. It is called once for
	// each pass, so a looping playback produces the frames again rather than
	// holding every one of them in memory: an animation is as many as
	// [ivctx.AnimateMax] leaves room for, each the size of the terminal,
	// which is more than there is any need to keep.
	Frames func(context.Context) iter.Seq2[Frame, error]
	// Count is how many frames a pass yields, 0 when that is not known ahead
	// of time.
	Count int
	// Duration is how long a pass runs for, 0 when not known.
	Duration time.Duration
	// Video is true when the frames are a video, which plays at its own
	// speed (D43). A pass is as long as the video, with no
	// [ivctx.AnimateMax], and the player drops each frame that comes late,
	// also when the decoder made it late, so that the video keeps its own
	// length.
	Video bool
	// Close releases whatever produced the frames -- a spooled copy of the
	// source, a loaded animation -- once the playback has finished with it.
	// Nil when there is nothing to release.
	Close func() error
}

Anim is a decoded animation, returned by a decoder in place of an image when the pipeline is decoding for playback -- see ivctx.Animate.

func DecodeAnimFile added in v0.18.0

func DecodeAnimFile(ctx context.Context, pathName string) (*Anim, string, error)

DecodeAnimFile decodes the animation in the file, returning it and the detected mime type. Returns ErrNotAnimated when the file holds a still image, or an animated format that the decoder handling it renders as one.

The config's Animate must be set for this to find an animation at all: it is what tells a decoder to produce the frames rather than pick one of them -- see ivctx.Animate.

func DecodeAnimString added in v0.18.0

func DecodeAnimString(ctx context.Context, s string) (*Anim, string, error)

DecodeAnimString decodes the animation named by a bare string argument, returning it and the mime type it was decoded as. Returns ErrNotAnimated when the string decoded to a still image.

type DecodeFunc

type DecodeFunc func(context.Context, io.Reader) (any, error)

DecodeFunc decodes from a reader, returning a pipeline result. See [Resolve] for the recognized result types.

type DetectFunc

type DetectFunc func(context.Context, io.Reader) (string, error)

DetectFunc sniffs a reader for a mime type, returning an empty string when the content is not recognized. It must not consume the reader.

type Entry

type Entry struct {
	// Name is the unique decoder name.
	Name string
	// Desc is the human readable description.
	Desc string
	// Builtin indicates the decoder's formats are in Go's [image.Decode]
	// registry, and so can be rendered from inside archives and other
	// containers. The decoder may still supply a decode func of its own --
	// see [RegisterBuiltin].
	Builtin bool
	// IsString indicates a string decoder, matched against a bare command
	// line argument instead of a byte stream.
	IsString bool
	// Exts are the file extensions, without a leading dot.
	Exts []string
	// Mimes are the mime types, optionally with a trailing /* wildcard.
	Mimes []string
	// Fallback indicates a decoder of last resort, tried only once every
	// other decoder has declined or failed.
	Fallback bool
	// contains filtered or unexported fields
}

Entry is a registered iv image decoder.

func All

func All() []*Entry

All returns all registered decoders in resolution order.

func Get

func Get(name string) (*Entry, bool)

Get returns the named decoder.

func Match

func Match(ctx context.Context, mime, ext string) []*Entry

Match returns the decoders matching the mime type and file extension, in resolution order.

func MatchString

func MatchString(ctx context.Context, s string) []*Entry

MatchString returns the string decoders matching s, in resolution order.

func Register

func Register(name string, opts ...Option) *Entry

Register registers a decoder. It panics when name is empty or already registered.

func RegisterBuiltin

func RegisterBuiltin(name string, opts ...Option) *Entry

RegisterBuiltin registers a decoder for a format that is in Go's image.Decode registry, which is what lets container formats such as comic archives render it directly -- see BuiltinExt. The format's package must be imported for its side effects.

It decodes through image.Decode unless a Decoder, ImageDecoder, or ImagesDecoder option supplies a decode func of its own, which formats needing more than one image or a fallback do.

func (*Entry) String

func (d *Entry) String() string

String satisfies the fmt.Stringer interface.

type Files

type Files struct {
	FS    fs.FS
	Names []string
}

Files is a set of named files within a file system, from which the configured page is decoded. Used by container formats such as comic archives.

func FS

func FS(fsys fs.FS, names ...string) *Files

FS creates a file set for the decoding pipeline.

type Frame added in v0.18.0

type Frame struct {
	Image image.Image
	Delay time.Duration
}

Frame is one frame of an animation, scaled to the size it is displayed at, with the time it is shown for.

type Handoff

type Handoff struct {
	Name string
}

Handoff hands decoding of the same stream to a specific decoder.

func Next

func Next(name string) *Handoff

Next creates a hand off to the named decoder.

type Image

type Image struct {
	Mime   string
	Ext    string
	Reader io.Reader
}

Image is a mime type and stream to run back through the decoding pipeline. Used by decoders that transform their input into another format, such as libreoffice converting a document to pdf.

func NewBytes

func NewBytes(mime string, buf []byte) *Image

NewBytes creates an image for the decoding pipeline to retry from a byte slice.

func NewImage

func NewImage(mime string, r io.Reader) *Image

NewImage creates an image for the decoding pipeline to retry.

func (*Image) WithExt

func (im *Image) WithExt(ext string) *Image

WithExt sets the file extension reported to the pipeline on retry.

type MatchFunc

type MatchFunc func(ctx context.Context, mime, ext string) bool

MatchFunc reports whether a decoder handles the mime type / file extension.

type Option

type Option func(*Entry)

Option is a decoder option.

func After

func After(names ...string) Option

After is a decoder option declaring that the decoder must be tried after the named decoders.

func Before

func Before(names ...string) Option

Before is a decoder option declaring that the decoder must be tried before the named decoders.

func BytesDecoder added in v0.19.0

func BytesDecoder(f func([]byte) (image.Image, error)) Option

BytesDecoder is a decoder option setting the decoding func from an image decoder that takes the whole of the input, such as ivimg.DecodePNG. A decoder that keeps the bytes of the input with the image takes them this way.

func Decoder

func Decoder(f DecodeFunc) Option

Decoder is a decoder option to set the decoding func.

func Desc

func Desc(desc string) Option

Desc is a decoder option to set the human readable description.

func Extension

func Extension(exts ...string) Option

Extension is a decoder option to add file extensions, without a leading dot.

func Fallback

func Fallback() Option

Fallback is a decoder option marking the decoder as a last resort, tried only after every other decoder has declined or failed.

func ImageDecoder

func ImageDecoder(f func(io.Reader) (image.Image, error)) Option

ImageDecoder is a decoder option setting the decoding func from a plain image decoder, such as [png.Decode].

func ImagesDecoder

func ImagesDecoder(f func(io.Reader) ([]image.Image, error)) Option

ImagesDecoder is a decoder option setting the decoding func from a multi-image decoder, such as [ico.DecodeAll]. The configured page selects which image is displayed.

func Init

func Init(initFunc func(context.Context) (any, error), closeFunc func(context.Context) error) Option

Init is a decoder option to set the lazy initialization and close funcs. The value returned by the init func is available through State to both the decode func and the close func, which is what the close func has to release.

The init func runs at most once, on the first decode the decoder is tried for, and the close func at most once, when Close is called.

func Matcher

func Matcher(f func(mime, ext string) bool) Option

Matcher is a decoder option adding an arbitrary mime type/extension matcher.

func MatcherContext

func MatcherContext(f MatchFunc) Option

MatcherContext is a decoder option adding a context aware matcher.

func MimeDetector

func MimeDetector(f DetectFunc) Option

MimeDetector is a decoder option to add a content sniffer, used when the standard mime detection cannot identify the content. The func must not consume the reader.

func MimeType

func MimeType(mimes ...string) Option

MimeType is a decoder option to add mime types. A type may end in /* to match every subtype.

func MimeTypeExtensionMatch

func MimeTypeExtensionMatch(pairs ...string) Option

MimeTypeExtensionMatch is a decoder option adding a matcher for mime type/extension pairs, where neither alone is enough to identify the format. Panics when not passed an even number of arguments.

func StringDecoder

func StringDecoder(f StringDecodeFunc) Option

StringDecoder is a decoder option marking the decoder as a string decoder and setting its decoding func.

func StringMatcher

func StringMatcher(f func(s string) bool) Option

StringMatcher is a decoder option marking the decoder as a string decoder and adding a matcher for bare command line arguments.

type Sink added in v0.18.0

type Sink struct {
	// contains filtered or unexported fields
}

Sink is what an animation's producer sends its frames to.

func (*Sink) Send added in v0.18.0

func (s *Sink) Send(ctx context.Context, img image.Image, delay time.Duration) bool

Send hands a frame to the playback, blocking while the playback is still working through the frames already sent. ctx is the context that Produce handed the producer, which is cancelled once the playback has ended.

The frame is scaled and composited here with ivimg.Prepare, in the producer's goroutine, so that the work of preparing one frame overlaps the drawing of the one before it.

Reports whether the frame was taken. A false return means that the producer must stop: the playback ended, because it was interrupted or ran out of time, or ivimg.Prepare returned an error for the frame. The playback reports that error.

type StringDecodeFunc

type StringDecodeFunc func(context.Context, string) (any, error)

StringDecodeFunc decodes a bare string (a data: URL, a WIFI: code, a http URL), returning a pipeline result.

type StringMatchFunc

type StringMatchFunc func(ctx context.Context, s string) bool

StringMatchFunc reports whether a string decoder handles the string.

Directories

Path Synopsis
Package all registers every iv decoder.
Package all registers every iv decoder.
Package archives supplies a comic book archive decoder for iv.
Package archives supplies a comic book archive decoder for iv.
Package binwalk supplies a decoder for iv that extracts images embedded in otherwise unrecognized files using the `binwalk` command.
Package binwalk supplies a decoder for iv that extracts images embedded in otherwise unrecognized files using the `binwalk` command.
Package blitz supplies a html and markdown decoder for iv, rendering documents and remote pages with the blitz browser engine.
Package blitz supplies a html and markdown decoder for iv, rendering documents and remote pages with the blitz browser engine.
Package bmp supplies a bmp decoder for iv.
Package bmp supplies a bmp decoder for iv.
Package calibre supplies an ebook decoder for iv, converting books to pdf with the `ebook-convert` command of calibre.
Package calibre supplies an ebook decoder for iv, converting books to pdf with the `ebook-convert` command of calibre.
Package data supplies an inline data: URL decoder for iv.
Package data supplies an inline data: URL decoder for iv.
Package ffmpeg supplies a video decoder for iv, snapshotting a frame with the `ffmpeg` command.
Package ffmpeg supplies a video decoder for iv, snapshotting a frame with the `ffmpeg` command.
Package fontimg supplies a font preview decoder for iv.
Package fontimg supplies a font preview decoder for iv.
Package gif supplies a gif decoder for iv.
Package gif supplies a gif decoder for iv.
Package gxps supplies an XPS decoder for iv, rendering a page to png with the `xpstopng` command of libgxps.
Package gxps supplies an XPS decoder for iv, rendering a page to png with the `xpstopng` command of libgxps.
Package icns supplies an icns (Apple Icon Image) decoder for iv.
Package icns supplies an icns (Apple Icon Image) decoder for iv.
Package ico supplies an ico decoder for iv.
Package ico supplies an ico decoder for iv.
Package jpeg supplies a jpeg decoder for iv.
Package jpeg supplies a jpeg decoder for iv.
Package libreoffice supplies an office document decoder for iv, converting documents to pdf with the `soffice` command.
Package libreoffice supplies an office document decoder for iv, converting documents to pdf with the `soffice` command.
Package lottie supplies a lottie animation decoder for iv.
Package lottie supplies a lottie animation decoder for iv.
Package nativewebp supplies a webp decoder for iv.
Package nativewebp supplies a webp decoder for iv.
Package netpbm supplies a netpbm decoder for iv.
Package netpbm supplies a netpbm decoder for iv.
Package okage supplies a svg decoder for iv.
Package okage supplies a svg decoder for iv.
Package png supplies a png decoder for iv.
Package png supplies a png decoder for iv.
Package poppler supplies a pdf decoder for iv, rendering a page to png with the `pdftoppm` command of poppler.
Package poppler supplies a pdf decoder for iv, rendering a page to png with the `pdftoppm` command of poppler.
Package psd supplies a Photoshop decoder for iv.
Package psd supplies a Photoshop decoder for iv.
Package qr supplies a QR code decoder for iv, rendering the uris that name something to act on rather than something to fetch.
Package qr supplies a QR code decoder for iv, rendering the uris that name something to act on rather than something to fetch.
Package tag supplies an audio metadata decoder for iv, rendering a card of the track's cover art, tags and waveform.
Package tag supplies an audio metadata decoder for iv, rendering a card of the track's cover art, tags and waveform.
Package tiff supplies a tiff decoder for iv.
Package tiff supplies a tiff decoder for iv.
Package vcard supplies a vcard decoder for iv, drawing a contact as a business card with a scannable copy of the record on it.
Package vcard supplies a vcard decoder for iv, drawing a contact as a business card with a scannable copy of the record on it.
Package vips supplies a libvips decoder for iv, covering the image formats and pdfs that the Go decoders do not handle.
Package vips supplies a libvips decoder for iv, covering the image formats and pdfs that the Go decoders do not handle.
Package winres supplies a Windows PE decoder for iv, rendering the icons embedded in an executable.
Package winres supplies a Windows PE decoder for iv, rendering the icons embedded in an executable.
Package zuzu supplies a DOT graph, Mermaid diagram, and Vega and Vega-Lite chart decoder for iv.
Package zuzu supplies a DOT graph, Mermaid diagram, and Vega and Vega-Lite chart decoder for iv.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL