/* * jpg_test.c - testa tinyblocks-jpg (libjpeg): round-trip com perda e blocos. */ #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) int main(void) { TinyBlocks_Image* img = tb_image_new(16, 16, 3); for (int y = 0; y < 16; y++) for (int x = 0; x < 16; x++) tb_image_set_pixel(img, x, y, x < 8 ? 220 : 20, 20, x < 8 ? 20 : 220, 255); CHECK(tb_jpg_save(img, "build/jpg_test_out.jpg", 90), "tb_jpg_save grava JPEG"); TinyBlocks_Image* rd = tb_jpg_load("build/jpg_test_out.jpg"); CHECK(rd && tb_image_width(rd) == 16 && tb_image_height(rd) == 16, "tb_jpg_load le 16x16"); if (rd) { uint8_t r, g, b, a; tb_image_get_pixel(rd, 2, 8, &r, &g, &b, &a); CHECK(r > 150 && g < 90 && b < 90, "metade esquerda avermelhada"); tb_image_get_pixel(rd, 13, 8, &r, &g, &b, &a); CHECK(b > 150 && r < 90, "metade direita azulada"); } remove("build/jpg_test_out.jpg"); if (rd) tb_image_release(rd); tb_image_release(img); CHECK(tb_jpg_load("build/nao_existe.jpg") == NULL, "load de ausente -> NULL"); CHECK(strlen(tb_jpg_last_error()) > 0, "mensagem de erro presente"); TinyBlocks* rt; tinyblocks_init(&rt, NULL); tb_library_register(rt, tb_jpg_get_library()); CHECK(tb_rt_find_type(rt, "JPG.Load") && tb_rt_find_type(rt, "JPG.Save"), "blocos JPG.Load/Save registrados"); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }