awtk/tests/image_loader_stb_test.cc

161 lines
5.6 KiB
C++
Raw Normal View History

#include "tkc/fs.h"
#include "tkc/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) {
ret_t ret = RET_OK;
image_loader_t* loader = image_loader_stb();
2021-09-16 16:06:55 +08:00
int32_t size = file_get_size(filename);
2020-12-14 11:03:58 +08:00
asset_info_t* info = asset_info_create(ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_PNG, "name", size);
2018-11-25 18:57:44 +08:00
return_value_if_fail(info != NULL, RET_OOM);
ENSURE(file_read_part(filename, info->data, size, 0) == size);
ret = image_loader_load(loader, info, image);
2020-12-14 11:03:58 +08:00
asset_info_destroy(info);
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
}
2022-07-06 06:36:58 +08:00
static ret_t load_image_ex(const char* filename, bitmap_t* image,
bitmap_format_t transparent_bitmap_format,
bitmap_format_t opaque_bitmap_format) {
2018-10-17 09:57:47 +08:00
uint32_t size = 0;
ret_t ret = RET_OK;
printf("%s\n", filename);
uint8_t* buff = (uint8_t*)read_file(filename, &size);
2022-07-06 06:36:58 +08:00
ret = stb_load_image(0, buff, size, image, transparent_bitmap_format, opaque_bitmap_format,
LCD_ORIENTATION_0);
2018-10-17 09:57:47 +08:00
TKMEM_FREE(buff);
return ret;
}
TEST(ImageLoaderStb, bgr565_apaque) {
bitmap_t image;
2022-06-23 17:55:44 +08:00
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGR565), RET_OK);
2018-10-17 09:57:47 +08:00
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), TRUE);
ASSERT_EQ(image.format, BITMAP_FMT_BGR565);
bitmap_destroy(&image);
2022-07-06 06:36:58 +08:00
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGRA8888),
RET_OK);
2018-10-17 09:57:47 +08:00
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;
2022-06-23 17:55:44 +08:00
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGR565), RET_OK);
2018-10-17 09:57:47 +08:00
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
2022-06-23 17:55:44 +08:00
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGRA8888), RET_OK);
2018-10-17 09:57:47 +08:00
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
TEST(ImageLoaderStb, rgba) {
bitmap_t image;
2022-06-23 17:55:44 +08:00
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_RGBA8888, BITMAP_FMT_RGB565), RET_OK);
2018-10-17 09:57:47 +08:00
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_RGBA8888);
bitmap_destroy(&image);
2022-06-23 17:55:44 +08:00
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_RGB565), RET_OK);
2018-10-17 09:57:47 +08:00
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
2022-06-23 17:55:44 +08:00
TEST(ImageLoaderStb, rgb) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_RGB888), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), TRUE);
ASSERT_EQ(image.format, BITMAP_FMT_RGB888);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_RGBA8888, BITMAP_FMT_RGB888), 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, BITMAP_FMT_BGRA8888, BITMAP_FMT_RGB888), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
TEST(ImageLoaderStb, bgr) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGR888), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), TRUE);
ASSERT_EQ(image.format, BITMAP_FMT_BGR888);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_RGBA8888, BITMAP_FMT_BGR888), 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, BITMAP_FMT_BGRA8888, BITMAP_FMT_BGR888), RET_OK);
ASSERT_EQ(!!(image.flags & BITMAP_FLAG_OPAQUE), FALSE);
ASSERT_EQ(image.format, BITMAP_FMT_BGRA8888);
bitmap_destroy(&image);
}
TEST(ImageLoaderStb, mono) {
bitmap_t image;
ASSERT_EQ(load_image_ex(PNG_OPAQUE_NAME, &image, BITMAP_FMT_BGRA8888, BITMAP_FMT_MONO), RET_OK);
ASSERT_EQ(image.format, BITMAP_FMT_MONO);
bitmap_destroy(&image);
ASSERT_EQ(load_image_ex(PNG_NAME, &image, BITMAP_FMT_RGBA8888, BITMAP_FMT_MONO), RET_OK);
ASSERT_EQ(image.format, BITMAP_FMT_MONO);
bitmap_destroy(&image);
}