-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
431 lines (382 loc) · 8.89 KB
/
type.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package exr
import (
"bufio"
"bytes"
"fmt"
"log"
"math"
)
// These types are defined by IlmImf.
// Please see following document.
//
// OpenEXRFileLayout.pdf - Predefined Attribute Types
//
type box2i struct {
xMin int32
yMin int32
xMax int32
yMax int32
}
func box2iFromBytes(b []byte) box2i {
if len(b) != 16 {
log.Fatal("box2iFromBytes: need bytes of length 16")
}
return box2i{
xMin: int32(parse.Uint32(b[0:4])),
yMin: int32(parse.Uint32(b[4:8])),
xMax: int32(parse.Uint32(b[8:12])),
yMax: int32(parse.Uint32(b[12:16])),
}
}
type box2f struct {
xMin float32
yMin float32
xMax float32
yMax float32
}
func box2fFromBytes(b []byte) box2f {
if len(b) != 16 {
log.Fatal("box2fFromBytes: need bytes of length 16")
}
return box2f{
xMin: math.Float32frombits(parse.Uint32(b[0:4])),
yMin: math.Float32frombits(parse.Uint32(b[4:8])),
xMax: math.Float32frombits(parse.Uint32(b[8:12])),
yMax: math.Float32frombits(parse.Uint32(b[12:16])),
}
}
type pixelType int32
const (
UINT = pixelType(iota)
HALF
FLOAT
)
func (p pixelType) String() string {
switch p {
case UINT:
return "UINT"
case HALF:
return "HALF"
case FLOAT:
return "FLOAT"
default:
return "UNKNOWN_PIXEL_TYPE"
}
}
// pixelSize returns pixelSize in bytes
func pixelSize(p pixelType) int {
switch p {
case UINT:
return 4
case HALF:
return 2
case FLOAT:
return 4
default:
return 0
}
}
type channel struct {
name string
pixelType pixelType
pLinear uint8
xSampling int32
ySampling int32
}
func (ch channel) String() string {
return fmt.Sprintf("{%s %s %d %d %d}", ch.name, ch.pixelType, ch.pLinear, ch.xSampling, ch.ySampling)
}
type chlist []channel
func chlistFromBytes(b []byte) chlist {
chans := make(chlist, 0)
buf := bufio.NewReader(bytes.NewBuffer(b))
for {
nameByte, err := buf.ReadBytes(0x00)
if err != nil {
log.Fatal(err)
}
name := string(nameByte[:len(nameByte)-1])
channelBytes, err := read(buf, 16)
if err != nil {
log.Fatal(err)
}
pixelType := pixelType(parse.Uint32(channelBytes[:4]))
pLinear := uint8(channelBytes[4])
// channelBytes[5:8] are place holders.
xSampling := int32(parse.Uint32(channelBytes[8:12]))
ySampling := int32(parse.Uint32(channelBytes[12:]))
ch := channel{
name: name,
pixelType: pixelType,
pLinear: pLinear,
xSampling: xSampling,
ySampling: ySampling,
}
chans = append(chans, ch)
if buf.Buffered() == 1 {
nullByte, err := buf.Peek(1)
if err != nil {
log.Fatal(err)
}
if nullByte[0] != 0x00 {
log.Fatal(FormatError("channels are must seperated by a null byte"))
}
break
}
}
return chans
}
type chromaticities struct {
redX float32
redY float32
greenX float32
greenY float32
blueX float32
blueY float32
whiteX float32
whiteY float32
}
func chromaticitiesFromBytes(b []byte) chromaticities {
if len(b) != 32 {
log.Fatal("chromaticitiesFromBytes: need bytes of length 32")
}
return chromaticities{
redX: math.Float32frombits(parse.Uint32(b[0:4])),
redY: math.Float32frombits(parse.Uint32(b[4:8])),
greenX: math.Float32frombits(parse.Uint32(b[8:12])),
greenY: math.Float32frombits(parse.Uint32(b[12:16])),
blueX: math.Float32frombits(parse.Uint32(b[16:20])),
blueY: math.Float32frombits(parse.Uint32(b[20:24])),
whiteX: math.Float32frombits(parse.Uint32(b[24:28])),
whiteY: math.Float32frombits(parse.Uint32(b[28:32])),
}
}
type compression uint8
const (
NO_COMPRESSION = compression(iota)
RLE_COMPRESSION
ZIPS_COMPRESSION
ZIP_COMPRESSION
PIZ_COMPRESSION
PXR24_COMPRESSION
B44_COMPRESSION
B44A_COMPRESSION
)
func (t compression) String() string {
switch t {
case NO_COMPRESSION:
return "NO_COMPRESSION"
case RLE_COMPRESSION:
return "RLE_COMPRESSION"
case ZIPS_COMPRESSION:
return "ZIPS_COMPRESSION"
case ZIP_COMPRESSION:
return "ZIP_COMPRESSION"
case PIZ_COMPRESSION:
return "PIZ_COMPRESSION"
case PXR24_COMPRESSION:
return "PXR24_COMPRESSION"
case B44_COMPRESSION:
return "B44_COMPRESSION"
case B44A_COMPRESSION:
return "B44A_COMPRESSION"
default:
return "UNKNOWN_COMPRESSION"
}
}
func compressionFromBytes(b []byte) compression {
if len(b) != 1 {
log.Fatal("compressionFromBytes: need bytes of length 1")
}
return compression(b[0])
}
type envmap uint8
func envmapFromBytes(b []byte) envmap {
if len(b) != 1 {
log.Fatal("envmapFromBytes: need bytes of length 1")
}
return envmap(b[0])
}
type keycode struct {
filmMfcCode int32
filmType int32
prefix int32
count int32
perfOffset int32
perfsPerFrame int32
perfsPerCount int32
}
func keycodeFromBytes(b []byte) keycode {
if len(b) != 28 {
log.Fatal("keycodeFromBytes: need bytes of length 28")
}
return keycode{
filmMfcCode: int32(parse.Uint32(b[:4])),
filmType: int32(parse.Uint32(b[4:8])),
prefix: int32(parse.Uint32(b[8:12])),
count: int32(parse.Uint32(b[12:16])),
perfOffset: int32(parse.Uint32(b[16:20])),
perfsPerFrame: int32(parse.Uint32(b[20:24])),
perfsPerCount: int32(parse.Uint32(b[24:28])),
}
}
type lineOrder uint8
const (
INCREASING_Y = lineOrder(iota)
DECREASING_Y
RANDOM_Y
)
func (l lineOrder) String() string {
switch l {
case INCREASING_Y:
return "INCREASING_Y"
case DECREASING_Y:
return "DECREASING_Y"
case RANDOM_Y:
return "RANDOM_Y"
default:
return "UNKNOWN_LINE_ORDER"
}
}
func lineOrderFromBytes(b []byte) lineOrder {
if len(b) != 1 {
log.Fatal("lineOrderFromBytes: need bytes of length 1")
}
return lineOrder(b[0])
}
type m33f [9]float32
func m33fFromBytes(b []byte) m33f {
if len(b) != 36 {
log.Fatal("m33fFromBytes: need bytes of length 36")
}
return [9]float32{
math.Float32frombits(parse.Uint32(b[:4])),
math.Float32frombits(parse.Uint32(b[4:8])),
math.Float32frombits(parse.Uint32(b[8:12])),
math.Float32frombits(parse.Uint32(b[12:16])),
math.Float32frombits(parse.Uint32(b[16:20])),
math.Float32frombits(parse.Uint32(b[20:24])),
math.Float32frombits(parse.Uint32(b[24:28])),
math.Float32frombits(parse.Uint32(b[28:32])),
math.Float32frombits(parse.Uint32(b[32:36])),
}
}
type m44f [16]float32
func m44fFromBytes(b []byte) m44f {
if len(b) != 64 {
log.Fatal("m44fFromBytes: need bytes of length 64")
}
return [16]float32{
math.Float32frombits(parse.Uint32(b[:4])),
math.Float32frombits(parse.Uint32(b[4:8])),
math.Float32frombits(parse.Uint32(b[8:12])),
math.Float32frombits(parse.Uint32(b[12:16])),
math.Float32frombits(parse.Uint32(b[16:20])),
math.Float32frombits(parse.Uint32(b[20:24])),
math.Float32frombits(parse.Uint32(b[24:28])),
math.Float32frombits(parse.Uint32(b[28:32])),
math.Float32frombits(parse.Uint32(b[32:36])),
math.Float32frombits(parse.Uint32(b[36:40])),
math.Float32frombits(parse.Uint32(b[40:44])),
math.Float32frombits(parse.Uint32(b[44:48])),
math.Float32frombits(parse.Uint32(b[48:52])),
math.Float32frombits(parse.Uint32(b[52:56])),
math.Float32frombits(parse.Uint32(b[56:60])),
math.Float32frombits(parse.Uint32(b[60:64])),
}
}
type preview struct {
width int32
height int32
data []byte
}
func previewFromBytes(b []byte) preview {
return preview{
width: int32(parse.Uint32(b[:4])),
height: int32(parse.Uint32(b[4:8])),
data: b[8:],
}
}
type rational struct {
a int32
b uint32
}
func rationalFromBytes(b []byte) rational {
if len(b) != 8 {
log.Fatal("rationalFromBytes: need bytes of length 8")
}
return rational{
a: int32(parse.Uint32(b[:4])),
b: parse.Uint32(b[4:8]),
}
}
type tiledesc struct {
xSize uint32
ySize uint32
mode uint8
}
func tiledescFromBytes(b []byte) tiledesc {
if len(b) != 9 {
log.Fatal("tiledescFromBytes: need bytes of length 9")
}
return tiledesc{
xSize: parse.Uint32(b[:4]),
ySize: parse.Uint32(b[4:8]),
mode: b[8],
}
}
type timecode struct {
timeAndFlags uint32
userData uint32
}
func timecodeFromBytes(b []byte) timecode {
if len(b) != 8 {
log.Fatal("timecodeFromBytes: need bytes of length 8")
}
return timecode{
timeAndFlags: parse.Uint32(b[:4]),
userData: parse.Uint32(b[4:8]),
}
}
type v2i [2]int32
func v2iFromBytes(b []byte) v2i {
if len(b) != 8 {
log.Fatal("v2iFromBytes: need bytes of length 8")
}
return v2i{
int32(parse.Uint32(b[:4])),
int32(parse.Uint32(b[4:8])),
}
}
type v2f [2]float32
func v2fFromBytes(b []byte) v2f {
if len(b) != 8 {
log.Fatal("v2fFromBytes: need bytes of length 8")
}
return v2f{
math.Float32frombits(parse.Uint32(b[:4])),
math.Float32frombits(parse.Uint32(b[4:8])),
}
}
type v3i [3]int32
func v3iFromBytes(b []byte) v3i {
if len(b) != 12 {
log.Fatal("v3iFromBytes: need bytes of length 12")
}
return v3i{
int32(parse.Uint32(b[:4])),
int32(parse.Uint32(b[4:8])),
int32(parse.Uint32(b[8:12])),
}
}
type v3f [3]float32
func v3fFromBytes(b []byte) v3f {
if len(b) != 12 {
log.Fatal("v3fFromBytes: need bytes of length 12")
}
return v3f{
math.Float32frombits(parse.Uint32(b[:4])),
math.Float32frombits(parse.Uint32(b[4:8])),
math.Float32frombits(parse.Uint32(b[8:12])),
}
}