/* * bmp_test.c - testa tinyblocks-bmp: round-trip de gravacao/leitura BMP 24 bits * e os blocos BMP.Load / BMP.Save. */ #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_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; }