-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas.go
224 lines (199 loc) · 4.58 KB
/
canvas.go
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
215
216
217
218
219
220
221
222
223
224
package wzexplorer
import (
"bytes"
"compress/zlib"
"errors"
"github.com/anonymous5l/wzexplorer/wzimage"
"image"
"io"
)
type CanvasFormat int
const (
CanvasFormatBGRA4444 CanvasFormat = 1
CanvasFormatBGRA8888 CanvasFormat = 2
CanvasFormatGray CanvasFormat = 3 // 0x2 + 1
CanvasFormatARGB1555 CanvasFormat = 257 // 0x100 + 1
CanvasFormatRGB565 CanvasFormat = 513 // 0x200
CanvasFormatRGB565Thumb CanvasFormat = 517 // 0x200 + 5
CanvasFormatDXT3 CanvasFormat = 1026 // 0x400 + 2
CanvasFormatDXT5 CanvasFormat = 2050 // 0x800 + 2
)
func (f CanvasFormat) String() string {
switch f {
case CanvasFormatBGRA4444:
return "BGRA4444"
case CanvasFormatBGRA8888:
return "BGRA8888"
case CanvasFormatGray:
return "Gray"
case CanvasFormatARGB1555:
return "ARGB1555"
case CanvasFormatRGB565:
return "RGB565"
case CanvasFormatRGB565Thumb:
return "RGB565Thumb"
case CanvasFormatDXT3:
return "DXT3"
case CanvasFormatDXT5:
return "DXT5"
}
return "Unknown"
}
type Canvas interface {
GetObject
Size() image.Point
Image() (image.Image, error)
Format() CanvasFormat
}
type canvas struct {
*object
img image.Image
format CanvasFormat
magLevel byte
width, height int32
}
func (c *canvas) Size() image.Point {
return image.Pt(int(c.width), int(c.height))
}
func (c *canvas) Format() CanvasFormat {
return c.format
}
func (c *canvas) build(deflated []byte) (err error) {
switch c.format {
case CanvasFormatBGRA4444:
c.img, err = wzimage.NewBGRA4444(c.Size(), deflated)
case CanvasFormatBGRA8888:
c.img, err = wzimage.NewBGRA8888(c.Size(), deflated)
case CanvasFormatARGB1555:
c.img, err = wzimage.NewARGB1555(c.Size(), deflated)
case CanvasFormatRGB565:
c.img, err = wzimage.NewRGB565(c.Size(), deflated)
case CanvasFormatRGB565Thumb:
c.img, err = wzimage.NewRGB565Thumb(c.Size(), deflated)
case CanvasFormatDXT3, CanvasFormatGray:
c.img, err = wzimage.NewDXT3(c.Size(), deflated)
case CanvasFormatDXT5:
c.img, err = wzimage.NewDXT5(c.Size(), deflated)
}
return
}
func (c *canvas) Image() (bitmap image.Image, err error) {
if c.img != nil {
return c.img, nil
}
b := c.f.b
offset := c.offset + 1
size := c.size - 1
if _, err = b.Seek(offset, io.SeekStart); err != nil {
return
}
var n int
data := make([]byte, size, size)
if n, err = b.Read(data); err != nil {
return
} else if n != int(size) {
err = io.EOF
return
}
var deflated []byte
header := b.o.Uint16(data)
if header != 0x9c78 && header != 0xda78 && header != 0x0178 && header != 0x5e78 {
var shrinkData []byte
for len(data) > 0 {
blockSize := int(b.o.Uint32(data))
transform := data[4 : 4+blockSize]
b.provider.crypt.Transform(transform)
shrinkData = append(shrinkData, transform...)
data = data[4+blockSize:]
}
deflated, err = c.deflate(shrinkData)
if err != nil {
return
}
} else {
deflated, err = c.deflate(data)
if err != nil {
return
}
}
if err = c.build(deflated); err != nil {
return
}
bitmap = c.img
return
}
func (c *canvas) deflate(data []byte) (deflated []byte, err error) {
var stream io.ReadCloser
if stream, err = zlib.NewReader(bytes.NewReader(data)); err != nil {
return
}
defer stream.Close()
buffer := bytes.NewBuffer([]byte{})
swap := make([]byte, 1024)
var n int
for {
if n, err = stream.Read(swap); err != nil {
if err != io.ErrUnexpectedEOF {
return
}
err = nil
break
}
buffer.Write(swap[:n])
}
deflated = buffer.Bytes()
return
}
func (c *canvas) parse(f *file, offset int64) error {
b := f.b
if _, err := b.Seek(1, io.SeekCurrent); err != nil {
return err
}
hasProperty, err := b.ReadByte()
if err != nil {
return err
}
c.object = newObject(f, offset)
c.object.t = ObjectTypeVariantNil
if hasProperty > 0 {
c.object.t = ObjectTypeProperties
if err = c.object.parse(); err != nil {
return err
}
}
if c.width, err = b.ReadCompressInt32(); err != nil {
return err
}
if c.height, err = b.ReadCompressInt32(); err != nil {
return err
}
var (
format int32
format2 byte
)
if format, err = b.ReadCompressInt32(); err != nil {
return err
}
if format2, err = b.ReadByte(); err != nil {
return err
}
format += int32(format2)
c.format = CanvasFormat(format)
var test int32
if test, err = b.ReadInt32(); err != nil {
return err
}
if test != 0 {
return errors.New("invalid image struct")
}
if c.size, err = b.ReadInt32(); err != nil {
return err
}
if c.offset, err = b.Seek(0, io.SeekCurrent); err != nil {
return err
}
if _, err = b.Seek(int64(c.size), io.SeekCurrent); err != nil {
return err
}
return nil
}