Android import for Libwebp

222 views
Skip to first unread message

Panagiotis Paraskeva

unread,
Aug 29, 2022, 8:51:30 AMAug 29
to WebP Discussion
I am an android developer that works for a start-up company.

I would like to import the libwebp library to our project and use it to generate animated webps and remove the dependency we have on FFMPEG.

Unfortunately, I do not have much experience with the android ndk to import the project.
Furthermore, i am already using the android ndk/cmake for audio optimization.

Can you please send me an explanation on how to import the library using my current cmake file?


Regards,
Panagiotis Paraskeva

James Zern

unread,
Aug 29, 2022, 3:43:39 PMAug 29
to WebP Discussion
Hi,

On its own, libwebp can be build using the Android toolchain file:
cmake ../libwebp -DCMAKE_TOOLCHAIN_FILE=<path-to-ndk>/build/cmake/android.toolchain.cmake [-DANDROID_ABI=...]

You could install that and use find_package() [1] or instead use add_subdirectory() [2] to pull in the targets and have them built along with your project:
add_subdirectory("libwebp" EXCLUDE_FROM_ALL)

You can then depend on the targets from libwebp like 'webp' and 'libwebpmux'.

Udara Wanasinghe

unread,
Aug 31, 2022, 12:49:32 PMAug 31
to WebP Discussion, panag...@beatroot.app
Hi,
I experimented with this months ago and created JNI bindings to encode a series of android bitmap images to an animated WebP image. I made my work public for your reference. Please check my repository on GitHub.

Pem Code

unread,
Oct 10, 2022, 4:19:45 AMOct 10
to WebP Discussion, udara.d...@gmail.com, panag...@beatroot.app
cd libwebp/android-build
cmake .. -DCMAKE_TOOLCHAIN_FILE=/sarc-c/gpusw/users/p.mowry/android-ndk-r25b/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a
make

This outputs files to libwebp root directory.  Unfortunately it does not output to android-build.  However, it does appear to output binary files: webp_quality img2webp dwebp get_disto cwebp webpinfo webpmux dwebp get_disto libcpufeatures.a  libexampleutil.a  libextras.a  libimagedec.a  libimageenc.a  libimageioutil.a  libsharpyuv.a  libwebp.a  libwebpdecoder.a  libwebpdemux.a  libwebpmux.a

I have an existing C++ project that builds using cmake command line on Linux for Android.  My existing code has an array of RGBA pixels (R8 G8 B8 A8) that writes the uint8_t array to a bmp file.  I want my project's cmake file to reference the appropriate webp Android binary and modify the C++ code to write to a webp file (instead of a bmp file).  How do I do this?

Udara Wanasinghe

unread,
Oct 10, 2022, 4:26:51 AMOct 10
to Pem Code, WebP Discussion, panag...@beatroot.app
You can allocate space for a new WebPPicture struct and copy pixel data directly to it. See how this is done in my project https://github.com/UdaraWanasinghe/webp-android/blob/dbd5ff0f7765b723c648c0919164a66ddc2d213c/webp-android/src/main/cpp/encoder.cpp#L446

James Zern

unread,
Oct 10, 2022, 3:54:25 PMOct 10
to WebP Discussion
On Mon, Oct 10, 2022 at 1:19 AM Pem Code <pem.s...@gmail.com> wrote:
cd libwebp/android-build
cmake .. -DCMAKE_TOOLCHAIN_FILE=/sarc-c/gpusw/users/p.mowry/android-ndk-r25b/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a
make

This outputs files to libwebp root directory. Unfortunately it does not output to android-build.  However, it does appear to output binary files: webp_quality img2webp dwebp get_disto cwebp webpinfo webpmux dwebp get_disto libcpufeatures.a  libexampleutil.a  libextras.a  libimagedec.a  libimageenc.a  libimageioutil.a  libsharpyuv.a  libwebp.a  libwebpdecoder.a  libwebpdemux.a  libwebpmux.a

All outputs should be under android-build unless you have already run cmake in the source directory. Starting with a fresh tree or deleting all CMakeFiles and CMakeCache.txt files and running cmake again should work
 

I have an existing C++ project that builds using cmake command line on Linux for Android.  My existing code has an array of RGBA pixels (R8 G8 B8 A8) that writes the uint8_t array to a bmp file.  I want my project's cmake file to reference the appropriate webp Android binary and modify the C++ code to write to a webp file (instead of a bmp file).  How do I do this?




