Handle loading of embedded textures
[opengl.git] / image.cpp
index e3c5cbdb81fe2355c63c9452b25f8a92adec68f9..6c09fa31caaca7335e18f302fb687d6393a85dc5 100644 (file)
--- a/image.cpp
+++ b/image.cpp
@@ -6,7 +6,18 @@ Image::Image(const std::string &path) {
        CFURLRef url = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, false);
        CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
        CGImageRef ref = CGImageSourceCreateImageAtIndex(source, 0, NULL);
+       initWithImageRef(ref);
+       CGImageRelease(ref);
+}
 
+Image::Image(const unsigned char *data, size_t length) {
+       CGDataProviderRef dpRef = CGDataProviderCreateWithData(NULL, data, length, NULL);
+       CGImageRef ref = CGImageCreateWithJPEGDataProvider(dpRef, NULL, false, kCGRenderingIntentDefault);
+       initWithImageRef(ref);
+       CGImageRelease(ref);
+}
+
+void Image::initWithImageRef(CGImageRef ref) {
        _width = CGImageGetWidth(ref), _height = CGImageGetHeight(ref);
        info = CGImageGetBitmapInfo(ref);
        alphaInfo = CGImageGetAlphaInfo(ref);
@@ -14,8 +25,6 @@ Image::Image(const std::string &path) {
        bitsPerComponent = CGImageGetBitsPerComponent(ref);
 
        dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref));
-       CGImageRelease(ref);
-
 }
 
 unsigned char *Image::data() const {