diff --git a/src/modules/image/CompressedImageData.cpp b/src/modules/image/CompressedImageData.cpp index 79c7b78f5..387632056 100644 --- a/src/modules/image/CompressedImageData.cpp +++ b/src/modules/image/CompressedImageData.cpp @@ -53,7 +53,7 @@ CompressedImageData::CompressedImageData(const std::list &forma if (format == PIXELFORMAT_UNKNOWN) 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?"); } @@ -61,8 +61,7 @@ CompressedImageData::CompressedImageData(const CompressedImageData &c) : format(c.format) , 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) { @@ -83,12 +82,12 @@ CompressedImageData::~CompressedImageData() size_t CompressedImageData::getSize() const { - return memory->size; + return memory->getSize(); } void *CompressedImageData::getData() const { - return memory->data; + return memory->getData(); } int CompressedImageData::getMipmapCount() const diff --git a/src/modules/image/CompressedImageData.h b/src/modules/image/CompressedImageData.h index 8234d81aa..924afb740 100644 --- a/src/modules/image/CompressedImageData.h +++ b/src/modules/image/CompressedImageData.h @@ -103,7 +103,7 @@ protected: bool sRGB; // Single block of memory containing all of the sub-images. - StrongRef memory; + StrongRef memory; // Texture info for each mipmap level. std::vector> dataImages; diff --git a/src/modules/image/CompressedSlice.cpp b/src/modules/image/CompressedSlice.cpp index f185da994..fb1dd29c9 100644 --- a/src/modules/image/CompressedSlice.cpp +++ b/src/modules/image/CompressedSlice.cpp @@ -26,30 +26,12 @@ namespace love 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) , memory(memory) , offset(offset) , dataSize(size) + , sRGB(false) { } @@ -58,6 +40,7 @@ CompressedSlice::CompressedSlice(const CompressedSlice &s) , memory(s.memory) , offset(s.offset) , dataSize(s.dataSize) + , sRGB(s.sRGB) { } diff --git a/src/modules/image/CompressedSlice.h b/src/modules/image/CompressedSlice.h index f322dd5eb..63004836b 100644 --- a/src/modules/image/CompressedSlice.h +++ b/src/modules/image/CompressedSlice.h @@ -23,7 +23,7 @@ // LOVE #include "common/int.h" #include "common/pixelformat.h" -#include "common/Object.h" +#include "data/ByteData.h" #include "ImageDataBase.h" namespace love @@ -31,17 +31,7 @@ namespace love 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 // sub-image. @@ -49,19 +39,19 @@ class CompressedSlice : public ImageDataBase { 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); virtual ~CompressedSlice(); 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; } bool isSRGB() const override { return sRGB; } size_t getOffset() const { return offset; } private: - StrongRef memory; + StrongRef memory; size_t offset; size_t dataSize; bool sRGB; diff --git a/src/modules/image/FormatHandler.cpp b/src/modules/image/FormatHandler.cpp index 1d37eabf5..9b4fc0050 100644 --- a/src/modules/image/FormatHandler.cpp +++ b/src/modules/image/FormatHandler.cpp @@ -60,7 +60,7 @@ bool FormatHandler::canParseCompressed(Data* /*data*/) return false; } -StrongRef FormatHandler::parseCompressed(Data* /*filedata*/, std::vector>& /*images*/, PixelFormat& /*format*/, bool& /*sRGB*/) +StrongRef FormatHandler::parseCompressed(Data* /*filedata*/, std::vector>& /*images*/, PixelFormat& /*format*/, bool& /*sRGB*/) { throw love::Exception("Compressed image parsing is not implemented for this format backend."); } diff --git a/src/modules/image/FormatHandler.h b/src/modules/image/FormatHandler.h index dd41d6568..2f3f63180 100644 --- a/src/modules/image/FormatHandler.h +++ b/src/modules/image/FormatHandler.h @@ -107,7 +107,7 @@ public: * * @return The single block of memory containing the parsed images. **/ - virtual StrongRef parseCompressed(Data *filedata, + virtual StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB); diff --git a/src/modules/image/magpie/ASTCHandler.cpp b/src/modules/image/magpie/ASTCHandler.cpp index 6a5c1d12e..817025759 100644 --- a/src/modules/image/magpie/ASTCHandler.cpp +++ b/src/modules/image/magpie/ASTCHandler.cpp @@ -105,7 +105,7 @@ bool ASTCHandler::canParseCompressed(Data *data) return true; } -StrongRef ASTCHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef ASTCHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParseCompressed(filedata)) throw love::Exception("Could not decode compressed data (not an .astc file?)"); @@ -125,15 +125,15 @@ StrongRef ASTCHandler::parseCompressed(Data *filedata, std::ve uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY; 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()) throw love::Exception("Could not parse .astc file: file is too small."); - StrongRef memory(new CompressedMemory(totalsize), Acquire::NORETAIN); + StrongRef memory(new ByteData(totalsize, false), Acquire::NORETAIN); // .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); diff --git a/src/modules/image/magpie/ASTCHandler.h b/src/modules/image/magpie/ASTCHandler.h index 6bd797d93..c6daba32f 100644 --- a/src/modules/image/magpie/ASTCHandler.h +++ b/src/modules/image/magpie/ASTCHandler.h @@ -43,7 +43,7 @@ public: // Implements FormatHandler. bool canParseCompressed(Data *data) override; - StrongRef parseCompressed(Data *filedata, + StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/KTXHandler.cpp b/src/modules/image/magpie/KTXHandler.cpp index 177742d7f..8fc2e52c6 100644 --- a/src/modules/image/magpie/KTXHandler.cpp +++ b/src/modules/image/magpie/KTXHandler.cpp @@ -298,7 +298,7 @@ bool KTXHandler::canParseCompressed(Data *data) return true; } -StrongRef KTXHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef KTXHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParseCompressed(filedata)) throw love::Exception("Could not decode compressed data (not a KTX file?)"); @@ -354,8 +354,7 @@ StrongRef KTXHandler::parseCompressed(Data *filedata, std::vec fileoffset += mipsizepadded; } - StrongRef memory; - memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN); + StrongRef memory(new ByteData(totalsize, false), Acquire::NORETAIN); // Reset the file offset to the start of the file's image data. fileoffset = sizeof(KTXHeader) + header.bytesOfKeyValueData; @@ -376,7 +375,7 @@ StrongRef KTXHandler::parseCompressed(Data *filedata, std::vec int width = (int) std::max(header.pixelWidth >> 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); images.push_back(slice); diff --git a/src/modules/image/magpie/KTXHandler.h b/src/modules/image/magpie/KTXHandler.h index 599d3a3a5..4a7bede72 100644 --- a/src/modules/image/magpie/KTXHandler.h +++ b/src/modules/image/magpie/KTXHandler.h @@ -42,7 +42,7 @@ public: // Implements FormatHandler. bool canParseCompressed(Data *data) override; - StrongRef parseCompressed(Data *filedata, + StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/PKMHandler.cpp b/src/modules/image/magpie/PKMHandler.cpp index e513599bc..8f276d257 100644 --- a/src/modules/image/magpie/PKMHandler.cpp +++ b/src/modules/image/magpie/PKMHandler.cpp @@ -114,7 +114,7 @@ bool PKMHandler::canParseCompressed(Data *data) return true; } -StrongRef PKMHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef PKMHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParseCompressed(filedata)) throw love::Exception("Could not decode compressed data (not a PKM file?)"); @@ -135,11 +135,10 @@ StrongRef PKMHandler::parseCompressed(Data *filedata, std::vec // The rest of the file after the header is all texture data. size_t totalsize = filedata->getSize() - sizeof(PKMHeader); - StrongRef memory; - memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN); + StrongRef memory(new ByteData(totalsize, false), Acquire::NORETAIN); // 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 // width and height values (extended == padded.) diff --git a/src/modules/image/magpie/PKMHandler.h b/src/modules/image/magpie/PKMHandler.h index 76c1e89e5..0752340ac 100644 --- a/src/modules/image/magpie/PKMHandler.h +++ b/src/modules/image/magpie/PKMHandler.h @@ -42,7 +42,7 @@ public: // Implements FormatHandler. bool canParseCompressed(Data *data) override; - StrongRef parseCompressed(Data *filedata, + StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/PVRHandler.cpp b/src/modules/image/magpie/PVRHandler.cpp index aa5d089a6..b752bec5f 100644 --- a/src/modules/image/magpie/PVRHandler.cpp +++ b/src/modules/image/magpie/PVRHandler.cpp @@ -475,7 +475,7 @@ bool PVRHandler::canParseCompressed(Data *data) return false; } -StrongRef PVRHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef PVRHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParseCompressed(filedata)) throw love::Exception("Could not decode compressed data (not a PVR file?)"); @@ -525,8 +525,8 @@ StrongRef PVRHandler::parseCompressed(Data *filedata, std::vec if (filedata->getSize() < fileoffset + totalsize) throw love::Exception("Could not parse PVR file: invalid size calculation."); - StrongRef memory; - memory.set(new CompressedMemory(totalsize), Acquire::NORETAIN); + ; + StrongRef memory(new ByteData(totalsize, false), Acquire::NORETAIN); size_t curoffset = 0; const uint8 *filebytes = (uint8 *) filedata->getData() + fileoffset; @@ -541,7 +541,7 @@ StrongRef PVRHandler::parseCompressed(Data *filedata, std::vec int width = std::max((int) header3.width >> 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); images.push_back(slice); diff --git a/src/modules/image/magpie/PVRHandler.h b/src/modules/image/magpie/PVRHandler.h index 8db4db322..11f612bb2 100644 --- a/src/modules/image/magpie/PVRHandler.h +++ b/src/modules/image/magpie/PVRHandler.h @@ -40,7 +40,7 @@ public: // Implements FormatHandler. bool canParseCompressed(Data *data) override; - StrongRef parseCompressed(Data *filedata, + StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/ddsHandler.cpp b/src/modules/image/magpie/ddsHandler.cpp index c0701e6a4..dd19e7c46 100644 --- a/src/modules/image/magpie/ddsHandler.cpp +++ b/src/modules/image/magpie/ddsHandler.cpp @@ -229,7 +229,7 @@ bool DDSHandler::canParseCompressed(Data *data) return dds::isCompressedDDS(data->getData(), data->getSize()); } -StrongRef DDSHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef DDSHandler::parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!dds::isCompressedDDS(filedata->getData(), filedata->getSize())) throw love::Exception("Could not decode compressed data (not a DDS file?)"); @@ -238,7 +238,6 @@ StrongRef DDSHandler::parseCompressed(Data *filedata, std::vec bool isSRGB = false; bool bgra = false; - StrongRef memory; size_t dataSize = 0; images.clear(); @@ -261,7 +260,7 @@ StrongRef DDSHandler::parseCompressed(Data *filedata, std::vec dataSize += img->dataSize; } - memory.set(new CompressedMemory(dataSize), Acquire::NORETAIN); + StrongRef memory(new ByteData(dataSize, false), Acquire::NORETAIN); size_t dataOffset = 0; @@ -272,7 +271,7 @@ StrongRef DDSHandler::parseCompressed(Data *filedata, std::vec const dds::Image *img = parser.getImageData(i); // 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); images.emplace_back(slice, Acquire::NORETAIN); diff --git a/src/modules/image/magpie/ddsHandler.h b/src/modules/image/magpie/ddsHandler.h index 0af73b9de..3d957911a 100644 --- a/src/modules/image/magpie/ddsHandler.h +++ b/src/modules/image/magpie/ddsHandler.h @@ -46,7 +46,7 @@ public: bool canDecode(Data *data) override; DecodedImage decode(Data *data) override; bool canParseCompressed(Data *data) override; - StrongRef parseCompressed(Data *filedata, + StrongRef parseCompressed(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override;