Browse Source

Clean up some compressed texture loading code.

Alex Szpakowski 3 years ago
parent
commit
a9cca65a14

+ 4 - 5
src/modules/image/CompressedImageData.cpp

@@ -53,7 +53,7 @@ CompressedImageData::CompressedImageData(const std::list<FormatHandler *> &forma
 	if (format == PIXELFORMAT_UNKNOWN)
 	if (format == PIXELFORMAT_UNKNOWN)
 		throw love::Exception("Could not parse compressed data: Unknown format.");
 		throw love::Exception("Could not parse compressed data: Unknown format.");
 
 
-	if (dataImages.size() == 0 || memory->size == 0)
+	if (dataImages.size() == 0 || memory->getSize() == 0)
 		throw love::Exception("Could not parse compressed data: No valid data?");
 		throw love::Exception("Could not parse compressed data: No valid data?");
 }
 }
 
 
@@ -61,8 +61,7 @@ CompressedImageData::CompressedImageData(const CompressedImageData &c)
 	: format(c.format)
 	: format(c.format)
 	, sRGB(c.sRGB)
 	, sRGB(c.sRGB)
 {
 {
-	memory.set(new CompressedMemory(c.memory->size), Acquire::NORETAIN);
-	memcpy(memory->data, c.memory->data, memory->size);
+	memory.set(c.memory->clone(), Acquire::NORETAIN);
 
 
 	for (const auto &i : c.dataImages)
 	for (const auto &i : c.dataImages)
 	{
 	{
@@ -83,12 +82,12 @@ CompressedImageData::~CompressedImageData()
 
 
 size_t CompressedImageData::getSize() const
 size_t CompressedImageData::getSize() const
 {
 {
-	return memory->size;
+	return memory->getSize();
 }
 }
 
 
 void *CompressedImageData::getData() const
 void *CompressedImageData::getData() const
 {
 {
-	return memory->data;
+	return memory->getData();
 }
 }
 
 
 int CompressedImageData::getMipmapCount() const
 int CompressedImageData::getMipmapCount() const

+ 1 - 1
src/modules/image/CompressedImageData.h

@@ -103,7 +103,7 @@ protected:
 	bool sRGB;
 	bool sRGB;
 
 
 	// Single block of memory containing all of the sub-images.
 	// Single block of memory containing all of the sub-images.
-	StrongRef<CompressedMemory> memory;
+	StrongRef<ByteData> memory;
 
 
 	// Texture info for each mipmap level.
 	// Texture info for each mipmap level.
 	std::vector<StrongRef<CompressedSlice>> dataImages;
 	std::vector<StrongRef<CompressedSlice>> dataImages;

+ 3 - 20
src/modules/image/CompressedSlice.cpp

@@ -26,30 +26,12 @@ namespace love
 namespace image
 namespace image
 {
 {
 
 
-CompressedMemory::CompressedMemory(size_t size)
-	: data(nullptr)
-	, size(size)
-{
-	try
-	{
-		data = new uint8[size];
-	}
-	catch (std::exception &)
-	{
-		throw love::Exception("Out of memory.");
-	}
-}
-
-CompressedMemory::~CompressedMemory()
-{
-	delete[] data;
-}
-
-CompressedSlice::CompressedSlice(PixelFormat format, int width, int height, CompressedMemory *memory, size_t offset, size_t size)
+CompressedSlice::CompressedSlice(PixelFormat format, int width, int height, ByteData *memory, size_t offset, size_t size)
 	: ImageDataBase(format, width, height)
 	: ImageDataBase(format, width, height)
 	, memory(memory)
 	, memory(memory)
 	, offset(offset)
 	, offset(offset)
 	, dataSize(size)
 	, dataSize(size)
+	, sRGB(false)
 {
 {
 }
 }
 
 
@@ -58,6 +40,7 @@ CompressedSlice::CompressedSlice(const CompressedSlice &s)
 	, memory(s.memory)
 	, memory(s.memory)
 	, offset(s.offset)
 	, offset(s.offset)
 	, dataSize(s.dataSize)
 	, dataSize(s.dataSize)
+	, sRGB(s.sRGB)
 {
 {
 }
 }
 
 

+ 5 - 15
src/modules/image/CompressedSlice.h

@@ -23,7 +23,7 @@
 // LOVE
 // LOVE
 #include "common/int.h"
 #include "common/int.h"
 #include "common/pixelformat.h"
 #include "common/pixelformat.h"
-#include "common/Object.h"
+#include "data/ByteData.h"
 #include "ImageDataBase.h"
 #include "ImageDataBase.h"
 
 
 namespace love
 namespace love
@@ -31,17 +31,7 @@ namespace love
 namespace image
 namespace image
 {
 {
 
 
-class CompressedMemory : public Object
-{
-public:
-
-	CompressedMemory(size_t size);
-	virtual ~CompressedMemory();
-
-	uint8 *data;
-	size_t size;
-
-}; // CompressedMemory
+using ByteData = love::data::ByteData;
 
 
 // Compressed image data can have multiple mipmap levels, each represented by a
 // Compressed image data can have multiple mipmap levels, each represented by a
 // sub-image.
 // sub-image.
@@ -49,19 +39,19 @@ class CompressedSlice : public ImageDataBase
 {
 {
 public:
 public:
 
 
-	CompressedSlice(PixelFormat format, int width, int height, CompressedMemory *memory, size_t offset, size_t size);
+	CompressedSlice(PixelFormat format, int width, int height, ByteData *memory, size_t offset, size_t size);
 	CompressedSlice(const CompressedSlice &slice);
 	CompressedSlice(const CompressedSlice &slice);
 	virtual ~CompressedSlice();
 	virtual ~CompressedSlice();
 
 
 	CompressedSlice *clone() const override;
 	CompressedSlice *clone() const override;
-	void *getData() const override { return memory->data + offset; }
+	void *getData() const override { return (uint8 *) memory->getData() + offset; }
 	size_t getSize() const override { return dataSize; }
 	size_t getSize() const override { return dataSize; }
 	bool isSRGB() const override { return sRGB; }
 	bool isSRGB() const override { return sRGB; }
 	size_t getOffset() const { return offset; }
 	size_t getOffset() const { return offset; }
 
 
 private:
 private:
 
 
-	StrongRef<CompressedMemory> memory;
+	StrongRef<ByteData> memory;
 	size_t offset;
 	size_t offset;
 	size_t dataSize;
 	size_t dataSize;
 	bool sRGB;
 	bool sRGB;

+ 1 - 1
src/modules/image/FormatHandler.cpp

@@ -60,7 +60,7 @@ bool FormatHandler::canParseCompressed(Data* /*data*/)
 	return false;
 	return false;
 }
 }
 
 
-StrongRef<CompressedMemory> FormatHandler::parseCompressed(Data* /*filedata*/, std::vector<StrongRef<CompressedSlice>>& /*images*/, PixelFormat& /*format*/, bool& /*sRGB*/)
+StrongRef<ByteData> FormatHandler::parseCompressed(Data* /*filedata*/, std::vector<StrongRef<CompressedSlice>>& /*images*/, PixelFormat& /*format*/, bool& /*sRGB*/)
 {
 {
 	throw love::Exception("Compressed image parsing is not implemented for this format backend.");
 	throw love::Exception("Compressed image parsing is not implemented for this format backend.");
 }
 }

+ 1 - 1
src/modules/image/FormatHandler.h

@@ -107,7 +107,7 @@ public:
 	 *
 	 *
 	 * @return The single block of memory containing the parsed images.
 	 * @return The single block of memory containing the parsed images.
 	 **/
 	 **/
-	virtual StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	virtual StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB);
 	        PixelFormat &format, bool &sRGB);
 
 

+ 4 - 4
src/modules/image/magpie/ASTCHandler.cpp

@@ -105,7 +105,7 @@ bool ASTCHandler::canParseCompressed(Data *data)
 	return true;
 	return true;
 }
 }
 
 
-StrongRef<CompressedMemory> ASTCHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
+StrongRef<ByteData> ASTCHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
 {
 {
 	if (!canParseCompressed(filedata))
 	if (!canParseCompressed(filedata))
 		throw love::Exception("Could not decode compressed data (not an .astc file?)");
 		throw love::Exception("Could not decode compressed data (not an .astc file?)");
@@ -125,15 +125,15 @@ StrongRef<CompressedMemory> ASTCHandler::parseCompressed(Data *filedata, std::ve
 	uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY;
 	uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY;
 	uint32 blocksZ = (sizeZ + header.blockdimZ - 1) / header.blockdimZ;
 	uint32 blocksZ = (sizeZ + header.blockdimZ - 1) / header.blockdimZ;
 
 
-	size_t totalsize = blocksX * blocksY * blocksZ * 16;
+	size_t totalsize = (size_t) blocksX * blocksY * blocksZ * 16;
 
 
 	if (totalsize + sizeof(header) > filedata->getSize())
 	if (totalsize + sizeof(header) > filedata->getSize())
 		throw love::Exception("Could not parse .astc file: file is too small.");
 		throw love::Exception("Could not parse .astc file: file is too small.");
 
 
-	StrongRef<CompressedMemory> memory(new CompressedMemory(totalsize), Acquire::NORETAIN);
+	StrongRef<ByteData> memory(new ByteData(totalsize, false), Acquire::NORETAIN);
 
 
 	// .astc files only store a single mipmap level.
 	// .astc files only store a single mipmap level.
-	memcpy(memory->data, (uint8 *) filedata->getData() + sizeof(ASTCHeader), totalsize);
+	memcpy(memory->getData(), (uint8 *) filedata->getData() + sizeof(ASTCHeader), totalsize);
 
 
 	images.emplace_back(new CompressedSlice(cformat, sizeX, sizeY, memory, 0, totalsize), Acquire::NORETAIN);
 	images.emplace_back(new CompressedSlice(cformat, sizeX, sizeY, memory, 0, totalsize), Acquire::NORETAIN);
 
 

+ 1 - 1
src/modules/image/magpie/ASTCHandler.h

@@ -43,7 +43,7 @@ public:
 	// Implements FormatHandler.
 	// Implements FormatHandler.
 	bool canParseCompressed(Data *data) override;
 	bool canParseCompressed(Data *data) override;
 
 
-	StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB) override;
 	        PixelFormat &format, bool &sRGB) override;
 
 

+ 3 - 4
src/modules/image/magpie/KTXHandler.cpp

@@ -298,7 +298,7 @@ bool KTXHandler::canParseCompressed(Data *data)
 	return true;
 	return true;
 }
 }
 
 
-StrongRef<CompressedMemory> KTXHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
+StrongRef<ByteData> KTXHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
 {
 {
 	if (!canParseCompressed(filedata))
 	if (!canParseCompressed(filedata))
 		throw love::Exception("Could not decode compressed data (not a KTX file?)");
 		throw love::Exception("Could not decode compressed data (not a KTX file?)");
@@ -354,8 +354,7 @@ StrongRef<CompressedMemory> KTXHandler::parseCompressed(Data *filedata, std::vec
 		fileoffset += mipsizepadded;
 		fileoffset += mipsizepadded;
 	}
 	}
 
 
-	StrongRef<CompressedMemory> memory;
-	memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN);
+	StrongRef<ByteData> memory(new ByteData(totalsize, false), Acquire::NORETAIN);
 
 
 	// Reset the file offset to the start of the file's image data.
 	// Reset the file offset to the start of the file's image data.
 	fileoffset = sizeof(KTXHeader) + header.bytesOfKeyValueData;
 	fileoffset = sizeof(KTXHeader) + header.bytesOfKeyValueData;
@@ -376,7 +375,7 @@ StrongRef<CompressedMemory> KTXHandler::parseCompressed(Data *filedata, std::vec
 		int width = (int) std::max(header.pixelWidth >> i, 1u);
 		int width = (int) std::max(header.pixelWidth >> i, 1u);
 		int height = (int) std::max(header.pixelHeight >> i, 1u);
 		int height = (int) std::max(header.pixelHeight >> i, 1u);
 
 
-		memcpy(memory->data + dataoffset, filebytes + fileoffset, mipsize);
+		memcpy((uint8 *) memory->getData() + dataoffset, filebytes + fileoffset, mipsize);
 
 
 		auto slice = new CompressedSlice(cformat, width, height, memory, dataoffset, mipsize);
 		auto slice = new CompressedSlice(cformat, width, height, memory, dataoffset, mipsize);
 		images.push_back(slice);
 		images.push_back(slice);

+ 1 - 1
src/modules/image/magpie/KTXHandler.h

@@ -42,7 +42,7 @@ public:
 	// Implements FormatHandler.
 	// Implements FormatHandler.
 	bool canParseCompressed(Data *data) override;
 	bool canParseCompressed(Data *data) override;
 
 
-	StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB) override;
 	        PixelFormat &format, bool &sRGB) override;
 
 

+ 3 - 4
src/modules/image/magpie/PKMHandler.cpp

@@ -114,7 +114,7 @@ bool PKMHandler::canParseCompressed(Data *data)
 	return true;
 	return true;
 }
 }
 
 
