Compare commits
5 commits
eeed063d0d
...
276100c556
Author | SHA1 | Date | |
---|---|---|---|
276100c556 | |||
1b8784dc8f | |||
e2f9566529 | |||
87459939cf | |||
41a8b18d66 |
4 changed files with 253 additions and 6 deletions
73
src/image.zig
Normal file
73
src/image.zig
Normal file
|
@ -0,0 +1,73 @@
|
|||
const std = @import("std");
|
||||
const c = @cImport({
|
||||
@cInclude("sndfile.h");
|
||||
});
|
||||
|
||||
pub const ImageError = error{OpenFail};
|
||||
|
||||
/// Low level integration function with libsndfile.
|
||||
fn sopen(
|
||||
allocator: *std.mem.Allocator,
|
||||
path: []const u8,
|
||||
mode: i32,
|
||||
fmt: *c.SF_INFO,
|
||||
) !*c.SNDFILE {
|
||||
var cstr_path = try std.cstr.addNullByte(allocator, path);
|
||||
defer allocator.free(cstr_path);
|
||||
|
||||
var file = c.sf_open(cstr_path.ptr, mode, fmt);
|
||||
const st: i32 = c.sf_error(file);
|
||||
|
||||
if (st != 0) {
|
||||
std.debug.warn(
|
||||
"Failed to open {} ({})\n",
|
||||
path,
|
||||
c.sf_error_number(st),
|
||||
);
|
||||
|
||||
return ImageError.OpenFail;
|
||||
}
|
||||
|
||||
return file.?;
|
||||
}
|
||||
|
||||
pub const Image = struct {
|
||||
allocator: *std.mem.Allocator,
|
||||
sndfile: *c.SNDFILE,
|
||||
path: []const u8,
|
||||
|
||||
/// Open a BMP file.
|
||||
pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image {
|
||||
var in_fmt = c.SF_INFO{
|
||||
.frames = c_int(0),
|
||||
.samplerate = c_int(44100),
|
||||
.channels = c_int(1),
|
||||
.format = c.SF_FORMAT_ULAW | c.SF_FORMAT_RAW | c.SF_ENDIAN_BIG,
|
||||
.sections = c_int(0),
|
||||
.seekable = c_int(0),
|
||||
};
|
||||
|
||||
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
|
||||
var image = try allocator.create(Image);
|
||||
|
||||
image.* = Image{
|
||||
.allocator = allocator,
|
||||
.sndfile = sndfile,
|
||||
.path = path,
|
||||
};
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
pub fn close(self: *Image) void {
|
||||
var st: i32 = c.sf_close(self.sndfile);
|
||||
|
||||
if (st != 0) {
|
||||
std.debug.warn(
|
||||
"Failed to close {} ({})\n",
|
||||
self.path,
|
||||
c.sf_error_number(st),
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -52,6 +52,7 @@ pub const Lang = struct {
|
|||
|
||||
while (splitted_it.next()) |stmt_orig| {
|
||||
var stmt = std.mem.trimRight(u8, stmt_orig, "\n");
|
||||
stmt = std.mem.trimLeft(u8, stmt, "\n");
|
||||
|
||||
if (stmt.len == 0) continue;
|
||||
|
||||
|
|
17
src/main.zig
17
src/main.zig
|
@ -17,16 +17,18 @@
|
|||
|
||||
const std = @import("std");
|
||||
const langs = @import("lang.zig");
|
||||
const runners = @import("runner.zig");
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("assert.h");
|
||||
@cInclude("math.h");
|
||||
@cInclude("sndfile.h");
|
||||
@cInclude("stdarg.h");
|
||||
@cInclude("stdio.h");
|
||||
@cInclude("stdlib.h");
|
||||
@cInclude("string.h");
|
||||
|
||||
@cInclude("sndfile.h");
|
||||
|
||||
@cInclude("lilv/lilv.h");
|
||||
@cInclude("lv2/core/lv2.h");
|
||||
|
||||
|
@ -301,11 +303,16 @@ fn print_usage() !void {
|
|||
|
||||
pub fn main() !void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const allocator = &arena.allocator;
|
||||
|
||||
var lang = langs.Lang.init(allocator);
|
||||
//defer lang.deinit();
|
||||
|
||||
var runner = runners.Runner.init(allocator);
|
||||
defer runner.deinit();
|
||||
|
||||
var args_it = std.process.args();
|
||||
|
||||
const exe_name = try (args_it.next(allocator) orelse @panic("expected exe name"));
|
||||
|
@ -324,10 +331,7 @@ pub fn main() !void {
|
|||
_ = try file.read(data);
|
||||
|
||||
var cmds = try lang.parse(data);
|
||||
var it = cmds.iterator();
|
||||
while (it.next()) |cmd| {
|
||||
cmd.print();
|
||||
}
|
||||
try runner.runCommands(cmds, true);
|
||||
}
|
||||
|
||||
pub fn oldMain() !void {
|
||||
|
@ -447,7 +451,8 @@ pub fn oldMain() !void {
|
|||
var out_fmt = c.SF_INFO{
|
||||
.frames = c_int(0),
|
||||
.samplerate = c_int(44100),
|
||||
.channels = @intCast(c_int, self.n_audio_out),
|
||||
//.channels = @intCast(c_int, self.n_audio_out),
|
||||
.channels = c_int(1),
|
||||
.format = c.SF_FORMAT_ULAW | c.SF_FORMAT_RAW,
|
||||
.sections = c_int(0),
|
||||
.seekable = c_int(0),
|
||||
|
|
168
src/runner.zig
Normal file
168
src/runner.zig
Normal file
|
@ -0,0 +1,168 @@
|
|||
const std = @import("std");
|
||||
const lang = @import("lang.zig");
|
||||
const images = @import("image.zig");
|
||||
|
||||
const Image = images.Image;
|
||||
|
||||
pub const RunError = error{
|
||||
UnknownCommand,
|
||||
NoBMP,
|
||||
ImageRequired,
|
||||
};
|
||||
|
||||
pub const Runner = struct {
|
||||
allocator: *std.mem.Allocator,
|
||||
image: ?*Image = null,
|
||||
|
||||
pub fn init(allocator: *std.mem.Allocator) Runner {
|
||||
return Runner{ .allocator = allocator };
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Runner) void {
|
||||
if (self.image) |image| {
|
||||
image.close();
|
||||
}
|
||||
}
|
||||
|
||||
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},
|
||||
);
|
||||
|
||||
return resolved_path;
|
||||
}
|
||||
|
||||
fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void {
|
||||
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;
|
||||
}
|
||||
|
||||
self.image = try Image.open(self.allocator, load_path);
|
||||
}
|
||||
|
||||
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 => {},
|
||||
}
|
||||
}
|
||||
|
||||
const out_path = try std.fmt.allocPrint(self.allocator, "{}_g{}{}", starts_with, max + 1, extension);
|
||||
std.debug.warn("out path: {}\n", out_path);
|
||||
}
|
||||
|
||||
fn runCommand(self: *Runner, cmd: *lang.Command) !void {
|
||||
return switch (cmd.command) {
|
||||
.Noop => {},
|
||||
.Load => blk: {
|
||||
var path = cmd.args.at(0);
|
||||
try self.loadCmd(path);
|
||||
break :blk;
|
||||
},
|
||||
|
||||
.Quicksave => try self.quicksaveCmd(),
|
||||
else => blk: {
|
||||
std.debug.warn("Unknown command: {}\n", cmd.command);
|
||||
break :blk RunError.UnknownCommand;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// Run a list of commands.
|
||||
pub fn runCommands(
|
||||
self: *Runner,
|
||||
cmds: lang.CommandList,
|
||||
debug_flag: bool,
|
||||
) !void {
|
||||
var it = cmds.iterator();
|
||||
|
||||
while (it.next()) |cmd| {
|
||||
if (debug_flag) cmd.print();
|
||||
try self.runCommand(cmd);
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue