-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
MagickSettings.cs
758 lines (651 loc) · 22 KB
/
MagickSettings.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
#if Q8
using QuantumType = System.Byte;
#elif Q16
using QuantumType = System.UInt16;
#elif Q16HDRI
using QuantumType = System.Single;
#else
#error Not implemented!
#endif
namespace ImageMagick;
/// <summary>
/// Class that contains various settings.
/// </summary>
public partial class MagickSettings : IMagickSettings<QuantumType>
{
private readonly Dictionary<string, string?> _options = new Dictionary<string, string?>();
private string? _font;
private double _fontPointsize;
internal MagickSettings()
{
using var instance = new NativeMagickSettings();
AntiAlias = instance.AntiAlias;
BackgroundColor = instance.BackgroundColor;
ColorSpace = instance.ColorSpace;
ColorType = instance.ColorType;
Compression = instance.Compression;
Debug = instance.Debug;
Density = CreateDensity(instance.Density);
Depth = instance.Depth;
Endian = instance.Endian;
Extract = MagickGeometry.FromString(instance.Extract);
_font = instance.Font;
_fontPointsize = instance.FontPointsize;
Format = EnumHelper.Parse(instance.Format, MagickFormat.Unknown);
Interlace = instance.Interlace;
Monochrome = instance.Monochrome;
Verbose = instance.Verbose;
Drawing = new DrawingSettings();
}
internal event EventHandler<ArtifactEventArgs>? Artifact;
/// <summary>
/// Gets or sets the affine to use when annotating with text or drawing.
/// </summary>
public IDrawableAffine? Affine
{
get => Drawing.Affine;
set => Drawing.Affine = value;
}
/// <summary>
/// Gets or sets a value indicating whether anti-aliasing should be enabled (default true).
/// </summary>
public bool AntiAlias { get; set; }
/// <summary>
/// Gets or sets the background color.
/// </summary>
public IMagickColor<QuantumType>? BackgroundColor { get; set; }
/// <summary>
/// Gets or sets the border color.
/// </summary>
public IMagickColor<QuantumType>? BorderColor
{
get => Drawing.BorderColor;
set => Drawing.BorderColor = value;
}
/// <summary>
/// Gets or sets the color space.
/// </summary>
public ColorSpace ColorSpace { get; set; }
/// <summary>
/// Gets or sets the color type of the image.
/// </summary>
public ColorType ColorType { get; set; }
/// <summary>
/// Gets or sets the compression method to use.
/// </summary>
public CompressionMethod Compression { get; set; }
/// <summary>
/// Gets or sets a value indicating whether printing of debug messages from ImageMagick is enabled when a debugger is attached.
/// </summary>
public bool Debug { get; set; }
/// <summary>
/// Gets or sets the vertical and horizontal resolution in pixels.
/// </summary>
public Density? Density { get; set; }
/// <summary>
/// Gets or sets the depth (bits allocated to red/green/blue components).
/// </summary>
public int Depth { get; set; }
/// <summary>
/// Gets or sets the endianness (little like Intel or big like SPARC) for image formats which support
/// endian-specific options.
/// </summary>
public Endian Endian { get; set; }
/// <summary>
/// Gets or sets the fill color.
/// </summary>
public IMagickColor<QuantumType>? FillColor
{
get => Drawing.FillColor;
set
{
SetOptionAndArtifact("fill", value?.ToString());
Drawing.FillColor = value;
}
}
/// <summary>
/// Gets or sets the fill pattern.
/// </summary>
public IMagickImage<QuantumType>? FillPattern
{
get => Drawing.FillPattern;
set => Drawing.FillPattern = value;
}
/// <summary>
/// Gets or sets the rule to use when filling drawn objects.
/// </summary>
public FillRule FillRule
{
get => Drawing.FillRule;
set => Drawing.FillRule = value;
}
/// <summary>
/// Gets or sets the text rendering font.
/// </summary>
public string? Font
{
get => _font;
set
{
_font = value;
Drawing.Font = value;
}
}
/// <summary>
/// Gets or sets the text font family.
/// </summary>
public string? FontFamily
{
get => GetOption("family");
set
{
SetOptionAndArtifact("family", value);
Drawing.FontFamily = value;
}
}
/// <summary>
/// Gets or sets the font point size.
/// </summary>
public double FontPointsize
{
get => _fontPointsize;
set
{
_fontPointsize = value;
Drawing.FontPointsize = value;
}
}
/// <summary>
/// Gets or sets the font style.
/// </summary>
public FontStyleType FontStyle
{
get => EnumHelper.Parse(GetOption("style"), FontStyleType.Undefined);
set
{
SetOptionAndArtifact("style", value);
Drawing.FontStyle = value;
}
}
/// <summary>
/// Gets or sets the font weight.
/// </summary>
public FontWeight FontWeight
{
get
{
var weight = GetOption("weight");
if (string.IsNullOrEmpty(weight))
return FontWeight.Undefined;
if (!int.TryParse(weight, NumberStyles.Number, CultureInfo.InvariantCulture, out var fontweight))
return FontWeight.Undefined;
return EnumHelper.Parse(fontweight, FontWeight.Undefined);
}
set
{
SetOptionAndArtifact("weight", ((int)value).ToString(CultureInfo.InvariantCulture));
Drawing.FontWeight = value;
}
}
/// <summary>
/// Gets or sets the the format of the image.
/// </summary>
public MagickFormat Format { get; set; }
/// <summary>
/// Gets or sets the interlace method.
/// </summary>
public Interlace Interlace { get; set; }
/// <summary>
/// Gets or sets the preferred size and location of an image canvas.
/// </summary>
public IMagickGeometry? Page { get; set; }
/// <summary>
/// Gets or sets a value indicating whether stroke anti-aliasing is enabled or disabled.
/// </summary>
public bool StrokeAntiAlias
{
get => Drawing.StrokeAntiAlias;
set => Drawing.StrokeAntiAlias = value;
}
/// <summary>
/// Gets or sets the color to use when drawing object outlines.
/// </summary>
public IMagickColor<QuantumType>? StrokeColor
{
get => Drawing.StrokeColor;
set
{
SetOptionAndArtifact("stroke", value?.ToString());
Drawing.StrokeColor = value;
}
}
/// <summary>
/// Gets or sets the pattern of dashes and gaps used to stroke paths. This represents a
/// zero-terminated array of numbers that specify the lengths of alternating dashes and gaps
/// in pixels. If a zero value is not found it will be added. If an odd number of values is
/// provided, then the list of values is repeated to yield an even number of values.
/// </summary>
public IEnumerable<double>? StrokeDashArray
{
get => Drawing.StrokeDashArray;
set => Drawing.StrokeDashArray = value;
}
/// <summary>
/// Gets or sets the distance into the dash pattern to start the dash (default 0) while
/// drawing using a dash pattern,.
/// </summary>
public double StrokeDashOffset
{
get => Drawing.StrokeDashOffset;
set => Drawing.StrokeDashOffset = value;
}
/// <summary>
/// Gets or sets the shape to be used at the end of open subpaths when they are stroked.
/// </summary>
public LineCap StrokeLineCap
{
get => Drawing.StrokeLineCap;
set => Drawing.StrokeLineCap = value;
}
/// <summary>
/// Gets or sets the shape to be used at the corners of paths (or other vector shapes) when they
/// are stroked.
/// </summary>
public LineJoin StrokeLineJoin
{
get => Drawing.StrokeLineJoin;
set => Drawing.StrokeLineJoin = value;
}
/// <summary>
/// Gets or sets the miter limit. When two line segments meet at a sharp angle and miter joins have
/// been specified for 'lineJoin', it is possible for the miter to extend far beyond the thickness
/// of the line stroking the path. The miterLimit' imposes a limit on the ratio of the miter
/// length to the 'lineWidth'. The default value is 4.
/// </summary>
public int StrokeMiterLimit
{
get => Drawing.StrokeMiterLimit;
set => Drawing.StrokeMiterLimit = value;
}
/// <summary>
/// Gets or sets the pattern image to use while stroking object outlines.
/// </summary>
public IMagickImage<QuantumType>? StrokePattern
{
get => Drawing.StrokePattern;
set => Drawing.StrokePattern = value;
}
/// <summary>
/// Gets or sets the stroke width for drawing lines, circles, ellipses, etc.
/// </summary>
public double StrokeWidth
{
get => Drawing.StrokeWidth;
set
{
SetOptionAndArtifact("strokewidth", value);
Drawing.StrokeWidth = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether Postscript and TrueType fonts should be anti-aliased (default true).
/// </summary>
public bool TextAntiAlias
{
get => Drawing.TextAntiAlias;
set => Drawing.TextAntiAlias = value;
}
/// <summary>
/// Gets or sets text direction (right-to-left or left-to-right).
/// </summary>
public TextDirection TextDirection
{
get => Drawing.TextDirection;
set => Drawing.TextDirection = value;
}
/// <summary>
/// Gets or sets the text annotation encoding (e.g. "UTF-16").
/// </summary>
public Encoding? TextEncoding
{
get => Drawing.TextEncoding;
set => Drawing.TextEncoding = value;
}
/// <summary>
/// Gets or sets the text annotation gravity.
/// </summary>
public Gravity TextGravity
{
get => Drawing.TextGravity;
set
{
SetOptionAndArtifact("gravity", value);
Drawing.TextGravity = value;
}
}
/// <summary>
/// Gets or sets the text inter-line spacing.
/// </summary>
public double TextInterlineSpacing
{
get => Drawing.TextInterlineSpacing;
set
{
SetOptionAndArtifact("interline-spacing", value);
Drawing.TextInterlineSpacing = value;
}
}
/// <summary>
/// Gets or sets the text inter-word spacing.
/// </summary>
public double TextInterwordSpacing
{
get => Drawing.TextInterwordSpacing;
set
{
SetOptionAndArtifact("interword-spacing", value);
Drawing.TextInterwordSpacing = value;
}
}
/// <summary>
/// Gets or sets the text inter-character kerning.
/// </summary>
public double TextKerning
{
get => Drawing.TextKerning;
set
{
SetOptionAndArtifact("kerning", value);
Drawing.TextKerning = value;
}
}
/// <summary>
/// Gets or sets the text undercolor box.
/// </summary>
public IMagickColor<QuantumType>? TextUnderColor
{
get => Drawing.TextUnderColor;
set
{
SetOptionAndArtifact("undercolor", value?.ToString());
Drawing.TextUnderColor = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether verbose output os turned on or off.
/// </summary>
public bool Verbose { get; set; }
internal DrawingSettings Drawing { get; private set; }
internal double ColorFuzz { get; set; }
internal string? FileName { get; set; }
internal bool Ping { get; set; }
internal int Quality { get; set; }
/// <summary>
/// Gets or sets the specified area to extract from the image.
/// </summary>
protected IMagickGeometry? Extract { get; set; }
/// <summary>
/// Gets or sets the number of scenes.
/// </summary>
protected int NumberScenes { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a monochrome reader should be used.
/// </summary>
protected bool Monochrome { get; set; }
/// <summary>
/// Gets or sets the size of the image.
/// </summary>
protected string? Size { get; set; }
/// <summary>
/// Gets or sets the active scene.
/// </summary>
protected int Scene { get; set; }
/// <summary>
/// Gets or sets scenes of the image.
/// </summary>
protected string? Scenes { get; set; }
/// <summary>
/// Returns the value of a format-specific option.
/// </summary>
/// <param name="format">The format to get the option for.</param>
/// <param name="name">The name of the option.</param>
/// <returns>The value of a format-specific option.</returns>
public string? GetDefine(MagickFormat format, string name)
{
Throw.IfNullOrEmpty(nameof(name), name);
return GetOption(ParseDefine(format, name));
}
/// <summary>
/// Returns the value of a format-specific option.
/// </summary>
/// <param name="name">The name of the option.</param>
/// <returns>The value of a format-specific option.</returns>
public string? GetDefine(string name)
{
Throw.IfNullOrEmpty(nameof(name), name);
return GetOption(name);
}
/// <summary>
/// Removes the define with the specified name.
/// </summary>
/// <param name="format">The format to set the define for.</param>
/// <param name="name">The name of the define.</param>
public void RemoveDefine(MagickFormat format, string name)
{
Throw.IfNullOrEmpty(nameof(name), name);
var key = ParseDefine(format, name);
if (_options.ContainsKey(key))
_options.Remove(key);
}
/// <summary>
/// Removes the define with the specified name.
/// </summary>
/// <param name="name">The name of the define.</param>
public void RemoveDefine(string name)
{
Throw.IfNullOrEmpty(nameof(name), name);
if (_options.ContainsKey(name))
_options.Remove(name);
}
/// <summary>
/// Sets a format-specific option.
/// </summary>
/// <param name="format">The format to set the define for.</param>
/// <param name="name">The name of the define.</param>
/// <param name="flag">The value of the define.</param>
public void SetDefine(MagickFormat format, string name, bool flag)
=> SetDefine(format, name, flag ? "true" : "false");
/// <summary>
/// Sets a format-specific option.
/// </summary>
/// <param name="format">The format to set the define for.</param>
/// <param name="name">The name of the define.</param>
/// <param name="value">The value of the define.</param>
public void SetDefine(MagickFormat format, string name, int value)
=> SetDefine(format, name, value.ToString(CultureInfo.InvariantCulture));
/// <summary>
/// Sets a format-specific option.
/// </summary>
/// <param name="format">The format to set the option for.</param>
/// <param name="name">The name of the option.</param>
/// <param name="value">The value of the option.</param>
public void SetDefine(MagickFormat format, string name, string value)
{
Throw.IfNullOrEmpty(nameof(name), name);
Throw.IfNull(nameof(value), value);
SetOption(ParseDefine(format, name), value);
}
/// <summary>
/// Sets a format-specific option.
/// </summary>
/// <param name="name">The name of the option.</param>
/// <param name="value">The value of the option.</param>
public void SetDefine(string name, string value)
{
Throw.IfNullOrEmpty(nameof(name), name);
Throw.IfNull(nameof(value), value);
SetOption(name, value);
}
/// <summary>
/// Sets format-specific options with the specified defines.
/// </summary>
/// <param name="defines">The defines to set.</param>
public void SetDefines(IDefines defines)
{
Throw.IfNull(nameof(defines), defines);
foreach (var define in defines.Defines)
{
if (define is not null)
SetDefine(define.Format, define.Name, define.Value);
}
}
internal MagickSettings Clone()
{
var clone = new MagickSettings();
clone.CopyFrom(this);
return clone;
}
/// <summary>
/// Copies the settings from the specified <see cref="MagickSettings"/>.
/// </summary>
/// <param name="settings">The settings to copy the data from.</param>
protected void CopyFrom(MagickSettings settings)
{
if (settings is null)
return;
AntiAlias = settings.AntiAlias;
BackgroundColor = MagickColor.Clone(settings.BackgroundColor);
ColorSpace = settings.ColorSpace;
ColorType = settings.ColorType;
Compression = settings.Compression;
Debug = settings.Debug;
Density = CloneDensity(settings.Density);
Depth = settings.Depth;
Endian = settings.Endian;
Extract = MagickGeometry.Clone(settings.Extract);
_font = settings._font;
_fontPointsize = settings._fontPointsize;
Format = settings.Format;
Monochrome = settings.Monochrome;
Page = MagickGeometry.Clone(settings.Page);
Verbose = settings.Verbose;
ColorFuzz = settings.ColorFuzz;
Interlace = settings.Interlace;
Ping = settings.Ping;
Quality = settings.Quality;
Size = settings.Size;
foreach (var key in settings._options.Keys)
_options[key] = settings._options[key];
Drawing = settings.Drawing.Clone();
}
/// <summary>
/// Gets an image option.
/// </summary>
/// <param name="key">The key of the option.</param>
/// <returns>The value of the option.</returns>
protected string? GetOption(string key)
{
Throw.IfNullOrEmpty(nameof(key), key);
if (_options.TryGetValue(key, out var result))
return result;
return null;
}
/// <summary>
/// Sets an image option.
/// </summary>
/// <param name="key">The key of the option.</param>
/// <param name="value">The value of the option.</param>
protected void SetOption(string key, string? value)
=> _options[key] = value;
private static string ParseDefine(MagickFormat format, string name)
{
if (format == MagickFormat.Unknown)
return name;
var module = GetModule(format);
return Enum.GetName(module.GetType(), module) + ":" + name;
}
private static MagickFormat GetModule(MagickFormat format)
{
var formatInfo = MagickFormatInfo.Create(format);
if (formatInfo is null)
return format;
return formatInfo.ModuleFormat;
}
private static Density? CreateDensity(string? value)
{
if (value is null || value.Length == 0)
return null;
return new Density(value);
}
private static Density? CloneDensity(Density? density)
{
if (density is null)
return null;
return new Density(density.X, density.Y, density.Units);
}
private static INativeInstance CreateNativeInstance(IMagickSettings<QuantumType> instance)
{
var settings = (MagickSettings)instance;
var format = settings.GetFormat();
var fileName = settings.FileName;
if (!string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(format))
fileName = format + ":" + fileName;
var result = new NativeMagickSettings
{
AntiAlias = settings.AntiAlias,
BackgroundColor = settings.BackgroundColor,
ColorSpace = settings.ColorSpace,
ColorType = settings.ColorType,
Compression = settings.Compression,
Debug = settings.Debug,
Density = settings.Density?.ToString(DensityUnit.Undefined),
Depth = settings.Depth,
Endian = settings.Endian,
Extract = settings.Extract?.ToString(),
Font = settings._font,
FontPointsize = settings._fontPointsize,
Format = format,
Interlace = settings.Interlace,
Monochrome = settings.Monochrome,
Verbose = settings.Verbose,
};
result.SetColorFuzz(settings.ColorFuzz);
result.SetFileName(fileName);
result.SetNumberScenes(settings.NumberScenes);
result.SetPage(settings.Page?.ToString());
result.SetPing(settings.Ping);
result.SetQuality(settings.Quality);
result.SetScene(settings.Scene);
result.SetScenes(settings.Scenes);
result.SetSize(settings.Size);
foreach (var key in settings._options.Keys)
result.SetOption(key, settings._options[key]);
return result;
}
private string? GetFormat()
=> Format switch
{
MagickFormat.Unknown => null,
MagickFormat.ThreeFr => "3FR",
MagickFormat.ThreeG2 => "3G2",
MagickFormat.ThreeGp => "3GP",
MagickFormat.RadialGradient => "RADIAL-GRADIENT",
MagickFormat.SparseColor => "SPARSE-COLOR",
_ => Enum.GetName(Format.GetType(), Format).ToUpperInvariant(),
};
private void SetOptionAndArtifact(string key, double value)
=> SetOptionAndArtifact(key, value.ToString(CultureInfo.InvariantCulture));
private void SetOptionAndArtifact(string key, Enum value)
=> SetOptionAndArtifact(key, Enum.GetName(value.GetType(), value).ToLowerInvariant());
private void SetOptionAndArtifact(string key, string? value)
{
SetOption(key, value);
Artifact?.Invoke(this, new ArtifactEventArgs(key, value));
}
}