/*
iPhly: Radio Control on the iPhone

Copyright (C) 2010-2011 Ari Krupnik & Associates

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License V2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

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