Support webp image output

This commit is contained in:
Luke Pulverenti 2014-08-28 00:32:45 -04:00
parent f4781b0bae
commit d2494148f7
5 changed files with 42 additions and 65 deletions

View file

@ -71,39 +71,6 @@ namespace MediaBrowser.Controller.Drawing
return Encoders.Length == 0 ? null : Encoders[0];
}
/// <summary>
/// Determines whether [is pixel format supported by graphics object] [the specified format].
/// </summary>
/// <param name="format">The format.</param>
/// <returns><c>true</c> if [is pixel format supported by graphics object] [the specified format]; otherwise, <c>false</c>.</returns>
public static bool IsPixelFormatSupportedByGraphicsObject(PixelFormat format)
{
// http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx
if ((format & PixelFormat.Indexed) == PixelFormat.Indexed)
{
return false;
}
if ((format & PixelFormat.Undefined) == PixelFormat.Undefined)
{
return false;
}
if ((format & PixelFormat.DontCare) == PixelFormat.DontCare)
{
return false;
}
if ((format & PixelFormat.Format16bppArgb1555) == PixelFormat.Format16bppArgb1555)
{
return false;
}
if ((format & PixelFormat.Format16bppGrayScale) == PixelFormat.Format16bppGrayScale)
{
return false;
}
return true;
}
/// <summary>
/// Crops an image by removing whitespace and transparency from the edges
/// </summary>

View file

@ -1,4 +1,5 @@
using MediaBrowser.Common.Extensions;
using Imazen.WebP;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
@ -210,7 +211,12 @@ namespace MediaBrowser.Server.Implementations.Drawing
var newHeight = Convert.ToInt32(newSize.Height);
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
using (var thumbnail = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppPArgb))
// Also, Webp only supports Format32bppArgb and Format32bppRgb
var pixelFormat = options.OutputFormat == ImageOutputFormat.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)
@ -242,8 +248,15 @@ namespace MediaBrowser.Server.Implementations.Drawing
// Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
{
// Save to the memory stream
thumbnail.Save(outputFormat, cacheFileStream, quality);
if (options.OutputFormat == ImageOutputFormat.Webp)
{
new SimpleEncoder().Encode(thumbnail, cacheFileStream, quality, false);
}
else
{
// Save to the memory stream
thumbnail.Save(outputFormat, cacheFileStream, quality);
}
}
return cacheFilePath;
@ -260,31 +273,6 @@ namespace MediaBrowser.Server.Implementations.Drawing
}
}
/// <summary>
/// Caches the resized image.
/// </summary>
/// <param name="cacheFilePath">The cache file path.</param>
/// <param name="bytes">The bytes.</param>
/// <returns>Task.</returns>
private async Task CacheResizedImage(string cacheFilePath, byte[] bytes)
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
// Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
{
// Save to the filestream
await cacheFileStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error writing to image cache file {0}", ex, cacheFilePath);
}
}
/// <summary>
/// Sets the color of the background.
/// </summary>

View file

@ -45,6 +45,10 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="Imazen.WebP, Version=0.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\ThirdParty\libwebp\Imazen.WebP.dll</HintPath>
</Reference>
<Reference Include="Mono.Nat, Version=1.2.21.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Mono.Nat.1.2.21.0\lib\net40\Mono.Nat.dll</HintPath>
@ -52,9 +56,6 @@
<Reference Include="MoreLinq">
<HintPath>..\packages\morelinq.1.0.16006\lib\net35\MoreLinq.dll</HintPath>
</Reference>
<Reference Include="Nowin">
<HintPath>..\ThirdParty\Nowin\Nowin.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Api.Swagger">
<HintPath>..\ThirdParty\ServiceStack\ServiceStack.Api.Swagger.dll</HintPath>
</Reference>
@ -392,6 +393,10 @@
<EmbeddedResource Include="Localization\Ratings\ca.txt" />
</ItemGroup>
<ItemGroup>
<Content Include="..\ThirdParty\libwebp\windows\x86\libwebp.dll">
<Link>libwebp.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ThirdParty\ServiceStack\swagger-ui\css\highlight.default.css">
<Link>swagger-ui\css\highlight.default.css</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View file

@ -0,0 +1,5 @@
<configuration>
<dllmap dll="libwebp" target="./libwebp/linux/lib/libwebp.so" os="linux"/>
<dllmap dll="libwebp" target="./libwebp/linux/lib64/libwebp.so" os="linux"/>
<dllmap dll="libwebp" target="./libwebp/mac/libwebp.dylib" os="osx"/>
</configuration>

View file

@ -145,6 +145,14 @@
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Content Include="..\ThirdParty\libwebp\linux\x86\libwebp.so">
<Link>libwebp\linux\lib\libwebp.so</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ThirdParty\libwebp\linux\x86_64\libwebp.so">
<Link>libwebp\linux\lib64\libwebp.so</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\ThirdParty\SQLite3\windows\x86\3.8.2\sqlite3.dll">
<Link>sqlite3.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@ -165,5 +173,9 @@
<Link>System.Data.SQLite.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Imazen.WebP.dll.config">
<Link>Imazen.WebP.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>