-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathPlaceholderHelper.cs
268 lines (234 loc) · 9.04 KB
/
PlaceholderHelper.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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using RLottie;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Threading.Tasks;
using System.Xml.Linq;
using Telegram.Native;
using Telegram.Services;
using Telegram.Td.Api;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace Telegram.Common
{
public static class PlaceholderHelper
{
public static ImageSource GetBitmap(IClientService clientService, PhotoSize photoSize)
{
return GetBitmap(clientService, photoSize.Photo, photoSize.Width, photoSize.Height);
}
public static ImageSource GetBitmap(IClientService clientService, File file, int width, int height)
{
if (file.Local.IsDownloadingCompleted)
{
return UriEx.ToBitmap(file.Local.Path, width, height);
}
else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive && clientService != null)
{
clientService.DownloadFile(file.Id, 1);
}
return null;
}
public static async Task<LoadedImageSurface> LoadBitmapAsync(File file)
{
try
{
var item = await StorageFile.GetFileFromPathAsync(file.Local.Path);
using (var stream = await item.OpenReadAsync())
{
return LoadedImageSurface.StartLoadFromStream(stream);
}
}
catch
{
return null;
}
}
private static readonly DisposableMutex _patternSurfaceLock = new();
public static async Task<LoadedImageSurface> LoadPatternBitmapAsync(File file, double rasterizationScale)
{
using var locked = await _patternSurfaceLock.WaitAsync();
var bitmap = default(LoadedImageSurface);
var scale = (int)(rasterizationScale * 100);
rasterizationScale = 0.25 * rasterizationScale;
var cache = $"{file.Remote.UniqueId}.scale-{scale}.png";
var relative = System.IO.Path.Combine("Wallpapers", cache);
var delete = false;
var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(relative) as StorageFile;
if (item == null)
{
try
{
item = await ApplicationData.Current.LocalFolder.CreateFileAsync(relative, CreationCollisionOption.ReplaceExisting);
using (var stream = await item.OpenAsync(FileAccessMode.ReadWrite))
{
var text = await ProcessSvgXmlAsync(file.Local.Path);
await PlaceholderImageHelper.Current.DrawSvgAsync(text, Colors.White, stream, rasterizationScale);
bitmap = LoadedImageSurface.StartLoadFromStream(stream, new Size(360, 740));
}
}
catch
{
delete = true;
}
}
else
{
try
{
using (var stream = await item.OpenReadAsync())
{
bitmap = LoadedImageSurface.StartLoadFromStream(stream, new Size(360, 740));
}
}
catch
{
delete = true;
}
}
if (item != null && delete)
{
try
{
await item.DeleteAsync();
}
catch
{
// Shit happens
}
}
return bitmap;
}
private static async Task<string> ProcessSvgXmlAsync(string filePath)
{
var styles = new Dictionary<string, string>();
var text = string.Empty;
using (var source = System.IO.File.OpenRead(filePath))
using (var decompress = new GZipStream(source, CompressionMode.Decompress))
using (var reader = new System.IO.StreamReader(decompress))
{
text = await reader.ReadToEndAsync();
}
var document = XDocument.Parse(text);
var svg = XNamespace.Get("http://www.w3.org/2000/svg");
foreach (var styleNode in document.Root.Descendants(svg + "style"))
{
var currentStyleString = styleNode.Value;
int currentClassNameStartIndex = -1;
int currentClassContentsStartIndex = -1;
string currentClassName = null;
for (int i = 0; i < currentStyleString.Length; i++)
{
var c = currentStyleString[i];
if (currentClassNameStartIndex != -1)
{
if (!char.IsLetterOrDigit(c))
{
currentClassName = currentStyleString.Substring(currentClassNameStartIndex, i - currentClassNameStartIndex);
currentClassNameStartIndex = -1;
}
}
else if (currentClassContentsStartIndex != -1)
{
if (c == '}')
{
var classContents = currentStyleString.Substring(currentClassContentsStartIndex, i - currentClassContentsStartIndex);
if (currentClassName != null && classContents != null)
{
styles[currentClassName] = classContents;
currentClassName = null;
}
currentClassContentsStartIndex = -1;
}
}
if (currentClassNameStartIndex == -1 && currentClassContentsStartIndex == -1)
{
if (c == '.')
{
currentClassNameStartIndex = i + 1;
}
else if (c == '{')
{
currentClassContentsStartIndex = i + 1;
}
}
}
}
foreach (var styleName in styles)
{
text = text.Replace($"class=\"{styleName.Key}\"", $"style=\"{styleName.Value}\"");
}
return text;
}
public static async void GetBlurred(BitmapImage bitmap, string path, float amount = 3)
{
using (var stream = new InMemoryRandomAccessStream())
{
try
{
await Task.Run(() => PlaceholderImageHelper.Current.DrawThumbnailPlaceholder(path, amount, stream));
await bitmap.SetSourceAsync(stream);
}
catch { }
}
}
public static async void GetBlurred(BitmapImage bitmap, IList<byte> bytes, float amount = 3)
{
using (var stream = new InMemoryRandomAccessStream())
{
try
{
await Task.Run(() => PlaceholderImageHelper.Current.DrawThumbnailPlaceholder(bytes, amount, stream));
await bitmap.SetSourceAsync(stream);
}
catch { }
}
}
public static ImageSource GetWebPFrame(string path, double maxWidth = 512)
{
try
{
var buffer = PlaceholderImageHelper.DrawWebP(path, (int)maxWidth, out int pixelWidth, out int pixelHeight);
if (pixelWidth > 0 && pixelHeight > 0)
{
var bitmap = new WriteableBitmap(pixelWidth, pixelHeight);
BufferSurface.Copy(buffer, bitmap.PixelBuffer);
return bitmap;
}
else
{
return UriEx.ToBitmap(path);
}
}
catch { }
return null;
}
public static ImageSource GetLottieFrame(string path, int frame, int width, int height, bool webp = true)
{
// Frame size affects disk cache, so we always use 256.
var animation = LottieAnimation.LoadFromFile(path, width, height, false, null);
if (animation == null)
{
if (webp)
{
return GetWebPFrame(path, width);
}
return null;
}
var bitmap = new WriteableBitmap(width, height);
animation.RenderSync(bitmap.PixelBuffer, frame);
animation.Dispose();
return bitmap;
}
}
}