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