-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
91 lines (73 loc) · 2.46 KB
/
util.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
package oiio
import (
"errors"
"fmt"
"reflect"
"unsafe"
)
// Given an ImageSpec, a slice is allocated to the size that
// is able to contain the pixels of the ImageSpec dimensions.
// The TypeDesc determines what format the pixels will be stored in.
// Returns the slice, casted to an interface.
func allocatePixelBuffer(spec *ImageSpec, format TypeDesc) (interface{}, unsafe.Pointer, error) {
size := spec.Width() * spec.Height() * spec.Depth() * spec.NumChannels()
return allocatePixelBufferSize(size, format)
}
// Given a specific size, a slice is allocated that
// is able to contain the number of pixels.
// The TypeDesc determines what format the pixels will be stored in.
// Returns the slice, casted to an interface.
func allocatePixelBufferSize(size int, format TypeDesc) (interface{}, unsafe.Pointer, error) {
if size <= 0 {
return nil, nil, fmt.Errorf("Invalid size %d; Must be greater than 0", size)
}
var (
pixel_iface interface{}
ptr unsafe.Pointer
)
switch format {
case TypeUint8:
pixels := make([]uint8, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeInt8:
pixels := make([]int8, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeUint16:
pixels := make([]uint16, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeInt16:
pixels := make([]int16, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeUint:
pixels := make([]uint, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeInt:
pixels := make([]int, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeUint64:
pixels := make([]uint64, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeInt64:
pixels := make([]int64, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeFloat, TypeHalf:
pixels := make([]float32, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
case TypeDouble:
pixels := make([]float64, size)
pixel_iface = reflect.ValueOf(pixels).Interface()
ptr = unsafe.Pointer(&pixels[0])
default:
return nil, nil, errors.New("TypeDesc is not valid for this operation")
}
return pixel_iface, ptr, nil
}