开发者

OpenGL ES texture from random image in iOS

开发者 https://www.devze.com 2023-03-23 21:01 出处:网络
I\'ve been challenged with the task of putting an oddly sized image (with fixed proportion, though) on a GL_QUAD (well, a GL_TRIANGLE_STRIP resem--you got the point) and that seemed fairily easy to me

I've been challenged with the task of putting an oddly sized image (with fixed proportion, though) on a GL_QUAD (well, a GL_TRIANGLE_STRIP resem--you got the point) and that seemed fairily easy to me at first, except for the part where I need to do this in iOS (4.2+). The solution is awkwardly easy anyway: just take the image, make a texture out of it, map it to the correct vertices and you're good to go.

As you may very well know, OpenGL ES textures are required to have width and height to be powers of 2, like 2, 4, 8, ..., 256, 512... (not sure this holds for regular OpenGL but I think it does... anyway, doesn't matter). Since I have to download these images from the Intertubes (actually, the YouTube) I can't really do anything beforehand, so I have these 480x360 images (if I remember it correctly) and I have to splat them on my triangle strips. Fortunately we have texture mapping which allows us to select portions of the texture to be mapped where we want, so the obvious solution would be to (optionally up/downsize) and pad with some matte color the source image, and live with it.

Enter iOS. I get the data from the Intertubes, I happily build the corresponding UIImage, then I make another UIImage (yes, I know, bear with me, I'll optimize it later) just scaled down to the nearest power-of-2 in width, preserving aspect, so let's say 256x192, then I make a bitmap context , paint it black (or, for what matters, any other colour, but I think you can see why I chose black in this case), draw the UIImage (a CGImage) on it, and return the UIImage built using the aforementioned bitmap context.

I am now the happy owner of a 256x256 image ready to be mapped on my GL_TRIANGLE_STRIP. Except that it does not work. I tried with a prepared 512x512 image and it worked flawlessly. The code I'm pasting 开发者_开发百科here does not include the retrieval of the image from YouTube, I just saved it locally to rule out networking problems. Also, I'm not including the GL code as it's clearly working.

- (void)viewDidLoad {
    images = [[NSMutableArray alloc] init];
    //NSURL *url = [NSURL URLWithString:@"http://i.ytimg.com/vi/d2wVgzXWE9Y/0.jpg"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"opengl_texture" ofType:@"jpg"];
    NSData *texData = [NSData dataWithContentsOfFile:path];
    UIImage *rawImage = [[UIImage alloc] initWithData:texData];
    float newWidth = (float)(1 << (int)floor(log2f(rawImage.size.width)));
    // Scale means the scale of the current image relative to the resulting image. 
    float scale = rawImage.size.width / newWidth;
    UIImage *midImage = [UIImage imageWithCGImage:[rawImage CGImage] scale:scale orientation:UIImageOrientationUp];
    NSLog(@"%f %f %f", midImage.size.width, midImage.size.height, scale);
    [rawImage release];
    UIImage *image = [self padImage:midImage withColor:[UIColor redColor]];

    NSLog(@"%f %f", image.size.width, image.size.height);

    [images addObject:image];

    textures = malloc(sizeof(GLuint));
    glGenTextures(1, textures);
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetWidth(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc(width * height * 4);
    CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, 4*width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
    CGContextTranslateCTM( context, 0, height - height );
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage ); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context);

    free(imageData);
    [midImage release];
    [image release];
    [texData release];
}

- (UIImage *)padImage:(UIImage *)image withColor:(UIColor *)color {
    CGFloat size = round(image.size.width);
    NSLog(@"%f", size);
    CGContextRef bContext = [self createBitmapContextOfSize:CGSizeMake(size, size)];
    CGContextSetFillColorWithColor(bContext, [color CGColor]);
    CGContextFillRect(bContext, CGRectMake(0, 0, size, size));
    CGContextDrawImage(bContext, CGRectMake(0, 0, size, size), [image CGImage]);
    UIImage *result = [UIImage imageWithCGImage:CGBitmapContextCreateImage(bContext)];
    CGContextRelease(bContext);
    return result;
}

- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     size.width,
                                     size.height,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    CGContextSetAllowsAntialiasing (context,NO);
    if (context== NULL) {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

Please don't bother mentioning obvious memory management issues unless you think they are the core of the problem. As for the "error message" or whatever: no, there's no such thing, the whole app just crashes.


Ok, now you can collectively smack my face with a large trout.

The problem was actually memory management, specifically I was releasing objects that were created with implicit methods (namely midImage and texData). Implicit creation does not increase the retain count, while explicit (alloc+init and friends) does. How may times did I already crash against this? Lots. Were them enough? Obviously not.

Second question: where can I find a large post-it, like 1x1m at least?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号