diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lua5/cdlua5.c | 70 | ||||
-rw-r--r-- | src/lua5/cdluaim5.c | 43 |
2 files changed, 82 insertions, 31 deletions
diff --git a/src/lua5/cdlua5.c b/src/lua5/cdlua5.c index bff00df..954389c 100644 --- a/src/lua5/cdlua5.c +++ b/src/lua5/cdlua5.c @@ -782,6 +782,13 @@ static int cdlua5_newindexchannel(lua_State *L) return 0; } +static int cdl_ischar(const char* str, char c) +{ + if ((str[0] == c || str[0] == c-32) && str[1] == 0) + return 1; + return 0; +} + /***************************************************************************\ * imagergb "gettable" fallback. This fallback is called when a LUA line * * like "c = imagergb.r[y*w + x]" or "imagergb.r[y*w + x] = c" is executed. * @@ -794,16 +801,22 @@ static int cdlua5_indeximagergb(lua_State *L) cdluaImageRGB* imagergb_p = cdlua_checkimagergb(L, 1); const char *index_s = luaL_checkstring(L, 2); - if (*index_s == 'r' || *index_s == 'R') + if (cdl_ischar(index_s, 'r')) channel = imagergb_p->red; - else if (*index_s == 'g' || *index_s == 'G') + else if (cdl_ischar(index_s, 'g')) channel = imagergb_p->green; - else if (*index_s == 'b' || *index_s == 'B') + else if (cdl_ischar(index_s, 'b')) channel = imagergb_p->blue; - else - luaL_argerror(L, 2, "index is an invalid channel name"); - cdlua_pushchannel(L, channel, imagergb_p->size); + if (channel) + cdlua_pushchannel(L, channel, imagergb_p->size); + else + { + /* get raw method */ + lua_getmetatable(L, 1); + lua_pushvalue(L, 2); + lua_rawget(L, -2); + } return 1; } @@ -820,18 +833,24 @@ static int cdlua5_indeximagergba(lua_State *L) cdluaImageRGBA* imagergba_p = cdlua_checkimagergba(L, 1); const char *index_s = luaL_checkstring(L, 2); - if (*index_s == 'r' || *index_s == 'R') + if (cdl_ischar(index_s, 'r')) channel = imagergba_p->red; - else if (*index_s == 'g' || *index_s == 'G') + else if (cdl_ischar(index_s, 'g')) channel = imagergba_p->green; - else if (*index_s == 'b' || *index_s == 'B') + else if (cdl_ischar(index_s, 'b')) channel = imagergba_p->blue; - else if (*index_s == 'a' || *index_s == 'A') + else if (cdl_ischar(index_s, 'a')) channel = imagergba_p->alpha; - else - luaL_argerror(L, 2, "index is an invalid channel name"); - cdlua_pushchannel(L, channel, imagergba_p->size); + if (channel) + cdlua_pushchannel(L, channel, imagergba_p->size); + else + { + /* get raw method */ + lua_getmetatable(L, 1); + lua_pushvalue(L, 2); + lua_rawget(L, -2); + } return 1; } @@ -845,31 +864,36 @@ static int cdlua5_indeximagergba(lua_State *L) static int cdlua5_indexbitmap(lua_State *L) { unsigned char* channel = NULL; - cdBitmap* bitmap = cdlua_checkbitmap(L, 1); const char *index_s = luaL_checkstring(L, 2); int size = bitmap->w * bitmap->h; - if (*index_s == 'r' || *index_s == 'R') + if (cdl_ischar(index_s, 'r')) channel = cdBitmapGetData(bitmap, CD_IRED); - else if (*index_s == 'g' || *index_s == 'G') + else if (cdl_ischar(index_s, 'g')) channel = cdBitmapGetData(bitmap, CD_IGREEN); - else if (*index_s == 'b' || *index_s == 'B') + else if (cdl_ischar(index_s, 'b')) channel = cdBitmapGetData(bitmap, CD_IBLUE); - else if (*index_s == 'a' || *index_s == 'A') + else if (cdl_ischar(index_s, 'a')) channel = cdBitmapGetData(bitmap, CD_IALPHA); - else if (*index_s == 'i' || *index_s == 'I') + else if (cdl_ischar(index_s, 'i')) channel = cdBitmapGetData(bitmap, CD_INDEX); - else if (*index_s == 'c' || *index_s == 'C') + else if (cdl_ischar(index_s, 'c')) { channel = cdBitmapGetData(bitmap, CD_COLORS); size = -1; } - else - luaL_argerror(L, 2, "index is an invalid channel name"); - cdlua_pushchannel(L, channel, size); + if (channel) + cdlua_pushchannel(L, channel, size); + else + { + /* get raw method */ + lua_getmetatable(L, 1); + lua_pushvalue(L, 2); + lua_rawget(L, -2); + } return 1; } diff --git a/src/lua5/cdluaim5.c b/src/lua5/cdluaim5.c index 77ffc4f..815cd0f 100644 --- a/src/lua5/cdluaim5.c +++ b/src/lua5/cdluaim5.c @@ -37,7 +37,12 @@ static int imlua_cdInitBitmap(lua_State *L) luaL_argerror(L, 1, "image is not a bitmap"); if (image->color_space == IM_RGB) - bitmap = cdInitBitmap(image->width, image->height, CD_RGB, image->data[0], image->data[1], image->data[2]); + { + if (image->has_alpha) + bitmap = cdInitBitmap(image->width, image->height, CD_RGBA, image->data[0], image->data[1], image->data[2], image->data[3]); + else + bitmap = cdInitBitmap(image->width, image->height, CD_RGB, image->data[0], image->data[1], image->data[2]); + } else bitmap = cdInitBitmap(image->width, image->height, CD_MAP, image->data[0], image->palette); @@ -60,7 +65,12 @@ static int imlua_cdCreateBitmap(lua_State *L) luaL_argerror(L, 1, "image is not a bitmap"); if (image->color_space == IM_RGB) - bitmap = cdCreateBitmap(image->width, image->height, CD_RGB); + { + if (image->has_alpha) + bitmap = cdCreateBitmap(image->width, image->height, CD_RGBA); + else + bitmap = cdCreateBitmap(image->width, image->height, CD_RGB); + } else bitmap = cdCreateBitmap(image->width, image->height, CD_MAP); @@ -72,6 +82,9 @@ static int imlua_cdCreateBitmap(lua_State *L) memcpy(cdBitmapGetData(bitmap, CD_IRED), image->data[0], image->plane_size); memcpy(cdBitmapGetData(bitmap, CD_IGREEN), image->data[1], image->plane_size); memcpy(cdBitmapGetData(bitmap, CD_IBLUE), image->data[2], image->plane_size); + + if (image->has_alpha) + memcpy(cdBitmapGetData(bitmap, CD_IALPHA), image->data[3], image->plane_size); } else { @@ -91,7 +104,7 @@ static int cdlua_imImageCreate(lua_State *L) imImage *image; cdBitmap* bitmap = cdlua_checkbitmap(L, 1); - if (bitmap->type == CD_RGB) + if (bitmap->type == CD_RGB || bitmap->type == CD_RGBA) image = imImageCreate(bitmap->w, bitmap->h, IM_RGB, IM_BYTE); else image = imImageCreate(bitmap->w, bitmap->h, IM_MAP, IM_BYTE); @@ -99,11 +112,14 @@ static int cdlua_imImageCreate(lua_State *L) if (!image) luaL_error(L, "insuficient memory to create image"); - if (bitmap->type == CD_RGB) + if (bitmap->type == CD_RGB || bitmap->type == CD_RGBA) { memcpy(image->data[0], cdBitmapGetData(bitmap, CD_IRED), image->plane_size); memcpy(image->data[1], cdBitmapGetData(bitmap, CD_IGREEN), image->plane_size); memcpy(image->data[2], cdBitmapGetData(bitmap, CD_IBLUE), image->plane_size); + + if (bitmap->type == CD_RGBA) + memcpy(image->data[3], cdBitmapGetData(bitmap, CD_IALPHA), image->plane_size); } else { @@ -192,16 +208,27 @@ static int imlua_cdCreateCanvas(lua_State * L) imImage *image = imlua_checkimage(L, 1); + if (image->color_space != IM_RGB || image->data_type != IM_BYTE) + luaL_argerror(L, 1, "image is not RGB/byte"); + if (lua_isnoneornil(L, 2)) { - sprintf(data_s, "%dx%d %p %p %p", image->width, image->height, - image->data[0], image->data[1], image->data[2]); + if (image->has_alpha) + sprintf(data_s, "%dx%d %p %p %p %p -a", image->width, image->height, + image->data[0], image->data[1], image->data[2], image->data[3]); + else + sprintf(data_s, "%dx%d %p %p %p", image->width, image->height, + image->data[0], image->data[1], image->data[2]); } else { double res_f = luaL_checknumber(L, 2); - sprintf(data_s, "%dx%d %p %p %p -r%g", image->width, image->height, - image->data[0], image->data[1], image->data[2], res_f); + if (image->has_alpha) + sprintf(data_s, "%dx%d %p %p %p %p -r%g -a", image->width, image->height, + image->data[0], image->data[1], image->data[2], image->data[3], res_f); + else + sprintf(data_s, "%dx%d %p %p %p -r%g", image->width, image->height, + image->data[0], image->data[1], image->data[2], res_f); } canvas = cdCreateCanvas(CD_IMAGERGB, data_s); |