Files
Chronos/tests/Chronos.Core.Windows.Tests/Wic/WicTestImages.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

167 lines
6.1 KiB
C#

using System.Buffers.Binary;
using System.Runtime.InteropServices;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Windows.Interop;
using Chronos.Core.Windows.Wic;
namespace Chronos.Core.Windows.Tests.Wic;
/// <summary>Synthetic fixtures: PNG through the managed encoder, JPEG through WIC's own encoder.</summary>
internal static unsafe partial class WicTestImages
{
private static readonly Guid JpegContainer = new("19e4a5aa-5662-4fc5-a0c0-1758028e1057");
private static readonly Guid PixelFormat8bppGray = new("6fddc324-4e03-4bfe-b185-3d77768dc908");
private const uint STGM_WRITE = 0x1, STGM_SHARE_EXCLUSIVE = 0x10, STGM_CREATE = 0x1000;
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
private const int WICBitmapEncoderNoCache = 2;
public static void WriteRgbPng(string path, int width, int height, Func<int, int, (byte R, byte G, byte B)> pixel)
{
var data = new byte[width * height * 3];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var (r, g, b) = pixel(x, y);
var i = ((y * width) + x) * 3;
data[i] = r;
data[i + 1] = g;
data[i + 2] = b;
}
}
File.WriteAllBytes(path, PngEncoder.Encode(data, width, height, PngColorType.Rgb));
}
public static void WriteGray16Png(string path, int width, int height, ushort value)
{
var data = new byte[width * height * 2];
for (var i = 0; i < data.Length; i += 2) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(i), value);
File.WriteAllBytes(path, PngEncoder.Encode(data, width, height, PngColorType.Gray, bitDepth: 16));
}
/// <summary>Grayscale baseline JPEG from WIC's encoder; optionally tagged with an Exif orientation.</summary>
public static void WriteGrayJpeg(string path, int width, int height, Func<int, int, byte> gray, int? exifOrientation = null)
{
var pixels = new byte[width * height];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++) pixels[(y * width) + x] = gray(x, y);
}
var container = JpegContainer;
Marshal.ThrowExceptionForHR(WicFactory.Instance.CreateEncoder(ref container, 0, out var encoderPointer));
IWICBitmapEncoder encoder;
try
{
encoder = (IWICBitmapEncoder)Marshal.GetObjectForIUnknown(encoderPointer);
}
finally
{
Marshal.Release(encoderPointer);
}
Marshal.ThrowExceptionForHR(SHCreateStreamOnFileEx(path, STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, FILE_ATTRIBUTE_NORMAL, 1, 0, out var stream));
IWICBitmapFrameEncode? frame = null;
nint options = 0;
try
{
encoder.Initialize(stream, WICBitmapEncoderNoCache);
encoder.CreateNewFrame(out frame, out options);
frame.Initialize(options);
frame.SetSize((uint)width, (uint)height);
var format = PixelFormat8bppGray;
frame.SetPixelFormat(ref format);
if (format != PixelFormat8bppGray) throw new InvalidOperationException("The WIC JPEG encoder did not accept 8bppGray.");
fixed (byte* p = pixels) frame.WritePixels((uint)height, (uint)width, (uint)pixels.Length, (nint)p);
frame.Commit();
encoder.Commit();
}
finally
{
Win32Com.Release(frame);
if (options != 0) Marshal.Release(options);
Win32Com.Release(encoder);
Marshal.Release(stream);
}
if (exifOrientation is { } orientation) InsertExifOrientation(path, orientation);
}
/// <summary>Adds an APP1 Exif segment holding only IFD0 Orientation (after APP0 when present, as cameras do).</summary>
private static void InsertExifOrientation(string path, int orientation)
{
var jpeg = File.ReadAllBytes(path);
byte[] tiff =
[
(byte)'I', (byte)'I', 42, 0, 8, 0, 0, 0, // little-endian header, IFD0 at offset 8
1, 0, // one entry
0x12, 0x01, 3, 0, 1, 0, 0, 0, (byte)orientation, 0, 0, 0, // 0x0112 Orientation, SHORT, count 1
0, 0, 0, 0, // no next IFD
];
var insertAt = 2;
if (jpeg[2] == 0xFF && jpeg[3] == 0xE0) insertAt = 4 + BinaryPrimitives.ReadUInt16BigEndian(jpeg.AsSpan(4));
var segmentLength = 2 + 6 + tiff.Length;
using var output = new MemoryStream();
output.Write(jpeg, 0, insertAt);
output.Write([0xFF, 0xE1, (byte)(segmentLength >> 8), (byte)segmentLength]);
output.Write("Exif\0\0"u8);
output.Write(tiff);
output.Write(jpeg, insertAt, jpeg.Length - insertAt);
File.WriteAllBytes(path, output.ToArray());
}
[LibraryImport("shlwapi.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int SHCreateStreamOnFileEx(string file, uint mode, uint attributes, int create, nint template, out nint stream);
}
[ComImport]
[Guid("00000103-a8f2-4877-ba0a-fd2b6645fb94")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IWICBitmapEncoder
{
void Initialize(nint stream, int cacheOption);
Guid GetContainerFormat();
void GetEncoderInfo(out nint encoderInfo);
void SetColorContexts(uint count, nint colorContexts);
void SetPalette(nint palette);
void SetThumbnail(nint thumbnail);
void SetPreview(nint preview);
void CreateNewFrame(out IWICBitmapFrameEncode frame, out nint encoderOptions);
void Commit();
}
[ComImport]
[Guid("00000105-a8f2-4877-ba0a-fd2b6645fb94")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IWICBitmapFrameEncode
{
void Initialize(nint encoderOptions);
void SetSize(uint width, uint height);
void SetResolution(double dpiX, double dpiY);
void SetPixelFormat(ref Guid pixelFormat);
void SetColorContexts(uint count, nint colorContexts);
void SetPalette(nint palette);
void SetThumbnail(nint thumbnail);
void WritePixels(uint lineCount, uint stride, uint bufferSize, nint pixels);
void WriteSource(nint source, nint rect);
void Commit();
}