@zappar/zappar-cv 0.3.13 → 0.4.0-beta.10

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +2 -2
  3. package/lib/additional.d.ts +2 -1
  4. package/lib/camera-source-map.d.ts +10 -0
  5. package/lib/camera-source-map.js +40 -0
  6. package/lib/camera-source.d.ts +3 -5
  7. package/lib/camera-source.js +8 -15
  8. package/lib/deserializer.d.ts +1 -0
  9. package/lib/deserializer.js +7 -0
  10. package/lib/drawcamera.js +2 -1
  11. package/lib/gen/zappar-client.d.ts +1 -0
  12. package/lib/gen/zappar-client.js +78 -41
  13. package/lib/gen/zappar-cwrap.js +71 -20
  14. package/lib/gen/zappar-native.d.ts +20 -3
  15. package/lib/gen/zappar-native.js +13 -1
  16. package/lib/gen/zappar-server.d.ts +3 -0
  17. package/lib/gen/zappar-server.js +70 -34
  18. package/lib/gen/zappar.d.ts +29 -1
  19. package/lib/gen/zappar.js +5 -3
  20. package/lib/gfx.d.ts +1 -0
  21. package/lib/gfx.js +4 -0
  22. package/lib/gl-state-manager.d.ts +9 -0
  23. package/lib/gl-state-manager.js +35 -0
  24. package/lib/html-element-source.d.ts +7 -22
  25. package/lib/html-element-source.js +30 -289
  26. package/lib/image-process-gl.d.ts +37 -0
  27. package/lib/image-process-gl.js +298 -0
  28. package/lib/imagebitmap-camera-source.d.ts +36 -0
  29. package/lib/imagebitmap-camera-source.js +277 -0
  30. package/lib/imagetracker.d.ts +28 -0
  31. package/lib/imagetracker.js +119 -0
  32. package/lib/index.d.ts +1 -1
  33. package/lib/mstp-camera-source.d.ts +29 -0
  34. package/lib/mstp-camera-source.js +230 -0
  35. package/lib/native.js +129 -6
  36. package/lib/pipeline.d.ts +22 -2
  37. package/lib/pipeline.js +140 -19
  38. package/lib/profile.d.ts +2 -0
  39. package/lib/profile.js +21 -6
  40. package/lib/riff-reader.d.ts +18 -0
  41. package/lib/riff-reader.js +56 -0
  42. package/lib/sequencerecorder.d.ts +83 -0
  43. package/lib/sequencerecorder.js +283 -0
  44. package/lib/sequencesource.d.ts +23 -0
  45. package/lib/sequencesource.js +170 -0
  46. package/lib/serializer.d.ts +1 -0
  47. package/lib/serializer.js +1 -0
  48. package/lib/source.d.ts +10 -3
  49. package/lib/version.d.ts +1 -1
  50. package/lib/version.js +1 -1
  51. package/lib/worker-imagebitmap.d.ts +5 -0
  52. package/lib/worker-imagebitmap.js +63 -0
  53. package/lib/worker-server.js +124 -8
  54. package/lib/workerinterface.d.ts +50 -1
  55. package/lib/zcv.js +121 -197
  56. package/lib/zcv.wasm +0 -0
  57. package/package.json +34 -31
  58. package/umd/e9486c1cdadb21608a4f7e2fa3be9517.wasm +0 -0
  59. package/umd/zappar-cv.js +1 -9
  60. package/umd/zappar-cv.worker.js +1 -1
  61. package/umd/0bdbfe863a384bcd2935e7437d8f1153.wasm +0 -0
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SequenceDecoder = exports.SequenceRecorder = exports.SequenceRecorderPacketType = void 0;
4
+ const loglevel_1 = require("./loglevel");
5
+ var SequenceRecorderPacketType;
6
+ (function (SequenceRecorderPacketType) {
7
+ SequenceRecorderPacketType[SequenceRecorderPacketType["CAMERA_FRAME"] = 0] = "CAMERA_FRAME";
8
+ SequenceRecorderPacketType[SequenceRecorderPacketType["ACCELEROMETER"] = 1] = "ACCELEROMETER";
9
+ SequenceRecorderPacketType[SequenceRecorderPacketType["ROTATION_RATE"] = 2] = "ROTATION_RATE";
10
+ SequenceRecorderPacketType[SequenceRecorderPacketType["ATTITUDE"] = 3] = "ATTITUDE";
11
+ SequenceRecorderPacketType[SequenceRecorderPacketType["ATTITUDE_MATRIX"] = 4] = "ATTITUDE_MATRIX";
12
+ })(SequenceRecorderPacketType = exports.SequenceRecorderPacketType || (exports.SequenceRecorderPacketType = {}));
13
+ class SequenceRecorder {
14
+ constructor(estimatedFrames) {
15
+ this._insertionByte = 0;
16
+ this._numberPackets = 0;
17
+ this._started = false;
18
+ this._hasLoggedUnsuppored = false;
19
+ this._data = new ArrayBuffer(this._estimatedSize(estimatedFrames, 320, 240));
20
+ this._dataView = new DataView(this._data);
21
+ this._dataUint8 = new Uint8Array(this._data);
22
+ this._dataFloat32 = new Float32Array(this._data);
23
+ }
24
+ _estimatedSize(frames, cameraFrameWidth, cameraFrameHeight) {
25
+ return SequenceRecorder.headerSizeBytes + (SequenceRecorder.psbPacketType + SequenceRecorder.cameraPacketSizeBytes + cameraFrameWidth * cameraFrameHeight) * frames;
26
+ }
27
+ start() {
28
+ this._started = true;
29
+ this._insertionByte = 0;
30
+ this._numberPackets = 0;
31
+ this._growArrayBuffer(SequenceRecorder.headerSizeBytes);
32
+ let enc = new TextEncoder();
33
+ let headerData = enc.encode("UAR1");
34
+ this._dataUint8.set(headerData);
35
+ this._dataView.setUint32(4, 0, true);
36
+ this._insertionByte += 8;
37
+ }
38
+ stop() {
39
+ this._started = false;
40
+ let trimmed = this._data.slice(0, this._insertionByte);
41
+ this._data = trimmed;
42
+ this._dataUint8 = new Uint8Array(this._data);
43
+ this._dataFloat32 = new Float32Array(this._data);
44
+ this._dataView = new DataView(this._data);
45
+ }
46
+ data() {
47
+ return this._dataUint8;
48
+ }
49
+ appendCameraFrame(c) {
50
+ if (!this._started)
51
+ return;
52
+ if (!c.data) {
53
+ if (this._hasLoggedUnsuppored)
54
+ return;
55
+ this._hasLoggedUnsuppored = true;
56
+ loglevel_1.zcerr("Unable to record frames from this camera source");
57
+ return;
58
+ }
59
+ if (c.data.byteLength !== (c.dataWidth * c.dataHeight)) {
60
+ loglevel_1.zcerr("Unable to record frame to sequence: greyscale array doesn't match dimensions");
61
+ return;
62
+ }
63
+ let increment = SequenceRecorder.psbPacketType + SequenceRecorder.cameraPacketSizeBytes + (c.dataWidth * c.dataHeight);
64
+ this._growArrayBuffer(increment);
65
+ this._dataView.setUint32(this._insertionByte, SequenceRecorderPacketType.CAMERA_FRAME, true);
66
+ this._insertionByte += SequenceRecorder.psbPacketType;
67
+ this._dataView.setUint32(this._insertionByte, c.dataWidth, true);
68
+ this._insertionByte += SequenceRecorder.psbCameraFrameWidth;
69
+ this._dataView.setUint32(this._insertionByte, c.dataHeight, true);
70
+ this._insertionByte += SequenceRecorder.psbCameraFrameHeight;
71
+ this._dataFloat32.set(c.cameraToDevice, this._insertionByte / 4);
72
+ this._insertionByte += SequenceRecorder.psbCameraToDeviceTransform;
73
+ this._dataFloat32.set(c.cameraModel, this._insertionByte / 4);
74
+ this._insertionByte += SequenceRecorder.psbCameraModel;
75
+ this._dataView.setUint32(this._insertionByte, c.userFacing ? 1 : 0, true);
76
+ this._insertionByte += SequenceRecorder.psbFlags;
77
+ this._dataUint8.set(new Uint8Array(c.data), this._insertionByte);
78
+ this._insertionByte += c.data.byteLength;
79
+ this._numberPackets++;
80
+ this._dataView.setUint32(4, this._numberPackets, true);
81
+ }
82
+ appendAccelerometer(t, x, y, z) {
83
+ this._appendTimestampedVec3(SequenceRecorderPacketType.ACCELEROMETER, t, x, y, z);
84
+ }
85
+ appendRotationRate(t, x, y, z) {
86
+ this._appendTimestampedVec3(SequenceRecorderPacketType.ROTATION_RATE, t, x, y, z);
87
+ }
88
+ appendAttitude(t, x, y, z) {
89
+ this._appendTimestampedVec3(SequenceRecorderPacketType.ATTITUDE, t, x, y, z);
90
+ }
91
+ _appendTimestampedVec3(type, t, x, y, z) {
92
+ if (!this._started)
93
+ return;
94
+ let increment = SequenceRecorder.psbPacketType + SequenceRecorder.accelerometerPacketSizeBytes;
95
+ this._growArrayBuffer(increment);
96
+ this._dataView.setUint32(this._insertionByte, type, true);
97
+ this._insertionByte += SequenceRecorder.psbPacketType;
98
+ this._dataView.setUint32(this._insertionByte, t, true);
99
+ this._insertionByte += 4;
100
+ this._dataView.setFloat32(this._insertionByte, x, true);
101
+ this._insertionByte += 4;
102
+ this._dataView.setFloat32(this._insertionByte, y, true);
103
+ this._insertionByte += 4;
104
+ this._dataView.setFloat32(this._insertionByte, z, true);
105
+ this._insertionByte += 4;
106
+ this._numberPackets++;
107
+ this._dataView.setUint32(4, this._numberPackets, true);
108
+ }
109
+ appendAttitudeMatrix(m) {
110
+ if (!this._started)
111
+ return;
112
+ let increment = SequenceRecorder.psbPacketType + SequenceRecorder.attitudeMatrixPacketSizeBytes;
113
+ this._growArrayBuffer(increment);
114
+ this._dataView.setUint32(this._insertionByte, SequenceRecorderPacketType.ATTITUDE_MATRIX, true);
115
+ this._insertionByte += SequenceRecorder.psbPacketType;
116
+ this._dataFloat32.set(m, this._insertionByte / 4);
117
+ this._insertionByte += SequenceRecorder.attitudeMatrixPacketSizeBytes;
118
+ this._numberPackets++;
119
+ this._dataView.setUint32(4, this._numberPackets, true);
120
+ }
121
+ _growArrayBuffer(requiredIncrement) {
122
+ while (this._insertionByte + requiredIncrement > this._data.byteLength) {
123
+ let newSize = Math.max(this._data.byteLength * 2, this._estimatedSize(50, 320, 240));
124
+ let newData = new ArrayBuffer(newSize);
125
+ let newDataUint8 = new Uint8Array(newData);
126
+ newDataUint8.set(this._dataUint8);
127
+ this._data = newData;
128
+ this._dataUint8 = newDataUint8;
129
+ this._dataFloat32 = new Float32Array(this._data);
130
+ this._dataView = new DataView(this._data);
131
+ }
132
+ }
133
+ }
134
+ exports.SequenceRecorder = SequenceRecorder;
135
+ // Header (8 bytes): "UAR1" + number of frames (uint32)
136
+ SequenceRecorder.headerSizeBytes = 8;
137
+ // Per-frame packet byte sizes
138
+ SequenceRecorder.psbPacketType = 4;
139
+ // Camera frames
140
+ SequenceRecorder.psbCameraFrameWidth = 4;
141
+ SequenceRecorder.psbCameraFrameHeight = 4;
142
+ SequenceRecorder.psbFlags = 4;
143
+ SequenceRecorder.psbCameraToDeviceTransform = 4 * 16;
144
+ SequenceRecorder.psbCameraModel = 4 * 6;
145
+ SequenceRecorder.cameraPacketSizeBytes = SequenceRecorder.psbCameraFrameWidth +
146
+ SequenceRecorder.psbCameraFrameHeight +
147
+ SequenceRecorder.psbCameraToDeviceTransform +
148
+ SequenceRecorder.psbCameraModel +
149
+ SequenceRecorder.psbFlags;
150
+ // Accelerometer
151
+ SequenceRecorder.accelerometerPacketSizeBytes = 4 * 4; // t, x, y, z
152
+ // Rotation rate
153
+ SequenceRecorder.rotationRatePacketSizeBytes = 4 * 4; // t, x, y, z
154
+ // Attitude
155
+ SequenceRecorder.attitudePacketSizeBytes = 4 * 4; // t, x, y, z
156
+ // Attitude matrix
157
+ SequenceRecorder.attitudeMatrixPacketSizeBytes = 4 * 16;
158
+ class SequenceDecoder {
159
+ constructor(_data) {
160
+ this._data = _data;
161
+ this.dataByPacket = new Map();
162
+ this.dataByCameraFrame = new Map();
163
+ this.numberPackets = 0;
164
+ this.numberCameraFrames = 0;
165
+ this.numberAccelerometerPackets = 0;
166
+ this.numberRotationRatePackets = 0;
167
+ this.numberAttitudePackets = 0;
168
+ this.numberAttitudeMatrixPackets = 0;
169
+ let dec = new TextDecoder();
170
+ let txt = "";
171
+ try {
172
+ txt = dec.decode(this._data.slice(0, 4));
173
+ }
174
+ catch (err) {
175
+ throw new Error("Unable to decode header - perhaps this isn't a sequence file?");
176
+ }
177
+ if (txt !== "UAR1") {
178
+ throw new Error(`Invalid - perhaps this isn't a sequence file: ${txt}`);
179
+ }
180
+ let readPoint = 0;
181
+ let dataView = new DataView(this._data);
182
+ this.numberPackets = dataView.getUint32(4, true);
183
+ readPoint += SequenceRecorder.headerSizeBytes;
184
+ for (let i = 0; i < this.numberPackets; i++) {
185
+ let type = dataView.getUint32(readPoint, true);
186
+ readPoint += SequenceRecorder.psbPacketType;
187
+ switch (type) {
188
+ case SequenceRecorderPacketType.CAMERA_FRAME:
189
+ {
190
+ let width = dataView.getUint32(readPoint, true);
191
+ readPoint += SequenceRecorder.psbCameraFrameWidth;
192
+ let height = dataView.getUint32(readPoint, true);
193
+ readPoint += SequenceRecorder.psbCameraFrameHeight;
194
+ let cameraToDevice = new Float32Array(this._data, readPoint, SequenceRecorder.psbCameraToDeviceTransform / 4);
195
+ readPoint += SequenceRecorder.psbCameraToDeviceTransform;
196
+ let cameraModel = new Float32Array(this._data, readPoint, SequenceRecorder.psbCameraModel / 4);
197
+ readPoint += SequenceRecorder.psbCameraModel;
198
+ let flags = dataView.getUint32(readPoint, true);
199
+ readPoint += SequenceRecorder.psbFlags;
200
+ let userFacing = flags === 1;
201
+ let greyscale = new Uint8Array(this._data, readPoint, width * height);
202
+ readPoint += width * height;
203
+ let d = {
204
+ type: SequenceRecorderPacketType.CAMERA_FRAME,
205
+ width,
206
+ height,
207
+ cameraToDevice,
208
+ cameraModel,
209
+ userFacing,
210
+ greyscale
211
+ };
212
+ this.dataByPacket.set(i, d);
213
+ this.dataByCameraFrame.set(this.numberCameraFrames, d);
214
+ this.numberCameraFrames++;
215
+ break;
216
+ }
217
+ case SequenceRecorderPacketType.ACCELEROMETER:
218
+ {
219
+ let t = dataView.getUint32(readPoint, true);
220
+ readPoint += 4;
221
+ let x = dataView.getFloat32(readPoint, true);
222
+ readPoint += 4;
223
+ let y = dataView.getFloat32(readPoint, true);
224
+ readPoint += 4;
225
+ let z = dataView.getFloat32(readPoint, true);
226
+ readPoint += 4;
227
+ this.dataByPacket.set(i, {
228
+ type: SequenceRecorderPacketType.ACCELEROMETER,
229
+ t, x, y, z
230
+ });
231
+ this.numberAccelerometerPackets++;
232
+ break;
233
+ }
234
+ case SequenceRecorderPacketType.ROTATION_RATE:
235
+ {
236
+ let t = dataView.getUint32(readPoint, true);
237
+ readPoint += 4;
238
+ let x = dataView.getFloat32(readPoint, true);
239
+ readPoint += 4;
240
+ let y = dataView.getFloat32(readPoint, true);
241
+ readPoint += 4;
242
+ let z = dataView.getFloat32(readPoint, true);
243
+ readPoint += 4;
244
+ this.dataByPacket.set(i, {
245
+ type: SequenceRecorderPacketType.ROTATION_RATE,
246
+ t, x, y, z
247
+ });
248
+ this.numberRotationRatePackets++;
249
+ break;
250
+ }
251
+ case SequenceRecorderPacketType.ATTITUDE:
252
+ {
253
+ let t = dataView.getUint32(readPoint, true);
254
+ readPoint += 4;
255
+ let x = dataView.getFloat32(readPoint, true);
256
+ readPoint += 4;
257
+ let y = dataView.getFloat32(readPoint, true);
258
+ readPoint += 4;
259
+ let z = dataView.getFloat32(readPoint, true);
260
+ readPoint += 4;
261
+ this.dataByPacket.set(i, {
262
+ type: SequenceRecorderPacketType.ATTITUDE,
263
+ t, x, y, z
264
+ });
265
+ this.numberAttitudePackets++;
266
+ break;
267
+ }
268
+ case SequenceRecorderPacketType.ATTITUDE_MATRIX:
269
+ {
270
+ let attitude = new Float32Array(this._data, readPoint, SequenceRecorder.attitudeMatrixPacketSizeBytes / 4);
271
+ readPoint += SequenceRecorder.attitudeMatrixPacketSizeBytes;
272
+ this.dataByPacket.set(i, {
273
+ type: SequenceRecorderPacketType.ATTITUDE_MATRIX,
274
+ attitude
275
+ });
276
+ this.numberAttitudeMatrixPackets++;
277
+ break;
278
+ }
279
+ }
280
+ }
281
+ }
282
+ }
283
+ exports.SequenceDecoder = SequenceDecoder;
@@ -0,0 +1,23 @@
1
+ import { CameraFrameInfo, Source } from "./source";
2
+ import { zappar_sequence_source_t, zappar_pipeline_t } from "./gen/zappar";
3
+ export declare class SequenceSource extends Source {
4
+ private _impl;
5
+ private _pipeline;
6
+ private _packetNumber;
7
+ private _motionUpdatesQueued;
8
+ private _motionUpdatesSent;
9
+ private _decoder;
10
+ private _minFrameGapMs;
11
+ private _lastUpdateTime;
12
+ static create(p: zappar_pipeline_t): zappar_sequence_source_t;
13
+ static get(m: zappar_sequence_source_t): SequenceSource | undefined;
14
+ constructor(_impl: zappar_sequence_source_t, _pipeline: zappar_pipeline_t);
15
+ loadFromMemory(data: ArrayBuffer): void;
16
+ maxPlaybackFpsSet(fps: number): void;
17
+ getFrame(currentlyProcessing: boolean): void;
18
+ destroy(): void;
19
+ start(): void;
20
+ pause(): void;
21
+ uploadGL(info: CameraFrameInfo): void;
22
+ private _submitMotionUpdates;
23
+ }
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SequenceSource = void 0;
4
+ const loglevel_1 = require("./loglevel");
5
+ const sequencerecorder_1 = require("./sequencerecorder");
6
+ const source_1 = require("./source");
7
+ const pipeline_1 = require("./pipeline");
8
+ const gl_matrix_1 = require("gl-matrix");
9
+ let latest = 1;
10
+ let byId = new Map();
11
+ class SequenceSource extends source_1.Source {
12
+ constructor(_impl, _pipeline) {
13
+ super();
14
+ this._impl = _impl;
15
+ this._pipeline = _pipeline;
16
+ this._packetNumber = 0;
17
+ this._motionUpdatesQueued = 0;
18
+ this._motionUpdatesSent = 0;
19
+ this._minFrameGapMs = 0;
20
+ this._lastUpdateTime = 0;
21
+ }
22
+ static create(p) {
23
+ let ret = (latest++);
24
+ byId.set(ret, new SequenceSource(ret, p));
25
+ loglevel_1.zcout("sequence_source_source_t initialized");
26
+ return ret;
27
+ }
28
+ static get(m) {
29
+ return byId.get(m);
30
+ }
31
+ loadFromMemory(data) {
32
+ try {
33
+ this._decoder = new sequencerecorder_1.SequenceDecoder(data);
34
+ this._packetNumber = 0;
35
+ this._lastUpdateTime = 0;
36
+ }
37
+ catch (err) {
38
+ loglevel_1.zcerr("Unable to decode sequence:", err);
39
+ }
40
+ }
41
+ maxPlaybackFpsSet(fps) {
42
+ if (fps <= 0) {
43
+ this._minFrameGapMs = 0;
44
+ return;
45
+ }
46
+ this._minFrameGapMs = 1000.0 / fps;
47
+ }
48
+ getFrame(currentlyProcessing) {
49
+ let pipeline = pipeline_1.Pipeline.get(this._pipeline);
50
+ if (!pipeline)
51
+ return;
52
+ if (!this._decoder)
53
+ return;
54
+ this._submitMotionUpdates(pipeline);
55
+ if (this._motionUpdatesQueued > this._motionUpdatesSent)
56
+ return;
57
+ if (currentlyProcessing)
58
+ return;
59
+ let curTime = performance.now();
60
+ if (curTime < this._lastUpdateTime + this._minFrameGapMs)
61
+ return;
62
+ if (this._packetNumber >= this._decoder.numberPackets)
63
+ return;
64
+ let packet = this._decoder.dataByPacket.get(this._packetNumber);
65
+ if (packet.type != sequencerecorder_1.SequenceRecorderPacketType.CAMERA_FRAME)
66
+ return;
67
+ this._packetNumber++;
68
+ let pixels = pipeline.cameraPixelArrays.pop();
69
+ while (pixels) {
70
+ if (pixels.byteLength === packet.greyscale.byteLength)
71
+ break;
72
+ pixels = pipeline.cameraPixelArrays.pop();
73
+ }
74
+ if (!pixels) {
75
+ pixels = new ArrayBuffer(packet.greyscale.byteLength);
76
+ }
77
+ (new Uint8Array(pixels)).set(packet.greyscale);
78
+ this._lastUpdateTime = curTime;
79
+ let info = {
80
+ data: pixels,
81
+ cameraSourceData: packet.greyscale,
82
+ cameraModel: packet.cameraModel,
83
+ cameraToDevice: packet.cameraToDevice,
84
+ dataWidth: packet.width,
85
+ dataHeight: packet.height,
86
+ texture: undefined,
87
+ userFacing: packet.userFacing,
88
+ uvTransform: gl_matrix_1.mat4.create(),
89
+ cameraSource: this
90
+ };
91
+ // We can queue the motion updates for the follwing frame immediately
92
+ // as this camera frame will be sent immediately, but the next motion
93
+ // updates will be one frame later
94
+ this._submitMotionUpdates(pipeline);
95
+ const token = pipeline.registerToken(info);
96
+ pipeline.sendDataToWorker(info.data || new ArrayBuffer(0), token, info.dataWidth, info.dataHeight, info.userFacing, info.cameraToDevice, info.cameraModel);
97
+ }
98
+ destroy() {
99
+ byId.delete(this._impl);
100
+ this.pause();
101
+ }
102
+ start() {
103
+ var _a;
104
+ let p = pipeline_1.Pipeline.get(this._pipeline);
105
+ if (p && p.currentCameraSource !== this) {
106
+ (_a = p.currentCameraSource) === null || _a === void 0 ? void 0 : _a.pause();
107
+ p.currentCameraSource = this;
108
+ }
109
+ }
110
+ pause() {
111
+ let p = pipeline_1.Pipeline.get(this._pipeline);
112
+ if (p && p.currentCameraSource === this)
113
+ p.currentCameraSource = undefined;
114
+ }
115
+ uploadGL(info) {
116
+ let pipeline = pipeline_1.Pipeline.get(this._pipeline);
117
+ if (!pipeline)
118
+ return;
119
+ let gl = pipeline === null || pipeline === void 0 ? void 0 : pipeline.glContext;
120
+ if (!gl)
121
+ return;
122
+ if (!info.texture)
123
+ info.texture = pipeline.getVideoTexture();
124
+ if (!info.texture)
125
+ return;
126
+ if (!info.cameraSourceData)
127
+ return;
128
+ gl.bindTexture(gl.TEXTURE_2D, info.texture);
129
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
130
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, info.dataWidth, info.dataHeight, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, new Uint8Array(info.cameraSourceData));
131
+ gl.bindTexture(gl.TEXTURE_2D, null);
132
+ }
133
+ _submitMotionUpdates(pipeline) {
134
+ let pendingMotionUpdate = false;
135
+ while (this._packetNumber < this._decoder.numberPackets) {
136
+ let packet = this._decoder.dataByPacket.get(this._packetNumber);
137
+ if (packet.type == sequencerecorder_1.SequenceRecorderPacketType.CAMERA_FRAME)
138
+ break;
139
+ switch (packet.type) {
140
+ case sequencerecorder_1.SequenceRecorderPacketType.ACCELEROMETER:
141
+ pipeline.motionAccelerometerSubmit(packet.t, packet.x, packet.y, packet.z);
142
+ pendingMotionUpdate = true;
143
+ break;
144
+ case sequencerecorder_1.SequenceRecorderPacketType.ROTATION_RATE:
145
+ pipeline.motionRotationRateSubmit(packet.t, packet.x, packet.y, packet.z);
146
+ pendingMotionUpdate = true;
147
+ break;
148
+ case sequencerecorder_1.SequenceRecorderPacketType.ATTITUDE:
149
+ pipeline.motionAttitudeSubmit(packet.t, packet.x, packet.y, packet.z);
150
+ pendingMotionUpdate = true;
151
+ break;
152
+ case sequencerecorder_1.SequenceRecorderPacketType.ATTITUDE_MATRIX:
153
+ pipeline.motionAttitudeMatrix(packet.attitude);
154
+ pendingMotionUpdate = true;
155
+ break;
156
+ }
157
+ this._packetNumber++;
158
+ }
159
+ // If we've submitted motion updates, add a setTimeout to reflect when
160
+ // the message serializer will actually have sent them over to the worker
161
+ if (pendingMotionUpdate) {
162
+ this._motionUpdatesQueued++;
163
+ setTimeout(() => {
164
+ this._motionUpdatesSent++;
165
+ }, 0);
166
+ }
167
+ return;
168
+ }
169
+ }
170
+ exports.SequenceSource = SequenceSource;
@@ -7,6 +7,7 @@ export interface MessageAppender {
7
7
  dataWithLength: (i: ArrayBuffer) => void;
8
8
  type: (t: number) => void;
9
9
  matrix4x4: (a: Float32Array) => void;
10
+ matrix3x3: (a: Float32Array) => void;
10
11
  cameraModel: (a: Float32Array) => void;
11
12
  identityCoefficients: (a: Float32Array) => void;
12
13
  expressionCoefficients: (a: Float32Array) => void;
package/lib/serializer.js CHANGED
@@ -24,6 +24,7 @@ class MessageSerializer {
24
24
  dataWithLength: i => this.arrayBuffer(i),
25
25
  type: i => this.int(i),
26
26
  matrix4x4: i => this.float32ArrayBuffer(i),
27
+ matrix3x3: i => this.float32ArrayBuffer(i),
27
28
  identityCoefficients: i => this.float32ArrayBuffer(i),
28
29
  expressionCoefficients: i => this.float32ArrayBuffer(i),
29
30
  cameraModel: i => this.float32ArrayBuffer(i),
package/lib/source.d.ts CHANGED
@@ -1,12 +1,19 @@
1
+ /// <reference types="dom-webcodecs" />
1
2
  export interface CameraFrameInfo {
2
- data: ArrayBuffer;
3
+ data?: ArrayBuffer;
3
4
  texture: WebGLTexture | undefined;
4
5
  dataWidth: number;
5
6
  dataHeight: number;
6
- uvTransform: Float32Array;
7
+ cameraModel: Float32Array;
8
+ cameraToDevice: Float32Array;
9
+ cameraSource: Source;
10
+ cameraSourceData?: any;
11
+ uvTransform?: Float32Array;
7
12
  userFacing: boolean;
13
+ frame?: VideoFrame | ImageBitmap;
8
14
  }
9
15
  export declare abstract class Source {
10
- abstract getFrame(currentlyProcessing: boolean): CameraFrameInfo | undefined;
16
+ abstract getFrame(currentlyProcessing: boolean): void;
11
17
  abstract pause(): void;
18
+ abstract uploadGL(info: CameraFrameInfo): void;
12
19
  }
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.3.13";
1
+ export declare const VERSION = "0.4.0-beta.10";
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = "0.3.13";
4
+ exports.VERSION = "0.4.0-beta.10";
@@ -0,0 +1,5 @@
1
+ import { zappar_cwrap } from "./gen/zappar-native";
2
+ import { zappar_server } from "./gen/zappar-server";
3
+ import { MsgManager } from "./messages";
4
+ import { ImageBitmapC2S } from "./workerinterface";
5
+ export declare function handleImageBitmap(m: ImageBitmapC2S, r: zappar_cwrap, server: zappar_server, mgr: MsgManager): void;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleImageBitmap = void 0;
4
+ const image_process_gl_1 = require("./image-process-gl");
5
+ const profile_1 = require("./profile");
6
+ const gl_matrix_1 = require("gl-matrix");
7
+ let pixels;
8
+ let texture;
9
+ let _imageProcessor;
10
+ let _gl;
11
+ function getImageProcessor() {
12
+ if (!_imageProcessor || !_gl) {
13
+ const canvas = new OffscreenCanvas(1, 1);
14
+ _gl = canvas.getContext("webgl");
15
+ if (!_gl)
16
+ throw new Error("Unable to get offscreen GL context");
17
+ _imageProcessor = new image_process_gl_1.ImageProcessGL(_gl);
18
+ }
19
+ return [_imageProcessor, _gl];
20
+ }
21
+ function handleImageBitmap(m, r, server, mgr) {
22
+ // img: ImageBitmap,
23
+ // rot: number,
24
+ // tokenId: number,
25
+ // r: zappar_cwrap,
26
+ // p: zappar_pipeline_t,
27
+ // userFacing: boolean,
28
+ // server: zappar_server,
29
+ // mgr: MsgManager) {
30
+ const [imageProcessor, gl] = getImageProcessor();
31
+ if (!texture) {
32
+ texture = gl.createTexture();
33
+ gl.bindTexture(gl.TEXTURE_2D, texture);
34
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
35
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
36
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
37
+ }
38
+ if (!texture)
39
+ return;
40
+ if (!pixels || pixels.byteLength < profile_1.profile.dataWidth * profile_1.profile.dataHeight) {
41
+ console.log("Generating pixel buffer", profile_1.profile.dataWidth * profile_1.profile.dataHeight);
42
+ pixels = new ArrayBuffer(profile_1.profile.dataWidth * profile_1.profile.dataHeight);
43
+ }
44
+ imageProcessor.uploadFrame(texture, m.i, m.r, m.userFacing);
45
+ let info = imageProcessor.readFrame(texture, pixels);
46
+ let msg = {
47
+ t: "imageBitmapS2C",
48
+ dataWidth: info.dataWidth,
49
+ dataHeight: info.dataHeight,
50
+ frame: m.i,
51
+ userFacing: info.userFacing,
52
+ uvTransform: info.uvTransform || gl_matrix_1.mat4.create(),
53
+ tokenId: m.tokenId, p: m.p
54
+ };
55
+ mgr.postOutgoingMessage(msg, [m.i]);
56
+ let pipeline = server._pipeline_by_instance.get(m.p);
57
+ if (pipeline) {
58
+ r.pipeline_camera_frame_submit(pipeline, pixels, info.dataWidth, info.dataHeight, m.tokenId, m.cameraToDevice, m.cameraModel, info.userFacing);
59
+ r.pipeline_frame_update(pipeline);
60
+ server.exploreState();
61
+ }
62
+ }
63
+ exports.handleImageBitmap = handleImageBitmap;