From 2168d509b3f2d5f26b24a29a8d6fb1920b68b4aa Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 4 Jul 2015 22:30:09 -0300 Subject: [PATCH] Reworked ImageData:encode: - It now returns a FileData containing the encoded ImageData. - The first argument is now the format to encode to. - The second argument is now the filename to write to, and is optional. No file will be written if a filename is not given. --- src/modules/image/ImageData.h | 4 +-- src/modules/image/magpie/ImageData.cpp | 13 +++++--- src/modules/image/magpie/ImageData.h | 3 +- src/modules/image/wrap_ImageData.cpp | 44 +++++++++++++++----------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/modules/image/ImageData.h b/src/modules/image/ImageData.h index 81e392660..bd31581e3 100644 --- a/src/modules/image/ImageData.h +++ b/src/modules/image/ImageData.h @@ -23,7 +23,7 @@ // LOVE #include "common/Data.h" -#include "filesystem/File.h" +#include "filesystem/FileData.h" #include "thread/threads.h" using love::thread::Mutex; @@ -121,7 +121,7 @@ public: * @param f The file to save the encoded image data to. * @param format The format of the encoded data. **/ - virtual void encode(love::filesystem::File *f, EncodedFormat format) = 0; + virtual love::filesystem::FileData *encode(EncodedFormat format, const char *filename) = 0; love::thread::Mutex *getMutex() const; diff --git a/src/modules/image/magpie/ImageData.cpp b/src/modules/image/magpie/ImageData.cpp index 7920c78a2..a0b76e875 100644 --- a/src/modules/image/magpie/ImageData.cpp +++ b/src/modules/image/magpie/ImageData.cpp @@ -144,7 +144,7 @@ void ImageData::decode(love::filesystem::FileData *data) decodeHandler = decoder; } -void ImageData::encode(love::filesystem::File *f, ImageData::EncodedFormat format) +love::filesystem::FileData *ImageData::encode(EncodedFormat format, const char *filename) { FormatHandler *encoder = nullptr; FormatHandler::EncodedImage encodedimage; @@ -174,14 +174,14 @@ void ImageData::encode(love::filesystem::File *f, ImageData::EncodedFormat forma { const char *fname = "unknown"; getConstant(format, fname); - throw love::Exception("no suitable image encoder for %s format.", fname); + throw love::Exception("No suitable image encoder for %s format.", fname); } + love::filesystem::FileData *filedata = nullptr; + try { - f->open(love::filesystem::File::MODE_WRITE); - f->write(encodedimage.data, encodedimage.size); - f->close(); + filedata = new love::filesystem::FileData(encodedimage.size, filename); } catch (love::Exception &) { @@ -189,7 +189,10 @@ void ImageData::encode(love::filesystem::File *f, ImageData::EncodedFormat forma throw; } + memcpy(filedata->getData(), encodedimage.data, encodedimage.size); encoder->free(encodedimage.data); + + return filedata; } } // magpie diff --git a/src/modules/image/magpie/ImageData.h b/src/modules/image/magpie/ImageData.h index 181b3a67a..6c396c8fd 100644 --- a/src/modules/image/magpie/ImageData.h +++ b/src/modules/image/magpie/ImageData.h @@ -23,7 +23,6 @@ // LOVE #include "FormatHandler.h" -#include "filesystem/File.h" #include "image/ImageData.h" // C++ @@ -46,7 +45,7 @@ public: virtual ~ImageData(); // Implements image::ImageData. - virtual void encode(love::filesystem::File *f, ImageData::EncodedFormat format); + virtual love::filesystem::FileData *encode(EncodedFormat format, const char *filename); private: diff --git a/src/modules/image/wrap_ImageData.cpp b/src/modules/image/wrap_ImageData.cpp index 970bf4865..021287907 100644 --- a/src/modules/image/wrap_ImageData.cpp +++ b/src/modules/image/wrap_ImageData.cpp @@ -217,31 +217,37 @@ int w_ImageData_paste(lua_State *L) int w_ImageData_encode(lua_State *L) { - std::string ext; - const char *fmt; - ImageData::EncodedFormat format = ImageData::ENCODED_MAX_ENUM; ImageData *t = luax_checkimagedata(L, 1); - if (lua_isstring(L, 2)) - luax_convobj(L, 2, "filesystem", "newFile"); - love::filesystem::File *file = luax_checktype(L, 2, FILESYSTEM_FILE_ID); + ImageData::EncodedFormat format; + const char *fmt = luaL_checkstring(L, 2); + if (!ImageData::getConstant(fmt, format)) + return luaL_error(L, "Invalid encoded image format '%s'.", fmt); - if (lua_isnoneornil(L, 3)) + bool hasfilename = false; + + std::string filename = "Image." + std::string(fmt); + if (!lua_isnoneornil(L, 3)) { - ext = file->getExtension(); - fmt = ext.c_str(); - if (!ImageData::getConstant(fmt, format)) - return luaL_error(L, "Invalid image format '%s'.", fmt); - } - else - { - fmt = luaL_checkstring(L, 3); - if (!ImageData::getConstant(fmt, format)) - return luaL_error(L, "Invalid image format '%s'.", fmt); + hasfilename = true; + filename = luax_checkstring(L, 3); } - luax_catchexcept(L, [&](){ t->encode(file, format); }); - return 0; + love::filesystem::FileData *filedata = nullptr; + luax_catchexcept(L, [&](){ filedata = t->encode(format, filename.c_str()); }); + + luax_pushtype(L, FILESYSTEM_FILE_DATA_ID, filedata); + filedata->release(); + + if (hasfilename) + { + luax_getfunction(L, "filesystem", "write"); + lua_pushvalue(L, 3); // filename + lua_pushvalue(L, -3); // FileData + lua_call(L, 2, 0); + } + + return 1; } int w_ImageData__performAtomic(lua_State *L)