/* * 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 #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); /* -- Conversoes: lista de pixels, tons de cinza, desenho em texto -- */ { TinyBlocks* rt2; tinyblocks_init(&rt2, NULL); tb_library_register(rt2, tb_imglib_get_library()); TinyBlocks_Block* fl = tb_block_create(rt2, tb_rt_find_type(rt2, "Image.FromList")); TinyBlocks_Value w = tb_value_integer(2); TinyBlocks_Value px = tb_value_text("[[255,0,0],[0,0,255,128],0,255]"); tb_block_set_default(fl, 0, w); tb_block_set_default(fl, 1, px); tb_value_free(&px); tb_block_eval(fl, rt2); TinyBlocks_Image* im = fl->outputs[0].kind == TB_VALUE_IMAGE ? fl->outputs[0].as.image : NULL; uint8_t r2 = 0, g2 = 0, b2 = 0, a2 = 0; if (im) tb_image_get_pixel(im, 1, 0, &r2, &g2, &b2, &a2); CHECK(im && tb_image_width(im) == 2 && tb_image_height(im) == 2 && b2 == 255 && a2 == 128, "imagem de largura 2 com 4 pixels (rgb, rgba e cinza)"); TinyBlocks_Block* tl = tb_block_create(rt2, tb_rt_find_type(rt2, "Image.ToList")); tb_block_connect(tl, 0, fl, 0); tb_block_eval(tl, rt2); char* js = tl->outputs[0].kind == TB_VALUE_JSON ? tb_json_stringify(tl->outputs[0].as.json) : NULL; CHECK(js && !strcmp(js, "[[255,0,0,255],[0,0,255,128],[0,0,0,255],[255,255,255,255]]"), "pixels da imagem voltam como lista [r,g,b,a]"); free(js); TinyBlocks_Block* gr = tb_block_create(rt2, tb_rt_find_type(rt2, "Image.Gray")); tb_block_connect(gr, 0, fl, 0); tb_block_eval(gr, rt2); TinyBlocks_Image* gi = gr->outputs[0].kind == TB_VALUE_IMAGE ? gr->outputs[0].as.image : NULL; if (gi) tb_image_get_pixel(gi, 0, 0, &r2, &g2, &b2, &a2); CHECK(gi && r2 == 76 && g2 == 76 && b2 == 76, "tons de cinza (vermelho puro = 76)"); TinyBlocks_Block* tt = tb_block_create(rt2, tb_rt_find_type(rt2, "Image.ToText")); TinyBlocks_Value cols = tb_value_integer(2); tb_block_connect(tt, 0, fl, 0); tb_block_set_default(tt, 1, cols); tb_block_eval(tt, rt2); const char* art = tt->outputs[0].kind == TB_VALUE_TEXT ? tt->outputs[0].as.text : ""; CHECK(art[0] && art[0] != ' ' && strchr(art, '\n'), "desenho em texto da imagem"); printf(" desenho: [%s]\n", art); tb_block_destroy(tt, rt2); tb_block_destroy(gr, rt2); tb_block_destroy(tl, rt2); tb_block_destroy(fl, rt2); tinyblocks_deinit(&rt2); } /* -- Palco retem a imagem (regressao: uso-apos-liberacao ao parar) --- * O bloco que produz a imagem (ex.: "3D.Image") e destruido no "parar", * mas a lista retida do palco continua apontando para ela. Sem o retain * no palco, isto era um use-after-free (crash no Windows). */ { TinyBlocks_Image* si = tb_image_new(4, 4, 4); tb_imglib_fill(si, 1, 2, 3, 255); CHECK(tb_stage_image(si, 0.f, 0.f), "palco aceita a imagem"); tb_image_release(si); /* agora so o palco segura */ const uint8_t* px = tb_image_data_const(si); CHECK(px && px[20] == 1 && px[21] == 2 && px[22] == 3, "imagem segue valida depois que o palco a retem"); tb_stage_clear(); /* o palco solta a referencia */ } if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }