diff --git a/platform/xcode/liblove.xcodeproj/project.pbxproj b/platform/xcode/liblove.xcodeproj/project.pbxproj index 34348cd66..652621b6b 100644 --- a/platform/xcode/liblove.xcodeproj/project.pbxproj +++ b/platform/xcode/liblove.xcodeproj/project.pbxproj @@ -486,9 +486,6 @@ FA0B7D961A95902C000E1D17 /* 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 */; }; - 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 */; }; FA0B7DA01A95902C000E1D17 /* KTXHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */; }; 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 = ""; }; FA0B7BD21A95902C000E1D17 /* ImageData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImageData.cpp; sourceTree = ""; }; FA0B7BD31A95902C000E1D17 /* ImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageData.h; sourceTree = ""; }; - FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImageIOHandler.cpp; sourceTree = ""; }; - FA0B7BD51A95902C000E1D17 /* ImageIOHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageIOHandler.h; sourceTree = ""; }; FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KTXHandler.cpp; sourceTree = ""; }; FA0B7BD91A95902C000E1D17 /* KTXHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KTXHandler.h; sourceTree = ""; }; FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PKMHandler.cpp; sourceTree = ""; }; @@ -2295,8 +2290,6 @@ FA0B7BD11A95902C000E1D17 /* Image.h */, FA0B7BD21A95902C000E1D17 /* ImageData.cpp */, FA0B7BD31A95902C000E1D17 /* ImageData.h */, - FA0B7BD41A95902C000E1D17 /* ImageIOHandler.cpp */, - FA0B7BD51A95902C000E1D17 /* ImageIOHandler.h */, FA0B7BD81A95902C000E1D17 /* KTXHandler.cpp */, FA0B7BD91A95902C000E1D17 /* KTXHandler.h */, FA0B7BDA1A95902C000E1D17 /* PKMHandler.cpp */, @@ -3006,7 +2999,6 @@ FA0B7E991A95902C000E1D17 /* Sound.h in Headers */, FA0B7D841A95902C000E1D17 /* CompressedImageData.h in Headers */, FA0B7D231A95902C000E1D17 /* Rasterizer.h in Headers */, - FA0B7D9B1A95902C000E1D17 /* ImageIOHandler.h in Headers */, FA0B7CDB1A95902C000E1D17 /* Pool.h in Headers */, FA0B7D0B1A95902C000E1D17 /* wrap_FileData.h in Headers */, FA0B7DF91A95902C000E1D17 /* Body.h in Headers */, @@ -3361,7 +3353,6 @@ FA620A331AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */, FA0B7E071A95902C000E1D17 /* DistanceJoint.cpp in Sources */, FA0B7A331A958EA3000E1D17 /* b2Collision.cpp in Sources */, - FA0B7D9A1A95902C000E1D17 /* ImageIOHandler.cpp in Sources */, FA0B7E5E1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */, FA0B7B3B1A958EA3000E1D17 /* wuff_memory.c in Sources */, FA0B79411A958E3B000E1D17 /* utf8.cpp in Sources */, @@ -3645,7 +3636,6 @@ FA0B7E061A95902C000E1D17 /* DistanceJoint.cpp in Sources */, FA620A321AA2F8DB005DB4C2 /* wrap_Quad.cpp in Sources */, FA0B793B1A958E3B000E1D17 /* runtime.cpp in Sources */, - FA0B7D991A95902C000E1D17 /* ImageIOHandler.cpp in Sources */, FA0B7E5D1A95902C000E1D17 /* wrap_MouseJoint.cpp in Sources */, FA0B7A741A958EA3000E1D17 /* b2ChainAndPolygonContact.cpp in Sources */, FA0B7AD41A958EA3000E1D17 /* unix.c in Sources */, @@ -3859,7 +3849,6 @@ ALWAYS_SEARCH_USER_PATHS = NO; GCC_PREPROCESSOR_DEFINITIONS = ( "$(inherited)", - LOVE_SUPPORT_IMAGEIO, LOVE_SUPPORT_COREAUDIO, LOVE_NO_MODPLUG, LOVE_NOMPG123, @@ -3901,7 +3890,6 @@ ENABLE_NS_ASSERTIONS = NO; GCC_PREPROCESSOR_DEFINITIONS = ( "$(inherited)", - LOVE_SUPPORT_IMAGEIO, LOVE_SUPPORT_COREAUDIO, LOVE_NO_MODPLUG, LOVE_NOMPG123, @@ -3943,7 +3931,6 @@ ENABLE_NS_ASSERTIONS = NO; GCC_PREPROCESSOR_DEFINITIONS = ( "$(inherited)", - LOVE_SUPPORT_IMAGEIO, LOVE_SUPPORT_COREAUDIO, LOVE_NO_MODPLUG, LOVE_NOMPG123, diff --git a/src/modules/image/magpie/Image.cpp b/src/modules/image/magpie/Image.cpp index a941d97fe..7caeb4710 100644 --- a/src/modules/image/magpie/Image.cpp +++ b/src/modules/image/magpie/Image.cpp @@ -25,7 +25,6 @@ #include "PNGHandler.h" #include "STBHandler.h" -#include "ImageIOHandler.h" #include "ddsHandler.h" #include "PVRHandler.h" @@ -44,10 +43,6 @@ Image::Image() formatHandlers.push_back(new PNGHandler); formatHandlers.push_back(new STBHandler); -#ifdef LOVE_SUPPORT_IMAGEIO - formatHandlers.push_back(new ImageIOHandler); -#endif - compressedFormatHandlers.push_back(new DDSHandler); compressedFormatHandlers.push_back(new PVRHandler); compressedFormatHandlers.push_back(new KTXHandler); diff --git a/src/modules/image/magpie/ImageIOHandler.cpp b/src/modules/image/magpie/ImageIOHandler.cpp deleted file mode 100644 index 1ae7e8d55..000000000 --- a/src/modules/image/magpie/ImageIOHandler.cpp +++ /dev/null @@ -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 - -#if defined(LOVE_IOS) -#include -#elif defined(LOVE_MACOSX) -#include -#endif - -// STL -#include -#include - -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 diff --git a/src/modules/image/magpie/ImageIOHandler.h b/src/modules/image/magpie/ImageIOHandler.h deleted file mode 100644 index d70836b0f..000000000 --- a/src/modules/image/magpie/ImageIOHandler.h +++ /dev/null @@ -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 diff --git a/src/modules/image/magpie/STBHandler.h b/src/modules/image/magpie/STBHandler.h index 2636fd65a..ec4b9e3f0 100644 --- a/src/modules/image/magpie/STBHandler.h +++ b/src/modules/image/magpie/STBHandler.h @@ -31,12 +31,11 @@ namespace magpie { /** - * Interface between ImageData and the stb_image library, for decoding TGA and - * BMP images. + * Interface between ImageData and the stb_image library, for decoding JPEG, + * TGA, and BMP images. * - * We could use stb_image to decode PNG and JPEG as well, but performance and - * comprehensive format support is lacking compared to some alternatives, plus - * stb_image_write doesn't have JPEG support. + * We could use stb_image to decode PNG as well, but performance and + * comprehensive format support is lacking compared to some alternatives. **/ class STBHandler : public FormatHandler {