Files
Chronos/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs
T
Alby96andClaude Opus 5.5 b471a33bfc fix(convert): a conversion never changes what Chronos reads from a photo
Owner review 2, phase 4b, fix round 3 (codecs).

Chronos's decoders never read an ICC profile: they take code values through
the sRGB curve and float samples as they are. Round 2 wrote a float TIFF's
values with its profile's own curves, so after a Replace the renders,
thumbnails, analyses and deflicker of an Adobe RGB, ProPhoto or gamma-1.0
float TIFF saw other pixels (up to 2.7 EV darker, shadows lifted). Every
converted file now holds the values the decoders read from the original, as
they read them back, and says truthfully what they are:

- A JPEG or TIFF-16 keeps the codes of code values and carries the original's
  profile. Float values are written as sRGB codes with a profile made for
  them: the original's colorants and media white with the sRGB curve. A gray
  profile's codes are kept, its curve carried as an RGB profile (they were
  converted to sRGB before, which changed them too).
- A Linear DNG, whose matrices Chronos develops it with as well, takes only
  values that are sRGB's as the decoders read them; any other profile is
  refused as color_profile instead of converted to colours Chronos would then
  show differently.
- A profile with colorant tags that convert nothing is never sRGB by its name.
- A float file with NaN or infinite samples fails as damaged data instead of
  being written black (and spending a DNG's headroom).

The plan and the decoders behind it:

- TooLarge and WorstBytes answer any size, int.MaxValue sides included,
  without a product that wraps around; the worst case saturates.
- A frame another host flagged unreadable (the server, for a CMYK or CIELab
  TIFF its reader refuses) is asked again with a small decode where the
  conversion has other decoders (WIC on a PC), so a PC still converts it.
  The too_large sentences say "or Chronos reads back" (en, it).
- WIC's TIFF decoder decodes an unknown compression as zeros, does not undo
  the floating-point predictor of Photoshop's 32-bit files and reads 32-bit
  integers as floats, all without an error: such TIFFs are left to the
  managed reader, on the desktop as in conversions, never black or noise.
- A conversion's WIC probe reads the headers (decoder, first frame's oriented
  size, pixel format), so the size bound applies to what only WIC reads; a
  read error stays a read error (inaccessible), never a verdict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 09:36:00 +01:00

867 lines
49 KiB
C#

using System.Globalization;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Localization;
using Chronos.Core.Mathematics;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Testing;
namespace Chronos.Core.Tests.Conversion;
/// <summary>
/// One photo converted (owner review 2, R7): to JPEG, TIFF-16 and linear DNG, decoded at full resolution only, with the
/// sidecar's capture facts to the tick and the original's camera, lens, modes and GPS, verified (read back, metadata, the
/// server's decoders) before it counts.
/// </summary>
[Category("conversion")]
public sealed class FrameConverterTests(TestContext context)
{
private static readonly DateTime Captured = new DateTime(2026, 9, 28, 18, 0, 5).AddTicks(1_234_567);
/// <summary>The camera's file says one second earlier, without sub-seconds: the sidecar wins.</summary>
private static readonly FrameMetadata Camera = new()
{
Make = "Canon",
Model = "Canon EOS R5",
BodySerial = "032021001234",
LensModel = "RF15-35mm F2.8 L IS USM",
CapturedAt = new DateTime(2026, 9, 28, 18, 0, 4),
UtcOffsetMinutes = 60,
ExposureTime = 1 / 250.0,
FNumber = 8,
Iso = 100,
FocalLength = 15,
FocalLength35 = 15,
ExposureBias = -2 / 3.0,
ExposureProgram = 3,
MeteringMode = 5,
Flash = 16,
WhiteBalanceMode = 1,
Artist = "Alberto",
Gps = new GpsPosition(45.4642035, 9.1899817, 122.5, null),
};
private static SidecarFrame Frame(string file, ImageFileFormat format, int width, int height) => new()
{
Id = Guid.NewGuid(),
File = "originals/" + file,
Original = file,
Sha256 = new string('a', 64),
CapturedAt = Captured,
UtcOffset = 120,
TimeSource = TimestampSource.ExifOriginal,
Exposure = 1 / 125.0,
FNumber = 5.6,
Iso = 200,
Focal = 24,
Bias = -1,
W = width,
H = height,
Format = format,
Flags = FrameFlags.Intervalometer,
};
/// <summary>A gradient with some texture, so every encoder has real work and errors show.</summary>
private static ImageBuffer Scene(int width, int height)
{
var image = new ImageBuffer(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
image[0, x, y] = 0.05f + 0.6f * x / width;
image[1, x, y] = 0.1f + 0.5f * y / height;
image[2, x, y] = 0.2f + 0.2f * ((x / 8 + y / 8) % 2);
}
}
return image;
}
private string Source(int width = 96, int height = 64, int orientation = 1)
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.JPG");
using var image = Scene(width, height);
var exif = ExifWriter.BuildJpegApp1(Camera with { Orientation = orientation });
File.WriteAllBytes(path, JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 97, Subsampling = JpegSubsampling.Yuv444, App1Segments = [exif[4..]] }));
return path;
}
private string Target(ConvertOptions options) => Path.Combine(context.CreateTempDirectory("staged"), "converted" + options.Extension);
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheSidecarsCaptureFactsAndTheCamerasMetadataRoundTrip(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((options.Format, 96, 64), (file.Format, file.Width, file.Height));
Assert.Equal(new FileInfo(file.Path).Length, file.Size);
Assert.Equal(file.Sha256, ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult());
var read = MetadataReader.Read(file.Path);
Assert.Equal(options.Format, read.Format);
// The sidecar wins: the capture time to the tick, its offset, exposure, aperture, ISO, focal length and bias.
Assert.Equal(Captured, read.CapturedAt);
Assert.Equal(120, read.UtcOffsetMinutes);
Assert.Equal(TimestampSource.ExifOriginal, read.TimestampSource);
Assert.Approximately(1 / 125.0, read.ExposureTime!.Value, 1e-12);
Assert.Approximately(5.6, read.FNumber!.Value, 1e-9);
Assert.Equal(200, read.Iso);
Assert.Approximately(24, read.FocalLength!.Value, 1e-9);
Assert.Approximately(-1, read.ExposureBias!.Value, 1e-4);
// The original's own: camera, serial, lens, 35 mm focal length, modes, GPS, artist.
Assert.Equal(("Canon", "Canon EOS R5", "032021001234", "RF15-35mm F2.8 L IS USM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Approximately(15, read.FocalLength35!.Value, 1e-9);
Assert.Equal((3, 5, 16, 1), (read.ExposureProgram, read.MeteringMode, read.Flash, read.WhiteBalanceMode));
var gps = Assert.NotNull(read.Gps);
Assert.Approximately(45.4642035, gps.Latitude, 1e-6);
Assert.Approximately(9.1899817, gps.Longitude, 1e-6);
Assert.Equal(1, read.Orientation);
Assert.Equal((96, 64), read.OrientedSize);
}
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheConvertedFileDecodesAtFullResolutionWithTheServersDecoders(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var server = DecoderFactory.CreateServer();
Assert.Equal(DecodeTier.Native, server.Probe(file.Path, file.Format).Tier);
using var original = server.Decode(new DecodeRequest(source, AllowPreview: false))!;
using var converted = Assert.NotNull(server.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Equal(DecodeTier.Native, converted.Tier);
Assert.Equal((96, 64), (converted.Pixels.Width, converted.Pixels.Height));
// The same picture: TIFF-16 and DNG keep it to 16 bits, the JPEG within its quantisation.
var tolerance = target == ConvertTarget.Jpeg ? 0.03 : 0.01;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
var a = original.Pixels.Plane(c);
var b = converted.Pixels.Plane(c);
for (var i = 0; i < a.Length; i++) error += Math.Abs(a[i] - b[i]);
}
Assert.Less(error / (3.0 * 96 * 64), tolerance, $"mean error of {target}");
}
[Test]
public void TheJpegQualityAndFullChromaAreTheOnesAsked()
{
var source = Source(160, 120);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 160, 120);
var decoder = DecoderFactory.CreateServer();
var high = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 95), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
var low = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 20), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
Assert.Less(low.Size, high.Size);
var info = JpegDecoder.ReadInfo(File.ReadAllBytes(high.Path));
Assert.Equal("4:4:4", info.SamplingNotation);
Assert.Equal(ConvertOptions.DefaultQuality, new ConvertOptions(ConvertTarget.Jpeg).Quality);
}
[Test]
public void TiffAndLinearDngAreSixteenBitsAndReadByTheInHouseReaders()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var decoder = DecoderFactory.CreateServer();
var tiff = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Tiff16), decoder,
Target(new ConvertOptions(ConvertTarget.Tiff16)), cancellationToken: context.Cancellation);
var dng = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.LinearDng), decoder,
Target(new ConvertOptions(ConvertTarget.LinearDng)), cancellationToken: context.Cancellation);
Assert.Equal(16, MetadataReader.Read(tiff.Path).BitsPerSample);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(tiff.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
Assert.Equal(ImageFileFormat.Dng, MetadataReader.Read(dng.Path).Format);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(dng.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
}
[Test]
public void APortraitOriginalIsConvertedUpright()
{
var source = Source(96, 64, orientation: 6);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64) with { Orientation = 6 };
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((64, 96), (file.Width, file.Height));
var read = MetadataReader.Read(file.Path);
Assert.Equal((1, 64, 96), (read.Orientation, read.Width, read.Height));
}
[Test]
public void ACameraAndLensTheFileLacksComeFromTheSequence()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.png");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, Chronos.Core.Codecs.Png.PngEncoder.Encode(image));
var frame = Frame("frame.png", ImageFileFormat.Png, 32, 24);
var options = new ConvertOptions(ConvertTarget.Tiff16);
var file = FrameConverter.Convert(new ConvertSource(path, frame, new SidecarCamera { Make = "Sony", Model = "ILCE-7RM5", Serial = "77" }, "FE 24mm F1.4 GM"),
options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var read = MetadataReader.Read(file.Path);
Assert.Equal(("Sony", "ILCE-7RM5", "77", "FE 24mm F1.4 GM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Equal(Captured, read.CapturedAt);
}
[Test]
public void APhotoReadOnlyFromItsPreviewIsNotConverted()
{
// A DNG the managed decoder reads natively, behind a chain that offers only a preview.
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var target = Target(options);
var preview = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([new PreviewOnlyProvider()]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.PreviewOnly, preview.Reason);
Assert.FileMissing(target);
var none = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.NoDecoder, none.Reason);
}
[Test]
public void AFileThatDoesNotReadBackIsRefused()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var written = FrameConverter.Metadata(MetadataReader.Read(source), new ConvertSource(source, frame));
Assert.DoesNotThrow(() => FrameConverter.Verify(file, written));
// A bit flipped on the way to the disk.
var bytes = File.ReadAllBytes(file.Path);
bytes[^10] ^= 0x40;
File.WriteAllBytes(file.Path, bytes);
Assert.Equal(ConversionFailure.NotVerified, Assert.Throws<ConversionException>(() => FrameConverter.Verify(file, written)).Reason);
// A capture time that is not the one written.
File.WriteAllBytes(file.Path, File.ReadAllBytes(source));
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file with { Sha256 = ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult(), Size = new FileInfo(file.Path).Length }, written));
}
[Test]
public void ConvertedNamesAreTheNamesFilingGives()
{
var dng = new ConvertOptions(ConvertTarget.LinearDng);
Assert.Equal("originals/IMG_0001.dng", dng.WithExtension("originals/IMG_0001.CR2"));
Assert.Equal("frame.jpg", new ConvertOptions(ConvertTarget.Jpeg).WithExtension("frame"));
// The second frame of a sequence filed by a Canon, named with the sequence's camera, its number, the title and the
// name it had on the card, as an upload or a Reorganize names it; {ext} is the target's.
var first = Frame("2026-09-28_0001.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 0, Original = "IMG_0001.CR3" };
var second = Frame("2026-09-28_0002.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 1, Original = "IMG_0002.CR3", CapturedAt = Captured.AddSeconds(10) };
var sequence = new SequenceManifest
{
Id = Guid.NewGuid(),
Title = "Harbour",
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
Frames = [first, second],
};
Assert.Equal("originals/Harbour_Canon EOS R5_0002_IMG_0002.dng",
dng.FileName(PathTemplate.Parse("{title}_{camera}_{n}_{name}.{ext}"), second, sequence));
Assert.Equal("originals/2026-09-28_0002.tif",
new ConvertOptions(ConvertTarget.Tiff16).FileName(PathTemplate.Parse("{date}_{n}"), second, sequence));
Assert.Equal(PathTemplate.Parse("{date}_{n}").ExpandFile(FrameNaming.Context(second with { File = "originals/x.dng" }, sequence, "Harbour")),
Path.GetFileName(dng.FileName(PathTemplate.Parse("{date}_{n}"), second, sequence)));
// A frame adopted in place stays in its folder; a long name keeps the target's extension whole.
var adopted = second with { File = "IMG_0002.CR3", Original = new string('x', 300) + ".CR3" };
var named = dng.FileName(PathTemplate.Parse("{name}.{ext}"), adopted, sequence);
Assert.False(named.Contains('/'));
Assert.True(named.EndsWith(".dng", StringComparison.Ordinal) && named.Length <= PathTemplate.MaxSegmentLength, named);
Assert.Equal(ConvertTarget.LinearDng, ConvertOptions.ArchivalTarget);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 0).IsValid);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 101).IsValid);
Assert.Equal(new ConvertOptions(ConvertTarget.LinearDng, 10).Key, new ConvertOptions(ConvertTarget.LinearDng, 90).Key);
}
/// <summary>
/// The managed decoder develops a RAW wherever it reads it natively, ahead of WIC (owner review 2, R7): a 16-bit file is
/// never filled from WIC's 8-bit RAW development, which a JPEG may still use.
/// </summary>
[Test]
public void ASixteenBitFileNeverComesFromAnEightBitRawDevelopment()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var wic = new EightBitRaw();
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng, ConvertTarget.Jpeg])
{
var options = new ConvertOptions(target);
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic, new ManagedDecoderProvider()]), Target(options),
cancellationToken: context.Cancellation);
}
Assert.Equal(0, wic.Calls);
// Where only the 8-bit development reads it: refused for 16 bits, converted to JPEG.
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
var options = new ConvertOptions(target);
var refused = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic]), Target(options), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.EightBit, refused.Reason);
}
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([wic]), Target(jpeg), cancellationToken: context.Cancellation);
Assert.Equal(1, wic.Calls);
}
/// <summary>A RAW developed above white and outside sRGB converts to a linear DNG that reads back the same.</summary>
[Test]
public void ALinearDngKeepsWhatTheRawHasAboveWhiteAndOutsideSrgb()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.CR2");
using (var image = Scene(40, 30)) File.WriteAllBytes(path, JpegEncoder.Encode(image));
var developed = Scene(40, 30);
(developed[0, 1, 1], developed[1, 1, 1], developed[2, 1, 1]) = (2.4f, 1.9f, 1.2f);
(developed[0, 2, 1], developed[1, 2, 1], developed[2, 2, 1]) = (-0.2f, 1.0f, -0.07f);
var options = new ConvertOptions(ConvertTarget.LinearDng);
var file = FrameConverter.Convert(new ConvertSource(path, Frame("IMG_0001.CR2", ImageFileFormat.Cr2, 40, 30)), options,
new ImageDecoder([new Developed(developed)]), Target(options), cancellationToken: context.Cancellation);
using var back = Assert.NotNull(DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false)));
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < developed.PlaneLength; i++) Assert.Approximately(developed.Plane(c)[i], back.Pixels.Plane(c)[i], 3e-4);
}
developed.Dispose();
}
/// <summary>
/// The colour space the original's values are in is kept: a JPEG or TIFF carries its ICC profile (or the Adobe RGB one
/// its Exif names) with the values unchanged. A linear DNG, whose matrices Chronos develops it with, cannot hold P3 codes
/// as Chronos reads them and say they are P3: it refuses the photo, which converting would show otherwise.
/// </summary>
[Test]
public void TheOriginalsColourSpaceIsKept()
{
var folder = context.CreateTempDirectory("source");
var p3 = IccProfile.DisplayP3;
using var scene = ColorFixtures.Scene();
var jpegPath = Path.Combine(folder, "p3.jpg");
File.WriteAllBytes(jpegPath, ColorFixtures.Jpeg(scene, p3.Data, ExifWriter.BuildTiff(Camera)));
var source = new ConvertSource(jpegPath, Frame("p3.jpg", ImageFileFormat.Jpeg, 48, 32));
var server = DecoderFactory.CreateServer();
using var original = server.Decode(new DecodeRequest(jpegPath, AllowPreview: false))!;
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(file.Path, file.Format).Profile!.Data);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) error += Math.Abs(original.Pixels.Plane(c)[i] - converted.Pixels.Plane(c)[i]);
}
Assert.Less(error / (3.0 * scene.PlaneLength), target == ConvertTarget.Jpeg ? 0.02 : 1e-3, $"the values of {target} pass unchanged");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
// A camera JPEG in Adobe RGB (Exif uncalibrated, Interop R03) carries the Adobe RGB profile; a PNG its iCCP.
var adobePath = Path.Combine(folder, "adobe.jpg");
File.WriteAllBytes(adobePath, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF, "R03")));
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
var adobe = FrameConverter.Convert(new ConvertSource(adobePath, Frame("adobe.jpg", ImageFileFormat.Jpeg, 48, 32)), jpeg, server, Target(jpeg),
cancellationToken: context.Cancellation);
Assert.Equal("Compatible with Adobe RGB (1998)", SourceColor.Read(adobe.Path, adobe.Format).Profile!.Description);
var pngPath = Path.Combine(folder, "p3.png");
File.WriteAllBytes(pngPath, ColorFixtures.Png(scene, p3.Data));
var tiff = new ConvertOptions(ConvertTarget.Tiff16);
var fromPng = FrameConverter.Convert(new ConvertSource(pngPath, Frame("p3.png", ImageFileFormat.Png, 48, 32)), tiff, server, Target(tiff),
cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(fromPng.Path, fromPng.Format).Profile!.Data);
}
/// <summary>A colour space nothing here can keep (uncalibrated without R03, a CMYK profile) refuses the photo, whatever the target.</summary>
[Test]
public void APhotoWhoseColourSpaceCannotBeKeptIsRefused()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
var unknown = Path.Combine(folder, "unknown.jpg");
File.WriteAllBytes(unknown, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)));
var cmyk = Path.Combine(folder, "cmyk.jpg");
File.WriteAllBytes(cmyk, ColorFixtures.Jpeg(scene, ColorFixtures.Cmyk()));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
foreach (var path in (string[])[unknown, cmyk])
{
var output = Target(options);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, Frame(Path.GetFileName(path), ImageFileFormat.Jpeg, 48, 32)),
options, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
}
}
}
/// <summary>
/// A CMYK TIFF without a profile is decoded by what reads CMYK (WIC on a PC), never by the managed reader as the gray of
/// its first ink; where nothing reads it (the server), it is not converted, and nothing is written.
/// </summary>
[Test]
public void ACmykTiffIsDecodedByWhatReadsItNeverAsGray()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "cmyk.tif");
File.WriteAllBytes(path, ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
var frame = Frame("cmyk.tif", ImageFileFormat.Tiff, 6, 4);
using var red = new ImageBuffer(6, 4);
red.Plane(0).Fill(0.6f);
red.Plane(1).Fill(0.003f);
red.Plane(2).Fill(0.002f);
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([new Developed(red, ImageFileFormat.Tiff), new ManagedDecoderProvider()]),
Target(jpeg), cancellationToken: context.Cancellation);
using var converted = DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
Assert.Greater(converted.Pixels[0, 3, 2], 0.5);
Assert.Less(converted.Pixels[1, 3, 2], 0.02);
var output = Target(jpeg);
var refused = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), jpeg, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.NoDecoder, refused.Reason);
Assert.FileMissing(output);
}
/// <summary>
/// What Chronos reads from a photo never changes with its conversion (owner review 2, R7): Chronos's decoders, which
/// never read a profile, read the converted file as they read the original, within what the target holds, whatever the
/// original's colour space (sRGB, Adobe RGB, ProPhoto, a linear-curve profile, gray) and samples (8-bit, 16-bit, float).
/// The file says truthfully what the values are: the original's profile where its codes are the original's, else one
/// made for them (the original's colorants with the sRGB curve, or a gray curve as RGB). A DNG takes only values that are
/// sRGB's as the decoders read them, and reads back the same; it refuses the others, which its matrices would show
/// otherwise.
/// </summary>
[Test]
public void WhatChronosReadsIsWhatTheConvertedFileReadsBack()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
using var grays = new ImageBuffer(48, 32, 1);
for (var i = 0; i < grays.PlaneLength; i++) grays.Plane(0)[i] = 0.002f + 0.95f * i / grays.PlaneLength;
var srgb = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
var g10 = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
var wideG10 = ColorFixtures.Profile("Wide gamma 1.0", IccCurve.Gamma(1), wide: true);
var prophoto = ColorFixtures.ProPhoto();
var gamma22 = ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2));
var sGray = ColorFixtures.Gray("sGray", IccCurve.Srgb);
(string Name, byte[] File, IccProfile? Profile, bool DngKeeps)[] sources =
[
("untagged.jpg", ColorFixtures.Jpeg(scene), null, true),
("srgb.jpg", ColorFixtures.Jpeg(scene, srgb.Data), srgb, true),
("adobe.jpg", ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("g10.jpg", ColorFixtures.Jpeg(scene, g10.Data), g10, false),
("adobe16.tif", ColorFixtures.Tiff16(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("prophoto16.tif", ColorFixtures.Tiff16(scene, prophoto.Data), prophoto, false),
("float.tif", ColorFixtures.FloatTiff(scene), null, true),
("float-srgb.tif", ColorFixtures.FloatTiff(scene, srgb.Data), srgb, true),
("float-adobe.tif", ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("float-prophoto.tif", ColorFixtures.FloatTiff(scene, prophoto.Data), prophoto, false),
("float-g10.tif", ColorFixtures.FloatTiff(scene, g10.Data), g10, true),
("float-wide-g10.tif", ColorFixtures.FloatTiff(scene, wideG10.Data), wideG10, false),
("gray22.jpg", ColorFixtures.GrayJpeg(grays, gamma22), IccProfile.Parse(gamma22), false),
("sgray.jpg", ColorFixtures.GrayJpeg(grays, sGray), IccProfile.Parse(sGray), true),
("float-gray22.tif", ColorFixtures.FloatTiff(grays, gamma22), IccProfile.Parse(gamma22), true),
];
var server = DecoderFactory.CreateServer();
foreach (var (name, bytes, profile, dngKeeps) in sources)
{
var path = Path.Combine(folder, name);
File.WriteAllBytes(path, bytes);
var format = name.EndsWith(".jpg", StringComparison.Ordinal) ? ImageFileFormat.Jpeg : ImageFileFormat.Tiff;
var color = SourceColor.Read(path, format);
var source = new ConvertSource(path, Frame(name, format, 48, 32));
using var original = server.Decode(new DecodeRequest(path, AllowPreview: false))!;
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var (max, mean) = CodeDifference(original.Pixels, converted.Pixels);
var what = $"{name} → {target}";
Assert.LessOrEqual(max, target == ConvertTarget.Jpeg ? 4 / 255.0 : 2 / 65535.0, what);
Assert.Less(mean, target == ConvertTarget.Jpeg ? 1 / 255.0 : 1 / 65535.0, what);
AssertTruthful(color, SourceColor.Read(file.Path, file.Format), original.Pixels, converted.Pixels, target == ConvertTarget.Jpeg ? 0.05 : 2e-4, what);
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
Assert.Equal(dngKeeps ? ColorTreatment.None : ColorTreatment.Refuse, FrameConverter.Treatment(color, ConvertTarget.LinearDng), name);
if (!dngKeeps)
{
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason, name);
Assert.FileMissing(output);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(output, AllowPreview: false))!;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) Assert.Approximately(original.Pixels.Plane(c)[i], developed.Pixels.Plane(c)[i], 3e-4, $"{name} → DNG");
}
}
}
/// <summary>The largest and the mean difference between two decodes of the same size, in sRGB code values (what the files hold).</summary>
private static (double Max, double Mean) CodeDifference(ImageBuffer a, ImageBuffer b)
{
Assert.Equal((a.Width, a.Height, a.Channels), (b.Width, b.Height, b.Channels));
double max = 0, sum = 0;
for (var c = 0; c < a.Channels; c++)
{
var pa = a.Plane(c);
var pb = b.Plane(c);
for (var i = 0; i < pa.Length; i++)
{
var d = Math.Abs(ColorSpace.LinearToSrgb(pa[i]) - ColorSpace.LinearToSrgb(pb[i]));
max = Math.Max(max, d);
sum += d;
}
}
return (max, sum / ((double)a.Channels * a.PlaneLength));
}
/// <summary>
/// The converted file's profile says what its values are: read through it, its codes give the light the original's
/// profile gives the original's values (linear Rec.709, to <paramref name="tolerance"/>); no profile where the original
/// had none, or where its values are linear gray.
/// </summary>
private static void AssertTruthful(SourceColor original, SourceColor converted, ImageBuffer originalPixels, ImageBuffer convertedPixels, double tolerance,
string what)
{
Assert.False(converted.IsLinear || converted.IsUnknown, what);
if (original.Profile is not { } source || (source.IsGray && original.IsLinear))
{
Assert.Null(converted.Profile, what);
return;
}
var carried = Assert.NotNull(converted.Profile, what);
if (!original.IsLinear && source.IsRgb)
{
Assert.BytesEqual(source.Data, carried.Data);
return;
}
Assert.True(carried.IsMatrixShaper, what);
var to709 = carried.ToLinearRec709();
for (var i = 0; i < originalPixels.PlaneLength; i += 11)
{
var (x, y) = (i % originalPixels.Width, i / originalPixels.Width);
Vector3D light;
if (source.IsGray)
{
// A gray profile's code: its curve's light, neutral.
var gray = source.Curves![0].Evaluate(ColorSpace.LinearToSrgb(originalPixels[0, x, y]));
light = new Vector3D(gray, gray, gray);
}
else
{
light = source.ToLinearRec709() * new Vector3D(originalPixels[0, x, y], originalPixels[1, x, y], originalPixels[2, x, y]);
}
var read = to709 * new Vector3D(
carried.Curves![0].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[0, x, y])),
carried.Curves[1].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[1, x, y])),
carried.Curves[2].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[2, x, y])));
Assert.Approximately(light.X, read.X, tolerance, what);
Assert.Approximately(light.Y, read.Y, tolerance, what);
Assert.Approximately(light.Z, read.Z, tolerance, what);
}
}
/// <summary>
/// A float TIFF's values are linear light in its profile's primaries (owner review 2, R7): a JPEG or TIFF holds them as
/// the decoders read them back, sRGB codes, and carries the profile's colorants with the sRGB curve, so colour-managed
/// programs read the same light; above white is clipped there, as in any file of code values. A DNG keeps above white,
/// for values in sRGB's primaries only: in other primaries it refuses them.
/// </summary>
[Theory]
[Case(1.0)]
[Case(1.8)]
public void AFloatTiffKeepsItsLinearValuesInItsColourSpace(double gamma)
{
foreach (var wide in (bool[])[true, false])
{
var profile = ColorFixtures.Profile($"Gamma {gamma}", IccCurve.Gamma(gamma), wide);
using var scene = new ImageBuffer(8, 4);
scene.Data.Fill(0.18f);
(scene[0, 1, 1], scene[1, 1, 1], scene[2, 1, 1]) = (0.5f, 0.1f, 0.05f);
(scene[0, 2, 1], scene[1, 2, 1], scene[2, 2, 1]) = (3f, 3f, 3f);
(scene[0, 3, 1], scene[1, 3, 1], scene[2, 3, 1]) = (0.001f, 0.01f, 0.0005f);
var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, profile.Data));
var source = new ConvertSource(path, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4));
var server = DecoderFactory.CreateServer();
Assert.Equal(ColorTreatment.Describe, FrameConverter.Treatment(SourceColor.Read(path, ImageFileFormat.Tiff), ConvertTarget.Tiff16));
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.Jpeg])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
var carried = SourceColor.Read(file.Path, file.Format).Profile!;
Assert.Equal(profile.Colorants, carried.Colorants);
Assert.True(carried.Curves!.All(c => c.IsSrgb), "the codes are sRGB's");
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var tolerance = target == ConvertTarget.Jpeg ? 0.012 : 1e-4;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (3, 1)])
{
for (var c = 0; c < 3; c++)
Assert.Approximately(ColorSpace.LinearToSrgb(scene[c, x, y]), ColorSpace.LinearToSrgb(converted.Pixels[c, x, y]), tolerance, $"{target}, gamma {gamma}");
}
Assert.Approximately(1, converted.Pixels[0, 2, 1], target == ConvertTarget.Jpeg ? 0.02 : 1e-3, "above white is clipped");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
if (wide)
{
Assert.Equal(ConversionFailure.ColorProfile,
Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation)).Reason);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(output, AllowPreview: false))!;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (2, 1), (3, 1)])
{
for (var c = 0; c < 3; c++) Assert.Approximately(scene[c, x, y], developed.Pixels[c, x, y], 2e-3 * Math.Max(1, scene[c, x, y]));
}
Assert.Greater(developed.Pixels[1, 2, 1], 2.9);
}
}
/// <summary>
/// A DNG takes values only where they are sRGB's as the decoders read them, whatever the profile's name: codes in one
/// named sRGB with a linear curve are not (the decoders read them through the sRGB curve), the same profile's linear
/// values are (sRGB's primaries), a real sRGB profile's codes are.
/// </summary>
[Test]
public void ADngTakesOnlyValuesThatAreSrgbsAsTheDecodersReadThem()
{
using var scene = ColorFixtures.Scene();
var linearCurve = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
var real = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
ColorTreatment Dng(byte[] file, ImageFileFormat format) => FrameConverter.Treatment(SourceColor.Read(new MemoryStream(file), format), ConvertTarget.LinearDng);
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, linearCurve.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.FloatTiff(scene, linearCurve.Data), ImageFileFormat.Tiff));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene, real.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), ImageFileFormat.Jpeg));
// Into a JPEG or TIFF the same codes are carried with their profile.
Assert.Equal(ColorTreatment.Carry, FrameConverter.Treatment(SourceColor.Read(new MemoryStream(ColorFixtures.Jpeg(scene, linearCurve.Data)), ImageFileFormat.Jpeg), ConvertTarget.Tiff16));
}
/// <summary>
/// A profile whose colorants convert nothing (a damaged one) refuses its photo for a DNG, at once and as a colour
/// profile, and is carried unchanged into a JPEG or TIFF with the codes it came with. One named sRGB is no sRGB: a float
/// TIFF's linear values in it, which a JPEG or TIFF would need its colorants to describe, are refused there too.
/// </summary>
[Test]
public void AProfileThatConvertsNothingIsRefusedForADngAndCarriedElsewhere()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "odd.jpg");
using var scene = ColorFixtures.Scene();
var degenerate = ColorFixtures.Degenerate();
File.WriteAllBytes(path, ColorFixtures.Jpeg(scene, degenerate));
var source = new ConvertSource(path, Frame("odd.jpg", ImageFileFormat.Jpeg, 48, 32));
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
var tiff = new ConvertOptions(ConvertTarget.Tiff16);
var carried = FrameConverter.Convert(source, tiff, DecoderFactory.CreateServer(), Target(tiff), cancellationToken: context.Cancellation);
Assert.BytesEqual(degenerate, SourceColor.Read(carried.Path, carried.Format).Profile!.Data);
var namedSrgb = ColorFixtures.Degenerate(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)).Data);
var floatPath = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(floatPath, ColorFixtures.FloatTiff(scene, namedSrgb));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
var floatOutput = Target(options);
Assert.Equal(ConversionFailure.ColorProfile, Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(floatPath, Frame("stack.tif", ImageFileFormat.Tiff, 48, 32)), options, DecoderFactory.CreateServer(), floatOutput,
cancellationToken: context.Cancellation)).Reason, target.ToString());
Assert.FileMissing(floatOutput);
}
}
/// <summary>
/// A float file whose samples are not numbers (NaN, ±infinity: damaged data) fails its conversion as damaged in every
/// target, nothing written, rather than written black (with a DNG's headroom spent on it).
/// </summary>
[Test]
public void SamplesThatAreNotNumbersFailAsDamagedData()
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
using var scene = new ImageBuffer(8, 4);
scene.Data.Fill(0.18f);
scene[1, 6, 3] = bad;
var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data));
File.WriteAllBytes(Path.ChangeExtension(path, ".untagged.tif"), ColorFixtures.FloatTiff(scene));
foreach (var file in (string[])[path, Path.ChangeExtension(path, ".untagged.tif")])
{
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
if (FrameConverter.Treatment(SourceColor.Read(file, ImageFileFormat.Tiff), target) == ColorTreatment.Refuse) continue;
var output = Target(options);
var failed = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(file, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4)), options,
DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.Damaged, failed.Reason, $"{bad} → {target}");
Assert.FileMissing(output);
}
}
}
}
/// <summary>Every NaN and infinity is found, wherever it is (in a vector or in the tail), and finite values pass, however large.</summary>
[Test]
public void NumbersAreToldFromWhatIsNot()
{
foreach (var length in (int[])[0, 1, 7, 8, 9, 31, 64, 1001])
{
var values = new float[length];
for (var i = 0; i < length; i++) values[i] = (i % 3 - 1) * (i % 2 == 0 ? float.MaxValue : float.Epsilon);
Assert.True(FrameConverter.AllFinite(values), $"{length} finite values");
for (var at = 0; at < length; at += Math.Max(1, length / 5))
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
var copy = (float[])values.Clone();
copy[at] = bad;
Assert.False(FrameConverter.AllFinite(copy), $"{bad} at {at} of {length}");
}
}
}
}
/// <summary>A photo larger than the target or one buffer holds fails as too large, not with the encoder's own exception.</summary>
[Test]
public void APhotoTooLargeForTheTargetFailsAsTooLarge()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "panorama.png");
using (var image = Scene(8, 8)) File.WriteAllBytes(path, PngEncoder.Encode(image));
var frame = Frame("panorama.png", ImageFileFormat.Png, 70_000, 1);
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
using var wide = new ImageBuffer(65_536, 1);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), jpeg,
new ImageDecoder([new Developed(wide, ImageFileFormat.Png)]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, refused.Reason);
var overflow = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), new ConvertOptions(ConvertTarget.Tiff16),
new ImageDecoder([new Overflowing()]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, overflow.Reason);
}
/// <summary>Why a photo failed reads as one sentence by cause in the job log, never the exception's English text.</summary>
[Test]
public void FailuresReadAsOneSentenceByCause()
{
foreach (var reason in Enum.GetValues<ConversionFailure>())
{
var text = FrameConverter.FailureText(new ConversionException(reason, "technical detail"));
Assert.Equal(Loc.L("Archive.Convert.Failed." + reason), text);
Assert.NotNull(Loc.Get("Archive.Convert.Failed." + reason, CultureInfo.GetCultureInfo("it")));
Assert.False(text.Contains("technical", StringComparison.Ordinal), text);
}
Assert.Equal(Loc.L("Archive.Convert.Failed.Full"), FrameConverter.FailureText(new IOException("full", unchecked((int)0x80070070))));
Assert.Equal(Loc.L("Archive.Failed.Access"), FrameConverter.FailureText(new UnauthorizedAccessException("denied")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Unreadable"), FrameConverter.FailureText(new InvalidDataException("JPEG: bad Huffman")));
Assert.Equal(Loc.L("Archive.Failed.Files"), FrameConverter.FailureText(new IOException("network")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Other"), FrameConverter.FailureText(new ArgumentOutOfRangeException("width")));
}
/// <summary>WIC with the Raw Image Extension, as a conversion sees it: every RAW native, developed at 8 bits (counted).</summary>
private sealed class EightBitRaw : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public int Calls { get; private set; }
public string Name => "wic";
public bool DevelopsRawAt8Bits => true;
public DecodeTier? TierFor(ImageFileFormat format) => ImageFormatInfo.Of(format).IsRaw ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
Calls++;
var decoded = _managed.Decode(request, cancellationToken)!;
foreach (ref var v in decoded.Pixels.Data) v = ColorSpace.Linear8(ColorSpace.Encode8(v));
return new DecodedImage(decoded.Pixels, DecodeTier.Native, Name, decoded.SourceWidth, decoded.SourceHeight);
}
}
/// <summary>A RAW decoder that develops every file of <paramref name="format"/> into a copy of <paramref name="image"/>.</summary>
private sealed class Developed(ImageBuffer image, ImageFileFormat format = ImageFileFormat.Cr2) : IImageDecoderProvider
{
public string Name => "developed";
public DecodeTier? TierFor(ImageFileFormat f) => f == format ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) =>
new(image.Clone(), DecodeTier.Native, Name, image.Width, image.Height);
}
/// <summary>A decoder whose image is too large for one buffer.</summary>
private sealed class Overflowing : IImageDecoderProvider
{
public string Name => "overflowing";
public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => throw new OverflowException();
}
/// <summary>A provider that claims DNG natively but only ever gives the embedded-preview tier.</summary>
private sealed class PreviewOnlyProvider : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public string Name => "preview-only";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Dng ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var decoded = _managed.Decode(request with { AllowPreview = true }, cancellationToken);
if (decoded is null) return null;
var copy = decoded.Pixels.Clone();
decoded.Dispose();
return new DecodedImage(copy, DecodeTier.EmbeddedPreview, Name, copy.Width, copy.Height);
}
}
}