Reorganized the ImageData decoding/encoding implementation slightly.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-07-21 21:37:48 -03:00
parent 518ed6f524
commit b568e267eb
4 changed files with 38 additions and 46 deletions
+8 -3
View File
@@ -79,17 +79,17 @@ const char *Image::getName() const
love::image::ImageData *Image::newImageData(love::filesystem::FileData *data)
{
return new ImageData(formatHandlers, data);
return new ImageData(data);
}
love::image::ImageData *Image::newImageData(int width, int height, PixelFormat format)
{
return new ImageData(formatHandlers, width, height, format);
return new ImageData(width, height, format);
}
love::image::ImageData *Image::newImageData(int width, int height, PixelFormat format, void *data, bool own)
{
return new ImageData(formatHandlers, width, height, format, data, own);
return new ImageData(width, height, format, data, own);
}
love::image::CompressedImageData *Image::newCompressedData(love::filesystem::FileData *data)
@@ -108,6 +108,11 @@ bool Image::isCompressed(love::filesystem::FileData *data)
return false;
}
const std::list<FormatHandler *> &Image::getFormatHandlers() const
{
return formatHandlers;
}
} // magpie
} // image
} // love
+9 -7
View File
@@ -41,7 +41,7 @@ namespace magpie
* multiple image libraries and determines the correct one to use on a
* per-image basis at runtime.
**/
class Image : public love::image::Image
class Image final : public love::image::Image
{
public:
@@ -49,15 +49,17 @@ public:
~Image();
// Implements Module.
const char *getName() const;
const char *getName() const override;
love::image::ImageData *newImageData(love::filesystem::FileData *data);
love::image::ImageData *newImageData(int width, int height, PixelFormat format = PIXELFORMAT_RGBA8);
love::image::ImageData *newImageData(int width, int height, PixelFormat format, void *data, bool own = false);
love::image::ImageData *newImageData(love::filesystem::FileData *data) override;
love::image::ImageData *newImageData(int width, int height, PixelFormat format = PIXELFORMAT_RGBA8) override;
love::image::ImageData *newImageData(int width, int height, PixelFormat format, void *data, bool own = false) override;
love::image::CompressedImageData *newCompressedData(love::filesystem::FileData *data);
love::image::CompressedImageData *newCompressedData(love::filesystem::FileData *data) override;
bool isCompressed(love::filesystem::FileData *data);
bool isCompressed(love::filesystem::FileData *data) override;
const std::list<FormatHandler *> &getFormatHandlers() const;
private:
+17 -29
View File
@@ -20,6 +20,7 @@
// LOVE
#include "ImageData.h"
#include "Image.h"
namespace love
{
@@ -28,19 +29,12 @@ namespace image
namespace magpie
{
ImageData::ImageData(std::list<FormatHandler *> formatHandlers, love::filesystem::FileData *data)
: formatHandlers(formatHandlers)
, decodeHandler(nullptr)
ImageData::ImageData(love::filesystem::FileData *data)
{
for (FormatHandler *handler : formatHandlers)
handler->retain();
decode(data);
}
ImageData::ImageData(std::list<FormatHandler *> formatHandlers, int width, int height, PixelFormat format)
: formatHandlers(formatHandlers)
, decodeHandler(nullptr)
ImageData::ImageData(int width, int height, PixelFormat format)
{
if (!validPixelFormat(format))
throw love::Exception("Unsupported pixel format for ImageData");
@@ -53,14 +47,9 @@ ImageData::ImageData(std::list<FormatHandler *> formatHandlers, int width, int h
// Set to black/transparency.
memset(data, 0, getSize());
for (FormatHandler *handler : formatHandlers)
handler->retain();
}
ImageData::ImageData(std::list<FormatHandler *> formatHandlers, int width, int height, PixelFormat format, void *data, bool own)
: formatHandlers(formatHandlers)
, decodeHandler(nullptr)
ImageData::ImageData(int width, int height, PixelFormat format, void *data, bool own)
{
if (!validPixelFormat(format))
throw love::Exception("Unsupported pixel format for ImageData");
@@ -73,34 +62,23 @@ ImageData::ImageData(std::list<FormatHandler *> formatHandlers, int width, int h
this->data = (unsigned char *) data;
else
create(width, height, format, data);
for (FormatHandler *handler : formatHandlers)
handler->retain();
}
ImageData::ImageData(const ImageData &c)
: formatHandlers(c.formatHandlers)
, decodeHandler(nullptr)
{
width = c.width;
height = c.height;
format = c.format;
for (FormatHandler *handler : formatHandlers)
handler->retain();
create(width, height, format, c.getData());
}
ImageData::~ImageData()
{
if (decodeHandler)
if (decodeHandler.get())
decodeHandler->free(data);
else
delete[] data;
for (FormatHandler *handler : formatHandlers)
handler->release();
}
love::image::ImageData *ImageData::clone() const
@@ -133,7 +111,12 @@ void ImageData::decode(love::filesystem::FileData *data)
FormatHandler *decoder = nullptr;
FormatHandler::DecodedImage decodedimage;
for (FormatHandler *handler : formatHandlers)
auto module = dynamic_cast<Image *>(Module::getInstance<love::image::Image>(Module::M_IMAGE));
if (module == nullptr)
throw love::Exception("love.image must be loaded in order to decode an ImageData.");
for (FormatHandler *handler : module->getFormatHandlers())
{
if (handler->canDecode(data))
{
@@ -183,7 +166,12 @@ love::filesystem::FileData *ImageData::encode(EncodedFormat encodedFormat, const
rawimage.data = data;
rawimage.format = format;
for (FormatHandler *handler : formatHandlers)
auto module = dynamic_cast<Image *>(Module::getInstance<love::image::Image>(Module::M_IMAGE));
if (module == nullptr)
throw love::Exception("love.image must be loaded in order to encode an ImageData.");
for (FormatHandler *handler : module->getFormatHandlers())
{
if (handler->canEncode(format, encodedFormat))
{
+4 -7
View File
@@ -39,9 +39,9 @@ class ImageData : public love::image::ImageData
{
public:
ImageData(std::list<FormatHandler *> formatHandlers, love::filesystem::FileData *data);
ImageData(std::list<FormatHandler *> formatHandlers, int width, int height, PixelFormat format = PIXELFORMAT_RGBA8);
ImageData(std::list<FormatHandler *> formatHandlers, int width, int height, PixelFormat format, void *data, bool own);
ImageData(love::filesystem::FileData *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);
virtual ~ImageData();
@@ -57,12 +57,9 @@ private:
// Decode and load an encoded format.
void decode(love::filesystem::FileData *data);
// Image format handlers we can use for decoding and encoding.
std::list<FormatHandler *> formatHandlers;
// The format handler that was used to decode the ImageData. We need to know
// this so we can properly delete memory allocated by the decoder.
FormatHandler *decodeHandler;
StrongRef<FormatHandler> decodeHandler;
}; // ImageData