From f4e255611031ffef56e24d0aeae409c258dd4795 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Tue, 20 Oct 2026 21:08:18 -0300 Subject: [PATCH] Adiciona registro de formatos no core e tinyblocks-image (ops genericas + Load/Save) --- Makefile | 27 ++- include/tinyblocks/tb_image.h | 14 ++ libraries/tinyblocks-bmp/src/tb_bmp.c | 4 +- libraries/tinyblocks-image/Makefile | 28 +++ .../tinyblocks-image/include/tb_imglib.h | 23 ++ libraries/tinyblocks-image/library.toml | 15 ++ libraries/tinyblocks-image/src/tb_imglib.c | 210 ++++++++++++++++++ libraries/tinyblocks-png/src/tb_png.c | 4 +- libraries/tinyblocks-ppm/src/tb_ppm.c | 4 +- src/tb_image.c | 59 +++++ tests/image_test.c | 100 +++++++++ 11 files changed, 479 insertions(+), 9 deletions(-) create mode 100644 libraries/tinyblocks-image/Makefile create mode 100644 libraries/tinyblocks-image/include/tb_imglib.h create mode 100644 libraries/tinyblocks-image/library.toml create mode 100644 libraries/tinyblocks-image/src/tb_imglib.c create mode 100644 tests/image_test.c diff --git a/Makefile b/Makefile index c3b3cf5..de1c3df 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ CV_DIR := $(LIBDIR)/tinyblocks-curves PPM_DIR := $(LIBDIR)/tinyblocks-ppm BMP_DIR := $(LIBDIR)/tinyblocks-bmp PNG_DIR := $(LIBDIR)/tinyblocks-png +IMG_DIR := $(LIBDIR)/tinyblocks-image API_DIR := $(LIBDIR)/tinyblocks-api LIBM_DIR := $(LIBDIR)/tinyblocks-libman ED_DIR := $(LIBDIR)/tinyblocks-editor @@ -84,12 +85,12 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \ .PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \ editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \ appdir appimage run-appimage icon \ - test-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png \ + test-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png test-image \ category-libs category-sos libs \ lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \ lib-environ \ lib-editor lib-graphics lib-sdl2 lib-python lib-mcp lib-toml \ - lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-api lib-libman \ + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-api lib-libman \ test-libman test-environ all: static example ## Compila a biblioteca estática e o exemplo hello (padrão) @@ -180,6 +181,9 @@ lib-bmp: lib-png: $(MAKE) -C $(PNG_DIR) +lib-image: + $(MAKE) -C $(IMG_DIR) + lib-api: $(MAKE) -C $(API_DIR) @@ -200,8 +204,8 @@ lib-mcp: libs: static lib-events lib-control lib-operators lib-variables \ lib-appearance lib-pen lib-fs lib-environ lib-graphics lib-sdl2 lib-toml \ - lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-api lib-libman lib-editor - @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + api + libman + editor" + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-api lib-libman lib-editor + @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + api + libman + editor" category-libs: static $(foreach l,$(CAT_LIBS),lib-$(l)) ## Compila as libraries de categoria (.a/.so) @echo "Build category libs OK" @@ -252,7 +256,7 @@ run-editor: editor ## Compila e abre o editor visual # ── Empacotamento AppImage ────────────────────────────────────────────── # Libraries com blocos (categoria) que vao empacotadas como extensoes. -APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png +APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image APP_NAME := TinyBlocks APP_VER := 0.1.0 @@ -497,6 +501,17 @@ $(BUILD)/png_test: tests/png_test.c $(PNG_DIR)/src/tb_png.c static test-png: $(BUILD)/png_test ## Roda o teste da png (ASan/UBSan) ./$(BUILD)/png_test +# -- Teste da library image (ops genericas + registro de formatos) --- +$(BUILD)/image_test: tests/image_test.c $(IMG_DIR)/src/tb_imglib.c $(PPM_DIR)/src/tb_ppm.c $(BMP_DIR)/src/tb_bmp.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(IMG_DIR)/include -I$(PPM_DIR)/include -I$(BMP_DIR)/include \ + $(IMG_DIR)/src/tb_imglib.c $(PPM_DIR)/src/tb_ppm.c $(BMP_DIR)/src/tb_bmp.c \ + tests/image_test.c -o $@ \ + -Wl,--start-group $(LIB_A) -Wl,--end-group -lm + +test-image: $(BUILD)/image_test ## Roda o teste da image (ASan/UBSan) + ./$(BUILD)/image_test + # -- Teste de plugin .so standalone --- FS_SO := $(FS_DIR)/build/libtinyblocks-fs.so @@ -513,7 +528,7 @@ clean: ## Remove os diretórios build/ (raiz e libraries) $(RM) -r $(BUILD) @for d in $(EV_DIR) $(CT_DIR) $(OP_DIR) $(VR_DIR) $(AP_DIR) $(PN_DIR) $(FS_DIR) \ $(TOML_DIR) $(JSON_DIR) $(XML_DIR) $(CSV_DIR) $(BP_DIR) \ - $(B64_DIR) $(CV_DIR) $(PPM_DIR) $(BMP_DIR) $(PNG_DIR) \ + $(B64_DIR) $(CV_DIR) $(PPM_DIR) $(BMP_DIR) $(PNG_DIR) $(IMG_DIR) \ $(API_DIR) $(LIBM_DIR) \ $(ED_DIR) $(GX_DIR) $(SD_DIR) $(PY_DIR) $(MC_DIR); do \ $(MAKE) -C $$d clean >/dev/null 2>&1 || true; \ diff --git a/include/tinyblocks/tb_image.h b/include/tinyblocks/tb_image.h index 0088029..087b31f 100644 --- a/include/tinyblocks/tb_image.h +++ b/include/tinyblocks/tb_image.h @@ -31,6 +31,20 @@ TB_API bool tb_image_set_pixel(TinyBlocks_Image* img, int x, int y, TB_API bool tb_image_get_pixel(const TinyBlocks_Image* img, int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a); +/* -- Registro de formatos (alimentado pelas libraries ppm/bmp/png/jpg) --- + * A library guarda-chuva tinyblocks-image usa este registro para carregar e + * gravar imagens escolhendo o decodificador/codificador pela extensao do + * arquivo, sem depender diretamente das libraries de formato. */ +typedef TinyBlocks_Image* (*TbImageDecoder)(const char* path); +typedef bool (*TbImageEncoder)(const TinyBlocks_Image* img, const char* path); + +TB_API bool tb_image_register_format(const char* ext, + TbImageDecoder dec, TbImageEncoder enc); +TB_API int tb_image_format_count(void); +/* Carrega/grava escolhendo o formato pela extensao do caminho. */ +TB_API TinyBlocks_Image* tb_image_load(const char* path); +TB_API bool tb_image_save(const TinyBlocks_Image* img, const char* path); + /* -- Helpers para TinyBlocks_Value --- */ TB_API TinyBlocks_Value tb_value_image (TinyBlocks_Image* img); /* retém ref */ TB_API TinyBlocks_Image* tb_value_as_image(TinyBlocks_Value v); diff --git a/libraries/tinyblocks-bmp/src/tb_bmp.c b/libraries/tinyblocks-bmp/src/tb_bmp.c index a811eec..948a1cc 100644 --- a/libraries/tinyblocks-bmp/src/tb_bmp.c +++ b/libraries/tinyblocks-bmp/src/tb_bmp.c @@ -167,8 +167,10 @@ static bool bmp_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { 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, "BMP.Load", load_in, 1, out_img, 1, eval_load) + bool ok = reg(rt, lib, "BMP.Load", load_in, 1, out_img, 1, eval_load) && reg(rt, lib, "BMP.Save", save_in, 2, NULL, 0, eval_save); + if (ok) tb_image_register_format("bmp", tb_bmp_load, tb_bmp_save); + return ok; } TINYBLOCKS_DEFINE_LIBRARY(bmp, "tinyblocks-bmp", "1.0.0", bmp_init, NULL) diff --git a/libraries/tinyblocks-image/Makefile b/libraries/tinyblocks-image/Makefile new file mode 100644 index 0000000..cfad7b7 --- /dev/null +++ b/libraries/tinyblocks-image/Makefile @@ -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) diff --git a/libraries/tinyblocks-image/include/tb_imglib.h b/libraries/tinyblocks-image/include/tb_imglib.h new file mode 100644 index 0000000..29f3103 --- /dev/null +++ b/libraries/tinyblocks-image/include/tb_imglib.h @@ -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 + +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 */ diff --git a/libraries/tinyblocks-image/library.toml b/libraries/tinyblocks-image/library.toml new file mode 100644 index 0000000..3967464 --- /dev/null +++ b/libraries/tinyblocks-image/library.toml @@ -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"] diff --git a/libraries/tinyblocks-image/src/tb_imglib.c b/libraries/tinyblocks-image/src/tb_imglib.c new file mode 100644 index 0000000..e47f5f9 --- /dev/null +++ b/libraries/tinyblocks-image/src/tb_imglib.c @@ -0,0 +1,210 @@ +/* + * TinyBlocks - tb_imglib.c + * Categoria "Imagem": operacoes genericas e Load/Save por extensao. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include + +#include +#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; +} diff --git a/libraries/tinyblocks-png/src/tb_png.c b/libraries/tinyblocks-png/src/tb_png.c index 10aad36..3a8daf7 100644 --- a/libraries/tinyblocks-png/src/tb_png.c +++ b/libraries/tinyblocks-png/src/tb_png.c @@ -118,8 +118,10 @@ static bool png_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { 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, "PNG.Load", load_in, 1, out_img, 1, eval_load) + bool ok = reg(rt, lib, "PNG.Load", load_in, 1, out_img, 1, eval_load) && reg(rt, lib, "PNG.Save", save_in, 2, NULL, 0, eval_save); + if (ok) tb_image_register_format("png", tb_png_load, tb_png_save); + return ok; } TINYBLOCKS_DEFINE_LIBRARY(png, "tinyblocks-png", "1.0.0", png_init, NULL) diff --git a/libraries/tinyblocks-ppm/src/tb_ppm.c b/libraries/tinyblocks-ppm/src/tb_ppm.c index f3ea139..d6a8d24 100644 --- a/libraries/tinyblocks-ppm/src/tb_ppm.c +++ b/libraries/tinyblocks-ppm/src/tb_ppm.c @@ -231,13 +231,15 @@ static bool ppm_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { {"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc getp_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER}}; - return reg(rt, lib, "PPM.Load", load_in, 1, img_out, 1, eval_load) + bool ok = reg(rt, lib, "PPM.Load", load_in, 1, img_out, 1, eval_load) && reg(rt, lib, "PPM.Save", save_in, 2, NULL, 0, eval_save) && reg(rt, lib, "PPM.New", new_in, 5, img_out, 1, eval_new) && reg(rt, lib, "PPM.Width", wh_in, 1, n_out, 1, eval_width) && reg(rt, lib, "PPM.Height", wh_in, 1, n_out, 1, eval_height) && reg(rt, lib, "PPM.SetPixel", setp_in, 6, NULL, 0, eval_setpixel) && reg(rt, lib, "PPM.GetPixel", getp_in, 3, rgb_out, 1, eval_getpixel); + if (ok) tb_image_register_format("ppm", tb_ppm_load, tb_ppm_save); + return ok; } TINYBLOCKS_DEFINE_LIBRARY(ppm, "tinyblocks-ppm", "1.0.0", ppm_init, NULL) diff --git a/src/tb_image.c b/src/tb_image.c index 6aa74df..1e81cd1 100644 --- a/src/tb_image.c +++ b/src/tb_image.c @@ -4,6 +4,7 @@ */ #include #include +#include #include "tinyblocks/tb_image.h" @@ -93,6 +94,64 @@ bool tb_image_get_pixel(const TinyBlocks_Image* img, int x, int y, return true; } +/* -- Registro de formatos ------------------------------------------------- */ + +#define TB_IMAGE_MAX_FORMATS 24 + +typedef struct { + char ext[12]; + TbImageDecoder dec; + TbImageEncoder enc; +} ImageFormat; + +static ImageFormat s_formats[TB_IMAGE_MAX_FORMATS]; +static int s_format_n = 0; + +bool tb_image_register_format(const char* ext, TbImageDecoder dec, TbImageEncoder enc) { + if (!ext || !ext[0]) return false; + for (int i = 0; i < s_format_n; i++) + if (strcmp(s_formats[i].ext, ext) == 0) { /* sobrescreve */ + s_formats[i].dec = dec; + s_formats[i].enc = enc; + return true; + } + if (s_format_n >= TB_IMAGE_MAX_FORMATS) return false; + ImageFormat* f = &s_formats[s_format_n++]; + size_t e = strlen(ext); + if (e >= sizeof(f->ext)) e = sizeof(f->ext) - 1; + for (size_t i = 0; i < e; i++) f->ext[i] = (char)tolower((unsigned char)ext[i]); + f->ext[e] = 0; + f->dec = dec; + f->enc = enc; + return true; +} + +int tb_image_format_count(void) { return s_format_n; } + +static const ImageFormat* format_for(const char* path) { + if (!path) return NULL; + const char* dot = strrchr(path, '.'); + if (!dot || !dot[1]) return NULL; + char ext[12]; + size_t n = 0; + for (const char* p = dot + 1; *p && n + 1 < sizeof(ext); p++) + ext[n++] = (char)tolower((unsigned char)*p); + ext[n] = 0; + for (int i = 0; i < s_format_n; i++) + if (strcmp(s_formats[i].ext, ext) == 0) return &s_formats[i]; + return NULL; +} + +TinyBlocks_Image* tb_image_load(const char* path) { + const ImageFormat* f = format_for(path); + return (f && f->dec) ? f->dec(path) : NULL; +} + +bool tb_image_save(const TinyBlocks_Image* img, const char* path) { + const ImageFormat* f = format_for(path); + return (f && f->enc) ? f->enc(img, path) : false; +} + TinyBlocks_Value tb_value_image(TinyBlocks_Image* img) { TinyBlocks_Value v = { .kind = TB_VALUE_IMAGE, .as.image = img }; if (img) tb_image_retain(img); diff --git a/tests/image_test.c b/tests/image_test.c new file mode 100644 index 0000000..6ad3c4b --- /dev/null +++ b/tests/image_test.c @@ -0,0 +1,100 @@ +/* + * image_test.c - testa tinyblocks-image: operacoes genericas (fill/crop/scale), + * o registro de formatos do core (Load/Save por extensao) e os blocos Image.*. + */ +#include +#include +#include +#include +#include +#include +#include + +static int fails = 0; +#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0) + +static void set_num(TinyBlocks_Block* b, uint32_t i, double v) { + TinyBlocks_Value vv = tb_value_real(v); + tb_block_set_default(b, i, vv); + tb_value_free(&vv); +} +static void set_img(TinyBlocks_Block* b, uint32_t i, TinyBlocks_Image* im) { + TinyBlocks_Value vv = tb_value_image(im); + tb_block_set_default(b, i, vv); + tb_value_free(&vv); +} + +int main(void) { + /* -- Registro de formatos --- */ + CHECK(tb_image_format_count() == 0, "registro de formatos comeca vazio"); + tb_image_register_format("ppm", tb_ppm_load, tb_ppm_save); + tb_image_register_format("bmp", tb_bmp_load, tb_bmp_save); + CHECK(tb_image_format_count() == 2, "dois formatos registrados"); + + TinyBlocks_Image* img = tb_image_new(8, 8, 4); + tb_imglib_fill(img, 10, 20, 30, 255); + uint8_t r, g, b, a; + tb_image_get_pixel(img, 4, 4, &r, &g, &b, &a); + CHECK(r == 10 && g == 20 && b == 30 && a == 255, "tb_imglib_fill preenche"); + + /* Save/Load por extensao (dispatch) */ + CHECK(tb_image_save(img, "build/image_test.bmp"), "tb_image_save despacha por .bmp"); + TinyBlocks_Image* rd = tb_image_load("build/image_test.bmp"); + CHECK(rd && tb_image_width(rd) == 8 && tb_image_height(rd) == 8, "tb_image_load despacha por .bmp"); + if (rd) tb_image_release(rd); + remove("build/image_test.bmp"); + + CHECK(tb_image_load("build/x.tga") == NULL, "extensao desconhecida -> NULL"); + + /* Crop e Scale */ + TinyBlocks_Image* c = tb_imglib_crop(img, 2, 2, 4, 4); + CHECK(c && tb_image_width(c) == 4 && tb_image_height(c) == 4, "tb_imglib_crop 4x4"); + TinyBlocks_Image* s = tb_imglib_scale(img, 16, 4, true); + CHECK(s && tb_image_width(s) == 16 && tb_image_height(s) == 4, "tb_imglib_scale 16x4"); + if (c) tb_image_release(c); + if (s) tb_image_release(s); + tb_image_release(img); + + /* -- Blocos --- */ + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_imglib_get_library()); + tb_library_register(rt, tb_ppm_get_library()); /* re-registra formatos */ + + const char* names[] = {"Image.New","Image.Width","Image.Height","Image.GetPixel", + "Image.SetPixel","Image.Fill","Image.Clone","Image.Crop", + "Image.Scale","Image.Load","Image.Save"}; + int all = 1; + for (size_t i = 0; i < sizeof(names)/sizeof(names[0]); i++) + if (!tb_rt_find_type(rt, names[i])) { all = 0; printf(" falta %s\n", names[i]); } + CHECK(all, "todos os blocos Image.* registrados"); + { + TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, "Image.New"); + const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)t->base.parent); + CHECK(cat && strcmp(cat->name, "Imagem") == 0, "categoria 'Imagem'"); + } + + TinyBlocks_Block* bn = tb_block_create(rt, tb_rt_find_type(rt, "Image.New")); + set_num(bn, 0, 5); set_num(bn, 1, 4); + set_num(bn, 2, 200); set_num(bn, 3, 100); set_num(bn, 4, 50); + tb_block_eval(bn, rt); + CHECK(bn->outputs[0].kind == TB_VALUE_IMAGE, "Image.New devolve imagem"); + TinyBlocks_Image* bi = bn->outputs[0].as.image; + tb_image_get_pixel(bi, 0, 0, &r, &g, &b, &a); + CHECK(r == 200 && g == 100 && b == 50, "Image.New com a cor pedida"); + + TinyBlocks_Block* bc = tb_block_create(rt, tb_rt_find_type(rt, "Image.Crop")); + set_img(bc, 0, bi); + set_num(bc, 1, 1); set_num(bc, 2, 1); set_num(bc, 3, 3); set_num(bc, 4, 2); + tb_block_eval(bc, rt); + CHECK(bc->outputs[0].kind == TB_VALUE_IMAGE && + tb_image_width(bc->outputs[0].as.image) == 3 && + tb_image_height(bc->outputs[0].as.image) == 2, "Image.Crop devolve 3x2"); + + tb_block_destroy(bn, rt); tb_block_destroy(bc, rt); + tinyblocks_deinit(&rt); + + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +}