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: a turned JPEG keeps its own quality

KnifeLemonc024fb7bedcd
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 439c4d4d..b6c6ca01 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -120,6 +120,9 @@ current.
the file's range when there is no window, to 8 bits. Compressed pixel data (JPEG, JPEG-LS, JPEG 2000, RLE) is left to
ImageMagick, which manages only 8-bit images; others fail with a message asking for an uncompressed export.
- **Fujifilm HEIF** (`.hif`) is read as HEIC.
+- **Rotate and flip** happen after the EXIF orientation is applied, in the one step of a route that turns images
+ (`IImageTransformer`). There is no lossless JPEG rotation: a turned JPEG is re-encoded, at its own estimated quality
+ when that is lower than the preset's, so a phone photo saved at 75 doesn't come back bigger at 95.
- **Layered files.** PSD/PSB convert from Photoshop's composite (ImageMagick merges the layers when a file was saved
without "maximize compatibility"). GIMP XCF layers are composited at their offsets with their blend modes; hidden
layers are left out. ImageMagick reads 8-bit XCF without zlib tile compression (GIMP 2.10+ "better but slower
diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs
index e9a232ea..c73b1ffe 100644
--- a/src/Filee.Engines/Magick/ImageEncoder.cs
+++ b/src/Filee.Engines/Magick/ImageEncoder.cs
@@ -275,6 +275,10 @@ internal static class ImageEncoder
}
var quality = (uint)Math.Clamp(options.Quality, 1, 100);
+ // Turning a JPEG has to re-encode it: at the photo's own quality (Magick's estimate when it read the file), not
+ // above it, or a phone photo saved at 75 comes back at 95, bigger and no better.
+ if (options.TurnsOrMirrors && targetFormat == "jpg" && image.Format == MagickFormat.Jpeg && image.Quality is > 0 and <= 100)
+ quality = Math.Min(quality, image.Quality);
switch (targetFormat)
{
case "jpg":
diff --git a/tests/Filee.Engines.Tests/ImageToolTests.cs b/tests/Filee.Engines.Tests/ImageToolTests.cs
index 14f4b2f6..f78ef883 100644
--- a/tests/Filee.Engines.Tests/ImageToolTests.cs
+++ b/tests/Filee.Engines.Tests/ImageToolTests.cs
@@ -188,6 +188,26 @@ public class ImageToolTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.False(File.Exists(Path.Combine(dir, "doc_left.pdf")));
}
+ [Fact]
+ public async Task A_turned_jpeg_keeps_its_own_quality_instead_of_the_presets_higher_one()
+ {
+ var dir = fx.NewFolder();
+ var source = Path.Combine(dir, "phone.jpg");
+ using (var photo = new MagickImage(MagickColors.SkyBlue, 320, 240))
+ {
+ photo.Draw(new Drawables().FillColor(MagickColors.OrangeRed).Ellipse(160, 120, 90, 60, 0, 360));
+ photo.Quality = 75;
+ photo.Write(source, MagickFormat.Jpeg);
+ }
+
+ var job = await fx.ConvertAsync([source], Tool("rotate-left")); // the preset says quality 95
+
+ Assert.Equal(JobState.Completed, job.State);
+ using var turned = new MagickImage(job.Outputs.Single());
+ Assert.Equal((240u, 320u), (turned.Width, turned.Height));
+ Assert.InRange(turned.Quality, 70u, 75u);
+ }
+
[Fact]
public async Task A_long_image_of_turned_pages_still_runs_top_to_bottom()
{