1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_objx/lv_img.c

360 lines
9.3 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_img.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
2017-01-02 10:48:21 +01:00
#include "misc_conf.h"
#if USE_LV_IMG != 0 && USE_FSINT != 0 && USE_UFS != 0
2016-06-08 07:25:08 +02:00
#include "lv_img.h"
#include "../lv_draw/lv_draw.h"
#include "misc/fs/fsint.h"
#include "misc/fs/ufs/ufs.h"
2017-01-16 12:30:22 +01:00
#if LV_IMG_ENABLE_SYMBOLS != 0
#include "../lv_misc/text.h"
#endif
2016-06-08 07:25:08 +02:00
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-10-07 11:15:46 +02:00
static bool lv_img_design(lv_obj_t * img, const area_t * mask, lv_design_mode_t mode);
2016-06-08 07:25:08 +02:00
2017-01-16 12:30:22 +01:00
static bool lv_img_is_symbol(const char * txt);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create an image objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new button
* @param copy pointer to a rectangle object, if not NULL then the new object will be copied from it
2016-06-08 07:25:08 +02:00
* @return pointer to the created image
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_img_create(lv_obj_t * par, lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_obj_t * new_img = NULL;
2016-06-08 07:25:08 +02:00
/*Create a basic object*/
2016-10-07 17:33:35 +02:00
new_img = lv_obj_create(par, copy);
dm_assert(new_img);
2016-06-08 07:25:08 +02:00
/*Extend the basic object to image object*/
lv_img_ext_t * ext = lv_obj_alloc_ext(new_img, sizeof(lv_img_ext_t));
dm_assert(ext);
ext->fn = NULL;
ext->w = lv_obj_get_width(new_img);
ext->h = lv_obj_get_height(new_img);
ext->transp = 0;
ext->upscale = 0;
2016-06-08 07:25:08 +02:00
/*Init the new object*/
2016-10-07 11:15:46 +02:00
lv_obj_set_signal_f(new_img, lv_img_signal);
lv_obj_set_design_f(new_img, lv_img_design);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
/* Enable auto size for non screens
* because image screens are wallpapers
* and must be screen sized*/
if(par != NULL) ext->auto_size = 1;
else ext->auto_size = 0;
lv_obj_set_style(new_img, lv_style_get(LV_STYLE_PLAIN, NULL));
2016-06-08 07:25:08 +02:00
} else {
ext->auto_size = lv_img_get_auto_size(copy);
lv_img_set_file(new_img, ext->fn);
/*Refresh the style with new signal function*/
lv_obj_refr_style(new_img);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
return new_img;
2016-06-08 07:25:08 +02:00
}
/**
* Signal function of the image
2016-10-07 11:15:46 +02:00
* @param img pointer to animage object
2016-06-08 07:25:08 +02:00
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
*/
2016-10-07 11:15:46 +02:00
bool lv_img_signal(lv_obj_t * img, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
bool valid = true;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_obj_signal(img, sign, param);
2016-06-08 07:25:08 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
2016-10-07 11:15:46 +02:00
lv_img_ext_t * img_p = lv_obj_get_ext(img);
2016-06-08 07:25:08 +02:00
switch(sign) {
/*TODO set file again if style changed with symbols*/
2016-06-08 07:25:08 +02:00
case LV_SIGNAL_CLEANUP:
2016-10-07 11:15:46 +02:00
dm_free(img_p->fn);
2016-06-08 07:25:08 +02:00
break;
default:
break;
}
}
return valid;
}
/**
* Create a file to the RAMFS from a picture data
* @param fn file name of the new file (e.g. "pic1", will be available at "U:/pic1")
2016-10-07 11:15:46 +02:00
* @param data pointer to a color map with lv_img_raw_header_t header
2016-06-08 07:25:08 +02:00
* @return result of the file operation. FS_RES_OK or any error from fs_res_t
*/
2016-10-07 11:15:46 +02:00
fs_res_t lv_img_create_file(const char * fn, const color_int_t * data)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
const lv_img_raw_header_t * raw_p = (lv_img_raw_header_t *) data;
2016-06-08 07:25:08 +02:00
fs_res_t res;
res = ufs_create_const(fn, data, raw_p->w * raw_p->h * sizeof(color_t) + sizeof(lv_img_raw_header_t));
2016-06-08 07:25:08 +02:00
return res;
}
/*=====================
* Setter functions
*====================*/
/**
* Set a file to the image
2016-10-07 11:15:46 +02:00
* @param img pointer to an image object
2016-06-08 07:25:08 +02:00
* @param fn file name in the RAMFS to set as picture (e.g. "U:/pic1").
*/
2016-10-07 11:15:46 +02:00
void lv_img_set_file(lv_obj_t * img, const char * fn)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_img_ext_t * ext = lv_obj_get_ext(img);
2016-06-08 07:25:08 +02:00
2017-01-16 12:30:22 +01:00
/*Handle normal images*/
if(lv_img_is_symbol(fn) == false) {
fs_file_t file;
fs_res_t res;
lv_img_raw_header_t header;
uint32_t rn;
res = fs_open(&file, fn, FS_MODE_RD);
if(res == FS_RES_OK) {
res = fs_read(&file, &header, sizeof(header), &rn);
}
2016-06-08 07:25:08 +02:00
2017-01-16 12:30:22 +01:00
/*Create a dummy header on fs error*/
if(res != FS_RES_OK || rn != sizeof(header)) {
header.w = lv_obj_get_width(img);
header.h = lv_obj_get_height(img);
header.transp = 0;
}
2016-06-08 07:25:08 +02:00
2017-01-16 12:30:22 +01:00
fs_close(&file);
2016-06-08 07:25:08 +02:00
2017-01-16 12:30:22 +01:00
ext->w = header.w;
ext->h = header.h;
ext->transp = header.transp;
2017-03-09 11:23:28 +01:00
#if LV_ANTIALIAS != 0
2017-01-16 12:30:22 +01:00
if(ext->upscale != 0) {
ext->w *= 2;
ext->h *= 2;
}
2017-03-09 11:23:28 +01:00
#endif
2017-01-16 12:30:22 +01:00
}
/*Handle symbol texts*/
else {
#if LV_IMG_ENABLE_SYMBOLS
lv_style_t * style = lv_obj_get_style(img);
2017-01-16 12:30:22 +01:00
point_t size;
txt_get_size(&size, fn, style->font, 0, 0, LV_CORD_MAX, TXT_FLAG_NONE);
2017-01-16 12:30:22 +01:00
ext->w = size.x;
ext->h = size.y;
ext->transp = 0;
#else
/*Never goes here, just to be sure handle this */
ext->w = lv_obj_get_width(img);
ext->h = lv_obj_get_height(img);
ext->transp = 0;
#endif
}
2016-06-08 07:25:08 +02:00
if(fn != NULL) {
2016-10-07 11:15:46 +02:00
ext->fn = dm_realloc(ext->fn, strlen(fn) + 1);
strcpy(ext->fn, fn);
2016-06-08 07:25:08 +02:00
} else {
2016-10-07 11:15:46 +02:00
ext->fn = NULL;
2016-06-08 07:25:08 +02:00
}
2017-01-16 12:30:22 +01:00
2016-10-07 11:15:46 +02:00
if(lv_img_get_auto_size(img) != false) {
2017-01-16 12:30:22 +01:00
lv_obj_set_size(img, ext->w, ext->h);
2016-06-08 07:25:08 +02:00
}
lv_obj_inv(img);
2016-06-08 07:25:08 +02:00
}
/**
2016-10-07 11:15:46 +02:00
* Enable the auto size feature.
* If enabled the object size will be same as the picture size.
* @param img pointer to an image
* @param en true: auto size enable, false: auto size disable
*/
void lv_img_set_auto_size(lv_obj_t * img, bool en)
{
lv_img_ext_t * ext = lv_obj_get_ext(img);
ext->auto_size = (en == false ? 0 : 1);
}
2017-01-16 12:30:22 +01:00
/**
* Enable the upscaling with LV_DOWNSCALE.
* If enabled the object size will be same as the picture size.
* @param img pointer to an image
* @param en true: upscale enable, false: upscale disable
2016-06-08 07:25:08 +02:00
*/
void lv_img_set_upscale(lv_obj_t * img, bool en)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_img_ext_t * ext = lv_obj_get_ext(img);
2017-03-09 11:23:28 +01:00
/*Upscale works only if antialiassing is enabled*/
#if LV_ANTIALIAS == 0
en = false;
#endif
ext->upscale = (en == false ? 0 : 1);
/*Refresh the image with the new size*/
lv_img_set_file(img, ext->fn);
2016-06-08 07:25:08 +02:00
}
/*=====================
* Getter functions
*====================*/
/**
* Get the auto size enable attribute
2016-10-07 11:15:46 +02:00
* @param img pointer to an image
2016-06-08 07:25:08 +02:00
* @return true: auto size is enabled, false: auto size is disabled
*/
2016-10-07 11:15:46 +02:00
bool lv_img_get_auto_size(lv_obj_t * img)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_img_ext_t * ext = lv_obj_get_ext(img);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
return ext->auto_size == 0 ? false : true;
2016-06-08 07:25:08 +02:00
}
/**
* Get the upscale enable attribute
* @param img pointer to an image
* @return true: upscale is enabled, false: upscale is disabled
*/
bool lv_img_get_upscale(lv_obj_t * img)
{
lv_img_ext_t * ext = lv_obj_get_ext(img);
return ext->upscale == 0 ? false : true;
}
2016-06-08 07:25:08 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the images
2016-10-07 11:15:46 +02:00
* @param img pointer to an object
2016-06-08 07:25:08 +02:00
* @param mask the object will be drawn only in this area
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
* (return 'true' if yes)
* LV_DESIGN_DRAW: draw the object (always return 'true')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
2016-06-08 07:25:08 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_img_design(lv_obj_t * img, const area_t * mask, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
lv_style_t * style = lv_obj_get_style(img);
2016-10-07 11:15:46 +02:00
lv_img_ext_t * ext = lv_obj_get_ext(img);
2016-06-08 07:25:08 +02:00
if(mode == LV_DESIGN_COVER_CHK) {
2016-10-07 11:15:46 +02:00
if(ext->transp == 0) {
2016-06-08 07:25:08 +02:00
bool cover;
2016-10-07 11:15:46 +02:00
cover = area_is_in(mask, &img->cords);
2016-06-08 07:25:08 +02:00
return cover;
}
else return false;
} else if(mode == LV_DESIGN_DRAW_MAIN) {
if(ext->h == 0 || ext->w == 0) return true;
area_t cords;
2017-01-16 12:30:22 +01:00
/*Create a default style for symbol texts*/
#if LV_IMG_ENABLE_SYMBOLS != 0
bool sym = lv_img_is_symbol(ext->fn);
#endif
2016-10-07 11:15:46 +02:00
lv_obj_get_cords(img, &cords);
area_t cords_tmp;
cords_tmp.y1 = cords.y1;
2016-10-07 11:15:46 +02:00
cords_tmp.y2 = cords.y1 + ext->h - 1;
2016-10-07 11:15:46 +02:00
for(; cords_tmp.y1 < cords.y2; cords_tmp.y1 += ext->h, cords_tmp.y2 += ext->h) {
cords_tmp.x1 = cords.x1;
2016-10-07 11:15:46 +02:00
cords_tmp.x2 = cords.x1 + ext->w - 1;
for(; cords_tmp.x1 < cords.x2; cords_tmp.x1 += ext->w, cords_tmp.x2 += ext->w) {
2017-01-16 12:30:22 +01:00
#if LV_IMG_ENABLE_SYMBOLS == 0
lv_draw_img(&cords_tmp, mask, style, opa, ext->fn);
2017-01-16 12:30:22 +01:00
#else
if(sym == false) lv_draw_img(&cords_tmp, mask, style, ext->fn);
else lv_draw_label(&cords_tmp, mask, style, ext->fn, TXT_FLAG_NONE);
2017-01-16 12:30:22 +01:00
#endif
}
}
2016-06-08 07:25:08 +02:00
}
return true;
}
2017-01-16 12:30:22 +01:00
/**
* From the settings in lv_conf.h and the file name
* tells it a filename or a symbol text.
* @param txt a file name (e.g. "U:/file1") or a symbol (e.g. SYMBOL_OK)
* @return true: 'txt' is a symbol text, false: 'txt' is a file name
*/
static bool lv_img_is_symbol(const char * txt)
{
/*If the symbols are not enabled always tell false*/
#if LV_IMG_ENABLE_SYMBOLS == 0
return false;
#endif
/* if txt begins with an upper case letter then it refers to a driver
* so it is a file name*/
if(txt[0] >= 'A' && txt[0] <= 'Z') return false;
/*If not returned during the above tests then it is symbol text*/
return true;
}
2016-06-08 07:25:08 +02:00
#endif