Skip to content

Instantly share code, notes, and snippets.

@etolstoy
Created February 18, 2015 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etolstoy/b885d0d46e41865d796b to your computer and use it in GitHub Desktop.
Save etolstoy/b885d0d46e41865d796b to your computer and use it in GitHub Desktop.
IDTMessaging Test
// 7. Briefly discuss pixelmap image rotation algorithms and their execution times (O(*)). If you would write an image rotation algorithm, do you find anything useful in the iOS Accelerate framework?
- (UIImage *)rotateImage:(UIImage *)sourceImage withDegrees:(CGFloat)degrees {
vImage_Buffer sourceBuffer = [self bufferFromImage:sourceImage];
vImage_Buffer destinationBuffer = [self newBufferForBuffer:sourceBuffer];
CGFloat radians = [self radiansFromDegrees:degrees];
Pixel_8 backColor = 0;
vImageRotate_Planar8(&sourceBuffer, &destinationBuffer, NULL, radians, backColor, 0);
return [self imageFromvBuffer:destinationBuffer];
}
- (UIImage *)imageFromvBuffer:(vImage_Buffer)buffer {
CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(buffer.data,
buffer.width,
buffer.height,
8,
buffer.width,
grayColorSpace,
0);
CGImageRef dstImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(grayColorSpace);
CGContextRelease(context);
return [[UIImage alloc] initWithCGImage:dstImage];
}
- (vImage_Buffer)bufferFromImage:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
Pixel_8 *bitmap = (Pixel_8 *)malloc(width * height * sizeof(Pixel_8));
long bytesPerPixel = 1;
long bytesPerRow = bytesPerPixel * width;
long bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(bitmap,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
0);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
vImage_Buffer buffer = { bitmap, height, width, bytesPerRow };
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
return buffer;
}
- (vImage_Buffer)newBufferForBuffer:(vImage_Buffer)buffer {
size_t width = buffer.width;
size_t height = buffer.height;
size_t bytesPerRow = buffer.rowBytes;
Pixel_8 *outData = (Pixel_8 *)malloc( bytesPerRow * height );
vImage_Buffer newBuffer = { outData, height, width, bytesPerRow };
return newBuffer;
}
#pragma mark - Helper Methods
- (CGFloat)radiansFromDegrees:(CGFloat)degrees {
return degrees * 2.0f * M_PI / 360.0f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment