image.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(const char* type)
  47. {
  48. struct rtgui_list_node *node;
  49. struct rtgui_image_engine *engine;
  50. rtgui_list_foreach(node, &_rtgui_system_image_list)
  51. {
  52. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  53. if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
  54. return engine;
  55. }
  56. return RT_NULL;
  57. }
  58. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  59. struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load)
  60. {
  61. struct rtgui_filerw* filerw;
  62. struct rtgui_image_engine* engine;
  63. struct rtgui_image* image = RT_NULL;
  64. /* create filerw context */
  65. filerw = rtgui_filerw_create_file(filename, "rb");
  66. if (filerw == RT_NULL) return RT_NULL;
  67. /* get image engine */
  68. engine = rtgui_image_get_engine(type);
  69. if (engine == RT_NULL)
  70. {
  71. /* close filerw context */
  72. rtgui_filerw_close(filerw);
  73. return RT_NULL;
  74. }
  75. if (engine->image_check(filerw) == RT_TRUE)
  76. {
  77. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  78. if (image == RT_NULL)
  79. {
  80. /* close filerw context */
  81. rtgui_filerw_close(filerw);
  82. return RT_NULL;
  83. }
  84. if (engine->image_load(image, filerw, load) != RT_TRUE)
  85. {
  86. /* close filerw context */
  87. rtgui_filerw_close(filerw);
  88. return RT_NULL;
  89. }
  90. /* set image engine */
  91. image->engine = engine;
  92. }
  93. else
  94. {
  95. rtgui_filerw_close(filerw);
  96. }
  97. return image;
  98. }
  99. #endif
  100. struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load)
  101. {
  102. struct rtgui_filerw* filerw;
  103. struct rtgui_image_engine* engine;
  104. struct rtgui_image* image = RT_NULL;
  105. /* create filerw context */
  106. filerw = rtgui_filerw_create_mem(data, length);
  107. if (filerw == RT_NULL) return RT_NULL;
  108. /* get image engine */
  109. engine = rtgui_image_get_engine(type);
  110. if (engine == RT_NULL)
  111. {
  112. /* close filerw context */
  113. rtgui_filerw_close(filerw);
  114. return RT_NULL;
  115. }
  116. if (engine->image_check(filerw) == RT_TRUE)
  117. {
  118. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  119. if (image == RT_NULL)
  120. {
  121. /* close filerw context */
  122. rtgui_filerw_close(filerw);
  123. return RT_NULL;
  124. }
  125. if (engine->image_load(image, filerw, load) != RT_TRUE)
  126. {
  127. /* close filerw context */
  128. rtgui_filerw_close(filerw);
  129. return RT_NULL;
  130. }
  131. /* set image engine */
  132. image->engine = engine;
  133. }
  134. else
  135. {
  136. rtgui_filerw_close(filerw);
  137. }
  138. return image;
  139. }
  140. void rtgui_image_destroy(struct rtgui_image* image)
  141. {
  142. RT_ASSERT(image != RT_NULL);
  143. image->engine->image_unload(image);
  144. rtgui_free(image);
  145. }
  146. /* register an image engine */
  147. void rtgui_image_register_engine(struct rtgui_image_engine* engine)
  148. {
  149. RT_ASSERT(engine!= RT_NULL);
  150. rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
  151. }
  152. void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  153. {
  154. RT_ASSERT(dc != RT_NULL);
  155. RT_ASSERT(rect != RT_NULL);
  156. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  157. if (image != RT_NULL && image->engine != RT_NULL)
  158. {
  159. /* use image engine to blit */
  160. image->engine->image_blit(image, dc, rect);
  161. }
  162. }