Rewrite OrientImage

This commit is contained in:
Patrick Barron 2020-07-19 17:59:33 -04:00
parent 2569793ff0
commit b51a10948a

View file

@ -372,118 +372,55 @@ namespace Jellyfin.Drawing.Skia
private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin) private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin)
{ {
if (origin == SKEncodedOrigin.Default)
{
return bitmap;
}
var needsFlip = origin == SKEncodedOrigin.LeftBottom
|| origin == SKEncodedOrigin.LeftTop
|| origin == SKEncodedOrigin.RightBottom
|| origin == SKEncodedOrigin.RightTop;
var rotated = needsFlip
? new SKBitmap(bitmap.Height, bitmap.Width)
: new SKBitmap(bitmap.Width, bitmap.Height);
using var surface = new SKCanvas(rotated);
var midX = (float)rotated.Width / 2;
var midY = (float)rotated.Height / 2;
switch (origin) switch (origin)
{ {
case SKEncodedOrigin.TopRight: case SKEncodedOrigin.TopRight:
{ surface.Scale(-1, 1, midX, midY);
var rotated = new SKBitmap(bitmap.Width, bitmap.Height); break;
using var surface = new SKCanvas(rotated);
surface.Translate(rotated.Width, 0);
surface.Scale(-1, 1);
surface.DrawBitmap(bitmap, 0, 0);
return rotated;
}
case SKEncodedOrigin.BottomRight: case SKEncodedOrigin.BottomRight:
{ surface.RotateDegrees(180, midX, midY);
var rotated = new SKBitmap(bitmap.Width, bitmap.Height); break;
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;
}
case SKEncodedOrigin.BottomLeft: case SKEncodedOrigin.BottomLeft:
{ surface.Scale(1, -1, midX, midY);
var rotated = new SKBitmap(bitmap.Width, bitmap.Height); break;
using var surface = new SKCanvas(rotated);
float px = (float)bitmap.Width / 2;
float py = (float)bitmap.Height / 2;
surface.Translate(rotated.Width, 0);
surface.Scale(-1, 1);
surface.RotateDegrees(180, px, py);
surface.DrawBitmap(bitmap, 0, 0);
return rotated;
}
case SKEncodedOrigin.LeftTop: case SKEncodedOrigin.LeftTop:
{ surface.Translate(0, -rotated.Height);
// TODO: Remove dual canvases, had trouble with flipping surface.Scale(1, -1, midX, midY);
using var rotated = new SKBitmap(bitmap.Height, bitmap.Width); surface.RotateDegrees(-90);
using (var surface = new SKCanvas(rotated)) break;
{
surface.Translate(rotated.Width, 0);
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap, 0, 0);
}
var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
using (var flippedCanvas = new SKCanvas(flippedBitmap))
{
flippedCanvas.Translate(flippedBitmap.Width, 0);
flippedCanvas.Scale(-1, 1);
flippedCanvas.DrawBitmap(rotated, 0, 0);
}
return flippedBitmap;
}
case SKEncodedOrigin.RightTop: case SKEncodedOrigin.RightTop:
{ surface.Translate(rotated.Width, 0);
var rotated = new SKBitmap(bitmap.Height, bitmap.Width); surface.RotateDegrees(90);
using var surface = new SKCanvas(rotated); break;
surface.Translate(rotated.Width, 0);
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap, 0, 0);
return rotated;
}
case SKEncodedOrigin.RightBottom: case SKEncodedOrigin.RightBottom:
{ surface.Translate(rotated.Width, 0);
// TODO: Remove dual canvases, had trouble with flipping surface.Scale(1, -1, midX, midY);
using var rotated = new SKBitmap(bitmap.Height, bitmap.Width); surface.RotateDegrees(90);
using (var surface = new SKCanvas(rotated)) break;
{
surface.Translate(0, rotated.Height);
surface.RotateDegrees(270);
surface.DrawBitmap(bitmap, 0, 0);
}
var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
using (var flippedCanvas = new SKCanvas(flippedBitmap))
{
flippedCanvas.Translate(flippedBitmap.Width, 0);
flippedCanvas.Scale(-1, 1);
flippedCanvas.DrawBitmap(rotated, 0, 0);
}
return flippedBitmap;
}
case SKEncodedOrigin.LeftBottom: case SKEncodedOrigin.LeftBottom:
{ surface.Translate(0, rotated.Height);
var rotated = new SKBitmap(bitmap.Height, bitmap.Width); surface.RotateDegrees(-90);
using var surface = new SKCanvas(rotated); break;
surface.Translate(0, rotated.Height);
surface.RotateDegrees(270);
surface.DrawBitmap(bitmap, 0, 0);
return rotated;
}
default: return bitmap;
} }
surface.DrawBitmap(bitmap, 0, 0);
return rotated;
} }
/// <inheritdoc/> /// <inheritdoc/>