image.c 4.1 KB

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