image.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * File : image.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. * 2012-01-24 onelife add TJpgDec (Tiny JPEG Decompressor) support
  24. * 2012-08-29 amsl add Image zoom interface.
  25. */
  26. #include <rtthread.h>
  27. #include <rtgui/image.h>
  28. #include <rtgui/image_hdc.h>
  29. #include <rtgui/rtgui_system.h>
  30. #include <rtgui/image_container.h>
  31. #include <string.h>
  32. #ifdef _WIN32
  33. #define strncasecmp strnicmp
  34. #endif
  35. #ifdef RTGUI_IMAGE_XPM
  36. extern void rtgui_image_xpm_init(void);
  37. #endif
  38. #ifdef RTGUI_IMAGE_BMP
  39. #include <rtgui/image_bmp.h>
  40. #endif
  41. #if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
  42. extern void rtgui_image_jpeg_init(void);
  43. #endif
  44. #if defined(RTGUI_IMAGE_PNG) || defined(RTGUI_IMAGE_LODEPNG)
  45. extern void rtgui_image_png_init(void);
  46. #endif
  47. static rtgui_list_t _rtgui_system_image_list = {RT_NULL};
  48. /* initialize rtgui image system */
  49. void rtgui_system_image_init(void)
  50. {
  51. /* always support HDC image */
  52. rtgui_image_hdc_init();
  53. #ifdef RTGUI_IMAGE_XPM
  54. rtgui_image_xpm_init();
  55. #endif
  56. #ifdef RTGUI_IMAGE_BMP
  57. rtgui_image_bmp_init();
  58. #endif
  59. #if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
  60. rtgui_image_jpeg_init();
  61. #endif
  62. #if defined(RTGUI_IMAGE_PNG) || defined(RTGUI_IMAGE_LODEPNG)
  63. rtgui_image_png_init();
  64. #endif
  65. #ifdef RTGUI_IMAGE_CONTAINER
  66. /* initialize image container */
  67. rtgui_system_image_container_init();
  68. #endif
  69. }
  70. static struct rtgui_image_engine *rtgui_image_get_engine(const char *type)
  71. {
  72. struct rtgui_list_node *node;
  73. struct rtgui_image_engine *engine;
  74. rtgui_list_foreach(node, &_rtgui_system_image_list)
  75. {
  76. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  77. if (strncasecmp(engine->name, type, strlen(engine->name)) == 0)
  78. return engine;
  79. }
  80. return RT_NULL;
  81. }
  82. #if defined(RTGUI_USING_DFS_FILERW)
  83. struct rtgui_image_engine *rtgui_image_get_engine_by_filename(const char *fn)
  84. {
  85. struct rtgui_list_node *node;
  86. struct rtgui_image_engine *engine;
  87. const char *ext;
  88. ext = fn + rt_strlen(fn);
  89. while (ext != fn)
  90. {
  91. if (*ext == '.')
  92. {
  93. ext ++;
  94. break;
  95. }
  96. ext --;
  97. }
  98. if (ext == fn) return RT_NULL; /* no ext */
  99. rtgui_list_foreach(node, &_rtgui_system_image_list)
  100. {
  101. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  102. if (strncasecmp(engine->name, ext, strlen(engine->name)) == 0)
  103. return engine;
  104. }
  105. return RT_NULL;
  106. }
  107. RTM_EXPORT(rtgui_image_get_engine_by_filename);
  108. struct rtgui_image *rtgui_image_create_from_file(const char *type, const char *filename, rt_bool_t load)
  109. {
  110. struct rtgui_filerw *filerw;
  111. struct rtgui_image_engine *engine;
  112. struct rtgui_image *image = RT_NULL;
  113. /* create filerw context */
  114. filerw = rtgui_filerw_create_file(filename, "rb");
  115. if (filerw == RT_NULL) return RT_NULL;
  116. /* get image engine */
  117. engine = rtgui_image_get_engine(type);
  118. if (engine == RT_NULL)
  119. {
  120. /* close filerw context */
  121. rtgui_filerw_close(filerw);
  122. return RT_NULL;
  123. }
  124. if (engine->image_check(filerw) == RT_TRUE)
  125. {
  126. image = (struct rtgui_image *) rtgui_malloc(sizeof(struct rtgui_image));
  127. if (image == RT_NULL)
  128. {
  129. /* close filerw context */
  130. rtgui_filerw_close(filerw);
  131. return RT_NULL;
  132. }
  133. image->palette = RT_NULL;
  134. if (engine->image_load(image, filerw, load) != RT_TRUE)
  135. {
  136. /* close filerw context */
  137. rtgui_filerw_close(filerw);
  138. return RT_NULL;
  139. }
  140. /* set image engine */
  141. image->engine = engine;
  142. }
  143. else
  144. {
  145. rtgui_filerw_close(filerw);
  146. }
  147. return image;
  148. }
  149. RTM_EXPORT(rtgui_image_create_from_file);
  150. struct rtgui_image *rtgui_image_create(const char *filename, rt_bool_t load)
  151. {
  152. struct rtgui_filerw *filerw;
  153. struct rtgui_image_engine *engine;
  154. struct rtgui_image *image = RT_NULL;
  155. /* create filerw context */
  156. filerw = rtgui_filerw_create_file(filename, "rb");
  157. if (filerw == RT_NULL)
  158. {
  159. rt_kprintf("create filerw failed!\n");
  160. return RT_NULL;
  161. }
  162. /* get image engine */
  163. engine = rtgui_image_get_engine_by_filename(filename);
  164. if (engine == RT_NULL)
  165. {
  166. rt_kprintf("no engine for file: %s\n", filename);
  167. /* close filerw context */
  168. rtgui_filerw_close(filerw);
  169. return RT_NULL;
  170. }
  171. if (engine->image_check(filerw) == RT_TRUE)
  172. {
  173. image = (struct rtgui_image *) rtgui_malloc(sizeof(struct rtgui_image));
  174. if (image == RT_NULL)
  175. {
  176. rt_kprintf("out of memory\n");
  177. /* close filerw context */
  178. rtgui_filerw_close(filerw);
  179. return RT_NULL;
  180. }
  181. image->palette = RT_NULL;
  182. if (engine->image_load(image, filerw, load) != RT_TRUE)
  183. {
  184. rt_kprintf("load image:%s failed!\n", filename);
  185. /* close filerw context */
  186. rtgui_filerw_close(filerw);
  187. return RT_NULL;
  188. }
  189. /* set image engine */
  190. image->engine = engine;
  191. }
  192. else
  193. {
  194. rt_kprintf("image:%s check failed!\n", filename);
  195. rtgui_filerw_close(filerw);
  196. }
  197. return image;
  198. }
  199. RTM_EXPORT(rtgui_image_create);
  200. #endif
  201. struct rtgui_image *rtgui_image_create_from_mem(const char *type, const rt_uint8_t *data, rt_size_t length, rt_bool_t load)
  202. {
  203. struct rtgui_filerw *filerw;
  204. struct rtgui_image_engine *engine;
  205. struct rtgui_image *image = RT_NULL;
  206. /* create filerw context */
  207. filerw = rtgui_filerw_create_mem(data, length);
  208. if (filerw == RT_NULL) return RT_NULL;
  209. /* get image engine */
  210. engine = rtgui_image_get_engine(type);
  211. if (engine == RT_NULL)
  212. {
  213. /* close filerw context */
  214. rtgui_filerw_close(filerw);
  215. return RT_NULL;
  216. }
  217. if (engine->image_check(filerw) == RT_TRUE)
  218. {
  219. image = (struct rtgui_image *) rtgui_malloc(sizeof(struct rtgui_image));
  220. if (image == RT_NULL)
  221. {
  222. /* close filerw context */
  223. rtgui_filerw_close(filerw);
  224. return RT_NULL;
  225. }
  226. image->palette = RT_NULL;
  227. if (engine->image_load(image, filerw, load) != RT_TRUE)
  228. {
  229. /* close filerw context */
  230. rtgui_filerw_close(filerw);
  231. return RT_NULL;
  232. }
  233. /* set image engine */
  234. image->engine = engine;
  235. }
  236. else
  237. {
  238. rtgui_filerw_close(filerw);
  239. }
  240. return image;
  241. }
  242. RTM_EXPORT(rtgui_image_create_from_mem);
  243. void rtgui_image_destroy(struct rtgui_image *image)
  244. {
  245. RT_ASSERT(image != RT_NULL);
  246. image->engine->image_unload(image);
  247. if (image->palette != RT_NULL)
  248. rtgui_free(image->palette);
  249. rtgui_free(image);
  250. }
  251. RTM_EXPORT(rtgui_image_destroy);
  252. /* register an image engine */
  253. void rtgui_image_register_engine(struct rtgui_image_engine *engine)
  254. {
  255. RT_ASSERT(engine != RT_NULL);
  256. rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
  257. }
  258. RTM_EXPORT(rtgui_image_register_engine);
  259. void rtgui_image_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  260. {
  261. struct rtgui_rect r;
  262. RT_ASSERT(dc != RT_NULL);
  263. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  264. rtgui_dc_get_rect(dc, &r);
  265. /* use rect of DC */
  266. if (rect == RT_NULL)
  267. {
  268. rect = &r;
  269. }
  270. else
  271. {
  272. /* Don't modify x1, y1, they are handled in engine->image_blit. */
  273. if (rect->x1 > r.x2)
  274. return;
  275. if (rect->y1 > r.y2)
  276. return;
  277. if (rect->x2 > r.x2)
  278. rect->x2 = r.x2;
  279. if (rect->y2 > r.y2)
  280. rect->y2 = r.y2;
  281. }
  282. if (image != RT_NULL && image->engine != RT_NULL)
  283. {
  284. /* use image engine to blit */
  285. image->engine->image_blit(image, dc, rect);
  286. }
  287. }
  288. RTM_EXPORT(rtgui_image_blit);
  289. struct rtgui_image_palette *rtgui_image_palette_create(rt_uint32_t ncolors)
  290. {
  291. struct rtgui_image_palette *palette = RT_NULL;
  292. if (ncolors > 0)
  293. {
  294. palette = (struct rtgui_image_palette *) rtgui_malloc(sizeof(struct rtgui_image_palette) +
  295. sizeof(rtgui_color_t) * ncolors);
  296. if (palette != RT_NULL) palette->colors = (rtgui_color_t *)(palette + 1);
  297. }
  298. return palette;
  299. }
  300. RTM_EXPORT(rtgui_image_palette_create);
  301. void rtgui_image_get_rect(struct rtgui_image *image, struct rtgui_rect *rect)
  302. {
  303. RT_ASSERT(image != RT_NULL);
  304. RT_ASSERT(rect != RT_NULL);
  305. rect->x1 = 0;
  306. rect->y1 = 0;
  307. rect->x2 = image->w;
  308. rect->y2 = image->h;
  309. }
  310. RTM_EXPORT(rtgui_image_get_rect);