mirror of
https://github.com/love2d/love.git
synced 2026-08-19 12:14:20 +02:00
Removed iOS-specific code to decode and encode images using the ImageIO framework (it's now redundant.)
This commit is contained in:
@@ -486,9 +486,6 @@
|
|||||||
FA0B7D961A95902C000E1D17 /* ImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD21A95902C000E1D17 /* ImageData.cpp */; };
|
FA0B7D961A95902C000E1D17 /* ImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD21A95902C000E1D17 /* ImageData.cpp */; };
|
||||||
FA0B7D971A95902C000E1D17 /* ImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD21A95902C000E1D17 /* ImageData.cpp */; };
|
FA0B7D971A95902C000E1D17 /* ImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD21A95902C000E1D17 /* ImageData.cpp */; };
|
||||||
FA0B7D981A95902C000E1D17 /* ImageData.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7BD31A95902C000E1D17 /* ImageData.h */; };
|
FA0B7D981A95902C000E1D17 /* ImageData.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7BD31A95902C000E1D17 /* ImageData.h */; };
|
||||||
FA0B7D991A95902C000E1D17 /* ImageIOHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */; };
|
|
||||||
FA0B7D9A1A95902C000E1D17 /* ImageIOHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */; };
|
|
||||||
FA0B7D9B1A95902C000E1D17 /* ImageIOHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7BD51A95902C000E1D17 /* ImageIOHandler.h */; };
|
|
||||||
FA0B7D9F1A95902C000E1D17 /* KTXHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */; };
|
FA0B7D9F1A95902C000E1D17 /* KTXHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */; };
|
||||||
FA0B7DA01A95902C000E1D17 /* KTXHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */; };
|
FA0B7DA01A95902C000E1D17 /* KTXHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */; };
|
||||||
FA0B7DA11A95902C000E1D17 /* KTXHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7BD91A95902C000E1D17 /* KTXHandler.h */; };
|
FA0B7DA11A95902C000E1D17 /* KTXHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7BD91A95902C000E1D17 /* KTXHandler.h */; };
|
||||||
@@ -1245,8 +1242,6 @@
|
|||||||
FA0B7BD11A95902C000E1D17 /* Image.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Image.h; sourceTree = "<group>"; };
|
FA0B7BD11A95902C000E1D17 /* Image.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Image.h; sourceTree = "<group>"; };
|
||||||
FA0B7BD21A95902C000E1D17 /* ImageData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImageData.cpp; sourceTree = "<group>"; };
|
FA0B7BD21A95902C000E1D17 /* ImageData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImageData.cpp; sourceTree = "<group>"; };
|
||||||
FA0B7BD31A95902C000E1D17 /* ImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageData.h; sourceTree = "<group>"; };
|
FA0B7BD31A95902C000E1D17 /* ImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageData.h; sourceTree = "<group>"; };
|
||||||
FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImageIOHandler.cpp; sourceTree = "<group>"; };
|
|
||||||
FA0B7BD51A95902C000E1D17 /* ImageIOHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageIOHandler.h; sourceTree = "<group>"; };
|
|
||||||
FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KTXHandler.cpp; sourceTree = "<group>"; };
|
FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KTXHandler.cpp; sourceTree = "<group>"; };
|
||||||
FA0B7BD91A95902C000E1D17 /* KTXHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KTXHandler.h; sourceTree = "<group>"; };
|
FA0B7BD91A95902C000E1D17 /* KTXHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KTXHandler.h; sourceTree = "<group>"; };
|
||||||
FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PKMHandler.cpp; sourceTree = "<group>"; };
|
FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PKMHandler.cpp; sourceTree = "<group>"; };
|
||||||
@@ -2295,8 +2290,6 @@
|
|||||||
FA0B7BD11A95902C000E1D17 /* Image.h */,
|
FA0B7BD11A95902C000E1D17 /* Image.h */,
|
||||||
FA0B7BD21A95902C000E1D17 /* ImageData.cpp */,
|
FA0B7BD21A95902C000E1D17 /* ImageData.cpp */,
|
||||||
FA0B7BD31A95902C000E1D17 /* ImageData.h */,
|
FA0B7BD31A95902C000E1D17 /* ImageData.h */,
|
||||||
FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */,
|
|
||||||
FA0B7BD51A95902C000E1D17 /* ImageIOHandler.h */,
|
|
||||||
FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */,
|
FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */,
|
||||||
FA0B7BD91A95902C000E1D17 /* KTXHandler.h */,
|
FA0B7BD91A95902C000E1D17 /* KTXHandler.h */,
|
||||||
FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */,
|
FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */,
|
||||||
@@ -3006,7 +2999,6 @@
|
|||||||
FA0B7E991A95902C000E1D17 /* Sound.h in Headers */,
|
FA0B7E991A95902C000E1D17 /* Sound.h in Headers */,
|
||||||
FA0B7D841A95902C000E1D17 /* CompressedImageData.h in Headers */,
|
FA0B7D841A95902C000E1D17 /* CompressedImageData.h in Headers */,
|
||||||
FA0B7D231A95902C000E1D17 /* Rasterizer.h in Headers */,
|
FA0B7D231A95902C000E1D17 /* Rasterizer.h in Headers */,
|
||||||
FA0B7D9B1A95902C000E1D17 /* ImageIOHandler.h in Headers */,
|
|
||||||
FA0B7CDB1A95902C000E1D17 /* Pool.h in Headers */,
|
FA0B7CDB1A95902C000E1D17 /* Pool.h in Headers */,
|
||||||
FA0B7D0B1A95902C000E1D17 /* wrap_FileData.h in Headers */,
|
FA0B7D0B1A95902C000E1D17 /* wrap_FileData.h in Headers */,
|
||||||
FA0B7DF91A95902C000E1D17 /* Body.h in Headers */,
|
FA0B7DF91A95902C000E1D17 /* Body.h in Headers */,
|
||||||
@@ -3361,7 +3353,6 @@
|
|||||||
FA620A331AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */,
|
FA620A331AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */,
|
||||||
FA0B7E071A95902C000E1D17 /* DistanceJoint.cpp in Sources */,
|
FA0B7E071A95902C000E1D17 /* DistanceJoint.cpp in Sources */,
|
||||||
FA0B7A331A958EA3000E1D17 /* b2Collision.cpp in Sources */,
|
FA0B7A331A958EA3000E1D17 /* b2Collision.cpp in Sources */,
|
||||||
FA0B7D9A1A95902C000E1D17 /* ImageIOHandler.cpp in Sources */,
|
|
||||||
FA0B7E5E1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */,
|
FA0B7E5E1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */,
|
||||||
FA0B7B3B1A958EA3000E1D17 /* wuff_memory.c in Sources */,
|
FA0B7B3B1A958EA3000E1D17 /* wuff_memory.c in Sources */,
|
||||||
FA0B79411A958E3B000E1D17 /* utf8.cpp in Sources */,
|
FA0B79411A958E3B000E1D17 /* utf8.cpp in Sources */,
|
||||||
@@ -3645,7 +3636,6 @@
|
|||||||
FA0B7E061A95902C000E1D17 /* DistanceJoint.cpp in Sources */,
|
FA0B7E061A95902C000E1D17 /* DistanceJoint.cpp in Sources */,
|
||||||
FA620A321AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */,
|
FA620A321AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */,
|
||||||
FA0B793B1A958E3B000E1D17 /* runtime.cpp in Sources */,
|
FA0B793B1A958E3B000E1D17 /* runtime.cpp in Sources */,
|
||||||
FA0B7D991A95902C000E1D17 /* ImageIOHandler.cpp in Sources */,
|
|
||||||
FA0B7E5D1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */,
|
FA0B7E5D1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */,
|
||||||
FA0B7A741A958EA3000E1D17 /* b2ChainAndPolygonContact.cpp in Sources */,
|
FA0B7A741A958EA3000E1D17 /* b2ChainAndPolygonContact.cpp in Sources */,
|
||||||
FA0B7AD41A958EA3000E1D17 /* unix.c in Sources */,
|
FA0B7AD41A958EA3000E1D17 /* unix.c in Sources */,
|
||||||
@@ -3859,7 +3849,6 @@
|
|||||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
LOVE_SUPPORT_IMAGEIO,
|
|
||||||
LOVE_SUPPORT_COREAUDIO,
|
LOVE_SUPPORT_COREAUDIO,
|
||||||
LOVE_NO_MODPLUG,
|
LOVE_NO_MODPLUG,
|
||||||
LOVE_NOMPG123,
|
LOVE_NOMPG123,
|
||||||
@@ -3901,7 +3890,6 @@
|
|||||||
ENABLE_NS_ASSERTIONS = NO;
|
ENABLE_NS_ASSERTIONS = NO;
|
||||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
LOVE_SUPPORT_IMAGEIO,
|
|
||||||
LOVE_SUPPORT_COREAUDIO,
|
LOVE_SUPPORT_COREAUDIO,
|
||||||
LOVE_NO_MODPLUG,
|
LOVE_NO_MODPLUG,
|
||||||
LOVE_NOMPG123,
|
LOVE_NOMPG123,
|
||||||
@@ -3943,7 +3931,6 @@
|
|||||||
ENABLE_NS_ASSERTIONS = NO;
|
ENABLE_NS_ASSERTIONS = NO;
|
||||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
LOVE_SUPPORT_IMAGEIO,
|
|
||||||
LOVE_SUPPORT_COREAUDIO,
|
LOVE_SUPPORT_COREAUDIO,
|
||||||
LOVE_NO_MODPLUG,
|
LOVE_NO_MODPLUG,
|
||||||
LOVE_NOMPG123,
|
LOVE_NOMPG123,
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
|
|
||||||
#include "PNGHandler.h"
|
#include "PNGHandler.h"
|
||||||
#include "STBHandler.h"
|
#include "STBHandler.h"
|
||||||
#include "ImageIOHandler.h"
|
|
||||||
|
|
||||||
#include "ddsHandler.h"
|
#include "ddsHandler.h"
|
||||||
#include "PVRHandler.h"
|
#include "PVRHandler.h"
|
||||||
@@ -44,10 +43,6 @@ Image::Image()
|
|||||||
formatHandlers.push_back(new PNGHandler);
|
formatHandlers.push_back(new PNGHandler);
|
||||||
formatHandlers.push_back(new STBHandler);
|
formatHandlers.push_back(new STBHandler);
|
||||||
|
|
||||||
#ifdef LOVE_SUPPORT_IMAGEIO
|
|
||||||
formatHandlers.push_back(new ImageIOHandler);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
compressedFormatHandlers.push_back(new DDSHandler);
|
compressedFormatHandlers.push_back(new DDSHandler);
|
||||||
compressedFormatHandlers.push_back(new PVRHandler);
|
compressedFormatHandlers.push_back(new PVRHandler);
|
||||||
compressedFormatHandlers.push_back(new KTXHandler);
|
compressedFormatHandlers.push_back(new KTXHandler);
|
||||||
|
|||||||
@@ -1,276 +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 "ImageIOHandler.h"
|
|
||||||
|
|
||||||
#ifdef LOVE_SUPPORT_IMAGEIO
|
|
||||||
|
|
||||||
// LOVE
|
|
||||||
#include "common/Exception.h"
|
|
||||||
#include "common/math.h"
|
|
||||||
#include "filesystem/File.h"
|
|
||||||
|
|
||||||
// ImageIO
|
|
||||||
#include <ImageIO/ImageIO.h>
|
|
||||||
|
|
||||||
#if defined(LOVE_IOS)
|
|
||||||
#include <MobileCoreServices/MobileCoreServices.h>
|
|
||||||
#elif defined(LOVE_MACOSX)
|
|
||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// STL
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using love::thread::Lock;
|
|
||||||
|
|
||||||
namespace love
|
|
||||||
{
|
|
||||||
namespace image
|
|
||||||
{
|
|
||||||
namespace magpie
|
|
||||||
{
|
|
||||||
|
|
||||||
static const CFStringRef invalidFormat = CFSTR("");
|
|
||||||
|
|
||||||
static CFStringRef getFormatType(ImageData::Format format)
|
|
||||||
{
|
|
||||||
switch (format)
|
|
||||||
{
|
|
||||||
case ImageData::FORMAT_TGA:
|
|
||||||
return CFSTR("com.truevision.tga-image");
|
|
||||||
case ImageData::FORMAT_PNG:
|
|
||||||
return kUTTypePNG;
|
|
||||||
default:
|
|
||||||
return invalidFormat;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageIOHandler::ImageIOHandler()
|
|
||||||
: mutex(nullptr)
|
|
||||||
{
|
|
||||||
mutex = love::thread::newMutex();
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageIOHandler::~ImageIOHandler()
|
|
||||||
{
|
|
||||||
delete mutex;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ImageIOHandler::canDecode(love::filesystem::FileData *data)
|
|
||||||
{
|
|
||||||
CFStringRef cfext = CFStringCreateWithCString(nullptr, data->getExtension().c_str(), kCFStringEncodingUTF8);
|
|
||||||
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, cfext, nullptr);
|
|
||||||
|
|
||||||
CFRelease(cfext);
|
|
||||||
|
|
||||||
if (!UTI)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
CFArrayRef types = CGImageSourceCopyTypeIdentifiers();
|
|
||||||
Boolean isdecodable = CFArrayContainsValue(types, CFRangeMake(0, CFArrayGetCount(types)), UTI);
|
|
||||||
|
|
||||||
CFRelease(UTI);
|
|
||||||
CFRelease(types);
|
|
||||||
|
|
||||||
return isdecodable;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ImageIOHandler::canEncode(ImageData::Format format)
|
|
||||||
{
|
|
||||||
CFStringRef cftype = getFormatType(format);
|
|
||||||
if (cftype == invalidFormat)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Is the format supported for writing on this system?
|
|
||||||
CFArrayRef types = CGImageDestinationCopyTypeIdentifiers();
|
|
||||||
|
|
||||||
Boolean iswritable = CFArrayContainsValue(types, CFRangeMake(0, CFArrayGetCount(types)), cftype);
|
|
||||||
CFRelease(types);
|
|
||||||
|
|
||||||
return iswritable;
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageIOHandler::DecodedImage ImageIOHandler::decode(love::filesystem::FileData *data)
|
|
||||||
{
|
|
||||||
Lock lock(mutex);
|
|
||||||
|
|
||||||
DecodedImage img;
|
|
||||||
|
|
||||||
CFDataRef cfdata = CFDataCreateWithBytesNoCopy(nullptr, (const UInt8 *) data->getData(), data->getSize(), kCFAllocatorNull);
|
|
||||||
CGImageSourceRef source = CGImageSourceCreateWithData(cfdata, nullptr);
|
|
||||||
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, nullptr);
|
|
||||||
|
|
||||||
CFRelease(cfdata);
|
|
||||||
CFRelease(source);
|
|
||||||
|
|
||||||
if (!image)
|
|
||||||
throw love::Exception("Could not decode image!");
|
|
||||||
|
|
||||||
img.width = (int) CGImageGetWidth(image);
|
|
||||||
img.height = (int) CGImageGetHeight(image);
|
|
||||||
|
|
||||||
CGImageAlphaInfo alphainfo = CGImageGetAlphaInfo(image);
|
|
||||||
|
|
||||||
size_t bpp = CGImageGetBitsPerPixel(image);
|
|
||||||
|
|
||||||
// If the bpp doesn't match, we need to draw the image onto a new 32 bpp canvas
|
|
||||||
if (bpp != 32)
|
|
||||||
{
|
|
||||||
// Create a new bitmap canvas
|
|
||||||
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
|
|
||||||
CGContextRef context = CGBitmapContextCreate(nullptr, img.width, img.height, 8, img.width*sizeof(pixel), colorspace, kCGImageAlphaPremultipliedLast);
|
|
||||||
CGColorSpaceRelease(colorspace);
|
|
||||||
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
CGImageRelease(image);
|
|
||||||
throw love::Exception("Could not decode image: error converting to 32 bpp!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw a black background
|
|
||||||
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
|
|
||||||
CGContextFillRect(context, CGRectMake(0, 0, img.width, img.height));
|
|
||||||
|
|
||||||
CGContextDrawImage(context, CGRectMake(0, 0, img.width, img.height), image);
|
|
||||||
|
|
||||||
// Replace the old image with the canvas
|
|
||||||
CGImageRef contextimage = CGBitmapContextCreateImage(context);
|
|
||||||
CGContextRelease(context);
|
|
||||||
|
|
||||||
CGImageRelease(image);
|
|
||||||
image = contextimage;
|
|
||||||
|
|
||||||
if (!image)
|
|
||||||
throw love::Exception("Could not decode image: error converting to 32 bpp!");
|
|
||||||
}
|
|
||||||
|
|
||||||
cfdata = CGDataProviderCopyData(CGImageGetDataProvider(image));
|
|
||||||
CGImageRelease(image);
|
|
||||||
|
|
||||||
img.size = CFDataGetLength(cfdata);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
img.data = new unsigned char[img.size];
|
|
||||||
}
|
|
||||||
catch (std::bad_alloc &)
|
|
||||||
{
|
|
||||||
CFRelease(cfdata);
|
|
||||||
throw love::Exception("Out of memory.");
|
|
||||||
}
|
|
||||||
|
|
||||||
CFDataGetBytes(cfdata, CFRangeMake(0, img.size), (UInt8 *) img.data);
|
|
||||||
CFRelease(cfdata);
|
|
||||||
|
|
||||||
// ImageIO might try to be "helpful" and premultiply the image's alpha for us,
|
|
||||||
// now we have to un-premultiply it ourselves. Thanks...
|
|
||||||
if (alphainfo == kCGImageAlphaPremultipliedLast)
|
|
||||||
{
|
|
||||||
pixel *pixels = (pixel *) img.data;
|
|
||||||
for (int i = 0; i < img.width * img.height; i++)
|
|
||||||
{
|
|
||||||
unsigned char alpha = pixels[i].a;
|
|
||||||
|
|
||||||
if (alpha > 0 && alpha < 255)
|
|
||||||
{
|
|
||||||
pixels[i].r = ((short) pixels[i].r * 255) / alpha;
|
|
||||||
pixels[i].g = ((short) pixels[i].g * 255) / alpha;
|
|
||||||
pixels[i].b = ((short) pixels[i].b * 255) / alpha;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return img;
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageIOHandler::EncodedImage ImageIOHandler::encode(const DecodedImage &img, ImageData::Format format)
|
|
||||||
{
|
|
||||||
Lock lock(mutex);
|
|
||||||
|
|
||||||
EncodedImage encodedimage;
|
|
||||||
|
|
||||||
CFStringRef cftype = getFormatType(format);
|
|
||||||
|
|
||||||
if (cftype == invalidFormat || !canEncode(format))
|
|
||||||
throw love::Exception("Could not encode image: format is not supported on this system.");
|
|
||||||
|
|
||||||
CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, img.data, img.size, nullptr);
|
|
||||||
|
|
||||||
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
|
|
||||||
CGBitmapInfo bitmapinfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
|
|
||||||
CGColorRenderingIntent intent = kCGRenderingIntentDefault;
|
|
||||||
size_t pixsize = sizeof(pixel);
|
|
||||||
|
|
||||||
// Create a CoreGraphics image using the ImageData
|
|
||||||
CGImageRef image = CGImageCreate(img.width, img.height, 8, 8*pixsize, img.width*pixsize, space, bitmapinfo, provider, nullptr, false, intent);
|
|
||||||
|
|
||||||
CGColorSpaceRelease(space);
|
|
||||||
CGDataProviderRelease(provider);
|
|
||||||
|
|
||||||
CFMutableDataRef encodeddata = CFDataCreateMutable(nullptr, 0);
|
|
||||||
if (!encodeddata)
|
|
||||||
{
|
|
||||||
CFRelease(image);
|
|
||||||
throw love::Exception("Could not create image for encoding!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the encoder, using encodeddata as a chunk of memory to store the final result
|
|
||||||
CGImageDestinationRef dest = CGImageDestinationCreateWithData(encodeddata, cftype, 1, nullptr);
|
|
||||||
CGImageDestinationAddImage(dest, image, nullptr);
|
|
||||||
|
|
||||||
// Encode the CoreGraphics image to the specified format
|
|
||||||
bool success = CGImageDestinationFinalize(dest);
|
|
||||||
|
|
||||||
CFRelease(image);
|
|
||||||
CFRelease(dest);
|
|
||||||
|
|
||||||
CFIndex encodedsize = CFDataGetLength(encodeddata);
|
|
||||||
|
|
||||||
if (!success || encodedsize <= 0)
|
|
||||||
{
|
|
||||||
CFRelease(encodeddata);
|
|
||||||
throw love::Exception("Could not encode image!");
|
|
||||||
}
|
|
||||||
|
|
||||||
encodedimage.size = encodedsize;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
encodedimage.data = new unsigned char[encodedsize];
|
|
||||||
}
|
|
||||||
catch (std::bad_alloc &)
|
|
||||||
{
|
|
||||||
CFRelease(encodeddata);
|
|
||||||
throw love::Exception("Out of memory");
|
|
||||||
}
|
|
||||||
|
|
||||||
CFDataGetBytes(encodeddata, CFRangeMake(0, encodedsize), encodedimage.data);
|
|
||||||
CFRelease(encodeddata);
|
|
||||||
|
|
||||||
return encodedimage;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // magpie
|
|
||||||
} // image
|
|
||||||
} // love
|
|
||||||
|
|
||||||
#endif // LOVE_SUPPORT_IMAGEIO
|
|
||||||
@@ -1,65 +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_IMAGEIO_HANDLER_H
|
|
||||||
#define LOVE_IMAGE_MAGPIE_IMAGEIO_HANDLER_H
|
|
||||||
|
|
||||||
#include "common/config.h"
|
|
||||||
|
|
||||||
#ifdef LOVE_SUPPORT_IMAGEIO
|
|
||||||
|
|
||||||
// LOVE
|
|
||||||
#include "FormatHandler.h"
|
|
||||||
|
|
||||||
namespace love
|
|
||||||
{
|
|
||||||
namespace image
|
|
||||||
{
|
|
||||||
namespace magpie
|
|
||||||
{
|
|
||||||
|
|
||||||
class ImageIOHandler : public FormatHandler
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
ImageIOHandler();
|
|
||||||
virtual ~ImageIOHandler();
|
|
||||||
|
|
||||||
// Implements FormatHandler.
|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
love::thread::Mutex *mutex;
|
|
||||||
|
|
||||||
}; // ImageIOHandler
|
|
||||||
|
|
||||||
} // magpie
|
|
||||||
} // image
|
|
||||||
} // love
|
|
||||||
|
|
||||||
#endif // LOVE_SUPPORT_IMAGEIO
|
|
||||||
|
|
||||||
#endif // LOVE_IMAGE_MAGPIE_IMAGEIO_HANDLER_H
|
|
||||||
@@ -31,12 +31,11 @@ namespace magpie
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface between ImageData and the stb_image library, for decoding TGA and
|
* Interface between ImageData and the stb_image library, for decoding JPEG,
|
||||||
* BMP images.
|
* TGA, and BMP images.
|
||||||
*
|
*
|
||||||
* We could use stb_image to decode PNG and JPEG as well, but performance and
|
* We could use stb_image to decode PNG as well, but performance and
|
||||||
* comprehensive format support is lacking compared to some alternatives, plus
|
* comprehensive format support is lacking compared to some alternatives.
|
||||||
* stb_image_write doesn't have JPEG support.
|
|
||||||
**/
|
**/
|
||||||
class STBHandler : public FormatHandler
|
class STBHandler : public FormatHandler
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user