Replaced the devil love.image module with love.image.magpie, which can decide which image library to use in a similar manner to love.sound.lullaby. Some rough edges still exist.

love.filesystem.newFileData now accepts filepaths and File objects.

--HG--
branch : image-CompressedData
This commit is contained in:
Alex Szpakowski
2013-04-07 17:18:34 -03:00
parent 8a8adcce39
commit 88d64954a4
22 changed files with 846 additions and 531 deletions
@@ -105,14 +105,36 @@ int w_newFile(lua_State *L)
int w_newFileData(lua_State *L)
{
if (!lua_isstring(L, 1))
return luaL_error(L, "String expected.");
if (!lua_isstring(L, 2))
return luaL_error(L, "String expected.");
// Single argument: treat as filepath or File.
if (lua_gettop(L) == 1)
{
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// Get FileData from the File.
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
{
File *file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
FileData *data = 0;
try
{
data = file->read();
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *) data);
return 1;
}
else
return luaL_argerror(L, 1, "string or File expected");
}
size_t length = 0;
const char *str = lua_tolstring(L, 1, &length);
const char *filename = lua_tostring(L, 2);
const char *str = luaL_checklstring(L, 1, &length);
const char *filename = luaL_checkstring(L, 2);
const char *decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
FileData::Decoder decoder = FileData::FILE;
+3 -3
View File
@@ -537,10 +537,10 @@ GLenum Image::getCompressedFormat(image::CompressedData::TextureType type) const
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case image::CompressedData::TYPE_DXT5:
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case image::CompressedData::TYPE_BC5s:
return GL_COMPRESSED_SIGNED_RG_RGTC2;
case image::CompressedData::TYPE_BC5:
return GL_COMPRESSED_RG_RGTC2;
case image::CompressedData::TYPE_BC5s:
return GL_COMPRESSED_SIGNED_RG_RGTC2;
case image::CompressedData::TYPE_BC7:
return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case image::CompressedData::TYPE_BC7srgb:
@@ -587,8 +587,8 @@ bool Image::hasCompressedTextureSupport(image::CompressedData::TextureType type)
case image::CompressedData::TYPE_DXT5:
return GLEE_EXT_texture_compression_s3tc;
case image::CompressedData::TYPE_BC5s:
case image::CompressedData::TYPE_BC5:
case image::CompressedData::TYPE_BC5s:
return (GLEE_VERSION_3_0 || GLEE_ARB_texture_compression_rgtc || GLEE_EXT_texture_compression_rgtc);
case image::CompressedData::TYPE_BC7:
@@ -279,12 +279,12 @@ int w_newImage(lua_State *L)
love::image::ImageData *data = 0;
love::image::CompressedData *cdata = 0;
// Convert to File, if necessary.
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
// Convert to ImageData/CompressedData, if necessary.
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
{
// Determine whether to convert to ImageData or CompressedData.
luax_getfunction(L, "image", "isCompressed");
+13 -14
View File
@@ -26,22 +26,20 @@ namespace image
{
CompressedData::CompressedData()
: type(TYPE_MAX_ENUM)
: type(TYPE_UNKNOWN)
{
}
CompressedData::~CompressedData()
{
}
int CompressedData::getSize() const
{
size_t totalsize = sizeof(MipmapInfo) * dataMipmapInfo.size();
size_t totalsize = sizeof(SubImage) * dataImages.size();
for (size_t i = 0; i < dataMipmapInfo.size(); i++)
totalsize += dataMipmapInfo[i].size;
for (size_t i = 0; i < dataImages.size(); i++)
totalsize += dataImages[i].size;
return totalsize;
}
@@ -49,40 +47,40 @@ int CompressedData::getSize() const
void *CompressedData::getData() const
{
// ?
return (void *) &dataMipmapInfo[0].data[0];
return (void *) &dataImages[0].data[0];
}
int CompressedData::getNumMipmaps() const
{
return dataMipmapInfo.size();
return dataImages.size();
}
int CompressedData::getSize(int miplevel) const
{
checkMipmapLevelExists(miplevel);
return dataMipmapInfo[miplevel].size;
return dataImages[miplevel].size;
}
void *CompressedData::getData(int miplevel) const
{
checkMipmapLevelExists(miplevel);
return (void *) &dataMipmapInfo[miplevel].data[0];
return (void *) &dataImages[miplevel].data[0];
}
int CompressedData::getWidth(int miplevel) const
{
checkMipmapLevelExists(miplevel);
return dataMipmapInfo[miplevel].width;
return dataImages[miplevel].width;
}
int CompressedData::getHeight(int miplevel) const
{
checkMipmapLevelExists(miplevel);
return dataMipmapInfo[miplevel].height;
return dataImages[miplevel].height;
}
CompressedData::TextureType CompressedData::getType() const
@@ -92,7 +90,7 @@ CompressedData::TextureType CompressedData::getType() const
void CompressedData::checkMipmapLevelExists(int miplevel) const
{
if (miplevel < 0 || miplevel >= dataMipmapInfo.size())
if (miplevel < 0 || miplevel >= dataImages.size())
throw love::Exception("Mipmap level %d does not exist", miplevel);
}
@@ -108,11 +106,12 @@ bool CompressedData::getConstant(CompressedData::TextureType in, const char *&ou
StringMap<CompressedData::TextureType, CompressedData::TYPE_MAX_ENUM>::Entry CompressedData::typeEntries[] =
{
{"unknown", CompressedData::TYPE_UNKNOWN},
{"dxt1", CompressedData::TYPE_DXT1},
{"dxt3", CompressedData::TYPE_DXT3},
{"dxt5", CompressedData::TYPE_DXT5},
{"bc5s", CompressedData::TYPE_BC5s},
{"bc5", CompressedData::TYPE_BC5},
{"bc5s", CompressedData::TYPE_BC5s},
{"bc7", CompressedData::TYPE_BC7},
{"bc7srgb", CompressedData::TYPE_BC7srgb},
};
+12 -10
View File
@@ -47,18 +47,27 @@ public:
// Types of compressed image data.
enum TextureType
{
TYPE_UNKNOWN,
TYPE_DXT1,
TYPE_DXT3,
TYPE_DXT5,
TYPE_BC5s,
TYPE_BC5,
TYPE_BC5s,
TYPE_BC7,
TYPE_BC7srgb,
TYPE_MAX_ENUM
};
CompressedData();
// Compressed image data can have multiple mipmap levels, each represented
// by a sub-image.
struct SubImage
{
size_t size;
int width, height;
std::vector<unsigned char> data;
};
CompressedData();
virtual ~CompressedData();
// Implements Data.
@@ -101,17 +110,10 @@ public:
protected:
struct MipmapInfo
{
size_t size;
int width, height;
std::vector<unsigned char> data;
};
TextureType type;
// Texture info for each mipmap level.
std::vector<MipmapInfo> dataMipmapInfo;
std::vector<SubImage> dataImages;
void checkMipmapLevelExists(int miplevel) const;
+13 -31
View File
@@ -35,8 +35,10 @@ namespace image
/**
* This module is responsible for decoding files such as PNG, GIF, JPEG
* into raw pixel data. This module does not know how to draw images on
* screen; only love.graphics knows that.
* into raw pixel data, as well as parsing compressed formats which are designed
* to be uploaded to the GPU and rendered without being un-compressed.
* This module does not know how to draw images on screen; only love.graphics
* knows that.
**/
class Image : public Module
{
@@ -48,18 +50,11 @@ public:
virtual ~Image() {};
/**
* Creates new ImageData from a file.
* @param file The file containing the encoded image data.
* Creates new ImageData from FileData.
* @param data The FileData containing the encoded image data.
* @return The new ImageData.
**/
virtual ImageData *newImageData(love::filesystem::File *file) = 0;
/**
* Creates new ImageData from a raw Data.
* @param data The object containing encoded pixel data.
* @return The new ImageData.
**/
virtual ImageData *newImageData(Data *data) = 0;
virtual ImageData *newImageData(love::filesystem::FileData *data) = 0;
/**
* Creates empty ImageData with the given size.
@@ -79,30 +74,17 @@ public:
virtual ImageData *newImageData(int width, int height, void *data) = 0;
/**
* Creates new CompressedData from a file.
* @param file The file containing the compressed image data.
* Creates new CompressedData from FileData.
* @param data The FileData containing the compressed image data.
* @return The new CompressedData.
**/
virtual CompressedData *newCompressedData(love::filesystem::File *file) = 0;
virtual CompressedData *newCompressedData(love::filesystem::FileData *data) = 0;
/**
* Creates new CompressedData from a raw Data.
* @param data The object containing the compressed image data.
* @return The new CompressedData.
* Determines whether a FileData is Compressed image data or not.
* @param data The FileData to test.
**/
virtual CompressedData *newCompressedData(Data *data) = 0;
/**
* Determines whether a File is Compressed image data or not.
* @param file The file to test.
**/
virtual bool isCompressed(love::filesystem::File *file) = 0;
/**
* Determines whether a raw Data is Compressed image data or not.
* @param data The data to test.
**/
virtual bool isCompressed(Data *data) = 0;
virtual bool isCompressed(love::filesystem::FileData *data) = 0;
}; // Image
+1 -2
View File
@@ -20,8 +20,6 @@
#include "ImageData.h"
#include <stdio.h>
using love::thread::Lock;
namespace love
@@ -30,6 +28,7 @@ namespace image
{
ImageData::ImageData()
: data(0)
{
mutex = thread::newMutex();
}
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "CompressedData.h"
#include "ddsHandler.h"
namespace love
{
namespace image
{
namespace magpie
{
CompressedData::CompressedData(love::filesystem::FileData *data)
{
load(data);
}
CompressedData::~CompressedData()
{
}
void CompressedData::load(love::filesystem::FileData *data)
{
std::vector<SubImage> imageMipmaps;
TextureType textype = TYPE_UNKNOWN;
if (ddsHandler::canParse(data))
textype = ddsHandler::parse(data, imageMipmaps);
if (textype == TYPE_UNKNOWN)
throw (love::Exception("Could not parse compressed data: Unknown format."));
dataImages = imageMipmaps;
type = textype;
}
bool CompressedData::isCompressed(love::filesystem::FileData *data)
{
if (ddsHandler::canParse(data))
return true;
return false;
}
} // magpie
} // image
} // love
@@ -18,43 +18,37 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_DEVIL_COMPRESSED_DATA_H
#define LOVE_DEVIL_COMPRESSED_DATA_H
#ifndef LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
#define LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
// LOVE
#include "filesystem/File.h"
#include "image/CompressedData.h"
// dds parser
#include "ddsparse/ddsparse.h"
namespace love
{
namespace image
{
namespace devil
namespace magpie
{
class CompressedData : public love::image::CompressedData
{
public:
CompressedData(love::filesystem::File *file);
CompressedData(Data *data);
CompressedData(love::filesystem::FileData *data);
virtual ~CompressedData();
static bool isCompressed(const Data *data);
static bool isCompressed(love::filesystem::FileData *data);
private:
bool convertFormat(dds::Format ddsformat);
void load(Data *data);
void load(love::filesystem::FileData *data);
}; // CompressedData
} // devil
} // magpie
} // image
} // love
#endif // LOVE_DEVIL_COMPRESSED_DATA_H
#endif // LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
@@ -18,16 +18,16 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ImageData.h"
// STD
#include <cstring>
#include <iostream>
#include "DevilHandler.h"
// LOVE
#include "common/Exception.h"
#include "common/math.h"
#include "filesystem/File.h"
#include "thread/threads.h"
// DevIL
#include <IL/il.h>
using love::thread::Lock;
@@ -37,7 +37,7 @@ namespace love
{
namespace image
{
namespace devil
namespace magpie
{
static inline void ilxClearErrors()
@@ -45,81 +45,61 @@ static inline void ilxClearErrors()
while (ilGetError() != IL_NO_ERROR);
}
ImageData::ImageData(Data *data)
void DevilHandler::init()
{
load(data);
ilInit();
ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
}
ImageData::ImageData(filesystem::File *file)
void DevilHandler::quit()
{
Data *data = file->read();
try
ilShutDown();
}
bool DevilHandler::canDecode(love::filesystem::FileData *data)
{
// DevIL can decode a lot of formats...
return true;
}
bool DevilHandler::canEncode(ImageData::Format format)
{
switch (format)
{
load(data);
}
catch (love::Exception &)
{
data->release();
throw;
}
data->release();
}
ImageData::ImageData(int width, int height)
{
this->width = width;
this->height = height;
create(width, height);
// Set to black.
memset(data, 0, width*height*4);
}
ImageData::ImageData(int width, int height, void *data)
{
this->width = width;
this->height = height;
create(width, height, data);
}
ImageData::~ImageData()
{
delete[] data;
}
void ImageData::create(int width, int height, void *data)
{
try
{
this->data = new unsigned char[width*height*sizeof(pixel)];
}
catch(std::bad_alloc &)
{
throw love::Exception("Out of memory");
case ImageData::FORMAT_BMP:
case ImageData::FORMAT_TGA:
case ImageData::FORMAT_JPG:
case ImageData::FORMAT_PNG:
return true;
default:
return false;
}
if (data)
memcpy(this->data, data, width*height*sizeof(pixel));
return false;
}
void ImageData::load(Data *data)
DevilHandler::DecodedImage DevilHandler::decode(love::filesystem::FileData *data)
{
if (!devilMutex)
devilMutex = thread::newMutex();
Lock lock(devilMutex);
ILuint image = ilGenImage();
ilBindImage(image);
DecodedImage img;
try
{
bool success = IL_TRUE == ilLoadL(IL_TYPE_UNKNOWN, (void *)data->getData(), data->getSize());
bool success = ilLoadL(IL_TYPE_UNKNOWN, (void *)data->getData(), data->getSize()) == IL_TRUE;
if (!success)
throw love::Exception("Could not decode image!");
width = ilGetInteger(IL_IMAGE_WIDTH);
height = ilGetInteger(IL_IMAGE_HEIGHT);
img.width = ilGetInteger(IL_IMAGE_WIDTH);
img.height = ilGetInteger(IL_IMAGE_HEIGHT);
// Make sure the image is in RGBA format.
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
@@ -129,7 +109,18 @@ void ImageData::load(Data *data)
if (bpp != sizeof(pixel))
throw love::Exception("Could not convert image!");
create(width, height, ilGetData());
img.size = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
try
{
img.data = new ILubyte[img.size];
}
catch (std::bad_alloc &)
{
throw love::Exception("Out of memory.");
}
memcpy(img.data, ilGetData(), img.size);
}
catch (std::exception &e)
{
@@ -139,25 +130,26 @@ void ImageData::load(Data *data)
}
ilDeleteImage(image);
return img;
}
void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
DevilHandler::EncodedImage DevilHandler::encode(const DecodedImage &img, ImageData::Format format)
{
if (!devilMutex)
devilMutex = thread::newMutex();
Lock lock1(devilMutex);
Lock lock2(mutex);
Lock lock(devilMutex);
ILuint tempimage = ilGenImage();
ilBindImage(tempimage);
ilxClearErrors();
ILubyte *encoded_data = NULL;
EncodedImage encodedimg;
try
{
bool success = IL_TRUE == ilTexImage(width, height, 1, sizeof(pixel), IL_RGBA, IL_UNSIGNED_BYTE, this->data);
bool success = ilTexImage(img.width, img.height, 1, sizeof(pixel), IL_RGBA, IL_UNSIGNED_BYTE, img.data) == IL_TRUE;
ILenum err = ilGetError();
ilxClearErrors();
@@ -202,37 +194,34 @@ void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
break;
}
ILuint size = ilSaveL(ilFormat, NULL, 0);
if (!size)
encodedimg.size = ilSaveL(ilFormat, NULL, 0);
if (!encodedimg.size)
throw love::Exception("Could not encode image!");
try
{
encoded_data = new ILubyte[size];
encodedimg.data = new ILubyte[encodedimg.size];
}
catch(std::bad_alloc &)
{
throw love::Exception("Out of memory");
}
ilSaveL(ilFormat, encoded_data, size);
f->open(love::filesystem::File::WRITE);
f->write(encoded_data, size);
f->close();
ilSaveL(ilFormat, encodedimg.data, encodedimg.size);
}
catch (std::exception &e)
{
// catches love and std exceptions
ilDeleteImage(tempimage);
delete[] encoded_data;
delete[] encodedimg.data;
throw love::Exception("%s", e.what());
}
ilDeleteImage(tempimage);
delete[] encoded_data;
return encodedimg;
}
} // devil
} // magpie
} // image
} // love
+56
View File
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_IMAGE_MAGPIE_DEVIL_HANDLER_H
#define LOVE_IMAGE_MAGPIE_DEVIL_HANDLER_H
// LOVE
#include "filesystem/File.h"
#include "FormatHandler.h"
namespace love
{
namespace image
{
namespace magpie
{
class DevilHandler : public FormatHandler
{
public:
static void init();
static void quit();
// Implements FormatHandler.
static bool canDecode(love::filesystem::FileData *data);
static bool canEncode(ImageData::Format format);
static DecodedImage decode(love::filesystem::FileData *data);
static EncodedImage encode(const DecodedImage &img, ImageData::Format format);
}; // DevilHandler
} // magpie
} // image
} // love
#endif // LOVE_IMAGE_MAGPIE_DEVIL_HANDLER_H
+94
View File
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_IMAGE_MAGPIE_FORMAT_HANDLER_H
#define LOVE_IMAGE_MAGPIE_FORMAT_HANDLER_H
// LOVE
#include "image/ImageData.h"
#include "filesystem/File.h"
namespace love
{
namespace image
{
namespace magpie
{
/**
* Base class for all ImageData encoder/decoder library interfaces.
**/
class FormatHandler
{
public:
// Raw RGBA pixel data.
struct DecodedImage
{
int width, height;
size_t size;
unsigned char *data;
DecodedImage() : width(0), height(0), size(0), data(0) {};
};
// Pixel data encoded in a particular format.
struct EncodedImage
{
size_t size;
unsigned char *data;
EncodedImage() : size(0), data(0) {};
};
// Lets pretend we have virtual static methods...
/**
* Determines whether a particular FileData can be decoded by this handler.
* @param data The data to decode.
**/
// virtual static bool canDecode(love::filesystem::FileData *data) = 0;
/**
* Determines whether this handler can encode to a particular format.
* @param format The format to encode to.
**/
// virtual static bool canEncode(ImageData::Format format) = 0;
/**
* Decodes an image from its encoded form into raw pixel data.
* @param data The encoded data to decode.
* @return The decoded pixel data.
**/
// virtual static DecodedImage decode(love::filesystem::FileData *data) = 0;
/**
* Encodes an image from raw pixel data into a particular format.
* @param img The raw image data to encode.
* @param format The format to encode to.
* @return The encoded image data.
**/
// virtual static EncodedImage encode(const DecodedImage &img, ImageData::Format format) = 0;
}; // FormatHandler
} // magpie
} // image
} // love
#endif // LOVE_IMAGE_MAGPIE_FORMAT_HANDLER_H
@@ -20,47 +20,34 @@
#include "Image.h"
#include "ImageData.h"
#include "imageData.h"
#include "CompressedData.h"
// DevIL
#include <IL/il.h>
#include "DevilHandler.h"
namespace love
{
namespace image
{
namespace devil
namespace magpie
{
const std::string Image::compressedExts[] =
{
".dds", ""
};
Image::Image()
{
ilInit();
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
ilEnable(IL_ORIGIN_SET);
DevilHandler::init();
}
Image::~Image()
{
ilShutDown();
DevilHandler::quit();
}
const char *Image::getName() const
{
return "love.image.devil";
return "love.image.magpie";
}
love::image::ImageData *Image::newImageData(love::filesystem::File *file)
{
return new ImageData(file);
}
love::image::ImageData *Image::newImageData(Data *data)
love::image::ImageData *Image::newImageData(love::filesystem::FileData *data)
{
return new ImageData(data);
}
@@ -75,47 +62,16 @@ love::image::ImageData *Image::newImageData(int width, int height, void *data)
return new ImageData(width, height, data);
}
love::image::CompressedData *Image::newCompressedData(love::filesystem::File *file)
{
return new CompressedData(file);
}
love::image::CompressedData *Image::newCompressedData(love::Data *data)
love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data)
{
return new CompressedData(data);
}
bool Image::isCompressed(love::filesystem::File *file)
{
bool hasExt = false;
// Check whether the file has an extension known to contain compressed data.
const std::string &ext = file->getExtension();
for (int i = 0; !(compressedExts[i].empty()); i++)
{
if (compressedExts[i].compare(ext))
{
hasExt = true;
break;
}
}
if (!hasExt)
return false;
// Check whether the actual data is compressed.
Data *data = file->read();
bool compressed = isCompressed(data);
data->release();
return compressed;
}
bool Image::isCompressed(love::Data *data)
bool Image::isCompressed(love::filesystem::FileData *data)
{
return CompressedData::isCompressed(data);
}
} // devil
} // magpie
} // image
} // love
@@ -18,22 +18,24 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_IMAGE_DEVIL_IMAGE_H
#define LOVE_IMAGE_DEVIL_IMAGE_H
#ifndef LOVE_IMAGE_MAGPIE_IMAGE_H
#define LOVE_IMAGE_MAGPIE_IMAGE_H
// LOVE
#include "image/Image.h"
// STL
#include <string>
namespace love
{
namespace image
{
namespace devil
namespace magpie
{
/**
* Similar to love.sound's Lullaby module, love.image.magpie interfaces with
* multiple image libraries and determines the correct one to use on a
* per-image basis at runtime.
**/
class Image : public love::image::Image
{
public:
@@ -44,23 +46,18 @@ public:
// Implements Module.
const char *getName() const;
love::image::ImageData *newImageData(love::filesystem::File *file);
love::image::ImageData *newImageData(Data *data);
love::image::ImageData *newImageData(love::filesystem::FileData *data);
love::image::ImageData *newImageData(int width, int height);
love::image::ImageData *newImageData(int width, int height, void *data);
love::image::CompressedData *newCompressedData(love::filesystem::File *file);
love::image::CompressedData *newCompressedData(Data *data);
love::image::CompressedData *newCompressedData(love::filesystem::FileData *data);
bool isCompressed(love::filesystem::File *file);
bool isCompressed(Data *data);
static const std::string compressedExts[];
bool isCompressed(love::filesystem::FileData *data);
}; // Image
} // devil
} // magpie
} // image
} // love
#endif // LOVE_IMAGE_DEVIL_IMAGE_H
#endif // LOVE_IMAGE_MAGPIE_IMAGE_H
+133
View File
@@ -0,0 +1,133 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ImageData.h"
#include "FormatHandler.h"
#include "DevilHandler.h"
namespace love
{
namespace image
{
namespace magpie
{
ImageData::ImageData(love::filesystem::FileData *data)
{
decode(data);
}
ImageData::ImageData(int width, int height)
{
this->width = width;
this->height = height;
create(width, height);
// Set to black/transparency.
memset(data, 0, width*height*sizeof(pixel));
}
ImageData::ImageData(int width, int height, void *data)
{
this->width = width;
this->height = height;
create(width, height, data);
}
ImageData::~ImageData()
{
delete[] data;
}
void ImageData::create(int width, int height, void *data)
{
try
{
this->data = new unsigned char[width*height*sizeof(pixel)];
}
catch(std::bad_alloc &)
{
throw love::Exception("Out of memory");
}
if (data)
memcpy(this->data, data, width*height*sizeof(pixel));
}
void ImageData::decode(love::filesystem::FileData *data)
{
FormatHandler::DecodedImage decodedimg;
if (DevilHandler::canDecode(data))
decodedimg = DevilHandler::decode(data);
else
throw love::Exception("Image format has no suitable decoder.");
// The decoder *must* output a 32 bits-per-pixel image.
if (decodedimg.size != decodedimg.width*decodedimg.height*sizeof(pixel))
{
delete[] decodedimg.data;
throw love::Exception("Coult not convert image!");
}
if (this->data)
delete[] this->data;
this->width = decodedimg.width;
this->height = decodedimg.height;
this->data = decodedimg.data;
}
void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
{
thread::Lock lock(mutex);
FormatHandler::DecodedImage rawimage;
rawimage.width = width;
rawimage.height = height;
rawimage.size = width*height*sizeof(pixel);
rawimage.data = data;
FormatHandler::EncodedImage encodedimage;
try
{
if (DevilHandler::canEncode(format))
encodedimage = DevilHandler::encode(rawimage, format);
else
throw love::Exception("Image format has no suitable encoder.");
f->open(love::filesystem::File::WRITE);
f->write(encodedimage.data, encodedimage.size);
f->close();
}
catch (love::Exception &)
{
delete[] encodedimage.data;
throw;
}
delete[] encodedimage.data;
}
} // magpie
} // image
} // love
@@ -18,49 +18,44 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_DEVIL_IMAGE_DATA_H
#define LOVE_DEVIL_IMAGE_DATA_H
#ifndef LOVE_IMAGE_MAGPIE_IMAGE_DATA_H
#define LOVE_IMAGE_MAGPIE_IMAGE_DATA_H
// LOVE
#include "filesystem/File.h"
#include "image/ImageData.h"
// DevIL
#include <IL/il.h>
namespace love
{
namespace image
{
namespace devil
namespace magpie
{
class ImageData : public love::image::ImageData
{
public:
ImageData(love::filesystem::FileData *data);
ImageData(int width, int height);
ImageData(int width, int height, void *data);
virtual ~ImageData();
// Implements image::ImageData.
virtual void encode(love::filesystem::File *f, ImageData::Format format);
private:
// Create imagedata. Initialize with data if not null.
void create(int width, int height, void *data = 0);
// Load an encoded format.
void load(Data *data);
public:
ImageData(Data *data);
ImageData(love::filesystem::File *file);
ImageData(int width, int height);
ImageData(int width, int height, void *data);
virtual ~ImageData();
// Implements ImageData.
void encode(love::filesystem::File *f, Format format);
// Decode and load an encoded format.
void decode(love::filesystem::FileData *data);
}; // ImageData
} // devil
} // magpie
} // image
} // love
#endif // LOVE_DEVIL_IMAGE_DATA_H
#endif // LOVE_IMAGE_MAGPIE_IMAGE_DATA_H
@@ -18,83 +18,53 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "CompressedData.h"
#include "ddsHandler.h"
namespace love
{
namespace image
{
namespace devil
namespace magpie
{
CompressedData::CompressedData(Data *data)
bool ddsHandler::canParse(const filesystem::FileData *data)
{
load(data);
}
CompressedData::CompressedData(love::filesystem::File *file)
{
Data *data = file->read();
try
{
load(data);
}
catch (love::Exception &)
{
data->release();
throw;
}
}
CompressedData::~CompressedData()
{
}
bool CompressedData::convertFormat(dds::Format ddsformat)
{
switch (ddsformat)
{
case dds::FORMAT_DXT1:
type = TYPE_DXT1;
break;
case dds::FORMAT_DXT3:
type = TYPE_DXT3;
break;
case dds::FORMAT_DXT5:
type = TYPE_DXT5;
break;
case dds::FORMAT_BC5s:
type = TYPE_BC5s;
break;
case dds::FORMAT_BC5:
type = TYPE_BC5;
break;
case dds::FORMAT_BC7:
type = TYPE_BC7;
break;
case dds::FORMAT_BC7srgb:
type = TYPE_BC7srgb;
break;
default:
if (!accepts(data->getExtension()))
return false;
}
return true;
return dds::Parser::isCompressedDDS(data->getData(), data->getSize());
}
void CompressedData::load(Data *data)
bool ddsHandler::accepts(const std::string &ext)
{
static const std::string supported[] =
{
"dds", ""
};
for (int i = 0; !(supported[i].empty()); i++)
{
if (supported[i].compare(ext) == 0)
return true;
}
return false;
}
CompressedData::TextureType ddsHandler::parse(filesystem::FileData *data, std::vector<CompressedData::SubImage> &images)
{
if (!dds::Parser::isDDS(data->getData(), data->getSize()))
throw love::Exception("Could not decode compressed data (not a DDS file?)");
CompressedData::TextureType textype = CompressedData::TYPE_UNKNOWN;
try
{
dds::Parser parser(data->getData(), data->getSize());
dds::Format format = parser.getFormat();
textype = convertFormat(parser.getFormat());
if (format == dds::FORMAT_UNKNOWN || !convertFormat(format))
if (textype == CompressedData::TYPE_UNKNOWN)
throw love::Exception("Could not parse compressed data: Unsupported format.");
if (parser.getNumMipmaps() == 0)
@@ -104,30 +74,49 @@ void CompressedData::load(Data *data)
{
const dds::Parser::Image *img = parser.getImageData(i);
MipmapInfo mip;
CompressedData::SubImage mip;
mip.width = img->width;
mip.height = img->height;
mip.size = img->dataSize;
mip.data.insert(mip.data.begin(), &img->data[0], &img->data[mip.size]);
mip.data.resize(mip.size);
memcpy(&mip.data[0], img->data, mip.size);
dataMipmapInfo.push_back(mip);
images.push_back(mip);
}
}
catch (std::exception &e)
{
throw love::Exception(e.what());
}
return textype;
}
bool CompressedData::isCompressed(const Data *data)
CompressedData::TextureType ddsHandler::convertFormat(dds::Format ddsformat)
{
return dds::Parser::isDDS(data->getData(), data->getSize());
switch (ddsformat)
{
case dds::FORMAT_DXT1:
return CompressedData::TYPE_DXT1;
case dds::FORMAT_DXT3:
return CompressedData::TYPE_DXT3;
case dds::FORMAT_DXT5:
return CompressedData::TYPE_DXT5;
case dds::FORMAT_BC5:
return CompressedData::TYPE_BC5;
case dds::FORMAT_BC5s:
return CompressedData::TYPE_BC5s;
case dds::FORMAT_BC7:
return CompressedData::TYPE_BC7;
case dds::FORMAT_BC7srgb:
return CompressedData::TYPE_BC7srgb;
default:
return CompressedData::TYPE_UNKNOWN;
}
return CompressedData::TYPE_UNKNOWN;
}
} // devil
} // magpie
} // image
} // love
+75
View File
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_IMAGE_MAGPIE_DDS_HANDLER_H
#define LOVE_IMAGE_MAGPIE_DDS_HANDLER_H
// LOVE
#include "filesystem/File.h"
#include "image/CompressedData.h"
// dds parser
#include "ddsparse/ddsparse.h"
// STL
#include <string>
namespace love
{
namespace image
{
namespace magpie
{
/**
* Interface between CompressedData and the ddsparse library.
**/
class ddsHandler
{
public:
/**
* Determines whether a particular FileData can be parsed as CompressedData
* by this handler.
* @param data The data to parse.
**/
static bool canParse(const filesystem::FileData *data);
/**
* Parses Compressed image data into a list of sub-images.
* @param[in] data The data to parse.
* @param[out] images The list of sub-images (including byte data for each)
* outputted by the parser.
* @return The type of CompressedData.
**/
static CompressedData::TextureType parse(filesystem::FileData *data, std::vector<CompressedData::SubImage> &images);
private:
static bool accepts(const std::string &ext);
static CompressedData::TextureType convertFormat(dds::Format ddsformat);
}; // ddsHandler
} // magpie
} // image
} // love
#endif // LOVE_IMAGE_MAGPIE_DDS_HANDLER_H
+39 -89
View File
@@ -23,7 +23,7 @@
#include "common/Data.h"
#include "common/StringMap.h"
#include "devil/Image.h"
#include "magpie/Image.h"
namespace love
{
@@ -55,82 +55,47 @@ int w_newImageData(lua_State *L)
return 1;
}
// Case 2: Data
if (luax_istype(L, 1, DATA_T))
{
Data *d = luax_checktype<Data>(L, 1, "Data", DATA_T);
ImageData *t = 0;
try
{
t = instance->newImageData(d);
}
catch(love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t);
return 1;
}
// Case 2: File(Data).
// Case 3: String/File.
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
// Convert to File, if necessary.
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
ImageData *t = 0;
try
{
t = instance->newImageData(file);
}
catch(love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t);
return 1;
}
int w_newCompressedData(lua_State *L)
{
// Case 1: Data
if (luax_istype(L, 1, DATA_T))
{
Data *d = luax_checktype<Data>(L, 1, "Data", DATA_T);
CompressedData *t = 0;
try
{
t = instance->newCompressedData(d);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, (void *) t);
return 1;
}
// Case 2: String/File.
// Convert to File, if necessary.
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
CompressedData *t = 0;
try
{
t = instance->newCompressedData(file);
t = instance->newImageData(data);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *) t);
return 1;
}
int w_newCompressedData(lua_State *L)
{
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
CompressedData *t = 0;
try
{
t = instance->newCompressedData(data);
}
catch(love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, (void *) t);
return 1;
@@ -138,37 +103,22 @@ int w_newCompressedData(lua_State *L)
int w_isCompressed(lua_State *L)
{
if (luax_istype(L, 1, DATA_T))
{
Data *d = luax_checktype<Data>(L, 1, "Data", DATA_T);
try
{
bool compressed = instance->isCompressed(d);
luax_pushboolean(L, compressed);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
return 1;
}
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
// Convert to File, if necessary.
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
filesystem::File *file = luax_checktype<filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
bool compressed = false;
try
{
bool compressed = instance->isCompressed(file);
luax_pushboolean(L, compressed);
compressed = instance->isCompressed(data);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_pushboolean(L, compressed);
return 1;
}
@@ -194,7 +144,7 @@ extern "C" int luaopen_love_image(lua_State *L)
{
try
{
instance = new love::image::devil::Image();
instance = new love::image::magpie::Image();
}
catch(Exception &e)
{