-
Notifications
You must be signed in to change notification settings - Fork 7
/
SmartImageOptimization.cs
215 lines (179 loc) · 7.08 KB
/
SmartImageOptimization.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
using System;
using System.Collections.Generic;
using System.IO;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
internal class SmartImageOptimizationExample
{
private static void Main(string[] args)
{
const int maxAllowedSize = 1024 * 1024; // 1 MB
const string srcPath = "../../../../_Input/Venice.jpg";
CallFitDimensionsToFileSize(
srcPath,
new JpegSettings(85, true, true),
maxAllowedSize,
"../../../../_Output/Venice_optimal_size.jpg");
CallFitDimensionsToFileSize(
srcPath,
new TiffSettings(CompressionType.Lzw),
maxAllowedSize,
"../../../../_Output/Venice_optimal_size.tif");
CallFitDimensionsToFileSize(
srcPath,
new PngSettings(false),
maxAllowedSize,
"../../../../_Output/Venice_optimal_size.png");
CallFitDimensionsToFileSize(
srcPath,
new EpsSettings(CompressionType.Jpeg, 85),
maxAllowedSize,
"../../../../_Output/Venice_optimal_size.eps");
CallFitDimensionsToFileSize(
srcPath,
new TgaSettings(CompressionType.Rle, true),
maxAllowedSize,
"../../../../_Output/Venice_optimal_size.tga");
CallFitJpegQualityToFileSize(
srcPath,
maxAllowedSize,
"../../../../_Output/Venice_optimal_quality.jpg");
}
/// <summary>
/// Calls FitDimensionsToFileSize method and writes result info to console
/// </summary>
private static void CallFitDimensionsToFileSize(string srcPath, WriterSettings settings, long maxAllowedSize, string dstPath)
{
Console.WriteLine("Fit image dimensions to {0}", FormatFileSize(maxAllowedSize));
using (var reader = ImageReader.Create(srcPath))
{
Console.WriteLine(
"Source: \n - Dimensions: {0} x {1}\n - Format: {2}",
reader.Width,
reader.Height,
reader.FileFormat);
}
FitDimensionsToFileSize(srcPath, settings, maxAllowedSize, dstPath);
Console.WriteLine(
"Destination:\n - Format: {0}\n - File size: {1}",
settings.Format,
FormatFileSize(new FileInfo(dstPath).Length));
var unsupportedFormats = new List<FileFormat> { FileFormat.Eps, FileFormat.Pdf };
if (!unsupportedFormats.Contains(settings.Format))
{
using (var reader = ImageReader.Create(dstPath))
{
Console.WriteLine(" - Dimensions: {0} x {1}", reader.Width, reader.Height);
}
}
Console.WriteLine();
}
/// <summary>
/// Calls FitJpegQualityToFileSize method and writes result info to console
/// </summary>
private static void CallFitJpegQualityToFileSize(string srcPath, long maxAllowedSize, string dstPath)
{
Console.WriteLine("Fit jpeg quality to {0}", FormatFileSize(maxAllowedSize));
using (var reader = ImageReader.Create(srcPath))
{
Console.WriteLine("Source:\n - Dimensions {0} x {1}\n - Format: {2}", reader.Width, reader.Height, reader.FileFormat);
}
FitJpegQualityToFileSize(srcPath, maxAllowedSize, dstPath);
Console.WriteLine("Destination:\n - File size: {0}", FormatFileSize(new FileInfo(dstPath).Length));
using (var reader = ImageReader.Create(dstPath))
{
Console.WriteLine(" - Dimensions: {0} x {1}", reader.Width, reader.Height);
}
Console.WriteLine();
}
/// <summary>
/// A wrapper for FitDimensionsToFileSize method with path arguments
/// </summary>
private static void FitDimensionsToFileSize(string srcPath, WriterSettings settings, long maxAllowedSize, string dstPath)
{
using (var srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
using (var dstStream = new FileStream(dstPath, FileMode.Create, FileAccess.Write))
{
FitDimensionsToFileSize(srcStream, settings, maxAllowedSize, dstStream);
}
}
/// <summary>
/// Incrementally applies a resize transform to fit encoded image into maxAllowedSize limit
/// </summary>
private static void FitDimensionsToFileSize(Stream srcStream, WriterSettings settings, long maxAllowedSize, Stream dstStream)
{
float scale = 1.0f;
const float minScale = 0.98f;
var ms = ResizeImageToMemoryStream(srcStream, scale, settings);
try
{
while (ms.Length > maxAllowedSize)
{
scale *= (float)Math.Min(Math.Sqrt((float)maxAllowedSize / (float)ms.Length), minScale);
ms.Dispose();
ms = ResizeImageToMemoryStream(srcStream, scale, settings);
}
ms.WriteTo(dstStream);
}
finally
{
ms.Dispose();
}
}
/// <summary>
/// A wrapper for FitJpegQualityToFileSize method with path arguments
/// </summary>
public static void FitJpegQualityToFileSize(string srcPath, long maxAllowedSize, string dstPath)
{
using (var srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
using (var dstStream = new FileStream(dstPath, FileMode.Create, FileAccess.Write))
{
FitJpegQualityToFileSize(srcStream, maxAllowedSize, dstStream);
}
}
/// <summary>
/// Tries to encode an image with different JPEG qualities and calls
/// FitDimensionsToFileSize if reducing the quality is not enough
/// </summary>
public static void FitJpegQualityToFileSize(Stream srcStream, long maxAllowedSize, Stream dstStream)
{
const int minQuality = 50;
const int maxQuality = 90;
const int step = 10;
for (int quality = maxQuality; quality >= minQuality; quality -= step)
{
using (var ms = ResizeImageToMemoryStream(srcStream, 1.0f, new JpegSettings(quality)))
{
if (ms.Length < maxAllowedSize)
{
ms.WriteTo(dstStream);
return;
}
}
}
FitDimensionsToFileSize(srcStream, new JpegSettings(minQuality), maxAllowedSize, dstStream);
}
/// <summary>
/// Resizes an image and gets a result as a memory stream
/// </summary>
private static MemoryStream ResizeImageToMemoryStream(Stream stream, float scale, WriterSettings settings)
{
var ms = new MemoryStream();
using (var reader = ImageReader.Create(stream))
using (var writer = ImageWriter.Create(ms, settings))
using (var resize = new Resize((int)(reader.Width * scale), (int)(reader.Height * scale)))
{
Pipeline.Run(reader + resize + writer);
writer.Close();
}
return ms;
}
/// <summary>
/// Formats file size string
/// </summary>
private static string FormatFileSize(long fileSize)
{
return string.Format("{0:0.##} KB", fileSize / 1024);
}
}