aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/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.Controller/Drawing
parent00da34b90a2f2fcee4d6aa584e25fccebb375b6d (diff)
parent9df9723fa8554df0fd51d777d3f781b0136de926 (diff)
Merge pull request #954 from MediaBrowser/dev
3.0.5462.0
Diffstat (limited to 'MediaBrowser.Controller/Drawing')
-rw-r--r--MediaBrowser.Controller/Drawing/IImageProcessor.cs2
-rw-r--r--MediaBrowser.Controller/Drawing/ImageExtensions.cs220
-rw-r--r--MediaBrowser.Controller/Drawing/ImageFormat.cs11
-rw-r--r--MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs2
-rw-r--r--MediaBrowser.Controller/Drawing/ImageStream.cs28
5 files changed, 30 insertions, 233 deletions
diff --git a/MediaBrowser.Controller/Drawing/IImageProcessor.cs b/MediaBrowser.Controller/Drawing/IImageProcessor.cs
index 3bd333527..8ac7d56d2 100644
--- a/MediaBrowser.Controller/Drawing/IImageProcessor.cs
+++ b/MediaBrowser.Controller/Drawing/IImageProcessor.cs
@@ -94,6 +94,6 @@ namespace MediaBrowser.Controller.Drawing
/// Gets the supported image output formats.
/// </summary>
/// <returns>ImageOutputFormat[].</returns>
- ImageOutputFormat[] GetSupportedImageOutputFormats();
+ ImageFormat[] GetSupportedImageOutputFormats();
}
}
diff --git a/MediaBrowser.Controller/Drawing/ImageExtensions.cs b/MediaBrowser.Controller/Drawing/ImageExtensions.cs
deleted file mode 100644
index 2511659c3..000000000
--- a/MediaBrowser.Controller/Drawing/ImageExtensions.cs
+++ /dev/null
@@ -1,220 +0,0 @@
-using System;
-using System.Drawing;
-using System.Drawing.Drawing2D;
-using System.Drawing.Imaging;
-using System.IO;
-
-namespace MediaBrowser.Controller.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, System.Drawing.Imaging.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 (System.Drawing.Imaging.ImageFormat.Jpeg.Equals(outputFormat))
- {
- SaveAsJpeg(image, toStream, quality);
- }
- else if (System.Drawing.Imaging.ImageFormat.Png.Equals(outputFormat))
- {
- image.Save(toStream, System.Drawing.Imaging.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.Controller/Drawing/ImageFormat.cs b/MediaBrowser.Controller/Drawing/ImageFormat.cs
deleted file mode 100644
index f78562556..000000000
--- a/MediaBrowser.Controller/Drawing/ImageFormat.cs
+++ /dev/null
@@ -1,11 +0,0 @@
-
-namespace MediaBrowser.Controller.Drawing
-{
- public enum ImageFormat
- {
- Jpg,
- Png,
- Gif,
- Bmp
- }
-}
diff --git a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs
index b99186f37..d8f5d52c3 100644
--- a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs
+++ b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs
@@ -29,7 +29,7 @@ namespace MediaBrowser.Controller.Drawing
public List<IImageEnhancer> Enhancers { get; set; }
- public ImageOutputFormat OutputFormat { get; set; }
+ public ImageFormat OutputFormat { get; set; }
public bool AddPlayedIndicator { get; set; }
diff --git a/MediaBrowser.Controller/Drawing/ImageStream.cs b/MediaBrowser.Controller/Drawing/ImageStream.cs
new file mode 100644
index 000000000..353abaca3
--- /dev/null
+++ b/MediaBrowser.Controller/Drawing/ImageStream.cs
@@ -0,0 +1,28 @@
+using MediaBrowser.Model.Drawing;
+using System;
+using System.IO;
+
+namespace MediaBrowser.Controller.Drawing
+{
+ public class ImageStream : IDisposable
+ {
+ /// <summary>
+ /// Gets or sets the stream.
+ /// </summary>
+ /// <value>The stream.</value>
+ public Stream Stream { get; set; }
+ /// <summary>
+ /// Gets or sets the format.
+ /// </summary>
+ /// <value>The format.</value>
+ public ImageFormat Format { get; set; }
+
+ public void Dispose()
+ {
+ if (Stream != null)
+ {
+ Stream.Dispose();
+ }
+ }
+ }
+}