// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Produces an image with the detected edges highlighted.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Compilation;
using ImageProcessor.Common.Extensions;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Processors;
///
/// Produces an image with the detected edges highlighted.
///
public class DetectEdges : IWebGraphicsProcessor
{
///
/// The regular expression to search strings for.
///
private static readonly Regex QueryRegex = BuildRegex();
///
/// The regular expression to search strings for the greyscale attribute.
///
private static readonly Regex GreyscaleRegex = new Regex(@"greyscale=false", RegexOptions.Compiled);
///
/// The edge detectors.
///
private static Dictionary detectors;
///
/// Initializes a new instance of the class.
///
public DetectEdges()
{
this.Processor = new ImageProcessor.Processors.DetectEdges();
}
///
/// Gets the regular expression to search strings for.
///
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
///
/// Gets the order in which this processor is to be used in a chain.
///
public int SortOrder { get; private set; }
///
/// Gets the processor.
///
///
/// The processor.
///
public IGraphicsProcessor Processor { get; private set; }
///
/// The position in the original string where the first character of the captured substring was found.
///
///
/// The query string to search.
///
///
/// The zero-based starting position in the original string where the captured substring was found.
///
public int MatchRegexIndex(string queryString)
{
int index = 0;
// Set the sort order to max to allow filtering.
this.SortOrder = int.MaxValue;
// First merge the matches so we can parse .
StringBuilder stringBuilder = new StringBuilder();
foreach (Match match in this.RegexPattern.Matches(queryString))
{
if (match.Success)
{
if (index == 0)
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
stringBuilder.Append(queryString);
}
index += 1;
}
}
if (this.SortOrder < int.MaxValue)
{
// Match syntax
string toParse = stringBuilder.ToString();
IEdgeFilter filter = this.ParseFilter(toParse);
bool greyscale = !GreyscaleRegex.IsMatch(toParse);
this.Processor.DynamicParameter = new Tuple(filter, greyscale);
}
return this.SortOrder;
}
///
/// Builds a regular expression from the type, this allows extensibility.
///
///
/// The to match matrix filters.
///
private static Regex BuildRegex()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("detectedges=(");
Type type = typeof(IEdgeFilter);
// Build a list of native IEdgeFilter instances.
detectors = BuildManager.GetReferencedAssemblies()
.Cast()
.SelectMany(s => s.GetLoadableTypes())
.Where(t => type.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
.ToDictionary(t => t.Name.ToLowerInvariant().Replace("edgefilter", string.Empty), Activator.CreateInstance);
stringBuilder.Append(string.Join("|", detectors.Keys.ToList()));
stringBuilder.Append(")");
return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase);
}
///
/// Parses the input string to return the correct .
///
///
/// The identifier.
///
///
/// The .
///
private IEdgeFilter ParseFilter(string identifier)
{
return (IEdgeFilter)detectors[this.RegexPattern.Match(identifier).Value.Split('=')[1]];
}
}
}