Convert to using declarations

This commit is contained in:
Patrick Barron 2020-07-19 14:12:53 -04:00
parent 6ab4493ecb
commit 196e8e131a
3 changed files with 223 additions and 276 deletions

View File

@ -19,8 +19,7 @@ namespace Jellyfin.Drawing.Skia
/// <param name="percent">The percentage played to display with the indicator.</param>
public static void Process(SKCanvas canvas, ImageDimensions imageSize, double percent)
{
using (var paint = new SKPaint())
{
using var paint = new SKPaint();
var endX = imageSize.Width - 1;
var endY = imageSize.Height - 1;
@ -37,4 +36,3 @@ namespace Jellyfin.Drawing.Skia
}
}
}
}

View File

@ -198,12 +198,10 @@ namespace Jellyfin.Drawing.Skia
var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);
using (var image = SKImage.FromBitmap(bitmap))
using (var subset = image.Subset(newRect))
{
using var image = SKImage.FromBitmap(bitmap);
using var subset = image.Subset(newRect);
return SKBitmap.FromImage(subset);
}
}
/// <inheritdoc />
/// <exception cref="ArgumentNullException">The path is null.</exception>
@ -216,15 +214,13 @@ namespace Jellyfin.Drawing.Skia
throw new FileNotFoundException("File not found", path);
}
using (var codec = SKCodec.Create(path, out SKCodecResult result))
{
using var codec = SKCodec.Create(path, out SKCodecResult result);
EnsureSuccess(result);
var info = codec.Info;
return new ImageDimensions(info.Width, info.Height);
}
}
/// <inheritdoc />
/// <exception cref="ArgumentNullException">The path is null.</exception>
@ -323,8 +319,7 @@ namespace Jellyfin.Drawing.Skia
if (requiresTransparencyHack || forceCleanBitmap)
{
using (var codec = SKCodec.Create(NormalizePath(path)))
{
using var codec = SKCodec.Create(NormalizePath(path));
if (codec == null)
{
origin = GetSKEncodedOrigin(orientation);
@ -341,7 +336,6 @@ namespace Jellyfin.Drawing.Skia
return bitmap;
}
}
var resultBitmap = SKBitmap.Decode(NormalizePath(path));
@ -367,8 +361,7 @@ namespace Jellyfin.Drawing.Skia
{
if (cropWhitespace)
{
using (var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin))
{
using var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin);
if (bitmap == null)
{
return null;
@ -376,7 +369,6 @@ namespace Jellyfin.Drawing.Skia
return CropWhiteSpace(bitmap);
}
}
return Decode(path, forceAnalyzeBitmap, orientation, out origin);
}
@ -408,12 +400,10 @@ namespace Jellyfin.Drawing.Skia
case SKEncodedOrigin.TopRight:
{
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
using (var surface = new SKCanvas(rotated))
{
using var surface = new SKCanvas(rotated);
surface.Translate(rotated.Width, 0);
surface.Scale(-1, 1);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
}
@ -421,14 +411,12 @@ namespace Jellyfin.Drawing.Skia
case SKEncodedOrigin.BottomRight:
{
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
using (var surface = new SKCanvas(rotated))
{
using var surface = new SKCanvas(rotated);
float px = (float)bitmap.Width / 2;
float py = (float)bitmap.Height / 2;
surface.RotateDegrees(180, px, py);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
}
@ -436,8 +424,7 @@ namespace Jellyfin.Drawing.Skia
case SKEncodedOrigin.BottomLeft:
{
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
using (var surface = new SKCanvas(rotated))
{
using var surface = new SKCanvas(rotated);
float px = (float)bitmap.Width / 2;
float py = (float)bitmap.Height / 2;
@ -447,7 +434,6 @@ namespace Jellyfin.Drawing.Skia
surface.RotateDegrees(180, px, py);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
}
@ -455,8 +441,7 @@ namespace Jellyfin.Drawing.Skia
case SKEncodedOrigin.LeftTop:
{
// TODO: Remove dual canvases, had trouble with flipping
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
{
using var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
surface.Translate(rotated.Width, 0);
@ -476,17 +461,13 @@ namespace Jellyfin.Drawing.Skia
return flippedBitmap;
}
}
case SKEncodedOrigin.RightTop:
{
var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
using var surface = new SKCanvas(rotated);
surface.Translate(rotated.Width, 0);
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
}
@ -494,8 +475,7 @@ namespace Jellyfin.Drawing.Skia
case SKEncodedOrigin.RightBottom:
{
// TODO: Remove dual canvases, had trouble with flipping
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
{
using var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
surface.Translate(0, rotated.Height);
@ -513,17 +493,14 @@ namespace Jellyfin.Drawing.Skia
return flippedBitmap;
}
}
case SKEncodedOrigin.LeftBottom:
{
var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
using var surface = new SKCanvas(rotated);
surface.Translate(0, rotated.Height);
surface.RotateDegrees(270);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
}
@ -552,8 +529,7 @@ namespace Jellyfin.Drawing.Skia
var blur = options.Blur ?? 0;
var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation))
{
using var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation);
if (bitmap == null)
{
throw new InvalidDataException($"Skia unable to read image {inputPath}");
@ -574,8 +550,7 @@ namespace Jellyfin.Drawing.Skia
var width = newImageSize.Width;
var height = newImageSize.Height;
using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
{
using var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType);
// scale image
bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
@ -583,18 +558,15 @@ namespace Jellyfin.Drawing.Skia
if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator)
{
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
using (var outputStream = new SKFileWStream(outputPath))
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels()))
{
using var outputStream = new SKFileWStream(outputPath);
using var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels());
pixmap.Encode(outputStream, skiaOutputFormat, quality);
return outputPath;
}
}
// create bitmap to use for canvas drawing used to draw into bitmap
using (var saveBitmap = new SKBitmap(width, height)) // , bitmap.ColorType, bitmap.AlphaType))
using (var canvas = new SKCanvas(saveBitmap))
{
using var saveBitmap = new SKBitmap(width, height);
using var canvas = new SKCanvas(saveBitmap);
// set background color if present
if (hasBackgroundColor)
{
@ -605,13 +577,11 @@ namespace Jellyfin.Drawing.Skia
if (blur > 0)
{
// create image from resized bitmap to apply blur
using (var paint = new SKPaint())
using (var filter = SKImageFilter.CreateBlur(blur, blur))
{
using var paint = new SKPaint();
using var filter = SKImageFilter.CreateBlur(blur, blur);
paint.ImageFilter = filter;
canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
}
}
else
{
// draw resized bitmap onto canvas
@ -642,9 +612,6 @@ namespace Jellyfin.Drawing.Skia
pixmap.Encode(outputStream, skiaOutputFormat, quality);
}
}
}
}
}
return outputPath;
}

View File

@ -69,13 +69,11 @@ namespace Jellyfin.Drawing.Skia
/// <param name="height">The desired height of the collage.</param>
public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
{
using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
using (var outputStream = new SKFileWStream(outputPath))
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
{
using var bitmap = BuildSquareCollageBitmap(paths, width, height);
using var outputStream = new SKFileWStream(outputPath);
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
}
}
/// <summary>
/// Create a thumb collage.
@ -86,20 +84,17 @@ namespace Jellyfin.Drawing.Skia
/// <param name="height">The desired height of the collage.</param>
public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
{
using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
using (var outputStream = new SKFileWStream(outputPath))
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
{
using var bitmap = BuildThumbCollageBitmap(paths, width, height);
using var outputStream = new SKFileWStream(outputPath);
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
}
}
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
{
var bitmap = new SKBitmap(width, height);
using (var canvas = new SKCanvas(bitmap))
{
using var canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.Black);
// number of images used in the thumbnail
@ -111,8 +106,7 @@ namespace Jellyfin.Drawing.Skia
int imageIndex = 0;
for (int i = 0; i < iCount; i++)
{
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
{
using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
imageIndex = newIndex;
if (currentBitmap == null)
{
@ -121,22 +115,16 @@ namespace Jellyfin.Drawing.Skia
// resize to the same aspect as the original
int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
{
using var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType);
currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
// crop image
int ix = Math.Abs((iWidth - iSlice) / 2);
using (var image = SKImage.FromBitmap(resizeBitmap))
using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
{
using var image = SKImage.FromBitmap(resizeBitmap);
using var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight));
// draw image onto canvas
canvas.DrawImage(subset ?? image, iSlice * i, 0);
}
}
}
}
}
return bitmap;
}
@ -176,14 +164,12 @@ namespace Jellyfin.Drawing.Skia
var cellWidth = width / 2;
var cellHeight = height / 2;
using (var canvas = new SKCanvas(bitmap))
{
using var canvas = new SKCanvas(bitmap);
for (var x = 0; x < 2; x++)
{
for (var y = 0; y < 2; y++)
{
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
{
using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
imageIndex = newIndex;
if (currentBitmap == null)
@ -191,8 +177,7 @@ namespace Jellyfin.Drawing.Skia
continue;
}
using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
{
using var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType);
// scale image
currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
@ -202,9 +187,6 @@ namespace Jellyfin.Drawing.Skia
canvas.DrawBitmap(resizedBitmap, xPos, yPos);
}
}
}
}
}
return bitmap;
}