-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageUtilities.cs
319 lines (196 loc) · 9.53 KB
/
ImageUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using Gsemac.Drawing.Imaging;
using System;
using System.Drawing;
namespace Gsemac.Drawing {
public static class ImageUtilities {
// Public members
public static bool HasIndexedPixelFormat(Image image) {
switch (image.PixelFormat) {
case System.Drawing.Imaging.PixelFormat.Format1bppIndexed:
case System.Drawing.Imaging.PixelFormat.Format4bppIndexed:
case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
case System.Drawing.Imaging.PixelFormat.Indexed:
return true;
default:
return false;
}
}
public static Image ConvertToNonIndexedPixelFormat(Image image, bool disposeSourceImage = false) {
// We can't create a graphics object from an image with an indexed pixel format, so we need to create a new bitmap.
if (!HasIndexedPixelFormat(image))
return image;
Bitmap resultImage = new Bitmap(image);
if (disposeSourceImage)
image.Dispose();
return resultImage;
}
public static Image ConvertToNonIndexedPixelFormat(IImage image, bool disposeSourceImage = false) {
Bitmap resultImage = image.ToBitmap();
if (disposeSourceImage)
image.Dispose();
return ConvertToNonIndexedPixelFormat(resultImage, disposeSourceImage: true);
}
public static Image Resize(Image image, IImageResizingOptions options) {
if (image is null)
throw new ArgumentNullException(nameof(image));
if (options is null)
throw new ArgumentNullException(nameof(options));
if (!options.Width.HasValue && !options.Height.HasValue && !options.HorizontalScale.HasValue && !options.VerticalScale.HasValue)
return (Image)image.Clone();
if (options.SizingMode == ImageSizingMode.ResizeIfLarger) {
if ((!options.Width.HasValue || image.Width <= options.Width.Value) && (!options.Height.HasValue || image.Height <= options.Height.Value))
return (Image)image.Clone();
}
if (options.SizingMode == ImageSizingMode.ResizeIfSmaller) {
if ((!options.Width.HasValue || image.Width >= options.Width.Value) && (!options.Height.HasValue || image.Height >= options.Height.Value))
return (Image)image.Clone();
}
int? newWidth = options.Width;
int? newHeight = options.Height;
// If new dimensions haven't been provided, then scaling takes secondary priority.
if (!newWidth.HasValue && options.HorizontalScale.HasValue)
newWidth = (int)(image.Width * options.HorizontalScale.Value);
if (!newHeight.HasValue && options.VerticalScale.HasValue)
newHeight = (int)(image.Height * options.VerticalScale.Value);
// Adjust one of the dimensions in order to maintain the original aspect ratio if necessary.
if (newWidth.HasValue && newHeight.HasValue && options.MaintainAspectRatio) {
double newHorizontalScale = (double)newWidth.Value / image.Width;
double newVerticalScale = (double)newHeight.Value / image.Height;
double minimumScale = Math.Min(newHorizontalScale, newVerticalScale);
newWidth = (int)(image.Width * minimumScale);
newHeight = (int)(image.Height * minimumScale);
}
// If the image hasn't been resized at all, just return the source image.
if (!newWidth.HasValue && !newHeight.HasValue)
return (Image)image.Clone();
return ResizeInternal(image, width: newWidth, height: newHeight);
}
public static Image Resize(Image image, int? width = null, int? height = null) {
return Resize(image, new ImageResizingOptions() {
Width = width,
Height = height,
});
}
public static Image Resize(Image image, double? horizontalScale = null, double? verticalScale = null) {
return Resize(image, new ImageResizingOptions() {
HorizontalScale = horizontalScale,
VerticalScale = verticalScale,
});
}
public static Bitmap Trim(Bitmap image) {
if (image is null)
throw new ArgumentNullException(nameof(image));
return Trim(image, 0.0);
}
public static Bitmap Trim(Bitmap image, Color trimColor) {
if (image is null)
throw new ArgumentNullException(nameof(image));
return Trim(image, trimColor, 0.0);
}
public static Bitmap Trim(Bitmap image, double tolerance) {
if (image is null)
throw new ArgumentNullException(nameof(image));
Color trimColor = image.GetPixel(0, 0);
return Trim(image, trimColor, tolerance);
}
public static Bitmap Trim(Bitmap image, Color trimColor, double tolerance) {
if (image is null)
throw new ArgumentNullException(nameof(image));
if (image.Width > 0 && image.Height > 0) {
// Calculate the area to trim.
int left, right, top, bottom;
for (left = 0; left < image.Width; ++left)
if (!ColumnIsColor(image, left, trimColor, tolerance))
break;
for (right = image.Width - 1; right >= 0; --right)
if (!ColumnIsColor(image, right, trimColor, tolerance))
break;
for (top = 0; top < image.Height; ++top)
if (!RowIsColor(image, top, trimColor, tolerance))
break;
for (bottom = image.Height - 1; bottom >= 0; --bottom)
if (!RowIsColor(image, bottom, trimColor, tolerance))
break;
// Crop the image.
int x = left;
int y = top;
int width = right + 1 - x;
int height = bottom + 1 - y;
// If the cropped area is impossible (this can occur if the tolerance is too high or the image is entirely cropped), use the original dimensions.
if (width <= 0) {
x = 0;
width = image.Width;
}
if (height <= 0) {
y = 0;
height = image.Height;
}
Bitmap newImage = new Bitmap(width, height);
try {
using (Graphics graphics = Graphics.FromImage(newImage)) {
graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
return newImage;
}
}
catch (Exception) {
newImage.Dispose();
throw;
}
}
else {
return image;
}
}
// Private members
private static Image ResizeInternal(Image image, int? width = null, int? height = null) {
if ((width ?? 0) <= 0)
width = null;
if ((height ?? 0) <= 0)
height = null;
int newWidth;
int newHeight;
if (width.HasValue && height.HasValue) {
newWidth = width.Value;
newHeight = height.Value;
}
else if (width.HasValue) {
float scaleFactor = (float)width.Value / image.Width;
newWidth = width.Value;
newHeight = (int)(image.Height * scaleFactor);
}
else if (height.HasValue) {
float scaleFactor = (float)height.Value / image.Height;
newWidth = (int)(image.Width * scaleFactor);
newHeight = height.Value;
}
else {
// If no dimensions have been specified, simply return the original image.
return image;
}
// Don't allow any dimension to be reduced to 0, because we'll get an exception.
// TODO: Add a unit test for this.
if (newWidth == 0)
newWidth = 1;
if (newHeight == 0)
newHeight = 1;
Bitmap resultImage = new Bitmap(image, new Size(newWidth, newHeight));
return resultImage;
}
private static bool ColorIsMatch(Color first, Color second, double tolerance) {
double requiredSimilarity = 1.0 - tolerance;
return ColorUtilities.ComputeSimilarity(first, second, ColorDistanceStrategy.DeltaE) >= requiredSimilarity;
}
private static bool RowIsColor(Bitmap bitmap, int row, Color trimColor, double tolerance) {
for (int x = 0; x < bitmap.Width; ++x)
if (!ColorIsMatch(bitmap.GetPixel(x, row), trimColor, tolerance))
return false;
return true;
}
private static bool ColumnIsColor(Bitmap bitmap, int column, Color trimColor, double tolerance) {
for (int y = 0; y < bitmap.Height; ++y)
if (!ColorIsMatch(bitmap.GetPixel(column, y), trimColor, tolerance))
return false;
return true;
}
}
}