1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00

65 lines
1.1 KiB
Go
Raw Normal View History

2019-10-10 19:20:25 +06:00
package imagesize
import (
"encoding/binary"
"io"
)
2019-10-03 21:43:33 +06:00
func icoBestSize(r io.Reader) (width, height byte, offset uint32, size uint32, 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-10-03 21:43:33 +06:00
size = binary.LittleEndian.Uint32(tmp[8:12])
offset = binary.LittleEndian.Uint32(tmp[12:16])
}
}
2019-09-30 16:03:07 +06:00
return
}
2019-10-03 21:43:33 +06:00
func BestIcoPage(r io.Reader) (int, int, error) {
_, _, offset, size, err := icoBestSize(r)
return int(offset), int(size), err
2019-09-30 16:03:07 +06:00
}
func DecodeIcoMeta(r io.Reader) (*Meta, error) {
2019-10-03 21:43:33 +06:00
bwidth, bheight, _, _, err := icoBestSize(r)
2019-09-30 16:03:07 +06:00
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)
}