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
Clamp custom sizes read from a file to the editor's 16-16384
KnifeLemon4776d07075f6
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.csindex 61cda32b..f8f5d55b 100644--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs@@ -687,7 +687,7 @@ public sealed partial class PresetEditorViewModel : ObservableObject// (it would read presets.json as broken and fall back to the built-in presets, and the next save would// write those over the user's own; issue #576).image.Resize = ResizeMode.None;- image.CustomSize = new CustomImageSize { Width = Math.Clamp(CustomWidth, 16, 16384), Height = Math.Clamp(CustomHeight, 16, 16384), LockAspect = LockAspect };+ image.CustomSize = new CustomImageSize { Width = Math.Clamp(CustomWidth, CustomImageSize.MinSide, CustomImageSize.MaxSide), Height = Math.Clamp(CustomHeight, CustomImageSize.MinSide, CustomImageSize.MaxSide), LockAspect = LockAspect };}else{diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.csindex b30d2e59..3db0ea34 100644--- a/src/Filee.Core/Presets/Preset.cs+++ b/src/Filee.Core/Presets/Preset.cs@@ -489,10 +489,17 @@ public sealed class ImageOptions}/// <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>+/// editor keeps both sides within <see cref="MinSide"/>-<see cref="MaxSide"/>; 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, and any other side is brought into that range (<see cref="ClampedWidth"/>, <see cref="ClampedHeight"/>).</summary>public sealed class CustomImageSize{+ /// <summary>The smallest side the editor offers.</summary>+ public const int MinSide = 16;++ /// <summary>The largest side the editor offers.</summary>+ public const int MaxSide = 16384;+/// <summary>Target width in pixels.</summary>public int Width { get; set; } = 1920;@@ -504,6 +511,15 @@ public sealed class CustomImageSize/// stretching it to exactly that size./// </summary>public bool LockAspect { get; set; }++ /// <summary><see cref="Width"/> within the editor's range, so a hand-edited 200000 cannot ask for a picture no+ /// machine can hold.</summary>+ [JsonIgnore]+ public int ClampedWidth => Math.Clamp(Width, MinSide, MaxSide);++ /// <summary><see cref="Height"/> within the editor's range (see <see cref="ClampedWidth"/>).</summary>+ [JsonIgnore]+ public int ClampedHeight => Math.Clamp(Height, MinSide, MaxSide);}/// <summary>Page size used when images are placed into a PDF.</summary>diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.csindex fb1a00c5..3847a1a1 100644--- a/src/Filee.Engines/Magick/ImageEncoder.cs+++ b/src/Filee.Engines/Magick/ImageEncoder.cs@@ -206,8 +206,8 @@ internal static class ImageEncoderpublic static double ResizeScale(double width, double height, ImageOptions options) =>options.CustomSize is { Width: > 0, Height: > 0 } custom? custom.LockAspect- ? Math.Min(custom.Width / width, custom.Height / height)- : custom.Height / height+ ? Math.Min(custom.ClampedWidth / width, custom.ClampedHeight / height)+ : custom.ClampedHeight / height: options.Resize switch{ResizeMode.Percent when options.ResizePercent > 0 => options.ResizePercent / 100.0,@@ -286,22 +286,25 @@ internal static class ImageEncoder// 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.+ // so the picture keeps the preset's own resize behavior (None for a Custom preset) instead. A size above the+ // editor's range is brought down to it, so a hand-edited 200000 x 200000 cannot ask for a 40-gigapixel picture.if (options.CustomSize is { Width: > 0, Height: > 0 } custom){+ var customWidth = custom.ClampedWidth;+ var customHeight = custom.ClampedHeight;if (custom.LockAspect){// The file's own shape is kept: scaled up or down to the largest size that fits inside the box. Both// sides come from one scale, so the aspect already matches and Resize keeps it, as it always does.- var scale = Math.Min(custom.Width / (double)image.Width, custom.Height / (double)image.Height);+ var scale = Math.Min(customWidth / (double)image.Width, customHeight / (double)image.Height);if (scale != 1)image.Resize((uint)Math.Max(1, Math.Round(image.Width * scale)), (uint)Math.Max(1, Math.Round(image.Height * scale)));}- else if (image.Width != (uint)custom.Width || image.Height != (uint)custom.Height)+ else if (image.Width != (uint)customWidth || image.Height != (uint)customHeight){// No lock: exactly the asked size, stretching or squeezing the picture on purpose (issue #576). Resize// fits inside a box and keeps the aspect on its own, so the flag is what makes this a real stretch.- image.Resize(new MagickGeometry((uint)custom.Width, (uint)custom.Height) { IgnoreAspectRatio = true });+ image.Resize(new MagickGeometry((uint)customWidth, (uint)customHeight) { IgnoreAspectRatio = true });}}else switch (options.Resize)diff --git a/src/Filee.Engines/Vector/SvgRenderer.cs b/src/Filee.Engines/Vector/SvgRenderer.csindex b64edc07..7cab50fd 100644--- a/src/Filee.Engines/Vector/SvgRenderer.cs+++ b/src/Filee.Engines/Vector/SvgRenderer.cs@@ -172,8 +172,8 @@ internal static class SvgRenderer// bitmap, which comes out soft. With the lock, at the fit-inside scale; without, at the larger side's// scale, so the stretch that follows only ever shrinks the other side.scale = custom.LockAspect- ? Math.Min(custom.Width / bounds.Width, custom.Height / bounds.Height)- : Math.Max(custom.Width / bounds.Width, custom.Height / bounds.Height);+ ? Math.Min(custom.ClampedWidth / bounds.Width, custom.ClampedHeight / bounds.Height)+ : Math.Max(custom.ClampedWidth / bounds.Width, custom.ClampedHeight / bounds.Height);}else switch (image.Resize){diff --git a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs b/tests/Filee.Engines.Tests/ImageAndPdfTests.csindex d8061959..4d12b647 100644--- a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs+++ b/tests/Filee.Engines.Tests/ImageAndPdfTests.cs@@ -421,6 +421,23 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal((250u, 444u), (negative.Width, negative.Height));}+ [Fact]+ public void A_custom_size_read_from_a_file_is_brought_into_the_editors_range()+ {+ // A hand-edited 200000 used to go straight into image.Resize; it now stops at the editor's 16384, and a side+ // under 16 comes up to it, the same range the editor writes.+ var options = new ImageOptions { CustomSize = new() { Width = 200000, Height = 10 } };+ using var image = new MagickImage(MagickColors.White, 250, 444);+ Filee.Engines.Magick.ImageEncoder.ApplyOptions(image, options, "png");+ Assert.Equal((16384u, 16u), (image.Width, image.Height));++ var locked = new ImageOptions { CustomSize = new() { Width = 200000, Height = 200000, LockAspect = true } };+ Assert.Equal(20.48, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, locked), 3);++ var stretched = new ImageOptions { CustomSize = new() { Width = 500, Height = 200000 } };+ Assert.Equal(20.48, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, stretched), 3);+ }+[Fact]public void Custom_resize_plans_pdf_page_runs_at_the_scale_the_resize_will_use(){