On Wednesday, August 31, 2022 at 9:49:32 AM UTC-7 udara.d...@gmail.com wrote:
Hi,
I experimented with this months ago and created JNI bindings to encode a series of android bitmap images to an animated WebP image. I made my work public for your reference. Please check my repository on GitHub.
On Monday, August 29, 2022 at 6:21:30 PM UTC+5:30 panag...@beatroot.app wrote:
I am an android developer that works for a start-up company.

I would like to import the libwebp library to our project and use it to generate animated webps and remove the dependency we have on FFMPEG.

Unfortunately, I do not have much experience with the android ndk to import the project.
Furthermore, i am already using the android ndk/cmake for audio optimization.

Can you please send me an explanation on how to import the library using my current cmake file?


Regards,
Panagiotis Paraskeva

--
You received this message because you are subscribed to the Google Groups "WebP Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webp-discuss...@webmproject.org.
To view this discussion on the web visit https://groups.google.com/a/webmproject.org/d/msgid/webp-discuss/073e2e42-d77b-433b-8a45-534d84dd739cn%40webmproject.org.

Pem Code

unread,
Oct 11, 2022, 3:20:34 AMOct 11
to WebP Discussion, James Zern
The following enabled me to reference libwebp binaries (in case it helps someone else) for Android C++ using cmake...

target_include_directories(foo PRIVATE
    ../../../../libwebp/src/webp
)
if(ANDROID)
    if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
        SET(WEBP_ANDROID_FOLDER "build-android32")
    else()
        SET(WEBP_ANDROID_FOLDER "build-android")
    endif()
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libcpufeatures.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libexampleutil.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libextras.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libimagedec.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libimageenc.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libimageioutil.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libsharpyuv.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libwebp.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libwebpdecoder.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libwebpdemux.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/libwebpmux.a)
    target_link_libraries(xgl PRIVATE ${GLOBAL_ROOT_SRC_DIR}/../libwebp/${WEBP_ANDROID_FOLDER}/cwebp)
endif()

Next I will see if I can get this reference code from @udara to write a webp file (taking a uint8_t array as input) (btw the uint8_t array is data from a vulkan VkImage's such as VK_FORMAT_R8G8B8A8_UNORM from the swapchain)
Message has been deleted

Udara Wanasinghe

unread,
Oct 11, 2022, 4:44:55 AMOct 11
to WebP Discussion, Pem Code, Udara Wanasinghe, panag...@beatroot.app
You can configure the build using configuration parameters available in libwebp.

set(WEBP_BUILD_ANIM_UTILS FALSE) # Build animation utilities.
set(WEBP_BUILD_CWEBP FALSE) # Build the cwebp command line tool.
set(WEBP_BUILD_DWEBP FALSE) # Build the dwebp command line tool.
set(WEBP_BUILD_GIF2WEBP FALSE) # Build the gif2webp conversion tool.
set(WEBP_BUILD_IMG2WEBP FALSE) # Build the img2webp animation tool.
set(WEBP_BUILD_VWEBP FALSE) # Build the vwebp viewer tool.
set(WEBP_BUILD_WEBPINFO FALSE) # Build the webpinfo command line tool.
set(WEBP_BUILD_LIBWEBPMUX TRUE) # Build the libwebpmux library.
set(WEBP_BUILD_WEBPMUX FALSE) # Build the webpmux command line tool.
set(WEBP_BUILD_EXTRAS FALSE) # Build extras.

Since we are required only the libwebpmux library, set WEBP_BUILD_LIBWEBPMUX to TRUE.

Avoid manually including library files. You can do this by adding libwebp as a subdirectory to your cmake build script.

set(LIBWEBP_PATH ../../libwebp CACHE STRING "libwebp path")
add_subdirectory(${LIBWEBP_PATH} ${CMAKE_CURRENT_BINARY_DIR}/libwebp)

See full example here https://github.com/UdaraWanasinghe/webp-android/blob/main/webp-android/CMakeLists.txt.
Message has been deleted

Pem Code

unread,
Oct 11, 2022, 5:15:49 PMOct 11
to WebP Discussion, udara.d...@gmail.com, Pem Code, panag...@beatroot.app
Oh maybe I should add libweb as an external aka git submodule...  I will try this way of building instead.  It does sound cleaner...

Pem Code

unread,
Oct 16, 2022, 8:48:02 AMOct 16
to WebP Discussion, Pem Code, udara.d...@gmail.com, panag...@beatroot.app
It is working now.  Thank you for the references
Reply all
Reply to author
Forward
0 new messages