Skip to content

Commit b142dba

Browse files
Add AOT compiler seeds for ICO, CUR, QOI formats
- Add ICO, CUR, and QOI encoder/decoder seeding in AotCompilerTools - Introduce SeedPixelOperations() to explicitly seed all built-in pixel format PixelOperations<T> specializations for Mono WASM AOT - Add [DynamicDependency] on Configuration constructors to keep the seed graph visible to .NET trimmers - Add AI coding guideline files (AGENTS.md, CLAUDE.md, GEMINI.md, copilot-instructions.md) - Update .gitattributes to treat .a, .dylib, and .so as binary
1 parent 776cb8b commit b142dba

7 files changed

Lines changed: 197 additions & 0 deletions

File tree

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@
8484
# treat as binary
8585
###############################################################################
8686
*.basis binary
87+
*.a binary
8788
*.dll binary
89+
*.dylib binary
8890
*.exe binary
8991
*.pdf binary
9092
*.ppt binary
9193
*.pptx binary
9294
*.pvr binary
95+
*.so binary
9396
*.snk binary
9497
*.xls binary
9598
*.xlsx binary

.github/copilot-instructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GitHub Copilot Instructions
2+
3+
Read and follow [AGENTS.md](../AGENTS.md) as the repository-wide source of coding, performance, and verification requirements. Prefer existing local patterns and repository configuration whenever generated code or suggestions are accepted.

AGENTS.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Six Labors AI Coding Guidelines
2+
3+
These instructions apply to the entire repository. More-specific `AGENTS.md` files may add to or override them for their directory tree.
4+
5+
## Working Practices
6+
7+
- Inspect the relevant implementation, tests, benchmarks, project files, and nearby code before proposing or making changes. Do not infer current behavior when the source is available.
8+
- Make the smallest complete change that solves the requested problem. Avoid unrelated cleanup, speculative abstractions, and formatting churn.
9+
- Match established architecture, naming, formatting, documentation, and test patterns. Treat `.editorconfig`, analyzers, and repository build settings as authoritative.
10+
- Preserve public API and observable behavior unless the task explicitly requires a change. Public API documentation must describe observable behavior, not implementation details.
11+
- Do not use reflection against built assemblies, ad hoc assembly loading, or temporary probe projects unless explicitly requested.
12+
- Build .NET projects in Release configuration unless explicitly instructed otherwise.
13+
14+
## Performance
15+
16+
- Treat throughput, latency, memory use, and binary size as design constraints, especially in pixel-processing, drawing, parsing, encoding, and other hot paths.
17+
- Avoid unnecessary allocations, copies, boxing, closures, interface dispatch, repeated enumeration, and extra passes over data.
18+
- Reuse the repository's existing memory ownership, pooling, span, vectorization, and parallelization patterns. Do not introduce a new mechanism when an established one fits.
19+
- Keep hot loops simple and bounds-check-friendly. Hoist invariant work, preserve locality, and use the narrowest suitable data types without sacrificing correctness.
20+
- Do not trade correctness or maintainability for assumed speed. Support non-obvious optimizations with measurements or clear evidence, and add or update benchmarks when performance is the purpose of the change.
21+
- Consider all supported target frameworks and runtime capabilities. Do not regress fallback paths while optimizing newer runtimes.
22+
23+
## C# Conventions
24+
25+
- Follow the existing code around the change; local patterns take precedence over generic preferences.
26+
- Do not use `record` or `record struct` types.
27+
- Prefer established invariants over redundant guards. Validate at real external boundaries and do not add defensive checks for internally controlled states.
28+
- Do not extract single-use helpers merely to name a block. Extract only for genuine reuse, an established local pattern, or meaningful complexity reduction.
29+
- Add vertical whitespace after multi-line statements and declarations and between distinct logical stages. Never add trailing whitespace.
30+
- Document every method, constructor, and property, regardless of whether it is public, internal, protected, or private. Keep public API documentation limited to observable behavior; use private and internal documentation to capture the contract and intent needed to maintain the code.
31+
- Add inline comments throughout complex code. Explain algorithms, formulas, invariants, ownership, compatibility behavior, and performance tradeoffs at the operations and decisions they govern. Explain why the code is shaped that way rather than narrating the syntax.
32+
- Document SIMD code especially thoroughly. Explain the vector layout, lane meaning, widening or narrowing, masks, shuffles, constants, alignment or remainder handling, supported instruction paths, scalar equivalence, and the reason each non-obvious operation is correct.
33+
- Write algorithm and SIMD comments for a maintainer who is unfamiliar with the implementation. The reader should not need to reconstruct intent from external documentation, issue history, or benchmark results.
34+
35+
## Verification
36+
37+
- Add or update focused tests when behavior changes, following the test framework and conventions already used by the project.
38+
- Never hack, weaken, skip, conditionally bypass, or otherwise manipulate a test to make it pass. Fix the production defect or the genuine test defect while preserving the test's intended coverage and sensitivity.
39+
- Do not update golden files, reference images, snapshots, baselines, or expected-output artifacts to resolve a test failure. Treat a mismatch as evidence to investigate and correct the implementation.
40+
- Run the narrowest relevant formatting, test, and Release build commands, then expand verification in proportion to the risk and scope of the change.
41+
- Report what changed, the verification performed, and any remaining risks or unverified assumptions.

