summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-12-19 21:12:07 -0500
committerWilliam Wilgus <wilgus.william@gmail.com>2022-12-20 19:07:46 -0500
commit88f662842340c6b9082b4ea9ea99bf24aefd2da7 (patch)
tree40eb3012231f1aca4d061f0d58af961e913f3adb
parent2b4a4070c99439898e22c9c9da2c21877d18a367 (diff)
downloadrockbox-88f662842340c6b9082b4ea9ea99bf24aefd2da7.tar.gz
rockbox-88f662842340c6b9082b4ea9ea99bf24aefd2da7.zip
consolidate bmp_read function between icons and skin_parser
uses fd now rather than opening file twice Change-Id: If35418cbc77adacf5e96fb6aa0fc8ffef2fffcbd
-rw-r--r--apps/gui/icon.c48
-rw-r--r--apps/gui/skin_engine/skin_parser.c79
-rw-r--r--apps/misc.c69
-rw-r--r--apps/misc.h11
4 files changed, 119 insertions, 88 deletions
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)
167 } 167 }
168 return BUFLIB_CB_OK; 168 return BUFLIB_CB_OK;
169} 169}
170static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
171 170
172static void load_icons(const char* filename, enum Iconset iconset, 171static void load_icons(const char* filename, enum Iconset iconset,
173 enum screen_type screen) 172 enum screen_type screen)
174{ 173{
175 ssize_t size_read; 174 static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
176 ssize_t buf_size; 175 const int bmpformat = (FORMAT_ANY|FORMAT_DITHER|FORMAT_TRANSPARENT);
177 int fd;
178 int bmpformat = (FORMAT_ANY|FORMAT_DITHER|FORMAT_TRANSPARENT);
179 struct iconset *ic = &iconsets[iconset][screen]; 176 struct iconset *ic = &iconsets[iconset][screen];
177 ssize_t buf_reqd;
180 178
181 ic->loaded = false; 179 ic->loaded = false;
182 ic->handle = 0; 180 ic->handle = CLB_ALOC_ERR;
183 if (filename[0] && filename[0] != '-') 181 if (filename[0] && filename[0] != '-')
184 { 182 {
185 char fname[MAX_PATH]; 183 char fname[MAX_PATH];
186 fd = open_pathfmt(fname, sizeof(fname), O_RDONLY, 184 snprintf(fname, sizeof(fname), ICON_DIR "/%s.bmp", filename);
187 ICON_DIR "/%s.bmp", filename); 185 ic->handle = core_load_bmp(fname, &ic->bmp, bmpformat, &buf_reqd, &buflib_ops);
188 if (fd < 0) 186 if (ic->handle != CLB_ALOC_ERR)
189 return;
190 buf_size = read_bmp_fd(fd, &ic->bmp, 0,
191 bmpformat|FORMAT_RETURN_SIZE, NULL);
192 if (buf_size > 0)
193 ic->handle = core_alloc_ex(filename, (size_t) buf_size, &buflib_ops);
194
195 if (ic->handle <= 0)
196 {
197 /* error */
198 goto finished;
199 }
200 lseek(fd, 0, SEEK_SET);
201 core_pin(ic->handle);
202 ic->bmp.data = core_get_data(ic->handle);
203 size_read = read_bmp_fd(fd, &ic->bmp, buf_size, bmpformat, NULL);
204 core_unpin(ic->handle);
205
206 if (size_read < 0)
207 { 187 {
208 /* error */ 188 ic->bmp.data = core_get_data(ic->handle);
209 size_read = 0;
210 }
211 /* free unused alpha channel, if any */
212 core_shrink(ic->handle, ic->bmp.data, size_read);
213
214 if (size_read == 0)
215 ic->handle = core_free(ic->handle);
216 else
217 ic->loaded = true; 189 ic->loaded = true;
218finished: 190 }
219 close(fd);
220 return;
221 } 191 }
222} 192}
223 193
diff --git a/apps/gui/skin_engine/skin_parser.c b/apps/gui/skin_engine/skin_parser.c
index ad5fa48756..9495e34f5b 100644
--- a/apps/gui/skin_engine/skin_parser.c
+++ b/apps/gui/skin_engine/skin_parser.c
@@ -1908,77 +1908,60 @@ static int buflib_move_callback(int handle, void* current, void* new)
1908 1908
1909 return BUFLIB_CB_OK; 1909 return BUFLIB_CB_OK;
1910} 1910}
1911static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
1912#endif 1911#endif
1913 1912
1914static int load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char* bmpdir) 1913static int load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char* bmpdir)
1915{ 1914{
1915 static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
1916 (void)wps_data; /* only needed for remote targets */ 1916 (void)wps_data; /* only needed for remote targets */
1917 char img_path[MAX_PATH]; 1917 char img_path[MAX_PATH];
1918 int fd; 1918
1919 int handle;
1920 get_image_filename(bitmap->data, bmpdir, 1919 get_image_filename(bitmap->data, bmpdir,
1921 img_path, sizeof(img_path)); 1920 img_path, sizeof(img_path));
1922 1921
1923 /* load the image */ 1922#ifdef __PCTOOL__ /* just check if image exists */
1924 int format; 1923 int fd = open(img_path, O_RDONLY);
1925#ifdef HAVE_REMOTE_LCD
1926 if (curr_screen == SCREEN_REMOTE)
1927 format = FORMAT_ANY|FORMAT_REMOTE;
1928 else
1929#endif
1930 format = FORMAT_ANY|FORMAT_TRANSPARENT;
1931
1932 fd = open(img_path, O_RDONLY);
1933 if (fd < 0) 1924 if (fd < 0)
1934 { 1925 {
1935 DEBUGF("Couldn't open %s\n", img_path); 1926 DEBUGF("Couldn't open %s\n", img_path);
1936 return fd; 1927 return fd;
1937 } 1928 }
1938#ifndef __PCTOOL__ 1929 close(fd);
1939 int buf_size = read_bmp_fd(fd, bitmap, 0, 1930 return 1;
1940 format|FORMAT_RETURN_SIZE, NULL); 1931#else /* load the image */
1941 if(buf_size < 0) 1932 int handle;
1942 { 1933 int bmpformat;
1943 close(fd); 1934 ssize_t buf_reqd;
1944 return buf_size; 1935#ifdef HAVE_REMOTE_LCD
1945 } 1936 if (curr_screen == SCREEN_REMOTE)
1937 bmpformat = FORMAT_ANY|FORMAT_REMOTE;
1938 else
1939#endif
1940 bmpformat = FORMAT_ANY|FORMAT_TRANSPARENT;
1946 1941
1947 handle = core_alloc_ex(bitmap->data, buf_size, &buflib_ops); 1942 handle = core_load_bmp(img_path, bitmap, bmpformat, &buf_reqd, &buflib_ops);
1948 if (handle <= 0) 1943 if (handle != CLB_ALOC_ERR)
1949 { 1944 {
1950 DEBUGF("Not enough skin buffer: need %zd more.\n", 1945 /* NOTE!: bitmap->data == NULL to force a crash later if the
1951 buf_size - skin_buffer_freespace()); 1946 caller doesnt call core_get_data() */
1952 close(fd); 1947 _stats->buflib_handles++;
1948 _stats->images_size += buf_reqd;
1953 return handle; 1949 return handle;
1954 } 1950 }
1955 _stats->buflib_handles++; 1951
1956 _stats->images_size += buf_size; 1952 if (buf_reqd == CLB_READ_ERR)
1957 lseek(fd, 0, SEEK_SET);
1958 core_pin(handle);
1959 bitmap->data = core_get_data(handle);
1960 int ret = read_bmp_fd(fd, bitmap, buf_size, format, NULL);
1961 bitmap->data = NULL; /* do this to force a crash later if the
1962 caller doesnt call core_get_data() */
1963 core_unpin(handle);
1964 close(fd);
1965 if (ret > 0)
1966 { 1953 {
1967 /* free unused alpha channel, if any */ 1954 /* Abort if we can't load an image */
1968 core_shrink(handle, core_get_data(handle), ret); 1955 DEBUGF("Couldn't load '%s' (%ld)\n", img_path, buf_reqd);
1969 return handle;
1970 } 1956 }
1971 else 1957 else
1972 { 1958 {
1973 /* Abort if we can't load an image */ 1959 DEBUGF("Not enough skin buffer: need %zd more.\n",
1974 DEBUGF("Couldn't load '%s'\n", img_path); 1960 buf_reqd - skin_buffer_freespace());
1975 core_free(handle);
1976 return -1;
1977 } 1961 }
1978#else /* !__PCTOOL__ */ 1962
1979 close(fd); 1963 return -1;
1980 return 1; 1964#endif/* !__PCTOOL__ */
1981#endif
1982} 1965}
1983 1966
1984static bool load_skin_bitmaps(struct wps_data *wps_data, char *bmpdir) 1967static bool load_skin_bitmaps(struct wps_data *wps_data, char *bmpdir)
diff --git a/apps/misc.c b/apps/misc.c
index f878bad833..36f45d46e0 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -99,6 +99,10 @@
99#endif 99#endif
100#endif 100#endif
101 101
102#ifndef PLUGIN
103#include "core_alloc.h" /*core_load_bmp()*/
104#endif
105
102#ifdef HAVE_HARDWARE_CLICK 106#ifdef HAVE_HARDWARE_CLICK
103#include "piezo.h" 107#include "piezo.h"
104#endif 108#endif
@@ -1572,4 +1576,67 @@ enum current_activity get_current_activity(void)
1572 return current_activity[current_activity_top?current_activity_top-1:0]; 1576 return current_activity[current_activity_top?current_activity_top-1:0];
1573} 1577}
1574 1578
1575#endif 1579/* core_load_bmp opens bitmp filename and allocates space for it
1580* you must set bm->data with the result from core_get_data(handle)
1581* you must also call core_free(handle) when finished with the bitmap
1582* returns handle, ALOC_ERR(0) on failure
1583* ** Extended error info truth table **
1584* [ Handle ][buf_reqd]
1585* [ > 0 ][ > 0 ] buf_reqd indicates how many bytes were used
1586* [ALOC_ERR][ > 0 ] buf_reqd indicates how many bytes are needed
1587* [ALOC_ERR][READ_ERR] there was an error reading the file or it is empty
1588*/
1589int core_load_bmp(const char * filename, struct bitmap *bm, const int bmformat,
1590 ssize_t *buf_reqd, struct buflib_callbacks *ops)
1591{
1592 ssize_t buf_size;
1593 ssize_t size_read = 0;
1594 int handle = CLB_ALOC_ERR;
1595
1596 int fd = open(filename, O_RDONLY);
1597 *buf_reqd = CLB_READ_ERR;
1598
1599 if (fd < 0) /* Exit if file opening failed */
1600 {
1601 DEBUGF("read_bmp_file: can't open '%s', rc: %d\n", filename, fd);
1602 return CLB_ALOC_ERR;
1603 }
1604
1605 buf_size = read_bmp_fd(fd, bm, 0, bmformat|FORMAT_RETURN_SIZE, NULL);
1606
1607 if (buf_size > 0)
1608 {
1609
1610 handle = core_alloc_ex(filename, (size_t) buf_size, ops);
1611
1612 if (handle > 0)
1613 {
1614 core_pin(handle);
1615 bm->data = core_get_data(handle);
1616 lseek(fd, 0, SEEK_SET); /* reset to beginning of file */
1617 size_read = read_bmp_fd(fd, bm, buf_size, bmformat, NULL);
1618
1619 if (size_read > 0) /* free unused alpha channel, if any */
1620 {
1621 core_shrink(handle, bm->data, size_read);
1622 *buf_reqd = size_read;
1623 }
1624 bm->data = NULL; /* do this to force a crash later if the
1625 caller doesnt call core_get_data() */
1626 core_unpin(handle);
1627 }
1628 else
1629 *buf_reqd = buf_size; /* couldn't allocate pass bytes needed */
1630
1631 if (size_read <= 0)
1632 {
1633 /* error reading file */
1634 core_free(handle); /* core_free() ignores free handles (<= 0) */
1635 handle = CLB_ALOC_ERR;
1636 }
1637 }
1638
1639 close(fd);
1640 return handle;
1641}
1642#endif /* ndef __PCTOOL__ */
diff --git a/apps/misc.h b/apps/misc.h
index e2e856f212..df2c649b0e 100644
--- a/apps/misc.h
+++ b/apps/misc.h
@@ -222,4 +222,15 @@ enum current_activity get_current_activity(void);
222/* format a sound value like: -1.05 dB */ 222/* format a sound value like: -1.05 dB */
223int format_sound_value(char *buf, size_t len, int snd, int val); 223int format_sound_value(char *buf, size_t len, int snd, int val);
224 224
225#ifndef PLUGIN
226enum core_load_bmp_error
227{
228 CLB_ALOC_ERR = 0,
229 CLB_READ_ERR = -1,
230};
231struct buflib_callbacks;
232int core_load_bmp(const char *filename, struct bitmap *bm, const int bmformat,
233 ssize_t *buf_reqd, struct buflib_callbacks *ops);
234#endif
235
225#endif /* MISC_H */ 236#endif /* MISC_H */