Repository navigation
Expand file tree
/
Copy pathImageRecolor.cs
More file actions
320 lines (301 loc) · 13.9 KB
/
Copy pathImageRecolor.cs
File metadata and controls
320 lines (301 loc) · 13.9 KB
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Monocle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Celeste.Mod.BetterRefillGemsPlus
{
public static class ImageRecolor
{
static int unique = 0;
[Obsolete]
static Dictionary<(Atlas atlas, string path), Task<MTexture>> result = [];
static List<DynamicVirtualTexture> allocated = [];
public static void Clear()
{
var o = allocated;
resultcomplete.Clear();
allocated = [];
foreach (var i in o)
{
i.Dispose();
}
o.Clear();
}
static Dictionary<(Atlas atlas, string path), MTexture> resultcomplete = [];
[Obsolete]
public static MTexture MTextureDrawOutline(MTexture mtex)
{
VirtualTexture? nvtex = null;
try
{
var orig = mtex.Texture.Texture_Safe;
var rect = mtex.ClipRect;
var fmt = orig.Format;
//var sin = stackalloc byte[16];
//BetterRefillGemsPlusModule.loadimmediately.TryAdd(Environment.CurrentManagedThreadId, null);
nvtex = new VirtualTexture(Environment.CurrentManagedThreadId.ToString() + nameof(BetterRefillGemsPlus) + (++unique).ToString(), rect.Width, rect.Height, Color.Transparent);
bool outrange(int v1, int v2)
{
return !(v1 >= 0 && v1 < rect.Height && v2 < rect.Width && v2 >= 0);
}
void calcTyped<T>(T sin, T transparent) where T : struct
{
T[] b = new T[1 * rect.Width * rect.Height];
orig.GetData(0, rect, b, 0, b.Length);
for (int i = 0; i < rect.Height; i++)
{
for (int j = 0; j < rect.Width; j++)
{
if (!transparent.Equals(b[((i * rect.Width + j) * 1)]))
{
if
(
(outrange(i + 1, j) || transparent.Equals(b[(((i + 1) * rect.Width + j) * 1)])) ||
(outrange(i, j - 1) || transparent.Equals(b[(((i) * rect.Width + j - 1) * 1)])) ||
(outrange(i - 1, j) || transparent.Equals(b[(((i - 1) * rect.Width + j) * 1)])) ||
(outrange(i, j + 1) || transparent.Equals(b[(((i) * rect.Width + j + 1) * 1)]))
)
{
int k = 0;
{
b[(i * rect.Width + j) * 1 + k] = sin;
}
}
}
}
}
nvtex.Texture_Safe.SetData(b);
}
void calcBigEndian(byte[] sin, byte[] transparent)
{
sin = sin.Reverse().ToArray();
transparent = transparent.Reverse().ToArray();
var size = sin.Length;
byte[] b = new byte[size * rect.Width * rect.Height];
orig.GetData(0, rect, b, 0, b.Length);
for (int i = 0; i < rect.Height; i++)
{
for (int j = 0; j < rect.Width; j++)
{
if (!transparent.SequenceEqual(b.Skip((i * rect.Width + j) * size).Take(size)))
{
if
(//trust jit.
(outrange(i + 1, j) || transparent.SequenceEqual(b.Skip(((i + 1) * rect.Width + j) * size).Take(size))) ||
(outrange(i, j - 1) || transparent.SequenceEqual(b.Skip(((i) * rect.Width + j - 1) * size).Take(size))) ||
(outrange(i - 1, j) || transparent.SequenceEqual(b.Skip(((i - 1) * rect.Width + j) * size).Take(size))) ||
(outrange(i, j + 1) || transparent.SequenceEqual(b.Skip(((i) * rect.Width + j + 1) * size).Take(size)))
)
{
for (int k = 0; k < size; k++)
{
b[(i * rect.Width + j) * size + k] = sin[k];
}
}
}
}
}
nvtex.Texture_Safe.SetData(b);
}
Color col = new(255, 41, 41, 255);
Color trans = new(0, 0, 0, 0);
if (fmt != Microsoft.Xna.Framework.Graphics.SurfaceFormat.Color)
{
Logger.Log(LogLevel.Error, nameof(BetterRefillGemsPlus), "Looks loke you have found a image in different format. Send it to me so I could add support for it.\n"
+ $"Metxture:{mtex.Atlas.DataPath} {mtex.AtlasPath}");
}
switch (fmt)
{
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Color:
calcTyped(col, trans);
break;
//todo: I don't have image in below format.
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Vector4:
{
calcTyped(col.ToVector4(), trans.ToVector4());
}
break;
//omg they're all big endian
//oh, just reverse them in func
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Bgra4444:
calcBigEndian([0, 255], [0, 0]);
break;
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Bgra5551:
calcBigEndian([0, 63], [0, 0]);
break;
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.ColorBgraEXT:
calcBigEndian([0, 0, 255, 255], [0, 0, 0, 0]);
break;
//case Microsoft.Xna.Framework.Graphics.SurfaceFormat.ColorSrgbEXT:
// //What's this?
// break;
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.HalfVector4:
var oh = BitConverter.HalfToUInt16Bits((Half)1.0);
var ohb = BitConverter.GetBytes(oh);
var zh = BitConverter.HalfToUInt16Bits((Half)0.0);
var zhb = BitConverter.GetBytes(zh);
calcBigEndian([.. ohb, .. ohb, .. zhb, .. zhb], [.. zhb, .. zhb, .. zhb, .. zhb,]);
break;
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Rgba1010102:
calcBigEndian([0, 0, 15, 255], [0, 0, 0, 0,]);
break;
case Microsoft.Xna.Framework.Graphics.SurfaceFormat.Rgba64:
calcBigEndian([255, 255, 0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 0, 0, 0, 0,]);
break;
default:
Logger.Log(LogLevel.Warn, nameof(BetterRefillGemsPlus), $"failed when loading {mtex.Atlas.DataPath} {mtex.AtlasPath} . unsupported surface format {fmt}.");
return mtex;
}
return new MTexture(nvtex)
{
Atlas = mtex.Atlas,
AtlasPath = mtex.AtlasPath,
Center = mtex.Center,
DrawOffset = mtex.DrawOffset,
ScaleFix = mtex.ScaleFix,
Width = mtex.Width,
Height = mtex.Height,
};
}
catch (Exception ex)
{
nvtex?.Dispose();
Logger.Log(LogLevel.Error, nameof(BetterRefillGemsPlus), $"Something went wrong when trying to create {mtex.Atlas.DataPath} {mtex.AtlasPath} .\nException: {ex.Message}\n{ex.StackTrace}"); ;
return mtex;
}
finally
{
//BetterRefillGemsPlusModule.loadimmediately.TryRemove(Environment.CurrentManagedThreadId, out _);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MTexture[] MTextureDrawOutlineBatch(IEnumerable<MTexture> mtex, bool samesize)
{
DynamicVirtualTexture? nvtex = null;
RenderTarget2D? newtex = null;
try
{
//var rect = mtex.First().ClipRect;
//int width = rect.Width;
//int height = rect.Height;
int maxWidth;
int totalHeight;
//if (!samesize)
//{
maxWidth = mtex.Select(x => x.ClipRect.Width).Max();
totalHeight = mtex.Select(y => y.ClipRect.Height).Sum();
//}
newtex = new(Engine.Instance.GraphicsDevice, maxWidth, totalHeight);
nvtex = new(Environment.CurrentManagedThreadId.ToString() + nameof(BetterRefillGemsPlus) + (++unique).ToString(),
newtex);
_ = nvtex.Texture_Safe;
nvtex.Texture_Safe = newtex;
nvtex.Width = maxWidth;
nvtex.Height = totalHeight;
Engine.Instance.GraphicsDevice.SetRenderTarget(newtex);
var full = new MTexture(nvtex);
Effect outlineEffect = BetterRefillGemsPlusModule.Instance.Outline;
EffectParameter effectParameter = outlineEffect.Parameters["size"];
//effectParameter.SetValue(new Vector2(width, height));
List<MTexture> result = [];
Draw.SpriteBatch.Begin(
samesize ? SpriteSortMode.Deferred : SpriteSortMode.Immediate,
BlendState.Opaque,
SamplerState.PointClamp,
DepthStencilState.Default,
RasterizerState.CullNone,
outlineEffect);
try
{
int i = 0;
foreach (var tex in mtex)
{
int h = tex.ClipRect.Height;
var tex_safe = tex.Texture.Texture_Safe;
//if (!samesize)
{
effectParameter.SetValue(new Vector2(tex_safe.Width, tex_safe.Height));
}
Draw.SpriteBatch.Draw(tex.Texture.Texture_Safe, new Vector2(0, i), tex.ClipRect, BetterRefillGemsPlusModule.Settings.OutlineColor);
var m = new MTexture(full, new Rectangle(0, i, tex.ClipRect.Width, tex.ClipRect.Height))
{
Atlas = tex.Atlas,
AtlasPath = tex.AtlasPath,
Center = tex.Center,
DrawOffset = tex.DrawOffset,
ScaleFix = tex.ScaleFix,
Width = tex.Width,
Height = tex.Height,
};
result.Add(m);
i += h;
}
}
finally
{
try
{
Draw.SpriteBatch.End();
}
catch { }
}
allocated.Add(nvtex);
return result.ToArray();
}
catch (Exception ex)
{
nvtex?.Dispose();
newtex?.Dispose();
Logger.Log(LogLevel.Error, nameof(BetterRefillGemsPlus), $"Something went wrong when trying to create {mtex.First().Atlas.DataPath} {mtex.First().AtlasPath} .\nException: {ex.Message}\n{ex.StackTrace}"); ;
return mtex.ToArray();
}
}
public static MTexture DrawOutlineSingle(string s, Atlas atlas = null)
{
atlas ??= GFX.Game;
return MTextureDrawOutlineBatch([atlas[s]], false)[0];
}
public static MTexture[] GetImageList(IEnumerable<MTexture> mtex)
{
List<MTexture> result = [];
foreach (var from in mtex)
{
if (resultcomplete.TryGetValue((from.Atlas, from.AtlasPath), out var mTexture))
{
result.Add(mTexture);
}
else
{
var size = mtex.Select(x =>
{
var rect = x.Texture.Texture_Safe.Bounds;
return new Vector2(rect.Width, rect.Height);
});
var ret = MTextureDrawOutlineBatch(mtex, size.All(x => x == size.First()));
foreach (var (from2, to) in mtex.Zip(ret))
{
resultcomplete[(from2.Atlas, from2.AtlasPath)] = to;
}
return ret;
}
}
return result.ToArray();
}
public static MTexture GetImage(MTexture mtex)
{
return GetImage(mtex.Atlas, mtex.AtlasPath);
}
public static MTexture GetImage(Atlas atlas, string v)
{
if (resultcomplete.TryGetValue((atlas, v), out var res))
{
return res;
}
return resultcomplete[(atlas, v)] = DrawOutlineSingle(v, atlas);
}
}
}