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.columns != null and
self.lines != null and
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;
self.frames.items.len >= 1)
{
// v8 may have breaking changes like changing the colormap xy direction
// (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
if (!(eql(u8, "16", self.colorFormat.?) or eql(u8, "256", self.colorFormat.?)))
@@ -99,15 +97,13 @@ const DurFormat = struct {
var dur_movie = if (dur_json_root.object.get("DurMovie")) |dm| dm.object else return error.NotValidFile;
// Depending on the version, a dur file can have different json object names (ie: columns vs sizeX)
self.formatVersion = if (dur_movie.get("formatVersion"))|x| x.integer 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.framerate = if (dur_movie.get("framerate")) |x| x.float else null;
self.columns = if (dur_movie.get("columns")) |x| x.integer
else if (dur_movie.get("sizeX")) |x| x.integer else null;
self.formatVersion = if (dur_movie.get("formatVersion")) |x| x.integer 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.framerate = if (dur_movie.get("framerate")) |x| x.float else null;
self.columns = if (dur_movie.get("columns")) |x| x.integer else if (dur_movie.get("sizeX")) |x| x.integer else null;
self.lines = if (dur_movie.get("lines")) |x| x.integer
else if (dur_movie.get("sizeY")) |x| x.integer else null;
self.lines = if (dur_movie.get("lines")) |x| x.integer else if (dur_movie.get("sizeY")) |x| x.integer else null;
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
// allocated memory making it difficult to deallocate without leaks
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)
};
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) };
for (0..frame.contents.len) |i| {
frame.contents[i] = try allocator.dupe(u8, frame_val.contents[i]);
@@ -149,7 +140,7 @@ const DurFormat = struct {
}
}
pub fn create_from_file(self: *DurFormat, allocator: Allocator, file_path: [] const u8) !void {
pub fn create_from_file(self: *DurFormat, allocator: Allocator, file_path: []const u8) !void {
const file_decompressed = try read_decompress_file(allocator, file_path);
defer allocator.free(file_decompressed);
@@ -158,7 +149,9 @@ const DurFormat = struct {
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 {
@@ -218,20 +211,20 @@ const rgb_color_16 = [16]u32{
// Made this table from looking at colormapping in dur source, not sure whats going on with the mapping logic
// Array indexes are dur colormappings which value maps to indexes in table above. Only needed for dur 16 color
const durcolor_table_to_color16 = [17]u32{
0, // 0 black
0, // 1 nothing?? dur source did not say why 1 is unused
4, // 2 blue
2, // 3 green
6, // 4 cyan
1, // 5 red
5, // 6 magenta
3, // 7 yellow
7, // 8 light gray
8, // 9 gray
0, // 0 black
0, // 1 nothing?? dur source did not say why 1 is unused
4, // 2 blue
2, // 3 green
6, // 4 cyan
1, // 5 red
5, // 6 magenta
3, // 7 yellow
7, // 8 light gray
8, // 9 gray
12, // 10 bright blue
10, // 11 bright green
14, // 12 bright cyan
9, // 13 bright red
9, // 13 bright red
13, // 14 bright magenta
11, // 15 bright yellow
15, // 16 bright white
@@ -293,7 +286,6 @@ fn convert_256_to_rgb(color_256: u32) u32 {
return rgb_color;
}
const DurFile = @This();
allocator: Allocator,
@@ -307,15 +299,9 @@ dur_movie: DurFormat,
frame_width: u32,
frame_height: u32,
frame_time: u32,
is_color_format_16 : bool,
is_color_format_16: bool,
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 {
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 {
var dur_movie: DurFormat = .init(allocator);
// 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_height = frame_height,
.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| {
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_1: u32 = @intCast(current_frame.colorMap[x][y][1]);
var color_map_0: u32 = @intCast(if (color_map[0] == -1) 0 else color_map[0]);
var color_map_1: u32 = @intCast(if (color_map[1] == -1) 0 else color_map[1]);
if (self.is_color_format_16) {
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 bg_color = if (self.full_color) convert_256_to_rgb(color_map_1) else tb_color_16[color_map_1];
const cell = Cell {
.ch = @intCast(codepoint),
.fg = fg_color,
.bg = bg_color
};
const cell = Cell{ .ch = @intCast(codepoint), .fg = fg_color, .bg = bg_color };
cell.put(x + self.x_offset, y + self.y_offset);
}