Support webp image output

pull/702/head
Luke Pulverenti 10 years ago
parent f4781b0bae
commit d2494148f7

@ -71,39 +71,6 @@ namespace MediaBrowser.Controller.Drawing
return Encoders.Length == 0 ? null : Encoders[0]; 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> /// <summary>
/// Crops an image by removing whitespace and transparency from the edges /// Crops an image by removing whitespace and transparency from the edges
/// </summary> /// </summary>

@ -1,4 +1,5 @@
using MediaBrowser.Common.Extensions; using Imazen.WebP;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Controller; using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Drawing;
@ -210,7 +211,12 @@ namespace MediaBrowser.Server.Implementations.Drawing
var newHeight = Convert.ToInt32(newSize.Height); var newHeight = Convert.ToInt32(newSize.Height);
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here // 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 // Mono throw an exeception if assign 0 to SetResolution
if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0) if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
@ -242,8 +248,15 @@ namespace MediaBrowser.Server.Implementations.Drawing
// Save to the cache location // Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false)) using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
{ {
// Save to the memory stream if (options.OutputFormat == ImageOutputFormat.Webp)
thumbnail.Save(outputFormat, cacheFileStream, quality); {
new SimpleEncoder().Encode(thumbnail, cacheFileStream, quality, false);
}
else
{
// Save to the memory stream
thumbnail.Save(outputFormat, cacheFileStream, quality);
}
} }
return cacheFilePath; 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> /// <summary>
/// Sets the color of the background. /// Sets the color of the background.
/// </summary> /// </summary>

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

@ -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>

@ -145,6 +145,14 @@
<None Include="app.config" /> <None Include="app.config" />
</ItemGroup> </ItemGroup>
<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"> <Content Include="..\ThirdParty\SQLite3\windows\x86\3.8.2\sqlite3.dll">
<Link>sqlite3.dll</Link> <Link>sqlite3.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@ -165,5 +173,9 @@
<Link>System.Data.SQLite.dll.config</Link> <Link>System.Data.SQLite.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Imazen.WebP.dll.config">
<Link>Imazen.WebP.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save