image_hdc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <rtthread.h>
  2. #include <rtgui/dc_hw.h>
  3. #include <rtgui/image.h>
  4. #include <rtgui/rtgui_system.h>
  5. #include <rtgui/image_hdc.h>
  6. #define HDC_MAGIC_LEN 4
  7. struct rtgui_image_hdc
  8. {
  9. rt_bool_t is_loaded;
  10. /* hdc image information */
  11. rt_uint16_t byte_per_pixel;
  12. rt_uint16_t pitch;
  13. rt_size_t pixel_offset;
  14. rt_uint8_t *pixels;
  15. struct rtgui_filerw* filerw;
  16. const struct rtgui_graphic_driver* hw_driver;
  17. };
  18. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file);
  19. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load);
  20. static void rtgui_image_hdc_unload(struct rtgui_image* image);
  21. static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect);
  22. static void rtgui_image_hdcmm_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect);
  23. struct rtgui_image_engine rtgui_image_hdc_engine =
  24. {
  25. "hdc",
  26. { RT_NULL },
  27. rtgui_image_hdc_check,
  28. rtgui_image_hdc_load,
  29. rtgui_image_hdc_unload,
  30. rtgui_image_hdc_blit
  31. };
  32. const struct rtgui_image_engine rtgui_image_hdcmm_engine =
  33. {
  34. "hdcmm",
  35. {RT_NULL},
  36. RT_NULL,
  37. RT_NULL,
  38. RT_NULL,
  39. rtgui_image_hdcmm_blit
  40. };
  41. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file)
  42. {
  43. int start;
  44. rt_bool_t is_HDC;
  45. rt_uint8_t magic[4];
  46. if ( !file ) return 0;
  47. start = rtgui_filerw_tell(file);
  48. /* move to the beginning of file */
  49. rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
  50. is_HDC = RT_FALSE;
  51. if ( rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic) )
  52. {
  53. if ( magic[0] == 'H' &&
  54. magic[1] == 'D' &&
  55. magic[2] == 'C' &&
  56. magic[3] == '\0' )
  57. {
  58. is_HDC = RT_TRUE;
  59. }
  60. }
  61. rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
  62. return(is_HDC);
  63. }
  64. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load)
  65. {
  66. rt_uint32_t header[5];
  67. struct rtgui_image_hdc* hdc;
  68. hdc = (struct rtgui_image_hdc*) rtgui_malloc(sizeof(struct rtgui_image_hdc));
  69. if (hdc == RT_NULL) return RT_FALSE;
  70. hdc->hw_driver = rtgui_graphic_driver_get_default();
  71. if (hdc->hw_driver == RT_NULL) { rtgui_free(hdc); return RT_FALSE; }
  72. rtgui_filerw_read(file, (char*)&header, 1, sizeof(header));
  73. /* set image information */
  74. image->w = (rt_uint16_t)header[1]; image->h = (rt_uint16_t)header[2];
  75. image->engine = &rtgui_image_hdc_engine;
  76. image->data = hdc;
  77. hdc->filerw = file;
  78. hdc->byte_per_pixel = hdc->hw_driver->byte_per_pixel;
  79. hdc->pitch = image->w * hdc->byte_per_pixel;
  80. hdc->pixel_offset = rtgui_filerw_tell(file);
  81. if (load == RT_TRUE)
  82. {
  83. /* load all pixels */
  84. hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
  85. if (hdc->pixels == RT_NULL)
  86. {
  87. /* release data */
  88. rtgui_free(hdc);
  89. return RT_FALSE;
  90. }
  91. rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
  92. rtgui_filerw_close(hdc->filerw);
  93. hdc->filerw = RT_NULL;
  94. hdc->pixel_offset = 0;
  95. }
  96. else
  97. {
  98. hdc->pixels = RT_NULL;
  99. }
  100. return RT_TRUE;
  101. }
  102. static void rtgui_image_hdc_unload(struct rtgui_image* image)
  103. {
  104. struct rtgui_image_hdc* hdc;
  105. if (image != RT_NULL)
  106. {
  107. hdc = (struct rtgui_image_hdc*) image->data;
  108. if (hdc->pixels != RT_NULL) rtgui_free(hdc->pixels);
  109. if (hdc->filerw != RT_NULL)
  110. {
  111. rtgui_filerw_close(hdc->filerw);
  112. hdc->filerw = RT_NULL;
  113. }
  114. /* release data */
  115. rtgui_free(hdc);
  116. }
  117. }
  118. static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect)
  119. {
  120. rt_uint16_t y, w, h;
  121. struct rtgui_image_hdc* hdc;
  122. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
  123. /* this dc is not visible */
  124. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  125. hdc = (struct rtgui_image_hdc*) image->data;
  126. RT_ASSERT(hdc != RT_NULL);
  127. if ((dc->type != RTGUI_DC_HW) && (dc->type != RTGUI_DC_CLIENT)) return;
  128. /* the minimum rect */
  129. if (image->w < rtgui_rect_width(*dst_rect)) w = image->w;
  130. else w = rtgui_rect_width(*dst_rect);
  131. if (image->h < rtgui_rect_height(*dst_rect)) h = image->h;
  132. else h = rtgui_rect_height(*dst_rect);
  133. if (hdc->pixels != RT_NULL)
  134. {
  135. rt_uint8_t* ptr;
  136. /* get pixel pointer */
  137. ptr = hdc->pixels;
  138. for (y = 0; y < h; y ++)
  139. {
  140. rtgui_dc_client_draw_raw_hline(dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y);
  141. ptr += hdc->pitch;
  142. }
  143. }
  144. else
  145. {
  146. rt_uint8_t* ptr;
  147. ptr = rtgui_malloc(hdc->pitch);
  148. if (ptr == RT_NULL) return; /* no memory */
  149. /* seek to the begin of pixel data */
  150. rtgui_filerw_seek(hdc->filerw, hdc->pixel_offset, RTGUI_FILE_SEEK_SET);
  151. for (y = 0; y < h; y ++)
  152. {
  153. /* read pixel data */
  154. if (rtgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch)
  155. break; /* read data failed */
  156. rtgui_dc_client_draw_raw_hline(dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y);
  157. }
  158. rtgui_free(ptr);
  159. }
  160. }
  161. static void rtgui_image_hdcmm_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect)
  162. {
  163. rt_uint8_t* ptr;
  164. rt_uint16_t y, w, h;
  165. struct rtgui_image_hdcmm* hdc;
  166. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
  167. /* this dc is not visible */
  168. if (rtgui_dc_get_visible(dc) != RT_TRUE || ((dc->type != RTGUI_DC_HW) && (dc->type != RTGUI_DC_CLIENT))) return;
  169. hdc = (struct rtgui_image_hdcmm*) image;
  170. RT_ASSERT(hdc != RT_NULL);
  171. /* the minimum rect */
  172. if (image->w < rtgui_rect_width(*dst_rect)) w = image->w;
  173. else w = rtgui_rect_width(*dst_rect);
  174. if (image->h < rtgui_rect_height(*dst_rect)) h = image->h;
  175. else h = rtgui_rect_height(*dst_rect);
  176. /* get pixel pointer */
  177. ptr = hdc->pixels;
  178. for (y = 0; y < h; y ++)
  179. {
  180. rtgui_dc_client_draw_raw_hline(dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y);
  181. ptr += hdc->pitch;
  182. }
  183. }
  184. void rtgui_image_hdc_init()
  185. {
  186. /* register hdc on image system */
  187. rtgui_image_register_engine(&rtgui_image_hdc_engine);
  188. }