PageRenderTime 222ms CodeModel.GetById 120ms app.highlight 16ms RepoModel.GetById 85ms app.codeStats 0ms

/src/os/iphone/ftk_image_iphone_decoder.m

http://ftk.googlecode.com/
Objective C | 84 lines | 67 code | 17 blank | 0 comment | 15 complexity | 253890997bc20ad039061a248309028a MD5 | raw file
 1
 2#include <UIKit/UIKit.h>
 3#include "ftk_image_iphone_decoder.h"
 4
 5static Ret ftk_image_iphone_decoder_match(FtkImageDecoder* thiz, const char* filename)
 6{
 7	return_val_if_fail(filename != NULL, RET_FAIL);
 8
 9	return (strstr(filename, ".bmp") != NULL
10		|| strstr(filename, ".png") != NULL
11		|| strstr(filename, ".jpg") != NULL
12		|| strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
13}
14
15static FtkBitmap* load_image(const char* filename)
16{
17	int width, height;
18	FtkColor bg = {0};
19	FtkBitmap* bitmap = NULL;
20	UIImage* img = NULL;
21	NSString* path = NULL;
22	CGRect rect;
23	CGContextRef ctx = NULL;
24	CGColorSpaceRef color_space = NULL;
25
26	ftk_logd("%s:%d %s\n", __FILE__, __LINE__, filename);
27
28	path = [[NSString alloc] initWithUTF8String:filename];
29	img = [UIImage imageWithContentsOfFile:path];
30	if(img == NULL)
31	{
32		[path release];
33		return NULL;
34	}
35	[path release];
36
37	width = CGImageGetWidth(img.CGImage);
38	height = CGImageGetHeight(img.CGImage);
39
40	bg.a = 0xff;
41	bitmap = ftk_bitmap_create(width, height, bg);
42
43	color_space = CGColorSpaceCreateDeviceRGB();
44	ctx = CGBitmapContextCreate(ftk_bitmap_bits(bitmap), width, height, 8, width * 4, color_space, kCGImageAlphaPremultipliedLast);
45	CGColorSpaceRelease(color_space);
46
47	rect = CGRectMake(0, 0, width, height);
48	CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
49	CGContextClearRect(ctx, rect);
50	CGContextDrawImage(ctx, rect, img.CGImage);
51
52	CGContextRelease(ctx);
53	[img release];
54
55	return bitmap;
56}
57
58static FtkBitmap* ftk_image_iphone_decoder_decode(FtkImageDecoder* thiz, const char* filename)
59{
60	return_val_if_fail(ftk_image_iphone_decoder_match(thiz, filename) == RET_OK, NULL);
61	return load_image(filename);
62}
63
64static void ftk_image_iphone_decoder_destroy(FtkImageDecoder* thiz)
65{
66	if(thiz != NULL)
67	{
68		FTK_ZFREE(thiz, sizeof(thiz));
69	}
70}
71
72FtkImageDecoder* ftk_image_iphone_decoder_create(void)
73{
74	FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
75
76	if(thiz != NULL)
77	{
78		thiz->match = ftk_image_iphone_decoder_match;
79		thiz->decode = ftk_image_iphone_decoder_decode;
80		thiz->destroy = ftk_image_iphone_decoder_destroy;
81	}
82
83	return thiz;
84}