image.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * File : image.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtgui/image.h>
  16. #include <rtgui/image_xpm.h>
  17. #include <rtgui/image_hdc.h>
  18. #include <rtgui/rtgui_system.h>
  19. #include <string.h>
  20. #ifdef RTGUI_IMAGE_BMP
  21. #include <rtgui/image_bmp.h>
  22. #endif
  23. #ifdef RTGUI_IMAGE_JPEG
  24. #include <rtgui/image_jpeg.h>
  25. #endif
  26. #ifdef RTGUI_IMAGE_PNG
  27. #include <rtgui/image_png.h>
  28. #endif
  29. static rtgui_list_t _rtgui_system_image_list = {RT_NULL};
  30. /* init rtgui image system */
  31. void rtgui_system_image_init(void)
  32. {
  33. /* always support XPM image */
  34. rtgui_image_xpm_init();
  35. rtgui_image_hdc_init();
  36. #ifdef RTGUI_IMAGE_BMP
  37. rtgui_image_bmp_init();
  38. #endif
  39. #ifdef RTGUI_IMAGE_JPEG
  40. rtgui_image_jpeg_init();
  41. #endif
  42. #ifdef RTGUI_IMAGE_PNG
  43. rtgui_image_png_init();
  44. #endif
  45. }
  46. static struct rtgui_image_engine* rtgui_image_get_engine_by_filename(const char* fn)
  47. {
  48. struct rtgui_list_node *node;
  49. struct rtgui_image_engine *engine;
  50. const char* ext;
  51. ext = fn + rt_strlen(fn);
  52. while (ext != fn)
  53. {
  54. if (*ext == '.') { ext ++; break; }
  55. ext --;
  56. }
  57. if (ext == fn) return RT_NULL; /* no ext */
  58. rtgui_list_foreach(node, &_rtgui_system_image_list)
  59. {
  60. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  61. if (strncasecmp(engine->name, ext, strlen(engine->name)) == 0)
  62. return engine;
  63. }
  64. return RT_NULL;
  65. }
  66. static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
  67. {
  68. struct rtgui_list_node *node;
  69. struct rtgui_image_engine *engine;
  70. rtgui_list_foreach(node, &_rtgui_system_image_list)
  71. {
  72. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  73. if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
  74. return engine;
  75. }
  76. return RT_NULL;
  77. }
  78. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  79. struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load)
  80. {
  81. struct rtgui_filerw* filerw;
  82. struct rtgui_image_engine* engine;
  83. struct rtgui_image* image = RT_NULL;
  84. /* create filerw context */
  85. filerw = rtgui_filerw_create_file(filename, "rb");
  86. if (filerw == RT_NULL) return RT_NULL;
  87. /* get image engine */
  88. engine = rtgui_image_get_engine(type);
  89. if (engine == RT_NULL)
  90. {
  91. /* close filerw context */
  92. rtgui_filerw_close(filerw);
  93. return RT_NULL;
  94. }
  95. if (engine->image_check(filerw) == RT_TRUE)
  96. {
  97. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  98. if (image == RT_NULL)
  99. {
  100. /* close filerw context */
  101. rtgui_filerw_close(filerw);
  102. return RT_NULL;
  103. }
  104. image->palette = RT_NULL;
  105. if (engine->image_load(image, filerw, load) != RT_TRUE)
  106. {
  107. /* close filerw context */
  108. rtgui_filerw_close(filerw);
  109. return RT_NULL;
  110. }
  111. /* set image engine */
  112. image->engine = engine;
  113. }
  114. else
  115. {
  116. rtgui_filerw_close(filerw);
  117. }
  118. return image;
  119. }
  120. struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load)
  121. {
  122. struct rtgui_filerw* filerw;
  123. struct rtgui_image_engine* engine;
  124. struct rtgui_image* image = RT_NULL;
  125. /* create filerw context */
  126. filerw = rtgui_filerw_create_file(filename, "rb");
  127. if (filerw == RT_NULL) return RT_NULL;
  128. /* get image engine */
  129. engine = rtgui_image_get_engine_by_filename(filename);
  130. if (engine == RT_NULL)
  131. {
  132. /* close filerw context */
  133. rtgui_filerw_close(filerw);
  134. return RT_NULL;
  135. }
  136. if (engine->image_check(filerw) == RT_TRUE)
  137. {
  138. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  139. if (image == RT_NULL)
  140. {
  141. /* close filerw context */
  142. rtgui_filerw_close(filerw);
  143. return RT_NULL;
  144. }
  145. image->palette = RT_NULL;
  146. if (engine->image_load(image, filerw, load) != RT_TRUE)
  147. {
  148. /* close filerw context */
  149. rtgui_filerw_close(filerw);
  150. return RT_NULL;
  151. }
  152. /* set image engine */
  153. image->engine = engine;
  154. }
  155. else
  156. {
  157. rtgui_filerw_close(filerw);
  158. }
  159. return image;
  160. }
  161. #endif
  162. struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load)
  163. {
  164. struct rtgui_filerw* filerw;
  165. struct rtgui_image_engine* engine;
  166. struct rtgui_image* image = RT_NULL;
  167. /* create filerw context */
  168. filerw = rtgui_filerw_create_mem(data, length);
  169. if (filerw == RT_NULL) return RT_NULL;
  170. /* get image engine */
  171. engine = rtgui_image_get_engine(type);
  172. if (engine == RT_NULL)
  173. {
  174. /* close filerw context */
  175. rtgui_filerw_close(filerw);
  176. return RT_NULL;
  177. }
  178. if (engine->image_check(filerw) == RT_TRUE)
  179. {
  180. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  181. if (image == RT_NULL)
  182. {
  183. /* close filerw context */
  184. rtgui_filerw_close(filerw);
  185. return RT_NULL;
  186. }
  187. image->palette = RT_NULL;
  188. if (engine->image_load(image, filerw, load) != RT_TRUE)
  189. {
  190. /* close filerw context */
  191. rtgui_filerw_close(filerw);
  192. return RT_NULL;
  193. }
  194. /* set image engine */
  195. image->engine = engine;
  196. }
  197. else
  198. {
  199. rtgui_filerw_close(filerw);
  200. }
  201. return image;
  202. }
  203. void rtgui_image_destroy(struct rtgui_image* image)
  204. {
  205. RT_ASSERT(image != RT_NULL);
  206. image->engine->image_unload(image);
  207. if (image->palette != RT_NULL)
  208. rtgui_free(image->palette);
  209. rtgui_free(image);
  210. }
  211. /* register an image engine */
  212. void rtgui_image_register_engine(struct rtgui_image_engine* engine)
  213. {
  214. RT_ASSERT(engine!= RT_NULL);
  215. rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
  216. }
  217. void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  218. {
  219. RT_ASSERT(dc != RT_NULL);
  220. RT_ASSERT(rect != RT_NULL);
  221. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  222. if (image != RT_NULL && image->engine != RT_NULL)
  223. {
  224. /* use image engine to blit */
  225. image->engine->image_blit(image, dc, rect);
  226. }
  227. }
  228. struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors)
  229. {
  230. struct rtgui_image_palette* palette = RT_NULL;
  231. if (ncolors > 0)
  232. {
  233. palette = (struct rtgui_image_palette*) rtgui_malloc(sizeof(struct rtgui_image_palette) +
  234. sizeof(rtgui_color_t) * ncolors);
  235. if (palette != RT_NULL) palette->colors = (rtgui_color_t*)(palette + 1);
  236. }
  237. return palette;
  238. }