consolidate bmp_read function between icons and skin_parser

uses fd now rather than opening file twice

Change-Id: If35418cbc77adacf5e96fb6aa0fc8ffef2fffcbd
This commit is contained in:
William Wilgus 2022-12-19 21:12:07 -05:00
parent 2b4a4070c9
commit 88f6628423
4 changed files with 120 additions and 89 deletions

View file

@ -167,57 +167,27 @@ static int buflib_move_callback(int handle, void* current, void* new)
}
return BUFLIB_CB_OK;
}
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
static void load_icons(const char* filename, enum Iconset iconset,
enum screen_type screen)
{
ssize_t size_read;
ssize_t buf_size;
int fd;
int bmpformat = (FORMAT_ANY|FORMAT_DITHER|FORMAT_TRANSPARENT);
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
const int bmpformat = (FORMAT_ANY|FORMAT_DITHER|FORMAT_TRANSPARENT);
struct iconset *ic = &iconsets[iconset][screen];
ssize_t buf_reqd;
ic->loaded = false;
ic->handle = 0;
ic->handle = CLB_ALOC_ERR;
if (filename[0] && filename[0] != '-')
{
char fname[MAX_PATH];
fd = open_pathfmt(fname, sizeof(fname), O_RDONLY,
ICON_DIR "/%s.bmp", filename);
if (fd < 0)
return;
buf_size = read_bmp_fd(fd, &ic->bmp, 0,
bmpformat|FORMAT_RETURN_SIZE, NULL);
if (buf_size > 0)
ic->handle = core_alloc_ex(filename, (size_t) buf_size, &buflib_ops);
if (ic->handle <= 0)
snprintf(fname, sizeof(fname), ICON_DIR "/%s.bmp", filename);
ic->handle = core_load_bmp(fname, &ic->bmp, bmpformat, &buf_reqd, &buflib_ops);
if (ic->handle != CLB_ALOC_ERR)
{
/* error */
goto finished;
}
lseek(fd, 0, SEEK_SET);
core_pin(ic->handle);
ic->bmp.data = core_get_data(ic->handle);
size_read = read_bmp_fd(fd, &ic->bmp, buf_size, bmpformat, NULL);
core_unpin(ic->handle);
if (size_read < 0)
{
/* error */
size_read = 0;
}
/* free unused alpha channel, if any */
core_shrink(ic->handle, ic->bmp.data, size_read);
if (size_read == 0)
ic->handle = core_free(ic->handle);
else
ic->bmp.data = core_get_data(ic->handle);
ic->loaded = true;
finished:
close(fd);
return;
}
}
}

View file

@ -1908,77 +1908,60 @@ static int buflib_move_callback(int handle, void* current, void* new)
return BUFLIB_CB_OK;
}
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
#endif
static int load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char* bmpdir)
{
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
(void)wps_data; /* only needed for remote targets */
char img_path[MAX_PATH];
int fd;
int handle;
get_image_filename(bitmap->data, bmpdir,
img_path, sizeof(img_path));
/* load the image */
int format;
#ifdef HAVE_REMOTE_LCD
if (curr_screen == SCREEN_REMOTE)
format = FORMAT_ANY|FORMAT_REMOTE;
else
#endif
format = FORMAT_ANY|FORMAT_TRANSPARENT;
fd = open(img_path, O_RDONLY);
#ifdef __PCTOOL__ /* just check if image exists */
int fd = open(img_path, O_RDONLY);
if (fd < 0)
{
DEBUGF("Couldn't open %s\n", img_path);
return fd;
}
#ifndef __PCTOOL__
int buf_size = read_bmp_fd(fd, bitmap, 0,
format|FORMAT_RETURN_SIZE, NULL);
if(buf_size < 0)
close(fd);
return 1;
#else /* load the image */
int handle;
int bmpformat;
ssize_t buf_reqd;
#ifdef HAVE_REMOTE_LCD
if (curr_screen == SCREEN_REMOTE)
bmpformat = FORMAT_ANY|FORMAT_REMOTE;
else
#endif
bmpformat = FORMAT_ANY|FORMAT_TRANSPARENT;
handle = core_load_bmp(img_path, bitmap, bmpformat, &buf_reqd, &buflib_ops);
if (handle != CLB_ALOC_ERR)
{
close(fd);
return buf_size;
/* NOTE!: bitmap->data == NULL to force a crash later if the
caller doesnt call core_get_data() */
_stats->buflib_handles++;
_stats->images_size += buf_reqd;
return handle;
}
handle = core_alloc_ex(bitmap->data, buf_size, &buflib_ops);
if (handle <= 0)
if (buf_reqd == CLB_READ_ERR)
{
DEBUGF("Not enough skin buffer: need %zd more.\n",
buf_size - skin_buffer_freespace());
close(fd);
return handle;
}
_stats->buflib_handles++;
_stats->images_size += buf_size;
lseek(fd, 0, SEEK_SET);
core_pin(handle);
bitmap->data = core_get_data(handle);
int ret = read_bmp_fd(fd, bitmap, buf_size, format, NULL);
bitmap->data = NULL; /* do this to force a crash later if the
caller doesnt call core_get_data() */
core_unpin(handle);
close(fd);
if (ret > 0)
{
/* free unused alpha channel, if any */
core_shrink(handle, core_get_data(handle), ret);
return handle;
/* Abort if we can't load an image */
DEBUGF("Couldn't load '%s' (%ld)\n", img_path, buf_reqd);
}
else
{
/* Abort if we can't load an image */
DEBUGF("Couldn't load '%s'\n", img_path);
core_free(handle);
return -1;
DEBUGF("Not enough skin buffer: need %zd more.\n",
buf_reqd - skin_buffer_freespace());
}
#else /* !__PCTOOL__ */
close(fd);
return 1;
#endif
return -1;
#endif/* !__PCTOOL__ */
}
static bool load_skin_bitmaps(struct wps_data *wps_data, char *bmpdir)