Browse Source

Funky Nuget stuff

Still can't copy unmanaged binaries across.


Former-commit-id: 7e31a7b715087b080fb5d38c225d662dd6cb91bb
pull/17/head
James South 12 years ago
parent
commit
de3d06f207
  1. 4
      build/NuSpecs/ImageProcessor.nuspec
  2. 10
      build/content/ImageProcessor/imageprocessor.targets
  3. 5
      src/ImageProcessor/ImageProcessor.csproj
  4. 100
      src/ImageProcessor/Imaging/Formats/NativeMethods.cs
  5. 60
      src/ImageProcessor/Imaging/Formats/WebPFormat.cs
  6. 1
      src/ImageProcessor/libwebp.dll.REMOVED.git-id
  7. 1
      src/ImageProcessor/x64/libwebp.dll.REMOVED.git-id
  8. 1
      src/ImageProcessor/x86/libwebp.dll.REMOVED.git-id
  9. 2
      src/ImageProcessorConsole/Program.cs
  10. 1
      src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id
  11. BIN
      src/ImageProcessorConsole/images/output/4.sm.webp
  12. 1
      src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id
  13. BIN
      src/ImageProcessorConsole/images/output/circle.webp
  14. 1
      src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id
  15. BIN
      src/ImageProcessorConsole/images/output/rotate.webp
  16. BIN
      src/ImageProcessorConsole/images/output/test.webp

4
build/NuSpecs/ImageProcessor.nuspec

@ -25,6 +25,8 @@ Feedback is always welcome.</description>
</metadata>
<files>
<file src="..\_BuildOutput\ImageProcessor\lib\ImageProcessor.dll" target="lib\ImageProcessor.dll" />
<file src="..\_BuildOutput\ImageProcessor\lib\libwebp.dll" target="lib\libwebp.dll" />
<file src="..\_BuildOutput\ImageProcessor\lib\x86\libwebp.dll" target="build\native\lib\x86\libwebp.dll" />
<file src="..\_BuildOutput\ImageProcessor\lib\x64\libwebp.dll" target="build\native\lib\x64\libwebp.dll" />
<file src="..\content\ImageProcessor\imageprocessor.targets" target="build\native\imageprocessor.targets" />
</files>
</package>

10
build/content/ImageProcessor/imageprocessor.targets

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This copies the native binaries to the bin folder. -->
<Target Name="Init" BeforeTargets="BeforeBuild">
<ItemGroup>
<NativeBinaries Include="$(MSBuildThisFileDirectory)..\..\build\native\lib\**\*" />
</ItemGroup>
<Copy SourceFiles="%(NativeBinaries.FullPath)" DestinationFiles="bin\%(RecursiveDir)%(Filename)%(Extension)" />
</Target>
</Project>

5
src/ImageProcessor/ImageProcessor.csproj

@ -130,7 +130,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="libwebp.dll">
<Content Include="x64\libwebp.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="x86\libwebp.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

100
src/ImageProcessor/Imaging/Formats/NativeMethods.cs

