forked from bahstrike/DyeAtlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DyeAtlas.cs
493 lines (391 loc) · 16.6 KB
/
DyeAtlas.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace DyeAtlas
{
public partial class DyeAtlas : Form
{
public DyeAtlas()
{
InitializeComponent();
}
public enum GameType
{
Ark,
Atlas
}
public Palette LoadPalette(GameType gt)
{
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream($"DyeAtlas.palette_{gt.ToString().ToLowerInvariant()}.csv");
Palette pal = Palette.LoadFromCSV(s);
s.Dispose();
return pal;
}
private Palette _palette = null;
public Palette palette
{
get
{
if(_palette == null)
{
_palette = LoadPalette((GameType)gamechoice.SelectedIndex);// should be ok to cast.. only 2 options lol
}
return _palette;
}
set
{
_palette = value;
}
}
private void DyeAtlas_Load(object sender, EventArgs e)
{
Bitmap bk = new Bitmap(preview.Width, preview.Height);
using (Graphics gfx = Graphics.FromImage(bk))
{
gfx.FillRectangle(new HatchBrush(HatchStyle.DarkDownwardDiagonal, Color.FromArgb(20, 20, 20), Color.FromArgb(128, 40, 128)), new Rectangle(0, 0, preview.Width, preview.Height));
}
preview.BackgroundImage = bk;
resolution.SelectedIndex = 1;// 256x256
// nothing is loaded yet, so nothing can be saved
savePNGButton.Enabled = false;
savePNTButton.Enabled = false;
// load settings
gamechoice.SelectedIndex = Properties.Settings.Default.GameType;
mypaintings.Text = Properties.Settings.Default.MyPaintings;
}
private void DyeAtlas_FormClosed(object sender, FormClosedEventArgs e)
{
// save settings
Properties.Settings.Default.GameType = gamechoice.SelectedIndex;
Properties.Settings.Default.MyPaintings = mypaintings.Text;
}
// convenience
public string MyPaintingsPath
{
get
{
return mypaintings.Text;
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
e.Effect = DragDropEffects.Link;
}
public void SaveFile(string file)
{
try
{
Bitmap bmp = preview.Image as Bitmap;
if (bmp == null)
return;
if(Path.GetExtension(file).Equals(".pnt", StringComparison.InvariantCultureIgnoreCase))//IsFilePNT(file))
{
PNTImage pnt = PNTImage.GenerateFromBitmap(palette, bmp, dithering.Checked, hsvcompare.Checked);
pnt.Save(file);
}else
{
bmp.Save(file);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
public Size ExportDimensions
{
get
{
Size sz = new Size(256, 256);//default
string[] parts = resolution.Text.Split(new char[] { 'x', 'X', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
if(parts.Length>=2)
{
sz.Width = int.Parse(parts[0]);
sz.Height = int.Parse(parts[1]);
}
return sz;
}
}
// for caching current bitmap (not pnt)
private string lastFilename = null;
private DateTime lastFileWrite = new DateTime();
private Size lastOutputSize = new Size(0, 0);
private Bitmap lastBitmap = null;
public void OpenFile(string file, bool autodetectResolution=true)
{
if (string.IsNullOrEmpty(file) || !File.Exists(file))
return;
try
{
PNTImage pnt = null;
switch (Path.GetExtension(file).ToLowerInvariant())
{
case ".pnt":
pnt = PNTImage.LoadPNT(palette, file);
if (autodetectResolution)
{
// autoselect resolution
if (pnt.width >= 256)
resolution.SelectedIndex = 1;//256x256
else
resolution.SelectedIndex = 0;//128x128 (shrug)
}
break;
case ".bmp":
case ".png":
case ".jpg":
case ".jpeg":
case ".gif":
case ".tga":
case ".tiff":
{
DateTime modifyTime = new FileInfo(file).LastWriteTimeUtc;
// do we already have a file open?
if (!string.IsNullOrEmpty(lastFilename))
{
// check if different/modified.. if so then invalidate our existing entry
if (!string.Equals(file, lastFilename, StringComparison.InvariantCultureIgnoreCase) ||
!modifyTime.Equals(lastFileWrite) ||
!ExportDimensions.Equals(lastOutputSize)/*hack to force reload if output dimensions change*/)
{
lastFilename = null;
lastFileWrite = new DateTime();
lastOutputSize = new Size(0, 0);
lastBitmap = null;
}
}
// only load if we need to
if (lastBitmap == null)
{
Bitmap bmp = (Bitmap)Bitmap.FromFile(file);
// powers-of-2 automatic rescale
int newWidth = (int)Math.Pow(2.0, Math.Ceiling(Math.Log((double)bmp.Width) / Math.Log(2.0)));
int newHeight = (int)Math.Pow(2.0, Math.Ceiling(Math.Log((double)bmp.Height) / Math.Log(2.0)));
// clamp to allowable export dimensions (assumed power-of-2)
newWidth = Math.Min(ExportDimensions.Width, newWidth);
newHeight = Math.Min(ExportDimensions.Height, newHeight);
// hold onto yerr hats, its time to rescale!
if (newWidth != bmp.Width || newHeight != bmp.Height)
{
Bitmap newBmp = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
newBmp.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
using (Graphics gfx = Graphics.FromImage(newBmp))
{
gfx.CompositingMode = CompositingMode.SourceCopy;
gfx.InterpolationMode = InterpolationMode.NearestNeighbor;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (ImageAttributes wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
gfx.DrawImage(bmp, new Rectangle(0, 0, newWidth, newHeight), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, wrapMode);
}
}
bmp.Dispose();
bmp = newBmp;
}
lastFilename = file;
lastFileWrite = modifyTime;
lastOutputSize = bmp.Size;
lastBitmap = bmp;
}
pnt = PNTImage.GenerateFromBitmap(palette, lastBitmap, dithering.Checked, hsvcompare.Checked);
}
break;
}
if(pnt == null)
{
// should we scold idiots? for dragging bad files
//Messagebo
return;
}
currentfile.Text = file;
if(autodetectResolution)
resolution.Text = $"{pnt.width}x{pnt.height}";
preview.Image = pnt.GenerateBitmap();
// we got stuff, we could save
savePNTButton.Enabled = true;
savePNGButton.Enabled = true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
OpenFile(files[0]);
}
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (Directory.Exists(MyPaintingsPath))
ofd.InitialDirectory = MyPaintingsPath;
ofd.Filter = "Compatible Files (*.pnt;*.png;*.bmp;*.jpg)|*.pnt;*.png;*.bmp;*.jpg|Paint Files (*.pnt)|*.pnt|Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg|All Files (*.*)|*.*";
if (ofd.ShowDialog() != DialogResult.OK)
return;
OpenFile(ofd.FileName);
}
private void dithering_CheckedChanged(object sender, EventArgs e)
{
Reprocess();
}
HSVSlidersPopup hsvPopup = null;
private void hsvcompare_CheckedChanged(object sender, EventArgs e)
{
if(hsvcompare.Checked)
{
// if enabling, provide popup
hsvPopup = new HSVSlidersPopup(this);
// manual spawn location for dialog.. right next to button
hsvPopup.Location = PointToScreen(hsvcompare.Location + new Size(hsvcompare.Width, 0));
// non-modal; let it live
hsvPopup.Show(this);
}
else
{
// if destroying, should kill popup
if (hsvPopup != null)
{
hsvPopup.Close();
hsvPopup = null;
}
}
Reprocess();
}
private void savePNTButton_Click(object sender, EventArgs e)
{
// dont do popup if image is blank; prolly button should be disabled
if (preview.Image == null)
return;
SaveFileDialog sfd = new SaveFileDialog();
if (Directory.Exists(MyPaintingsPath))
sfd.InitialDirectory = MyPaintingsPath;
sfd.Filter = "Paint File (*.pnt)|*.pnt|All Files (*.*)|*.*";
// default some save filename
sfd.FileName = Path.GetFileNameWithoutExtension(currentfile.Text) + ".pnt";
if (sfd.ShowDialog() != DialogResult.OK)
return;
SaveFile(sfd.FileName);
}
private void savePNGButton_Click(object sender, EventArgs e)
{
// dont do popup if image is blank; prolly button should be disabled
if (preview.Image == null)
return;
SaveFileDialog sfd = new SaveFileDialog();
if (Directory.Exists(MyPaintingsPath))
sfd.InitialDirectory = MyPaintingsPath;
sfd.RestoreDirectory = true;// if we are exporting image to desktop or whatever then we should not mess with open file
sfd.Filter = "PNG File (*.png)|*.png|Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg|All Files (*.*)|*.*";
// default some save filename
sfd.FileName = Path.GetFileNameWithoutExtension(currentfile.Text) + ".png";
if (sfd.ShowDialog() != DialogResult.OK)
return;
SaveFile(sfd.FileName);
}
private void resolution_TextChanged(object sender, EventArgs e)
{
Reprocess();
}
private void mypaintingsbrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog fld = new FolderBrowserDialog();
fld.SelectedPath = mypaintings.Text;
fld.Description = @"This is usually like: [GAME INSTALL]\ShooterGame\Saved\MyPaintings";
if (fld.ShowDialog() != DialogResult.OK)
return;
mypaintings.Text = fld.SelectedPath;
}
private void openfolder_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mypaintings.Text) || !Directory.Exists(mypaintings.Text))
return;
System.Diagnostics.Process.Start("explorer", mypaintings.Text);
}
private void gamechoice_SelectedIndexChanged(object sender, EventArgs e)
{
// invalidate palette
palette = null;
Reprocess();
}
public void Reprocess()
{
// reload current file
OpenFile(currentfile.Text, false);
}
#region RGB<->HSV
// https://stackoverflow.com/questions/359612/how-to-convert-rgb-color-to-hsv
public static class ColorConversion
{
public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
hue = color.GetHue();
saturation = (max == 0) ? 0 : 1d - (1d * min / max);
value = max / 255d;
}
public static Color ColorFromHSV(double hue, double saturation, double value)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
int v = Convert.ToInt32(value);
int p = Convert.ToInt32(value * (1 - saturation));
int q = Convert.ToInt32(value * (1 - f * saturation));
int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
else if (hi == 1)
return Color.FromArgb(255, q, v, p);
else if (hi == 2)
return Color.FromArgb(255, p, v, t);
else if (hi == 3)
return Color.FromArgb(255, p, q, v);
else if (hi == 4)
return Color.FromArgb(255, t, p, v);
else
return Color.FromArgb(255, v, p, q);
}
}
#endregion
#region AngleDiff
public static class AngleDiff
{
//https://stackoverflow.com/questions/7570808/how-do-i-calculate-the-difference-of-two-angle-measures
/**
* Shortest distance (angular) between two angles.
* It will be in range [0, 180].
*/
public static int distance(int alpha, int beta)
{
int phi = Math.Abs(beta - alpha) % 360; // This is either the distance or 360 - distance
int distance = phi > 180 ? 360 - phi : phi;
return distance;
}
public static double distance(double alpha, double beta)
{
return (double)distance((int)Math.Round(alpha), (int)Math.Round(beta));
}
}
#endregion
}
}