E3: prove di codec, rimuxaggio, fotogrammi, copie e conversione
Le prove del nucleo della tappa E3 (WP6a), con i media costruiti dalle prove: il controllo delle foto (stesso formato, HEIC/AVIF/raw, animate, CMYK e oltre 8 bit, budget di pixel), metadati nei blocchi giusti e perdite annotate, pixel identici in WEBP senza perdita; il rimuxaggio da MOV (nuovo TestVideos.Mov) e Matroska a MP4 con campioni, date, rotazione, lingua ed edit; i fotogrammi H.264 e H.265 all'istante, ogni k secondi e N in tutto; i suffissi protetti dal taglio a 240; le copie convertite, com'è con nota, adozione, «già copiato» in ogni forma, «Copia di nuovo» esatta, spazio sul convertito, fotogrammi nella cartella delle copie (FrameOnly, una copia per campione, lotti su più pagine, tetto); la conversione della libreria (perdite, rimuxaggio, doppioni, importazione delle copie convertite). Servono a fissare il comportamento di E3 prima dell'unione. Nessun codice del prodotto cambia; un limite trovato (istanti a meno di 1 ms in «N in tutto») è annotato per Alberto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d06b587806
commit
f5e12c5d74
9 files changed
+1408
-4
No files matched your search
@@ -452,6 +452,128 @@ public sealed class ImageProcessingTests
|
||||
Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => ImageConverter.Convert(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, ImageFormat.Png));
|
||||
}
|
||||
|
||||
// ---- Conversions of tappa E3 ----
|
||||
|
||||
[TestMethod]
|
||||
public void PngWithTransparencyToJpgIsWhite()
|
||||
{
|
||||
using var clear = Flat(16, 16, 10, 200, 30, 0);
|
||||
var png = PngEncoder.Encode(clear);
|
||||
|
||||
var result = ImageConverter.Convert(png, ImageFormat.Jpeg);
|
||||
|
||||
CollectionAssert.AreEqual(new[] { ImageConversionLosses.TransparencyWhite }, result.Losses.ToArray());
|
||||
using var back = ImageDecoder.Decode(result.Data);
|
||||
AssertEveryPixel(back, (255, 255, 255, 255), 2, "trasparente diventa bianco");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WebpLosslessHasTheSamePixelsAndPngToWebpAndBackIsIdentical()
|
||||
{
|
||||
using var picture = TestImages.Random(37, 23, 71); // random alpha: some 0, some 255, the colour under it too
|
||||
var png = PngEncoder.Encode(picture);
|
||||
|
||||
var webp = ImageConverter.Convert(png, ImageFormat.WebP);
|
||||
Assert.IsFalse(webp.Lossy);
|
||||
Assert.IsEmpty(webp.Losses);
|
||||
using var fromWebp = ImageDecoder.Decode(webp.Data);
|
||||
CollectionAssert.AreEqual(picture.ToArray(), fromWebp.ToArray(), "ogni pixel identico");
|
||||
|
||||
var again = ImageConverter.Convert(webp.Data, ImageFormat.Png);
|
||||
using var fromPng = ImageDecoder.Decode(again.Data);
|
||||
CollectionAssert.AreEqual(picture.ToArray(), fromPng.ToArray(), "PNG → WEBP → PNG");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MetadataIsCarriedIntoTheRightChunksOfEachFormat()
|
||||
{
|
||||
using var scene = Scenes.Scene(11, 64, 40);
|
||||
var exif = Tiff(orientation: 8);
|
||||
var icc = Profile(500);
|
||||
const string xmp = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF/></x:xmpmeta>";
|
||||
var jpeg = JpegEncoder.Encode(scene, new JpegEncoderOptions { Quality = 90, Exif = exif, IccProfile = icc, Xmp = xmp });
|
||||
|
||||
// PNG: eXIf, iCCP and an iTXt «XML:com.adobe.xmp», all before the image data.
|
||||
var png = ImageConverter.Convert(jpeg, ImageFormat.Png).Data;
|
||||
var chunks = TestImages.PngChunks(png);
|
||||
var idat = chunks.FindIndex(c => c.Type == "IDAT");
|
||||
CollectionAssert.AreEqual(exif, chunks.Single(c => c.Type == "eXIf").Payload);
|
||||
Assert.IsLessThan(idat, chunks.FindIndex(c => c.Type == "eXIf"));
|
||||
Assert.IsLessThan(idat, chunks.FindIndex(c => c.Type == "iCCP"));
|
||||
var itxt = chunks.Single(c => c.Type == "iTXt").Payload;
|
||||
StringAssert.StartsWith(Encoding.Latin1.GetString(itxt), "XML:com.adobe.xmp\0");
|
||||
StringAssert.EndsWith(Encoding.UTF8.GetString(itxt), xmp);
|
||||
|
||||
// WEBP: VP8X first with its flags, ICCP before the image, EXIF and XMP after it.
|
||||
var webp = ImageConverter.Convert(jpeg, ImageFormat.WebP).Data;
|
||||
var riff = WebPTestFiles.Chunks(webp);
|
||||
CollectionAssert.AreEqual(new[] { "VP8X", "ICCP", "VP8L", "EXIF", "XMP " }, riff.Select(c => c.Type).ToArray());
|
||||
Assert.AreEqual(0x20 | 0x08 | 0x04, riff[0].Payload[0] & 0x2C, "flag di ICC, EXIF e XMP");
|
||||
CollectionAssert.AreEqual(icc, riff[1].Payload);
|
||||
CollectionAssert.AreEqual(exif, riff[3].Payload);
|
||||
Assert.AreEqual(xmp, Encoding.UTF8.GetString(riff[4].Payload));
|
||||
|
||||
// JPG from the PNG: APP1 «Exif», APP2 «ICC_PROFILE» and APP1 of the XMP namespace.
|
||||
var back = ImageConverter.Convert(png, ImageFormat.Jpeg).Data;
|
||||
var segments = JpegSegments(back);
|
||||
var app1 = segments.Where(s => s.Marker == 0xE1).Select(s => s.Payload).ToList();
|
||||
Assert.IsTrue(app1.Any(p => p.AsSpan().StartsWith("Exif\0\0"u8) && p.AsSpan(6).SequenceEqual(exif)), "EXIF in APP1");
|
||||
Assert.IsTrue(app1.Any(p => p.AsSpan().StartsWith("http://ns.adobe.com/xap/1.0/\0"u8) && Encoding.UTF8.GetString(p.AsSpan(29)) == xmp), "XMP in APP1");
|
||||
var app2 = segments.Single(s => s.Marker == 0xE2).Payload;
|
||||
Assert.IsTrue(app2.AsSpan().StartsWith("ICC_PROFILE\0"u8));
|
||||
CollectionAssert.AreEqual(icc, app2[14..]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhatCannotBeCarriedBecomesANote()
|
||||
{
|
||||
// A grey JPEG with a grey profile, to every target: the RGB output cannot carry the profile.
|
||||
using var scene = Scenes.Scene(12, 32, 24);
|
||||
var grey = JpegEncoder.Encode(scene, new JpegEncoderOptions { Grayscale = true, IccProfile = Profile(300, "GRAY") });
|
||||
foreach (var target in new[] { ImageFormat.Png, ImageFormat.WebP, ImageFormat.Jpeg })
|
||||
{
|
||||
CollectionAssert.AreEqual(new[] { ImageConversionLosses.LostIcc }, ImageConverter.Convert(grey, target).Losses.ToArray(), $"grigio in {target}");
|
||||
}
|
||||
|
||||
// A CMYK JPEG with its profile, to JPG: converted (it is a JPEG, so only an explicit conversion), profile left behind.
|
||||
const int width = 16;
|
||||
const int height = 8;
|
||||
var fine = Enumerable.Repeat((ushort)1, 64).ToArray();
|
||||
int[] Plane(int value) => Enumerable.Repeat(value, width * height).ToArray();
|
||||
var cmyk = WithIcc(JpegTestEncoder.Write(
|
||||
JpegEncoder.TransformPlanes(width, height, 8, [(1, 1, 1, 0, Plane(200)), (2, 1, 1, 0, Plane(120)), (3, 1, 1, 0, Plane(60)), (4, 1, 1, 0, Plane(230))], [fine], 0xC1),
|
||||
new JpegTestOptions { Jfif = false, AdobeTransform = 0 }), Profile(400, "CMYK"));
|
||||
CollectionAssert.AreEqual(new[] { ImageConversionLosses.LostIcc }, ImageConverter.Convert(cmyk, ImageFormat.Jpeg).Losses.ToArray());
|
||||
|
||||
// A PNG with texts and colour chunks, to WEBP: no texts in WebP, the colour chunks are not written.
|
||||
using var picture = TestImages.Random(6, 5, 3, opaque: true);
|
||||
var gamma = new byte[4];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(gamma, 45455);
|
||||
var parts = TestImages.PngChunks(PngEncoder.Encode(picture, new PngEncoderOptions { Texts = [new ImageText("Title", "Lago")] }));
|
||||
var png = TestImages.Png([parts[0], ("gAMA", gamma), .. parts.Skip(1)]);
|
||||
var webp = ImageConverter.Convert(png, ImageFormat.WebP);
|
||||
CollectionAssert.AreEqual(new[] { ImageConversionLosses.LostTexts, ImageConversionLosses.LostColorChunks }, webp.Losses.ToArray(), "in un ordine fisso");
|
||||
Assert.HasCount(2, webp.NotCarried);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ADamagedExifIsDroppedNotThrown()
|
||||
{
|
||||
// A WebP keeps whatever its EXIF chunk holds (PNG and JPEG readers already skip a block that is not TIFF).
|
||||
using var picture = TestImages.Random(6, 5, 4, opaque: true);
|
||||
var vp8l = WebPTestFiles.Payload(WebPEncoder.Encode(picture), "VP8L");
|
||||
var webp = WebPTestFiles.Riff(("VP8X", WebPTestFiles.Vp8X(0x08, 6, 5)), ("VP8L", vp8l), ("EXIF", [1, 2, 3, 4, 5, 6, 7, 8, 9]));
|
||||
Assert.IsNotNull(ImageDecoder.Identify(webp).Metadata.Exif);
|
||||
|
||||
foreach (var target in new[] { ImageFormat.Jpeg, ImageFormat.Png })
|
||||
{
|
||||
var result = ImageConverter.Convert(webp, target);
|
||||
Assert.Contains(ImageConversionLosses.LostExifUnreadable, result.Losses, $"{target}");
|
||||
Assert.IsNull(result.Metadata.Exif, $"{target}");
|
||||
Assert.IsNull(ImageDecoder.Identify(result.Data).Metadata.Exif, $"{target}: il file scritto non ha l'EXIF");
|
||||
}
|
||||
}
|
||||
|
||||
// ---- PNG texts ----
|
||||
|
||||
[TestMethod]
|
||||
@@ -674,6 +796,21 @@ public sealed class ImageProcessingTests
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>The marker segments of a JPEG up to its first scan: marker (second byte) and payload.</summary>
|
||||
private static List<(int Marker, byte[] Payload)> JpegSegments(byte[] jpeg)
|
||||
{
|
||||
var segments = new List<(int, byte[])>();
|
||||
var pos = 2;
|
||||
while (pos + 4 <= jpeg.Length && jpeg[pos] == 0xFF && jpeg[pos + 1] != 0xDA)
|
||||
{
|
||||
var length = BinaryPrimitives.ReadUInt16BigEndian(jpeg.AsSpan(pos + 2));
|
||||
segments.Add((jpeg[pos + 1], jpeg.AsSpan(pos + 4, length - 2).ToArray()));
|
||||
pos += 2 + length;
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
||||
/// <summary>A JPEG with an APP2 ICC_PROFILE segment (one chunk) right after SOI.</summary>
|
||||
private static byte[] WithIcc(byte[] jpeg, byte[] profile)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user