Skip to content

Commit

Permalink
add new filterslides and corresponding image filters
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgriffin committed May 29, 2021
1 parent b4eb2f1 commit d18bd0a
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 13 deletions.
198 changes: 190 additions & 8 deletions Xenon/Compiler/AST/XenonASTFilterImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ namespace Xenon.Compiler
{
class XenonASTFilterImage : IXenonASTCommand
{
public string AssetName { get; set; }
public List<(ImageFilter Type, ImageFilterParams FParams)> Filters { get; set; } = new List<(ImageFilter Type, ImageFilterParams)>();

private Dictionary<int, string> assetstoresolve = new Dictionary<int, string>();
private int assetids = 0;

public IXenonASTElement Compile(Lexer Lexer, XenonErrorLogger Logger)
{
XenonASTFilterImage filterimage = new XenonASTFilterImage();
Expand Down Expand Up @@ -42,7 +44,23 @@ public IXenonASTElement Compile(Lexer Lexer, XenonErrorLogger Logger)
{
CompileFilterCommand_crop(Lexer, Logger);
}

if (filtername == "centerassetfill")
{
CompileFilterCommand_centerassetfill(Lexer, Logger);
}
if (filtername == "uniformstretch")
{
CompileFilterCommand_uniformstetch(Lexer, Logger);
}
if (filtername == "centeronbackground")
{
CompileFilterCommand_centeronbackground(Lexer, Logger);
}
if (filtername == "coloredit")
{
CompileFilterCommand_coloredit(Lexer, Logger);
}
Lexer.GobbleWhitespace();
}

Lexer.GobbleWhitespace();
Expand Down Expand Up @@ -163,6 +181,163 @@ private void CompileFilterCommand_solidcolorcanvas(Lexer Lexer, XenonErrorLogger

}

private void CompileFilterCommand_centerassetfill(Lexer Lexer, XenonErrorLogger Logger)
{
Lexer.GobbleandLog("::", "Expected '::' after filtername and before filter params.");
CenterAssetFillFilterParams fparams = new CenterAssetFillFilterParams();
// parse params

Lexer.GobbleandLog("asset", "Expected 'asset' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting '(' to enclose asset name");
string pname = Lexer.ConsumeUntil(")", "Expecting paramater: asset");
Lexer.GobbleandLog(")", "Expecting ')' to enclose asset name");
fparams.AssetPath = assetids.ToString();
assetstoresolve[assetids] = pname;
assetids++;

Lexer.GobbleandLog(";", "Expecting ';' at end of filter");

Filters.Add((ImageFilter.CenterAssetFill, fparams));

}

private void CompileFilterCommand_uniformstetch(Lexer Lexer, XenonErrorLogger Logger)
{
Lexer.GobbleandLog("::", "Expected '::' after filtername and before filter params.");
UniformStretchFilterParams fparams = new UniformStretchFilterParams();
// parse params

Lexer.GobbleandLog("width", "Expected 'width' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pwidth = Lexer.ConsumeUntil(",");
Lexer.GobbleandLog(",", "Expecting ',' before height parameter");
int.TryParse(pwidth, out int width);
fparams.Width = width;

Lexer.GobbleandLog("height", "Expected 'height' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pheight = Lexer.ConsumeUntil(",");
Lexer.GobbleandLog(",", "Expecting ',' before fill parameter");
int.TryParse(pheight, out int height);
fparams.Height = height;

Lexer.GobbleandLog("fill", "Expecting 'fill' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string pcolorfill = Lexer.ConsumeUntil(")", "Expecting parameter: fill (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
Lexer.GobbleandLog(",", "Expecting , before next parameter");
fparams.Fill = GraphicsHelper.ColorFromRGB(pcolorfill);

Lexer.GobbleandLog("kfill", "Expecting 'kfill' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string pkeyfill = Lexer.ConsumeUntil(")", "Expecting parameter: kfill (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
fparams.KFill = GraphicsHelper.ColorFromRGB(pkeyfill);


Lexer.GobbleandLog(";", "Expecting ';' at end of filter");

Filters.Add((ImageFilter.UniformStretch, fparams));
}

private void CompileFilterCommand_centeronbackground(Lexer Lexer, XenonErrorLogger Logger)
{
Lexer.GobbleandLog("::", "Expected '::' after filtername and before filter params.");
CenterOnBackgroundFilterParams fparams = new CenterOnBackgroundFilterParams();
// parse params

Lexer.GobbleandLog("width", "Expected 'width' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pwidth = Lexer.ConsumeUntil(",");
Lexer.GobbleandLog(",", "Expecting ',' before height parameter");
int.TryParse(pwidth, out int width);
fparams.Width = width;

Lexer.GobbleandLog("height", "Expected 'height' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pheight = Lexer.ConsumeUntil(",");
Lexer.GobbleandLog(",", "Expecting ',' before fill parameter");
int.TryParse(pheight, out int height);
fparams.Height = height;

Lexer.GobbleandLog("fill", "Expecting 'fill' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string pcolorfill = Lexer.ConsumeUntil(")", "Expecting parameter: fill (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
Lexer.GobbleandLog(",", "Expecting , before next parameter");
fparams.Fill = GraphicsHelper.ColorFromRGB(pcolorfill);

Lexer.GobbleandLog("kfill", "Expecting 'kfill' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string pkeyfill = Lexer.ConsumeUntil(")", "Expecting parameter: kfill (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
fparams.KFill = GraphicsHelper.ColorFromRGB(pkeyfill);


Lexer.GobbleandLog(";", "Expecting ';' at end of filter");

Filters.Add((ImageFilter.CenterOnBackground, fparams));
}

private void CompileFilterCommand_coloredit(Lexer Lexer, XenonErrorLogger Logger)
{
Lexer.GobbleandLog("::", "Expected '::' after filtername and before filter params.");
ColorEditFilterParams fparams = new ColorEditFilterParams();
// parse params

Lexer.GobbleandLog("identifier", "Expecting 'identifier' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string pcolorrgb = Lexer.ConsumeUntil(")", "Expecting parameter: identifier (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
Lexer.GobbleandLog(",", "Expecting , before next parameter");
fparams.Identifier = GraphicsHelper.ColorFromRGB(pcolorrgb);

Lexer.GobbleandLog("replace", "Expecting 'replace' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
Lexer.GobbleandLog("(", "Expecting open '('");
string preplacergb = Lexer.ConsumeUntil(")", "Expecting parameter: replace (r,g,b) e.g. (0, 132, 39)");
Lexer.GobbleandLog(")", "Expecting closing ')'");
Lexer.GobbleandLog(",", "Expecting , before next parameter");
fparams.Replace = GraphicsHelper.ColorFromRGB(preplacergb);

Lexer.GobbleandLog("exclude", "Expected 'exclude' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pexclude = Lexer.ConsumeUntil(",", "Expecting paramater: exclude (True, False)");
Lexer.GobbleandLog(",", "Expecting ',' before next parameter");
bool.TryParse(pexclude, out bool exclude);
fparams.IsExcludeMatch = exclude;

Lexer.GobbleandLog("forkey", "Expected 'exclude' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to start parameter value");
string pkey = Lexer.ConsumeUntil(",", "Expecting paramater: exclude (True, False)");
Lexer.GobbleandLog(",", "Expecting ',' before next parameter");
bool.TryParse(pkey, out bool key);
fparams.ForKey = key;

Lexer.GobbleandLog("rtol", "Expecting 'rtol' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to starrt parameter value");
fparams.RTolerance = Convert.ToInt32(Lexer.ConsumeUntil(",", "Expecting tolerance value e.g. 123"));
Lexer.GobbleandLog(",", "Expecting ',' before next parameter");
Lexer.GobbleWhitespace();
Lexer.GobbleandLog("gtol", "Expecting 'gtol' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to starrt parameter value");
fparams.GTolerance = Convert.ToInt32(Lexer.ConsumeUntil(",", "Expecting tolerance value e.g. 123"));
Lexer.GobbleandLog(",", "Expecting ',' before next parameter");
Lexer.GobbleWhitespace();
Lexer.GobbleandLog("btol", "Expecting 'btol' parameter");
Lexer.GobbleandLog("=", "Expecting '=' to starrt parameter value");
fparams.BTolerance = Convert.ToInt32(Lexer.ConsumeUntil(";", "Expecting tolerance value e.g. 123"));
Lexer.GobbleandLog(";", "Expecting ';' at end of filter");

Filters.Add((ImageFilter.ColorEdit, fparams));

}

public void Generate(Project project, IXenonASTElement _Project)
{
Expand All @@ -171,16 +346,24 @@ public void Generate(Project project, IXenonASTElement _Project)
imageslide.Name = "UNNAMED_image";
imageslide.Number = project.NewSlideNumber;
imageslide.Lines = new List<SlideLine>();
imageslide.Asset = "";

string assetpath = "";
var asset = project.Assets.Find(p => p.Name == AssetName);
if (asset != null)

// resolve assets used in filters
foreach (var f in Filters)
{
assetpath = asset.CurrentPath;
if (f.Type == ImageFilter.CenterAssetFill)
{
int id = int.Parse((f.FParams as CenterAssetFillFilterParams).AssetPath);
var asset = project.Assets.Find(p => p.Name == assetstoresolve[id]);
if (asset != null)
{
(f.FParams as CenterAssetFillFilterParams).AssetPath = asset.CurrentPath;
}
}
}

imageslide.Format = SlideFormat.FilterImage;
imageslide.Asset = assetpath;
imageslide.MediaType = MediaType.Image;

// set filter data
Expand All @@ -193,7 +376,6 @@ public void Generate(Project project, IXenonASTElement _Project)
public void GenerateDebug(Project project)
{
Debug.WriteLine("<XenonASTFilterImage>");
Debug.WriteLine(AssetName);
Debug.WriteLine("<Filters>");
Debug.WriteLine(Filters);
Debug.WriteLine("</Filters>");
Expand Down
29 changes: 29 additions & 0 deletions Xenon/Compiler/LanguageKeywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ namespace Xenon.Compiler
static class LanguageKeywords
{

public static List<string> WholeWords = new List<string>()
{
"solidcolorcanvas",
"crop",
"centerassetfill",
"asset",
"width",
"height",
"color",
"kcolor",
"bound",
"Top",
"Left",
"Bottom",
"Right",
"True",
"False",
"icolor",
"rtol",
"gtol",
"btol",
"uniformstretch",
"fill",
"kfill",
"centeronbackground",
"coloredit",
"forkey",
};

public static Dictionary<LanguageKeywordCommand, string> Commands = new Dictionary<LanguageKeywordCommand, string>()
{
[LanguageKeywordCommand.Script_LiturgyOff] = "liturgyoff",
Expand Down
2 changes: 2 additions & 0 deletions Xenon/Compiler/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Lexer(ILexerLogger Logger)

SplitWords = new List<string>();
SplitWords.AddRange(LanguageKeywords.Commands.Values);
SplitWords.AddRange(LanguageKeywords.WholeWords);
List<string> Seperators = new List<string>() {
"\r\n",
"//",
Expand All @@ -97,6 +98,7 @@ public Lexer(ILexerLogger Logger)
"=",
};
SplitWords.AddRange(Seperators);
SplitWords = SplitWords.OrderByDescending(s => s.Length).ToList();
}

private List<string> SplitAndKeep(string text, List<string> splitwords)
Expand Down
5 changes: 4 additions & 1 deletion Xenon/Renderer/ImageFilters/ImageFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ namespace Xenon.Renderer.ImageFilters
{
enum ImageFilter
{
CenterAssetFill,
SolidColorCanvas,
Crop,
Resize,
UniformStretch,
CenterOnBackground,
ColorEdit,
}
}
72 changes: 71 additions & 1 deletion Xenon/Renderer/ImageFilters/ImageFilterParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ class ImageFilterParams
{
}

class SolidColorCanvasFilterParams: ImageFilterParams
class CenterAssetFillFilterParams : ImageFilterParams
{
/// <summary>
/// Path to image asset.
/// </summary>
public string AssetPath { get; set; }
}

class SolidColorCanvasFilterParams : ImageFilterParams
{
/// <summary>
/// Width of canvas.
Expand Down Expand Up @@ -66,6 +74,39 @@ public enum CropBound
public int BTolerance { get; set; }
}

class ColorEditFilterParams : ImageFilterParams
{
/// <summary>
/// Color to match for applying color edit to.
/// </summary>
public Color Identifier { get; set; }
/// <summary>
/// Color to replace editied pixels with.
/// </summary>
public Color Replace { get; set; }
/// <summary>
/// If True will instead replace all pixels that don't match identifier within tolerance.
/// </summary>
public bool IsExcludeMatch { get; set; }
/// <summary>
/// True if should be applied on key instead of slide.
/// </summary>
public bool ForKey { get; set; }
/// <summary>
/// R tolerance.
/// </summary>
public int RTolerance { get; set; }
/// <summary>
/// G tolerance.
/// </summary>
public int GTolerance { get; set; }
/// <summary>
/// B tolerance.
/// </summary>
public int BTolerance { get; set; }

}

class UniformStretchFilterParams : ImageFilterParams
{
/// <summary>
Expand All @@ -76,6 +117,35 @@ class UniformStretchFilterParams : ImageFilterParams
/// Target Height.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Color to fill extra space.
/// </summary>
public Color Fill { get; set; }
/// <summary>
/// Color to fill key extra space.
/// </summary>
public Color KFill { get; set; }
}

class CenterOnBackgroundFilterParams : ImageFilterParams
{
/// <summary>
/// Background width.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Background height.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Background color.
/// </summary>
public Color Fill { get; set; }
/// <summary>
/// Background key color.
/// </summary>
public Color KFill { get; set; }
}


}
Loading

0 comments on commit d18bd0a

Please sign in to comment.