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 ¶
- Variables
- func BuiltinExt(name string) bool
- func Close(ctx context.Context) error
- func Decode(ctx context.Context, mime, ext string, r io.Reader) (image.Image, string, error)
- func DecodeFile(ctx context.Context, pathName string) (image.Image, string, error)
- func DecodeString(ctx context.Context, s string) (image.Image, string, error)
- func Describe(buf []byte) (string, error)
- func DescribeMime(buf []byte) (string, error)
- func Detect(ctx context.Context, rs io.ReadSeeker) (string, error)
- func Extensions() []string
- func MimeMatch(pattern, mime string) bool
- func Produce(f func(context.Context, *Sink) error) func(context.Context) iter.Seq2[Frame, error]
- func RegisterMimeType(mime string, patterns ...string)
- func State(ctx context.Context) any
- func SupportedExt(name string) bool
- func Unregister(name string) bool
- type Anim
- type DecodeFunc
- type DetectFunc
- type Entry
- type Files
- type Frame
- type Handoff
- type Image
- type MatchFunc
- type Option
- func After(names ...string) Option
- func Before(names ...string) Option
- func BytesDecoder(f func([]byte) (image.Image, error)) Option
- func Decoder(f DecodeFunc) Option
- func Desc(desc string) Option
- func Extension(exts ...string) Option
- func Fallback() Option
- func ImageDecoder(f func(io.Reader) (image.Image, error)) Option
- func ImagesDecoder(f func(io.Reader) ([]image.Image, error)) Option
- func Init(initFunc func(context.Context) (any, error), ...) Option
- func Matcher(f func(mime, ext string) bool) Option
- func MatcherContext(f MatchFunc) Option
- func MimeDetector(f DetectFunc) Option
- func MimeType(mimes ...string) Option
- func MimeTypeExtensionMatch(pairs ...string) Option
- func StringDecoder(f StringDecodeFunc) Option
- func StringMatcher(f func(s string) bool) Option
- type Sink
- type StringDecodeFunc
- type StringMatchFunc
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
DecodeFile decodes the image from the file, returning the image and the detected mime type.
func DecodeString ¶
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 DescribeMime ¶ added in v0.17.2
DescribeMime returns libmagic's mime type for the buffer.
func Detect ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
SupportedExt reports whether the extension of the name is registered by any decoder.
func Unregister ¶
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
// 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
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
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 ¶
DecodeFunc decodes from a reader, returning a pipeline result. See [Resolve] for the recognized result types.
type DetectFunc ¶
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 Match ¶
Match returns the decoders matching the mime type and file extension, in resolution order.
func MatchString ¶
MatchString returns the string decoders matching s, in resolution order.
func RegisterBuiltin ¶
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.
type Files ¶
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.
type Frame ¶ added in v0.18.0
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.
type Image ¶
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.
type Option ¶
type Option func(*Entry)
Option is a decoder option.
func After ¶
After is a decoder option declaring that the decoder must be tried after the named decoders.
func Before ¶
Before is a decoder option declaring that the decoder must be tried before the named decoders.
func BytesDecoder ¶ added in v0.19.0
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 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 ¶
ImageDecoder is a decoder option setting the decoding func from a plain image decoder, such as [png.Decode].
func ImagesDecoder ¶
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 MatcherContext ¶
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 ¶
MimeType is a decoder option to add mime types. A type may end in /* to match every subtype.
func MimeTypeExtensionMatch ¶
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 ¶
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
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 ¶
StringDecodeFunc decodes a bare string (a data: URL, a WIFI: code, a http URL), returning a pipeline result.
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 graphviz supplies a graphviz (dot) decoder for iv.
|
Package graphviz supplies a graphviz (dot) 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, 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 to svg with the mermaid renderer.
|
Package mermaid supplies a mermaid diagram decoder for iv, rendering diagrams to svg with the mermaid renderer. |
|
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 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 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 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. |