//
//  util.m
//  iPhly
//
//  Created by Ari Krupnik on 8/16/10.
//  Copyright 2010 lib.aero. All rights reserved.
//

#import "util_m.h"

UIImage *thumbNail(UIImage *orig, int height, int round_corner) {
	static CGImageRef cornerImage;
	if (!cornerImage) cornerImage = [[UIImage imageNamed:@"round-corner.png"] CGImage];
	
	CGImageRef origCG = [orig CGImage];
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, height, height));
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                8, //CGImageGetBitsPerComponent(orig),
                                                0,
                                                CGImageGetColorSpace(origCG),
												kCGImageAlphaNoneSkipFirst);
    CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
    CGContextDrawImage(bitmap, newRect, origCG);
	if (round_corner) {
		CGContextDrawImage(bitmap, newRect, cornerImage);
	}
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);
    
    return newImage;
}
