aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Drawing
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2014-12-14 00:38:07 -0500
committerLuke <luke.pulverenti@gmail.com>2014-12-14 00:38:07 -0500
commit524293ea79ab61228f8326561be70bcca4d0ea8f (patch)
treeccfe163c8edafc8dd14b0b63d48712a6d504de9d /MediaBrowser.Server.Implementations/Drawing
parent00da34b90a2f2fcee4d6aa584e25fccebb375b6d (diff)
parent9df9723fa8554df0fd51d777d3f781b0136de926 (diff)
Merge pull request #954 from MediaBrowser/dev
3.0.5462.0
Diffstat (limited to 'MediaBrowser.Server.Implementations/Drawing')
-rw-r--r--MediaBrowser.Server.Implementations/Drawing/ImageExtensions.cs220
-rw-r--r--MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs50
-rw-r--r--MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs99
3 files changed, 320 insertions, 49 deletions
diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageExtensions.cs b/MediaBrowser.Server.Implementations/Drawing/ImageExtensions.cs
new file mode 100644
index 000000000..28ea26a32
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/Drawing/ImageExtensions.cs
@@ -0,0 +1,220 @@
+using System;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Drawing.Imaging;
+using System.IO;
+
+namespace MediaBrowser.Server.Implementations.Drawing
+{
+ /// <summary>
+ /// Class ImageExtensions
+ /// </summary>
+ public static class ImageExtensions
+ {
+ /// <summary>
+ /// Saves the image.
+ /// </summary>
+ /// <param name="outputFormat">The output format.</param>
+ /// <param name="image">The image.</param>
+ /// <param name="toStream">To stream.</param>
+ /// <param name="quality">The quality.</param>
+ public static void Save(this Image image, ImageFormat outputFormat, Stream toStream, int quality)
+ {
+ // Use special save methods for jpeg and png that will result in a much higher quality image
+ // All other formats use the generic Image.Save
+ if (ImageFormat.Jpeg.Equals(outputFormat))
+ {
+ SaveAsJpeg(image, toStream, quality);
+ }
+ else if (ImageFormat.Png.Equals(outputFormat))
+ {
+ image.Save(toStream, ImageFormat.Png);
+ }
+ else
+ {
+ image.Save(toStream, outputFormat);
+ }
+ }
+
+ /// <summary>
+ /// Saves the JPEG.
+ /// </summary>
+ /// <param name="image">The image.</param>
+ /// <param name="target">The target.</param>
+ /// <param name="quality">The quality.</param>
+ public static void SaveAsJpeg(this Image image, Stream target, int quality)
+ {
+ using (var encoderParameters = new EncoderParameters(1))
+ {
+ encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
+ image.Save(target, GetImageCodecInfo("image/jpeg"), encoderParameters);
+ }
+ }
+
+ private static readonly ImageCodecInfo[] Encoders = ImageCodecInfo.GetImageEncoders();
+
+ /// <summary>
+ /// Gets the image codec info.
+ /// </summary>
+ /// <param name="mimeType">Type of the MIME.</param>
+ /// <returns>ImageCodecInfo.</returns>
+ private static ImageCodecInfo GetImageCodecInfo(string mimeType)
+ {
+ foreach (var encoder in Encoders)
+ {
+ if (string.Equals(encoder.MimeType, mimeType, StringComparison.OrdinalIgnoreCase))
+ {
+ return encoder;
+ }
+ }
+
+ return Encoders.Length == 0 ? null : Encoders[0];
+ }
+
+ /// <summary>
+ /// Crops an image by removing whitespace and transparency from the edges
+ /// </summary>
+ /// <param name="bmp">The BMP.</param>
+ /// <returns>Bitmap.</returns>
+ /// <exception cref="System.Exception"></exception>
+ public static Bitmap CropWhitespace(this Bitmap bmp)
+ {
+ var width = bmp.Width;
+ var height = bmp.Height;
+
+ var topmost = 0;
+ for (int row = 0; row < height; ++row)
+ {
+ if (IsAllWhiteRow(bmp, row, width))
+ topmost = row;
+ else break;
+ }
+
+ int bottommost = 0;
+ for (int row = height - 1; row >= 0; --row)
+ {
+ if (IsAllWhiteRow(bmp, row, width))
+ bottommost = row;
+ else break;
+ }
+
+ int leftmost = 0, rightmost = 0;
+ for (int col = 0; col < width; ++col)
+ {
+ if (IsAllWhiteColumn(bmp, col, height))
+ leftmost = col;
+ else
+ break;
+ }
+
+ for (int col = width - 1; col >= 0; --col)
+ {
+ if (IsAllWhiteColumn(bmp, col, height))
+ rightmost = col;
+ else
+ break;
+ }
+
+ if (rightmost == 0) rightmost = width; // As reached left
+ if (bottommost == 0) bottommost = height; // As reached top.
+
+ var croppedWidth = rightmost - leftmost;
+ var croppedHeight = bottommost - topmost;
+
+ if (croppedWidth == 0) // No border on left or right
+ {
+ leftmost = 0;
+ croppedWidth = width;
+ }
+
+ if (croppedHeight == 0) // No border on top or bottom
+ {
+ topmost = 0;
+ croppedHeight = height;
+ }
+
+ // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
+ var thumbnail = new Bitmap(croppedWidth, croppedHeight, PixelFormat.Format32bppPArgb);
+
+ // Preserve the original resolution
+ TrySetResolution(thumbnail, bmp.HorizontalResolution, bmp.VerticalResolution);
+
+ using (var thumbnailGraph = Graphics.FromImage(thumbnail))
+ {
+ thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
+ thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
+ thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
+ thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
+ thumbnailGraph.CompositingMode = CompositingMode.SourceCopy;
+
+ thumbnailGraph.DrawImage(bmp,
+ new RectangleF(0, 0, croppedWidth, croppedHeight),
+ new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
+ GraphicsUnit.Pixel);
+ }
+ return thumbnail;
+ }
+
+ /// <summary>
+ /// Tries the set resolution.
+ /// </summary>
+ /// <param name="bmp">The BMP.</param>
+ /// <param name="x">The x.</param>
+ /// <param name="y">The y.</param>
+ private static void TrySetResolution(Bitmap bmp, float x, float y)
+ {
+ if (x > 0 && y > 0)
+ {
+ bmp.SetResolution(x, y);
+ }
+ }
+
+ /// <summary>
+ /// Determines whether or not a row of pixels is all whitespace
+ /// </summary>
+ /// <param name="bmp">The BMP.</param>
+ /// <param name="row">The row.</param>
+ /// <param name="width">The width.</param>
+ /// <returns><c>true</c> if [is all white row] [the specified BMP]; otherwise, <c>false</c>.</returns>
+ private static bool IsAllWhiteRow(Bitmap bmp, int row, int width)
+ {
+ for (var i = 0; i < width; ++i)
+ {
+ if (!IsWhiteSpace(bmp.GetPixel(i, row)))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Determines whether or not a column of pixels is all whitespace
+ /// </summary>
+ /// <param name="bmp">The BMP.</param>
+ /// <param name="col">The col.</param>
+ /// <param name="height">The height.</param>
+ /// <returns><c>true</c> if [is all white column] [the specified BMP]; otherwise, <c>false</c>.</returns>
+ private static bool IsAllWhiteColumn(Bitmap bmp, int col, int height)
+ {
+ for (var i = 0; i < height; ++i)
+ {
+ if (!IsWhiteSpace(bmp.GetPixel(col, i)))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Determines if a color is whitespace
+ /// </summary>
+ /// <param name="color">The color.</param>
+ /// <returns><c>true</c> if [is white space] [the specified color]; otherwise, <c>false</c>.</returns>
+ private static bool IsWhiteSpace(Color color)
+ {
+ return (color.R == 255 && color.G == 255 && color.B == 255) || color.A == 0;
+ }
+ }
+}
diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs b/MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs
index 3d53d2b86..e9c67bf48 100644
--- a/MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs
+++ b/MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs
@@ -1,8 +1,8 @@
using MediaBrowser.Common.IO;
+using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
-using System.Drawing;
using System.IO;
using System.Linq;
@@ -24,7 +24,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <summary>
/// The image format decoders
/// </summary>
- private static readonly KeyValuePair<byte[], Func<BinaryReader, Size>>[] ImageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>>
+ private static readonly KeyValuePair<byte[], Func<BinaryReader, ImageSize>>[] ImageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, ImageSize>>
{
{ new byte[] { 0x42, 0x4D }, DecodeBitmap },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
@@ -44,7 +44,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="fileSystem">The file system.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>
- public static Size GetDimensions(string path, ILogger logger, IFileSystem fileSystem)
+ public static ImageSize GetDimensions(string path, ILogger logger, IFileSystem fileSystem)
{
try
{
@@ -71,9 +71,15 @@ namespace MediaBrowser.Server.Implementations.Drawing
memoryStream.Position = 0;
// Co it the old fashioned way
- using (var b = Image.FromStream(memoryStream, true, false))
+ using (var b = System.Drawing.Image.FromStream(memoryStream, true, false))
{
- return b.Size;
+ var size = b.Size;
+
+ return new ImageSize
+ {
+ Width = size.Width,
+ Height = size.Height
+ };
}
}
}
@@ -86,7 +92,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <returns>Size.</returns>
/// <exception cref="System.ArgumentException">binaryReader</exception>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
- private static Size GetDimensions(BinaryReader binaryReader)
+ private static ImageSize GetDimensions(BinaryReader binaryReader)
{
var magicBytes = new byte[MaxMagicBytesLength];
@@ -161,12 +167,16 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
- private static Size DecodeBitmap(BinaryReader binaryReader)
+ private static ImageSize DecodeBitmap(BinaryReader binaryReader)
{
binaryReader.ReadBytes(16);
int width = binaryReader.ReadInt32();
int height = binaryReader.ReadInt32();
- return new Size(width, height);
+ return new ImageSize
+ {
+ Width = width,
+ Height = height
+ };
}
/// <summary>
@@ -174,11 +184,15 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
- private static Size DecodeGif(BinaryReader binaryReader)
+ private static ImageSize DecodeGif(BinaryReader binaryReader)
{
int width = binaryReader.ReadInt16();
int height = binaryReader.ReadInt16();
- return new Size(width, height);
+ return new ImageSize
+ {
+ Width = width,
+ Height = height
+ };
}
/// <summary>
@@ -186,12 +200,16 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
- private static Size DecodePng(BinaryReader binaryReader)
+ private static ImageSize DecodePng(BinaryReader binaryReader)
{
binaryReader.ReadBytes(8);
int width = ReadLittleEndianInt32(binaryReader);
int height = ReadLittleEndianInt32(binaryReader);
- return new Size(width, height);
+ return new ImageSize
+ {
+ Width = width,
+ Height = height
+ };
}
/// <summary>
@@ -200,7 +218,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
/// <exception cref="System.ArgumentException"></exception>
- private static Size DecodeJfif(BinaryReader binaryReader)
+ private static ImageSize DecodeJfif(BinaryReader binaryReader)
{
while (binaryReader.ReadByte() == 0xff)
{
@@ -211,7 +229,11 @@ namespace MediaBrowser.Server.Implementations.Drawing
binaryReader.ReadByte();
int height = ReadLittleEndianInt16(binaryReader);
int width = ReadLittleEndianInt16(binaryReader);
- return new Size(width, height);
+ return new ImageSize
+ {
+ Width = width,
+ Height = height
+ };
}
if (chunkLength < 0)
diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs
index feda361c8..b141fea1e 100644
--- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs
+++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs
@@ -129,13 +129,13 @@ namespace MediaBrowser.Server.Implementations.Drawing
}
}
- public ImageOutputFormat[] GetSupportedImageOutputFormats()
+ public Model.Drawing.ImageFormat[] GetSupportedImageOutputFormats()
{
if (_webpAvailable)
{
- return new[] { ImageOutputFormat.Webp, ImageOutputFormat.Gif, ImageOutputFormat.Jpg, ImageOutputFormat.Png };
+ return new[] { Model.Drawing.ImageFormat.Webp, Model.Drawing.ImageFormat.Gif, Model.Drawing.ImageFormat.Jpg, Model.Drawing.ImageFormat.Png };
}
- return new[] { ImageOutputFormat.Gif, ImageOutputFormat.Jpg, ImageOutputFormat.Png };
+ return new[] { Model.Drawing.ImageFormat.Gif, Model.Drawing.ImageFormat.Jpg, Model.Drawing.ImageFormat.Png };
}
public async Task<string> ProcessImage(ImageProcessingOptions options)
@@ -227,7 +227,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
// 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 == ImageOutputFormat.Webp
+ var pixelFormat = selectedOutputFormat == Model.Drawing.ImageFormat.Webp
? PixelFormat.Format32bppArgb
: PixelFormat.Format32bppPArgb;
@@ -263,7 +263,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
// Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
{
- if (selectedOutputFormat == ImageOutputFormat.Webp)
+ if (selectedOutputFormat == Model.Drawing.ImageFormat.Webp)
{
SaveToWebP(thumbnail, cacheFileStream, quality);
}
@@ -381,17 +381,17 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="image">The image.</param>
/// <param name="outputFormat">The output format.</param>
/// <returns>ImageFormat.</returns>
- private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageOutputFormat outputFormat)
+ private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, Model.Drawing.ImageFormat outputFormat)
{
switch (outputFormat)
{
- case ImageOutputFormat.Bmp:
+ case Model.Drawing.ImageFormat.Bmp:
return System.Drawing.Imaging.ImageFormat.Bmp;
- case ImageOutputFormat.Gif:
+ case Model.Drawing.ImageFormat.Gif:
return System.Drawing.Imaging.ImageFormat.Gif;
- case ImageOutputFormat.Jpg:
+ case Model.Drawing.ImageFormat.Jpg:
return System.Drawing.Imaging.ImageFormat.Jpeg;
- case ImageOutputFormat.Png:
+ case Model.Drawing.ImageFormat.Png:
return System.Drawing.Imaging.ImageFormat.Png;
default:
return image.RawFormat;
@@ -471,7 +471,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <summary>
/// Gets the cache file path based on a set of parameters
/// </summary>
- private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, string backgroundColor)
+ private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, Model.Drawing.ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, string backgroundColor)
{
var filename = originalPath;
@@ -515,7 +515,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
filename += "b=" + backgroundColor;
}
- return GetCachePath(ResizedImageCachePath, filename, Path.GetExtension(originalPath));
+ return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
}
/// <summary>
@@ -772,19 +772,39 @@ namespace MediaBrowser.Server.Implementations.Drawing
{
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
- using (var originalImage = Image.FromStream(memoryStream, true, false))
+ memoryStream.Position = 0;
+
+ var imageStream = new ImageStream
{
- //Pass the image through registered enhancers
- using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
- {
- var parentDirectory = Path.GetDirectoryName(enhancedImagePath);
+ Stream = memoryStream,
+ Format = GetFormat(originalImagePath)
+ };
+
+ //Pass the image through registered enhancers
+ using (var newImageStream = await ExecuteImageEnhancers(supportedEnhancers, imageStream, item, imageType, imageIndex).ConfigureAwait(false))
+ {
+ var parentDirectory = Path.GetDirectoryName(enhancedImagePath);
- Directory.CreateDirectory(parentDirectory);
+ Directory.CreateDirectory(parentDirectory);
+ // Save as png
+ if (newImageStream.Format == Model.Drawing.ImageFormat.Png)
+ {
//And then save it in the cache
using (var outputStream = _fileSystem.GetFileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
{
- newImage.Save(System.Drawing.Imaging.ImageFormat.Png, outputStream, 100);
+ await newImageStream.Stream.CopyToAsync(outputStream).ConfigureAwait(false);
+ }
+ }
+ else
+ {
+ using (var newImage = Image.FromStream(newImageStream.Stream, true, false))
+ {
+ //And then save it in the cache
+ using (var outputStream = _fileSystem.GetFileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
+ {
+ newImage.Save(System.Drawing.Imaging.ImageFormat.Png, outputStream, 100);
+ }
}
}
}
@@ -799,6 +819,30 @@ namespace MediaBrowser.Server.Implementations.Drawing
return enhancedImagePath;
}
+ private Model.Drawing.ImageFormat GetFormat(string path)
+ {
+ var extension = Path.GetExtension(path);
+
+ if (string.Equals(extension, ".png", StringComparison.OrdinalIgnoreCase))
+ {
+ return Model.Drawing.ImageFormat.Png;
+ }
+ if (string.Equals(extension, ".gif", StringComparison.OrdinalIgnoreCase))
+ {
+ return Model.Drawing.ImageFormat.Gif;
+ }
+ if (string.Equals(extension, ".webp", StringComparison.OrdinalIgnoreCase))
+ {
+ return Model.Drawing.ImageFormat.Webp;
+ }
+ if (string.Equals(extension, ".bmp", StringComparison.OrdinalIgnoreCase))
+ {
+ return Model.Drawing.ImageFormat.Bmp;
+ }
+
+ return Model.Drawing.ImageFormat.Jpg;
+ }
+
/// <summary>
/// Executes the image enhancers.
/// </summary>
@@ -808,7 +852,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="imageType">Type of the image.</param>
/// <param name="imageIndex">Index of the image.</param>
/// <returns>Task{EnhancedImage}.</returns>
- private async Task<Image> ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, Image originalImage, IHasImages item, ImageType imageType, int imageIndex)
+ private async Task<ImageStream> ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, ImageStream originalImage, IHasImages item, ImageType imageType, int imageIndex)
{
var result = originalImage;
@@ -835,21 +879,6 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <summary>
/// The _semaphoreLocks
/// </summary>
- private readonly ConcurrentDictionary<string, object> _locks = new ConcurrentDictionary<string, object>();
-
- /// <summary>
- /// Gets the lock.
- /// </summary>
- /// <param name="filename">The filename.</param>
- /// <returns>System.Object.</returns>
- private object GetObjectLock(string filename)
- {
- return _locks.GetOrAdd(filename, key => new object());
- }
-
- /// <summary>
- /// The _semaphoreLocks
- /// </summary>
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
/// <summary>