-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.go
More file actions
243 lines (218 loc) · 5.51 KB
/
encode.go
File metadata and controls
243 lines (218 loc) · 5.51 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/bcn
package bcn
import (
"runtime"
"sync"
)
// decodeBlocks is a convenience wrapper that decodes with default options.
func decodeBlocks(data []byte, width, height int, format Format) ([]byte, error) {
return decodeBlocksWithOptions(data, width, height, format, nil)
}
// decodeBlocksWithOptions decodes compressed or uncompressed payload into tight RGBA.
func decodeBlocksWithOptions(data []byte, width, height int, format Format, opts *DecodeOptions) ([]byte, error) {
if width <= 0 || height <= 0 {
return nil, ErrInvalidDimensions
}
blockSize := format.blockSize()
if blockSize == 0 {
return nil, ErrUnsupportedFormat
}
if !format.isCompressed() {
expected := width * height * blockSize
if len(data) < expected {
return nil, ErrInsufficientData
}
switch format {
case FormatRGBA8:
out := make([]byte, expected)
copy(out, data[:expected])
return out, nil
case FormatBGRA8:
out := make([]byte, expected)
for i := 0; i < expected; i += 4 {
out[i] = data[i+2]
out[i+1] = data[i+1]
out[i+2] = data[i]
out[i+3] = data[i+3]
}
return out, nil
default:
return nil, ErrUnsupportedUncompressedFormat
}
}
bx := (width + 3) / 4
by := (height + 3) / 4
if len(data) < bx*by*blockSize {
return nil, ErrInsufficientData
}
out := make([]byte, width*height*4)
totalBlocks := bx * by
workers := runtime.GOMAXPROCS(0)
if opts != nil {
workers = opts.Workers
if workers == 0 {
workers = runtime.GOMAXPROCS(0)
}
}
if workers > totalBlocks {
workers = totalBlocks
}
parallelMinBlocks := 256 * workers
if totalBlocks >= parallelMinBlocks && workers > 1 {
// Split block-space into contiguous ranges to keep writes non-overlapping.
pool := getDecodePool(workers)
var wg sync.WaitGroup
wg.Add(workers)
for w := 0; w < workers; w++ {
start := (totalBlocks * w) / workers
end := (totalBlocks * (w + 1)) / workers
pool.jobs <- decodeJob{
start: start,
end: end,
bx: bx,
width: width,
height: height,
blockSize: blockSize,
format: format,
data: data,
out: out,
wg: &wg,
}
}
wg.Wait()
return out, nil
}
pos := 0
for y := range by {
for x := range bx {
var block [16]rgba8
switch format {
case FormatDXT1:
block = decodeBlockDXT1(data[pos : pos+8])
pos += 8
case FormatDXT3:
block = decodeBlockDXT3(data[pos : pos+16])
pos += 16
case FormatDXT5:
block = decodeBlockDXT5(data[pos : pos+16])
pos += 16
case FormatBC4:
alpha := decodeBlockBC4(data[pos : pos+8])
pos += 8
for i := range 16 {
block[i] = rgba8{r: alpha[i], g: alpha[i], b: alpha[i], a: 255}
}
case FormatBC5:
block = decodeBlockBC5(data[pos : pos+16])
pos += 16
default:
return nil, ErrUnsupportedFormat
}
storeBlock(out, width, height, x, y, block)
}
}
return out, nil
}
// encodeBlocksWithOptions encodes tight RGBA pixels into the selected BCn format.
func encodeBlocksWithOptions(rgba []byte, width, height int, format Format, opts *EncodeOptions) ([]byte, error) {
if width <= 0 || height <= 0 {
return nil, ErrInvalidDimensions
}
if len(rgba) != width*height*4 {
return nil, ErrInvalidRGBALength
}
blockSize := format.blockSize()
if blockSize == 0 {
return nil, ErrUnsupportedFormat
}
if !format.isCompressed() {
switch format {
case FormatRGBA8:
out := make([]byte, len(rgba))
copy(out, rgba)
return out, nil
case FormatBGRA8:
out := make([]byte, len(rgba))
for i := 0; i < len(rgba); i += 4 {
out[i] = rgba[i+2]
out[i+1] = rgba[i+1]
out[i+2] = rgba[i]
out[i+3] = rgba[i+3]
}
return out, nil
default:
return nil, ErrUnsupportedUncompressedFormat
}
}
options := normalizeEncodeOptions(opts)
bx := (width + 3) / 4
by := (height + 3) / 4
out := make([]byte, bx*by*blockSize)
totalBlocks := bx * by
workers := options.Workers
if workers == 0 {
workers = runtime.GOMAXPROCS(0)
}
if workers > totalBlocks {
workers = totalBlocks
}
parallelMinBlocks := 256 * workers
if totalBlocks >= parallelMinBlocks && workers > 1 {
// Encode worker ranges directly into preallocated output block slices.
pool := getEncodePool(workers)
var wg sync.WaitGroup
wg.Add(workers)
for w := 0; w < workers; w++ {
start := (totalBlocks * w) / workers
end := (totalBlocks * (w + 1)) / workers
pool.jobs <- encodeJob{
start: start,
end: end,
bx: bx,
width: width,
height: height,
blockSize: blockSize,
format: format,
options: options,
rgba: rgba,
out: out,
wg: &wg,
}
}
wg.Wait()
return out, nil
}
pos := 0
for y := range by {
for x := range bx {
block := extractBlock(rgba, width, height, x, y)
switch format {
case FormatDXT1:
b := encodeBlockDXT1WithOptions(block, options)
copy(out[pos:pos+8], b[:])
pos += 8
case FormatDXT3:
b := encodeBlockDXT3WithOptions(block, options)
copy(out[pos:pos+16], b[:])
pos += 16
case FormatDXT5:
b := encodeBlockDXT5WithOptions(block, options)
copy(out[pos:pos+16], b[:])
pos += 16
case FormatBC4:
b := encodeBlockBC4(block, options, func(c rgba8) uint8 { return c.r })
copy(out[pos:pos+8], b[:])
pos += 8
case FormatBC5:
b := encodeBlockBC5(block, options)
copy(out[pos:pos+16], b[:])
pos += 16
default:
return nil, ErrUnsupportedFormat
}
}
}
return out, nil
}