image.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_hdc.h>
  17. #include <rtgui/rtgui_system.h>
  18. #include <rtgui/image_container.h>
  19. #include <string.h>
  20. #ifdef RTGUI_IMAGE_XPM
  21. #include <rtgui/image_xpm.h>
  22. #endif
  23. #ifdef RTGUI_IMAGE_BMP
  24. #include <rtgui/image_bmp.h>
  25. #endif
  26. #ifdef RTGUI_IMAGE_JPEG
  27. #include <rtgui/image_jpeg.h>
  28. #endif
  29. #ifdef RTGUI_IMAGE_PNG
  30. #include <rtgui/image_png.h>
  31. #endif
  32. static rtgui_list_t _rtgui_system_image_list = {RT_NULL};
  33. /* initialize rtgui image system */
  34. void rtgui_system_image_init(void)
  35. {
  36. /* always support HDC image */
  37. rtgui_image_hdc_init();
  38. #ifdef RTGUI_IMAGE_XPM
  39. rtgui_image_xpm_init();
  40. #endif
  41. #ifdef RTGUI_IMAGE_BMP
  42. rtgui_image_bmp_init();
  43. #endif
  44. #ifdef RTGUI_IMAGE_JPEG
  45. rtgui_image_jpeg_init();
  46. #endif
  47. #ifdef RTGUI_IMAGE_PNG
  48. rtgui_image_png_init();
  49. #endif
  50. #ifdef RTGUI_IMAGE_CONTAINER
  51. /* initialize image container */
  52. rtgui_system_image_container_init(RT_FALSE);
  53. #endif
  54. }
  55. static struct rtgui_image_engine* rtgui_image_get_engine_by_filename(const char* fn)
  56. {
  57. struct rtgui_list_node *node;
  58. struct rtgui_image_engine *engine;
  59. const char* ext;
  60. ext = fn + rt_strlen(fn);
  61. while (ext != fn)
  62. {
  63. if (*ext == '.') { ext ++; break; }
  64. ext --;
  65. }
  66. if (ext == fn) return RT_NULL; /* no ext */
  67. rtgui_list_foreach(node, &_rtgui_system_image_list)
  68. {
  69. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  70. if (strncasecmp(engine->name, ext, strlen(engine->name)) == 0)
  71. return engine;
  72. }
  73. return RT_NULL;
  74. }
  75. static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
  76. {
  77. struct rtgui_list_node *node;
  78. struct rtgui_image_engine *engine;
  79. rtgui_list_foreach(node, &_rtgui_system_image_list)
  80. {
  81. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  82. if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
  83. return engine;
  84. }
  85. return RT_NULL;
  86. }
  87. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  88. struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load)
  89. {
  90. struct rtgui_filerw* filerw;
  91. struct rtgui_image_engine* engine;
  92. struct rtgui_image* image = RT_NULL;
  93. /* create filerw context */
  94. filerw = rtgui_filerw_create_file(filename, "rb");
  95. if (filerw == RT_NULL) return RT_NULL;
  96. /* get image engine */
  97. engine = rtgui_image_get_engine(type);
  98. if (engine == RT_NULL)
  99. {
  100. /* close filerw context */
  101. rtgui_filerw_close(filerw);
  102. return RT_NULL;
  103. }
  104. if (engine->image_check(filerw) == RT_TRUE)
  105. {
  106. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  107. if (image == RT_NULL)
  108. {
  109. /* close filerw context */
  110. rtgui_filerw_close(filerw);
  111. return RT_NULL;
  112. }
  113. image->palette = RT_NULL;
  114. if (engine->image_load(image, filerw, load) != RT_TRUE)
  115. {
  116. /* close filerw context */
  117. rtgui_filerw_close(filerw);
  118. return RT_NULL;
  119. }
  120. /* set image engine */
  121. image->engine = engine;
  122. }
  123. else
  124. {
  125. rtgui_filerw_close(filerw);
  126. }
  127. return image;
  128. }
  129. struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load)
  130. {
  131. struct rtgui_filerw* filerw;
  132. struct rtgui_image_engine* engine;
  133. struct rtgui_image* image = RT_NULL;
  134. /* create filerw context */
  135. filerw = rtgui_filerw_create_file(filename, "rb");
  136. if (filerw == RT_NULL) return RT_NULL;
  137. /* get image engine */
  138. engine = rtgui_image_get_engine_by_filename(filename);
  139. if (engine == RT_NULL)
  140. {
  141. /* close filerw context */
  142. rtgui_filerw_close(filerw);
  143. return RT_NULL;
  144. }
  145. if (engine->image_check(filerw) == RT_TRUE)
  146. {
  147. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  148. if (image == RT_NULL)
  149. {
  150. /* close filerw context */
  151. rtgui_filerw_close(filerw);
  152. return RT_NULL;
  153. }
  154. image->palette = RT_NULL;
  155. if (engine->image_load(image, filerw, load) != RT_TRUE)
  156. {
  157. /* close filerw context */
  158. rtgui_filerw_close(filerw);
  159. return RT_NULL;
  160. }
  161. /* set image engine */
  162. image->engine = engine;
  163. }
  164. else
  165. {
  166. rtgui_filerw_close(filerw);
  167. }
  168. return image;
  169. }
  170. #endif
  171. struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load)
  172. {
  173. struct rtgui_filerw* filerw;
  174. struct rtgui_image_engine* engine;
  175. struct rtgui_image* image = RT_NULL;
  176. /* create filerw context */
  177. filerw = rtgui_filerw_create_mem(data, length);
  178. if (filerw == RT_NULL) return RT_NULL;
  179. /* get image engine */
  180. engine = rtgui_image_get_engine(type);
  181. if (engine == RT_NULL)
  182. {
  183. /* close filerw context */
  184. rtgui_filerw_close(filerw);
  185. return RT_NULL;
  186. }
  187. if (engine->image_check(filerw) == RT_TRUE)
  188. {
  189. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  190. if (image == RT_NULL)
  191. {
  192. /* close filerw context */
  193. rtgui_filerw_close(filerw);
  194. return RT_NULL;
  195. }
  196. image->palette = RT_NULL;
  197. if (engine->image_load(image, filerw, load) != RT_TRUE)
  198. {
  199. /* close filerw context */
  200. rtgui_filerw_close(filerw);
  201. return RT_NULL;
  202. }
  203. /* set image engine */
  204. image->engine = engine;
  205. }
  206. else
  207. {
  208. rtgui_filerw_close(filerw);
  209. }
  210. return image;
  211. }
  212. void rtgui_image_destroy(struct rtgui_image* image)
  213. {
  214. RT_ASSERT(image != RT_NULL);
  215. image->engine->image_unload(image);
  216. if (image->palette != RT_NULL)
  217. rtgui_free(image->palette);
  218. rtgui_free(image);
  219. }
  220. /* register an image engine */
  221. void rtgui_image_register_engine(struct rtgui_image_engine* engine)
  222. {
  223. RT_ASSERT(engine!= RT_NULL);
  224. rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
  225. }
  226. void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  227. {
  228. RT_ASSERT(dc != RT_NULL);
  229. RT_ASSERT(rect != RT_NULL);
  230. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  231. if (image != RT_NULL && image->engine != RT_NULL)
  232. {
  233. /* use image engine to blit */
  234. image->engine->image_blit(image, dc, rect);
  235. }
  236. }
  237. struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors)
  238. {
  239. struct rtgui_image_palette* palette = RT_NULL;
  240. if (ncolors > 0)
  241. {
  242. palette = (struct rtgui_image_palette*) rtgui_malloc(sizeof(struct rtgui_image_palette) +
  243. sizeof(rtgui_color_t) * ncolors);
  244. if (palette != RT_NULL) palette->colors = (rtgui_color_t*)(palette + 1);
  245. }
  246. return palette;
  247. }
  248. void rtgui_image_get_rect(struct rtgui_image* image, struct rtgui_rect* rect)
  249. {
  250. RT_ASSERT(image != RT_NULL);
  251. RT_ASSERT(rect != RT_NULL);
  252. rect->x1 = 0; rect->y1 = 0;
  253. rect->x2 = image->w; rect->y2 = image->h;
  254. }