forked from dlemstra/Magick.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagickNET.cs
More file actions
549 lines (466 loc) · 18.4 KB
/
Copy pathMagickNET.cs
File metadata and controls
549 lines (466 loc) · 18.4 KB
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
// 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.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using ImageMagick.Configuration;
namespace ImageMagick;
/// <summary>
/// Class that can be used to initialize Magick.NET.
/// </summary>
public partial class MagickNET : IMagickNET
{
private static LogDelegate? _nativeLog;
private static EventHandler<LogEventArgs>? _log;
private static LogEvents _logEvents = LogEvents.None;
/// <summary>
/// Event that will be raised when something is logged by ImageMagick.
/// </summary>
public static event EventHandler<LogEventArgs> Log
{
add
{
if (_log is null)
{
_nativeLog = new LogDelegate(OnLog);
NativeMagickNET.SetLogDelegate(_nativeLog);
SetLogEvents();
}
_log += value;
}
remove
{
_log -= value;
if (_log is null)
{
NativeMagickNET.SetLogDelegate(null);
NativeMagickNET.SetLogEvents("None");
_nativeLog = null;
}
}
}
/// <summary>
/// Event that will be raised when something is logged by ImageMagick.
/// </summary>
event EventHandler<LogEventArgs> IMagickNET.Log
{
add => Log += value;
remove => Log -= value;
}
/// <summary>
/// Gets the ImageMagick delegate libraries.
/// </summary>
public static string Delegates
=> NativeMagickNET.Delegates;
/// <summary>
/// Gets the ImageMagick features.
/// </summary>
public static string Features
=> NativeMagickNET.Features;
/// <summary>
/// Gets the information about the supported formats.
/// </summary>
public static IReadOnlyCollection<IMagickFormatInfo> SupportedFormats
=> MagickFormatInfo.All;
/// <summary>
/// Gets the font families that are known by ImageMagick.
/// </summary>
public static IReadOnlyCollection<string> FontFamilies
{
get
{
var result = new List<string>();
var list = IntPtr.Zero;
var length = (UIntPtr)0;
try
{
list = NativeMagickNET.GetFonts(out length);
for (var i = 0; i < (int)length; i++)
{
var fontFamily = NativeMagickNET.GetFontFamily(list, i);
if (fontFamily is not null && fontFamily.Length > 0 && !result.Contains(fontFamily))
result.Add(fontFamily);
}
}
finally
{
if (list != IntPtr.Zero)
NativeMagickNET.DisposeFonts(list);
}
return result;
}
}
/// <summary>
/// Gets the font names that are known by ImageMagick.
/// </summary>
public static IReadOnlyCollection<string> FontNames
{
get
{
var result = new List<string>();
var list = IntPtr.Zero;
var length = (UIntPtr)0;
try
{
list = NativeMagickNET.GetFonts(out length);
for (var i = 0; i < (int)length; i++)
{
var fontName = NativeMagickNET.GetFontName(list, i);
if (fontName is not null && fontName.Length > 0)
result.Add(fontName);
}
}
finally
{
if (list != IntPtr.Zero)
NativeMagickNET.DisposeFonts(list);
}
return result;
}
}
/// <summary>
/// Gets the version of ImageMagick.
/// </summary>
public static string ImageMagickVersion
=> NativeMagickNET.ImageMagickVersion;
/// <summary>
/// Gets the version of Magick.NET.
/// </summary>
public static string Version
{
get
{
var title = TypeHelper.GetCustomAttribute<AssemblyTitleAttribute>(typeof(MagickNET));
var version = TypeHelper.GetCustomAttribute<AssemblyFileVersionAttribute>(typeof(MagickNET));
return title.Title + " " + version.Version;
}
}
/// <summary>
/// Gets the ImageMagick delegate libraries.
/// </summary>
string IMagickNET.Delegates
=> Delegates;
/// <summary>
/// Gets the ImageMagick features.
/// </summary>
string IMagickNET.Features
=> Features;
/// <summary>
/// Gets the information about the supported formats.
/// </summary>
IReadOnlyCollection<IMagickFormatInfo> IMagickNET.SupportedFormats
=> SupportedFormats;
/// <summary>
/// Gets the font families that are known by ImageMagick.
/// </summary>
IReadOnlyCollection<string> IMagickNET.FontFamilies
=> FontFamilies;
/// <summary>
/// Gets the font names that are known by ImageMagick.
/// </summary>
IReadOnlyCollection<string> IMagickNET.FontNames
=> FontNames;
/// <summary>
/// Gets the version of ImageMagick.
/// </summary>
string IMagickNET.ImageMagickVersion
=> ImageMagickVersion;
/// <summary>
/// Gets the version of Magick.NET.
/// </summary>
string IMagickNET.Version
=> Version;
internal static string TemporaryDirectory { get; private set; } = Path.GetTempPath();
/// <summary>
/// Gets the environment variable with the specified name.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <returns>The environment variable with the specified name.</returns>
public static string? GetEnvironmentVariable(string name)
{
Throw.IfNullOrEmpty(nameof(name), name);
return Environment.GetEnv(name);
}
/// <summary>
/// Initializes ImageMagick.
/// </summary>
public static void Initialize()
=> Environment.Initialize();
/// <summary>
/// Initializes ImageMagick with the xml files that are located in the specified path.
/// </summary>
/// <param name="path">The path that contains the ImageMagick xml files.</param>
public static void Initialize(string path)
{
var newPath = FileHelper.GetFullPath(path);
CheckImageMagickFiles(newPath);
Environment.SetEnv("MAGICK_CONFIGURE_PATH", path);
}
/// <summary>
/// Initializes ImageMagick with the specified configuration files and returns the path to the
/// temporary directory where the xml files were saved.
/// </summary>
/// <param name="configFiles">The configuration files ot initialize ImageMagick with.</param>
/// <returns>The path of the folder that was created and contains the configuration files.</returns>
public static string Initialize(IConfigurationFiles configFiles)
{
Throw.IfNull(nameof(configFiles), configFiles);
var path = Path.Combine(TemporaryDirectory, Guid.NewGuid().ToString());
Directory.CreateDirectory(path);
InitializeConfiguration(configFiles, path);
return path;
}
/// <summary>
/// Initializes ImageMagick with the specified configuration files in the specified the path.
/// </summary>
/// <param name="configFiles">The configuration files ot initialize ImageMagick with.</param>
/// <param name="path">The directory to save the configuration files in.</param>
public static void Initialize(IConfigurationFiles configFiles, string path)
{
Throw.IfNull(nameof(configFiles), configFiles);
var newPath = FileHelper.GetFullPath(path);
InitializeConfiguration(configFiles, newPath);
}
/// <summary>
/// Resets the pseudo-random number generator secret key.
/// </summary>
public static void ResetRandomSeed()
=> NativeMagickNET.SetRandomSeed(-1);
/// <summary>
/// Set the path to the default font file.
/// </summary>
/// <param name="file">The file to use at the default font file.</param>
public static void SetDefaultFontFile(FileInfo file)
{
Throw.IfNull(nameof(file), file);
SetDefaultFontFile(file.FullName);
}
/// <summary>
/// Set the path to the default font file.
/// </summary>
/// <param name="fileName">The file name to use at the default font file.</param>
public static void SetDefaultFontFile(string fileName)
{
Throw.IfNullOrEmpty(nameof(fileName), fileName);
NativeMagickNET.SetDefaultFontFile(fileName);
}
/// <summary>
/// Set the environment variable with the specified name to the specified value.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <param name="value">The value of the environment variable.</param>
public static void SetEnvironmentVariable(string name, string value)
{
Throw.IfNullOrEmpty(nameof(name), name);
Environment.SetEnv(name, value);
}
/// <summary>
/// Sets the directory that contains the FontConfig configuration files.
/// </summary>
/// <param name="path">The path of the FontConfig directory.</param>
public static void SetFontConfigDirectory(string path)
=> Environment.SetEnv("FONTCONFIG_PATH", FileHelper.GetFullPath(path));
/// <summary>
/// Sets the directory that contains the Ghostscript file gsdll32.dll / gsdll64.dll.
/// This method is only supported on Windows.
/// </summary>
/// <param name="path">The path of the Ghostscript directory.</param>
public static void SetGhostscriptDirectory(string path)
=> Environment.SetEnv("MAGICK_GHOSTSCRIPT_PATH", FileHelper.GetFullPath(path));
/// <summary>
/// Sets the directory that contains the Ghostscript font files.
/// This method is only supported on Windows.
/// </summary>
/// <param name="path">The path of the Ghostscript font directory.</param>
public static void SetGhostscriptFontDirectory(string path)
=> Environment.SetEnv("MAGICK_GHOSTSCRIPT_FONT_PATH", FileHelper.GetFullPath(path));
/// <summary>
/// Set the events that will be written to the log. The log will be written to the Log event
/// and the debug window in VisualStudio. To change the log settings you must use a custom
/// log.xml file.
/// </summary>
/// <param name="events">The events that will be logged.</param>
public static void SetLogEvents(LogEvents events)
{
_logEvents = events;
if (_log is not null)
SetLogEvents();
}
/// <summary>
/// Sets the directory that contains the Native library. This currently only works on Windows.
/// </summary>
/// <param name="path">The path of the directory that contains the native library.</param>
public static void SetNativeLibraryDirectory(string path)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
NativeWindowsMethods.SetDllDirectory(FileHelper.GetFullPath(path));
}
/// <summary>
/// Sets the directory that will be used when ImageMagick does not have enough memory for the
/// pixel cache.
/// </summary>
/// <param name="path">The path where temp files will be written.</param>
public static void SetTempDirectory(string path)
{
TemporaryDirectory = FileHelper.GetFullPath(path);
Environment.SetEnv("MAGICK_TEMPORARY_PATH", TemporaryDirectory);
}
/// <summary>
/// Sets the pseudo-random number generator secret key.
/// </summary>
/// <param name="seed">The secret key.</param>
public static void SetRandomSeed(int seed)
=> NativeMagickNET.SetRandomSeed(seed);
/// <summary>
/// Gets the environment variable with the specified name.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <returns>The environment variable with the specified name.</returns>
string? IMagickNET.GetEnvironmentVariable(string name)
=> GetEnvironmentVariable(name);
/// <summary>
/// Initializes ImageMagick.
/// </summary>
void IMagickNET.Initialize()
=> Initialize();
/// <summary>
/// Initializes ImageMagick with the xml files that are located in the specified path.
/// </summary>
/// <param name="path">The path that contains the ImageMagick xml files.</param>
void IMagickNET.Initialize(string path)
=> Initialize(path);
/// <summary>
/// Initializes ImageMagick with the specified configuration files and returns the path to the
/// temporary directory where the xml files were saved.
/// </summary>
/// <param name="configFiles">The configuration files ot initialize ImageMagick with.</param>
/// <returns>The path of the folder that was created and contains the configuration files.</returns>
string IMagickNET.Initialize(IConfigurationFiles configFiles)
=> Initialize(configFiles);
/// <summary>
/// Initializes ImageMagick with the specified configuration files in the specified the path.
/// </summary>
/// <param name="configFiles">The configuration files ot initialize ImageMagick with.</param>
/// <param name="path">The directory to save the configuration files in.</param>
void IMagickNET.Initialize(IConfigurationFiles configFiles, string path)
=> Initialize(configFiles, path);
/// <summary>
/// Resets the pseudo-random number generator secret key.
/// </summary>
void IMagickNET.ResetRandomSeed()
=> ResetRandomSeed();
/// <summary>
/// Set the path to the default font file.
/// </summary>
/// <param name="file">The file to use at the default font file.</param>
void IMagickNET.SetDefaultFontFile(FileInfo file)
=> SetDefaultFontFile(file);
/// <summary>
/// Set the path to the default font file.
/// </summary>
/// <param name="fileName">The file name to use at the default font file.</param>
void IMagickNET.SetDefaultFontFile(string fileName)
=> SetDefaultFontFile(fileName);
/// <summary>
/// Set the environment variable with the specified name to the specified value.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <param name="value">The value of the environment variable.</param>
void IMagickNET.SetEnvironmentVariable(string name, string value)
=> SetEnvironmentVariable(name, value);
/// <summary>
/// Sets the directory that contains the FontConfig configuration files.
/// </summary>
/// <param name="path">The path of the FontConfig directory.</param>
void IMagickNET.SetFontConfigDirectory(string path)
=> SetFontConfigDirectory(path);
/// <summary>
/// Sets the directory that contains the Ghostscript file gsdll32.dll / gsdll64.dll.
/// This method is only supported on Windows.
/// </summary>
/// <param name="path">The path of the Ghostscript directory.</param>
void IMagickNET.SetGhostscriptDirectory(string path)
=> SetGhostscriptDirectory(path);
/// <summary>
/// Sets the directory that contains the Ghostscript font files.
/// This method is only supported on Windows.
/// </summary>
/// <param name="path">The path of the Ghostscript font directory.</param>
void IMagickNET.SetGhostscriptFontDirectory(string path)
=> SetGhostscriptDirectory(path);
/// <summary>
/// Set the events that will be written to the log. The log will be written to the Log event
/// and the debug window in VisualStudio. To change the log settings you must use a custom
/// log.xml file.
/// </summary>
/// <param name="events">The events that will be logged.</param>
void IMagickNET.SetLogEvents(LogEvents events)
=> SetLogEvents(events);
/// <summary>
/// Sets the directory that contains the Native library. This currently only works on Windows.
/// </summary>
/// <param name="path">The path of the directory that contains the native library.</param>
void IMagickNET.SetNativeLibraryDirectory(string path)
=> SetNativeLibraryDirectory(path);
/// <summary>
/// Sets the directory that will be used when ImageMagick does not have enough memory for the
/// pixel cache.
/// </summary>
/// <param name="path">The path where temp files will be written.</param>
void IMagickNET.SetTempDirectory(string path)
=> SetTempDirectory(path);
/// <summary>
/// Sets the pseudo-random number generator secret key.
/// </summary>
/// <param name="seed">The secret key.</param>
void IMagickNET.SetRandomSeed(int seed)
=> SetRandomSeed(seed);
private static void CheckImageMagickFiles(string path)
{
foreach (var configurationFile in ConfigurationFiles.Default.All)
{
var fileName = Path.Combine(path, configurationFile.FileName);
Throw.IfFalse(nameof(path), File.Exists(fileName), "Unable to find file: {0}", fileName);
}
}
private static void InitializeConfiguration(IConfigurationFiles configFiles, string path)
{
foreach (var configFile in configFiles.All)
{
var outputFile = Path.Combine(path, configFile.FileName);
if (File.Exists(outputFile))
continue;
using var fileStream = File.Open(outputFile, FileMode.CreateNew);
var data = Encoding.UTF8.GetBytes(configFile.Data);
fileStream.Write(data, 0, data.Length);
}
Environment.SetEnv("MAGICK_CONFIGURE_PATH", path);
}
private static void OnLog(UIntPtr type, IntPtr text)
{
if (_log is null)
return;
var managedText = UTF8Marshaler.NativeToManaged(text);
_log(null, new LogEventArgs((LogEvents)type, managedText));
}
private static void SetLogEvents()
{
string eventFlags;
if (_logEvents == LogEvents.All)
eventFlags = "All,Trace";
else if (_logEvents == LogEvents.Detailed)
eventFlags = "All";
else
eventFlags = EnumHelper.ConvertFlags(_logEvents);
NativeMagickNET.SetLogEvents(eventFlags);
}
private static class NativeWindowsMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDllDirectory(string lpPathName);
}
}