Implement RConsPixel and RBraile APIs ##cons

This commit is contained in:
pancake 2021-03-28 22:24:18 +02:00 committed by pancake
parent 8b3f56d803
commit 625aca16e6
7 changed files with 136 additions and 38 deletions

View File

@ -3,7 +3,7 @@ include ../config.mk
NAME=r_cons NAME=r_cons
OBJS=cons.o cpipe.o output.o grep.o less.o more.o pager.o cutf8.o 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+=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 R2DEPS=r_util
include ../rules.mk include ../rules.mk

View File

@ -9,6 +9,7 @@ r_cons_sources = [
'grep.c', 'grep.c',
'hud.c', 'hud.c',
'input.c', 'input.c',
'pixel.c',
'less.c', 'less.c',
'stiv.c', 'stiv.c',
'line.c', 'line.c',

71
libr/cons/pixel.c Normal file
View File

@ -0,0 +1,71 @@
/* radare2 - LGPL - Copyright 2021 - pancake */
#include <r_cons.h>
#include <r_util/r_print.h>
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);
}

View File

@ -3242,6 +3242,14 @@ static bool cmd_print_blocks(RCore *core, const char *input) {
switch (mode) { switch (mode) {
case '-': // "p--" 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); r_print_graphline (core->print, core->block, core->blocksize);
goto cleanup; goto cleanup;
case 'j': // "p-j" case 'j': // "p-j"

View File

@ -952,6 +952,21 @@ R_API void r_cons_bind(RConsBind *bind);
R_API const char* r_cons_get_rune(const ut8 ch); R_API const char* r_cons_get_rune(const ut8 ch);
#endif #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 */ /* r_line */
#define R_LINE_BUFSIZE 4096 #define R_LINE_BUFSIZE 4096
#define R_LINE_HISTSIZE 256 #define R_LINE_HISTSIZE 256

View File

@ -34,6 +34,39 @@ extern "C" {
#define R_PRINT_FLAGS_BGFILL 0x00100000 #define R_PRINT_FLAGS_BGFILL 0x00100000
#define R_PRINT_FLAGS_SECTION 0x00200000 #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 int (*RPrintZoomCallback)(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size);
typedef const char *(*RPrintNameCallback)(void *user, ut64 addr); typedef const char *(*RPrintNameCallback)(void *user, ut64 addr);
typedef int (*RPrintSizeCallback)(void *user, ut64 addr); typedef int (*RPrintSizeCallback)(void *user, ut64 addr);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2020 - pancake */ /* radare2 - LGPL - Copyright 2007-2021 - pancake */
#include <r_util/r_print.h> #include <r_util/r_print.h>
#include <r_anal.h> #include <r_anal.h>
@ -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) { R_API RBraile r_print_braile(int u) {
#define CH0(x) ((x) >> 8)
#define CH1(x) ((x) & 0xff)
RBraile b = {0}; RBraile b = {0};
b.str[0] = 0xe2; b.str[0] = 0xe2;
b.str[1] = 0xa0 | CH0(u); 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]; ut8 ch = buf[i];
switch (0|(ch / 64)) { switch (0|(ch / 64)) {
case 0: case 0:
brailechar = D0 + D1; brailechar = $30 + $31;
break; break;
case 1: case 1:
brailechar = C0 + C1; brailechar = $20 + $21;
break; break;
case 2: case 2:
brailechar = B0 + B1; brailechar = $10 + $11;
break; break;
case 3: case 3:
brailechar = A0 + A1; brailechar = $00 + $01;
break; break;
} }
if (brailechar) { if (brailechar) {