image_hdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * File : image_hdc.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-09-15 Bernard first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtgui/dc.h>
  26. #include <rtgui/image.h>
  27. #include <rtgui/rtgui_system.h>
  28. #include <rtgui/image_hdc.h>
  29. #include <rtgui/blit.h>
  30. #define HDC_MAGIC_LEN 4
  31. #ifdef PKG_USING_FASTLZ
  32. extern int fastlz_decompress(const void *input, int length, void *output, int maxout);
  33. #endif
  34. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file);
  35. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  36. static void rtgui_image_hdc_unload(struct rtgui_image *image);
  37. static void rtgui_image_hdc_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  38. static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect);
  39. struct rtgui_image_engine rtgui_image_hdc_engine =
  40. {
  41. "hdc",
  42. { RT_NULL },
  43. rtgui_image_hdc_check,
  44. rtgui_image_hdc_load,
  45. rtgui_image_hdc_unload,
  46. rtgui_image_hdc_blit,
  47. };
  48. const struct rtgui_image_engine rtgui_image_hdcmm_engine =
  49. {
  50. "hdcmm",
  51. {RT_NULL},
  52. RT_NULL,
  53. RT_NULL,
  54. RT_NULL,
  55. rtgui_image_hdcmm_blit,
  56. };
  57. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file)
  58. {
  59. int start;
  60. rt_bool_t is_HDC;
  61. rt_uint8_t magic[4];
  62. if (!file) return 0;
  63. start = rtgui_filerw_tell(file);
  64. /* move to the beginning of file */
  65. rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
  66. is_HDC = RT_FALSE;
  67. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  68. {
  69. if (magic[0] == 'H' &&
  70. magic[1] == 'D' &&
  71. magic[2] == 'C' &&
  72. magic[3] == '\0')
  73. {
  74. is_HDC = RT_TRUE;
  75. }
  76. }
  77. rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
  78. return (is_HDC);
  79. }
  80. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  81. {
  82. rt_uint32_t header[5];
  83. struct rtgui_image_hdc *hdc;
  84. hdc = (struct rtgui_image_hdc *) rtgui_malloc(sizeof(struct rtgui_image_hdc));
  85. if (hdc == RT_NULL) return RT_FALSE;
  86. rtgui_filerw_read(file, (char *)&header, 1, sizeof(header));
  87. /* set image information */
  88. image->w = (rt_uint16_t)header[1];
  89. image->h = (rt_uint16_t)header[2];
  90. image->engine = &rtgui_image_hdc_engine;
  91. image->data = hdc;
  92. hdc->pixel_format = header[3];
  93. hdc->filerw = file;
  94. if (header[3] == 0)
  95. {
  96. /* 0.x version */
  97. hdc->pixel_format = rtgui_graphic_driver_get_default()->pixel_format;
  98. }
  99. else if (header[3] == 1)
  100. {
  101. /* 1.x version */
  102. hdc->pixel_format = header[4];
  103. }
  104. else if (header[3] == 2)
  105. {
  106. /* 2.x version */
  107. hdc->pixel_format = header[4];
  108. }
  109. hdc->byte_per_pixel = rtgui_color_get_bpp(hdc->pixel_format);
  110. hdc->pitch = image->w * hdc->byte_per_pixel;
  111. hdc->pixel_offset = rtgui_filerw_tell(file);
  112. if (load == RT_TRUE)
  113. {
  114. if (header[3] == 2)
  115. {
  116. #ifdef PKG_USING_FASTLZ
  117. /* TODO: add HDC with fastlz compressed */
  118. int data_length, dec_length;
  119. rt_uint8_t *data;
  120. data_length = rtgui_filerw_seek(hdc->filerw, 0, SEEK_END) - sizeof(header);
  121. rtgui_filerw_seek(hdc->filerw, sizeof(header), SEEK_SET);
  122. data = (rt_uint8_t *)rtgui_malloc(data_length);
  123. if (data == RT_NULL)
  124. {
  125. /* release data */
  126. rtgui_free(hdc);
  127. return RT_FALSE;
  128. }
  129. if (rtgui_filerw_read(hdc->filerw, data, 1, data_length) != data_length)
  130. {
  131. rtgui_filerw_close(hdc->filerw);
  132. /* release data */
  133. rtgui_free(hdc);
  134. return RT_FALSE;
  135. }
  136. rtgui_filerw_close(hdc->filerw);
  137. hdc->filerw = RT_NULL;
  138. hdc->pixels = (rt_uint8_t *)rtgui_malloc(image->h * hdc->pitch);
  139. if (hdc->pixels == RT_NULL)
  140. {
  141. /* release data */
  142. rtgui_free(hdc);
  143. rtgui_free(data);
  144. return RT_FALSE;
  145. }
  146. dec_length = fastlz_decompress(data, data_length, hdc->pixels, image->h * hdc->pitch);
  147. if (dec_length != image->h * hdc->pitch)
  148. {
  149. /* release data */
  150. rtgui_free(hdc->pixels);
  151. rtgui_free(hdc);
  152. rtgui_free(data);
  153. return RT_FALSE;
  154. }
  155. hdc->pixel_offset = 0;
  156. rtgui_free(data);
  157. #else
  158. hdc->pixels = RT_NULL;
  159. return RT_FALSE;
  160. #endif
  161. }
  162. else
  163. {
  164. /* load all pixels */
  165. hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
  166. if (hdc->pixels == RT_NULL)
  167. {
  168. /* release data */
  169. rtgui_free(hdc);
  170. return RT_FALSE;
  171. }
  172. rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
  173. rtgui_filerw_close(hdc->filerw);
  174. hdc->filerw = RT_NULL;
  175. hdc->pixel_offset = 0;
  176. }
  177. }
  178. else
  179. {
  180. hdc->pixels = RT_NULL;
  181. }
  182. return RT_TRUE;
  183. }
  184. static void rtgui_image_hdc_unload(struct rtgui_image *image)
  185. {
  186. struct rtgui_image_hdc *hdc;
  187. if (image != RT_NULL)
  188. {
  189. hdc = (struct rtgui_image_hdc *) image->data;
  190. if (hdc->pixels != RT_NULL)
  191. rtgui_free(hdc->pixels);
  192. if (hdc->filerw != RT_NULL)
  193. rtgui_filerw_close(hdc->filerw);
  194. /* release data */
  195. rtgui_free(hdc);
  196. }
  197. }
  198. static void rtgui_image_hdc_blit(struct rtgui_image *image,
  199. struct rtgui_dc *dc,
  200. struct rtgui_rect *dst_rect)
  201. {
  202. rt_int16_t y, w, h, xoff, yoff;
  203. struct rtgui_image_hdc *hdc;
  204. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
  205. /* this dc is not visible */
  206. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  207. return;
  208. if (!image)
  209. return;
  210. hdc = (struct rtgui_image_hdc *) image->data;
  211. RT_ASSERT(hdc != RT_NULL);
  212. xoff = 0;
  213. if (dst_rect->x1 < 0)
  214. {
  215. xoff = -dst_rect->x1;
  216. dst_rect->x1 = 0;
  217. }
  218. yoff = 0;
  219. if (dst_rect->y1 < 0)
  220. {
  221. yoff = -dst_rect->y1;
  222. dst_rect->y1 = 0;
  223. }
  224. if (dst_rect->x2 <= 0 || dst_rect->y2 <= 0)
  225. return;
  226. if (xoff >= image->w || yoff >= image->h)
  227. return;
  228. /* the minimum rect */
  229. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  230. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  231. if (w == 0 || h == 0)
  232. return;
  233. if (hdc->pixels != RT_NULL)
  234. {
  235. struct rtgui_image_info info;
  236. struct rtgui_rect dest = *dst_rect;
  237. info.a = 255;
  238. info.pixels = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  239. info.src_fmt = hdc->pixel_format;
  240. info.src_pitch = hdc->pitch;
  241. dest.x2 = dst_rect->x1 + w;
  242. dest.y2 = dst_rect->y1 + h;
  243. rtgui_image_info_blit(&info, dc, &dest);
  244. }
  245. else
  246. {
  247. rt_uint8_t *ptr;
  248. ptr = rtgui_malloc(hdc->byte_per_pixel * w);
  249. if (ptr == RT_NULL)
  250. return; /* no memory */
  251. /* seek to the begin of pixel data */
  252. rtgui_filerw_seek(hdc->filerw,
  253. hdc->pixel_offset + hdc->pitch * yoff + hdc->byte_per_pixel * xoff,
  254. RTGUI_FILE_SEEK_SET);
  255. for (y = 0; y < h; y ++)
  256. {
  257. /* read pixel data */
  258. if (rtgui_filerw_read(hdc->filerw, ptr, 1,
  259. hdc->byte_per_pixel * w) != hdc->byte_per_pixel * w)
  260. break; /* read data failed */
  261. dc->engine->blit_line(dc,
  262. dst_rect->x1,
  263. dst_rect->x1 + w,
  264. dst_rect->y1 + y,
  265. ptr);
  266. rtgui_filerw_seek(hdc->filerw, hdc->byte_per_pixel * xoff, RTGUI_FILE_SEEK_CUR);
  267. }
  268. rtgui_free(ptr);
  269. }
  270. }
  271. static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  272. {
  273. rt_uint8_t *ptr;
  274. rt_uint16_t y, w, h, xoff, yoff;
  275. struct rtgui_image_hdcmm *hdc;
  276. RT_ASSERT(image != RT_NULL && dc != RT_NULL && dst_rect != RT_NULL);
  277. /* this dc is not visible */
  278. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  279. return;
  280. hdc = (struct rtgui_image_hdcmm *) image;
  281. if (!hdc->pixels)
  282. return;
  283. xoff = 0;
  284. if (dst_rect->x1 < 0)
  285. {
  286. xoff = -dst_rect->x1;
  287. dst_rect->x1 = 0;
  288. }
  289. yoff = 0;
  290. if (dst_rect->y1 < 0)
  291. {
  292. yoff = -dst_rect->y1;
  293. dst_rect->y1 = 0;
  294. }
  295. if (xoff >= image->w || yoff >= image->h)
  296. return;
  297. /* the minimum rect */
  298. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  299. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  300. /* get pixel pointer */
  301. ptr = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  302. for (y = 0; y < h; y++)
  303. {
  304. dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
  305. ptr += hdc->pitch;
  306. }
  307. }
  308. void rtgui_image_hdc_init()
  309. {
  310. /* register hdc on image system */
  311. rtgui_image_register_engine(&rtgui_image_hdc_engine);
  312. }