image_hdc.c 6.3 KB

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