2014-03-25 03:29:48 +01:00
|
|
|
// Copyright 2011 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 webp implements a decoder for WEBP images.
|
|
|
|
//
|
2014-06-17 13:51:57 +02:00
|
|
|
// WEBP is defined at:
|
|
|
|
// https://developers.google.com/speed/webp/docs/riff_container
|
2014-03-25 03:29:48 +01:00
|
|
|
package webp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"code.google.com/p/go.image/vp8"
|
2014-06-17 13:51:57 +02:00
|
|
|
"code.google.com/p/go.image/vp8l"
|
2014-03-25 03:29:48 +01:00
|
|
|
)
|
|
|
|
|
2014-06-17 13:51:57 +02:00
|
|
|
const (
|
|
|
|
formatVP8 = 1
|
|
|
|
formatVP8L = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
func decode(r io.Reader, configOnly bool) (image.Image, image.Config, error) {
|
2014-03-25 03:29:48 +01:00
|
|
|
var b [20]byte
|
2014-06-17 13:51:57 +02:00
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
|
|
|
return nil, image.Config{}, err
|
|
|
|
}
|
|
|
|
format := 0
|
|
|
|
switch string(b[8:16]) {
|
|
|
|
case "WEBPVP8 ":
|
|
|
|
format = formatVP8
|
|
|
|
case "WEBPVP8L":
|
|
|
|
format = formatVP8L
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
2014-06-17 13:51:57 +02:00
|
|
|
if string(b[:4]) != "RIFF" || format == 0 {
|
|
|
|
return nil, image.Config{}, errors.New("webp: invalid format")
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
|
|
|
riffLen := uint32(b[4]) | uint32(b[5])<<8 | uint32(b[6])<<16 | uint32(b[7])<<24
|
|
|
|
dataLen := uint32(b[16]) | uint32(b[17])<<8 | uint32(b[18])<<16 | uint32(b[19])<<24
|
|
|
|
if riffLen < dataLen+12 {
|
2014-06-17 13:51:57 +02:00
|
|
|
return nil, image.Config{}, errors.New("webp: invalid format")
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
|
|
|
if dataLen >= 1<<31 {
|
2014-06-17 13:51:57 +02:00
|
|
|
return nil, image.Config{}, errors.New("webp: invalid format")
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
2014-06-17 13:51:57 +02:00
|
|
|
|
|
|
|
if format == formatVP8 {
|
|
|
|
d := vp8.NewDecoder()
|
|
|
|
d.Init(r, int(dataLen))
|
|
|
|
fh, err := d.DecodeFrameHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, image.Config{}, err
|
|
|
|
}
|
|
|
|
if configOnly {
|
|
|
|
return nil, image.Config{
|
|
|
|
ColorModel: color.YCbCrModel,
|
|
|
|
Width: fh.Width,
|
|
|
|
Height: fh.Height,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
m, err := d.DecodeFrame()
|
|
|
|
return m, image.Config{}, nil
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
2014-06-17 13:51:57 +02:00
|
|
|
|
2014-06-30 02:36:25 +02:00
|
|
|
r = &io.LimitedReader{R: r, N: int64(dataLen)}
|
2014-06-17 13:51:57 +02:00
|
|
|
if configOnly {
|
|
|
|
c, err := vp8l.DecodeConfig(r)
|
|
|
|
return nil, c, err
|
|
|
|
}
|
|
|
|
m, err := vp8l.Decode(r)
|
|
|
|
return m, image.Config{}, err
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode reads a WEBP image from r and returns it as an image.Image.
|
|
|
|
func Decode(r io.Reader) (image.Image, error) {
|
2014-06-17 13:51:57 +02:00
|
|
|
m, _, err := decode(r, false)
|
2014-03-25 03:29:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-17 13:51:57 +02:00
|
|
|
return m, err
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeConfig returns the color model and dimensions of a WEBP image without
|
|
|
|
// decoding the entire image.
|
|
|
|
func DecodeConfig(r io.Reader) (image.Config, error) {
|
2014-06-17 13:51:57 +02:00
|
|
|
_, c, err := decode(r, true)
|
|
|
|
return c, err
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-06-17 13:51:57 +02:00
|
|
|
image.RegisterFormat("webp", "RIFF????WEBPVP8", Decode, DecodeConfig)
|
2014-03-25 03:29:48 +01:00
|
|
|
}
|