/// <summary> /// Parses the command collection returning the resize options. /// </summary> /// <param name="image">The image to process.</param> /// <param name="commands">The ordered collection containing the processing commands.</param> /// <param name="parser">The command parser use for parting commands.</param> /// <param name="culture"> /// The <see cref="CultureInfo"/> to use as the current parsing culture. /// </param> /// <returns>The <see cref="ResizeOptions"/>.</returns> internal static ResizeOptions GetResizeOptions( FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture) { if (!commands.Contains(Width) && !commands.Contains(Height)) { return(null); } ushort orientation = GetExifOrientation(image, commands, parser, culture); Size size = ParseSize(orientation, commands, parser, culture); if (size.Width <= 0 && size.Height <= 0) { return(null); } return(new() { Size = size, CenterCoordinates = GetCenter(orientation, commands, parser, culture), Position = GetAnchor(orientation, commands, parser, culture), Mode = GetMode(commands, parser, culture), Compand = GetCompandMode(commands, parser, culture), Sampler = GetSampler(commands), PadColor = parser.ParseValue <Color>(commands.GetValueOrDefault(Color), culture) }); }
async Task <(bool, int)> doALine(string?line) { if (line != null) { ParseResult result = replParser.Parse(line); int exitCode; ICommand currentCommand = result.CommandResult.Command; if (currentCommand == replCommand) { exitCode = await replParser.InvokeAsync(result, terminal); } else { if (currentCommand is Command command && commands.Contains(command)) // extension command { IExtension ext = commands.GetExtension(command); string?targetCommandName = commands.GetProvider(command).GetType().FullName; context.Logs.Debug($"Command {targetCommandName} from extension {ext.Publisher}.{ext.Name} invoking."); { PipelineContext subContext = new PipelineContext(context.Services.CreateExtensionScope(), context.Logs.CreateScope(targetCommandName ?? "sub", context.Logs.Level)); Parser subParser = CommandLines.CreateDefaultParser(replCommand, subContext); exitCode = await subParser.InvokeAsync(result, terminal); } context.Logs.Debug($"Command {targetCommandName} invoked with {exitCode}."); } else { exitCode = await replParser.InvokeAsync(result, terminal); } }
private static ushort GetExifOrientation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture) { if (commands.Contains(Orient) && !parser.ParseValue <bool>(commands.GetValueOrDefault(Orient), culture)) { return(ExifOrientationMode.Unknown); } image.TryGetExifOrientation(out ushort orientation); return(orientation); }
/// <inheritdoc/> public FormattedImage Process( FormattedImage image, ILogger logger, CommandCollection commands, CommandParser parser, CultureInfo culture) { if (commands.Contains(Quality)) { // The encoders clamp any values so no validation is required. int quality = parser.ParseValue <int>(commands.GetValueOrDefault(Quality), culture); if (image.Format is JpegFormat) { var reference = (JpegEncoder)image.Image .GetConfiguration() .ImageFormatsManager .FindEncoder(image.Format); if (quality != reference.Quality) { image.Encoder = new JpegEncoder() { Quality = quality, ColorType = reference.ColorType }; } } else if (image.Format is WebpFormat) { var reference = (WebpEncoder)image.Image .GetConfiguration() .ImageFormatsManager .FindEncoder(image.Format); image.Encoder = new WebpEncoder() { FileFormat = quality < 100 ? WebpFileFormatType.Lossy : reference.FileFormat, Quality = quality, Method = reference.Method, UseAlphaCompression = reference.UseAlphaCompression, EntropyPasses = reference.EntropyPasses, SpatialNoiseShaping = reference.SpatialNoiseShaping, FilterStrength = reference.FilterStrength, TransparentColorMode = reference.TransparentColorMode, NearLossless = reference.NearLossless, NearLosslessQuality = reference.NearLosslessQuality, }; } } return(image); }