2016-06-17 23:54:08 +03:00
|
|
|
//
|
|
|
|
// Bindings for SKImageDecoder
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Matthew Leibowitz
|
|
|
|
//
|
|
|
|
// Copyright 2016 Xamarin Inc
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace SkiaSharp
|
|
|
|
{
|
2017-09-05 18:29:58 +03:00
|
|
|
// TODO: `Create(...)` should have overloads that accept a SKPngChunkReader
|
|
|
|
// TODO: missing the `QueryYuv8` and `GetYuv8Planes` members
|
|
|
|
// TODO: might be useful to wrap `GetFrameInfo` (single result)
|
|
|
|
|
2016-06-17 23:54:08 +03:00
|
|
|
public class SKCodec : SKObject
|
|
|
|
{
|
|
|
|
[Preserve]
|
|
|
|
internal SKCodec (IntPtr handle, bool owns)
|
|
|
|
: base (handle, owns)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (Handle != IntPtr.Zero && OwnsHandle) {
|
|
|
|
SkiaApi.sk_codec_destroy (Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose (disposing);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int MinBufferedBytesNeeded {
|
|
|
|
get { return (int)SkiaApi.sk_codec_min_buffered_bytes_needed (); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKImageInfo Info {
|
|
|
|
get {
|
2017-05-12 04:07:52 +03:00
|
|
|
SKImageInfoNative cinfo;
|
|
|
|
SkiaApi.sk_codec_get_info (Handle, out cinfo);
|
|
|
|
return SKImageInfoNative.ToManaged (ref cinfo);
|
2016-06-17 23:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 11:55:03 +03:00
|
|
|
public SKEncodedInfo EncodedInfo {
|
|
|
|
get {
|
|
|
|
SKEncodedInfo info;
|
|
|
|
SkiaApi.sk_codec_get_encodedinfo (Handle, out info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:54:08 +03:00
|
|
|
public SKCodecOrigin Origin {
|
|
|
|
get { return SkiaApi.sk_codec_get_origin (Handle); }
|
|
|
|
}
|
|
|
|
|
2017-03-14 05:26:57 +03:00
|
|
|
public SKEncodedImageFormat EncodedFormat {
|
2016-06-17 23:54:08 +03:00
|
|
|
get { return SkiaApi.sk_codec_get_encoded_format (Handle); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKSizeI GetScaledDimensions (float desiredScale)
|
|
|
|
{
|
|
|
|
SKSizeI dimensions;
|
|
|
|
SkiaApi.sk_codec_get_scaled_dimensions (Handle, desiredScale, out dimensions);
|
|
|
|
return dimensions;
|
|
|
|
}
|
|
|
|
|
2016-10-31 00:45:39 +03:00
|
|
|
public bool GetValidSubset (ref SKRectI desiredSubset)
|
2016-06-17 23:54:08 +03:00
|
|
|
{
|
2016-10-31 00:45:39 +03:00
|
|
|
return SkiaApi.sk_codec_get_valid_subset (Handle, ref desiredSubset);
|
2016-06-17 23:54:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] Pixels {
|
|
|
|
get {
|
|
|
|
byte[] pixels = null;
|
|
|
|
var result = GetPixels (out pixels);
|
|
|
|
if (result != SKCodecResult.Success && result != SKCodecResult.IncompleteInput) {
|
|
|
|
throw new Exception (result.ToString ());
|
|
|
|
}
|
|
|
|
return pixels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 09:24:25 +03:00
|
|
|
public int RepetitionCount => SkiaApi.sk_codec_get_repetition_count (Handle);
|
|
|
|
|
|
|
|
public int FrameCount => SkiaApi.sk_codec_get_frame_count (Handle);
|
|
|
|
|
|
|
|
public SKCodecFrameInfo[] FrameInfo {
|
|
|
|
get {
|
|
|
|
var length = SkiaApi.sk_codec_get_frame_count (Handle);
|
|
|
|
var info = new SKCodecFrameInfo [length];
|
|
|
|
SkiaApi.sk_codec_get_frame_info (Handle, info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:54:08 +03:00
|
|
|
public SKCodecResult GetPixels (out byte[] pixels)
|
|
|
|
{
|
|
|
|
return GetPixels (Info, out pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, out byte[] pixels)
|
|
|
|
{
|
|
|
|
pixels = new byte[info.BytesSize];
|
|
|
|
return GetPixels (info, pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, byte[] pixels)
|
|
|
|
{
|
2016-10-24 23:33:53 +03:00
|
|
|
if (pixels == null)
|
|
|
|
throw new ArgumentNullException (nameof (pixels));
|
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
unsafe {
|
|
|
|
fixed (byte* p = &pixels[0]) {
|
|
|
|
return GetPixels (info, (IntPtr)p);
|
2016-06-17 23:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options)
|
2016-11-26 09:24:25 +03:00
|
|
|
{
|
|
|
|
var colorTableCount = 0;
|
|
|
|
return GetPixels (info, pixels, rowBytes, options, IntPtr.Zero, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options, IntPtr colorTable, ref int colorTableCount)
|
2016-08-04 19:16:16 +03:00
|
|
|
{
|
2016-10-24 23:33:53 +03:00
|
|
|
if (pixels == IntPtr.Zero)
|
|
|
|
throw new ArgumentNullException (nameof (pixels));
|
|
|
|
|
2017-07-17 15:48:34 +03:00
|
|
|
var nativeOptions = SKCodecOptionsInternal.FromManaged (ref options);
|
2017-05-12 04:07:52 +03:00
|
|
|
var cinfo = SKImageInfoNative.FromManaged (ref info);
|
|
|
|
return SkiaApi.sk_codec_get_pixels (Handle, ref cinfo, pixels, (IntPtr)rowBytes, ref nativeOptions, colorTable, ref colorTableCount);
|
2016-08-04 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
2016-11-26 09:24:25 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, SKCodecOptions options)
|
|
|
|
{
|
|
|
|
var colorTableCount = 0;
|
|
|
|
return GetPixels (info, pixels, options, IntPtr.Zero, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
2016-08-04 19:16:16 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, SKCodecOptions options, IntPtr colorTable, ref int colorTableCount)
|
|
|
|
{
|
|
|
|
return GetPixels (info, pixels, info.RowBytes, options, colorTable, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, IntPtr colorTable, ref int colorTableCount)
|
|
|
|
{
|
|
|
|
return GetPixels (info, pixels, info.RowBytes, SKCodecOptions.Default, colorTable, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:54:08 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels)
|
|
|
|
{
|
2016-08-04 19:16:16 +03:00
|
|
|
int colorTableCount = 0;
|
|
|
|
return GetPixels (info, pixels, info.RowBytes, SKCodecOptions.Default, IntPtr.Zero, ref colorTableCount);
|
2016-06-17 23:54:08 +03:00
|
|
|
}
|
|
|
|
|
2016-08-04 19:16:16 +03:00
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options, SKColorTable colorTable, ref int colorTableCount)
|
|
|
|
{
|
2016-08-04 19:21:06 +03:00
|
|
|
return GetPixels (info, pixels, rowBytes, options, colorTable == null ? IntPtr.Zero : colorTable.ReadColors (), ref colorTableCount);
|
2016-08-04 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, SKCodecOptions options, SKColorTable colorTable, ref int colorTableCount)
|
|
|
|
{
|
|
|
|
return GetPixels (info, pixels, info.RowBytes, options, colorTable, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult GetPixels (SKImageInfo info, IntPtr pixels, SKColorTable colorTable, ref int colorTableCount)
|
|
|
|
{
|
|
|
|
return GetPixels (info, pixels, info.RowBytes, SKCodecOptions.Default, colorTable, ref colorTableCount);
|
|
|
|
}
|
2016-08-04 19:21:06 +03:00
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
public SKCodecResult StartIncrementalDecode(SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options, IntPtr colorTable, ref int colorTableCount)
|
2016-11-17 05:34:48 +03:00
|
|
|
{
|
|
|
|
if (pixels == IntPtr.Zero)
|
|
|
|
throw new ArgumentNullException (nameof (pixels));
|
|
|
|
|
2017-07-17 15:48:34 +03:00
|
|
|
var nativeOptions = SKCodecOptionsInternal.FromManaged (ref options);
|
2017-05-12 04:07:52 +03:00
|
|
|
var cinfo = SKImageInfoNative.FromManaged (ref info);
|
|
|
|
return SkiaApi.sk_codec_start_incremental_decode (Handle, ref cinfo, pixels, (IntPtr)rowBytes, ref nativeOptions, colorTable, ref colorTableCount);
|
2016-11-17 05:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult StartIncrementalDecode (SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options)
|
|
|
|
{
|
|
|
|
int colorTableCount = 0;
|
|
|
|
return StartIncrementalDecode (info, pixels, rowBytes, options, IntPtr.Zero, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult StartIncrementalDecode (SKImageInfo info, IntPtr pixels, int rowBytes)
|
|
|
|
{
|
2017-05-12 04:07:52 +03:00
|
|
|
var cinfo = SKImageInfoNative.FromManaged (ref info);
|
|
|
|
return SkiaApi.sk_codec_start_incremental_decode (Handle, ref cinfo, pixels, (IntPtr)rowBytes, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
2016-11-17 05:34:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
public SKCodecResult StartIncrementalDecode(SKImageInfo info, IntPtr pixels, int rowBytes, SKCodecOptions options, SKColorTable colorTable, ref int colorTableCount)
|
2016-11-17 05:34:48 +03:00
|
|
|
{
|
|
|
|
return StartIncrementalDecode (info, pixels, rowBytes, options, colorTable == null ? IntPtr.Zero : colorTable.ReadColors (), ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult IncrementalDecode (out int rowsDecoded)
|
|
|
|
{
|
|
|
|
return SkiaApi.sk_codec_incremental_decode (Handle, out rowsDecoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult IncrementalDecode ()
|
|
|
|
{
|
|
|
|
int rowsDecoded;
|
|
|
|
return SkiaApi.sk_codec_incremental_decode (Handle, out rowsDecoded);
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:15:33 +03:00
|
|
|
public SKCodecResult StartScanlineDecode (SKImageInfo info, SKCodecOptions options, IntPtr colorTable, ref int colorTableCount)
|
2017-02-06 14:17:57 +03:00
|
|
|
{
|
2017-07-17 15:48:34 +03:00
|
|
|
var nativeOptions = SKCodecOptionsInternal.FromManaged (ref options);
|
2017-05-12 04:07:52 +03:00
|
|
|
var cinfo = SKImageInfoNative.FromManaged (ref info);
|
|
|
|
return SkiaApi.sk_codec_start_scanline_decode (Handle, ref cinfo, ref nativeOptions, colorTable, ref colorTableCount);
|
2017-02-06 14:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult StartScanlineDecode (SKImageInfo info, SKCodecOptions options)
|
|
|
|
{
|
|
|
|
int colorTableCount = 0;
|
|
|
|
return StartScanlineDecode (info, options, IntPtr.Zero, ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult StartScanlineDecode (SKImageInfo info)
|
|
|
|
{
|
2017-05-12 04:07:52 +03:00
|
|
|
var cinfo = SKImageInfoNative.FromManaged (ref info);
|
|
|
|
return SkiaApi.sk_codec_start_scanline_decode (Handle, ref cinfo, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
2017-02-06 14:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SKCodecResult StartScanlineDecode (SKImageInfo info, SKCodecOptions options, SKColorTable colorTable, ref int colorTableCount)
|
|
|
|
{
|
|
|
|
return StartScanlineDecode (info, options, colorTable == null ? IntPtr.Zero : colorTable.ReadColors (), ref colorTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetScanlines (IntPtr dst, int countLines, int rowBytes)
|
|
|
|
{
|
|
|
|
if (dst == IntPtr.Zero)
|
|
|
|
throw new ArgumentNullException (nameof (dst));
|
|
|
|
|
|
|
|
return SkiaApi.sk_codec_get_scanlines (Handle, dst, countLines, (IntPtr)rowBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SkipScanlines (int countLines) => SkiaApi.sk_codec_skip_scanlines (Handle, countLines);
|
|
|
|
|
|
|
|
public SKCodecScanlineOrder ScanlineOrder => SkiaApi.sk_codec_get_scanline_order (Handle);
|
|
|
|
|
|
|
|
public int NextScanline => SkiaApi.sk_codec_next_scanline (Handle);
|
|
|
|
|
|
|
|
public int GetOutputScanline (int inputScanline) => SkiaApi.sk_codec_output_scanline (Handle, inputScanline);
|
|
|
|
|
2016-06-17 23:54:08 +03:00
|
|
|
public static SKCodec Create (SKStream stream)
|
|
|
|
{
|
2016-08-04 19:21:06 +03:00
|
|
|
if (stream == null)
|
2016-10-24 23:33:53 +03:00
|
|
|
throw new ArgumentNullException (nameof (stream));
|
2016-06-17 23:54:08 +03:00
|
|
|
var codec = GetObject<SKCodec> (SkiaApi.sk_codec_new_from_stream (stream.Handle));
|
2017-10-25 17:29:51 +03:00
|
|
|
stream.RevokeOwnership ();
|
2016-06-17 23:54:08 +03:00
|
|
|
return codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SKCodec Create (SKData data)
|
|
|
|
{
|
2016-08-04 19:21:06 +03:00
|
|
|
if (data == null)
|
2016-10-24 23:33:53 +03:00
|
|
|
throw new ArgumentNullException (nameof (data));
|
2016-06-17 23:54:08 +03:00
|
|
|
return GetObject<SKCodec> (SkiaApi.sk_codec_new_from_data (data.Handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|