Skiasharp: ToBitmap Fails for some images on Android

Created on 5 Jul 2017  路  12Comments  路  Source: mono/SkiaSharp

I can't exactly pin this one down, but with _some_ images on Android the call to ToBitmap will come back with null. It appears to be failing on the internal call to success = skiaBitmap.CopyPixelsTo(ptr, bmp.Width * bmp.RowBytes);

Example image that fails

area-SkiaSharp.Views os-Android type-bug

Most helpful comment

@michaelstonis Thanks for reporting this. This is actually a new issue.

With v1.58, I was able to make use of the native copy function. But, in v1.59, Google removed that function. As a result, I wrote the equivalent function using managed code. Is this process, I added a new method, SKPixmap.ReadPixels. However, this did not get enough testing and I missed the fact that the marshalling was not correct.

The current function is this:

[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public extern static bool sk_pixmap_read_pixels(sk_pixmap_t cpixmap, ref SKImageInfo dstInfo, IntPtr dstPixels, IntPtr dstRowBytes, int srcX, int srcY);

This is mostly correct, except for the SKImageInfo type. This should have been the SKImageInfoNative type. I will fix this and get a new version out ASAP.

All 12 comments

I'm having the same issue. Seems to be images whose height is greater than their width.

I have identified the problem. I will submit a pull request to fix the issue

I don't believe that this issue was resolved with the fix in 1.59.0. If you take a look at the dstSize parameter of SKBitmap.CopyPixelsTo it is not actually referencing or using it at all. With 1.59.0, my Android application now crashes with the following exception.

[mono-rt] at (wrapper managed-to-native) SkiaSharp.SkiaApi.sk_pixmap_read_pixels (intptr,SkiaSharp.SKImageInfo&,intptr,intptr,int,int) <0x00007> [mono-rt] at SkiaSharp.SKPixmap.ReadPixels (SkiaSharp.SKImageInfo,intptr,int,int,int) [0x00013] in <7398af18bbfa455c8a1e84f1a7eb063a>:0 [mono-rt] at SkiaSharp.SKPixmap.ReadPixels (SkiaSharp.SKImageInfo,intptr,int) [0x00006] in <7398af18bbfa455c8a1e84f1a7eb063a>:0

I am going to see if I can cook up a different solution in the meantime. I will follow-up if I come up with anything useful.

@michaelstonis Thanks for reporting this. This is actually a new issue.

With v1.58, I was able to make use of the native copy function. But, in v1.59, Google removed that function. As a result, I wrote the equivalent function using managed code. Is this process, I added a new method, SKPixmap.ReadPixels. However, this did not get enough testing and I missed the fact that the marshalling was not correct.

The current function is this:

[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public extern static bool sk_pixmap_read_pixels(sk_pixmap_t cpixmap, ref SKImageInfo dstInfo, IntPtr dstPixels, IntPtr dstRowBytes, int srcX, int srcY);

This is mostly correct, except for the SKImageInfo type. This should have been the SKImageInfoNative type. I will fix this and get a new version out ASAP.

@mattleibow I really appreciate the quick turnaround. Did this version replace the current release of 1.59 in nuget or will this be a new release?

It will be in a new version. I am just doing a few things and should be out soon.

I have made a few other changes as well, so I _may_ bump the version a bit higher to v1.59.1

@michaelstonis I pushed out a release that should have fixed the issue: https://github.com/mono/SkiaSharp/releases/tag/v1.59.1

Thank you @mattleibow. I just gave it a shot and it looks to be working perfectly. Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings