Implement IBL diffuse part
[opengl.git] / image.cpp
1 #include "image.hpp"
2 #include <ImageIO/ImageIO.h>
3
4 Image::Image(const std::string &path) {
5         CFStringRef str = CFStringCreateWithCString(NULL, path.c_str(), kCFStringEncodingUTF8);
6         CFURLRef url = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, false);
7         CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
8         CGImageRef ref = CGImageSourceCreateImageAtIndex(source, 0, NULL);
9
10         _width = CGImageGetWidth(ref), _height = CGImageGetHeight(ref);
11         info = CGImageGetBitmapInfo(ref);
12         alphaInfo = CGImageGetAlphaInfo(ref);
13         colorSpace = CGImageGetColorSpace(ref);
14
15         dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref));
16         CGImageRelease(ref);
17
18 }
19
20 unsigned char *Image::data() const {
21         return (unsigned char*) CFDataGetBytePtr(dataRef);
22 }
23
24 GLfloat Image::width() const { return _width; }
25 GLfloat Image::height() const { return _height; }
26
27 // TODO: Properly implement this for both internal format + format
28 GLuint Image::format() const {
29         if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
30                 return GL_DEPTH_COMPONENT;
31         }
32         if (alphaInfo == kCGImageAlphaNone) {
33                 return GL_RGB;
34         }
35         return GL_RGBA;
36 }
37
38 Image::~Image() { CFRelease(dataRef); }