Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
fix(presets): skip non-positive custom sizes from hand-edited files and give width and height full rows
Manas Choksi60a2db0f62f3
diff --git a/src/Filee.App/Views/PresetEditorView.axaml b/src/Filee.App/Views/PresetEditorView.axamlindex a574f484..ab90cab6 100644--- a/src/Filee.App/Views/PresetEditorView.axaml+++ b/src/Filee.App/Views/PresetEditorView.axaml@@ -133,17 +133,13 @@<TextBlock Classes="label" Text="{DynamicResource presets.width}" /><NumericUpDown Value="{Binding Width}" Minimum="16" Maximum="20000" Increment="100" FormatString="0" AutomationProperties.Name="{DynamicResource presets.width}" /></StackPanel>+ <!-- Full rows instead of half-width boxes: the spinner kept most of a half-width box for itself and+ clipped a three-digit size to its first digit, so width and height each get the whole row. --><StackPanel Grid.Column="2" IsVisible="{Binding ShowCustom}">- <Grid ColumnDefinitions="*,8,*">- <StackPanel>- <TextBlock Classes="label" Text="{DynamicResource presets.width}" />- <NumericUpDown Value="{Binding CustomWidth}" Minimum="16" Maximum="16384" Increment="10" FormatString="0" AutomationProperties.Name="{DynamicResource presets.width}" />- </StackPanel>- <StackPanel Grid.Column="2">- <TextBlock Classes="label" Text="{DynamicResource presets.height}" />- <NumericUpDown Value="{Binding CustomHeight}" Minimum="16" Maximum="16384" Increment="10" FormatString="0" AutomationProperties.Name="{DynamicResource presets.height}" />- </StackPanel>- </Grid>+ <TextBlock Classes="label" Text="{DynamicResource presets.width}" />+ <NumericUpDown Value="{Binding CustomWidth}" Minimum="16" Maximum="16384" Increment="10" FormatString="0" ShowButtonSpinner="False" HorizontalContentAlignment="Center" AutomationProperties.Name="{DynamicResource presets.width}" />+ <TextBlock Classes="label" Margin="0,8,0,0" Text="{DynamicResource presets.height}" />+ <NumericUpDown Value="{Binding CustomHeight}" Minimum="16" Maximum="16384" Increment="10" FormatString="0" ShowButtonSpinner="False" HorizontalContentAlignment="Center" AutomationProperties.Name="{DynamicResource presets.height}" /><CheckBox Content="{DynamicResource presets.lock_aspect}" IsChecked="{Binding LockAspect}" Margin="0,8,0,0" AutomationProperties.Name="{DynamicResource presets.lock_aspect}" /></StackPanel></Grid>diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.csindex 5a44b142..b30d2e59 100644--- a/src/Filee.Core/Presets/Preset.cs+++ b/src/Filee.Core/Presets/Preset.cs@@ -359,7 +359,7 @@ public sealed class ImageOptions/// <summary>Whether the preset edits picture content, size, or playback speed.</summary>[JsonIgnore]- public bool EditsPicture => Resize != ResizeMode.None || Grayscale || TrimTransparent || RemoveBackground || MaxCompression || ChangesSpeed;+ public bool EditsPicture => Resize != ResizeMode.None || Grayscale || TrimTransparent || RemoveBackground || MaxCompression || ChangesSpeed || CustomSize is not null;/// <summary>Scale in percent when <see cref="Resize"/> is <see cref="ResizeMode.Percent"/>.</summary>public int ResizePercent { get; set; } = 50;@@ -488,7 +488,9 @@ public sealed class ImageOptions}}-/// <summary>The free width x height a preset resizes to: <see cref="ImageOptions.CustomSize"/> (issue #576).</summary>+/// <summary>The free width x height a preset resizes to: <see cref="ImageOptions.CustomSize"/> (issue #576). The+/// editor keeps both sides within 16-16384; a size read from a hand-edited file with a side of 0 or below is+/// ignored where it is used, so the picture keeps the preset's own resize behavior instead.</summary>public sealed class CustomImageSize{/// <summary>Target width in pixels.</summary>diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.csindex 2344ea6e..fb1a00c5 100644--- a/src/Filee.Engines/Magick/ImageEncoder.cs+++ b/src/Filee.Engines/Magick/ImageEncoder.cs@@ -204,7 +204,7 @@ internal static class ImageEncoder/// is what a tall run of pages has to fit./// </summary>public static double ResizeScale(double width, double height, ImageOptions options) =>- options.CustomSize is { } custom+ options.CustomSize is { Width: > 0, Height: > 0 } custom? custom.LockAspect? Math.Min(custom.Width / width, custom.Height / height): custom.Height / height@@ -284,7 +284,10 @@ internal static class ImageEncoderimage.SetProfile(exif);}- if (options.CustomSize is { } custom)+ // A hand-edited presets.json or an imported bundle can carry sizes the editor would never write, and a+ // width of 0 or below would wrap to about 4 billion px in the (uint) casts below: such a size is skipped,+ // so the picture keeps the preset's own resize behavior (None for a Custom preset) instead.+ if (options.CustomSize is { Width: > 0, Height: > 0 } custom){if (custom.LockAspect){@@ -302,18 +305,18 @@ internal static class ImageEncoder}}else switch (options.Resize)- {- case ResizeMode.Percent when options.ResizePercent is > 0 and not 100:- image.Resize(new Percentage(options.ResizePercent));- break;- case ResizeMode.LongEdge when options.LongEdge > 0:- // Greater = only shrink images that are larger than the box, never upscale.- image.Resize(new MagickGeometry((uint)options.LongEdge, (uint)options.LongEdge) { Greater = true });- break;- case ResizeMode.Width when options.Width > 0 && image.Width > options.Width:- image.Resize((uint)options.Width, (uint)Math.Max(1, Math.Round(image.Height * (double)options.Width / image.Width)));- break;- }+ {+ case ResizeMode.Percent when options.ResizePercent is > 0 and not 100:+ image.Resize(new Percentage(options.ResizePercent));+ break;+ case ResizeMode.LongEdge when options.LongEdge > 0:+ // Greater = only shrink images that are larger than the box, never upscale.+ image.Resize(new MagickGeometry((uint)options.LongEdge, (uint)options.LongEdge) { Greater = true });+ break;+ case ResizeMode.Width when options.Width > 0 && image.Width > options.Width:+ image.Resize((uint)options.Width, (uint)Math.Max(1, Math.Round(image.Height * (double)options.Width / image.Width)));+ break;+ }FitFormat(image, targetFormat);// Before grayscale (a blue signature stays blue until then) and before alpha is flattened for JPG and co.diff --git a/src/Filee.Engines/Vector/SvgRenderer.cs b/src/Filee.Engines/Vector/SvgRenderer.csindex 47c81862..b64edc07 100644--- a/src/Filee.Engines/Vector/SvgRenderer.cs+++ b/src/Filee.Engines/Vector/SvgRenderer.cs@@ -165,7 +165,8 @@ internal static class SvgRenderervar longEdge = Math.Max(bounds.Width, bounds.Height);var dpi = Math.Clamp(pdf.RenderDpi, 36, 1200);var scale = Math.Max(dpi / CssDpi, MinimumLongEdge / longEdge);- if (image.CustomSize is { } custom)+ // Same guard as ImageEncoder.ApplyOptions: a size of 0 or below from a hand-edited file is not a size.+ if (image.CustomSize is { Width: > 0, Height: > 0 } custom){// Render at the size the custom resize asks for, so the box is not reached by stretching a default-size// bitmap, which comes out soft. With the lock, at the fit-inside scale; without, at the larger side's@@ -175,14 +176,14 @@ internal static class SvgRenderer: Math.Max(custom.Width / bounds.Width, custom.Height / bounds.Height);}else switch (image.Resize)- {- case ResizeMode.LongEdge when image.LongEdge > 0:- scale = image.LongEdge / longEdge;- break;- case ResizeMode.Percent when image.ResizePercent is > 0 and not 100:- scale *= image.ResizePercent / 100f;- break;- }+ {+ case ResizeMode.LongEdge when image.LongEdge > 0:+ scale = image.LongEdge / longEdge;+ break;+ case ResizeMode.Percent when image.ResizePercent is > 0 and not 100:+ scale *= image.ResizePercent / 100f;+ break;+ }var limit = Math.Min(MaximumLongEdge / longEdge, MathF.Sqrt(MaximumPixels / (bounds.Width * bounds.Height)));return Math.Max(Math.Min(scale, limit), 1e-3f);}diff --git a/tests/Filee.App.Tests/DonutGroupTests.cs b/tests/Filee.App.Tests/DonutGroupTests.csindex 6ef75250..26678bf5 100644--- a/tests/Filee.App.Tests/DonutGroupTests.cs+++ b/tests/Filee.App.Tests/DonutGroupTests.cs@@ -472,6 +472,16 @@ public class DonutGroupTestsAssert.Equal("Works on images only", availability.Check(removeBackground, [mp3]));Assert.Null(availability.Check(removeBackground, [png]));+ // Custom sizing edits the picture too: a same-format Custom preset stores Resize None plus CustomSize, so+ // EditsPicture has to read CustomSize as well, or it would still be offered for an MP3 (#604 follow-up).+ var customSize = new Preset+ {+ TargetFormat = BuiltInData.SameAsSource,+ Image = { CustomSize = new CustomImageSize { Width = 777, Height = 333 } }+ };+ Assert.Equal("Works on images only", availability.Check(customSize, [mp3]));+ Assert.Null(availability.Check(customSize, [png]));+var mixedOptions = new Preset{TargetFormat = BuiltInData.SameAsSource,diff --git a/tests/Filee.Engines.Tests/GraphicsTests.cs b/tests/Filee.Engines.Tests/GraphicsTests.csindex 1b3b1f4d..ed739b79 100644--- a/tests/Filee.Engines.Tests/GraphicsTests.cs+++ b/tests/Filee.Engines.Tests/GraphicsTests.cs@@ -3,7 +3,6 @@using System.IO.Compression;using System.Text;-using SkiaSharp;using Filee.Core.Conversion;using Filee.Core.Presets;using Filee.Engines.Icns;@@ -12,6 +11,7 @@ using Filee.Engines.Vector;using ImageMagick;using PdfSharp.Pdf;using PdfSharp.Pdf.IO;+using SkiaSharp;namespace Filee.Engines.Tests;@@ -469,6 +469,12 @@ public class GraphicsTests(EngineFixture fx) : IClassFixture<EngineFixture>// Without, the larger side's scale, so the stretch that follows only ever shrinks the other side.image.CustomSize = new() { Width = 777, Height = 333 };Assert.Equal(1.9425f, SvgRenderer.RasterScale(new SKRect(0, 0, 400, 200), image, new PdfOptions()), 3);++ // A size of 0 or below from a hand-edited file is no size: the raster keeps the default DPI scale.+ var zeroed = new ImageOptions { CustomSize = new() { Width = 0, Height = 333 } };+ var bounds = new SKRect(0, 0, 400, 200);+ Assert.Equal(SvgRenderer.RasterScale(bounds, new ImageOptions(), new PdfOptions()),+ SvgRenderer.RasterScale(bounds, zeroed, new PdfOptions()));}[Fact]diff --git a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs b/tests/Filee.Engines.Tests/ImageAndPdfTests.csindex 456b5daa..610dc8a8 100644--- a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs+++ b/tests/Filee.Engines.Tests/ImageAndPdfTests.cs@@ -343,6 +343,22 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal((500u, 2000u), (exact.Width, exact.Height));}+ [Fact]+ public void A_custom_size_read_from_a_file_with_a_side_of_zero_or_below_is_skipped()+ {+ // The editor keeps both sides within 16-16384, but a hand-edited presets.json or an imported bundle can+ // carry anything, and a width of 0 or below used to reach (uint)custom.Width and wrap to about 4 billion px.+ var options = new ImageOptions { CustomSize = new() { Width = 0, Height = 2000 } };+ using var zeroed = new MagickImage(MagickColors.White, 250, 444);+ Filee.Engines.Magick.ImageEncoder.ApplyOptions(zeroed, options, "png");+ Assert.Equal((250u, 444u), (zeroed.Width, zeroed.Height));++ options.CustomSize = new() { Width = -5, Height = 2000 };+ using var negative = new MagickImage(MagickColors.White, 250, 444);+ Filee.Engines.Magick.ImageEncoder.ApplyOptions(negative, options, "png");+ Assert.Equal((250u, 444u), (negative.Width, negative.Height));+ }+[Fact]public void Custom_resize_plans_pdf_page_runs_at_the_scale_the_resize_will_use(){@@ -354,6 +370,13 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal(2.5, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, stretched), 3);Assert.Equal(1, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, new ImageOptions()), 3);++ // A size of 0 or below from a hand-edited file is no size: the preset's own resize runs instead of it.+ var zeroed = new ImageOptions { CustomSize = new() { Width = 0, Height = 2000 } };+ Assert.Equal(1, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, zeroed), 3);++ var negative = new ImageOptions { CustomSize = new() { Width = 500, Height = -1 } };+ Assert.Equal(1, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, negative), 3);}[Fact]