0xC0000054 / libheif-sharp

Provides .NET bindings for libheif.
https://0xc0000054.github.io/libheif-sharp/
GNU Lesser General Public License v3.0
60 stars 3 forks source link

support for android. #9

Closed prabhav-mehra closed 1 year ago

prabhav-mehra commented 1 year ago

Hi. I am trying to get ImageSharp work with HEIC and have tried https://github.com/hey-red/ImageSharp.Heif/issues/3 and this library. I was wondering about how I can get ARM builds for libheif and the steps to make it work for android

0xC0000054 commented 1 year ago

I have no experience with android development, but you should be able to cross-compile libheif and its dependencies for ARM/ARM64. I have had success using vcpkg to cross-compile libheif, libde265 and x265 for Windows on ARM64. Vcpkg has a few cross-compilation targets for android, but I do not know if they would work for libheif.

The ImageSharp.Heif Linux native binaries appear to be built from the following repository: https://github.com/hey-red/LibHeif-Build

farindk commented 1 year ago

I have build scripts for libheif and libde265 to compile Android libraries (all architectures). HEIC decoding only. However, they are hard-coded for my own use. These generate plain native libraries (.so) compiled with the Android NDK. I do not use libheif-sharp. If it helps, I can put them into the libheif repository.

prabhav-mehra commented 1 year ago

@farindk that would be of great help. I only require it for decoding anyway.

Thanks.

prabhav-mehra commented 1 year ago

@0xC0000054 I get the same error when trying to load a HEIF on iOS as well, "dll not found"? is that expected/it only works on windows?

prabhav-mehra commented 1 year ago

@farindk just checking if you have uploaded the scripts and i might have missed it. based on my previous comment, do you also have ios build scripts?

0xC0000054 commented 1 year ago

It should work on any platform that provides a libheif binary.

prabhav-mehra commented 1 year ago

@0xC0000054 should I binaries be in a certain location for android and ios to work? There is a specific path for windows where the dll needs to be.

0xC0000054 commented 1 year ago

The paths that are searched searched for native libraries would probably depend on the OS and/or .NET runtime. Mono has a page on its native library handling: https://www.mono-project.com/docs/advanced/pinvoke/#library-handling

LibHeifSharp looks for a shared library named libheif, the runtime adds the appropriate file extension (e.g. .dll on Windows, .so on Linux) before it searches for the library.

prabhav-mehra commented 1 year ago

@0xC0000054 would it be possible if you could add support for android and ios in the library itself? I tried few things but I never got this working.

Since you have already the system/steps sorted with the builds for linux and windows, it would be very helpful to get it working on android and ios as well.

0xC0000054 commented 1 year ago

LibHeifSharp should already support android and ios. As far as I am aware there are no differences in the libheif API across platforms. The "dll not found" error is probably a packaging issue with the native library, e.g. the library is not located in the path(s) that the runtime searches for native libraries or the name doesn't match what LibHeifSharp is requesting.

I do not have the hardware or development experience with android or ios to do any testing on those platforms.

prabhav-mehra commented 1 year ago

@0xC0000054 I am unable to get the necessary dlls of libheif for ios and android at the moment. Tried using vspkg but no luck. Are there any steps/procedures I could refer you know of which could help in this case.

0xC0000054 commented 1 year ago

I posted Windows build instruction forlibheif using vcpkg at: https://0xc0000054.github.io/libheif-sharp/libheif_windows_build_vcpkg.html You can try changing the x64-windows portion of the command to one of the android and/or ios triplets, e.g. x64-android or arm64-ios.

If the vcpkg install commands are from a command prompt that is configured with the appropriate android/ios compiler environment it should build. If you have further issues with the vcpkg build process, I would recommend opening an issue in that repository. The people there would know more about its build processes for non-Windows platforms than I do.

farindk commented 1 year ago

I have added scripts to build Android libraries into the scripts folder of libde265 and libheif. I have also uploaded pre-built libraries for all Android architectures to the release pages: libde265-1.0.12 Android libs libheif-1.16.2 Android libs

prabhav-mehra commented 1 year ago

@farindk thanks, that worked out well.

prabhav-mehra commented 1 year ago

@0xC0000054 After getting the android build/lib I was able to get libheif-sharp working partially.

I took the program class from decoder from libheif-sharp-samples class an imported it to my .net maui project to test it out. I am facing some issues with that.

list of versions heif-dec 1.0.0 LibHeifSharp v3.1.0 libheif v1.16.2

My program gets suck at https://github.com/0xC0000054/libheif-sharp-samples/blob/b32cd0a6ebd3b64d58ec7f6ce813d91c60a92d72/src/decoder/Program.cs#L577 if I hardcode the value to either true or false, it then gets stuck at https://github.com/0xC0000054/libheif-sharp-samples/blob/b32cd0a6ebd3b64d58ec7f6ce813d91c60a92d72/src/decoder/Program.cs#L578.

I don't get any exception or no crahses/logs. After setting the variables to false and 8 respectively, I get Unsupported feature: Unsupported codec when I call
var i = imageHandle.Decode(HeifColorspace.Rgb, chroma, decodingOptions);

this is my options:

  bool extractDepthImages = false;
  bool extractThumbnailImages = false;
  bool extractVendorAuxiliaryImages = false;
  bool extractPrimaryImage = true;
  string decoderId = null;
  string chromaUpsampling = null;
  bool listDecoders = true;
  bool convertHdrToEightBit = true;
  bool strict = true;
  bool showHelp = false;
  bool showVersion = true;

I am using a Stream instead of a filepath for android. I pick a file, get the stream and then use that. This works out well with windows tho, using the same image

I have placed both libde265 and libheif in the same dir and using the pre-built libs

@farindk

0xC0000054 commented 1 year ago

That sounds like a problem with the android libheif binaries, especially since it works on Windows.

TheHasAlphaChannel property calls heif_image_handle_has_alpha_channel, and the BitDepth property calls heif_image_handle_get_luma_bits_per_pixel. I do not know why either of those methods cause a hang (something I/O related?), maybe @farindk has some insight.

The Unsupported feature: Unsupported codec error makes me wonder if the android version of libheif is correctly loading libde265. What is the output of the list-decoders command?

prabhav-mehra commented 1 year ago

@0xC0000054 thats exactly what's happening.

The Unsupported feature: Unsupported codec error makes me wonder if the android version of libheif is correctly loading libde265. What is the output of the list-decoders command?

On windows I can see libde265 HEVC decoder, version 1.0.11 but on android I don't see anything

prabhav-mehra commented 1 year ago

After building libheif myself using libde256, I was able to get it working for android successfully. Cheers guys.

Closing.