2018-02-21 19:36:38 +08:00
|
|
|
/**
|
|
|
|
* File: image_gen.c
|
2018-05-15 09:31:58 +08:00
|
|
|
* Author: AWTK Develop Team
|
2018-02-21 19:36:38 +08:00
|
|
|
* Brief: bitmap image generator
|
|
|
|
*
|
2019-01-07 10:58:36 +08:00
|
|
|
* Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
2018-02-21 19:36:38 +08:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* License file for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History:
|
|
|
|
* ================================================================
|
|
|
|
* 2018-01-27 Li XianJing <xianjimli@hotmail.com> created
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-15 17:22:05 +08:00
|
|
|
#include "tkc/mem.h"
|
|
|
|
#include "tkc/utils.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "common/utils.h"
|
2018-03-18 11:29:31 +08:00
|
|
|
#include "image_gen/image_gen.h"
|
|
|
|
#include "base/image_manager.h"
|
2018-08-24 07:45:37 +08:00
|
|
|
#include "base/assets_manager.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "image_loader/image_loader_stb.h"
|
|
|
|
|
2018-06-08 19:19:06 +08:00
|
|
|
#define MAX_BUFF_SIZE 2 * 1024 * 1024
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2019-08-12 10:47:09 +08:00
|
|
|
ret_t image_gen(bitmap_t* image, const char* output_filename, bool_t mono) {
|
2018-02-21 19:36:38 +08:00
|
|
|
uint32_t size = 0;
|
2018-04-29 16:51:54 +08:00
|
|
|
uint8_t* buff = (uint8_t*)TKMEM_ALLOC(MAX_BUFF_SIZE);
|
2018-03-18 11:29:31 +08:00
|
|
|
return_value_if_fail(buff != NULL, RET_FAIL);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2019-08-12 10:47:09 +08:00
|
|
|
size = image_gen_buff(image, buff, MAX_BUFF_SIZE, mono);
|
2018-08-24 07:45:37 +08:00
|
|
|
output_res_c_source(output_filename, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_RAW, buff, size);
|
2018-04-29 16:51:54 +08:00
|
|
|
TKMEM_FREE(buff);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:47:09 +08:00
|
|
|
uint32_t image_gen_buff(bitmap_t* image, uint8_t* output_buff, uint32_t buff_size, bool_t mono) {
|
2018-02-21 19:36:38 +08:00
|
|
|
size_t size = 0;
|
2019-10-31 11:21:46 +08:00
|
|
|
uint8_t* image_data = NULL;
|
2018-02-21 19:36:38 +08:00
|
|
|
bitmap_header_t* header = (bitmap_header_t*)output_buff;
|
2018-03-18 11:29:31 +08:00
|
|
|
return_value_if_fail(image != NULL && output_buff != NULL, 0);
|
2018-06-08 19:19:06 +08:00
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
header->w = image->w;
|
|
|
|
header->h = image->h;
|
2018-04-18 14:27:14 +08:00
|
|
|
header->flags = image->flags;
|
2019-10-31 11:21:46 +08:00
|
|
|
image_data = bitmap_lock_buffer_for_read(image);
|
2019-08-12 10:47:09 +08:00
|
|
|
if (!mono) {
|
|
|
|
size = bitmap_get_line_length(image) * image->h;
|
|
|
|
ENSURE((size + sizeof(bitmap_header_t)) < buff_size);
|
|
|
|
|
|
|
|
header->format = image->format;
|
2019-10-31 11:21:46 +08:00
|
|
|
memcpy(header->data, image_data, size);
|
2019-08-12 10:47:09 +08:00
|
|
|
} else {
|
|
|
|
bitmap_t b;
|
2019-10-31 11:21:46 +08:00
|
|
|
uint8_t* bdata = NULL;
|
|
|
|
bitmap_init_from_rgba(&b, image->w, image->h, BITMAP_FMT_MONO, image_data, 4);
|
2019-08-12 10:47:09 +08:00
|
|
|
header->format = b.format;
|
|
|
|
size = bitmap_get_line_length(&b) * b.h;
|
|
|
|
ENSURE((size + sizeof(bitmap_header_t)) < buff_size);
|
|
|
|
|
2019-10-31 11:21:46 +08:00
|
|
|
bdata = bitmap_lock_buffer_for_read(&b);
|
|
|
|
memcpy(header->data, bdata, size);
|
2018-06-08 19:19:06 +08:00
|
|
|
|
2019-10-31 11:21:46 +08:00
|
|
|
bitmap_mono_dump(bdata, b.w, b.h);
|
|
|
|
bitmap_unlock_buffer(&b);
|
2019-08-12 10:47:09 +08:00
|
|
|
bitmap_destroy(&b);
|
|
|
|
}
|
2019-10-31 11:21:46 +08:00
|
|
|
bitmap_unlock_buffer(image);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return size + sizeof(bitmap_header_t);
|
|
|
|
}
|