mirror of
https://github.com/love2d/love.git
synced 2026-08-20 12:40:20 +02:00
Updated CompressedData to store its data in a single block of memory, like other Data objects
This commit is contained in:
@@ -27,6 +27,8 @@ namespace image
|
|||||||
|
|
||||||
CompressedData::CompressedData()
|
CompressedData::CompressedData()
|
||||||
: format(FORMAT_UNKNOWN)
|
: format(FORMAT_UNKNOWN)
|
||||||
|
, data(0)
|
||||||
|
, dataSize(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,23 +38,12 @@ CompressedData::~CompressedData()
|
|||||||
|
|
||||||
int CompressedData::getSize() const
|
int CompressedData::getSize() const
|
||||||
{
|
{
|
||||||
// Adding up the total size for all mipmap levels would make more sense, but
|
return int(dataSize);
|
||||||
// it's probably better for getSize() to match getData() so no bad memory
|
|
||||||
// accesses happen...
|
|
||||||
if (dataImages.size() > 0)
|
|
||||||
return dataImages[0].size;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *CompressedData::getData() const
|
void *CompressedData::getData() const
|
||||||
{
|
{
|
||||||
// Data for different mipmap levels is not stored contiguously in memory, so
|
return data;
|
||||||
// getData() won't work properly for CompressedData.
|
|
||||||
if (dataImages.size() > 0 && dataImages[0].size > 0)
|
|
||||||
return (void *) dataImages[0].data;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CompressedData::getMipmapCount() const
|
int CompressedData::getMipmapCount() const
|
||||||
@@ -64,7 +55,7 @@ int CompressedData::getSize(int miplevel) const
|
|||||||
{
|
{
|
||||||
checkMipmapLevelExists(miplevel);
|
checkMipmapLevelExists(miplevel);
|
||||||
|
|
||||||
return dataImages[miplevel].size;
|
return int(dataImages[miplevel].size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *CompressedData::getData(int miplevel) const
|
void *CompressedData::getData(int miplevel) const
|
||||||
|
|||||||
@@ -63,15 +63,13 @@ public:
|
|||||||
{
|
{
|
||||||
int width, height;
|
int width, height;
|
||||||
size_t size;
|
size_t size;
|
||||||
uint8 *data;
|
uint8 *data; // Should not have ownership of the data.
|
||||||
};
|
};
|
||||||
|
|
||||||
CompressedData();
|
CompressedData();
|
||||||
virtual ~CompressedData();
|
virtual ~CompressedData();
|
||||||
|
|
||||||
// Implements Data. Note that data for different mipmap levels is not always
|
// Implements Data.
|
||||||
// stored contiguously in memory, so getData() and getSize() don't make
|
|
||||||
// much sense. Use getData(miplevel) and getSize(mipleveL) instead.
|
|
||||||
virtual void *getData() const;
|
virtual void *getData() const;
|
||||||
virtual int getSize() const;
|
virtual int getSize() const;
|
||||||
|
|
||||||
@@ -113,6 +111,10 @@ protected:
|
|||||||
|
|
||||||
Format format;
|
Format format;
|
||||||
|
|
||||||
|
// Single block of memory containing all of the sub-images.
|
||||||
|
uint8 *data;
|
||||||
|
size_t dataSize;
|
||||||
|
|
||||||
// Texture info for each mipmap level.
|
// Texture info for each mipmap level.
|
||||||
std::vector<SubImage> dataImages;
|
std::vector<SubImage> dataImages;
|
||||||
|
|
||||||
|
|||||||
@@ -29,47 +29,56 @@ namespace image
|
|||||||
namespace magpie
|
namespace magpie
|
||||||
{
|
{
|
||||||
|
|
||||||
CompressedData::CompressedData(love::filesystem::FileData *data)
|
CompressedData::CompressedData(love::filesystem::FileData *filedata)
|
||||||
{
|
{
|
||||||
load(data);
|
load(filedata);
|
||||||
}
|
}
|
||||||
|
|
||||||
CompressedData::~CompressedData()
|
CompressedData::~CompressedData()
|
||||||
{
|
{
|
||||||
// We have ownership of the heap memory in dataImages, so we have to free it.
|
delete[] data;
|
||||||
for (size_t i = 0; i < dataImages.size(); i++)
|
|
||||||
delete[] dataImages[i].data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompressedData::load(love::filesystem::FileData *data)
|
void CompressedData::load(love::filesystem::FileData *filedata)
|
||||||
{
|
{
|
||||||
// SubImage vector will be populated by a parser.
|
// SubImage vector will be populated by a parser.
|
||||||
std::vector<SubImage> parsedimages;
|
std::vector<SubImage> parsedimages;
|
||||||
Format texformat = FORMAT_UNKNOWN;
|
Format texformat = FORMAT_UNKNOWN;
|
||||||
|
|
||||||
if (ddsHandler::canParse(data))
|
uint8 *newdata = 0;
|
||||||
texformat = ddsHandler::parse(data, parsedimages);
|
size_t newdata_size = 0;
|
||||||
|
|
||||||
|
if (ddsHandler::canParse(filedata))
|
||||||
|
newdata = ddsHandler::parse(filedata, parsedimages, newdata_size, texformat);
|
||||||
|
|
||||||
|
if (newdata == 0)
|
||||||
|
throw love::Exception("Could not parse compressed data.");
|
||||||
|
|
||||||
if (texformat == FORMAT_UNKNOWN)
|
if (texformat == FORMAT_UNKNOWN)
|
||||||
|
{
|
||||||
|
delete[] newdata;
|
||||||
throw love::Exception("Could not parse compressed data: Unknown format.");
|
throw love::Exception("Could not parse compressed data: Unknown format.");
|
||||||
|
}
|
||||||
|
|
||||||
if (parsedimages.size() == 0)
|
if (parsedimages.size() == 0 || newdata_size == 0)
|
||||||
|
{
|
||||||
|
delete[] newdata;
|
||||||
throw love::Exception("Could not parse compressed data: No valid data?");
|
throw love::Exception("Could not parse compressed data: No valid data?");
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure to clean up any previously loaded data.
|
// Make sure to clean up any previously loaded data.
|
||||||
for (size_t i = 0; i < dataImages.size(); i++)
|
delete[] data;
|
||||||
{
|
|
||||||
delete[] dataImages[i].data;
|
data = newdata;
|
||||||
dataImages[i].data = 0;
|
dataSize = newdata_size;
|
||||||
}
|
|
||||||
|
|
||||||
dataImages = parsedimages;
|
dataImages = parsedimages;
|
||||||
format = texformat;
|
format = texformat;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CompressedData::isCompressed(love::filesystem::FileData *data)
|
bool CompressedData::isCompressed(love::filesystem::FileData *filedata)
|
||||||
{
|
{
|
||||||
if (ddsHandler::canParse(data))
|
if (ddsHandler::canParse(filedata))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -36,14 +36,14 @@ class CompressedData : public love::image::CompressedData
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
CompressedData(love::filesystem::FileData *data);
|
CompressedData(love::filesystem::FileData *filedata);
|
||||||
virtual ~CompressedData();
|
virtual ~CompressedData();
|
||||||
|
|
||||||
static bool isCompressed(love::filesystem::FileData *data);
|
static bool isCompressed(love::filesystem::FileData *filedata);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void load(love::filesystem::FileData *data);
|
void load(love::filesystem::FileData *filedata);
|
||||||
|
|
||||||
}; // CompressedData
|
}; // CompressedData
|
||||||
|
|
||||||
|
|||||||
@@ -40,17 +40,21 @@ bool ddsHandler::canParse(const filesystem::FileData *data)
|
|||||||
return dds::isCompressedDDS(data->getData(), data->getSize());
|
return dds::isCompressedDDS(data->getData(), data->getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
CompressedData::Format ddsHandler::parse(filesystem::FileData *data, std::vector<CompressedData::SubImage> &images)
|
uint8 *ddsHandler::parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format)
|
||||||
{
|
{
|
||||||
if (!dds::isDDS(data->getData(), data->getSize()))
|
if (!dds::isDDS(filedata->getData(), filedata->getSize()))
|
||||||
throw love::Exception("Could not decode compressed data (not a DDS file?)");
|
throw love::Exception("Could not decode compressed data (not a DDS file?)");
|
||||||
|
|
||||||
CompressedData::Format texformat = CompressedData::FORMAT_UNKNOWN;
|
CompressedData::Format texformat = CompressedData::FORMAT_UNKNOWN;
|
||||||
|
|
||||||
|
uint8 *data = 0;
|
||||||
|
dataSize = 0;
|
||||||
|
images.clear();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Attempt to parse the dds file.
|
// Attempt to parse the dds file.
|
||||||
dds::Parser parser(data->getData(), data->getSize());
|
dds::Parser parser(filedata->getData(), filedata->getSize());
|
||||||
|
|
||||||
texformat = convertFormat(parser.getFormat());
|
texformat = convertFormat(parser.getFormat());
|
||||||
|
|
||||||
@@ -60,6 +64,17 @@ CompressedData::Format ddsHandler::parse(filesystem::FileData *data, std::vector
|
|||||||
if (parser.getMipmapCount() == 0)
|
if (parser.getMipmapCount() == 0)
|
||||||
throw love::Exception("Could not parse compressed data: No readable texture data.");
|
throw love::Exception("Could not parse compressed data: No readable texture data.");
|
||||||
|
|
||||||
|
// Calculate the size of the block of memory we're returning.
|
||||||
|
for (size_t i = 0; i < parser.getMipmapCount(); i++)
|
||||||
|
{
|
||||||
|
const dds::Image *img = parser.getImageData(i);
|
||||||
|
dataSize += img->dataSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
data = new uint8[dataSize];
|
||||||
|
|
||||||
|
size_t dataOffset = 0;
|
||||||
|
|
||||||
// Copy the parsed mipmap levels from the FileData to our CompressedData.
|
// Copy the parsed mipmap levels from the FileData to our CompressedData.
|
||||||
for (size_t i = 0; i < parser.getMipmapCount(); i++)
|
for (size_t i = 0; i < parser.getMipmapCount(); i++)
|
||||||
{
|
{
|
||||||
@@ -72,23 +87,24 @@ CompressedData::Format ddsHandler::parse(filesystem::FileData *data, std::vector
|
|||||||
mip.height = img->height;
|
mip.height = img->height;
|
||||||
mip.size = img->dataSize;
|
mip.size = img->dataSize;
|
||||||
|
|
||||||
// Copy the mipmap image from the FileData.
|
// Copy the mipmap image from the FileData to our block of memory.
|
||||||
mip.data = new uint8[mip.size];
|
memcpy(data + dataOffset, img->data, mip.size);
|
||||||
memcpy(mip.data, img->data, mip.size);
|
mip.data = data + dataOffset;
|
||||||
|
|
||||||
|
dataOffset += mip.size;
|
||||||
|
|
||||||
images.push_back(mip);
|
images.push_back(mip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
// Clean up any newly allocated heap memory before throwing.
|
delete[] data;
|
||||||
for (size_t i = 0; i < images.size(); i++)
|
images.clear();
|
||||||
delete[] images[i].data;
|
throw love::Exception("%s", e.what());
|
||||||
|
|
||||||
throw love::Exception(e.what());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return texformat;
|
format = texformat;
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
CompressedData::Format ddsHandler::convertFormat(dds::Format ddsformat)
|
CompressedData::Format ddsHandler::convertFormat(dds::Format ddsformat)
|
||||||
|
|||||||
@@ -54,13 +54,18 @@ public:
|
|||||||
static bool canParse(const filesystem::FileData *data);
|
static bool canParse(const filesystem::FileData *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses Compressed image data into a list of sub-images.
|
* Parses compressed image filedata into a list of sub-images and returns
|
||||||
* @param[in] data The data to parse.
|
* a single block of memory containing all the images.
|
||||||
* @param[out] images The list of sub-images (including byte data for each)
|
*
|
||||||
* parsed from the file data.
|
* @param[in] filedata The data to parse.
|
||||||
* @return The format of the CompressedData.
|
* @param[out] image The list of sub-images generated. Byte data is a pointer
|
||||||
|
* to the returned data.
|
||||||
|
* @param[out] dataSize The total size in bytes of the returned data.
|
||||||
|
* @param[out] format The format of the Compressed Data.
|
||||||
|
*
|
||||||
|
* @return The single block of memory containing the parsed images.
|
||||||
**/
|
**/
|
||||||
static CompressedData::Format parse(filesystem::FileData *data, std::vector<CompressedData::SubImage> &images);
|
static uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -31,62 +31,6 @@ CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
|
|||||||
return luax_checktype<CompressedData>(L, idx, "CompressedData", IMAGE_COMPRESSED_DATA_T);
|
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->getMipmapCount(); 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->getMipmapCount(); 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->getMipmapCount(); i++)
|
|
||||||
totalsize += t->getSize(i);
|
|
||||||
|
|
||||||
lua_pushnumber(L, (lua_Number) totalsize);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int w_CompressedData_getWidth(lua_State *L)
|
int w_CompressedData_getWidth(lua_State *L)
|
||||||
{
|
{
|
||||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||||
@@ -165,9 +109,8 @@ int w_CompressedData_getFormat(lua_State *L)
|
|||||||
static const luaL_Reg functions[] =
|
static const luaL_Reg functions[] =
|
||||||
{
|
{
|
||||||
// Data
|
// Data
|
||||||
// CompressedData's data is not in contiguous memory, so it needs custom functions.
|
{ "getString", w_Data_getString },
|
||||||
{ "getString", w_CompressedData_getString },
|
{ "getSize", w_Data_getSize },
|
||||||
{ "getSize", w_CompressedData_getSize },
|
|
||||||
|
|
||||||
{ "getWidth", w_CompressedData_getWidth },
|
{ "getWidth", w_CompressedData_getWidth },
|
||||||
{ "getHeight", w_CompressedData_getHeight },
|
{ "getHeight", w_CompressedData_getHeight },
|
||||||
|
|||||||
@@ -31,8 +31,6 @@ namespace image
|
|||||||
{
|
{
|
||||||
|
|
||||||
CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
|
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_getWidth(lua_State *L);
|
||||||
int w_CompressedData_getHeight(lua_State *L);
|
int w_CompressedData_getHeight(lua_State *L);
|
||||||
int w_CompressedData_getDimensions(lua_State *L);
|
int w_CompressedData_getDimensions(lua_State *L);
|
||||||
|
|||||||
Reference in New Issue
Block a user