mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
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:
@@ -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
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* 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_COMPRESSED_DATA_H
|
||||
#define LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "filesystem/File.h"
|
||||
#include "image/CompressedData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
class CompressedData : public love::image::CompressedData
|
||||
{
|
||||
public:
|
||||
|
||||
CompressedData(love::filesystem::FileData *data);
|
||||
virtual ~CompressedData();
|
||||
|
||||
static bool isCompressed(love::filesystem::FileData *data);
|
||||
|
||||
private:
|
||||
|
||||
void load(love::filesystem::FileData *data);
|
||||
|
||||
}; // CompressedData
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_MAGPIE_COMPRESSED_DATA_H
|
||||
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
* 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 "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;
|
||||
|
||||
static Mutex *devilMutex = 0;
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
static inline void ilxClearErrors()
|
||||
{
|
||||
while (ilGetError() != IL_NO_ERROR);
|
||||
}
|
||||
|
||||
void DevilHandler::init()
|
||||
{
|
||||
ilInit();
|
||||
ilEnable(IL_ORIGIN_SET);
|
||||
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
||||
}
|
||||
|
||||
void DevilHandler::quit()
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ImageData::FORMAT_BMP:
|
||||
case ImageData::FORMAT_TGA:
|
||||
case ImageData::FORMAT_JPG:
|
||||
case ImageData::FORMAT_PNG:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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 = ilLoadL(IL_TYPE_UNKNOWN, (void *)data->getData(), data->getSize()) == IL_TRUE;
|
||||
|
||||
if (!success)
|
||||
throw love::Exception("Could not decode image!");
|
||||
|
||||
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);
|
||||
|
||||
// This should always be four.
|
||||
int bpp = ilGetInteger(IL_IMAGE_BPP);
|
||||
if (bpp != sizeof(pixel))
|
||||
throw love::Exception("Could not convert image!");
|
||||
|
||||
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)
|
||||
{
|
||||
// catches love and std exceptions
|
||||
ilDeleteImage(image);
|
||||
throw love::Exception("%s", e.what());
|
||||
}
|
||||
|
||||
ilDeleteImage(image);
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
DevilHandler::EncodedImage DevilHandler::encode(const DecodedImage &img, ImageData::Format format)
|
||||
{
|
||||
if (!devilMutex)
|
||||
devilMutex = thread::newMutex();
|
||||
|
||||
Lock lock(devilMutex);
|
||||
|
||||
ILuint tempimage = ilGenImage();
|
||||
ilBindImage(tempimage);
|
||||
ilxClearErrors();
|
||||
|
||||
EncodedImage encodedimg;
|
||||
|
||||
try
|
||||
{
|
||||
bool success = ilTexImage(img.width, img.height, 1, sizeof(pixel), IL_RGBA, IL_UNSIGNED_BYTE, img.data) == IL_TRUE;
|
||||
|
||||
ILenum err = ilGetError();
|
||||
ilxClearErrors();
|
||||
|
||||
if (!success)
|
||||
{
|
||||
if (err != IL_NO_ERROR)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case IL_ILLEGAL_OPERATION:
|
||||
throw love::Exception("Illegal operation");
|
||||
case IL_INVALID_PARAM:
|
||||
throw love::Exception("Invalid parameters");
|
||||
case IL_OUT_OF_MEMORY:
|
||||
throw love::Exception("Out of memory");
|
||||
default:
|
||||
throw love::Exception("Unknown error (%d)", (int) err);
|
||||
}
|
||||
}
|
||||
|
||||
throw love::Exception("Could not create image for the encoding!");
|
||||
}
|
||||
|
||||
ilRegisterOrigin(IL_ORIGIN_UPPER_LEFT);
|
||||
|
||||
ILuint ilFormat;
|
||||
switch (format)
|
||||
{
|
||||
case ImageData::FORMAT_BMP:
|
||||
ilFormat = IL_BMP;
|
||||
break;
|
||||
case ImageData::FORMAT_TGA:
|
||||
ilFormat = IL_TGA;
|
||||
break;
|
||||
case ImageData::FORMAT_JPG:
|
||||
ilFormat = IL_JPG;
|
||||
break;
|
||||
case ImageData::FORMAT_PNG:
|
||||
default: // PNG is the default format
|
||||
ilFormat = IL_PNG;
|
||||
break;
|
||||
}
|
||||
|
||||
encodedimg.size = ilSaveL(ilFormat, NULL, 0);
|
||||
if (!encodedimg.size)
|
||||
throw love::Exception("Could not encode image!");
|
||||
|
||||
try
|
||||
{
|
||||
encodedimg.data = new ILubyte[encodedimg.size];
|
||||
}
|
||||
catch(std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory");
|
||||
}
|
||||
|
||||
ilSaveL(ilFormat, encodedimg.data, encodedimg.size);
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
// catches love and std exceptions
|
||||
ilDeleteImage(tempimage);
|
||||
delete[] encodedimg.data;
|
||||
throw love::Exception("%s", e.what());
|
||||
}
|
||||
|
||||
ilDeleteImage(tempimage);
|
||||
|
||||
return encodedimg;
|
||||
}
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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 "Image.h"
|
||||
|
||||
#include "imageData.h"
|
||||
#include "CompressedData.h"
|
||||
|
||||
#include "DevilHandler.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
Image::Image()
|
||||
{
|
||||
DevilHandler::init();
|
||||
}
|
||||
|
||||
Image::~Image()
|
||||
{
|
||||
DevilHandler::quit();
|
||||
}
|
||||
|
||||
const char *Image::getName() const
|
||||
{
|
||||
return "love.image.magpie";
|
||||
}
|
||||
|
||||
love::image::ImageData *Image::newImageData(love::filesystem::FileData *data)
|
||||
{
|
||||
return new ImageData(data);
|
||||
}
|
||||
|
||||
love::image::ImageData *Image::newImageData(int width, int height)
|
||||
{
|
||||
return new ImageData(width, height);
|
||||
}
|
||||
|
||||
love::image::ImageData *Image::newImageData(int width, int height, void *data)
|
||||
{
|
||||
return new ImageData(width, height, data);
|
||||
}
|
||||
|
||||
love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data)
|
||||
{
|
||||
return new CompressedData(data);
|
||||
}
|
||||
|
||||
bool Image::isCompressed(love::filesystem::FileData *data)
|
||||
{
|
||||
return CompressedData::isCompressed(data);
|
||||
}
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* 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_IMAGE_H
|
||||
#define LOVE_IMAGE_MAGPIE_IMAGE_H
|
||||
|
||||
// LOVE
|
||||
#include "image/Image.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
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:
|
||||
|
||||
Image();
|
||||
~Image();
|
||||
|
||||
// Implements Module.
|
||||
const char *getName() const;
|
||||
|
||||
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::FileData *data);
|
||||
|
||||
bool isCompressed(love::filesystem::FileData *data);
|
||||
|
||||
}; // Image
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_MAGPIE_IMAGE_H
|
||||
@@ -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
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_MAGPIE_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "filesystem/File.h"
|
||||
#include "image/ImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
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);
|
||||
|
||||
// Decode and load an encoded format.
|
||||
void decode(love::filesystem::FileData *data);
|
||||
|
||||
}; // ImageData
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_MAGPIE_IMAGE_DATA_H
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* 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 "ddsHandler.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
bool ddsHandler::canParse(const filesystem::FileData *data)
|
||||
{
|
||||
if (!accepts(data->getExtension()))
|
||||
return false;
|
||||
|
||||
return dds::Parser::isCompressedDDS(data->getData(), data->getSize());
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
textype = convertFormat(parser.getFormat());
|
||||
|
||||
if (textype == CompressedData::TYPE_UNKNOWN)
|
||||
throw love::Exception("Could not parse compressed data: Unsupported format.");
|
||||
|
||||
if (parser.getNumMipmaps() == 0)
|
||||
throw love::Exception("Could not parse compressed data: No readable texture data.");
|
||||
|
||||
for (size_t i = 0; i < parser.getNumMipmaps(); i++)
|
||||
{
|
||||
const dds::Parser::Image *img = parser.getImageData(i);
|
||||
|
||||
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]);
|
||||
|
||||
images.push_back(mip);
|
||||
}
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
throw love::Exception(e.what());
|
||||
}
|
||||
|
||||
return textype;
|
||||
}
|
||||
|
||||
CompressedData::TextureType ddsHandler::convertFormat(dds::Format ddsformat)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user