1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/imagemeta/png.go

40 lines
741 B
Go
Raw Normal View History

2019-12-25 11:06:15 +02:00
package imagemeta
import (
"bytes"
"encoding/binary"
"io"
)
var pngMagick = []byte("\x89PNG\r\n\x1a\n")
type PngFormatError string
func (e PngFormatError) Error() string { return "invalid PNG format: " + string(e) }
2019-12-25 11:06:15 +02:00
func DecodePngMeta(r io.Reader) (Meta, error) {
var tmp [16]byte
if _, err := io.ReadFull(r, tmp[:8]); err != nil {
return nil, err
}
if !bytes.Equal(pngMagick, tmp[:8]) {
return nil, PngFormatError("not a PNG image")
}
if _, err := io.ReadFull(r, tmp[:]); err != nil {
return nil, err
}
2019-12-25 11:06:15 +02:00
return &meta{
format: "png",
width: int(binary.BigEndian.Uint32(tmp[8:12])),
height: int(binary.BigEndian.Uint32(tmp[12:16])),
}, nil
}
func init() {
RegisterFormat(string(pngMagick), DecodePngMeta)
}