-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase64FileType.cs
537 lines (463 loc) · 19.9 KB
/
Base64FileType.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
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
/////////////////////////////////////////////////////////////////////////////////
//
// Base64 FileType Plugin for Paint.NET
//
// This software is provided under the MIT License:
// Copyright (c) 2013-2018, 2020, 2021, 2022 Nicholas Hayes
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
// Portions of this file has been adapted from:
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See src/Resources/Files/License.txt for full licensing and attribution //
// details. //
// . //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using PaintDotNet;
using System.Text;
using System.Globalization;
namespace Base64FileTypePlugin
{
public sealed class Base64FileType : FileType
{
private const string DataURIFormat = "data:image/{0};base64,";
private static readonly string[] ImageFormats = new string[] { "bmp", "png", "jpeg", "gif" };
// The end markers for unquoted base64 data in a HTML img tag.
private static readonly string[] HtmlDataEndMarkers = new[]
{
" ", // The space separating the HTML tag attributes: <img src=<base64 data> alt="..."/>
"/>", // The HTML end-of-tag marker: <img src=<base64 data>/>
};
public Base64FileType() : base("Base64",
FileTypeFlags.SupportsSaving | FileTypeFlags.SupportsLoading,
new string[] { ".b64" })
{
}
private enum SavableBitDepths
{
Rgba32, // 2^24 colors, plus a full 8-bit alpha channel
Rgb24, // 2^24 colors
Rgb8, // 256 colors
Rgba8 // 255 colors + 1 transparent
}
protected override SaveConfigToken OnCreateDefaultSaveConfigToken()
{
return new Base64SaveConfigToken(FileFormat.Png,
string.Empty,
false,
false,
UriDataType.None,
string.Empty,
new CssTokenData());
}
public override SaveConfigWidget CreateSaveConfigWidget()
{
return new Base64SaveConfigWidget();
}
protected override Document OnLoad(Stream input)
{
using (StreamReader sr = new StreamReader(input, Encoding.UTF8))
{
string data = sr.ReadToEnd();
bool hasEndChar = false;
char endChar = '\0';
for (int i = 0; i < ImageFormats.Length; i++)
{
string dataFormat = string.Format(CultureInfo.InvariantCulture, DataURIFormat, ImageFormats[i]);
int dataStartIndex = data.IndexOf(dataFormat, StringComparison.OrdinalIgnoreCase);
if (dataStartIndex >= 0)
{
if (dataStartIndex > 0)
{
char startChar = data[dataStartIndex - 1];
if (startChar == '\'' || startChar == '"')
{
// The base 64 data can optionally be surrounded by single or double quotes.
hasEndChar = true;
endChar = startChar;
}
else if (startChar == '(')
{
// In the CSS format the unquoted base 64 data is enclosed in
// parentheses, e.g. url(<base64 data>)
hasEndChar = true;
endChar = ')';
}
}
data = data.Remove(0, dataFormat.Length + dataStartIndex);
break;
}
}
if (hasEndChar)
{
int endIndex = data.IndexOf(endChar);
if (endIndex >= 0)
{
data = data.Remove(endIndex, data.Length - endIndex).TrimEnd();
}
}
else
{
for (int i = 0; i < HtmlDataEndMarkers.Length; i++)
{
int uriEndIndex = data.IndexOf(HtmlDataEndMarkers[i], StringComparison.OrdinalIgnoreCase);
if (uriEndIndex >= 0)
{
if (uriEndIndex != data.Length)
{
// Remove any trailing characters.
data = data.Remove(uriEndIndex, data.Length - uriEndIndex).TrimEnd();
}
break;
}
}
}
byte[] bytes = Convert.FromBase64String(data);
using (MemoryStream stream = new MemoryStream(bytes))
{
using (Image image = Image.FromStream(stream))
{
return Document.FromImage(image);
}
}
}
}
private static EncoderParameters SetEncodeParameters(SavableBitDepths bitDepth)
{
int colorDepth = 0;
switch (bitDepth)
{
case SavableBitDepths.Rgba32:
colorDepth = 32;
break;
case SavableBitDepths.Rgb24:
colorDepth = 24;
break;
case SavableBitDepths.Rgb8:
case SavableBitDepths.Rgba8:
colorDepth = 8;
break;
}
EncoderParameters encodeOptions = new EncoderParameters(1);
try
{
encodeOptions.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth,
colorDepth);
}
catch (Exception)
{
encodeOptions.Dispose();
throw;
}
return encodeOptions;
}
private static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo icf in encoders)
{
if (icf.FormatID == format.Guid)
{
return icf;
}
}
return null;
}
private static unsafe void SquishSurfaceTo24Bpp(Surface surface)
{
byte* dst = (byte*)surface.GetRowAddress(0);
int byteWidth = surface.Width * 3;
int stride24bpp = ((byteWidth + 3) / 4) * 4; // round up to multiple of 4
int delta = stride24bpp - byteWidth;
for (int y = 0; y < surface.Height; ++y)
{
ColorBgra* src = surface.GetRowAddress(y);
ColorBgra* srcEnd = src + surface.Width;
while (src < srcEnd)
{
dst[0] = src->B;
dst[1] = src->G;
dst[2] = src->R;
++src;
dst += 3;
}
dst += delta;
}
}
private static unsafe Bitmap CreateAliased24BppBitmap(Surface surface)
{
int stride = surface.Width * 3;
int realStride = ((stride + 3) / 4) * 4; // round up to multiple of 4
return new Bitmap(surface.Width,
surface.Height,
realStride,
PixelFormat.Format24bppRgb,
new IntPtr(surface.Scan0.VoidStar));
}
private static unsafe void Analyze(Surface scratchSurface,
out bool allOpaque,
out bool all0or255Alpha,
out int uniqueColorCount)
{
allOpaque = true;
all0or255Alpha = true;
HashSet<ColorBgra> uniqueColors = new HashSet<ColorBgra>();
for (int y = 0; y < scratchSurface.Height; ++y)
{
ColorBgra* srcPtr = scratchSurface.GetRowAddress(y);
ColorBgra* endPtr = srcPtr + scratchSurface.Width;
while (srcPtr < endPtr)
{
ColorBgra p = *srcPtr;
if (p.A != 255)
{
allOpaque = false;
}
if (p.A > 0 && p.A < 255)
{
all0or255Alpha = false;
}
if (p.A == 255 && !uniqueColors.Contains(p) && uniqueColors.Count < 300)
{
uniqueColors.Add(*srcPtr);
}
++srcPtr;
}
}
uniqueColorCount = uniqueColors.Count;
}
private long GetSavedFileLength(Surface scratchSurface, ImageFormat format, SavableBitDepths bitDepth)
{
long size = 0;
using (MemoryStream ms = new MemoryStream())
{
switch (bitDepth)
{
case SavableBitDepths.Rgba32:
using (Bitmap temp = scratchSurface.CreateAliasedBitmap())
{
temp.Save(ms, format);
}
break;
case SavableBitDepths.Rgb24:
SquishSurfaceTo24Bpp(scratchSurface);
using (Bitmap temp = CreateAliased24BppBitmap(scratchSurface))
{
temp.Save(ms, format);
}
break;
case SavableBitDepths.Rgb8:
case SavableBitDepths.Rgba8:
using (Bitmap temp = Quantize(scratchSurface,
7,
256,
bitDepth == SavableBitDepths.Rgba8,
null))
{
temp.Save(ms, format);
}
break;
}
size = ms.Length;
}
return size;
}
private SavableBitDepths ChooseBestBitDepth(Document input, Surface scratchSurface, ImageFormat format)
{
scratchSurface.Clear(ColorBgra.Transparent);
input.Flatten(scratchSurface);
Analyze(scratchSurface, out bool allOpaque, out bool all0or255Alpha, out int uniqueColorCount);
HashSet<SavableBitDepths> allowedBitDepths = new HashSet<SavableBitDepths>();
if (allOpaque || format == ImageFormat.Bmp)
{
allowedBitDepths.Add(SavableBitDepths.Rgb24);
if (uniqueColorCount <= 256)
{
allowedBitDepths.Add(SavableBitDepths.Rgb8);
}
}
else
{
allowedBitDepths.Add(SavableBitDepths.Rgba32);
if (all0or255Alpha && uniqueColorCount <= 255)
{
allowedBitDepths.Add(SavableBitDepths.Rgba8);
}
}
SavableBitDepths bestBitDepth;
if (allowedBitDepths.Count == 1)
{
bestBitDepth = allowedBitDepths.First();
}
else
{
long totalPixelSize = scratchSurface.Width * scratchSurface.Height;
if (allowedBitDepths.SetEquals(new SavableBitDepths[] { SavableBitDepths.Rgb8, SavableBitDepths.Rgb24 })
&& totalPixelSize <= 65536)
{
long rgb8Length;
long rgb24Length;
try
{
rgb8Length = GetSavedFileLength(scratchSurface, format, SavableBitDepths.Rgb8);
rgb24Length = GetSavedFileLength(scratchSurface, format, SavableBitDepths.Rgb24);
}
catch (OutOfMemoryException)
{
return SavableBitDepths.Rgb8;
}
bestBitDepth = rgb8Length <= rgb24Length ? SavableBitDepths.Rgb8 : SavableBitDepths.Rgb24;
}
else if (allowedBitDepths.SetEquals(new SavableBitDepths[] { SavableBitDepths.Rgba8, SavableBitDepths.Rgba32 })
&& totalPixelSize <= 65536)
{
long rgba8Length;
long rgba32Length;
try
{
rgba8Length = GetSavedFileLength(scratchSurface, ImageFormat.Png, SavableBitDepths.Rgb8);
rgba32Length = GetSavedFileLength(scratchSurface, ImageFormat.Png, SavableBitDepths.Rgba32);
}
catch (OutOfMemoryException)
{
return SavableBitDepths.Rgba8;
}
bestBitDepth = rgba8Length <= rgba32Length ? SavableBitDepths.Rgba8 : SavableBitDepths.Rgba32;
}
else if (allowedBitDepths.Contains(SavableBitDepths.Rgb8))
{
bestBitDepth = SavableBitDepths.Rgb8;
}
else if (allowedBitDepths.Contains(SavableBitDepths.Rgba8))
{
bestBitDepth = SavableBitDepths.Rgba8;
}
else if (allowedBitDepths.Contains(SavableBitDepths.Rgb24))
{
bestBitDepth = SavableBitDepths.Rgb24;
}
else
{
bestBitDepth = SavableBitDepths.Rgba32;
}
}
return bestBitDepth;
}
protected override void OnSave(Document input,
Stream output,
SaveConfigToken token,
Surface scratchSurface,
ProgressEventHandler progressCallback)
{
Base64SaveConfigToken configToken = (Base64SaveConfigToken)token;
ImageFormat gdipFormat = null;
switch (configToken.ImageType)
{
case FileFormat.Bmp:
gdipFormat = ImageFormat.Bmp;
break;
case FileFormat.Png:
gdipFormat = ImageFormat.Png;
break;
default:
throw new InvalidEnumArgumentException("configToken.ImageType", (int)configToken.ImageType, typeof(FileFormat));
}
SavableBitDepths bitDepth = ChooseBestBitDepth(input, scratchSurface, gdipFormat);
scratchSurface.Clear(ColorBgra.Transparent);
input.Flatten(scratchSurface);
// if bit depth is 24 or 8, then we have to do away with the alpha channel
// for 8-bit, we must have pixels that have either 0 or 255 alpha
if (bitDepth == SavableBitDepths.Rgb8 ||
bitDepth == SavableBitDepths.Rgba8 ||
bitDepth == SavableBitDepths.Rgb24)
{
UserBlendOps.NormalBlendOp blendOp = new UserBlendOps.NormalBlendOp();
unsafe
{
for (int y = 0; y < scratchSurface.Height; ++y)
{
ColorBgra* ptr = scratchSurface.GetRowAddressUnchecked(y);
ColorBgra* endPtr = ptr + scratchSurface.Width;
while (ptr < endPtr)
{
if (ptr->A < 128 && bitDepth == SavableBitDepths.Rgba8)
{
ptr->Bgra = 0;
}
else
{
*ptr = blendOp.Apply(ColorBgra.White, *ptr);
}
ptr++;
}
}
}
}
ImageCodecInfo codecInfo = GetImageCodecInfo(gdipFormat);
string base64 = null;
using (MemoryStream ms = new MemoryStream())
{
EncoderParameters encoderParams = SetEncodeParameters(bitDepth);
switch (bitDepth)
{
case SavableBitDepths.Rgba32:
using (Bitmap temp = scratchSurface.CreateAliasedBitmap())
{
temp.Save(ms, codecInfo, encoderParams);
}
break;
case SavableBitDepths.Rgb24:
SquishSurfaceTo24Bpp(scratchSurface);
using (Bitmap temp = CreateAliased24BppBitmap(scratchSurface))
{
temp.Save(ms, codecInfo, encoderParams);
}
break;
case SavableBitDepths.Rgb8:
case SavableBitDepths.Rgba8:
using (Bitmap temp = Quantize(scratchSurface, 7, 256, bitDepth == SavableBitDepths.Rgba8, progressCallback))
{
temp.Save(ms, codecInfo, encoderParams);
}
break;
}
Base64FormattingOptions options = configToken.LineBreaks ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None;
base64 = Convert.ToBase64String(ms.GetBuffer(), options);
}
byte[] encodedBytes = null;
if (configToken.UriEncode)
{
string uriEncodedText;
switch (configToken.DataType)
{
case UriDataType.None:
case UriDataType.Html:
uriEncodedText = string.Format(CultureInfo.InvariantCulture, configToken.Base64Format, codecInfo.MimeType, base64);
break;
case UriDataType.Css:
uriEncodedText = configToken.CssData.ToString(codecInfo.MimeType, base64);
break;
default:
throw new InvalidEnumArgumentException("configToken.DataType", (int)configToken.DataType, typeof(UriDataType));
}
encodedBytes = Encoding.UTF8.GetBytes(uriEncodedText);
}
else
{
encodedBytes = Encoding.UTF8.GetBytes(base64);
}
output.Write(encodedBytes, 0, encodedBytes.Length);
}
}
}