/* * vg_test.c - testa tinyblocks-vg: construcao de desenho vetorial, rasterizacao * e os blocos VG.*. */ #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_vec(TinyBlocks_Block* b, uint32_t i, TinyBlocks_VectorData* v) { TinyBlocks_Value vv = tb_value_vector(v); tb_block_set_default(b, i, vv); tb_value_free(&vv); } int main(void) { /* -- Rasterizacao direta --- */ TinyBlocks_VectorData* v = tb_vg_new(); CHECK(v != NULL, "tb_vg_new cria vetor"); tb_vg_rect(v, 4, 4, 8, 8, 1, 0, 0, 1); /* retangulo vermelho */ tb_vg_circle(v, 8, 16, 2, 0, 0, 1, 1); /* circulo azul */ TinyBlocks_Image* im = tb_vg_render(v, 20, 20, 1); CHECK(im && tb_image_width(im) == 20 && tb_image_height(im) == 20, "tb_vg_render 20x20"); uint8_t r, g, b, a; if (im) { tb_image_get_pixel(im, 8, 8, &r, &g, &b, &a); CHECK(r > 200 && g < 60 && b < 60 && a > 200, "retangulo pintado de vermelho"); tb_image_get_pixel(im, 8, 16, &r, &g, &b, &a); CHECK(b > 200 && r < 60, "circulo pintado de azul"); tb_image_get_pixel(im, 0, 0, &r, &g, &b, &a); CHECK(a == 0, "fundo transparente"); } /* Poligono preenchido (triangulo verde) */ TinyBlocks_VectorData* p = tb_vg_new(); double tri[] = {2,2, 18,2, 10,18}; tb_vg_polygon(p, tri, 3, true, 0, 1, 0, 1); TinyBlocks_Image* pi = tb_vg_render(p, 20, 20, 1); if (pi) { tb_image_get_pixel(pi, 10, 8, &r, &g, &b, &a); CHECK(g > 150 && r < 80, "poligono preenchido de verde"); tb_image_release(pi); } tb_vector_data_release(p); /* Linha espessa */ TinyBlocks_VectorData* l = tb_vg_new(); tb_vg_line(l, 2, 10, 18, 10, 1, 1, 1, 1, 4); TinyBlocks_Image* li = tb_vg_render(l, 20, 20, 1); if (li) { tb_image_get_pixel(li, 10, 10, &r, &g, &b, &a); CHECK(r > 200 && g > 200 && b > 200, "linha branca espessa"); tb_image_release(li); } tb_vector_data_release(l); if (im) tb_image_release(im); tb_vector_data_release(v); /* -- Blocos --- */ TinyBlocks* rt; tinyblocks_init(&rt, NULL); tb_library_register(rt, tb_vg_get_library()); const char* names[] = {"VG.New","VG.Line","VG.Rect","VG.Circle","VG.Polygon","VG.Render"}; 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 VG.* registrados"); { TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, "VG.New"); const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)t->base.parent); CHECK(cat && strcmp(cat->name, "Vetor") == 0, "categoria 'Vetor'"); } TinyBlocks_Block* bn = tb_block_create(rt, tb_rt_find_type(rt, "VG.New")); tb_block_eval(bn, rt); CHECK(bn->outputs[0].kind == TB_VALUE_VECTOR, "VG.New devolve vetor"); TinyBlocks_Block* br = tb_block_create(rt, tb_rt_find_type(rt, "VG.Rect")); set_vec(br, 0, bn->outputs[0].as.vector); set_num(br, 1, 0); set_num(br, 2, 0); set_num(br, 3, 10); set_num(br, 4, 10); set_num(br, 5, 1); set_num(br, 6, 0); set_num(br, 7, 1); /* magenta */ tb_block_eval(br, rt); CHECK(br->outputs[0].kind == TB_VALUE_VECTOR, "VG.Rect devolve o mesmo vetor"); TinyBlocks_Block* bren = tb_block_create(rt, tb_rt_find_type(rt, "VG.Render")); set_vec(bren, 0, br->outputs[0].as.vector); set_num(bren, 1, 12); set_num(bren, 2, 12); set_num(bren, 3, 1); tb_block_eval(bren, rt); CHECK(bren->outputs[0].kind == TB_VALUE_IMAGE, "VG.Render devolve imagem"); if (bren->outputs[0].kind == TB_VALUE_IMAGE) { tb_image_get_pixel(bren->outputs[0].as.image, 5, 5, &r, &g, &b, &a); CHECK(r > 200 && b > 200, "retangulo magenta rasterizado pelo bloco"); } tb_block_destroy(bn, rt); tb_block_destroy(br, rt); tb_block_destroy(bren, rt); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }