* read_bmp_*(): add FORMAT_RETURN_SIZE

* Lua: add luaL_checkboolean() & luaL_optboolean()
* Lua: add read_bmp_file


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21074 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2009-05-25 11:12:27 +00:00
parent b0e0ec832b
commit aec37aa5fe
5 changed files with 64 additions and 5 deletions

View File

@ -200,6 +200,19 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
}
LUALIB_API int luaL_checkboolean (lua_State *L, int narg) {
int b = lua_toboolean(L, narg);
if( b == 0 && !lua_isboolean(L, narg))
tag_error(L, narg, LUA_TBOOLEAN);
return b;
}
LUALIB_API int luaL_optboolean (lua_State *L, int narg, int def) {
return luaL_opt(L, luaL_checkboolean, narg, def);
}
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
if (!lua_getmetatable(L, obj)) /* no metatable? */
return 0;

View File

@ -58,6 +58,10 @@ LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
lua_Integer def);
LUALIB_API int (luaL_checkboolean) (lua_State *L, int numArg);
LUALIB_API int (luaL_optboolean) (lua_State *L, int nArg,
int def);
LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
LUALIB_API void (luaL_checkany) (lua_State *L, int narg);

View File

@ -69,11 +69,9 @@ static void rli_wrap(lua_State *L, fb_data *src, int width, int height)
a->data = src;
}
static int rli_new(lua_State *L)
static fb_data* rli_alloc(lua_State *L, int width, int height)
{
int width = luaL_checkint(L, 1);
int height = luaL_checkint(L, 2);
size_t nbytes = sizeof(struct rocklua_image) + (width - 1)*(height - 1)*sizeof(fb_data);
size_t nbytes = sizeof(struct rocklua_image) + ((width*height) - 1) * sizeof(fb_data);
struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, nbytes);
luaL_getmetatable(L, ROCKLUA_IMAGE);
@ -82,6 +80,17 @@ static int rli_new(lua_State *L)
a->width = width;
a->height = height;
a->data = &a->dummy[0][0];
return a->data;
}
static int rli_new(lua_State *L)
{
int width = luaL_checkint(L, 1);
int height = luaL_checkint(L, 2);
rli_alloc(L, width, height);
return 1;
}
@ -450,7 +459,7 @@ RB_WRAP(current_tick)
RB_WRAP(button_get)
{
bool block = lua_toboolean(L, 1);
bool block = luaL_checkboolean(L, 1);
long result = rb->button_get(block);
lua_pushinteger(L, result);
return 1;
@ -714,6 +723,33 @@ RB_WRAP(lcd_rgbunpack)
}
#endif
RB_WRAP(read_bmp_file)
{
struct bitmap bm;
const char* filename = luaL_checkstring(L, 1);
bool dither = luaL_optboolean(L, 2, true);
bool transparent = luaL_optboolean(L, 3, false);
int format = FORMAT_NATIVE;
if(dither)
format |= FORMAT_DITHER;
if(transparent)
format |= FORMAT_TRANSPARENT;
int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL);
if(result > 0)
{
bm.data = (unsigned char*) rli_alloc(L, bm.width, bm.height);
rb->read_bmp_file(filename, &bm, result, format, NULL);
return 1;
}
return 0;
}
#define R(NAME) {#NAME, rock_##NAME}
static const luaL_Reg rocklib[] =
{
@ -800,6 +836,7 @@ static const luaL_Reg rocklib[] =
#endif
R(font_getstringsize),
R(read_bmp_file),
{"new_image", rli_new},

View File

@ -460,6 +460,7 @@ int read_bmp_fd(int fd,
int read_width;
int depth, numcolors, compression, totalsize;
int ret;
bool return_size = format & FORMAT_RETURN_SIZE;
unsigned char *bitmap = bm->data;
struct uint8_rgb palette[256];
@ -595,6 +596,9 @@ int read_bmp_fd(int fd,
else
totalsize = BM_SIZE(bm->width,bm->height,format,remote);
if(return_size)
return totalsize;
/* Check if this fits the buffer */
if (totalsize > maxsize) {
DEBUGF("read_bmp_fd: Bitmap too large for buffer: "

View File

@ -388,6 +388,7 @@ enum
#define FORMAT_REMOTE 0x10000000
#define FORMAT_RESIZE 0x08000000
#define FORMAT_KEEP_ASPECT 0x04000000
#define FORMAT_RETURN_SIZE 0x02000000
#define TRANSPARENT_COLOR LCD_RGBPACK(255,0,255)
#define REPLACEWITHFG_COLOR LCD_RGBPACK(0,255,255)