-
Notifications
You must be signed in to change notification settings - Fork 0
/
image3d.go
310 lines (266 loc) · 7.79 KB
/
image3d.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
package image3d
import (
"errors"
"fmt"
_ "image/jpeg"
"path/filepath"
"strings"
"unsafe"
gl "github.com/adrianderstroff/pbr/pkg/core/gl"
"github.com/adrianderstroff/pbr/pkg/view/image/image2d"
)
// Image3D stores the dimensions, data format and it's pixel data.
// It can be used to manipulate single pixels and is used to
// upload it's data to a texture.
type Image3D struct {
width int
height int
slices int
channels int
bytedepth int
pixelType uint32
data []image2d.Image2D
}
// Make constructs an image of the specified length, width, height and with all
// pixels set to the specified rgba value.
func Make(width, height, slices, channels int) (Image3D, error) {
// create image data
var data []image2d.Image2D
for i := 0; i < slices; i++ {
image, err := image2d.Make(width, height, channels)
if err != nil {
return Image3D{}, err
}
data = append(data, image)
}
return Image3D{
width: width,
height: height,
slices: slices,
channels: channels,
bytedepth: 1,
pixelType: data[0].GetPixelType(),
data: data,
}, nil
}
// MakeFromData constructs an image of the specified width, height, slices and
// the specified data.
func MakeFromData(width, height, slices, channels int, data []uint8) (Image3D, error) {
// determine number of channels
bytedepth := len(data) / (width * height * slices * channels)
// create the individual images
var images []image2d.Image2D
size := width * height * channels
for i := 0; i < slices; i++ {
s, e := i*size, (i+1)*size
image, err := image2d.MakeFromData(width, height, channels, data[s:e])
if err != nil {
return Image3D{}, err
}
images = append(images, image)
}
pixeltype, err := getPixelTypeFromByteDepth(bytedepth)
if err != nil {
return Image3D{}, err
}
return Image3D{
width: width,
height: height,
slices: slices,
channels: channels,
bytedepth: bytedepth,
pixelType: uint32(pixeltype),
data: images,
}, nil
}
// MakeFromPath constructs the image data from the specified paths.
// If there is no image at the specified path an error is returned instead.
// The dimensions of all images must match.
func MakeFromPath(paths []string) (Image3D, error) {
// early exit if no path had been provided
if len(paths) == 0 {
return Image3D{}, errors.New("No image paths provided")
}
// get the first path
first, err := image2d.MakeFromPath(paths[0])
if err != nil {
return Image3D{}, err
}
// load multiple images for each path
var images []image2d.Image2D
images = append(images, first)
for _, path := range paths[1:] {
// load current image
image, err := image2d.MakeFromPath(path)
if err != nil {
return Image3D{}, err
}
// check if dimensions and formats match
if first.GetWidth() != image.GetWidth() ||
first.GetHeight() != image.GetHeight() ||
first.GetChannels() != image.GetChannels() ||
first.GetPixelType() != image.GetPixelType() ||
first.GetByteDepth() != image.GetByteDepth() {
return Image3D{}, errors.New("image dimensions or formats don't match")
}
// append images
images = append(images, image)
}
return Image3D{
width: first.GetWidth(),
height: first.GetHeight(),
slices: len(paths),
channels: first.GetChannels(),
bytedepth: first.GetByteDepth(),
pixelType: first.GetPixelType(),
data: images,
}, nil
}
// SaveToPath saves all slices as png images to the specified path.
// All images will be enumerated starting with 0.
// The file names will look like the following: dir/filename<NUMBER>.png
func (image *Image3D) SaveToPath(path string) error {
ext := filepath.Ext(path)
pathnoext := strings.TrimSuffix(path, ext)
for i := 0; i < image.slices; i++ {
curpath := pathnoext + fmt.Sprint(i) + ext
err := image.data[i].SaveToPath(curpath)
if err != nil {
return err
}
}
return nil
}
// FlipX flips all slices horizontally.
func (image *Image3D) FlipX() {
for _, slice := range image.data {
slice.FlipX()
}
}
// FlipY flips all slices vertically.
func (image *Image3D) FlipY() {
for _, slice := range image.data {
slice.FlipY()
}
}
// GetWidth returns the width of the image.
func (image *Image3D) GetWidth() int {
return image.width
}
// GetHeight returns the height of the image.
func (image *Image3D) GetHeight() int {
return image.height
}
// GetSlices returns the number of slices of the image.
func (image *Image3D) GetSlices() int {
return image.slices
}
// GetChannels return the number of the channels of the image.
func (image *Image3D) GetChannels() int {
return image.channels
}
// GetByteDepth returns the number of bytes a channel consists of.
func (image *Image3D) GetByteDepth() int {
return image.bytedepth
}
// GetPixelType gets the data type of the pixel data.
func (image *Image3D) GetPixelType() uint32 {
return image.pixelType
}
// GetDataPointer returns an pointer to the beginning of the image data.
func (image *Image3D) GetDataPointer() unsafe.Pointer {
return gl.Ptr(image.data)
}
// GetData returns a copy of the images data.
func (image *Image3D) GetData() []uint8 {
// collect data of all slices
var data []uint8
for _, slice := range image.data {
data = append(data, slice.GetData()...)
}
return data
}
// GetR returns the red value of the pixel at (x,y) in slice z.
func (image *Image3D) GetR(x, y, z int) uint8 {
return image.data[z].GetR(x, y)
}
// GetG returns the green value of the pixel at (x,y) in slice z.
func (image *Image3D) GetG(x, y, z int) uint8 {
return image.data[z].GetG(x, y)
}
// GetB returns the blue value of the pixel at (x,y) in slice z.
func (image *Image3D) GetB(x, y, z int) uint8 {
return image.data[z].GetB(x, y)
}
// GetA returns the alpha value of the pixel at (x,y) in slice z.
func (image *Image3D) GetA(x, y, z int) uint8 {
return image.data[z].GetA(x, y)
}
// GetRGB returns the RGB values of the pixel at (x,y) in slice z.
func (image *Image3D) GetRGB(x, y, z int) (uint8, uint8, uint8) {
return image.data[z].GetRGB(x, y)
}
// GetRGBA returns the RGBA value of the pixel at (x,y) in slice z.
func (image *Image3D) GetRGBA(x, y, z int) (uint8, uint8, uint8, uint8) {
return image.data[z].GetRGBA(x, y)
}
// SetR sets the red value of the pixel at (x,y) in slice z.
func (image *Image3D) SetR(x, y, z int, r uint8) {
image.data[z].SetR(x, y, r)
}
// SetG sets the green value of the pixel at (x,y) in slice z.
func (image *Image3D) SetG(x, y, z int, g uint8) {
image.data[z].SetG(x, y, g)
}
// SetB sets the blue value of the pixel at (x,y) in slice z.
func (image *Image3D) SetB(x, y, z int, b uint8) {
image.data[z].SetB(x, y, b)
}
// SetA sets the alpha value of the pixel at (x,y) in slice z.
func (image *Image3D) SetA(x, y, z int, a uint8) {
image.data[z].SetA(x, y, a)
}
// SetRGB sets the RGB values of the pixel at (x,y) in slice z.
func (image *Image3D) SetRGB(x, y, z int, r, g, b uint8) {
image.data[z].SetRGB(x, y, r, g, b)
}
// SetRGBA sets the RGBA values of the pixel at (x,y) in slice z.
func (image *Image3D) SetRGBA(x, y, z int, r, g, b, a uint8) {
image.data[z].SetRGBA(x, y, r, g, b, a)
}
// String pretty prints information about the image.
func (image Image3D) String() string {
c := getChannelsName(image.channels)
d := image.bytedepth * 8
return fmt.Sprintf("Image3D (%v,%v,%v) %v %vbit", image.width, image.height,
image.slices, c, d)
}
func getChannelsName(channels int) string {
c := "Unknown Channel Number"
switch channels {
case 1:
c = "RED"
break
case 2:
c = "RG"
break
case 3:
c = "RGB"
break
case 4:
c = "RGBA"
break
}
return c
}
// getPixelTypeFromByteDepth returns the appropriate pixel type for the given
// bytedepth. So far online a bytedepth of 1 and 4 is supported.
func getPixelTypeFromByteDepth(bytedepth int) (int, error) {
switch bytedepth {
case 1:
return gl.UNSIGNED_BYTE, nil
case 4:
return gl.FLOAT, nil
}
return 0, errors.New("bytedepth not supported")
}