/src/canvas/default/ftk_bitmap_factory.c

http://ftk.googlecode.com/ · C · 146 lines · 89 code · 28 blank · 29 comment · 22 complexity · f08c516d476a583fb8ae0d24cccd15c3 MD5 · raw file

  1. /*
  2. * File: ftk_bitmap_factory.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: bitmap factory.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_allocator.h"
  32. #include "ftk_bitmap_factory.h"
  33. #ifdef HAS_BMP
  34. #include "ftk_image_bmp_decoder.h"
  35. #endif
  36. #ifdef HAS_JPEG
  37. #include "ftk_image_jpeg_decoder.h"
  38. #endif
  39. #ifdef HAS_PNG
  40. #include "ftk_image_png_decoder.h"
  41. #endif
  42. #if defined(WIN32) && !defined(WINCE)
  43. #include "ftk_image_win32_decoder.h"
  44. #endif
  45. #ifdef IPHONE
  46. #include "ftk_image_iphone_decoder.h"
  47. #endif
  48. #if defined(ANDROID) && defined(ANDROID_NDK)
  49. #include "ftk_image_android_decoder.h"
  50. #endif
  51. struct _FtkBitmapFactory
  52. {
  53. int nr;
  54. FtkImageDecoder* decoders[FTK_MAX_IMAGE_DECODERS];
  55. };
  56. FtkBitmapFactory* ftk_bitmap_factory_create(void)
  57. {
  58. FtkBitmapFactory* thiz = (FtkBitmapFactory*)FTK_ZALLOC(sizeof(FtkBitmapFactory));
  59. if(thiz != NULL)
  60. {
  61. #ifdef HAS_BMP
  62. ftk_bitmap_factory_add_decoder(thiz, ftk_image_bmp_decoder_create());
  63. #endif
  64. #ifdef HAS_JPEG
  65. ftk_bitmap_factory_add_decoder(thiz, ftk_image_jpeg_decoder_create());
  66. #endif
  67. #ifdef HAS_PNG
  68. ftk_bitmap_factory_add_decoder(thiz, ftk_image_png_decoder_create());
  69. #endif
  70. #if defined(WIN32) && !defined(WINCE)
  71. ftk_bitmap_factory_add_decoder(thiz, ftk_image_win32_decoder_create());
  72. #endif
  73. #ifdef IPHONE
  74. ftk_bitmap_factory_add_decoder(thiz, ftk_image_iphone_decoder_create());
  75. #endif
  76. #if defined(ANDROID) && defined(ANDROID_NDK)
  77. ftk_bitmap_factory_add_decoder(thiz, ftk_image_android_decoder_create());
  78. #endif
  79. }
  80. return thiz;
  81. }
  82. FtkBitmap* ftk_bitmap_factory_load(FtkBitmapFactory* thiz, const char* filename)
  83. {
  84. int i = 0;
  85. return_val_if_fail(thiz != NULL && filename != NULL, NULL);
  86. for(i = 0; i < thiz->nr; i++)
  87. {
  88. if(thiz->decoders[i] != NULL && ftk_image_decoder_match(thiz->decoders[i], filename) == RET_OK)
  89. {
  90. return ftk_image_decoder_decode(thiz->decoders[i], filename);
  91. }
  92. }
  93. ftk_loge("%s:%d %s is not supported format.\n", __func__, __LINE__, filename);
  94. return NULL;
  95. }
  96. Ret ftk_bitmap_factory_add_decoder(FtkBitmapFactory* thiz, FtkImageDecoder* decoder)
  97. {
  98. return_val_if_fail(thiz != NULL && decoder != NULL, RET_FAIL);
  99. return_val_if_fail(thiz->nr < FTK_MAX_IMAGE_DECODERS, RET_FAIL);
  100. thiz->decoders[thiz->nr++] = decoder;
  101. return RET_OK;
  102. }
  103. void ftk_bitmap_factory_destroy(FtkBitmapFactory* thiz)
  104. {
  105. int i = 0;
  106. if(thiz != NULL)
  107. {
  108. for(i = 0; i < thiz->nr; i++)
  109. {
  110. if(thiz->decoders[i] != NULL)
  111. {
  112. ftk_image_decoder_destroy(thiz->decoders[i]);
  113. }
  114. }
  115. FTK_ZFREE(thiz, sizeof(FtkBitmapFactory));
  116. }
  117. return;
  118. }