basic rectangle drawing functions - ploot - simple plotting tools HTML git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 6e13cd89684d0129aa13995c3f0e2ba4f0a7aae1 DIR parent 1fc2d491d5051732caac4426d73cb22c2d870f6d HTML Author: Josuah Demangeon <mail@josuah.net> Date: Mon, 30 Apr 2018 11:33:21 +0200 basic rectangle drawing functions Diffstat: D deffont.h | 27 --------------------------- M font-14x7.h | 16 +++++++--------- M font.c | 113 +++++++++++++++++++++---------- A font.h | 29 +++++++++++++++++++++++++++++ D txt | 0 5 files changed, 113 insertions(+), 72 deletions(-) --- DIR diff --git a/deffont.h b/deffont.h @@ -1,27 +0,0 @@ -#include <stddef.h> - -#define _ 0 -#define X 1 -#define B(x) char glyph_ ## x[WIDTH * HEIGHT] -#define b(x) glyph_ ## x -#define NOPR NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL -#define NOPRINT NOPR, NOPR, NOPR, NOPR - -#define FONT(x) char *font_ ## x[] = { \ - NULL, NULL, NULL, NULL, \ - NULL, NULL, NULL, NULL, \ - NULL, NULL, NULL, NULL, \ - NULL, NULL, NULL, NULL, \ - NULL, NULL, NULL, NULL, \ - NULL, NULL, NULL, NULL, \ - b(space), b(bang), b(double), b(hash), \ - b(dollar), b(percent), b(ampersand), b(single), \ - b(l_round), b(r_round), b(asterisk), b(plus), \ - b(coma), b(minus), b(dot), b(slash), \ - b(0), b(1), b(2), b(3), \ - b(4), b(5), b(6), b(7), \ - b(8), b(9), b(column), b(semicolumn), \ - b(l_angle), b(equal), b(r_angle), b(question), \ - b(column), b(semicolumn), b(l_angle), b(equal), \ - b(r_angle), b(question) \ -} DIR diff --git a/font-14x7.h b/font-14x7.h @@ -1,7 +1,5 @@ -#include "deffont.h" - -#define WIDTH 14 -#define HEIGHT 7 +#define FONT_WIDTH 14 +#define FONT_HEIGHT 6 B(space) = { _ ,_,_,_,_,_, @@ -73,6 +71,7 @@ _ ,_,_,_,_,_ B(dollar) = { _ ,_,_,_,_,_, +_ ,_,_,_,_,_, _ ,_,_,X,_,_, _ ,_,X,X,X,_, _ ,X,_,X,_,X, @@ -85,21 +84,20 @@ _ ,X,_,X,_,X, _ ,_,X,X,X,_, _ ,_,_,X,_,_, _ ,_,_,_,_,_, -_ ,_,_,_,_,_ }; B(percent) = { _ ,_,_,_,_,_, _ ,_,_,_,_,_, -_ ,_,_,_,_,X, +_ ,_,_,_,_,_, _ ,X,X,_,_,X, _ ,X,X,_,X,_, _ ,_,_,_,X,_, _ ,_,_,X,_,_, +_ ,_,_,X,_,_, _ ,_,X,_,_,_, _ ,_,X,_,X,X, _ ,X,_,_,X,X, -_ ,X,_,_,_,_, _ ,_,_,_,_,_, _ ,_,_,_,_,_, _ ,_,_,_,_,_ @@ -381,7 +379,7 @@ B(6) = { _ ,_,_,_,_,_, _ ,_,_,_,_,_, _ ,_,X,X,X,_, -_ ,X,_,_,_,_, +_ ,X,_,_,_,X, _ ,X,_,_,_,_, _ ,X,_,_,_,_, _ ,X,X,X,X,_, @@ -438,7 +436,7 @@ _ ,X,_,_,_,X, _ ,_,X,X,X,X, _ ,_,_,_,_,X, _ ,_,_,_,_,X, -_ ,_,_,_,_,X, +_ ,X,_,_,_,X, _ ,_,X,X,X,_, _ ,_,_,_,_,_, _ ,_,_,_,_,_, DIR diff --git a/font.c b/font.c @@ -9,63 +9,104 @@ #include <stdlib.h> #include <string.h> -#include "arg.h" +#include "font.h" +#include "font-14x7.h" + +#define WIDTH 100 +#define HEIGHT 100 + +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) typedef uint16_t Color[4]; -enum { R, G, B, A }; +typedef struct { + Color *b; /* buffer */ + int w; /* width */ + int h; /* height */ +} Canvas; -Color *canvas; +Color buffer[WIDTH * HEIGHT]; -char *argv0; -int wflag = 0; -int hflag = 0; +void +ffdraw_pixel(Canvas *can, Color c, + int x, int y) +{ + memcpy(can->b + x + (can->h - 1 - y) * can->w, c, sizeof(*can->b)); +} void -usage() +ffdraw_rectangle(Canvas *can, Color c, + int x1, int y1, + int x2, int y2) { - fputs("ploot -w width -h height <data.csv >image.ff\n", stderr); - exit(1); + int x, y, xmin, ymin, xmax, ymax; + + xmin = MIN(x1, x2); xmax = MAX(x1, x2); + ymin = MIN(y1, y2); ymax = MAX(y1, y2); + + for (x = xmin; x <= xmax; x++) + for (y = ymin; y <= ymax; y++) + ffdraw_pixel(can, c, x, y); +} + +void +ffdraw_line(Canvas *can, Color c, + int x1, int y1, + int x2, int y2) +{ + int x, y; + + (void)c; + (void)can; + + x = x1; + y = y1; + while (x < x2 && y < y2) { + x++; y++; + } } void -ffdraw(Color *canvas, int w, int h) +ffdraw_fill(Canvas *can, Color c) { - Color col = { 0xffff, 0xffff, 0x0000, 0xffff }; - size_t n; + ffdraw_rectangle(can, c, 0, 0, can->w - 1, can->h - 1); +} - for (n = w * h; n > 0; n--) - memcpy(canvas + n - 1, col, sizeof(col)); +void +ffdraw(Canvas *can) +{ + Color c1 = { 0x2222, 0x2222, 0x2222, 0xffff }; + Color c2 = { 0x3333, 0xffff, 0x8888, 0xffff }; + + ffdraw_fill(can, c1); + ffdraw_rectangle(can, c2, + 0, 20, + can->w - 10, 4); +} + +void +usage(void) +{ + fprintf(stderr, "ploot <data.csv >image.ff\n"); + exit(1); } int -main(int argc, char **argv) +main(void) { uint32_t w, h; + Canvas can; - ARGBEGIN { - case 'w': - wflag = atoi(EARGF(usage())); - break; - case 'h': - hflag = atoi(EARGF(usage())); - break; - } ARGEND; - - if (wflag == 0 || hflag == 0) - usage(); - - if ((canvas = calloc(wflag * hflag, sizeof(*canvas))) == NULL) { - perror("allocating memory for the canvas"); - return 1; - } - + can.b = buffer; + can.w = WIDTH; + can.h = HEIGHT; + w = htonl(WIDTH); + h = htonl(HEIGHT); fputs("farbfeld", stdout); - w = htonl(wflag); - h = htonl(hflag); fwrite(&w, sizeof(w), 1, stdout); fwrite(&h, sizeof(h), 1, stdout); - ffdraw(canvas, wflag, hflag); - fwrite(canvas, wflag * hflag, sizeof(*canvas), stdout); + ffdraw(&can); + fwrite(can.b, WIDTH * HEIGHT, sizeof(*can.b), stdout); return 0; } DIR diff --git a/font.h b/font.h @@ -0,0 +1,29 @@ +#include <stddef.h> + +/* + * Macros to make the fonts header file more readable. + */ +#define _ 0 +#define X 1 +#define B(x) char glyph_ ## x[FONT_WIDTH * FONT_HEIGHT] +#define b(x) glyph_ ## x +#define NOPR NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +#define NOPRINT NOPR, NOPR, NOPR, NOPR +#define FONT(x) char *font_ ## x[] = { \ + NULL, NULL, NULL, NULL, \ + NULL, NULL, NULL, NULL, \ + NULL, NULL, NULL, NULL, \ + NULL, NULL, NULL, NULL, \ + NULL, NULL, NULL, NULL, \ + NULL, NULL, NULL, NULL, \ + b(space), b(bang), b(double), b(hash), \ + b(dollar), b(percent), b(ampersand), b(single), \ + b(l_round), b(r_round), b(asterisk), b(plus), \ + b(coma), b(minus), b(dot), b(slash), \ + b(0), b(1), b(2), b(3), \ + b(4), b(5), b(6), b(7), \ + b(8), b(9), b(column), b(semicolumn), \ + b(l_angle), b(equal), b(r_angle), b(question), \ + b(column), b(semicolumn), b(l_angle), b(equal), \ + b(r_angle), b(question) \ +} DIR diff --git a/txt b/txt