feat: generate image
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.zig-cache
|
||||
zig-out
|
||||
*.ppm
|
||||
|
||||
28
src/main.zig
28
src/main.zig
@@ -1,13 +1,29 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() !void {
|
||||
var allocator = std.heap.ArenaAllocator{};
|
||||
defer allocator.deinit();
|
||||
const img_width = 512;
|
||||
const img_height = 512;
|
||||
|
||||
const alloc = allocator.allocator();
|
||||
var buf: [img_width * img_height * 3]u8 = undefined;
|
||||
|
||||
const hello_world = try std.fmt.allocPrint(alloc, "Hello, World!\n", .{});
|
||||
defer allocator.free(hello_world);
|
||||
const stdout = std.fs.File.stdout();
|
||||
var writer_f: std.fs.File.Writer = stdout.writer(&buf);
|
||||
var writer = &writer_f.interface;
|
||||
|
||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||
try writer.print("P3\n{d} {d}\n255\n", .{ img_width, img_height });
|
||||
try writer.flush();
|
||||
for (0..img_height) |j| {
|
||||
for (0..img_width) |i| {
|
||||
const r = @as(f64, @floatFromInt(j)) / (img_width - 1);
|
||||
const g = 0.0;
|
||||
const b = @as(f64, @floatFromInt(i)) / (img_height - 1);
|
||||
|
||||
const ir = @as(i64, @intFromFloat(r * 255.999));
|
||||
const ig = @as(i64, @intFromFloat(g * 255.999));
|
||||
const ib = @as(i64, @intFromFloat(b * 255.999));
|
||||
|
||||
try writer.print("{d} {d} {d}\n", .{ ir, ig, ib });
|
||||
}
|
||||
}
|
||||
try writer.flush();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user