@ -37,8 +37,53 @@ namespace ImageProcessor.Imaging.Formats
/// <returns>
/// 1 if success, otherwise error code returned in the case of (a) formatting error(s).
/// </returns>
[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPGetInfo(IntPtr data, uint dataSize, out int width, out int height);
[DllImport("x86\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPGetInfo")]
public static extern int WebPGetInfo86(IntPtr data, uint dataSize, out int width, out int height);
/// <summary>
/// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant
/// </summary>
/// <param name="data">
/// Pointer to WebP image data
/// </param>
/// <param name="dataSize">
/// This is the size of the memory block pointed to by data containing the image data
/// </param>
/// <param name="width">
/// The width range is limited currently from 1 to 16383
/// </param>
/// <param name="height">
/// The height range is limited currently from 1 to 16383
/// </param>
/// <returns>
/// 1 if success, otherwise error code returned in the case of (a) formatting error(s).
/// </returns>
[DllImport("x64\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPGetInfo")]
public static extern int WebPGetInfo64(IntPtr data, uint dataSize, out int width, out int height);
/// <summary>
/// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer
/// </summary>
/// <param name="data">
/// Pointer to WebP image data
/// </param>
/// <param name="dataSize">
/// This is the size of the memory block pointed to by data containing the image data
/// </param>
/// <param name="outputBuffer">
/// Pointer to decoded WebP image
/// </param>
/// <param name="outputBufferSize">
/// Size of allocated buffer
/// </param>
/// <param name="outputStride">
/// Specifies the distance between scan-lines
/// </param>
/// <returns>
/// output_buffer if function succeeds; NULL otherwise
/// </returns>
[DllImport("x86\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPDecodeBGRAInto")]
public static extern IntPtr WebPDecodeBGRAInto86(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride);
/// <summary>
/// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer
@ -61,8 +106,35 @@ namespace ImageProcessor.Imaging.Formats
/// <returns>
/// output_buffer if function succeeds; NULL otherwise
/// </returns>
[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr WebPDecodeBGRAInto(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride);
[DllImport("x64\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPDecodeBGRAInto")]
public static extern IntPtr WebPDecodeBGRAInto64(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride);
/// <summary>
/// Lossy encoding images pointed to by *data in WebP format
/// </summary>
/// <param name="rgb">
/// Pointer to RGB image data
/// </param>
/// <param name="width">
/// The width range is limited currently from 1 to 16383
/// </param>
/// <param name="height">
/// The height range is limited currently from 1 to 16383
/// </param>
/// <param name="stride">
/// The stride.
/// </param>
/// <param name="qualityFactor">
/// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression
/// </param>
/// <param name="output">
/// output_buffer with WebP image
/// </param>
/// <returns>
/// Size of WebP Image
/// </returns>
[DllImport("x86\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeBGRA")]
public static extern int WebPEncodeBGRA86(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
/// <summary>
/// Lossy encoding images pointed to by *data in WebP format
@ -88,8 +160,20 @@ namespace ImageProcessor.Imaging.Formats
/// <returns>
/// Size of WebP Image
/// </returns>
[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPEncodeBGRA(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
[DllImport("x64\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeBGRA")]
public static extern int WebPEncodeBGRA64(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
/// <summary>
/// Frees the unmanaged memory.
/// </summary>
/// <param name="pointer">
/// The pointer.
/// </param>
/// <returns>
/// 1 if success, otherwise error code returned in the case of (a) error(s).
/// </returns>
[DllImport("x86\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPFree")]
public static extern int WebPFree86(IntPtr pointer);
/// <summary>
/// Frees the unmanaged memory.
@ -100,8 +184,8 @@ namespace ImageProcessor.Imaging.Formats
/// <returns>
/// 1 if success, otherwise error code returned in the case of (a) error(s).
/// </returns>
[DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPFree(IntPtr pointer);
[DllImport("x64\\libwebp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPFree")]
public static extern int WebPFree64(IntPtr pointer);
#endregion
}
}

60
src/ImageProcessor/Imaging/Formats/WebPFormat.cs

@ -31,6 +31,12 @@ namespace ImageProcessor.Imaging.Formats
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
public class WebPFormat : FormatBase
{
/// <summary>
/// Whether the process is running in 63bit mode. Used for calling the correct dllimport method.
/// Clunky I know but I couldn't get dynamic methods to work.
/// </summary>
private static readonly bool Is64Bit = Environment.Is64BitProcess;
/// <summary>
/// Gets the file headers.
/// </summary>
@ -184,9 +190,19 @@ namespace ImageProcessor.Imaging.Formats
int width;
int height;
if (NativeMethods.WebPGetInfo(ptrData, dataSize, out width, out height) != 1)
if (Is64Bit)
{
if (NativeMethods.WebPGetInfo64(ptrData, dataSize, out width, out height) != 1)
{
throw new ImageFormatException("WebP image header is corrupted.");
}
}
else
{
throw new ImageFormatException("WebP image header is corrupted.");
if (NativeMethods.WebPGetInfo86(ptrData, dataSize, out width, out height) != 1)
{
throw new ImageFormatException("WebP image header is corrupted.");
}
}
try
@ -199,8 +215,17 @@ namespace ImageProcessor.Imaging.Formats
int outputBufferSize = bitmapData.Stride * height;
outputBuffer = Marshal.AllocHGlobal(outputBufferSize);
// Uncompress the image
outputBuffer = NativeMethods.WebPDecodeBGRAInto(ptrData, dataSize, outputBuffer, outputBufferSize, bitmapData.Stride);
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (Is64Bit)
{
// Uncompress the image
outputBuffer = NativeMethods.WebPDecodeBGRAInto64(ptrData, dataSize, outputBuffer, outputBufferSize, bitmapData.Stride);
}
else
{
// Uncompress the image
outputBuffer = NativeMethods.WebPDecodeBGRAInto86(ptrData, dataSize, outputBuffer, outputBufferSize, bitmapData.Stride);
}
if (bitmapData.Scan0 != outputBuffer)
{
@ -252,8 +277,19 @@ namespace ImageProcessor.Imaging.Formats
try
{
// Attempt to lossy encode the image.
int size = NativeMethods.WebPEncodeBGRA(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, quality, out unmanagedData);
int size;
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (Is64Bit)
{
// Attempt to lossy encode the image.
size = NativeMethods.WebPEncodeBGRA64(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, quality, out unmanagedData);
}
else
{
// Attempt to lossy encode the image.
size = NativeMethods.WebPEncodeBGRA86(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, quality, out unmanagedData);
}
// Copy image compress data to output array
webpData = new byte[size];
@ -269,8 +305,16 @@ namespace ImageProcessor.Imaging.Formats
// Unlock the pixels
bitmap.UnlockBits(bmpData);
// Free memory
NativeMethods.WebPFree(unmanagedData);
if (Is64Bit)
{
// Free memory
NativeMethods.WebPFree64(unmanagedData);
}
else
{
// Free memory
NativeMethods.WebPFree86(unmanagedData);
}
}
return encoded;

1
src/ImageProcessor/libwebp.dll.REMOVED.git-id

@ -1 +0,0 @@
ed0333da619a813e8337d77ba7ee206ea5f20a51

1
src/ImageProcessor/x64/libwebp.dll.REMOVED.git-id

@ -0,0 +1 @@
dcadab1b5b86469758707bff22558b33e5c51552

1
src/ImageProcessor/x86/libwebp.dll.REMOVED.git-id

@ -0,0 +1 @@
ad47008dd1e64ea220bc745e28f568277434d661

2
src/ImageProcessorConsole/Program.cs

@ -52,7 +52,7 @@ namespace ImageProcessorConsole
// ImageProcessor
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (ImageFactory imageFactory = new ImageFactory(true))
using (ImageFactory imageFactory = new ImageFactory())
{
Size size = new Size(200, 200);

1
src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id

@ -1 +0,0 @@
30ec5c05548fd350f9b7c699715848b9fbfb8ca9

BIN
src/ImageProcessorConsole/images/output/4.sm.webp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

1
src/ImageProcessorConsole/images/output/Tl4Yb.gif.REMOVED.git-id

@ -1 +0,0 @@
fdc62fc2d056ab885eb9e8fd12b9155ee86d7c43

BIN
src/ImageProcessorConsole/images/output/circle.webp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

1
src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id

@ -1 +0,0 @@
23a1c81a2d1422076373796e0c47f5d968c56d0b

BIN
src/ImageProcessorConsole/images/output/rotate.webp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
src/ImageProcessorConsole/images/output/test.webp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

Loading…
Cancel
Save