mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
61bd5e7b55
Fix js ximgproc edge drawing #29387 ## Summary This PR fixes OpenCV.js binding generation for factory functions that return `cv::Ptr<T>` where `T` is a namespaced class exported with a module-prefixed JavaScript binding name. The failure is reproduced with OpenCV.js plus `opencv_contrib/modules/ximgproc`. The generated binding for `cv::ximgproc::EdgeDrawing` currently contains: ```cpp .constructor(select_overload<Ptr<EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) ``` but `EdgeDrawing` is not available in the generated C++ scope as an unqualified type. The generated constructor should use: ```cpp .constructor(select_overload<Ptr<cv::ximgproc::EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) ``` ## Related issues Fixes opencv/opencv_contrib#4161. Related to #27963, #28130, and #28143. #28143 added namespace qualification for factory `Ptr<...>` return types when the inner `Ptr` type matches the generator class key. The remaining `ximgproc::EdgeDrawing` case is different: ```text inner Ptr type: EdgeDrawing generator class key: ximgproc_EdgeDrawing C++ class name: cv::ximgproc::EdgeDrawing ``` Because `EdgeDrawing` does not match `ximgproc_EdgeDrawing`, the previous condition does not handle this case. ## Fix approach The JS generator now centralizes factory `Ptr<...>` return-type qualification in a helper used by both generator paths: * `gen_function_binding_with_wrapper` * `gen_function_binding` The helper keeps the existing behavior for already qualified types and for the existing class-key match. It additionally handles the case where the inner `Ptr` type matches the basename of the C++ class name: ```text cv::ximgproc::EdgeDrawing -> EdgeDrawing ``` This allows the generator to emit `Ptr<cv::ximgproc::EdgeDrawing>` for `createEdgeDrawing()` without changing generated files directly. ## Necessity as an opencv code change The failing API is exposed by `opencv_contrib/modules/ximgproc`, but the invalid C++ line is emitted by OpenCV core's JavaScript binding generator in `modules/js/generator/embindgen.py`. A contrib-only workaround would have to change the `ximgproc` public declaration or special-case the JS export list for `createEdgeDrawing`. That would only work around one symbol and would not fix the generator's handling of namespaced factory `Ptr` return types. The generator already has the class metadata needed to produce the correct fully qualified C++ type, so the fix belongs in OpenCV core. ## Verification Tested with: * OpenCV core `4.x` * opencv_contrib `4.x` * Emscripten `6.0.1` * CMake generator: Ninja * Build list: `core,imgproc,imgcodecs,video,calib3d,ximgproc,js` Generator target: ```bash emcmake cmake \ -S /path/to/opencv \ -B /path/to/build \ -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_CXX_STANDARD=17 \ -DOPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib/modules \ -DBUILD_LIST=core,imgproc,imgcodecs,video,calib3d,ximgproc,js \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_opencv_js=ON \ -DBUILD_TESTS=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_DOCS=OFF ninja -C /path/to/build gen_opencv_js_source ``` This generated the expected binding: ```cpp .constructor(select_overload<Ptr<cv::ximgproc::EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) .smart_ptr<Ptr<cv::ximgproc::EdgeDrawing>>("Ptr<ximgproc_EdgeDrawing>") ``` The same patch was also verified with the full OpenCV.js target: ```bash ninja -C /path/to/build opencv.js ```