-
Notifications
You must be signed in to change notification settings - Fork 90
/
image_pix_slice.go
242 lines (210 loc) · 5.73 KB
/
image_pix_slice.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
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webp
import (
"reflect"
"unsafe"
)
type PixSlice []byte
// AsPixSilce convert a normal slice to byte slice.
//
// Convert []X to []byte:
//
// x := make([]X, xLen)
// y := AsPixSilce(x)
//
func AsPixSilce(slice interface{}) (d PixSlice) {
sv := reflect.ValueOf(slice)
h := (*reflect.SliceHeader)((unsafe.Pointer(&d)))
h.Cap = sv.Cap() * int(sv.Type().Elem().Size())
h.Len = sv.Len() * int(sv.Type().Elem().Size())
h.Data = sv.Pointer()
return
}
// Slice convert a normal slice to new type slice.
//
// Convert []byte to []Y:
// x := make([]byte, xLen)
// y := PixSlice(x).Slice(reflect.TypeOf([]Y(nil))).([]Y)
//
func (d PixSlice) Slice(newSliceType reflect.Type) interface{} {
sv := reflect.ValueOf(d)
newSlice := reflect.New(newSliceType)
hdr := (*reflect.SliceHeader)(unsafe.Pointer(newSlice.Pointer()))
hdr.Cap = sv.Cap() * int(sv.Type().Elem().Size()) / int(newSliceType.Elem().Size())
hdr.Len = sv.Len() * int(sv.Type().Elem().Size()) / int(newSliceType.Elem().Size())
hdr.Data = uintptr(sv.Pointer())
return newSlice.Elem().Interface()
}
func (d PixSlice) Bytes() (v []byte) {
return d
}
func (d PixSlice) Int8s() (v []int8) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap
h1.Len = h0.Len
h1.Data = h0.Data
return
}
func (d PixSlice) Int16s() (v []int16) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 2
h1.Len = h0.Len / 2
h1.Data = h0.Data
return
}
func (d PixSlice) Int32s() (v []int32) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 4
h1.Len = h0.Len / 4
h1.Data = h0.Data
return
}
func (d PixSlice) Int64s() (v []int64) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 8
h1.Len = h0.Len / 8
h1.Data = h0.Data
return
}
func (d PixSlice) Uint8s() []uint8 {
return d
}
func (d PixSlice) Uint16s() (v []uint16) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 2
h1.Len = h0.Len / 2
h1.Data = h0.Data
return
}
func (d PixSlice) Uint32s() (v []uint32) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 4
h1.Len = h0.Len / 4
h1.Data = h0.Data
return
}
func (d PixSlice) Uint64s() (v []uint64) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 8
h1.Len = h0.Len / 8
h1.Data = h0.Data
return
}
func (d PixSlice) Float32s() (v []float32) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 4
h1.Len = h0.Len / 4
h1.Data = h0.Data
return
}
func (d PixSlice) Float64s() (v []float64) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 8
h1.Len = h0.Len / 8
h1.Data = h0.Data
return
}
func (d PixSlice) Complex64s() (v []complex64) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 16
h1.Len = h0.Len / 16
h1.Data = h0.Data
return
}
func (d PixSlice) Complex128s() (v []complex128) {
h0 := (*reflect.SliceHeader)(unsafe.Pointer(&d))
h1 := (*reflect.SliceHeader)(unsafe.Pointer(&v))
h1.Cap = h0.Cap / 32
h1.Len = h0.Len / 32
h1.Data = h0.Data
return
}
func (d PixSlice) Value(i int, dataType reflect.Kind) float64 {
switch dataType {
case reflect.Int8:
return float64(d.Int8s()[i])
case reflect.Int16:
return float64(d.Int16s()[i])
case reflect.Int32:
return float64(d.Int32s()[i])
case reflect.Int64:
return float64(d.Int64s()[i])
case reflect.Uint8:
return float64(d[i])
case reflect.Uint16:
return float64(d.Uint16s()[i])
case reflect.Uint32:
return float64(d.Uint32s()[i])
case reflect.Uint64:
return float64(d.Uint64s()[i])
case reflect.Float32:
return float64(d.Float32s()[i])
case reflect.Float64:
return float64(d.Float64s()[i])
case reflect.Complex64:
return float64(real(d.Complex64s()[i]))
case reflect.Complex128:
return float64(real(d.Complex128s()[i]))
}
return 0
}
func (d PixSlice) SetValue(i int, dataType reflect.Kind, v float64) {
switch dataType {
case reflect.Int8:
d.Int8s()[i] = int8(v)
case reflect.Int16:
d.Int16s()[i] = int16(v)
case reflect.Int32:
d.Int32s()[i] = int32(v)
case reflect.Int64:
d.Int64s()[i] = int64(v)
case reflect.Uint8:
d[i] = byte(v)
case reflect.Uint16:
d.Uint16s()[i] = uint16(v)
case reflect.Uint32:
d.Uint32s()[i] = uint32(v)
case reflect.Uint64:
d.Uint64s()[i] = uint64(v)
case reflect.Float32:
d.Float32s()[i] = float32(v)
case reflect.Float64:
d.Float64s()[i] = float64(v)
case reflect.Complex64:
d.Complex64s()[i] = complex(float32(v), 0)
case reflect.Complex128:
d.Complex128s()[i] = complex(float64(v), 0)
}
}
func (d PixSlice) SwapEndian(dataType reflect.Kind) {
switch dataType {
case reflect.Int16, reflect.Uint16:
for i := 0; i+2-1 < len(d); i = i + 2 {
d[i+0], d[i+1] = d[i+1], d[i+0]
}
case reflect.Int32, reflect.Uint32, reflect.Float32, reflect.Complex64:
for i := 0; i+4-1 < len(d); i = i + 4 {
d[i+0], d[i+3] = d[i+3], d[i+0]
d[i+1], d[i+2] = d[i+2], d[i+1]
}
case reflect.Int64, reflect.Uint64, reflect.Float64, reflect.Complex128:
for i := 0; i+8-1 < len(d); i = i + 8 {
d[i+0], d[i+7] = d[i+7], d[i+0]
d[i+1], d[i+6] = d[i+6], d[i+1]
d[i+2], d[i+5] = d[i+5], d[i+2]
d[i+3], d[i+4] = d[i+4], d[i+3]
}
}
}