Skip to content

armgit / Filee

Public Imported from GitHub
0

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.

Rotate and flip images, with "Rotate" and "Flip" donut groups

SeoJCalive9e01d5fdf986
diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json
index 816e91b9..db8962e3 100644
--- a/src/Filee.App/Assets/i18n/en.json
+++ b/src/Filee.App/Assets/i18n/en.json
@@ -106,6 +106,13 @@
"preset.half_size": "Half size",
"preset.grayscale": "Grayscale",
"preset.remove_background": "Remove background",
+ "preset.group_rotate": "Rotate",
+ "preset.rotate_left": "Left 90°",
+ "preset.rotate_right": "Right 90°",
+ "preset.rotate_180": "180°",
+ "preset.group_flip": "Flip",
+ "preset.flip_horizontal": "Mirror",
+ "preset.flip_vertical": "Upside down",
"preset.merge_pdf": "Merge PDF",
"preset.split_pdf": "Split PDF",
"preset.long_image": "Long image",
diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json
index 2ee62259..f22ee536 100644
--- a/src/Filee.App/Assets/i18n/ko.json
+++ b/src/Filee.App/Assets/i18n/ko.json
@@ -106,6 +106,13 @@
"preset.half_size": "50% 축소",
"preset.grayscale": "흑백",
"preset.remove_background": "배경 제거",
+ "preset.group_rotate": "회전",
+ "preset.rotate_left": "왼쪽 90°",
+ "preset.rotate_right": "오른쪽 90°",
+ "preset.rotate_180": "180°",
+ "preset.group_flip": "뒤집기",
+ "preset.flip_horizontal": "좌우 반전",
+ "preset.flip_vertical": "상하 반전",
"preset.merge_pdf": "PDF 병합",
"preset.split_pdf": "PDF 분할",
"preset.long_image": "긴 이미지",
diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json
index bcbbb91f..bdd50fa4 100644
--- a/src/Filee.App/Assets/i18n/zh-CN.json
+++ b/src/Filee.App/Assets/i18n/zh-CN.json
@@ -106,6 +106,13 @@
"preset.half_size": "缩小 50%",
"preset.grayscale": "灰度",
"preset.remove_background": "去除背景",
+ "preset.group_rotate": "旋转",
+ "preset.rotate_left": "向左 90°",
+ "preset.rotate_right": "向右 90°",
+ "preset.rotate_180": "180°",
+ "preset.group_flip": "翻转",
+ "preset.flip_horizontal": "水平翻转",
+ "preset.flip_vertical": "垂直翻转",
"preset.merge_pdf": "合并 PDF",
"preset.split_pdf": "拆分 PDF",
"preset.long_image": "长图",
diff --git a/src/Filee.Core/Presets/BuiltInData.cs b/src/Filee.Core/Presets/BuiltInData.cs
index 65fab5c3..089fe66d 100644
--- a/src/Filee.Core/Presets/BuiltInData.cs
+++ b/src/Filee.Core/Presets/BuiltInData.cs
@@ -43,6 +43,7 @@ public static class BuiltInData
Id = "remove-background", NameKey = "preset.remove_background", TargetFormat = "png",
Image = { RemoveBackground = true }, Output = { FileNamePattern = "{name}_transparent" },
},
+ .. ImageToolPresets(),
new()
{
Id = "tiff-300", Name = "TIFF 300dpi", TargetFormat = "tiff",
@@ -165,6 +166,36 @@ public static class BuiltInData
new() { Id = "to-eot", Name = "EOT", TargetFormat = "eot" },
];
+ /// <summary>
+ /// Rotate and flip presets, and the two groups that fan them out on the donut (added in settings schema 11). On no
+ /// default donut: users add "Rotate" / "Flip" (or single presets) in Settings → Toolbar.
+ /// </summary>
+ public static List<Preset> ImageToolPresets() =>
+ [
+ Tool("rotate-left", "preset.rotate_left", "{name}_left", i => i.Rotation = ImageRotation.Left90),
+ Tool("rotate-right", "preset.rotate_right", "{name}_right", i => i.Rotation = ImageRotation.Right90),
+ Tool("rotate-180", "preset.rotate_180", "{name}_180", i => i.Rotation = ImageRotation.Rotate180),
+ Tool("flip-horizontal", "preset.flip_horizontal", "{name}_mirrored", i => i.Flip = ImageFlip.Horizontal),
+ Tool("flip-vertical", "preset.flip_vertical", "{name}_flipped", i => i.Flip = ImageFlip.Vertical),
+ new()
+ {
+ Id = "rotate", NameKey = "preset.group_rotate", TargetFormat = SameAsSource, Kind = PresetKind.Group,
+ SubPresetIds = ["rotate-left", "rotate-right", "rotate-180"],
+ },
+ new()
+ {
+ Id = "flip", NameKey = "preset.group_flip", TargetFormat = SameAsSource, Kind = PresetKind.Group,
+ SubPresetIds = ["flip-horizontal", "flip-vertical"],
+ },
+ ];
+
+ private static Preset Tool(string id, string nameKey, string pattern, Action<ImageOptions> set)
+ {
+ var preset = new Preset { Id = id, NameKey = nameKey, TargetFormat = SameAsSource, Output = { FileNamePattern = pattern } };
+ set(preset.Image);
+ return preset;
+ }
+
/// <summary>Creates the default toolbar profiles.</summary>
public static List<ToolbarProfile> CreateProfiles() =>
[
diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.cs
index 759d30e3..5412c8d2 100644
--- a/src/Filee.Core/Presets/Preset.cs
+++ b/src/Filee.Core/Presets/Preset.cs
@@ -133,6 +133,27 @@ public enum ResizeMode
Width,
}
+/// <summary>How an image is turned, after its EXIF orientation is applied.</summary>
+public enum ImageRotation
+{
+ None,
+ /// <summary>90° clockwise.</summary>
+ Right90,
+ /// <summary>90° counter-clockwise.</summary>
+ Left90,
+ Rotate180,
+}
+
+/// <summary>How an image is mirrored, after it is turned.</summary>
+public enum ImageFlip
+{
+ None,
+ /// <summary>Left ↔ right.</summary>
+ Horizontal,
+ /// <summary>Top ↔ bottom.</summary>
+ Vertical,
+}
+
/// <summary>TIFF compression schemes.</summary>
public enum TiffCompression
{
@@ -149,6 +170,12 @@ public sealed class ImageOptions
public ResizeMode Resize { get; set; } = ResizeMode.None;
+ /// <summary>Turns the image (after its EXIF orientation is applied).</summary>
+ public ImageRotation Rotation { get; set; }
+
+ /// <summary>Mirrors the image (after <see cref="Rotation"/>).</summary>
+ public ImageFlip Flip { get; set; }
+
/// <summary>Scale in percent when <see cref="Resize"/> is <see cref="ResizeMode.Percent"/>.</summary>
public int ResizePercent { get; set; } = 50;
diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs
index 7e4f98ea..4df29b84 100644
--- a/src/Filee.Core/Settings/AppSettings.cs
+++ b/src/Filee.Core/Settings/AppSettings.cs
@@ -50,7 +50,7 @@ public sealed class DonutSettings
public sealed class AppSettings
{
/// <summary>Current schema version. Increase when the format changes and add a migration.</summary>
- public const int CurrentSchemaVersion = 10;
+ public const int CurrentSchemaVersion = 11;
public int SchemaVersion { get; set; } = CurrentSchemaVersion;
diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs
index cd138f18..09398ef6 100644
--- a/src/Filee.Core/Settings/UserDataStore.cs
+++ b/src/Filee.Core/Settings/UserDataStore.cs
@@ -372,6 +372,16 @@ internal static class SettingsMigrations
changed = true;
}
+ // v11: rotate / flip presets and the "Rotate" / "Flip" groups that fan them out, on no donut.
+ if (from < 11)
+ {
+ foreach (var preset in BuiltInData.ImageToolPresets().Where(p => presets.All(existing => existing.Id != p.Id)))
+ {
+ presets.Add(preset);
+ changed = true;
+ }
+ }
+
return changed;
}
diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs
index 574820af..bf692d80 100644
--- a/src/Filee.Engines/Magick/ImageEncoder.cs
+++ b/src/Filee.Engines/Magick/ImageEncoder.cs
@@ -165,6 +165,25 @@ internal static class ImageEncoder
{
image.AutoOrient();
+ // After AutoOrient, so a phone photo turns the way it looks, not the way its pixels are stored.
+ switch (options.Rotation)
+ {
+ case ImageRotation.Right90:
+ image.Rotate(90);
+ break;
+ case ImageRotation.Left90:
+ image.Rotate(-90);
+ break;
+ case ImageRotation.Rotate180:
+ image.Rotate(180);
+ break;
+ }
+ // ImageMagick's Flop mirrors left ↔ right, Flip top ↔ bottom.
+ if (options.Flip == ImageFlip.Horizontal)
+ image.Flop();
+ else if (options.Flip == ImageFlip.Vertical)
+ image.Flip();
+
switch (options.Resize)
{
case ResizeMode.Percent when options.ResizePercent is > 0 and not 100:
diff --git a/tests/Filee.Cli.Tests/CliTests.cs b/tests/Filee.Cli.Tests/CliTests.cs
index c20718b9..485f5b9f 100644
--- a/tests/Filee.Cli.Tests/CliTests.cs
+++ b/tests/Filee.Cli.Tests/CliTests.cs
@@ -105,6 +105,7 @@ public sealed class CliTests : IDisposable
[InlineData(new[] { "convert", "a.png", "--to", "jpg", "--preset", "x" }, "--to")]
[InlineData(new[] { "convert", "a.png", "--to", "nosuchformat" }, "unknown format")]
[InlineData(new[] { "convert", "a.png", "--preset", "nosuchpreset" }, "no preset")]
+ [InlineData(new[] { "convert", "a.png", "--preset", "rotate" }, "rotate-left, rotate-right, rotate-180")]
[InlineData(new[] { "convert", "--to", "jpg" }, "at least one file")]
[InlineData(new[] { "convert", "a.png", "--to", "jpg", "--quality", "300" }, "--quality")]
[InlineData(new[] { "frobnicate" }, "Unknown command")]
diff --git a/tests/Filee.Core.Tests/SmallParsersTests.cs b/tests/Filee.Core.Tests/SmallParsersTests.cs
index 3fda3f52..d67cdd56 100644
--- a/tests/Filee.Core.Tests/SmallParsersTests.cs
+++ b/tests/Filee.Core.Tests/SmallParsersTests.cs
@@ -61,6 +61,20 @@ public class BuiltInDataTests
Assert.False(new Preset { SubPresetIds = ["to-png"] }.IsGroup); // members alone don't make a group
}
+ [Fact]
+ public void Built_in_groups_fan_out_existing_conversions_on_no_default_donut()
+ {
+ var presets = BuiltInData.CreatePresets().ToDictionary(p => p.Id);
+ var groups = presets.Values.Where(p => p.IsGroup).ToList();
+ Assert.NotEmpty(groups);
+ foreach (var group in groups)
+ {
+ Assert.InRange(group.SubPresetIds.Count, 1, Preset.MaxSubPresets);
+ Assert.All(group.SubPresetIds, id => Assert.False(presets[id].IsGroup, id));
+ }
+ Assert.DoesNotContain(BuiltInData.CreateProfiles(), p => p.PresetIds.Any(id => presets[id].IsGroup));
+ }
+
[Fact]
public void Preset_ids_are_unique()
{
diff --git a/tests/Filee.Core.Tests/UserDataStoreTests.cs b/tests/Filee.Core.Tests/UserDataStoreTests.cs
index bad173f7..713e1a55 100644
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@ -272,6 +272,24 @@ public class UserDataStoreTests
Assert.False(SettingsMigrations.ApplyToLibrary(9, presets, profiles)); // once only
}
+ [Fact]
+ public void Version_10_libraries_get_rotate_and_flip_presets_and_groups_on_no_donut()
+ {
+ var presets = BuiltInData.CreatePresets();
+ presets.RemoveAll(p => p.Id is "rotate-left" or "rotate-right" or "rotate-180" or "flip-horizontal" or "flip-vertical" or "rotate" or "flip");
+ var profiles = BuiltInData.CreateProfiles();
+
+ Assert.True(SettingsMigrations.ApplyToLibrary(10, presets, profiles));
+
+ var rotate = presets.Single(p => p.Id == "rotate");
+ Assert.True(rotate.IsGroup);
+ Assert.Equal(["rotate-left", "rotate-right", "rotate-180"], rotate.SubPresetIds);
+ Assert.Equal(ImageRotation.Left90, presets.Single(p => p.Id == "rotate-left").Image.Rotation);
+ Assert.Equal(ImageFlip.Horizontal, presets.Single(p => p.Id == "flip-horizontal").Image.Flip);
+ Assert.DoesNotContain(profiles, p => p.PresetIds.Any(id => id.StartsWith("rotate") || id.StartsWith("flip")));
+ Assert.False(SettingsMigrations.ApplyToLibrary(10, presets, profiles)); // once only
+ }
+
[Fact]
public void Catalog_migration_ignores_extensions_checked_on_mixed_profiles()
{
diff --git a/tests/Filee.Engines.Tests/ImageToolTests.cs b/tests/Filee.Engines.Tests/ImageToolTests.cs
new file mode 100644
index 00000000..6d32eff3
--- /dev/null
+++ b/tests/Filee.Engines.Tests/ImageToolTests.cs
@@ -0,0 +1,39 @@
+// Rotate and flip presets: the image turns and mirrors the way the preset names say.
+
+using Filee.Core.Conversion;
+using Filee.Core.Presets;
+using ImageMagick;
+using ImageMagick.Drawing;
+
+namespace Filee.Engines.Tests;
+
+public class ImageToolTests(EngineFixture fx) : IClassFixture<EngineFixture>
+{
+ /// <summary>A 4 × 2 white image with one red pixel in the top left corner, run through a built-in preset.</summary>
+ [Theory]
+ [InlineData("rotate-right", "a_right.png", 2, 4, 1, 0)]
+ [InlineData("rotate-left", "a_left.png", 2, 4, 0, 3)]
+ [InlineData("rotate-180", "a_180.png", 4, 2, 3, 1)]
+ [InlineData("flip-horizontal", "a_mirrored.png", 4, 2, 3, 0)]
+ [InlineData("flip-vertical", "a_flipped.png", 4, 2, 0, 1)]
+ public async Task The_corner_pixel_moves_where_the_preset_says(string presetId, string name, int width, int height, int x, int y)
+ {
+ var dir = fx.NewFolder();
+ var source = Path.Combine(dir, "a.png");
+ using (var image = new MagickImage(MagickColors.White, 4, 2))
+ {
+ new Drawables().FillColor(MagickColors.Red).Point(0, 0).Draw(image);
+ image.Write(source, MagickFormat.Png);
+ }
+
+ var job = await fx.ConvertAsync([source], BuiltInData.CreatePresets().Single(p => p.Id == presetId));
+
+ Assert.Equal(JobState.Completed, job.State);
+ var output = job.Outputs.Single();
+ Assert.Equal(name, Path.GetFileName(output));
+ using var result = new MagickImage(output);
+ Assert.Equal(((uint)width, (uint)height), (result.Width, result.Height));
+ var red = result.GetPixels().GetPixel(x, y).ToColor()!;
+ Assert.True(red.R > 200 && red.G < 60, $"{presetId}: ({x},{y}) is {red}");
+ }
+}