Added the getString method to all Data objects (issue #608)

This commit is contained in:
Alex Szpakowski
2013-04-26 22:35:01 -07:00
parent 16f3a93f28
commit fb9f8d2962
8 changed files with 73 additions and 12 deletions
@@ -50,8 +50,8 @@ int w_FileData_getExtension(lua_State *L)
static const luaL_Reg w_FileData_functions[] =
{
// Data
{ "getString", w_Data_getString },
{ "getSize", w_Data_getSize },
{ "getFilename", w_FileData_getFilename },
+59 -1
View File
@@ -31,6 +31,62 @@ CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
return luax_checktype<CompressedData>(L, idx, "CompressedData", IMAGE_COMPRESSED_DATA_T);
}
int w_CompressedData_getString(lua_State *L)
{
CompressedData *t = luax_checkcompresseddata(L, 1);
// CompressedData's data isn't contiguous in memory, so we need to copy it
// all to a single block before sending the string.
size_t totalsize = 0;
for (int i = 0; i < t->getNumMipmaps(); i++)
totalsize += t->getSize(i);
if (totalsize == 0)
{
lua_pushstring(L, "");
return 1;
}
char *datastr = 0;
try
{
datastr = new char[totalsize];
}
catch (std::exception &)
{
return luaL_error(L, "Out of memory.");
}
size_t curpos = 0;
for (int i = 0; i < t->getNumMipmaps(); i++)
{
memcpy(&datastr[curpos], t->getData(i), t->getSize(i));
curpos += t->getSize(i);
}
lua_pushlstring(L, datastr, totalsize);
delete[] datastr;
return 1;
}
int w_CompressedData_getSize(lua_State *L)
{
CompressedData *t = luax_checkcompresseddata(L, 1);
size_t totalsize = 0;
for (int i = 0; i < t->getNumMipmaps(); i++)
totalsize += t->getSize(i);
lua_pushnumber(L, (lua_Number) totalsize);
return 1;
}
int w_CompressedData_getWidth(lua_State *L)
{
CompressedData *t = luax_checkcompresseddata(L, 1);
@@ -109,7 +165,9 @@ int w_CompressedData_getType(lua_State *L)
static const luaL_Reg functions[] =
{
// Data
{ "getSize", w_Data_getSize },
// CompressedData's data is not in contiguous memory, so it needs custom functions.
{ "getString", w_CompressedData_getString },
{ "getSize", w_CompressedData_getSize },
{ "getWidth", w_CompressedData_getWidth },
{ "getHeight", w_CompressedData_getHeight },
+2
View File
@@ -31,6 +31,8 @@ namespace image
{
CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
int w_CompressedData_getString(lua_State *L);
int w_CompressedData_getSize(lua_State *L);
int w_CompressedData_getWidth(lua_State *L);
int w_CompressedData_getHeight(lua_State *L);
int w_CompressedData_getDimensions(lua_State *L);
+1 -8
View File
@@ -131,13 +131,6 @@ int w_ImageData_mapPixel(lua_State *L)
return 0;
}
int w_ImageData_getString(lua_State *L)
{
ImageData *t = luax_checkimagedata(L, 1);
lua_pushlstring(L, (const char *)t->getData(), t->getSize());
return 1;
}
int w_ImageData_paste(lua_State *L)
{
ImageData *t = luax_checkimagedata(L, 1);
@@ -191,6 +184,7 @@ int w_ImageData_encode(lua_State *L)
static const luaL_Reg functions[] =
{
// Data
{ "getString", w_Data_getString },
{ "getSize", w_Data_getSize },
{ "getWidth", w_ImageData_getWidth },
@@ -199,7 +193,6 @@ static const luaL_Reg functions[] =
{ "getPixel", w_ImageData_getPixel },
{ "setPixel", w_ImageData_setPixel },
{ "mapPixel", w_ImageData_mapPixel },
{ "getString", w_ImageData_getString },
{ "paste", w_ImageData_paste },
{ "encode", w_ImageData_encode },
{ 0, 0 }
-1
View File
@@ -37,7 +37,6 @@ int w_ImageData_getDimensions(lua_State *L);
int w_ImageData_getPixel(lua_State *L);
int w_ImageData_setPixel(lua_State *L);
int w_ImageData_mapPixel(lua_State *L);
int w_ImageData_getString(lua_State *L);
int w_ImageData_paste(lua_State *L);
int w_ImageData_encode(lua_State *L);
extern "C" int luaopen_imagedata(lua_State *L);
+1 -1
View File
@@ -86,8 +86,8 @@ int w_SoundData_getSample(lua_State *L)
static const luaL_Reg functions[] =
{
// Data
{ "getString", w_Data_getString },
{ "getSize", w_Data_getSize },
{ "getChannels", w_SoundData_getChannels },