1
0

image_hdc.c 4.6 KB

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