CLAUDE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Claude Code Instructions
2+
3+
Read and follow [AGENTS.md](AGENTS.md) as the repository-wide source of coding, performance, and verification requirements. Apply any more-specific `AGENTS.md` or `CLAUDE.md` found below the files being changed.

GEMINI.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Gemini CLI Instructions
2+
3+
Read and follow [AGENTS.md](AGENTS.md) as the repository-wide source of coding, performance, and verification requirements. Apply any more-specific `AGENTS.md` or `GEMINI.md` found below the files being changed.

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
using System.Runtime.CompilerServices;
88
using SixLabors.ImageSharp.Formats;
99
using SixLabors.ImageSharp.Formats.Bmp;
10+
using SixLabors.ImageSharp.Formats.Cur;
1011
using SixLabors.ImageSharp.Formats.Exr;
1112
using SixLabors.ImageSharp.Formats.Gif;
13+
using SixLabors.ImageSharp.Formats.Ico;
1214
using SixLabors.ImageSharp.Formats.Jpeg;
1315
using SixLabors.ImageSharp.Formats.Jpeg.Components;
1416
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
@@ -128,6 +130,67 @@ private static void SeedPixelFormats()
128130
throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
129131
}
130132

133+
/// <summary>
134+
/// Seeds the modern .NET AOT compiler with bulk pixel operations for every built-in pixel format.
135+
/// </summary>
136+
/// <exception cref="InvalidOperationException">
137+
/// This method is used for AOT code generation only. Do not call it at runtime.
138+
/// </exception>
139+
[Preserve]
140+
public static void SeedPixelOperations()
141+
{
142+
try
143+
{
144+
// Keep this inventory finite and explicit. ImageSharp cannot precompile consumer-defined pixel types, while
145+
// these closed calls make every built-in specialization visible without retaining the much larger legacy seed graph.
146+
AotCompilePixelOperations<A8>();
147+
AotCompilePixelOperations<Argb32>();
148+
AotCompilePixelOperations<Argb32P>();
149+
AotCompilePixelOperations<Abgr32>();
150+
AotCompilePixelOperations<Abgr32P>();
151+
AotCompilePixelOperations<Bgr24>();
152+
AotCompilePixelOperations<Bgr565>();
153+
AotCompilePixelOperations<Bgra32>();
154+
AotCompilePixelOperations<Bgra32P>();
155+
AotCompilePixelOperations<Bgra4444>();
156+
AotCompilePixelOperations<Bgra5551>();
157+
AotCompilePixelOperations<Byte4>();
158+
AotCompilePixelOperations<L16>();
159+
AotCompilePixelOperations<L8>();
160+
AotCompilePixelOperations<La16>();
161+
AotCompilePixelOperations<La32>();
162+
AotCompilePixelOperations<HalfSingle>();
163+
AotCompilePixelOperations<HalfVector2>();
164+
AotCompilePixelOperations<HalfVector4>();
165+
AotCompilePixelOperations<HalfVector4P>();
166+
AotCompilePixelOperations<NormalizedByte2>();
167+
AotCompilePixelOperations<NormalizedByte4>();
168+
AotCompilePixelOperations<NormalizedByte4P>();
169+
AotCompilePixelOperations<NormalizedShort2>();
170+
AotCompilePixelOperations<NormalizedShort4>();
171+
AotCompilePixelOperations<Rg32>();
172+
AotCompilePixelOperations<Rgb24>();
173+
AotCompilePixelOperations<Rgb48>();
174+
AotCompilePixelOperations<Rgb96>();
175+
AotCompilePixelOperations<Rgba1010102>();
176+
AotCompilePixelOperations<Rgba128>();
177+
AotCompilePixelOperations<Rgba32>();
178+
AotCompilePixelOperations<Rgba32P>();
179+
AotCompilePixelOperations<Rgba64>();
180+
AotCompilePixelOperations<RgbaHalf>();
181+
AotCompilePixelOperations<RgbaHalfP>();
182+
AotCompilePixelOperations<RgbaVector>();
183+
AotCompilePixelOperations<Short2>();
184+
AotCompilePixelOperations<Short4>();
185+
}
186+
catch
187+
{
188+
// The calls only need to exist in IL; this method must never contribute a runtime execution path.
189+
}
190+
191+
throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
192+
}
193+
131194
/// <summary>
132195
/// Seeds the compiler using the given pixel format.
133196
/// </summary>
@@ -138,6 +201,7 @@ private static void Seed<TPixel>()
138201
{
139202
// This is we actually call all the individual methods you need to seed.
140203
AotCompileImage<TPixel>();
204+
AotCompilePixelOperations<TPixel>();
141205
AotCompileImageProcessingContextFactory<TPixel>();
142206
AotCompileImageEncoderInternals<TPixel>();
143207
AotCompileImageDecoderInternals<TPixel>();
@@ -158,6 +222,69 @@ private static void Seed<TPixel>()
158222
// TODO: Do the discovery work to figure out what works and what doesn't.
159223
}
160224

