Initial QSfera import

This commit is contained in:
Курнат Андрей
2026-06-07 10:20:04 +03:00
commit 2315f25754
16485 changed files with 4826827 additions and 0 deletions
+209
View File
@@ -0,0 +1,209 @@
package webp
import (
"bytes"
"errors"
"image"
"io"
"golang.org/x/image/riff"
"golang.org/x/image/vp8"
"golang.org/x/image/vp8l"
)
var (
errNotExtended = errors.New("there was no vp8x header in this webp file, it cannot be animated")
errNotAnimated = errors.New("the vp8x header did not have the animation bit set")
)
func decodeAnimated(r io.Reader) (*AnimatedWEBP, error) {
riffReader, err := webpRiffReader(r)
if err != nil {
return nil, err
}
vp8xHeader, err := validateVP8XHeader(riffReader)
if err != nil {
return nil, err
}
animHeader, err := validateANIMHeader(riffReader)
if err != nil {
return nil, err
}
awp := AnimatedWEBP{
Frames: make([]Frame, 0, 128),
Header: animHeader,
Config: image.Config{
ColorModel: nil, // TODO(patricsss) set the color model correctly
Width: int(vp8xHeader.CanvasWidth),
Height: int(vp8xHeader.CanvasHeight),
},
}
for {
frame, err := parseFrame(riffReader)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
awp.Frames = append(awp.Frames, *frame)
}
return &awp, nil
}
func validateVP8XHeader(r *riff.Reader) (VP8XHeader, error) {
fourCC, chunkLen, chunkData, err := r.Next()
if err != nil {
return VP8XHeader{}, err
}
if fourCC != fccVP8X {
return VP8XHeader{}, errNotExtended
}
if chunkLen != 10 {
return VP8XHeader{}, errInvalidFormat
}
h := parseVP8XHeader(chunkData)
if !h.Animation {
return VP8XHeader{}, errNotAnimated
}
return h, nil
}
func validateANIMHeader(r *riff.Reader) (ANIMHeader, error) {
fourCC, chunkLen, chunkData, err := r.Next()
if err != nil {
return ANIMHeader{}, err
}
if fourCC != fccANIM {
return ANIMHeader{}, errInvalidFormat
}
if chunkLen != 6 {
return ANIMHeader{}, errInvalidFormat
}
h := parseANIMHeader(chunkData)
return h, nil
}
func parseFrame(r *riff.Reader) (*Frame, error) {
fourCC, chunkLen, chunkData, err := r.Next()
if err != nil {
return nil, err
}
if fourCC != fccANMF {
return nil, errInvalidFormat
}
anmfHeader := parseANMFHeader(chunkData)
// buffer chunk data based on chunkLen for safety
// TODO(patricsss): establish if this is necessary, perhaps chunkData has a bounds
// ANMF headers are 16 bytes
wrappedChunkData, err := rewrap(chunkData, int(chunkLen-16))
if err != nil {
return nil, err
}
subReader := NewSubChunkReader(wrappedChunkData)
var (
alpha []byte
stride int
i *image.YCbCr
)
subFourCC, subChunkData, subChunkLen, err := subReader.Next()
if subFourCC == fccALPH {
alpha, stride, err = decodeAlpha(subChunkData, int(subChunkLen), anmfHeader)
if err != nil {
return nil, err
}
// read next chunk
subFourCC, subChunkData, subChunkLen, err = subReader.Next()
if err != nil {
return nil, err
}
}
var out image.Image
switch subFourCC {
case fccVP8:
i, err = decodeVp8Bitstream(subChunkData, int(subChunkLen))
if err != nil {
return nil, err
}
if len(alpha) > 0 {
out = &image.NYCbCrA{
YCbCr: *i,
A: alpha,
AStride: stride,
}
} else {
out = i
}
case fccVP8L:
out, err = vp8l.Decode(subChunkData)
if err != nil {
return nil, err
}
default:
return nil, errInvalidFormat
}
return &Frame{
Header: anmfHeader,
Frame: out,
}, nil
}
func decodeVp8Bitstream(chunkData io.Reader, chunkLen int) (*image.YCbCr, error) {
dec := vp8.NewDecoder()
dec.Init(chunkData, chunkLen)
_, err := dec.DecodeFrameHeader()
if err != nil {
return nil, err
}
i, err := dec.DecodeFrame()
if err != nil {
return nil, err
}
return i, nil
}
func decodeAlpha(chunkData io.Reader, chunkLen int, h ANMFHeader) (alpha []byte, alphaStride int, err error) {
alphHeader := parseALPHHeader(chunkData)
// Length of the chunk minus 1 byte for the ALPH header
buf := make([]byte, chunkLen-1)
n, err := io.ReadFull(chunkData, buf)
if err != nil {
return nil, 0, err
}
if n != len(buf) {
return nil, 0, errInvalidFormat
}
alpha, alphaStride, err = readAlpha(bytes.NewReader(buf), h.FrameWidth-1, h.FrameHeight-1, alphHeader.Compression)
unfilterAlpha(alpha, alphaStride, alphHeader.FilteringMethod)
return alpha, alphaStride, nil
}
func rewrap(r io.Reader, length int) (io.Reader, error) {
data := make([]byte, length)
n, err := io.ReadFull(r, data)
if err != nil {
return nil, err
}
if n != length {
return nil, errInvalidFormat
}
return bytes.NewReader(data), nil
}
+302
View File
@@ -0,0 +1,302 @@
// 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
import (
"bytes"
"errors"
"image"
"image/color"
"io"
"golang.org/x/image/riff"
"golang.org/x/image/vp8"
"golang.org/x/image/vp8l"
)
var errInvalidFormat = errors.New("webp: invalid format")
var (
fccANIM = riff.FourCC{'A', 'N', 'I', 'M'}
fccANMF = riff.FourCC{'A', 'N', 'M', 'F'}
fccALPH = riff.FourCC{'A', 'L', 'P', 'H'}
fccVP8 = riff.FourCC{'V', 'P', '8', ' '}
fccVP8L = riff.FourCC{'V', 'P', '8', 'L'}
fccVP8X = riff.FourCC{'V', 'P', '8', 'X'}
fccWEBP = riff.FourCC{'W', 'E', 'B', 'P'}
)
func webpRiffReader(r io.Reader) (*riff.Reader, error) {
formType, riffReader, err := riff.NewReader(r)
if err != nil {
return nil, err
}
if formType != fccWEBP {
return nil, errInvalidFormat
}
return riffReader, nil
}
func decode(r io.Reader, configOnly bool) (image.Image, image.Config, error) {
riffReader, err := webpRiffReader(r)
if err != nil {
return nil, image.Config{}, err
}
var (
alpha []byte
alphaStride int
wantAlpha bool
seenVP8X bool
widthMinusOne uint32
heightMinusOne uint32
buf [10]byte
)
for {
chunkID, chunkLen, chunkData, err := riffReader.Next()
if err == io.EOF {
err = errInvalidFormat
}
if err != nil {
return nil, image.Config{}, err
}
switch chunkID {
case fccALPH:
if !wantAlpha {
return nil, image.Config{}, errInvalidFormat
}
wantAlpha = false
// Read the Pre-processing | Filter | Compression byte.
if _, err := io.ReadFull(chunkData, buf[:1]); err != nil {
if err == io.EOF {
err = errInvalidFormat
}
return nil, image.Config{}, err
}
alpha, alphaStride, err = readAlpha(chunkData, widthMinusOne, heightMinusOne, buf[0]&0x03)
if err != nil {
return nil, image.Config{}, err
}
unfilterAlpha(alpha, alphaStride, (buf[0]>>2)&0x03)
case fccVP8:
if wantAlpha || int32(chunkLen) < 0 {
return nil, image.Config{}, errInvalidFormat
}
d := vp8.NewDecoder()
d.Init(chunkData, int(chunkLen))
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()
if err != nil {
return nil, image.Config{}, err
}
if alpha != nil {
return &image.NYCbCrA{
YCbCr: *m,
A: alpha,
AStride: alphaStride,
}, image.Config{}, nil
}
return m, image.Config{}, nil
case fccVP8L:
if wantAlpha || alpha != nil {
return nil, image.Config{}, errInvalidFormat
}
if configOnly {
c, err := vp8l.DecodeConfig(chunkData)
return nil, c, err
}
m, err := vp8l.Decode(chunkData)
return m, image.Config{}, err
case fccVP8X:
if seenVP8X {
return nil, image.Config{}, errInvalidFormat
}
seenVP8X = true
if chunkLen != 10 {
return nil, image.Config{}, errInvalidFormat
}
h := parseVP8XHeader(chunkData)
wantAlpha = h.Alpha
widthMinusOne = h.CanvasWidth - 1
heightMinusOne = h.CanvasHeight - 1
if configOnly {
if wantAlpha {
return nil, image.Config{
ColorModel: color.NYCbCrAModel,
Width: int(widthMinusOne) + 1,
Height: int(heightMinusOne) + 1,
}, nil
}
return nil, image.Config{
ColorModel: color.YCbCrModel,
Width: int(widthMinusOne) + 1,
Height: int(heightMinusOne) + 1,
}, nil
}
}
}
}
func readAlpha(chunkData io.Reader, widthMinusOne, heightMinusOne uint32, compression byte) (
alpha []byte, alphaStride int, err error) {
switch compression {
case 0:
w := int(widthMinusOne) + 1
h := int(heightMinusOne) + 1
alpha = make([]byte, w*h)
if _, err := io.ReadFull(chunkData, alpha); err != nil {
return nil, 0, err
}
return alpha, w, nil
case 1:
// Read the VP8L-compressed alpha values. First, synthesize a 5-byte VP8L header:
// a 1-byte magic number, a 14-bit widthMinusOne, a 14-bit heightMinusOne,
// a 1-bit (ignored, zero) alphaIsUsed and a 3-bit (zero) version.
// TODO(nigeltao): be more efficient than decoding an *image.NRGBA just to
// extract the green values to a separately allocated []byte. Fixing this
// will require changes to the vp8l package's API.
if widthMinusOne > 0x3fff || heightMinusOne > 0x3fff {
return nil, 0, errors.New("webp: invalid format")
}
alphaImage, err := vp8l.Decode(io.MultiReader(
bytes.NewReader([]byte{
0x2f, // VP8L magic number.
uint8(widthMinusOne),
uint8(widthMinusOne>>8) | uint8(heightMinusOne<<6),
uint8(heightMinusOne >> 2),
uint8(heightMinusOne >> 10),
}),
chunkData,
))
if err != nil {
return nil, 0, err
}
// The green values of the inner NRGBA image are the alpha values of the
// outer NYCbCrA image.
pix := alphaImage.(*image.NRGBA).Pix
alpha = make([]byte, len(pix)/4)
for i := range alpha {
alpha[i] = pix[4*i+1]
}
return alpha, int(widthMinusOne) + 1, nil
}
return nil, 0, errInvalidFormat
}
func unfilterAlpha(alpha []byte, alphaStride int, filter byte) {
if len(alpha) == 0 || alphaStride == 0 {
return
}
switch filter {
case 1: // Horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i += alphaStride {
// The first column is equivalent to the vertical filter.
alpha[i] += alpha[i-alphaStride]
for j := 1; j < alphaStride; j++ {
alpha[i+j] += alpha[i+j-1]
}
}
case 2: // Vertical filter.
// The first row is equivalent to the horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i++ {
alpha[i] += alpha[i-alphaStride]
}
case 3: // Gradient filter.
// The first row is equivalent to the horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i += alphaStride {
// The first column is equivalent to the vertical filter.
alpha[i] += alpha[i-alphaStride]
// The interior is predicted on the three top/left pixels.
for j := 1; j < alphaStride; j++ {
c := int(alpha[i+j-alphaStride-1])
b := int(alpha[i+j-alphaStride])
a := int(alpha[i+j-1])
x := a + b - c
if x < 0 {
x = 0
} else if x > 255 {
x = 255
}
alpha[i+j] += uint8(x)
}
}
}
}
// Decode reads a WEBP image from r and returns it as an image.Image.
func Decode(r io.Reader) (image.Image, error) {
m, _, err := decode(r, false)
if err != nil {
return nil, err
}
return m, nil
}
func DecodeAnimated(r io.Reader) (*AnimatedWEBP, error) {
return decodeAnimated(r)
}
// DecodeConfig returns the color model and dimensions of a WEBP image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (image.Config, error) {
_, c, err := decode(r, true)
return c, err
}
// DecodeVP8XHeader will return the decoded VP8XHeader if this file is in the Extended File Format
// as defined by the webp specification. The VP8X chunk must be the first chunk of the file.
// If the first chunk of the file is anything else, it is not in the extended format and this
// will return a nil VP8XHeader. An error is only returned if the chunk is found, but invalid
// or a generic io.Reader error occurs.
func DecodeVP8XHeader(r io.Reader) (*VP8XHeader, error) {
riffReader, err := webpRiffReader(r)
if err != nil {
return nil, err
}
fourCC, chunkLen, chunkData, err := riffReader.Next()
if err != nil {
return nil, err
}
if fourCC != fccVP8X {
return nil, nil
}
if chunkLen != 10 {
return nil, errInvalidFormat
}
h := parseVP8XHeader(chunkData)
return &h, nil
}
+277
View File
@@ -0,0 +1,277 @@
// 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
import (
"bytes"
"errors"
"image"
"image/color"
"io"
"golang.org/x/image/riff"
"golang.org/x/image/vp8"
"golang.org/x/image/vp8l"
)
var errInvalidFormat = errors.New("webp: invalid format")
var (
fccALPH = riff.FourCC{'A', 'L', 'P', 'H'}
fccVP8 = riff.FourCC{'V', 'P', '8', ' '}
fccVP8L = riff.FourCC{'V', 'P', '8', 'L'}
fccVP8X = riff.FourCC{'V', 'P', '8', 'X'}
fccWEBP = riff.FourCC{'W', 'E', 'B', 'P'}
)
func decode(r io.Reader, configOnly bool) (image.Image, image.Config, error) {
formType, riffReader, err := riff.NewReader(r)
if err != nil {
return nil, image.Config{}, err
}
if formType != fccWEBP {
return nil, image.Config{}, errInvalidFormat
}
var (
alpha []byte
alphaStride int
wantAlpha bool
seenVP8X bool
widthMinusOne uint32
heightMinusOne uint32
buf [10]byte
)
for {
chunkID, chunkLen, chunkData, err := riffReader.Next()
if err == io.EOF {
err = errInvalidFormat
}
if err != nil {
return nil, image.Config{}, err
}
switch chunkID {
case fccALPH:
if !wantAlpha {
return nil, image.Config{}, errInvalidFormat
}
wantAlpha = false
// Read the Pre-processing | Filter | Compression byte.
if _, err := io.ReadFull(chunkData, buf[:1]); err != nil {
if err == io.EOF {
err = errInvalidFormat
}
return nil, image.Config{}, err
}
alpha, alphaStride, err = readAlpha(chunkData, widthMinusOne, heightMinusOne, buf[0]&0x03)
if err != nil {
return nil, image.Config{}, err
}
unfilterAlpha(alpha, alphaStride, (buf[0]>>2)&0x03)
case fccVP8:
if wantAlpha || int32(chunkLen) < 0 {
return nil, image.Config{}, errInvalidFormat
}
d := vp8.NewDecoder()
d.Init(chunkData, int(chunkLen))
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()
if err != nil {
return nil, image.Config{}, err
}
if alpha != nil {
return &image.NYCbCrA{
YCbCr: *m,
A: alpha,
AStride: alphaStride,
}, image.Config{}, nil
}
return m, image.Config{}, nil
case fccVP8L:
if wantAlpha || alpha != nil {
return nil, image.Config{}, errInvalidFormat
}
if configOnly {
c, err := vp8l.DecodeConfig(chunkData)
return nil, c, err
}
m, err := vp8l.Decode(chunkData)
return m, image.Config{}, err
case fccVP8X:
if seenVP8X {
return nil, image.Config{}, errInvalidFormat
}
seenVP8X = true
if chunkLen != 10 {
return nil, image.Config{}, errInvalidFormat
}
if _, err := io.ReadFull(chunkData, buf[:10]); err != nil {
return nil, image.Config{}, err
}
const (
animationBit = 1 << 1
xmpMetadataBit = 1 << 2
exifMetadataBit = 1 << 3
alphaBit = 1 << 4
iccProfileBit = 1 << 5
)
wantAlpha = (buf[0] & alphaBit) != 0
widthMinusOne = uint32(buf[4]) | uint32(buf[5])<<8 | uint32(buf[6])<<16
heightMinusOne = uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16
if configOnly {
if wantAlpha {
return nil, image.Config{
ColorModel: color.NYCbCrAModel,
Width: int(widthMinusOne) + 1,
Height: int(heightMinusOne) + 1,
}, nil
}
return nil, image.Config{
ColorModel: color.YCbCrModel,
Width: int(widthMinusOne) + 1,
Height: int(heightMinusOne) + 1,
}, nil
}
}
}
}
func readAlpha(chunkData io.Reader, widthMinusOne, heightMinusOne uint32, compression byte) (
alpha []byte, alphaStride int, err error) {
switch compression {
case 0:
w := int(widthMinusOne) + 1
h := int(heightMinusOne) + 1
alpha = make([]byte, w*h)
if _, err := io.ReadFull(chunkData, alpha); err != nil {
return nil, 0, err
}
return alpha, w, nil
case 1:
// Read the VP8L-compressed alpha values. First, synthesize a 5-byte VP8L header:
// a 1-byte magic number, a 14-bit widthMinusOne, a 14-bit heightMinusOne,
// a 1-bit (ignored, zero) alphaIsUsed and a 3-bit (zero) version.
// TODO(nigeltao): be more efficient than decoding an *image.NRGBA just to
// extract the green values to a separately allocated []byte. Fixing this
// will require changes to the vp8l package's API.
if widthMinusOne > 0x3fff || heightMinusOne > 0x3fff {
return nil, 0, errors.New("webp: invalid format")
}
alphaImage, err := vp8l.Decode(io.MultiReader(
bytes.NewReader([]byte{
0x2f, // VP8L magic number.
uint8(widthMinusOne),
uint8(widthMinusOne>>8) | uint8(heightMinusOne<<6),
uint8(heightMinusOne >> 2),
uint8(heightMinusOne >> 10),
}),
chunkData,
))
if err != nil {
return nil, 0, err
}
// The green values of the inner NRGBA image are the alpha values of the
// outer NYCbCrA image.
pix := alphaImage.(*image.NRGBA).Pix
alpha = make([]byte, len(pix)/4)
for i := range alpha {
alpha[i] = pix[4*i+1]
}
return alpha, int(widthMinusOne) + 1, nil
}
return nil, 0, errInvalidFormat
}
func unfilterAlpha(alpha []byte, alphaStride int, filter byte) {
if len(alpha) == 0 || alphaStride == 0 {
return
}
switch filter {
case 1: // Horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i += alphaStride {
// The first column is equivalent to the vertical filter.
alpha[i] += alpha[i-alphaStride]
for j := 1; j < alphaStride; j++ {
alpha[i+j] += alpha[i+j-1]
}
}
case 2: // Vertical filter.
// The first row is equivalent to the horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i++ {
alpha[i] += alpha[i-alphaStride]
}
case 3: // Gradient filter.
// The first row is equivalent to the horizontal filter.
for i := 1; i < alphaStride; i++ {
alpha[i] += alpha[i-1]
}
for i := alphaStride; i < len(alpha); i += alphaStride {
// The first column is equivalent to the vertical filter.
alpha[i] += alpha[i-alphaStride]
// The interior is predicted on the three top/left pixels.
for j := 1; j < alphaStride; j++ {
c := int(alpha[i+j-alphaStride-1])
b := int(alpha[i+j-alphaStride])
a := int(alpha[i+j-1])
x := a + b - c
if x < 0 {
x = 0
} else if x > 255 {
x = 255
}
alpha[i+j] += uint8(x)
}
}
}
}
// Decode reads a WEBP image from r and returns it as an image.Image.
func Decode(r io.Reader) (image.Image, error) {
m, _, err := decode(r, false)
if err != nil {
return nil, err
}
return m, nil
}
// DecodeConfig returns the color model and dimensions of a WEBP image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (image.Config, error) {
_, c, err := decode(r, true)
return c, err
}
func init() {
image.RegisterFormat("webp", "RIFF????WEBPVP8", Decode, DecodeConfig)
}
+73
View File
@@ -0,0 +1,73 @@
package webp
import (
"bytes"
"errors"
"io"
"golang.org/x/image/riff"
)
var (
errInvalidHeader = errors.New("could not read an 8 byte header, sub-chunk is not valid")
)
// SubChunkReader helps in reading riff data from an existing chunk which are comprised of sub-chunks.
// A good example would be ANMF chunks of animated webp files. These chunks can contain headers, ALPH chunks
// and VP8 or VP8L chunks within the main riff data chunk.
type SubChunkReader struct {
r io.Reader
}
// Next will return the FourCC, data, and data length of a subchunk.
// The io.Reader returned for data will not be the same as the provided reader
// and is safe to discord without fully reading the contents.
// Will return an error if the format is invalid or an underlying read operation fails.
func (c SubChunkReader) Next() (riff.FourCC, io.Reader, uint32, error) {
header := make([]byte, 8)
n, err := io.ReadFull(c.r, header)
if err != nil {
if err == io.ErrUnexpectedEOF {
return riff.FourCC{}, nil, 0, errInvalidHeader
}
return riff.FourCC{}, nil, 0, err
}
if n != 8 {
return riff.FourCC{}, nil, 0, errInvalidHeader
}
fourCC := riff.FourCC{header[0], header[1], header[2], header[3]}
chunkLen := u32(header[4:8])
buf := make([]byte, chunkLen)
n, err = io.ReadFull(c.r, buf)
if err != nil {
if err == io.ErrUnexpectedEOF {
return riff.FourCC{}, nil, 0, errInvalidFormat
}
return riff.FourCC{}, nil, 0, err
}
if n != int(chunkLen) {
return riff.FourCC{}, nil, 0, errInvalidFormat
}
// if chunkLen was odd, we need to maintain a 2-byte boundary per RIFF spec.
// in this case read off a single byte of padding to re-align with the next
// fourCC header
if chunkLen%2 == 1 {
n, err := c.r.Read([]byte{0})
if err != nil {
return riff.FourCC{}, nil, 0, err
}
if n != 1 {
return riff.FourCC{}, nil, 0, errInvalidFormat
}
}
return fourCC, bytes.NewReader(buf), chunkLen, nil
}
func NewSubChunkReader(r io.Reader) *SubChunkReader {
return &SubChunkReader{
r: r,
}
}
+138
View File
@@ -0,0 +1,138 @@
package webp
import (
"image"
"image/color"
"io"
)
// AnimatedWEBP is the struct of a AnimatedWEBP container and the image data contained within.
type AnimatedWEBP struct {
Frames []Frame
Header ANIMHeader
Config image.Config
}
type Frame struct {
Header ANMFHeader
Frame image.Image
}
type VP8XHeader struct {
ICCProfile bool
Alpha bool
ExifMetadata bool
XmpMetadata bool
Animation bool
CanvasWidth uint32
CanvasHeight uint32
}
type ALPHHeader struct {
Preprocessing uint8
FilteringMethod uint8
Compression uint8
}
type ANIMHeader struct {
BackgroundColor color.Color
LoopCount uint16
}
type ANMFHeader struct {
FrameX uint32
FrameY uint32
FrameWidth uint32
FrameHeight uint32
FrameDuration uint32
AlphaBlend bool
DisposalBitSet bool
}
func parseALPHHeader(r io.Reader) ALPHHeader {
h := make([]byte, 1)
_, _ = io.ReadFull(r, h)
const (
twoBits = byte(3)
)
return ALPHHeader{
Preprocessing: h[0] >> 4 & twoBits,
FilteringMethod: h[0] >> 2 & twoBits,
Compression: h[0] & twoBits,
}
}
func parseANIMHeader(r io.Reader) ANIMHeader {
h := make([]byte, 6)
_, _ = io.ReadFull(r, h)
loopCount := uint16(h[4]) | uint16(h[5])<<8
bg := color.RGBA{
R: h[2],
G: h[1],
B: h[0],
A: h[3],
}
return ANIMHeader{
BackgroundColor: bg,
LoopCount: loopCount,
}
}
func parseANMFHeader(r io.Reader) ANMFHeader {
h := make([]byte, 16)
_, _ = io.ReadFull(r, h)
const (
disposeBit = 1
blendBit = 1 << 1
)
return ANMFHeader{
FrameX: u24(h[0:3]),
FrameY: u24(h[3:6]),
FrameWidth: u24(h[6:9]) + 1,
FrameHeight: u24(h[9:12]) + 1,
FrameDuration: u24(h[12:15]),
AlphaBlend: (h[15] & blendBit) == 0,
DisposalBitSet: (h[15] & disposeBit) != 0,
}
}
func parseVP8XHeader(r io.Reader) VP8XHeader {
const (
anim = 1 << 1
xmp = 1 << 2
exif = 1 << 3
alpha = 1 << 4
icc = 1 << 5
)
h := make([]byte, 10)
_, _ = io.ReadFull(r, h)
widthMinusOne := u24(h[4:])
heightMinusOne := u24(h[7:])
header := VP8XHeader{
ICCProfile: h[0]&icc != 0,
Alpha: h[0]&alpha != 0,
ExifMetadata: h[0]&exif != 0,
XmpMetadata: h[0]&xmp != 0,
Animation: h[0]&anim != 0,
CanvasWidth: widthMinusOne + 1,
CanvasHeight: heightMinusOne + 1,
}
return header
}
func u24(b []byte) uint32 {
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16
}
func u32(b []byte) uint32 {
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}