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.

fix(hide): Pixelate and Black box clear a TIFF's extra channels

lightscape-jm73d98ac8ba97
diff --git a/src/Filee.Engines/Magick/AreaHider.cs b/src/Filee.Engines/Magick/AreaHider.cs
index 8974d0d6..cda3b9e2 100644
--- a/src/Filee.Engines/Magick/AreaHider.cs
+++ b/src/Filee.Engines/Magick/AreaHider.cs
@@ -135,6 +135,7 @@ public static class AreaHider
using var squares = new MagickImage();
squares.ReadPixels(data, new PixelReadSettings((uint)regionWidth, (uint)regionHeight, StorageType.Char, mapping));
image.Composite(squares, left, top, CompositeOperator.Copy);
+ ClearExtraChannels(image, bounds);
}
/// <summary>
@@ -153,6 +154,26 @@ public static class AreaHider
if (image.HasAlpha)
black.Alpha(AlphaOption.Opaque);
image.Composite(black, x, y, CompositeOperator.Copy);
+ ClearExtraChannels(image, bounds);
+ }
+
+ /// <summary>
+ /// The channels a picture can carry beside its colours and alpha (a TIFF's extra samples, read as meta channels)
+ /// set to 0 inside <paramref name="bounds"/>. Pixelate and Box write colours and alpha only, and what was under a
+ /// part must not stay behind in them. Blur works on a copy of the picture, so it covers them with the rest.
+ /// </summary>
+ private static void ClearExtraChannels(IMagickImage<byte> image, (int X, int Y, int Width, int Height) bounds)
+ {
+ var extra = image.Channels.Where(channel => channel >= PixelChannel.Meta0).ToList();
+ if (extra.Count == 0)
+ return;
+ var (x, y, w, h) = bounds;
+ // A copy of the part keeps every channel, so copying it back covers the extra ones too.
+ using var part = image.CloneArea(new MagickGeometry(x, y, (uint)w, (uint)h));
+ part.ResetPage();
+ foreach (var channel in extra)
+ part.Evaluate(Enum.Parse<Channels>(channel.ToString()), EvaluateOperator.Set, 0);
+ image.Composite(part, x, y, CompositeOperator.Copy);
}
/// <summary>
diff --git a/tests/Filee.Engines.Tests/HideAreasTests.cs b/tests/Filee.Engines.Tests/HideAreasTests.cs
index da8d979a..69125fd1 100644
--- a/tests/Filee.Engines.Tests/HideAreasTests.cs
+++ b/tests/Filee.Engines.Tests/HideAreasTests.cs
@@ -160,6 +160,45 @@ public class HideAreasTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.True(pixels.GetValue(10, 50)![3] <= tolerance);
}
+ [Theory]
+ [InlineData(HideStyle.Box)]
+ [InlineData(HideStyle.Pixelate)]
+ public async Task Extra_channels_of_a_tiff_keep_nothing_of_the_part(HideStyle style)
+ {
+ // A TIFF with two extra samples beside RGBA (read as meta channels), each its own noise. Pixelate and Box write
+ // colours and alpha only: without clearing them, the part stayed in those channels pixel for pixel.
+ var folder = fx.NewFolder();
+ var path = Path.Combine(folder, "layers.tif");
+ using (var channels = new MagickImageCollection())
+ {
+ for (var seed = 1; seed <= 6; seed++)
+ {
+ var channel = Noise(300, 200, seed);
+ channel.ColorType = ColorType.Grayscale;
+ channels.Add(channel);
+ }
+ using var combined = channels.Combine(ColorSpace.sRGB);
+ combined.Write(path, MagickFormat.Tiff);
+ }
+ var area = Area(0.1, 0.1, 0.5, 0.5); // 30, 20, 150 × 100
+
+ var output = await RunAsync(path, Hide(style, 5, area));
+
+ using var before = new MagickImage(path);
+ using var after = new MagickImage(output);
+ Assert.Equal([PixelChannel.Meta0, PixelChannel.Meta1], after.Channels.Where(c => c >= PixelChannel.Meta0));
+ using var a = before.GetPixels();
+ using var b = after.GetPixels();
+ int[] extra = [4, 5];
+ for (var y = 20; y < 120; y++)
+ for (var x = 30; x < 180; x++)
+ foreach (var c in extra)
+ Assert.Equal(0, b.GetValue(x, y)![c]);
+ // Outside the part, every channel as it was.
+ foreach (var (x, y) in new[] { (29, 50), (180, 50), (100, 19), (100, 120), (250, 150) })
+ Assert.Equal(a.GetValue(x, y), b.GetValue(x, y));
+ }
+
[Fact]
public async Task Blur_leaves_none_of_the_parts_detail_and_keeps_everything_else()
{