Compare commits
4 Commits
fa522b007d
...
92f8430454
Author | SHA1 | Date | |
---|---|---|---|
92f8430454 | |||
4baf837256 | |||
52d79817ad | |||
e39ee53b86 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.cache
|
.cache
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
tiny
|
tiny
|
||||||
|
*.pam
|
||||||
|
6357
resources/head.obj
Normal file
6357
resources/head.obj
Normal file
File diff suppressed because it is too large
Load Diff
70
src/img.c
Normal file
70
src/img.c
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#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);
|
||||||
|
}
|
29
src/img.h
Normal file
29
src/img.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#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
|
26
src/main.c
26
src/main.c
@ -1,26 +1,34 @@
|
|||||||
#include "aliases.h"
|
#include "img.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>
|
||||||
|
|
||||||
typedef struct img Image;
|
enum {
|
||||||
struct img {
|
TINY_EXIT_SUCCESS,
|
||||||
u64 width;
|
TINY_EXIT_ARENA_INIT_FAILED,
|
||||||
u64 height;
|
TINY_EXIT_IMAGE_INIT_FAILED,
|
||||||
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 -1;
|
return TINY_EXIT_ARENA_INIT_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 0;
|
return TINY_EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
10
src/utils.c
Normal file
10
src/utils.c
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
|
||||||
|
i64 absolute(i64 value) {
|
||||||
|
if (value >= 0) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value * -1;
|
||||||
|
}
|
14
src/utils.h
Normal file
14
src/utils.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#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
|
Loading…
x
Reference in New Issue
Block a user