Something went wrong. Try again.
Qoi codec implemented in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138const std = @import("std");
const qoi = @import("qoi.zig");
const QoiEncodeError = error{ Todo,};
fn pixelEql(a: [4]u8, b: [4]u8) bool { return std.mem.eql(u8, &a, &b);}
pub fn encode( image: qoi.Image, destination: *std.Io.Writer,) !void { const header = qoi.Header{ .magic = qoi.MAGIC_QOI, .width = image.width, .height = image.height, .channels = image.channels, .color_space = 0, };
try destination.writeAll(&header.encode());
var running = [_][4]u8{.{ 0, 0, 0, 0 }} ** 64;
var prev_px: [4]u8 = .{ 0, 0, 0, 255 }; var repeat_count: u8 = 0; for (image.pixels) |cur_px| { const idx = qoi.index_hash(cur_px[0], cur_px[1], cur_px[2], cur_px[3]);
const da: u8 = cur_px[3] - prev_px[3];
const dr = @as(i16, cur_px[0]) - @as(i16, prev_px[0]); const dg = @as(i16, cur_px[1]) - @as(i16, prev_px[1]); const db = @as(i16, cur_px[2]) - @as(i16, prev_px[2]);
const dr_dg: i16 = dr - dg; const db_dg: i16 = db - dg;
if (pixelEql(cur_px, prev_px)) { repeat_count += 1; } else { if (repeat_count > 0) { // QOI_OP_RUN path, bias is 1 const run_chunk: u8 = @intFromEnum(qoi.qoiTag.QOI_OP_RUN) | (repeat_count - 1);
try destination.writeByte(run_chunk); repeat_count = 0; } if (pixelEql(running[idx], cur_px)) { // QOI_OP_INDEX path const chunk = @intFromEnum(qoi.qoiTag.QOI_OP_INDEX) | @as(u8, @intCast(idx));
try destination.writeByte(chunk); } else if (dr >= -2 and dr <= 1 and dg >= -2 and dg <= 1 and db >= -2 and db <= 1 and da == 0) { // QOI_OP_DIFF path, bias is 2 const bdr: u8 = @intCast(dr + 2); const bdg: u8 = @intCast(dg + 2); const bdb: u8 = @intCast(db + 2); const chunk = @intFromEnum(qoi.qoiTag.QOI_OP_DIFF) | (bdr << 4 | bdg << 2 | bdb);
try destination.writeByte(chunk); } else if (dg >= -32 and dg <= 31 and dr_dg >= -8 and dr_dg <= 7 and db_dg >= -8 and db_dg <= 7 and da == 0) { // QOI_OP_LUMA path, bias is 32 for dg, and 8 for dr_dg/db_dg
const bdg: u8 = @intCast(dg + 32); const bdr_dg: u8 = @intCast(dr_dg + 8); const bdb_dg: u8 = @intCast(db_dg + 8);
// first byte holds the tag and dg const chunk1 = @intFromEnum(qoi.qoiTag.QOI_OP_LUMA) | bdg; // Second byte holds dr_dg and bdb_dg const chunk2 = bdr_dg << 4 | bdb_dg;
try destination.writeAll(&[_]u8{ chunk1, chunk2 }); // try data.appendSlice(gpa, &[_]u8{ chunk1, chunk2 }); } else { if (da == 0) { // QOI_OP_RGB path const tag_chunk = @intFromEnum(qoi.qoiTag.QOI_OP_RGB); // std.debug.print("QOI_OP_RGB: dr={}, dg={}, db={}\n", .{ dr, dg, db }); try destination.writeAll(&[_]u8{ tag_chunk, cur_px[0], cur_px[1], cur_px[2] }); } else { // QOI_OP_RGBA path const tag_chunk = @intFromEnum(qoi.qoiTag.QOI_OP_RGBA); try destination.writeAll(&[_]u8{ tag_chunk, cur_px[0], cur_px[1], cur_px[2], cur_px[3] }); } } }
running[idx] = cur_px; prev_px = cur_px; }
if (repeat_count > 0) { // Flush the remaining run, if any // QOI_OP_RUN path, bias is 1 const run_chunk: u8 = @intFromEnum(qoi.qoiTag.QOI_OP_RUN) | (repeat_count - 1); try destination.writeByte(run_chunk); }
try destination.writeAll(&qoi.END_MARKER);}
pub fn encodeAlloc(allocator: std.mem.Allocator, image: qoi.Image) ![]u8 { var aw: std.Io.Writer.Allocating = .init(allocator);
try encode(image, &aw.writer); return aw.toOwnedSlice();}
test "test image" { const allocator = std.testing.allocator; const testImage: qoi.Image = .{ .width = 3, .height = 3, .channels = .rgba, .pixels = &[_][4]u8{ .{ 255, 0, 0, 255 }, .{ 0, 255, 0, 255 }, .{ 0, 0, 255, 255 }, .{ 255, 255, 0, 255 }, .{ 255, 0, 255, 255 }, .{ 0, 255, 255, 255 }, .{ 128, 128, 128, 255 }, .{ 64, 64, 64, 255 }, .{ 192, 192, 192, 255 }, }, };
const result = try encodeAlloc(allocator, testImage); defer allocator.free(result); try std.testing.expectEqual(qoi.MAGIC_QOI[0], result[0]);}