improve mutable image

This commit is contained in:
xianjimli 2020-03-06 10:32:11 +08:00
parent b99eb1c3ce
commit 324dbc08cc
3 changed files with 59 additions and 12 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
* 2020/03/06
* 完善mutable image支持自定义的图片创建函数。
* 2020/03/05
* 增加path\_replace\_extname函数。
* 完善path\_replace\_basename函数。

View File

@ -26,20 +26,33 @@
#include "base/widget_vtable.h"
#include "mutable_image/mutable_image.h"
static bitmap_format_t mutable_image_get_disire_format(widget_t* widget, canvas_t* c) {
mutable_image_t* mutable_image = MUTABLE_IMAGE(widget);
bitmap_format_t format = BITMAP_FMT_NONE;
if (mutable_image->fb != NULL) {
format = (bitmap_format_t)(mutable_image->fb->format);
} else {
format = lcd_get_desired_bitmap_format(c->lcd);
}
return format;
}
static bitmap_t* mutable_image_prepare_image(widget_t* widget, canvas_t* c) {
mutable_image_t* mutable_image = MUTABLE_IMAGE(widget);
bitmap_format_t format = mutable_image_get_disire_format(widget, c);
return_value_if_fail(mutable_image != NULL && mutable_image->prepare_image != NULL, NULL);
if (mutable_image->create_image != NULL) {
void* ctx = mutable_image->create_image_ctx;
mutable_image->image = mutable_image->create_image(ctx, format, mutable_image->image);
} else if (mutable_image->image == NULL) {
mutable_image->image = bitmap_create_ex(widget->w, widget->h, 0, format);
}
if (mutable_image->image == NULL) {
bitmap_format_t format = BITMAP_FMT_NONE;
if (mutable_image->fb != NULL) {
format = (bitmap_format_t)(mutable_image->fb->format);
} else {
format = lcd_get_desired_bitmap_format(c->lcd);
}
mutable_image->image = bitmap_create_ex(widget->w, widget->h, widget->w * 2, format);
return NULL;
}
if (mutable_image->prepare_image != NULL) {
@ -57,7 +70,9 @@ ret_t mutable_image_on_paint_self(widget_t* widget, canvas_t* canvas) {
mutable_image_t* mutable_image = MUTABLE_IMAGE(widget);
bitmap_t* bitmap = mutable_image_prepare_image(widget, canvas);
return_value_if_fail(bitmap != NULL, RET_BAD_PARAMS);
if (bitmap == NULL) {
return RET_FAIL;
}
if (mutable_image->fb != NULL) {
rect_t r = rect_init(0, 0, bitmap->w, bitmap->h);
@ -76,9 +91,8 @@ ret_t mutable_image_on_paint_self(widget_t* widget, canvas_t* canvas) {
}
if (bitmap->buffer != NULL) {
rect_t src = rect_init(0, 0, bitmap->w, bitmap->h);
rect_t dst = rect_init(0, 0, widget->w, widget->h);
canvas_draw_image(canvas, bitmap, &src, &dst);
canvas_draw_image_center(canvas, bitmap, &dst);
}
}
@ -152,6 +166,17 @@ ret_t mutable_image_set_prepare_image(widget_t* widget, mutable_image_prepare_im
return RET_OK;
}
ret_t mutable_image_set_create_image(widget_t* widget, mutable_image_create_image_t create_image,
void* create_image_ctx) {
mutable_image_t* mutable_image = MUTABLE_IMAGE(widget);
return_value_if_fail(mutable_image != NULL && create_image != NULL, RET_BAD_PARAMS);
mutable_image->create_image = create_image;
mutable_image->create_image_ctx = create_image_ctx;
return RET_OK;
}
ret_t mutable_image_set_framebuffer(widget_t* widget, uint32_t w, uint32_t h,
bitmap_format_t format, uint8_t* buff) {
mutable_image_t* mutable_image = MUTABLE_IMAGE(widget);

View File

@ -28,6 +28,8 @@
BEGIN_C_DECLS
typedef ret_t (*mutable_image_prepare_image_t)(void* ctx, bitmap_t* image);
typedef bitmap_t* (*mutable_image_create_image_t)(void* ctx, bitmap_format_t format,
bitmap_t* old_image);
/**
* @class mutable_image_t
@ -61,6 +63,7 @@ typedef ret_t (*mutable_image_prepare_image_t)(void* ctx, bitmap_t* image);
*
* > :
* >
* > mutable\_image\_set\_create\_image设置创建图片的回调函数
* > mutable\_image\_set\_prepare\_image设置准备图片的回调函数
*
* > [mutable image demo](
@ -76,6 +79,9 @@ typedef struct _mutable_image_t {
void* prepare_image_ctx;
mutable_image_prepare_image_t prepare_image;
void* create_image_ctx;
mutable_image_create_image_t create_image;
bitmap_t* fb;
bitmap_t* image;
canvas_t* canvas;
@ -113,6 +119,19 @@ widget_t* mutable_image_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h)
ret_t mutable_image_set_prepare_image(widget_t* widget, mutable_image_prepare_image_t prepare_image,
void* prepare_image_ctx);
/**
* @method mutable_image_set_create_image
* create_image回调函数
*
* @param {widget_t*} widget mutable_image对象
* @param {mutable_image_create_image_t} create_image
* @param {void*} create_image_ctx create_image回调函数的上下文
*
* @return {ret_t} RET_OK表示成功
*/
ret_t mutable_image_set_create_image(widget_t* widget, mutable_image_create_image_t create_image,
void* create_image_ctx);
/**
* @method mutable_image_set_framebuffer
* framebuffer()