mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
Removed the DevIL backend from love.image. Replaced it with PNG and JPEG encoders/decoders via LodePNG, zlib, and libjpeg-turbo.
PNG and JPEG are now the only two supported image formats, aside from compressed DDS (DXT, etc.) --HG-- branch : minor
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2014 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 "PNGHandler.h"
|
||||
|
||||
// LOVE
|
||||
#include "common/Exception.h"
|
||||
#include "common/math.h"
|
||||
|
||||
// LodePNG
|
||||
#include "lodepng/lodepng.h"
|
||||
|
||||
// zlib
|
||||
#include <zlib.h>
|
||||
|
||||
// C++
|
||||
#include <algorithm>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
// Custom PNG decompression function for LodePNG, using zlib.
|
||||
static unsigned zlibDecompress(unsigned char **out, size_t *outsize, const unsigned char *in,
|
||||
size_t insize, const LodePNGDecompressSettings* /*settings*/)
|
||||
{
|
||||
int status = Z_OK;
|
||||
|
||||
size_t outdataSize = insize;
|
||||
size_t sizeMultiplier = 0;
|
||||
unsigned char *outdata = nullptr;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Enough size to hold the decompressed data, hopefully.
|
||||
outdataSize = insize << (++sizeMultiplier);
|
||||
|
||||
try
|
||||
{
|
||||
outdata = new unsigned char[outdataSize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
return 83; // "Memory allocation failed" error code for LodePNG.
|
||||
}
|
||||
|
||||
// Use zlib to decompress the PNG data.
|
||||
status = uncompress(outdata, &outdataSize, in, insize);
|
||||
|
||||
// If the out buffer was big enough, break out of the loop.
|
||||
if (status != Z_BUF_ERROR)
|
||||
break;
|
||||
|
||||
// Otherwise delete the out buffer and try again with a larger size...
|
||||
delete[] outdata;
|
||||
outdata = nullptr;
|
||||
}
|
||||
|
||||
if (status != Z_OK)
|
||||
{
|
||||
delete[] outdata;
|
||||
return 10000; // "Unknown error code" for LodePNG.
|
||||
}
|
||||
|
||||
if (out != nullptr)
|
||||
*out = outdata;
|
||||
|
||||
if (outsize != nullptr)
|
||||
*outsize = outdataSize;
|
||||
|
||||
return 0; // Success.
|
||||
}
|
||||
|
||||
// Custom PNG compression function for LodePNG, using zlib.
|
||||
static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigned char *in,
|
||||
size_t insize, const LodePNGCompressSettings* /*settings*/)
|
||||
{
|
||||
// Get the maximum compressed size of the data.
|
||||
uLongf outdataSize = compressBound(insize);
|
||||
unsigned char *outdata = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
outdata = new unsigned char[outdataSize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
return 83; // "Memory allocation failed" error code for LodePNG.
|
||||
}
|
||||
|
||||
// Use zlib to compress the PNG data.
|
||||
int status = compress(outdata, &outdataSize, in, insize);
|
||||
|
||||
if (status != Z_OK)
|
||||
{
|
||||
delete[] outdata;
|
||||
return 10000; // "Unknown error code" for LodePNG.
|
||||
}
|
||||
|
||||
if (out != nullptr)
|
||||
*out = outdata;
|
||||
|
||||
if (outsize != nullptr)
|
||||
*outsize = (size_t) outdataSize;
|
||||
|
||||
return 0; // Success.
|
||||
}
|
||||
|
||||
bool PNGHandler::canDecode(love::filesystem::FileData *data)
|
||||
{
|
||||
std::string ext = data->getExtension();
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
|
||||
|
||||
return ext.compare("png") == 0;
|
||||
}
|
||||
|
||||
bool PNGHandler::canEncode(ImageData::Format format)
|
||||
{
|
||||
return format == ImageData::FORMAT_PNG;
|
||||
}
|
||||
|
||||
PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata)
|
||||
{
|
||||
unsigned int width = 0, height = 0;
|
||||
unsigned char *indata = (unsigned char *) fdata->getData();
|
||||
size_t insize = fdata->getSize();
|
||||
|
||||
DecodedImage img;
|
||||
|
||||
LodePNGState state;
|
||||
lodepng_state_init(&state);
|
||||
|
||||
state.info_raw.colortype = LCT_RGBA;
|
||||
state.info_raw.bitdepth = 8;
|
||||
|
||||
state.decoder.zlibsettings.custom_zlib = zlibDecompress;
|
||||
|
||||
unsigned status = lodepng_decode(&img.data, &width, &height,
|
||||
&state, indata, insize);
|
||||
|
||||
lodepng_state_cleanup(&state);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
const char *err = lodepng_error_text(status);
|
||||
throw love::Exception("Could not decode PNG image (%s)", err);
|
||||
}
|
||||
|
||||
img.width = (int) width;
|
||||
img.height = (int) height;
|
||||
img.size = width * height * 4;
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
PNGHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, ImageData::Format format)
|
||||
{
|
||||
if (format != ImageData::FORMAT_PNG)
|
||||
throw love::Exception("PNG encoder cannot encode to non-PNG format.");
|
||||
|
||||
EncodedImage encimg;
|
||||
|
||||
LodePNGState state;
|
||||
lodepng_state_init(&state);
|
||||
|
||||
state.info_raw.colortype = LCT_RGBA;
|
||||
state.info_raw.bitdepth = 8;
|
||||
|
||||
// TODO: support plain RGB (24-bit) encoding in the future?
|
||||
state.info_png.color.colortype = LCT_RGBA;
|
||||
state.info_png.color.bitdepth = 8;
|
||||
|
||||
state.encoder.zlibsettings.custom_zlib = zlibCompress;
|
||||
|
||||
unsigned status = lodepng_encode(&encimg.data, &encimg.size,
|
||||
img.data, img.width, img.height, &state);
|
||||
|
||||
lodepng_state_cleanup(&state);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
const char *err = lodepng_error_text(status);
|
||||
throw love::Exception("Could not encode PNG image (%s)", err);
|
||||
}
|
||||
|
||||
return encimg;
|
||||
}
|
||||
|
||||
void PNGHandler::free(unsigned char *mem)
|
||||
{
|
||||
// LodePNG uses malloc, realloc, and free.
|
||||
if (mem)
|
||||
::free(mem);
|
||||
}
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
Reference in New Issue
Block a user