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 Detect(ctx context.Context, rs io.ReadSeeker) (string, error)
- func Extensions() []string
- func MimeMatch(pattern, mime string) bool
- func RegisterMimeType(mime string, patterns ...string)
- func State(ctx context.Context) any
- func SupportedExt(name string) bool
- func Unregister(name string) bool
- type DecodeFunc
- type DetectFunc
- type Entry
- type Files
- type Handoff
- type Image
- type MatchFunc
- type Option
- func After(names ...string) Option
- func Before(names ...string) 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 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") )
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 Detect ¶
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 ¶
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 ¶
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 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 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 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 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 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. |