diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-03-18 22:23:53 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-03-18 22:24:10 +0100 |
| commit | 1e7b589e77c7935cbaa8381f50cdcb86c85fa532 (patch) | |
| tree | 6a087761a4b09cd01caff8ae51fb1971d9b0ba8a | |
Implement first structure of jpg and farbfeld
| -rw-r--r-- | .envrc | 1 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | build.zig | 67 | ||||
| -rw-r--r-- | build.zig.zon | 81 | ||||
| -rw-r--r-- | default.nix | 4 | ||||
| -rw-r--r-- | src/bitmap.zig | 42 | ||||
| -rw-r--r-- | src/color.zig | 22 | ||||
| -rw-r--r-- | src/farbfeld.zig | 53 | ||||
| -rw-r--r-- | src/jpg/marker.zig | 122 | ||||
| -rw-r--r-- | src/jpg/root.zig | 8 | ||||
| -rw-r--r-- | src/jpg/segment.zig | 69 | ||||
| -rw-r--r-- | src/main.zig | 5 | ||||
| -rw-r--r-- | src/root.zig | 10 | ||||
| -rw-r--r-- | src/testing/rgb.ff | bin | 0 -> 2160016 bytes | |||
| -rw-r--r-- | src/testing/rgb.jpg | bin | 0 -> 15646 bytes |
15 files changed, 486 insertions, 0 deletions
@@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..880cd5d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-out +.zig-cache diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..6eab5f5 --- /dev/null +++ b/build.zig @@ -0,0 +1,67 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const mod = b.addModule("picasso", .{ + .root_source_file = b.path("src/root.zig"), + .target = target, + }); + + const exe = b.addExecutable(.{ + .name = "picasso", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "picasso", .module = mod }, + }, + }), + }); + + b.installArtifact(exe); + + const run_step = b.step("run", "Run the app"); + + const run_cmd = b.addRunArtifact(exe); + run_step.dependOn(&run_cmd.step); + + run_cmd.step.dependOn(b.getInstallStep()); + + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const mod_tests = b.addTest(.{ + .root_module = mod, + }); + + const run_mod_tests = b.addRunArtifact(mod_tests); + + const exe_tests = b.addTest(.{ + .root_module = exe.root_module, + }); + + const run_exe_tests = b.addRunArtifact(exe_tests); + + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_mod_tests.step); + test_step.dependOn(&run_exe_tests.step); + + const docs_step = b.step("docs", "Emit documentation"); + + const autodoc_test = b.addObject(.{ + .name = "picasso", + .root_module = mod, + }); + + const docs_install = b.addInstallDirectory(.{ + .install_dir = .prefix, + .install_subdir = "docs", + .source_dir = autodoc_test.getEmittedDocs(), + }); + + docs_step.dependOn(&docs_install.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..36a7b64 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,81 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save <url>`, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = .picasso, + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0x8c5f4cc92f9ef641, // Changing this has security and trust implications. + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.15.2", + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save <url>` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. If the contents of a URL change this will result in a hash mismatch + // // which will prevent zig from using it. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + // + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + // For example... + //"LICENSE", + //"README.md", + }, +} diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..ec45582 --- /dev/null +++ b/default.nix @@ -0,0 +1,4 @@ +{ pkgs ? import <nixpkgs> { }, ... }: +pkgs.mkShell { + packages = [ pkgs.zig ]; +} diff --git a/src/bitmap.zig b/src/bitmap.zig new file mode 100644 index 0000000..9a9f7ac --- /dev/null +++ b/src/bitmap.zig @@ -0,0 +1,42 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +pub fn Bitmap(PixelType: type) type { + return struct { + const Self = @This(); + + pixels: []PixelType, + width: usize, + height: usize, + + /// Creates a new Bitmap instance and allocates the `pixels` buffer. + pub fn init_undefined(gpa: Allocator, width: usize, height: usize) !Self { + const self: Self = .{ + .width = width, + .height = height, + .pixels = try gpa.alloc(PixelType, width * height), + }; + + return self; + } + + pub fn from_buffer(buffer: []PixelType, width: usize, height: usize) Self { + return .{ + .width = width, + .height = height, + .pixels = buffer, + }; + } + + pub inline fn pixel(self: *Self, x: usize, y: usize) ?*PixelType { + return if (x < self.width and y < self.height) + self.pixels[x + self.width * y] + else + null; + } + + pub fn deinit(self: Self, gpa: Allocator) void { + gpa.free(self.pixels); + } + }; +} diff --git a/src/color.zig b/src/color.zig new file mode 100644 index 0000000..e27ccb2 --- /dev/null +++ b/src/color.zig @@ -0,0 +1,22 @@ +const std = @import("std"); + +pub fn Rgba(comptime bits: u16) type { + const Uint = std.meta.Int(.unsigned, bits); + return packed struct { + const Self = @This(); + + red: Uint, + green: Uint, + blue: Uint, + alpha: Uint, + + pub fn init(r: Uint, g: Uint, b: Uint, a: Uint) Self { + return .{ + .red = r, + .green = g, + .blue = b, + .alpha = a, + }; + } + }; +} diff --git a/src/farbfeld.zig b/src/farbfeld.zig new file mode 100644 index 0000000..dbdf338 --- /dev/null +++ b/src/farbfeld.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const picasso = @import("root.zig"); +const Rgba = picasso.color.Rgba; +const Bitmap = picasso.Bitmap; + +pub const Image = struct { + const Self = @This(); + + pub const ReadError = error { + InvalidMagicValue, + InvalidSize, + } || std.Io.Reader.Error || Allocator.Error; + + bitmap: Bitmap(Rgba(16)), + + pub fn read(gpa: Allocator, reader: *std.Io.Reader) ReadError!Self { + if (!std.mem.eql(u8, "farbfeld", try reader.takeArray(8))) { + return ReadError.InvalidMagicValue; + } + + var self: Self = undefined; + + const width = try reader.takeInt(u32, .big); + const height = try reader.takeInt(u32, .big); + + self.bitmap = try .init_undefined(gpa, width, height); + errdefer self.bitmap.deinit(gpa); + + for (self.bitmap.pixels) |*pixel| { + pixel.* = try reader.takeStruct(Rgba(16), .big); + } + + return self; + } + + pub fn deinit(self: Self, gpa: Allocator) void { + self.bitmap.deinit(gpa); + } +}; + +test "rgb" { + const buffer = @embedFile("testing/rgb.ff"); + var reader: std.Io.Reader = .fixed(buffer); + const gpa = std.testing.allocator; + + const image: Image = try .read(gpa, &reader); + defer image.deinit(gpa); + + try std.testing.expectEqual(600, image.bitmap.width); + try std.testing.expectEqual(450, image.bitmap.height); +} diff --git a/src/jpg/marker.zig b/src/jpg/marker.zig new file mode 100644 index 0000000..63eefe5 --- /dev/null +++ b/src/jpg/marker.zig @@ -0,0 +1,122 @@ +const std = @import("std"); + +pub const Marker = union(enum) { + pub const Kind = std.meta.Tag(@This()); + + const Tag = struct { + const temporary = 0x1; + const reset = .{ .start = 0xd0, .end = 0xd7 }; + const start_of_frame = .{ + .{ .start = 0xc0, .end = 0xc3 }, + .{ .start = 0xc5, .end = 0xc7 }, + .{ .start = 0xc9, .end = 0xcb }, + }; + const start_of_image = 0xd8; + const end_of_image = 0xd9; + const define_quantization_table = 0xdb; + const define_huffman_table = 0xc4; + const define_restart_interval = 0xdd; + const define_number_of_lines = 0xdc; + const define_hierarchy_progression = 0xde; + const extend_huffman_table = 0xdf; + const application_segment = .{ .start = 0xe0, .end = 0xef }; + const comment = 0xfe; + const start_of_scan = 0xda; + }; + + temporary: void, + reset: u3, + start_of_image: void, + end_of_image: void, + start_of_frame: u4, + define_quantization_table: void, + define_huffman_table: void, + define_restart_interval: void, + define_number_of_lines: void, + define_hierarchy_progression: void, + extend_huffman_table: void, + application_segment: u4, + comment: void, + start_of_scan: void, + + + pub fn from_u8(value: u8) !@This() { + return switch (value) { + Tag.reset.start...Tag.reset.end => .{ + .reset = @intCast(value - Tag.reset.start), + }, + Tag.start_of_frame[0].start...Tag.start_of_frame[0].end => .{ + .start_of_frame = @intCast(value - Tag.start_of_frame[0].start), + }, + Tag.start_of_frame[1].start...Tag.start_of_frame[1].end => .{ + .start_of_frame = @intCast(value - Tag.start_of_frame[1].start + 4), + }, + Tag.start_of_frame[2].start...Tag.start_of_frame[2].end => .{ + .start_of_frame = @intCast(value - Tag.start_of_frame[2].start + 7), + }, + Tag.application_segment.start...Tag.application_segment.end => .{ + .application_segment = @intCast(value - Tag.application_segment.start), + }, + Tag.start_of_image => .{ .start_of_image = void{} }, + Tag.end_of_image => .{ .end_of_image = void{} }, + Tag.define_quantization_table => .{ .define_quantization_table = void{} }, + Tag.define_huffman_table => .{ .define_huffman_table = void{} }, + Tag.define_restart_interval => .{ .define_restart_interval = void{} }, + Tag.define_number_of_lines => .{ .define_number_of_lines = void{} }, + Tag.define_hierarchy_progression => .{ .define_hierarchy_progression = void{} }, + Tag.extend_huffman_table => .{ .extend_huffman_table = void{} }, + Tag.comment => .{ .comment = void{} }, + Tag.start_of_scan => .{ .start_of_scan = void{} }, + Tag.temporary => .{ .temporary = void{} }, + else => error.InvalidByte, + }; + } +}; + +test "marker" { + const Testing = struct { + pub fn empty_tag(value: comptime_int, tag: std.meta.Tag(Marker)) !void { + const marker: Marker = try .from_u8(value); + try std.testing.expectEqual( + tag, + std.meta.activeTag(marker), + ); + } + }; + + for (0..8) |offset| { + const marker: Marker = try .from_u8(0xd0 + @as(u8, @intCast(offset))); + try std.testing.expectEqual(offset, marker.reset); + } + + { + for (0..4) |offset| { + const marker: Marker = try .from_u8(0xc0 + @as(u8, @intCast(offset))); + try std.testing.expectEqual(offset, marker.start_of_frame); + } + + for (0..3) |offset| { + const marker: Marker = try .from_u8(0xc5 + @as(u8, @intCast(offset))); + try std.testing.expectEqual(offset + 4, marker.start_of_frame); + } + + for (0..3) |offset| { + const marker: Marker = try .from_u8(0xc9 + @as(u8, @intCast(offset))); + try std.testing.expectEqual(offset + 7, marker.start_of_frame); + } + } + + for (0..16) |offset| { + const marker: Marker = try .from_u8(0xe0 + @as(u8, @intCast(offset))); + try std.testing.expectEqual(offset, marker.application_segment); + } + + try Testing.empty_tag(Marker.Tag.temporary, .temporary); + try Testing.empty_tag(Marker.Tag.start_of_image, .start_of_image); + try Testing.empty_tag(Marker.Tag.end_of_image, .end_of_image); + try Testing.empty_tag(Marker.Tag.define_quantization_table, .define_quantization_table); + try Testing.empty_tag(Marker.Tag.define_huffman_table, .define_huffman_table); + try Testing.empty_tag(Marker.Tag.define_restart_interval, .define_restart_interval); + try Testing.empty_tag(Marker.Tag.define_number_of_lines, .define_number_of_lines); + try Testing.empty_tag(Marker.Tag.define_hierarchy_progression, .define_hierarchy_progression); +} diff --git a/src/jpg/root.zig b/src/jpg/root.zig new file mode 100644 index 0000000..55b37e8 --- /dev/null +++ b/src/jpg/root.zig @@ -0,0 +1,8 @@ +const std = @import("std"); + +pub const Marker = @import("marker.zig").Marker; +pub const Segment = @import("segment.zig").Segment; + +test { + _ = std.testing.refAllDecls(@This()); +} diff --git a/src/jpg/segment.zig b/src/jpg/segment.zig new file mode 100644 index 0000000..ec1346a --- /dev/null +++ b/src/jpg/segment.zig @@ -0,0 +1,69 @@ +const std = @import("std"); +const jpg = @import("root.zig"); + +pub const Segment = union(jpg.Marker.Kind) { + temporary: void, + reset: void, + start_of_image: void, + end_of_image: void, + start_of_frame: void, + define_quantization_table: void, + define_huffman_table: void, + define_restart_interval: void, + define_number_of_lines: void, + define_hierarchy_progression: void, + extend_huffman_table: void, + application_segment: void, + comment: void, + start_of_scan: void, + + const Self = @This(); + + pub fn read(gpa: std.mem.Allocator, reader: *std.Io.Reader) !Self { + if (try reader.peekByte() != 0xff) { + return error.InvalidMarkerHeader; + } + reader.toss(1); + + const marker = jpg.Marker.from_u8(try reader.peekByte()) + catch return error.InvalidMarkerByte; + reader.toss(1); + + return switch (marker) { + .temporary => .{ .temporary = void{} }, + .reset => .{ .reset = void{} }, + .start_of_image => .{ .start_of_image = void{} }, + .end_of_image => .{ .end_of_image = void{} }, + .start_of_frame => data: { + const buffer = try read_data_with_length(gpa, reader); + defer gpa.free(buffer); + break :data .{ .start_of_frame = void{} }; + }, + .application_segment => data: { + const buffer = try read_data_with_length(gpa, reader); + defer gpa.free(buffer); + break :data .{ .application_segment = void{} }; + }, + else => { + std.debug.panic("unimplemented tag '{s}'\n", .{@tagName(marker)}); + }, + }; + } + + fn read_data_with_length(gpa: std.mem.Allocator, reader: *std.Io.Reader) ![]u8 { + const length = try reader.takeInt(u16, .big); + return try reader.readAlloc(gpa, @intCast(length)); + } +}; + + +test "file" { + const buffer = @embedFile("../testing/rgb.jpg"); + var reader: std.Io.Reader = .fixed(buffer); + + var segment: Segment = try .read(std.testing.allocator, &reader); + try std.testing.expectEqual(.start_of_image, std.meta.activeTag(segment)); + + segment = try .read(std.testing.allocator, &reader); + try std.testing.expectEqual(.application_segment, std.meta.activeTag(segment)); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..a82714e --- /dev/null +++ b/src/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); +const picasso = @import("picasso"); + +pub fn main() !void { +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..66d909c --- /dev/null +++ b/src/root.zig @@ -0,0 +1,10 @@ +const std = @import("std"); + +pub const color = @import("color.zig"); +pub const farbfeld = @import("farbfeld.zig"); +pub const jpg = @import("jpg/root.zig"); +pub const Bitmap = @import("bitmap.zig").Bitmap; + +test { + _ = std.testing.refAllDecls(@This()); +} diff --git a/src/testing/rgb.ff b/src/testing/rgb.ff Binary files differnew file mode 100644 index 0000000..fe0f62f --- /dev/null +++ b/src/testing/rgb.ff diff --git a/src/testing/rgb.jpg b/src/testing/rgb.jpg Binary files differnew file mode 100644 index 0000000..7fbb361 --- /dev/null +++ b/src/testing/rgb.jpg |