mirror of
https://github.com/love2d/love.git
synced 2026-08-17 19:23:38 +02:00
Updated CompressedData to store its data in a single block of memory, like other Data objects
This commit is contained in:
@@ -29,47 +29,56 @@ namespace image
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
CompressedData::CompressedData(love::filesystem::FileData *data)
|
||||
CompressedData::CompressedData(love::filesystem::FileData *filedata)
|
||||
{
|
||||
load(data);
|
||||
load(filedata);
|
||||
}
|
||||
|
||||
CompressedData::~CompressedData()
|
||||
{
|
||||
// We have ownership of the heap memory in dataImages, so we have to free it.
|
||||
for (size_t i = 0; i < dataImages.size(); i++)
|
||||
delete[] dataImages[i].data;
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
void CompressedData::load(love::filesystem::FileData *data)
|
||||
void CompressedData::load(love::filesystem::FileData *filedata)
|
||||
{
|
||||
// SubImage vector will be populated by a parser.
|
||||
std::vector<SubImage> parsedimages;
|
||||
Format texformat = FORMAT_UNKNOWN;
|
||||
|
||||
if (ddsHandler::canParse(data))
|
||||
texformat = ddsHandler::parse(data, parsedimages);
|
||||
uint8 *newdata = 0;
|
||||
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)
|
||||
{
|
||||
delete[] newdata;
|
||||
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?");
|
||||
}
|
||||
|
||||
// Make sure to clean up any previously loaded data.
|
||||
for (size_t i = 0; i < dataImages.size(); i++)
|
||||
{
|
||||
delete[] dataImages[i].data;
|
||||
dataImages[i].data = 0;
|
||||
}
|
||||
delete[] data;
|
||||
|
||||
data = newdata;
|
||||
dataSize = newdata_size;
|
||||
|
||||
dataImages = parsedimages;
|
||||
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 false;
|
||||
|
||||
@@ -36,14 +36,14 @@ class CompressedData : public love::image::CompressedData
|
||||
{
|
||||
public:
|
||||
|
||||
CompressedData(love::filesystem::FileData *data);
|
||||
CompressedData(love::filesystem::FileData *filedata);
|
||||
virtual ~CompressedData();
|
||||
|
||||
static bool isCompressed(love::filesystem::FileData *data);
|
||||
static bool isCompressed(love::filesystem::FileData *filedata);
|
||||
|
||||
private:
|
||||
|
||||
void load(love::filesystem::FileData *data);
|
||||
void load(love::filesystem::FileData *filedata);
|
||||
|
||||
}; // CompressedData
|
||||
|
||||
|
||||
@@ -40,17 +40,21 @@ bool ddsHandler::canParse(const filesystem::FileData *data)
|
||||
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?)");
|
||||
|
||||
CompressedData::Format texformat = CompressedData::FORMAT_UNKNOWN;
|
||||
|
||||
uint8 *data = 0;
|
||||
dataSize = 0;
|
||||
images.clear();
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to parse the dds file.
|
||||
dds::Parser parser(data->getData(), data->getSize());
|
||||
dds::Parser parser(filedata->getData(), filedata->getSize());
|
||||
|
||||
texformat = convertFormat(parser.getFormat());
|
||||
|
||||
@@ -60,6 +64,17 @@ CompressedData::Format ddsHandler::parse(filesystem::FileData *data, std::vector
|
||||
if (parser.getMipmapCount() == 0)
|
||||
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.
|
||||
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.size = img->dataSize;
|
||||
|
||||
// Copy the mipmap image from the FileData.
|
||||
mip.data = new uint8[mip.size];
|
||||
memcpy(mip.data, img->data, mip.size);
|
||||
// Copy the mipmap image from the FileData to our block of memory.
|
||||
memcpy(data + dataOffset, img->data, mip.size);
|
||||
mip.data = data + dataOffset;
|
||||
|
||||
dataOffset += mip.size;
|
||||
|
||||
images.push_back(mip);
|
||||
}
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
// Clean up any newly allocated heap memory before throwing.
|
||||
for (size_t i = 0; i < images.size(); i++)
|
||||
delete[] images[i].data;
|
||||
|
||||
throw love::Exception(e.what());
|
||||
delete[] data;
|
||||
images.clear();
|
||||
throw love::Exception("%s", e.what());
|
||||
}
|
||||
|
||||
return texformat;
|
||||
format = texformat;
|
||||
return data;
|
||||
}
|
||||
|
||||
CompressedData::Format ddsHandler::convertFormat(dds::Format ddsformat)
|
||||
|
||||
@@ -54,13 +54,18 @@ public:
|
||||
static bool canParse(const filesystem::FileData *data);
|
||||
|
||||
/**
|
||||
* Parses Compressed image data into a list of sub-images.
|
||||
* @param[in] data The data to parse.
|
||||
* @param[out] images The list of sub-images (including byte data for each)
|
||||
* parsed from the file data.
|
||||
* @return The format of the CompressedData.
|
||||
* Parses compressed image filedata into a list of sub-images and returns
|
||||
* a single block of memory containing all the images.
|
||||
*
|
||||
* @param[in] filedata The data to parse.
|
||||
* @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:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user