make background optional

This commit is contained in:
Emil Lerch 2023-03-30 09:30:00 -07:00
parent 556e14532f
commit 1de48ff236
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -17,15 +17,17 @@ const HEIGHT = 64;
const FONT_WIDTH = 5; const FONT_WIDTH = 5;
const FONT_HEIGHT = 8; const FONT_HEIGHT = 8;
const CHARS_PER_LINE = 21; // 21 * 6 = 126 so we have 2px left over const CHARS_PER_LINE = 25; // 25 * 5 = 125 so we have 3px left over
const BORDER_LEFT = 1; // 1 empty px left, 2 empty on right
const LINES = 8; const LINES = 8;
// Device specifications // Device specifications
const PAGES = 8; const PAGES = 8;
// const lines = [LINES][]const u8
fn usage(args: [][]u8) !void { fn usage(args: [][]u8) !void {
const stderr = std.io.getStdErr(); const stderr = std.io.getStdErr();
try stderr.writer().print("usage: {s} <image file> <device>\n", .{args[0]}); // TODO: will need more try stderr.writer().print("usage: {s} <device> [-bg <image file>]\n", .{args[0]}); // TODO: will need more
std.os.exit(1); std.os.exit(1);
} }
pub fn main() !void { pub fn main() !void {
@ -33,9 +35,9 @@ pub fn main() !void {
//defer alloc.deinit(); //defer alloc.deinit();
const args = try std.process.argsAlloc(alloc); const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args); defer std.process.argsFree(alloc, args);
if (args.len < 3) try usage(args); if (args.len < 2) try usage(args);
const prefix = "/dev/ttyUSB"; const prefix = "/dev/ttyUSB";
const device = try alloc.dupeZ(u8, args[2]); const device = try alloc.dupeZ(u8, args[1]);
defer alloc.free(device); defer alloc.free(device);
if (!std.mem.startsWith(u8, device, prefix)) try usage(args); if (!std.mem.startsWith(u8, device, prefix)) try usage(args);
@ -47,8 +49,22 @@ pub fn main() !void {
const stdout = bw.writer(); const stdout = bw.writer();
defer bw.flush() catch unreachable; // don't forget to flush! defer bw.flush() catch unreachable; // don't forget to flush!
const filename = args[1]; // Is this ceremony really needed?
try stdout.print("Converting {s}\n", .{filename}); var b: [1]u8 = undefined;
var filename: [:0]u8 = try std.fmt.bufPrintZ(&b, "", .{});
var is_filename = false;
for (args) |arg| {
if (std.mem.eql(u8, "-bg", arg)) {
is_filename = true;
continue;
}
if (is_filename) {
filename = arg;
break;
}
}
if (filename.len > 0) try stdout.print("Converting {s}\n", .{filename});
var pixels: [WIDTH * HEIGHT]u8 = undefined; var pixels: [WIDTH * HEIGHT]u8 = undefined;
try convertImage(alloc, filename, &pixels); try convertImage(alloc, filename, &pixels);
// try convertImage(alloc, filename, &pixels); // try convertImage(alloc, filename, &pixels);
@ -223,8 +239,13 @@ fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HE
// Reading an image into ImageMagick is problematic if it isn't a bmp // Reading an image into ImageMagick is problematic if it isn't a bmp
// as the library needs a bunch of dependencies available // as the library needs a bunch of dependencies available
// var status = c.MagickReadImage(mw, "logo:"); var status: c.MagickBooleanType = undefined;
var status = c.MagickReadImage(mw, filename); if (filename.len > 0) {
status = c.MagickReadImage(mw, filename);
} else {
const blob = @embedFile("images/blank.bmp");
status = c.MagickReadImageBlob(mw, blob, blob.len);
}
if (status == c.MagickFalse) { if (status == c.MagickFalse) {
if (!std.mem.eql(u8, filename[filename.len - 3 ..], "bmp")) if (!std.mem.eql(u8, filename[filename.len - 3 ..], "bmp"))
try std.io.getStdErr().writer().print("File is not .bmp. That is probably the problem\n", .{}); try std.io.getStdErr().writer().print("File is not .bmp. That is probably the problem\n", .{});
@ -275,7 +296,7 @@ fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HE
if (status == c.MagickFalse) if (status == c.MagickFalse)
return error.CouldNotSetExtent; return error.CouldNotSetExtent;
mw = try drawString(mw, "hello, world!", 30, 38); mw = try drawString(mw, "I like pizza!", 30, 38);
// We make the image monochrome by quantizing the image with 2 colors in the // We make the image monochrome by quantizing the image with 2 colors in the
// gray colorspace. See: // gray colorspace. See: