Browse Source

Tidying up

Former-commit-id: a6275865ab1c0fd55ad8e7e4dee28e6eba0258ac
af/merge-core
James South 12 years ago
parent
commit
5fe6c8abf1
  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>
/// Gets the cair path.
/// </summary>
public static string CairPath { get; private set; }
public static string CairExecutablePath { get; private set; }
/// <summary>
/// Gets the cair image path.
/// Gets the cair base path.
/// </summary>
public static string CairImagePath { get; private set; }
public static string CairPath { get; private set; }
/// <summary>
/// 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.
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"));
CairPath = Path.GetFullPath(Path.Combine(new Uri(assembly.Location).LocalPath, "..\\" + folder + "\\imageprocessor.cair\\"));
CairExecutablePath = Path.Combine(CairPath, "CAIR.exe");
string multithreaderTargetPath = Path.Combine(CairPath, "pthreadVSE2.dll");
// Set the global variable.
CairPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "CAIR.exe"));
CairImagePath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + "cairimages\\"));
// Create the folder for storing temporary images.
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(CairPath));
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
// Get the resources and copy them across.
const string CairResourcePath = "ImageProcessor.Plugins.Cair.Resources.Unmanaged.x86.CAIR.exe";
@ -64,13 +69,6 @@ namespace ImageProcessor.Plugins.Cair
{
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);
@ -83,27 +81,12 @@ namespace ImageProcessor.Plugins.Cair
{
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))
using (FileStream fileStream = File.OpenWrite(CairExecutablePath))
{
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>
/// The convolution type to apply to the layer.
/// </summary>
private ContentAwareResizeConvolutionType convolutionType = ContentAwareResizeConvolutionType.Prewitt;
private ConvolutionType convolutionType = ConvolutionType.Prewitt;
/// <summary>
/// The energy function to apply to the layer.
@ -28,14 +28,14 @@ namespace ImageProcessor.Plugins.Cair.Imaging
private EnergyFunction energyFunction = EnergyFunction.Forward;
/// <summary>
/// Whether to assign multiple threads to the resizing method.
/// The expected output type.
/// </summary>
private bool parallelize = true;
private OutputType outputType = OutputType.Cair;
/// <summary>
/// Whether to pre-scale the image to reduce errors in the output.
/// Whether to assign multiple threads to the resizing method.
/// </summary>
private bool prescale = true;
private bool parallelize = true;
/// <summary>
/// The timeout in milliseconds to attempt to resize for.
@ -61,7 +61,7 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// <summary>
/// Gets or sets the content aware resize convolution type (Default ContentAwareResizeConvolutionType.Prewitt).
/// </summary>
public ContentAwareResizeConvolutionType ConvolutionType
public ConvolutionType ConvolutionType
{
get
{
@ -91,36 +91,35 @@ namespace ImageProcessor.Plugins.Cair.Imaging
}
/// <summary>
/// Gets or sets a value indicating whether to assign multiple threads to the resizing method.
/// (Default true)
/// Gets or sets the expected output type.
/// </summary>
public bool Parallelize
public OutputType OutputType
{
get
{
return this.parallelize;
return this.outputType;
}
set
{
this.parallelize = value;
this.outputType = value;
}
}
/// <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)
/// </summary>
public bool PreScale
public bool Parallelize
{
get
{
return this.prescale;
return this.parallelize;
}
set
{
this.prescale = value;
this.parallelize = value;
}
}
@ -162,7 +161,11 @@ namespace ImageProcessor.Plugins.Cair.Imaging
}
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>
@ -173,7 +176,12 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// </returns>
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.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -13,7 +13,7 @@ namespace ImageProcessor.Plugins.Cair.Imaging
/// <summary>
/// Provides enumeration of the content aware resize convolution types.
/// </summary>
public enum ContentAwareResizeConvolutionType
public enum ConvolutionType
{
/// <summary>
/// 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();
// Use bmp's as the temporary files since they are lossless and support transparency.
string sourcePath = Path.Combine(CairBootstrapper.CairImagePath, fileName + ".bmp");
string resizedPath = Path.Combine(CairBootstrapper.CairImagePath, fileName + "-r.bmp");
string sourcePath = Path.Combine(CairBootstrapper.CairPath, fileName + ".bmp");
string resizedPath = Path.Combine(CairBootstrapper.CairPath, fileName + "-r.bmp");
// Gather the parameters.
int width = this.DynamicParameter.Size.Width ?? 0;
int height = this.DynamicParameter.Size.Height ?? 0;
ContentAwareResizeConvolutionType convolutionType = this.DynamicParameter.ConvolutionType;
EnergyFunction energyFunction = this.DynamicParameter.EnergyFunction;
bool prescale = this.DynamicParameter.PreScale;
bool parallelize = this.DynamicParameter.Parallelize;
int timeout = this.DynamicParameter.Timeout ?? 60000;
ContentAwareResizeLayer layer = (ContentAwareResizeLayer)this.DynamicParameter;
int width = layer.Size.Width;
int height = layer.Size.Height;
ConvolutionType convolutionType = layer.ConvolutionType;
EnergyFunction energyFunction = layer.EnergyFunction;
bool parallelize = layer.Parallelize;
int timeout = layer.Timeout > 0 ? layer.Timeout : 60000;
int defaultMaxWidth;
int defaultMaxHeight;
@ -115,26 +115,6 @@ namespace ImageProcessor.Plugins.Cair.Processors
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.
image.Save(sourcePath, ImageFormat.Bmp);
@ -208,7 +188,7 @@ namespace ImageProcessor.Plugins.Cair.Processors
private bool ProcessCairImage(string arguments, int timeout)
{
// 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,
UseShellExecute = false,

Loading…
Cancel
Save