-StrongRef<CompressedMemory> PKMHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
+StrongRef<ByteData> PKMHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
 {
 {
 	if (!canParseCompressed(filedata))
 	if (!canParseCompressed(filedata))
 		throw love::Exception("Could not decode compressed data (not a PKM file?)");
 		throw love::Exception("Could not decode compressed data (not a PKM file?)");
@@ -135,11 +135,10 @@ StrongRef<CompressedMemory> PKMHandler::parseCompressed(Data *filedata, std::vec
 	// The rest of the file after the header is all texture data.
 	// The rest of the file after the header is all texture data.
 	size_t totalsize = filedata->getSize() - sizeof(PKMHeader);
 	size_t totalsize = filedata->getSize() - sizeof(PKMHeader);
 
 
-	StrongRef<CompressedMemory> memory;
-	memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN);
+	StrongRef<ByteData> memory(new ByteData(totalsize, false), Acquire::NORETAIN);
 
 
 	// PKM files only store a single mipmap level.
 	// PKM files only store a single mipmap level.
-	memcpy(memory->data, (uint8 *) filedata->getData() + sizeof(PKMHeader), totalsize);
+	memcpy(memory->getData(), (uint8 *) filedata->getData() + sizeof(PKMHeader), totalsize);
 
 
 	// TODO: verify whether glCompressedTexImage works properly with the unpadded
 	// TODO: verify whether glCompressedTexImage works properly with the unpadded
 	// width and height values (extended == padded.)
 	// width and height values (extended == padded.)

+ 1 - 1
src/modules/image/magpie/PKMHandler.h

@@ -42,7 +42,7 @@ public:
 	// Implements FormatHandler.
 	// Implements FormatHandler.
 	bool canParseCompressed(Data *data) override;
 	bool canParseCompressed(Data *data) override;
 
 
-	StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB) override;
 	        PixelFormat &format, bool &sRGB) override;
 
 

