Skip to content
  • Rate limit · GitHub

    Whoa there!

    You have triggered an abuse detection mechanism.

    Please wait a few minutes before you try again;
    in some cases this may take up to an hour.

  • Notifications You must be signed in to change notification settings
  • Fork 857

Commit

Permalink
Initial CAIR plugin addition
Browse files Browse the repository at this point in the history
Former-commit-id: e120f457ff05251ec312fc546e70ac3b09febbe9
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

JimBobSquarePants committed Sep 1, 2014
1 parent dd5d2f6 commit ef21a55
Showing 49 changed files with 1,630 additions and 21 deletions.
8 changes: 7 additions & 1 deletion src/ImageProcessor.Web/Processors/Resize.cs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public class Resize : IWebGraphicsProcessor
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(width|height)=|(width|height)ratio=|mode=|anchor=|center=|upscale=", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"(width|height)=|(width|height)ratio=|mode=(carve)?|anchor=|center=|upscale=", RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the size attribute.
@@ -111,6 +111,12 @@ public int MatchRegexIndex(string queryString)
{
if (match.Success)
{
// We don't want any resize carve requests to interfere.
if (match.Value.ToUpperInvariant().Contains("CARVE"))
{
break;
}

if (index == 0)
{
// Set the index on the first instance only.
17 changes: 17 additions & 0 deletions src/ImageProcessor.sln
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web.Tests", "Web.Test\Web.T
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_Website_MVC", "TestWebsites\MVC\Test_Website_MVC.csproj", "{F6A208E9-C18F-43E9-B051-3C6EED30FDAF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessor.Plugins.Cair", "Plugins\ImageProcessor\ImageProcessor.Plugins.Cair\ImageProcessor.Plugins.Cair.csproj", "{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
All|Any CPU = All|Any CPU
@@ -164,6 +166,21 @@ Global
{F6A208E9-C18F-43E9-B051-3C6EED30FDAF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F6A208E9-C18F-43E9-B051-3C6EED30FDAF}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{F6A208E9-C18F-43E9-B051-3C6EED30FDAF}.Release|x86.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.All|Any CPU.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.All|Any CPU.Build.0 = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.All|Mixed Platforms.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.All|Mixed Platforms.Build.0 = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.All|x86.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Debug|x86.ActiveCfg = Debug|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Release|Any CPU.Build.0 = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{C7D1F0DD-CBD6-4127-82B4-51949EFF0BF5}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
4 changes: 4 additions & 0 deletions src/ImageProcessorConsole/ImageProcessorConsole.csproj
Original file line number Diff line number Diff line change
@@ -56,6 +56,10 @@
<Project>{3B5DD734-FB7A-487D-8CE6-55E7AF9AEA7E}</Project>
<Name>ImageProcessor</Name>
</ProjectReference>
<ProjectReference Include="..\Plugins\ImageProcessor\ImageProcessor.Plugins.Cair\ImageProcessor.Plugins.Cair.csproj">
<Project>{c7d1f0dd-cbd6-4127-82b4-51949eff0bf5}</Project>
<Name>ImageProcessor.Plugins.Cair</Name>
</ProjectReference>
<ProjectReference Include="..\Plugins\ImageProcessor\ImageProcessor.Plugins.WebP\ImageProcessor.Plugins.WebP.csproj">
<Project>{2cf69699-959a-44dc-a281-4e2596c25043}</Project>
<Name>ImageProcessor.Plugins.WebP</Name>
12 changes: 8 additions & 4 deletions src/ImageProcessorConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ namespace ImageProcessorConsole
using System.IO;
using System.Linq;
using ImageProcessor;
using ImageProcessor.Plugins.Cair;

/// <summary>
/// The program.
@@ -40,8 +41,8 @@ public static void Main(string[] args)
di.Create();
}

//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jpg");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jpg");
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png");

foreach (FileInfo fileInfo in files)
{
@@ -52,11 +53,14 @@ public static void Main(string[] args)
{
using (ImageFactory imageFactory = new ImageFactory(true))
{
Size size = new Size(200, 200);
Size size = new Size(800, 0);

// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Constrain(size)
//.BackgroundColor(Color.White)
//.Resize(new Size((int)(size.Width * 1.1), 0))
.ContentAwareResize(size)
//.Constrain(size)
.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7f6786517fa67c9dfccac831e8491e4d08866d24
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7eb554b7681119b1170b70f1c1c2fc0de28a60ac
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8d39e89104598b85035a2c29592b847edcfdb8ac
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ff6cd6bf1cd388e8933071ec36880c33d1bc08f2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
68e14cae5c31c58b8b66d26bca6eb67c79ecd203
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c679b986c1ccbd9f6f8d72d7e4717f380b923a75
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6b1252209f60025427722f765dbbc271d125114e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
66b1f9f5ef7ca628ca4a44de9188e11e4af17229
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
414f5a48bd8d020beae85c2dee480cb5d22ec9b7
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a2cccdee0f806b2d41bfcdbb2dc9a1fe3d8656c6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
99704952050f56a69f591220bb9f0f2014215c7f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
98c3f8fce4fdd9eebafe12431c8e014fd39d243f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30ec5c05548fd350f9b7c699715848b9fbfb8ca9
e41022eb5fe724ba5fe2b7aeda29a1f4b6878484
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
33f7e25da5675197f18bb2fa2c9fe5fa366e84ec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f24f17627804b11d823240d49c91ec17fb7fd55d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def19dd7469cf2eba1f595e7263d9d48fda85825
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8cf61b55acca1a3d1a69177ddc439685ee0ba192
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
629284a6f834f5d1cf185e3603a50c484dd9121a
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fdc62fc2d056ab885eb9e8fd12b9155ee86d7c43
f5681324d193a42492647f4952057c2b72026096
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3d652e0628f2042e7aca0751384ba21f1182ef5f
4 changes: 2 additions & 2 deletions src/ImageProcessorConsole/images/output/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d8d3354b684cad79d9a46f92ed3a58747a808ad1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1c48519b2675b9dd90e8e95d6421148b25b8c317
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23a1c81a2d1422076373796e0c47f5d968c56d0b
b65d9e937cad7ddd02ff0d01a3be2f09c55e63e8
3 changes: 3 additions & 0 deletions src/ImageProcessorConsole/images/output/night-bridge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions src/ImageProcessorConsole/images/output/rotate.jpg

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a0aa8338f74f3539c2531f43fa9027a3ce0a39fb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
04f6093d3345a4c8b48333c32972753dd0949730
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f7b1543810ada7598773d6ee31bc7c0c20afaa9f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2d5b165c27545a3860d8a6b35f8ac4438f57593a
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CairBootstrapper.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Defines the CairBootstrapper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ImageProcessor.Plugins.Cair
{
using System;
using System.IO;
using System.Reflection;

using ImageProcessor.Configuration;

/// <summary>
/// The cair bootstrapper.
/// </summary>
internal static class CairBootstrapper
{
/// <summary>
/// Initializes static members of the <see cref="CairBootstrapper"/> class.
/// </summary>
static CairBootstrapper()
{
RegisterCairExecutable();
}

/// <summary>
/// Gets the cair path.
/// </summary>
public static string CairPath { get; private set; }

/// <summary>
/// Gets the cair image path.
/// </summary>
public static string CairImagePath { get; private set; }

/// <summary>
/// Registers the embedded CAIR executable.
/// </summary>
public static void RegisterCairExecutable()
{
// None of the tools used here are called using dllimport so we don't go through the normal registration channel.
string folder = ImageProcessorBootstrapper.Instance.NativeBinaryFactory.Is64BitEnvironment ? "x64" : "x86";
Assembly assembly = Assembly.GetExecutingAssembly();
string targetBasePath = new Uri(assembly.Location).LocalPath;
string multithreaderTargetPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "pthreadVSE2.dll"));

// Set the global variable.
CairPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "CAIR.exe"));
CairImagePath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "cairimages\\"));

// Get the resources and copy them across.
const string CairResourcePath = "ImageProcessor.Plugins.Cair.Resources.Unmanaged.x86.CAIR.exe";
const string MultithreaderResourcePath = "ImageProcessor.Plugins.Cair.Resources.Unmanaged.x86.pthreadVSE2.dll";

// Write the two files out to the bin folder.
// Copy out the threading binary.
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(MultithreaderResourcePath))
{
if (resourceStream != null)
{
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo threaderDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(multithreaderTargetPath));
if (!threaderDirectoryInfo.Exists)
{
threaderDirectoryInfo.Create();
}

using (FileStream fileStream = File.OpenWrite(multithreaderTargetPath))
{
resourceStream.CopyTo(fileStream);
}
}
}

// Copy out the cair executable.
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(CairResourcePath))
{
if (resourceStream != null)
{
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo cairDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(CairPath));
if (!cairDirectoryInfo.Exists)
{
cairDirectoryInfo.Create();
}

using (FileStream fileStream = File.OpenWrite(CairPath))
{
resourceStream.CopyTo(fileStream);
}
}
}

// Lastly create the image folder for storing temporary images.
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(CairImagePath));
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageFactoryExtensions.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Extends the ImageFactory class to provide a fluent API.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ImageProcessor.Plugins.Cair
{
using System.Collections.Generic;
using System.Drawing;

using ImageProcessor.Plugins.Cair.Imaging;
using ImageProcessor.Plugins.Cair.Processors;

/// <summary>
/// Extends the ImageFactory class to provide a fluent API.
/// </summary>
public static class ImageFactoryExtensions
{
/// <summary>
/// Resizes the current image to the given dimensions using Content Aware Resizing.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class
/// that this method extends.
/// </param>
/// <param name="size">
/// The <see cref="T:System.Drawing.Size"/> containing the width and height to set the image to.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public static ImageFactory ContentAwareResize(this ImageFactory factory, Size size)
{
if (factory.ShouldProcess)
{
int width = size.Width;
int height = size.Height;

ContentAwareResizeLayer resizeLayer = new ContentAwareResizeLayer(new Size(width, height));
return factory.ContentAwareResize(resizeLayer);
}

return factory;
}

/// <summary>
/// Resizes the current image to the given dimensions using Content Aware Resizing.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class
/// that this method extends.
/// </param>
/// <param name="layer">
/// The <see cref="ContentAwareResizeLayer"/> containing the properties required to resize the image.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public static ImageFactory ContentAwareResize(this ImageFactory factory, ContentAwareResizeLayer layer)
{
if (factory.ShouldProcess)
{
Dictionary<string, string> resizeSettings = new Dictionary<string, string> { { "MaxWidth", layer.Size.Width.ToString("G") }, { "MaxHeight", layer.Size.Height.ToString("G") } };

ContentAwareResize resize = new ContentAwareResize { DynamicParameter = layer, Settings = resizeSettings };
factory.CurrentImageFormat.ApplyProcessor(resize.ProcessImage, factory);
}

return factory;
}
}
}
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

0 comments on commit ef21a55

Please sign in to comment.