awtk/tests/image_loader_stb_test.cc
2018-12-29 12:17:06 +08:00

146 lines
4.6 KiB
C++

#include "tkc/fs.h"
#include "tkc/mem.h"
#include "gtest/gtest.h"
#include "tools/common/utils.h"
#include "base/image_manager.h"
#include "base/assets_manager.h"
#include "tools/image_gen/image_gen.h"
#include "image_loader/image_loader_stb.h"
#define PNG_NAME TK_ROOT "/tests/testdata/test.png"
#define JPG_NAME TK_ROOT "/tests/testdata/test.jpg"
#define PNG_OPAQUE_NAME TK_ROOT "/tests/testdata/test_opaque.png"
static ret_t load_image(const char* filename, bitmap_t* image) {
ret_t ret = RET_OK;
image_loader_t* loader = image_loader_stb();
uint32_t size = file_get_size(filename);
asset_info_t* info = (asset_info_t*)TKMEM_ALLOC(sizeof(asset_info_t) + size);
return_value_if_fail(info != NULL, RET_OOM);
memset(info, 0x00, sizeof(asset_info_t));
info->size = size;
info->type = ASSET_TYPE_IMAGE;
info->subtype = 0;
info->refcount = 1;
info->is_in_rom = FALSE;
strncpy(info->name, "name", TK_NAME_LEN);
ENSURE(file_read_part(filename, info->data, size, 0) == size);
ret = image_loader_load(loader, info, image);
TKMEM_FREE(info);
return ret;
}
TEST(ImageLoaderStb, basic) {
bitmap_t image;
memset(&image, 0x00, sizeof(image));
ret_t ret = load_image(PNG_NAME, &image);
ASSERT_EQ(ret, RET_OK);
ASSERT_EQ(32, image.w);
ASSERT_EQ(32, image.h);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_IMMUTABLE), true);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), false);
bitmap_destroy(&image);
ret = load_image(JPG_NAME, &image);
ASSERT_EQ(ret, RET_OK);
ASSERT_EQ(32, image.w);
ASSERT_EQ(32, image.h);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_IMMUTABLE), true);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), true);
bitmap_destroy(&image);
ret = load_image(PNG_OPAQUE_NAME, &image);
ASSERT_EQ(ret, RET_OK);
ASSERT_EQ(32, image.w);
ASSERT_EQ(32, image.h);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_IMMUTABLE), true);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), true);
bitmap_destroy(&image);
}
static ret_t add_image_res(const char* filename, const char* name) {
bitmap_t image;
static uint8_t buff[8092];
memset(&image, 0x00, sizeof(image));
ret_t ret = load_image(filename, &image);
asset_info_t* r = (asset_info_t*)buff;
return_value_if_fail(ret == RET_OK, RET_FAIL);
strcpy(r->name, name);
r->is_in_rom = TRUE;
r->type = ASSET_TYPE_IMAGE;
r->subtype = ASSET_TYPE_IMAGE_RAW;
r->size = image_gen_buff(&image, r->data, sizeof(buff) - sizeof(asset_info_t));
bitmap_destroy(&image);
return assets_manager_add(assets_manager(), buff);
}
TEST(ImageLoaderStb, gen) {
bitmap_t image;
ASSERT_EQ(add_image_res(PNG_OPAQUE_NAME, "test.png"), RET_OK);
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "test.png", &image), RET_OK);
ASSERT_EQ(32, image.w);
ASSERT_EQ(32, image.h);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_IMMUTABLE), true);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), true);
image_manager_unload_unused(image_manager(), 0);
}
static ret_t load_image_ex(const char* filename, bitmap_t* image, bool_t require_bgra,
bool_t enable_bgr565) {
uint32_t size = 0;
ret_t ret = RET_OK;
printf("%s\n", filename);
uint8_t* buff = (uint8_t*)read_file(filename, &size);
ret = stb_load_image(0, buff, size, image, require_bgra, enable_bgr565);
TKMEM_FREE(buff);
return ret;
}
TEST(ImageLoaderStb, bgr565_apaque) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, TRUE, TRUE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), TRUE);
ASSERT_EQ(image.format, BITMAP_FMT_BGR565);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, TRUE, FALSE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), TRUE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
TEST(ImageLoaderStb, bgr565_trans) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_NAME, &image, TRUE, TRUE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_NAME, &image, TRUE, FALSE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
TEST(ImageLoaderStb, rgba) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_NAME, &image, FALSE, FALSE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_RGBA8888);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_NAME, &image, TRUE, FALSE), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}