Compare commits

..

No commits in common. "92f8430454b66ac7bdded235440aa8a9e805d243" and "fa522b007d057481393f900629b3d3ca2c7e1eca" have entirely different histories.

7 changed files with 9 additions and 6498 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
.cache .cache
compile_commands.json compile_commands.json
tiny tiny
*.pam

File diff suppressed because it is too large Load Diff

View File

@ -1,70 +0,0 @@
#include "img.h"
#include "mem_arena.h"
#include "pam.h"
#include "utils.h"
#include <stdbool.h>
#include <stddef.h>
#define SAMPLES_PER_PIXEL 4
bool init_image(Arena *arena, Image *img) {
if (!arena || !img || img->width == 0 || img->height == 0) {
return false;
}
u64 size = img->width * img->height * SAMPLES_PER_PIXEL;
img->buf = wapp_mem_arena_alloc(arena, size);
return img->buf != NULL;
}
void set_pixel(Image *img, u64 x, u64 y, Colour colour) {
u64 idx = (y * img->width + x) * SAMPLES_PER_PIXEL;
for (u64 i = 0; i < SAMPLES_PER_PIXEL; ++i) {
img->buf[idx + i] = *(((u8 *)&colour) + i);
}
}
void clear_image(Image *img, Colour colour) {
for (u64 y = 0; y < img->height; ++y) {
for (u64 x = 0; x < img->width; ++x) {
set_pixel(img, x, y, colour);
}
}
}
void draw_line(Image *img, u64 x0, u64 y0, u64 x1, u64 y1, Colour colour) {
u64 dx = absolute((i64)x1 - (i64)x0);
u64 dy = absolute((i64)y1 - (i64)y0);
f32 t = 0.0f;
i64 x = 0;
i64 y = 0;
if (dx > dy) {
if (x0 > x1) {
swap(u64, x0, x1);
swap(u64, y0, y1);
}
for (u64 x = x0; x < x1; ++x) {
t = (f32)(x - x0) / (f32)(x1 - x0);
y = (i64)y0 + ((i64)y1 - (i64)y0) * t;
set_pixel(img, x, y, colour);
}
} else {
if (y0 > y1) {
swap(u64, x0, x1);
swap(u64, y0, y1);
}
for (u64 y = y0; y < y1; ++y) {
t = (f32)(y - y0) / (f32)(y1 - y0);
x = (i64)x0 + ((i64)x1 - (i64)x0) * t;
set_pixel(img, x, y, colour);
}
}
}
void save_image(const Image *img, const char *filename) {
write_p7_image(img->width, img->height, img->buf, filename);
}

View File

@ -1,29 +0,0 @@
#ifndef IMG_H
#define IMG_H
#include "aliases.h"
#include "mem_arena.h"
#include <stdbool.h>
typedef struct img Image;
struct img {
u64 width;
u64 height;
u8 *buf;
};
typedef struct colour Colour;
struct colour {
u8 r;
u8 g;
u8 b;
u8 a;
};
bool init_image(Arena *arena, Image *img);
void set_pixel(Image *img, u64 x, u64 y, Colour colour);
void clear_image(Image *img, Colour colour);
void draw_line(Image *img, u64 x0, u64 y0, u64 x1, u64 y1, Colour colour);
void save_image(const Image *img, const char *filename);
#endif // IMG_H

View File

@ -1,34 +1,26 @@
#include "img.h" #include "aliases.h"
#include "mem_arena.h" #include "mem_arena.h"
#include "mem_utils.h" #include "mem_utils.h"
#include "pam.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
enum { typedef struct img Image;
TINY_EXIT_SUCCESS, struct img {
TINY_EXIT_ARENA_INIT_FAILED, u64 width;
TINY_EXIT_IMAGE_INIT_FAILED, u64 height;
u8 *buf;
}; };
int main(void) { int main(void) {
Arena *arena = NULL; Arena *arena = NULL;
if (!wapp_mem_arena_init(&arena, 10ul * 1024ul * 1024ul * 1024ul, if (!wapp_mem_arena_init(&arena, 10ul * 1024ul * 1024ul * 1024ul,
WAPP_MEM_ALLOC_RESERVE, false)) { WAPP_MEM_ALLOC_RESERVE, false)) {
return TINY_EXIT_ARENA_INIT_FAILED; return -1;
} }
Colour cyan = {.r = 0, .g = 255, .b = 255, .a = 255};
Colour red = {.r = 255, .g = 0, .b = 0, .a = 255};
Image img = {.width = 200, .height = 200};
if (!init_image(arena, &img)) {
return TINY_EXIT_IMAGE_INIT_FAILED;
}
clear_image(&img, cyan);
save_image(&img, "result.pam");
wapp_mem_arena_destroy(&arena); wapp_mem_arena_destroy(&arena);
return TINY_EXIT_SUCCESS; return 0;
} }

View File

@ -1,10 +0,0 @@
#include "utils.h"
#include "aliases.h"
i64 absolute(i64 value) {
if (value >= 0) {
return value;
}
return value * -1;
}

View File

@ -1,14 +0,0 @@
#ifndef UTILS_H
#define UTILS_H
#include "aliases.h"
#define swap(T, v0, v1) \
do { \
T tmp = v0; \
v0 = v1; \
v1 = tmp; \
} while (0)
i64 absolute(i64 value);
#endif // UTILS_H