File: ico.go

package info (click to toggle)
golang-github-akavel-rsrc 1%3A0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: xml: 23; makefile: 4
file content (214 lines) | stat: -rw-r--r-- 5,089 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Package ico describes Windows ICO file format.
package ico

// ICO: http://msdn.microsoft.com/en-us/library/ms997538.aspx
// BMP/DIB: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183562%28v=vs.85%29.aspx

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"image"
	"image/color"
	"io"
	"io/ioutil"
	"sort"
)

const (
	BI_RGB = 0
)

type ICONDIR struct {
	Reserved uint16 // must be 0
	Type     uint16 // Resource Type (1 for icons)
	Count    uint16 // How many images?
}

type IconDirEntryCommon struct {
	Width      byte   // Width, in pixels, of the image
	Height     byte   // Height, in pixels, of the image
	ColorCount byte   // Number of colors in image (0 if >=8bpp)
	Reserved   byte   // Reserved (must be 0)
	Planes     uint16 // Color Planes
	BitCount   uint16 // Bits per pixel
	BytesInRes uint32 // How many bytes in this resource?
}

type ICONDIRENTRY struct {
	IconDirEntryCommon
	ImageOffset uint32 // Where in the file is this image? [from beginning of file]
}

type BITMAPINFOHEADER struct {
	Size          uint32
	Width         int32
	Height        int32  // NOTE: "represents the combined height of the XOR and AND masks. Remember to divide this number by two before using it to perform calculations for either of the XOR or AND masks."
	Planes        uint16 // [BMP/DIB]: "is always 1"
	BitCount      uint16
	Compression   uint32 // for ico = 0
	SizeImage     uint32
	XPelsPerMeter int32  // for ico = 0
	YPelsPerMeter int32  // for ico = 0
	ClrUsed       uint32 // for ico = 0
	ClrImportant  uint32 // for ico = 0
}

type RGBQUAD struct {
	Blue     byte
	Green    byte
	Red      byte
	Reserved byte // must be 0
}

func skip(r io.Reader, n int64) error {
	_, err := io.CopyN(ioutil.Discard, r, n)
	return err
}

type icoOffset struct {
	n      int
	offset uint32
}

type rawico struct {
	icoinfo ICONDIRENTRY
	bmpinfo *BITMAPINFOHEADER
	idx     int
	data    []byte
}

type byOffsets []rawico

func (o byOffsets) Len() int           { return len(o) }
func (o byOffsets) Less(i, j int) bool { return o[i].icoinfo.ImageOffset < o[j].icoinfo.ImageOffset }
func (o byOffsets) Swap(i, j int) {
	tmp := o[i]
	o[i] = o[j]
	o[j] = tmp
}

type ICO struct {
	image.Image
}

func DecodeHeaders(r io.Reader) ([]ICONDIRENTRY, error) {
	var hdr ICONDIR
	err := binary.Read(r, binary.LittleEndian, &hdr)
	if err != nil {
		return nil, err
	}
	if hdr.Reserved != 0 || hdr.Type != 1 {
		return nil, fmt.Errorf("bad magic number")
	}

	entries := make([]ICONDIRENTRY, hdr.Count)
	for i := 0; i < len(entries); i++ {
		err = binary.Read(r, binary.LittleEndian, &entries[i])
		if err != nil {
			return nil, err
		}
	}
	return entries, nil
}

// NOTE: won't succeed on files with overlapping offsets
func unused_decodeAll(r io.Reader) ([]*ICO, error) {
	var hdr ICONDIR
	err := binary.Read(r, binary.LittleEndian, &hdr)
	if err != nil {
		return nil, err
	}
	if hdr.Reserved != 0 || hdr.Type != 1 {
		return nil, fmt.Errorf("bad magic number")
	}

	raws := make([]rawico, hdr.Count)
	for i := 0; i < len(raws); i++ {
		err = binary.Read(r, binary.LittleEndian, &raws[i].icoinfo)
		if err != nil {
			return nil, err
		}
		raws[i].idx = i
	}

	sort.Sort(byOffsets(raws))

	offset := uint32(binary.Size(&hdr) + len(raws)*binary.Size(ICONDIRENTRY{}))
	for i := 0; i < len(raws); i++ {
		err = skip(r, int64(raws[i].icoinfo.ImageOffset-offset))
		if err != nil {
			return nil, err
		}
		offset = raws[i].icoinfo.ImageOffset

		raws[i].bmpinfo = &BITMAPINFOHEADER{}
		err = binary.Read(r, binary.LittleEndian, raws[i].bmpinfo)
		if err != nil {
			return nil, err
		}

		err = skip(r, int64(raws[i].bmpinfo.Size-uint32(binary.Size(BITMAPINFOHEADER{}))))
		if err != nil {
			return nil, err
		}
		raws[i].data = make([]byte, raws[i].icoinfo.BytesInRes-raws[i].bmpinfo.Size)
		_, err = io.ReadFull(r, raws[i].data)
		if err != nil {
			return nil, err
		}
	}

	icos := make([]*ICO, len(raws))
	for i := 0; i < len(raws); i++ {
		fmt.Println(i)
		icos[raws[i].idx], err = decode(raws[i].bmpinfo, &raws[i].icoinfo, raws[i].data)
		if err != nil {
			return nil, err
		}
	}
	return icos, nil
}

func decode(info *BITMAPINFOHEADER, icoinfo *ICONDIRENTRY, data []byte) (*ICO, error) {
	if info.Compression != BI_RGB {
		return nil, fmt.Errorf("ICO compression not supported (got %d)", info.Compression)
	}

	//if info.ClrUsed!=0 {
	//	panic(info.ClrUsed)
	//}

	r := bytes.NewBuffer(data)

	bottomup := info.Height > 0
	if !bottomup {
		info.Height = -info.Height
	}

	switch info.BitCount {
	case 8:
		ncol := int(icoinfo.ColorCount)
		if ncol == 0 {
			ncol = 256
		}

		pal := make(color.Palette, ncol)
		for i := 0; i < ncol; i++ {
			var rgb RGBQUAD
			err := binary.Read(r, binary.LittleEndian, &rgb)
			if err != nil {
				return nil, err
			}
			pal[i] = color.NRGBA{R: rgb.Red, G: rgb.Green, B: rgb.Blue, A: 0xff} //FIXME: is Alpha ok 0xff?
		}
		fmt.Println(pal)

		fmt.Println(info.SizeImage, len(data)-binary.Size(RGBQUAD{})*len(pal), info.Width, info.Height)

	default:
		return nil, fmt.Errorf("unsupported ICO bit depth (BitCount) %d", info.BitCount)
	}

	return nil, nil
}