Adiciona tinyblocks-bmp e tinyblocks-png (libpng)

This commit is contained in:
mguinhos committed 2026-10-20 21:05:47 -03:00
1 parent 959a4027c3
commit fca7aa2f63
11 files changed
+574 -6

No files matched your search

+36 -6
View File
@@ -27,6 +27,8 @@ BP_DIR := $(LIBDIR)/tinyblocks-blueprint
B64_DIR := $(LIBDIR)/tinyblocks-base64
CV_DIR := $(LIBDIR)/tinyblocks-curves
PPM_DIR := $(LIBDIR)/tinyblocks-ppm
BMP_DIR := $(LIBDIR)/tinyblocks-bmp
PNG_DIR := $(LIBDIR)/tinyblocks-png
API_DIR := $(LIBDIR)/tinyblocks-api
LIBM_DIR := $(LIBDIR)/tinyblocks-libman
ED_DIR := $(LIBDIR)/tinyblocks-editor
@@ -82,12 +84,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-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png \
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-api lib-libman \
lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-api lib-libman \
test-libman test-environ
all: static example ## Compila a biblioteca estática e o exemplo hello (padrão)
@@ -172,6 +174,12 @@ lib-curves:
lib-ppm:
$(MAKE) -C $(PPM_DIR)
lib-bmp:
$(MAKE) -C $(BMP_DIR)
lib-png:
$(MAKE) -C $(PNG_DIR)
lib-api:
$(MAKE) -C $(API_DIR)
@@ -192,8 +200,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-api lib-libman lib-editor
@echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + api + libman + editor"
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"
category-libs: static $(foreach l,$(CAT_LIBS),lib-$(l)) ## Compila as libraries de categoria (.a/.so)
@echo "Build category libs OK"
@@ -244,7 +252,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
APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png
APP_NAME := TinyBlocks
APP_VER := 0.1.0
@@ -467,6 +475,28 @@ $(BUILD)/ppm_test: tests/ppm_test.c $(PPM_DIR)/src/tb_ppm.c static
test-ppm: $(BUILD)/ppm_test ## Roda o teste da ppm (ASan/UBSan)
./$(BUILD)/ppm_test
# -- Teste da library bmp ---
$(BUILD)/bmp_test: tests/bmp_test.c $(BMP_DIR)/src/tb_bmp.c static
@mkdir -p $(BUILD)
$(CC) $(CFLAGS_DBG) -I$(BMP_DIR)/include $(BMP_DIR)/src/tb_bmp.c \
tests/bmp_test.c -o $@ \
-Wl,--start-group $(LIB_A) -Wl,--end-group
test-bmp: $(BUILD)/bmp_test ## Roda o teste da bmp (ASan/UBSan)
./$(BUILD)/bmp_test
# -- Teste da library png (libpng) ---
PNG_CFLAGS := $(shell pkg-config --cflags libpng 2>/dev/null)
PNG_LIBS := $(shell pkg-config --libs libpng 2>/dev/null)
$(BUILD)/png_test: tests/png_test.c $(PNG_DIR)/src/tb_png.c static
@mkdir -p $(BUILD)
$(CC) $(CFLAGS_DBG) $(PNG_CFLAGS) -I$(PNG_DIR)/include $(PNG_DIR)/src/tb_png.c \
tests/png_test.c -o $@ \
-Wl,--start-group $(LIB_A) -Wl,--end-group $(PNG_LIBS)
test-png: $(BUILD)/png_test ## Roda o teste da png (ASan/UBSan)
./$(BUILD)/png_test
# -- Teste de plugin .so standalone ---
FS_SO := $(FS_DIR)/build/libtinyblocks-fs.so
@@ -483,7 +513,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) \
$(B64_DIR) $(CV_DIR) $(PPM_DIR) $(BMP_DIR) $(PNG_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; \
+28
View File
@@ -0,0 +1,28 @@
# Makefile da library bmp (categoria "BMP")
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
OBJ := $(BUILD)/tb_bmp.o
LIB_A := $(BUILD)/libtinyblocks-bmp.a
LIB_SO := $(BUILD)/libtinyblocks-bmp.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_bmp.c include/tb_bmp.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^
clean:
$(RM) -r $(BUILD)
+17
View File
@@ -0,0 +1,17 @@
/*
* TinyBlocks - tb_bmp.h
* Library tinyblocks-bmp: carrega/grava bitmap BMP (BITMAPINFOHEADER, BI_RGB)
* de 24 e 32 bits como TinyBlocks_Image (TB_VALUE_IMAGE).
*/
#ifndef TINYBLOCKS_BMP_H
#define TINYBLOCKS_BMP_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_bmp_get_library(void);
TB_API TinyBlocks_Image* tb_bmp_load(const char* path);
TB_API bool tb_bmp_save(const TinyBlocks_Image* img, const char* path);
TB_API const char* tb_bmp_last_error(void);
#endif /* TINYBLOCKS_BMP_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library bmp. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-bmp"
version = "1.0.0"
category = "BMP"
description = "Imagens BMP: carregar e gravar (24/32 bits)"
color = [126, 87, 194, 255]
dark = [81, 45, 168, 255]
[build]
sources = ["src/tb_bmp.c"]
includes = ["include"]
shared = "build/libtinyblocks-bmp.so"
static = "build/libtinyblocks-bmp.a"
depends = []
+178
View File
@@ -0,0 +1,178 @@
/*
* TinyBlocks - tb_bmp.c
* Categoria "BMP": BMP.Load / BMP.Save. Le e grava BMP BI_RGB 24/32 bits.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_bmp.h"
static char s_err[200] = "";
const char* tb_bmp_last_error(void) { return s_err; }
static uint16_t rd16(const uint8_t* p) { return (uint16_t)(p[0] | (p[1] << 8)); }
static uint32_t rd32(const uint8_t* p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
static void wr16(FILE* f, uint16_t v) { fputc(v & 0xff, f); fputc((v >> 8) & 0xff, f); }
static void wr32(FILE* f, uint32_t v) {
fputc(v & 0xff, f); fputc((v >> 8) & 0xff, f);
fputc((v >> 16) & 0xff, f); fputc((v >> 24) & 0xff, f);
}
TinyBlocks_Image* tb_bmp_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; }
uint8_t hdr[54];
if (fread(hdr, 1, 54, f) != 54 || hdr[0] != 'B' || hdr[1] != 'M') {
snprintf(s_err, sizeof(s_err), "nao e um BMP");
fclose(f); return NULL;
}
uint32_t data_off = rd32(hdr + 10);
uint32_t dib_size = rd32(hdr + 14);
int32_t w = (int32_t)rd32(hdr + 18);
int32_t h = (int32_t)rd32(hdr + 22);
uint16_t bpp = rd16(hdr + 28);
uint32_t comp = rd32(hdr + 30);
if (dib_size < 40 || w <= 0 || h == 0) {
snprintf(s_err, sizeof(s_err), "cabecalho BMP nao suportado");
fclose(f); return NULL;
}
if (bpp != 24 && bpp != 32) {
snprintf(s_err, sizeof(s_err), "somente BMP 24/32 bits (bpp=%u)", bpp);
fclose(f); return NULL;
}
if (comp != 0 && !(comp == 3 && bpp == 32)) {
snprintf(s_err, sizeof(s_err), "compressao BMP nao suportada (%u)", comp);
fclose(f); return NULL;
}
int topdown = h < 0;
int ih = topdown ? -h : h;
TinyBlocks_Image* img = tb_image_new(w, ih, 4);
if (!img) { snprintf(s_err, sizeof(s_err), "sem memoria"); fclose(f); return NULL; }
size_t stride = ((size_t)w * bpp / 8 + 3) / 4 * 4;
uint8_t* row = (uint8_t*)malloc(stride);
if (!row) { tb_image_release(img); snprintf(s_err, sizeof(s_err), "sem memoria"); fclose(f); return NULL; }
fseek(f, (long)data_off, SEEK_SET);
for (int y = 0; y < ih; y++) {
if (fread(row, 1, stride, f) != stride) {
snprintf(s_err, sizeof(s_err), "dados truncados");
free(row); tb_image_release(img); fclose(f); return NULL;
}
int dy = topdown ? y : (ih - 1 - y);
for (int x = 0; x < w; x++) {
const uint8_t* p = row + (size_t)x * (bpp / 8);
uint8_t a = (bpp == 32) ? p[3] : 255;
tb_image_set_pixel(img, x, dy, p[2], p[1], p[0], a);
}
}
free(row);
fclose(f);
return img;
}
bool tb_bmp_save(const TinyBlocks_Image* img, const char* path) {
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; }
size_t stride = ((size_t)w * 3 + 3) / 4 * 4;
uint32_t data_size = (uint32_t)(stride * (size_t)h);
FILE* f = fopen(path, "wb");
if (!f) { snprintf(s_err, sizeof(s_err), "nao criou '%s'", path); return false; }
/* FILEHEADER (14) */
fputc('B', f); fputc('M', f);
wr32(f, 14 + 40 + data_size);
wr16(f, 0); wr16(f, 0);
wr32(f, 14 + 40);
/* BITMAPINFOHEADER (40) */
wr32(f, 40);
wr32(f, (uint32_t)w);
wr32(f, (uint32_t)h);
wr16(f, 1);
wr16(f, 24);
wr32(f, 0); /* BI_RGB */
wr32(f, data_size);
wr32(f, 2835); wr32(f, 2835);
wr32(f, 0); wr32(f, 0);
uint8_t pad[3] = {0, 0, 0};
for (int y = h - 1; y >= 0; y--) { /* BMP e' bottom-up */
for (int x = 0; x < w; x++) {
uint8_t r, g, b, a;
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
fputc(b, f); fputc(g, f); fputc(r, f);
}
size_t wrote = (size_t)w * 3;
if (wrote < stride) fwrite(pad, 1, stride - wrote, f);
}
bool ok = (ferror(f) == 0);
fclose(f);
if (!ok) snprintf(s_err, sizeof(s_err), "erro ao gravar");
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 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_bmp_load(in_text(self, rt, 0));
if (!img) { tinyblocks_set_error(rt, "%s", tb_bmp_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_bmp_save(img, in_text(self, rt, 1)))
tinyblocks_set_error(rt, "%s", tb_bmp_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 bmp_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"BMP",
{ 126/255.f, 87/255.f, 194/255.f, 1.f},
{ 81/255.f, 45/255.f, 168/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}};
return 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);
}
TINYBLOCKS_DEFINE_LIBRARY(bmp, "tinyblocks-bmp", "1.0.0", bmp_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_bmp_get_library(void) {
return &_tb_lib_desc_bmp;
}
+31
View File
@@ -0,0 +1,31 @@
# Makefile da library png (categoria "PNG"); depende de libpng.
CC := gcc
BUILD := build
PNG_CFLAGS := $(shell pkg-config --cflags libpng 2>/dev/null)
PNG_LIBS := $(shell pkg-config --libs libpng 2>/dev/null)
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude $(PNG_CFLAGS)
OBJ := $(BUILD)/tb_png.o
LIB_A := $(BUILD)/libtinyblocks-png.a
LIB_SO := $(BUILD)/libtinyblocks-png.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_png.c include/tb_png.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^ $(PNG_LIBS)
clean:
$(RM) -r $(BUILD)
+17
View File
@@ -0,0 +1,17 @@
/*
* TinyBlocks - tb_png.h
* Library tinyblocks-png: carrega/grava PNG (dependencia de terceiros: libpng)
* como TinyBlocks_Image (TB_VALUE_IMAGE, RGBA).
*/
#ifndef TINYBLOCKS_PNG_H
#define TINYBLOCKS_PNG_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_png_get_library(void);
TB_API TinyBlocks_Image* tb_png_load(const char* path);
TB_API bool tb_png_save(const TinyBlocks_Image* img, const char* path);
TB_API const char* tb_png_last_error(void);
#endif /* TINYBLOCKS_PNG_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library png. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-png"
version = "1.0.0"
category = "PNG"
description = "Imagens PNG: carregar e gravar (libpng)"
color = [0, 172, 193, 255]
dark = [0, 131, 143, 255]
[build]
sources = ["src/tb_png.c"]
includes = ["include"]
shared = "build/libtinyblocks-png.so"
static = "build/libtinyblocks-png.a"
depends = ["libpng"]
+129
View File
@@ -0,0 +1,129 @@
/*
* TinyBlocks - tb_png.c
* Categoria "PNG": PNG.Load / PNG.Save (libpng, API simplificada png_image).
* As imagens sao sempre convertidas para RGBA em memoria, de modo que a
* previa (balao) do editor funciona independente do formato de origem.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <png.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_png.h"
static char s_err[200] = "";
const char* tb_png_last_error(void) { return s_err; }
TinyBlocks_Image* tb_png_load(const char* path) {
s_err[0] = 0;
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
png_image pv;
memset(&pv, 0, sizeof(pv));
pv.version = PNG_IMAGE_VERSION;
if (!png_image_begin_read_from_file(&pv, path)) {
snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
return NULL;
}
pv.format = PNG_FORMAT_RGBA;
size_t sz = PNG_IMAGE_SIZE(pv);
uint8_t* buf = (uint8_t*)malloc(sz ? sz : 1);
if (!buf) { png_image_free(&pv); snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
if (!png_image_finish_read(&pv, NULL, buf, 0, NULL)) {
snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
free(buf); png_image_free(&pv); return NULL;
}
int w = (int)pv.width, h = (int)pv.height;
png_image_free(&pv);
TinyBlocks_Image* img = tb_image_new(w, h, 4);
if (!img) { free(buf); snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
memcpy(tb_image_data(img), buf, (size_t)w * h * 4);
free(buf);
return img;
}
bool tb_png_save(const TinyBlocks_Image* img, const char* path) {
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; }
size_t sz = (size_t)w * h * 4;
uint8_t* buf = (uint8_t*)malloc(sz);
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return false; }
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
uint8_t r, g, b, a;
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
uint8_t* p = buf + ((size_t)y * w + x) * 4;
p[0] = r; p[1] = g; p[2] = b; p[3] = a;
}
png_image pv;
memset(&pv, 0, sizeof(pv));
pv.version = PNG_IMAGE_VERSION;
pv.width = (png_uint_32)w;
pv.height = (png_uint_32)h;
pv.format = PNG_FORMAT_RGBA;
bool ok = png_image_write_to_file(&pv, path, 0, buf, 0, NULL) != 0;
if (!ok) snprintf(s_err, sizeof(s_err), "libpng: %s", pv.message);
free(buf);
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 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_png_load(in_text(self, rt, 0));
if (!img) { tinyblocks_set_error(rt, "%s", tb_png_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_png_save(img, in_text(self, rt, 1)))
tinyblocks_set_error(rt, "%s", tb_png_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 png_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"PNG",
{ 0/255.f, 172/255.f, 193/255.f, 1.f},
{ 0/255.f, 131/255.f, 143/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}};
return 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);
}
TINYBLOCKS_DEFINE_LIBRARY(png, "tinyblocks-png", "1.0.0", png_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_png_get_library(void) {
return &_tb_lib_desc_png;
}
+62
View File
@@ -0,0 +1,62 @@
/*
* bmp_test.c - testa tinyblocks-bmp: round-trip de gravacao/leitura BMP 24 bits
* e os blocos BMP.Load / BMP.Save.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_bmp.h>
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_txt(TinyBlocks_Block* b, uint32_t i, const char* s) {
TinyBlocks_Value vv = tb_value_text(s);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
int main(void) {
TinyBlocks_Image* img = tb_image_new(3, 2, 3);
tb_image_set_pixel(img, 0, 0, 255, 0, 0, 255);
tb_image_set_pixel(img, 2, 0, 0, 0, 255, 255);
tb_image_set_pixel(img, 1, 1, 0, 255, 0, 255);
tb_image_set_pixel(img, 2, 1, 255, 255, 255, 255);
CHECK(tb_bmp_save(img, "build/bmp_test_out.bmp"), "tb_bmp_save grava");
TinyBlocks_Image* rd = tb_bmp_load("build/bmp_test_out.bmp");
CHECK(rd && tb_image_width(rd) == 3 && tb_image_height(rd) == 2, "tb_bmp_load le 3x2");
if (rd) {
uint8_t r, g, b, a;
tb_image_get_pixel(rd, 2, 1, &r, &g, &b, &a);
CHECK(r == 255 && g == 255 && b == 255, "pixel branco preservado");
tb_image_get_pixel(rd, 0, 0, &r, &g, &b, &a);
CHECK(r == 255 && g == 0 && b == 0, "pixel vermelho preservado");
}
remove("build/bmp_test_out.bmp");
if (rd) tb_image_release(rd);
tb_image_release(img);
CHECK(tb_bmp_load("build/nao_existe.bmp") == NULL, "load de ausente -> NULL");
CHECK(strlen(tb_bmp_last_error()) > 0, "mensagem de erro presente");
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_bmp_get_library());
CHECK(tb_rt_find_type(rt, "BMP.Load") && tb_rt_find_type(rt, "BMP.Save"),
"blocos BMP.Load/Save registrados");
TinyBlocks_Block* bl = tb_block_create(rt, tb_rt_find_type(rt, "BMP.Load"));
set_txt(bl, 0, "build/nao_existe.bmp");
tb_block_eval(bl, rt);
CHECK(bl->outputs[0].kind == TB_VALUE_NONE, "BMP.Load invalido nao devolve imagem");
tb_block_destroy(bl, rt);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}
+46
View File
@@ -0,0 +1,46 @@
/*
* png_test.c - testa tinyblocks-png (libpng): round-trip RGBA e os blocos.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_png.h>
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(4, 4, 4);
tb_image_set_pixel(img, 0, 0, 255, 0, 0, 255);
tb_image_set_pixel(img, 3, 3, 0, 128, 255, 255);
tb_image_set_pixel(img, 2, 1, 0, 255, 0, 128); /* com alpha */
CHECK(tb_png_save(img, "build/png_test_out.png"), "tb_png_save grava PNG");
TinyBlocks_Image* rd = tb_png_load("build/png_test_out.png");
CHECK(rd && tb_image_width(rd) == 4 && tb_image_height(rd) == 4, "tb_png_load le 4x4");
if (rd) {
uint8_t r, g, b, a;
tb_image_get_pixel(rd, 3, 3, &r, &g, &b, &a);
CHECK(r == 0 && g == 128 && b == 255 && a == 255, "pixel azul preservado");
tb_image_get_pixel(rd, 2, 1, &r, &g, &b, &a);
CHECK(r == 0 && g == 255 && b == 0 && a == 128, "canal alpha preservado");
}
remove("build/png_test_out.png");
if (rd) tb_image_release(rd);
tb_image_release(img);
CHECK(tb_png_load("build/nao_existe.png") == NULL, "load de ausente -> NULL");
CHECK(strlen(tb_png_last_error()) > 0, "mensagem de erro presente");
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_png_get_library());
CHECK(tb_rt_find_type(rt, "PNG.Load") && tb_rt_find_type(rt, "PNG.Save"),
"blocos PNG.Load/Save registrados");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}