// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) CaptureImage Contributors
using System.Collections.Concurrent;
using System.Text;
using CaptureImage.Core.Abstractions;
using CaptureImage.Core.Models;
namespace CaptureImage.Core.Tests.TestSupport;
/// One call as seen by .
/// The frame that was encoded.
/// The requested format.
/// The encoder options.
/// File the destination stream writes to (the writer's temp file).
internal sealed record EncodeCall(CapturedFrame Frame, ImageFormat Format, EncoderSettings Settings, string? DestinationPath);
///
/// Writes a short, format-specific marker instead of a real image. The marker is written in
/// two halves with in between, so a failure there leaves a partially
/// written file behind — exactly what the writer has to clean up.
///
/// Format/pixel-format pairs to accept; empty means .
internal sealed class FakeImageEncoder(
params (ImageFormat Format, FramePixelFormat PixelFormat)[] supported) : IImageEncoder
{
private readonly ConcurrentQueue _calls = new();
private readonly HashSet<(ImageFormat, FramePixelFormat)> _supported =
supported.Length > 0 ? [.. supported] : [.. DefaultSupport];
/// Every 8-bit output format, plus the JPEG XR sidecar from FP16.
public static IReadOnlyList<(ImageFormat Format, FramePixelFormat PixelFormat)> DefaultSupport { get; } =
[
(ImageFormat.Png, FramePixelFormat.Bgra8),
(ImageFormat.Jpeg, FramePixelFormat.Bgra8),
(ImageFormat.Webp, FramePixelFormat.Bgra8),
(ImageFormat.Tiff, FramePixelFormat.Bgra8),
(ImageFormat.Jxr, FramePixelFormat.RgbaHalf),
];
/// Runs after the first half of the marker is written; throw or wait from here.
public Func? Midway { get; set; }
public IReadOnlyList Calls => [.. _calls];
/// The exact bytes written for .
public static byte[] ContentFor(ImageFormat format) => Encoding.ASCII.GetBytes($"fake {format} image data");
public bool Supports(ImageFormat format, FramePixelFormat pixelFormat) => _supported.Contains((format, pixelFormat));
public async Task EncodeAsync(
CapturedFrame frame,
ImageFormat format,
EncoderSettings settings,
Stream destination,
CancellationToken cancellationToken = default)
{
var call = new EncodeCall(frame, format, settings, (destination as FileStream)?.Name);
_calls.Enqueue(call);
var content = ContentFor(format);
var half = content.Length / 2;
await destination.WriteAsync(content.AsMemory(0, half), cancellationToken);
if (Midway is { } midway)
{
await midway(call, cancellationToken);
}
await destination.WriteAsync(content.AsMemory(half), cancellationToken);
}
}