-
-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathMagickImageCollection.cs
More file actions
1953 lines (1708 loc) · 85.3 KB
/
Copy pathMagickImageCollection.cs
File metadata and controls
1953 lines (1708 loc) · 85.3 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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#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>
/// Represents the collection of images.
/// </summary>
public sealed partial class MagickImageCollection : IMagickImageCollection<QuantumType>
{
private readonly List<IMagickImage<QuantumType>> _images;
private readonly NativeMagickImageCollection _nativeInstance;
private EventHandler<WarningEventArgs>? _warning;
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
public MagickImageCollection()
{
_images = new List<IMagickImage<QuantumType>>();
_nativeInstance = new NativeMagickImageCollection();
_nativeInstance.Warning += OnWarning;
}
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data)
: this()
=> Read(data);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data, uint offset, uint count)
: this()
=> Read(data, offset, count);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data, uint offset, uint count, MagickFormat format)
: this()
=> Read(data, offset, count, format);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data, uint offset, uint count, IMagickReadSettings<QuantumType> readSettings)
: this()
=> Read(data, offset, count, readSettings);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data, MagickFormat format)
: this()
=> Read(data, format);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(byte[] data, IMagickReadSettings<QuantumType> readSettings)
: this()
=> Read(data, readSettings);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="file">The file to read the image from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(FileInfo file)
: this()
=> Read(file);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="file">The file to read the image from.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(FileInfo file, MagickFormat format)
: this()
=> Read(file, format);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="file">The file to read the image from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(FileInfo file, IMagickReadSettings<QuantumType> readSettings)
: this()
=> Read(file, readSettings);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="images">The images to add to the collection.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(IEnumerable<IMagickImage<QuantumType>> images)
: this()
=> AddRange(images);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(Stream stream)
: this()
=> Read(stream);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(Stream stream, MagickFormat format)
: this()
=> Read(stream, format);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(Stream stream, IMagickReadSettings<QuantumType> readSettings)
: this()
=> Read(stream, readSettings);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(string fileName)
: this()
=> Read(fileName);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(string fileName, MagickFormat format)
: this()
=> Read(fileName, format);
/// <summary>
/// Initializes a new instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public MagickImageCollection(string fileName, IMagickReadSettings<QuantumType> readSettings)
: this()
=> Read(fileName, readSettings);
/// <summary>
/// Finalizes an instance of the <see cref="MagickImageCollection"/> class.
/// </summary>
~MagickImageCollection()
=> Dispose(false);
/// <summary>
/// Event that will we raised when a warning is raised by ImageMagick.
/// </summary>
public event EventHandler<WarningEventArgs> Warning
{
add => _warning += value;
remove => _warning -= value;
}
/// <summary>
/// Gets the number of images in the collection.
/// </summary>
public int Count
=> _images.Count;
/// <summary>
/// Gets a value indicating whether the collection is read-only.
/// </summary>
public bool IsReadOnly
=> false;
/// <summary>
/// Gets or sets the image at the specified index.
/// </summary>
/// <param name="index">The index of the image to get.</param>
public IMagickImage<QuantumType> this[int index]
{
get => _images[index];
set
{
if (value is null)
throw new InvalidOperationException("Not allowed to set null value.");
if (!ReferenceEquals(value, _images[index]))
CheckDuplicate(value);
_images[index] = value;
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that iterates through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
=> _images.GetEnumerator();
/// <summary>
/// Adds an image to the collection.
/// </summary>
/// <param name="item">The image to add.</param>
public void Add(IMagickImage<QuantumType> item)
{
Throw.IfNull(item);
CheckDuplicate(item);
_images.Add(item);
}
/// <summary>
/// Adds an image with the specified file name to the collection.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Add(string fileName)
=> _images.Add(new MagickImage(fileName));
/// <summary>
/// Adds the image(s) from the specified byte array to the collection.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(byte[] data)
=> AddRange(data, null);
/// <summary>
/// Adds the image(s) from the specified byte array to the collection.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNullOrEmpty(data);
AddImages(data, 0, (uint)data.Length, readSettings, false);
}
/// <summary>
/// Adds the specified images to this collection.
/// </summary>
/// <param name="images">The images to add to the collection.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(IEnumerable<IMagickImage<QuantumType>> images)
{
Throw.IfNull(images);
Throw.IfTrue(images is MagickImageCollection, nameof(images), "Not allowed to add collection.");
foreach (var image in images)
{
Add(image);
}
}
/// <summary>
/// Adds the image(s) from the specified file name to the collection.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(string fileName)
=> AddRange(fileName, null);
/// <summary>
/// Adds the image(s) from the specified file name to the collection.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(string fileName, IMagickReadSettings<QuantumType>? readSettings)
=> AddImages(fileName, readSettings, false);
/// <summary>
/// Adds the image(s) from the specified stream to the collection.
/// </summary>
/// <param name="stream">The stream to read the images from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(Stream stream)
=> AddRange(stream, null);
/// <summary>
/// Adds the image(s) from the specified stream to the collection.
/// </summary>
/// <param name="stream">The stream to read the images from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AddRange(Stream stream, IMagickReadSettings<QuantumType>? readSettings)
=> AddImages(stream, readSettings, false);
/// <summary>
/// Creates a single image, by appending all the images in the collection horizontally (+append).
/// </summary>
/// <returns>A single image, by appending all the images in the collection horizontally (+append).</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> AppendHorizontally()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Append(_images[0], false);
return MagickImage.Create(image, GetSettings());
}
/// <summary>
/// Creates a single image, by appending all the images in the collection vertically (-append).
/// </summary>
/// <returns>A single image, by appending all the images in the collection vertically (-append).</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> AppendVertically()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Append(_images[0], true);
return MagickImage.Create(image, GetSettings());
}
/// <summary>
/// Removes all images from the collection.
/// </summary>
public void Clear()
{
foreach (var image in _images)
{
if (image is not null)
image.Dispose();
}
_images.Clear();
}
/// <summary>
/// Creates a clone of the current image collection.
/// </summary>
/// <returns>A clone of the current image collection.</returns>
public IMagickImageCollection<QuantumType> Clone()
{
var result = new MagickImageCollection();
foreach (var image in this)
result.Add(image.Clone());
return result;
}
/// <summary>
/// Merge a sequence of images. This is useful for GIF animation sequences that have page
/// offsets and disposal methods.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Coalesce()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.Coalesce(_images[0]);
ReplaceImages(images);
}
/// <summary>
/// Combines the images into a single image. The typical ordering would be
/// image 1 => Red, 2 => Green, 3 => Blue, etc.
/// </summary>
/// <returns>The images combined into a single image.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Combine()
=> Combine(ColorSpace.sRGB);
/// <summary>
/// Combines the images into a single image. The grayscale value of the pixels of each image
/// in the sequence is assigned in order to the specified channels of the combined image.
/// The typical ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
/// </summary>
/// <param name="colorSpace">The image colorspace.</param>
/// <returns>The images combined into a single image.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Combine(ColorSpace colorSpace)
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Combine(_images[0], colorSpace);
return MagickImage.Create(image, GetSettings());
}
/// <summary>
/// Perform complex mathematics on an image sequence.
/// </summary>
/// <param name="complexSettings">The complex settings.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Complex(IComplexSettings complexSettings)
{
Throw.IfNull(complexSettings);
using var imageAttacher = new TemporaryImageAttacher(_images);
using var temporaryDefines = new TemporaryDefines(_images[0]);
if (complexSettings.SignalToNoiseRatio is not null)
temporaryDefines.SetArtifact("complex:snr", complexSettings.SignalToNoiseRatio.Value.ToString(CultureInfo.InvariantCulture));
var images = _nativeInstance.Complex(_images[0], complexSettings.ComplexOperator);
ReplaceImages(images);
}
/// <summary>
/// Determines whether the collection contains the specified image.
/// </summary>
/// <param name="item">The image to check.</param>
/// <returns>True when the collection contains the specified image.</returns>
public bool Contains(IMagickImage<QuantumType> item)
=> _images.Contains(item);
/// <summary>
/// Copies the images to an <see cref="Array"/>, starting at a particular <see cref="Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination.</param>
/// <param name="arrayIndex">The zero-based index in 'destination' at which copying begins.</param>
public void CopyTo(IMagickImage<QuantumType>[] array, int arrayIndex)
{
if (_images.Count == 0)
return;
Throw.IfNull(array);
Throw.IfOutOfRange(arrayIndex, (uint)_images.Count);
Throw.IfOutOfRange(arrayIndex, (uint)array.Length);
var indexI = 0;
var indexA = arrayIndex;
var count = Math.Min(array.Length - arrayIndex, _images.Count);
while (count-- > 0)
{
array[indexA++] = _images[indexI++].Clone();
}
}
/// <summary>
/// Break down an image sequence into constituent parts. This is useful for creating GIF or
/// MNG animation sequences.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Deconstruct()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.Deconstruct(_images[0]);
ReplaceImages(images);
}
/// <summary>
/// Disposes the <see cref="MagickImageCollection"/> instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Evaluate image pixels into a single image. All the images in the collection must be the
/// same size in pixels.
/// </summary>
/// <param name="evaluateOperator">The operator.</param>
/// <returns>The resulting image of the evaluation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Evaluate(EvaluateOperator evaluateOperator)
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Evaluate(_images[0], evaluateOperator);
return MagickImage.Create(image, GetSettings());
}
/// <summary>
/// Flatten this collection into a single image.
/// Use the virtual canvas size of first image. Images which fall outside this canvas is clipped.
/// This can be used to 'fill out' a given virtual canvas.
/// </summary>
/// <returns>The resulting image of the flatten operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Flatten()
=> Merge(LayerMethod.Flatten);
/// <summary>
/// Flatten this collection into a single image.
/// This is useful for combining Photoshop layers into a single image.
/// </summary>
/// <param name="backgroundColor">The background color of the output image.</param>
/// <returns>The resulting image of the flatten operation.</returns>
public IMagickImage<QuantumType> Flatten(IMagickColor<QuantumType> backgroundColor)
{
var originalColor = _images[0].BackgroundColor;
_images[0].BackgroundColor = backgroundColor;
try
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Merge(_images[0], LayerMethod.Flatten);
return MagickImage.Create(image, GetSettings());
}
finally
{
_images[0].BackgroundColor = originalColor;
}
}
/// <summary>
/// Applies a mathematical expression to the images and returns the result.
/// </summary>
/// <param name="expression">The expression to apply.</param>
/// <returns>The resulting image of the fx operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Fx(string expression)
=> Fx(expression, Channels.All);
/// <summary>
/// Applies a mathematical expression to the images and returns the result.
/// </summary>
/// <param name="expression">The expression to apply.</param>
/// <param name="channels">The channel(s) to apply the expression to.</param>
/// <returns>The resulting image of the fx operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Fx(string expression, Channels channels)
{
Throw.IfNullOrEmpty(expression);
using var imageAttacher = new TemporaryImageAttacher(_images);
var nativeImage = MagickImage.GetNativeImage(_images[0]);
var newInstance = nativeImage.Fx(expression, channels);
return MagickImage.Create(newInstance, MagickImage.GetSettings(_images[0]));
}
/// <summary>
/// Returns an enumerator that iterates through the images.
/// </summary>
/// <returns>An enumerator that iterates through the images.</returns>
public IEnumerator<IMagickImage<QuantumType>> GetEnumerator()
=> _images.GetEnumerator();
/// <summary>
/// Determines the index of the specified image.
/// </summary>
/// <param name="item">The image to check.</param>
/// <returns>The index of the specified image.</returns>
public int IndexOf(IMagickImage<QuantumType> item)
=> _images.IndexOf(item);
/// <summary>
/// Inserts an image into the collection.
/// </summary>
/// <param name="index">The index to insert the image.</param>
/// <param name="item">The image to insert.</param>
public void Insert(int index, IMagickImage<QuantumType> item)
{
CheckDuplicate(item);
_images.Insert(index, item);
}
/// <summary>
/// Inserts an image with the specified file name into the collection.
/// </summary>
/// <param name="index">The index to insert the image.</param>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
public void Insert(int index, string fileName)
=> _images.Insert(index, new MagickImage(fileName));
/// <summary>
/// Merge all layers onto a canvas just large enough to hold all the actual images. The virtual
/// canvas of the first image is preserved but otherwise ignored.
/// </summary>
/// <returns>The resulting image of the merge operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Merge()
=> Merge(LayerMethod.Merge);
/// <summary>
/// Create a composite image by combining the images with the specified settings.
/// </summary>
/// <param name="settings">The settings to use.</param>
/// <returns>The resulting image of the montage operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Montage(IMontageSettings<QuantumType> settings)
{
Throw.IfNull(settings);
if (!string.IsNullOrEmpty(settings.Label))
_images[0].Label = settings.Label;
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.Montage(_images[0], settings);
using var result = new MagickImageCollection();
result.AddRange(MagickImage.CreateList(images, GetSettings()));
if (settings.TransparentColor is not null)
{
foreach (var image in result)
{
image.Transparent(settings.TransparentColor);
}
}
return result.Merge();
}
/// <summary>
/// The Morph method requires a minimum of two images. The first image is transformed into
/// the second by a number of intervening images as specified by frames.
/// </summary>
/// <param name="frames">The number of in-between images to generate.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morph(uint frames)
{
if (_images.Count < 2)
throw new InvalidOperationException("Operation requires at least two images.");
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.Morph(_images[0], frames);
ReplaceImages(images);
}
/// <summary>
/// Start with the virtual canvas of the first image, enlarging left and right edges to contain
/// all images. Images with negative offsets will be clipped.
/// </summary>
/// <returns>The resulting image of the mosaic operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Mosaic()
=> Merge(LayerMethod.Mosaic);
/// <summary>
/// Compares each image the GIF disposed forms of the previous image in the sequence. From
/// this it attempts to select the smallest cropped image to replace each frame, while
/// preserving the results of the GIF animation.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Optimize()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.Optimize(_images[0]);
ReplaceImages(images);
}
/// <summary>
/// OptimizePlus is exactly as Optimize, but may also add or even remove extra frames in the
/// animation, if it improves the total number of pixels in the resulting GIF animation.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void OptimizePlus()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
var images = _nativeInstance.OptimizePlus(_images[0]);
ReplaceImages(images);
}
/// <summary>
/// Compares each image the GIF disposed forms of the previous image in the sequence. Any
/// pixel that does not change the displayed result is replaced with transparency.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void OptimizeTransparency()
{
using var imageAttacher = new TemporaryImageAttacher(_images);
_nativeInstance.OptimizeTransparency(_images[0]);
}
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(byte[] data)
=> Ping(data, null);
/// <summary>
/// Reads only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(byte[] data, uint offset, uint count)
=> Ping(data, offset, count, null);
/// <summary>
/// Reads only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(byte[] data, uint offset, uint count, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNullOrEmpty(data);
Throw.IfTrue(count < 1, nameof(count), "The number of bytes should be at least 1.");
Throw.IfTrue(offset >= data.Length, nameof(offset), "The offset should not exceed the length of the array.");
Throw.IfTrue(offset + count > data.Length, nameof(count), "The number of bytes should not exceed the length of the array.");
Clear();
AddImages(data, offset, count, readSettings, true);
}
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNullOrEmpty(data);
Clear();
AddImages(data, 0, (uint)data.Length, readSettings, true);
}
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="file">The file to read the frames from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(FileInfo file)
=> Ping(file, null);
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="file">The file to read the frames from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(FileInfo file, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNull(file);
Ping(file.FullName, readSettings);
}
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(Stream stream)
=> Ping(stream, null);
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(Stream stream, IMagickReadSettings<QuantumType>? readSettings)
{
Clear();
AddImages(stream, readSettings, true);
}
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(string fileName)
=> Ping(fileName, null);
/// <summary>
/// Read only metadata and not the pixel data from all image frames.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Ping(string fileName, IMagickReadSettings<QuantumType>? readSettings)
{
Clear();
AddImages(fileName, readSettings, true);
}
/// <summary>
/// Returns a new image where each pixel is the sum of the pixels in the image sequence after applying its
/// corresponding terms (coefficient and degree pairs).
/// </summary>
/// <param name="terms">The list of polynomial coefficients and degree pairs and a constant.</param>
/// <returns>A new image where each pixel is the sum of the pixels in the image sequence after applying its
/// corresponding terms (coefficient and degree pairs).</returns>
public IMagickImage<QuantumType> Polynomial(double[] terms)
{
Throw.IfNullOrEmpty(terms);
using var imageAttacher = new TemporaryImageAttacher(_images);
var image = _nativeInstance.Polynomial(_images[0], terms, (nuint)terms.Length);
return MagickImage.Create(image, GetSettings());
}
/// <summary>
/// Remap image colors with closest color from reference image.
/// </summary>
/// <param name="image">The image to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Remap(IMagickImage image)
=> Remap(image, new QuantizeSettings());
/// <summary>
/// Remap image colors with closest color from reference image.
/// </summary>
/// <param name="image">The image to use.</param>
/// <param name="settings">Quantize settings.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Remap(IMagickImage image, IQuantizeSettings settings)
{
Throw.IfNull(image);
Throw.IfNull(settings);
using var imageAttacher = new TemporaryImageAttacher(_images);
_nativeInstance.Remap(_images[0], settings, image);
}
/// <summary>
/// Quantize images (reduce number of colors).
/// </summary>
/// <returns>The resulting image of the quantize operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickErrorInfo? Quantize()
=> Quantize(new QuantizeSettings());
/// <summary>
/// Quantize images (reduce number of colors).
/// </summary>
/// <param name="settings">Quantize settings.</param>
/// <returns>The resulting image of the quantize operation.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickErrorInfo? Quantize(IQuantizeSettings settings)
{
Throw.IfNull(settings);
using var imageAttacher = new TemporaryImageAttacher(_images);
_nativeInstance.Quantize(_images[0], settings);
if (settings.MeasureErrors && _images[0] is MagickImage image)
return MagickImage.CreateErrorInfo(image);
else
return null;
}
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data)
=> Read(data, null);
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data, uint offset, uint count)
=> Read(data, offset, count, null);
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data, uint offset, uint count, MagickFormat format)
=> Read(data, offset, count, new MagickReadSettings { Format = format });
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data, uint offset, uint count, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNullOrEmpty(data);
Throw.IfTrue(count < 1, nameof(count), "The number of bytes should be at least 1.");
Throw.IfTrue(offset >= data.Length, nameof(offset), "The offset should not exceed the length of the array.");
Throw.IfTrue(offset + count > data.Length, nameof(count), "The number of bytes should not exceed the length of the array.");
Clear();
AddImages(data, offset, count, readSettings, false);
}
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data, MagickFormat format)
=> Read(data, new MagickReadSettings { Format = format });
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="data">The byte array to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNullOrEmpty(data);
Clear();
AddImages(data, 0, (uint)data.Length, readSettings, false);
}
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="file">The file to read the frames from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(FileInfo file)
=> Read(file, null);
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="file">The file to read the frames from.</param>
/// <param name="format">The format to use.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(FileInfo file, MagickFormat format)
=> Read(file, new MagickReadSettings { Format = format });
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="file">The file to read the frames from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(FileInfo file, IMagickReadSettings<QuantumType>? readSettings)
{
Throw.IfNull(file);
Read(file.FullName, readSettings);
}
/// <summary>
/// Read all image frames.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Read(Stream stream)
=> Read(stream, null);