1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-07 23:32:55 +02:00
Files
imgproxy/image_size/ico.go

64 lines
1008 B
Go
Raw Normal View History

package imageSize
import (
"encoding/binary"
"io"
)
2019-09-30 16:03:07 +06:00
func icoBestSize(r io.Reader) (width, height byte, page uint16, err error) {
var tmp [16]byte
2019-09-30 16:03:07 +06:00
if _, err = io.ReadFull(r, tmp[:6]); err != nil {
return
}
count := binary.LittleEndian.Uint16(tmp[4:6])
for i := uint16(0); i < count; i++ {
2019-09-30 16:03:07 +06:00
if _, err = io.ReadFull(r, tmp[:]); err != nil {
return
}
2019-09-30 16:03:07 +06:00
if tmp[0] > width || tmp[1] > height || tmp[0] == 0 || tmp[1] == 0 {
width = tmp[0]
height = tmp[1]
2019-09-30 16:03:07 +06:00
page = i
}
}
2019-09-30 16:03:07 +06:00
return
}
func BestIcoPage(r io.Reader) (int, error) {
_, _, page, err := icoBestSize(r)
return int(page), err
}
func DecodeIcoMeta(r io.Reader) (*Meta, error) {
bwidth, bheight, _, err := icoBestSize(r)
if err != nil {
return nil, err
}
width := int(bwidth)
height := int(bheight)
if width == 0 {
width = 256
}
if height == 0 {
height = 256
}
return &Meta{
Format: "ico",
2019-09-30 16:03:07 +06:00
Width: width,
Height: height,
}, nil
}
func init() {
RegisterFormat("\x00\x00\x01\x00", DecodeIcoMeta)
}