Commits

Alex Szpakowski committed 3607b06

imageio: fixed unreleased filedata when imagedata loading fails, slightly improved loading performance

Comments (0)

Files changed (1)

src/modules/image/imageio/ImageData.cpp

 ImageData::ImageData(filesystem::File *file)
 {
 	Data *data = file->read();
-	load(data);
+
+	try
+	{
+		load(data);
+	}
+	catch (love::Exception &)
+	{
+		data->release();
+		throw;
+	}
+
 	data->release();
 }
 
 
 	Lock lock(ioMutex);
 
-	CFDataRef cfdata = CFDataCreate(NULL, (const UInt8 *) data->getData(), data->getSize());
+	CFDataRef cfdata = CFDataCreateWithBytesNoCopy(NULL, (const UInt8 *) data->getData(), data->getSize(), kCFAllocatorNull);
 	CGImageSourceRef source = CGImageSourceCreateWithData(cfdata, NULL);
 	CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
 
 	size_t pixsize = sizeof(pixel);
 	size_t bitspp = 8;
 	CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
-	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
+	CGBitmapInfo bitmapinfo = kCGBitmapByteOrderDefault;
 
-	CGImageRef image = CGImageCreate(width, height, bitspp, bitspp*pixsize, width*pixsize, space, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault);
+	CGImageRef image = CGImageCreate(width, height, bitspp, bitspp*pixsize, width*pixsize, space, bitmapinfo, provider, NULL, false, kCGRenderingIntentDefault);
 
 	CGColorSpaceRelease(space);
 	CGDataProviderRelease(provider);
 
-	CFMutableDataRef mutabledata = CFDataCreateMutable(NULL, 0);
-	if (!mutabledata)
-	{
-		CFRelease(image);
-		throw love::Exception("Could not create image for encoding!");
-	}
-
 	CFStringRef type;
 	switch (format)
 	{
 			break;
 	}
 
-	CGImageDestinationRef dest = CGImageDestinationCreateWithData(mutabledata, type, 1, NULL);
+	CFMutableDataRef encodeddata = CFDataCreateMutable(NULL, 0);
+	if (!encodeddata)
+	{
+		CFRelease(image);
+		CFRelease(type);
+		throw love::Exception("Could not create image for encoding!");
+	}
+
+	CGImageDestinationRef dest = CGImageDestinationCreateWithData(encodeddata, type, 1, NULL);
 	CFRelease(type);
 
 	CGImageDestinationAddImage(dest, image, NULL);
 	CFRelease(image);
 	CFRelease(dest);
 
-	CFIndex encodedsize = CFDataGetLength(mutabledata);
+	CFIndex encodedsize = CFDataGetLength(encodeddata);
 
 	if (!success || encodedsize == 0)
 	{
-		CFRelease(mutabledata);
+		CFRelease(encodeddata);
 		throw love::Exception("Could not encode image!");
 	}
 
 	try
 	{
 		f->open(love::filesystem::File::WRITE);
-		f->write(CFDataGetMutableBytePtr(mutabledata), encodedsize);
+		f->write(CFDataGetMutableBytePtr(encodeddata), encodedsize);
 		f->close();
 	}
 	catch (love::Exception &)
 	{
-		CFRelease(mutabledata);
+		CFRelease(encodeddata);
 		throw;
 	}
 
-	CFRelease(mutabledata);
+	CFRelease(encodeddata);
 }
 
 } // imageio