How to decode an image using Emscripten?
Hi, I've been trying to use OpenCV as a dependency in an Emscripten project.
Currently I'm having an issue with trying to decode an image.
When I try to use cv::imdecode(buf, flags)
I get the compilation (EDIT linker) error:
error: undefined symbol: _ZN2cv8imdecodeERKNS_11_InputArrayEi
The exact code I'm using is available on github, I've set up a minimalist example here: https://github.com/mpizenberg/emscripten-opencv
The line causing the linker error is the commented line in hello.cpp
:
// this->decoded = cv::imdecode(this->buffer, IMREAD_GRAYSCALE);
I don't know what is the reason of this issue. Is there another way I can decode an image from a raw u8 buffer?
EDIT 1
As @berak commented I need to link to libopencv_imgcodecs.a
. But unfortunately this opens new undefined symbol
issues. In details, here is what I did.
- There is a line
"DBUILD_opencv_imgcodecs=OFF"
in the build_js.py
script file. So I've switched that to ON
. - I deleted
build_wasm/
and rebuild it from scratch. Surprisingly, It did not created the libopencv_imgcodecs.a
file. Apparently the build created the correct make files but I had to do a make
inside build_wasm/
to actually compile and produce the libopencv_imgcodecs.a
file. I'll make a dedicated issue regarding that behavior on Github if that's ok. - In my repo files, cleaned
build/
dir and recompiled my project. That's when I get an awful lot of new undefined symbol
errors.
The new linking issues:
~/t/e/opencv * build emmake make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
[100%] Linking CXX executable hello.js
error: undefined symbol: _Z7cvRoundRKN2cv10softdoubleE
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
error: undefined symbol: _Z7cvRoundRKN2cv9softfloatE
error: undefined symbol: _Z7cvTruncRKN2cv9softfloatE
error: undefined symbol: _ZN2cv10softdoubleC1Ei
error: undefined symbol: _ZN2cv3powERKNS_10softdoubleES2_
error: undefined symbol: _ZN2cv4cbrtERKNS_9softfloatE
error: undefined symbol: _ZN2cv5mergeERKNS_11_InputArrayERKNS_12_OutputArrayE
error: undefined symbol: _ZN2cv6mulAddERKNS_9softfloatES2_S2_
error: undefined symbol: _ZN2cv8cubeRootEf
error: undefined symbol: _ZN2cv9softfloatC1Ei
error: undefined symbol: _ZNK2cv10softdoublecvNS_9softfloatEEv
error: undefined symbol: _ZNK2cv10softdoubledvERKS0_
error: undefined symbol: _ZNK2cv10softdoubleeqERKS0_
error: undefined symbol: _ZNK2cv10softdoubleleERKS0_
error: undefined symbol: _ZNK2cv10softdoublemiERKS0_
error: undefined symbol: _ZNK2cv10softdoublemlERKS0_
error: undefined symbol: _ZNK2cv10softdoubleplERKS0_
error: undefined symbol: _ZNK2cv9softfloatcvNS_10softdoubleEEv
error: undefined symbol: _ZNK2cv9softfloatdvERKS0_
error: undefined symbol: _ZNK2cv9softfloatgtERKS0_
error: undefined symbol: _ZNK2cv9softfloatltERKS0_
error: undefined symbol: _ZNK2cv9softfloatmiERKS0_
error: undefined symbol: _ZNK2cv9softfloatmlERKS0_
error: undefined symbol: _ZNK2cv9softfloatplERKS0_
...
Error: Aborting compilation due to previous errors
shared:ERROR: '/home/matthieu/programs/emsdk/node/8.9.1_64bit/bin/node /usr/lib/emscripten/src/compiler.js /tmp/tmputw1eyfv.txt /usr/lib/emscripten/src/embind/emval.js /usr/lib/emscripten/src/embind/embind.js /usr/lib/emscripten/src/library_pthread_stub.js' failed (1)
make[2]: *** [CMakeFiles/hello.dir/build.make:97: hello.js] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
EDIT 2
Ok, so from reading parts of OpenCV.js tutorial, I realized that reading images was always done from already decoded data in a canvas (cf https://github.com/opencv/opencv/blob/master/modules/js/src/helpers.js#L41). So I guess even though we can compile imgcodecs apparently, it does not include all the functionalities. I can't use canvas because I need decoding of 16bits png.
I've tried reading libpng doc (because that is the only format I need reading for now) and it is not very pleasant ... At least it seems to be already integrated with emscripten (https://github.com/emscripten-core/emscripten/issues/4626#issuecomment-253898574)