forked from JimBobSquarePants/ImageProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFactory.cs
More file actions
1575 lines (1377 loc) · 56 KB
/
Copy pathImageFactory.cs
File metadata and controls
1575 lines (1377 loc) · 56 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 file="ImageFactory.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods for processing image files in a fluent manner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Common.Extensions;
using ImageProcessor.Configuration;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Filters.EdgeDetection;
// using ImageProcessor.Imaging.Filters.ObjectDetection;
using ImageProcessor.Imaging.Filters.Photo;
using ImageProcessor.Imaging.Formats;
using ImageProcessor.Imaging.MetaData;
using ImageProcessor.Processors;
/// <summary>
/// Encapsulates methods for processing image files in a fluent manner.
/// </summary>
public class ImageFactory : IDisposable
{
/// <summary>
/// The default quality for image files.
/// </summary>
private const int DefaultQuality = 90;
/// <summary>
/// Whether to preserve exif metadata
/// </summary>
private bool preserveExifData;
/// <summary>
/// The backup supported image format.
/// </summary>
private ISupportedImageFormat backupFormat;
/// <summary>
/// The backup collection of property items containing EXIF metadata.
/// </summary>
private ConcurrentDictionary<int, PropertyItem> backupExifPropertyItems;
/// <summary>
/// A value indicating whether this instance of the given entity has been disposed.
/// </summary>
/// <value><see langword="true"/> if this instance has been disposed; otherwise, <see langword="false"/>.</value>
/// <remarks>
/// If the entity is disposed, it must not be disposed a second
/// time. The isDisposed field is set the first time the entity
/// is disposed. If the isDisposed field is true, then the Dispose()
/// method will not dispose again. This help not to prolong the entity's
/// life in the Garbage Collector.
/// </remarks>
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="ImageFactory"/> class.
/// </summary>
/// <param name="preserveExifData">
/// Whether to preserve exif metadata. Defaults to false.
/// </param>
public ImageFactory(bool preserveExifData = false)
: this(preserveExifData, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageFactory"/> class.
/// </summary>
/// <param name="preserveExifData">
/// Whether to preserve exif metadata. Defaults to false.
/// </param>
/// <param name="fixGamma">
/// Whether to fix the gamma component of the image.
/// </param>
public ImageFactory(bool preserveExifData, bool fixGamma)
: this(!preserveExifData ? MetaDataMode.None : MetaDataMode.All, fixGamma)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageFactory"/> class.
/// </summary>
/// <param name="metaDataMode">The metadata mode to use</param>
public ImageFactory(MetaDataMode metaDataMode)
: this(metaDataMode, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageFactory"/> class.
/// </summary>
/// <param name="metaDataMode">The metadata mode to use</param>
/// <param name="fixGamma">Whether to fix the gamma component of the image.</param>
public ImageFactory(MetaDataMode metaDataMode, bool fixGamma)
{
// Note the order here.
// We need to set MetaDataMode after PreserveExifData as the first option doesn't allow the granular control allowed by the constructor.
this.PreserveExifData = metaDataMode != MetaDataMode.None;
this.MetaDataMode = metaDataMode;
this.ExifPropertyItems = new ConcurrentDictionary<int, PropertyItem>();
this.backupExifPropertyItems = new ConcurrentDictionary<int, PropertyItem>();
this.FixGamma = fixGamma;
}
/// <summary>
/// Finalizes an instance of the <see cref="ImageFactory"/> class.
/// </summary>
/// <remarks>
/// Use C# destructor syntax for finalization code.
/// This destructor will run only if the Dispose method
/// does not get called.
/// It gives your base class the opportunity to finalize.
/// Do not provide destructors in types derived from this class.
/// </remarks>
~ImageFactory()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
this.Dispose(false);
}
/// <summary>
/// Gets the color depth in number of bits per pixel to save the image with.
/// This can be used to change the bit depth of images that can be saved with different
/// bit depths such as TIFF.
/// </summary>
public long CurrentBitDepth { get; internal set; }
/// <summary>
/// Gets the path to the local image for manipulation.
/// </summary>
public string ImagePath { get; private set; }
/// <summary>
/// Gets a value indicating whether the image factory should process the file.
/// </summary>
public bool ShouldProcess { get; private set; }
/// <summary>
/// Gets the supported image format.
/// </summary>
public ISupportedImageFormat CurrentImageFormat { get; private set; }
/// <summary>
/// Gets the metadata mode.
/// </summary>
public MetaDataMode MetaDataMode { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether to preserve exif metadata.
/// This property is only settable for backwards compatibility. Set <see cref="MetaDataMode"/> via the constructor instead.
/// </summary>
public bool PreserveExifData
{
get => this.preserveExifData;
set
{
this.preserveExifData = value;
this.MetaDataMode = this.preserveExifData ? MetaDataMode.All : MetaDataMode.None;
}
}
/// <summary>
/// Gets or sets a value indicating whether to fix the gamma component of the current image.
/// </summary>
public bool FixGamma { get; set; }
/// <summary>
/// Gets or the current gamma value.
/// </summary>
public float CurrentGamma { get; private set; }
/// <summary>
/// Gets or sets the collection of property items containing EXIF metadata.
/// </summary>
public ConcurrentDictionary<int, PropertyItem> ExifPropertyItems { get; set; }
/// <summary>
/// Gets or the local image for manipulation.
/// </summary>
public Image Image { get; internal set; }
/// <summary>
/// Gets or sets the process mode for frames in animated images.
/// </summary>
public AnimationProcessMode AnimationProcessMode { get; set; }
/// <summary>
/// Gets or sets the stream for storing any input stream to prevent disposal.
/// </summary>
internal Stream InputStream { get; set; }
/// <summary>
/// Loads the image to process. Always call this method first.
/// </summary>
/// <param name="stream">
/// The <see cref="T:System.IO.Stream"/> containing the image information.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Load(Stream stream)
{
var memoryStream = new MemoryStream();
// Copy the stream. Disposal of the input stream is the responsibility
// of the user.
stream.CopyTo(memoryStream);
// Set the position to 0 afterward.
if (stream.CanSeek)
{
stream.Position = 0;
}
ISupportedImageFormat format = FormatUtilities.GetFormat(memoryStream);
if (format == null)
{
throw new ImageFormatException("Input stream is not a supported format.");
}
// Set our image as the memory stream value.
this.Image = format.Load(memoryStream);
// Save the bit depth
this.CurrentBitDepth = Image.GetPixelFormatSize(this.Image.PixelFormat);
// Store the stream so we can dispose of it later.
this.InputStream = memoryStream;
// Set the other properties.
format.Quality = DefaultQuality;
format.IsIndexed = FormatUtilities.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data.
// TODO. Some custom data doesn't seem to get copied by default methods.
foreach (int id in this.Image.PropertyIdList)
{
this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id);
}
if (this.CurrentImageFormat is IAnimatedImageFormat imageFormat)
{
imageFormat.AnimationProcessMode = this.AnimationProcessMode;
}
this.backupExifPropertyItems = new ConcurrentDictionary<int, PropertyItem>(this.ExifPropertyItems);
// Ensure the image is in the most efficient format but don't reserve exif data.
Image formatted = this.Image.Copy(this.AnimationProcessMode);
this.Image.Dispose();
this.Image = formatted;
this.ShouldProcess = true;
return this;
}
/// <summary>
/// Loads the image to process. Always call this method first.
/// </summary>
/// <param name="imagePath">The absolute path to the image to load.</param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Load(string imagePath)
{
var fileInfo = new FileInfo(imagePath);
if (fileInfo.Exists)
{
this.ImagePath = imagePath;
// Open a file stream to prevent the need for lock.
using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
ISupportedImageFormat format = FormatUtilities.GetFormat(fileStream);
if (format == null)
{
throw new ImageFormatException("Input stream is not a supported format.");
}
var memoryStream = new MemoryStream();
// Copy the stream.
fileStream.CopyTo(memoryStream);
// Set the position to 0 afterward.
memoryStream.Position = 0;
// Set our image as the memory stream value.
this.Image = format.Load(memoryStream);
// Save the bit depth
this.CurrentBitDepth = Image.GetPixelFormatSize(this.Image.PixelFormat);
// Store the stream so we can dispose of it later.
this.InputStream = memoryStream;
// Set the other properties.
format.Quality = DefaultQuality;
format.IsIndexed = FormatUtilities.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data.
foreach (PropertyItem propertyItem in this.Image.PropertyItems)
{
this.ExifPropertyItems[propertyItem.Id] = propertyItem;
}
this.backupExifPropertyItems = new ConcurrentDictionary<int, PropertyItem>(this.ExifPropertyItems);
if (this.CurrentImageFormat is IAnimatedImageFormat imageFormat)
{
imageFormat.AnimationProcessMode = this.AnimationProcessMode;
}
// Ensure the image is in the most efficient format but don't reserve exif data.
Image formatted = this.Image.Copy(this.AnimationProcessMode);
this.Image.Dispose();
this.Image = formatted;
this.ShouldProcess = true;
}
}
else
{
throw new FileNotFoundException(imagePath);
}
return this;
}
/// <summary>
/// Loads the image to process from an array of bytes. Always call this method first.
/// </summary>
/// <param name="bytes">
/// The <see cref="T:System.Byte"/> containing the image information.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Load(byte[] bytes)
{
var memoryStream = new MemoryStream(bytes);
ISupportedImageFormat format = FormatUtilities.GetFormat(memoryStream);
if (format == null)
{
throw new ImageFormatException("Input stream is not a supported format.");
}
// Set our image as the memory stream value.
this.Image = format.Load(memoryStream);
// Save the bit depth
this.CurrentBitDepth = Image.GetPixelFormatSize(this.Image.PixelFormat);
// Store the stream so we can dispose of it later.
this.InputStream = memoryStream;
// Set the other properties.
format.Quality = DefaultQuality;
format.IsIndexed = FormatUtilities.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data.
foreach (int id in this.Image.PropertyIdList)
{
this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id);
}
if (this.CurrentImageFormat is IAnimatedImageFormat imageFormat)
{
imageFormat.AnimationProcessMode = this.AnimationProcessMode;
}
// Ensure the image is in the most efficient format but don't reserve exif data.
Image formatted = this.Image.Copy(this.AnimationProcessMode);
this.Image.Dispose();
this.Image = formatted;
this.ShouldProcess = true;
return this;
}
/// <summary>
/// Loads the image to process from an array of bytes. Always call this method first.
/// </summary>
/// <param name="image">
/// The <see cref="T:System.Drawing.Image"/> to load.
/// The original image is untouched during manipulation as a copy is made. Disposal of the input image is the responsibility of the user.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Load(Image image)
{
// Try saving with the raw format. This might not be possible if the image was created
// in-memory so we fall back to BMP to keep in line with the default in System.Drawing
// if no format found.
var memoryStream = new MemoryStream();
ISupportedImageFormat format = new BitmapFormat();
try
{
image.Save(memoryStream, image.RawFormat);
format = ImageProcessorBootstrapper.Instance.SupportedImageFormats
.First(f => f.ImageFormat.Equals(image.RawFormat));
}
catch
{
image.Save(memoryStream, ImageFormat.Bmp);
}
if (format is IAnimatedImageFormat imageFormat)
{
imageFormat.AnimationProcessMode = this.AnimationProcessMode;
}
// Ensure the image is in the most efficient format.
// Set our image.
this.Image = image.Copy(this.AnimationProcessMode);
// Save the bit depth
this.CurrentBitDepth = Image.GetPixelFormatSize(this.Image.PixelFormat);
// Store the stream so we can dispose of it later.
this.InputStream = memoryStream;
// Set the other properties.
format.Quality = DefaultQuality;
format.IsIndexed = FormatUtilities.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data.
foreach (int id in this.Image.PropertyIdList)
{
this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id);
}
this.ShouldProcess = true;
return this;
}
/// <summary>
/// Resets the current image to its original loaded state.
/// </summary>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Reset()
{
if (this.ShouldProcess)
{
// Set our new image as the memory stream value.
if (this.InputStream.CanSeek)
{
this.InputStream.Position = 0;
}
// Reset properties.
this.CurrentImageFormat = this.backupFormat;
this.ExifPropertyItems = new ConcurrentDictionary<int, PropertyItem>(this.backupExifPropertyItems);
this.CurrentImageFormat.Quality = DefaultQuality;
Image newImage = this.backupFormat.Load(this.InputStream);
// Dispose and reassign the image.
// Ensure the image is in the most efficient format.
Image formatted = newImage.Copy(this.AnimationProcessMode);
newImage.Dispose();
this.Image.Dispose();
this.Image = formatted;
}
return this;
}
/// <summary>
/// Changes the opacity of the current image.
/// </summary>
/// <param name="percentage">
/// The percentage by which to alter the images opacity.
/// Any integer between 0 and 100.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Alpha(int percentage)
{
if (this.ShouldProcess)
{
// Sanitize the input.
// You can't make an image less transparent.
if (percentage < 0 || percentage > 99)
{
return this;
}
var alpha = new Alpha { DynamicParameter = percentage };
this.backupFormat.ApplyProcessor(alpha.ProcessImage, this);
}
return this;
}
/// <summary>
/// Performs auto-rotation to ensure that EXIF defined rotation is reflected in
/// the final image.
/// </summary>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory AutoRotate()
{
if (this.ShouldProcess)
{
var autoRotate = new AutoRotate();
this.backupFormat.ApplyProcessor(autoRotate.ProcessImage, this);
}
return this;
}
/// <summary>
/// Alters the bit depth of the current image.
/// <remarks>
/// This can only be used to change the bit depth of images that can be saved
/// by <see cref="System.Drawing"/> with different bit depths such as TIFF.
/// </remarks>
/// </summary>
/// <param name="bitDepth">A value over 0.</param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory BitDepth(long bitDepth)
{
if (bitDepth > 0 && this.ShouldProcess)
{
this.CurrentBitDepth = bitDepth;
}
return this;
}
/// <summary>
/// Changes the brightness of the current image.
/// </summary>
/// <param name="percentage">
/// The percentage by which to alter the images brightness.
/// Any integer between -100 and 100.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Brightness(int percentage)
{
if (this.ShouldProcess)
{
// Sanitize the input.
if (percentage > 100 || percentage < -100 || percentage == 0)
{
return this;
}
var brightness = new Brightness { DynamicParameter = percentage };
this.backupFormat.ApplyProcessor(brightness.ProcessImage, this);
}
return this;
}
/// <summary>
/// Changes the background color of the current image.
/// </summary>
/// <param name="color">
/// The <see cref="T:System.Drawing.Color"/> to paint the image with.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory BackgroundColor(Color color)
{
if (this.ShouldProcess)
{
var backgroundColor = new BackgroundColor { DynamicParameter = color };
this.backupFormat.ApplyProcessor(backgroundColor.ProcessImage, this);
}
return this;
}
/// <summary>
/// Constrains the current image, resizing it to fit within the given dimensions whilst keeping its aspect ratio.
/// </summary>
/// <param name="size">
/// The <see cref="T:System.Drawing.Size"/> containing the maximum width and height to set the image to.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Constrain(Size size)
{
if (this.ShouldProcess)
{
var layer = new ResizeLayer(size, ResizeMode.Max);
return this.Resize(layer);
}
return this;
}
/// <summary>
/// Changes the contrast of the current image.
/// </summary>
/// <param name="percentage">
/// The percentage by which to alter the images contrast.
/// Any integer between -100 and 100.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Contrast(int percentage)
{
if (this.ShouldProcess)
{
// Sanitize the input.
if (percentage > 100 || percentage < -100)
{
return this;
}
var contrast = new Contrast { DynamicParameter = percentage };
this.backupFormat.ApplyProcessor(contrast.ProcessImage, this);
}
return this;
}
/// <summary>
/// Crops the current image to the given location and size.
/// </summary>
/// <param name="rectangle">
/// The <see cref="T:System.Drawing.Rectangle"/> containing the coordinates to crop the image to.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Crop(Rectangle rectangle)
{
if (this.ShouldProcess)
{
var cropLayer = new CropLayer(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, CropMode.Pixels);
return this.Crop(cropLayer);
}
return this;
}
/// <summary>
/// Crops the current image to the given location and size.
/// </summary>
/// <param name="cropLayer">
/// The <see cref="Imaging.CropLayer"/> containing the coordinates and mode to crop the image with.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Crop(CropLayer cropLayer)
{
if (this.ShouldProcess)
{
var crop = new Crop { DynamicParameter = cropLayer };
this.backupFormat.ApplyProcessor(crop.ProcessImage, this);
}
return this;
}
/// <summary>
/// Detects the edges in the current image.
/// </summary>
/// <param name="filter">
/// The <see cref="IEdgeFilter"/> to detect edges with.
/// </param>
/// <param name="greyscale">
/// Whether to convert the image to greyscale first - Defaults to true.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory DetectEdges(IEdgeFilter filter, bool greyscale = true)
{
if (this.ShouldProcess)
{
var detectEdges = new DetectEdges { DynamicParameter = new Tuple<IEdgeFilter, bool>(filter, greyscale) };
this.backupFormat.ApplyProcessor(detectEdges.ProcessImage, this);
}
return this;
}
/// <summary>
/// Sets the resolution of the image.
/// <remarks>
/// This method sets both the bitmap data and EXIF resolution if available.
/// </remarks>
/// </summary>
/// <param name="horizontal">The horizontal resolution.</param>
/// <param name="vertical">The vertical resolution.</param>
/// <param name="unit">
/// The unit of measure for the horizontal resolution and the vertical resolution.
/// Defaults to inches
/// </param>
/// <returns>
/// The <see cref="ImageFactory"/>.
/// </returns>
public ImageFactory Resolution(int horizontal, int vertical, PropertyTagResolutionUnit unit = PropertyTagResolutionUnit.Inch)
{
if (this.ShouldProcess)
{
// Sanitize the input.
if (horizontal < 0 || vertical < 0)
{
return this;
}
var resolution =
new Tuple<int, int, PropertyTagResolutionUnit>(horizontal, vertical, unit);
var dpi = new Resolution { DynamicParameter = resolution };
this.backupFormat.ApplyProcessor(dpi.ProcessImage, this);
}
return this;
}
// public ImageFactory DetectObjects(HaarCascade cascade, bool drawRectangles = true, Color color = default(Color))
// {
// if (this.ShouldProcess)
// {
// DetectObjects detectObjects = new DetectObjects { DynamicParameter = cascade };
// this.backupFormat.ApplyProcessor(detectObjects.ProcessImage, this);
// }
// return this;
// }
/// <summary>
/// Crops an image to the area of greatest entropy.
/// </summary>
/// <param name="threshold">
/// The threshold in bytes to control the entropy.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory EntropyCrop(byte threshold = 128)
{
if (this.ShouldProcess)
{
var autoCrop = new EntropyCrop { DynamicParameter = threshold };
this.backupFormat.ApplyProcessor(autoCrop.ProcessImage, this);
}
return this;
}
/// <summary>
/// Applies a filter to the current image. Use the <see cref="MatrixFilters"/> class to
/// assign the correct filter.
/// </summary>
/// <param name="matrixFilter">
/// The <see cref="IMatrixFilter"/> of the filter to add to the image.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Filter(IMatrixFilter matrixFilter)
{
if (this.ShouldProcess)
{
var filter = new Filter { DynamicParameter = matrixFilter };
this.backupFormat.ApplyProcessor(filter.ProcessImage, this);
}
return this;
}
/// <summary>
/// Flips the current image either horizontally or vertically.
/// </summary>
/// <param name="flipVertically">
/// Whether to flip the image vertically.
/// </param>
/// <param name="flipBoth">
/// Whether to flip the image both vertically and horizontally.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Flip(bool flipVertically = false, bool flipBoth = false)
{
if (this.ShouldProcess)
{
RotateFlipType rotateFlipType;
if (flipBoth)
{
rotateFlipType = RotateFlipType.RotateNoneFlipXY;
}
else
{
rotateFlipType = flipVertically
? RotateFlipType.RotateNoneFlipY
: RotateFlipType.RotateNoneFlipX;
}
var flip = new Flip { DynamicParameter = rotateFlipType };
this.backupFormat.ApplyProcessor(flip.ProcessImage, this);
}
return this;
}
/// <summary>
/// Sets the output format of the current image to the matching <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
/// </summary>
/// <param name="format">The <see cref="ISupportedImageFormat"/>. to set the image to.</param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Format(ISupportedImageFormat format)
{
if (this.ShouldProcess)
{
this.CurrentImageFormat = format;
// Apply any fomatting quirks.
// this.backupFormat.ApplyProcessor(factory => factory.Image, this);
}
return this;
}
/// <summary>
/// Adjust the gamma (intensity of the light) component of the given image.
/// </summary>
/// <param name="value">
/// The value to adjust the gamma by (typically between .2 and 5).
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Gamma(float value)
{
if (this.ShouldProcess)
{
// Sanitize the input.
if (value > 5 || value < .1)
{
return this;
}
this.CurrentGamma = value;
var gamma = new Gamma { DynamicParameter = value };
this.backupFormat.ApplyProcessor(gamma.ProcessImage, this);
}
return this;
}
/// <summary>
/// Uses a Gaussian kernel to blur the current image.
/// <remarks>
/// <para>
/// The sigma and threshold values applied to the kernel are
/// 1.4 and 0 respectively.
/// </para>
/// </remarks>
/// </summary>
/// <param name="size">
/// The size to set the Gaussian kernel to.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory GaussianBlur(int size)
{
if (this.ShouldProcess && size > 0)
{
var layer = new GaussianLayer(size);
return this.GaussianBlur(layer);
}
return this;
}
/// <summary>
/// Uses a Gaussian kernel to blur the current image.
/// </summary>
/// <param name="gaussianLayer">
/// The <see cref="T:ImageProcessor.Imaging.GaussianLayer"/> for applying sharpening and
/// blurring methods to an image.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory GaussianBlur(GaussianLayer gaussianLayer)
{
if (this.ShouldProcess)
{
var gaussianBlur = new GaussianBlur { DynamicParameter = gaussianLayer };
this.backupFormat.ApplyProcessor(gaussianBlur.ProcessImage, this);
}
return this;
}
/// <summary>
/// Uses a Gaussian kernel to sharpen the current image.
/// <remarks>
/// <para>
/// The sigma and threshold values applied to the kernel are
/// 1.4 and 0 respectively.
/// </para>
/// </remarks>
/// </summary>
/// <param name="size">
/// The size to set the Gaussian kernel to.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory GaussianSharpen(int size)
{
if (this.ShouldProcess && size > 0)
{
var layer = new GaussianLayer(size);
return this.GaussianSharpen(layer);
}
return this;
}
/// <summary>
/// Uses a Gaussian kernel to sharpen the current image.
/// </summary>
/// <param name="gaussianLayer">
/// The <see cref="T:ImageProcessor.Imaging.GaussianLayer"/> for applying sharpening and
/// blurring methods to an image.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory GaussianSharpen(GaussianLayer gaussianLayer)
{
if (this.ShouldProcess)
{
var gaussianSharpen = new GaussianSharpen { DynamicParameter = gaussianLayer };
this.backupFormat.ApplyProcessor(gaussianSharpen.ProcessImage, this);
}
return this;
}
/// <summary>
/// Alters the hue of the current image changing the overall color.