Le 16 osservazioni confermate della revisione, ognuna con la sua prova: - JPEG: il controllo stima la memoria di lavoro del decodificatore (progressivo), così un file che non si decodifica esce «troppo grande» e non «illeggibile». - Un EXIF che non si tiene lascia un EXIF minimo con l'orientamento, perché la foto convertita non esca girata. - Fotogrammi: istanti in millisecondi interi; la copia trova l'indice del nome dall'istante, non dall'ordine delle righe. - Video di durata sconosciuta misurati prima di registrare: contano nei limiti di 500 per richiesta e 5000 per video. - Rimuxaggio: la durata del segmento di elst passa nell'MP4, la fine tagliata resta nascosta (anche nei fotogrammi). - H.264: il punto di recupero vale come IDR solo con exact_match_flag. - MP3 in MP4: objectTypeIndication 0x69 fino a 24 kHz, 0x6B oltre. - Conversione della libreria: il lavoro toglie le righe di un formato spento o cambiato; spegnere toglie anche i «Non riuscito»; «Riprova» solo i formati di adesso; pausa per spazio prima di rileggere l'originale. - «Anche i N» conta e mette in coda solo il tipo che si cambia; le impostazioni dell'esportazione si salvano una per volta. - Eliminazione e «Tieni il più grande» scelgono i convertiti nella stessa transazione: nessun convertito orfano. - Un file arrivato tardi in un elemento vecchio mette in coda solo sé (file nuovo = salvato da quando il formato è scelto). - Zip dei fotogrammi a pezzi di 64 KB: un browser lento che legge non si interrompe, uno fermo sì. Decisioni in docs/decisioni.md (28/11), §4.10 della specifica precisato. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
5.8 KiB
C#
138 lines
5.8 KiB
C#
using System.Security.Cryptography;
|
|
using Febe.Core.Codecs.Images;
|
|
using static Febe.Tests.Imaging.TestImages;
|
|
|
|
namespace Febe.Tests.Imaging;
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
[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.");
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(true)]
|
|
[DataRow(false)]
|
|
public void AnExifTooLargeForJpgStillKeepsTheOrientation(bool littleEndian)
|
|
{
|
|
// 70 000 bytes of TIFF (a big maker note, say) with Orientation 6: more than one APP1 segment holds. The block is
|
|
// left behind, but the pixels are never turned (§4.10): without the tag the JPG would show turned.
|
|
var exif = new byte[70_000];
|
|
var tiff = exif.AsSpan();
|
|
tiff[0] = tiff[1] = littleEndian ? (byte)'I' : (byte)'M';
|
|
void W16(int at, ushort v)
|
|
{
|
|
if (littleEndian)
|
|
{
|
|
System.Buffers.Binary.BinaryPrimitives.WriteUInt16LittleEndian(exif.AsSpan(at), v);
|
|
}
|
|
else
|
|
{
|
|
System.Buffers.Binary.BinaryPrimitives.WriteUInt16BigEndian(exif.AsSpan(at), v);
|
|
}
|
|
}
|
|
|
|
W16(2, 42);
|
|
W16(littleEndian ? 4 : 6, 8); // first IFD at 8
|
|
W16(8, 1);
|
|
W16(10, 0x0112);
|
|
W16(12, 3);
|
|
W16(littleEndian ? 14 : 16, 1);
|
|
W16(18, 6);
|
|
using var picture = Gradient(4, 4, seed: 5);
|
|
var png = ImageConverter.Encode(picture, ImageFormat.Png, new ImageMetadata { Exif = exif, Orientation = 6 });
|
|
Assert.AreEqual(6, ImageDecoder.Identify(png).Metadata.Orientation);
|
|
|
|
var result = ImageConverter.Convert(png, ImageFormat.Jpeg);
|
|
|
|
CollectionAssert.Contains(result.Losses.ToList(), ImageConversionLosses.LostExifTooLarge);
|
|
var written = ImageDecoder.Identify(result.Data).Metadata;
|
|
Assert.AreEqual(6, written.Orientation);
|
|
Assert.AreEqual(6, result.Metadata.Orientation);
|
|
Assert.AreEqual(26, written.Exif!.Length);
|
|
Assert.AreEqual(littleEndian ? (byte)'I' : (byte)'M', written.Exif[0], "l'ordine dei byte della sorgente");
|
|
|
|
// Orientation 1 needs no block at all.
|
|
var upright = ImageConverter.Encode(picture, ImageFormat.Png, new ImageMetadata { Exif = exif.AsSpan(0, 8).ToArray().Concat(new byte[70_000]).ToArray(), Orientation = 1 });
|
|
Assert.IsNull(ImageDecoder.Identify(ImageConverter.Convert(upright, ImageFormat.Jpeg).Data).Metadata.Exif);
|
|
}
|
|
}
|