Browse Source

update skia

Luke Pulverenti 8 years ago
parent
commit
3feeb04ffc
1 changed files with 23 additions and 28 deletions
  1. 23 28
      Emby.Drawing.Skia/SkiaEncoder.cs

+ 23 - 28
Emby.Drawing.Skia/SkiaEncoder.cs

@@ -6,6 +6,8 @@ using MediaBrowser.Model.IO;
 using MediaBrowser.Model.Logging;
 using SkiaSharp;
 using System;
+using System.IO;
+using System.Linq;
 using System.Reflection;
 using System.Threading.Tasks;
 
@@ -120,8 +122,6 @@ namespace Emby.Drawing.Skia
 
         private SKBitmap CropWhiteSpace(SKBitmap bitmap)
         {
-            CheckDisposed();
-
             var topmost = 0;
             for (int row = 0; row < bitmap.Height; ++row)
             {
@@ -175,8 +175,6 @@ namespace Emby.Drawing.Skia
 
         public ImageSize GetImageSize(string path)
         {
-            CheckDisposed();
-
             using (var s = new SKFileStream(path))
             {
                 using (var codec = SKCodec.Create(s))
@@ -192,19 +190,27 @@ namespace Emby.Drawing.Skia
             }
         }
 
+        private string[] TransparentImageTypes = new string[] { ".png", ".gif", ".webp" };
         private SKBitmap Decode(string path)
         {
-            using (var stream = new SKFileStream(path))
+            var requiresTransparencyHack = TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);
+
+            if (requiresTransparencyHack)
             {
-                var codec = SKCodec.Create(stream);
+                using (var stream = new SKFileStream(path))
+                {
+                    var codec = SKCodec.Create(stream);
 
-                // create the bitmap
-                var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height);
-                // decode
-                codec.GetPixels(bitmap.Info, bitmap.GetPixels());
+                    // create the bitmap
+                    var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height);
+                    // decode
+                    codec.GetPixels(bitmap.Info, bitmap.GetPixels());
 
-                return bitmap;
+                    return bitmap;
+                }
             }
+
+            return SKBitmap.Decode(path);
         }
 
         private SKBitmap GetBitmap(string path, bool cropWhitespace)
@@ -215,7 +221,7 @@ namespace Emby.Drawing.Skia
                 {
                     return CropWhiteSpace(bitmap);
                 }
-            } 
+            }
 
             return Decode(path);
         }
@@ -236,11 +242,11 @@ namespace Emby.Drawing.Skia
             var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor);
             var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer);
             var blur = options.Blur ?? 0;
-            var hasIndicator = !options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0);
+            var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
 
             using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace))
             {
-                using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
+                using (var resizedBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
                 {
                     // scale image
                     var resizeMethod = SKBitmapResizeMethod.Lanczos3;
@@ -258,7 +264,7 @@ namespace Emby.Drawing.Skia
                     }
 
                     // create bitmap to use for canvas drawing
-                    using (var saveBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
+                    using (var saveBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
                     {
                         // create canvas used to draw into bitmap
                         using (var canvas = new SKCanvas(saveBitmap))
@@ -275,7 +281,7 @@ namespace Emby.Drawing.Skia
                                 using (var paint = new SKPaint())
                                 {
                                     // create image from resized bitmap to apply blur
-                                    using (var filter = SKImageFilter.CreateBlur(5, 5))
+                                    using (var filter = SKImageFilter.CreateBlur(blur, blur))
                                     {
                                         paint.ImageFilter = filter;
                                         canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
@@ -294,8 +300,7 @@ namespace Emby.Drawing.Skia
                                 Double opacity;
                                 if (!Double.TryParse(options.ForegroundLayer, out opacity)) opacity = .4;
 
-                                var foregroundColor = String.Format("#{0:X2}000000", (Byte)((1 - opacity) * 0xFF));
-                                canvas.DrawColor(SKColor.Parse(foregroundColor), SKBlendMode.SrcOver);
+                                canvas.DrawColor(new SKColor(0, 0, 0, (Byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver);
                             }
 
                             if (hasIndicator)
@@ -365,18 +370,8 @@ namespace Emby.Drawing.Skia
             get { return "Skia"; }
         }
 
-        private bool _disposed;
         public void Dispose()
         {
-            _disposed = true;
-        }
-
-        private void CheckDisposed()
-        {
-            if (_disposed)
-            {
-                throw new ObjectDisposedException(GetType().Name);
-            }
         }
 
         public bool SupportsImageCollageCreation