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.
This commit is contained in:
Alex Szpakowski
2015-07-04 22:30:09 -03:00
parent 3199482c3b
commit 2168d509b3
4 changed files with 36 additions and 28 deletions
+2 -2
View File
@@ -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;
+8 -5
View File
@@ -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
+1 -2
View File
@@ -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:
+25 -19
View File
@@ -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<love::filesystem::File>(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)