Initial implementation of p3 the 3D stereogram print mode

This commit is contained in:
pancake 2015-02-14 03:23:21 +01:00
parent 36ecbc21ff
commit 4c58ac2416
9 changed files with 258 additions and 2 deletions

41
doc/3D/3D-logo.r2 Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/r2 --
ecr
o malloc://2048
b 18
# -. R
wow 27 @ 14+(80*5) + 2!16-4
wow 00 @ 10+(80*5) + 13!2
wow 27 @ 14+(80*6) + 0!14
wow 27 @ 14+(80*7) + 0!14
wow 00 @ 14+(80*7) + 0!1
wow 00 @ 14+(80*7) + 4!5
wow 27 @ 10+(80*8) + 15!3
wow 27 @ 10+(80*9) + 15!3
wow 27 @ 10+(80*10) + 15!3
wow 27 @ 10+(80*11) + 15!3
wow 27 @ 10+(80*12) + 15!3
wow 27 @ 10+(80*13) + 15!3
# <|
wow 27 @ 25+(80*5) + 17!1
wow 27 @ 25+(80*6) + 14!4
wow 27 @ 25+(80*7) + 10!8
wow 27 @ 25+(80*8) + 5!13
wow 27 @ 25+(80*9) + 2!16
wow 27 @ 25+(80*10) + 5!13
wow 27 @ 25+(80*11) + 10!8
wow 27 @ 25+(80*12) + 14!4
wow 27 @ 25+(80*13) + 17!1
# <|
wow 27 @ 40+(80*5) + 17!1
wow 27 @ 40+(80*6) + 14!4
wow 27 @ 40+(80*7) + 10!8
wow 27 @ 40+(80*8) + 5!13
wow 27 @ 40+(80*9) + 2!16
wow 27 @ 40+(80*10) + 5!13
wow 27 @ 40+(80*11) + 10!8
wow 27 @ 40+(80*12) + 14!4
wow 27 @ 40+(80*13) + 17!1
b 2048
e hex.cols=80
px
p3

38
doc/3D/3D-pyramid.r2 Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/r2 --
o malloc://2048
b 28
# []
wow 27 @ 25+(80*3)
wow 27 @ 25+(80*4)
wow 27 @ 25+(80*5)
wow 27 @ 25+(80*6)
wow 27 @ 25+(80*7)
wow 27 @ 25+(80*8)
wow 27 @ 25+(80*9)
wow 27 @ 25+(80*10)
wow 27 @ 25+(80*11)
wow 27 @ 25+(80*12)
wow 27 @ 25+(80*13)
wow 27 @ 25+(80*14)
# []
f delta=4
b 24-delta
wow 40 @ 25+delta+(80*5)
wow 40 @ 25+delta+(80*6)
wow 40 @ 25+delta+(80*7)
wow 40 @ 25+delta+(80*8)
wow 40 @ 25+delta+(80*9)
wow 40 @ 25+delta+(80*10)
wow 40 @ 25+delta+(80*11)
wow 40 @ 25+delta+(80*12)
# []
f delta=8
b 24-delta-4
wow 52 @ 25+delta+(80*7)
wow 52 @ 25+delta+(80*8)
wow 52 @ 25+delta+(80*9)
wow 52 @ 25+delta+(80*10)
b 18*80
e hex.cols=80
# px
p3

20
doc/3D/3D-square.r2 Normal file
View File

@ -0,0 +1,20 @@
#!/usr/bin/r2 --
o malloc://2048
b 28
# []
wow 27 @ 25+(80*3)
wow 27 @ 25+(80*4)
wow 27 @ 25+(80*5)
wow 27 @ 25+(80*6)
wow 27 @ 25+(80*7)
wow 27 @ 25+(80*8)
wow 27 @ 25+(80*9)
wow 27 @ 25+(80*10)
wow 27 @ 25+(80*11)
wow 27 @ 25+(80*12)
wow 27 @ 25+(80*13)
wow 27 @ 25+(80*14)
b 18*80
e hex.cols=80
px
p3

10
doc/3D/Makefile Normal file
View File

@ -0,0 +1,10 @@
RUN=square pyramid logo
TARGETS=$(addsuffix .r2, $(addprefix 3D-,${RUN}))
all: $(TARGETS)
*.r2:
r2 -qi $@ --
.PHONY: *.r2

18
doc/3D/pyramid Normal file
View File

@ -0,0 +1,18 @@
11111111111111111111111111111111111
11111111111111111111111111111111111
11112222222222222222222222222211111
11112222222222222222222222222211111
11112222222222222222222222222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222233333333333333322222211111
11112222222222222222222222222211111
11112222222222222222222222222211111
11112222222222222222222222222211111
11111111111111111111111111111111111
11111111111111111111111111111111111

View File

@ -1763,6 +1763,23 @@ static int cmd_print(void *data, const char *input) {
r_print_raw (core->print, core->block, len, 0);
}
break;
case '3': // "p3" [file]
if (input[1]=='?') {
eprintf ("Usage: p3 [file] - print 3D stereogram image of current block\n");
} else
if (input[1]==' ') {
char *data = r_file_slurp (input+2, NULL);
char *res = r_print_stereogram (data, 78, 20);
r_print_stereogram_print (core->print, res);
//if (data) eprintf ("%s\n", data);
free (res);
free (data);
} else {
char *res = r_print_stereogram_bytes (core->block, core->blocksize);
r_print_stereogram_print (core->print, res);
free (res);
}
break;
case 'x': // "px"
{
int show_offset = r_config_get_i (core->config, "asm.offset");

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2014 - pancake */
/* radare - LGPL - Copyright 2009-2015 - pancake */
#include "r_core.h"

View File

@ -119,6 +119,9 @@ R_API void r_print_set_interrupted(int i);
// WIP
R_API int r_print_unpack7bit(const char *src, char *dest);
R_API int r_print_pack7bit(const char *src, char *dest);
R_API char *r_print_stereogram_bytes(const ut8 *buf, int len);
R_API char *r_print_stereogram(const char *bump, int w, int h);
R_API void r_print_stereogram_print(RPrint *p, const char *buf);
#endif
#ifdef __cplusplus

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2014 - pancake */
/* radare - LGPL - Copyright 2007-2015 - pancake */
#include "r_anal.h"
#include "r_cons.h"
@ -31,6 +31,115 @@ R_API int r_print_mute(RPrint *p, int x) {
return 0;
}
static int r_print_stereogram_private(const char *bump, int w, int h, char *out, int size) {
static char data[32768]; // ???
const char *string = "Az+|.-=/^@_pT";
const int string_len = strlen (string);
if (!bump || !out)
return 0;
int x, y, s, l = 0, l2 = 0, ch;
int skip = 7;
int bumpi = 0, outi = 0;
for (y = 0; bump[bumpi] && outi<size; y++) {
l = l2 = 0;
for (x = 0; bump[bumpi] && outi<size && x<w; x++) {
ch = string[x%string_len];
if (!l && x > skip) {
s = bump[bumpi++];
if (s >= '0' && s <='9') {
s = '0' - s;
} else switch (s) {
case 0: bumpi--; /* passthru */
case '\n': s = 0; l = 1; break;
case ' ': s = 0; break;
default: s = -2; break;
}
} else {
s = 0;
}
s += skip;
s = x - s;
if (s>=0)
ch = data[s];
if (!ch) ch = *string;
data[x] = ch;
if (outi<size) {
out[outi++] = ch;
} else break;
}
out[outi++] = '\n';
s = 'a';
while (!l && s != '\n') {
s = bump[bumpi++];
if (!s) {
bumpi--;
break;
}
}
}
out[outi] = 0;
return 1;
}
R_API char *r_print_stereogram(const char *bump, int w, int h) {
ut64 size;
char *out;
if (w<1 || h<1)
return NULL;
size = w * h*2;
if (size>UT32_MAX) {
return NULL;
}
out = calloc (1, size * 2);
if (!out) {
return NULL;
}
//eprintf ("%s\n", bump);
(void)r_print_stereogram_private (bump, w, h, out, size);
return out;
}
#define STEREOGRAM_IN_COLOR 1
R_API char *r_print_stereogram_bytes(const ut8 *buf, int len) {
int i, bumpi;
char *ret, *bump;
int scr_width = 80;
int rows, cols, size;
if (!buf || len<1)
return NULL;
//scr_width = r_cons_get_size (NULL) -10;
cols = scr_width;
rows = len / cols;
size = (2+cols) * rows;
bump = malloc (size+1); //(cols+2) * rows);
for (i = bumpi = 0; bumpi < size && i < len; i++) {
int v = buf[i] / 26;
if (i && !(i%scr_width))
bump[bumpi++] = '\n';
bump[bumpi++] = '0' + v;
}
bump[bumpi] = 0;
ret = r_print_stereogram (bump, cols, rows);
return ret;
}
R_API void r_print_stereogram_print(RPrint *p, const char *ret) {
int i;
int use_color = p? (p->flags & R_PRINT_FLAGS_COLOR): 0;
if (!ret) return;
if (use_color) {
for (i=0; ret[i]; i++) {
p->printf ("\x1b[%dm%c", 30+(ret[i]%8), ret[i]);
}
p->printf ("\x1b[0m\n");
} else {
p->printf ("%s\n", ret);
}
}
R_API RPrint *r_print_new() {
RPrint *p = R_NEW0 (RPrint);
if (!p) return NULL;