From e36fbc43e1f1dc0e377085aed64b8c59ec5cca4a Mon Sep 17 00:00:00 2001 From: mguinhos Date: Tue, 20 Oct 2026 21:15:29 -0300 Subject: [PATCH] Adiciona tinyblocks-jpg (libjpeg, build condicional) e melhora o empacotamento AppImage --- Makefile | 39 ++++- libraries/tinyblocks-jpg/Makefile | 43 +++++ libraries/tinyblocks-jpg/include/tb_jpg.h | 17 ++ libraries/tinyblocks-jpg/src/tb_jpg.c | 197 ++++++++++++++++++++++ libraries/tinyblocks-svg/Makefile | 2 +- tests/jpg_test.c | 46 +++++ 6 files changed, 337 insertions(+), 7 deletions(-) create mode 100644 libraries/tinyblocks-jpg/Makefile create mode 100644 libraries/tinyblocks-jpg/include/tb_jpg.h create mode 100644 libraries/tinyblocks-jpg/src/tb_jpg.c create mode 100644 tests/jpg_test.c diff --git a/Makefile b/Makefile index 72a9c77..7b752ab 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ PNG_DIR := $(LIBDIR)/tinyblocks-png IMG_DIR := $(LIBDIR)/tinyblocks-image VG_DIR := $(LIBDIR)/tinyblocks-vg SVG_DIR := $(LIBDIR)/tinyblocks-svg +JPG_DIR := $(LIBDIR)/tinyblocks-jpg API_DIR := $(LIBDIR)/tinyblocks-api LIBM_DIR := $(LIBDIR)/tinyblocks-libman ED_DIR := $(LIBDIR)/tinyblocks-editor @@ -87,12 +88,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-image test-vg test-svg \ + test-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ 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-image lib-vg lib-svg 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-vg lib-svg lib-jpg lib-api lib-libman \ test-libman test-environ all: static example ## Compila a biblioteca estática e o exemplo hello (padrão) @@ -192,6 +193,9 @@ lib-vg: lib-svg: lib-vg lib-base64 $(MAKE) -C $(SVG_DIR) +lib-jpg: + $(MAKE) -C $(JPG_DIR) + lib-api: $(MAKE) -C $(API_DIR) @@ -212,8 +216,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-image lib-vg lib-svg lib-api lib-libman lib-editor - @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + vg + svg + api + libman + editor" + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-vg lib-svg lib-jpg lib-api lib-libman lib-editor + @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + vg + svg + jpg + api + libman + editor" category-libs: static $(foreach l,$(CAT_LIBS),lib-$(l)) ## Compila as libraries de categoria (.a/.so) @echo "Build category libs OK" @@ -264,7 +268,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 image vg svg +APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image vg svg jpg APP_NAME := TinyBlocks APP_VER := 0.1.0 @@ -318,6 +322,12 @@ appdir: editor ## Monta o AppDir (build/TinyBlocks.AppDir) para o AppImage mkdir -p "$(APPDIR)/usr/share/tinyblocks/libraries/tinyblocks-$$l/build"; \ cp "$$d/library.toml" "$(APPDIR)/usr/share/tinyblocks/libraries/tinyblocks-$$l/"; \ cp "$$so" "$(APPDIR)/usr/share/tinyblocks/libraries/tinyblocks-$$l/build/"; \ + ldd "$$so" 2>/dev/null | awk '/=> \//{print $$3}' | while read dep; do \ + dn=$$(basename "$$dep"); \ + skip=0; \ + for ex in $(APPIMAGE_EXCLUDE); do case "$$dn" in $$ex*) skip=1;; esac; done; \ + [ $$skip -eq 0 ] && cp -L "$$dep" $(APPDIR)/usr/lib/ 2>/dev/null || true; \ + done; \ done @echo "AppDir pronto: $(APPDIR)" @@ -541,6 +551,23 @@ $(BUILD)/svg_test: tests/svg_test.c $(SVG_DIR)/src/tb_svg.c $(VG_DIR)/src/tb_vg. test-svg: $(BUILD)/svg_test ## Roda o teste da svg (ASan/UBSan) ./$(BUILD)/svg_test +# -- Teste da library jpg (libjpeg; pulado se os headers faltarem) --- +JPG_TEST_AVAILABLE := $(shell printf '#include \nint main(void){return 0;}\n' | \ + $(CC) -xc - -ljpeg -o /dev/null >/dev/null 2>&1 && echo yes) +ifeq ($(JPG_TEST_AVAILABLE),yes) +$(BUILD)/jpg_test: tests/jpg_test.c $(JPG_DIR)/src/tb_jpg.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(JPG_DIR)/include $(JPG_DIR)/src/tb_jpg.c \ + tests/jpg_test.c -o $@ \ + -Wl,--start-group $(LIB_A) -Wl,--end-group -ljpeg + +test-jpg: $(BUILD)/jpg_test ## Roda o teste da jpg (ASan/UBSan) + ./$(BUILD)/jpg_test +else +test-jpg: ## (libjpeg-dev ausente: teste ignorado) + @echo "test-jpg: /libjpeg-dev ausente - pulando" +endif + # -- Teste de plugin .so standalone --- FS_SO := $(FS_DIR)/build/libtinyblocks-fs.so @@ -557,7 +584,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) $(IMG_DIR) $(VG_DIR) $(SVG_DIR) \ + $(B64_DIR) $(CV_DIR) $(PPM_DIR) $(BMP_DIR) $(PNG_DIR) $(IMG_DIR) $(VG_DIR) $(SVG_DIR) $(JPG_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/libraries/tinyblocks-jpg/Makefile b/libraries/tinyblocks-jpg/Makefile new file mode 100644 index 0000000..79e4ac2 --- /dev/null +++ b/libraries/tinyblocks-jpg/Makefile @@ -0,0 +1,43 @@ +# Makefile da library jpg (categoria "JPG"); depende de libjpeg. +# Como os headers de desenvolvimento podem nao estar instalados, o build e' +# condicional: sem a library e' apenas ignorada. +CC := gcc +BUILD := build + +JPEG_CFLAGS := $(shell pkg-config --cflags libjpeg 2>/dev/null) +JPEG_LIBS := $(shell pkg-config --libs libjpeg 2>/dev/null) +JPEG_HAVE := $(shell printf '\#include \nint main(void){return 0;}\n' | \ + $(CC) -xc - -o /dev/null $(JPEG_CFLAGS) $(JPEG_LIBS) -ljpeg >/dev/null 2>&1 && echo yes) + +CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ + -D_POSIX_C_SOURCE=200809L -I../../include -Iinclude $(JPEG_CFLAGS) + +OBJ := $(BUILD)/tb_jpg.o +LIB_A := $(BUILD)/libtinyblocks-jpg.a +LIB_SO := $(BUILD)/libtinyblocks-jpg.so + +ifeq ($(JPEG_HAVE),yes) +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(OBJ): src/tb_jpg.c include/tb_jpg.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJ) + ar rcs $@ $^ + +$(LIB_SO): $(OBJ) + $(CC) -shared -o $@ $^ $(JPEG_LIBS) -ljpeg + +clean: + $(RM) -r $(BUILD) +else +.PHONY: all static shared clean +all static shared: + @echo "tinyblocks-jpg: ausente (instale libjpeg-dev/libjpeg-turbo8-dev) - pulando" +clean: + $(RM) -r $(BUILD) +endif diff --git a/libraries/tinyblocks-jpg/include/tb_jpg.h b/libraries/tinyblocks-jpg/include/tb_jpg.h new file mode 100644 index 0000000..ccbfb2f --- /dev/null +++ b/libraries/tinyblocks-jpg/include/tb_jpg.h @@ -0,0 +1,17 @@ +/* + * TinyBlocks - tb_jpg.h + * Library tinyblocks-jpg: carrega/grava JPEG (dependencia de terceiros: + * libjpeg). As imagens entram/saem como TinyBlocks_Image (TB_VALUE_IMAGE). + */ +#ifndef TINYBLOCKS_JPG_H +#define TINYBLOCKS_JPG_H + +#include + +TB_API const TinyBlocks_LibraryDescriptor* tb_jpg_get_library(void); + +TB_API TinyBlocks_Image* tb_jpg_load(const char* path); +TB_API bool tb_jpg_save(const TinyBlocks_Image* img, const char* path, int quality); +TB_API const char* tb_jpg_last_error(void); + +#endif /* TINYBLOCKS_JPG_H */ diff --git a/libraries/tinyblocks-jpg/src/tb_jpg.c b/libraries/tinyblocks-jpg/src/tb_jpg.c new file mode 100644 index 0000000..f940a87 --- /dev/null +++ b/libraries/tinyblocks-jpg/src/tb_jpg.c @@ -0,0 +1,197 @@ +/* + * TinyBlocks - tb_jpg.c + * Categoria "JPG": JPG.Load / JPG.Save (libjpeg). Decodifica para RGB e + * codifica a partir de RGB. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include + +#include + +#include +#include "tb_jpg.h" + +static char s_err[200] = ""; +const char* tb_jpg_last_error(void) { return s_err; } + +typedef struct { + struct jpeg_error_mgr pub; + jmp_buf jb; +} JpgErr; + +static void jpg_error_exit(j_common_ptr cinfo) { + JpgErr* e = (JpgErr*)cinfo->err; + char msg[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, msg); + snprintf(s_err, sizeof(s_err), "%s", msg); + longjmp(e->jb, 1); +} + +TinyBlocks_Image* tb_jpg_load(const char* path) { + s_err[0] = 0; + if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; } + FILE* f = fopen(path, "rb"); + if (!f) { snprintf(s_err, sizeof(s_err), "nao abriu '%s'", path); return NULL; } + + struct jpeg_decompress_struct cinfo; + JpgErr jerr; + cinfo.err = jpeg_std_error(&jerr.pub); + jerr.pub.error_exit = jpg_error_exit; + + TinyBlocks_Image* img = NULL; + if (setjmp(jerr.jb)) { + jpeg_destroy_decompress(&cinfo); + if (img) tb_image_release(img); + fclose(f); + return NULL; + } + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo, f); + jpeg_read_header(&cinfo, TRUE); + cinfo.out_color_space = JCS_RGB; + jpeg_start_decompress(&cinfo); + int w = (int)cinfo.output_width, h = (int)cinfo.output_height; + img = tb_image_new(w, h, 3); + if (!img) { snprintf(s_err, sizeof(s_err), "sem memoria"); jpeg_destroy_decompress(&cinfo); fclose(f); return NULL; } + uint8_t* px = tb_image_data(img); + int comps = cinfo.output_components; + JSAMPARRAY row = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, + (JDIMENSION)w * (JDIMENSION)comps, 1); + while (cinfo.output_scanline < cinfo.output_height) { + JDIMENSION y = cinfo.output_scanline; + jpeg_read_scanlines(&cinfo, row, 1); + if (comps == 3) memcpy(px + (size_t)y * w * 3, row[0], (size_t)w * 3); + else for (int x = 0; x < w; x++) { + uint8_t v = row[0][x]; + px[((size_t)y*w + x)*3 + 0] = v; + px[((size_t)y*w + x)*3 + 1] = v; + px[((size_t)y*w + x)*3 + 2] = v; + } + } + jpeg_finish_decompress(&cinfo); + jpeg_destroy_decompress(&cinfo); + fclose(f); + return img; +} + +bool tb_jpg_save(const TinyBlocks_Image* img, const char* path, int quality) { + s_err[0] = 0; + if (!img || !path) { snprintf(s_err, sizeof(s_err), "imagem/caminho nulo"); return false; } + int w = tb_image_width(img), h = tb_image_height(img); + if (w <= 0 || h <= 0) { snprintf(s_err, sizeof(s_err), "imagem vazia"); return false; } + if (quality < 1) quality = 1; + if (quality > 100) quality = 100; + + FILE* f = fopen(path, "wb"); + if (!f) { snprintf(s_err, sizeof(s_err), "nao criou '%s'", path); return false; } + + struct jpeg_compress_struct cinfo; + JpgErr jerr; + cinfo.err = jpeg_std_error(&jerr.pub); + jerr.pub.error_exit = jpg_error_exit; + + uint8_t* rowbuf = (uint8_t*)malloc((size_t)w * 3); + if (!rowbuf) { fclose(f); snprintf(s_err, sizeof(s_err), "sem memoria"); return false; } + + bool ok = true; + if (setjmp(jerr.jb)) { + jpeg_destroy_compress(&cinfo); + free(rowbuf); fclose(f); + return false; + } + jpeg_create_compress(&cinfo); + jpeg_stdio_dest(&cinfo, f); + cinfo.image_width = (JDIMENSION)w; + cinfo.image_height = (JDIMENSION)h; + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, quality, TRUE); + jpeg_start_compress(&cinfo, TRUE); + while (cinfo.next_scanline < cinfo.image_height) { + int y = (int)cinfo.next_scanline; + for (int x = 0; x < w; x++) { + uint8_t r, g, b, a; + tb_image_get_pixel(img, x, y, &r, &g, &b, &a); + rowbuf[x*3+0] = r; rowbuf[x*3+1] = g; rowbuf[x*3+2] = b; + } + JSAMPROW rp = rowbuf; + jpeg_write_scanlines(&cinfo, &rp, 1); + } + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + free(rowbuf); + fclose(f); + return ok; +} + +/* -- Blocos -------------------------------------------------------------- */ + +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; + return 0; +} +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 void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { + TinyBlocks_Image* img = tb_jpg_load(in_text(self, rt, 0)); + if (!img) { tinyblocks_set_error(rt, "%s", tb_jpg_last_error()); self->dirty = false; return; } + tb_block_set_output(self, 0, tb_value_image(img)); + tb_image_release(img); + self->dirty = false; +} +static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { + TinyBlocks_Image* img = in_img(self, rt, 0); + if (!img || !tb_jpg_save(img, in_text(self, rt, 1), (int)in_real(self, rt, 2))) + tinyblocks_set_error(rt, "%s", tb_jpg_last_error()); + self->dirty = false; +} + +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 jpg_encode_default(const TinyBlocks_Image* img, const char* path) { + return tb_jpg_save(img, path, 90); +} + +static bool jpg_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { + static const TinyBlocks_Category cat = { + "JPG", + { 239/255.f, 83/255.f, 80/255.f, 1.f}, + { 198/255.f, 40/255.f, 40/255.f, 1.f}, + }; + tb_library_set_category(lib, &cat); + + static const TinyBlocks_SlotDesc out_img[] = {{"imagem", TB_VALUE_IMAGE}}; + static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc save_in[] = {{"imagem", TB_VALUE_IMAGE},{"caminho", TB_VALUE_TEXT},{"qualidade", TB_VALUE_REAL}}; + + bool ok = reg(rt, lib, "JPG.Load", load_in, 1, out_img, 1, eval_load) + && reg(rt, lib, "JPG.Save", save_in, 3, NULL, 0, eval_save); + if (ok) tb_image_register_format("jpg", tb_jpg_load, jpg_encode_default); + return ok; +} + +TINYBLOCKS_DEFINE_LIBRARY(jpg, "tinyblocks-jpg", "1.0.0", jpg_init, NULL) + +const TinyBlocks_LibraryDescriptor* tb_jpg_get_library(void) { + return &_tb_lib_desc_jpg; +} diff --git a/libraries/tinyblocks-svg/Makefile b/libraries/tinyblocks-svg/Makefile index 90ceec8..d6107ce 100644 --- a/libraries/tinyblocks-svg/Makefile +++ b/libraries/tinyblocks-svg/Makefile @@ -28,7 +28,7 @@ $(LIB_A): $(OBJ) $(LIB_SO): $(OBJ) $(VG_LIB)/libtinyblocks-vg.so $(B64_LIB)/libtinyblocks-base64.so $(CC) -shared -o $@ $< -L$(VG_LIB) -ltinyblocks-vg \ -L$(B64_LIB) -ltinyblocks-base64 \ - -Wl,-rpath,'$$ORIGIN' -lm + -Wl,-rpath,'$$ORIGIN/../../tinyblocks-vg/build:$$ORIGIN/../../tinyblocks-base64/build' -lm clean: $(RM) -r $(BUILD) diff --git a/tests/jpg_test.c b/tests/jpg_test.c new file mode 100644 index 0000000..fad325e --- /dev/null +++ b/tests/jpg_test.c @@ -0,0 +1,46 @@ +/* + * jpg_test.c - testa tinyblocks-jpg (libjpeg): round-trip com perda e blocos. + */ +#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) + +int main(void) { + TinyBlocks_Image* img = tb_image_new(16, 16, 3); + for (int y = 0; y < 16; y++) + for (int x = 0; x < 16; x++) + tb_image_set_pixel(img, x, y, x < 8 ? 220 : 20, 20, x < 8 ? 20 : 220, 255); + CHECK(tb_jpg_save(img, "build/jpg_test_out.jpg", 90), "tb_jpg_save grava JPEG"); + + TinyBlocks_Image* rd = tb_jpg_load("build/jpg_test_out.jpg"); + CHECK(rd && tb_image_width(rd) == 16 && tb_image_height(rd) == 16, "tb_jpg_load le 16x16"); + if (rd) { + uint8_t r, g, b, a; + tb_image_get_pixel(rd, 2, 8, &r, &g, &b, &a); + CHECK(r > 150 && g < 90 && b < 90, "metade esquerda avermelhada"); + tb_image_get_pixel(rd, 13, 8, &r, &g, &b, &a); + CHECK(b > 150 && r < 90, "metade direita azulada"); + } + remove("build/jpg_test_out.jpg"); + if (rd) tb_image_release(rd); + tb_image_release(img); + + CHECK(tb_jpg_load("build/nao_existe.jpg") == NULL, "load de ausente -> NULL"); + CHECK(strlen(tb_jpg_last_error()) > 0, "mensagem de erro presente"); + + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_jpg_get_library()); + CHECK(tb_rt_find_type(rt, "JPG.Load") && tb_rt_find_type(rt, "JPG.Save"), + "blocos JPG.Load/Save registrados"); + tinyblocks_deinit(&rt); + + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +}