mirror of
https://github.com/love2d/love.git
synced 2026-08-14 01:20:56 +02:00
Cleaned up some ImageData code.
This commit is contained in:
@@ -74,7 +74,7 @@ void ImageData::setPixel(int x, int y, pixel c)
|
||||
pixels[y*getWidth()+x] = c;
|
||||
}
|
||||
|
||||
void ImageData::setPixelUnsafe(int x, int y, love::image::pixel c)
|
||||
void ImageData::setPixelUnsafe(int x, int y, pixel c)
|
||||
{
|
||||
if (!inside(x, y))
|
||||
throw love::Exception("Attempt to set out-of-range pixel!");
|
||||
@@ -162,23 +162,23 @@ love::thread::Mutex *ImageData::getMutex() const
|
||||
return mutex;
|
||||
}
|
||||
|
||||
bool ImageData::getConstant(const char *in, ImageData::Format &out)
|
||||
bool ImageData::getConstant(const char *in, EncodedFormat &out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
return encodedFormats.find(in, out);
|
||||
}
|
||||
|
||||
bool ImageData::getConstant(ImageData::Format in, const char *&out)
|
||||
bool ImageData::getConstant(EncodedFormat in, const char *&out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
return encodedFormats.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] =
|
||||
StringMap<ImageData::EncodedFormat, ImageData::ENCODED_MAX_ENUM>::Entry ImageData::encodedFormatEntries[] =
|
||||
{
|
||||
{"tga", ImageData::FORMAT_TGA},
|
||||
{"png", ImageData::FORMAT_PNG},
|
||||
{"tga", ENCODED_TGA},
|
||||
{"png", ENCODED_PNG},
|
||||
};
|
||||
|
||||
StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM> ImageData::formats(ImageData::formatEntries, sizeof(ImageData::formatEntries));
|
||||
StringMap<ImageData::EncodedFormat, ImageData::ENCODED_MAX_ENUM> ImageData::encodedFormats(ImageData::encodedFormatEntries, sizeof(ImageData::encodedFormatEntries));
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
@@ -47,23 +47,16 @@ class ImageData : public Data
|
||||
{
|
||||
public:
|
||||
|
||||
enum Format
|
||||
enum EncodedFormat
|
||||
{
|
||||
FORMAT_TGA,
|
||||
FORMAT_PNG,
|
||||
FORMAT_MAX_ENUM
|
||||
ENCODED_TGA,
|
||||
ENCODED_PNG,
|
||||
ENCODED_MAX_ENUM
|
||||
};
|
||||
|
||||
ImageData();
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
**/
|
||||
virtual ~ImageData();
|
||||
|
||||
static bool getConstant(const char *in, Format &out);
|
||||
static bool getConstant(Format in, const char *&out);
|
||||
|
||||
/**
|
||||
* Paste part of one ImageData onto another. The subregion defined by the top-left
|
||||
* corner (sx, sy) and the size (sw,sh) will be pasted to (dx,dy) in this ImageData.
|
||||
@@ -122,7 +115,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, Format format) = 0;
|
||||
virtual void encode(love::filesystem::File *f, EncodedFormat format) = 0;
|
||||
|
||||
love::thread::Mutex *getMutex() const;
|
||||
|
||||
@@ -130,6 +123,9 @@ public:
|
||||
virtual void *getData() const;
|
||||
virtual size_t getSize() const;
|
||||
|
||||
static bool getConstant(const char *in, EncodedFormat &out);
|
||||
static bool getConstant(EncodedFormat in, const char *&out);
|
||||
|
||||
protected:
|
||||
|
||||
// The width of the image data.
|
||||
@@ -148,8 +144,8 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
|
||||
static StringMap<Format, FORMAT_MAX_ENUM> formats;
|
||||
static StringMap<EncodedFormat, ENCODED_MAX_ENUM>::Entry encodedFormatEntries[];
|
||||
static StringMap<EncodedFormat, ENCODED_MAX_ENUM> encodedFormats;
|
||||
|
||||
}; // ImageData
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ bool FormatHandler::canDecode(love::filesystem::FileData* /*data*/)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FormatHandler::canEncode(ImageData::Format /*format*/)
|
||||
bool FormatHandler::canEncode(ImageData::EncodedFormat /*format*/)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ FormatHandler::DecodedImage FormatHandler::decode(love::filesystem::FileData* /*
|
||||
throw love::Exception("Image decoding is not implemented for this format backend.");
|
||||
}
|
||||
|
||||
FormatHandler::EncodedImage FormatHandler::encode(const DecodedImage& /*img*/, ImageData::Format /*format*/)
|
||||
FormatHandler::EncodedImage FormatHandler::encode(const DecodedImage& /*img*/, ImageData::EncodedFormat /*format*/)
|
||||
{
|
||||
throw love::Exception("Image encoding is not implemented for this format backend.");
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
/**
|
||||
* Whether this format handler can encode to a particular format.
|
||||
**/
|
||||
virtual bool canEncode(ImageData::Format format);
|
||||
virtual bool canEncode(ImageData::EncodedFormat format);
|
||||
|
||||
/**
|
||||
* Decodes an image from its encoded form into raw pixel data.
|
||||
@@ -90,7 +90,7 @@ public:
|
||||
* @param format The format to encode to.
|
||||
* @return The encoded image data.
|
||||
**/
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::Format format);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::EncodedFormat format);
|
||||
|
||||
/**
|
||||
* Frees memory allocated by the format handler.
|
||||
|
||||
@@ -141,7 +141,7 @@ void ImageData::decode(love::filesystem::FileData *data)
|
||||
decodeHandler = decoder;
|
||||
}
|
||||
|
||||
void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
|
||||
void ImageData::encode(love::filesystem::File *f, ImageData::EncodedFormat format)
|
||||
{
|
||||
FormatHandler *encoder = nullptr;
|
||||
FormatHandler::EncodedImage encodedimage;
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
virtual ~ImageData();
|
||||
|
||||
// Implements image::ImageData.
|
||||
virtual void encode(love::filesystem::File *f, ImageData::Format format);
|
||||
virtual void encode(love::filesystem::File *f, ImageData::EncodedFormat format);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -140,9 +140,9 @@ bool PNGHandler::canDecode(love::filesystem::FileData *data)
|
||||
return status == 0 && width > 0 && height > 0;
|
||||
}
|
||||
|
||||
bool PNGHandler::canEncode(ImageData::Format format)
|
||||
bool PNGHandler::canEncode(ImageData::EncodedFormat format)
|
||||
{
|
||||
return format == ImageData::FORMAT_PNG;
|
||||
return format == ImageData::ENCODED_PNG;
|
||||
}
|
||||
|
||||
PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata)
|
||||
@@ -176,9 +176,9 @@ PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata)
|
||||
return img;
|
||||
}
|
||||
|
||||
PNGHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, ImageData::Format format)
|
||||
PNGHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, ImageData::EncodedFormat format)
|
||||
{
|
||||
if (format != ImageData::FORMAT_PNG)
|
||||
if (format != ImageData::ENCODED_PNG)
|
||||
throw love::Exception("PNG encoder cannot encode to non-PNG format.");
|
||||
|
||||
EncodedImage encimg;
|
||||
|
||||
@@ -41,10 +41,10 @@ public:
|
||||
// Implements FormatHandler.
|
||||
|
||||
virtual bool canDecode(love::filesystem::FileData *data);
|
||||
virtual bool canEncode(ImageData::Format format);
|
||||
virtual bool canEncode(ImageData::EncodedFormat format);
|
||||
|
||||
virtual DecodedImage decode(love::filesystem::FileData *data);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::Format format);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::EncodedFormat format);
|
||||
|
||||
virtual void free(unsigned char *mem);
|
||||
|
||||
|
||||
@@ -59,9 +59,9 @@ bool STBHandler::canDecode(love::filesystem::FileData *data)
|
||||
return status == 1 && w > 0 && h > 0;
|
||||
}
|
||||
|
||||
bool STBHandler::canEncode(ImageData::Format format)
|
||||
bool STBHandler::canEncode(ImageData::EncodedFormat format)
|
||||
{
|
||||
return format == ImageData::FORMAT_TGA;
|
||||
return format == ImageData::ENCODED_TGA;
|
||||
}
|
||||
|
||||
FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data)
|
||||
@@ -82,7 +82,7 @@ FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data)
|
||||
return img;
|
||||
}
|
||||
|
||||
FormatHandler::EncodedImage STBHandler::encode(const DecodedImage &img, ImageData::Format format)
|
||||
FormatHandler::EncodedImage STBHandler::encode(const DecodedImage &img, ImageData::EncodedFormat format)
|
||||
{
|
||||
if (!canEncode(format))
|
||||
throw love::Exception("Invalid format.");
|
||||
|
||||
@@ -44,10 +44,10 @@ public:
|
||||
// Implements FormatHandler.
|
||||
|
||||
virtual bool canDecode(love::filesystem::FileData *data);
|
||||
virtual bool canEncode(ImageData::Format format);
|
||||
virtual bool canEncode(ImageData::EncodedFormat format);
|
||||
|
||||
virtual DecodedImage decode(love::filesystem::FileData *data);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::Format format);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::EncodedFormat format);
|
||||
|
||||
virtual void free(unsigned char *mem);
|
||||
|
||||
|
||||
@@ -240,7 +240,7 @@ int w_ImageData_encode(lua_State *L)
|
||||
{
|
||||
std::string ext;
|
||||
const char *fmt;
|
||||
ImageData::Format format = ImageData::FORMAT_MAX_ENUM;
|
||||
ImageData::EncodedFormat format = ImageData::ENCODED_MAX_ENUM;
|
||||
ImageData *t = luax_checkimagedata(L, 1);
|
||||
|
||||
if (lua_isstring(L, 2))
|
||||
|
||||
Reference in New Issue
Block a user