2012-09-18 19:33:57 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-09-18 22:32:37 +00:00
|
|
|
|
using System;
|
2012-09-18 21:39:42 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
2012-09-18 19:33:57 +00:00
|
|
|
|
using System.Drawing;
|
2012-09-18 21:39:42 +00:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2012-09-18 19:33:57 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a base image processor class that plugins can use to process images as they are being writen to http responses
|
|
|
|
|
/// Since this is completely modular with MEF, a plugin only needs to have a subclass in their assembly with the following attribute on the class:
|
|
|
|
|
/// [Export(typeof(BaseImageProcessor))]
|
|
|
|
|
/// This will require a reference to System.ComponentModel.Composition
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class BaseImageProcessor
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2012-09-18 22:32:37 +00:00
|
|
|
|
/// Processes an image for a BaseEntity
|
2012-09-18 19:33:57 +00:00
|
|
|
|
/// </summary>
|
2012-09-18 21:39:42 +00:00
|
|
|
|
/// <param name="originalImage">The original Image, before re-sizing</param>
|
2012-09-18 19:33:57 +00:00
|
|
|
|
/// <param name="bitmap">The bitmap holding the original image, after re-sizing</param>
|
|
|
|
|
/// <param name="graphics">The graphics surface on which the output is drawn</param>
|
|
|
|
|
/// <param name="entity">The entity that owns the image</param>
|
|
|
|
|
/// <param name="imageType">The image type</param>
|
|
|
|
|
/// <param name="imageIndex">The image index (currently only used with backdrops)</param>
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public abstract void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex);
|
2012-09-18 21:39:42 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true, the image output format will be forced to png, resulting in an output size that will generally run larger than jpg
|
2012-09-18 22:32:37 +00:00
|
|
|
|
/// If false, the original image format is preserved.
|
2012-09-18 21:39:42 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual bool RequiresTransparency
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-18 22:32:37 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if the image processor is configured to process the specified entity, image type and image index
|
|
|
|
|
/// This will aid http response caching so that we don't invalidate image caches when we don't have to
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract bool IsConfiguredToProcess(BaseEntity entity, ImageType imageType, int imageIndex);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is used for caching purposes, since a configuration change needs to invalidate a user's image cache
|
|
|
|
|
/// If the image processor is hosted within a plugin then this should be the plugin ConfigurationDateLastModified
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract DateTime ProcessingConfigurationDateLastModifiedUtc { get; }
|
2012-09-18 21:39:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is demo-ware and should be deleted eventually
|
|
|
|
|
/// </summary>
|
|
|
|
|
//[Export(typeof(BaseImageProcessor))]
|
|
|
|
|
public class MyRoundedCornerImageProcessor : BaseImageProcessor
|
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public override void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex)
|
2012-09-18 21:39:42 +00:00
|
|
|
|
{
|
|
|
|
|
var CornerRadius = 20;
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
graphics.Clear(Color.Transparent);
|
|
|
|
|
|
2012-09-18 21:39:42 +00:00
|
|
|
|
using (GraphicsPath gp = new GraphicsPath())
|
|
|
|
|
{
|
|
|
|
|
gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
|
|
|
|
|
gp.AddArc(0 + bitmap.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
|
|
|
|
|
gp.AddArc(0 + bitmap.Width - CornerRadius, 0 + bitmap.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
|
|
|
|
|
gp.AddArc(0, 0 + bitmap.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
|
2012-09-18 22:32:37 +00:00
|
|
|
|
|
|
|
|
|
graphics.SetClip(gp);
|
|
|
|
|
graphics.DrawImage(originalImage, 0, 0, bitmap.Width, bitmap.Height);
|
2012-09-18 21:39:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public override bool RequiresTransparency
|
2012-09-18 21:39:42 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-09-18 21:39:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 22:50:26 +00:00
|
|
|
|
private static DateTime testDate = DateTime.UtcNow;
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public override DateTime ProcessingConfigurationDateLastModifiedUtc
|
2012-09-18 21:39:42 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-18 22:50:26 +00:00
|
|
|
|
// This will result in a situation where images are only cached throughout a server session, but again, this is a prototype
|
|
|
|
|
return testDate;
|
2012-09-18 21:39:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-18 22:32:37 +00:00
|
|
|
|
|
|
|
|
|
public override bool IsConfiguredToProcess(BaseEntity entity, ImageType imageType, int imageIndex)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-09-18 19:33:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|