mirror of
https://github.com/zebrajr/opencv.git
synced 2026-01-15 12:15:17 +00:00
js: restore deep copy behavior for Mat.clone()
Emscripten 3.1.71+ introduced ClassHandle.clone() which performs a shallow copy. This shadowed the original OpenCV clone() method which was intended for deep copying. This patch: 1. Hooks into onRuntimeInitialized in helpers.js 2. Overrides cv.Mat.prototype.clone to point to mat_clone (deep copy) 3. Updates JS tests to use clone() to verify the fix
This commit is contained in:
@@ -410,3 +410,20 @@ if (
|
||||
if (cv.UMat) cv.UMat.prototype[Symbol.dispose] = cv.UMat.prototype.delete;
|
||||
// Add more as OpenCV gains new manual-cleanup classes
|
||||
}
|
||||
|
||||
// Override Emscripten's shallow clone() with OpenCV's deep copy mat_clone()
|
||||
// This restores the expected behavior where clone() performs a deep copy.
|
||||
// Background: Emscripten 3.1.71+ added ClassHandle.clone() which only does shallow copy.
|
||||
// See: https://github.com/opencv/opencv/pull/26643
|
||||
// See: https://github.com/opencv/opencv/issues/27572
|
||||
var _opencv_onRuntimeInitialized_backup = Module['onRuntimeInitialized'];
|
||||
Module['onRuntimeInitialized'] = function() {
|
||||
if (_opencv_onRuntimeInitialized_backup) {
|
||||
_opencv_onRuntimeInitialized_backup();
|
||||
}
|
||||
if (typeof cv !== 'undefined' && cv.Mat &&
|
||||
typeof cv.Mat.prototype.mat_clone === 'function') {
|
||||
cv.Mat.prototype.clone = cv.Mat.prototype.mat_clone;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -173,7 +173,7 @@ QUnit.test('test_mat_creation', function(assert) {
|
||||
// clone
|
||||
{
|
||||
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
|
||||
let mat2 = mat.mat_clone();
|
||||
let mat2 = mat.clone();
|
||||
|
||||
assert.equal(mat.channels, mat2.channels);
|
||||
assert.equal(mat.size().height, mat2.size().height);
|
||||
|
||||
Reference in New Issue
Block a user