image_hdc.c 5.8 KB

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