diff options
| -rw-r--r-- | .clangd | 5 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | examples/circle.sql | 25 | ||||
| -rw-r--r-- | flake.nix | 5 | ||||
| -rwxr-xr-x | image.sh | 13 | ||||
| -rw-r--r-- | justfile | 27 | ||||
| -rw-r--r-- | setup.sql | 98 | ||||
| -rw-r--r-- | src/main.c | 70 |
8 files changed, 102 insertions, 143 deletions
diff --git a/.clangd b/.clangd deleted file mode 100644 index e6c43d0..0000000 --- a/.clangd +++ /dev/null @@ -1,5 +0,0 @@ -CompileFlags: - Compiler: gcc -Diagnostics: - Suppress: unknown-attributes - Add: bugprone-*, readability-* @@ -1,4 +1,2 @@ -build/ -compile_flags.txt *.db *.ignore.* diff --git a/examples/circle.sql b/examples/circle.sql new file mode 100644 index 0000000..73e7118 --- /dev/null +++ b/examples/circle.sql @@ -0,0 +1,25 @@ +CREATE TEMP TABLE variables(name TEXT PRIMARY KEY, value Text); + +INSERT INTO images (width, height) VALUES (6, 6) RETURNING id; +INSERT INTO variables VALUES ('image_id', last_insert_rowid()); + +WITH RECURSIVE + image_id AS (SELECT value FROM variables WHERE name = 'image_id'), + horizontal(x) AS ( + SELECT width FROM images WHERE id = (SELECT * FROM image_id) + UNION ALL + SELECT x - 1 FROM horizontal + WHERE x > 1 + ), + vertical(y) AS ( + SELECT height FROM images WHERE id = (SELECT * FROM image_id) + UNION ALL + SELECT y - 1 FROM vertical + WHERE y > 1 + ), + pixels_temp(x, y, r, g, b) AS ( + SELECT x, y, 255, 0, 0 FROM vertical, horizontal + ) +INSERT INTO pixels (image_id, x, y, r, g, b) SELECT (SELECT * FROM image_id), x, y, r, g, b FROM pixels_temp; + +DROP TABLE variables; @@ -17,11 +17,8 @@ { default = pkgs.mkShell { buildInputs = with pkgs; [ - gcc + imagemagick sqlite - clang-tools - unixtools.xxd - pkg-config ]; }; } diff --git a/image.sh b/image.sh new file mode 100755 index 0000000..b697688 --- /dev/null +++ b/image.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +DB=fun.db + +[ $# -lt 1 ] && echo "Fuck" && exit 1; + +db() { sqlite3 -list "$DB" "$@"; } + +image_id="$1" +echo "P3" +db "SELECT width, height FROM images WHERE id=$image_id" | awk -F'|' '{ print $1 " " $2 }' +echo "255" +db "SELECT r,g,b FROM pixels WHERE image_id=$image_id ORDER BY y ASC, x ASC" | tr '|' ' ' @@ -1,27 +1,14 @@ -set export +setup: setup-db -CC := "cc" -CFLAGS := f"-std=c11 -O2 \ --Wall -Wextra -Wshadow -Wformat=2 -fsanitize=address,undefined \ -{{shell('pkg-config --cflags --libs sqlite3')}}" -OUTDIR := "build" +run file *args: setup-db + sqlite3 fun.db {{args}} < {{file}} -build: compile-flags - mkdir -p "{{OUTDIR}}" - {{CC}} {{CFLAGS}} src/*.c -o "{{OUTDIR}}/sqlheavy" +image image_id: + ./image.sh {{image_id}} | magick display -resize 500% - -run: build - "./{{OUTDIR}}/sqlheavy" - -@compile-flags: - echo '{{CFLAGS}}' | tr ' ' '\n' > ./compile_flags.txt - -format: - find src/ -iname '*.h' -o -iname '*.c' | xargs clang-format -i +repl *args: + rlwrap sqlite3 fun.db {{args}} setup-db: rm fun.db sqlite3 fun.db < setup.sql - -image: - just run | magick display -resize 500% - @@ -1,45 +1,59 @@ +PRAGMA foreign_keys = ON; +PRAGMA temp_store = 2; + +CREATE TABLE images ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + width INTEGER NOT NULL, + height INTEGER NOT NULL +); + CREATE TABLE pixels ( - x INTEGER, - y INTEGER, - r INTEGER, - g INTEGER, - b INTEGER, - UNIQUE(x, y) + id INTEGER PRIMARY KEY AUTOINCREMENT, + image_id INTEGER NOT NULL, + x INTEGER NOT NULL, + y INTEGER NOT NULL, + r INTEGER NOT NULL CHECK(r >= 0 AND r <= 255), + g INTEGER NOT NULL CHECK(g >= 0 AND g <= 255), + b INTEGER NOT NULL CHECK(b >= 0 AND b <= 255), + FOREIGN KEY(image_id) REFERENCES images(id), + UNIQUE(image_id, x, y) ); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 1, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 2, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 2, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 2, 0, 255, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 2, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 2, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 2, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 3, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 4, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 5, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 5, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 5, 255, 0, 0); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 5, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 5, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 5, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (1, 6, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (2, 6, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (3, 6, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (4, 6, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (5, 6, 0, 0, 255); -INSERT INTO pixels (x, y, r, g, b) VALUES (6, 6, 0, 0, 255); +INSERT INTO images (id, width, height) VALUES (1, 6, 6); + +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 1, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 2, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 2, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 2, 0, 255, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 2, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 2, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 2, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 3, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 4, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 5, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 5, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 5, 255, 0, 0); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 5, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 5, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 5, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 1, 6, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 2, 6, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 3, 6, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 4, 6, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 5, 6, 0, 0, 255); +INSERT INTO pixels (image_id, x, y, r, g, b) VALUES (1, 6, 6, 0, 0, 255); diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 2da78b0..0000000 --- a/src/main.c +++ /dev/null @@ -1,70 +0,0 @@ -#include <sqlite3.h> -#include <stdio.h> -#include <string.h> - -typedef struct { - int x; - int y; - int r; - int g; - int b; -} Pixel; - -void show_pixels(Pixel *pixels, int len) { - for (int i = 0; i < len; i++) { - Pixel p = pixels[i]; - printf("%d %d %d\n", p.r, p.g, p.b); - } -} - -void show_image(Pixel *pixels, int len) { - printf("P3\n"); - printf("%d %d\n", 6, 6); - printf("%d\n", 255); - show_pixels(pixels, len); -} - -int main(void) { - sqlite3 *db; - - sqlite3_open("fun.db", &db); - if (db == NULL) { - printf("Unable to open db\n"); - return 1; - } - - sqlite3_stmt *stmt; - sqlite3_prepare_v2(db, - "select x, y, r, g, b from pixels ORDER BY y ASC, x ASC", - -1, &stmt, NULL); - - Pixel pixels[200 * 200]; - int index = 0; - - while (sqlite3_step(stmt) != SQLITE_DONE) { - int num_cols = sqlite3_column_count(stmt); - Pixel pixel; - for (int col = 0; col < num_cols; col++) { - const char *name = sqlite3_column_name(stmt, col); - if (strcmp(name, "x") == 0) - pixel.x = sqlite3_column_int(stmt, col); - else if (strcmp(name, "y") == 0) - pixel.y = sqlite3_column_int(stmt, col); - else if (strcmp(name, "r") == 0) - pixel.r = sqlite3_column_int(stmt, col); - else if (strcmp(name, "g") == 0) - pixel.g = sqlite3_column_int(stmt, col); - else if (strcmp(name, "b") == 0) - pixel.b = sqlite3_column_int(stmt, col); - } - pixels[index++] = pixel; - } - int frames = 1; - for (int i = 0; i < frames; i++) - show_image(pixels, index); - - sqlite3_finalize(stmt); - sqlite3_close(db); - - return 0; -} |
