Source File
format.go
Belonging Package
image
// Copyright 2010 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package imageimport ()// ErrFormat indicates that decoding encountered an unknown format.var ErrFormat = errors.New("image: unknown format")// A format holds an image format's name, magic header and how to decode it.type format struct {name, magic stringdecode func(io.Reader) (Image, error)decodeConfig func(io.Reader) (Config, error)}// Formats is the list of registered formats.var (formatsMu sync.MutexatomicFormats atomic.Value)// RegisterFormat registers an image format for use by [Decode].// Name is the name of the format, like "jpeg" or "png".// Magic is the magic prefix that identifies the format's encoding. The magic// string can contain "?" wildcards that each match any one byte.// [Decode] is the function that decodes the encoded image.// [DecodeConfig] is the function that decodes just its configuration.func (, string, func(io.Reader) (Image, error), func(io.Reader) (Config, error)) {formatsMu.Lock(), := atomicFormats.Load().([]format)atomicFormats.Store(append(, format{, , , }))formatsMu.Unlock()}// A reader is an io.Reader that can also peek ahead.type reader interface {io.ReaderPeek(int) ([]byte, error)}// asReader converts an io.Reader to a reader.func ( io.Reader) reader {if , := .(reader); {return}return bufio.NewReader()}// match reports whether magic matches b. Magic may contain "?" wildcards.func ( string, []byte) bool {if len() != len() {return false}for , := range {if [] != && [] != '?' {return false}}return true}// sniff determines the format of r's data.func ( reader) format {, := atomicFormats.Load().([]format)for , := range {, := .Peek(len(.magic))if == nil && match(.magic, ) {return}}return format{}}// Decode decodes an image that has been encoded in a registered format.// The string returned is the format name used during format registration.// Format registration is typically done by an init function in the codec-// specific package.//// Decoding may allocate memory proportional to the width and height in the// image header before all pixel data is consumed or validated. When// decoding untrusted input, call [DecodeConfig] first to inspect dimensions// and reject images that would exceed resource limits; see the "Security// Considerations" section in the [image] package documentation.func ( io.Reader) (Image, string, error) {:= asReader():= sniff()if .decode == nil {return nil, "", ErrFormat}, := .decode()return , .name,}// DecodeConfig decodes the color model and dimensions of an image that has// been encoded in a registered format. The string returned is the format name// used during format registration. Format registration is typically done by// an init function in the codec-specific package.//// DecodeConfig reads only format headers and does not allocate a full-size// pixel buffer, so it can be used to check dimensions before calling [Decode].func ( io.Reader) (Config, string, error) {:= asReader():= sniff()if .decodeConfig == nil {return Config{}, "", ErrFormat}, := .decodeConfig()return , .name,}
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64)