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> /// <param name="percent">The percentage played to display with the indicator.</param>
public static void Process(SKCanvas canvas, ImageDimensions imageSize, double percent) 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 endX = imageSize.Width - 1;
var endY = imageSize.Height - 1; var endY = imageSize.Height - 1;
@ -36,5 +35,4 @@ namespace Jellyfin.Drawing.Skia
canvas.DrawRect(SKRect.Create(0, (float)endY - IndicatorHeight, Convert.ToInt32(foregroundWidth), (float)endY), paint); canvas.DrawRect(SKRect.Create(0, (float)endY - IndicatorHeight, Convert.ToInt32(foregroundWidth), (float)endY), paint);
} }
} }
}
} }

View File

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

View File

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