Optimizing OpenGl Rotation of a 2D grayscale image on iphone

会有一股神秘感。 提交于 2019-12-12 02:33:25

问题


I am rotating an image on the iPhone. It is a full size grayscale image (3264 by 2448 on the 4s). OpenGl appears to be roughly twice as fast as core graphics by my measurements, running about 1.22 seconds as opposed to 2.6 seconds. But this is not fast enough for my needs, I need sub-second rotation, if it is possible. (If not, we go to Plan B which involves rotating a subsection of the image, which is more elegant perhaps but has it's own issues.)

I should make clear that this only for internal processing of the image, not for display purposes.

I would like to make sure that I am doing this correctly and not making any obvious beginner's mistakes. Here is the code, I would appreciate any hints for improvements if possible.

Thank you, Ken

    void LTT_RotateGL(
                  unsigned char *src,
                  unsigned char *dst,
                  int nHeight,
                  int nWidth,
                  int nRowBytes,
                  double radians,
                  unsigned char borderColor)
{
    unsigned char *tmpSrc  = (unsigned char *)malloc( (nWidth * nHeight) );
    memcpy( tmpSrc, src, (nWidth * nHeight) );

    CGContextRef context2 = CGBitmapContextCreate(
                                                  tmpSrc, nWidth, nHeight, (size_t)8, nRowBytes,
                                                  CGColorSpaceCreateDeviceGray(),
                                                  kCGImageAlphaNone );

    CGImageRef imageRef2 = CGBitmapContextCreateImage( context2 );
    UIImage *image2     = [[UIImage alloc] initWithCGImage:imageRef2];
    GLuint width = CGImageGetWidth(image2.CGImage);
    GLuint height = CGImageGetHeight(image2.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    void *imageData = malloc( height * width);
    CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, width, colorSpace, kCGImageAlphaNone );
    CGColorSpaceRelease( colorSpace );  
    CGContextTranslateCTM( context, 0, height - height );
    CGContextRotateCTM(context, -radians); 
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image2.CGImage );
    glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_LUMINANCE, imageData);  
    CGContextRelease(context);
    memcpy( dst, imageData, (nWidth * nHeight) );
    free( tmpSrc );
   }

来源:https://stackoverflow.com/questions/17799250/optimizing-opengl-rotation-of-a-2d-grayscale-image-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!