diff --git a/CMakeLists.txt b/CMakeLists.txt index d42ad7d5..e4f06b94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,14 @@ option(WEBP_BUILD_CWEBP "Build the cwebp command line tool." OFF) option(WEBP_BUILD_DWEBP "Build the dwebp command line tool." OFF) option(WEBP_BUILD_GIF2WEBP "Build the gif2webp conversion tool." OFF) option(WEBP_BUILD_IMG2WEBP "Build the img2webp animation tool." OFF) +option(WEBP_BUILD_WEBP_JS "Emscripten build of webp.js." OFF) option(WEBP_EXPERIMENTAL_FEATURES "Build with experimental features." OFF) option(WEBP_ENABLE_SWAP_16BIT_CSP "Enable byte swap for 16 bit colorspaces." OFF) +if(WEBP_BUILD_WEBP_JS) + set(WEBP_ENABLE_SIMD OFF) +endif() + set(WEBP_DEP_LIBRARIES) set(WEBP_DEP_INCLUDE_DIRS) @@ -135,7 +140,7 @@ endforeach() # Build the executables if asked for. if(WEBP_BUILD_CWEBP OR WEBP_BUILD_DWEBP OR - WEBP_BUILD_GIF2WEBP OR WEBP_BUILD_IMG2WEBP) + WEBP_BUILD_GIF2WEBP OR WEBP_BUILD_IMG2WEBP OR WEBP_BUILD_WEBP_JS) # Example utility library. parse_Makefile_am(${CMAKE_CURRENT_SOURCE_DIR}/examples "EXAMPLEUTIL_SRCS" "example_util_[^ ]*") @@ -204,3 +209,14 @@ if(WEBP_BUILD_IMG2WEBP) add_executable(img2webp ${IMG2WEBP_SRCS}) target_link_libraries(img2webp exampleutil imagedec imageioutil webp webpmux) endif() + +if(WEBP_BUILD_WEBP_JS) + add_executable(webp_js + ${CMAKE_CURRENT_SOURCE_DIR}/extras/webp_to_sdl.c) + target_link_libraries(webp_js webpdecoder SDL) + set_target_properties(webp_js PROPERTIES LINK_FLAGS + "-s EXPORTED_FUNCTIONS='[\"_WebpToSDL\"]' -s INVOKE_RUN=0") + set_target_properties(webp_js PROPERTIES OUTPUT_NAME webp) + target_compile_definitions(webp_js PUBLIC EMSCRIPTEN WEBP_HAVE_SDL) + target_compile_definitions(webpdecoder PUBLIC EMSCRIPTEN) +endif() diff --git a/README.webp_js b/README.webp_js new file mode 100644 index 00000000..74c4f4c3 --- /dev/null +++ b/README.webp_js @@ -0,0 +1,63 @@ + __ __ ____ ____ ____ __ ____ + / \\/ \ _ \ _ \ _ \ (__)/ __\ + \ / __/ _ \ __/ _) \_ \ + \__\__/_____/____/_/ /____/____/ + +Description: +============ + +This file describes the compilation of libwebp into a JavaScript decoder +using Emscripten and CMake. + + - install the Emscripten SDK following the procedure described at: + https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html + After installation, you should have some global variable positioned to the + location of the SDK. In particular, $EMSCRIPTEN should point to the + top-level directory containing Emscripten tools. + + - make sure the file $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake is + accessible. This is the toolchain file used by CMake to invoke Emscripten. + + - configure the project 'WEBP_JS' with CMake using: + + cd webp_js && \ + cmake -DWEBP_BUILD_WEBP_JS=ON \ + -DEMSCRIPTEN_GENERATE_BITCODE_STATIC_LIBRARIES=1 \ + -DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \ + ../ + + - compile webp.js using 'make'. + + - that's it! Upon completion, you should have the webp.js and + webp.js.mem files generated. + +The callable JavaScript function is WebPToSDL(), which decodes a raw WebP +bitstream into a canvas. See webp_js/index.html for a simple usage sample. + +Demo HTML page: +=============== + + The HTML page webp_js/index.html requires an HTTP server to serve the WebP + image example. It's easy to just use Python for that. + +cd webp_js && python -m SimpleHTTPServer 8080 + +and then navigate to http://localhost:8080 in your favorite browser. + + +Caveat: +======= + + - First decoding using the library is usually slower, due to just-in-time + compilation. + + - Some versions of llvm produce the following compile error when SSE2 is + enabled. + +"Unsupported: %516 = bitcast <8 x i16> %481 to i128 + LLVM ERROR: BitCast Instruction not yet supported for integer types larger than 64 bits" + + The corresponding Emscripten bug is at: + https://github.com/kripken/emscripten/issues/3788 + + Therefore, SSE2 optimization is currently disabled in CMakeLists.txt. diff --git a/webp_js/index.html b/webp_js/index.html new file mode 100644 index 00000000..096ef151 --- /dev/null +++ b/webp_js/index.html @@ -0,0 +1,69 @@ + + + + + + simple Javascript WebP decoding demo + + + + + + +

+ WebP in JavaScript demo - +

+

+ WebP decoder in JavaScript, using libwebp compiled with + Emscripten. +

+

+ +

+

Timing: N/A

+ Your browser does not support canvas + + + diff --git a/webp_js/test_webp_js.webp b/webp_js/test_webp_js.webp new file mode 100644 index 00000000..f41d3670 Binary files /dev/null and b/webp_js/test_webp_js.webp differ