From 88f662842340c6b9082b4ea9ea99bf24aefd2da7 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Mon, 19 Dec 2022 21:12:07 -0500 Subject: consolidate bmp_read function between icons and skin_parser uses fd now rather than opening file twice Change-Id: If35418cbc77adacf5e96fb6aa0fc8ffef2fffcbd --- apps/gui/icon.c | 48 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) (limited to 'apps/gui/icon.c') diff --git a/apps/gui/icon.c b/apps/gui/icon.c index 9deb1a0c65..7a59a72151 100644 --- a/apps/gui/icon.c +++ b/apps/gui/icon.c @@ -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) - { - /* 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) + 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 */ - 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; + } } } -- cgit v1.2.3