Add more encoding options to SKPixmap
This commit is contained in:
Родитель
b0ddea825a
Коммит
a87713b422
|
@ -2311,4 +2311,139 @@ namespace SkiaSharp
|
|||
None = 0,
|
||||
ZeroPixels = 1 << 0,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum SKPngEncoderFilterFlags {
|
||||
Zero = 0x00,
|
||||
None = 0x08,
|
||||
Sub = 0x10,
|
||||
Up = 0x20,
|
||||
Avg = 0x40,
|
||||
Paeth = 0x80,
|
||||
All = None | Sub | Up | Avg | Paeth,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SKPngEncoderOptions {
|
||||
private SKPngEncoderFilterFlags fFilterFlags;
|
||||
private int fZLibLevel;
|
||||
private SKTransferFunctionBehavior fUnpremulBehavior;
|
||||
|
||||
public static readonly SKPngEncoderOptions Default;
|
||||
|
||||
static SKPngEncoderOptions ()
|
||||
{
|
||||
Default = new SKPngEncoderOptions (SKPngEncoderFilterFlags.All, 6, SKTransferFunctionBehavior.Respect);
|
||||
}
|
||||
|
||||
public SKPngEncoderOptions (SKPngEncoderFilterFlags filterFlags, int zLibLevel, SKTransferFunctionBehavior unpremulBehavior)
|
||||
{
|
||||
fFilterFlags = filterFlags;
|
||||
fZLibLevel = zLibLevel;
|
||||
fUnpremulBehavior = unpremulBehavior;
|
||||
}
|
||||
|
||||
public SKPngEncoderFilterFlags FilterFlags {
|
||||
get { return fFilterFlags; }
|
||||
set { fFilterFlags = value; }
|
||||
}
|
||||
public int ZLibLevel {
|
||||
get { return fZLibLevel; }
|
||||
set { fZLibLevel = value; }
|
||||
}
|
||||
public SKTransferFunctionBehavior UnpremulBehavior {
|
||||
get { return fUnpremulBehavior; }
|
||||
set { fUnpremulBehavior = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public enum SKJpegEncoderDownsample {
|
||||
Downsample420,
|
||||
Downsample422,
|
||||
Downsample444,
|
||||
}
|
||||
|
||||
public enum SKJpegEncoderAlphaOption {
|
||||
Ignore,
|
||||
BlendOnBlack,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SKJpegEncoderOptions {
|
||||
private int fQuality;
|
||||
private SKJpegEncoderDownsample fDownsample;
|
||||
private SKJpegEncoderAlphaOption fAlphaOption;
|
||||
private SKTransferFunctionBehavior fBlendBehavior;
|
||||
|
||||
public static readonly SKJpegEncoderOptions Default;
|
||||
|
||||
static SKJpegEncoderOptions ()
|
||||
{
|
||||
Default = new SKJpegEncoderOptions (100, SKJpegEncoderDownsample.Downsample420, SKJpegEncoderAlphaOption.Ignore, SKTransferFunctionBehavior.Respect);
|
||||
}
|
||||
|
||||
public SKJpegEncoderOptions (int quality, SKJpegEncoderDownsample downsample, SKJpegEncoderAlphaOption alphaOption, SKTransferFunctionBehavior blendBehavior)
|
||||
{
|
||||
fQuality = quality;
|
||||
fDownsample = downsample;
|
||||
fAlphaOption = alphaOption;
|
||||
fBlendBehavior = blendBehavior;
|
||||
}
|
||||
|
||||
public int Quality {
|
||||
get { return fQuality; }
|
||||
set { fQuality = value; }
|
||||
}
|
||||
public SKJpegEncoderDownsample Downsample {
|
||||
get { return fDownsample; }
|
||||
set { fDownsample = value; }
|
||||
}
|
||||
public SKJpegEncoderAlphaOption AlphaOption {
|
||||
get { return fAlphaOption; }
|
||||
set { fAlphaOption = value; }
|
||||
}
|
||||
public SKTransferFunctionBehavior BlendBehavior {
|
||||
get { return fBlendBehavior; }
|
||||
set { fBlendBehavior = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public enum SKWebpEncoderCompression {
|
||||
Lossy,
|
||||
Lossless,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SKWebpEncoderOptions {
|
||||
private SKWebpEncoderCompression fCompression;
|
||||
private float fQuality;
|
||||
private SKTransferFunctionBehavior fUnpremulBehavior;
|
||||
|
||||
public static readonly SKWebpEncoderOptions Default;
|
||||
|
||||
static SKWebpEncoderOptions ()
|
||||
{
|
||||
Default = new SKWebpEncoderOptions (SKWebpEncoderCompression.Lossy, 100, SKTransferFunctionBehavior.Respect);
|
||||
}
|
||||
|
||||
public SKWebpEncoderOptions (SKWebpEncoderCompression compression, float quality, SKTransferFunctionBehavior unpremulBehavior)
|
||||
{
|
||||
fCompression = compression;
|
||||
fQuality = quality;
|
||||
fUnpremulBehavior = unpremulBehavior;
|
||||
}
|
||||
|
||||
public SKWebpEncoderCompression Compression {
|
||||
get { return fCompression; }
|
||||
set { fCompression = value; }
|
||||
}
|
||||
public float Quality {
|
||||
get { return fQuality; }
|
||||
set { fQuality = value; }
|
||||
}
|
||||
public SKTransferFunctionBehavior UnpremulBehavior {
|
||||
get { return fUnpremulBehavior; }
|
||||
set { fUnpremulBehavior = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -722,13 +722,12 @@ namespace SkiaSharp
|
|||
return bmp;
|
||||
}
|
||||
|
||||
[Obsolete ("Use SKPixmap.Encode instead.")]
|
||||
public bool Encode (SKWStream dst, SKEncodedImageFormat format, int quality)
|
||||
{
|
||||
using (var pixmap = new SKPixmap ()) {
|
||||
return PeekPixels (pixmap) && pixmap.Encode (dst, format, quality);
|
||||
}
|
||||
return SKPixmap.Encode (dst, this, format, quality);
|
||||
}
|
||||
|
||||
|
||||
private void Swap (SKBitmap other)
|
||||
{
|
||||
SkiaApi.sk_bitmap_swap (Handle, other.Handle);
|
||||
|
|
|
@ -150,6 +150,18 @@ namespace SkiaSharp
|
|||
return Encode (dst, this, encoder, quality);
|
||||
}
|
||||
|
||||
public static bool Encode (SKWStream dst, SKBitmap src, SKEncodedImageFormat format, int quality)
|
||||
{
|
||||
if (dst == null)
|
||||
throw new ArgumentNullException (nameof (dst));
|
||||
if (src == null)
|
||||
throw new ArgumentNullException (nameof (src));
|
||||
|
||||
using (var pixmap = new SKPixmap ()) {
|
||||
return src.PeekPixels (pixmap) && Encode (dst, pixmap, format, quality);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Encode (SKWStream dst, SKPixmap src, SKEncodedImageFormat encoder, int quality)
|
||||
{
|
||||
if (dst == null)
|
||||
|
@ -160,6 +172,75 @@ namespace SkiaSharp
|
|||
return SkiaApi.sk_pixmap_encode_image (dst.Handle, src.Handle, encoder, quality);
|
||||
}
|
||||
|
||||
public SKData Encode (SKWebpEncoderOptions options)
|
||||
{
|
||||
using (var stream = new SKDynamicMemoryWStream ()) {
|
||||
var result = Encode (stream, this, options);
|
||||
return result ? stream.DetachAsData () : null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Encode (SKWStream dst, SKWebpEncoderOptions options)
|
||||
{
|
||||
return Encode (dst, this, options);
|
||||
}
|
||||
|
||||
public static bool Encode (SKWStream dst, SKPixmap src, SKWebpEncoderOptions options)
|
||||
{
|
||||
if (dst == null)
|
||||
throw new ArgumentNullException (nameof (dst));
|
||||
if (src == null)
|
||||
throw new ArgumentNullException (nameof (src));
|
||||
|
||||
return SkiaApi.sk_webpencoder_encode (dst.Handle, src.Handle, options);
|
||||
}
|
||||
|
||||
public SKData Encode (SKJpegEncoderOptions options)
|
||||
{
|
||||
using (var stream = new SKDynamicMemoryWStream ()) {
|
||||
var result = Encode (stream, this, options);
|
||||
return result ? stream.DetachAsData () : null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Encode (SKWStream dst, SKJpegEncoderOptions options)
|
||||
{
|
||||
return Encode (dst, this, options);
|
||||
}
|
||||
|
||||
public static bool Encode (SKWStream dst, SKPixmap src, SKJpegEncoderOptions options)
|
||||
{
|
||||
if (dst == null)
|
||||
throw new ArgumentNullException (nameof (dst));
|
||||
if (src == null)
|
||||
throw new ArgumentNullException (nameof (src));
|
||||
|
||||
return SkiaApi.sk_jpegencoder_encode (dst.Handle, src.Handle, options);
|
||||
}
|
||||
|
||||
public SKData Encode (SKPngEncoderOptions options)
|
||||
{
|
||||
using (var stream = new SKDynamicMemoryWStream ()) {
|
||||
var result = Encode (stream, this, options);
|
||||
return result ? stream.DetachAsData () : null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Encode (SKWStream dst, SKPngEncoderOptions options)
|
||||
{
|
||||
return Encode (dst, this, options);
|
||||
}
|
||||
|
||||
public static bool Encode (SKWStream dst, SKPixmap src, SKPngEncoderOptions options)
|
||||
{
|
||||
if (dst == null)
|
||||
throw new ArgumentNullException (nameof (dst));
|
||||
if (src == null)
|
||||
throw new ArgumentNullException (nameof (src));
|
||||
|
||||
return SkiaApi.sk_pngencoder_encode (dst.Handle, src.Handle, options);
|
||||
}
|
||||
|
||||
public SKPixmap WithColorType (SKColorType newColorType)
|
||||
{
|
||||
return new SKPixmap (Info.WithColorType (newColorType), GetPixels (), RowBytes, ColorTable);
|
||||
|
|
|
@ -1413,6 +1413,16 @@ namespace SkiaSharp
|
|||
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
|
||||
public extern static void sk_swizzle_swap_rb(IntPtr dest, IntPtr src, int count);
|
||||
|
||||
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
|
||||
[return: MarshalAs (UnmanagedType.I1)]
|
||||
public extern static bool sk_webpencoder_encode (sk_wstream_t dst, sk_pixmap_t src, SKWebpEncoderOptions options);
|
||||
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
|
||||
[return: MarshalAs (UnmanagedType.I1)]
|
||||
public extern static bool sk_jpegencoder_encode (sk_wstream_t dst, sk_pixmap_t src, SKJpegEncoderOptions options);
|
||||
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
|
||||
[return: MarshalAs (UnmanagedType.I1)]
|
||||
public extern static bool sk_pngencoder_encode (sk_wstream_t dst, sk_pixmap_t src, SKPngEncoderOptions options);
|
||||
|
||||
// Mask
|
||||
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
|
||||
public extern static IntPtr sk_mask_alloc_image(IntPtr bytes);
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit ceff8e6562f6909837741b7798cd8727c10cf8e6
|
||||
Subproject commit 30bcea2ecdac4fb410196fcef8aad72f2ad769e5
|
|
@ -343,6 +343,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/gpu,
|
||||
|
@ -383,6 +384,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/gpu,
|
||||
|
|
|
@ -307,6 +307,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/gpu,
|
||||
../../externals/skia/include/utils,
|
||||
|
@ -340,6 +341,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/gpu,
|
||||
../../externals/skia/include/utils,
|
||||
|
|
|
@ -16,6 +16,7 @@ USER_INC_DIRS = ../../externals/skia/src/c \
|
|||
../../externals/skia/include/core \
|
||||
../../externals/skia/include/codec \
|
||||
../../externals/skia/include/effects \
|
||||
../../externals/skia/include/encode \
|
||||
../../externals/skia/include/pathops \
|
||||
../../externals/skia/include/gpu \
|
||||
../../externals/skia/include/config \
|
||||
|
|
|
@ -338,6 +338,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/ports,
|
||||
|
@ -378,6 +379,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/ports,
|
||||
|
|
|
@ -339,6 +339,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/ports,
|
||||
|
@ -380,6 +381,7 @@
|
|||
../../externals/skia/include/core,
|
||||
../../externals/skia/include/codec,
|
||||
../../externals/skia/include/effects,
|
||||
../../externals/skia/include/encode,
|
||||
../../externals/skia/include/pathops,
|
||||
../../externals/skia/include/utils,
|
||||
../../externals/skia/include/ports,
|
||||
|
|
|
@ -77,5 +77,50 @@ namespace SkiaSharp.Tests
|
|||
Assert.Equal(SKColors.Blue, bmp.Pixels[0]);
|
||||
}
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void EncodeWithPngEncoder()
|
||||
{
|
||||
var bitmap = CreateTestBitmap();
|
||||
var pixmap = bitmap.PeekPixels();
|
||||
|
||||
var data = pixmap.Encode(SKPngEncoderOptions.Default);
|
||||
|
||||
Assert.NotNull(data);
|
||||
|
||||
var codec = SKCodec.Create(data);
|
||||
|
||||
Assert.Equal(SKEncodedImageFormat.Png, codec.EncodedFormat);
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void EncodeWithJpegEncoder()
|
||||
{
|
||||
var bitmap = CreateTestBitmap();
|
||||
var pixmap = bitmap.PeekPixels();
|
||||
|
||||
var data = pixmap.Encode(SKJpegEncoderOptions.Default);
|
||||
|
||||
Assert.NotNull(data);
|
||||
|
||||
var codec = SKCodec.Create(data);
|
||||
|
||||
Assert.Equal(SKEncodedImageFormat.Jpeg, codec.EncodedFormat);
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void EncodeWithWebpEncoder()
|
||||
{
|
||||
var bitmap = CreateTestBitmap();
|
||||
var pixmap = bitmap.PeekPixels();
|
||||
|
||||
var data = pixmap.Encode(SKWebpEncoderOptions.Default);
|
||||
|
||||
Assert.NotNull(data);
|
||||
|
||||
var codec = SKCodec.Create(data);
|
||||
|
||||
Assert.Equal(SKEncodedImageFormat.Webp, codec.EncodedFormat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче