media: Update to zig 0.16.0, harden tests and add test build step

This commit is contained in:
2026-05-13 00:11:38 +02:00
parent 380145a986
commit dc839e098c
12 changed files with 200 additions and 44 deletions

View File

@@ -27,7 +27,7 @@ pub const Header = struct {
filter_method: FilterMethod,
interlace_method: InterlaceMethod,
pub fn decode(chunk: Chunk) Header {
pub fn decode(chunk: Chunk) !Header {
std.debug.assert(chunk.chunk_type == .IHDR);
if (chunk.data.len != 13) return error.InvalidPng;
@@ -79,6 +79,10 @@ pub const Header = struct {
.interlace_method = @enumFromInt(interlace_method),
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
pub const BitDepth = enum(u8) {
@@ -89,7 +93,11 @@ pub const BitDepth = enum(u8) {
@"16" = 16,
pub fn range(self: BitDepth) usize {
return @as(usize, 2) << @intFromEnum(@intFromEnum(self));
return @as(usize, 2) << @intCast(@intFromEnum(self));
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
@@ -111,6 +119,10 @@ pub const ColorType = enum(u8) {
pub fn alphaChannelUsed(self: ColorType) bool {
return @intFromEnum(self) & 0b0000_0100 != 0;
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
pub const CompressionMethod = enum(u8) {
@@ -156,6 +168,10 @@ pub const Palette = struct {
.entries = entries,
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// --- tRNS --------------------------------------------------------------------
@@ -209,6 +225,10 @@ pub const Transparency = union(enum) {
.rgba => error.InvalidPng,
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
const TransparencyPalette = struct {
@@ -218,6 +238,10 @@ const TransparencyPalette = struct {
pub fn asSlice(self: *const TransparencyPalette) []const u8 {
return self.entries[0..self.len];
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// --- gAMA --------------------------------------------------------------------
@@ -245,6 +269,10 @@ const Gamma = struct {
const gamma = std.mem.readInt(u32, chunk.data[0..4], .big);
return .{ .gamma = gamma };
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// --- cHRM --------------------------------------------------------------------
@@ -274,6 +302,10 @@ const Chromaticities = struct {
.blue_y = std.mem.readInt(u32, chunk.data[28..32], .big),
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// --- sRGB --------------------------------------------------------------------
@@ -301,6 +333,10 @@ const RenderingIntent = enum(u8) {
if (rendering_intent > 3) return error.InvalidPng;
return @enumFromInt(rendering_intent);
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// --- pHYs --------------------------------------------------------------------
@@ -310,7 +346,7 @@ pub const PhysicalPixelDimensions = struct {
pixels_per_unit_y: u32,
unit: PhysicalUnit,
pub fn decode(chunk: Chunk) PhysicalPixelDimensions {
pub fn decode(chunk: Chunk) !PhysicalPixelDimensions {
std.debug.assert(chunk.chunk_type == .pHYs);
if (chunk.data.len != 9) return error.InvalidPng;
@@ -328,6 +364,10 @@ pub const PhysicalPixelDimensions = struct {
.unit = @enumFromInt(unit),
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
pub const PhysicalUnit = enum(u8) {
@@ -381,41 +421,61 @@ pub const LastModificationTime = struct {
.second = second,
};
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
// -----------------------------------------------------------------------------
pub const ChunkType = enum(u32) {
IHDR = @bitCast("IHDR".*),
PLTE = @bitCast("PLTE".*),
IDAT = @bitCast("IDAT".*),
IEND = @bitCast("IEND".*),
tRNS = @bitCast("tRNS".*),
gAMA = @bitCast("gAMA".*),
cHRM = @bitCast("cHRM".*),
sRGB = @bitCast("sRGB".*),
iCCP = @bitCast("iCCP".*),
tEXt = @bitCast("tEXt".*),
zTXt = @bitCast("zTXt".*),
iTXt = @bitCast("iTXt".*),
bKGD = @bitCast("bKGD".*),
pHYs = @bitCast("pHYs".*),
sBIT = @bitCast("sBIT".*),
sPLT = @bitCast("sPLT".*),
hIST = @bitCast("hIST".*),
tIME = @bitCast("tIME".*),
IHDR = fromName("IHDR"),
PLTE = fromName("PLTE"),
IDAT = fromName("IDAT"),
IEND = fromName("IEND"),
tRNS = fromName("tRNS"),
gAMA = fromName("gAMA"),
cHRM = fromName("cHRM"),
sRGB = fromName("sRGB"),
iCCP = fromName("iCCP"),
tEXt = fromName("tEXt"),
zTXt = fromName("zTXt"),
iTXt = fromName("iTXt"),
bKGD = fromName("bKGD"),
pHYs = fromName("pHYs"),
sBIT = fromName("sBIT"),
sPLT = fromName("sPLT"),
hIST = fromName("hIST"),
tIME = fromName("tIME"),
_,
fn fromName(name: *const [4]u8) u32 {
return @bitCast(name.*);
}
pub fn fromBytes(bytes: *const [4]u8) ChunkType {
return @enumFromInt(fromName(bytes));
}
pub fn toBytes(self: ChunkType) [4]u8 {
return @bitCast(@intFromEnum(self));
}
pub fn ancillary(self: ChunkType) bool {
return @as([4]u8, @bitCast(self))[0] & 0b0010_0000 != 0;
return self.toBytes()[0] & 0b0010_0000 != 0;
}
pub fn private(self: ChunkType) bool {
return @as([4]u8, @bitCast(self))[1] & 0b0010_0000 != 0;
return self.toBytes()[1] & 0b0010_0000 != 0;
}
pub fn safeToCopy(self: ChunkType) bool {
return @as([4]u8, @bitCast(self))[3] & 0b0010_0000 != 0;
return self.toBytes()[3] & 0b0010_0000 != 0;
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
@@ -460,6 +520,10 @@ pub const StandardKeyword = enum {
pub fn isStandardKeyword(keyword: []const u8) ?StandardKeyword {
return map.get(keyword);
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}
};
/// The caller asserts that the buffer is at least `format.magic_length` bytes
@@ -477,7 +541,7 @@ pub fn info(buffer: []const u8) ?Header {
return null;
}
const chunk, _ = decodeChunk(buffer[format.magic_length..]) catch return null orelse return null;
const chunk, _ = (decodeChunk(buffer[format.magic_length..]) catch return null) orelse return null;
if (chunk.chunk_type != .IHDR) {
return null;
}
@@ -495,7 +559,7 @@ pub fn decodeChunk(buffer: []const u8) !?struct { Chunk, []const u8 } {
}
const length = std.mem.readInt(u32, rest[0..4], .big);
const chunk_type: ChunkType = @bitCast(rest[4..8].*);
const chunk_type: ChunkType = .fromBytes(rest[4..8]);
rest = rest[8..];
if (length > 0x7FFF_FFFF) {
@@ -575,3 +639,7 @@ pub fn encodeChunks(chunks: []const Chunk, writer: *std.Io.Writer) !void {
try writer.writeInt(u32, crc.final(), .big);
}
}
test "refAllDecls" {
std.testing.refAllDecls(@This());
}