mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
Added love.math.compress(data, format [, level]) and love.math.decompress(compresseddata) / love.math.decompress(compressedstring, format).
Renamed the love.image CompressedData type to CompressedImageData. love.math.compress returns a love.math CompressedData object which holds the newly compressed data. Currently supported formats are "lz4" and "zlib". Note that the formats are not file formats and don't compress filesystem hierarchies.
This commit is contained in:
@@ -656,7 +656,7 @@ Image *Graphics::newImage(love::image::ImageData *data, const Image::Flags &flag
|
||||
return new Image(data, flags);
|
||||
}
|
||||
|
||||
Image *Graphics::newImage(love::image::CompressedData *cdata, const Image::Flags &flags)
|
||||
Image *Graphics::newImage(love::image::CompressedImageData *cdata, const Image::Flags &flags)
|
||||
{
|
||||
return new Image(cdata, flags);
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ public:
|
||||
* Creates an Image object with padding and/or optimization.
|
||||
**/
|
||||
Image *newImage(love::image::ImageData *data, const Image::Flags &flags);
|
||||
Image *newImage(love::image::CompressedData *cdata, const Image::Flags &flags);
|
||||
Image *newImage(love::image::CompressedImageData *cdata, const Image::Flags &flags);
|
||||
|
||||
Quad *newQuad(Quad::Viewport v, float sw, float sh);
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ Image::Image(love::image::ImageData *data, const Flags &flags)
|
||||
++imageCount;
|
||||
}
|
||||
|
||||
Image::Image(love::image::CompressedData *cdata, const Flags &flags)
|
||||
Image::Image(love::image::CompressedImageData *cdata, const Flags &flags)
|
||||
: data(nullptr)
|
||||
, cdata(cdata)
|
||||
, texture(0)
|
||||
@@ -76,7 +76,7 @@ Image::Image(love::image::CompressedData *cdata, const Flags &flags)
|
||||
|
||||
if (flags.mipmaps)
|
||||
{
|
||||
// The mipmap texture data comes from the CompressedData in this case,
|
||||
// The mipmap texture data comes from the CompressedImageData in this case,
|
||||
// so we should make sure it has all necessary mipmap levels.
|
||||
if (cdata->getMipmapCount() < (int) log2(std::max(width, height)) + 1)
|
||||
throw love::Exception("Image cannot have mipmaps: compressed image data does not have all required mipmap levels.");
|
||||
@@ -198,7 +198,7 @@ bool Image::loadVolatile()
|
||||
if (isCompressed() && !hasCompressedTextureSupport(cdata->getFormat(), flags.sRGB))
|
||||
{
|
||||
const char *str;
|
||||
if (image::CompressedData::getConstant(cdata->getFormat(), str))
|
||||
if (image::CompressedImageData::getConstant(cdata->getFormat(), str))
|
||||
{
|
||||
throw love::Exception("Cannot create image: "
|
||||
"%s%s compressed images are not supported on this system.", flags.sRGB ? "sRGB " : "", str);
|
||||
@@ -392,7 +392,7 @@ love::image::ImageData *Image::getImageData() const
|
||||
return data.get();
|
||||
}
|
||||
|
||||
love::image::CompressedData *Image::getCompressedData() const
|
||||
love::image::CompressedImageData *Image::getCompressedData() const
|
||||
{
|
||||
return cdata.get();
|
||||
}
|
||||
@@ -491,87 +491,87 @@ bool Image::isCompressed() const
|
||||
return compressed;
|
||||
}
|
||||
|
||||
GLenum Image::getCompressedFormat(image::CompressedData::Format cformat) const
|
||||
GLenum Image::getCompressedFormat(image::CompressedImageData::Format cformat) const
|
||||
{
|
||||
switch (cformat)
|
||||
{
|
||||
case image::CompressedData::FORMAT_DXT1:
|
||||
case image::CompressedImageData::FORMAT_DXT1:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
case image::CompressedData::FORMAT_DXT3:
|
||||
case image::CompressedImageData::FORMAT_DXT3:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
||||
case image::CompressedData::FORMAT_DXT5:
|
||||
case image::CompressedImageData::FORMAT_DXT5:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
case image::CompressedData::FORMAT_BC4:
|
||||
case image::CompressedImageData::FORMAT_BC4:
|
||||
return GL_COMPRESSED_RED_RGTC1;
|
||||
case image::CompressedData::FORMAT_BC4s:
|
||||
case image::CompressedImageData::FORMAT_BC4s:
|
||||
return GL_COMPRESSED_SIGNED_RED_RGTC1;
|
||||
case image::CompressedData::FORMAT_BC5:
|
||||
case image::CompressedImageData::FORMAT_BC5:
|
||||
return GL_COMPRESSED_RG_RGTC2;
|
||||
case image::CompressedData::FORMAT_BC5s:
|
||||
case image::CompressedImageData::FORMAT_BC5s:
|
||||
return GL_COMPRESSED_SIGNED_RG_RGTC2;
|
||||
case image::CompressedData::FORMAT_BC6H:
|
||||
case image::CompressedImageData::FORMAT_BC6H:
|
||||
return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
|
||||
case image::CompressedData::FORMAT_BC6Hs:
|
||||
case image::CompressedImageData::FORMAT_BC6Hs:
|
||||
return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
|
||||
case image::CompressedData::FORMAT_BC7:
|
||||
case image::CompressedImageData::FORMAT_BC7:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
|
||||
else
|
||||
return GL_COMPRESSED_RGBA_BPTC_UNORM;
|
||||
case image::CompressedData::FORMAT_ETC1:
|
||||
case image::CompressedImageData::FORMAT_ETC1:
|
||||
// The ETC2 format can load ETC1 textures.
|
||||
if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility)
|
||||
return GL_COMPRESSED_RGB8_ETC2;
|
||||
else
|
||||
return GL_ETC1_RGB8_OES;
|
||||
case image::CompressedData::FORMAT_ETC2_RGB:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGB:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB8_ETC2;
|
||||
else
|
||||
return GL_COMPRESSED_RGB8_ETC2;
|
||||
case image::CompressedData::FORMAT_ETC2_RGBA:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGBA:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
|
||||
else
|
||||
return GL_COMPRESSED_RGBA8_ETC2_EAC;
|
||||
case image::CompressedData::FORMAT_ETC2_RGBA1:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGBA1:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
|
||||
else
|
||||
return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
|
||||
case image::CompressedData::FORMAT_EAC_R:
|
||||
case image::CompressedImageData::FORMAT_EAC_R:
|
||||
return GL_COMPRESSED_R11_EAC;
|
||||
case image::CompressedData::FORMAT_EAC_Rs:
|
||||
case image::CompressedImageData::FORMAT_EAC_Rs:
|
||||
return GL_COMPRESSED_SIGNED_R11_EAC;
|
||||
case image::CompressedData::FORMAT_EAC_RG:
|
||||
case image::CompressedImageData::FORMAT_EAC_RG:
|
||||
return GL_COMPRESSED_RG11_EAC;
|
||||
case image::CompressedData::FORMAT_EAC_RGs:
|
||||
case image::CompressedImageData::FORMAT_EAC_RGs:
|
||||
return GL_COMPRESSED_SIGNED_RG11_EAC;
|
||||
case image::CompressedData::FORMAT_PVR1_RGB2:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGB2:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
|
||||
case image::CompressedData::FORMAT_PVR1_RGB4:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGB4:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
|
||||
case image::CompressedData::FORMAT_PVR1_RGBA2:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGBA2:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT;
|
||||
else
|
||||
return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
|
||||
case image::CompressedData::FORMAT_PVR1_RGBA4:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGBA4:
|
||||
if (flags.sRGB)
|
||||
return GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT;
|
||||
else
|
||||
@@ -589,40 +589,40 @@ bool Image::hasAnisotropicFilteringSupport()
|
||||
return GLAD_EXT_texture_filter_anisotropic;
|
||||
}
|
||||
|
||||
bool Image::hasCompressedTextureSupport(image::CompressedData::Format format, bool sRGB)
|
||||
bool Image::hasCompressedTextureSupport(image::CompressedImageData::Format format, bool sRGB)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case image::CompressedData::FORMAT_DXT1:
|
||||
case image::CompressedImageData::FORMAT_DXT1:
|
||||
return GLAD_EXT_texture_compression_s3tc || GLAD_EXT_texture_compression_dxt1;
|
||||
case image::CompressedData::FORMAT_DXT3:
|
||||
case image::CompressedImageData::FORMAT_DXT3:
|
||||
return GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt3;
|
||||
case image::CompressedData::FORMAT_DXT5:
|
||||
case image::CompressedImageData::FORMAT_DXT5:
|
||||
return GLAD_EXT_texture_compression_s3tc || GLAD_ANGLE_texture_compression_dxt5;
|
||||
case image::CompressedData::FORMAT_BC4:
|
||||
case image::CompressedData::FORMAT_BC4s:
|
||||
case image::CompressedData::FORMAT_BC5:
|
||||
case image::CompressedData::FORMAT_BC5s:
|
||||
case image::CompressedImageData::FORMAT_BC4:
|
||||
case image::CompressedImageData::FORMAT_BC4s:
|
||||
case image::CompressedImageData::FORMAT_BC5:
|
||||
case image::CompressedImageData::FORMAT_BC5s:
|
||||
return (GLAD_VERSION_3_0 || GLAD_ARB_texture_compression_rgtc || GLAD_EXT_texture_compression_rgtc);
|
||||
case image::CompressedData::FORMAT_BC6H:
|
||||
case image::CompressedData::FORMAT_BC6Hs:
|
||||
case image::CompressedData::FORMAT_BC7:
|
||||
case image::CompressedImageData::FORMAT_BC6H:
|
||||
case image::CompressedImageData::FORMAT_BC6Hs:
|
||||
case image::CompressedImageData::FORMAT_BC7:
|
||||
return GLAD_VERSION_4_2 || GLAD_ARB_texture_compression_bptc;
|
||||
case image::CompressedData::FORMAT_ETC1:
|
||||
case image::CompressedImageData::FORMAT_ETC1:
|
||||
// ETC2 support guarantees ETC1 support as well.
|
||||
return GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility || GLAD_OES_compressed_ETC1_RGB8_texture;
|
||||
case image::CompressedData::FORMAT_ETC2_RGB:
|
||||
case image::CompressedData::FORMAT_ETC2_RGBA:
|
||||
case image::CompressedData::FORMAT_ETC2_RGBA1:
|
||||
case image::CompressedData::FORMAT_EAC_R:
|
||||
case image::CompressedData::FORMAT_EAC_Rs:
|
||||
case image::CompressedData::FORMAT_EAC_RG:
|
||||
case image::CompressedData::FORMAT_EAC_RGs:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGB:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGBA:
|
||||
case image::CompressedImageData::FORMAT_ETC2_RGBA1:
|
||||
case image::CompressedImageData::FORMAT_EAC_R:
|
||||
case image::CompressedImageData::FORMAT_EAC_Rs:
|
||||
case image::CompressedImageData::FORMAT_EAC_RG:
|
||||
case image::CompressedImageData::FORMAT_EAC_RGs:
|
||||
return GLAD_ES_VERSION_3_0 || GLAD_VERSION_4_3 || GLAD_ARB_ES3_compatibility;
|
||||
case image::CompressedData::FORMAT_PVR1_RGB2:
|
||||
case image::CompressedData::FORMAT_PVR1_RGB4:
|
||||
case image::CompressedData::FORMAT_PVR1_RGBA2:
|
||||
case image::CompressedData::FORMAT_PVR1_RGBA4:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGB2:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGB4:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGBA2:
|
||||
case image::CompressedImageData::FORMAT_PVR1_RGBA4:
|
||||
if (sRGB)
|
||||
return GLAD_EXT_pvrtc_sRGB;
|
||||
else
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include "common/StringMap.h"
|
||||
#include "common/math.h"
|
||||
#include "image/ImageData.h"
|
||||
#include "image/CompressedData.h"
|
||||
#include "image/CompressedImageData.h"
|
||||
#include "graphics/Texture.h"
|
||||
#include "graphics/Volatile.h"
|
||||
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
*
|
||||
* @param cdata The compressed data from which to load the image.
|
||||
**/
|
||||
Image(love::image::CompressedData *cdata, const Flags &flags);
|
||||
Image(love::image::CompressedImageData *cdata, const Flags &flags);
|
||||
|
||||
virtual ~Image();
|
||||
|
||||
@@ -99,7 +99,7 @@ public:
|
||||
virtual const void *getHandle() const;
|
||||
|
||||
love::image::ImageData *getImageData() const;
|
||||
love::image::CompressedData *getCompressedData() const;
|
||||
love::image::CompressedImageData *getCompressedData() const;
|
||||
|
||||
virtual void setFilter(const Texture::Filter &f);
|
||||
virtual bool setWrap(const Texture::Wrap &w);
|
||||
@@ -108,12 +108,12 @@ public:
|
||||
float getMipmapSharpness() const;
|
||||
|
||||
/**
|
||||
* Whether this Image is using a compressed texture (via CompressedData).
|
||||
* Whether this Image is using a compressed texture (via CompressedImageData).
|
||||
**/
|
||||
bool isCompressed() const;
|
||||
|
||||
/**
|
||||
* Re-uploads the ImageData or CompressedData associated with this Image to
|
||||
* Re-uploads the ImageData or CompressedImageData associated with this Image to
|
||||
* the GPU.
|
||||
**/
|
||||
bool refresh(int xoffset, int yoffset, int w, int h);
|
||||
@@ -126,7 +126,7 @@ public:
|
||||
static FilterMode getDefaultMipmapFilter();
|
||||
|
||||
static bool hasAnisotropicFilteringSupport();
|
||||
static bool hasCompressedTextureSupport(image::CompressedData::Format format, bool sRGB);
|
||||
static bool hasCompressedTextureSupport(image::CompressedImageData::Format format, bool sRGB);
|
||||
static bool hasSRGBSupport();
|
||||
|
||||
static bool getConstant(const char *in, FlagType &out);
|
||||
@@ -145,7 +145,7 @@ private:
|
||||
void loadFromCompressedData();
|
||||
void loadFromImageData();
|
||||
|
||||
GLenum getCompressedFormat(image::CompressedData::Format cformat) const;
|
||||
GLenum getCompressedFormat(image::CompressedImageData::Format cformat) const;
|
||||
|
||||
// The ImageData from which the texture is created. May be null if
|
||||
// Compressed image data was used to create the texture.
|
||||
@@ -153,7 +153,7 @@ private:
|
||||
|
||||
// Or the Compressed Image Data from which the texture is created. May be
|
||||
// null if raw ImageData was used to create the texture.
|
||||
StrongRef<love::image::CompressedData> cdata;
|
||||
StrongRef<love::image::CompressedImageData> cdata;
|
||||
|
||||
// OpenGL texture identifier.
|
||||
GLuint texture;
|
||||
|
||||
@@ -231,7 +231,7 @@ static const char *imageFlagName(Image::FlagType flagtype)
|
||||
int w_newImage(lua_State *L)
|
||||
{
|
||||
love::image::ImageData *data = nullptr;
|
||||
love::image::CompressedData *cdata = nullptr;
|
||||
love::image::CompressedImageData *cdata = nullptr;
|
||||
|
||||
Image::Flags flags;
|
||||
if (!lua_isnoneornil(L, 2))
|
||||
@@ -243,7 +243,7 @@ int w_newImage(lua_State *L)
|
||||
|
||||
bool releasedata = false;
|
||||
|
||||
// Convert to ImageData / CompressedData, if necessary.
|
||||
// Convert to ImageData / CompressedImageData, if necessary.
|
||||
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_ID) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_ID))
|
||||
{
|
||||
love::image::Image *image = Module::getInstance<love::image::Image>(Module::M_IMAGE);
|
||||
@@ -270,8 +270,8 @@ int w_newImage(lua_State *L)
|
||||
// Lua's GC won't release the image data, so we should do it ourselves.
|
||||
releasedata = true;
|
||||
}
|
||||
else if (luax_istype(L, 1, IMAGE_COMPRESSED_DATA_ID))
|
||||
cdata = luax_checktype<love::image::CompressedData>(L, 1, IMAGE_COMPRESSED_DATA_ID);
|
||||
else if (luax_istype(L, 1, IMAGE_COMPRESSED_IMAGE_DATA_ID))
|
||||
cdata = luax_checktype<love::image::CompressedImageData>(L, 1, IMAGE_COMPRESSED_IMAGE_DATA_ID);
|
||||
else
|
||||
data = luax_checktype<love::image::ImageData>(L, 1, IMAGE_IMAGE_DATA_ID);
|
||||
|
||||
@@ -1125,17 +1125,17 @@ int w_getCanvasFormats(lua_State *L)
|
||||
|
||||
int w_getCompressedImageFormats(lua_State *L)
|
||||
{
|
||||
lua_createtable(L, 0, (int) image::CompressedData::FORMAT_MAX_ENUM);
|
||||
lua_createtable(L, 0, (int) image::CompressedImageData::FORMAT_MAX_ENUM);
|
||||
|
||||
for (int i = 0; i < (int) image::CompressedData::FORMAT_MAX_ENUM; i++)
|
||||
for (int i = 0; i < (int) image::CompressedImageData::FORMAT_MAX_ENUM; i++)
|
||||
{
|
||||
image::CompressedData::Format format = (image::CompressedData::Format) i;
|
||||
image::CompressedImageData::Format format = (image::CompressedImageData::Format) i;
|
||||
const char *name = nullptr;
|
||||
|
||||
if (format == image::CompressedData::FORMAT_UNKNOWN)
|
||||
if (format == image::CompressedImageData::FORMAT_UNKNOWN)
|
||||
continue;
|
||||
|
||||
if (!image::CompressedData::getConstant(format, name))
|
||||
if (!image::CompressedImageData::getConstant(format, name))
|
||||
continue;
|
||||
|
||||
luax_pushboolean(L, Image::hasCompressedTextureSupport(format, false));
|
||||
|
||||
@@ -94,7 +94,7 @@ int w_Image_getData(lua_State *L)
|
||||
Image *i = luax_checkimage(L, 1);
|
||||
|
||||
if (i->isCompressed())
|
||||
luax_pushtype(L, IMAGE_COMPRESSED_DATA_ID, i->getCompressedData());
|
||||
luax_pushtype(L, IMAGE_COMPRESSED_IMAGE_DATA_ID, i->getCompressedData());
|
||||
else
|
||||
luax_pushtype(L, IMAGE_IMAGE_DATA_ID, i->getImageData());
|
||||
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
|
||||
CompressedData::CompressedData()
|
||||
: format(FORMAT_UNKNOWN)
|
||||
, sRGB(false)
|
||||
, data(nullptr)
|
||||
, dataSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
CompressedData::~CompressedData()
|
||||
{
|
||||
}
|
||||
|
||||
size_t CompressedData::getSize() const
|
||||
{
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
void *CompressedData::getData() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
int CompressedData::getMipmapCount() const
|
||||
{
|
||||
return (int) dataImages.size();
|
||||
}
|
||||
|
||||
size_t CompressedData::getSize(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].size;
|
||||
}
|
||||
|
||||
void *CompressedData::getData(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return &dataImages[miplevel].data[0];
|
||||
}
|
||||
|
||||
int CompressedData::getWidth(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].width;
|
||||
}
|
||||
|
||||
int CompressedData::getHeight(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].height;
|
||||
}
|
||||
|
||||
CompressedData::Format CompressedData::getFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
bool CompressedData::isSRGB() const
|
||||
{
|
||||
return sRGB;
|
||||
}
|
||||
|
||||
void CompressedData::checkMipmapLevelExists(int miplevel) const
|
||||
{
|
||||
if (miplevel < 0 || miplevel >= (int) dataImages.size())
|
||||
throw love::Exception("Mipmap level %d does not exist", miplevel + 1);
|
||||
}
|
||||
|
||||
bool CompressedData::getConstant(const char *in, CompressedData::Format &out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
}
|
||||
|
||||
bool CompressedData::getConstant(CompressedData::Format in, const char *&out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<CompressedData::Format, CompressedData::FORMAT_MAX_ENUM>::Entry CompressedData::formatEntries[] =
|
||||
{
|
||||
{"unknown", CompressedData::FORMAT_UNKNOWN},
|
||||
{"DXT1", CompressedData::FORMAT_DXT1},
|
||||
{"DXT3", CompressedData::FORMAT_DXT3},
|
||||
{"DXT5", CompressedData::FORMAT_DXT5},
|
||||
{"BC4", CompressedData::FORMAT_BC4},
|
||||
{"BC4s", CompressedData::FORMAT_BC4s},
|
||||
{"BC5", CompressedData::FORMAT_BC5},
|
||||
{"BC5s", CompressedData::FORMAT_BC5s},
|
||||
{"BC6h", CompressedData::FORMAT_BC6H},
|
||||
{"BC6hs", CompressedData::FORMAT_BC6Hs},
|
||||
{"BC7", CompressedData::FORMAT_BC7},
|
||||
{"ETC1", CompressedData::FORMAT_ETC1},
|
||||
{"ETC2rgb", CompressedData::FORMAT_ETC2_RGB},
|
||||
{"ETC2rgba", CompressedData::FORMAT_ETC2_RGBA},
|
||||
{"ETC2rgba1", CompressedData::FORMAT_ETC2_RGBA1},
|
||||
{"EACr", CompressedData::FORMAT_EAC_R},
|
||||
{"EACrs", CompressedData::FORMAT_EAC_Rs},
|
||||
{"EACrg", CompressedData::FORMAT_EAC_RG},
|
||||
{"EACrgs", CompressedData::FORMAT_EAC_RGs},
|
||||
{"PVR1rgb2", CompressedData::FORMAT_PVR1_RGB2},
|
||||
{"PVR1rgb4", CompressedData::FORMAT_PVR1_RGB4},
|
||||
{"PVR1rgba2", CompressedData::FORMAT_PVR1_RGBA2},
|
||||
{"PVR1rgba4", CompressedData::FORMAT_PVR1_RGBA4},
|
||||
};
|
||||
|
||||
StringMap<CompressedData::Format, CompressedData::FORMAT_MAX_ENUM> CompressedData::formats(CompressedData::formatEntries, sizeof(CompressedData::formatEntries));
|
||||
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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 "CompressedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
|
||||
CompressedImageData::CompressedImageData()
|
||||
: format(FORMAT_UNKNOWN)
|
||||
, sRGB(false)
|
||||
, data(nullptr)
|
||||
, dataSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
CompressedImageData::~CompressedImageData()
|
||||
{
|
||||
}
|
||||
|
||||
size_t CompressedImageData::getSize() const
|
||||
{
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
void *CompressedImageData::getData() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
int CompressedImageData::getMipmapCount() const
|
||||
{
|
||||
return (int) dataImages.size();
|
||||
}
|
||||
|
||||
size_t CompressedImageData::getSize(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].size;
|
||||
}
|
||||
|
||||
void *CompressedImageData::getData(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return &dataImages[miplevel].data[0];
|
||||
}
|
||||
|
||||
int CompressedImageData::getWidth(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].width;
|
||||
}
|
||||
|
||||
int CompressedImageData::getHeight(int miplevel) const
|
||||
{
|
||||
checkMipmapLevelExists(miplevel);
|
||||
|
||||
return dataImages[miplevel].height;
|
||||
}
|
||||
|
||||
CompressedImageData::Format CompressedImageData::getFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
bool CompressedImageData::isSRGB() const
|
||||
{
|
||||
return sRGB;
|
||||
}
|
||||
|
||||
void CompressedImageData::checkMipmapLevelExists(int miplevel) const
|
||||
{
|
||||
if (miplevel < 0 || miplevel >= (int) dataImages.size())
|
||||
throw love::Exception("Mipmap level %d does not exist", miplevel + 1);
|
||||
}
|
||||
|
||||
bool CompressedImageData::getConstant(const char *in, CompressedImageData::Format &out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
}
|
||||
|
||||
bool CompressedImageData::getConstant(CompressedImageData::Format in, const char *&out)
|
||||
{
|
||||
return formats.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<CompressedImageData::Format, CompressedImageData::FORMAT_MAX_ENUM>::Entry CompressedImageData::formatEntries[] =
|
||||
{
|
||||
{"unknown", FORMAT_UNKNOWN},
|
||||
{"DXT1", FORMAT_DXT1},
|
||||
{"DXT3", FORMAT_DXT3},
|
||||
{"DXT5", FORMAT_DXT5},
|
||||
{"BC4", FORMAT_BC4},
|
||||
{"BC4s", FORMAT_BC4s},
|
||||
{"BC5", FORMAT_BC5},
|
||||
{"BC5s", FORMAT_BC5s},
|
||||
{"BC6h", FORMAT_BC6H},
|
||||
{"BC6hs", FORMAT_BC6Hs},
|
||||
{"BC7", FORMAT_BC7},
|
||||
{"ETC1", FORMAT_ETC1},
|
||||
{"ETC2rgb", FORMAT_ETC2_RGB},
|
||||
{"ETC2rgba", FORMAT_ETC2_RGBA},
|
||||
{"ETC2rgba1", FORMAT_ETC2_RGBA1},
|
||||
{"EACr", FORMAT_EAC_R},
|
||||
{"EACrs", FORMAT_EAC_Rs},
|
||||
{"EACrg", FORMAT_EAC_RG},
|
||||
{"EACrgs", FORMAT_EAC_RGs},
|
||||
{"PVR1rgb2", FORMAT_PVR1_RGB2},
|
||||
{"PVR1rgb4", FORMAT_PVR1_RGB4},
|
||||
{"PVR1rgba2", FORMAT_PVR1_RGBA2},
|
||||
{"PVR1rgba4", FORMAT_PVR1_RGBA4},
|
||||
};
|
||||
|
||||
StringMap<CompressedImageData::Format, CompressedImageData::FORMAT_MAX_ENUM> CompressedImageData::formats(CompressedImageData::formatEntries, sizeof(CompressedImageData::formatEntries));
|
||||
|
||||
} // image
|
||||
} // love
|
||||
@@ -18,8 +18,8 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_COMPRESSED_DATA_H
|
||||
#define LOVE_IMAGE_COMPRESSED_DATA_H
|
||||
#ifndef LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "common/Data.h"
|
||||
@@ -35,11 +35,11 @@ namespace image
|
||||
{
|
||||
|
||||
/**
|
||||
* CompressedData represents image data which is designed to be uploaded to the
|
||||
* GPU and rendered in its compressed form, without being un-compressed.
|
||||
* CompressedImageData represents image data which is designed to be uploaded to
|
||||
* the GPU and rendered in its compressed form, without being decompressed.
|
||||
* http://renderingpipeline.com/2012/07/texture-compression/
|
||||
**/
|
||||
class CompressedData : public Data
|
||||
class CompressedImageData : public Data
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -81,8 +81,8 @@ public:
|
||||
uint8 *data; // Should not have ownership of the data.
|
||||
};
|
||||
|
||||
CompressedData();
|
||||
virtual ~CompressedData();
|
||||
CompressedImageData();
|
||||
virtual ~CompressedImageData();
|
||||
|
||||
// Implements Data.
|
||||
virtual void *getData() const;
|
||||
@@ -144,9 +144,9 @@ private:
|
||||
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
|
||||
static StringMap<Format, FORMAT_MAX_ENUM> formats;
|
||||
|
||||
}; // CompressedData
|
||||
}; // CompressedImageData
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_COMPRESSED_DATA_H
|
||||
#endif // LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "common/Module.h"
|
||||
#include "filesystem/File.h"
|
||||
#include "ImageData.h"
|
||||
#include "CompressedData.h"
|
||||
#include "CompressedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -76,11 +76,11 @@ public:
|
||||
virtual ImageData *newImageData(int width, int height, void *data, bool own = false) = 0;
|
||||
|
||||
/**
|
||||
* Creates new CompressedData from FileData.
|
||||
* Creates new CompressedImageData from FileData.
|
||||
* @param data The FileData containing the compressed image data.
|
||||
* @return The new CompressedData.
|
||||
* @return The new CompressedImageData.
|
||||
**/
|
||||
virtual CompressedData *newCompressedData(love::filesystem::FileData *data) = 0;
|
||||
virtual CompressedImageData *newCompressedData(love::filesystem::FileData *data) = 0;
|
||||
|
||||
/**
|
||||
* Determines whether a FileData is Compressed image data or not.
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
// LOVE
|
||||
#include "filesystem/FileData.h"
|
||||
#include "image/CompressedData.h"
|
||||
#include "image/CompressedImageData.h"
|
||||
#include "common/Object.h"
|
||||
|
||||
namespace love
|
||||
@@ -34,7 +34,7 @@ namespace magpie
|
||||
{
|
||||
|
||||
/**
|
||||
* Base class for all CompressedData parser library interfaces.
|
||||
* Base class for all CompressedImageData parser library interfaces.
|
||||
* We inherit from love::Object to take advantage of reference counting...
|
||||
**/
|
||||
class CompressedFormatHandler : public love::Object
|
||||
@@ -45,7 +45,7 @@ public:
|
||||
virtual ~CompressedFormatHandler() {}
|
||||
|
||||
/**
|
||||
* Determines whether a particular FileData can be parsed as CompressedData
|
||||
* Determines whether a particular FileData can be parsed as CompressedImageData
|
||||
* by this handler.
|
||||
* @param data The data to parse.
|
||||
**/
|
||||
@@ -64,8 +64,8 @@ public:
|
||||
*
|
||||
* @return The single block of memory containing the parsed images.
|
||||
**/
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images,
|
||||
size_t &dataSize, CompressedData::Format &format, bool &sRGB) = 0;
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images,
|
||||
size_t &dataSize, CompressedImageData::Format &format, bool &sRGB) = 0;
|
||||
|
||||
}; // CompressedFormatHandler
|
||||
|
||||
|
||||
+3
-3
@@ -18,7 +18,7 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "CompressedData.h"
|
||||
#include "CompressedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -27,7 +27,7 @@ namespace image
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
CompressedData::CompressedData(std::list<CompressedFormatHandler *> formats, love::filesystem::FileData *filedata)
|
||||
CompressedImageData::CompressedImageData(std::list<CompressedFormatHandler *> formats, love::filesystem::FileData *filedata)
|
||||
{
|
||||
CompressedFormatHandler *parser = nullptr;
|
||||
|
||||
@@ -61,7 +61,7 @@ CompressedData::CompressedData(std::list<CompressedFormatHandler *> formats, lov
|
||||
}
|
||||
}
|
||||
|
||||
CompressedData::~CompressedData()
|
||||
CompressedImageData::~CompressedImageData()
|
||||
{
|
||||
delete[] data;
|
||||
}
|
||||
+8
-8
@@ -18,13 +18,13 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
|
||||
#define LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
|
||||
#ifndef LOVE_IMAGE_MAGPIE_COMPRESSED_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_MAGPIE_COMPRESSED_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "CompressedFormatHandler.h"
|
||||
#include "filesystem/FileData.h"
|
||||
#include "image/CompressedData.h"
|
||||
#include "image/CompressedImageData.h"
|
||||
|
||||
// C++
|
||||
#include <list>
|
||||
@@ -36,17 +36,17 @@ namespace image
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
class CompressedData : public love::image::CompressedData
|
||||
class CompressedImageData : public love::image::CompressedImageData
|
||||
{
|
||||
public:
|
||||
|
||||
CompressedData(std::list<CompressedFormatHandler *> formats, love::filesystem::FileData *filedata);
|
||||
virtual ~CompressedData();
|
||||
CompressedImageData(std::list<CompressedFormatHandler *> formats, love::filesystem::FileData *filedata);
|
||||
virtual ~CompressedImageData();
|
||||
|
||||
}; // CompressedData
|
||||
}; // CompressedImageData
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
|
||||
#endif // LOVE_IMAGE_MAGPIE_COMPRESSED_IMAGE_DATA_H
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "Image.h"
|
||||
|
||||
#include "ImageData.h"
|
||||
#include "CompressedData.h"
|
||||
#include "CompressedImageData.h"
|
||||
|
||||
#include "JPEGHandler.h"
|
||||
#include "PNGHandler.h"
|
||||
@@ -91,9 +91,9 @@ love::image::ImageData *Image::newImageData(int width, int height, void *data, b
|
||||
return new ImageData(formatHandlers, width, height, data, own);
|
||||
}
|
||||
|
||||
love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data)
|
||||
love::image::CompressedImageData *Image::newCompressedData(love::filesystem::FileData *data)
|
||||
{
|
||||
return new CompressedData(compressedFormatHandlers, data);
|
||||
return new CompressedImageData(compressedFormatHandlers, data);
|
||||
}
|
||||
|
||||
bool Image::isCompressed(love::filesystem::FileData *data)
|
||||
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
love::image::ImageData *newImageData(int width, int height);
|
||||
love::image::ImageData *newImageData(int width, int height, void *data, bool own = false);
|
||||
|
||||
love::image::CompressedData *newCompressedData(love::filesystem::FileData *data);
|
||||
love::image::CompressedImageData *newCompressedData(love::filesystem::FileData *data);
|
||||
|
||||
bool isCompressed(love::filesystem::FileData *data);
|
||||
|
||||
@@ -64,7 +64,7 @@ private:
|
||||
// Image format handlers we can use for decoding and encoding ImageData.
|
||||
std::list<FormatHandler *> formatHandlers;
|
||||
|
||||
// Compressed image format handers we can use for parsing CompressedData.
|
||||
// Compressed image format handers we can use for parsing CompressedImageData.
|
||||
std::list<CompressedFormatHandler *> compressedFormatHandlers;
|
||||
|
||||
}; // Image
|
||||
|
||||
@@ -90,53 +90,53 @@ enum KTXGLInternalFormat
|
||||
KTX_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3
|
||||
};
|
||||
|
||||
CompressedData::Format convertFormat(uint32 glformat, bool &sRGB)
|
||||
CompressedImageData::Format convertFormat(uint32 glformat, bool &sRGB)
|
||||
{
|
||||
sRGB = false;
|
||||
|
||||
switch (glformat)
|
||||
{
|
||||
case KTX_GL_ETC1_RGB8_OES:
|
||||
return CompressedData::FORMAT_ETC1;
|
||||
return CompressedImageData::FORMAT_ETC1;
|
||||
case KTX_GL_COMPRESSED_R11_EAC:
|
||||
return CompressedData::FORMAT_EAC_R;
|
||||
return CompressedImageData::FORMAT_EAC_R;
|
||||
case KTX_GL_COMPRESSED_SIGNED_R11_EAC:
|
||||
return CompressedData::FORMAT_EAC_Rs;
|
||||
return CompressedImageData::FORMAT_EAC_Rs;
|
||||
case KTX_GL_COMPRESSED_RG11_EAC:
|
||||
return CompressedData::FORMAT_EAC_RG;
|
||||
return CompressedImageData::FORMAT_EAC_RG;
|
||||
case KTX_GL_COMPRESSED_SIGNED_RG11_EAC:
|
||||
return CompressedData::FORMAT_EAC_RGs;
|
||||
return CompressedImageData::FORMAT_EAC_RGs;
|
||||
case KTX_GL_COMPRESSED_RGB8_ETC2:
|
||||
return CompressedData::FORMAT_ETC2_RGB;
|
||||
return CompressedImageData::FORMAT_ETC2_RGB;
|
||||
case KTX_GL_COMPRESSED_SRGB8_ETC2:
|
||||
sRGB = true;
|
||||
return CompressedData::FORMAT_ETC2_RGB;
|
||||
return CompressedImageData::FORMAT_ETC2_RGB;
|
||||
case KTX_GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
|
||||
return CompressedData::FORMAT_ETC2_RGBA1;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA1;
|
||||
case KTX_GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
|
||||
sRGB = true;
|
||||
return CompressedData::FORMAT_ETC2_RGBA1;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA1;
|
||||
case KTX_GL_COMPRESSED_RGBA8_ETC2_EAC:
|
||||
return CompressedData::FORMAT_ETC2_RGBA;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA;
|
||||
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
|
||||
sRGB = true;
|
||||
return CompressedData::FORMAT_ETC2_RGBA;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA;
|
||||
case KTX_GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
|
||||
return CompressedData::FORMAT_PVR1_RGB4;
|
||||
return CompressedImageData::FORMAT_PVR1_RGB4;
|
||||
case KTX_GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
|
||||
return CompressedData::FORMAT_PVR1_RGB2;
|
||||
return CompressedImageData::FORMAT_PVR1_RGB2;
|
||||
case KTX_GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
|
||||
return CompressedData::FORMAT_PVR1_RGBA4;
|
||||
return CompressedImageData::FORMAT_PVR1_RGBA4;
|
||||
case KTX_GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
|
||||
return CompressedData::FORMAT_PVR1_RGBA2;
|
||||
return CompressedImageData::FORMAT_PVR1_RGBA2;
|
||||
case KTX_GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
|
||||
return CompressedData::FORMAT_DXT1;
|
||||
return CompressedImageData::FORMAT_DXT1;
|
||||
case KTX_GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
|
||||
return CompressedData::FORMAT_DXT3;
|
||||
return CompressedImageData::FORMAT_DXT3;
|
||||
case KTX_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
|
||||
return CompressedData::FORMAT_DXT5;
|
||||
return CompressedImageData::FORMAT_DXT5;
|
||||
default:
|
||||
return CompressedData::FORMAT_UNKNOWN;
|
||||
return CompressedImageData::FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ bool KTXHandler::canParse(const filesystem::FileData *data)
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8 *KTXHandler::parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB)
|
||||
uint8 *KTXHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
|
||||
{
|
||||
if (!canParse(filedata))
|
||||
throw love::Exception("Could not decode compressed data (not a KTX file?)");
|
||||
@@ -176,9 +176,9 @@ uint8 *KTXHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
header.numberOfMipmapLevels = std::max(header.numberOfMipmapLevels, 1u);
|
||||
|
||||
bool isSRGB = false;
|
||||
CompressedData::Format cformat = convertFormat(header.glInternalFormat, isSRGB);
|
||||
CompressedImageData::Format cformat = convertFormat(header.glInternalFormat, isSRGB);
|
||||
|
||||
if (cformat == CompressedData::FORMAT_UNKNOWN)
|
||||
if (cformat == CompressedImageData::FORMAT_UNKNOWN)
|
||||
throw love::Exception("Unsupported image format in KTX file.");
|
||||
|
||||
if (header.numberOfArrayElements > 0)
|
||||
@@ -241,7 +241,7 @@ uint8 *KTXHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
|
||||
uint32 mipsizepadded = (mipsize + 3) & ~uint32(3);
|
||||
|
||||
CompressedData::SubImage mip;
|
||||
CompressedImageData::SubImage mip;
|
||||
mip.width = (int) std::max(header.pixelWidth >> i, 1u);
|
||||
mip.height = (int) std::max(header.pixelHeight >> i, 1u);
|
||||
mip.size = mipsize;
|
||||
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
|
||||
// Implements CompressedFormatHandler.
|
||||
virtual bool canParse(const filesystem::FileData *data);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB);
|
||||
|
||||
}; // KTXHandler
|
||||
|
||||
|
||||
@@ -68,29 +68,29 @@ enum PKMTextureFormat
|
||||
ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS
|
||||
};
|
||||
|
||||
CompressedData::Format convertFormat(uint16 texformat)
|
||||
static CompressedImageData::Format convertFormat(uint16 texformat)
|
||||
{
|
||||
switch (texformat)
|
||||
{
|
||||
case ETC1_RGB_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_ETC1;
|
||||
return CompressedImageData::FORMAT_ETC1;
|
||||
case ETC2PACKAGE_RGB_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_ETC2_RGB;
|
||||
return CompressedImageData::FORMAT_ETC2_RGB;
|
||||
case ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD:
|
||||
case ETC2PACKAGE_RGBA_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_ETC2_RGBA;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA;
|
||||
case ETC2PACKAGE_RGBA1_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_ETC2_RGBA1;
|
||||
return CompressedImageData::FORMAT_ETC2_RGBA1;
|
||||
case ETC2PACKAGE_R_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_EAC_R;
|
||||
return CompressedImageData::FORMAT_EAC_R;
|
||||
case ETC2PACKAGE_RG_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_EAC_RG;
|
||||
return CompressedImageData::FORMAT_EAC_RG;
|
||||
case ETC2PACKAGE_R_SIGNED_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_EAC_Rs;
|
||||
return CompressedImageData::FORMAT_EAC_Rs;
|
||||
case ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS:
|
||||
return CompressedData::FORMAT_EAC_RGs;
|
||||
return CompressedImageData::FORMAT_EAC_RGs;
|
||||
default:
|
||||
return CompressedData::FORMAT_UNKNOWN;
|
||||
return CompressedImageData::FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ bool PKMHandler::canParse(const filesystem::FileData *data)
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8 *PKMHandler::parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB)
|
||||
uint8 *PKMHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
|
||||
{
|
||||
if (!canParse(filedata))
|
||||
throw love::Exception("Could not decode compressed data (not a PKM file?)");
|
||||
@@ -126,9 +126,9 @@ uint8 *PKMHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
header.widthBig = swap16big(header.widthBig);
|
||||
header.heightBig = swap16big(header.heightBig);
|
||||
|
||||
CompressedData::Format cformat = convertFormat(header.textureFormatBig);
|
||||
CompressedImageData::Format cformat = convertFormat(header.textureFormatBig);
|
||||
|
||||
if (cformat == CompressedData::FORMAT_UNKNOWN)
|
||||
if (cformat == CompressedImageData::FORMAT_UNKNOWN)
|
||||
throw love::Exception("Could not parse PKM file: unsupported texture format.");
|
||||
|
||||
// The rest of the file after the header is all texture data.
|
||||
@@ -147,7 +147,7 @@ uint8 *PKMHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
// PKM files only store a single mipmap level.
|
||||
memcpy(data, (uint8 *) filedata->getData() + sizeof(PKMHeader), totalsize);
|
||||
|
||||
CompressedData::SubImage mip;
|
||||
CompressedImageData::SubImage mip;
|
||||
|
||||
// TODO: verify whether glCompressedTexImage works properly with the unpadded
|
||||
// width and height values (extended == padded.)
|
||||
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
|
||||
// Implements CompressedFormatHandler.
|
||||
virtual bool canParse(const filesystem::FileData *data);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB);
|
||||
|
||||
}; // PKMHandler
|
||||
|
||||
|
||||
@@ -163,28 +163,28 @@ void ConvertPVRHeader(PVRTexHeaderV2 header2, PVRTexHeaderV3 *header3)
|
||||
}
|
||||
}
|
||||
|
||||
CompressedData::Format convertFormat(PVRV3PixelFormat format)
|
||||
static CompressedImageData::Format convertFormat(PVRV3PixelFormat format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case ePVRTPF_PVRTCI_2bpp_RGB:
|
||||
return CompressedData::FORMAT_PVR1_RGB2;
|
||||
return CompressedImageData::FORMAT_PVR1_RGB2;
|
||||
case ePVRTPF_PVRTCI_2bpp_RGBA:
|
||||
return CompressedData::FORMAT_PVR1_RGBA2;
|
||||
return CompressedImageData::FORMAT_PVR1_RGBA2;
|
||||
case ePVRTPF_PVRTCI_4bpp_RGB:
|
||||
return CompressedData::FORMAT_PVR1_RGB4;
|
||||
return CompressedImageData::FORMAT_PVR1_RGB4;
|
||||
case ePVRTPF_PVRTCI_4bpp_RGBA:
|
||||
return CompressedData::FORMAT_PVR1_RGBA4;
|
||||
return CompressedImageData::FORMAT_PVR1_RGBA4;
|
||||
case ePVRTPF_ETC1:
|
||||
return CompressedData::FORMAT_ETC1;
|
||||
return CompressedImageData::FORMAT_ETC1;
|
||||
case ePVRTPF_DXT1:
|
||||
return CompressedData::FORMAT_DXT1;
|
||||
return CompressedImageData::FORMAT_DXT1;
|
||||
case ePVRTPF_DXT3:
|
||||
return CompressedData::FORMAT_DXT3;
|
||||
return CompressedImageData::FORMAT_DXT3;
|
||||
case ePVRTPF_DXT5:
|
||||
return CompressedData::FORMAT_DXT5;
|
||||
return CompressedImageData::FORMAT_DXT5;
|
||||
default:
|
||||
return CompressedData::FORMAT_UNKNOWN;
|
||||
return CompressedImageData::FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ bool PVRHandler::canParse(const filesystem::FileData *data)
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8 *PVRHandler::parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB)
|
||||
uint8 *PVRHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
|
||||
{
|
||||
if (!canParse(filedata))
|
||||
throw love::Exception("Could not decode compressed data (not a PVR file?)");
|
||||
@@ -324,9 +324,9 @@ uint8 *PVRHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
if (header3.depth > 1)
|
||||
throw love::Exception("Image depths greater than 1 in PVR files are unsupported.");
|
||||
|
||||
CompressedData::Format cformat = convertFormat((PVRV3PixelFormat) header3.pixelFormat);
|
||||
CompressedImageData::Format cformat = convertFormat((PVRV3PixelFormat) header3.pixelFormat);
|
||||
|
||||
if (cformat == CompressedData::FORMAT_UNKNOWN)
|
||||
if (cformat == CompressedImageData::FORMAT_UNKNOWN)
|
||||
throw love::Exception("Could not parse PVR file: unsupported image format.");
|
||||
|
||||
size_t totalsize = 0;
|
||||
@@ -361,7 +361,7 @@ uint8 *PVRHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
if (curoffset + mipsize > totalsize)
|
||||
break; // Just in case.
|
||||
|
||||
CompressedData::SubImage mip;
|
||||
CompressedImageData::SubImage mip;
|
||||
mip.width = std::max((int) header3.width >> i, 1);
|
||||
mip.height = std::max((int) header3.height >> i, 1);
|
||||
mip.size = mipsize;
|
||||
|
||||
@@ -40,7 +40,7 @@ public:
|
||||
|
||||
// Implements CompressedFormatHandler.
|
||||
virtual bool canParse(const filesystem::FileData *data);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB);
|
||||
|
||||
}; // PVRHandler
|
||||
|
||||
|
||||
@@ -32,12 +32,12 @@ bool DDSHandler::canParse(const filesystem::FileData *data)
|
||||
return dds::isCompressedDDS(data->getData(), data->getSize());
|
||||
}
|
||||
|
||||
uint8 *DDSHandler::parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB)
|
||||
uint8 *DDSHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
|
||||
{
|
||||
if (!dds::isDDS(filedata->getData(), filedata->getSize()))
|
||||
throw love::Exception("Could not decode compressed data (not a DDS file?)");
|
||||
|
||||
CompressedData::Format texformat = CompressedData::FORMAT_UNKNOWN;
|
||||
CompressedImageData::Format texformat = CompressedImageData::FORMAT_UNKNOWN;
|
||||
bool isSRGB = false;
|
||||
|
||||
uint8 *data = nullptr;
|
||||
@@ -51,7 +51,7 @@ uint8 *DDSHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
|
||||
texformat = convertFormat(parser.getFormat(), isSRGB);
|
||||
|
||||
if (texformat == CompressedData::FORMAT_UNKNOWN)
|
||||
if (texformat == CompressedImageData::FORMAT_UNKNOWN)
|
||||
throw love::Exception("Could not parse compressed data: Unsupported format.");
|
||||
|
||||
if (parser.getMipmapCount() == 0)
|
||||
@@ -68,13 +68,13 @@ uint8 *DDSHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
|
||||
size_t dataOffset = 0;
|
||||
|
||||
// Copy the parsed mipmap levels from the FileData to our CompressedData.
|
||||
// Copy the parsed mipmap levels from the FileData to our CompressedImageData.
|
||||
for (size_t i = 0; i < parser.getMipmapCount(); i++)
|
||||
{
|
||||
// Fetch the data for this mipmap level.
|
||||
const dds::Image *img = parser.getImageData(i);
|
||||
|
||||
CompressedData::SubImage mip;
|
||||
CompressedImageData::SubImage mip;
|
||||
|
||||
mip.width = img->width;
|
||||
mip.height = img->height;
|
||||
@@ -101,37 +101,37 @@ uint8 *DDSHandler::parse(filesystem::FileData *filedata, std::vector<CompressedD
|
||||
return data;
|
||||
}
|
||||
|
||||
CompressedData::Format DDSHandler::convertFormat(dds::Format ddsformat, bool &sRGB)
|
||||
CompressedImageData::Format DDSHandler::convertFormat(dds::Format ddsformat, bool &sRGB)
|
||||
{
|
||||
sRGB = false;
|
||||
|
||||
switch (ddsformat)
|
||||
{
|
||||
case dds::FORMAT_DXT1:
|
||||
return CompressedData::FORMAT_DXT1;
|
||||
return CompressedImageData::FORMAT_DXT1;
|
||||
case dds::FORMAT_DXT3:
|
||||
return CompressedData::FORMAT_DXT3;
|
||||
return CompressedImageData::FORMAT_DXT3;
|
||||
case dds::FORMAT_DXT5:
|
||||
return CompressedData::FORMAT_DXT5;
|
||||
return CompressedImageData::FORMAT_DXT5;
|
||||
case dds::FORMAT_BC4:
|
||||
return CompressedData::FORMAT_BC4;
|
||||
return CompressedImageData::FORMAT_BC4;
|
||||
case dds::FORMAT_BC4s:
|
||||
return CompressedData::FORMAT_BC4s;
|
||||
return CompressedImageData::FORMAT_BC4s;
|
||||
case dds::FORMAT_BC5:
|
||||
return CompressedData::FORMAT_BC5;
|
||||
return CompressedImageData::FORMAT_BC5;
|
||||
case dds::FORMAT_BC5s:
|
||||
return CompressedData::FORMAT_BC5s;
|
||||
return CompressedImageData::FORMAT_BC5s;
|
||||
case dds::FORMAT_BC6H:
|
||||
return CompressedData::FORMAT_BC6H;
|
||||
return CompressedImageData::FORMAT_BC6H;
|
||||
case dds::FORMAT_BC6Hs:
|
||||
return CompressedData::FORMAT_BC6Hs;
|
||||
return CompressedImageData::FORMAT_BC6Hs;
|
||||
case dds::FORMAT_BC7:
|
||||
return CompressedData::FORMAT_BC7;
|
||||
return CompressedImageData::FORMAT_BC7;
|
||||
case dds::FORMAT_BC7srgb:
|
||||
sRGB = true;
|
||||
return CompressedData::FORMAT_BC7;
|
||||
return CompressedImageData::FORMAT_BC7;
|
||||
default:
|
||||
return CompressedData::FORMAT_UNKNOWN;
|
||||
return CompressedImageData::FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace magpie
|
||||
{
|
||||
|
||||
/**
|
||||
* Interface between CompressedData and the ddsparse library.
|
||||
* Interface between CompressedImageData and the ddsparse library.
|
||||
**/
|
||||
class DDSHandler : public CompressedFormatHandler
|
||||
{
|
||||
@@ -48,11 +48,11 @@ public:
|
||||
|
||||
// Implements CompressedFormatHandler.
|
||||
virtual bool canParse(const filesystem::FileData *data);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedData::SubImage> &images, size_t &dataSize, CompressedData::Format &format, bool &sRGB);
|
||||
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB);
|
||||
|
||||
private:
|
||||
|
||||
static CompressedData::Format convertFormat(dds::Format ddsformat, bool &sRGB);
|
||||
static CompressedImageData::Format convertFormat(dds::Format ddsformat, bool &sRGB);
|
||||
|
||||
}; // DDSHandler
|
||||
|
||||
|
||||
+22
-22
@@ -18,7 +18,7 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "wrap_CompressedData.h"
|
||||
#include "wrap_CompressedImageData.h"
|
||||
#include "common/wrap_Data.h"
|
||||
|
||||
namespace love
|
||||
@@ -26,14 +26,14 @@ namespace love
|
||||
namespace image
|
||||
{
|
||||
|
||||
CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
|
||||
CompressedImageData *luax_checkcompressedimagedata(lua_State *L, int idx)
|
||||
{
|
||||
return luax_checktype<CompressedData>(L, idx, IMAGE_COMPRESSED_DATA_ID);
|
||||
return luax_checktype<CompressedImageData>(L, idx, IMAGE_COMPRESSED_IMAGE_DATA_ID);
|
||||
}
|
||||
|
||||
int w_CompressedData_getWidth(lua_State *L)
|
||||
int w_CompressedImageData_getWidth(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
|
||||
int miplevel = luaL_optint(L, 2, 1);
|
||||
int width = 0;
|
||||
|
||||
@@ -43,9 +43,9 @@ int w_CompressedData_getWidth(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CompressedData_getHeight(lua_State *L)
|
||||
int w_CompressedImageData_getHeight(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
|
||||
int miplevel = luaL_optint(L, 2, 1);
|
||||
int height = 0;
|
||||
|
||||
@@ -55,9 +55,9 @@ int w_CompressedData_getHeight(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CompressedData_getDimensions(lua_State *L)
|
||||
int w_CompressedImageData_getDimensions(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
|
||||
int miplevel = luaL_optint(L, 2, 1);
|
||||
int width = 0, height = 0;
|
||||
|
||||
@@ -72,21 +72,21 @@ int w_CompressedData_getDimensions(lua_State *L)
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_CompressedData_getMipmapCount(lua_State *L)
|
||||
int w_CompressedImageData_getMipmapCount(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
|
||||
lua_pushinteger(L, t->getMipmapCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CompressedData_getFormat(lua_State *L)
|
||||
int w_CompressedImageData_getFormat(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
|
||||
|
||||
image::CompressedData::Format format = t->getFormat();
|
||||
image::CompressedImageData::Format format = t->getFormat();
|
||||
const char *str;
|
||||
|
||||
if (image::CompressedData::getConstant(format, str))
|
||||
if (image::CompressedImageData::getConstant(format, str))
|
||||
lua_pushstring(L, str);
|
||||
else
|
||||
lua_pushstring(L, "unknown");
|
||||
@@ -101,17 +101,17 @@ static const luaL_Reg functions[] =
|
||||
{ "getPointer", w_Data_getPointer },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getWidth", w_CompressedData_getWidth },
|
||||
{ "getHeight", w_CompressedData_getHeight },
|
||||
{ "getDimensions", w_CompressedData_getDimensions },
|
||||
{ "getMipmapCount", w_CompressedData_getMipmapCount },
|
||||
{ "getFormat", w_CompressedData_getFormat },
|
||||
{ "getWidth", w_CompressedImageData_getWidth },
|
||||
{ "getHeight", w_CompressedImageData_getHeight },
|
||||
{ "getDimensions", w_CompressedImageData_getDimensions },
|
||||
{ "getMipmapCount", w_CompressedImageData_getMipmapCount },
|
||||
{ "getFormat", w_CompressedImageData_getFormat },
|
||||
{ 0, 0 },
|
||||
};
|
||||
|
||||
extern "C" int luaopen_compresseddata(lua_State *L)
|
||||
extern "C" int luaopen_compressedimagedata(lua_State *L)
|
||||
{
|
||||
return luax_register_type(L, IMAGE_COMPRESSED_DATA_ID, functions);
|
||||
return luax_register_type(L, IMAGE_COMPRESSED_IMAGE_DATA_ID, functions);
|
||||
}
|
||||
|
||||
} // image
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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_WRAP_COMRESSED_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_WRAP_COMRESSED_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "common/runtime.h"
|
||||
#include "CompressedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
|
||||
CompressedImageData *luax_checkcompressedimagedata(lua_State *L, int idx);
|
||||
int w_CompressedImageData_getWidth(lua_State *L);
|
||||
int w_CompressedImageData_getHeight(lua_State *L);
|
||||
int w_CompressedImageData_getDimensions(lua_State *L);
|
||||
int w_CompressedImageData_getMipmapCount(lua_State *L);
|
||||
int w_CompressedImageData_getFormat(lua_State *L);
|
||||
extern "C" int luaopen_compressedimagedata(lua_State *L);
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_WRAP_COMRESSED_IMAGE_DATA_H
|
||||
@@ -70,13 +70,13 @@ int w_newCompressedData(lua_State *L)
|
||||
{
|
||||
love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
|
||||
|
||||
CompressedData *t = nullptr;
|
||||
CompressedImageData *t = nullptr;
|
||||
luax_catchexcept(L,
|
||||
[&]() { t = instance()->newCompressedData(data); },
|
||||
[&]() { data->release(); }
|
||||
);
|
||||
|
||||
luax_pushtype(L, IMAGE_COMPRESSED_DATA_ID, t);
|
||||
luax_pushtype(L, IMAGE_COMPRESSED_IMAGE_DATA_ID, t);
|
||||
t->release();
|
||||
return 1;
|
||||
}
|
||||
@@ -103,7 +103,7 @@ static const luaL_Reg functions[] =
|
||||
static const lua_CFunction types[] =
|
||||
{
|
||||
luaopen_imagedata,
|
||||
luaopen_compresseddata,
|
||||
luaopen_compressedimagedata,
|
||||
0
|
||||
};
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
// LOVE
|
||||
#include "Image.h"
|
||||
#include "wrap_ImageData.h"
|
||||
#include "wrap_CompressedData.h"
|
||||
#include "wrap_CompressedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "CompressedData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
{
|
||||
|
||||
CompressedData::CompressedData(Compressor::Format format, char *cdata, size_t compressedsize, size_t rawsize, bool own)
|
||||
: format(format)
|
||||
, data(nullptr)
|
||||
, dataSize(compressedsize)
|
||||
, originalSize(rawsize)
|
||||
{
|
||||
if (own)
|
||||
data = cdata;
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
data = new char[dataSize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
memcpy(data, cdata, dataSize);
|
||||
}
|
||||
}
|
||||
|
||||
CompressedData::~CompressedData()
|
||||
{
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
Compressor::Format CompressedData::getFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
size_t CompressedData::getDecompressedSize() const
|
||||
{
|
||||
return originalSize;
|
||||
}
|
||||
|
||||
void *CompressedData::getData() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
size_t CompressedData::getSize() const
|
||||
{
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
} // math
|
||||
} // love
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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_MATH_COMPRESSED_DATA_H
|
||||
#define LOVE_MATH_COMPRESSED_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "common/Data.h"
|
||||
#include "Compressor.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
{
|
||||
|
||||
/**
|
||||
* Stores byte data compressed via Math::compress.
|
||||
**/
|
||||
class CompressedData : public love::Data
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor just stores already-compressed data in the object.
|
||||
**/
|
||||
CompressedData(Compressor::Format format, char *cdata, size_t compressedsize, size_t rawsize, bool own = true);
|
||||
virtual ~CompressedData();
|
||||
|
||||
/**
|
||||
* Gets the format that was used to compress the data.
|
||||
**/
|
||||
Compressor::Format getFormat() const;
|
||||
|
||||
/**
|
||||
* Gets the original (uncompressed) size of the compressed data. May return
|
||||
* 0 if the uncompressed size is unknown.
|
||||
**/
|
||||
size_t getDecompressedSize() const;
|
||||
|
||||
// Implements Data.
|
||||
void *getData() const override;
|
||||
size_t getSize() const override;
|
||||
|
||||
private:
|
||||
|
||||
Compressor::Format format;
|
||||
|
||||
char *data;
|
||||
size_t dataSize;
|
||||
|
||||
size_t originalSize;
|
||||
|
||||
}; // CompressedData
|
||||
|
||||
} // math
|
||||
} // love
|
||||
|
||||
#endif // LOVE_MATH_COMPRESSED_DATA_H
|
||||
@@ -0,0 +1,291 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "Compressor.h"
|
||||
#include "common/config.h"
|
||||
#include "common/int.h"
|
||||
|
||||
// LZ4
|
||||
#include "libraries/lz4/lz4.h"
|
||||
#include "libraries/lz4/lz4hc.h"
|
||||
|
||||
// zlib
|
||||
#include <zlib.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
{
|
||||
|
||||
class LZ4Compressor : public Compressor
|
||||
{
|
||||
public:
|
||||
|
||||
char *compress(const char *data, size_t dataSize, int level, size_t &compressedSize) override
|
||||
{
|
||||
if (dataSize > LZ4_MAX_INPUT_SIZE)
|
||||
throw love::Exception("Data is too large for LZ4 compressor.");
|
||||
|
||||
// We use a custom header to store some info with the compressed data.
|
||||
const size_t headersize = sizeof(uint32);
|
||||
|
||||
size_t maxsize = headersize + (size_t) LZ4_compressBound((int) dataSize);
|
||||
char *compressedbytes = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
compressedbytes = new char[maxsize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
// Store the size of the uncompressed data as a header.
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
// Make sure it's little-endian for storage.
|
||||
*(uint32 *) compressedbytes = swap32((uint32) dataSize);
|
||||
#else
|
||||
*(uint32 *) compressedbytes = (uint32) dataSize;
|
||||
#endif
|
||||
|
||||
// Use LZ4-HC for compression level 8 and higher.
|
||||
int csize = 0;
|
||||
if (level > 7)
|
||||
csize = LZ4_compressHC(data, compressedbytes + headersize, (int) dataSize);
|
||||
else
|
||||
csize = LZ4_compress(data, compressedbytes + headersize, (int) dataSize);
|
||||
|
||||
if (csize <= 0)
|
||||
{
|
||||
delete[] compressedbytes;
|
||||
throw love::Exception("Could not LZ4-compress data.");
|
||||
}
|
||||
|
||||
// We allocated space for the maximum possible amount of data, but the
|
||||
// actual compressed size might be much smaller, so we should shrink the
|
||||
// data buffer if so.
|
||||
if ((double) maxsize / (double) (csize + headersize) >= 1.2)
|
||||
{
|
||||
char *cbytes = new (std::nothrow) char[csize + headersize];
|
||||
if (cbytes)
|
||||
{
|
||||
memcpy(cbytes, compressedbytes, csize + headersize);
|
||||
delete[] compressedbytes;
|
||||
compressedbytes = cbytes;
|
||||
}
|
||||
}
|
||||
|
||||
compressedSize = (size_t) csize + headersize;
|
||||
return compressedbytes;
|
||||
}
|
||||
|
||||
char *decompress(const char *data, size_t dataSize, size_t &decompressedSize) override
|
||||
{
|
||||
const size_t headersize = sizeof(uint32);
|
||||
char *rawbytes = nullptr;
|
||||
|
||||
if (dataSize < headersize)
|
||||
throw love::Exception("Invalid LZ4-compressed data size.");
|
||||
|
||||
// Extract the original uncompressed size (stored in our custom header.)
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
// Convert from stored little-endian to big-endian.
|
||||
uint32 rawsize = swap32(*(uint32 *) data);
|
||||
#else
|
||||
uint32 rawsize = *(uint32 *) data;
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
rawbytes = new char[rawsize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
// If the uncompressed size is passed in as an argument (non-zero) and
|
||||
// it matches the header's stored size, then we assume it's 100% accurate
|
||||
// and we use a more efficient decompression function.
|
||||
if (decompressedSize > 0 && decompressedSize == (size_t) rawsize)
|
||||
{
|
||||
// We don't use the header here, but we need to account for its size.
|
||||
if (LZ4_decompress_fast(data + headersize, rawbytes, (int) decompressedSize) < 0)
|
||||
{
|
||||
delete[] rawbytes;
|
||||
throw love::Exception("Could not decompress LZ4-compressed data.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Account for our custom header's size in the decompress arguments.
|
||||
int result = LZ4_decompress_safe(data + headersize, rawbytes,
|
||||
dataSize - headersize, rawsize);
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
delete[] rawbytes;
|
||||
throw love::Exception("Could not decompress LZ4-compressed data.");
|
||||
}
|
||||
|
||||
decompressedSize = (size_t) result;
|
||||
}
|
||||
|
||||
return rawbytes;
|
||||
}
|
||||
|
||||
Compressor::Format getFormat() const override { return FORMAT_LZ4; }
|
||||
|
||||
}; // LZ4Compressor
|
||||
|
||||
|
||||
class zlibCompressor : public Compressor
|
||||
{
|
||||
public:
|
||||
|
||||
char *compress(const char *data, size_t dataSize, int level, size_t &compressedSize) override
|
||||
{
|
||||
if (level < 0)
|
||||
level = Z_DEFAULT_COMPRESSION;
|
||||
else if (level > 9)
|
||||
level = 9;
|
||||
|
||||
uLong maxsize = compressBound((uLong) dataSize);
|
||||
char *compressedbytes = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
compressedbytes = new char[maxsize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
uLongf destlen = maxsize;
|
||||
int status = compress2((Bytef *) compressedbytes, &destlen, (const Bytef *) data, (uLong) dataSize, level);
|
||||
|
||||
if (status != Z_OK)
|
||||
{
|
||||
delete[] compressedbytes;
|
||||
throw love::Exception("Could not zlib-compress data.");
|
||||
}
|
||||
|
||||
// We allocated space for the maximum possible amount of data, but the
|
||||
// actual compressed size might be much smaller, so we should shrink the
|
||||
// data buffer if so.
|
||||
if ((double) maxsize / (double) destlen >= 1.2)
|
||||
{
|
||||
char *cbytes = new (std::nothrow) char[destlen];
|
||||
if (cbytes)
|
||||
{
|
||||
memcpy(cbytes, compressedbytes, destlen);
|
||||
delete[] compressedbytes;
|
||||
compressedbytes = cbytes;
|
||||
}
|
||||
}
|
||||
|
||||
compressedSize = (size_t) destlen;
|
||||
return compressedbytes;
|
||||
}
|
||||
|
||||
char *decompress(const char *data, size_t dataSize, size_t &decompressedSize) override
|
||||
{
|
||||
char *rawbytes = nullptr;
|
||||
|
||||
// We might know the output size before decompression. If not, we guess.
|
||||
size_t rawsize = decompressedSize > 0 ? decompressedSize : dataSize * 2;
|
||||
|
||||
// Repeatedly try to decompress with an increasingly large output buffer.
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
rawbytes = new char[rawsize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
uLongf destLen = (uLongf) rawsize;
|
||||
int status = uncompress((Bytef *) rawbytes, &destLen, (const Bytef *) data, (uLong) dataSize);
|
||||
|
||||
if (status == Z_OK)
|
||||
{
|
||||
decompressedSize = (size_t) destLen;
|
||||
break;
|
||||
}
|
||||
else if (status != Z_BUF_ERROR)
|
||||
{
|
||||
// For any error other than "not enough room", throw an exception.
|
||||
delete[] rawbytes;
|
||||
throw love::Exception("Could not decompress zlib-compressed data.");
|
||||
}
|
||||
|
||||
// Not enough room in the output buffer: try again with a larger size.
|
||||
delete[] rawbytes;
|
||||
rawsize *= 2;
|
||||
}
|
||||
|
||||
return rawbytes;
|
||||
}
|
||||
|
||||
Compressor::Format getFormat() const override { return FORMAT_ZLIB; }
|
||||
|
||||
}; // zlibCompressor
|
||||
|
||||
|
||||
Compressor *Compressor::Create(Format format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case FORMAT_LZ4:
|
||||
return new LZ4Compressor;
|
||||
case FORMAT_ZLIB:
|
||||
return new zlibCompressor;
|
||||
default:
|
||||
throw love::Exception("Invalid compressor format.");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool Compressor::getConstant(const char *in, Format &out)
|
||||
{
|
||||
return formatNames.find(in, out);
|
||||
}
|
||||
|
||||
bool Compressor::getConstant(Format in, const char *&out)
|
||||
{
|
||||
return formatNames.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<Compressor::Format, Compressor::FORMAT_MAX_ENUM>::Entry Compressor::formatEntries[] =
|
||||
{
|
||||
{"lz4", Compressor::FORMAT_LZ4},
|
||||
{"zlib", Compressor::FORMAT_ZLIB},
|
||||
};
|
||||
|
||||
StringMap<Compressor::Format, Compressor::FORMAT_MAX_ENUM> Compressor::formatNames(Compressor::formatEntries, sizeof(Compressor::formatEntries));
|
||||
|
||||
} // math
|
||||
} // love
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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_MATH_COMPRESSOR_H
|
||||
#define LOVE_MATH_COMPRESSOR_H
|
||||
|
||||
// LOVE
|
||||
#include "common/StringMap.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
{
|
||||
|
||||
/**
|
||||
* Base class for backends for different compression formats.
|
||||
**/
|
||||
class Compressor
|
||||
{
|
||||
public:
|
||||
|
||||
enum Format
|
||||
{
|
||||
FORMAT_LZ4,
|
||||
FORMAT_ZLIB,
|
||||
FORMAT_MAX_ENUM
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new Compressor that can compress and decompress a specific
|
||||
* format.
|
||||
**/
|
||||
static Compressor *Create(Format format);
|
||||
|
||||
virtual ~Compressor() {}
|
||||
|
||||
/**
|
||||
* Compresses input data, and returns the compressed result.
|
||||
*
|
||||
* @param[in] data The input (uncompressed) data.
|
||||
* @param[in] dataSize The size in bytes of the input data.
|
||||
* @param[in] level The amount of compression to apply (between 0 and 9.)
|
||||
* A value of -1 indicates the default amount of compression.
|
||||
* Specific formats may not use every level.
|
||||
* @param[out] compressedSize The size in bytes of the compressed result.
|
||||
*
|
||||
* @return The newly compressed data (allocated with new[]).
|
||||
**/
|
||||
virtual char *compress(const char *data, size_t dataSize, int level, size_t &compressedSize) = 0;
|
||||
|
||||
/**
|
||||
* Decompresses compressed data, and returns the decompressed result.
|
||||
*
|
||||
* @param[in] data The input (compressed) data.
|
||||
* @param[in] dataSize The size in bytes of the compressed data.
|
||||
* @param[inout] decompressedSize On input, the size in bytes of the
|
||||
* original uncompressed data, or 0 if unknown. On return, the
|
||||
* size in bytes of the decompressed data.
|
||||
*
|
||||
* @return The decompressed data (allocated with new[]).
|
||||
**/
|
||||
virtual char *decompress(const char *data, size_t dataSize, size_t &decompressedSize) = 0;
|
||||
|
||||
/**
|
||||
* Gets the compression format implemented by this backend.
|
||||
*
|
||||
* @return The supported format.
|
||||
**/
|
||||
virtual Format getFormat() const = 0;
|
||||
|
||||
static bool getConstant(const char *in, Format &out);
|
||||
static bool getConstant(Format in, const char *&out);
|
||||
|
||||
private:
|
||||
|
||||
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
|
||||
static StringMap<Format, FORMAT_MAX_ENUM> formatNames;
|
||||
|
||||
}; // Compressor
|
||||
|
||||
} // math
|
||||
} // love
|
||||
|
||||
#endif // LOVE_MATH_COMPRESSOR_H
|
||||
@@ -86,9 +86,19 @@ Math Math::instance;
|
||||
|
||||
Math::Math()
|
||||
: rng()
|
||||
, compressors()
|
||||
{
|
||||
// prevent the runtime from free()-ing this
|
||||
retain();
|
||||
|
||||
for (int i = 0; i < (int) Compressor::FORMAT_MAX_ENUM; i++)
|
||||
compressors[i] = Compressor::Create((Compressor::Format) i);
|
||||
}
|
||||
|
||||
Math::~Math()
|
||||
{
|
||||
for (Compressor *c : compressors)
|
||||
delete c;
|
||||
}
|
||||
|
||||
RandomGenerator *Math::newRandomGenerator()
|
||||
@@ -223,5 +233,54 @@ float Math::linearToGamma(float c) const
|
||||
return 1.055f * powf(c, 0.41666f) - 0.055f;
|
||||
}
|
||||
|
||||
CompressedData *Math::compress(Compressor::Format format, love::Data *rawdata, int level)
|
||||
{
|
||||
return compress(format, (const char *) rawdata->getData(), rawdata->getSize(), level);
|
||||
}
|
||||
|
||||
CompressedData *Math::compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level)
|
||||
{
|
||||
if (format == Compressor::FORMAT_MAX_ENUM || !compressors[format])
|
||||
throw love::Exception("Invalid compression format.");
|
||||
|
||||
size_t compressedsize = 0;
|
||||
Compressor *compressor = compressors[format];
|
||||
|
||||
char *cbytes = compressor->compress(rawbytes, rawsize, level, compressedsize);
|
||||
|
||||
CompressedData *data = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
data = new CompressedData(format, cbytes, compressedsize, rawsize, true);
|
||||
}
|
||||
catch (love::Exception &)
|
||||
{
|
||||
delete[] cbytes;
|
||||
throw;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
char *Math::decompress(CompressedData *data, size_t &decompressedsize)
|
||||
{
|
||||
size_t rawsize = data->getDecompressedSize();
|
||||
|
||||
char *rawbytes = decompress(data->getFormat(), (const char *) data->getData(),
|
||||
data->getSize(), rawsize);
|
||||
|
||||
decompressedsize = rawsize;
|
||||
return rawbytes;
|
||||
}
|
||||
|
||||
char *Math::decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize)
|
||||
{
|
||||
if (format == Compressor::FORMAT_MAX_ENUM || !compressors[format])
|
||||
throw love::Exception("Invalid compression format.");
|
||||
|
||||
return compressors[format]->decompress(cbytes, compressedsize, rawsize);
|
||||
}
|
||||
|
||||
} // math
|
||||
} // love
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#define LOVE_MATH_MODMATH_H
|
||||
|
||||
#include "RandomGenerator.h"
|
||||
#include "CompressedData.h"
|
||||
#include "Compressor.h"
|
||||
|
||||
// LOVE
|
||||
#include "common/Module.h"
|
||||
@@ -50,8 +52,7 @@ private:
|
||||
|
||||
public:
|
||||
|
||||
virtual ~Math()
|
||||
{}
|
||||
virtual ~Math();
|
||||
|
||||
/**
|
||||
* @copydoc RandomGenerator::random()
|
||||
@@ -162,12 +163,49 @@ public:
|
||||
float noise(float x, float y, float z) const;
|
||||
float noise(float x, float y, float z, float w) const;
|
||||
|
||||
/**
|
||||
* Compresses a block of memory using the given compression format.
|
||||
*
|
||||
* @param format The compression format to use.
|
||||
* @param rawdata The data to compress.
|
||||
* @param level The amount of compression to apply (between 0 and 9.)
|
||||
* A value of -1 indicates the default amount of compression.
|
||||
* Specific formats may not use every level.
|
||||
* @return The newly compressed data.
|
||||
**/
|
||||
CompressedData *compress(Compressor::Format format, Data *rawdata, int level = -1);
|
||||
CompressedData *compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level = -1);
|
||||
|
||||
/**
|
||||
* Decompresses existing compressed data into raw bytes.
|
||||
*
|
||||
* @param[in] data The compressed data to decompress.
|
||||
* @param[out] decompressedsize The size in bytes of the decompressed data.
|
||||
* @return The newly decompressed data (allocated with new[]).
|
||||
**/
|
||||
char *decompress(CompressedData *data, size_t &decompressedsize);
|
||||
|
||||
/**
|
||||
* Decompresses existing compressed data into raw bytes.
|
||||
*
|
||||
* @param[in] format The compression format the data is in.
|
||||
* @param[in] cbytes The compressed data to decompress.
|
||||
* @param[in] compressedSize The size in bytes of the compressed data.
|
||||
* @param[inout] rawsize On input, the size in bytes of the original
|
||||
* uncompressed data, or 0 if unknown. On return, the size in
|
||||
* bytes of the newly decompressed data.
|
||||
* @return The newly decompressed data (allocated with new[]).
|
||||
**/
|
||||
char *decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize);
|
||||
|
||||
static Math instance;
|
||||
|
||||
private:
|
||||
|
||||
Math();
|
||||
|
||||
Compressor *compressors[Compressor::FORMAT_MAX_ENUM];
|
||||
|
||||
}; // Math
|
||||
|
||||
inline float Math::noise(float x) const
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "wrap_CompressedData.h"
|
||||
#include "common/wrap_Data.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
{
|
||||
|
||||
CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
|
||||
{
|
||||
return luax_checktype<CompressedData>(L, idx, MATH_COMPRESSED_DATA_ID);
|
||||
}
|
||||
|
||||
int w_CompressedData_getFormat(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
|
||||
const char *fname = nullptr;
|
||||
if (!Compressor::getConstant(t->getFormat(), fname))
|
||||
return luaL_error(L, "Unknown compressed data format.");
|
||||
|
||||
lua_pushstring(L, fname);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
// Data
|
||||
{ "getString", w_Data_getString },
|
||||
{ "getPointer", w_Data_getPointer },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getFormat", w_CompressedData_getFormat },
|
||||
{ 0, 0 },
|
||||
};
|
||||
|
||||
|
||||
extern "C" int luaopen_compresseddata(lua_State *L)
|
||||
{
|
||||
return luax_register_type(L, MATH_COMPRESSED_DATA_ID, functions);
|
||||
}
|
||||
|
||||
} // math
|
||||
} // love
|
||||
@@ -18,8 +18,8 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_WRAP_COMRESSED_DATA_H
|
||||
#define LOVE_IMAGE_WRAP_COMRESSED_DATA_H
|
||||
#ifndef LOVE_MATH_WRAP_COMPRESSED_DATA_H
|
||||
#define LOVE_MATH_WRAP_COMPRESSED_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "common/runtime.h"
|
||||
@@ -27,18 +27,14 @@
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
namespace math
|
||||
{
|
||||
|
||||
CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
|
||||
int w_CompressedData_getWidth(lua_State *L);
|
||||
int w_CompressedData_getHeight(lua_State *L);
|
||||
int w_CompressedData_getDimensions(lua_State *L);
|
||||
int w_CompressedData_getMipmapCount(lua_State *L);
|
||||
int w_CompressedData_getFormat(lua_State *L);
|
||||
extern "C" int luaopen_compresseddata(lua_State *L);
|
||||
|
||||
} // image
|
||||
} // math
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_WRAP_COMRESSED_DATA_H
|
||||
#endif // LOVE_MATH_WRAP_COMPRESSED_DATA_H
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "wrap_Math.h"
|
||||
#include "wrap_RandomGenerator.h"
|
||||
#include "wrap_BezierCurve.h"
|
||||
#include "wrap_CompressedData.h"
|
||||
#include "MathModule.h"
|
||||
#include "BezierCurve.h"
|
||||
|
||||
@@ -351,6 +352,64 @@ int w_noise(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_compress(lua_State *L)
|
||||
{
|
||||
const char *fstr = lua_isnoneornil(L, 2) ? nullptr : luaL_checkstring(L, 2);
|
||||
Compressor::Format format = Compressor::FORMAT_LZ4;
|
||||
|
||||
if (fstr && !Compressor::getConstant(fstr, format))
|
||||
return luaL_error(L, "Invalid compressed format: %s", fstr);
|
||||
|
||||
int level = luaL_optint(L, 3, -1);
|
||||
|
||||
CompressedData *cdata = nullptr;
|
||||
if (lua_isstring(L, 1))
|
||||
{
|
||||
size_t rawsize = 0;
|
||||
const char *rawbytes = luaL_checklstring(L, 1, &rawsize);
|
||||
luax_catchexcept(L, [&](){ cdata = Math::instance.compress(format, rawbytes, rawsize, level); });
|
||||
}
|
||||
else
|
||||
{
|
||||
Data *rawdata = luax_checktype<Data>(L, 1, DATA_ID);
|
||||
luax_catchexcept(L, [&](){ cdata = Math::instance.compress(format, rawdata, level); });
|
||||
}
|
||||
|
||||
luax_pushtype(L, MATH_COMPRESSED_DATA_ID, cdata);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_decompress(lua_State *L)
|
||||
{
|
||||
char *rawbytes = nullptr;
|
||||
size_t rawsize = 0;
|
||||
|
||||
if (lua_isstring(L, 1))
|
||||
{
|
||||
Compressor::Format format = Compressor::FORMAT_LZ4;
|
||||
const char *fstr = luaL_checkstring(L, 2);
|
||||
|
||||
if (!Compressor::getConstant(fstr, format))
|
||||
return luaL_error(L, "Invalid compressed format: %s", fstr);
|
||||
|
||||
size_t compressedsize = 0;
|
||||
const char *cbytes = luaL_checklstring(L, 1, &compressedsize);
|
||||
|
||||
luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(format, cbytes, compressedsize, rawsize); });
|
||||
}
|
||||
else
|
||||
{
|
||||
CompressedData *data = luax_checkcompresseddata(L, 1);
|
||||
rawsize = data->getDecompressedSize();
|
||||
luax_catchexcept(L, [&](){ rawbytes = Math::instance.decompress(data, rawsize); });
|
||||
}
|
||||
|
||||
lua_pushlstring(L, rawbytes, rawsize);
|
||||
delete[] rawbytes;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
@@ -367,6 +426,8 @@ static const luaL_Reg functions[] =
|
||||
{ "gammaToLinear", w_gammaToLinear },
|
||||
{ "linearToGamma", w_linearToGamma },
|
||||
{ "noise", w_noise },
|
||||
{ "compress", w_compress },
|
||||
{ "decompress", w_decompress },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@@ -374,6 +435,7 @@ static const lua_CFunction types[] =
|
||||
{
|
||||
luaopen_randomgenerator,
|
||||
luaopen_beziercurve,
|
||||
luaopen_compresseddata,
|
||||
0
|
||||
};
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@ int w_isConvex(lua_State *L);
|
||||
int w_gammaToLinear(lua_State *L);
|
||||
int w_linearToGamma(lua_State *L);
|
||||
int w_noise(lua_State *L);
|
||||
int w_compress(lua_State *L);
|
||||
int w_decompress(lua_State *L);
|
||||
extern "C" LOVE_EXPORT int luaopen_love_math(lua_State *L);
|
||||
|
||||
} // random
|
||||
|
||||
Reference in New Issue
Block a user