diff options
Diffstat (limited to 'macosx/HBImageUtilities.m')
-rw-r--r-- | macosx/HBImageUtilities.m | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/macosx/HBImageUtilities.m b/macosx/HBImageUtilities.m index 07530d815..d5858c60d 100644 --- a/macosx/HBImageUtilities.m +++ b/macosx/HBImageUtilities.m @@ -8,6 +8,59 @@ #import <Cocoa/Cocoa.h> #include "hb.h" +CGImageRef CreateScaledCGImageFromCGImage(CGImageRef image, CGFloat thumbnailHeight) +{ + // Create the bitmap context + CGContextRef context = NULL; + void * bitmapData; + int bitmapByteCount; + int bitmapBytesPerRow; + + // Get image width, height. We'll use the entire image. + int width = (CGFloat)CGImageGetWidth(image) / (CGFloat)CGImageGetHeight(image) * thumbnailHeight; + int height = thumbnailHeight; + + // Declare the number of bytes per row. Each pixel in the bitmap in this + // example is represented by 4 bytes; 8 bits each of red, green, blue, and + // alpha. + bitmapBytesPerRow = (width * 4); + bitmapByteCount = (bitmapBytesPerRow * height); + + // Allocate memory for image data. This is the destination in memory + // where any drawing to the bitmap context will be rendered. + bitmapData = malloc(bitmapByteCount); + if (bitmapData == NULL) + { + return nil; + } + + // Create the bitmap context. We want pre-multiplied ARGB, 8-bits + // per component. Regardless of what the source image format is + // (CMYK, Grayscale, and so on) it will be converted over to the format + // specified here by CGBitmapContextCreate. + CGColorSpaceRef colorspace = CGImageGetColorSpace(image); + context = CGBitmapContextCreate (bitmapData,width,height,8,bitmapBytesPerRow, + colorspace,kCGImageAlphaNoneSkipFirst); + + if (context == NULL) + { + // error creating context + return nil; + } + + // Draw the image to the bitmap context. Once we draw, the memory + // allocated for the context for rendering will then contain the + // raw image data in the specified color space. + CGContextDrawImage(context, CGRectMake(0,0,width, height), image); + + CGImageRef imgRef = CGBitmapContextCreateImage(context); + CGContextRelease(context); + free(bitmapData); + + return imgRef; +} + + CGImageRef CGImageRotated(CGImageRef imgRef, CGFloat angle, BOOL flipped) CF_RETURNS_RETAINED { CGFloat angleInRadians = angle * (M_PI / 180); |