From 625aca16e67bec87ba3159422ab3397da83f3146 Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 28 Mar 2021 22:24:18 +0200 Subject: [PATCH] Implement RConsPixel and RBraile APIs ##cons --- libr/cons/Makefile | 2 +- libr/cons/meson.build | 1 + libr/cons/pixel.c | 71 +++++++++++++++++++++++++++++++++++ libr/core/cmd_print.c | 8 ++++ libr/include/r_cons.h | 15 ++++++++ libr/include/r_util/r_print.h | 33 ++++++++++++++++ libr/util/print.c | 44 ++++------------------ 7 files changed, 136 insertions(+), 38 deletions(-) create mode 100644 libr/cons/pixel.c diff --git a/libr/cons/Makefile b/libr/cons/Makefile index 7346b12d91..baf2de2eda 100644 --- a/libr/cons/Makefile +++ b/libr/cons/Makefile @@ -3,7 +3,7 @@ include ../config.mk NAME=r_cons OBJS=cons.o cpipe.o output.o grep.o less.o more.o pager.o cutf8.o OBJS+=line.o hud.o rgb.o input.o pal.o editor.o 2048.o html.o -OBJS+=canvas.o canvas_line.o stiv.o +OBJS+=canvas.o canvas_line.o stiv.o pixel.o R2DEPS=r_util include ../rules.mk diff --git a/libr/cons/meson.build b/libr/cons/meson.build index 6fcfaff8ad..e757749bc9 100644 --- a/libr/cons/meson.build +++ b/libr/cons/meson.build @@ -9,6 +9,7 @@ r_cons_sources = [ 'grep.c', 'hud.c', 'input.c', + 'pixel.c', 'less.c', 'stiv.c', 'line.c', diff --git a/libr/cons/pixel.c b/libr/cons/pixel.c new file mode 100644 index 0000000000..a547c00844 --- /dev/null +++ b/libr/cons/pixel.c @@ -0,0 +1,71 @@ +/* radare2 - LGPL - Copyright 2021 - pancake */ + +#include +#include + +R_API RConsPixel *r_cons_pixel_new(int w, int h) { + RConsPixel *p = R_NEW (RConsPixel); + p->w = w; + p->h = h; + p->buf_size = w * h; + p->buf = calloc (w, h); + return p; +} + +R_API void r_cons_pixel_free(RConsPixel *p) { + if (p) { + free (p->buf); + free (p); + } +} + +R_API void r_cons_pixel_set(RConsPixel *p, int x, int y, int v) { + int pos = x + (y * p->w); + if (pos > 0 && pos < p->buf_size) { + p->buf [pos] = v; + } +} + +R_API void r_cons_pixel_fill(RConsPixel *p, int _x, int _y, int w, int h, int v) { + int x, y; + for (x = _x; x < _x+w; x++) { + for (y = _y; y < _y+h; y++) { + int pos = x + (y * p->w); + if (pos > 0 && pos < p->buf_size) { + p->buf [pos] = v; + } + } + } +} + +R_API char *r_cons_pixel_drain(RConsPixel *p) { + char *s = r_cons_pixel_tostring (p); + r_cons_pixel_free (p); + return s; +} + +R_API char *r_cons_pixel_tostring(RConsPixel *p) { + RStrBuf *sb = r_strbuf_new (NULL); + size_t x, y; + for (y = 0; y < p->h; y += 4) { + for (x = 0; x < p->w; x += 2) { + ut8 *X = p->buf + (x + (y * p->w)); + int u = 0; + u |= (X[0]?$00:0); + u |= (X[1]?$01:0); + X = p->buf + (x + ((y + 1) * p->w)); + u |= (X[0]?$10:0); + u |= (X[1]?$11:0); + X = p->buf + (x + ((y + 2) * p->w)); + u |= (X[0]?$20:0); + u |= (X[1]?$21:0); + X = p->buf + (x + ((y + 3) * p->w)); + u |= (X[0]?$30:0); + u |= (X[1]?$31:0); + RBraile b = r_print_braile (u); + r_strbuf_append (sb, b.str); + } + r_strbuf_append (sb, "\n"); + } + return r_strbuf_drain (sb); +} diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index ea5e17fd83..3ce43d359c 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -3242,6 +3242,14 @@ static bool cmd_print_blocks(RCore *core, const char *input) { switch (mode) { case '-': // "p--" +{ +RConsPixel *p = r_cons_pixel_new (80, 80); +r_cons_pixel_set (p, 5, 5, 1); +r_cons_pixel_fill (p, 10, 10, 30, 30, 1); +char *s = r_cons_pixel_drain (p); +r_cons_printf ("%s%c", s, 10); +free (s); +} r_print_graphline (core->print, core->block, core->blocksize); goto cleanup; case 'j': // "p-j" diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 3305d89365..6e91729f53 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -952,6 +952,21 @@ R_API void r_cons_bind(RConsBind *bind); R_API const char* r_cons_get_rune(const ut8 ch); #endif +/* pixel.c */ +typedef struct { + int w; + int h; + ut8 *buf; + size_t buf_size; +} RConsPixel; + +R_API RConsPixel *r_cons_pixel_new(int w, int h); +R_API void r_cons_pixel_free(RConsPixel *p); +R_API char *r_cons_pixel_drain(RConsPixel *p); +R_API void r_cons_pixel_set(RConsPixel *p, int x, int y, int v); +R_API void r_cons_pixel_fill(RConsPixel *p, int _x, int _y, int w, int h, int v); +R_API char *r_cons_pixel_tostring(RConsPixel *p); + /* r_line */ #define R_LINE_BUFSIZE 4096 #define R_LINE_HISTSIZE 256 diff --git a/libr/include/r_util/r_print.h b/libr/include/r_util/r_print.h index 66c91a0fc8..88783e6da6 100644 --- a/libr/include/r_util/r_print.h +++ b/libr/include/r_util/r_print.h @@ -34,6 +34,39 @@ extern "C" { #define R_PRINT_FLAGS_BGFILL 0x00100000 #define R_PRINT_FLAGS_SECTION 0x00200000 +/* + + 1 2 1 8 x xx xx x xx x xx xx xx + 3 4 2 16 x x x xx x x x xx xx + 5 6 4 32 x x x x x xx x xx x + 7 8 1< 2< x xx xx x x xx x xx x + +*/ + +#define $00 1 +#define $01 8 +#define $10 2 +#define $11 16 +#define $20 4 +#define $21 32 +#define $30 (1 << 8) +#define $31 (2 << 8) +#define BRAILE_ONE $00+$01+$11+$21+$31 +#define BRAILE_TWO $00+$01+$11+$20+$30+$31 +#define BRAILE_TRI $00+$01+$11+$21+$30+$31 +#define BRAILE_FUR $00+$10+$11+$21+$31 +#define BRAILE_FIV $00+$01+$10+$21+$30 +#define BRAILE_SIX $01+$10+$20+$21+$30+$31 +#define BRAILE_SEV $00+$01+$11+$20+$30 +#define BRAILE_EIG $00+$01+$10+$11+$20+$21+$30+$31 +#define BRAILE_NIN $00+$01+$10+$11+$21+$30 + +typedef struct { + char str[4]; +} RBraile; + +R_API RBraile r_print_braile(int u); + typedef int (*RPrintZoomCallback)(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size); typedef const char *(*RPrintNameCallback)(void *user, ut64 addr); typedef int (*RPrintSizeCallback)(void *user, ut64 addr); diff --git a/libr/util/print.c b/libr/util/print.c index 840acab98a..ed803c7a95 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2007-2020 - pancake */ +/* radare2 - LGPL - Copyright 2007-2021 - pancake */ #include #include @@ -2386,40 +2386,10 @@ R_API void r_print_rowlog_done(RPrint *print, const char *str) { } } -/* - - 1 2 x xx xx x xx x xx xx xx - 3 4 x x x xx x x x xx xx - 5 6 x x x x x xx x xx x - 7 8 x xx xx x x xx x xx x - -*/ - -#define A0 1 -#define A1 8 -#define B0 2 -#define B1 16 -#define C0 4 -#define C1 32 -#define D0 (1 << 8) -#define D1 (2 << 8) -#define CH0(x) (x >> 8) -#define CH1(x) (x & 0xff) -#define BRAILE_ONE A1+B1+C1+D1 -#define BRAILE_TWO A0+A1+B1+C0+D0+D1 -#define BRAILE_TRI A0+A1+B1+C1+D0+D1 -#define BRAILE_FUR A0+B0+B1+C1+D1 -#define BRAILE_FIV A0+A1+B0+C1+D0 -#define BRAILE_SIX A1+B0+C0+C1+D0+D1 -#define BRAILE_SEV A0+A1+B1+C0+D0 -#define BRAILE_EIG A0+A1+B0+B1+C0+C1+D0+D1 -#define BRAILE_NIN A0+A1+B0+B1+C1+D0 - -typedef struct { - char str[4]; -} RBraile; R_API RBraile r_print_braile(int u) { +#define CH0(x) ((x) >> 8) +#define CH1(x) ((x) & 0xff) RBraile b = {0}; b.str[0] = 0xe2; b.str[1] = 0xa0 | CH0(u); @@ -2437,16 +2407,16 @@ R_API void r_print_graphline(RPrint *print, const ut8 *buf, size_t len) { ut8 ch = buf[i]; switch (0|(ch / 64)) { case 0: - brailechar = D0 + D1; + brailechar = $30 + $31; break; case 1: - brailechar = C0 + C1; + brailechar = $20 + $21; break; case 2: - brailechar = B0 + B1; + brailechar = $10 + $11; break; case 3: - brailechar = A0 + A1; + brailechar = $00 + $01; break; } if (brailechar) {