static void Main(string[] args) { if (!ModuleInitializer.VipsInitialized) { Console.WriteLine("Error: Unable to init libvips. Please check your PATH environment variable."); Console.ReadLine(); return; } Console.WriteLine($"libvips {Base.Version(0)}.{Base.Version(1)}.{Base.Version(2)}"); Console.WriteLine( $"Type a number (1-{Samples.Count}) to execute a sample of your choice. Press <Enter> or type 'Q' to quit."); DisplayMenu(); string input; do { string[] sampleArgs = { }; if (args.Length > 0) { var sampleId = Samples.Select((value, index) => new { Index = index + 1, value.Name }) .FirstOrDefault(s => s.Name.Equals(args[0]))?.Index; input = sampleId != null ? $"{sampleId}" : "0"; sampleArgs = args.Skip(1).ToArray(); } else { input = Console.ReadLine(); } if (int.TryParse(input, out var userChoice) && TryGetSample(userChoice, out var sample)) { Console.WriteLine($"Executing sample: {sample.Name}"); var result = sample.Execute(sampleArgs); Console.WriteLine("Sample successfully executed!"); if (result != null) { Console.WriteLine($"Result: {result}"); } } else { Console.WriteLine("Sample doesn't exists, try again"); } // Clear any arguments args = new string[] { }; } while (!string.IsNullOrEmpty(input) && !string.Equals(input, "Q", StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Initializes the module. /// </summary> public static void Initialize() { try { VipsInitialized = Base.VipsInit(); if (VipsInitialized) { Version = Base.Version(0, false); Version = (Version << 8) + Base.Version(1, false); Version = (Version << 8) + Base.Version(2, false); } else { Exception = new VipsException("unable to initialize libvips"); } } catch (Exception e) { VipsInitialized = false; Exception = e; } }
/// <summary> /// Generate the `Image.Generated.cs` file. /// </summary> /// <remarks> /// This is used to generate the `Image.Generated.cs` file (<see cref="Image"/>). /// Use it with something like: /// <code language="lang-csharp"> /// File.WriteAllText("Image.Generated.cs", Operation.GenerateImageClass()); /// </code> /// </remarks> /// <returns></returns> public static string GenerateImageClass(string indent = " ") { // generate list of all nicknames we can generate docstrings for var allNickNames = new List <string>(); IntPtr TypeMap(ulong type, IntPtr a, IntPtr b) { AddNickname(type, allNickNames); return(IntPtr.Zero); } Base.TypeMap(Base.TypeFromName("VipsOperation"), TypeMap); // Sort allNickNames.Sort(); // Filter duplicates allNickNames = allNickNames.Distinct().ToList(); // remove operations we have to wrap by hand var exclude = new[] { "scale", "ifthenelse", "bandjoin", "bandrank", "composite" }; allNickNames = allNickNames.Where(x => !exclude.Contains(x)).ToList(); const string preamble = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // libvips version: {0} // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------"; var stringBuilder = new StringBuilder(string.Format(preamble, $"{Base.Version(0)}.{Base.Version(1)}.{Base.Version(2)}")); stringBuilder.AppendLine() .AppendLine() .AppendLine("namespace NetVips") .AppendLine("{") .AppendLine(" public sealed partial class Image") .AppendLine(" {") .AppendLine($"{indent}#region auto-generated functions") .AppendLine(); foreach (var nickname in allNickNames) { try { stringBuilder.AppendLine(GenerateFunction(nickname, indent)); } catch (Exception) { // ignore } } stringBuilder.AppendLine($"{indent}#endregion") .AppendLine() .AppendLine($"{indent}#region auto-generated properties") .AppendLine(); var tmpFile = Image.NewTempFile("%s.v"); var allProperties = tmpFile.GetFields(); foreach (var property in allProperties) { var type = GValue.GTypeToCSharp(tmpFile.GetTypeOf(property)); stringBuilder.AppendLine($"{indent}/// <summary>") .AppendLine($"{indent}/// {tmpFile.GetBlurb(property)}") .AppendLine($"{indent}/// </summary>") .AppendLine($"{indent}public {type} {property.ToPascalCase()} => ({type})Get(\"{property}\");") .AppendLine(); } stringBuilder.AppendLine($"{indent}#endregion") .AppendLine(" }") .AppendLine("}"); return(stringBuilder.ToString()); }