image.c 4.1 KB

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