// 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.varErrFormat = 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 string decode func(io.Reader) (Image, error) decodeConfig func(io.Reader) (Config, error)}// Formats is the list of registered formats.var ( formatsMu sync.Mutex atomicFormats 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.Reader Peek(int) ([]byte, error)}// asReader converts an io.Reader to a reader.func asReader( io.Reader) reader {if , := .(reader); {return }returnbufio.NewReader()}// match reports whether magic matches b. Magic may contain "?" wildcards.func match( string, []byte) bool {iflen() != len() {returnfalse }for , := range {if [] != && [] != '?' {returnfalse } }returntrue}// sniff determines the format of r's data.func sniff( reader) format { , := atomicFormats.Load().([]format)for , := range { , := .Peek(len(.magic))if == nil && match(.magic, ) {return } }returnformat{}}// 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.func ( io.Reader) (Image, string, error) { := asReader() := sniff()if .decode == nil {returnnil, "", 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.func ( io.Reader) (Config, string, error) { := asReader() := sniff()if .decodeConfig == nil {returnConfig{}, "", ErrFormat } , := .decodeConfig()return , .name, }
The pages are generated with Goldsv0.7.3-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.