-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixmap.go
381 lines (317 loc) · 9.35 KB
/
pixmap.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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi
import (
"fmt"
"github.com/elgopher/pi/internal/sfmt"
)
// PixMap is a generic data structure for manipulating any kind of pixel data - screen, sprite-sheet etc.
// PixMap uses a single byte (8 bits) for storing single color/pixel. This means that max 256 colors
// can be used. PixMap can also be used for maps which not necessary contain pixel colors, such as world map
// (as long as only 256 different tiles are used).
//
// To create PixMap please use either NewPixMap or NewPixMapWithPix function.
//
// All PixMap functions (besides Clear and ClearCol) take into account the clipping region.
type PixMap struct {
pix []byte
width int
height int
clip Region
zeroPix []byte
wholeLinePix []byte
}
// NewPixMap creates new instance of PixMap with specified size.
// Width and height cannot be negative.
func NewPixMap(width, height int) PixMap {
if width < 0 {
panic("negative PixMap with")
}
if height < 0 {
panic("negative PixMap height")
}
pix := make([]byte, width*height)
return PixMap{
pix: pix,
width: width,
height: height,
clip: Region{W: width, H: height},
zeroPix: make([]byte, len(pix)),
wholeLinePix: make([]byte, width),
}
}
// NewPixMapWithPix creates new instance of PixMap using the slice of pixel colors
// as a source. pix slice contains colors for the entire PixMap.
// Pixels are organized from left to right, top to bottom. Slice element
// number 0 has pixel located in the top-left corner. Slice element number 1
// has pixel color on the right, and so on.
//
// The lineWidth is the width of PixMap. Height is calculated by dividing pixels
// by lineWith.
//
// This function is handy when you already have a pix slice and want to create a PixMap
// out of it. This function does not allocate anything on the heap.
func NewPixMapWithPix(pix []byte, lineWidth int) PixMap {
if lineWidth < 0 {
panic("PixMap lineWidth cant be negative")
}
if lineWidth == 0 {
if len(pix) > 0 {
panic("PixMap lineWidth cant be zero when pix slice is not empty")
}
return PixMap{}
}
if len(pix)%lineWidth != 0 {
panic("invalid pixmap lineWidth. Length of pix slice must be multiple of lineWidth.")
}
height := len(pix) / lineWidth
return PixMap{
pix: pix,
width: lineWidth,
height: height,
clip: Region{W: lineWidth, H: height},
zeroPix: make([]byte, len(pix)),
wholeLinePix: make([]byte, lineWidth),
}
}
// Pix return pixel colors. Pixels are organized from left to right,
// top to bottom. Slice element number 0 has pixel located
// in the top-left corner. Slice element number 1 has pixel color
// on the right and so on.
//
// Returned slice can be freely read and updated. Useful when
// you want to use your own functions for pixel manipulation.
func (p PixMap) Pix() []byte {
return p.pix
}
// Width returns the width of PixMap (lineWidth), without taking
// into account the current clipping region.
func (p PixMap) Width() int {
return p.width
}
// Height returns the height of PixMap, without taking into account
// the current clipping region.
func (p PixMap) Height() int {
return p.height
}
// Clip returns the clipping region, which specifies which fragment
// of the PixMap can be accessed by its functions. By default, clipping
// region has a size of the entire PixMap (no clipping).
func (p PixMap) Clip() Region {
return p.clip
}
// WithClip creates a new PixMap which has a different clipping region.
// The newly created PixMap still refers to the same pixels though.
func (p PixMap) WithClip(x, y, w, h int) PixMap {
if x < 0 {
w += x
x = 0
}
if y < 0 {
h += y
y = 0
}
if x+w > p.width {
w = p.width - x
}
if y+h > p.height {
h = p.height - y
}
p.clip = Region{X: x, Y: y, W: w, H: h}
return p
}
// Clear clears the entire PixMap with color 0. It does not take into account the clipping region.
func (p PixMap) Clear() {
copy(p.pix, p.zeroPix)
}
// ClearCol clears the entire PixMap with specified color. It does not take into account the clipping region.
func (p PixMap) ClearCol(col byte) {
line := p.lineOfColor(col, p.width)
pix := p.pix
copy(pix, line)
for i := 1; i < p.height; i++ {
pix = pix[p.width:]
copy(pix, line)
}
}
// Pointer finds the index of (x,y) coordinates and returns a pointer to pixel data at this position
// in Pointer.Pix.
//
// If the pixel is outside the clipping region, then the closest pixel to the bottom-right
// is returned. The difference in position is returned in Pointer.DeltaX and Pointer.DeltaY.
//
// If the pixel is either below or right after the clipping region then ok=false and empty
// Pointer are returned.
func (p PixMap) Pointer(x, y, w, h int) (ptr Pointer, ok bool) {
if w <= 0 || h <= 0 {
return ptr, false
}
clip := p.clip
if x >= clip.X+clip.W {
return ptr, false
}
if y >= clip.Y+clip.H {
return ptr, false
}
if x+w <= clip.X {
return ptr, false
}
if y+h <= clip.Y {
return ptr, false
}
var dx, dy int
if x < clip.X {
dx = clip.X - x
x += dx
w -= dx
}
if y < clip.Y {
dy = clip.Y - y
y += dy
h -= dy
}
pix := p.pix[y*p.width+x:]
return Pointer{
DeltaX: dx,
DeltaY: dy,
Pix: pix,
RemainingPixels: MinInt(w, clip.X+clip.W-x),
RemainingLines: MinInt(h, clip.Y+clip.H-y),
}, true
}
// Pointer is a low-level struct for fast pixel processing created by PixMap.Pointer function.
type Pointer struct {
// Pix is a slice of PixMap.Pix at the position specified when calling PixMap.Pointer function.
Pix []byte
DeltaX, DeltaY int
RemainingPixels int // in line
RemainingLines int
}
// Copy copies the region specified by x, y, w, h into dst PixMap at dstX,dstY position.
func (p PixMap) Copy(x, y, w, h int, dst PixMap, dstX, dstY int) {
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)
remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
if remainingLines == 0 {
return
}
remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels)
copy(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
for i := 1; i < remainingLines; i++ {
dstPtr.Pix = dstPtr.Pix[dst.width:]
srcPtr.Pix = srcPtr.Pix[p.width:]
copy(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
}
}
// Merge merges destination with source by running merge operation for each destination line.
func (p PixMap) Merge(x, y, w, h int, dst PixMap, dstX, dstY int, merge func(dst, src []byte)) {
dstPtr, srcPtr := p.pointersForCopy(x, y, w, h, dst, dstX, dstY)
remainingLines := MinInt(dstPtr.RemainingLines, srcPtr.RemainingLines)
if remainingLines == 0 {
return
}
remainingPixels := MinInt(dstPtr.RemainingPixels, srcPtr.RemainingPixels)
merge(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
for i := 1; i < remainingLines; i++ {
dstPtr.Pix = dstPtr.Pix[dst.width:]
srcPtr.Pix = srcPtr.Pix[p.width:]
merge(dstPtr.Pix[:remainingPixels], srcPtr.Pix)
}
}
// Foreach runs the update function on PixMap fragment specified by x, y, w and h.
//
// The update function accepts entire line to increase the performance.
func (p PixMap) Foreach(x, y, w, h int, update func(x, y int, dst []byte)) {
if update == nil {
return
}
ptr, ok := p.Pointer(x, y, w, h)
if !ok {
return
}
x = x + ptr.DeltaX
y = y + ptr.DeltaY
update(x, y, ptr.Pix[:ptr.RemainingPixels])
for i := 1; i < ptr.RemainingLines; i++ {
ptr.Pix = ptr.Pix[p.width:]
update(x, y+i, ptr.Pix[:ptr.RemainingPixels])
}
}
// Set sets the pixel color at given position.
func (p PixMap) Set(x, y int, col byte) {
if x < p.clip.X {
return
}
if y < p.clip.Y {
return
}
if x >= p.clip.X+p.clip.W {
return
}
if y >= p.clip.Y+p.clip.H {
return
}
p.pix[y*p.width+x] = col
}
// Get returns the pixel color at given position. If coordinates
// are outside clipping region than color 0 is returned.
func (p PixMap) Get(x, y int) byte {
if x < p.clip.X {
return 0
}
if y < p.clip.Y {
return 0
}
if x >= p.clip.X+p.clip.W {
return 0
}
if y >= p.clip.Y+p.clip.H {
return 0
}
return p.pix[y*p.width+x]
}
func (p PixMap) lineOfColor(col byte, length int) []byte {
line := p.wholeLinePix[:length]
for i := 0; i < len(line); i++ {
p.wholeLinePix[i] = col
}
return line
}
func (p PixMap) pointersForCopy(srcX int, srcY int, w int, h int, dst PixMap, dstX int, dstY int) (Pointer, Pointer) {
dstPtr, _ := dst.Pointer(dstX, dstY, w, h)
srcPtr, _ := p.Pointer(srcX, srcY, w, h)
// both maps must be moved by the same DeltaX and DeltaY
if srcPtr.DeltaX > dstPtr.DeltaX {
dstPtr = addX(dstPtr, srcPtr.DeltaX-dstPtr.DeltaX)
} else {
srcPtr = addX(srcPtr, dstPtr.DeltaX-srcPtr.DeltaX)
}
if srcPtr.DeltaY > dstPtr.DeltaY {
dstPtr = addY(dstPtr, srcPtr.DeltaY-dstPtr.DeltaY, dst.width)
} else {
srcPtr = addY(srcPtr, dstPtr.DeltaY-srcPtr.DeltaY, p.width)
}
return dstPtr, srcPtr
}
func addX(p Pointer, n int) Pointer {
p.DeltaX += n
p.RemainingPixels -= n
if p.RemainingPixels <= 0 {
return Pointer{}
}
p.Pix = p.Pix[n:]
return p
}
func addY(p Pointer, n int, lineWidth int) Pointer {
p.DeltaY += n
p.RemainingLines -= n
if p.RemainingLines <= 0 {
return Pointer{}
}
p.Pix = p.Pix[n*lineWidth:]
return p
}
// String returns PixMap as string for debugging purposes.
func (p PixMap) String() string {
return fmt.Sprintf("{width:%d, height:%d, clip:%+v, pix:%s}",
p.width, p.height, p.clip, sfmt.FormatBigSlice(p.pix, 1024))
}