Skip to content

Commit

Permalink
Command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbradsmith committed Oct 11, 2024
1 parent f85245a commit 61a162f
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 26 deletions.
184 changes: 159 additions & 25 deletions BinxelviewForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,130 @@ public bool loadFile(string path)
}
};

//
// Options
//

string parseOption(string optline)
{
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 == "PRESETFILE") // load preset file to replace default
{
Preset p = new Preset();
if (!p.loadFile(val)) return "Could not load preset file: "+val+"\n"+Preset.last_error;
default_preset = p;
preset = default_preset.copy();
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 == "PAL") // load palette file
{
bool image =
valu.EndsWith(".BMP") ||
valu.EndsWith(".GIF") ||
valu.EndsWith(".PNG") ||
valu.EndsWith(".TIF");
bool vga =
valu.EndsWith(".VGA");
if (!openPalette(val,image,vga)) return "Could not load palette file: "+val+"\n"+palette_error;
return "";
}
if (opt == "AUTOPAL")
{
if (valu == "RGB" ) { palette_mode = PaletteMode.PALETTE_RGB; }
else if (valu == "RANDOM" ) { palette_mode = PaletteMode.PALETTE_RANDOM; }
else if (valu == "GREYSCALE") { palette_mode = PaletteMode.PALETTE_GREY; }
else if (valu == "CUBEHELIX") { palette_mode = PaletteMode.PALETTE_CUBEHELIX; }
else return "Unknown autopal value: "+val;
comboBoxPalette.SelectedIndex = (int)palette_mode - 1;
return "";
}
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;
numericZoom.Value = 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);
gridToolStripMenuItem.Checked = !hidegrid;
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);
decimalPositionToolStripMenuItem.Checked = decimal_position;
hexadecimalPositionToolStripMenuItem.Checked = !decimal_position;
numericPosByte.Font = decimal_position ? posfont_regular : posfont_bold;
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);
snapScrollToNextStrideToolStripMenuItem.Checked = snap_scroll;
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);
verticalLayoutToolStripMenuItem.Checked = !horizontal_layout;
horizontalLayoutToolStripMenuItem.Checked = horizontal_layout;
return "";
}
if (opt == "TWIDDLE")
{
int v;
if (!int.TryParse(val,out v)) return "Could not parse integer for twiddle: "+val;
if (v < 0 || v > 2) return "Twiddle value must be 0, 1 or 2: "+val;
twiddle = v;
twiddleZToolStripMenuItem.Checked = twiddle == 1;
twiddleNToolStripMenuItem.Checked = twiddle == 2;
return "";
}
return "Invalid option: "+optline;
}

//
// Pixel building
//
Expand Down Expand Up @@ -744,11 +868,7 @@ Color getPalette(long x)
{
if (preset.bpp <= PALETTE_BITS) return palette[x];
int p = autoPaletteRaw(x);
int b = (p >> 0) & 0xFF;
int g = (p >> 8) & 0xFF;
int r = (p >> 16) & 0xFF;
int a = (p >> 24) & 0xFF;
return Color.FromArgb(a,r,g,b);
return Color.FromArgb(p);
}

void randomPalette()
Expand Down Expand Up @@ -850,7 +970,7 @@ bool openFile(string path)
}
catch (Exception ex)
{
MessageBox.Show("Unable to open file:\n" + path + "\n\n" + ex.ToString(), "File open error!");
MessageBox.Show("Unable to open file:\n" + path + "\n\n" + ex.ToString(), "Binxelview");
return false;
}
data = read_data;
Expand Down Expand Up @@ -925,11 +1045,6 @@ void reloadPresets()

bool openPalette(string path, bool image, bool sixbit_vga)
{
if (preset.bpp > PALETTE_BITS)
{
palette_error = String.Format("Custom palettes are limited to {0} BPP.",PALETTE_BITS);
return false;
}
palette_mode = PaletteMode.PALETTE_CUSTOM;

if (image)
Expand All @@ -951,7 +1066,7 @@ bool openPalette(string path, bool image, bool sixbit_vga)
palette_error = "Image does not contain a palette.";
return false;
}
for (int i=0; (i<(1<<preset.bpp)) && (i<cols.Length); ++i)
for (int i=0; (i<(PALETTE_DIM*PALETTE_DIM)) && (i<cols.Length); ++i)
{
Color c = cols[i];
setPalette(i, c.R, c.G, c.B);
Expand All @@ -970,7 +1085,7 @@ bool openPalette(string path, bool image, bool sixbit_vga)
return false;
}

for (int i=0; (i<(1<<preset.bpp)) && (((i*3)+2)<read_data.Length); ++i)
for (int i=0; (i<(PALETTE_DIM*PALETTE_DIM)) && (((i*3)+2)<read_data.Length); ++i)
{
int r = read_data[(i * 3) + 0];
int g = read_data[(i * 3) + 1];
Expand Down Expand Up @@ -1200,16 +1315,35 @@ private void BinxelviewForm_Load(object sender, EventArgs e)
// initialize Auto palette selection
comboBoxPalette.SelectedIndex = (int)PaletteMode.PALETTE_RGB - 1;

// setup presets
default_preset.empty();
reloadPresets(); // loads "Default" preset if it exists
preset = default_preset.copy();

// open file from the command line
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
string arg_err = "";
for (int i=1; i<args.Length; ++i)
{
string arg = args[i];
if (!arg.StartsWith("-")) // anything that doesn't start with - is the file to open
{
openFile(arg);
}
else // anything that starts with - or -- is an option
{
string sarg = arg.Substring(1);
if (sarg.StartsWith("-")) sarg = sarg.Substring(1);
string opt_err = parseOption(sarg);
if (opt_err.Length > 0 && arg_err.Length > 0) arg_err += "\n";
arg_err += opt_err;
}
}
if (arg_err.Length > 0)
{
openFile(args[1]);
MessageBox.Show("Command line error:\n" + arg_err,"Binxelview");
}

default_preset.empty();
reloadPresets(); // loads "Default" preset if it exists
preset = default_preset.copy();
scrollRange();
redrawPreset();
autoPalette();
Expand Down Expand Up @@ -1288,7 +1422,7 @@ private void buttonLoadPreset_Click(object sender, EventArgs e)
}
else
{
MessageBox.Show("Unable to load preset:\n" + d.FileName + "\n\n" + Preset.last_error, "Preset load error!");
MessageBox.Show("Unable to load preset:\n" + d.FileName + "\n\n" + Preset.last_error, "Binxelview");
}
}
}
Expand All @@ -1303,7 +1437,7 @@ private void buttonSavePreset_Click(object sender, EventArgs e)
{
if (!preset.saveFile(d.FileName))
{
MessageBox.Show("Unable to save preset:\n" + d.FileName + "\n\n" + Preset.last_error, "Preset save error!");
MessageBox.Show("Unable to save preset:\n" + d.FileName + "\n\n" + Preset.last_error, "Binxelview");
}
else
{
Expand Down Expand Up @@ -1537,7 +1671,7 @@ private void buttonLoadPal_Click(object sender, EventArgs e)
}
else
{
MessageBox.Show("Unable to load palette:\n" + d.FileName + "\n\n" + palette_error, "Palette load error!");
MessageBox.Show("Unable to load palette:\n" + d.FileName + "\n\n" + palette_error, "Binxelview");
}
}
}
Expand All @@ -1554,7 +1688,7 @@ private void buttonSavePal_Click(object sender, EventArgs e)
{
if (!savePalette(d.FileName))
{
MessageBox.Show("Unable to save palette:\n" + d.FileName + "\n\n" + palette_error, "Palette save error!");
MessageBox.Show("Unable to save palette:\n" + d.FileName + "\n\n" + palette_error, "Binxelview");
}
}
}
Expand Down Expand Up @@ -1607,7 +1741,7 @@ private void saveImageContextItem_Click(object sender, EventArgs e)

if (selected_tile < 0) // shouldn't happen, but giving an error just in case
{
MessageBox.Show("No image selected?", "Image save error!");
MessageBox.Show("No image selected?", "Binxelview");
return;
}

Expand Down Expand Up @@ -1642,7 +1776,7 @@ private void saveImageContextItem_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
MessageBox.Show("Unable to save image:\n" + d.FileName + "\n\n" + ex.ToString(), "Image save error!");
MessageBox.Show("Unable to save image:\n" + d.FileName + "\n\n" + ex.ToString(), "Binxelview");
}

redrawPixels();
Expand Down Expand Up @@ -1686,7 +1820,7 @@ private void saveAllVisibleToolStripMenuItem_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
MessageBox.Show("Unable to save image:\n" + d.FileName + "\n\n" + ex.ToString(), "Image save error!");
MessageBox.Show("Unable to save image:\n" + d.FileName + "\n\n" + ex.ToString(), "Binxelview");
}

redrawPixels();
Expand Down
53 changes: 52 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,56 @@ Morton (Z/N) ordering, commonly seen in square textures "twiddled" or "swizzled"
GPU cache coherence.


Command Line Options
--------------------

To open a file, just add that file's path to the command line.

Options can be set with a command line argument beginning with '-',
followed by the option name, an '=' separator, and the value for the option.
If a space is required in the value, you should enclose the entire argument in quotes.

"-presetfile=C:\mypreset.bxp"
Replaces the default preset with one from a file.

"-preset=Atari ST 4BPP"
Chooses a named preset from your preset library instead of the default.

"-pal=C:\red.pal"
Loads a palette file.
If the extension is .BMP .GIF .PNG or .TIF it will load it as an palette from an image.
If the extension is .VGA it will load it as 6-bit RGB18 format.
Otherwise it will load it as RGB24.

-autopal=Greyscale
Chooses an automatic palette, one of:
RGB - RGB with the current preset BPP, bits evenly divided with G >= R >= B, red as the highest bits.
Random - Every colour is randomized.
Greyscale - Gradient from black to white.
Cubehelix - Smooth gradient with rotating hue, created by Dave Green. See: https://people.phy.cam.ac.uk/dag9/CUBEHELIX/

-background=FF0088
Set the Background grid colour, uses a 6 digit hexadecimal RRGGBB value.

-zoom=3
Set the Zoom value.

-grid=1
Set the Grid Padding option (1 on, 0 off).

-hexpos=1
Set the Position byte display to use hexadecimal (1 hexadecimal, 0 decimal).

-snapscroll=1
Set the "Snap scroll to next stride" option (1 on, 0 off).

-horizontal=1
Set the layout option (1 horizontal, 0 vertical).

-twiddle=1
Set the twiddle option (0 off, 1 twiddle Z, 2 twiddle N).


Changes
-------

Expand All @@ -178,12 +228,13 @@ Changes
- Right click context menu option to move position to the selected pixel.
- File menu reload option.
- Added global Ctrl hotkeys.
- Added Cubehelix automatic palette option: https://people.phy.cam.ac.uk/dag9/CUBEHELIX/
- Added Cubehelix automatic palette option.
- Automatic palette modes are now a dropdown list.
- Right click context menu option to copy to the palette starting from the selected pixel.
- PS1 15BPP and 4BPP presets. (Contributor: HeyItsLollie)
- VGA Palette preset.
- Indicate hexadecimal position with bold font.
- Command line arguments for options.

1.5.0.0 (2020-07-31)
- Twiddle option for inspecting textures stored with morton ordering of pixels.
Expand Down

0 comments on commit 61a162f

Please sign in to comment.