-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
401 lines (344 loc) · 9.77 KB
/
image.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
package hdr
import (
"image"
"image/color"
"github.com/mdouchement/hdr/hdrcolor"
)
// Image is a finite rectangular grid of hdrcolor.Color values taken from a color
// model.
type Image interface {
image.Image
// HDRAt returns the HDR pixel color at given coordinates.
HDRAt(x, y int) hdrcolor.Color
// Size returns the number of pixels.
Size() int
}
// ImageSet is an Image where we can set pixels.
type ImageSet interface {
// Set adds pixel to Image at given x, y.
Set(x, y int, c color.Color)
}
// EmptyAs returns a new empty image with the same underlying type and params as src.
func EmptyAs(src Image) Image {
switch m := src.(type) {
case *RGB:
return NewRGB(m.Bounds())
case *RGB64:
return NewRGB64(m.Bounds())
case *XYZ:
return NewXYZ(m.Bounds())
case *XYZ64:
return NewXYZ64(m.Bounds())
default:
// fallback
return NewRGB64(m.Bounds())
}
}
// Copy copies the src into a new image.
func Copy(src Image) Image {
switch m := src.(type) {
case *RGB:
dst := NewRGB(m.Bounds())
copy(dst.Pix, m.Pix)
return dst
case *RGB64:
dst := NewRGB64(m.Bounds())
copy(dst.Pix, m.Pix)
return dst
case *XYZ:
dst := NewXYZ(m.Bounds())
copy(dst.Pix, m.Pix)
return dst
case *XYZ64:
dst := NewXYZ64(m.Bounds())
copy(dst.Pix, m.Pix)
return dst
default:
// fallback
dst := NewRGB64(m.Bounds())
for y := 0; y < m.Bounds().Dy(); y++ {
for x := 0; x < m.Bounds().Dx(); x++ {
dst.Set(x, y, src.HDRAt(x, y))
}
}
return dst
}
}
//===============//
// RGB //
//===============//
// RGB is an in-memory 32 bits floating points image whose At method returns hdrcolor.RGB values.
type RGB struct {
// Pix holds the image's pixels, in R, G, B order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8].
Pix []float32
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewRGB returns a new HDR RGB image with the given bounds.
func NewRGB(r image.Rectangle) *RGB {
w, h := r.Dx(), r.Dy()
buf := make([]float32, 3*w*h)
return &RGB{buf, 3 * w, r}
}
// ColorModel implements Image.
func (p *RGB) ColorModel() color.Model { return hdrcolor.RGBModel }
// Bounds implements Image.
func (p *RGB) Bounds() image.Rectangle { return p.Rect }
// Size implements Image.
func (p *RGB) Size() int {
return p.Bounds().Dx() * p.Bounds().Dy()
}
// At implements Image.
func (p *RGB) At(x, y int) color.Color {
return p.RGBAt(x, y)
}
// HDRAt implements Image.
func (p *RGB) HDRAt(x, y int) hdrcolor.Color {
return p.RGBAt(x, y)
}
// RGBAt returns the RGB color at this coordinate.
func (p *RGB) RGBAt(x, y int) hdrcolor.RGB {
if !(image.Point{x, y}.In(p.Rect)) {
return hdrcolor.RGB{}
}
i := p.PixOffset(x, y)
return hdrcolor.RGB{
R: float64(p.Pix[i+0]),
G: float64(p.Pix[i+1]),
B: float64(p.Pix[i+2]),
}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *RGB) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
}
// Set adds pixel to Image at given x, y.
func (p *RGB) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := hdrcolor.RGBModel.Convert(c).(hdrcolor.RGB)
p.Pix[i+0] = float32(c1.R)
p.Pix[i+1] = float32(c1.G)
p.Pix[i+2] = float32(c1.B)
}
// SetRGB applies the given RGB color at this coordinate.
func (p *RGB) SetRGB(x, y int, c hdrcolor.RGB) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
p.Pix[i+0] = float32(c.R)
p.Pix[i+1] = float32(c.G)
p.Pix[i+2] = float32(c.B)
}
// RGB64 is an in-memory 64 bits floating points image whose At method returns hdrcolor.RGB values.
type RGB64 struct {
// Pix holds the image's pixels, in R, G, B order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8].
Pix []float64
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewRGB64 returns a new HDR RGB image with the given bounds.
func NewRGB64(r image.Rectangle) *RGB64 {
w, h := r.Dx(), r.Dy()
buf := make([]float64, 3*w*h)
return &RGB64{buf, 3 * w, r}
}
// ColorModel implements Image.
func (p *RGB64) ColorModel() color.Model { return hdrcolor.RGBModel }
// Bounds implements Image.
func (p *RGB64) Bounds() image.Rectangle { return p.Rect }
// Size implements Image.
func (p *RGB64) Size() int {
return p.Bounds().Dx() * p.Bounds().Dy()
}
// At implements Image.
func (p *RGB64) At(x, y int) color.Color {
return p.RGBAt(x, y)
}
// HDRAt implements Image.
func (p *RGB64) HDRAt(x, y int) hdrcolor.Color {
return p.RGBAt(x, y)
}
// RGBAt returns the RGB color at this coordinate.
func (p *RGB64) RGBAt(x, y int) hdrcolor.RGB {
if !(image.Point{x, y}.In(p.Rect)) {
return hdrcolor.RGB{}
}
i := p.PixOffset(x, y)
return hdrcolor.RGB{R: p.Pix[i+0], G: p.Pix[i+1], B: p.Pix[i+2]}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *RGB64) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
}
// Set adds pixel to Image at given x, y.
func (p *RGB64) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := hdrcolor.RGBModel.Convert(c).(hdrcolor.RGB)
p.Pix[i+0] = c1.R
p.Pix[i+1] = c1.G
p.Pix[i+2] = c1.B
}
// SetRGB applies the given RGB color at this coordinate.
func (p *RGB64) SetRGB(x, y int, c hdrcolor.RGB) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
p.Pix[i+0] = c.R
p.Pix[i+1] = c.G
p.Pix[i+2] = c.B
}
//===============//
// XYZ //
//===============//
// XYZ is an in-memory 32 bits floating points image whose At method returns hdrcolor.XYZ values.
type XYZ struct {
// Pix holds the image's pixels, in X, Y and Z order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8].
Pix []float32
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewXYZ returns a new HDR RGB image with the given bounds.
func NewXYZ(r image.Rectangle) *XYZ {
w, h := r.Dx(), r.Dy()
buf := make([]float32, 3*w*h)
return &XYZ{buf, 3 * w, r}
}
// ColorModel implements Image.
func (p *XYZ) ColorModel() color.Model { return hdrcolor.XYZModel }
// Bounds implements Image.
func (p *XYZ) Bounds() image.Rectangle { return p.Rect }
// Size implements Image.
func (p *XYZ) Size() int {
return p.Bounds().Dx() * p.Bounds().Dy()
}
// At implements Image.
func (p *XYZ) At(x, y int) color.Color {
return p.XYZAt(x, y)
}
// HDRAt implements Image.
func (p *XYZ) HDRAt(x, y int) hdrcolor.Color {
return p.XYZAt(x, y)
}
// XYZAt returns the XYZ color at this coordinate.
func (p *XYZ) XYZAt(x, y int) hdrcolor.XYZ {
if !(image.Point{x, y}.In(p.Rect)) {
return hdrcolor.XYZ{}
}
i := p.PixOffset(x, y)
return hdrcolor.XYZ{
X: float64(p.Pix[i+0]),
Y: float64(p.Pix[i+1]),
Z: float64(p.Pix[i+2]),
}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *XYZ) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
}
// Set adds pixel to Image at given x, y.
func (p *XYZ) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := hdrcolor.XYZModel.Convert(c).(hdrcolor.XYZ)
p.Pix[i+0] = float32(c1.X)
p.Pix[i+1] = float32(c1.Y)
p.Pix[i+2] = float32(c1.Z)
}
// SetXYZ applies the given XYZ color at this coordinate.
func (p *XYZ) SetXYZ(x, y int, c hdrcolor.XYZ) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
p.Pix[i+0] = float32(c.X)
p.Pix[i+1] = float32(c.Y)
p.Pix[i+2] = float32(c.Z)
}
// XYZ64 is an in-memory 64 bits floating points image whose At method returns hdrcolor.XYZ values.
type XYZ64 struct {
// Pix holds the image's pixels, in X, Y and Z order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*8].
Pix []float64
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewXYZ64 returns a new HDR RGB image with the given bounds.
func NewXYZ64(r image.Rectangle) *XYZ64 {
w, h := r.Dx(), r.Dy()
buf := make([]float64, 3*w*h)
return &XYZ64{buf, 3 * w, r}
}
// ColorModel implements Image.
func (p *XYZ64) ColorModel() color.Model { return hdrcolor.XYZModel }
// Bounds implements Image.
func (p *XYZ64) Bounds() image.Rectangle { return p.Rect }
// Size implements Image.
func (p *XYZ64) Size() int {
return p.Bounds().Dx() * p.Bounds().Dy()
}
// At implements Image.
func (p *XYZ64) At(x, y int) color.Color {
return p.XYZAt(x, y)
}
// HDRAt implements Image.
func (p *XYZ64) HDRAt(x, y int) hdrcolor.Color {
return p.XYZAt(x, y)
}
// XYZAt returns the XYZ color at this coordinate.
func (p *XYZ64) XYZAt(x, y int) hdrcolor.XYZ {
if !(image.Point{x, y}.In(p.Rect)) {
return hdrcolor.XYZ{}
}
i := p.PixOffset(x, y)
return hdrcolor.XYZ{X: p.Pix[i+0], Y: p.Pix[i+1], Z: p.Pix[i+2]}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *XYZ64) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
}
// Set adds pixel to Image at given x, y.
func (p *XYZ64) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := hdrcolor.XYZModel.Convert(c).(hdrcolor.XYZ)
p.Pix[i+0] = c1.X
p.Pix[i+1] = c1.Y
p.Pix[i+2] = c1.Z
}
// SetXYZ applies the given XYZ color at this coordinate.
func (p *XYZ64) SetXYZ(x, y int, c hdrcolor.XYZ) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
p.Pix[i+0] = c.X
p.Pix[i+1] = c.Y
p.Pix[i+2] = c.Z
}