From 2dd6408d0048a20c9ce8ea3ad590a3143b900b8c Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 5 Aug 2017 23:55:20 -0300 Subject: [PATCH] Images/[Compressed]ImageData can be created using any Data, instead of just files or FileData. --HG-- branch : minor --- src/modules/filesystem/wrap_Filesystem.cpp | 39 +++++++++++++++++++++ src/modules/filesystem/wrap_Filesystem.h | 3 ++ src/modules/graphics/wrap_Graphics.cpp | 26 ++++++++------ src/modules/image/CompressedFormatHandler.h | 6 ++-- src/modules/image/CompressedImageData.cpp | 2 +- src/modules/image/CompressedImageData.h | 3 +- src/modules/image/FormatHandler.cpp | 4 +-- src/modules/image/FormatHandler.h | 6 ++-- src/modules/image/Image.cpp | 6 ++-- src/modules/image/Image.h | 6 ++-- src/modules/image/ImageData.cpp | 15 +++++--- src/modules/image/ImageData.h | 4 +-- src/modules/image/magpie/ASTCHandler.cpp | 5 +-- src/modules/image/magpie/ASTCHandler.h | 4 +-- src/modules/image/magpie/EXRHandler.cpp | 5 +-- src/modules/image/magpie/EXRHandler.h | 4 +-- src/modules/image/magpie/KTXHandler.cpp | 5 +-- src/modules/image/magpie/KTXHandler.h | 4 +-- src/modules/image/magpie/PKMHandler.cpp | 5 +-- src/modules/image/magpie/PKMHandler.h | 4 +-- src/modules/image/magpie/PNGHandler.cpp | 4 +-- src/modules/image/magpie/PNGHandler.h | 4 +-- src/modules/image/magpie/PVRHandler.cpp | 5 +-- src/modules/image/magpie/PVRHandler.h | 4 +-- src/modules/image/magpie/STBHandler.cpp | 6 ++-- src/modules/image/magpie/STBHandler.h | 4 +-- src/modules/image/magpie/ddsHandler.cpp | 5 +-- src/modules/image/magpie/ddsHandler.h | 4 +-- src/modules/image/wrap_Image.cpp | 10 +++--- 29 files changed, 130 insertions(+), 72 deletions(-) diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index d44df9943..fa746850e 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -23,6 +23,7 @@ #include "wrap_File.h" #include "wrap_DroppedFile.h" #include "wrap_FileData.h" +#include "common/wrap_Data.h" #include "physfs/Filesystem.h" @@ -218,11 +219,49 @@ FileData *luax_getfiledata(lua_State *L, int idx) return data; } +Data *luax_getdata(lua_State *L, int idx) +{ + Data *data = nullptr; + File *file = nullptr; + + if (lua_isstring(L, idx) || luax_istype(L, idx, File::type)) + { + file = luax_getfile(L, idx); + file->retain(); + } + else if (luax_istype(L, idx, Data::type)) + { + data = luax_checkdata(L, idx); + data->retain(); + } + + if (!data && !file) + { + luaL_argerror(L, idx, "filename, File, or Data expected"); + return nullptr; // Never reached. + } + + if (file) + { + luax_catchexcept(L, + [&]() { data = file->read(); }, + [&](bool) { file->release(); } + ); + } + + return data; +} + bool luax_cangetfiledata(lua_State *L, int idx) { return lua_isstring(L, idx) || luax_istype(L, idx, File::type) || luax_istype(L, idx, FileData::type); } +bool luax_cangetdata(lua_State *L, int idx) +{ + return lua_isstring(L, idx) || luax_istype(L, idx, File::type) || luax_istype(L, idx, Data::type); +} + int w_newFileData(lua_State *L) { // Single argument: treat as filepath or File. diff --git a/src/modules/filesystem/wrap_Filesystem.h b/src/modules/filesystem/wrap_Filesystem.h index ee360355a..edd1d2e20 100644 --- a/src/modules/filesystem/wrap_Filesystem.h +++ b/src/modules/filesystem/wrap_Filesystem.h @@ -42,6 +42,9 @@ FileData *luax_getfiledata(lua_State *L, int idx); bool luax_cangetfiledata(lua_State *L, int idx); File *luax_getfile(lua_State *L, int idx); +Data *luax_getdata(lua_State *L, int idx); +bool luax_cangetdata(lua_State *L, int idx); + bool hack_setupWriteDirectory(); int loader(lua_State *L); int extloader(lua_State *L); diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index 074c777f2..80d183c52 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -631,10 +631,14 @@ int w_getStencilTest(lua_State *L) return 2; } -static void parsePixelDensity(love::filesystem::FileData *d, float *pixeldensity) +static void parsePixelDensity(Data *d, float *pixeldensity) { + auto fd = dynamic_cast(d); + if (fd == nullptr) + return; + // Parse a density scale of 2.0 from "image@2x.png". - const std::string &fname = d->getName(); + const std::string &fname = fd->getName(); size_t namelen = fname.length(); size_t atpos = fname.rfind('@'); @@ -664,20 +668,20 @@ static Image::Settings w__optImageSettings(lua_State *L, int idx, const Image::S return settings; } -static std::pair, StrongRef> +static std::pair, StrongRef> getImageData(lua_State *L, int idx, bool allowcompressed, float *density) { - StrongRef idata; - StrongRef cdata; + StrongRef idata; + StrongRef cdata; // Convert to ImageData / CompressedImageData, if necessary. - if (lua_isstring(L, idx) || luax_istype(L, idx, love::filesystem::File::type) || luax_istype(L, idx, love::filesystem::FileData::type)) + if (lua_isstring(L, idx) || luax_istype(L, idx, filesystem::File::type) || luax_istype(L, idx, Data::type)) { - auto imagemodule = Module::getInstance(Module::M_IMAGE); + auto imagemodule = Module::getInstance(Module::M_IMAGE); if (imagemodule == nullptr) luaL_error(L, "Cannot load images without the love.image module."); - StrongRef fdata(love::filesystem::luax_getfiledata(L, idx), Acquire::NORETAIN); + StrongRef fdata(filesystem::luax_getdata(L, idx), Acquire::NORETAIN); if (density != nullptr) parsePixelDensity(fdata, density); @@ -688,10 +692,10 @@ getImageData(lua_State *L, int idx, bool allowcompressed, float *density) luax_catchexcept(L, [&]() { idata.set(imagemodule->newImageData(fdata), Acquire::NORETAIN); }); } - else if (luax_istype(L, idx, love::image::CompressedImageData::type)) - cdata.set(love::image::luax_checkcompressedimagedata(L, idx)); + else if (luax_istype(L, idx, image::CompressedImageData::type)) + cdata.set(image::luax_checkcompressedimagedata(L, idx)); else - idata.set(love::image::luax_checkimagedata(L, idx)); + idata.set(image::luax_checkimagedata(L, idx)); return std::make_pair(idata, cdata); } diff --git a/src/modules/image/CompressedFormatHandler.h b/src/modules/image/CompressedFormatHandler.h index 43aa54cc9..99bdc5030 100644 --- a/src/modules/image/CompressedFormatHandler.h +++ b/src/modules/image/CompressedFormatHandler.h @@ -22,7 +22,7 @@ // LOVE #include "common/Object.h" -#include "filesystem/FileData.h" +#include "common/Data.h" #include "CompressedSlice.h" namespace love @@ -46,7 +46,7 @@ public: * CompressedImageData by this handler. * @param data The data to parse. **/ - virtual bool canParse(const filesystem::FileData *data) = 0; + virtual bool canParse(const Data *data) = 0; /** * Parses compressed image filedata into a list of sub-images and returns @@ -60,7 +60,7 @@ public: * * @return The single block of memory containing the parsed images. **/ - virtual StrongRef parse(filesystem::FileData *filedata, + virtual StrongRef parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) = 0; diff --git a/src/modules/image/CompressedImageData.cpp b/src/modules/image/CompressedImageData.cpp index 984c93df5..715a382a1 100644 --- a/src/modules/image/CompressedImageData.cpp +++ b/src/modules/image/CompressedImageData.cpp @@ -27,7 +27,7 @@ namespace image love::Type CompressedImageData::type("CompressedImageData", &Data::type); -CompressedImageData::CompressedImageData(const std::list &formats, love::filesystem::FileData *filedata) +CompressedImageData::CompressedImageData(const std::list &formats, Data *filedata) : format(PIXELFORMAT_UNKNOWN) , sRGB(false) { diff --git a/src/modules/image/CompressedImageData.h b/src/modules/image/CompressedImageData.h index 5a2ecfea2..f74ae2cef 100644 --- a/src/modules/image/CompressedImageData.h +++ b/src/modules/image/CompressedImageData.h @@ -25,7 +25,6 @@ #include "common/StringMap.h" #include "common/int.h" #include "common/pixelformat.h" -#include "filesystem/FileData.h" #include "CompressedSlice.h" #include "CompressedFormatHandler.h" @@ -49,7 +48,7 @@ public: static love::Type type; - CompressedImageData(const std::list &formats, love::filesystem::FileData *filedata); + CompressedImageData(const std::list &formats, Data *filedata); CompressedImageData(const CompressedImageData &c); virtual ~CompressedImageData(); diff --git a/src/modules/image/FormatHandler.cpp b/src/modules/image/FormatHandler.cpp index 689609f56..951ab70d3 100644 --- a/src/modules/image/FormatHandler.cpp +++ b/src/modules/image/FormatHandler.cpp @@ -35,7 +35,7 @@ FormatHandler::~FormatHandler() { } -bool FormatHandler::canDecode(love::filesystem::FileData* /*data*/) +bool FormatHandler::canDecode(Data* /*data*/) { return false; } @@ -45,7 +45,7 @@ bool FormatHandler::canEncode(PixelFormat /*rawFormat*/, EncodedFormat /*encoded return false; } -FormatHandler::DecodedImage FormatHandler::decode(love::filesystem::FileData* /*data*/) +FormatHandler::DecodedImage FormatHandler::decode(Data* /*data*/) { throw love::Exception("Image decoding is not implemented for this format backend."); } diff --git a/src/modules/image/FormatHandler.h b/src/modules/image/FormatHandler.h index a29b42d95..994e0dc77 100644 --- a/src/modules/image/FormatHandler.h +++ b/src/modules/image/FormatHandler.h @@ -23,7 +23,7 @@ // LOVE #include "common/Object.h" #include "common/pixelformat.h" -#include "filesystem/FileData.h" +#include "common/Data.h" namespace love { @@ -75,7 +75,7 @@ public: /** * Whether this format handler can decode a particular FileData. **/ - virtual bool canDecode(love::filesystem::FileData *data); + virtual bool canDecode(Data *data); /** * Whether this format handler can encode to a particular format. @@ -87,7 +87,7 @@ public: * @param data The encoded data to decode. * @return The decoded pixel data. **/ - virtual DecodedImage decode(love::filesystem::FileData *data); + virtual DecodedImage decode(Data *data); /** * Encodes an image from raw pixel data into a particular format. diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index 9d69de4b8..2839eec6e 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -76,7 +76,7 @@ const char *Image::getName() const return "love.image.magpie"; } -love::image::ImageData *Image::newImageData(love::filesystem::FileData *data) +love::image::ImageData *Image::newImageData(Data *data) { return new ImageData(data); } @@ -91,12 +91,12 @@ love::image::ImageData *Image::newImageData(int width, int height, PixelFormat f return new ImageData(width, height, format, data, own); } -love::image::CompressedImageData *Image::newCompressedData(love::filesystem::FileData *data) +love::image::CompressedImageData *Image::newCompressedData(Data *data) { return new CompressedImageData(compressedFormatHandlers, data); } -bool Image::isCompressed(love::filesystem::FileData *data) +bool Image::isCompressed(Data *data) { for (CompressedFormatHandler *handler : compressedFormatHandlers) { diff --git a/src/modules/image/Image.h b/src/modules/image/Image.h index a8a835439..6283c286c 100644 --- a/src/modules/image/Image.h +++ b/src/modules/image/Image.h @@ -61,7 +61,7 @@ public: * @param data The FileData containing the encoded image data. * @return The new ImageData. **/ - ImageData *newImageData(love::filesystem::FileData *data); + ImageData *newImageData(Data *data); /** * Creates empty ImageData with the given size. @@ -87,13 +87,13 @@ public: * @param data The FileData containing the compressed image data. * @return The new CompressedImageData. **/ - CompressedImageData *newCompressedData(love::filesystem::FileData *data); + CompressedImageData *newCompressedData(Data *data); /** * Determines whether a FileData is Compressed image data or not. * @param data The FileData to test. **/ - bool isCompressed(love::filesystem::FileData *data); + bool isCompressed(Data *data); std::vector> newCubeFaces(ImageData *src); std::vector> newVolumeLayers(ImageData *src); diff --git a/src/modules/image/ImageData.cpp b/src/modules/image/ImageData.cpp index 0313d5ae2..863e8de37 100644 --- a/src/modules/image/ImageData.cpp +++ b/src/modules/image/ImageData.cpp @@ -31,7 +31,7 @@ namespace image love::Type ImageData::type("ImageData", &Data::type); -ImageData::ImageData(love::filesystem::FileData *data) +ImageData::ImageData(Data *data) { decode(data); } @@ -108,7 +108,7 @@ void ImageData::create(int width, int height, PixelFormat format, void *data) this->format = format; } -void ImageData::decode(love::filesystem::FileData *data) +void ImageData::decode(Data *data) { FormatHandler *decoder = nullptr; FormatHandler::DecodedImage decodedimage; @@ -132,8 +132,15 @@ void ImageData::decode(love::filesystem::FileData *data) if (decodedimage.data == nullptr) { - const std::string &name = data->getFilename(); - throw love::Exception("Could not decode file '%s' to ImageData: unsupported file format", name.c_str()); + auto filedata = dynamic_cast(data); + + if (filedata != nullptr) + { + const std::string &name = filedata->getFilename(); + throw love::Exception("Could not decode file '%s' to ImageData: unsupported file format", name.c_str()); + } + else + throw love::Exception("Could not decode data to ImageData: unsupported encoded format"); } if (decodedimage.size != decodedimage.width * decodedimage.height * getPixelFormatSize(decodedimage.format)) diff --git a/src/modules/image/ImageData.h b/src/modules/image/ImageData.h index a12355b8e..03cea54cc 100644 --- a/src/modules/image/ImageData.h +++ b/src/modules/image/ImageData.h @@ -55,7 +55,7 @@ public: static love::Type type; - ImageData(love::filesystem::FileData *data); + ImageData(Data *data); ImageData(int width, int height, PixelFormat format = PIXELFORMAT_RGBA8); ImageData(int width, int height, PixelFormat format, void *data, bool own); ImageData(const ImageData &c); @@ -132,7 +132,7 @@ private: void create(int width, int height, PixelFormat format, void *data = nullptr); // Decode and load an encoded format. - void decode(love::filesystem::FileData *data); + void decode(Data *data); // The actual data. unsigned char *data = nullptr; diff --git a/src/modules/image/magpie/ASTCHandler.cpp b/src/modules/image/magpie/ASTCHandler.cpp index 05363fe24..fc938348f 100644 --- a/src/modules/image/magpie/ASTCHandler.cpp +++ b/src/modules/image/magpie/ASTCHandler.cpp @@ -21,6 +21,7 @@ // LOVE #include "ASTCHandler.h" #include "common/int.h" +#include "common/Exception.h" namespace love { @@ -86,7 +87,7 @@ static PixelFormat convertFormat(uint32 blockX, uint32 blockY, uint32 blockZ) } // Anonymous namespace. -bool ASTCHandler::canParse(const filesystem::FileData *data) +bool ASTCHandler::canParse(const Data *data) { if (data->getSize() <= sizeof(ASTCHeader)) return false; @@ -104,7 +105,7 @@ bool ASTCHandler::canParse(const filesystem::FileData *data) return true; } -StrongRef ASTCHandler::parse(filesystem::FileData *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef ASTCHandler::parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParse(filedata)) throw love::Exception("Could not decode compressed data (not an .astc file?)"); diff --git a/src/modules/image/magpie/ASTCHandler.h b/src/modules/image/magpie/ASTCHandler.h index d83fc4ce0..9d876b4f3 100644 --- a/src/modules/image/magpie/ASTCHandler.h +++ b/src/modules/image/magpie/ASTCHandler.h @@ -41,9 +41,9 @@ public: virtual ~ASTCHandler() {} // Implements CompressedFormatHandler. - bool canParse(const filesystem::FileData *data) override; + bool canParse(const Data *data) override; - StrongRef parse(filesystem::FileData *filedata, + StrongRef parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/EXRHandler.cpp b/src/modules/image/magpie/EXRHandler.cpp index 177ad753f..1180eee5d 100644 --- a/src/modules/image/magpie/EXRHandler.cpp +++ b/src/modules/image/magpie/EXRHandler.cpp @@ -21,6 +21,7 @@ // LOVE #include "EXRHandler.h" #include "common/halffloat.h" +#include "common/Exception.h" // tinyexr #define TINYEXR_IMPLEMENTATION @@ -36,7 +37,7 @@ namespace image namespace magpie { -bool EXRHandler::canDecode(love::filesystem::FileData *data) +bool EXRHandler::canDecode(Data *data) { EXRVersion version; return ParseEXRVersionFromMemory(&version, (const unsigned char *) data->getData(), data->getSize()) == TINYEXR_SUCCESS; @@ -100,7 +101,7 @@ static T *loadEXRChannels(int width, int height, T *rgba[4], T one) return data; } -FormatHandler::DecodedImage EXRHandler::decode(love::filesystem::FileData *data) +FormatHandler::DecodedImage EXRHandler::decode(Data *data) { const char *err = "unknown error"; auto mem = (const unsigned char *) data->getData(); diff --git a/src/modules/image/magpie/EXRHandler.h b/src/modules/image/magpie/EXRHandler.h index ff50f2ece..bb7e2559b 100644 --- a/src/modules/image/magpie/EXRHandler.h +++ b/src/modules/image/magpie/EXRHandler.h @@ -38,10 +38,10 @@ public: // Implements FormatHandler. - virtual bool canDecode(love::filesystem::FileData *data); + virtual bool canDecode(Data *data); virtual bool canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat); - virtual DecodedImage decode(love::filesystem::FileData *data); + virtual DecodedImage decode(Data *data); virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format); virtual void free(unsigned char *mem); diff --git a/src/modules/image/magpie/KTXHandler.cpp b/src/modules/image/magpie/KTXHandler.cpp index da88f157e..78cde9477 100644 --- a/src/modules/image/magpie/KTXHandler.cpp +++ b/src/modules/image/magpie/KTXHandler.cpp @@ -21,6 +21,7 @@ // LOVE #include "KTXHandler.h" #include "common/int.h" +#include "common/Exception.h" // C #include @@ -280,7 +281,7 @@ PixelFormat convertFormat(uint32 glformat, bool &sRGB) } // Anonymous namespace. -bool KTXHandler::canParse(const filesystem::FileData *data) +bool KTXHandler::canParse(const Data *data) { if (data->getSize() < sizeof(KTXHeader)) return false; @@ -297,7 +298,7 @@ bool KTXHandler::canParse(const filesystem::FileData *data) return true; } -StrongRef KTXHandler::parse(filesystem::FileData *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef KTXHandler::parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParse(filedata)) throw love::Exception("Could not decode compressed data (not a KTX file?)"); diff --git a/src/modules/image/magpie/KTXHandler.h b/src/modules/image/magpie/KTXHandler.h index 71d8090e7..5716a7d64 100644 --- a/src/modules/image/magpie/KTXHandler.h +++ b/src/modules/image/magpie/KTXHandler.h @@ -40,9 +40,9 @@ public: virtual ~KTXHandler() {} // Implements CompressedFormatHandler. - bool canParse(const filesystem::FileData *data) override; + bool canParse(const Data *data) override; - StrongRef parse(filesystem::FileData *filedata, + StrongRef parse(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 f78c73333..df522c2b5 100644 --- a/src/modules/image/magpie/PKMHandler.cpp +++ b/src/modules/image/magpie/PKMHandler.cpp @@ -21,6 +21,7 @@ // LOVE #include "PKMHandler.h" #include "common/int.h" +#include "common/Exception.h" namespace love { @@ -96,7 +97,7 @@ static PixelFormat convertFormat(uint16 texformat) } // Anonymous namespace. -bool PKMHandler::canParse(const filesystem::FileData *data) +bool PKMHandler::canParse(const Data *data) { if (data->getSize() <= sizeof(PKMHeader)) return false; @@ -113,7 +114,7 @@ bool PKMHandler::canParse(const filesystem::FileData *data) return true; } -StrongRef PKMHandler::parse(filesystem::FileData *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef PKMHandler::parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParse(filedata)) throw love::Exception("Could not decode compressed data (not a PKM file?)"); diff --git a/src/modules/image/magpie/PKMHandler.h b/src/modules/image/magpie/PKMHandler.h index 1da404e35..ff6a93896 100644 --- a/src/modules/image/magpie/PKMHandler.h +++ b/src/modules/image/magpie/PKMHandler.h @@ -40,9 +40,9 @@ public: virtual ~PKMHandler() {} // Implements CompressedFormatHandler. - bool canParse(const filesystem::FileData *data) override; + bool canParse(const Data *data) override; - StrongRef parse(filesystem::FileData *filedata, + StrongRef parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/PNGHandler.cpp b/src/modules/image/magpie/PNGHandler.cpp index b0d3265bc..814e58bee 100644 --- a/src/modules/image/magpie/PNGHandler.cpp +++ b/src/modules/image/magpie/PNGHandler.cpp @@ -128,7 +128,7 @@ static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigne return 0; // Success. } -bool PNGHandler::canDecode(love::filesystem::FileData *data) +bool PNGHandler::canDecode(Data *data) { unsigned int width = 0, height = 0; unsigned char *indata = (unsigned char *) data->getData(); @@ -146,7 +146,7 @@ bool PNGHandler::canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat) && (rawFormat == PIXELFORMAT_RGBA8 || rawFormat == PIXELFORMAT_RGBA16); } -PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata) +PNGHandler::DecodedImage PNGHandler::decode(Data *fdata) { unsigned int width = 0, height = 0; unsigned char *indata = (unsigned char *) fdata->getData(); diff --git a/src/modules/image/magpie/PNGHandler.h b/src/modules/image/magpie/PNGHandler.h index 071cac8ba..f969f5630 100644 --- a/src/modules/image/magpie/PNGHandler.h +++ b/src/modules/image/magpie/PNGHandler.h @@ -39,10 +39,10 @@ public: // Implements FormatHandler. - virtual bool canDecode(love::filesystem::FileData *data); + virtual bool canDecode(Data *data); virtual bool canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat); - virtual DecodedImage decode(love::filesystem::FileData *data); + virtual DecodedImage decode(Data *data); virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format); virtual void free(unsigned char *mem); diff --git a/src/modules/image/magpie/PVRHandler.cpp b/src/modules/image/magpie/PVRHandler.cpp index 3afec5748..6b076a780 100644 --- a/src/modules/image/magpie/PVRHandler.cpp +++ b/src/modules/image/magpie/PVRHandler.cpp @@ -21,6 +21,7 @@ // LOVE #include "PVRHandler.h" #include "common/int.h" +#include "common/Exception.h" // C++ #include @@ -453,7 +454,7 @@ size_t getMipLevelSize(const PVRTexHeaderV3 &header, int miplevel) } // Anonymous namespace. -bool PVRHandler::canParse(const filesystem::FileData *data) +bool PVRHandler::canParse(const Data *data) { if (data->getSize() < sizeof(PVRTexHeaderV2) || data->getSize() < sizeof(PVRTexHeaderV3)) return false; @@ -474,7 +475,7 @@ bool PVRHandler::canParse(const filesystem::FileData *data) return false; } -StrongRef PVRHandler::parse(filesystem::FileData *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef PVRHandler::parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!canParse(filedata)) throw love::Exception("Could not decode compressed data (not a PVR file?)"); diff --git a/src/modules/image/magpie/PVRHandler.h b/src/modules/image/magpie/PVRHandler.h index de06b7f73..33d0b76f3 100644 --- a/src/modules/image/magpie/PVRHandler.h +++ b/src/modules/image/magpie/PVRHandler.h @@ -38,9 +38,9 @@ public: virtual ~PVRHandler() {} // Implements CompressedFormatHandler. - bool canParse(const filesystem::FileData *data) override; + bool canParse(const Data *data) override; - StrongRef parse(filesystem::FileData *filedata, + StrongRef parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/magpie/STBHandler.cpp b/src/modules/image/magpie/STBHandler.cpp index a9f782b3f..48a50c601 100644 --- a/src/modules/image/magpie/STBHandler.cpp +++ b/src/modules/image/magpie/STBHandler.cpp @@ -20,7 +20,7 @@ // LOVE #include "STBHandler.h" -#include "image/ImageData.h" +#include "common/Exception.h" #include "common/Color.h" static void loveSTBIAssert(bool test, const char *teststr) @@ -52,7 +52,7 @@ namespace magpie static_assert(sizeof(Color) == 4, "sizeof(Color) must equal 4 bytes!"); -bool STBHandler::canDecode(love::filesystem::FileData *data) +bool STBHandler::canDecode(Data *data) { int w = 0; int h = 0; @@ -69,7 +69,7 @@ bool STBHandler::canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat) return encodedFormat == ENCODED_TGA && rawFormat == PIXELFORMAT_RGBA8; } -FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data) +FormatHandler::DecodedImage STBHandler::decode(Data *data) { DecodedImage img; diff --git a/src/modules/image/magpie/STBHandler.h b/src/modules/image/magpie/STBHandler.h index df2740623..09cee4a65 100644 --- a/src/modules/image/magpie/STBHandler.h +++ b/src/modules/image/magpie/STBHandler.h @@ -42,10 +42,10 @@ public: // Implements FormatHandler. - bool canDecode(love::filesystem::FileData *data) override; + bool canDecode(Data *data) override; bool canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat) override; - DecodedImage decode(love::filesystem::FileData *data) override; + DecodedImage decode(Data *data) override; EncodedImage encode(const DecodedImage &img, EncodedFormat format) override; void free(unsigned char *mem) override; diff --git a/src/modules/image/magpie/ddsHandler.cpp b/src/modules/image/magpie/ddsHandler.cpp index caa3896e7..1330efe03 100644 --- a/src/modules/image/magpie/ddsHandler.cpp +++ b/src/modules/image/magpie/ddsHandler.cpp @@ -19,6 +19,7 @@ **/ #include "ddsHandler.h" +#include "common/Exception.h" namespace love { @@ -27,12 +28,12 @@ namespace image namespace magpie { -bool DDSHandler::canParse(const filesystem::FileData *data) +bool DDSHandler::canParse(const Data *data) { return dds::isCompressedDDS(data->getData(), data->getSize()); } -StrongRef DDSHandler::parse(filesystem::FileData *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) +StrongRef DDSHandler::parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) { if (!dds::isDDS(filedata->getData(), filedata->getSize())) throw love::Exception("Could not decode compressed data (not a DDS file?)"); diff --git a/src/modules/image/magpie/ddsHandler.h b/src/modules/image/magpie/ddsHandler.h index a875e055f..138e19f49 100644 --- a/src/modules/image/magpie/ddsHandler.h +++ b/src/modules/image/magpie/ddsHandler.h @@ -46,9 +46,9 @@ public: virtual ~DDSHandler() {} // Implements CompressedFormatHandler. - bool canParse(const filesystem::FileData *data) override; + bool canParse(const Data *data) override; - StrongRef parse(filesystem::FileData *filedata, + StrongRef parse(Data *filedata, std::vector> &images, PixelFormat &format, bool &sRGB) override; diff --git a/src/modules/image/wrap_Image.cpp b/src/modules/image/wrap_Image.cpp index d42dea40c..9ea219633 100644 --- a/src/modules/image/wrap_Image.cpp +++ b/src/modules/image/wrap_Image.cpp @@ -19,7 +19,7 @@ **/ #include "wrap_Image.h" - +#include "common/wrap_Data.h" #include "common/Data.h" #include "common/StringMap.h" @@ -77,9 +77,9 @@ int w_newImageData(lua_State *L) t->release(); return 1; } - else if (filesystem::luax_cangetfiledata(L, 1)) // Case 2: File(Data). + else if (filesystem::luax_cangetfiledata(L, 1) || luax_istype(L, 1, Data::type)) // Case 2: File(Data). { - filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); + Data *data = love::filesystem::luax_getdata(L, 1); ImageData *t = nullptr; luax_catchexcept(L, @@ -99,7 +99,7 @@ int w_newImageData(lua_State *L) int w_newCompressedData(lua_State *L) { - love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); + Data *data = love::filesystem::luax_getdata(L, 1); CompressedImageData *t = nullptr; luax_catchexcept(L, @@ -114,7 +114,7 @@ int w_newCompressedData(lua_State *L) int w_isCompressed(lua_State *L) { - love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); + Data *data = love::filesystem::luax_getdata(L, 1); bool compressed = instance()->isCompressed(data); data->release();