Adiciona registro de formatos no core e tinyblocks-image (ops genericas + Load/Save)
This commit is contained in:
1 parent
fca7aa2f63
commit
f4e2556110
11 files changed
+479
-9
No files matched your search
@@ -0,0 +1,28 @@
|
||||
# Makefile da library image (categoria "Imagem")
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_imglib.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-image.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-image.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_imglib.c include/tb_imglib.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^ -lm
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* TinyBlocks - tb_imglib.h
|
||||
* Library tinyblocks-image: operacoes genericas sobre TinyBlocks_Image e
|
||||
* carregamento/gravacao por extensao (via registro de formatos do core).
|
||||
* Reune os formatos ppm/bmp/png/jpg (quando carregados) e serve de base para
|
||||
* as libraries de alto nivel (svg, vg).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_IMGLIB_H
|
||||
#define TINYBLOCKS_IMGLIB_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_imglib_get_library(void);
|
||||
|
||||
/* -- API C --- */
|
||||
TB_API TinyBlocks_Image* tb_imglib_fill (TinyBlocks_Image* img,
|
||||
uint8_t r, uint8_t g, uint8_t b, uint8_t a);
|
||||
TB_API TinyBlocks_Image* tb_imglib_crop (const TinyBlocks_Image* img,
|
||||
int x, int y, int w, int h);
|
||||
TB_API TinyBlocks_Image* tb_imglib_scale(const TinyBlocks_Image* img,
|
||||
int w, int h, bool smooth);
|
||||
|
||||
#endif /* TINYBLOCKS_IMGLIB_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library image. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-image"
|
||||
version = "1.0.0"
|
||||
category = "Imagem"
|
||||
description = "Operacoes genericas de imagem e Load/Save por extensao"
|
||||
color = [63, 81, 181, 255]
|
||||
dark = [40, 53, 147, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_imglib.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-image.so"
|
||||
static = "build/libtinyblocks-image.a"
|
||||
depends = ["ppm", "bmp", "png"]
|
||||
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* TinyBlocks - tb_imglib.c
|
||||
* Categoria "Imagem": operacoes genericas e Load/Save por extensao.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_imglib.h"
|
||||
|
||||
TinyBlocks_Image* tb_imglib_fill(TinyBlocks_Image* img,
|
||||
uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
if (!img) return NULL;
|
||||
for (int y = 0; y < tb_image_height(img); y++)
|
||||
for (int x = 0; x < tb_image_width(img); x++)
|
||||
tb_image_set_pixel(img, x, y, r, g, b, a);
|
||||
return img;
|
||||
}
|
||||
|
||||
TinyBlocks_Image* tb_imglib_crop(const TinyBlocks_Image* img,
|
||||
int x, int y, int w, int h) {
|
||||
if (!img || w <= 0 || h <= 0) return NULL;
|
||||
TinyBlocks_Image* out = tb_image_new(w, h, tb_image_channels(img));
|
||||
if (!out) return NULL;
|
||||
for (int j = 0; j < h; j++)
|
||||
for (int i = 0; i < w; i++) {
|
||||
uint8_t r, g, b, a;
|
||||
if (tb_image_get_pixel(img, x + i, y + j, &r, &g, &b, &a))
|
||||
tb_image_set_pixel(out, i, j, r, g, b, a);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
TinyBlocks_Image* tb_imglib_scale(const TinyBlocks_Image* img,
|
||||
int w, int h, bool smooth) {
|
||||
if (!img || w <= 0 || h <= 0) return NULL;
|
||||
int sw = tb_image_width(img), sh = tb_image_height(img);
|
||||
if (sw <= 0 || sh <= 0) return NULL;
|
||||
TinyBlocks_Image* out = tb_image_new(w, h, tb_image_channels(img));
|
||||
if (!out) return NULL;
|
||||
for (int j = 0; j < h; j++) {
|
||||
for (int i = 0; i < w; i++) {
|
||||
uint8_t r, g, b, a;
|
||||
if (smooth) {
|
||||
double sx = (i + 0.5) * sw / w - 0.5;
|
||||
double sy = (j + 0.5) * sh / h - 0.5;
|
||||
int x0 = (int)floor(sx), y0 = (int)floor(sy);
|
||||
double fx = sx - x0, fy = sy - y0;
|
||||
double ar = 0, ag = 0, ab = 0, aa = 0, wsum = 0;
|
||||
for (int dy = 0; dy <= 1; dy++)
|
||||
for (int dx = 0; dx <= 1; dx++) {
|
||||
int xx = x0 + dx, yy = y0 + dy;
|
||||
if (xx < 0) xx = 0;
|
||||
if (xx >= sw) xx = sw - 1;
|
||||
if (yy < 0) yy = 0;
|
||||
if (yy >= sh) yy = sh - 1;
|
||||
double ww = (dx ? fx : 1 - fx) * (dy ? fy : 1 - fy);
|
||||
uint8_t rr, gg, bb, al;
|
||||
if (tb_image_get_pixel(img, xx, yy, &rr, &gg, &bb, &al)) {
|
||||
ar += rr*ww; ag += gg*ww; ab += bb*ww; aa += al*ww; wsum += ww;
|
||||
}
|
||||
}
|
||||
if (wsum <= 0) wsum = 1;
|
||||
r = (uint8_t)(ar/wsum); g = (uint8_t)(ag/wsum);
|
||||
b = (uint8_t)(ab/wsum); a = (uint8_t)(aa/wsum);
|
||||
} else {
|
||||
int xx = i * sw / w, yy = j * sh / h;
|
||||
if (!tb_image_get_pixel(img, xx, yy, &r, &g, &b, &a)) continue;
|
||||
}
|
||||
tb_image_set_pixel(out, i, j, r, g, b, a);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* -- Helpers de entrada -------------------------------------------------- */
|
||||
|
||||
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
}
|
||||
static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text);
|
||||
return 0.0;
|
||||
}
|
||||
static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return (int)in_real(self, rt, i); }
|
||||
static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL;
|
||||
}
|
||||
static uint8_t clamp8(double v) { return v < 0 ? 0 : v > 255 ? 255 : (uint8_t)(v + 0.5); }
|
||||
|
||||
static void out_img(TinyBlocks_Block* self, TinyBlocks_Image* img) {
|
||||
if (!img) { self->dirty = false; return; }
|
||||
tb_block_set_output(self, 0, tb_value_image(img));
|
||||
tb_image_release(img);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Blocos -------------------------------------------------------------- */
|
||||
|
||||
static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int w = in_int(self, rt, 0), h = in_int(self, rt, 1);
|
||||
TinyBlocks_Image* img = tb_image_new(w > 0 ? w : 1, h > 0 ? h : 1, 4);
|
||||
if (!img) { tinyblocks_set_error(rt, "sem memoria"); self->dirty = false; return; }
|
||||
tb_imglib_fill(img, clamp8(in_real(self,rt,2)), clamp8(in_real(self,rt,3)),
|
||||
clamp8(in_real(self,rt,4)), 255);
|
||||
out_img(self, img);
|
||||
}
|
||||
static void eval_width (TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self,0,tb_value_integer(tb_image_width (in_img(self,rt,0)))); self->dirty = false; }
|
||||
static void eval_height(TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self,0,tb_value_integer(tb_image_height(in_img(self,rt,0)))); self->dirty = false; }
|
||||
|
||||
static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
uint8_t r=0,g=0,b=0,a=0;
|
||||
tb_image_get_pixel(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), &r,&g,&b,&a);
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
tb_json_array_push(arr, tb_json_number(r));
|
||||
tb_json_array_push(arr, tb_json_number(g));
|
||||
tb_json_array_push(arr, tb_json_number(b));
|
||||
tb_json_array_push(arr, tb_json_number(a));
|
||||
tb_block_set_output(self, 0, tb_value_json(arr));
|
||||
tb_json_release(arr);
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_setpixel(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self,rt,0);
|
||||
if (img) tb_image_set_pixel(img, in_int(self,rt,1), in_int(self,rt,2),
|
||||
clamp8(in_real(self,rt,3)), clamp8(in_real(self,rt,4)),
|
||||
clamp8(in_real(self,rt,5)), 255);
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_fill(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_imglib_fill(in_img(self,rt,0), clamp8(in_real(self,rt,1)),
|
||||
clamp8(in_real(self,rt,2)), clamp8(in_real(self,rt,3)), 255);
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_clone(TinyBlocks_Block* self, TinyBlocks* rt) { out_img(self, tb_image_clone(in_img(self,rt,0))); }
|
||||
static void eval_crop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_img(self, tb_imglib_crop(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2),
|
||||
in_int(self,rt,3), in_int(self,rt,4)));
|
||||
}
|
||||
static void eval_scale(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_img(self, tb_imglib_scale(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), true));
|
||||
}
|
||||
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = tb_image_load(in_text(self,rt,0));
|
||||
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", in_text(self,rt,0)); self->dirty = false; return; }
|
||||
out_img(self, img);
|
||||
}
|
||||
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self,rt,0);
|
||||
if (!img || !tb_image_save(img, in_text(self,rt,1)))
|
||||
tinyblocks_set_error(rt, "nao gravou '%s' (formato?)", in_text(self,rt,1));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool imglib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Imagem",
|
||||
{ 63/255.f, 81/255.f, 181/255.f, 1.f},
|
||||
{ 40/255.f, 53/255.f, 147/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc out_img[] = {{"imagem", TB_VALUE_IMAGE}};
|
||||
static const TinyBlocks_SlotDesc n_out[] = {{"n", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc pix_out[] = {{"cor", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc new_in[] = {{"largura",TB_VALUE_REAL},{"altura",TB_VALUE_REAL},{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc wh_in[] = {{"imagem",TB_VALUE_IMAGE}};
|
||||
static const TinyBlocks_SlotDesc getp_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc setp_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER},{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc fill_in[] = {{"imagem",TB_VALUE_IMAGE},{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc crop_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER},{"largura",TB_VALUE_INTEGER},{"altura",TB_VALUE_INTEGER}};
|
||||
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}};
|
||||
|
||||
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)
|
||||
&& reg(rt, lib, "Image.Height", wh_in, 1, n_out, 1, eval_height)
|
||||
&& reg(rt, lib, "Image.GetPixel", getp_in, 3, pix_out, 1, eval_getpixel)
|
||||
&& reg(rt, lib, "Image.SetPixel", setp_in, 6, NULL, 0, eval_setpixel)
|
||||
&& reg(rt, lib, "Image.Fill", fill_in, 4, NULL, 0, eval_fill)
|
||||
&& reg(rt, lib, "Image.Clone", wh_in, 1, out_img, 1, eval_clone)
|
||||
&& 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);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(imglib, "tinyblocks-image", "1.0.0", imglib_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_imglib_get_library(void) {
|
||||
return &_tb_lib_desc_imglib;
|
||||
}
|
||||
Reference in new issue
Block a user