opengl打开jpeg图片

来源:互联网 发布:合肥博然软件怎么样 编辑:程序博客网 时间:2024/05/01 02:33

opengl没有提供直接函数来打开jpeg、png之类的压缩图片, 目前可用的方法有:1. 调用第三方库打开图片,比如opencv等;2.直接利用系统提供的函数打开图片。对于写个demo之类的小程序,直接引入opencv显然是没有必要的,这里就介绍mac下如果读取jpeg的图片的方法。具体实现如下:

unsigned char* readImageData(const char* file_name, int* width, int* height){    NSString * string = [NSString stringWithFormat:@"%s", file_name];    CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:string];    CGImageSourceRef myImageSourceRef = CGImageSourceCreateWithURL(urlRef, NULL);    CGImageRef myImageRef = CGImageSourceCreateImageAtIndex(myImageSourceRef, 0, NULL);    *width = CGImageGetWidth(myImageRef);    *height = CGImageGetHeight(myImageRef);    CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(myImageRef));    unsigned char *data = (unsigned char*)CFDataGetBytePtr(dataRef);    return data;}
0 0