-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelbuffer.go
234 lines (201 loc) · 4.82 KB
/
pixelbuffer.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
package main
import (
"fmt"
"image"
"image/color"
"math"
"github.com/airbusgeo/godal"
"github.com/hschendel/stl"
)
type PixelBuffer struct {
buf []float32
width uint
height uint
minMaxComputed bool
min float32
max float32
}
func (pb *PixelBuffer) minMax() {
pb.min = pb.buf[0]
pb.max = pb.buf[0]
for _, p := range pb.buf {
if p < pb.min {
pb.min = p
}
if p > pb.max {
pb.max = p
}
}
pb.minMaxComputed = true
}
func (pb *PixelBuffer) Min() float32 {
if !pb.minMaxComputed {
pb.minMax()
}
return pb.min
}
func (pb *PixelBuffer) Max() float32 {
if !pb.minMaxComputed {
pb.minMax()
}
return pb.max
}
// Imagine width is three, height is two and pixel data is:
//
// a b c
// d e f
//
// This will be in the buf as: a b c d e f
// So for each 'y' we need to advance by 'width'
// x + y*width
func (pb *PixelBuffer) get(x uint, y uint) float32 {
return pb.buf[x+y*pb.width]
}
func (pb *PixelBuffer) getVec3(x uint, y uint) stl.Vec3 {
return stl.Vec3{
float32(x),
float32(y),
pb.get(x, pb.height-y-1), // STL coordinate system is inverted, so flip y
}
}
func AppendRectangle(solid *stl.Solid, tl, tr, bl, br stl.Vec3) {
solid.AppendTriangle(stl.Triangle{Vertices: [3]stl.Vec3{tl, bl, tr}})
solid.AppendTriangle(stl.Triangle{Vertices: [3]stl.Vec3{tr, bl, br}})
}
func (pb *PixelBuffer) toSTL() *stl.Solid {
solid := &stl.Solid{}
// The code below supports only converting a window, but
// I moved that to the GeoTIFF parsing.
x1 := uint(0)
y1 := uint(0)
x2 := pb.width - 1
y2 := pb.height - 1
// Top
for i := x1; i < x2; i++ {
for j := y1; j < y2; j++ {
AppendRectangle(
solid,
pb.getVec3(i, j),
pb.getVec3(i+1, j),
pb.getVec3(i, j+1),
pb.getVec3(i+1, j+1))
}
}
for i := x1; i < x2; i++ {
// Back
AppendRectangle(
solid,
pb.getVec3(i+1, y1),
pb.getVec3(i, y1),
stl.Vec3{float32(i + 1), float32(y1), 0},
stl.Vec3{float32(i), float32(y1), 0},
)
// Front
AppendRectangle(
solid,
pb.getVec3(i, y2),
pb.getVec3(i+1, y2),
stl.Vec3{float32(i), float32(y2), 0},
stl.Vec3{float32(i + 1), float32(y2), 0},
)
}
for j := y1; j < y2; j++ {
// Left
AppendRectangle(
solid,
pb.getVec3(x1, j),
pb.getVec3(x1, j+1),
stl.Vec3{float32(x1), float32(j), 0},
stl.Vec3{float32(x1), float32(j + 1), 0},
)
// Right
AppendRectangle(
solid,
pb.getVec3(x2, j+1),
pb.getVec3(x2, j),
stl.Vec3{float32(x2), float32(j + 1), 0},
stl.Vec3{float32(x2), float32(j), 0},
)
}
// Bottom
AppendRectangle(
solid,
stl.Vec3{float32(x1), float32(y2), 0},
stl.Vec3{float32(x2), float32(y2), 0},
stl.Vec3{float32(x1), float32(y1), 0},
stl.Vec3{float32(x2), float32(y1), 0},
)
solid.RecalculateNormals()
solid.Validate()
return solid
}
func (pb *PixelBuffer) ToImage() image.Image {
r := image.Rect(0, 0, int(pb.width)-1, int(pb.height)-1)
img := image.NewNRGBA(r)
fmt.Printf("Rescaling image with height min %f and max %f\n", pb.Min(), pb.Max())
for i := uint(0); i < pb.width; i++ {
for j := uint(0); j < pb.height; j++ {
z := pb.get(i, j)
c := 65535 * (z - pb.Min()) / (pb.Max() - pb.Min())
img.Set(int(i), int(j), color.Gray16{uint16(c)})
}
}
return img
}
func (pb *PixelBuffer) Zero(min float32) {
if len(pb.buf) == 0 {
return
}
for i := range pb.buf {
pb.buf[i] = pb.buf[i] - pb.Min() + min
}
}
func (pb *PixelBuffer) Scale(s float32) {
for i := range pb.buf {
pb.buf[i] = pb.buf[i] * s
}
}
func (pb *PixelBuffer) Diff(pb2 *PixelBuffer) {
for i := range pb.buf {
pb.buf[i] = pb.buf[i] - pb2.buf[i]
}
}
// FromGeoTIFF loads a GeoTIFF file from path using gdal and reads a rectangle of pixels
// with upper left corner at (x, y), width w and height h into a PixelBuffer.
func FromGeoTIFF(path string, x, y, w, h uint) (*PixelBuffer, error) {
godal.RegisterAll()
hDataset, err := godal.Open(path)
if err != nil {
return nil, err
}
defer hDataset.Close()
structure := hDataset.Structure()
if uint(structure.SizeX) < x+w {
return nil, fmt.Errorf("selected window goes outside image bounds (image width=%d, window max x=%d)", structure.SizeX, x+w)
}
if uint(structure.SizeY) < y+h {
return nil, fmt.Errorf("selected window goes outside image bounds (image height=%d, window max y=%d)", structure.SizeY, y+h)
}
if w == 0 {
w = uint(structure.SizeX) - x
}
if h == 0 {
h = uint(structure.SizeY) - y
}
band := hDataset.Bands()[0]
buf := make([]float32, w*h)
fmt.Printf("Reading GeoTIFF band %d (of %d) window (%dx%d+%dx%d) into buffer size %d...",
1, len(hDataset.Bands()), x, y, w, h, len(buf))
err = band.Read(int(x), int(y), buf, int(w), int(h))
fmt.Println("done")
if err != nil {
return nil, err
}
// Set undefined to zero, for now
for i := range buf {
if buf[i] == -math.MaxFloat32 {
buf[i] = 0
}
}
return &PixelBuffer{buf: buf, width: w, height: h}, nil
}