225+
/// <summary>
226+
/// Seeds every bulk operation exposed by <see cref="PixelOperations{TPixel}"/>.
227+
/// </summary>
228+
/// <typeparam name="TPixel">The pixel format.</typeparam>
229+
[Preserve]
230+
private static void AotCompilePixelOperations<TPixel>()
231+
where TPixel : unmanaged, IPixel<TPixel>
232+
{
233+
// These default arguments are never consumed. Direct calls are required so the IL contains the exact closed
234+
// MethodSpecs that Mono WASM AOT can otherwise miss when following static-abstract pixel dispatch indirectly.
235+
_ = PixelOperations<TPixel>.Instance;
236+
PixelOperations<TPixel> operations = default;
237+
238+
_ = operations.GetPixelTypeInfo();
239+
_ = operations.GetPixelBlender(default(GraphicsOptions));
240+
_ = operations.GetPixelBlender(default, default);
241+
operations.FromVector4Destructive(default, default, default);
242+
operations.FromVector4Destructive(default, default, default, default);
243+
operations.ToVector4(default, default, default);
244+
operations.ToVector4(default, default, default, default);
245+
operations.PackFromRgbPlanes(default, default, default, default);
246+
operations.UnpackIntoRgbPlanes(default, default, default, default);
247+
248+
operations.FromArgb32Bytes(default, default, default, default);
249+
operations.ToArgb32Bytes(default, default, default, default);
250+
251+
operations.FromAbgr32Bytes(default, default, default, default);
252+
operations.ToAbgr32Bytes(default, default, default, default);
253+
254+
operations.FromBgr24Bytes(default, default, default, default);
255+
operations.ToBgr24Bytes(default, default, default, default);
256+
257+
operations.FromBgra32Bytes(default, default, default, default);
258+
operations.ToBgra32Bytes(default, default, default, default);
259+
260+
operations.FromL8Bytes(default, default, default, default);
261+
operations.ToL8Bytes(default, default, default, default);
262+
263+
operations.FromL16Bytes(default, default, default, default);
264+
operations.ToL16Bytes(default, default, default, default);
265+
266+
operations.FromLa16Bytes(default, default, default, default);
267+
operations.ToLa16Bytes(default, default, default, default);
268+
269+
operations.FromLa32Bytes(default, default, default, default);
270+
operations.ToLa32Bytes(default, default, default, default);
271+
272+
operations.FromRgb24Bytes(default, default, default, default);
273+
operations.ToRgb24Bytes(default, default, default, default);
274+
275+
operations.FromRgba32Bytes(default, default, default, default);
276+
operations.ToRgba32Bytes(default, default, default, default);
277+
278+
operations.FromRgb48Bytes(default, default, default, default);
279+
operations.ToRgb48Bytes(default, default, default, default);
280+
281+
operations.FromRgba64Bytes(default, default, default, default);
282+
operations.ToRgba64Bytes(default, default, default, default);
283+
284+
operations.FromBgra5551Bytes(default, default, default, default);
285+
operations.ToBgra5551Bytes(default, default, default, default);
286+
}
287+
161288
/// <summary>
162289
/// This method pre-seeds the <see cref="Image{TPixel}"/> for a given pixel format in the AoT compiler.
163290
/// </summary>
@@ -229,8 +356,10 @@ private static void AotCompileImageEncoderInternals<TPixel>()
229356
where TPixel : unmanaged, IPixel<TPixel>
230357
{
231358
default(BmpEncoderCore).Encode<TPixel>(default, default, default);
359+
default(CurEncoderCore).Encode<TPixel>(default, default, default);
232360
default(ExrEncoderCore).Encode<TPixel>(default, default, default);
233361
default(GifEncoderCore).Encode<TPixel>(default, default, default);
362+
default(IcoEncoderCore).Encode<TPixel>(default, default, default);
234363
default(JpegEncoderCore).Encode<TPixel>(default, default, default);
235364
default(PbmEncoderCore).Encode<TPixel>(default, default, default);
236365
default(PngEncoderCore).Encode<TPixel>(default, default, default);
@@ -249,8 +378,10 @@ private static void AotCompileImageDecoderInternals<TPixel>()
249378
where TPixel : unmanaged, IPixel<TPixel>
250379
{
251380
default(BmpDecoderCore).Decode<TPixel>(default, default, default);
381+
default(CurDecoderCore).Decode<TPixel>(default, default, default);
252382
default(ExrDecoderCore).Decode<TPixel>(default, default, default);
253383
default(GifDecoderCore).Decode<TPixel>(default, default, default);
384+
default(IcoDecoderCore).Decode<TPixel>(default, default, default);
254385
default(JpegDecoderCore).Decode<TPixel>(default, default, default);
255386
default(PbmDecoderCore).Decode<TPixel>(default, default, default);
256387
default(PngDecoderCore).Decode<TPixel>(default, default, default);
@@ -270,11 +401,14 @@ private static void AotCompileImageEncoders<TPixel>()
270401
{
271402
AotCompileImageEncoder<TPixel, WebpEncoder>();
272403
AotCompileImageEncoder<TPixel, BmpEncoder>();
404+
AotCompileImageEncoder<TPixel, CurEncoder>();
273405
AotCompileImageEncoder<TPixel, ExrEncoder>();
274406
AotCompileImageEncoder<TPixel, GifEncoder>();
407+
AotCompileImageEncoder<TPixel, IcoEncoder>();
275408
AotCompileImageEncoder<TPixel, JpegEncoder>();
276409
AotCompileImageEncoder<TPixel, PbmEncoder>();
277410
AotCompileImageEncoder<TPixel, PngEncoder>();
411+
AotCompileImageEncoder<TPixel, QoiEncoder>();
278412
AotCompileImageEncoder<TPixel, TgaEncoder>();
279413
AotCompileImageEncoder<TPixel, TiffEncoder>();
280414
}
@@ -289,11 +423,14 @@ private static void AotCompileImageDecoders<TPixel>()
289423
{
290424
AotCompileImageDecoder<TPixel, WebpDecoder>();
291425
AotCompileImageDecoder<TPixel, BmpDecoder>();
426+
AotCompileImageDecoder<TPixel, CurDecoder>();
292427
AotCompileImageDecoder<TPixel, ExrDecoder>();
293428
AotCompileImageDecoder<TPixel, GifDecoder>();
429+
AotCompileImageDecoder<TPixel, IcoDecoder>();
294430
AotCompileImageDecoder<TPixel, JpegDecoder>();
295431
AotCompileImageDecoder<TPixel, PbmDecoder>();
296432
AotCompileImageDecoder<TPixel, PngDecoder>();
433+
AotCompileImageDecoder<TPixel, QoiDecoder>();
297434
AotCompileImageDecoder<TPixel, TgaDecoder>();
298435
AotCompileImageDecoder<TPixel, TiffDecoder>();
299436
}

src/ImageSharp/Configuration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Collections.Concurrent;
5+
using System.Diagnostics.CodeAnalysis;
6+
using SixLabors.ImageSharp.Advanced;
57
using SixLabors.ImageSharp.Formats;
68
using SixLabors.ImageSharp.Formats.Bmp;
79
using SixLabors.ImageSharp.Formats.Cur;
@@ -38,6 +40,9 @@ public sealed class Configuration
3840
/// <summary>
3941
/// Initializes a new instance of the <see cref="Configuration" /> class.
4042
/// </summary>
43+
// Every image operation requires a Configuration. Attaching the dependency to its constructors keeps the compile-only
44+
// seed graph visible to modern .NET trimmers without executing that graph or adding module-initialization work.
45+
[DynamicDependency(nameof(AotCompilerTools.SeedPixelOperations), typeof(AotCompilerTools))]
4146
public Configuration()
4247
{
4348
}
@@ -46,6 +51,8 @@ public Configuration()
4651
/// Initializes a new instance of the <see cref="Configuration" /> class.
4752
/// </summary>
4853
/// <param name="configurationModules">A collection of configuration modules to register.</param>
54+
// The default Configuration is created through this overload, while callers may use either constructor.
55+
[DynamicDependency(nameof(AotCompilerTools.SeedPixelOperations), typeof(AotCompilerTools))]
4956
public Configuration(params IImageFormatConfigurationModule[] configurationModules)
5057
{
5158
if (configurationModules != null)

0 commit comments

Comments
 (0)