-
Notifications
You must be signed in to change notification settings - Fork 967
/
ImageAttributes.cs
465 lines (373 loc) · 16.1 KB
/
ImageAttributes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using System.IO;
using System.Runtime.CompilerServices;
#if NET9_0_OR_GREATER
using System.ComponentModel;
#endif
namespace System.Drawing.Imaging;
// sdkinc\GDIplusImageAttributes.h
// There are 5 possible sets of color adjustments:
// ColorAdjustDefault,
// ColorAdjustBitmap,
// ColorAdjustBrush,
// ColorAdjustPen,
// ColorAdjustText,
// Bitmaps, Brushes, Pens, and Text will all use any color adjustments
// that have been set into the default ImageAttributes until their own
// color adjustments have been set. So as soon as any "Set" method is
// called for Bitmaps, Brushes, Pens, or Text, then they start from
// scratch with only the color adjustments that have been set for them.
// Calling Reset removes any individual color adjustments for a type
// and makes it revert back to using all the default color adjustments
// (if any). The SetToIdentity method is a way to force a type to
// have no color adjustments at all, regardless of what previous adjustments
// have been set for the defaults or for that type.
/// <summary>
/// Contains information about how image colors are manipulated during rendering.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public sealed unsafe class ImageAttributes : ICloneable, IDisposable
{
private const int ColorMapStackSpace = 32;
internal GpImageAttributes* _nativeImageAttributes;
internal void SetNativeImageAttributes(GpImageAttributes* handle)
{
if (handle is null)
throw new ArgumentNullException(nameof(handle));
_nativeImageAttributes = handle;
}
/// <summary>
/// Initializes a new instance of the <see cref='ImageAttributes'/> class.
/// </summary>
public ImageAttributes()
{
GpImageAttributes* newImageAttributes;
PInvoke.GdipCreateImageAttributes(&newImageAttributes).ThrowIfFailed();
SetNativeImageAttributes(newImageAttributes);
}
internal ImageAttributes(GpImageAttributes* newNativeImageAttributes)
=> SetNativeImageAttributes(newNativeImageAttributes);
/// <summary>
/// Cleans up Windows resources for this <see cref='ImageAttributes'/>.
/// </summary>
public void Dispose()
{
if (_nativeImageAttributes is null)
{
return;
}
Status status = !Gdip.Initialized ? Status.Ok : PInvoke.GdipDisposeImageAttributes(_nativeImageAttributes);
_nativeImageAttributes = null;
Debug.Assert(status == Status.Ok, $"GDI+ returned an error status: {status}");
GC.SuppressFinalize(this);
}
/// <summary>
/// Cleans up Windows resources for this <see cref='ImageAttributes'/>.
/// </summary>
~ImageAttributes() => Dispose();
/// <summary>
/// Creates an exact copy of this <see cref='ImageAttributes'/>.
/// </summary>
public object Clone()
{
GpImageAttributes* clone;
PInvoke.GdipCloneImageAttributes(_nativeImageAttributes, &clone).ThrowIfFailed();
GC.KeepAlive(this);
return new ImageAttributes(clone);
}
/// <summary>
/// Sets the 5 X 5 color adjust matrix to the specified <see cref='Matrix'/>.
/// </summary>
public void SetColorMatrix(ColorMatrix newColorMatrix) =>
SetColorMatrix(newColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
/// <summary>
/// Sets the 5 X 5 color adjust matrix to the specified 'Matrix' with the specified 'ColorMatrixFlags'.
/// </summary>
public void SetColorMatrix(ColorMatrix newColorMatrix, ColorMatrixFlag flags) =>
SetColorMatrix(newColorMatrix, flags, ColorAdjustType.Default);
/// <summary>
/// Sets the 5 X 5 color adjust matrix to the specified 'Matrix' with the specified 'ColorMatrixFlags'.
/// </summary>
public void SetColorMatrix(ColorMatrix newColorMatrix, ColorMatrixFlag mode, ColorAdjustType type) =>
SetColorMatrices(newColorMatrix, null, mode, type);
/// <summary>
/// Clears the color adjust matrix to all zeroes.
/// </summary>
public void ClearColorMatrix() => ClearColorMatrix(ColorAdjustType.Default);
/// <summary>
/// Clears the color adjust matrix.
/// </summary>
public void ClearColorMatrix(ColorAdjustType type)
{
PInvoke.GdipSetImageAttributesColorMatrix(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
false,
null,
null,
(ColorMatrixFlags)ColorMatrixFlag.Default).ThrowIfFailed();
GC.KeepAlive(this);
}
/// <summary>
/// Sets a color adjust matrix for image colors and a separate gray scale adjust matrix for gray scale values.
/// </summary>
public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix? grayMatrix) =>
SetColorMatrices(newColorMatrix, grayMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix? grayMatrix, ColorMatrixFlag flags) =>
SetColorMatrices(newColorMatrix, grayMatrix, flags, ColorAdjustType.Default);
public void SetColorMatrices(
ColorMatrix newColorMatrix,
ColorMatrix? grayMatrix,
ColorMatrixFlag mode,
ColorAdjustType type)
{
ArgumentNullException.ThrowIfNull(newColorMatrix);
if (grayMatrix is not null)
{
fixed (void* cm = &newColorMatrix.GetPinnableReference())
fixed (void* gm = &grayMatrix.GetPinnableReference())
{
PInvoke.GdipSetImageAttributesColorMatrix(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: true,
(GdiPlus.ColorMatrix*)cm,
(GdiPlus.ColorMatrix*)gm,
(ColorMatrixFlags)mode).ThrowIfFailed();
}
}
else
{
fixed (void* cm = &newColorMatrix.GetPinnableReference())
{
PInvoke.GdipSetImageAttributesColorMatrix(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: true,
(GdiPlus.ColorMatrix*)cm,
null,
(ColorMatrixFlags)mode).ThrowIfFailed();
}
}
GC.KeepAlive(this);
}
public void SetThreshold(float threshold) => SetThreshold(threshold, ColorAdjustType.Default);
public void SetThreshold(float threshold, ColorAdjustType type) => SetThreshold(threshold, type, enableFlag: true);
public void ClearThreshold() => ClearThreshold(ColorAdjustType.Default);
public void ClearThreshold(ColorAdjustType type) => SetThreshold(0.0f, type, enableFlag: false);
private void SetThreshold(float threshold, ColorAdjustType type, bool enableFlag)
{
PInvoke.GdipSetImageAttributesThreshold(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag,
threshold).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetGamma(float gamma) => SetGamma(gamma, ColorAdjustType.Default);
public void SetGamma(float gamma, ColorAdjustType type) => SetGamma(gamma, type, enableFlag: true);
public void ClearGamma() => ClearGamma(ColorAdjustType.Default);
public void ClearGamma(ColorAdjustType type) => SetGamma(0.0f, type, enableFlag: false);
private void SetGamma(float gamma, ColorAdjustType type, bool enableFlag)
{
PInvoke.GdipSetImageAttributesGamma(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag,
gamma).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetNoOp() => SetNoOp(ColorAdjustType.Default);
public void SetNoOp(ColorAdjustType type) => SetNoOp(type, enableFlag: true);
public void ClearNoOp() => ClearNoOp(ColorAdjustType.Default);
public void ClearNoOp(ColorAdjustType type) => SetNoOp(type, enableFlag: false);
private void SetNoOp(ColorAdjustType type, bool enableFlag)
{
PInvoke.GdipSetImageAttributesNoOp(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetColorKey(Color colorLow, Color colorHigh) =>
SetColorKey(colorLow, colorHigh, ColorAdjustType.Default);
public void SetColorKey(Color colorLow, Color colorHigh, ColorAdjustType type) =>
SetColorKey(colorLow, colorHigh, type, enableFlag: true);
public void ClearColorKey() => ClearColorKey(ColorAdjustType.Default);
public void ClearColorKey(ColorAdjustType type) => SetColorKey(Color.Empty, Color.Empty, type, enableFlag: false);
private void SetColorKey(Color colorLow, Color colorHigh, ColorAdjustType type, bool enableFlag)
{
PInvoke.GdipSetImageAttributesColorKeys(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag,
(uint)colorLow.ToArgb(),
(uint)colorHigh.ToArgb()).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetOutputChannel(ColorChannelFlag flags) => SetOutputChannel(flags, ColorAdjustType.Default);
public void SetOutputChannel(ColorChannelFlag flags, ColorAdjustType type) =>
SetOutputChannel(type, flags, enableFlag: true);
public void ClearOutputChannel() => ClearOutputChannel(ColorAdjustType.Default);
public void ClearOutputChannel(ColorAdjustType type) =>
SetOutputChannel(type, ColorChannelFlag.ColorChannelLast, enableFlag: false);
private void SetOutputChannel(ColorAdjustType type, ColorChannelFlag flags, bool enableFlag)
{
PInvoke.GdipSetImageAttributesOutputChannel(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag,
(ColorChannelFlags)flags).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetOutputChannelColorProfile(string colorProfileFilename) =>
SetOutputChannelColorProfile(colorProfileFilename, ColorAdjustType.Default);
public void SetOutputChannelColorProfile(string colorProfileFilename, ColorAdjustType type)
{
// Called in order to emulate exception behavior from .NET Framework related to invalid file paths.
Path.GetFullPath(colorProfileFilename);
fixed (char* n = colorProfileFilename)
{
PInvoke.GdipSetImageAttributesOutputChannelColorProfile(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: true,
n).ThrowIfFailed();
}
GC.KeepAlive(this);
}
public void ClearOutputChannelColorProfile() => ClearOutputChannel(ColorAdjustType.Default);
public void ClearOutputChannelColorProfile(ColorAdjustType type)
{
PInvoke.GdipSetImageAttributesOutputChannel(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
false,
ColorChannelFlags.ColorChannelFlagsLast).ThrowIfFailed();
GC.KeepAlive(this);
}
/// <inheritdoc cref="SetRemapTable(ColorMap[], ColorAdjustType)"/>
public void SetRemapTable(params ColorMap[] map) => SetRemapTable(map, ColorAdjustType.Default);
/// <summary>
/// Sets the default color-remap table.
/// </summary>
/// <inheritdoc cref="SetRemapTable(ColorAdjustType, ReadOnlySpan{ColorMap})"/>
#if NET9_0_OR_GREATER
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public void SetRemapTable(ColorMap[] map, ColorAdjustType type)
{
ArgumentNullException.ThrowIfNull(map);
SetRemapTable(type, map);
}
#if NET9_0_OR_GREATER
/// <inheritdoc cref="SetRemapTable(ColorMap[], ColorAdjustType)"/>
public void SetRemapTable(ReadOnlySpan<ColorMap> map) => SetRemapTable(ColorAdjustType.Default, map);
/// <inheritdoc cref="SetRemapTable(ColorMap[], ColorAdjustType)"/>
public void SetRemapTable(ReadOnlySpan<(Color OldColor, Color NewColor)> map) => SetRemapTable(ColorAdjustType.Default, map);
#endif
/// <summary>
/// Sets the color-remap table for a specified category.
/// </summary>
/// <param name="type">
/// An element of <see cref="ColorAdjustType"/> that specifies the category for which the color-remap table is set.
/// </param>
/// <param name="map">
/// A series of color pairs mapping an existing color to a new color.
/// </param>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void SetRemapTable(ColorAdjustType type, ReadOnlySpan<ColorMap> map)
{
// Color is being generated incorrectly so we can't use GdiPlus.ColorMap directly.
// https://github.com/microsoft/CsWin32/issues/1121
StackBuffer stackBuffer = default;
using BufferScope<(ARGB, ARGB)> buffer = new(stackBuffer, map.Length);
for (int i = 0; i < map.Length; i++)
{
buffer[i] = (map[i].OldColor, map[i].NewColor);
}
fixed (void* m = buffer)
{
PInvoke.GdipSetImageAttributesRemapTable(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: true,
(uint)map.Length,
(GdiPlus.ColorMap*)m).ThrowIfFailed();
}
GC.KeepAlive(this);
}
#if NET9_0_OR_GREATER
/// <inheritdoc cref="SetRemapTable(ColorAdjustType, ReadOnlySpan{ColorMap})"/>
public void SetRemapTable(ColorAdjustType type, ReadOnlySpan<(Color OldColor, Color NewColor)> map)
{
StackBuffer stackBuffer = default;
using BufferScope<(ARGB, ARGB)> buffer = new(stackBuffer, map.Length);
for (int i = 0; i < map.Length; i++)
{
buffer[i] = (map[i].OldColor, map[i].NewColor);
}
fixed (void* m = buffer)
{
PInvoke.GdipSetImageAttributesRemapTable(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: true,
(uint)map.Length,
(GdiPlus.ColorMap*)m).ThrowIfFailed();
}
GC.KeepAlive(this);
}
#endif
[InlineArray(ColorMapStackSpace)]
private struct StackBuffer
{
internal (ARGB, ARGB) _element0;
}
public void ClearRemapTable() => ClearRemapTable(ColorAdjustType.Default);
public void ClearRemapTable(ColorAdjustType type)
{
PInvoke.GdipSetImageAttributesRemapTable(
_nativeImageAttributes,
(GdiPlus.ColorAdjustType)type,
enableFlag: false,
0,
null).ThrowIfFailed();
GC.KeepAlive(this);
}
public void SetBrushRemapTable(params ColorMap[] map) => SetRemapTable(map, ColorAdjustType.Brush);
#if NET9_0_OR_GREATER
public void SetBrushRemapTable(ReadOnlySpan<ColorMap> map) => SetRemapTable(ColorAdjustType.Brush, map);
public void SetBrushRemapTable(ReadOnlySpan<(Color OldColor, Color NewColor)> map) => SetRemapTable(ColorAdjustType.Brush, map);
#endif
public void ClearBrushRemapTable() => ClearRemapTable(ColorAdjustType.Brush);
public void SetWrapMode(Drawing2D.WrapMode mode) => SetWrapMode(mode, default, clamp: false);
public void SetWrapMode(Drawing2D.WrapMode mode, Color color) => SetWrapMode(mode, color, clamp: false);
public void SetWrapMode(Drawing2D.WrapMode mode, Color color, bool clamp)
{
PInvoke.GdipSetImageAttributesWrapMode(
_nativeImageAttributes,
(WrapMode)mode,
(uint)color.ToArgb(),
clamp).ThrowIfFailed();
GC.KeepAlive(this);
}
public void GetAdjustedPalette(ColorPalette palette, ColorAdjustType type)
{
using var buffer = palette.ConvertToBuffer();
fixed (void* p = buffer)
{
PInvoke.GdipGetImageAttributesAdjustedPalette(
_nativeImageAttributes,
(GdiPlus.ColorPalette*)p,
(GdiPlus.ColorAdjustType)type).ThrowIfFailed();
}
GC.KeepAlive(this);
}
}