jellyfin/Emby.Drawing.Net/GDIImageEncoder.cs

283 lines
9.8 KiB
C#
Raw Normal View History

2015-10-26 05:29:32 +00:00
using MediaBrowser.Controller.Drawing;
2015-04-08 14:38:02 +00:00
using MediaBrowser.Model.Drawing;
using Microsoft.Extensions.Logging;
2015-04-08 14:38:02 +00:00
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
2015-04-09 05:20:23 +00:00
using System.Linq;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2015-04-08 14:38:02 +00:00
using ImageFormat = MediaBrowser.Model.Drawing.ImageFormat;
2017-05-15 02:27:58 +00:00
using Emby.Drawing;
2015-04-08 14:38:02 +00:00
2016-11-11 17:33:10 +00:00
namespace Emby.Drawing.Net
2015-04-08 14:38:02 +00:00
{
public class GDIImageEncoder : IImageEncoder
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
public GDIImageEncoder(IFileSystem fileSystem, ILogger logger)
{
_fileSystem = fileSystem;
_logger = logger;
2015-05-11 00:24:02 +00:00
2015-10-26 05:29:32 +00:00
LogInfo();
}
private void LogInfo()
{
_logger.LogInformation("GDIImageEncoder starting");
2015-10-26 05:29:32 +00:00
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType().Namespace + ".empty.png"))
{
using (var img = Image.FromStream(stream))
{
2015-11-12 19:28:43 +00:00
2015-10-26 05:29:32 +00:00
}
}
_logger.LogInformation("GDIImageEncoder started");
2015-04-08 14:38:02 +00:00
}
public string[] SupportedInputFormats
{
get
{
return new[]
{
"png",
"jpeg",
"jpg",
"gif",
"bmp"
};
}
}
public ImageFormat[] SupportedOutputFormats
{
get
{
return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
}
}
public ImageSize GetImageSize(string path)
{
using (var image = Image.FromFile(path))
{
return new ImageSize
{
Width = image.Width,
Height = image.Height
};
}
}
private Image GetImage(string path, bool cropWhitespace)
2015-04-08 14:38:02 +00:00
{
if (cropWhitespace)
2015-04-08 14:38:02 +00:00
{
using (var originalImage = (Bitmap)Image.FromFile(path))
2015-04-08 14:38:02 +00:00
{
return originalImage.CropWhitespace();
2015-11-12 19:28:43 +00:00
}
2015-04-08 14:38:02 +00:00
}
return Image.FromFile(path);
2015-04-08 14:38:02 +00:00
}
2017-05-15 02:27:58 +00:00
public void EncodeImage(string inputPath, ImageSize? originalImageSize, string outputPath, bool autoOrient, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
2015-04-08 14:38:02 +00:00
{
using (var originalImage = GetImage(inputPath, options.CropWhiteSpace))
2015-04-08 14:38:02 +00:00
{
2017-05-15 02:27:58 +00:00
if (options.CropWhiteSpace || !originalImageSize.HasValue)
{
originalImageSize = new ImageSize(originalImage.Width, originalImage.Height);
}
var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
var newWidth = Convert.ToInt32(Math.Round(newImageSize.Width));
var newHeight = Convert.ToInt32(Math.Round(newImageSize.Height));
2015-04-08 14:38:02 +00:00
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
// Also, Webp only supports Format32bppArgb and Format32bppRgb
var pixelFormat = selectedOutputFormat == ImageFormat.Webp
? PixelFormat.Format32bppArgb
: PixelFormat.Format32bppPArgb;
using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
{
// Mono throw an exeception if assign 0 to SetResolution
if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
{
// Preserve the original resolution
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
}
using (var thumbnailGraph = Graphics.FromImage(thumbnail))
{
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
2016-08-25 04:54:06 +00:00
// SourceCopy causes the image to be blank in OSX
//thumbnailGraph.CompositingMode = !hasPostProcessing ?
// CompositingMode.SourceCopy :
// CompositingMode.SourceOver;
2015-04-08 14:38:02 +00:00
SetBackgroundColor(thumbnailGraph, options);
thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
DrawIndicator(thumbnailGraph, newWidth, newHeight, options);
var outputFormat = GetOutputFormat(originalImage, selectedOutputFormat);
// Save to the cache location
2017-05-15 02:27:58 +00:00
using (var cacheFileStream = _fileSystem.GetFileStream(outputPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, false))
2015-04-08 14:38:02 +00:00
{
// Save to the memory stream
thumbnail.Save(outputFormat, cacheFileStream, quality);
}
}
}
}
}
/// <summary>
/// Sets the color of the background.
/// </summary>
/// <param name="graphics">The graphics.</param>
/// <param name="options">The options.</param>
private void SetBackgroundColor(Graphics graphics, ImageProcessingOptions options)
{
var color = options.BackgroundColor;
if (!string.IsNullOrEmpty(color))
{
Color drawingColor;
try
{
drawingColor = ColorTranslator.FromHtml(color);
}
catch
{
drawingColor = ColorTranslator.FromHtml("#" + color);
}
graphics.Clear(drawingColor);
}
}
/// <summary>
/// Draws the indicator.
/// </summary>
/// <param name="graphics">The graphics.</param>
/// <param name="imageWidth">Width of the image.</param>
/// <param name="imageHeight">Height of the image.</param>
/// <param name="options">The options.</param>
private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageProcessingOptions options)
{
if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
{
return;
}
try
{
if (options.AddPlayedIndicator)
{
var currentImageSize = new Size(imageWidth, imageHeight);
new PlayedIndicatorDrawer().DrawPlayedIndicator(graphics, currentImageSize);
}
else if (options.UnplayedCount.HasValue)
{
var currentImageSize = new Size(imageWidth, imageHeight);
new UnplayedCountIndicator().DrawUnplayedCountIndicator(graphics, currentImageSize, options.UnplayedCount.Value);
}
if (options.PercentPlayed > 0)
{
var currentImageSize = new Size(imageWidth, imageHeight);
new PercentPlayedDrawer().Process(graphics, currentImageSize, options.PercentPlayed);
}
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error drawing indicator overlay");
2015-04-08 14:38:02 +00:00
}
}
/// <summary>
/// Gets the output format.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="outputFormat">The output format.</param>
/// <returns>ImageFormat.</returns>
private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageFormat outputFormat)
{
switch (outputFormat)
{
case ImageFormat.Bmp:
return System.Drawing.Imaging.ImageFormat.Bmp;
case ImageFormat.Gif:
return System.Drawing.Imaging.ImageFormat.Gif;
case ImageFormat.Jpg:
return System.Drawing.Imaging.ImageFormat.Jpeg;
case ImageFormat.Png:
return System.Drawing.Imaging.ImageFormat.Png;
default:
return image.RawFormat;
}
}
public void CreateImageCollage(ImageCollageOptions options)
{
2015-04-08 15:45:30 +00:00
double ratio = options.Width;
ratio /= options.Height;
if (ratio >= 1.4)
{
DynamicImageHelpers.CreateThumbCollage(options.InputPaths.ToList(), _fileSystem, options.OutputPath, options.Width, options.Height);
}
else if (ratio >= .9)
{
DynamicImageHelpers.CreateSquareCollage(options.InputPaths.ToList(), _fileSystem, options.OutputPath, options.Width, options.Height);
}
else
{
DynamicImageHelpers.CreateSquareCollage(options.InputPaths.ToList(), _fileSystem, options.OutputPath, options.Width, options.Width);
}
2015-04-08 14:38:02 +00:00
}
public void Dispose()
{
}
2015-04-09 05:20:23 +00:00
public string Name
{
get { return "GDI"; }
}
2015-10-26 05:29:32 +00:00
public bool SupportsImageCollageCreation
{
get { return true; }
}
public bool SupportsImageEncoding
{
get { return true; }
}
2015-04-08 14:38:02 +00:00
}
}