-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBinxelviewForm.cs
2816 lines (2539 loc) · 101 KB
/
BinxelviewForm.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
using System.Xml.Linq;
using Binxelview.Dialogs;
using System.Security.Policy;
namespace Binxelview
{
public partial class BinxelviewForm : Form
{
const string APPNAME = "Binxelview";
const string APPDATA_FOLDER = "Binxelview";
const int MAX_BPP = 32;
const int PRESET_VERSION = 3;
const int PALETTE_BITS = 16; // maximum bits supported in palette
const int PALETTE_SIZE = (1 << PALETTE_BITS);
const int PALETTEBOX_BITS = 14; // maximum bits the paletteBox can display (128x128 = 1<<14)
const int PALETTEBOX_DIM = 128; // should match paletteBox size
const int ZOOM_MAX = 32;
enum PaletteMode
{
PALETTE_CUSTOM = 0, // entries below are same order as comboBoxPalette.Items
PALETTE_RGB,
PALETTE_RANDOM,
PALETTE_GREY,
PALETTE_CUBEHELIX,
};
static readonly string[] PALETTE_MODE_STRING =
{
"Custom",
"RGB",
"Random",
"Greyscale",
"Cubehelix",
};
byte[] data = {};
string data_path = "";
string data_file = "";
long pos_byte = 0;
int pos_bit = 0;
int next_increment_byte = 1;
int next_increment_bit = 0;
int selected_tile = -1;
long selected_pos = -1;
int selected_palette_filter = 0;
int option_palette_type;
string ini_path = "";
int palette_path_type = -1;
Preset preset;
List<Preset> presets;
ViewForm split_view_form;
PictureBox view_box;
VScrollBar view_scroll;
DirectoryInfo dir_cwd, dir_exe, dir_loc;
string ini_exe, ini_loc;
Bitmap palette_bmp = new Bitmap(PALETTEBOX_DIM, PALETTEBOX_DIM);
Bitmap pixel_bmp = null;
Color[] palette = new Color[PALETTE_SIZE];
Color[] palette_custom = new Color[PALETTE_SIZE];
int[] palette_raw = new int[PALETTE_SIZE]; // int version of palette
string palette_error = "";
int background_raw = SystemColors.Control.ToArgb(); // int version of background setting
int[] twiddle_cache = null;
int twiddle_cache_order = 0;
int twiddle_cache_w = 0;
int twiddle_cache_h = 0;
bool disable_pixel_redraw = false; // used to temporarily block redraws during repeated updates
Font posfont_regular, posfont_bold;
Random random = new Random();
uint random_seed;
int preset_menu_fixed_items;
int main_w, main_h; // used to restore size during split_view switch
int fixed_w, fixed_h;
// settings
int zoom;
bool hidegrid;
Color background;
PaletteMode palette_mode;
bool decimal_position;
bool snap_scroll;
bool horizontal_layout;
bool split_view;
string palette_path = "";
string preset_dir = "";
bool save_ini = true; // not changed by defaultOption
//
// Preset
//
struct Preset
{
public string name;
public bool little_endian;
public bool chunky;
public int bpp;
public int width;
public int height;
public int pixel_stride_byte;
public int pixel_stride_bit;
public bool pixel_stride_auto;
public int row_stride_byte;
public int row_stride_bit;
public bool row_stride_auto;
public int next_stride_byte;
public int next_stride_bit;
public bool next_stride_auto;
public int twiddle;
public int tile_size_x;
public int tile_stride_byte_x;
public int tile_stride_bit_x;
public int tile_size_y;
public int tile_stride_byte_y;
public int tile_stride_bit_y;
public int[] bit_stride_byte;
public int[] bit_stride_bit;
static public string last_error = "";
public void empty()
{
name = "";
little_endian = true;
chunky = true;
bpp = 8;
width = 8;
height = 1;
pixel_stride_byte = 1;
row_stride_byte = 8;
next_stride_byte = 8;
pixel_stride_bit = 0;
row_stride_bit = 0;
next_stride_bit = 0;
tile_size_x = 0;
tile_size_y = 0;
tile_stride_byte_x = 0;
tile_stride_byte_y = 0;
tile_stride_bit_x = 0;
tile_stride_bit_y = 0;
pixel_stride_auto = true;
row_stride_auto = true;
next_stride_auto = true;
bit_stride_byte = new int[MAX_BPP];
bit_stride_bit = new int[MAX_BPP];
for (int i = 0; i < MAX_BPP; ++i)
{
bit_stride_byte[i] = i / 8;
bit_stride_bit[i] = i % 8;
}
}
public Preset copy()
{
Preset p = new Preset();
p = this;
p.bit_stride_byte = (int[])bit_stride_byte.Clone();
p.bit_stride_bit = (int[])bit_stride_bit.Clone();
return p;
}
public bool saveFile(string path)
{
Debug.WriteLine("Preset.saveFile(\""+path+"\")");
try
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(string.Format("{0}", PRESET_VERSION));
sw.WriteLine(string.Format("{0} {1} {2}", little_endian ? 1 : 0, chunky ? 1 : 0, twiddle));
sw.WriteLine(string.Format("{0} {1} {2}", bpp, width, height));
sw.WriteLine(string.Format("{0} {1} {2}", pixel_stride_byte, row_stride_byte, next_stride_byte));
sw.WriteLine(string.Format("{0} {1} {2}", pixel_stride_bit, row_stride_bit, next_stride_bit));
sw.WriteLine(string.Format("{0} {1} {2}", pixel_stride_auto ? 1 : 0, row_stride_auto ? 1 : 0, next_stride_auto ? 1 : 0));
sw.WriteLine(string.Format("{0} {1} {2}", tile_size_x, tile_stride_byte_x, tile_stride_bit_x));
sw.WriteLine(string.Format("{0} {1} {2}", tile_size_y, tile_stride_byte_y, tile_stride_bit_y));
if (!chunky)
{
for (int i = 0; i < bpp; ++i)
{
sw.WriteLine(string.Format("{0} {1}", bit_stride_byte[i], bit_stride_bit[i]));
}
}
}
}
catch (Exception ex)
{
last_error = ex.ToString();
return false;
}
return true;
}
public bool loadFile(string path)
{
Debug.WriteLine("Preset.loadFile(\""+path+"\")");
empty();
int linecount = 0;
try
{
// VERSION 3
// version
// endian chunky twiddle
// bpp width height
// (stride byte) pixel row next
// (stride bit) pixel row next
// (stride auto) pixel row next
// (tile stride X) size byte bit
// (tile stride Y) size byte bit
// (bpp * bit stride) byte bit
// VERSION 2
// same as VERSION 3 without twiddle
// VERSION 1
// same as VERSION 2 but tile stride was relative to pixel stride, instead of absolute
name = Path.GetFileNameWithoutExtension(path);
using (TextReader tr = File.OpenText(path))
{
string l = tr.ReadLine(); ++linecount;
int v = int.Parse(l);
if (v > PRESET_VERSION)
{
last_error = string.Format("Unknown preset version: {0} > {1}",v,PRESET_VERSION);
return false;
}
int version = v;
l = tr.ReadLine();
string[] ls = l.Split(' ');
little_endian = int.Parse(ls[0]) != 0;
chunky = int.Parse(ls[1]) != 0;
if (version >= 3) twiddle = int.Parse(ls[2]);
else twiddle = 0;
if (twiddle < 0 || twiddle > 2) twiddle = 0;
l = tr.ReadLine();
ls = l.Split(' ');
bpp = int.Parse(ls[0]);
width = int.Parse(ls[1]);
height = int.Parse(ls[2]);
if (bpp < 0) bpp = 1;
if (width < 1) width = 1;
if (height < 1) height = 1;
l = tr.ReadLine();
ls = l.Split(' ');
pixel_stride_byte = int.Parse(ls[0]);
row_stride_byte = int.Parse(ls[1]);
next_stride_byte = int.Parse(ls[2]);
l = tr.ReadLine();
ls = l.Split(' ');
pixel_stride_bit = int.Parse(ls[0]);
row_stride_bit = int.Parse(ls[1]);
next_stride_bit = int.Parse(ls[2]);
l = tr.ReadLine();
ls = l.Split(' ');
pixel_stride_auto = int.Parse(ls[0]) != 0;
row_stride_auto = int.Parse(ls[1]) != 0;
next_stride_auto = int.Parse(ls[2]) != 0;
l = tr.ReadLine();
ls = l.Split(' ');
tile_size_x = int.Parse(ls[0]);
tile_stride_byte_x = int.Parse(ls[1]);
tile_stride_bit_x = int.Parse(ls[2]);
l = tr.ReadLine();
ls = l.Split(' ');
tile_size_y = int.Parse(ls[0]);
tile_stride_byte_y = int.Parse(ls[1]);
tile_stride_bit_y = int.Parse(ls[2]);
if (!chunky)
{
for (int i = 0; i < bpp; ++i)
{
l = tr.ReadLine();
ls = l.Split(' ');
bit_stride_byte[i] = int.Parse(ls[0]);
bit_stride_bit[i] = int.Parse(ls[1]);
}
}
if (version == 1)
{
// version 1 used tile stride as an additional "shift" on top of the pixel/row strides
// convert the shift to an absolute stride
int tile_stride_x = tile_stride_bit_x + (tile_stride_byte_x * 8);
int tile_stride_y = tile_stride_bit_y + (tile_stride_byte_y * 8);
int stride_x = pixel_stride_bit + (pixel_stride_byte * 8);
int stride_y = row_stride_bit + (row_stride_byte * 8);
if (tile_stride_x == 0)
{
tile_size_x = 0;
}
else
{
tile_stride_x += (stride_x * tile_size_x);
tile_stride_byte_x = tile_stride_x >> 3;
tile_stride_bit_x = tile_stride_x & 7;
}
if (tile_stride_y == 0)
{
tile_size_y = 0;
}
else
{
tile_stride_y += (stride_y * tile_size_y);
tile_stride_byte_y = tile_stride_y >> 3;
tile_stride_bit_y = tile_stride_y & 7;
}
}
}
}
catch (Exception ex)
{
last_error = string.Format("Line {0}: ",linecount) + ex.ToString();
Debug.WriteLine(last_error);
return false;
}
return true;
}
};
//
// Options
//
void defaultOption()
{
zoom = 2;
hidegrid = false;
background = SystemColors.Control;
background_raw = background.ToArgb();
palette_mode = PaletteMode.PALETTE_RGB;
decimal_position = false;
snap_scroll = true;
horizontal_layout = false;
split_view = false;
palette_path = "";
preset_dir = "";
// note: save_ini is not changed, intentionally, default options shouldn't alter the current ini read-only state
}
string parseOption(string optline, string base_path, bool ini_file)
{
int eqpos = optline.IndexOf("=");
if (eqpos < 0) return "No = in option: "+optline;
string opt = optline.Substring(0,eqpos).ToUpperInvariant();
string val = optline.Substring(eqpos+1);
string valu = val.ToUpperInvariant();
if (opt == "INI" && !ini_file)
{
string path = val;
if (!Path.IsPathRooted(path)) path = Path.Combine(base_path,path);
ini_path = path;
string result = loadIni(path);
if (result.Length > 0) return "-ini error: "+path+"\n"+result;
return "";
}
if (opt == "SAVEINI")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for saveini: "+val;
if (v != 0 && v != 1) return "Saveini value must be 0 or 1: "+val;
save_ini = (v != 0);
return "";
}
if (opt == "PRESETDIR") // set preset library location and reload presets
{
string path = val;
if (!Path.IsPathRooted(path)) path = Path.Combine(base_path,path);
preset_dir = path;
reloadPresets();
return "";
}
if (opt == "PRESETFILE") // load preset file
{
string path = val;
if (!Path.IsPathRooted(path)) path = Path.Combine(base_path,path);
Preset p = new Preset();
if (!p.loadFile(path)) return "Could not load preset file: "+path+"\n"+Preset.last_error;
preset = p;
return "";
}
if (opt == "PRESET") // select named preset from the library
{
foreach (Preset p in presets)
{
if (val == p.name)
{
preset = p.copy();
return "";
}
}
return "Preset not found in loaded presets: "+val;
}
if (opt == "PALTYPE") // override next PAL file type detection, use before PAL
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for paltype: "+val;
option_palette_type = v;
return "";
}
if (opt == "PAL") // load palette file
{
string path = val;
if (!Path.IsPathRooted(path)) path = Path.Combine(base_path,path);
int paltype = 0;
if (
valu.EndsWith(".BMP") ||
valu.EndsWith(".GIF") ||
valu.EndsWith(".PNG") ||
valu.EndsWith(".TIF"))
paltype = 1;
if (valu.EndsWith(".VGA")) paltype = 2;
if (valu.EndsWith(".RIFF")) paltype = 3;
if (option_palette_type != -1)
{
paltype = option_palette_type;
option_palette_type = -1;
}
if (!loadPalette(path,paltype)) return "Could not load palette file: "+path+"\n"+palette_error;
return "";
}
if (opt == "AUTOPAL")
{
for (int i=1; i<PALETTE_MODE_STRING.Length; ++i)
{
if (valu == PALETTE_MODE_STRING[i].ToUpperInvariant())
{
palette_mode = (PaletteMode)i;
return "";
}
}
return "Unknown autopal value: "+val;
}
if (opt == "BACKGROUND")
{
int v;
if (!int.TryParse(val,System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.CurrentCulture, out v))
return "Could not parse hex color for background: "+val;
background_raw = (v & 0x00FFFFFF) | unchecked((int)0xFF000000);
background = Color.FromArgb(background_raw);
return "";
}
if (opt == "ZOOM")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for zoom: "+val;
if (v < 1) return "Zoom must be 1 or greater: "+val;
zoom = v;
return "";
}
if (opt == "GRID")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for grid: "+val;
if (v != 0 && v != 1) return "Grid value must be 0 or 1: "+val;
hidegrid = (v == 0);
return "";
}
if (opt == "HEXPOS")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for hexpos: "+val;
if (v != 0 && v != 1) return "Hexpos value must be 0 or 1: "+val;
decimal_position = (v == 0);
return "";
}
if (opt == "SNAPSCROLL")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for snapscroll: "+val;
if (v != 0 && v != 1) return "Snapscroll value must be 0 or 1: "+val;
snap_scroll = (v != 0);
return "";
}
if (opt == "SPLITVIEW")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for splitview: "+val;
if (v != 0 && v != 1) return "Splitview value must be 0 or 1: "+val;
split_view = (v != 0);
return "";
}
if (opt == "SPLITVIEWW")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for splitvieww: "+val;
if (v <= 0) return "Splitvieww value must be positive: "+val;
split_view_form.Width = v;
return "";
}
if (opt == "SPLITVIEWH")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for splitviewh: "+val;
if (v <= 0) return "Splitviewh value must be positive: "+val;
split_view_form.Height = v;
return "";
}
if (opt == "HORIZONTAL")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for horizontal: "+val;
if (v != 0 && v != 1) return "Horizontal value must be 0 or 1: "+val;
horizontal_layout = (v != 0);
return "";
}
return "Invalid option: "+optline;
}
string loadIni(string path)
{
Debug.WriteLine("loadIni(\""+path+"\")");
string ini_base = Path.GetDirectoryName(path);
option_palette_type = -1;
string ini_err = "";
try
{
using (TextReader tr = File.OpenText(path))
{
string l;
while ((l = tr.ReadLine()) != null)
{
l = l.Trim(); // eliminate leading and trailing whitespace
if (l.Length < 1) continue;
if (l.StartsWith("#")) continue; // comment line
string opt_err = parseOption(l,ini_base,true);
if (opt_err.Length > 0 && ini_err.Length > 0) ini_err += "\n";
ini_err += opt_err;
}
}
}
catch (Exception ex)
{
if (ini_err.Length > 0) ini_err += "\n";
return ini_err + ex.ToString();
}
return ini_err;
}
string saveIni(string path)
{
Debug.WriteLine("saveIni(\""+path+"\")");
string fullpath = Path.GetFullPath(path);
string fulldir = Path.GetDirectoryName(fullpath);
// note: Path.GetRelativePath unavailable in .NET framework 4
try
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("# Binxelview options file");
sw.WriteLine("# " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine(string.Format("saveini={0}", save_ini ? 1 : 0));
if (preset_dir.Length > 0)
{
string p = Path.GetFullPath(preset_dir);
if (p == fulldir) p = "";
else if (p.StartsWith(fulldir+Path.PathSeparator)) p = p.Substring(fulldir.Length+1);
sw.WriteLine("presetdir=" + p);
}
foreach (Preset pi in presets) // save current preset if it exists in the library
{
if (preset.name == pi.name)
{
sw.WriteLine("preset="+preset.name);
break;
}
}
if (palette_mode == PaletteMode.PALETTE_CUSTOM && palette_path.Length > 0)
{
sw.WriteLine(string.Format("paltype={0}",palette_path_type));
string p = Path.GetFullPath(palette_path);
if (p.StartsWith(fulldir)) p = p.Substring(fulldir.Length+1);
sw.WriteLine("pal={0}", p);
}
else if (palette_mode != PaletteMode.PALETTE_CUSTOM)
{
sw.WriteLine("autopal=" + PALETTE_MODE_STRING[(int)palette_mode]);
}
sw.WriteLine(string.Format("background={0:X6}",background_raw & 0x00FFFFFF));
sw.WriteLine(string.Format("zoom={0}",zoom));
sw.WriteLine(string.Format("grid={0}",hidegrid ? 0 : 1));
sw.WriteLine(string.Format("hexpos={0}",decimal_position ? 0 : 1));
sw.WriteLine(string.Format("snapscroll={0}",snap_scroll ? 1 : 0));
sw.WriteLine(string.Format("splitview={0}",split_view ? 1 : 0));
if (split_view && split_view_form.WindowState == FormWindowState.Normal) // save split_view window size if active
{
sw.WriteLine(string.Format("splitvieww={0}",split_view_form.Width));
sw.WriteLine(string.Format("splitviewh={0}",split_view_form.Height));
}
sw.WriteLine(string.Format("horizontal={0}",horizontal_layout ? 1 : 0));
sw.WriteLine("# end");
}
}
catch (Exception ex)
{
return ex.ToString();
}
return "";
}
string saveCurrentIni()
{
if (ini_path.Length > 0) // if a default ini was found, or loaded with -ini, save back to it
{
string ini_err = saveIni(ini_path);
if (ini_err.Length < 1) return "";
return "Save options error: "+ini_path+"\n"+ini_err;
}
else // try to create a new ini
{
// first try to save in the executable directory
string ini_exe_err = saveIni(ini_exe);
if(ini_exe_err.Length < 1) return "";
// if that fails try to save in appdata local
// try to create directory first, ignore errors
try {
Directory.CreateDirectory(Path.GetDirectoryName(ini_loc));
} catch { }
string ini_loc_err = saveIni(ini_loc);
if(ini_loc_err.Length < 1) return "";
return "Save options attempt 1: "+ini_exe+"\n"+ini_exe_err+
"\n\n"+
"Save options attempt 2: "+ini_loc+"\n"+ini_loc_err;
}
}
//
// Pixel building
//
int[] bit_stride = new int[MAX_BPP];
long[] pixel_buffer = null;
int[] color_buffer = null;
int pixel_buffer_width;
int pixel_buffer_height;
int pixel_gx = 1;
int pixel_gy = 1;
int pixel_padx = 0;
int pixel_pady = 0;
int pixel_twp = 1;
int pixel_thp = 1;
void prepareBitStride()
{
if (preset.chunky)
{
for (int i = 0; i < preset.bpp; ++i)
{
bit_stride[i] = i;
}
}
else
{
for (int i = 0; i < preset.bpp; ++i)
{
bit_stride[i] = preset.bit_stride_bit[i] + (preset.bit_stride_byte[i] * 8);
}
}
}
void twiddleCacheCheck(int tw, int th)
{
// rebuild index of twiddle ordering
if (twiddle_cache == null ||
twiddle_cache_order != preset.twiddle ||
twiddle_cache_w != tw ||
twiddle_cache_h != th )
{
twiddle_cache_order = preset.twiddle;
twiddle_cache_w = tw;
twiddle_cache_h = th;
twiddle_cache = new int[tw * th];
for (int y = 0; y < th; ++y)
{
for (int x = 0; x < tw; ++x)
{
int twx = x;
int twy = y;
int bit = 0;
int twxy = 0;
if (preset.twiddle == 2) // N instead of Z order
{
int temp = twx;
twx = twy;
twy = temp;
}
while (twx > 0 || twy > 0) // interleaved bits = Z order / morton
{
twxy |=
((twx >> bit) & 1) << (bit * 2 + 0) |
((twy >> bit) & 1) << (bit * 2 + 1);
twx &= ~(1 << bit);
twy &= ~(1 << bit);
bit += 1;
}
twiddle_cache[x + (y * tw)] = twxy;
}
}
}
}
unsafe long buildPixel(long pos, int bpp, bool little_endian, int length, byte* data_raw, int* bit_stride_raw)
{
uint p = 0;
for (int b = 0; b < bpp; ++b)
{
long bpos = pos + bit_stride_raw[b];
long dpos_byte = bpos >> 3;
if (dpos_byte < 0 || dpos_byte >= length)
{
return -1;
}
int dpos_bit = (int)(bpos & 7);
if (!little_endian) dpos_bit = 7 - dpos_bit;
p |= (uint)((data_raw[dpos_byte] >> dpos_bit) & 1) << b;
}
return (long)p;
}
long readPixel(long pos) // slow/safe version of buildPixel, for convenient single pixel queries
{
prepareBitStride();
long p;
unsafe
{
fixed (byte* data_raw = data)
fixed (int* bit_stride_raw = bit_stride)
{
p = buildPixel(pos, preset.bpp, preset.little_endian, data.Length, data_raw, bit_stride_raw);
}
}
return p;
}
unsafe void renderTile(
long pos, int bpp, bool little_endian,
int length, int w, int h,
int pixel_stride, int row_stride, int render_stride,
int tile_size_x, int tile_size_y, int tile_shift_x, int tile_shift_y,
byte* data, int* bit_stride, long* render_buffer, int* twiddle_raw)
{
long pos_row = pos;
int plane_y = 0;
for (int y=0; y<h; ++y)
{
int plane_x = 0;
long pos_pixel = pos_row;
long* render_row = render_buffer + (y * render_stride);
for (int x=0; x<w; ++x)
{
if (twiddle_raw != null)
{
int twxy = twiddle_raw[x + (y * w)];
int twy = twxy / w;
int twx = twxy % w;
pos_pixel = pos + (twy * row_stride) + (twx * pixel_stride);
}
render_row[x] = buildPixel(pos_pixel, bpp, little_endian, length, data, bit_stride);
pos_pixel += pixel_stride;
++plane_x;
if (plane_x >= tile_size_x)
{
plane_x = 0;
pos_pixel += tile_shift_x;
}
}
pos_row += row_stride;
++plane_y;
if (plane_y >= tile_size_y)
{
plane_y = 0;
pos_row += tile_shift_y;
}
}
}
void renderGrid(long pos, int gx, int gy, int padx, int pady, int minx, int miny, bool color)
{
// prepares pixel_buffer
int tw = preset.width;
int th = preset.height;
int twp = tw + padx;
int thp = th + pady;
int pixels_w = (gx * twp) + padx;
int pixels_h = (gy * thp) + pady;
if (pixels_w < minx) pixels_w = minx;
if (pixels_h < miny) pixels_h = miny;
int pixels_needed = pixels_w * pixels_h;
if (pixel_buffer == null ||
color_buffer == null ||
pixel_buffer_width != pixels_w ||
pixel_buffer_height != pixels_h)
{
pixel_buffer = new long[pixels_needed];
color_buffer = new int[pixels_needed];
pixel_buffer_width = pixels_w;
pixel_buffer_height = pixels_h;
}
prepareBitStride();
int pixel_stride = preset.pixel_stride_bit + (preset.pixel_stride_byte * 8);
int row_stride = preset.row_stride_bit + (preset.row_stride_byte * 8);
int next_stride = preset.next_stride_bit + (preset.next_stride_byte * 8);
int tile_size_x = preset.tile_size_x;
int tile_size_y = preset.tile_size_y;
int tile_shift_x = preset.tile_stride_bit_x + (preset.tile_stride_byte_x * 8) - (tile_size_x * pixel_stride);
int tile_shift_y = preset.tile_stride_bit_y + (preset.tile_stride_byte_y * 8) - (tile_size_y * row_stride);
int bpp = preset.bpp;
bool little_endian = preset.little_endian;
if (tile_size_x == 0) tile_shift_x = 0;
if (tile_size_y == 0) tile_shift_y = 0;
// tile stride is converted to a relative shift that is applied at the end of each tile
if (preset.twiddle != 0)
{
twiddleCacheCheck(tw,th);
}
else
{
twiddle_cache = null;
}
int rgx = gx;
int rgy = gy;
if (horizontal_layout)
{
rgx = gy;
rgy = gx;
}
int length = data.Length;
unsafe
{
fixed (byte* data_raw = data)
fixed (int* bit_stride_raw = bit_stride)
fixed (int* color_buffer_raw = color_buffer)
fixed (long* pixel_buffer_raw = pixel_buffer)
fixed (int* twiddle_raw = twiddle_cache)
{
for (int i=0; i < pixels_needed; ++i)
{
pixel_buffer_raw[i] = -1;
}
for (int tx = 0; tx < rgx; ++tx)
{
int sx = padx + (twp * tx);
for (int ty = 0; ty < rgy; ++ty)
{
int sy = pady + (thp * ty);
if (horizontal_layout)
{
sx = padx + (twp * ty);
sy = pady + (thp * tx);
}
renderTile(pos, bpp, little_endian, length, tw, th,
pixel_stride, row_stride, pixel_buffer_width,
tile_size_x, tile_size_y, tile_shift_x, tile_shift_y,
data_raw, bit_stride_raw,
pixel_buffer_raw + sx + (sy * pixel_buffer_width),
twiddle_raw);
pos += next_stride;
}
}
if (color)
{
for (int i = 0; i < pixels_needed; ++i)
{
color_buffer_raw[i] = getPaletteRaw(pixel_buffer_raw[i]);
}
}
}
}
}
void renderGridColorToBitmap(Bitmap b, int zoom)
{
int w = b.Width;
int h = b.Height;
BitmapData bdata = b.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.WriteOnly,
b.PixelFormat);
unsafe
{
fixed (int* color_buffer_raw = color_buffer)
{
int* braw = (int*)bdata.Scan0;
int stride = bdata.Stride / (Image.GetPixelFormatSize(b.PixelFormat) / 8);
int zy = 0;
int cy = 0;
for (int y=0; y<h; ++y)
{
int* out_row = braw + (y * stride);
if (zy <= 0)
{
int zx = 0;
int cx = 0;
int* color_row = color_buffer_raw + (pixel_buffer_width * cy);
++cy;
for (int x = 0; x < w; ++x)
{
if (zx <= 0)
{
out_row[x] = color_row[cx];
++cx;
zx = zoom;
}
else
{
out_row[x] = out_row[x - 1];
}
--zx;
}
zy = zoom;
}
else
{
for (int x=0; x<w; ++x)
{
out_row[x] = (out_row - stride)[x];
}
}
--zy;
}
}
}
b.UnlockBits(bdata);
}
Bitmap renderGridIndexToBitmap()
{
int w = pixel_buffer_width;
int h = pixel_buffer_height;
Bitmap b = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
BitmapData bdata = b.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.WriteOnly,
b.PixelFormat);
unsafe
{
byte* braw = (byte*)bdata.Scan0;
int stride = bdata.Stride;
fixed(long* pixel_buffer_raw = pixel_buffer)
{
int i = 0;
for (int y=0; y<h; ++y)
{
byte* out_row = braw + (y * stride);
for (int x = 0; x < w; ++x)
{
long p = pixel_buffer_raw[i];
++i;
if (p < 0) p = 255;
if (p > 255) p = 255;
out_row[x] = (byte)p;
}
}
}
}
b.UnlockBits(bdata);