2018-06-27 13:46:42 +08:00
|
|
|
|
#include "base/mem.h"
|
2018-04-18 14:27:14 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "tools/common/utils.h"
|
|
|
|
|
#include "base/image_manager.h"
|
2018-08-24 07:45:37 +08:00
|
|
|
|
#include "base/assets_manager.h"
|
2018-04-18 14:27:14 +08:00
|
|
|
|
#include "tools/image_gen/image_gen.h"
|
|
|
|
|
#include "image_loader/image_loader_stb.h"
|
|
|
|
|
|
2018-04-27 11:23:09 +08:00
|
|
|
|
#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"
|
2018-04-18 14:27:14 +08:00
|
|
|
|
|
|
|
|
|
static ret_t load_image(const char* filename, bitmap_t* image) {
|
|
|
|
|
uint32_t size = 0;
|
|
|
|
|
ret_t ret = RET_OK;
|
|
|
|
|
image_loader_t* loader = image_loader_stb();
|
|
|
|
|
printf("%s\n", filename);
|
|
|
|
|
uint8_t* buff = (uint8_t*)read_file(filename, &size);
|
|
|
|
|
ret = image_loader_load(loader, buff, size, image);
|
2018-04-29 16:51:54 +08:00
|
|
|
|
TKMEM_FREE(buff);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ImageLoaderStb, basic) {
|
|
|
|
|
bitmap_t image;
|
2018-08-04 11:58:43 +08:00
|
|
|
|
memset(&image, 0x00, sizeof(image));
|
|
|
|
|
|
2018-04-18 14:27:14 +08:00
|
|
|
|
ret_t ret = load_image(PNG_NAME, &image);
|
2018-04-20 21:49:47 +08:00
|
|
|
|
|
2018-04-18 14:27:14 +08:00
|
|
|
|
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);
|
2018-08-04 11:58:43 +08:00
|
|
|
|
bitmap_destroy(&image);
|
2018-04-20 21:49:47 +08:00
|
|
|
|
|
2018-04-18 14:27:14 +08:00
|
|
|
|
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);
|
2018-08-04 11:58:43 +08:00
|
|
|
|
bitmap_destroy(&image);
|
2018-04-20 21:49:47 +08:00
|
|
|
|
|
2018-04-18 14:27:14 +08:00
|
|
|
|
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);
|
2018-08-04 11:58:43 +08:00
|
|
|
|
bitmap_destroy(&image);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ret_t add_image_res(const char* filename, const char* name) {
|
|
|
|
|
bitmap_t image;
|
|
|
|
|
static uint8_t buff[8092];
|
2018-08-04 11:58:43 +08:00
|
|
|
|
memset(&image, 0x00, sizeof(image));
|
2018-04-18 14:27:14 +08:00
|
|
|
|
ret_t ret = load_image(filename, &image);
|
2018-08-24 07:45:37 +08:00
|
|
|
|
asset_info_t* r = (asset_info_t*)buff;
|
2018-04-18 14:27:14 +08:00
|
|
|
|
return_value_if_fail(ret == RET_OK, RET_FAIL);
|
|
|
|
|
|
|
|
|
|
strcpy(r->name, name);
|
2018-04-27 18:12:12 +08:00
|
|
|
|
r->is_in_rom = TRUE;
|
2018-08-24 07:45:37 +08:00
|
|
|
|
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));
|
2018-08-04 11:58:43 +08:00
|
|
|
|
bitmap_destroy(&image);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
|
2018-08-24 07:45:37 +08:00
|
|
|
|
return assets_manager_add(assets_manager(), buff);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ImageLoaderStb, gen) {
|
|
|
|
|
bitmap_t image;
|
|
|
|
|
ASSERT_EQ(add_image_res(PNG_OPAQUE_NAME, "test.png"), RET_OK);
|
2018-04-27 11:23:09 +08:00
|
|
|
|
ASSERT_EQ(image_manager_load(image_manager(), "test.png", &image), RET_OK);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
|
|
|
|
|
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);
|
2018-08-04 11:58:43 +08:00
|
|
|
|
image_manager_unload_unused(image_manager(), 0);
|
2018-04-18 14:27:14 +08:00
|
|
|
|
}
|