mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-14 06:42:58 +08:00
fix(png) memory leak for sjpg and use lv_mem_... in lv_png (#2704)
* Unified format * fix memory leak for lv_sjpg.c * unified format for lib_png
This commit is contained in:
parent
3a4ade1e1f
commit
c5900e36fa
@ -76,7 +76,7 @@ static void* lodepng_malloc(size_t size) {
|
||||
#ifdef LODEPNG_MAX_ALLOC
|
||||
if(size > LODEPNG_MAX_ALLOC) return 0;
|
||||
#endif
|
||||
return malloc(size);
|
||||
return lv_mem_alloc(size);
|
||||
}
|
||||
|
||||
/* NOTE: when realloc returns NULL, it leaves the original memory untouched */
|
||||
@ -84,11 +84,11 @@ static void* lodepng_realloc(void* ptr, size_t new_size) {
|
||||
#ifdef LODEPNG_MAX_ALLOC
|
||||
if(new_size > LODEPNG_MAX_ALLOC) return 0;
|
||||
#endif
|
||||
return realloc(ptr, new_size);
|
||||
return lv_mem_realloc(ptr, new_size);
|
||||
}
|
||||
|
||||
static void lodepng_free(void* ptr) {
|
||||
free(ptr);
|
||||
lv_mem_free(ptr);
|
||||
}
|
||||
#else /*LODEPNG_COMPILE_ALLOCATORS*/
|
||||
/* TODO: support giving additional void* payload to the custom allocators */
|
||||
@ -348,7 +348,7 @@ static long lodepng_filesize(const char* filename) {
|
||||
lv_fs_res_t res = lv_fs_open(&f, filename, LV_FS_MODE_RD);
|
||||
if(res != LV_FS_RES_OK) return -1;
|
||||
uint32_t size = 0;
|
||||
if(lv_fs_seek(&f, 0, SEEK_END) != 0) {
|
||||
if(lv_fs_seek(&f, 0, LV_FS_SEEK_END) != 0) {
|
||||
lv_fs_close(&f);
|
||||
return -1;
|
||||
}
|
||||
@ -385,11 +385,13 @@ unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* fil
|
||||
|
||||
/*write given buffer to the file, overwriting the file, it doesn't append to it.*/
|
||||
unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
|
||||
FILE* file;
|
||||
file = fopen(filename, "wb" );
|
||||
if(!file) return 79;
|
||||
fwrite(buffer, 1, buffersize, file);
|
||||
fclose(file);
|
||||
lv_fs_file_t f;
|
||||
lv_fs_res_t res = lv_fs_open(&f, filename, LV_FS_MODE_WR);
|
||||
if(res != LV_FS_RES_OK) return 79;
|
||||
|
||||
uint32_t bw;
|
||||
res = lv_fs_write(&f, buffer, buffersize, &bw);
|
||||
lv_fs_close(&f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -146,7 +146,7 @@ static lv_res_t decoder_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t *
|
||||
|
||||
/*Decode the loaded image in ARGB8888 */
|
||||
error = lodepng_decode32(&img_data, &png_width, &png_height, png_data, png_data_size);
|
||||
free(png_data); /*Free the loaded file*/
|
||||
lv_mem_free(png_data); /*Free the loaded file*/
|
||||
if(error) {
|
||||
printf("error %u: %s\n", error, lodepng_error_text(error));
|
||||
return LV_RES_INV;
|
||||
@ -188,7 +188,7 @@ static void decoder_close(lv_img_decoder_t *decoder, lv_img_decoder_dsc_t *dsc)
|
||||
{
|
||||
(void)decoder; /*Unused*/
|
||||
if (dsc->img_data) {
|
||||
free((uint8_t *)dsc->img_data);
|
||||
lv_mem_free((uint8_t *)dsc->img_data);
|
||||
dsc->img_data = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -833,11 +833,11 @@ static void decoder_close( lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * ds
|
||||
if(sjpeg->io.lv_file.file_d) {
|
||||
lv_fs_close(&(sjpeg->io.lv_file));
|
||||
}
|
||||
lv_sjpg_free(sjpeg);
|
||||
lv_sjpg_cleanup(sjpeg);
|
||||
break;
|
||||
|
||||
case LV_IMG_SRC_VARIABLE:
|
||||
lv_sjpg_free(sjpeg);
|
||||
lv_sjpg_cleanup(sjpeg);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -847,7 +847,7 @@ static void decoder_close( lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * ds
|
||||
|
||||
static int is_jpg( const uint8_t *raw_data )
|
||||
{
|
||||
const uint8_t jpg_signature[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46};//ÿØÿà<C3BF>JFIF
|
||||
const uint8_t jpg_signature[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46};
|
||||
return memcmp( jpg_signature, raw_data, sizeof( jpg_signature ) ) == 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user