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,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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tb_imglib.h>
|
||||
#include <tb_ppm.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_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;
|
||||
}
|
||||
Reference in new issue
Block a user