This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
/
WriteableBitmapExtensions.cs
369 lines (309 loc) · 16.7 KB
/
WriteableBitmapExtensions.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
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
// ****************************************************************************
// <copyright file="WriteableBitmapExtensions.cs" company="Pedro Lamas">
// Copyright © Pedro Lamas 2013
// </copyright>
// ****************************************************************************
// <author>Pedro Lamas</author>
// <email>pedrolamas@gmail.com</email>
// <date>28-10-2013</date>
// <project>Cimbalino.Phone.Toolkit.Background</project>
// <web>http://www.pedrolamas.com</web>
// <license>
// See license.txt in this solution or http://www.pedrolamas.com/license_MIT.txt
// </license>
// ****************************************************************************
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Cimbalino.Phone.Toolkit.Compression;
using Cimbalino.Phone.Toolkit.Helpers;
namespace Cimbalino.Phone.Toolkit.Extensions
{
/// <summary>
/// Provides a set of static (Shared in Visual Basic) methods for <see cref="WriteableBitmap"/> instances.
/// </summary>
public static class WriteableBitmapExtensions
{
private const string PngChunkTypeHeader = "IHDR";
private const string PngChunkTypePhysical = "pHYs";
private const string PngChunkTypeGamma = "gAMA";
private const string PngChunkTypeData = "IDAT";
private const string PngChunkTypeEnd = "IEND";
private const byte PngHeaderBitDepth = 8;
private const byte PngHeaderColorType = 6;
private const byte PngHeaderCompressionMethod = 0;
private const byte PngHeaderFilterMethod = 0;
private const byte PngHeaderInterlaceMethod = 0;
private const int MaximumChunkSize = 0xFFFF;
private const double MetreToInch = 39.3700787d;
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
public static void SavePng(this WriteableBitmap writeableBitmap, Stream outputStream)
{
SavePng(writeableBitmap, outputStream, new WriteableBitmapSavePngParameters());
}
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream, using the specified output compression.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="compressionLevel">The image compression level.</param>
public static void SavePng(this WriteableBitmap writeableBitmap, Stream outputStream, CompressionLevel compressionLevel)
{
SavePng(writeableBitmap, outputStream, new WriteableBitmapSavePngParameters { CompressionLevel = compressionLevel });
}
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream, using the specified output <see cref="WriteableBitmapSavePngParameters"/>.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="parameters">The image save parameters.</param>
public static void SavePng(this WriteableBitmap writeableBitmap, Stream outputStream, WriteableBitmapSavePngParameters parameters)
{
WriteHeader(outputStream, writeableBitmap);
WritePhysics(outputStream, parameters);
WriteGamma(outputStream, parameters);
WriteDataChunks(outputStream, writeableBitmap, parameters);
WriteFooter(outputStream);
outputStream.Flush();
}
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SavePngAsync(this WriteableBitmap writeableBitmap, Stream outputStream)
{
return Task.Factory.StartNew(() =>
{
writeableBitmap.SavePng(outputStream);
});
}
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream, using the specified output compression.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="compressionLevel">The image compression level.</param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SavePngAsync(this WriteableBitmap writeableBitmap, Stream outputStream, CompressionLevel compressionLevel)
{
return Task.Factory.StartNew(() =>
{
writeableBitmap.SavePng(outputStream, compressionLevel);
});
}
/// <summary>
/// Encodes a WriteableBitmap object into a PNG stream, using the specified output <see cref="WriteableBitmapSavePngParameters"/>.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="parameters">The image save parameters.</param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SavePngAsync(this WriteableBitmap writeableBitmap, Stream outputStream, WriteableBitmapSavePngParameters parameters)
{
return Task.Factory.StartNew(() =>
{
writeableBitmap.SavePng(outputStream, parameters);
});
}
/// <summary>
/// Encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target quality of the JPEG file.
/// </summary>
/// <param name="writeableBitmap">The WriteableBitmap object.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="quality">This parameter represents the quality of the JPEG photo with a range between 0 and 100, with 100 being the best photo quality. We recommend that you do not fall lower than a value of 70. because JPEG picture quality diminishes significantly below that level. </param>
public static void SaveJpeg(this WriteableBitmap writeableBitmap, Stream outputStream, int quality)
{
writeableBitmap.SaveJpeg(outputStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, quality);
}
/// <summary>
/// Encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target width, height, and quality of the JPEG file.
/// </summary>
/// <param name="writeableBitmap">The WriteableBitmap object.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="targetWidth">The target width of the file.</param>
/// <param name="targetHeight">The target height of the file.</param>
/// <param name="quality">This parameter represents the quality of the JPEG photo with a range between 0 and 100, with 100 being the best photo quality. We recommend that you do not fall lower than a value of 70. because JPEG picture quality diminishes significantly below that level. </param>
public static void SaveJpeg(this WriteableBitmap writeableBitmap, Stream outputStream, int targetWidth, int targetHeight, int quality)
{
writeableBitmap.SaveJpeg(outputStream, targetWidth, targetHeight, 0, quality);
}
/// <summary>
/// Encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target quality of the JPEG file.
/// </summary>
/// <param name="writeableBitmap">The WriteableBitmap object.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="quality">This parameter represents the quality of the JPEG photo with a range between 0 and 100, with 100 being the best photo quality. We recommend that you do not fall lower than a value of 70. because JPEG picture quality diminishes significantly below that level. </param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SaveJpegAsync(this WriteableBitmap writeableBitmap, Stream outputStream, int quality)
{
return writeableBitmap.SaveJpegAsync(outputStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, quality);
}
/// <summary>
/// Encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target width, height, and quality of the JPEG file.
/// </summary>
/// <param name="writeableBitmap">The WriteableBitmap object.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="targetWidth">The target width of the file.</param>
/// <param name="targetHeight">The target height of the file.</param>
/// <param name="quality">This parameter represents the quality of the JPEG photo with a range between 0 and 100, with 100 being the best photo quality. We recommend that you do not fall lower than a value of 70. because JPEG picture quality diminishes significantly below that level. </param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SaveJpegAsync(this WriteableBitmap writeableBitmap, Stream outputStream, int targetWidth, int targetHeight, int quality)
{
return writeableBitmap.SaveJpegAsync(outputStream, targetWidth, targetHeight, 0, quality);
}
/// <summary>
/// Encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target width, height, orientation, and quality of the JPEG file.
/// </summary>
/// <param name="writeableBitmap">The WriteableBitmap object.</param>
/// <param name="outputStream">The image data stream.</param>
/// <param name="targetWidth">The target width of the file.</param>
/// <param name="targetHeight">The target height of the file.</param>
/// <param name="orientation">This parameter is not currently used by this method. Use a value of 0 as a placeholder.</param>
/// <param name="quality">This parameter represents the quality of the JPEG photo with a range between 0 and 100, with 100 being the best photo quality. We recommend that you do not fall lower than a value of 70. because JPEG picture quality diminishes significantly below that level. </param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public static Task SaveJpegAsync(this WriteableBitmap writeableBitmap, Stream outputStream, int targetWidth, int targetHeight, int orientation, int quality)
{
return Task.Factory.StartNew(() =>
{
writeableBitmap.SaveJpeg(outputStream, targetWidth, targetHeight, orientation, quality);
});
}
private static void WriteHeader(Stream outputStream, WriteableBitmap writeableBitmap)
{
outputStream.Write(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, 0, 8);
var chunkData = CreateChunk(
writeableBitmap.PixelWidth,
writeableBitmap.PixelHeight,
PngHeaderBitDepth,
PngHeaderColorType,
PngHeaderCompressionMethod,
PngHeaderFilterMethod,
PngHeaderInterlaceMethod);
WriteChunk(outputStream, PngChunkTypeHeader, chunkData);
}
private static void WritePhysics(Stream outputStream, WriteableBitmapSavePngParameters parameters)
{
if (parameters.SaveResolution)
{
var dpmX = (int)Math.Round(parameters.HorizontalResolution * MetreToInch);
var dpmY = (int)Math.Round(parameters.VerticalResolution * MetreToInch);
var chunkData = CreateChunk(dpmX, dpmY, (byte)1);
WriteChunk(outputStream, PngChunkTypePhysical, chunkData);
}
}
private static void WriteGamma(Stream outputStream, WriteableBitmapSavePngParameters parameters)
{
if (parameters.SaveGamma)
{
var gammaValue = (int)(parameters.Gamma * 100000f);
var chunkData = CreateChunk(gammaValue);
WriteChunk(outputStream, PngChunkTypeGamma, chunkData);
}
}
private static void WriteDataChunks(Stream outputStream, WriteableBitmap writeableBitmap, WriteableBitmapSavePngParameters parameters)
{
using (var chunkedStream = new ChunkedStream(MaximumChunkSize, data => WriteChunk(outputStream, PngChunkTypeData, data)))
{
using (var zlibStream = new ZlibStream(chunkedStream, CompressionMode.Compress, parameters.CompressionLevel, true))
{
var pixels = writeableBitmap.Pixels;
var width = writeableBitmap.PixelWidth;
var height = writeableBitmap.PixelHeight;
var index = 0;
var dataRowLength = width * 4;
var dataRow = new byte[dataRowLength];
for (var y = 0; y < height; y++)
{
zlibStream.WriteByte(0);
for (var x = 0; x < width; x++)
{
var color = pixels[index++];
var alpha = (byte)(color >> 24);
int alphaInt = alpha;
if (alphaInt == 0)
{
alphaInt = 1;
}
alphaInt = (255 << 8) / alphaInt;
var dataRowOffset = x * 4;
dataRow[dataRowOffset] = (byte)((((color >> 16) & 0xFF) * alphaInt) >> 8);
dataRow[dataRowOffset + 1] = (byte)((((color >> 8) & 0xFF) * alphaInt) >> 8);
dataRow[dataRowOffset + 2] = (byte)(((color & 0xFF) * alphaInt) >> 8);
dataRow[dataRowOffset + 3] = alpha;
}
zlibStream.Write(dataRow, 0, dataRowLength);
}
}
}
}
private static void WriteFooter(Stream outputStream)
{
WriteChunk(outputStream, PngChunkTypeEnd, null);
}
private static void WriteChunk(Stream outputStream, string type, byte[] data)
{
var length = data != null ? data.Length : 0;
outputStream.Write(CreateChunkFromInt(length), 0, 4);
using (var crc32 = new CrcCalculatorStream(outputStream, true))
{
crc32.Write(CreateChunkFromString(type), 0, 4);
if (data != null)
{
crc32.Write(data, 0, length);
}
crc32.Flush();
outputStream.Write(CreateChunkFromUint((uint)crc32.Crc), 0, 4);
}
}
private static byte[] CreateChunk(params object[] paramObjects)
{
return paramObjects.SelectMany(x =>
{
if (x is byte)
{
return new[] { (byte)x };
}
if (x is int)
{
return CreateChunkFromInt((int)x);
}
if (x is uint)
{
return CreateChunkFromUint((uint)x);
}
var stringValue = x as string;
if (stringValue != null)
{
return CreateChunkFromString(stringValue);
}
throw new ArgumentException();
}).ToArray();
}
private static byte[] CreateChunkFromInt(int value)
{
var dataChunk = BitConverter.GetBytes(value);
Array.Reverse(dataChunk);
return dataChunk;
}
private static byte[] CreateChunkFromUint(uint value)
{
var dataChunk = BitConverter.GetBytes(value);
Array.Reverse(dataChunk);
return dataChunk;
}
private static byte[] CreateChunkFromString(string value)
{
return value
.Select(x => (byte)x)
.ToArray();
}
}
}