image.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/rtgui_system.h>
  18. #include <string.h>
  19. #ifdef RTGUI_IMAGE_BMP
  20. #include <rtgui/image_bmp.h>
  21. #endif
  22. #ifdef RTGUI_IMAGE_JPEG
  23. #include <rtgui/image_jpeg.h>
  24. #endif
  25. #ifdef RTGUI_IMAGE_PNG
  26. #include <rtgui/image_png.h>
  27. #endif
  28. static rtgui_list_t _rtgui_system_image_list = {RT_NULL};
  29. /* init rtgui image system */
  30. void rtgui_system_image_init(void)
  31. {
  32. /* always support XPM image */
  33. rtgui_image_xpm_init();
  34. #ifdef RTGUI_IMAGE_BMP
  35. rtgui_image_bmp_init();
  36. #endif
  37. #ifdef RTGUI_IMAGE_JPEG
  38. rtgui_image_jpeg_init();
  39. #endif
  40. #ifdef RTGUI_IMAGE_PNG
  41. rtgui_image_png_init();
  42. #endif
  43. }
  44. static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
  45. {
  46. struct rtgui_list_node *node;
  47. struct rtgui_image_engine *engine;
  48. rtgui_list_foreach(node, &_rtgui_system_image_list)
  49. {
  50. engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
  51. if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
  52. return engine;
  53. }
  54. return RT_NULL;
  55. }
  56. struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load)
  57. {
  58. struct rtgui_filerw* filerw;
  59. struct rtgui_image_engine* engine;
  60. struct rtgui_image* image = RT_NULL;
  61. /* create filerw context */
  62. filerw = rtgui_filerw_create_file(filename, "rb");
  63. if (filerw == RT_NULL) return RT_NULL;
  64. /* get image engine */
  65. engine = rtgui_image_get_engine(type);
  66. if (engine == RT_NULL)
  67. {
  68. /* close filerw context */
  69. rtgui_filerw_close(filerw);
  70. return RT_NULL;
  71. }
  72. if (engine->image_check(filerw) == RT_TRUE)
  73. {
  74. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  75. if (image == RT_NULL)
  76. {
  77. /* close filerw context */
  78. rtgui_filerw_close(filerw);
  79. return RT_NULL;
  80. }
  81. if (engine->image_load(image, filerw, load) != RT_TRUE)
  82. {
  83. /* close filerw context */
  84. rtgui_filerw_close(filerw);
  85. return RT_NULL;
  86. }
  87. /* set image engine */
  88. image->engine = engine;
  89. }
  90. else
  91. {
  92. rtgui_filerw_close(filerw);
  93. }
  94. return image;
  95. }
  96. struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length)
  97. {
  98. struct rtgui_filerw* filerw;
  99. struct rtgui_image_engine* engine;
  100. struct rtgui_image* image = RT_NULL;
  101. /* create filerw context */
  102. filerw = rtgui_filerw_create_mem((rt_uint8_t*)data, length);
  103. if (filerw == RT_NULL) return RT_NULL;
  104. /* get image engine */
  105. engine = rtgui_image_get_engine(type);
  106. if (engine == RT_NULL)
  107. {
  108. /* close filerw context */
  109. rtgui_filerw_close(filerw);
  110. return RT_NULL;
  111. }
  112. if (engine->image_check(filerw) == RT_TRUE)
  113. {
  114. image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
  115. if (image == RT_NULL)
  116. {
  117. /* close filerw context */
  118. rtgui_filerw_close(filerw);
  119. return RT_NULL;
  120. }
  121. if (engine->image_load(image, filerw, RT_TRUE) != RT_TRUE)
  122. {
  123. /* close filerw context */
  124. rtgui_filerw_close(filerw);
  125. return RT_NULL;
  126. }
  127. /* set image engine */
  128. image->engine = engine;
  129. }
  130. else
  131. {
  132. rtgui_filerw_close(filerw);
  133. }
  134. return image;
  135. }
  136. void rtgui_image_destroy(struct rtgui_image* image)
  137. {
  138. RT_ASSERT(image != RT_NULL);
  139. image->engine->image_unload(image);
  140. rtgui_free(image);
  141. }
  142. /* register an image engine */
  143. void rtgui_image_register_engine(struct rtgui_image_engine* engine)
  144. {
  145. RT_ASSERT(engine!= RT_NULL);
  146. rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
  147. }
  148. void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  149. {
  150. RT_ASSERT(dc != RT_NULL);
  151. RT_ASSERT(rect != RT_NULL);
  152. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  153. if (image != RT_NULL && image->engine != RT_NULL)
  154. {
  155. /* use image engine to blit */
  156. image->engine->image_blit(image, dc, rect);
  157. }
  158. }