2 #include <ImageIO/ImageIO.h>
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);
10 _width = CGImageGetWidth(ref), _height = CGImageGetHeight(ref);
11 info = CGImageGetBitmapInfo(ref);
12 alphaInfo = CGImageGetAlphaInfo(ref);
13 colorSpace = CGImageGetColorSpace(ref);
15 dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref));
20 unsigned char *Image::data() const {
21 return (unsigned char*) CFDataGetBytePtr(dataRef);
24 GLfloat Image::width() const { return _width; }
25 GLfloat Image::height() const { return _height; }
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;
32 if (alphaInfo == kCGImageAlphaNone) {
38 Image::~Image() { CFRelease(dataRef); }