+ 4 - 4
src/modules/image/magpie/PVRHandler.cpp

@@ -475,7 +475,7 @@ bool PVRHandler::canParseCompressed(Data *data)
 	return false;
 	return false;
 }
 }
 
 
-StrongRef<CompressedMemory> PVRHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
+StrongRef<ByteData> PVRHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
 {
 {
 	if (!canParseCompressed(filedata))
 	if (!canParseCompressed(filedata))
 		throw love::Exception("Could not decode compressed data (not a PVR file?)");
 		throw love::Exception("Could not decode compressed data (not a PVR file?)");
@@ -525,8 +525,8 @@ StrongRef<CompressedMemory> PVRHandler::parseCompressed(Data *filedata, std::vec
 	if (filedata->getSize() < fileoffset + totalsize)
 	if (filedata->getSize() < fileoffset + totalsize)
 		throw love::Exception("Could not parse PVR file: invalid size calculation.");
 		throw love::Exception("Could not parse PVR file: invalid size calculation.");
 
 
-	StrongRef<CompressedMemory> memory;
-	memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN);
+	;
+	StrongRef<ByteData> memory(new ByteData(totalsize, false), Acquire::NORETAIN);
 
 
 	size_t curoffset = 0;
 	size_t curoffset = 0;
 	const uint8 *filebytes = (uint8 *) filedata->getData() + fileoffset;
 	const uint8 *filebytes = (uint8 *) filedata->getData() + fileoffset;
@@ -541,7 +541,7 @@ StrongRef<CompressedMemory> PVRHandler::parseCompressed(Data *filedata, std::vec
 		int width = std::max((int) header3.width >> i, 1);
 		int width = std::max((int) header3.width >> i, 1);
 		int height = std::max((int) header3.height >> i, 1);
 		int height = std::max((int) header3.height >> i, 1);
 
 
-		memcpy(memory->data + curoffset, filebytes + curoffset, mipsize);
+		memcpy((uint8 *) memory->getData() + curoffset, filebytes + curoffset, mipsize);
 
 
 		auto slice = new CompressedSlice(cformat, width, height, memory, curoffset, mipsize);
 		auto slice = new CompressedSlice(cformat, width, height, memory, curoffset, mipsize);
 		images.push_back(slice);
 		images.push_back(slice);

+ 1 - 1
src/modules/image/magpie/PVRHandler.h

@@ -40,7 +40,7 @@ public:
 	// Implements FormatHandler.
 	// Implements FormatHandler.
 	bool canParseCompressed(Data *data) override;
 	bool canParseCompressed(Data *data) override;
 
 
-	StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB) override;
 	        PixelFormat &format, bool &sRGB) override;
 
 

+ 3 - 4
src/modules/image/magpie/ddsHandler.cpp

@@ -229,7 +229,7 @@ bool DDSHandler::canParseCompressed(Data *data)
 	return dds::isCompressedDDS(data->getData(), data->getSize());
 	return dds::isCompressedDDS(data->getData(), data->getSize());
 }
 }
 
 
-StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
+StrongRef<ByteData> DDSHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
 {
 {
 	if (!dds::isCompressedDDS(filedata->getData(), filedata->getSize()))
 	if (!dds::isCompressedDDS(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?)");
@@ -238,7 +238,6 @@ StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vec
 	bool isSRGB = false;
 	bool isSRGB = false;
 	bool bgra = false;
 	bool bgra = false;
 
 
-	StrongRef<CompressedMemory> memory;
 	size_t dataSize = 0;
 	size_t dataSize = 0;
 
 
 	images.clear();
 	images.clear();
@@ -261,7 +260,7 @@ StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vec
 		dataSize += img->dataSize;
 		dataSize += img->dataSize;
 	}
 	}
 
 
-	memory.set(new CompressedMemory(dataSize), Acquire::NORETAIN);
+	StrongRef<ByteData> memory(new ByteData(dataSize, false), Acquire::NORETAIN);
 
 
 	size_t dataOffset = 0;
 	size_t dataOffset = 0;
 
 
@@ -272,7 +271,7 @@ StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vec
 		const dds::Image *img = parser.getImageData(i);
 		const dds::Image *img = parser.getImageData(i);
 
 
 		// Copy the mipmap image from the FileData to our block of memory.
 		// Copy the mipmap image from the FileData to our block of memory.
-		memcpy(memory->data + dataOffset, img->data, img->dataSize);
+		memcpy((uint8 *) memory->getData() + dataOffset, img->data, img->dataSize);
 
 
 		auto slice = new CompressedSlice(texformat, img->width, img->height, memory, dataOffset, img->dataSize);
 		auto slice = new CompressedSlice(texformat, img->width, img->height, memory, dataOffset, img->dataSize);
 		images.emplace_back(slice, Acquire::NORETAIN);
 		images.emplace_back(slice, Acquire::NORETAIN);

+ 1 - 1
src/modules/image/magpie/ddsHandler.h

@@ -46,7 +46,7 @@ public:
 	bool canDecode(Data *data) override;
 	bool canDecode(Data *data) override;
 	DecodedImage decode(Data *data) override;
 	DecodedImage decode(Data *data) override;
 	bool canParseCompressed(Data *data) override;
 	bool canParseCompressed(Data *data) override;
-	StrongRef<CompressedMemory> parseCompressed(Data *filedata,
+	StrongRef<ByteData> parseCompressed(Data *filedata,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        std::vector<StrongRef<CompressedSlice>> &images,
 	        PixelFormat &format, bool &sRGB) override;
 	        PixelFormat &format, bool &sRGB) override;