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 );
}