@zappar/zappar-cv 0.3.14 → 0.4.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/additional.d.ts +2 -1
- package/lib/camera-source-map.d.ts +10 -0
- package/lib/camera-source-map.js +40 -0
- package/lib/camera-source.d.ts +3 -5
- package/lib/camera-source.js +8 -15
- package/lib/deserializer.d.ts +1 -0
- package/lib/deserializer.js +7 -0
- package/lib/drawcamera.js +2 -1
- package/lib/gen/zappar-client.d.ts +1 -0
- package/lib/gen/zappar-client.js +78 -41
- package/lib/gen/zappar-cwrap.js +71 -20
- package/lib/gen/zappar-native.d.ts +20 -3
- package/lib/gen/zappar-native.js +13 -1
- package/lib/gen/zappar-server.d.ts +3 -0
- package/lib/gen/zappar-server.js +70 -34
- package/lib/gen/zappar.d.ts +29 -1
- package/lib/gen/zappar.js +5 -3
- package/lib/gfx.d.ts +1 -0
- package/lib/gfx.js +4 -0
- package/lib/gl-state-manager.d.ts +9 -0
- package/lib/gl-state-manager.js +35 -0
- package/lib/html-element-source.d.ts +7 -22
- package/lib/html-element-source.js +30 -289
- package/lib/image-process-gl.d.ts +37 -0
- package/lib/image-process-gl.js +298 -0
- package/lib/imagebitmap-camera-source.d.ts +36 -0
- package/lib/imagebitmap-camera-source.js +277 -0
- package/lib/imagetracker.d.ts +28 -0
- package/lib/imagetracker.js +119 -0
- package/lib/index.d.ts +1 -1
- package/lib/mstp-camera-source.d.ts +29 -0
- package/lib/mstp-camera-source.js +230 -0
- package/lib/native.js +129 -6
- package/lib/pipeline.d.ts +22 -2
- package/lib/pipeline.js +140 -19
- package/lib/profile.d.ts +2 -0
- package/lib/profile.js +21 -6
- package/lib/riff-reader.d.ts +18 -0
- package/lib/riff-reader.js +56 -0
- package/lib/sequencerecorder.d.ts +83 -0
- package/lib/sequencerecorder.js +283 -0
- package/lib/sequencesource.d.ts +23 -0
- package/lib/sequencesource.js +170 -0
- package/lib/serializer.d.ts +1 -0
- package/lib/serializer.js +1 -0
- package/lib/source.d.ts +10 -3
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/worker-imagebitmap.d.ts +5 -0
- package/lib/worker-imagebitmap.js +63 -0
- package/lib/worker-server.js +124 -8
- package/lib/workerinterface.d.ts +50 -1
- package/lib/zcv.js +121 -197
- package/lib/zcv.wasm +0 -0
- package/package.json +6 -3
- package/umd/e9486c1cdadb21608a4f7e2fa3be9517.wasm +0 -0
- package/umd/zappar-cv.js +1 -1
- package/umd/zappar-cv.worker.js +1 -1
- package/umd/0bdbfe863a384bcd2935e7437d8f1153.wasm +0 -0
|
@@ -4,9 +4,9 @@ exports.HTMLElementSource = void 0;
|
|
|
4
4
|
const pipeline_1 = require("./pipeline");
|
|
5
5
|
const source_1 = require("./source");
|
|
6
6
|
const profile_1 = require("./profile");
|
|
7
|
-
const shader_1 = require("./shader");
|
|
8
7
|
const gl_matrix_1 = require("gl-matrix");
|
|
9
8
|
const loglevel_1 = require("./loglevel");
|
|
9
|
+
const image_process_gl_1 = require("./image-process-gl");
|
|
10
10
|
let latest = 1;
|
|
11
11
|
let byId = new Map();
|
|
12
12
|
class HTMLElementSource extends source_1.Source {
|
|
@@ -19,14 +19,10 @@ class HTMLElementSource extends source_1.Source {
|
|
|
19
19
|
this._isUserFacing = false;
|
|
20
20
|
this._cameraToScreenRotation = 0;
|
|
21
21
|
this._isUploadFrame = true;
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this._framebufferWidth = 0;
|
|
27
|
-
this._framebufferHeight = 0;
|
|
28
|
-
this._framebufferId = null;
|
|
29
|
-
this._renderTexture = null;
|
|
22
|
+
this._cameraToDeviceTransform = gl_matrix_1.mat4.create();
|
|
23
|
+
this._cameraToDeviceTransformUserFacing = gl_matrix_1.mat4.create();
|
|
24
|
+
this._cameraModel = new Float32Array([300, 300, 160, 120, 0, 0]);
|
|
25
|
+
gl_matrix_1.mat4.fromScaling(this._cameraToDeviceTransformUserFacing, [-1, 1, -1]);
|
|
30
26
|
let video = this._video;
|
|
31
27
|
if (this._video instanceof HTMLVideoElement) {
|
|
32
28
|
video.addEventListener("loadedmetadata", () => { this._hadFrames = true; });
|
|
@@ -49,12 +45,9 @@ class HTMLElementSource extends source_1.Source {
|
|
|
49
45
|
return byId.get(m);
|
|
50
46
|
}
|
|
51
47
|
_resetGLContext() {
|
|
48
|
+
var _a, _b;
|
|
52
49
|
this._currentVideoTexture = undefined;
|
|
53
|
-
this.
|
|
54
|
-
this._renderTexture = null;
|
|
55
|
-
this._vertexBuffer = undefined;
|
|
56
|
-
this._indexBuffer = undefined;
|
|
57
|
-
this._greyscaleShader = undefined;
|
|
50
|
+
(_b = (_a = this._imageProcessor) === null || _a === void 0 ? void 0 : _a.resetGLContext) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
58
51
|
}
|
|
59
52
|
destroy() {
|
|
60
53
|
let p = pipeline_1.Pipeline.get(this._pipeline);
|
|
@@ -95,7 +88,11 @@ class HTMLElementSource extends source_1.Source {
|
|
|
95
88
|
if (!this._hadFrames)
|
|
96
89
|
return;
|
|
97
90
|
try {
|
|
98
|
-
|
|
91
|
+
let info = this._processFrame(gl, this._cameraToScreenRotation, currentlyProcessing);
|
|
92
|
+
if (info) {
|
|
93
|
+
let token = pipeline.registerToken(info);
|
|
94
|
+
pipeline.sendDataToWorker(info.data || new ArrayBuffer(0), token, info.dataWidth, info.dataHeight, info.userFacing, info.cameraToDevice, info.cameraModel);
|
|
95
|
+
}
|
|
99
96
|
}
|
|
100
97
|
catch (ex) {
|
|
101
98
|
console.log("Unable to process frame");
|
|
@@ -106,298 +103,42 @@ class HTMLElementSource extends source_1.Source {
|
|
|
106
103
|
let pipeline = pipeline_1.Pipeline.get(this._pipeline);
|
|
107
104
|
if (!pipeline)
|
|
108
105
|
return undefined;
|
|
106
|
+
if (!this._imageProcessor)
|
|
107
|
+
this._imageProcessor = new image_process_gl_1.ImageProcessGL(gl);
|
|
109
108
|
if (this._isUploadFrame) {
|
|
110
109
|
if (!this._currentVideoTexture) {
|
|
111
110
|
this._currentVideoTexture = pipeline.getVideoTexture();
|
|
112
111
|
}
|
|
113
|
-
|
|
112
|
+
if (!this._currentVideoTexture)
|
|
113
|
+
return undefined;
|
|
114
|
+
this._imageProcessor.uploadFrame(this._currentVideoTexture, this._video, rotation, this._isUserFacing);
|
|
114
115
|
this._isUploadFrame = !this._isUploadFrame;
|
|
115
116
|
return undefined;
|
|
116
117
|
}
|
|
117
|
-
if (currentlyProcessing)
|
|
118
|
+
if (currentlyProcessing || !this._currentVideoTexture)
|
|
118
119
|
return undefined;
|
|
119
120
|
this._isUploadFrame = !this._isUploadFrame;
|
|
120
|
-
return this._readFrame(pipeline, gl);
|
|
121
|
-
}
|
|
122
|
-
_uploadFrame(rotation, fc) {
|
|
123
|
-
if (!this._currentVideoTexture)
|
|
124
|
-
return;
|
|
125
|
-
let pipeline = pipeline_1.Pipeline.get(this._pipeline);
|
|
126
|
-
if (!pipeline)
|
|
127
|
-
return;
|
|
128
|
-
let gl = pipeline.glContext;
|
|
129
|
-
if (!gl)
|
|
130
|
-
return;
|
|
131
|
-
gl.disable(gl.SCISSOR_TEST);
|
|
132
|
-
gl.disable(gl.DEPTH_TEST);
|
|
133
|
-
gl.disable(gl.BLEND);
|
|
134
|
-
gl.disable(gl.CULL_FACE);
|
|
135
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
136
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
137
|
-
gl.bindTexture(gl.TEXTURE_2D, this._currentVideoTexture);
|
|
138
|
-
const level = 0;
|
|
139
|
-
const internalFormat = gl.RGBA;
|
|
140
|
-
const srcFormat = gl.RGBA;
|
|
141
|
-
const srcType = gl.UNSIGNED_BYTE;
|
|
142
|
-
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, srcFormat, srcType, this._video);
|
|
143
|
-
let videoWidth = 0;
|
|
144
|
-
let videoHeight = 0;
|
|
145
|
-
if (this._video instanceof HTMLVideoElement) {
|
|
146
|
-
videoWidth = this._video.videoWidth;
|
|
147
|
-
videoHeight = this._video.videoHeight;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
videoWidth = this._video.width;
|
|
151
|
-
videoHeight = this._video.height;
|
|
152
|
-
}
|
|
153
|
-
if (videoHeight > videoWidth)
|
|
154
|
-
videoHeight = [videoWidth, videoWidth = videoHeight][0];
|
|
155
|
-
this._updateTransforms(rotation, fc);
|
|
156
|
-
let framebuffer = this._getFramebuffer(gl, profile_1.profile.dataWidth / 4, profile_1.profile.dataHeight);
|
|
157
|
-
let vbo = this._getVertexBuffer(gl);
|
|
158
|
-
let ibo = this._getIndexBuffer(gl);
|
|
159
|
-
let shader = this._getGreyscaleShader(gl);
|
|
160
|
-
// // Rendering to the greyscale conversion buffer - bind the framebuffer
|
|
161
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
162
|
-
gl.viewport(0, 0, this._framebufferWidth, this._framebufferHeight);
|
|
163
|
-
// // We'll be replacing all the content - clear is a good hint for this on mobile
|
|
164
|
-
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
|
165
|
-
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
166
|
-
// // Set up bindings for vertex attributes
|
|
167
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
168
|
-
gl.vertexAttribPointer(shader.aVertexPositionLoc, 2, gl.FLOAT, false, 4 * 4, 0);
|
|
169
|
-
gl.enableVertexAttribArray(shader.aVertexPositionLoc);
|
|
170
|
-
gl.vertexAttribPointer(shader.aTextureCoordLoc, 2, gl.FLOAT, false, 4 * 4, 2 * 4);
|
|
171
|
-
gl.enableVertexAttribArray(shader.aTextureCoordLoc);
|
|
172
|
-
// Bind the index buffer
|
|
173
|
-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);
|
|
174
|
-
// Tell WebGL to use our program when drawing
|
|
175
|
-
gl.useProgram(shader.program);
|
|
176
|
-
// Specify greyscale width for the correct offsets, and the uv transform
|
|
177
|
-
gl.uniform1f(shader.uTexWidthLoc, profile_1.profile.dataWidth);
|
|
178
|
-
gl.uniformMatrix4fv(shader.uUvTransformLoc, false, this._cameraUvTransform);
|
|
179
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
180
|
-
// Bind the texture to texture unit 0
|
|
181
|
-
gl.bindTexture(gl.TEXTURE_2D, this._currentVideoTexture);
|
|
182
|
-
// Tell the shader we bound the texture to texture unit 0
|
|
183
|
-
gl.uniform1i(shader.uSamplerLoc, 0);
|
|
184
|
-
// Do the drawing...
|
|
185
|
-
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
186
|
-
// Disable attribute arrays
|
|
187
|
-
gl.disableVertexAttribArray(shader.aVertexPositionLoc);
|
|
188
|
-
gl.disableVertexAttribArray(shader.aTextureCoordLoc);
|
|
189
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
190
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
191
|
-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
|
192
|
-
gl.useProgram(null);
|
|
193
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
194
|
-
}
|
|
195
|
-
_readFrame(p, gl) {
|
|
196
|
-
if (!this._currentVideoTexture)
|
|
197
|
-
throw new Error("No video texture");
|
|
198
|
-
let tex = this._currentVideoTexture;
|
|
199
|
-
this._currentVideoTexture = undefined;
|
|
200
121
|
let greySize = profile_1.profile.dataWidth * profile_1.profile.dataHeight;
|
|
201
|
-
let pixels =
|
|
122
|
+
let pixels = pipeline.cameraPixelArrays.pop();
|
|
202
123
|
while (pixels) {
|
|
203
124
|
if (pixels.byteLength === greySize)
|
|
204
125
|
break;
|
|
205
|
-
pixels =
|
|
126
|
+
pixels = pipeline.cameraPixelArrays.pop();
|
|
206
127
|
}
|
|
207
128
|
if (!pixels) {
|
|
208
129
|
pixels = new ArrayBuffer(greySize);
|
|
209
130
|
}
|
|
210
|
-
let
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
texture: tex,
|
|
219
|
-
dataWidth: profile_1.profile.dataWidth,
|
|
220
|
-
dataHeight: profile_1.profile.dataHeight,
|
|
221
|
-
userFacing: this._computedFrontCameraRotation
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
_updateTransforms(rot, fc) {
|
|
225
|
-
if (rot == this._computedTransformRotation && fc == this._computedFrontCameraRotation)
|
|
226
|
-
return;
|
|
227
|
-
this._computedTransformRotation = rot;
|
|
228
|
-
this._computedFrontCameraRotation = fc;
|
|
229
|
-
this._cameraUvTransform = this._getCameraUvTransform();
|
|
230
|
-
this._cameraVertexTransform = this._getCameraVertexTransform();
|
|
231
|
-
}
|
|
232
|
-
_getCameraUvTransform() {
|
|
233
|
-
switch (this._computedTransformRotation) {
|
|
234
|
-
case 270: return new Float32Array([0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]);
|
|
235
|
-
case 180: return new Float32Array([-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]);
|
|
236
|
-
case 90: return new Float32Array([0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]);
|
|
237
|
-
}
|
|
238
|
-
return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
|
|
239
|
-
}
|
|
240
|
-
_getCameraVertexTransform() {
|
|
241
|
-
let identity = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
242
|
-
if (!this._computedFrontCameraRotation)
|
|
243
|
-
return identity;
|
|
244
|
-
// It's a little odd there's only one special case here.
|
|
245
|
-
// This is because the camera rotation should really depend on
|
|
246
|
-
// isFrontCamera as well; we should aim to rotate the camera to
|
|
247
|
-
// process it in sensor-native orientation and then rotate and
|
|
248
|
-
// flip for rendering based on the screen orientation.
|
|
249
|
-
// For now I've kept with the rotation values calculated for the
|
|
250
|
-
// rear camera and correct the rendering of the front camera here
|
|
251
|
-
// for all rotations (tested on iPad which allows inverse
|
|
252
|
-
// portrait).
|
|
253
|
-
// TODO: Figure this out correctly. Probably need to do this to
|
|
254
|
-
// correctly use accelerometer / gyro data alongside vision data
|
|
255
|
-
// regardless of orientation
|
|
256
|
-
switch (this._computedTransformRotation) {
|
|
257
|
-
case 0:
|
|
258
|
-
case 90:
|
|
259
|
-
case 180:
|
|
260
|
-
identity[0] = -1;
|
|
261
|
-
break;
|
|
262
|
-
case 270:
|
|
263
|
-
identity[5] = -1;
|
|
264
|
-
break;
|
|
265
|
-
}
|
|
266
|
-
return identity;
|
|
267
|
-
}
|
|
268
|
-
_getFramebuffer(gl, fbWidth, fbHeight) {
|
|
269
|
-
if (this._framebufferWidth === fbWidth && this._framebufferHeight === fbHeight && this._framebufferId)
|
|
270
|
-
return this._framebufferId;
|
|
271
|
-
if (this._framebufferId) {
|
|
272
|
-
gl.deleteFramebuffer(this._framebufferId);
|
|
273
|
-
this._framebufferId = null;
|
|
274
|
-
}
|
|
275
|
-
if (this._renderTexture) {
|
|
276
|
-
gl.deleteTexture(this._renderTexture);
|
|
277
|
-
this._renderTexture = null;
|
|
278
|
-
}
|
|
279
|
-
this._framebufferId = gl.createFramebuffer();
|
|
280
|
-
if (!this._framebufferId)
|
|
281
|
-
throw new Error("Unable to create framebuffer");
|
|
282
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebufferId);
|
|
283
|
-
this._renderTexture = gl.createTexture();
|
|
284
|
-
if (!this._renderTexture)
|
|
285
|
-
throw new Error("Unable to create render texture");
|
|
286
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
287
|
-
gl.bindTexture(gl.TEXTURE_2D, this._renderTexture);
|
|
288
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, fbWidth, fbHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
289
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
290
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
291
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
292
|
-
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._renderTexture, 0);
|
|
293
|
-
let fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
294
|
-
if (fbStatus !== gl.FRAMEBUFFER_COMPLETE)
|
|
295
|
-
throw new Error("Framebuffer not complete: " + fbStatus.toString());
|
|
296
|
-
this._framebufferWidth = fbWidth;
|
|
297
|
-
this._framebufferHeight = fbHeight;
|
|
298
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
299
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
300
|
-
return this._framebufferId;
|
|
301
|
-
}
|
|
302
|
-
_getVertexBuffer(gl) {
|
|
303
|
-
if (this._vertexBuffer)
|
|
304
|
-
return this._vertexBuffer;
|
|
305
|
-
this._vertexBuffer = gl.createBuffer();
|
|
306
|
-
if (!this._vertexBuffer)
|
|
307
|
-
throw new Error("Unable to create vertex buffer");
|
|
308
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer);
|
|
309
|
-
let buffer = new Float32Array([-1.0, -1.0, 0.0, 0.0,
|
|
310
|
-
-1.0, 1.0, 0.0, 1.0,
|
|
311
|
-
1.0, 1.0, 1.0, 1.0,
|
|
312
|
-
1.0, -1.0, 1.0, 0.0]);
|
|
313
|
-
gl.bufferData(gl.ARRAY_BUFFER, buffer, gl.STATIC_DRAW);
|
|
314
|
-
return this._vertexBuffer;
|
|
315
|
-
}
|
|
316
|
-
_getIndexBuffer(gl) {
|
|
317
|
-
if (this._indexBuffer)
|
|
318
|
-
return this._indexBuffer;
|
|
319
|
-
this._indexBuffer = gl.createBuffer();
|
|
320
|
-
if (!this._indexBuffer)
|
|
321
|
-
throw new Error("Unable to create index buffer");
|
|
322
|
-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indexBuffer);
|
|
323
|
-
let buffer = new Uint16Array([0, 1, 2, 0, 2, 3]);
|
|
324
|
-
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer, gl.STATIC_DRAW);
|
|
325
|
-
return this._indexBuffer;
|
|
131
|
+
let tex = this._currentVideoTexture;
|
|
132
|
+
this._currentVideoTexture = undefined;
|
|
133
|
+
let focalLength = 300.0 * profile_1.profile.dataWidth / 320.0;
|
|
134
|
+
this._cameraModel[0] = focalLength;
|
|
135
|
+
this._cameraModel[1] = focalLength;
|
|
136
|
+
this._cameraModel[2] = profile_1.profile.dataWidth * 0.5;
|
|
137
|
+
this._cameraModel[3] = profile_1.profile.dataHeight * 0.5;
|
|
138
|
+
return Object.assign(Object.assign({}, this._imageProcessor.readFrame(tex, pixels)), { cameraModel: this._cameraModel, cameraSource: this, cameraToDevice: this._isUserFacing ? this._cameraToDeviceTransformUserFacing : this._cameraToDeviceTransform });
|
|
326
139
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
return this._greyscaleShader;
|
|
330
|
-
let prog = gl.createProgram();
|
|
331
|
-
if (!prog)
|
|
332
|
-
throw new Error("Unable to create program");
|
|
333
|
-
let vertexShader = shader_1.compileShader(gl, gl.VERTEX_SHADER, greyscaleVsSource);
|
|
334
|
-
let fragmentShader = shader_1.compileShader(gl, gl.FRAGMENT_SHADER, greyscaleFsSource);
|
|
335
|
-
gl.attachShader(prog, vertexShader);
|
|
336
|
-
gl.attachShader(prog, fragmentShader);
|
|
337
|
-
shader_1.linkProgram(gl, prog);
|
|
338
|
-
let uTexWidthLoc = gl.getUniformLocation(prog, "uTexWidth");
|
|
339
|
-
if (!uTexWidthLoc)
|
|
340
|
-
throw new Error("Unable to get uniform location uTexWidth");
|
|
341
|
-
let uUvTransformLoc = gl.getUniformLocation(prog, "uUvTransform");
|
|
342
|
-
if (!uUvTransformLoc)
|
|
343
|
-
throw new Error("Unable to get uniform location uUvTransform");
|
|
344
|
-
let uSamplerLoc = gl.getUniformLocation(prog, "uSampler");
|
|
345
|
-
if (!uSamplerLoc)
|
|
346
|
-
throw new Error("Unable to get uniform location uSampler");
|
|
347
|
-
this._greyscaleShader = {
|
|
348
|
-
program: prog,
|
|
349
|
-
aVertexPositionLoc: gl.getAttribLocation(prog, "aVertexPosition"),
|
|
350
|
-
aTextureCoordLoc: gl.getAttribLocation(prog, "aTextureCoord"),
|
|
351
|
-
uTexWidthLoc: uTexWidthLoc,
|
|
352
|
-
uUvTransformLoc: uUvTransformLoc,
|
|
353
|
-
uSamplerLoc: uSamplerLoc
|
|
354
|
-
};
|
|
355
|
-
return this._greyscaleShader;
|
|
140
|
+
uploadGL() {
|
|
141
|
+
// No-op as already uploaded
|
|
356
142
|
}
|
|
357
143
|
}
|
|
358
144
|
exports.HTMLElementSource = HTMLElementSource;
|
|
359
|
-
let greyscaleVsSource = `
|
|
360
|
-
attribute vec4 aVertexPosition;
|
|
361
|
-
attribute vec2 aTextureCoord;
|
|
362
|
-
|
|
363
|
-
varying highp vec2 vTextureCoord1;
|
|
364
|
-
varying highp vec2 vTextureCoord2;
|
|
365
|
-
varying highp vec2 vTextureCoord3;
|
|
366
|
-
varying highp vec2 vTextureCoord4;
|
|
367
|
-
|
|
368
|
-
uniform float uTexWidth;
|
|
369
|
-
uniform mat4 uUvTransform;
|
|
370
|
-
|
|
371
|
-
void main(void) {
|
|
372
|
-
highp vec2 offset1 = vec2(1.5 / uTexWidth, 0);
|
|
373
|
-
highp vec2 offset2 = vec2(0.5 / uTexWidth, 0);
|
|
374
|
-
|
|
375
|
-
gl_Position = aVertexPosition;
|
|
376
|
-
vTextureCoord1 = (uUvTransform * vec4(aTextureCoord - offset1, 0, 1)).xy;
|
|
377
|
-
vTextureCoord2 = (uUvTransform * vec4(aTextureCoord - offset2, 0, 1)).xy;
|
|
378
|
-
vTextureCoord3 = (uUvTransform * vec4(aTextureCoord + offset2, 0, 1)).xy;
|
|
379
|
-
vTextureCoord4 = (uUvTransform * vec4(aTextureCoord + offset1, 0, 1)).xy;
|
|
380
|
-
}
|
|
381
|
-
`;
|
|
382
|
-
// Fragment shader program
|
|
383
|
-
let greyscaleFsSource = `
|
|
384
|
-
varying highp vec2 vTextureCoord1;
|
|
385
|
-
varying highp vec2 vTextureCoord2;
|
|
386
|
-
varying highp vec2 vTextureCoord3;
|
|
387
|
-
varying highp vec2 vTextureCoord4;
|
|
388
|
-
|
|
389
|
-
uniform sampler2D uSampler;
|
|
390
|
-
|
|
391
|
-
const lowp vec3 colorWeights = vec3(77.0 / 256.0, 150.0 / 256.0, 29.0 / 256.0);
|
|
392
|
-
|
|
393
|
-
void main(void) {
|
|
394
|
-
lowp vec4 outpx;
|
|
395
|
-
|
|
396
|
-
outpx.r = dot(colorWeights, texture2D(uSampler, vTextureCoord1).xyz);
|
|
397
|
-
outpx.g = dot(colorWeights, texture2D(uSampler, vTextureCoord2).xyz);
|
|
398
|
-
outpx.b = dot(colorWeights, texture2D(uSampler, vTextureCoord3).xyz);
|
|
399
|
-
outpx.a = dot(colorWeights, texture2D(uSampler, vTextureCoord4).xyz);
|
|
400
|
-
|
|
401
|
-
gl_FragColor = outpx;
|
|
402
|
-
}
|
|
403
|
-
`;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface ProcessedFrame {
|
|
2
|
+
data?: ArrayBuffer;
|
|
3
|
+
texture: WebGLTexture | undefined;
|
|
4
|
+
dataWidth: number;
|
|
5
|
+
dataHeight: number;
|
|
6
|
+
uvTransform?: Float32Array;
|
|
7
|
+
userFacing: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare class ImageProcessGL {
|
|
10
|
+
private _gl;
|
|
11
|
+
protected _isPaused: boolean;
|
|
12
|
+
protected _hadFrames: boolean;
|
|
13
|
+
protected _isUserFacing: boolean;
|
|
14
|
+
protected _cameraToScreenRotation: number;
|
|
15
|
+
private _isUploadFrame;
|
|
16
|
+
private _computedTransformRotation;
|
|
17
|
+
private _computedFrontCameraRotation;
|
|
18
|
+
private _cameraUvTransform;
|
|
19
|
+
private _framebufferWidth;
|
|
20
|
+
private _framebufferHeight;
|
|
21
|
+
private _framebufferId;
|
|
22
|
+
private _renderTexture;
|
|
23
|
+
private _vertexBuffer;
|
|
24
|
+
private _indexBuffer;
|
|
25
|
+
private _greyscaleShader;
|
|
26
|
+
constructor(_gl: WebGLRenderingContext);
|
|
27
|
+
resetGLContext(): void;
|
|
28
|
+
destroy(): void;
|
|
29
|
+
uploadFrame(texture: WebGLTexture, img: HTMLVideoElement | HTMLImageElement | ImageBitmap, rotation: number, fc: boolean): void;
|
|
30
|
+
readFrame(texture: WebGLTexture, pixels: ArrayBuffer): ProcessedFrame;
|
|
31
|
+
private _updateTransforms;
|
|
32
|
+
private _getCameraUvTransform;
|
|
33
|
+
private _getFramebuffer;
|
|
34
|
+
private _getVertexBuffer;
|
|
35
|
+
private _getIndexBuffer;
|
|
36
|
+
private _getGreyscaleShader;
|
|
37
|
+
}
|