decoder

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 17 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")
)

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(buf []byte) (string, error)

Describe returns libmagic's textual description of the buffer.

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 content sniffing, and finally libmagic's description of the content matched against the patterns registered with RegisterMimeType.

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 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 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 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 graphviz rendering dot to svg.

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 (img *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 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 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 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 fitz supplies a mupdf (fitz) decoder for iv, covering document formats such as epub, xps, mobi, and fb2.
Package fitz supplies a mupdf (fitz) decoder for iv, covering document formats such as epub, xps, mobi, and fb2.
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 graphviz supplies a graphviz (dot) decoder for iv.
Package graphviz supplies a graphviz (dot) decoder for iv.
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, rasterizing frames with the thorvg vector engine.
Package lottie supplies a lottie animation decoder for iv, rasterizing frames with the thorvg vector engine.
Package mermaid supplies a mermaid diagram decoder for iv, rendering diagrams with the mermaid cli (mmdc).
Package mermaid supplies a mermaid diagram decoder for iv, rendering diagrams with the mermaid cli (mmdc).
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 png supplies a png decoder for iv.
Package png supplies a png decoder for iv.
Package qr supplies a QR code decoder for iv, rendering WIFI: codes as images.
Package qr supplies a QR code decoder for iv, rendering WIFI: codes as images.
Package resvg supplies a svg decoder for iv.
Package resvg supplies a svg decoder for iv.
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 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.

Jump to

Keyboard shortcuts

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