2010-08-08 18:44:38 -06:00
|
|
|
// 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 image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2011-11-01 20:04:37 -06:00
|
|
|
"errors"
|
2010-08-08 18:44:38 -06:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2012-02-05 17:04:12 -07:00
|
|
|
// ErrFormat indicates that decoding encountered an unknown format.
|
|
|
|
var ErrFormat = errors.New("image: unknown format")
|
2010-08-08 18:44:38 -06:00
|
|
|
|
|
|
|
// A format holds an image format's name, magic header and how to decode it.
|
|
|
|
type format struct {
|
2010-09-03 02:03:08 -06:00
|
|
|
name, magic string
|
2011-11-01 20:04:37 -06:00
|
|
|
decode func(io.Reader) (Image, error)
|
|
|
|
decodeConfig func(io.Reader) (Config, error)
|
2010-08-08 18:44:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formats is the list of registered formats.
|
|
|
|
var formats []format
|
|
|
|
|
|
|
|
// RegisterFormat registers an image format for use by Decode.
|
|
|
|
// Name is the name of the format, like "jpeg" or "png".
|
2011-04-13 17:56:42 -06:00
|
|
|
// Magic is the magic prefix that identifies the format's encoding. The magic
|
|
|
|
// string can contain "?" wildcards that each match any one byte.
|
2010-08-08 18:44:38 -06:00
|
|
|
// Decode is the function that decodes the encoded image.
|
2010-09-03 02:03:08 -06:00
|
|
|
// DecodeConfig is the function that decodes just its configuration.
|
2011-11-01 20:04:37 -06:00
|
|
|
func RegisterFormat(name, magic string, decode func(io.Reader) (Image, error), decodeConfig func(io.Reader) (Config, error)) {
|
2010-10-27 20:47:23 -06:00
|
|
|
formats = append(formats, format{name, magic, decode, decodeConfig})
|
2010-08-08 18:44:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A reader is an io.Reader that can also peek ahead.
|
|
|
|
type reader interface {
|
|
|
|
io.Reader
|
2011-11-01 20:04:37 -06:00
|
|
|
Peek(int) ([]byte, error)
|
2010-08-08 18:44:38 -06:00
|
|
|
}
|
|
|
|
|
2013-01-06 17:15:53 -07:00
|
|
|
// asReader converts an io.Reader to a reader.
|
2010-08-08 18:44:38 -06:00
|
|
|
func asReader(r io.Reader) reader {
|
|
|
|
if rr, ok := r.(reader); ok {
|
|
|
|
return rr
|
|
|
|
}
|
|
|
|
return bufio.NewReader(r)
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:59:49 -06:00
|
|
|
// Match reports whether magic matches b. Magic may contain "?" wildcards.
|
2011-04-13 17:56:42 -06:00
|
|
|
func match(magic string, b []byte) bool {
|
|
|
|
if len(magic) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, c := range b {
|
|
|
|
if magic[i] != c && magic[i] != '?' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sniff determines the format of r's data.
|
2010-09-03 02:03:08 -06:00
|
|
|
func sniff(r reader) format {
|
|
|
|
for _, f := range formats {
|
2011-04-13 17:56:42 -06:00
|
|
|
b, err := r.Peek(len(f.magic))
|
|
|
|
if err == nil && match(f.magic, b) {
|
2010-09-03 02:03:08 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return format{}
|
|
|
|
}
|
|
|
|
|
2010-08-08 18:44:38 -06:00
|
|
|
// Decode decodes an image that has been encoded in a registered format.
|
2010-09-03 02:03:08 -06:00
|
|
|
// The string returned is the format name used during format registration.
|
2013-05-14 14:28:16 -06:00
|
|
|
// Format registration is typically done by an init function in the codec-
|
2010-08-08 18:44:38 -06:00
|
|
|
// specific package.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Decode(r io.Reader) (Image, string, error) {
|
2010-08-08 18:44:38 -06:00
|
|
|
rr := asReader(r)
|
2010-09-03 02:03:08 -06:00
|
|
|
f := sniff(rr)
|
2010-08-08 18:44:38 -06:00
|
|
|
if f.decode == nil {
|
2012-02-05 17:04:12 -07:00
|
|
|
return nil, "", ErrFormat
|
2010-08-08 18:44:38 -06:00
|
|
|
}
|
2010-09-03 02:03:08 -06:00
|
|
|
m, err := f.decode(rr)
|
2010-08-08 18:44:38 -06:00
|
|
|
return m, f.name, err
|
|
|
|
}
|
2010-09-03 02:03:08 -06:00
|
|
|
|
|
|
|
// 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
|
2013-05-14 14:28:16 -06:00
|
|
|
// an init function in the codec-specific package.
|
2011-11-01 20:04:37 -06:00
|
|
|
func DecodeConfig(r io.Reader) (Config, string, error) {
|
2010-09-03 02:03:08 -06:00
|
|
|
rr := asReader(r)
|
|
|
|
f := sniff(rr)
|
|
|
|
if f.decodeConfig == nil {
|
2012-02-05 17:04:12 -07:00
|
|
|
return Config{}, "", ErrFormat
|
2010-09-03 02:03:08 -06:00
|
|
|
}
|
|
|
|
c, err := f.decodeConfig(rr)
|
|
|
|
return c, f.name, err
|
|
|
|
}
|