using System.Security.Cryptography; using Febe.Core.Codecs.Images; using static Febe.Tests.Imaging.TestImages; namespace Febe.Tests.Imaging; /// /// The photo conversion of tappa E3: what is copied as it is and why (D5, D6), what a conversion reports instead of /// throwing, and that converting the same picture twice gives the same bytes (D9). /// [TestClass] public sealed class ImageConversionTests { private static readonly byte[] Palette4 = [0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]; [TestMethod] [DataRow(ImageFormat.Jpeg)] [DataRow(ImageFormat.Png)] [DataRow(ImageFormat.WebP)] public void ConvertingTwiceGivesTheSameBytes(ImageFormat target) { using var picture = Gradient(97, 61, seed: 7); var source = ImageConverter.Encode(picture, target == ImageFormat.Png ? ImageFormat.Jpeg : ImageFormat.Png, null); var first = ImageConverter.Convert(source, target); var second = ImageConverter.Convert(source, target); Assert.AreEqual(target, ImageFormatSniffer.Detect(first.Data)); CollectionAssert.AreEqual(SHA256.HashData(first.Data), SHA256.HashData(second.Data)); Assert.AreEqual(target == ImageFormat.Jpeg, first.Lossy); } [TestMethod] public void WebpCostsUseNoFloatingPointLog() { // The fixed-point log2 of the VP8L cost estimates is exact on powers of two and monotonic. var previous = -1L; for (ulong v = 1; v < 70_000; v += 13) { var log = Vp8LEncoder.Log2Fixed(v); Assert.IsGreaterThanOrEqualTo(previous, log); previous = log; } Assert.AreEqual(10L << 16, Vp8LEncoder.Log2Fixed(1024)); Assert.AreEqual(20L << 16, Vp8LEncoder.Log2Fixed(1UL << 20)); Assert.AreEqual(0L, Vp8LEncoder.Log2Fixed(1)); } [TestMethod] public void TheSameFormatIsCopiedAsItIs() { using var picture = Gradient(16, 16, seed: 3); var png = ImageConverter.Encode(picture, ImageFormat.Png, null); var check = ImageConverter.Check(png, ImageFormat.Png); Assert.AreEqual(ImageConversionAction.CopyAsIs, check.Action); Assert.IsNull(check.NoteCode); Assert.AreEqual(ImageConversionAction.Convert, ImageConverter.Check(png, ImageFormat.Jpeg).Action); } [TestMethod] public void AnimationsAndDamagedFilesAreCopiedWithTheirReason() { byte[] indices = [0, 1]; var animated = Gif(2, 1, Palette4, GifFrame(0, 0, 2, 1, indices, 2), GifControl(1), GifFrame(0, 0, 2, 1, indices, 2)); var still = Gif(2, 1, Palette4, GifFrame(0, 0, 2, 1, indices, 2)); Assert.AreEqual(ImageConversionLosses.AsIsAnimated, ImageConverter.Check(animated, ImageFormat.Png).NoteCode); Assert.AreEqual(ImageConversionAction.Convert, ImageConverter.Check(still, ImageFormat.Png).Action); var damaged = ImageConverter.Check(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, ImageFormat.Jpeg); Assert.AreEqual(ImageConversionAction.CopyOriginal, damaged.Action); Assert.AreEqual(ImageConversionLosses.AsIsNotDecodable, damaged.NoteCode); } [TestMethod] public void TransparencyToJpgIsAnnouncedAndTheImageIsLeftAlone() { using var picture = Random(8, 8, seed: 11); var before = picture.ToArray(); var png = ImageConverter.Encode(picture, ImageFormat.Png, null); var result = ImageConverter.Convert(png, ImageFormat.Jpeg); _ = ImageConverter.Encode(picture, ImageFormat.Jpeg, null); CollectionAssert.Contains(result.Losses.ToList(), ImageConversionLosses.TransparencyWhite); CollectionAssert.AreEqual(before, picture.ToArray(), "Encode lavora su una copia."); } }