mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Removed libjpeg-turbo as a dependency (closes issue #1031.) Removed jpeg support for ImageData:encode. stb_image now handles jpeg image loading.
This commit is contained in:
@@ -28,7 +28,7 @@ namespace image
|
||||
{
|
||||
|
||||
ImageData::ImageData()
|
||||
: data(0)
|
||||
: data(nullptr)
|
||||
{
|
||||
mutex = thread::newMutex();
|
||||
}
|
||||
@@ -179,7 +179,6 @@ bool ImageData::getConstant(ImageData::Format in, const char *&out)
|
||||
StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] =
|
||||
{
|
||||
{"tga", ImageData::FORMAT_TGA},
|
||||
{"jpg", ImageData::FORMAT_JPG},
|
||||
{"png", ImageData::FORMAT_PNG},
|
||||
};
|
||||
|
||||
|
||||
@@ -50,7 +50,6 @@ public:
|
||||
enum Format
|
||||
{
|
||||
FORMAT_TGA,
|
||||
FORMAT_JPG,
|
||||
FORMAT_PNG,
|
||||
FORMAT_MAX_ENUM
|
||||
};
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "ImageData.h"
|
||||
#include "CompressedImageData.h"
|
||||
|
||||
#include "JPEGHandler.h"
|
||||
#include "PNGHandler.h"
|
||||
#include "STBHandler.h"
|
||||
#include "ImageIOHandler.h"
|
||||
@@ -43,11 +42,6 @@ namespace magpie
|
||||
Image::Image()
|
||||
{
|
||||
formatHandlers.push_back(new PNGHandler);
|
||||
|
||||
#ifndef LOVE_NO_TURBOJPEG
|
||||
formatHandlers.push_back(new JPEGHandler);
|
||||
#endif
|
||||
|
||||
formatHandlers.push_back(new STBHandler);
|
||||
|
||||
#ifdef LOVE_SUPPORT_IMAGEIO
|
||||
|
||||
@@ -55,8 +55,6 @@ static CFStringRef getFormatType(ImageData::Format format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case ImageData::FORMAT_JPG:
|
||||
return kUTTypeJPEG;
|
||||
case ImageData::FORMAT_TGA:
|
||||
return CFSTR("com.truevision.tga-image");
|
||||
case ImageData::FORMAT_PNG:
|
||||
|
||||
@@ -1,168 +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 "JPEGHandler.h"
|
||||
|
||||
#ifndef LOVE_NO_TURBOJPEG
|
||||
|
||||
// LOVE
|
||||
#include "common/Exception.h"
|
||||
#include "common/math.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
JPEGHandler::JPEGHandler()
|
||||
{
|
||||
mutex = love::thread::newMutex();
|
||||
|
||||
decompressor = tjInitDecompress();
|
||||
compressor = tjInitCompress();
|
||||
}
|
||||
|
||||
JPEGHandler::~JPEGHandler()
|
||||
{
|
||||
delete mutex;
|
||||
|
||||
if (decompressor)
|
||||
tjDestroy(decompressor);
|
||||
|
||||
if (compressor)
|
||||
tjDestroy(compressor);
|
||||
}
|
||||
|
||||
bool JPEGHandler::canDecode(love::filesystem::FileData *data)
|
||||
{
|
||||
if (!decompressor)
|
||||
return false;
|
||||
|
||||
love::thread::Lock lock(mutex);
|
||||
|
||||
int w, h, subsamp;
|
||||
int status = tjDecompressHeader2(decompressor,
|
||||
(unsigned char *) data->getData(),
|
||||
data->getSize(),
|
||||
&w, &h, &subsamp);
|
||||
|
||||
return (status == 0);
|
||||
}
|
||||
|
||||
bool JPEGHandler::canEncode(ImageData::Format format)
|
||||
{
|
||||
if (!compressor)
|
||||
return false;
|
||||
|
||||
return format == ImageData::FORMAT_JPG;
|
||||
}
|
||||
|
||||
FormatHandler::DecodedImage JPEGHandler::decode(love::filesystem::FileData *data)
|
||||
{
|
||||
if (!decompressor)
|
||||
throw love::Exception("Could not decode jpeg image: %s", tjGetErrorStr());
|
||||
|
||||
DecodedImage img;
|
||||
unsigned char *jpegdata = (unsigned char *) data->getData();
|
||||
|
||||
love::thread::Lock lock(mutex);
|
||||
|
||||
int unused;
|
||||
int status = tjDecompressHeader2(decompressor,
|
||||
jpegdata, data->getSize(),
|
||||
&img.width, &img.height,
|
||||
&unused);
|
||||
|
||||
if (status < 0)
|
||||
throw love::Exception("Could not decode jpeg image: %s", tjGetErrorStr());
|
||||
|
||||
img.size = img.width * img.height * sizeof(pixel);
|
||||
|
||||
try
|
||||
{
|
||||
img.data = new unsigned char[img.size];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
status = tjDecompress2(decompressor,
|
||||
jpegdata, data->getSize(),
|
||||
img.data, 0, 0, 0, TJPF_RGBA, 0);
|
||||
|
||||
if (status < 0)
|
||||
{
|
||||
delete[] img.data;
|
||||
throw love::Exception("Could not decode jpeg image: %s", tjGetErrorStr());
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
FormatHandler::EncodedImage JPEGHandler::encode(const DecodedImage &img, ImageData::Format format)
|
||||
{
|
||||
if (!canEncode(format))
|
||||
throw love::Exception("JPEG encoder cannot encode specified format.");
|
||||
|
||||
EncodedImage encodedimage;
|
||||
|
||||
love::thread::Lock lock(mutex);
|
||||
|
||||
unsigned long tjsize = tjBufSize(img.width, img.height, TJSAMP_444);
|
||||
|
||||
try
|
||||
{
|
||||
// We want to allocate the memory ourselves instead of letting
|
||||
// TurboJPEG do it, so we can safely use delete[] in ImageData.cpp.
|
||||
encodedimage.data = new unsigned char[tjsize];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
unsigned long jpegSize = tjsize;
|
||||
|
||||
int status = tjCompress2(compressor,
|
||||
img.data,
|
||||
img.width, 0, img.height,
|
||||
TJPF_RGBA,
|
||||
&encodedimage.data, &jpegSize,
|
||||
TJSAMP_444, COMPRESS_QUALITY, TJFLAG_NOREALLOC);
|
||||
|
||||
if (status < 0)
|
||||
{
|
||||
delete[] encodedimage.data;
|
||||
throw love::Exception("Could not encode jpeg image: %s", tjGetErrorStr());
|
||||
}
|
||||
|
||||
encodedimage.size = jpegSize;
|
||||
|
||||
return encodedimage;
|
||||
}
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_NO_TURBOJPEG
|
||||
@@ -1,81 +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.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_MAGPIE_JPEG_HANDLER_H
|
||||
#define LOVE_IMAGE_MAGPIE_JPEG_HANDLER_H
|
||||
|
||||
#include "common/config.h"
|
||||
|
||||
#ifndef LOVE_NO_TURBOJPEG
|
||||
|
||||
// LOVE
|
||||
#include "FormatHandler.h"
|
||||
#include "thread/threads.h"
|
||||
|
||||
// libjpeg-turbo
|
||||
#ifdef LOVE_APPLE_USE_FRAMEWORKS
|
||||
#include <jpeg-turbo/turbojpeg.h>
|
||||
#else
|
||||
#include <turbojpeg.h>
|
||||
#endif
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace magpie
|
||||
{
|
||||
|
||||
/**
|
||||
* Interface between ImageData and TurboJPEG.
|
||||
**/
|
||||
class JPEGHandler : public FormatHandler
|
||||
{
|
||||
public:
|
||||
|
||||
// Implements FormatHandler.
|
||||
|
||||
JPEGHandler();
|
||||
virtual ~JPEGHandler();
|
||||
|
||||
virtual bool canDecode(love::filesystem::FileData *data);
|
||||
virtual bool canEncode(ImageData::Format format);
|
||||
|
||||
virtual DecodedImage decode(love::filesystem::FileData *data);
|
||||
virtual EncodedImage encode(const DecodedImage &img, ImageData::Format format);
|
||||
|
||||
private:
|
||||
|
||||
Mutex *mutex;
|
||||
|
||||
tjhandle decompressor;
|
||||
tjhandle compressor;
|
||||
|
||||
static const int COMPRESS_QUALITY = 90;
|
||||
|
||||
}; // JPEGHandler
|
||||
|
||||
} // magpie
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_NO_TURBOJPEG
|
||||
|
||||
#endif // LOVE_IMAGE_MAGPIE_JPEG_HANDLER_H
|
||||
@@ -21,20 +21,20 @@
|
||||
// LOVE
|
||||
#include "STBHandler.h"
|
||||
|
||||
static void loveSTBAssert(bool test, const char *teststr)
|
||||
static void loveSTBIAssert(bool test, const char *teststr)
|
||||
{
|
||||
if (!test)
|
||||
throw love::Exception("Could not decode image (stb_image assertion '%s' failed)", teststr);
|
||||
}
|
||||
|
||||
// stb_image
|
||||
// #define STBI_ONLY_JPEG
|
||||
#define STBI_ONLY_JPEG
|
||||
// #define STBI_ONLY_PNG
|
||||
#define STBI_ONLY_BMP
|
||||
#define STBI_ONLY_TGA
|
||||
#define STBI_NO_STDIO
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_ASSERT(A) loveSTBAssert((A), #A)
|
||||
#define STBI_ASSERT(A) loveSTBIAssert((A), #A)
|
||||
#include "libraries/stb/stb_image.h"
|
||||
|
||||
// C
|
||||
@@ -75,7 +75,7 @@ FormatHandler::DecodedImage STBHandler::decode(love::filesystem::FileData *data)
|
||||
&comp, 4);
|
||||
|
||||
if (img.data == nullptr || img.width <= 0 || img.height <= 0)
|
||||
throw love::Exception("Could not decode TGA or BMP image.");
|
||||
throw love::Exception("Could not decode image with stb_image.");
|
||||
|
||||
img.size = img.width * img.height * 4;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user