Only support dur format v7, set -1 color to black

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2025-12-06 10:05:41 +01:00
parent 92beb24c80
commit a9ff0a6d07

View File

@@ -69,13 +69,11 @@ const DurFormat = struct {
self.framerate != null and self.framerate != null and
self.columns != null and self.columns != null and
self.lines != null and self.lines != null and
self.frames.items.len >= 1) { self.frames.items.len >= 1)
{
// Oldest example in dur repo was 5 so unsure if older changes json layout
if (self.formatVersion.? < 5) return false;
// v8 may have breaking changes like changing the colormap xy direction // v8 may have breaking changes like changing the colormap xy direction
// (https://github.com/cmang/durdraw/issues/24) // (https://github.com/cmang/durdraw/issues/24)
if (self.formatVersion.? > 7) return false; if (self.formatVersion.? != 7) return false;
// Code currently only supports 16 and 256 color format only // Code currently only supports 16 and 256 color format only
if (!(eql(u8, "16", self.colorFormat.?) or eql(u8, "256", self.colorFormat.?))) if (!(eql(u8, "16", self.colorFormat.?) or eql(u8, "256", self.colorFormat.?)))
@@ -103,11 +101,9 @@ const DurFormat = struct {
self.colorFormat = if (dur_movie.get("colorFormat")) |x| try allocator.dupe(u8, x.string) else null; self.colorFormat = if (dur_movie.get("colorFormat")) |x| try allocator.dupe(u8, x.string) else null;
self.encoding = if (dur_movie.get("encoding")) |x| try allocator.dupe(u8, x.string) else null; self.encoding = if (dur_movie.get("encoding")) |x| try allocator.dupe(u8, x.string) else null;
self.framerate = if (dur_movie.get("framerate")) |x| x.float else null; self.framerate = if (dur_movie.get("framerate")) |x| x.float else null;
self.columns = if (dur_movie.get("columns")) |x| x.integer self.columns = if (dur_movie.get("columns")) |x| x.integer else if (dur_movie.get("sizeX")) |x| x.integer else null;
else if (dur_movie.get("sizeX")) |x| x.integer else null;
self.lines = if (dur_movie.get("lines")) |x| x.integer self.lines = if (dur_movie.get("lines")) |x| x.integer else if (dur_movie.get("sizeY")) |x| x.integer else null;
else if (dur_movie.get("sizeY")) |x| x.integer else null;
const frames = dur_movie.get("frames") orelse return error.NotValidFile; const frames = dur_movie.get("frames") orelse return error.NotValidFile;
@@ -121,12 +117,7 @@ const DurFormat = struct {
// copy all fields to own the ptrs for deallocation, the parsed_frame has some other // copy all fields to own the ptrs for deallocation, the parsed_frame has some other
// allocated memory making it difficult to deallocate without leaks // allocated memory making it difficult to deallocate without leaks
const frame: Frame = .{ const frame: Frame = .{ .frameNumber = frame_val.frameNumber, .delay = frame_val.delay, .contents = try allocator.alloc([]u8, frame_val.contents.len), .colorMap = try allocator.alloc([][]i32, frame_val.colorMap.len) };
.frameNumber = frame_val.frameNumber,
.delay = frame_val.delay,
.contents = try allocator.alloc([]u8, frame_val.contents.len),
.colorMap = try allocator.alloc([][]i32, frame_val.colorMap.len)
};
for (0..frame.contents.len) |i| { for (0..frame.contents.len) |i| {
frame.contents[i] = try allocator.dupe(u8, frame_val.contents[i]); frame.contents[i] = try allocator.dupe(u8, frame_val.contents[i]);
@@ -158,7 +149,9 @@ const DurFormat = struct {
try parse_dur_from_json(self, allocator, parsed.value); try parse_dur_from_json(self, allocator, parsed.value);
if (!self.valid()) { return error.NotValidFile; } if (!self.valid()) {
return error.NotValidFile;
}
} }
pub fn init(allocator: Allocator) DurFormat { pub fn init(allocator: Allocator) DurFormat {
@@ -293,7 +286,6 @@ fn convert_256_to_rgb(color_256: u32) u32 {
return rgb_color; return rgb_color;
} }
const DurFile = @This(); const DurFile = @This();
allocator: Allocator, allocator: Allocator,
@@ -309,13 +301,7 @@ frame_height: u32,
frame_time: u32, frame_time: u32,
is_color_format_16: bool, is_color_format_16: bool,
pub fn init(allocator: Allocator, pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: *std.io.Writer, file_path: []const u8, x_offset: u32, y_offset: u32, full_color: bool) !DurFile {
terminal_buffer: *TerminalBuffer,
log_writer: *std.io.Writer,
file_path: []const u8,
x_offset: u32,
y_offset: u32,
full_color: bool) !DurFile {
var dur_movie: DurFormat = .init(allocator); var dur_movie: DurFormat = .init(allocator);
// error state is recoverable when thrown to main and results in no background with Dummy in main // error state is recoverable when thrown to main and results in no background with Dummy in main
@@ -367,7 +353,7 @@ pub fn init(allocator: Allocator,
.frame_width = frame_width, .frame_width = frame_width,
.frame_height = frame_height, .frame_height = frame_height,
.frame_time = frame_time, .frame_time = frame_time,
.is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16") .is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16"),
}; };
} }
@@ -389,9 +375,10 @@ fn draw(self: *DurFile) void {
for (0..self.frame_width) |x| { for (0..self.frame_width) |x| {
const codepoint: u21 = iter.nextCodepoint().?; const codepoint: u21 = iter.nextCodepoint().?;
const color_map = current_frame.colorMap[x][y];
var color_map_0: u32 = @intCast(current_frame.colorMap[x][y][0]); var color_map_0: u32 = @intCast(if (color_map[0] == -1) 0 else color_map[0]);
var color_map_1: u32 = @intCast(current_frame.colorMap[x][y][1]); var color_map_1: u32 = @intCast(if (color_map[1] == -1) 0 else color_map[1]);
if (self.is_color_format_16) { if (self.is_color_format_16) {
color_map_0 = durcolor_table_to_color16[color_map_0]; color_map_0 = durcolor_table_to_color16[color_map_0];
@@ -401,11 +388,7 @@ fn draw(self: *DurFile) void {
const fg_color = if (self.full_color) convert_256_to_rgb(color_map_0) else tb_color_16[color_map_0]; const fg_color = if (self.full_color) convert_256_to_rgb(color_map_0) else tb_color_16[color_map_0];
const bg_color = if (self.full_color) convert_256_to_rgb(color_map_1) else tb_color_16[color_map_1]; const bg_color = if (self.full_color) convert_256_to_rgb(color_map_1) else tb_color_16[color_map_1];
const cell = Cell { const cell = Cell{ .ch = @intCast(codepoint), .fg = fg_color, .bg = bg_color };
.ch = @intCast(codepoint),
.fg = fg_color,
.bg = bg_color
};
cell.put(x + self.x_offset, y + self.y_offset); cell.put(x + self.x_offset, y + self.y_offset);
} }