Skip to content

Commit

Permalink
✨ Add terminal support
Browse files Browse the repository at this point in the history
  • Loading branch information
mekb-turtle committed Dec 7, 2024
1 parent acfb3de commit 61c5747
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 89 deletions.
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project('Foto', 'c',
default_options: ['warning_level=3'])

# define source files
src = files('src/main.c', 'src/arg.c', 'src/arg.h', 'src/image.c', 'src/image.h', 'src/util.c', 'src/util.h')
src = files('src/main.c', 'src/arg.c', 'src/arg.h', 'src/image.c', 'src/image.h', 'src/util.c', 'src/util.h', 'src/term.c', 'src/term.h', 'src/color.c', 'src/color.h')

# define project metadata
url = 'https://github.com/mekb-turtle/Foto'
Expand All @@ -20,4 +20,5 @@ add_project_arguments(
exe = executable('foto', sources: src, install: true, dependencies: [
dependency('SDL2'),
dependency('SDL2_image'),
dependency('ncurses')
])
4 changes: 2 additions & 2 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <SDL2/SDL.h>

#include "./arg.h"
#include "./util.h"
#include "arg.h"
#include "util.h"

bool is_num(char *str) {
// locale-aware
Expand Down
69 changes: 69 additions & 0 deletions src/color.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "color.h"

#define SQ(x) ((x) * (x))

size_t closest_color(struct color color, struct color *color_table, size_t color_len) {
size_t closest;
uint32_t closest_dist = -1; // max value
for (size_t i = 0; i < color_len; ++i) {
uint32_t dist = SQ(color.r - color_table[i].r) + SQ(color.g - color_table[i].g) + SQ(color.b - color_table[i].b);
if (dist < closest_dist) {
closest = i;
closest_dist = dist;
}
}
return closest;
}

#define VAL(n) ((n) == 0 ? 0 : 40 * (n) + 55) // 0, 95, 135, 175, 215, 255

uint8_t rgb_to_8bit(struct color color) {
static struct color color_table[240];
static bool color_table_init = false;
if (!color_table_init) {
color_table_init = true;
uint8_t i = 0;
struct color color;
color.a = 0xff;

// fill colors (6^3)
for (color.r = 0; color.r < 6; ++color.r)
for (color.g = 0; color.g < 6; ++color.g)
for (color.b = 0; color.b < 6; ++color.b, ++i) {
color_table[i] = (struct color){
.r = VAL(color.r),
.g = VAL(color.g),
.b = VAL(color.b),
};
}
// fill grayscale (24)
for (color.r = 8; color.r < 238; color.r += 10, ++i) {
color.b = color.g = color.r;
color_table[i] = color;
}
}

// find closest color (16-255)
return closest_color(color, color_table, 240) + 16;
}

uint8_t rgb_to_4bit(struct color color) {
static struct color color_table[16];
static bool color_table_init = false;
if (!color_table_init) {
color_table_init = true;
for (uint8_t i = 0; i < 16; ++i) {
color_table[i] = (struct color){.a = 0xff};
uint8_t n = (i & 0x8) ? 0xff : 0x80;
// dynamically generate color table
color_table[i].r = (i & 0x1) ? n : 0x00;
color_table[i].g = (i & 0x2) ? n : 0x00;
color_table[i].b = (i & 0x4) ? n : 0x00;
};
color_table[8] = color_table[7];
color_table[7] = (struct color){{{0xc0, 0xc0, 0xc0, 0xff}}};
}

// find closest color (0-15)
return closest_color(color, color_table, 16);
}
21 changes: 21 additions & 0 deletions src/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef COLOR_H
#define COLOR_H
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <SDL2/SDL.h>

struct color {
union {
struct {
uint8_t r, g, b, a;
};
uint32_t u32;
};
};

size_t closest_color(struct color color, struct color *color_table, size_t color_len);
uint8_t rgb_to_8bit(struct color color);
uint8_t rgb_to_4bit(struct color color);

#endif
4 changes: 2 additions & 2 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "./util.h"
#include "./image.h"
#include "util.h"
#include "image.h"

// get file pointer from filename
FILE *open_file(char *filename) {
Expand Down
Loading

0 comments on commit 61c5747

Please sign in to comment.