Browse Source

Reorganized the ImageData decoding/encoding implementation slightly.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
b568e267eb

+ 8 - 3
src/modules/image/magpie/Image.cpp

@@ -79,17 +79,17 @@ const char *Image::getName() const
 
 
 love::image::ImageData *Image::newImageData(love::filesystem::FileData *data)
 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)
 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)
 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)
 love::image::CompressedImageData *Image::newCompressedData(love::filesystem::FileData *data)
@@ -108,6 +108,11 @@ bool Image::isCompressed(love::filesystem::FileData *data)
 	return false;
 	return false;
 }
 }
 
 
+const std::list<FormatHandler *> &Image::getFormatHandlers() const
+{
+	return formatHandlers;
+}
+
 } // magpie
 } // magpie
 } // image
 } // image
 } // love
 } // love

+ 9 - 7
src/modules/image/magpie/Image.h

@@ -41,7 +41,7 @@ namespace magpie
  * multiple image libraries and determines the correct one to use on a
  * multiple image libraries and determines the correct one to use on a
  * per-image basis at runtime.
  * per-image basis at runtime.
  **/
  **/
-class Image : public love::image::Image
+class Image final : public love::image::Image
 {
 {
 public:
 public:
 
 
@@ -49,15 +49,17 @@ public:
 	~Image();
 	~Image();
 
 
 	// Implements Module.
 	// 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:
 private:
 
 

+ 17 - 29
src/modules/image/magpie/ImageData.cpp

@@ -20,6 +20,7 @@
 
 
 // LOVE
 // LOVE
 #include "ImageData.h"
 #include "ImageData.h"
+#include "Image.h"
 
 
 namespace love
 namespace love
 {
 {
@@ -28,19 +29,12 @@ namespace image
 namespace magpie
 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);
 	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))
 	if (!validPixelFormat(format))
 		throw love::Exception("Unsupported pixel format for ImageData");
 		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.
 	// Set to black/transparency.
 	memset(data, 0, getSize());
 	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))
 	if (!validPixelFormat(format))
 		throw love::Exception("Unsupported pixel format for ImageData");
 		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;
 		this->data = (unsigned char *) data;
 	else
 	else
 		create(width, height, format, data);
 		create(width, height, format, data);
-
-	for (FormatHandler *handler : formatHandlers)
-		handler->retain();
 }
 }
 
 
 ImageData::ImageData(const ImageData &c)
 ImageData::ImageData(const ImageData &c)
-	: formatHandlers(c.formatHandlers)
-	, decodeHandler(nullptr)
 {
 {
 	width = c.width;
 	width = c.width;
 	height = c.height;
 	height = c.height;
 	format = c.format;
 	format = c.format;
 
 
-	for (FormatHandler *handler : formatHandlers)
-		handler->retain();
-
 	create(width, height, format, c.getData());
 	create(width, height, format, c.getData());
 }
 }
 
 
 ImageData::~ImageData()
 ImageData::~ImageData()
 {
 {
-	if (decodeHandler)
+	if (decodeHandler.get())
 		decodeHandler->free(data);
 		decodeHandler->free(data);
 	else
 	else
 		delete[] data;
 		delete[] data;
-
-	for (FormatHandler *handler : formatHandlers)
-		handler->release();
 }
 }
 
 
 love::image::ImageData *ImageData::clone() const
 love::image::ImageData *ImageData::clone() const
@@ -133,7 +111,12 @@ void ImageData::decode(love::filesystem::FileData *data)
 	FormatHandler *decoder = nullptr;
 	FormatHandler *decoder = nullptr;
 	FormatHandler::DecodedImage decodedimage;
 	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))
 		if (handler->canDecode(data))
 		{
 		{
@@ -183,7 +166,12 @@ love::filesystem::FileData *ImageData::encode(EncodedFormat encodedFormat, const
 	rawimage.data = data;
 	rawimage.data = data;
 	rawimage.format = format;
 	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))
 		if (handler->canEncode(format, encodedFormat))
 		{
 		{

+ 4 - 7
src/modules/image/magpie/ImageData.h

@@ -39,9 +39,9 @@ class ImageData : public love::image::ImageData
 {
 {
 public:
 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);
 	ImageData(const ImageData &c);
 	virtual ~ImageData();
 	virtual ~ImageData();
 
 
@@ -57,12 +57,9 @@ private:
 	// Decode and load an encoded format.
 	// Decode and load an encoded format.
 	void decode(love::filesystem::FileData *data);
 	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
 	// 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.
 	// this so we can properly delete memory allocated by the decoder.
-	FormatHandler *decodeHandler;
+	StrongRef<FormatHandler> decodeHandler;
 
 
 }; // ImageData
 }; // ImageData