Conversões de tipo em Texto, Números, Listas e Imagem
- Texto: "texto de ( )" (qualquer valor; lista vira JSON), "código da letra ( )" e "letra do código ( )" (Unicode). - Números: "número de ( )" (aceita vírgula decimal; não-número vira 0), "( ) na base ( )" e "( ) da base ( ) para número"; tb_number_parse na API. - Listas: "lista de ( )" (texto com vírgulas ou JSON; objeto vira a lista dos valores). - Imagem: "pixels da imagem ( )" ([r,g,b,a] linha a linha), "imagem de largura ( ) com os pixels ( )", "imagem ( ) em tons de cinza" e "desenho em texto da imagem ( ) com ( ) colunas" (arte ASCII). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0c73e81c1a
commit
0103f39ffe
7 files changed
+426
-7
No files matched your search
@@ -1,6 +1,8 @@
|
||||
/*
|
||||
* TinyBlocks - tb_imglib.c
|
||||
* Categoria "Imagem": operacoes genericas e Load/Save por extensao.
|
||||
* Conversoes: imagem <-> lista de pixels ([r,g,b,a] linha a linha), tons de
|
||||
* cinza e desenho em texto (letras mais "cheias" onde e mais escuro).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
@@ -165,6 +167,155 @@ static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Conversoes ---------------------------------------------------------- */
|
||||
|
||||
#define IMG_MAX_PIXELS (4096 * 4096)
|
||||
|
||||
static TinyBlocks_JsonValue* json_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
TinyBlocks_JsonValue* c = tb_json_array();
|
||||
if (!c) return NULL;
|
||||
uint8_t v[4] = { r, g, b, a };
|
||||
for (int k = 0; k < 4; k++) {
|
||||
TinyBlocks_JsonValue* n = tb_json_number(v[k]);
|
||||
tb_json_array_push(c, n);
|
||||
tb_json_release(n);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/* "pixels da imagem": [[r,g,b,a], ...] da esquerda p/ direita, de cima p/ baixo */
|
||||
static void eval_to_list(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
for (int y = 0; img && arr && y < h; y++)
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
if (!tb_image_get_pixel(img, x, y, &r, &g, &b, &a)) continue;
|
||||
TinyBlocks_JsonValue* c = json_rgba(r, g, b, a);
|
||||
if (c) { tb_json_array_push(arr, c); tb_json_release(c); }
|
||||
}
|
||||
tb_block_set_output(self, 0, tb_value_json(arr));
|
||||
tb_json_release(arr);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static double json_num(const TinyBlocks_JsonValue* j) {
|
||||
const char* s = tb_json_as_string(j);
|
||||
return s ? strtod(s, NULL) : tb_json_as_number(j, 0);
|
||||
}
|
||||
|
||||
/* Lista de entrada: valor JSON ou texto "[...]" (ref nova ou NULL) */
|
||||
static TinyBlocks_JsonValue* in_json_list(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
TinyBlocks_JsonValue* j = NULL;
|
||||
if (v.kind == TB_VALUE_JSON && v.as.json) { j = v.as.json; tb_json_retain(j); }
|
||||
else if (v.kind == TB_VALUE_TEXT && v.as.text) j = tb_json_parse(v.as.text, NULL, 0);
|
||||
if (j && tb_json_kind(j) != TB_JSON_ARRAY) { tb_json_release(j); j = NULL; }
|
||||
return j;
|
||||
}
|
||||
|
||||
/* "imagem de largura (w) com os pixels (lista)": cada pixel e [r,g,b],
|
||||
[r,g,b,a] ou um numero (cinza); a altura sai do tamanho da lista */
|
||||
static void eval_from_list(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int w = (int)in_int(self, rt, 0);
|
||||
TinyBlocks_JsonValue* list = in_json_list(self, rt, 1);
|
||||
uint32_t n = list ? tb_json_array_length(list) : 0;
|
||||
if (w <= 0 || !n) {
|
||||
tinyblocks_set_error(rt, "precisa de largura > 0 e de uma lista de pixels");
|
||||
if (list) tb_json_release(list);
|
||||
self->dirty = false;
|
||||
return;
|
||||
}
|
||||
int h = (int)((n + (uint32_t)w - 1) / (uint32_t)w);
|
||||
if ((long long)w * h > IMG_MAX_PIXELS) {
|
||||
tinyblocks_set_error(rt, "imagem grande demais (%d x %d)", w, h);
|
||||
tb_json_release(list);
|
||||
self->dirty = false;
|
||||
return;
|
||||
}
|
||||
TinyBlocks_Image* img = tb_image_new(w, h, 4);
|
||||
for (uint32_t i = 0; img && i < n; i++) {
|
||||
TinyBlocks_JsonValue* p = tb_json_array_get(list, i);
|
||||
uint8_t c[4] = { 0, 0, 0, 255 };
|
||||
if (p && tb_json_kind(p) == TB_JSON_ARRAY) {
|
||||
uint32_t m = tb_json_array_length(p);
|
||||
for (uint32_t k = 0; k < m && k < 4; k++) {
|
||||
TinyBlocks_JsonValue* e = tb_json_array_get(p, k);
|
||||
c[k] = clamp8(json_num(e));
|
||||
if (e) tb_json_release(e);
|
||||
}
|
||||
} else if (p) {
|
||||
c[0] = c[1] = c[2] = clamp8(json_num(p));
|
||||
}
|
||||
if (p) tb_json_release(p);
|
||||
tb_image_set_pixel(img, (int)(i % (uint32_t)w), (int)(i / (uint32_t)w), c[0], c[1], c[2], c[3]);
|
||||
}
|
||||
tb_json_release(list);
|
||||
out_img(self, img);
|
||||
}
|
||||
|
||||
static uint8_t luma(uint8_t r, uint8_t g, uint8_t b) {
|
||||
return clamp8(0.299 * r + 0.587 * g + 0.114 * b);
|
||||
}
|
||||
|
||||
/* "imagem (i) em tons de cinza": copia nova, mesma transparencia */
|
||||
static void eval_gray(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* src = in_img(self, rt, 0);
|
||||
TinyBlocks_Image* img = src ? tb_image_clone(src) : NULL;
|
||||
for (int y = 0; img && y < tb_image_height(img); y++)
|
||||
for (int x = 0; x < tb_image_width(img); x++) {
|
||||
uint8_t r, g, b, a;
|
||||
if (!tb_image_get_pixel(img, x, y, &r, &g, &b, &a)) continue;
|
||||
uint8_t l = luma(r, g, b);
|
||||
tb_image_set_pixel(img, x, y, l, l, l, a);
|
||||
}
|
||||
out_img(self, img);
|
||||
}
|
||||
|
||||
/* "desenho em texto da imagem (i) com (n) colunas": arte ASCII. Cada letra
|
||||
cobre um bloco de pixels; a altura e corrigida (letras sao ~2x altas). */
|
||||
static void eval_to_text(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
static const char ramp[] = " .:-=+*#%@"; /* claro -> escuro */
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
int cols = (int)in_int(self, rt, 1);
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
if (!img || w <= 0 || h <= 0) { tb_block_set_output(self, 0, tb_value_text("")); self->dirty = false; return; }
|
||||
if (cols <= 0 || cols > w) cols = w;
|
||||
if (cols > 400) cols = 400;
|
||||
double cw = (double)w / cols, chh = cw * 2.0;
|
||||
int rows = (int)ceil(h / chh);
|
||||
if (rows < 1) rows = 1;
|
||||
char* out = malloc((size_t)(cols + 1) * (size_t)rows + 1);
|
||||
if (!out) { self->dirty = false; return; }
|
||||
char* o = out;
|
||||
for (int ry = 0; ry < rows; ry++) {
|
||||
for (int cx = 0; cx < cols; cx++) {
|
||||
int x0 = (int)(cx * cw), x1 = (int)((cx + 1) * cw), y0 = (int)(ry * chh), y1 = (int)((ry + 1) * chh);
|
||||
if (x1 <= x0) x1 = x0 + 1;
|
||||
if (y1 <= y0) y1 = y0 + 1;
|
||||
double sum = 0; int cnt = 0;
|
||||
for (int y = y0; y < y1 && y < h; y++)
|
||||
for (int x = x0; x < x1 && x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
if (!tb_image_get_pixel(img, x, y, &r, &g, &b, &a)) continue;
|
||||
/* transparente conta como claro (fundo branco) */
|
||||
sum += 255 - (255 - luma(r, g, b)) * (a / 255.0);
|
||||
cnt++;
|
||||
}
|
||||
double l = cnt ? sum / cnt : 255;
|
||||
int k = (int)((255 - l) / 256.0 * (sizeof(ramp) - 1));
|
||||
*o++ = ramp[k < 0 ? 0 : k > (int)sizeof(ramp) - 2 ? (int)sizeof(ramp) - 2 : k];
|
||||
}
|
||||
while (o > out && o[-1] == ' ') o--; /* sem espacos no fim */
|
||||
*o++ = '\n';
|
||||
}
|
||||
*o = 0;
|
||||
tb_block_set_output(self, 0, tb_value_text(out));
|
||||
free(out);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
@@ -195,6 +346,10 @@ static bool imglib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc scale_in[]= {{"imagem",TB_VALUE_IMAGE},{"largura",TB_VALUE_INTEGER},{"altura",TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc load_in[] = {{"caminho",TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc save_in[] = {{"imagem",TB_VALUE_IMAGE},{"caminho",TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc list_out[]= {{"pixels", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc text_out[]= {{"texto", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc fl_in[] = {{"largura",TB_VALUE_INTEGER},{"pixels",TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc txt_in[] = {{"imagem",TB_VALUE_IMAGE},{"colunas",TB_VALUE_INTEGER}};
|
||||
|
||||
return reg(rt, lib, "Image.New", new_in, 5, out_img, 1, eval_new)
|
||||
&& reg(rt, lib, "Image.Width", wh_in, 1, n_out, 1, eval_width)
|
||||
@@ -206,7 +361,11 @@ static bool imglib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
&& reg(rt, lib, "Image.Crop", crop_in, 5, out_img, 1, eval_crop)
|
||||
&& reg(rt, lib, "Image.Scale", scale_in,3, out_img, 1, eval_scale)
|
||||
&& reg(rt, lib, "Image.Load", load_in, 1, out_img, 1, eval_load)
|
||||
&& reg(rt, lib, "Image.Save", save_in, 2, NULL, 0, eval_save);
|
||||
&& reg(rt, lib, "Image.Save", save_in, 2, NULL, 0, eval_save)
|
||||
&& reg(rt, lib, "Image.ToList", wh_in, 1, list_out,1, eval_to_list)
|
||||
&& reg(rt, lib, "Image.FromList", fl_in, 2, out_img, 1, eval_from_list)
|
||||
&& reg(rt, lib, "Image.Gray", wh_in, 1, out_img, 1, eval_gray)
|
||||
&& reg(rt, lib, "Image.ToText", txt_in, 2, text_out,1, eval_to_text);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(imglib, "tinyblocks-image", "1.0.0", imglib_init, NULL)
|
||||
|
||||
Reference in new issue
Block a user