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

49 lines
925 B
Go
Raw Normal View History

2019-12-25 11:06:15 +02:00
package imagemeta
2019-09-30 13:28:38 +02:00
import (
"bytes"
"encoding/binary"
"io"
)
var bmpMagick = []byte("BM")
type BmpFormatError string
func (e BmpFormatError) Error() string { return "invalid BMP format: " + string(e) }
2019-12-25 11:06:15 +02:00
func DecodeBmpMeta(r io.Reader) (Meta, error) {
2019-09-30 13:28:38 +02:00
var tmp [26]byte
if _, err := io.ReadFull(r, tmp[:]); err != nil {
return nil, err
}
if !bytes.Equal(tmp[:2], bmpMagick) {
return nil, BmpFormatError("malformed header")
}
infoSize := binary.LittleEndian.Uint32(tmp[14:18])
var width, height int
if infoSize >= 40 {
width = int(binary.LittleEndian.Uint32(tmp[18:22]))
height = int(binary.LittleEndian.Uint32(tmp[22:26]))
} else {
// CORE
width = int(binary.LittleEndian.Uint16(tmp[18:20]))
height = int(binary.LittleEndian.Uint16(tmp[20:22]))
}
2019-12-25 11:06:15 +02:00
return &meta{
format: "bmp",
width: width,
height: height,
2019-09-30 13:28:38 +02:00
}, nil
}
func init() {
RegisterFormat(string(bmpMagick), DecodeBmpMeta)
}