-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpuFilters.js
302 lines (235 loc) · 9.25 KB
/
gpuFilters.js
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
import * as FILTERS from "./filters.js"
const gpu = new GPU()
///////////////////////////////////////////////////////////////////////////////
// General Utility Functions
///////////////////////////////////////////////////////////////////////////////
export function applyFilters(filters, imageData) {
const numFilters = filters.length
let data = imageData.data
for (let i = 0; i < numFilters; i += 1) {
const [ filter, params ] = filters[i]
filter.setOutput([ imageData.width, imageData.height ])
filter.setConstants({
width: imageData.width,
height: imageData.height,
})
// gpu.js doesn't like when params is an empty array, so make it non-empty.
filter(data, params.length ? params : [0])
data = filter.getPixels()
}
return new ImageData(data, imageData.width, imageData.height)
}
///////////////////////////////////////////////////////////////////////////////
// Kernel Utility Functions
///////////////////////////////////////////////////////////////////////////////
// The "this" in these functions will resolve to the kernel that calls it.
function getContext () {
const col = this.thread.x
const row = this.thread.y
// Convert position to index.
const i = 4 * (col + this.constants.width * (this.constants.height - 1 - row))
return [ i, col, row ]
}
function setColor (pixel) {
const [ r, g, b, a ] = pixel
this.color(r / 255, g / 255, b / 255, a / 255)
}
///////////////////////////////////////////////////////////////////////////////
// GPU-agnostic Filter Functions
///////////////////////////////////////////////////////////////////////////////
// Threshold Filter
export const thresholdFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(thresholdFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.calcPixelBrightness, FILTERS.thresholdFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Brightness Filter
export const brightnessFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(brightnessFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.brightnessFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Channel Filter
export const channelFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(channelFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.channelFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Color Gain Filter
export const colorGainFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(colorGainFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.colorGainFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Color Reducer Filter
export const colorReducerFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(colorReducerFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.colorReducerFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Row Blanker Filter
export const rowBlankerFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(rowBlankerFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.rowBlankerFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Col Blanker Filter
export const colBlankerFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(colBlankerFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.colBlankerFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Invert Filter
export const invertFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(invertFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.invertFilter ])
.setDynamicOutput(true)
.setGraphical(true)
// Y-Plot Filter
export const audioPlotFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(audioPlotFilter(pixel, params, i, col, row, 0, width, height))
})
.setFunctions([ getContext, setColor, FILTERS.clamp8, FILTERS.audioPlotFilter ])
.setDynamicOutput(true)
.setGraphical(true)
///////////////////////////////////////////////////////////////////////////////
//
// GPU-specific Filter Functions
//
// These functions must have their logic implemented in the GPU kernel function
// instead of leveraging FILTERS.* functions because they require access to
// the entire `data` array, and it's not currently possible to pass this array
// as an argument to an internal function.
// See: https://github.com/gpujs/gpu.js/issues/394
//
///////////////////////////////////////////////////////////////////////////////
// Flip Horizontal Filter
export const flipHorizontalFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
i = 4 * (width - 1 - col + width * (height - row))
const pixel = [ data[i], data[i + 1], data[i + 2], data[i + 3] ]
setColor(pixel)
})
.setFunctions([ getContext, setColor ])
.setDynamicOutput(true)
.setGraphical(true)
// Vertical Mirror Filter
export const verticalMirrorFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const middleRow = Math.floor(height / 2)
if (row >= middleRow) {
i = 4 * (col + this.constants.width * row)
}
const pixel = [ data[i], data[i + 1], data[i + 2], data[i + 3] ]
setColor(pixel)
})
.setFunctions([ getContext, setColor ])
.setDynamicOutput(true)
.setGraphical(true)
// Horizontal Mirror Filter
export const horizontalMirrorFilter = gpu.createKernel(function (data, params) {
const { width, height } = this.constants
const [ i, col, row ] = getContext()
const middleCol = Math.floor(width / 2)
if (col >= middleCol) {
i = 4 * (middleCol - col - middleCol + this.constants.width * (this.constants.height - 1 - row))
}
const pixel = [ data[i], data[i + 1], data[i + 2], data[i + 3] ]
setColor(pixel)
})
.setFunctions([ getContext, setColor ])
.setDynamicOutput(true)
.setGraphical(true)
// Blur Filter
export const blurFilter = gpu.createKernel(function (data, params) {
const [ level ] = params
const { width, height } = this.constants
let [ i, col, row ] = getContext()
let pixel = [ data[i], data[i + 1], data[i + 2], data[i + 3] ]
for (let lev = level; lev > 0; lev -= 1) {
let nCol = col - lev
if (nCol >= 0 && nCol < width) {
i = convertPositionToIndex(nCol, row, width, height)
pixel[0] += data[i]
pixel[1] += data[i + 1]
pixel[2] += data[i + 2]
pixel[3] += data[i + 3]
}
nCol = col + lev
if (nCol >= 0 && nCol < width) {
i = convertPositionToIndex(nCol, row, width, height)
pixel[0] += data[i]
pixel[1] += data[i + 1]
pixel[2] += data[i + 2]
pixel[3] += data[i + 3]
}
}
const numPixels = level * 2 + 1
pixel = [
Math.floor(pixel[0] / numPixels),
Math.floor(pixel[1] / numPixels),
Math.floor(pixel[2] / numPixels),
Math.floor(pixel[3] / numPixels),
]
setColor(pixel)
})
.setFunctions([ getContext, setColor, FILTERS.convertPositionToIndex ])
.setDynamicOutput(true)
.setGraphical(true)
// Pan + Zoom Filter
export const panZoomFilter = gpu.createKernel(function (data, params) {
const [ x, y, zoom ] = params
const { width, height } = this.constants
let [ i, col, row ] = getContext()
col = Math.floor((col + x) / zoom + (width - width / zoom) / 2)
if (col >= width) {
col -= width
}
row = Math.floor((row + y) / zoom + (height - height / zoom) / 2)
if (row >= height) {
row -= height
}
i = convertPositionToIndex(col, row, width, height)
const pixel = [ data[i], data[i+1], data[i+2], data[i+3] ]
setColor(pixel)
})
.setFunctions([ getContext, setColor, FILTERS.convertPositionToIndex ])
.setDynamicOutput(true)
.setGraphical(true)