Files

122 lines
5.0 KiB
C

/*
* ppm_test.c - testa tinyblocks-ppm e o tipo imagem do core:
* TinyBlocks_Image (pixels, clone), leitura/gravacao P6 e P3, e os blocos.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_ppm.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);
}
int main(void) {
/* -- Tipo imagem (core) --- */
TinyBlocks_Image* img = tb_image_new(4, 3, 3);
CHECK(img && tb_image_width(img) == 4 && tb_image_height(img) == 3 &&
tb_image_channels(img) == 3, "tb_image_new cria 4x3 RGB");
tb_image_set_pixel(img, 1, 2, 10, 20, 30, 255);
uint8_t r, g, b, a;
CHECK(tb_image_get_pixel(img, 1, 2, &r, &g, &b, &a) && r == 10 && g == 20 && b == 30,
"set/get pixel consistentes");
CHECK(!tb_image_get_pixel(img, 9, 9, &r, &g, &b, &a), "pixel fora da imagem -> false");
TinyBlocks_Image* cp = tb_image_clone(img);
CHECK(cp && tb_image_get_pixel(cp, 1, 2, &r, &g, &b, &a) && r == 10, "clone copia os pixels");
/* valor ref-counted */
TinyBlocks_Value vi = tb_value_image(img);
CHECK(vi.kind == TB_VALUE_IMAGE && tb_value_as_image(vi) == img, "tb_value_image/as_image");
tb_value_free(&vi); /* solta o ref do valor */
tb_image_release(cp);
tb_image_release(img);
/* -- PPM: grava P6 e le de volta --- */
TinyBlocks_Image* pic = tb_image_new(2, 2, 3);
tb_image_set_pixel(pic, 0, 0, 255, 0, 0, 255);
tb_image_set_pixel(pic, 1, 0, 0, 255, 0, 255);
tb_image_set_pixel(pic, 0, 1, 0, 0, 255, 255);
tb_image_set_pixel(pic, 1, 1, 255, 255, 0, 255);
CHECK(tb_ppm_save(pic, "build/ppm_test_out.ppm"), "tb_ppm_save grava P6");
TinyBlocks_Image* rd = tb_ppm_load("build/ppm_test_out.ppm");
CHECK(rd && tb_image_width(rd) == 2 && tb_image_height(rd) == 2, "tb_ppm_load le as dimensoes");
if (rd) {
tb_image_get_pixel(rd, 1, 1, &r, &g, &b, &a);
CHECK(r == 255 && g == 255 && b == 0, "pixel amarelo preservado no round-trip");
}
remove("build/ppm_test_out.ppm");
/* -- PPM P3 (ASCII) escrito a mao --- */
{
FILE* f = fopen("build/ppm_test_ascii.ppm", "wb");
fprintf(f, "P3\n# comentario\n2 1\n255\n255 0 0 0 255 0\n");
fclose(f);
TinyBlocks_Image* a3 = tb_ppm_load("build/ppm_test_ascii.ppm");
CHECK(a3 && tb_image_width(a3) == 2 && tb_image_height(a3) == 1, "le P3 ASCII com comentario");
if (a3) {
tb_image_get_pixel(a3, 0, 0, &r, &g, &b, &a);
tb_image_get_pixel(a3, 1, 0, &r, &g, &b, &a);
CHECK(g == 255 && r == 0, "segundo pixel do P3 = verde");
}
if (a3) tb_image_release(a3);
remove("build/ppm_test_ascii.ppm");
}
/* arquivo inexistente */
CHECK(tb_ppm_load("build/nao_existe.ppm") == NULL, "load de arquivo ausente -> NULL");
CHECK(strlen(tb_ppm_last_error()) > 0, "mensagem de erro preenchida");
tb_image_release(rd);
tb_image_release(pic);
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_ppm_get_library());
const char* names[] = {"PPM.Load","PPM.Save","PPM.New","PPM.Width","PPM.Height",
"PPM.SetPixel","PPM.GetPixel"};
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 PPM.* registrados");
{
TinyBlocks_TypeBlock* t0 = tb_rt_find_type(rt, "PPM.New");
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)t0->base.parent);
CHECK(cat && strcmp(cat->name, "PPM") == 0, "categoria 'PPM'");
}
/* PPM.New -> largura/altura */
TinyBlocks_Block* bn = tb_block_create(rt, tb_rt_find_type(rt, "PPM.New"));
set_num(bn, 0, 3); set_num(bn, 1, 2);
set_num(bn, 2, 12); set_num(bn, 3, 34); set_num(bn, 4, 56);
tb_block_eval(bn, rt);
CHECK(bn->outputs[0].kind == TB_VALUE_IMAGE, "PPM.New devolve imagem");
TinyBlocks_Image* bi = bn->outputs[0].as.image;
CHECK(tb_image_width(bi) == 3 && tb_image_height(bi) == 2, "PPM.New respeita dimensoes");
tb_image_get_pixel(bi, 0, 0, &r, &g, &b, &a);
CHECK(r == 12 && g == 34 && b == 56, "PPM.New preenche com a cor");
TinyBlocks_Block* bw = tb_block_create(rt, tb_rt_find_type(rt, "PPM.Width"));
{
TinyBlocks_Value vv = tb_value_image(bi);
tb_block_set_default(bw, 0, vv);
tb_value_free(&vv);
}
tb_block_eval(bw, rt);
CHECK(bw->outputs[0].as.integer == 3, "PPM.Width -> 3");
tb_block_destroy(bn, rt); tb_block_destroy(bw, rt);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}