Browse Source

Tidying up

Former-commit-id: 3f39654100cf1d43dc7be439cc0cca38f31334c0
af/merge-core
James South 12 years ago
parent
commit
0562e1c87c
  1. 47
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/CairBootstrapper.cs
  2. 42
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/ContentAwareResizeLayer.cs
  3. 4
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/ConvolutionType.cs
  4. 53
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/OutputType.cs
  5. 40
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Processors/ContentAwareResize.cs

47
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/CairBootstrapper.cs

@ -32,12 +32,12 @@ namespace ImageProcessor.Plugins.Cair
/// <summary> /// <summary>
/// Gets the cair path. /// Gets the cair path.
/// </summary> /// </summary>
public static string CairPath { get; private set; } public static string CairExecutablePath { get; private set; }
/// <summary> /// <summary>
/// Gets the cair image path. /// Gets the cair base path.
/// </summary> /// </summary>
public static string CairImagePath { get; private set; } public static string CairPath { get; private set; }
/// <summary> /// <summary>
/// Registers the embedded CAIR executable. /// Registers the embedded CAIR executable.
@ -47,12 +47,17 @@ namespace ImageProcessor.Plugins.Cair
// None of the tools used here are called using dllimport so we don't go through the normal registration channel. // 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"; string folder = ImageProcessorBootstrapper.Instance.NativeBinaryFactory.Is64BitEnvironment ? "x64" : "x86";
Assembly assembly = Assembly.GetExecutingAssembly(); Assembly assembly = Assembly.GetExecutingAssembly();
string targetBasePath = new Uri(assembly.Location).LocalPath; CairPath = Path.GetFullPath(Path.Combine(new Uri(assembly.Location).LocalPath, "..\\" + folder + "\\imageprocessor.cair\\"));
string multithreaderTargetPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "pthreadVSE2.dll")); CairExecutablePath = Path.Combine(CairPath, "CAIR.exe");
string multithreaderTargetPath = Path.Combine(CairPath, "pthreadVSE2.dll");
// Set the global variable. // Create the folder for storing temporary images.
CairPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "CAIR.exe")); // ReSharper disable once AssignNullToNotNullAttribute
CairImagePath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "cairimages\\")); DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(CairPath));
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
// Get the resources and copy them across. // Get the resources and copy them across.
const string CairResourcePath = "ImageProcessor.Plugins.Cair.Resources.Unmanaged.x86.CAIR.exe"; const string CairResourcePath = "ImageProcessor.Plugins.Cair.Resources.Unmanaged.x86.CAIR.exe";
@ -64,13 +69,6 @@ namespace ImageProcessor.Plugins.Cair
{ {
if (resourceStream != null) 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)) using (FileStream fileStream = File.OpenWrite(multithreaderTargetPath))
{ {
resourceStream.CopyTo(fileStream); resourceStream.CopyTo(fileStream);
@ -83,27 +81,12 @@ namespace ImageProcessor.Plugins.Cair
{ {
if (resourceStream != null) if (resourceStream != null)
{ {
// ReSharper disable once AssignNullToNotNullAttribute using (FileStream fileStream = File.OpenWrite(CairExecutablePath))
DirectoryInfo cairDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(CairPath));
if (!cairDirectoryInfo.Exists)
{
cairDirectoryInfo.Create();
}
using (FileStream fileStream = File.OpenWrite(CairPath))
{ {
resourceStream.CopyTo(fileStream); 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();
}
} }
} }
} }

42
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/ContentAwareResizeLayer.cs

@ -20,7 +20,7 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// <summary> /// <summary>
/// The convolution type to apply to the layer. /// The convolution type to apply to the layer.
/// </summary> /// </summary>
private ContentAwareResizeConvolutionType convolutionType = ContentAwareResizeConvolutionType.Prewitt; private ConvolutionType convolutionType = ConvolutionType.Prewitt;
/// <summary> /// <summary>
/// The energy function to apply to the layer. /// The energy function to apply to the layer.
@ -28,14 +28,14 @@ namespace ImageProcessor.Plugins.Cair.Imaging
private EnergyFunction energyFunction = EnergyFunction.Forward; private EnergyFunction energyFunction = EnergyFunction.Forward;
/// <summary> /// <summary>
/// Whether to assign multiple threads to the resizing method. /// The expected output type.
/// </summary> /// </summary>
private bool parallelize = true; private OutputType outputType = OutputType.Cair;
/// <summary> /// <summary>
/// Whether to pre-scale the image to reduce errors in the output. /// Whether to assign multiple threads to the resizing method.
/// </summary> /// </summary>
private bool prescale = true; private bool parallelize = true;
/// <summary> /// <summary>
/// The timeout in milliseconds to attempt to resize for. /// The timeout in milliseconds to attempt to resize for.
@ -61,7 +61,7 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// <summary> /// <summary>
/// Gets or sets the content aware resize convolution type (Default ContentAwareResizeConvolutionType.Prewitt). /// Gets or sets the content aware resize convolution type (Default ContentAwareResizeConvolutionType.Prewitt).
/// </summary> /// </summary>
public ContentAwareResizeConvolutionType ConvolutionType public ConvolutionType ConvolutionType
{ {
get get
{ {
@ -91,36 +91,35 @@ namespace ImageProcessor.Plugins.Cair.Imaging
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to assign multiple threads to the resizing method. /// Gets or sets the expected output type.
/// (Default true)
/// </summary> /// </summary>
public bool Parallelize public OutputType OutputType
{ {
get get
{ {
return this.parallelize; return this.outputType;
} }
set set
{ {
this.parallelize = value; this.outputType = value;
} }
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to pre-scale the image to reduce errors in the output. /// Gets or sets a value indicating whether to assign multiple threads to the resizing method.
/// (Default true) /// (Default true)
/// </summary> /// </summary>
public bool PreScale public bool Parallelize
{ {
get get
{ {
return this.prescale; return this.parallelize;
} }
set set
{ {
this.prescale = value; this.parallelize = value;
} }
} }
@ -162,7 +161,11 @@ namespace ImageProcessor.Plugins.Cair.Imaging
} }
return this.Size == resizeLayer.Size return this.Size == resizeLayer.Size
&& this.ConvolutionType == resizeLayer.ConvolutionType; && this.ConvolutionType == resizeLayer.ConvolutionType
&& this.EnergyFunction == resizeLayer.EnergyFunction
&& this.OutputType == resizeLayer.OutputType
&& this.Parallelize == resizeLayer.Parallelize
&& this.Timeout == resizeLayer.Timeout;
} }
/// <summary> /// <summary>
@ -173,7 +176,12 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// </returns> /// </returns>
public override int GetHashCode() public override int GetHashCode()
{ {
return this.Size.GetHashCode() + this.ConvolutionType.GetHashCode(); return this.Size.GetHashCode() +
this.ConvolutionType.GetHashCode() +
this.EnergyFunction.GetHashCode() +
this.OutputType.GetHashCode() +
this.Parallelize.GetHashCode() +
this.Timeout.GetHashCode();
} }
} }
} }

4
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/ContentAwareResizeConvolutionType.cs → src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/ConvolutionType.cs

@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContentAwareResizeConvolutionType.cs" company="James South"> // <copyright file="ConvolutionType.cs" company="James South">
// Copyright (c) James South. // Copyright (c) James South.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
@ -13,7 +13,7 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// <summary> /// <summary>
/// Provides enumeration of the content aware resize convolution types. /// Provides enumeration of the content aware resize convolution types.
/// </summary> /// </summary>
public enum ContentAwareResizeConvolutionType public enum ConvolutionType
{ {
/// <summary> /// <summary>
/// The Prewitt kernel convolution type. /// The Prewitt kernel convolution type.

53
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Imaging/OutputType.cs

@ -0,0 +1,53 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OutputType.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Enumerates the output type to produce.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Plugins.Cair.Imaging
{
/// <summary>
/// Enumerates the output type to produce.
/// </summary>
public enum OutputType
{
/// <summary>
/// Output the result as a carved image. The default action.
/// </summary>
Cair = 0,
/// <summary>
/// Output the result as a greyscale image.
/// </summary>
Grayscale = 1,
/// <summary>
/// Output the result highlighting the detected edges.
/// </summary>
Edge = 2,
/// <summary>
/// Output the result highlighting the vertical energy patterns.
/// </summary>
VerticalEnergy = 3,
/// <summary>
/// Output the result highlighting the vertical energy patterns.
/// </summary>
HorizontalEnergy = 4,
/// <summary>
/// Appears to do nothing.
/// </summary>
Removal = 5,
/// <summary>
/// Output the result as a carved image with the focus on high quality output over speed.
/// </summary>
CairHighDefinition = 6
}
}

40
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Processors/ContentAwareResize.cs

@ -69,17 +69,17 @@ namespace ImageProcessor.Plugins.Cair.Processors
string fileName = Guid.NewGuid().ToString(); string fileName = Guid.NewGuid().ToString();
// Use bmp's as the temporary files since they are lossless and support transparency. // Use bmp's as the temporary files since they are lossless and support transparency.
string sourcePath = Path.Combine(CairBootstrapper.CairImagePath, fileName + ".bmp"); string sourcePath = Path.Combine(CairBootstrapper.CairPath, fileName + ".bmp");
string resizedPath = Path.Combine(CairBootstrapper.CairImagePath, fileName + "-r.bmp"); string resizedPath = Path.Combine(CairBootstrapper.CairPath, fileName + "-r.bmp");
// Gather the parameters. // Gather the parameters.
int width = this.DynamicParameter.Size.Width ?? 0; ContentAwareResizeLayer layer = (ContentAwareResizeLayer)this.DynamicParameter;
int height = this.DynamicParameter.Size.Height ?? 0; int width = layer.Size.Width;
ContentAwareResizeConvolutionType convolutionType = this.DynamicParameter.ConvolutionType; int height = layer.Size.Height;
EnergyFunction energyFunction = this.DynamicParameter.EnergyFunction; ConvolutionType convolutionType = layer.ConvolutionType;
bool prescale = this.DynamicParameter.PreScale; EnergyFunction energyFunction = layer.EnergyFunction;
bool parallelize = this.DynamicParameter.Parallelize; bool parallelize = layer.Parallelize;
int timeout = this.DynamicParameter.Timeout ?? 60000; int timeout = layer.Timeout > 0 ? layer.Timeout : 60000;
int defaultMaxWidth; int defaultMaxWidth;
int defaultMaxHeight; int defaultMaxHeight;
@ -115,26 +115,6 @@ namespace ImageProcessor.Plugins.Cair.Processors
if (width > 0 && height > 0 && width <= maxWidth && height <= maxHeight) if (width > 0 && height > 0 && width <= maxWidth && height <= maxHeight)
{ {
if (prescale)
{
if (width < image.Width || height < image.Height)
{
int preWidth = Math.Min(image.Width, width + 50); //(int)Math.Ceiling(width * 1.25));
ResizeLayer layer = new ResizeLayer(new Size(preWidth, 0));
Dictionary<string, string> resizeSettings = new Dictionary<string, string>
{
{
"MaxWidth", image.Width.ToString("G")
},
{
"MaxHeight", image.Height.ToString("G")
}
};
Resize resize = new Resize { DynamicParameter = layer, Settings = resizeSettings };
image = resize.ProcessImage(factory);
}
}
// Save the temporary bitmap. // Save the temporary bitmap.
image.Save(sourcePath, ImageFormat.Bmp); image.Save(sourcePath, ImageFormat.Bmp);
@ -208,7 +188,7 @@ namespace ImageProcessor.Plugins.Cair.Processors
private bool ProcessCairImage(string arguments, int timeout) private bool ProcessCairImage(string arguments, int timeout)
{ {
// Set up and start a new process to resize the image. // Set up and start a new process to resize the image.
ProcessStartInfo start = new ProcessStartInfo(CairBootstrapper.CairPath, arguments) ProcessStartInfo start = new ProcessStartInfo(CairBootstrapper.CairExecutablePath, arguments)
{ {
WindowStyle = ProcessWindowStyle.Hidden, WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false, UseShellExecute = false,

Loading…
Cancel
Save