scritcher/src/runner.zig

239 lines
7.5 KiB
Zig
Raw Normal View History

2019-07-08 15:38:16 +00:00
const std = @import("std");
const lang = @import("lang.zig");
const images = @import("image.zig");
2019-07-09 01:40:52 +00:00
const plugin = @import("plugin.zig");
2019-07-08 15:38:16 +00:00
const Image = images.Image;
pub const RunError = error{
UnknownCommand,
NoBMP,
ImageRequired,
};
2019-07-08 15:38:16 +00:00
pub const Runner = struct {
allocator: *std.mem.Allocator,
image: ?*Image = null,
2019-07-08 15:38:16 +00:00
pub fn init(allocator: *std.mem.Allocator) Runner {
2019-07-09 03:04:01 +00:00
return Runner{
.allocator = allocator,
};
2019-07-08 15:38:16 +00:00
}
pub fn deinit(self: *Runner) void {
if (self.image) |image| {
image.close();
}
}
2019-07-08 16:55:54 +00:00
fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 {
if (load_path[0] == ':') {
// parse the index from 1 to end
const index = try std.fmt.parseInt(usize, load_path[1..], 10);
var args_it = std.process.args();
_ = args_it.skip();
var i: usize = 0;
while (i <= index) : (i += 1) {
_ = args_it.skip();
}
const arg = try (args_it.next(self.allocator) orelse @panic("expected argument"));
return arg;
} else {
return load_path;
}
}
fn resolveArgPath(self: *Runner, path_or_argidx: []const u8) ![]const u8 {
const path = try self.resolveArg(path_or_argidx);
const resolved_path = try std.fs.path.resolve(
self.allocator,
[_][]const u8{path},
);
2019-07-08 16:55:54 +00:00
return resolved_path;
}
fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void {
2019-07-08 16:55:54 +00:00
var load_path = try self.resolveArgPath(path_or_argidx);
std.debug.warn("load path: {}\n", load_path);
// we could use ImageMagick to convert from X to BMP
// but i can't find an easy way to do things in memory.
// the upside is that this allows some pre-processing by the user
// before loading the file into scritcher. for example, you can start
// krita/gimp and make it export a bmp and while in the program you can
// apply filters, etc.
if (!std.mem.endsWith(u8, load_path, ".bmp")) {
std.debug.warn("Only BMP files are allowed to be loaded.\n");
return RunError.NoBMP;
}
2019-07-10 15:00:43 +00:00
// we don't copy load_path into a temporary file because we're already
// loading it under the SFM_READ mode, which won't cause any destructive
// operations on the file.
self.image = try Image.open(self.allocator, load_path);
2019-07-08 16:55:54 +00:00
}
fn getImage(self: *Runner) !*Image {
if (self.image) |image| {
return image;
} else {
std.debug.warn("image is required!\n");
return RunError.ImageRequired;
}
}
fn quicksaveCmd(self: *Runner) !void {
// we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where
// N is the maximum non-used integer.
var image = try self.getImage();
const basename = std.fs.path.basename(image.path);
const dirname = std.fs.path.dirname(image.path).?;
var dir = try std.fs.Dir.open(self.allocator, dirname);
defer dir.close();
const period_idx = std.mem.lastIndexOf(u8, basename, ".").?;
const extension = basename[period_idx..basename.len];
// starts_with would be "x_g", we want to find all files in the directory
// that start with that name.
const starts_with = try std.fmt.allocPrint(
self.allocator,
"{}_g",
basename[0..period_idx],
);
var max: usize = 0;
while (try dir.next()) |entry| {
switch (entry.kind) {
.File => blk: {
if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {};
// we want to get the N in x_gN.ext
const entry_gidx = std.mem.lastIndexOf(u8, entry.name, "_g").?;
const entry_pidx_opt = std.mem.lastIndexOf(u8, entry.name, ".");
if (entry_pidx_opt == null) break :blk {};
const entry_pidx = entry_pidx_opt.?;
// if N isn't a number, we just ignore that file
const idx_str = entry.name[entry_gidx + 2 .. entry_pidx];
const idx = std.fmt.parseInt(usize, idx_str, 10) catch |err| {
break :blk {};
};
if (idx > max) max = idx;
},
else => {},
}
}
2019-07-10 02:04:05 +00:00
const out_path = try std.fmt.allocPrint(
self.allocator,
"{}/{}{}{}",
dirname,
starts_with,
max + 1,
extension,
);
try image.saveTo(out_path);
}
2019-07-09 01:40:52 +00:00
/// Run the http://lv2plug.in/plugins/eg-amp plugin over the file.
2019-07-09 03:04:01 +00:00
fn ampCmd(self: *Runner, split: usize, index: usize, gain: f32) !void {
var image = try self.getImage();
2019-07-09 01:40:52 +00:00
var params = plugin.ParamList.init(self.allocator);
defer params.deinit();
2019-07-10 16:24:03 +00:00
try params.append(plugin.Param{ .sym = "gain", .value = gain });
2019-07-09 18:15:02 +00:00
// TODO if we could detect that the next command is a quicksave then
// we don't need the temporary file in runPlugin and instead we
// dump it all on the output image for it.
2019-07-09 01:40:52 +00:00
try image.runPlugin(
"http://lv2plug.in/plugins/eg-amp",
plugin.Position{ .split = split, .index = index },
params,
);
}
2019-07-10 16:24:03 +00:00
fn rFlangerCmd(
self: *Runner,
split: usize,
index: usize,
delay_depth_avg: f32,
law_freq: f32,
) !void {
var image = try self.getImage();
var params = plugin.ParamList.init(self.allocator);
defer params.deinit();
try params.append(plugin.Param{ .sym = "delay_depth_avg", .value = delay_depth_avg });
try params.append(plugin.Param{ .sym = "law_freq", .value = law_freq });
try image.runPlugin(
"http://plugin.org.uk/swh-plugins/retroFlange",
plugin.Position{ .split = split, .index = index },
params,
);
}
2019-07-08 15:38:16 +00:00
fn runCommand(self: *Runner, cmd: *lang.Command) !void {
return switch (cmd.command) {
.Noop => {},
2019-07-08 16:55:54 +00:00
.Load => blk: {
var path = cmd.args.at(0);
try self.loadCmd(path);
break :blk;
},
.Quicksave => try self.quicksaveCmd(),
2019-07-09 01:40:52 +00:00
2019-07-09 03:04:01 +00:00
.Amp => blk: {
const split = try cmd.usizeArgAt(0);
const index = try cmd.usizeArgAt(1);
const gain = try cmd.floatArgAt(2);
try self.ampCmd(split, index, gain);
},
2019-07-09 01:40:52 +00:00
2019-07-10 16:24:03 +00:00
.RFlanger => blk: {
const split = try cmd.usizeArgAt(0);
const index = try cmd.usizeArgAt(1);
const delay_depth_avg = try cmd.floatArgAt(2);
const law_freq = try cmd.floatArgAt(3);
try self.rFlangerCmd(split, index, delay_depth_avg, law_freq);
},
2019-07-08 15:38:16 +00:00
else => blk: {
std.debug.warn("Unknown command: {}\n", cmd.command);
break :blk RunError.UnknownCommand;
},
};
}
/// Run a list of commands.
2019-07-08 16:13:03 +00:00
pub fn runCommands(
self: *Runner,
cmds: lang.CommandList,
debug_flag: bool,
) !void {
2019-07-08 15:38:16 +00:00
var it = cmds.iterator();
while (it.next()) |cmd| {
2019-07-08 16:13:03 +00:00
if (debug_flag) cmd.print();
2019-07-08 15:38:16 +00:00
try self.runCommand(cmd);
}
}
};